Merge pull request #69 from wtaverni/master
[osm/vim-emu.git] / src / emuvim / dcemulator / node.py
1 """
2 Distributed Cloud Emulator (dcemulator)
3 (c) 2015 by Manuel Peuster <manuel.peuster@upb.de>
4 """
5 from mininet.node import Docker
6 from mininet.link import Link
7 import logging
8
9 LOG = logging.getLogger("dcemulator")
10 LOG.setLevel(logging.DEBUG)
11
12
13 DCDPID_BASE = 1000 # start of switch dpid's used for data center switches
14
15
16 class EmulatorCompute(Docker):
17 """
18 Emulator specific compute node class.
19 Inherits from Dockernet's Docker host class.
20 Represents a single container connected to a (logical)
21 data center.
22 We can add emulator specific helper functions to it.
23 """
24
25 def __init__(
26 self, name, dimage, **kwargs):
27 self.datacenter = kwargs.get("datacenter") # pointer to current DC
28 self.flavor_name = kwargs.get("flavor_name")
29 LOG.debug("Starting compute instance %r in data center %r" % (name, str(self.datacenter)))
30 # call original Docker.__init__
31 Docker.__init__(self, name, dimage, **kwargs)
32
33 def getNetworkStatus(self):
34 """
35 Helper method to receive information about the virtual networks
36 this compute instance is connected to.
37 """
38 # format list of tuples (name, Ip, MAC, isUp, status)
39 return [(str(i), i.IP(), i.MAC(), i.isUp(), i.status())
40 for i in self.intfList()]
41
42 def getStatus(self):
43 """
44 Helper method to receive information about this compute instance.
45 """
46 status = {}
47 status["name"] = self.name
48 status["network"] = self.getNetworkStatus()
49 status["image"] = self.dimage
50 status["cpu_quota"] = self.cpu_quota
51 status["cpu_period"] = self.cpu_period
52 status["cpu_shares"] = self.cpu_shares
53 status["cpuset"] = self.cpuset
54 status["mem_limit"] = self.mem_limit
55 status["memswap_limit"] = self.memswap_limit
56 status["state"] = self.dcli.inspect_container(self.dc)["State"]
57 status["id"] = self.dcli.inspect_container(self.dc)["Id"]
58 status["datacenter"] = (None if self.datacenter is None
59 else self.datacenter.label)
60 return status
61
62
63 class Datacenter(object):
64 """
65 Represents a logical data center to which compute resources
66 (Docker containers) can be added at runtime.
67
68 Will also implement resource bookkeeping in later versions.
69 """
70
71 DC_COUNTER = 1
72
73 def __init__(self, label, metadata={}):
74 self.net = None # DCNetwork to which we belong
75 # each node (DC) has a short internal name used by Mininet
76 # this is caused by Mininets naming limitations for swtiches etc.
77 self.name = "dc%d" % Datacenter.DC_COUNTER
78 Datacenter.DC_COUNTER += 1
79 # use this for user defined names that can be longer than self.name
80 self.label = label
81 # dict to store arbitrary metadata (e.g. latitude and longitude)
82 self.metadata = metadata
83 # first prototype assumes one "bigswitch" per DC
84 self.switch = None
85 # keep track of running containers
86 self.containers = {}
87 # pointer to assigned resource model
88 self._resource_model = None
89
90 def __repr__(self):
91 return self.label
92
93 def _get_next_dc_dpid(self):
94 global DCDPID_BASE
95 DCDPID_BASE += 1
96 return DCDPID_BASE
97
98 def create(self):
99 """
100 Each data center is represented by a single switch to which
101 compute resources can be connected at run time.
102
103 TODO: This will be changed in the future to support multiple networks
104 per data center
105 """
106 self.switch = self.net.addSwitch(
107 "%s.s1" % self.name, dpid=hex(self._get_next_dc_dpid())[2:])
108 LOG.debug("created data center switch: %s" % str(self.switch))
109
110 def start(self):
111 pass
112
113 def startCompute(self, name, image=None, command=None, network=None, flavor_name="tiny"):
114 """
115 Create a new container as compute resource and connect it to this
116 data center.
117 :param name: name (string)
118 :param image: image name (string)
119 :param command: command (string)
120 :param network: networks list({"ip": "10.0.0.254/8"}, {"ip": "11.0.0.254/24"})
121 :param flavor_name: name of the flavor for this compute container
122 :return:
123 """
124 assert name is not None
125 # no duplications
126 if name in [c.name for c in self.net.getAllContainers()]:
127 raise Exception("Container with name %s already exists." % name)
128 # set default parameter
129 if image is None:
130 image = "ubuntu"
131 if network is None:
132 network = {} # {"ip": "10.0.0.254/8"}
133 if isinstance(network, dict):
134 network = [network] # if we have only one network, put it in a list
135 if isinstance(network, list):
136 if len(network) < 1:
137 network.append({})
138
139 # allocate in resource resource model and compute resource limits for new container
140 cpu_limit = mem_limit = disk_limit = -1
141 cpu_period = cpu_quota = None
142 if self._resource_model is not None:
143 # call allocate in resource model to calculate resource limit for this container
144 (cpu_limit, mem_limit, disk_limit) = alloc = self._resource_model.allocate(name, flavor_name)
145 LOG.debug("Allocation result: %r" % str(alloc))
146 # check if we have a cpu_limit given by the used resource model
147 if cpu_limit > 0:
148 # calculate cpu period and quota for CFS
149 # (see: https://www.kernel.org/doc/Documentation/scheduler/sched-bwc.txt)
150 # TODO consider multi core machines etc! non trivial!
151 # Attention minimum cpu_quota is 1ms (micro)
152 cpu_period = 100000 # lets consider a fixed period of 100000 microseconds for now
153 cpu_quota = cpu_period * cpu_limit # calculate the fraction of cpu time for this container
154 LOG.debug(
155 "CPU limit: cpu_quota = cpu_period * cpu_limit = %f * %f = %f" % (cpu_period, cpu_limit, cpu_quota))
156 # ATTENTION >= 1000 to avoid a invalid argument system error ... no idea why
157 if cpu_quota < 1000:
158 cpu_quota = 1000
159 LOG.warning("Increased CPU quota for %r to avoid system error." % name)
160 # check if we have a mem_limit given by the used resource model
161 if mem_limit > 0:
162 LOG.debug(
163 "MEM limit: mem_limit = %f MB" % mem_limit)
164 # ATTENTION minimum mem_limit per container is 4MB
165 if mem_limit < 4:
166 mem_limit = 4
167 LOG.warning("Increased MEM limit for %r because it was less than 4.0 MB." % name)
168 # create the container
169 d = self.net.addDocker(
170 "%s" % (name),
171 dimage=image,
172 dcmd=command,
173 datacenter=self,
174 flavor_name=flavor_name,
175 cpu_period=int(cpu_period) if cpu_limit > 0 else None, # set cpu limits if needed
176 cpu_quota=int(cpu_quota) if cpu_limit > 0 else None,
177 mem_limit="%dm" % int(mem_limit) if mem_limit > 0 else None, # set mem limits if needed
178 memswap_limit="%dm" % int(mem_limit) if mem_limit > 0 else None # lets set swap to mem limit for now
179 )
180 # connect all given networks
181 for nw in network:
182 # TODO we cannot use TCLink here (see: https://github.com/mpeuster/dockernet/issues/3)
183 self.net.addLink(d, self.switch, params1=nw, cls=Link)
184 # do bookkeeping
185 self.containers[name] = d
186 return d # we might use UUIDs for naming later on
187
188 def stopCompute(self, name):
189 """
190 Stop and remove a container from this data center.
191 """
192 assert name is not None
193 if name not in self.containers:
194 raise Exception("Container with name %s not found." % name)
195 self.net.removeLink(
196 link=None, node1=self.containers[name], node2=self.switch)
197 self.net.removeDocker("%s" % (name))
198 del self.containers[name]
199 # call resource model and free resources
200 if self._resource_model is not None:
201 self._resource_model.free(name)
202 return True
203
204 def listCompute(self):
205 """
206 Return a list of all running containers assigned to this
207 data center.
208 """
209 return list(self.containers.itervalues())
210
211 def getStatus(self):
212 """
213 Return a dict with status information about this DC.
214 """
215 return {
216 "label": self.label,
217 "internalname": self.name,
218 "switch": self.switch.name,
219 "n_running_containers": len(self.containers),
220 "metadata": self.metadata
221 }
222
223 def assignResourceModel(self, rm):
224 """
225 Assign a resource model to this DC.
226 :param rm: a BaseResourceModel object
227 :return:
228 """
229 if self._resource_model is not None:
230 raise Exception("There is already an resource model assigned to this DC.")
231 self._resource_model = rm
232 self.net.rm_registrar.register(self, rm)
233 LOG.info("Assigned RM: %r to DC: %r" % (rm, self))
234