def flow_adjust(net, path, flow):
    for node in path:

        index = path.index(node)
        if index != 0 and index != len(path)-1 and index != len(path)-2:
            print node
            parser = node.ofproto_parser
            if flow.ip_proto == inet.IPPROTO_TCP:
                match = parser.OFPMatch(eth_src=flow.src_mac,
                                        eth_dst=flow.dst_mac,
                                        eth_type=ether.ETH_TYPE_IP,
                                        ipv4_src=flow.src_ip,
                                        ipv4_dst=flow.dst_ip,
                                        ip_proto=flow.ip_proto,
                                        tcp_src=flow.src_port,
                                        tcp_dst=flow.dst_port)
            else:
                match = parser.OFPMatch(eth_src=flow.src_mac,
                                        eth_dst=flow.dst_mac,
                                        eth_type=ether.ETH_TYPE_IP,
                                        ipv4_src=flow.src_ip,
                                        ipv4_dst=flow.dst_ip,
                                        ip_proto=flow.ip_proto,
                                        udp_src=flow.src_port,
                                        udp_dst=flow.dst_port)
            priority = 10
            out_port = net[node.id][path[index+1].id]['port']
            actions = [parser.OFPActionOutput(out_port)]
            add_flow(node, priority, match, actions)
Beispiel #2
0
 def _handle_arp(self, msg, datapath, port, pkt_ethernet, pkt_arp):
     """Handle ARP Setting method."""
     tuple_m = (datapath.id, port)
     # print datapath.id, port
     if tuple_m not in data_collection.switch_inner_port:
         self._handle_member_info(datapath, port, pkt_ethernet, pkt_arp)
     parser = datapath.ofproto_parser
     if pkt_arp.opcode == arp.ARP_REPLY:
         group = data_collection.group_list.get('whole')
         net = group.topology
         dst = data_collection.member_list.get(pkt_arp.dst_mac)
         if dst is not None:
             # print dst.port, dst.datapath
             arp_path = self._generate_path(net, pkt_ethernet.src,
                                            pkt_ethernet.dst, port,
                                            dst.port, datapath.id,
                                            dst.datapath.id)
             for next_n in arp_path:
                 index = arp_path.index(next_n)
                 if index != 0 and index != len(arp_path) - 1:
                     out_port = None
                     if index == len(arp_path) - 2:
                         out_port = dst.port
                     else:
                         out_port = net[next_n][arp_path[index + 1]]['port']
                     actions = [
                         datapath.ofproto_parser.OFPActionOutput(out_port)
                     ]
                     out_datapath = get_switch(self.topology_api_app,
                                               dpid=next_n)
                     match = parser.OFPMatch(eth_src=pkt_ethernet.src,
                                             eth_dst=pkt_ethernet.dst,
                                             eth_type=ether.ETH_TYPE_ARP,
                                             arp_op=arp.ARP_REPLY)
                     ofputils.add_flow(out_datapath[0].dp, 10, match,
                                       actions)
             actions_o = [parser.OFPActionOutput(dst.port)]
             datapath_o = dst.datapath
             data = None
             if msg.buffer_id == datapath.ofproto.OFP_NO_BUFFER:
                 data = msg.data
             out = datapath_o.ofproto_parser.OFPPacketOut(
                 datapath=datapath_o,
                 buffer_id=msg.buffer_id,
                 in_port=msg.match['in_port'],
                 actions=actions_o,
                 data=data)
             datapath_o.send_msg(out)
         else:
             self._broadcast_pkt(msg)
     else:
         self._broadcast_pkt(msg)
