def inject_vtag_match_flow(self,cookie,src_port_id,serverIp, forward_port,vid, dl_type = ethernet.IP_TYPE, nw_proto = ipv4.ICMP_PROTOCOL, new_src_ip=None,new_dst_ip=None,new_dst_mac=None, ): msg = of.ofp_flow_mod() msg.match.in_port=src_port_id if dl_type: msg.match.dl_type = dl_type msg.match.nw_proto = nw_proto msg.match.nw_src =serverIp target_forward_port = forward_port '''set new src IP''' if new_src_ip: msg.actions.append(of.ofp_action_nw_addr.set_src(IPAddr(new_src_ip))) '''set new dst IP''' if new_dst_ip: msg.actions.append(of.ofp_action_nw_addr.set_dst(IPAddr(new_dst_ip))) if new_dst_mac: msg.actions.append(of.ofp_action_dl_addr.set_dst(EthAddr(new_dst_mac))) '''add VLAN ID''' if vid: msg.actions.append(of.ofp_action_set_vlan_vid(vlan_vid=vid)) msg.actions.append(of.ofp_action_output(port = forward_port)) msg.cookie = cookie self.connection.send(msg)
def f_nat_forward_packet(self, vid, packet_in, new_dst_ip, new_dst_mac, forward_port, new_src_ip=None): msg = of.ofp_packet_out(in_port=packet_in.in_port) msg.data = packet_in '''set new src IP''' if new_src_ip: msg.actions.append( of.ofp_action_nw_addr.set_src(IPAddr(new_src_ip))) '''set new dst IP''' if new_dst_ip: msg.actions.append( of.ofp_action_nw_addr.set_dst(IPAddr(new_dst_ip))) if new_dst_mac: msg.actions.append( of.ofp_action_dl_addr.set_dst(EthAddr(new_dst_mac))) '''add VLAN ID''' if vid: msg.actions.append(of.ofp_action_set_vlan_vid(vlan_vid=vid)) msg.actions.append(of.ofp_action_output(port=forward_port)) #msg.buffer_id = <some buffer id, if any> msg.cookie = 100 self.connection.send(msg)
def f_add_vid_forward_packet(self,vid,packet_in,forward_port,new_srcIP=None): msg = of.ofp_packet_out(in_port=packet_in.in_port) msg.data = packet_in '''change DST IP''' if new_srcIP: msg.actions.append(of.ofp_action_nw_addr.set_src(IPAddr(new_srcIP))) '''add VLAN ID''' if vid: msg.actions.append(of.ofp_action_set_vlan_vid(vlan_vid=vid)) msg.actions.append(of.ofp_action_output(port = forward_port)) #msg.buffer_id = <some buffer id, if any> msg.cookie = 200 self.connection.send(msg)
def f_nat_forward_packet(self,vid,packet_in,new_dst_ip,new_dst_mac,forward_port,new_src_ip=None): msg = of.ofp_packet_out(in_port=packet_in.in_port) msg.data = packet_in '''set new src IP''' if new_src_ip: msg.actions.append(of.ofp_action_nw_addr.set_src(IPAddr(new_src_ip))) '''set new dst IP''' if new_dst_ip: msg.actions.append(of.ofp_action_nw_addr.set_dst(IPAddr(new_dst_ip))) if new_dst_mac: msg.actions.append(of.ofp_action_dl_addr.set_dst(EthAddr(new_dst_mac))) '''add VLAN ID''' if vid: msg.actions.append(of.ofp_action_set_vlan_vid(vlan_vid=vid)) msg.actions.append(of.ofp_action_output(port = forward_port)) #msg.buffer_id = <some buffer id, if any> msg.cookie = 100 self.connection.send(msg)
def inject_vtag_match_flow( self, cookie, src_port_id, serverIp, forward_port, vid, dl_type=ethernet.IP_TYPE, nw_proto=ipv4.ICMP_PROTOCOL, new_src_ip=None, new_dst_ip=None, new_dst_mac=None, ): msg = of.ofp_flow_mod() msg.match.in_port = src_port_id if dl_type: msg.match.dl_type = dl_type msg.match.nw_proto = nw_proto msg.match.nw_src = serverIp target_forward_port = forward_port '''set new src IP''' if new_src_ip: msg.actions.append( of.ofp_action_nw_addr.set_src(IPAddr(new_src_ip))) '''set new dst IP''' if new_dst_ip: msg.actions.append( of.ofp_action_nw_addr.set_dst(IPAddr(new_dst_ip))) if new_dst_mac: msg.actions.append( of.ofp_action_dl_addr.set_dst(EthAddr(new_dst_mac))) '''add VLAN ID''' if vid: msg.actions.append(of.ofp_action_set_vlan_vid(vlan_vid=vid)) msg.actions.append(of.ofp_action_output(port=forward_port)) msg.cookie = cookie self.connection.send(msg)
def set_hosts (self, host_data): """ Receive list of hosts This gets called with a list of dictionaries that each contain information about a host. Each time this is called, you get a complete list of all current hosts. Each entry looks something like this: {"ether" : "01:02:03:04:05:06", "ip" : "1.2.3.4", "attached_switch" : dpid, "attached_port" : portno}, In a datacenter, you might get this kind of information from a Cloud Management System. In our case, garnet's sync_hosts() sends us the list of Host entities in the "emulated" network garnet is managing. We receive it via the POX Messenger component and the messenger bot above. """ self.last_host_data = host_data for host in host_data: self.log.info("Got host: %s", " ".join("%s=%s" % kv for kv in sorted(host.items()))) host_e = str(host['ether']) switch_dpid = host['attached_switch'] switch_port = host['attached_port'] switch_name = self.graph.names[switch_dpid] self.hosts[host_e] = switch_dpid self.edge[switch_dpid] = switch_port if host_e in self.graph: self.graph.remove_node(host_e) # alter table info on attached switch # add host to networkX graph attached_switch = self.graph.names[switch_dpid] self.graph.add_edge(host_e, attached_switch) self.graph.add_edge(attached_switch, host_e) port_dict = {'ports': {attached_switch: switch_port}} self.graph.edge[host_e][attached_switch] = port_dict self.graph.edge[attached_switch][host_e] = port_dict core.openflow.sendToDPID(switch_dpid, nx_flow_mod_table_id()) # Enables multiple tables data = [] # construct command to remove VLAN and output to host fm = ofp_flow_mod_table_id( table_id = 0, match = of01.ofp_match(dl_dst=EthAddr(host_e)), command = of01.OFPFC_MODIFY, actions = [ofp_action_strip_vlan(), ofp_action_output(port=switch_port)]) data.append(fm.pack()) for dst_host, dst_switch_dpid in self.hosts.items(): if dst_host == host_e: continue if not self._connection_is_permitted(host_e, dst_host): # If we're not allowed to send to this host (or this host is not allowed to receive), tell our switch # to drop all traffic going to this host. self.log.info("MatchedDenyACE: src=%s dst=%s" % host_e, dst_host) fm = ofp_flow_mod_table_id( table_id = 0, command = of01.OFPFC_MODIFY, match = of01.ofp_match(dl_src=EthAddr(host_e), dl_dst=EthAddr(dst_host)), actions = None) data.append(fm.pack()) continue dst_switch_name = self.graph.names[dst_switch_dpid] #self.log.info(switch_name + ' ' + dst_switch_name) if switch_name == dst_switch_name: continue try: next_hop = nx.shortest_path(self.graph, source=switch_name, target=dst_switch_name)[1] except: continue shortest_path_port = self.graph[switch_name][next_hop]['ports'][switch_name] #self.log.info(str(host_e) + ' ' + str(dst_host)) #self.log.info(str(dst_switch_dpid) + ' ' + str(shortest_path_port)) # inform attached switch where other hosts are fm = ofp_flow_mod_table_id( table_id = 0, command = of01.OFPFC_MODIFY, match = of01.ofp_match(dl_src=EthAddr(host_e), dl_dst=EthAddr(dst_host)), actions = [ofp_action_set_vlan_vid(vlan_vid=dst_switch_dpid), ofp_action_output(port=shortest_path_port)]) data.append(fm.pack()) core.openflow.sendToDPID(dst_switch_dpid, nx_flow_mod_table_id()) # Enables multiple tables try: next_hop = nx.shortest_path(self.graph, source=dst_switch_name, target=switch_name)[1] except: continue shortest_path_port = self.graph[dst_switch_name][next_hop]['ports'][dst_switch_name] # inform other attached switches where this host is fm = ofp_flow_mod_table_id( table_id = 0, command = of01.OFPFC_MODIFY, match = of01.ofp_match(dl_src=EthAddr(dst_host), dl_dst=EthAddr(host_e)), actions = [ofp_action_set_vlan_vid(vlan_vid=switch_dpid), ofp_action_output(port=shortest_path_port)]) core.openflow.sendToDPID(dst_switch_dpid, fm.pack()) core.openflow.sendToDPID(switch_dpid, b''.join(data)) self.broadcast_paths()