blob: 191d6a855e1aef380106c658c752af079df0505d [file] [log] [blame]
peustermcbcd4c22015-12-28 11:33:42 +01001"""
peusterm79ef6ae2016-07-08 13:53:57 +02002Copyright (c) 2015 SONATA-NFV and Paderborn University
3ALL RIGHTS RESERVED.
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16
17Neither the name of the SONATA-NFV [, ANY ADDITIONAL AFFILIATION]
18nor the names of its contributors may be used to endorse or promote
19products derived from this software without specific prior written
20permission.
21
22This work has been performed in the framework of the SONATA project,
23funded by the European Commission under Grant number 671517 through
24the Horizon 2020 and 5G-PPP programmes. The authors would like to
25acknowledge the contributions of their colleagues of the SONATA
26partner consortium (www.sonata-nfv.eu).
peustermcbcd4c22015-12-28 11:33:42 +010027"""
28import logging
29
stevenvanrossem9ebd0942016-02-22 10:13:05 +010030import site
peustermef6629e2016-03-14 17:21:56 +010031import time
stevenvanrossem9ebd0942016-02-22 10:13:05 +010032from subprocess import Popen
stevenvanrossem6b1d9b92016-05-02 13:10:40 +020033import re
stevenvanrossem3fc13932016-08-09 23:39:16 +020034import requests
stevenvanrossemc3a344f2016-11-04 19:34:47 +010035import os
stevenvanrossem9ebd0942016-02-22 10:13:05 +010036
peusterm5877ea22016-05-11 13:44:59 +020037from mininet.net import Containernet
peustermef6629e2016-03-14 17:21:56 +010038from mininet.node import Controller, DefaultController, OVSSwitch, OVSKernelSwitch, Docker, RemoteController
peustermcbcd4c22015-12-28 11:33:42 +010039from mininet.cli import CLI
peustermea8db832016-03-08 10:25:58 +010040from mininet.link import TCLink
peusterm8b04b532016-07-19 16:55:38 +020041from mininet.clean import cleanup
stevenvanrossemc5a536a2016-02-16 14:52:39 +010042import networkx as nx
cgeoffroy9524ad32016-03-03 18:24:15 +010043from emuvim.dcemulator.monitoring import DCNetworkMonitor
cgeoffroy9524ad32016-03-03 18:24:15 +010044from emuvim.dcemulator.node import Datacenter, EmulatorCompute
peusterm42f08be2016-03-10 21:56:34 +010045from emuvim.dcemulator.resourcemodel import ResourceModelRegistrar
peustermcbcd4c22015-12-28 11:33:42 +010046
peustermf9a817d2016-07-18 09:06:04 +020047LOG = logging.getLogger("dcemulator.net")
48LOG.setLevel(logging.DEBUG)
49
stevenvanrossemb3f34172016-11-16 23:30:57 +010050# default CPU period used for cpu percentage-based cfs values (microseconds)
51CPU_PERIOD = 1000000
52
peusterm5877ea22016-05-11 13:44:59 +020053class DCNetwork(Containernet):
peusterme4e89d32016-01-07 09:14:54 +010054 """
peusterm5877ea22016-05-11 13:44:59 +020055 Wraps the original Mininet/Containernet class and provides
peusterme4e89d32016-01-07 09:14:54 +010056 methods to add data centers, switches, etc.
57
58 This class is used by topology definition scripts.
59 """
peustermcbcd4c22015-12-28 11:33:42 +010060
peusterm0ec25102016-04-16 02:16:20 +020061 def __init__(self, controller=RemoteController, monitor=False,
stevenvanrossemb3f34172016-11-16 23:30:57 +010062 enable_learning=False, # learning switch behavior of the default ovs switches icw Ryu controller can be turned off/on, needed for E-LAN functionality
peusterma4d84792016-03-25 12:27:07 +010063 dc_emulation_max_cpu=1.0, # fraction of overall CPU time for emulation
64 dc_emulation_max_mem=512, # emulation max mem in MB
65 **kwargs):
peusterm42f08be2016-03-10 21:56:34 +010066 """
peusterm5877ea22016-05-11 13:44:59 +020067 Create an extended version of a Containernet network
peusterm42f08be2016-03-10 21:56:34 +010068 :param dc_emulation_max_cpu: max. CPU time used by containers in data centers
69 :param kwargs: path through for Mininet parameters
70 :return:
71 """
peusterm8b04b532016-07-19 16:55:38 +020072 # members
peustermcbcd4c22015-12-28 11:33:42 +010073 self.dcs = {}
peusterm8b04b532016-07-19 16:55:38 +020074 self.ryu_process = None
stevenvanrossembecc7c52016-11-07 05:52:01 +010075 #list of deployed nsds.E_Lines and E_LANs (uploaded from the dummy gatekeeper)
76 self.deployed_nsds = []
77 self.deployed_elines = []
78 self.deployed_elans = []
79 self.installed_chains = []
80
peusterm42f08be2016-03-10 21:56:34 +010081
peusterm8b04b532016-07-19 16:55:38 +020082 # always cleanup environment before we start the emulator
stevenvanrossem89706802016-07-19 02:54:45 +020083 self.killRyu()
peusterm8b04b532016-07-19 16:55:38 +020084 cleanup()
stevenvanrossem89706802016-07-19 02:54:45 +020085
peusterm293cbc32016-01-13 17:05:28 +010086 # call original Docker.__init__ and setup default controller
peusterm5877ea22016-05-11 13:44:59 +020087 Containernet.__init__(
stevenvanrossem7cd3c252016-05-11 22:55:15 +020088 self, switch=OVSKernelSwitch, controller=controller, **kwargs)
stevenvanrossemc5a536a2016-02-16 14:52:39 +010089
stevenvanrossemc3a344f2016-11-04 19:34:47 +010090 # default switch configuration
91 enable_ryu_learning = False
92 if enable_learning :
93 self.failMode = 'standalone'
94 enable_ryu_learning = True
95 else:
96 self.failMode = 'secure'
97
peustermde14f332016-03-15 16:14:21 +010098 # Ryu management
peustermde14f332016-03-15 16:14:21 +010099 if controller == RemoteController:
100 # start Ryu controller
stevenvanrossemc3a344f2016-11-04 19:34:47 +0100101 self.startRyu(learning_switch=enable_ryu_learning)
stevenvanrossem9ebd0942016-02-22 10:13:05 +0100102
peustermde14f332016-03-15 16:14:21 +0100103 # add the specified controller
peustermef6629e2016-03-14 17:21:56 +0100104 self.addController('c0', controller=controller)
105
106 # graph of the complete DC network
stevenvanrossemc1149022016-04-11 01:16:44 +0200107 self.DCNetwork_graph = nx.MultiDiGraph()
peustermef6629e2016-03-14 17:21:56 +0100108
stevenvanrossem461941c2016-05-10 11:41:29 +0200109 # initialize pool of vlan tags to setup the SDN paths
110 self.vlans = range(4096)[::-1]
111
stevenvanrossem27b6d952016-05-10 16:37:57 +0200112 # link to Ryu REST_API
stevenvanrossem51d4ae72016-08-10 13:22:53 +0200113 ryu_ip = 'localhost'
stevenvanrossem27b6d952016-05-10 16:37:57 +0200114 ryu_port = '8080'
115 self.ryu_REST_api = 'http://{0}:{1}'.format(ryu_ip, ryu_port)
stevenvanrossem51d4ae72016-08-10 13:22:53 +0200116 self.RyuSession = requests.Session()
stevenvanrossem27b6d952016-05-10 16:37:57 +0200117
peustermef6629e2016-03-14 17:21:56 +0100118 # monitoring agent
stevenvanrossem60670da2016-04-15 15:31:28 +0200119 if monitor:
120 self.monitor_agent = DCNetworkMonitor(self)
121 else:
122 self.monitor_agent = None
peustermef6629e2016-03-14 17:21:56 +0100123
peusterm42f08be2016-03-10 21:56:34 +0100124 # initialize resource model registrar
peusterma4d84792016-03-25 12:27:07 +0100125 self.rm_registrar = ResourceModelRegistrar(
126 dc_emulation_max_cpu, dc_emulation_max_mem)
stevenvanrossemb3f34172016-11-16 23:30:57 +0100127 self.cpu_period = CPU_PERIOD
peustermcbcd4c22015-12-28 11:33:42 +0100128
peusterm60bf8b82016-04-06 14:12:35 +0200129 def addDatacenter(self, label, metadata={}, resource_log_path=None):
peustermcbcd4c22015-12-28 11:33:42 +0100130 """
131 Create and add a logical cloud data center to the network.
132 """
peusterma47db032016-02-04 14:55:29 +0100133 if label in self.dcs:
134 raise Exception("Data center label already exists: %s" % label)
peusterm60bf8b82016-04-06 14:12:35 +0200135 dc = Datacenter(label, metadata=metadata, resource_log_path=resource_log_path)
peustermcbcd4c22015-12-28 11:33:42 +0100136 dc.net = self # set reference to network
peusterma47db032016-02-04 14:55:29 +0100137 self.dcs[label] = dc
peustermcbcd4c22015-12-28 11:33:42 +0100138 dc.create() # finally create the data center in our Mininet instance
peustermf9a817d2016-07-18 09:06:04 +0200139 LOG.info("added data center: %s" % label)
peustermcbcd4c22015-12-28 11:33:42 +0100140 return dc
141
peusterme6092692016-01-11 16:32:58 +0100142 def addLink(self, node1, node2, **params):
peusterm5b844a12016-01-11 15:58:15 +0100143 """
144 Able to handle Datacenter objects as link
145 end points.
146 """
peustermcbcd4c22015-12-28 11:33:42 +0100147 assert node1 is not None
148 assert node2 is not None
stevenvanrossemc3a344f2016-11-04 19:34:47 +0100149
peustermcbcd4c22015-12-28 11:33:42 +0100150 # ensure type of node1
151 if isinstance( node1, basestring ):
152 if node1 in self.dcs:
153 node1 = self.dcs[node1].switch
peustermcbcd4c22015-12-28 11:33:42 +0100154 if isinstance( node1, Datacenter ):
155 node1 = node1.switch
156 # ensure type of node2
157 if isinstance( node2, basestring ):
158 if node2 in self.dcs:
159 node2 = self.dcs[node2].switch
peustermcbcd4c22015-12-28 11:33:42 +0100160 if isinstance( node2, Datacenter ):
161 node2 = node2.switch
peustermc3b977e2016-01-12 10:09:35 +0100162 # try to give containers a default IP
163 if isinstance( node1, Docker ):
peustermea8db832016-03-08 10:25:58 +0100164 if "params1" not in params:
peustermc3b977e2016-01-12 10:09:35 +0100165 params["params1"] = {}
peustermea8db832016-03-08 10:25:58 +0100166 if "ip" not in params["params1"]:
peustermc3b977e2016-01-12 10:09:35 +0100167 params["params1"]["ip"] = self.getNextIp()
168 if isinstance( node2, Docker ):
peustermea8db832016-03-08 10:25:58 +0100169 if "params2" not in params:
peustermc3b977e2016-01-12 10:09:35 +0100170 params["params2"] = {}
peustermea8db832016-03-08 10:25:58 +0100171 if "ip" not in params["params2"]:
peustermc3b977e2016-01-12 10:09:35 +0100172 params["params2"]["ip"] = self.getNextIp()
peustermea8db832016-03-08 10:25:58 +0100173 # ensure that we allow TCLinks between data centers
174 # TODO this is not optimal, we use cls=Link for containers and TCLink for data centers
peusterm5877ea22016-05-11 13:44:59 +0200175 # see Containernet issue: https://github.com/mpeuster/containernet/issues/3
peustermea8db832016-03-08 10:25:58 +0100176 if "cls" not in params:
177 params["cls"] = TCLink
peustermc3b977e2016-01-12 10:09:35 +0100178
peusterm5877ea22016-05-11 13:44:59 +0200179 link = Containernet.addLink(self, node1, node2, **params)
stevenvanrossemc5a536a2016-02-16 14:52:39 +0100180
stevenvanrossemc1149022016-04-11 01:16:44 +0200181 # try to give container interfaces a default id
182 node1_port_id = node1.ports[link.intf1]
183 if isinstance(node1, Docker):
184 if "id" in params["params1"]:
185 node1_port_id = params["params1"]["id"]
stevenvanrossem5b376412016-05-04 15:34:49 +0200186 node1_port_name = link.intf1.name
stevenvanrossemc1149022016-04-11 01:16:44 +0200187
188 node2_port_id = node2.ports[link.intf2]
189 if isinstance(node2, Docker):
190 if "id" in params["params2"]:
191 node2_port_id = params["params2"]["id"]
stevenvanrossem5b376412016-05-04 15:34:49 +0200192 node2_port_name = link.intf2.name
stevenvanrossem6b1d9b92016-05-02 13:10:40 +0200193
194
stevenvanrossemc5a536a2016-02-16 14:52:39 +0100195 # add edge and assigned port number to graph in both directions between node1 and node2
stevenvanrossemc1149022016-04-11 01:16:44 +0200196 # port_id: id given in descriptor (if available, otherwise same as port)
peusterm5877ea22016-05-11 13:44:59 +0200197 # port: portnumber assigned by Containernet
stevenvanrossemc1149022016-04-11 01:16:44 +0200198
stevenvanrossem6b1d9b92016-05-02 13:10:40 +0200199 attr_dict = {}
200 # possible weight metrics allowed by TClink class:
201 weight_metrics = ['bw', 'delay', 'jitter', 'loss']
202 edge_attributes = [p for p in params if p in weight_metrics]
203 for attr in edge_attributes:
204 # if delay: strip ms (need number as weight in graph)
205 match = re.search('([0-9]*\.?[0-9]+)', params[attr])
206 if match:
207 attr_number = match.group(1)
208 else:
209 attr_number = None
210 attr_dict[attr] = attr_number
211
212
stevenvanrossem5b376412016-05-04 15:34:49 +0200213 attr_dict2 = {'src_port_id': node1_port_id, 'src_port_nr': node1.ports[link.intf1],
214 'src_port_name': node1_port_name,
215 'dst_port_id': node2_port_id, 'dst_port_nr': node2.ports[link.intf2],
216 'dst_port_name': node2_port_name}
stevenvanrossem6b1d9b92016-05-02 13:10:40 +0200217 attr_dict2.update(attr_dict)
218 self.DCNetwork_graph.add_edge(node1.name, node2.name, attr_dict=attr_dict2)
219
stevenvanrossem5b376412016-05-04 15:34:49 +0200220 attr_dict2 = {'src_port_id': node2_port_id, 'src_port_nr': node2.ports[link.intf2],
221 'src_port_name': node2_port_name,
222 'dst_port_id': node1_port_id, 'dst_port_nr': node1.ports[link.intf1],
223 'dst_port_name': node1_port_name}
stevenvanrossem6b1d9b92016-05-02 13:10:40 +0200224 attr_dict2.update(attr_dict)
225 self.DCNetwork_graph.add_edge(node2.name, node1.name, attr_dict=attr_dict2)
stevenvanrossemc5a536a2016-02-16 14:52:39 +0100226
stevenvanrossemc3a344f2016-11-04 19:34:47 +0100227 LOG.debug("addLink: n1={0} intf1={1} -- n2={2} intf2={3}".format(
228 str(node1),node1_port_name, str(node2), node2_port_name))
229
stevenvanrossemc5a536a2016-02-16 14:52:39 +0100230 return link
peustermcbcd4c22015-12-28 11:33:42 +0100231
peusterma47db032016-02-04 14:55:29 +0100232 def addDocker( self, label, **params ):
peusterm5b844a12016-01-11 15:58:15 +0100233 """
peusterm293cbc32016-01-13 17:05:28 +0100234 Wrapper for addDocker method to use custom container class.
peusterm5b844a12016-01-11 15:58:15 +0100235 """
stevenvanrossem8fbf9782016-02-17 11:40:23 +0100236 self.DCNetwork_graph.add_node(label)
peusterm5877ea22016-05-11 13:44:59 +0200237 return Containernet.addDocker(self, label, cls=EmulatorCompute, **params)
stevenvanrossemc5a536a2016-02-16 14:52:39 +0100238
stevenvanrossem8fbf9782016-02-17 11:40:23 +0100239 def removeDocker( self, label, **params ):
stevenvanrossemc5a536a2016-02-16 14:52:39 +0100240 """
241 Wrapper for removeDocker method to update graph.
242 """
stevenvanrossem8fbf9782016-02-17 11:40:23 +0100243 self.DCNetwork_graph.remove_node(label)
peusterm5877ea22016-05-11 13:44:59 +0200244 return Containernet.removeDocker(self, label, **params)
stevenvanrossemc5a536a2016-02-16 14:52:39 +0100245
246 def addSwitch( self, name, add_to_graph=True, **params ):
247 """
248 Wrapper for addSwitch method to store switch also in graph.
249 """
stevenvanrossemc3a344f2016-11-04 19:34:47 +0100250
251 # add this switch to the global topology overview
stevenvanrossemc5a536a2016-02-16 14:52:39 +0100252 if add_to_graph:
253 self.DCNetwork_graph.add_node(name)
stevenvanrossemc3a344f2016-11-04 19:34:47 +0100254
255 # set the learning switch behavior
256 if 'failMode' in params :
257 failMode = params['failMode']
258 else :
259 failMode = self.failMode
260
261 s = Containernet.addSwitch(self, name, protocols='OpenFlow10,OpenFlow12,OpenFlow13', failMode=failMode, **params)
262
263 # set flow entry that enables learning switch behavior (needed to enable E-LAN functionality)
264 #LOG.info('failmode {0}'.format(failMode))
265 #if failMode == 'standalone' :
266 # LOG.info('add NORMAL')
267 # s.dpctl('add-flow', 'actions=NORMAL')
268
269 return s
peustermc3b977e2016-01-12 10:09:35 +0100270
peustermbd44f4a2016-01-13 14:53:30 +0100271 def getAllContainers(self):
272 """
273 Returns a list with all containers within all data centers.
274 """
275 all_containers = []
276 for dc in self.dcs.itervalues():
277 all_containers += dc.listCompute()
278 return all_containers
279
peustermcbcd4c22015-12-28 11:33:42 +0100280 def start(self):
281 # start
282 for dc in self.dcs.itervalues():
283 dc.start()
peusterm5877ea22016-05-11 13:44:59 +0200284 Containernet.start(self)
peustermcbcd4c22015-12-28 11:33:42 +0100285
286 def stop(self):
stevenvanrossem60670da2016-04-15 15:31:28 +0200287
stevenvanrossemc6abf132016-04-14 11:15:58 +0200288 # stop the monitor agent
stevenvanrossem60670da2016-04-15 15:31:28 +0200289 if self.monitor_agent is not None:
290 self.monitor_agent.stop()
peustermcbcd4c22015-12-28 11:33:42 +0100291
stevenvanrossembbdb5ee2016-04-15 15:18:44 +0200292 # stop emulator net
peusterm5877ea22016-05-11 13:44:59 +0200293 Containernet.stop(self)
stevenvanrossembbdb5ee2016-04-15 15:18:44 +0200294
295 # stop Ryu controller
peusterm8b04b532016-07-19 16:55:38 +0200296 self.killRyu()
stevenvanrossembbdb5ee2016-04-15 15:18:44 +0200297
298
peustermcbcd4c22015-12-28 11:33:42 +0100299 def CLI(self):
peusterm293cbc32016-01-13 17:05:28 +0100300 CLI(self)
stevenvanrossemc5a536a2016-02-16 14:52:39 +0100301
stevenvanrossemc3a344f2016-11-04 19:34:47 +0100302 def setLAN(self, vnf_list):
303 """
304 setup an E-LAN network by assigning the same VLAN tag to each DC interface of the VNFs in the E-LAN
305
306 :param vnf_list: names of the VNFs in this E-LAN [{name:,interface:},...]
307 :return:
308 """
309 src_sw = None
310 src_sw_inport_nr = 0
311 src_sw_inport_name = None
312
313 # get a vlan tag for this E-LAN
314 vlan = self.vlans.pop()
315
316 for vnf in vnf_list:
317 vnf_src_name = vnf['name']
318 vnf_src_interface = vnf['interface']
319
320 # check if port is specified (vnf:port)
321 if vnf_src_interface is None:
322 # take first interface by default
323 connected_sw = self.DCNetwork_graph.neighbors(vnf_src_name)[0]
324 link_dict = self.DCNetwork_graph[vnf_src_name][connected_sw]
325 vnf_src_interface = link_dict[0]['src_port_id']
326
327 for connected_sw in self.DCNetwork_graph.neighbors(vnf_src_name):
328 link_dict = self.DCNetwork_graph[vnf_src_name][connected_sw]
329 for link in link_dict:
330 if (link_dict[link]['src_port_id'] == vnf_src_interface or
331 link_dict[link]['src_port_name'] == vnf_src_interface): # Fix: we might also get interface names, e.g, from a son-emu-cli call
332 # found the right link and connected switch
333 src_sw = connected_sw
334 src_sw_inport_nr = link_dict[link]['dst_port_nr']
335 src_sw_inport_name = link_dict[link]['dst_port_name']
336 break
337
338 # set the tag on the dc switch interface
339 LOG.debug('set E-LAN: vnf name: {0} interface: {1} tag: {2}'.format(vnf_src_name, vnf_src_interface,vlan))
340 switch_node = self.getNodeByName(src_sw)
341 self._set_vlan_tag(switch_node, src_sw_inport_name, vlan)
342
stevenvanrossembecc7c52016-11-07 05:52:01 +0100343 def _addMonitorFlow(self, vnf_src_name, vnf_dst_name, vnf_src_interface=None, vnf_dst_interface=None,
344 tag=None, **kwargs):
stevenvanrossembf1754e2016-11-17 10:20:52 +0100345 """
346 Add a monitoring flow entry that adds a special flowentry/counter at the begin or end of a chain.
347 So this monitoring flowrule exists on top of a previously defined chain rule and uses the same vlan tag/routing.
348 :param vnf_src_name:
349 :param vnf_dst_name:
350 :param vnf_src_interface:
351 :param vnf_dst_interface:
352 :param tag: vlan tag to be used for this chain (same tag as existing chain)
353 :param monitor_placement: 'tx' or 'rx' indicating to place the extra flowentry resp. at the beginning or end of the chain
354 :return:
355 """
stevenvanrossembecc7c52016-11-07 05:52:01 +0100356
357 src_sw = None
358 src_sw_inport_nr = 0
359 src_sw_inport_name = None
360 dst_sw = None
361 dst_sw_outport_nr = 0
362 dst_sw_outport_name = None
363
364 LOG.debug("call AddMonitorFlow vnf_src_name=%r, vnf_src_interface=%r, vnf_dst_name=%r, vnf_dst_interface=%r",
365 vnf_src_name, vnf_src_interface, vnf_dst_name, vnf_dst_interface)
366
367 #check if port is specified (vnf:port)
368 if vnf_src_interface is None:
369 # take first interface by default
370 connected_sw = self.DCNetwork_graph.neighbors(vnf_src_name)[0]
371 link_dict = self.DCNetwork_graph[vnf_src_name][connected_sw]
372 vnf_src_interface = link_dict[0]['src_port_id']
373
374 for connected_sw in self.DCNetwork_graph.neighbors(vnf_src_name):
375 link_dict = self.DCNetwork_graph[vnf_src_name][connected_sw]
376 for link in link_dict:
377 if (link_dict[link]['src_port_id'] == vnf_src_interface or
378 link_dict[link]['src_port_name'] == vnf_src_interface): # Fix: we might also get interface names, e.g, from a son-emu-cli call
379 # found the right link and connected switch
380 src_sw = connected_sw
381 src_sw_inport_nr = link_dict[link]['dst_port_nr']
382 src_sw_inport_name = link_dict[link]['dst_port_name']
383 break
384
385 if vnf_dst_interface is None:
386 # take first interface by default
387 connected_sw = self.DCNetwork_graph.neighbors(vnf_dst_name)[0]
388 link_dict = self.DCNetwork_graph[connected_sw][vnf_dst_name]
389 vnf_dst_interface = link_dict[0]['dst_port_id']
390
391 vnf_dst_name = vnf_dst_name.split(':')[0]
392 for connected_sw in self.DCNetwork_graph.neighbors(vnf_dst_name):
393 link_dict = self.DCNetwork_graph[connected_sw][vnf_dst_name]
394 for link in link_dict:
395 if link_dict[link]['dst_port_id'] == vnf_dst_interface or \
396 link_dict[link]['dst_port_name'] == vnf_dst_interface: # Fix: we might also get interface names, e.g, from a son-emu-cli call
397 # found the right link and connected switch
398 dst_sw = connected_sw
399 dst_sw_outport_nr = link_dict[link]['src_port_nr']
400 dst_sw_outport_name = link_dict[link]['src_port_name']
401 break
402
403 if not tag >= 0:
404 LOG.exception('tag not valid: {0}'.format(tag))
405
406 # get shortest path
407 try:
408 # returns the first found shortest path
409 # if all shortest paths are wanted, use: all_shortest_paths
410 path = nx.shortest_path(self.DCNetwork_graph, src_sw, dst_sw, weight=kwargs.get('weight'))
411 except:
412 LOG.exception("No path could be found between {0} and {1} using src_sw={2} and dst_sw={3}".format(
413 vnf_src_name, vnf_dst_name, src_sw, dst_sw))
414 LOG.debug("Graph nodes: %r" % self.DCNetwork_graph.nodes())
415 LOG.debug("Graph edges: %r" % self.DCNetwork_graph.edges())
416 for e, v in self.DCNetwork_graph.edges():
417 LOG.debug("%r" % self.DCNetwork_graph[e][v])
418 return "No path could be found between {0} and {1}".format(vnf_src_name, vnf_dst_name)
419
420 LOG.info("Path between {0} and {1}: {2}".format(vnf_src_name, vnf_dst_name, path))
421
422 current_hop = src_sw
423 switch_inport_nr = src_sw_inport_nr
424
425 cmd = kwargs.get('cmd')
426
427 #iterate through the path to install the flow-entries
428 for i in range(0,len(path)):
429 current_node = self.getNodeByName(current_hop)
430
431 if path.index(current_hop) < len(path)-1:
432 next_hop = path[path.index(current_hop)+1]
433 else:
434 #last switch reached
435 next_hop = vnf_dst_name
436
437 next_node = self.getNodeByName(next_hop)
438
439 if next_hop == vnf_dst_name:
440 switch_outport_nr = dst_sw_outport_nr
441 LOG.info("end node reached: {0}".format(vnf_dst_name))
442 elif not isinstance( next_node, OVSSwitch ):
443 LOG.info("Next node: {0} is not a switch".format(next_hop))
444 return "Next node: {0} is not a switch".format(next_hop)
445 else:
446 # take first link between switches by default
447 index_edge_out = 0
448 switch_outport_nr = self.DCNetwork_graph[current_hop][next_hop][index_edge_out]['src_port_nr']
stevenvanrossemc3a344f2016-11-04 19:34:47 +0100449
450
stevenvanrossembecc7c52016-11-07 05:52:01 +0100451 # set of entry via ovs-ofctl
452 if isinstance( current_node, OVSSwitch ):
453 kwargs['vlan'] = tag
454 kwargs['path'] = path
455 kwargs['current_hop'] = current_hop
456 kwargs['switch_inport_name'] = src_sw_inport_name
457 kwargs['switch_outport_name'] = dst_sw_outport_name
458 kwargs['skip_vlan_tag'] = True
459
stevenvanrossem4fac2af2016-12-22 01:26:02 +0100460 monitor_placement = kwargs.get('monitor_placement').strip()
stevenvanrossembecc7c52016-11-07 05:52:01 +0100461 # put monitor flow at the dst switch
462 insert_flow = False
463 if monitor_placement == 'tx' and path.index(current_hop) == 0: # first node:
464 insert_flow = True
465 # put monitoring flow at the src switch
466 elif monitor_placement == 'rx' and path.index(current_hop) == len(path) - 1: # last node:
467 insert_flow = True
stevenvanrossem4fac2af2016-12-22 01:26:02 +0100468 elif monitor_placement not in ['rx', 'tx']:
stevenvanrossembecc7c52016-11-07 05:52:01 +0100469 LOG.exception('invalid monitor command: {0}'.format(monitor_placement))
470
471
472 if self.controller == RemoteController and insert_flow:
473 ## set flow entry via ryu rest api
474 self._set_flow_entry_ryu_rest(current_node, switch_inport_nr, switch_outport_nr, **kwargs)
475 break
476 elif insert_flow:
477 ## set flow entry via ovs-ofctl
478 self._set_flow_entry_dpctl(current_node, switch_inport_nr, switch_outport_nr, **kwargs)
479 break
480
481 # take first link between switches by default
482 if isinstance( next_node, OVSSwitch ):
483 switch_inport_nr = self.DCNetwork_graph[current_hop][next_hop][0]['dst_port_nr']
484 current_hop = next_hop
485
486 return "path {2} between {0} and {1}".format(vnf_src_name, vnf_dst_name, cmd)
stevenvanrossemc3a344f2016-11-04 19:34:47 +0100487
488
stevenvanrossem461941c2016-05-10 11:41:29 +0200489 def setChain(self, vnf_src_name, vnf_dst_name, vnf_src_interface=None, vnf_dst_interface=None, **kwargs):
stevenvanrossem51d4ae72016-08-10 13:22:53 +0200490 """
491 Chain 2 vnf interfaces together by installing the flowrules in the switches along their path.
492 Currently the path is found using the default networkx shortest path function.
493 Each chain gets a unique vlan id , so different chains wil not interfere.
494
495 :param vnf_src_name: vnf name (string)
496 :param vnf_dst_name: vnf name (string)
497 :param vnf_src_interface: source interface name (string)
498 :param vnf_dst_interface: destination interface name (string)
499 :param cmd: 'add-flow' (default) to add a chain, 'del-flows' to remove a chain
500 :param cookie: cookie for the installed flowrules (can be used later as identifier for a set of installed chains)
501 :param match: custom match entry to be added to the flowrules (default: only in_port and vlan tag)
502 :param priority: custom flowrule priority
stevenvanrossembf1754e2016-11-17 10:20:52 +0100503 :param monitor: boolean to indicate whether this chain is a monitoring chain
504 :param tag: vlan tag to be used for this chain (pre-defined or new one if none is specified)
stevenvanrossem51d4ae72016-08-10 13:22:53 +0200505 :return: output log string
506 """
stevenvanrossembecc7c52016-11-07 05:52:01 +0100507
508 # special procedure for monitoring flows
509 if kwargs.get('monitor'):
510
511 # check if chain already exists
512 found_chains = [chain_dict for chain_dict in self.installed_chains if
513 (chain_dict['vnf_src_name'] == vnf_src_name and chain_dict['vnf_src_interface'] == vnf_src_interface
514 and chain_dict['vnf_dst_name'] == vnf_dst_name and chain_dict['vnf_dst_interface'] == vnf_dst_interface)]
515
516 if len(found_chains) > 0:
517 # this chain exists, so need an extra monitoring flow
518 # assume only 1 chain per vnf/interface pair
519 LOG.debug('*** installing monitoring chain on top of pre-defined chain from {0}:{1} -> {2}:{3}'.
520 format(vnf_src_name, vnf_src_interface, vnf_dst_name, vnf_dst_interface))
521 tag = found_chains[0]['tag']
522 ret = self._addMonitorFlow(vnf_src_name, vnf_dst_name, vnf_src_interface, vnf_dst_interface,
523 tag=tag, table_id=0, **kwargs)
524 return ret
525 else:
526 # no chain existing (or E-LAN) -> install normal chain
stevenvanrossem4fac2af2016-12-22 01:26:02 +0100527 LOG.warning('*** installing monitoring chain without pre-defined NSD chain from {0}:{1} -> {2}:{3}'.
stevenvanrossembecc7c52016-11-07 05:52:01 +0100528 format(vnf_src_name, vnf_src_interface, vnf_dst_name, vnf_dst_interface))
529 pass
530
531
stevenvanrossem461941c2016-05-10 11:41:29 +0200532 cmd = kwargs.get('cmd')
stevenvanrossemc3a344f2016-11-04 19:34:47 +0100533 if cmd == 'add-flow' or cmd == 'del-flows':
stevenvanrossem461941c2016-05-10 11:41:29 +0200534 ret = self._chainAddFlow(vnf_src_name, vnf_dst_name, vnf_src_interface, vnf_dst_interface, **kwargs)
535 if kwargs.get('bidirectional'):
stevenvanrossem81955a52016-05-12 14:34:12 +0200536 ret = ret +'\n' + self._chainAddFlow(vnf_dst_name, vnf_src_name, vnf_dst_interface, vnf_src_interface, **kwargs)
stevenvanrossem9315da42016-04-11 12:10:06 +0200537
stevenvanrossem461941c2016-05-10 11:41:29 +0200538 else:
stevenvanrossem81955a52016-05-12 14:34:12 +0200539 ret = "Command unknown"
540
541 return ret
stevenvanrossem461941c2016-05-10 11:41:29 +0200542
543
544 def _chainAddFlow(self, vnf_src_name, vnf_dst_name, vnf_src_interface=None, vnf_dst_interface=None, **kwargs):
545
peusterm53d3c142016-07-18 10:10:11 +0200546 src_sw = None
peusterm53d3c142016-07-18 10:10:11 +0200547 src_sw_inport_nr = 0
stevenvanrossemc3a344f2016-11-04 19:34:47 +0100548 src_sw_inport_name = None
549 dst_sw = None
peusterm53d3c142016-07-18 10:10:11 +0200550 dst_sw_outport_nr = 0
stevenvanrossemc3a344f2016-11-04 19:34:47 +0100551 dst_sw_outport_name = None
peusterm53d3c142016-07-18 10:10:11 +0200552
553 LOG.debug("call chainAddFlow vnf_src_name=%r, vnf_src_interface=%r, vnf_dst_name=%r, vnf_dst_interface=%r",
554 vnf_src_name, vnf_src_interface, vnf_dst_name, vnf_dst_interface)
555
stevenvanrossem9315da42016-04-11 12:10:06 +0200556 #check if port is specified (vnf:port)
stevenvanrossemed711fd2016-04-11 16:59:29 +0200557 if vnf_src_interface is None:
stevenvanrossem9315da42016-04-11 12:10:06 +0200558 # take first interface by default
559 connected_sw = self.DCNetwork_graph.neighbors(vnf_src_name)[0]
560 link_dict = self.DCNetwork_graph[vnf_src_name][connected_sw]
stevenvanrossemed711fd2016-04-11 16:59:29 +0200561 vnf_src_interface = link_dict[0]['src_port_id']
stevenvanrossem9315da42016-04-11 12:10:06 +0200562
stevenvanrossem9315da42016-04-11 12:10:06 +0200563 for connected_sw in self.DCNetwork_graph.neighbors(vnf_src_name):
564 link_dict = self.DCNetwork_graph[vnf_src_name][connected_sw]
565 for link in link_dict:
peusterm53d3c142016-07-18 10:10:11 +0200566 if (link_dict[link]['src_port_id'] == vnf_src_interface or
567 link_dict[link]['src_port_name'] == vnf_src_interface): # Fix: we might also get interface names, e.g, from a son-emu-cli call
stevenvanrossem9315da42016-04-11 12:10:06 +0200568 # found the right link and connected switch
stevenvanrossem9315da42016-04-11 12:10:06 +0200569 src_sw = connected_sw
stevenvanrossemedcbeeb2016-05-04 17:28:08 +0200570 src_sw_inport_nr = link_dict[link]['dst_port_nr']
stevenvanrossemc3a344f2016-11-04 19:34:47 +0100571 src_sw_inport_name = link_dict[link]['dst_port_name']
stevenvanrossem9315da42016-04-11 12:10:06 +0200572 break
573
stevenvanrossemed711fd2016-04-11 16:59:29 +0200574 if vnf_dst_interface is None:
stevenvanrossem9315da42016-04-11 12:10:06 +0200575 # take first interface by default
576 connected_sw = self.DCNetwork_graph.neighbors(vnf_dst_name)[0]
577 link_dict = self.DCNetwork_graph[connected_sw][vnf_dst_name]
stevenvanrossemed711fd2016-04-11 16:59:29 +0200578 vnf_dst_interface = link_dict[0]['dst_port_id']
stevenvanrossem9315da42016-04-11 12:10:06 +0200579
580 vnf_dst_name = vnf_dst_name.split(':')[0]
581 for connected_sw in self.DCNetwork_graph.neighbors(vnf_dst_name):
582 link_dict = self.DCNetwork_graph[connected_sw][vnf_dst_name]
583 for link in link_dict:
peusterm53d3c142016-07-18 10:10:11 +0200584 if link_dict[link]['dst_port_id'] == vnf_dst_interface or \
585 link_dict[link]['dst_port_name'] == vnf_dst_interface: # Fix: we might also get interface names, e.g, from a son-emu-cli call
stevenvanrossem9315da42016-04-11 12:10:06 +0200586 # found the right link and connected switch
587 dst_sw = connected_sw
stevenvanrossemedcbeeb2016-05-04 17:28:08 +0200588 dst_sw_outport_nr = link_dict[link]['src_port_nr']
stevenvanrossemc3a344f2016-11-04 19:34:47 +0100589 dst_sw_outport_name = link_dict[link]['src_port_name']
stevenvanrossem9315da42016-04-11 12:10:06 +0200590 break
591
592
stevenvanrossemc5a536a2016-02-16 14:52:39 +0100593 # get shortest path
stevenvanrossem9315da42016-04-11 12:10:06 +0200594 try:
stevenvanrossem1f68afb2016-05-02 15:36:04 +0200595 # returns the first found shortest path
596 # if all shortest paths are wanted, use: all_shortest_paths
stevenvanrossem461941c2016-05-10 11:41:29 +0200597 path = nx.shortest_path(self.DCNetwork_graph, src_sw, dst_sw, weight=kwargs.get('weight'))
stevenvanrossem9315da42016-04-11 12:10:06 +0200598 except:
peusterm53d3c142016-07-18 10:10:11 +0200599 LOG.exception("No path could be found between {0} and {1} using src_sw={2} and dst_sw={3}".format(
600 vnf_src_name, vnf_dst_name, src_sw, dst_sw))
peustermf9a817d2016-07-18 09:06:04 +0200601 LOG.debug("Graph nodes: %r" % self.DCNetwork_graph.nodes())
602 LOG.debug("Graph edges: %r" % self.DCNetwork_graph.edges())
peusterm53d3c142016-07-18 10:10:11 +0200603 for e, v in self.DCNetwork_graph.edges():
604 LOG.debug("%r" % self.DCNetwork_graph[e][v])
stevenvanrossem9315da42016-04-11 12:10:06 +0200605 return "No path could be found between {0} and {1}".format(vnf_src_name, vnf_dst_name)
606
peustermf9a817d2016-07-18 09:06:04 +0200607 LOG.info("Path between {0} and {1}: {2}".format(vnf_src_name, vnf_dst_name, path))
stevenvanrossemc5a536a2016-02-16 14:52:39 +0100608
stevenvanrossem9315da42016-04-11 12:10:06 +0200609 current_hop = src_sw
stevenvanrossemedcbeeb2016-05-04 17:28:08 +0200610 switch_inport_nr = src_sw_inport_nr
stevenvanrossem9315da42016-04-11 12:10:06 +0200611
stevenvanrossemc3a344f2016-11-04 19:34:47 +0100612 # choose free vlan
613 ## if path contains more than 1 switch
stevenvanrossem27b6d952016-05-10 16:37:57 +0200614 cmd = kwargs.get('cmd')
615 vlan = None
616 if cmd == 'add-flow':
stevenvanrossembecc7c52016-11-07 05:52:01 +0100617 if kwargs.get('tag'):
618 # use pre-defined tag
619 vlan = kwargs.get('tag')
620 else:
621 vlan = self.vlans.pop()
stevenvanrossem461941c2016-05-10 11:41:29 +0200622
stevenvanrossembecc7c52016-11-07 05:52:01 +0100623 # store the used vlan tag to identify this chain
624 if not kwargs.get('monitor'):
625 chain_dict = {}
626 chain_dict['vnf_src_name'] = vnf_src_name
627 chain_dict['vnf_dst_name'] = vnf_dst_name
628 chain_dict['vnf_src_interface'] = vnf_src_interface
629 chain_dict['vnf_dst_interface'] = vnf_dst_interface
630 chain_dict['tag'] = vlan
631 self.installed_chains.append(chain_dict)
632
633 #iterate through the path to install the flow-entries
stevenvanrossemc5a536a2016-02-16 14:52:39 +0100634 for i in range(0,len(path)):
stevenvanrossem9315da42016-04-11 12:10:06 +0200635 current_node = self.getNodeByName(current_hop)
stevenvanrossem461941c2016-05-10 11:41:29 +0200636
stevenvanrossem9315da42016-04-11 12:10:06 +0200637 if path.index(current_hop) < len(path)-1:
638 next_hop = path[path.index(current_hop)+1]
639 else:
640 #last switch reached
641 next_hop = vnf_dst_name
642
stevenvanrossemc5a536a2016-02-16 14:52:39 +0100643 next_node = self.getNodeByName(next_hop)
644
645 if next_hop == vnf_dst_name:
stevenvanrossemedcbeeb2016-05-04 17:28:08 +0200646 switch_outport_nr = dst_sw_outport_nr
peustermf9a817d2016-07-18 09:06:04 +0200647 LOG.info("end node reached: {0}".format(vnf_dst_name))
stevenvanrossemc5a536a2016-02-16 14:52:39 +0100648 elif not isinstance( next_node, OVSSwitch ):
peustermf9a817d2016-07-18 09:06:04 +0200649 LOG.info("Next node: {0} is not a switch".format(next_hop))
stevenvanrossemeefea6c2016-02-17 12:03:26 +0100650 return "Next node: {0} is not a switch".format(next_hop)
stevenvanrossem9315da42016-04-11 12:10:06 +0200651 else:
stevenvanrossemed711fd2016-04-11 16:59:29 +0200652 # take first link between switches by default
stevenvanrossem9315da42016-04-11 12:10:06 +0200653 index_edge_out = 0
stevenvanrossemedcbeeb2016-05-04 17:28:08 +0200654 switch_outport_nr = self.DCNetwork_graph[current_hop][next_hop][index_edge_out]['src_port_nr']
stevenvanrossem9315da42016-04-11 12:10:06 +0200655
stevenvanrossem9315da42016-04-11 12:10:06 +0200656
stevenvanrossem461941c2016-05-10 11:41:29 +0200657 # set of entry via ovs-ofctl
stevenvanrossem9315da42016-04-11 12:10:06 +0200658 if isinstance( current_node, OVSSwitch ):
stevenvanrossem461941c2016-05-10 11:41:29 +0200659 kwargs['vlan'] = vlan
660 kwargs['path'] = path
661 kwargs['current_hop'] = current_hop
stevenvanrossemc3a344f2016-11-04 19:34:47 +0100662 kwargs['switch_inport_name'] = src_sw_inport_name
663 kwargs['switch_outport_name'] = dst_sw_outport_name
stevenvanrossem7cd3c252016-05-11 22:55:15 +0200664
665 if self.controller == RemoteController:
666 ## set flow entry via ryu rest api
667 self._set_flow_entry_ryu_rest(current_node, switch_inport_nr, switch_outport_nr, **kwargs)
668 else:
669 ## set flow entry via ovs-ofctl
670 self._set_flow_entry_dpctl(current_node, switch_inport_nr, switch_outport_nr, **kwargs)
671
stevenvanrossemed711fd2016-04-11 16:59:29 +0200672 # take first link between switches by default
673 if isinstance( next_node, OVSSwitch ):
stevenvanrossemedcbeeb2016-05-04 17:28:08 +0200674 switch_inport_nr = self.DCNetwork_graph[current_hop][next_hop][0]['dst_port_nr']
stevenvanrossemed711fd2016-04-11 16:59:29 +0200675 current_hop = next_hop
stevenvanrossemc5a536a2016-02-16 14:52:39 +0100676
stevenvanrossem27b6d952016-05-10 16:37:57 +0200677 return "path {2} between {0} and {1}".format(vnf_src_name, vnf_dst_name, cmd)
678
679 def _set_flow_entry_ryu_rest(self, node, switch_inport_nr, switch_outport_nr, **kwargs):
680 match = 'in_port=%s' % switch_inport_nr
681
682 cookie = kwargs.get('cookie')
683 match_input = kwargs.get('match')
684 cmd = kwargs.get('cmd')
685 path = kwargs.get('path')
686 current_hop = kwargs.get('current_hop')
687 vlan = kwargs.get('vlan')
stevenvanrossem61699eb2016-08-05 15:57:59 +0200688 priority = kwargs.get('priority')
stevenvanrossembecc7c52016-11-07 05:52:01 +0100689 # flag to not set the ovs port vlan tag
690 skip_vlan_tag = kwargs.get('skip_vlan_tag')
691 # table id to put this flowentry
692 table_id = kwargs.get('table_id')
693 if not table_id:
694 table_id = 0
stevenvanrossem27b6d952016-05-10 16:37:57 +0200695
696 s = ','
697 if match_input:
698 match = s.join([match, match_input])
699
700 flow = {}
701 flow['dpid'] = int(node.dpid, 16)
stevenvanrossem7cd3c252016-05-11 22:55:15 +0200702
stevenvanrossem27b6d952016-05-10 16:37:57 +0200703 if cookie:
704 flow['cookie'] = int(cookie)
stevenvanrossem61699eb2016-08-05 15:57:59 +0200705 if priority:
706 flow['priority'] = int(priority)
stevenvanrossem27b6d952016-05-10 16:37:57 +0200707
stevenvanrossembecc7c52016-11-07 05:52:01 +0100708 flow['table_id'] = table_id
709
stevenvanrossem27b6d952016-05-10 16:37:57 +0200710 flow['actions'] = []
711
712 # possible Ryu actions, match fields:
713 # http://ryu.readthedocs.io/en/latest/app/ofctl_rest.html#add-a-flow-entry
714 if cmd == 'add-flow':
715 prefix = 'stats/flowentry/add'
stevenvanrossem27b6d952016-05-10 16:37:57 +0200716 if vlan != None:
717 if path.index(current_hop) == 0: # first node
stevenvanrossemc3a344f2016-11-04 19:34:47 +0100718 # set vlan tag in ovs instance (to isolate E-LANs)
stevenvanrossembecc7c52016-11-07 05:52:01 +0100719 if not skip_vlan_tag:
720 in_port_name = kwargs.get('switch_inport_name')
721 self._set_vlan_tag(node, in_port_name, vlan)
stevenvanrossemc3a344f2016-11-04 19:34:47 +0100722 # set vlan push action if more than 1 switch in the path
723 if len(path) > 1:
724 action = {}
725 action['type'] = 'PUSH_VLAN' # Push a new VLAN tag if a input frame is non-VLAN-tagged
726 action['ethertype'] = 33024 # Ethertype 0x8100(=33024): IEEE 802.1Q VLAN-tagged frame
727 flow['actions'].append(action)
728 action = {}
729 action['type'] = 'SET_FIELD'
730 action['field'] = 'vlan_vid'
731 action['value'] = vlan
732 flow['actions'].append(action)
733
734 if path.index(current_hop) == len(path) - 1: # last node
735 # set vlan tag in ovs instance (to isolate E-LANs)
stevenvanrossembecc7c52016-11-07 05:52:01 +0100736 if not skip_vlan_tag:
737 out_port_name = kwargs.get('switch_outport_name')
738 self._set_vlan_tag(node, out_port_name, vlan)
stevenvanrossemc3a344f2016-11-04 19:34:47 +0100739 # set vlan pop action if more than 1 switch in the path
740 if len(path) > 1:
741 match += ',dl_vlan=%s' % vlan
742 action = {}
743 action['type'] = 'POP_VLAN'
744 flow['actions'].append(action)
745
746 if 0 < path.index(current_hop) < (len(path) - 1): # middle nodes
stevenvanrossem27b6d952016-05-10 16:37:57 +0200747 match += ',dl_vlan=%s' % vlan
stevenvanrossemc3a344f2016-11-04 19:34:47 +0100748
stevenvanrossem7cd3c252016-05-11 22:55:15 +0200749 # output action must come last
750 action = {}
751 action['type'] = 'OUTPUT'
752 action['port'] = switch_outport_nr
753 flow['actions'].append(action)
stevenvanrossem7fce49b2016-05-12 01:34:15 +0200754
stevenvanrossem27b6d952016-05-10 16:37:57 +0200755 elif cmd == 'del-flows':
stevenvanrossem27b6d952016-05-10 16:37:57 +0200756 prefix = 'stats/flowentry/delete'
stevenvanrossem7fce49b2016-05-12 01:34:15 +0200757
stevenvanrossem27b6d952016-05-10 16:37:57 +0200758 if cookie:
stevenvanrossem1ef77022016-05-12 16:36:10 +0200759 # TODO: add cookie_mask as argument
stevenvanrossem7fce49b2016-05-12 01:34:15 +0200760 flow['cookie_mask'] = int('0xffffffffffffffff', 16) # need full mask to match complete cookie
stevenvanrossem27b6d952016-05-10 16:37:57 +0200761
762 action = {}
763 action['type'] = 'OUTPUT'
764 action['port'] = switch_outport_nr
765 flow['actions'].append(action)
766
767 flow['match'] = self._parse_match(match)
768 self.ryu_REST(prefix, data=flow)
stevenvanrossem9ebd0942016-02-22 10:13:05 +0100769
stevenvanrossemc3a344f2016-11-04 19:34:47 +0100770 def _set_vlan_tag(self, node, switch_port, tag):
771 node.vsctl('set', 'port {0} tag={1}'.format(switch_port,tag))
772 LOG.debug("set vlan in switch: {0} in_port: {1} vlan tag: {2}".format(node.name, switch_port, tag))
773
stevenvanrossem461941c2016-05-10 11:41:29 +0200774 def _set_flow_entry_dpctl(self, node, switch_inport_nr, switch_outport_nr, **kwargs):
stevenvanrossemc3a344f2016-11-04 19:34:47 +0100775
stevenvanrossem23c48092016-05-06 17:21:12 +0200776 match = 'in_port=%s' % switch_inport_nr
stevenvanrossem461941c2016-05-10 11:41:29 +0200777
778 cookie = kwargs.get('cookie')
779 match_input = kwargs.get('match')
780 cmd = kwargs.get('cmd')
781 path = kwargs.get('path')
782 current_hop = kwargs.get('current_hop')
783 vlan = kwargs.get('vlan')
784
stevenvanrossem898a2af2016-05-06 18:28:57 +0200785 s = ','
786 if cookie:
787 cookie = 'cookie=%s' % cookie
788 match = s.join([cookie, match])
stevenvanrossem23c48092016-05-06 17:21:12 +0200789 if match_input:
stevenvanrossem23c48092016-05-06 17:21:12 +0200790 match = s.join([match, match_input])
stevenvanrossem23c48092016-05-06 17:21:12 +0200791 if cmd == 'add-flow':
792 action = 'action=%s' % switch_outport_nr
stevenvanrossem461941c2016-05-10 11:41:29 +0200793 if vlan != None:
794 if path.index(current_hop) == 0: # first node
795 action = ('action=mod_vlan_vid:%s' % vlan) + (',output=%s' % switch_outport_nr)
796 match = '-O OpenFlow13 ' + match
797 elif path.index(current_hop) == len(path) - 1: # last node
798 match += ',dl_vlan=%s' % vlan
799 action = 'action=strip_vlan,output=%s' % switch_outport_nr
800 else: # middle nodes
801 match += ',dl_vlan=%s' % vlan
stevenvanrossem23c48092016-05-06 17:21:12 +0200802 ofcmd = s.join([match, action])
803 elif cmd == 'del-flows':
804 ofcmd = match
805 else:
806 ofcmd = ''
807
808 node.dpctl(cmd, ofcmd)
peustermf9a817d2016-07-18 09:06:04 +0200809 LOG.info("{3} in switch: {0} in_port: {1} out_port: {2}".format(node.name, switch_inport_nr,
stevenvanrossem461941c2016-05-10 11:41:29 +0200810 switch_outport_nr, cmd))
stevenvanrossem23c48092016-05-06 17:21:12 +0200811
stevenvanrossem9ebd0942016-02-22 10:13:05 +0100812 # start Ryu Openflow controller as Remote Controller for the DCNetwork
stevenvanrossem7cd3c252016-05-11 22:55:15 +0200813 def startRyu(self, learning_switch=True):
stevenvanrossem9ebd0942016-02-22 10:13:05 +0100814 # start Ryu controller with rest-API
815 python_install_path = site.getsitepackages()[0]
stevenvanrossemc3a344f2016-11-04 19:34:47 +0100816 # ryu default learning switch
817 #ryu_path = python_install_path + '/ryu/app/simple_switch_13.py'
818 #custom learning switch that installs a default NORMAL action in the ovs switches
819 dir_path = os.path.dirname(os.path.realpath(__file__))
820 ryu_path = dir_path + '/son_emu_simple_switch_13.py'
peustermde14f332016-03-15 16:14:21 +0100821 ryu_path2 = python_install_path + '/ryu/app/ofctl_rest.py'
stevenvanrossem9ebd0942016-02-22 10:13:05 +0100822 # change the default Openflow controller port to 6653 (official IANA-assigned port number), as used by Mininet
823 # Ryu still uses 6633 as default
824 ryu_option = '--ofp-tcp-listen-port'
825 ryu_of_port = '6653'
peustermde14f332016-03-15 16:14:21 +0100826 ryu_cmd = 'ryu-manager'
peustermef6629e2016-03-14 17:21:56 +0100827 FNULL = open("/tmp/ryu.log", 'w')
stevenvanrossem7cd3c252016-05-11 22:55:15 +0200828 if learning_switch:
829 self.ryu_process = Popen([ryu_cmd, ryu_path, ryu_path2, ryu_option, ryu_of_port], stdout=FNULL, stderr=FNULL)
stevenvanrossemc3a344f2016-11-04 19:34:47 +0100830 LOG.debug('starting ryu-controller with {0}'.format(ryu_path))
831 LOG.debug('starting ryu-controller with {0}'.format(ryu_path2))
stevenvanrossem7cd3c252016-05-11 22:55:15 +0200832 else:
stevenvanrossem73efd192016-06-29 01:44:07 +0200833 # no learning switch, but with rest api
stevenvanrossem7cd3c252016-05-11 22:55:15 +0200834 self.ryu_process = Popen([ryu_cmd, ryu_path2, ryu_option, ryu_of_port], stdout=FNULL, stderr=FNULL)
stevenvanrossemc3a344f2016-11-04 19:34:47 +0100835 LOG.debug('starting ryu-controller with {0}'.format(ryu_path2))
peusterm391773a2016-03-14 17:40:43 +0100836 time.sleep(1)
837
peusterm8b04b532016-07-19 16:55:38 +0200838 def killRyu(self):
839 """
840 Stop the Ryu controller that might be started by son-emu.
841 :return:
842 """
843 # try it nicely
peustermde14f332016-03-15 16:14:21 +0100844 if self.ryu_process is not None:
peusterm391773a2016-03-14 17:40:43 +0100845 self.ryu_process.terminate()
846 self.ryu_process.kill()
peusterm8b04b532016-07-19 16:55:38 +0200847 # ensure its death ;-)
stevenvanrossem89706802016-07-19 02:54:45 +0200848 Popen(['pkill', '-f', 'ryu-manager'])
peusterm391773a2016-03-14 17:40:43 +0100849
stevenvanrossem27b6d952016-05-10 16:37:57 +0200850 def ryu_REST(self, prefix, dpid=None, data=None):
stevenvanrossem27b6d952016-05-10 16:37:57 +0200851
stevenvanrossem51d4ae72016-08-10 13:22:53 +0200852 if dpid:
853 url = self.ryu_REST_api + '/' + str(prefix) + '/' + str(dpid)
854 else:
855 url = self.ryu_REST_api + '/' + str(prefix)
856 if data:
857 req = self.RyuSession.post(url, json=data)
858 else:
859 req = self.RyuSession.get(url)
860
861
862 # do extra logging if status code is not 200 (OK)
863 if req.status_code is not requests.codes.ok:
864 logging.info(
865 'type {0} encoding: {1} text: {2} headers: {3} history: {4}'.format(req.headers['content-type'],
866 req.encoding, req.text,
867 req.headers, req.history))
868 LOG.info('url: {0}'.format(str(url)))
869 if data: LOG.info('POST: {0}'.format(str(data)))
870 LOG.info('status: {0} reason: {1}'.format(req.status_code, req.reason))
871
872
873 if 'json' in req.headers['content-type']:
874 ret = req.json()
stevenvanrossem7cd3c252016-05-11 22:55:15 +0200875 return ret
stevenvanrossem51d4ae72016-08-10 13:22:53 +0200876
877 ret = req.text.rstrip()
878 return ret
879
stevenvanrossem27b6d952016-05-10 16:37:57 +0200880
881 # need to respect that some match fields must be integers
882 # http://ryu.readthedocs.io/en/latest/app/ofctl_rest.html#description-of-match-and-actions
883 def _parse_match(self, match):
884 matches = match.split(',')
885 dict = {}
886 for m in matches:
887 match = m.split('=')
888 if len(match) == 2:
889 try:
890 m2 = int(match[1], 0)
891 except:
892 m2 = match[1]
893
894 dict.update({match[0]:m2})
895 return dict
896
stevenvanrossem566779d2016-11-07 06:33:44 +0100897 def find_connected_dc_interface(self, vnf_src_name, vnf_src_interface):
898 for connected_sw in self.DCNetwork_graph.neighbors(vnf_src_name):
899 link_dict = self.DCNetwork_graph[vnf_src_name][connected_sw]
900 for link in link_dict:
901 if (link_dict[link]['src_port_id'] == vnf_src_interface or
902 link_dict[link]['src_port_name'] == vnf_src_interface): # Fix: we might also get interface names, e.g, from a son-emu-cli call
903 # found the right link and connected switch
904 src_sw = connected_sw
905 src_sw_inport_nr = link_dict[link]['dst_port_nr']
906 src_sw_inport_name = link_dict[link]['dst_port_name']
stevenvanrossem7062cee2016-12-22 10:31:38 +0100907 return src_sw_inport_name