Beispiel #3
0
 def _handle_arp(self, msg, datapath, port, pkt_ethernet, pkt_arp):
     """Handle ARP Setting method."""
     tuple_m = (datapath.id, port)
     # print datapath.id, port
     if tuple_m not in data_collection.switch_inner_port:
         self._handle_member_info(datapath, port, pkt_ethernet, pkt_arp)
     parser = datapath.ofproto_parser
     if pkt_arp.opcode == arp.ARP_REPLY:
         group = data_collection.group_list.get('whole')
         net = group.topology
         dst = data_collection.member_list.get(pkt_arp.dst_mac)
         if dst is not None:
             # print dst.port, dst.datapath
             arp_path = self._generate_path(net, pkt_ethernet.src,
                                            pkt_ethernet.dst, port,
                                            dst.port, datapath.id,
                                            dst.datapath.id)
             for next_n in arp_path:
                 index = arp_path.index(next_n)
                 if index != 0 and index != len(arp_path)-1:
                     out_port = None
                     if index == len(arp_path)-2:
                         out_port = dst.port
                     else:
                         out_port = net[next_n][arp_path[index+1]]['port']
                     actions = [datapath.ofproto_parser.
                                OFPActionOutput(out_port)]
                     out_datapath = get_switch(self.topology_api_app,
                                               dpid=next_n)
                     match = parser.OFPMatch(eth_src=pkt_ethernet.src,
                                             eth_dst=pkt_ethernet.dst,
                                             eth_type=ether.ETH_TYPE_ARP,
                                             arp_op=arp.ARP_REPLY)
                     ofputils.add_flow(out_datapath[0].dp, 10, match,
                                       actions)
             actions_o = [parser.OFPActionOutput(dst.port)]
             datapath_o = dst.datapath
             data = None
             if msg.buffer_id == datapath.ofproto.OFP_NO_BUFFER:
                 data = msg.data
             out = datapath_o.ofproto_parser.OFPPacketOut(datapath=datapath_o,
                                       buffer_id=msg.buffer_id,
                                       in_port=msg.match['in_port'],
                                       actions=actions_o,
                                       data=data)
             datapath_o.send_msg(out)
         else:
             self._broadcast_pkt(msg)
     else:
         self._broadcast_pkt(msg)
