示例#1
0
    def sendNixPacket(self, ofproto, parser, srcSwitch, sdnNix, msg, src_ip, dst_ip, po=True):
        actions = []
        out_port = 0
        first = 1
        for curNix in sdnNix:
            if curNix[0].dp.id == srcSwitch.dp.id:
                # Save the output port from the source switch
                out_port = curNix[1]
            else:
                #self.logger.info ("Switch %s send out port %s", curNix[0].dp.id, curNix[1])
                actions.append(parser.OFPActionPushMpls())
                actions.append(parser.OFPActionSetField(mpls_ttl=64))
                actions.append(parser.OFPActionSetField(mpls_label=curNix[1]))
        actions.append(parser.OFPActionOutput(out_port))

        inst = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions)]
        match = parser.OFPMatch()
        match.append_field(ofproto.OXM_OF_ETH_TYPE, ether_types.ETH_TYPE_IP)
        # Hack for GENI since it gives the "wrong" MAC addresses... Will still work fine for ns-3
        match.append_field(ofproto.OXM_OF_IPV4_SRC, ip.ipv4_to_int(src_ip), ip.text_to_int("255.255.0.0"))
        match.append_field(ofproto.OXM_OF_IPV4_DST, ip.ipv4_to_int(dst_ip), ip.text_to_int("255.255.0.0"))
        mod = parser.OFPFlowMod(datapath=srcSwitch.dp, priority=10, table_id=0,
                                match=match, instructions=inst)
        srcSwitch.dp.send_msg(mod)

        if po == True:
            data = None
            if msg.buffer_id == ofproto.OFP_NO_BUFFER:
                data = msg.data

            out = parser.OFPPacketOut(datapath=srcSwitch.dp, buffer_id=msg.buffer_id,
                                      in_port=msg.match['in_port'],
                                      actions=actions, data=data)
            srcSwitch.dp.send_msg(out)
示例#2
0
    def _same_subnet(self, alpha, bravo, smask):

        alpha_int = alpha if type(alpha) == type(
            int()) else ip.ipv4_to_int(alpha)
        bravo_int = bravo if type(bravo) == type(
            int()) else ip.ipv4_to_int(bravo)
        smask_int = smask if type(smask) == type(
            int()) else ip.ipv4_to_int(smask)

        if (alpha_int ^ bravo_int) & smask_int:
            print(alpha + ' and ' + bravo + ' are not in the same subnet: ' +
                  smask)

        return (alpha_int ^ bravo_int) & smask_int == 0
示例#3
0
def from_inet_ptoi(bgp_id):
    """Convert an IPv4 address string format to a four byte long.
    """
    four_byte_id = None
    try:
        four_byte_id = ip.ipv4_to_int(bgp_id)
    except ValueError:
        LOG.debug('Invalid bgp id given for conversion to integer value %s',
                  bgp_id)

    return four_byte_id
示例#4
0
def from_inet_ptoi(bgp_id):
    """Convert an IPv4 address string format to a four byte long.
    """
    four_byte_id = None
    try:
        four_byte_id = ip.ipv4_to_int(bgp_id)
    except ValueError:
        LOG.debug('Invalid bgp id given for conversion to integer value %s',
                  bgp_id)

    return four_byte_id
    def add_flow(self, datapath, in_port, src_ip, dst_ip, actions, eth):

        ofproto = datapath.ofproto

        match = datapath.ofproto_parser.OFPMatch(
            in_port=in_port,
            nw_src=ipv4_to_int(src_ip),
            nw_dst=ipv4_to_int(dst_ip),
            dl_type=ether_types.ETH_TYPE_IP or ether_types.ETH_TYPE_ARP)

        mod = datapath.ofproto_parser.OFPFlowMod(
            datapath=datapath,
            match=match,
            cookie=0,
            command=ofproto.OFPFC_ADD,
            idle_timeout=0,
            hard_timeout=0,
            priority=ofproto.OFP_DEFAULT_PRIORITY,
            flags=ofproto.OFPFF_SEND_FLOW_REM,
            actions=actions)

        datapath.send_msg(mod)
	def add_flow(self, datapath, in_port, src_ip, dst_ip, actions, eth):
        
		ofproto = datapath.ofproto
        
		match = datapath.ofproto_parser.OFPMatch(
            
			in_port=in_port, nw_src=ipv4_to_int(src_ip), nw_dst=ipv4_to_int(dst_ip), dl_type = 
			ether_types.ETH_TYPE_IP or ether_types.ETH_TYPE_ARP )         

		mod = datapath.ofproto_parser.OFPFlowMod(
            
		      datapath=datapath, match=match, cookie=0,
            
	              command=ofproto.OFPFC_ADD, idle_timeout=0, hard_timeout=0,
            
		      priority=ofproto.OFP_DEFAULT_PRIORITY,
            
	              flags=ofproto.OFPFF_SEND_FLOW_REM, actions=actions)
        
       

		datapath.send_msg(mod) 
