blob: 7746e9e05fcd2f400ad9a80d2cd0f4132e5e47b4 [file] [log] [blame]
peustermcbcd4c22015-12-28 11:33:42 +01001"""
2Distributed Cloud Emulator (dcemulator)
3(c) 2015 by Manuel Peuster <manuel.peuster@upb.de>
4"""
peusterm7aae6852016-01-12 14:53:18 +01005from mininet.node import Docker
peustermea8db832016-03-08 10:25:58 +01006from mininet.link import Link
peustermcbcd4c22015-12-28 11:33:42 +01007import logging
8
peusterm3444ae42016-03-16 20:46:41 +01009LOG = logging.getLogger("dcemulator")
10LOG.setLevel(logging.DEBUG)
11
peustermcbcd4c22015-12-28 11:33:42 +010012
13DCDPID_BASE = 1000 # start of switch dpid's used for data center switches
14
15
peusterm7aae6852016-01-12 14:53:18 +010016class 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):
peusterm42f08be2016-03-10 21:56:34 +010027 self.datacenter = kwargs.get("datacenter") # pointer to current DC
28 self.flavor_name = kwargs.get("flavor_name")
peusterm3444ae42016-03-16 20:46:41 +010029 LOG.debug("Starting compute instance %r in data center %r" % (name, str(self.datacenter)))
peusterm7aae6852016-01-12 14:53:18 +010030 # 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 """
peusterm056fd452016-01-12 15:32:25 +010038 # 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()]
peusterm7aae6852016-01-12 14:53:18 +010041
42 def getStatus(self):
43 """
44 Helper method to receive information about this compute instance.
45 """
peusterm056fd452016-01-12 15:32:25 +010046 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"]
peusterm2ec74e12016-01-13 11:17:53 +010058 status["datacenter"] = (None if self.datacenter is None
peusterma47db032016-02-04 14:55:29 +010059 else self.datacenter.label)
peusterm056fd452016-01-12 15:32:25 +010060 return status
peusterm7aae6852016-01-12 14:53:18 +010061
62
peustermcbcd4c22015-12-28 11:33:42 +010063class Datacenter(object):
64 """
65 Represents a logical data center to which compute resources
66 (Docker containers) can be added at runtime.
peusterme4e89d32016-01-07 09:14:54 +010067
68 Will also implement resource bookkeeping in later versions.
peustermcbcd4c22015-12-28 11:33:42 +010069 """
70
peusterma47db032016-02-04 14:55:29 +010071 DC_COUNTER = 1
72
peusterm53504942016-02-04 16:09:28 +010073 def __init__(self, label, metadata={}):
peustermcbcd4c22015-12-28 11:33:42 +010074 self.net = None # DCNetwork to which we belong
peusterma47db032016-02-04 14:55:29 +010075 # 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
peusterm53504942016-02-04 16:09:28 +010079 # 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
peusterm42f08be2016-03-10 21:56:34 +010083 # 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
peustermcbcd4c22015-12-28 11:33:42 +010089
peusterme26487b2016-03-08 14:00:21 +010090 def __repr__(self):
91 return self.label
92
peustermcbcd4c22015-12-28 11:33:42 +010093 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
peusterm9c252b62016-01-06 16:59:53 +0100103 TODO: This will be changed in the future to support multiple networks
peustermcbcd4c22015-12-28 11:33:42 +0100104 per data center
105 """
peusterm293cbc32016-01-13 17:05:28 +0100106 self.switch = self.net.addSwitch(
peustermcbcd4c22015-12-28 11:33:42 +0100107 "%s.s1" % self.name, dpid=hex(self._get_next_dc_dpid())[2:])
peusterm3444ae42016-03-16 20:46:41 +0100108 LOG.debug("created data center switch: %s" % str(self.switch))
peustermcbcd4c22015-12-28 11:33:42 +0100109
110 def start(self):
111 pass
112
peusterm42f08be2016-03-10 21:56:34 +0100113 def startCompute(self, name, image=None, command=None, network=None, flavor_name="tiny"):
peusterme4e89d32016-01-07 09:14:54 +0100114 """
115 Create a new container as compute resource and connect it to this
116 data center.
peusterm7f8e8402016-02-28 18:38:10 +0100117 :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"})
peusterm42f08be2016-03-10 21:56:34 +0100121 :param flavor_name: name of the flavor for this compute container
peusterm7f8e8402016-02-28 18:38:10 +0100122 :return:
peusterme4e89d32016-01-07 09:14:54 +0100123 """
peusterm4e98b632016-01-12 14:08:07 +0100124 assert name is not None
peusterm9165ef92016-01-13 13:50:39 +0100125 # no duplications
peustermbd44f4a2016-01-13 14:53:30 +0100126 if name in [c.name for c in self.net.getAllContainers()]:
peusterm9165ef92016-01-13 13:50:39 +0100127 raise Exception("Container with name %s already exists." % name)
peusterm4e98b632016-01-12 14:08:07 +0100128 # set default parameter
129 if image is None:
130 image = "ubuntu"
131 if network is None:
132 network = {} # {"ip": "10.0.0.254/8"}
peusterm7f8e8402016-02-28 18:38:10 +0100133 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
peusterm42f08be2016-03-10 21:56:34 +0100139 # allocate in resource resource model and compute resource limits for new container
peusterm71b3a2f2016-03-19 12:56:11 +0100140 cpu_limit = mem_limit = disk_limit = -1
141 cpu_period = cpu_quota = None
peusterm42f08be2016-03-10 21:56:34 +0100142 if self._resource_model is not None:
peusterm71b3a2f2016-03-19 12:56:11 +0100143 # call allocate in resource model to calculate resource limit for this container
peusterma769d952016-03-12 12:01:27 +0100144 (cpu_limit, mem_limit, disk_limit) = alloc = self._resource_model.allocate(name, flavor_name)
peusterm3444ae42016-03-16 20:46:41 +0100145 LOG.debug("Allocation result: %r" % str(alloc))
peusterm71b3a2f2016-03-19 12:56:11 +0100146 # 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 %d to avoid system error." % name)
160 # TODO add memory and disc limitations
peusterm7f8e8402016-02-28 18:38:10 +0100161 # create the container
peusterm42f08be2016-03-10 21:56:34 +0100162 d = self.net.addDocker(
163 "%s" % (name),
164 dimage=image,
165 dcmd=command,
166 datacenter=self,
peusterm71b3a2f2016-03-19 12:56:11 +0100167 flavor_name=flavor_name,
168 cpu_period=int(cpu_period) if cpu_limit > 0 else None, # set cpu limits if needed
169 cpu_quota=int(cpu_quota) if cpu_limit > 0 else None,
170 )
peusterm7f8e8402016-02-28 18:38:10 +0100171 # connect all given networks
172 for nw in network:
peustermea8db832016-03-08 10:25:58 +0100173 # TODO we cannot use TCLink here (see: https://github.com/mpeuster/dockernet/issues/3)
174 self.net.addLink(d, self.switch, params1=nw, cls=Link)
peusterm2ec74e12016-01-13 11:17:53 +0100175 # do bookkeeping
peusterma2ad9ff2016-01-11 17:10:07 +0100176 self.containers[name] = d
peustermfa4bcc72016-01-15 11:08:09 +0100177 return d # we might use UUIDs for naming later on
peustermcbcd4c22015-12-28 11:33:42 +0100178
peusterm7aae6852016-01-12 14:53:18 +0100179 def stopCompute(self, name):
peusterma2ad9ff2016-01-11 17:10:07 +0100180 """
181 Stop and remove a container from this data center.
182 """
peusterm9165ef92016-01-13 13:50:39 +0100183 assert name is not None
184 if name not in self.containers:
185 raise Exception("Container with name %s not found." % name)
peusterma2ad9ff2016-01-11 17:10:07 +0100186 self.net.removeLink(
187 link=None, node1=self.containers[name], node2=self.switch)
peustermc3b977e2016-01-12 10:09:35 +0100188 self.net.removeDocker("%s" % (name))
peusterma2ad9ff2016-01-11 17:10:07 +0100189 del self.containers[name]
peusterm42f08be2016-03-10 21:56:34 +0100190 # call resource model and free resources
191 if self._resource_model is not None:
192 self._resource_model.free(name)
peusterm4e98b632016-01-12 14:08:07 +0100193 return True
194
195 def listCompute(self):
196 """
197 Return a list of all running containers assigned to this
198 data center.
199 """
peusterm5aa8cf22016-01-15 11:12:17 +0100200 return list(self.containers.itervalues())
peustermd313dc12016-02-04 15:36:02 +0100201
202 def getStatus(self):
203 """
204 Return a dict with status information about this DC.
205 """
peusterm53504942016-02-04 16:09:28 +0100206 return {
207 "label": self.label,
208 "internalname": self.name,
209 "switch": self.switch.name,
210 "n_running_containers": len(self.containers),
211 "metadata": self.metadata
212 }
peusterm42f08be2016-03-10 21:56:34 +0100213
214 def assignResourceModel(self, rm):
peusterm279565d2016-03-19 10:36:52 +0100215 """
216 Assign a resource model to this DC.
217 :param rm: a BaseResourceModel object
218 :return:
219 """
peusterm42f08be2016-03-10 21:56:34 +0100220 if self._resource_model is not None:
221 raise Exception("There is already an resource model assigned to this DC.")
222 self._resource_model = rm
223 self.net.rm_registrar.register(self, rm)
peusterm3444ae42016-03-16 20:46:41 +0100224 LOG.info("Assigned RM: %r to DC: %r" % (rm, self))
peusterm42f08be2016-03-10 21:56:34 +0100225