Beispiel #4
0
    def _handle_ipv4(self, msg, datapath, port, pkt_ethernet, pkt_ipv4, pkt,
                     dst_mac, group_id):
        # print 'ipv4', group_id
        parser = datapath.ofproto_parser
        group = data_collection.group_list.get(group_id)
        net = group.topology
        m_dst = data_collection.member_list.get(dst_mac)
        # print 'm_dst', m_dst
        if m_dst is not None:

            ipv4_path = self._generate_path(net, pkt_ethernet.src,
                                            pkt_ethernet.dst, port, m_dst.port,
                                            datapath.id, m_dst.datapath.id)
            for next_n in ipv4_path:
                index = ipv4_path.index(next_n)
                if index != 0 and index != len(ipv4_path) - 1:
                    out_port = None
                    net = group.topology
                    if index == len(ipv4_path) - 2:
                        out_port = m_dst.port
                    else:
                        out_port = net[next_n][ipv4_path[index + 1]]['port']

                    if index == 1:
                        out_port2 = port
                    else:
                        out_port2 = net[next_n][ipv4_path[index - 1]]['port']

                    actions = [parser.OFPActionOutput(out_port)]
                    actions2 = [parser.OFPActionOutput(out_port2)]
                    out_datapath = get_switch(self.topology_api_app,
                                              dpid=next_n)
                    if pkt_ipv4.proto == inet.IPPROTO_TCP:
                        # print 'tcp'
                        pkt_tcp = pkt.get_protocol(tcp.tcp)
                        match = parser.OFPMatch(eth_src=pkt_ethernet.src,
                                                eth_dst=pkt_ethernet.dst,
                                                eth_type=ether.ETH_TYPE_IP,
                                                ipv4_src=pkt_ipv4.src,
                                                ipv4_dst=pkt_ipv4.dst,
                                                ip_proto=pkt_ipv4.proto,
                                                tcp_src=pkt_tcp.src_port,
                                                tcp_dst=pkt_tcp.dst_port)
                        match2 = parser.OFPMatch(eth_src=pkt_ethernet.dst,
                                                 eth_dst=pkt_ethernet.src,
                                                 eth_type=ether.ETH_TYPE_IP,
                                                 ipv4_src=pkt_ipv4.dst,
                                                 ipv4_dst=pkt_ipv4.src,
                                                 ip_proto=pkt_ipv4.proto,
                                                 tcp_src=pkt_tcp.dst_port,
                                                 tcp_dst=pkt_tcp.src_port)
                    elif pkt_ipv4.proto == inet.IPPROTO_UDP:
                        # print 'udp'
                        pkt_udp = pkt.get_protocol(udp.udp)
                        match = parser.OFPMatch(eth_src=pkt_ethernet.src,
                                                eth_dst=pkt_ethernet.dst,
                                                eth_type=ether.ETH_TYPE_IP,
                                                ipv4_src=pkt_ipv4.src,
                                                ipv4_dst=pkt_ipv4.dst,
                                                ip_proto=pkt_ipv4.proto,
                                                udp_src=pkt_udp.src_port,
                                                udp_dst=pkt_udp.dst_port)
                        match2 = parser.OFPMatch(eth_src=pkt_ethernet.dst,
                                                 eth_dst=pkt_ethernet.src,
                                                 eth_type=ether.ETH_TYPE_IP,
                                                 ipv4_src=pkt_ipv4.dst,
                                                 ipv4_dst=pkt_ipv4.src,
                                                 ip_proto=pkt_ipv4.proto,
                                                 udp_src=pkt_udp.dst_port,
                                                 udp_dst=pkt_udp.src_port)
                    else:
                        # print 'icmp'
                        match = parser.OFPMatch(eth_src=pkt_ethernet.src,
                                                eth_dst=pkt_ethernet.dst,
                                                eth_type=ether.ETH_TYPE_IP,
                                                ipv4_src=pkt_ipv4.src,
                                                ipv4_dst=pkt_ipv4.dst)
                        match2 = parser.OFPMatch(eth_src=pkt_ethernet.dst,
                                                 eth_dst=pkt_ethernet.src,
                                                 eth_type=ether.ETH_TYPE_IP,
                                                 ipv4_src=pkt_ipv4.dst,
                                                 ipv4_dst=pkt_ipv4.src)

                    ofputils.add_flow(out_datapath[0].dp, 10, match, actions)
                    ofputils.add_flow(out_datapath[0].dp, 10, match2, actions2)

                    if datapath.id == out_datapath[0].dp.id:
                        actions_o = actions
                        datapath_o = datapath
                        data = None
                        if msg.buffer_id == datapath.ofproto.OFP_NO_BUFFER:
                            data = msg.data
                        out = parser.OFPPacketOut(datapath=datapath_o,
                                                  buffer_id=msg.buffer_id,
                                                  in_port=msg.match['in_port'],
                                                  actions=actions_o,
                                                  data=data)
                        datapath_o.send_msg(out)
        else:
            self._broadcast_pkt(msg)