示例#7
0
    def test_ipv4_to_int(self):
        ipv4_str = '10.28.197.1'
        val = 169657601

        res = ip.ipv4_to_int(ipv4_str)
        eq_(val, res)
示例#8
0
文件: test_ip.py 项目: MrCocoaCat/ryu
    def test_ipv4_to_int(self):
        ipv4_str = '10.28.197.1'
        val = 169657601

        res = ip.ipv4_to_int(ipv4_str)
        eq_(val, res)
示例#9
0
    def installMplsPath(self, path, src_ip, dst_ip, begin_dp, end_dp,
                        begin_in_port, end_out_port, pkt_ipv4):

        path_size = len(path)
        qos_info_dict = self.judge(pkt_ipv4, self.qos_ip_bw_list)
        self.logger.info("qos_info_dict: %s", qos_info_dict)

        if path_size <= 0:
            return
        elif path_size == 1:
            if begin_dp != end_dp:
                return
            if qos_info_dict:
                queue_id = 1
                #a_path = self.spf_path_set[src_ip][dst_ip][bw]
            else:
                queue_id = 0
            #a_path = self.spf_path_set[src_ip][dst_ip]
            dpid = path[0]
            dp = self.switch_dict[dpid]
            match = dp.ofproto_parser.OFPMatch()
            match.set_dl_type(ether.ETH_TYPE_IP)
            match.set_in_port(begin_in_port)

            actions = [
                dp.ofproto_parser.OFPActionSetQueue(queue_id=queue_id),
                dp.ofproto_parser.OFPActionOutput(end_out_port, 0)
            ]
            self.add_flow(dp, match, actions)
            return
        else:
            # mpls operation
            if path[0] != begin_dp:
                return
            if path[path_size - 1] != end_dp:
                return

            # first switch -- push mpls label
            i = 0
            j = 1
            dpid = path[i]
            dpid_next = path[j]
            (prev_port_out,
             next_port_in) = self.link_dp2port_table[(dpid, dpid_next)]

            if qos_info_dict:
                queue_id = 1
            else:
                queue_id = 0

            dp = self.switch_dict[dpid]

            match = dp.ofproto_parser.OFPMatch()
            match.set_dl_type(ether.ETH_TYPE_IP)
            match.set_in_port(begin_in_port)
            match.set_ipv4_dst(ip.ipv4_to_int(dst_ip))

            label = self.get_avaliable_label()
            field = dp.ofproto_parser.OFPMatchField.make(
                dp.ofproto.OXM_OF_MPLS_LABEL, int(label))

            actions = [
                dp.ofproto_parser.OFPActionSetQueue(queue_id=queue_id),
                dp.ofproto_parser.OFPActionPushMpls(ether.ETH_TYPE_MPLS),
                dp.ofproto_parser.OFPActionSetField(field),
                dp.ofproto_parser.OFPActionOutput(prev_port_out.port_no, 0)
            ]

            print "Push Label Number = %s." % label
            self.add_flow(dp, match, actions, 0xff)

            # mediate switch -- switch mpls label
            i = i + 1
            j = j + 1
            #tmp_label = begin_label
            while path[i] != end_dp:
                dpid = path[i]
                dpid_next = path[j]
                (prev_port_out,
                 next_port_in) = self.link_dp2port_table[(dpid, dpid_next)]

                if qos_info_dict:
                    queue_id = 1
                else:
                    queue_id = 0

                dp = self.switch_dict[dpid]
                match = dp.ofproto_parser.OFPMatch()
                match.set_dl_type(ether.ETH_TYPE_MPLS)
                match.set_mpls_label(label)

                flabel = self.get_avaliable_label()
                field = dp.ofproto_parser.OFPMatchField.make(
                    dp.ofproto.OXM_OF_MPLS_LABEL, int(label))
                #self.forward_label_table[tmp_label] = flabel

                actions = [
                    dp.ofproto_parser.OFPActionSetQueue(queue_id=queue_id),
                    dp.ofproto_parser.OFPActionPopMpls(ether.ETH_TYPE_IP),
                    dp.ofproto_parser.OFPActionPushMpls(ether.ETH_TYPE_MPLS),
                    dp.ofproto_parser.OFPActionSetField(field),
                    dp.ofproto_parser.OFPActionOutput(prev_port_out.port_no, 0)
                ]
                #print "Swap Label Number = %s -> %s." % (tmp_label, flabel)
                print "Swap Label Number = %s -> %s." % (label, label)
                self.add_flow(dp, match, actions, 0xff)

                #tmp_label = flabel
                i = i + 1
                j = j + 1

            # last switch -- pop mpls label
            if qos_info_dict:
                queue_id = 1
            else:
                queue_id = 0
            dpid = end_dp
            dp = self.switch_dict[dpid]
            match = dp.ofproto_parser.OFPMatch()
            match.set_dl_type(ether.ETH_TYPE_MPLS)
            match.set_mpls_label(label)
            actions = [
                dp.ofproto_parser.OFPActionSetQueue(queue_id=queue_id),
                dp.ofproto_parser.OFPActionPopMpls(ether.ETH_TYPE_IP),
                dp.ofproto_parser.OFPActionOutput(end_out_port, 0)
            ]
            print "Pop Label Number = %s." % label
            self.add_flow(dp, match, actions, 0xff)
