+ next_node = self.getNodeByName(next_hop)
+
+ if next_hop == vnf_dst_name:
+ switch_outport_nr = dst_sw_outport_nr
+ LOG.info("end node reached: {0}".format(vnf_dst_name))
+ elif not isinstance( next_node, OVSSwitch ):
+ LOG.info("Next node: {0} is not a switch".format(next_hop))
+ return "Next node: {0} is not a switch".format(next_hop)
+ else:
+ # take first link between switches by default
+ index_edge_out = 0
+ switch_outport_nr = self.DCNetwork_graph[current_hop][next_hop][index_edge_out]['src_port_nr']
+
+
+ # set OpenFlow entry
+ if isinstance( current_node, OVSSwitch ):
+ kwargs['vlan'] = vlan
+ kwargs['path'] = path
+ kwargs['current_hop'] = current_hop
+ kwargs['switch_inport_name'] = src_sw_inport_name
+ kwargs['switch_outport_name'] = dst_sw_outport_name
+ kwargs['pathindex'] = i
+
+ if self.controller == RemoteController:
+ ## set flow entry via ryu rest api
+ self._set_flow_entry_ryu_rest(current_node, switch_inport_nr, switch_outport_nr, **kwargs)
+ else:
+ ## set flow entry via ovs-ofctl
+ self._set_flow_entry_dpctl(current_node, switch_inport_nr, switch_outport_nr, **kwargs)
+
+ # take first link between switches by default
+ if isinstance( next_node, OVSSwitch ):
+ switch_inport_nr = self.DCNetwork_graph[current_hop][next_hop][0]['dst_port_nr']
+ current_hop = next_hop
+
+ return "path {2} between {0} and {1}".format(vnf_src_name, vnf_dst_name, cmd)
+
+ def _set_flow_entry_ryu_rest(self, node, switch_inport_nr, switch_outport_nr, **kwargs):
+ match = 'in_port=%s' % switch_inport_nr
+
+ cookie = kwargs.get('cookie')
+ match_input = kwargs.get('match')
+ cmd = kwargs.get('cmd')
+ path = kwargs.get('path')
+ index = kwargs.get('pathindex')
+
+ vlan = kwargs.get('vlan')
+ priority = kwargs.get('priority')
+ # flag to not set the ovs port vlan tag
+ skip_vlan_tag = kwargs.get('skip_vlan_tag')
+ # table id to put this flowentry
+ table_id = kwargs.get('table_id')
+ if not table_id:
+ table_id = 0
+
+ s = ','
+ if match_input:
+ match = s.join([match, match_input])
+
+ flow = {}
+ flow['dpid'] = int(node.dpid, 16)
+
+ if cookie:
+ flow['cookie'] = int(cookie)
+ if priority:
+ flow['priority'] = int(priority)
+
+ flow['table_id'] = table_id
+
+ flow['actions'] = []
+
+ # possible Ryu actions, match fields:
+ # http://ryu.readthedocs.io/en/latest/app/ofctl_rest.html#add-a-flow-entry
+ if cmd == 'add-flow':
+ prefix = 'stats/flowentry/add'
+ if vlan != None:
+ if index == 0: # first node
+ # set vlan tag in ovs instance (to isolate E-LANs)
+ if not skip_vlan_tag:
+ in_port_name = kwargs.get('switch_inport_name')
+ self._set_vlan_tag(node, in_port_name, vlan)
+ # set vlan push action if more than 1 switch in the path
+ if len(path) > 1:
+ action = {}
+ action['type'] = 'PUSH_VLAN' # Push a new VLAN tag if a input frame is non-VLAN-tagged
+ action['ethertype'] = 33024 # Ethertype 0x8100(=33024): IEEE 802.1Q VLAN-tagged frame
+ flow['actions'].append(action)
+ action = {}
+ action['type'] = 'SET_FIELD'
+ action['field'] = 'vlan_vid'
+ # ryu expects the field to be masked
+ action['value'] = vlan | 0x1000
+ flow['actions'].append(action)
+
+ elif index == len(path) - 1: # last node
+ # set vlan tag in ovs instance (to isolate E-LANs)
+ if not skip_vlan_tag:
+ out_port_name = kwargs.get('switch_outport_name')
+ self._set_vlan_tag(node, out_port_name, vlan)
+ # set vlan pop action if more than 1 switch in the path
+ if len(path) > 1:
+ match += ',dl_vlan=%s' % vlan
+ action = {}
+ action['type'] = 'POP_VLAN'
+ flow['actions'].append(action)
+
+ else: # middle nodes
+ match += ',dl_vlan=%s' % vlan
+
+ # output action must come last
+ action = {}
+ action['type'] = 'OUTPUT'
+ action['port'] = switch_outport_nr
+ flow['actions'].append(action)
+
+ elif cmd == 'del-flows':
+ prefix = 'stats/flowentry/delete'
+
+ if cookie:
+ # TODO: add cookie_mask as argument
+ flow['cookie_mask'] = int('0xffffffffffffffff', 16) # need full mask to match complete cookie
+
+ action = {}
+ action['type'] = 'OUTPUT'
+ action['port'] = switch_outport_nr
+ flow['actions'].append(action)
+
+ flow['match'] = self._parse_match(match)
+ self.ryu_REST(prefix, data=flow)
+
+ def _set_vlan_tag(self, node, switch_port, tag):
+ node.vsctl('set', 'port {0} tag={1}'.format(switch_port,tag))
+ LOG.debug("set vlan in switch: {0} in_port: {1} vlan tag: {2}".format(node.name, switch_port, tag))
+
+ def _set_flow_entry_dpctl(self, node, switch_inport_nr, switch_outport_nr, **kwargs):
+
+ match = 'in_port=%s' % switch_inport_nr
+
+ cookie = kwargs.get('cookie')
+ match_input = kwargs.get('match')
+ cmd = kwargs.get('cmd')
+ path = kwargs.get('path')
+ index = kwargs.get('pathindex')
+ vlan = kwargs.get('vlan')
+
+ s = ','
+ if cookie:
+ cookie = 'cookie=%s' % cookie
+ match = s.join([cookie, match])
+ if match_input:
+ match = s.join([match, match_input])
+ if cmd == 'add-flow':
+ action = 'action=%s' % switch_outport_nr
+ if vlan != None:
+ if index == 0: # first node
+ action = ('action=mod_vlan_vid:%s' % vlan) + (',output=%s' % switch_outport_nr)
+ match = '-O OpenFlow13 ' + match
+ elif index == len(path) - 1: # last node
+ match += ',dl_vlan=%s' % vlan
+ action = 'action=strip_vlan,output=%s' % switch_outport_nr
+ else: # middle nodes
+ match += ',dl_vlan=%s' % vlan
+ ofcmd = s.join([match, action])
+ elif cmd == 'del-flows':
+ ofcmd = match
+ else:
+ ofcmd = ''
+
+ node.dpctl(cmd, ofcmd)
+ LOG.info("{3} in switch: {0} in_port: {1} out_port: {2}".format(node.name, switch_inport_nr,
+ switch_outport_nr, cmd))