Beispiel #5
0
    def _handle_ipv4(self, msg, datapath, port, pkt_ethernet, pkt_ipv4, pkt,
                     dst_mac, group_id):
        # print 'ipv4', group_id
        parser = datapath.ofproto_parser
        group = data_collection.group_list.get(group_id)
        net = group.topology
        m_dst = data_collection.member_list.get(dst_mac)
        # print 'm_dst', m_dst
        if m_dst is not None:
            ipv4_path = self._generate_path(net,
                                            pkt_ethernet.src, pkt_ethernet.dst,
                                            port, m_dst.port,
                                            datapath.id, m_dst.datapath.id)
            for next_n in ipv4_path:
                index = ipv4_path.index(next_n)
                if index != 0 and index != len(ipv4_path)-1:
                    out_port = None
                    net = group.topology
                    if index == len(ipv4_path)-2:
                        out_port = m_dst.port
                    else:
                        out_port = net[next_n][ipv4_path[index+1]]['port']

                    if index == 1:
                        out_port2 = port
                    else:
                        out_port2 = net[next_n][ipv4_path[index-1]]['port']

                    actions = [parser.OFPActionOutput(out_port)]
                    actions2 = [parser.OFPActionOutput(out_port2)]
                    out_datapath = get_switch(self.topology_api_app, dpid=next_n)
                    if pkt_ipv4.proto == inet.IPPROTO_TCP:
                        # print 'tcp'
                        pkt_tcp = pkt.get_protocol(tcp.tcp)
                        match = parser.OFPMatch(eth_src=pkt_ethernet.src,
                                                eth_dst=pkt_ethernet.dst,
                                                eth_type=ether.ETH_TYPE_IP,
                                                ipv4_src=pkt_ipv4.src,
                                                ipv4_dst=pkt_ipv4.dst,
                                                ip_proto=pkt_ipv4.proto,
                                                tcp_src=pkt_tcp.src_port,
                                                tcp_dst=pkt_tcp.dst_port)
                        match2 = parser.OFPMatch(eth_src=pkt_ethernet.dst,
                                                 eth_dst=pkt_ethernet.src,
                                                 eth_type=ether.ETH_TYPE_IP,
                                                 ipv4_src=pkt_ipv4.dst,
                                                 ipv4_dst=pkt_ipv4.src,
                                                 ip_proto=pkt_ipv4.proto,
                                                 tcp_src=pkt_tcp.dst_port,
                                                 tcp_dst=pkt_tcp.src_port)
                    elif pkt_ipv4.proto == inet.IPPROTO_UDP:
                        # print 'udp'
                        pkt_udp = pkt.get_protocol(udp.udp)
                        match = parser.OFPMatch(eth_src=pkt_ethernet.src,
                                                eth_dst=pkt_ethernet.dst,
                                                eth_type=ether.ETH_TYPE_IP,
                                                ipv4_src=pkt_ipv4.src,
                                                ipv4_dst=pkt_ipv4.dst,
                                                ip_proto=pkt_ipv4.proto,
                                                udp_src=pkt_udp.src_port,
                                                udp_dst=pkt_udp.dst_port)
                        match2 = parser.OFPMatch(eth_src=pkt_ethernet.dst,
                                                 eth_dst=pkt_ethernet.src,
                                                 eth_type=ether.ETH_TYPE_IP,
                                                 ipv4_src=pkt_ipv4.dst,
                                                 ipv4_dst=pkt_ipv4.src,
                                                 ip_proto=pkt_ipv4.proto,
                                                 udp_src=pkt_udp.dst_port,
                                                 udp_dst=pkt_udp.src_port)
                    else:
                        # print 'icmp'
                        match = parser.OFPMatch(eth_src=pkt_ethernet.src,
                                                eth_dst=pkt_ethernet.dst,
                                                eth_type=ether.ETH_TYPE_IP,
                                                ipv4_src=pkt_ipv4.src,
                                                ipv4_dst=pkt_ipv4.dst)
                        match2 = parser.OFPMatch(eth_src=pkt_ethernet.dst,
                                                 eth_dst=pkt_ethernet.src,
                                                 eth_type=ether.ETH_TYPE_IP,
                                                 ipv4_src=pkt_ipv4.dst,
                                                 ipv4_dst=pkt_ipv4.src)

                    ofputils.add_flow(out_datapath[0].dp, 10, match, actions)
                    ofputils.add_flow(out_datapath[0].dp, 10, match2, actions2)
                    if datapath.id == out_datapath[0].dp.id:
                        actions_o = actions
                        datapath_o = datapath
                        data = None
                        if msg.buffer_id == datapath.ofproto.OFP_NO_BUFFER:
                            data = msg.data
                        out = parser.OFPPacketOut(datapath=datapath_o,
                                                  buffer_id=msg.buffer_id,
                                                  in_port=msg.match['in_port'],
                                                  actions=actions_o,
                                                  data=data)
                        datapath_o.send_msg(out)
        else:
            self._broadcast_pkt(msg)