示例#10
0
    def match(flow, pkt, in_port, ofp, in_phy_port=None):
        """
        
            Args:
                flow: 
                pkt: 
                in_port: 
                ofp: 
                in_phy_port: 
    
            Returns:

        """
        eth = pkt[0]
        vlan = pkt[1]

        print flow
        if 'in_port' in flow:
            if flow['in_port'] != in_port:
                return False

        if 'in_phy_port' in flow:
            if flow['in_phy_port'] != in_phy_port:
                return False

        if 'eth_dst' in flow:
            eth_dst = flow['eth_dst']
            if isinstance(eth_dst, tuple):
                edst, mask = map(mac.haddr_to_int, eth_dst)
                if edst & mask != mac.haddr_to_int(eth.dst) & mask:
                    return False
            else:
                if eth_dst != eth.dst:
                    return False

        if 'eth_src' in flow:
            eth_src = flow['eth_src']
            if isinstance(eth_src, tuple):
                esrc, mask = map(mac.haddr_to_int, eth_src)
                if esrc & mask != mac.haddr_to_int(eth.src) & mask:
                    return False
            else:
                if eth_src != eth.src:
                    return False

        if 'eth_type' in flow:
            if flow['eth_type'] != vlan.ethertype:
                return False

        def test_vid(vid):
            if vid == ofp.OFPVID_NONE and vlan.vid:
                return False
            if vid != vlan.vid | ofp.OFPVID_PRESENT:
                return False
            return True

        if 'vlan_vid' in flow:
            vlan_vid = flow['vlan_vid']
            if isinstance(vlan_vid, tuple):
                vid, mask = vlan_vid
                if vid == mask == ofp.OFPVID_PRESENT and not vlan.vid:
                    return False
                else:
                    test_vid(vid)
            else:
                vid = vlan_vid
                if not test_vid(vid):
                    return False

        if 'vlan_pcp' in flow:
            if flow['vlan_pcp'] != vlan.vid:
                return False

        ip_dscp = None
        ip_ecn = None
        ip_proto = None

        if vlan.ethertype == 0x0800:
            ip4 = pkt[2]
            tp = pkt[3]

            ip_dscp = ip4.tos >> 2
            ip_ecn = ip4.tos & 0x03
            ip_proto = ip4.proto

            if 'ipv4_src' in flow:
                ipv4_src = flow['ipv4_src']
                if isinstance(ipv4_src, tuple):
                    ipsrc, mask = map(ip.ipv4_to_int, ipv4_src)
                    if ipsrc & mask != ip.ipv4_to_int(ip4.src) & mask:
                        return False
                else:
                    if ipv4_src != ip4.src:
                        return False

            if 'ipv4_dst' in flow:
                ipv4_dst = flow['ipv4_dst']
                if isinstance(ipv4_dst, tuple):
                    ipsrc, mask = map(ip.ipv4_to_int, ipv4_dst)
                    if ipsrc & mask != ip.ipv4_to_int(ip4.dst) & mask:
                        return False
                else:
                    if ipv4_dst != ip4.dst:
                        return False

        if vlan.ethertype == 0x86dd:
            ip6 = pkt[2]
            tp = pkt[3]

            ip_dscp = ip6.traffic_class >> 2
            ip_ecn = ip6.traffic_class & 0x03
            ip_proto = ip6.nxt

            if 'ipv6_src' in flow:
                ipv6_src = flow['ipv6_src']
                if isinstance(ipv6_src, tuple):
                    ipsrc, mask = map(ip.ipv6_to_int, ipv6_src)
                    if ipsrc & mask != ip.ipv6_to_int(ip6.src) & mask:
                        return False
                else:
                    if ipv6_src != ip6.src:
                        return False

            if 'ipv6_dst' in flow:
                ipv6_dst = flow['ipv64_dst']
                if isinstance(ipv6_dst, tuple):
                    ipsrc, mask = map(ip.ipv6_to_int, ipv6_dst)
                    if ipsrc & mask != ip.ipv6_to_int(ip6.dst) & mask:
                        return False
                else:
                    if ipv6_dst != ip6.dst:
                        return False

        if 'ip_dscp' in flow and ip_dscp is not None:
            if flow['ip_dscp'] != ip_dscp:
                return False

        if 'ip_ecn' in flow and ip_ecn is not None:
            if flow['ip_ecn'] != ip_ecn:
                return False

        if 'ip_proto' in flow and ip_proto is not None:
            if flow['ip_proto'] != ip_proto:
                return False

        if ip_proto == 6:  # TCP
            if 'tcp_src' in flow:
                if flow['tcp_src'] != tp.src_port:
                    return False

            if 'tcp_dst' in flow:
                if flow['tcp_dst'] != tp.dst_port:
                    return False

        if ip_proto == 17:  # UDP
            if 'udp_src' in flow:
                if flow['udp_src'] != tp.src_port:
                    return False

            if 'udp_dst' in flow:
                if flow['udp_dst'] != tp.dst_port:
                    return False

        if ip_proto == 132:  # SCTP
            if 'sctp_src' in flow:
                if flow['sctp_src'] != tp.src_port:
                    return False

            if 'sctp_dst' in flow:
                if flow['sctp_dst'] != tp.dst_port:
                    return False

        return True
示例#11
0
    def match(flow, pkt, in_port, ofp):
        eth = pkt[0]
        vlan = pkt[1]

        if not flow.wildcards & ofp.OFPFW_IN_PORT:
            if flow.in_port != in_port:
                return False

        if not flow.wildcards & ofp.OFPFW_DL_VLAN_PCP:
            if flow.dl_vlan_pcp != vlan.pcp:
                return False

        if not flow.wildcards & ofp.OFPFW_DL_VLAN:
            if flow.dl_vlan != vlan.vid:
                return False

        if not flow.wildcards & ofp.OFPFW_DL_SRC:
            if flow.dl_src != addrconv.mac.text_to_bin(eth.src):
                return False

        if not flow.wildcards & ofp.OFPFW_DL_DST:
            if flow.dl_dst != addrconv.mac.text_to_bin(eth.dst):
                return False

        if not flow.wildcards & ofp.OFPFW_DL_TYPE:
            if flow.dl_type != vlan.ethertype:
                return False

        if vlan.ethertype == 0x0800:
            ipp = pkt[2]
            tp = pkt[3]

            ip_src = ip.ipv4_to_int(ipp.src)
            if ip_src != 0:
                mask = (flow.wildcards
                        & ofp.OFPFW_NW_SRC_MASK) >> ofp.OFPFW_NW_SRC_SHIFT
                if mask > 32:
                    mask = 32
                mask = (0xffffffff << mask) & 0xffffffff
                if ip_src & mask != flow.nw_src & mask:
                    return False

            ip_dst = ip.ipv4_to_int(ipp.dst)
            if ip_dst != 0:
                mask = (flow.wildcards
                        & ofp.OFPFW_NW_DST_MASK) >> ofp.OFPFW_NW_DST_SHIFT
                if mask > 32:
                    mask = 32
                mask = (0xffffffff << mask) & 0xffffffff
                if ip_dst & mask != flow.nw_dst & mask:
                    return False

            if not flow.wildcards & ofp.OFPFW_NW_TOS:
                if flow.nw_tos != ipp.tos:
                    return False

            if not flow.wildcards & ofp.OFPFW_NW_PROTO:
                if flow.nw_proto != ipp.proto:
                    return False

            if not flow.wildcards & ofp.OFPFW_TP_SRC:
                if flow.tp_src != tp.src:
                    return False

            if not flow.wildcards & ofp.OFPFW_TP_DST:
                if flow.tp_dst != tp.dst:
                    return False

        return True