예제 #1
0
 def test_masked_match_from_ryu(self):
     """ Test conversion of arbitrarily masked matches from ryu """
     self.assertEqual(
         match_from_ryu(OFPMatch(ipv4_dst=("192.168.2.0",
                                           "255.255.255.0"))),
         Match([("IPV4_DST", 0xC0A80200, 0xFFFFFF00)]))
     self.assertEqual(
         match_from_ryu(
             OFPMatch(eth_src=("01:00:00:00:00:00", "01:00:00:00:00:00"))),
         Match([("ETH_SRC", 0x010000000000, 0x010000000000)]))
예제 #2
0
 def test_exact_match_from_ryu(self):
     """ Test conversion of an exact match from ryu """
     self.assertEqual(match_from_ryu(OFPMatch(in_port=1)),
                      Match([("IN_PORT", 1, None)]))
     self.assertEqual(match_from_ryu(OFPMatch(in_phy_port=2)),
                      Match([("IN_PHY_PORT", 2, None)]))
     self.assertEqual(match_from_ryu(OFPMatch(metadata=3)),
                      Match([("METADATA", 3, None)]))
     self.assertEqual(match_from_ryu(OFPMatch(eth_src=4)),
                      Match([("ETH_SRC", 4, None)]))
     self.assertEqual(match_from_ryu(OFPMatch(mpls_bos=7)),
                      Match([("MPLS_BOS", 7, None)]))
    def test_Packet_in_2_icmpEcho1(self):
        print "*** Case2: HOST2のMAC未学習の時、HOST1からICMP Echoを受信 ***"

        sr = SimpleForward()
        dstMac = ROUTER_MACADDR1
        srcMac = HOST_MACADDR1
        srcIp = HOST_IPADDR1
        dstIp = HOST_IPADDR2
        targetMac = dstMac
        targetIp = dstIp
        ttl = 64

        datapath = _Datapath()
        e = ethernet(dstMac, srcMac, ether.ETH_TYPE_IP)

        iph = ipv4(4, 5, 0, 0, 0, 2, 0, ttl, 1, 0, srcIp, dstIp)
        echo = icmp.echo(1, 1, 'unit test')
        icmph = icmp.icmp(8, 0, 0, echo)

        p = Packet()
        p.add_protocol(e)
        p.add_protocol(iph)
        p.add_protocol(icmph)
        p.serialize()

        packetIn = OFPPacketIn(datapath,
                               match=OFPMatch(in_port=1),
                               data=buffer(p.data))

        ev = ofp_event.EventOFPPacketIn(packetIn)

        result = sr.packet_in_handler(ev)
        self.assertEqual(result, 1)
        print ""
    def test_Packet_in_4_icmpEcho2(self):
        print "*** Case4: HOST2からHOST1以外の宛先IPへのIPパケットを受信 ***"

        sr = SimpleForward()
        sr.HOST_MACADDR1 = HOST_MACADDR1
        sr.HOST_MACADDR2 = HOST_MACADDR2

        dstMac = ROUTER_MACADDR2
        srcMac = HOST_MACADDR2
        srcIp = HOST_IPADDR2
        dstIp = "91.189.89.22"
        ttl = 64

        datapath = _Datapath()
        e = ethernet(dstMac, srcMac, ether.ETH_TYPE_IP)

        iph = ipv4(4, 5, 0, 0, 0, 2, 0, ttl, 1, 0, srcIp, dstIp)

        p = Packet()
        p.add_protocol(e)
        p.add_protocol(iph)
        p.serialize()

        packetIn = OFPPacketIn(datapath,
                               match=OFPMatch(in_port=2),
                               data=buffer(p.data))

        ev = ofp_event.EventOFPPacketIn(packetIn)

        result = sr.packet_in_handler(ev)
        self.assertEqual(result, 1)
        print ""
예제 #5
0
    def parser(cls, datapath, version, msg_type, msg_len, xid, buf):
        msg = super(OFPFlowMod, cls).parser(
            datapath, version, msg_type,
            msg_len, xid, buf)
        
        (msg.cookie, msg.cookie_mask,
         msg.table_id, msg.command,
         msg.idle_timeout, msg.hard_timeout,
         msg.priority, msg.buffer_id,
         msg.out_port, msg.out_group, msg.flags) = struct.unpack_from(
            ofproto.OFP_FLOW_MOD_PACK_STR0,
            msg.buf,ofproto.OFP_HEADER_SIZE)

        #### DECODE MATCHES ####
        # how far from the top of the message the match is located.
        match_offset = (ofproto.OFP_FLOW_MOD_SIZE -
                  ofproto.OFP_MATCH_SIZE)

        msg.match = OFPMatch.parser(msg.buf,match_offset)

        ##### PARSING INSTRUCTIONS
        match_length = ryu.utils.round_up(msg.match.length, 8)
        instructions_offset = match_offset + match_length
        inst_length = msg_len - instructions_offset
        
        instructions = []
        while inst_length > 0:
            inst = OFPInstruction.parser(msg.buf, instructions_offset)
            instructions.append(inst)
            instructions_offset += inst.len
            inst_length -= inst.len

        return msg
    def test_Packet_in_1_arpRequest(self):
        print "*** Case1: HOST1からARP Request受信 ***"

        sr = SimpleForward()
        dstMac = "ff:ff:ff:ff:ff:ff"
        srcMac = HOST_MACADDR1
        srcIp = HOST_IPADDR1
        dstIp = ROUTER_IPADDR1
        targetMac = dstMac
        targetIp = dstIp

        datapath = _Datapath()
        e = ethernet(dstMac, srcMac, ether.ETH_TYPE_ARP)
        a = arp(1, 0x0800, 6, 4, 1, srcMac, srcIp, targetMac, targetIp)
        p = Packet()
        p.add_protocol(e)
        p.add_protocol(a)
        p.serialize()

        packetIn = OFPPacketIn(datapath,
                               match=OFPMatch(in_port=1),
                               data=buffer(p.data))

        ev = ofp_event.EventOFPPacketIn(packetIn)

        result = sr.packet_in_handler(ev)
        self.assertEqual(result, 0)
        print ""
    def test_Packet_in_3_arpReply2(self):
        print "*** Case3: HOST1のMAC学習済の時、HOST2からARP Replyを受信 ***"

        sr = SimpleForward()
        sr.HOST_MACADDR1 = HOST_MACADDR1
        sr.HOST_MACADDR2 = HOST_MACADDR2

        dstMac = ROUTER_MACADDR2
        srcMac = HOST_MACADDR2
        srcIp = HOST_IPADDR2
        dstIp = ROUTER_IPADDR2
        targetMac = dstMac
        targetIp = dstIp

        datapath = _Datapath()
        e = ethernet(dstMac, srcMac, ether.ETH_TYPE_ARP)
        a = arp(1, 0x0800, 6, 4, 2, srcMac, srcIp, targetMac, targetIp)
        p = Packet()
        p.add_protocol(e)
        p.add_protocol(a)
        p.serialize()

        packetIn = OFPPacketIn(datapath,
                               match=OFPMatch(in_port=2),
                               data=buffer(p.data))

        ev = ofp_event.EventOFPPacketIn(packetIn)

        result = sr.packet_in_handler(ev)
        self.assertEqual(result, 0)
        print ""
예제 #8
0
 def install_color(self, color):
     """
         Prepare to send the FlowMod to install colored flows
         Args:
             color: eth_src to be used
     """
     mac_color = "ee:ee:ee:ee:ee:%s" % int(color, 2)
     self.push_color(OFPMatch(eth_src=mac_color))
예제 #9
0
    def switch_features_handler(self, ev):
        datapath = ev.msg.datapath

        # install the table-miss flow entry.
        match = OFPMatch()
        actions = [OFPActionOutput(ofproto_v1_3.OFPP_CONTROLLER,
                                   ofproto_v1_3.OFPCML_NO_BUFFER)]
        self.add_flow(datapath, 0, match, actions)
예제 #10
0
def create_l3_flow_with_wildcard(datapath,
                                 in_port,
                                 out_port,
                                 ip_pkt_src,
                                 ip_pkt_dst,
                                 msg,
                                 priority=32768,
                                 modify_rule=False):
    ofp = datapath.ofproto
    #    ofp.OFPP_IN_PORT
    ofp_parser = datapath.ofproto_parser

    cookie = cookie_mask = 0
    table_id = 0
    idle_timeout = 30
    #hard_timeout = 15
    hard_timeout = 3600
    #priority = 32768
    buffer_id = ofp.OFP_NO_BUFFER
    data = None
    if msg != None:
        print msg
        buffer_id = msg.buffer_id  # ofp.OFP_NO_BUFFER
        if msg.buffer_id == ofp.OFP_NO_BUFFER:
            data = msg.data
    match = OFPMatch(  #in_port=in_port,
        eth_type=ether_types.ETH_TYPE_IP,  #ipv4_src=ip_pkt_src,
        ipv4_dst=ip_pkt_dst)  #(ip_pkt_dst, "255.255.255.0"))

    actions = []

    actions.append(ofp_parser.OFPActionOutput(port=out_port))

    inst = [ofp_parser.OFPInstructionActions(ofp.OFPIT_APPLY_ACTIONS, actions)]

    add_or_mod_a_rule = ofp.OFPFC_ADD
    if modify_rule:
        add_or_mod_a_rule = ofp.OFPFC_MODIFY

    req = ofp_parser.OFPFlowMod(
        datapath,
        cookie,
        cookie_mask,
        table_id,
        add_or_mod_a_rule,
        idle_timeout,
        hard_timeout,
        priority,
        buffer_id,
        #in_port, ofp.OFPG_ANY, <<<<<<<<<<<<<<<<<<<<<<<<<<<Verficar se estah ok!
        out_port,
        ofp.OFPG_ANY,
        ofp.OFPFF_SEND_FLOW_REM,
        match,
        inst)
    print datapath.id, ":", req
    datapath.send_msg(req)
예제 #11
0
 def prepare_default_flow(self):
     """
         Push our default flow (MAC + LLDP + VLAN)
     """
     # TODO: Commented the ethertype for tests
     # TODO: with Corsa
     match = OFPMatch(
         eth_dst=lldp.LLDP_MAC_NEAREST_BRIDGE,
         # eth_type=ether.ETH_TYPE_LLDP,
         vlan_vid=self.config.topo.vlan_discovery
         | ofproto_v1_3.OFPVID_PRESENT)
     self.add_default_flow(match)
예제 #12
0
    def handle(self, in_port, data):
        pkt = packet.Packet(data)
        eth_pkt = pkt.get_protocol(ethernet.ethernet)

        ports = self.get_forwarding_ports(in_port, eth_pkt)
        self.forward_packet(in_port, ports[0], data)

        for port in ports:
            match = OFPMatch(in_port=in_port, eth_dst=eth_pkt.dst)
            actions = [OFPActionOutput(port)]
            self.add_flow(10, match, actions)
            print 'add flow for %s from port %s to %s' % (self.dp.id,in_port, port)
예제 #13
0
    def handle(self, in_port, data):
        pkt = packet.Packet(data)
        eth_pkt = pkt.get_protocol(ethernet.ethernet)

        self.host_table[eth_pkt.src] = in_port

        if eth_pkt.dst in self.host_table:
            port = self.host_table[eth_pkt.dst]
            match = OFPMatch(in_port=in_port, eth_dst=eth_pkt.dst)
            actions = [OFPActionOutput(port)]
            self.add_flow(10, match, actions)
            self.forward_packet(in_port, port, data)
        else:
            self.forward_packet(in_port, ofproto_v1_3.OFPP_FLOOD, data)
예제 #14
0
    def _handle_switch_features(self, event):
        """
        Installs the default flow rule for every switch that connects
        to the controller
        :param event: The SwitchFeatures event
        :type event: EventOFPSwitchFeatures
        :return: None
        :rtype: None
        """
        datapath = event.msg.datapath
        self.logger.debug("[S{}] SwitchFeatures Event".format(datapath.id))
        self.switches[datapath.id] = datapath

        self.program_flow(datapath, OFPMatch(),
                          [OFPActionOutput(OFPP_CONTROLLER)], 0, 0, 0)
        datapath.send_msg(OFPSetConfig(datapath, OFPC_FRAG_NORMAL, 0xffff))
        self.handle_switch_features(event)
예제 #15
0
    def test_convert_flow(self):
        """ Test converting OFPFlowMod and OFPFlowStats from ryu """
        ryu_match = OFPMatch(ipv4_src=1, eth_type=0x8100, vlan_vid=0x1100)
        ryu_write_actions = OFPInstructionActions(OFPIT_WRITE_ACTIONS, [
            OFPActionPopVlan(),
            OFPActionSetField(eth_dst="10:10:10:10:10:10"),
            OFPActionOutput(7)
        ])
        ryu_instructions = [
            OFPInstructionActions(OFPIT_CLEAR_ACTIONS, []), ryu_write_actions,
            OFPInstructionGotoTable(9),
            OFPInstructionWriteMetadata(0x99, 0xff)
        ]
        ryu_flow_mod = OFPFlowMod(datapath=None,
                                  cookie=0xABCD,
                                  table_id=3,
                                  command=OFPFC_ADD,
                                  priority=789,
                                  match=ryu_match,
                                  instructions=ryu_instructions)
        ryu_flow_stats = OFPFlowStats(cookie=0xABCD,
                                      table_id=3,
                                      priority=789,
                                      match=ryu_match,
                                      instructions=ryu_instructions)

        match = Match([("IPV4_SRC", 1, None), ("ETH_TYPE", 0x8100, None),
                       ("VLAN_VID", 0x1100, None)])
        instructions = Instructions()
        instructions.goto_table = 9
        instructions.clear_actions = True
        instructions.write_actions.append("POP_VLAN", None)
        instructions.write_actions.append("SET_FIELD",
                                          ("ETH_DST", 0x101010101010))
        instructions.write_actions.append("OUTPUT", 7)
        instructions.write_metadata = (0x99, 0xff)

        rule = Rule(priority=789,
                    cookie=0xABCD,
                    table=3,
                    match=match,
                    instructions=instructions)

        self.assertEqual(rule_from_ryu(ryu_flow_stats), rule)
        self.assertEqual(rule_from_ryu(ryu_flow_mod), rule)
예제 #16
0
    def test_Packet_in(self):
        sa = SimpleArp()

        datapath = _Datapath()
        e = ethernet.ethernet(dstMac, srcMac, ether.ETH_TYPE_ARP)
        a = arp(1, 0x0800, 6, 4, 1, srcMac, srcIp, targetMac, dstIp)
        p = Packet()
        p.add_protocol(e)
        p.add_protocol(a)
        p.serialize()

        packetIn = OFPPacketIn(datapath,
                               match=OFPMatch(in_port=1),
                               data=buffer(p.data))

        ev = ofp_event.EventOFPPacketIn(packetIn)

        result = sa.packet_in_handler(ev)
        self.assertEqual(result, 0)
예제 #17
0
def create_l3_flow(datapath, in_port, out_port, eth_pkt, ip_pkt, msg):
    ofp = datapath.ofproto
    #    ofp.OFPP_IN_PORT
    ofp_parser = datapath.ofproto_parser

    cookie = cookie_mask = 0
    table_id = 0
    idle_timeout = 60
    #hard_timeout = 31
    hard_timeout = 3600
    priority = 32768
    buffer_id = msg.buffer_id  # ofp.OFP_NO_BUFFER
    data = None
    if msg.buffer_id == ofp.OFP_NO_BUFFER:
        data = msg.data
    match = OFPMatch(
        in_port=in_port,
        eth_type=ether_types.ETH_TYPE_IP,
        eth_dst=eth_pkt.dst,  #eth_src = eth_pkt.src,
        ipv4_src=ip_pkt.src,
        ipv4_dst=(ip_pkt.dst, "255.255.255.0"))

    actions = [ofp_parser.OFPActionOutput(port=out_port)]
    inst = [ofp_parser.OFPInstructionActions(ofp.OFPIT_APPLY_ACTIONS, actions)]

    req = ofp_parser.OFPFlowMod(
        datapath,
        cookie,
        cookie_mask,
        table_id,
        ofp.OFPFC_ADD,
        idle_timeout,
        hard_timeout,
        priority,
        buffer_id,
        #in_port, ofp.OFPG_ANY, <<<<<<<<<<<<<<<<<<<<<<<<<<<Verficar se estah ok!
        out_port,
        ofp.OFPG_ANY,
        ofp.OFPFF_SEND_FLOW_REM,
        match,
        inst)
    #print datapath.id,":",req
    datapath.send_msg(req)
예제 #18
0
    def handle_packet_in(self, event):
        """
        Installs flow rules for incoming packets using Dijkstra
        :param event: The PacketIn event
        :type event: EventOFPPacketIn
        :return: Any generated flow IDs
        :rtype: list
        """
        message = event.msg
        datapath = message.datapath
        switch_id = datapath.id
        packet = Packet(message.data)
        ipv4_info = packet.get_protocol(ipv4)
        if ipv4_info is None:
            return []

        out_port, src_subnet, dst_subnet = \
            self.routing_algo.calculate_routing_decision(
                switch_id, ipv4_info.src, ipv4_info.dst
            )
        match = OFPMatch(eth_type=ether_types.ETH_TYPE_IP,
                         ipv4_src=src_subnet,
                         ipv4_dst=dst_subnet)

        # If packets come in very quickly, the flow rule may not be installed
        # yet. To avoid creating new flows, we check if the flow rules were
        # already registered in active_flows.
        for existing_flow in self.active_flows.values():
            if existing_flow.is_ipv4_match_equal(switch_id, match):
                self.send_pkt(datapath, message.data, out_port)
                return []

        actions = [OFPActionOutput(out_port)]
        flow_id = self.program_flow(datapath=datapath,
                                    match=match,
                                    actions=actions,
                                    priority=10,
                                    hard_timeout=120,
                                    idle_timeout=5)
        self.send_pkt(datapath, message.data, out_port)
        return [flow_id]
예제 #19
0
    def install_interdomain_color(self, color, in_port, priority):
        """
            Prepare to send the FlowMod to install interdomain
            colored flows
            Args:
                color: dl_src to be used
                in_port: incoming port
                priority: flow priority
        """
        if not isinstance(priority, int):
            priority = int(priority)
        if not isinstance(in_port, int):
            in_port = int(in_port)

        datapath = self.obj.msg.datapath
        ofproto = datapath.ofproto
        match = OFPMatch(in_port=in_port, eth_src=color)

        op = ofproto.OFPP_CONTROLLER
        actions = [datapath.ofproto_parser.OFPActionOutput(op)]

        self.push_flow(datapath, self.cookie, priority, ofproto.OFPFC_ADD,
                       match, actions)
예제 #20
0
def create_routing_with_wildcard(datapath,
                                 in_port,
                                 out_port,
                                 set_arp_dst,
                                 set_arp_src,
                                 network,
                                 network_mask,
                                 msg,
                                 priority=32768,
                                 modify_rule=False,
                                 routing_instructions=None):
    ofp = datapath.ofproto
    #    ofp.OFPP_IN_PORT
    ofp_parser = datapath.ofproto_parser

    cookie = cookie_mask = 0
    table_id = 0
    idle_timeout = 30
    #hard_timeout = 15
    hard_timeout = 3600
    #priority = 32768
    buffer_id = ofp.OFP_NO_BUFFER
    data = None
    if msg != None:
        buffer_id = msg.buffer_id  # ofp.OFP_NO_BUFFER
        if msg.buffer_id == ofp.OFP_NO_BUFFER:
            data = msg.data
    match = OFPMatch(in_port=in_port,
                     eth_type=ether_types.ETH_TYPE_IP,
                     ipv4_dst=(network, network_mask))
    # match = ofp_parser.OFPMatch(in_port=4,eth_type=ether_types.ETH_TYPE_IP, eth_dst=lb_eth)

    # match = ofp_parser.OFPMatch(eth_type=ether_types.ETH_TYPE_IP, ip_proto=6, ipv4_src=ip_pkt.src,
    #                             ipv4_dst=lb_ipv4, tcp_src=tcp_pkt.src_port,
    #                             tcp_dst=tcp_pkt.dst_port)

    actions = [ofp_parser.OFPActionSetField(eth_dst=set_arp_dst)]
    actions.append(ofp_parser.OFPActionSetField(eth_src=set_arp_src))
    actions.append(ofp_parser.OFPActionOutput(port=out_port))

    inst = [ofp_parser.OFPInstructionActions(ofp.OFPIT_APPLY_ACTIONS, actions)]

    add_or_mod_a_rule = ofp.OFPFC_ADD
    if modify_rule:
        add_or_mod_a_rule = ofp.OFPFC_MODIFY

    req = ofp_parser.OFPFlowMod(
        datapath,
        cookie,
        cookie_mask,
        table_id,
        add_or_mod_a_rule,
        idle_timeout,
        hard_timeout,
        priority,
        buffer_id,
        #in_port, ofp.OFPG_ANY, <<<<<<<<<<<<<<<<<<<<<<<<<<<Verficar se estah ok!
        out_port,
        ofp.OFPG_ANY,
        ofp.OFPFF_SEND_FLOW_REM,
        match,
        inst)
    print datapath.id, ":", req
    datapath.send_msg(req)
예제 #21
0
def create_l3_failover_flow_with_VLAN(datapaths,
                                      sw_src,
                                      instructions,
                                      group_id,
                                      msg=None):
    #create_group_id = False):
    has_backup_path = instructions[sw_src]['has_failover_path']
    failover_in_port = instructions[sw_src]['failover_in_port']
    backup_switch = instructions[sw_src]['backup_switch']
    failover_out_port = instructions[sw_src]['failover_out_port']

    primary_output_port = instructions[sw_src]['out_port']
    primary_input_port = instructions[sw_src]['in_port']
    sw_dst = instructions[sw_src]['forward_switch']
    vlan_tag = instructions[sw_src]['vlan']
    vlan_tag_failover = instructions[sw_src]['vlan_tag_failover']

    datapath = datapaths[sw_src]
    ofp = datapath.ofproto
    ofp_parser = datapath.ofproto_parser

    primary_path_actions = None
    backup_path_actions = None

    primary_path = instructions['primary_path']
    backup_path = instructions['backup_path']

    sw_primary_header = primary_path[0]
    sw_primary_tail = primary_path[-1]

    sw_backup_header = backup_path[0]
    sw_backup_tail = backup_path[-1]

    # Recovering the IPv4 packet from Packet-In event
    pkt = packet.Packet(msg.data)
    ip_pkt = pkt.get_protocol(ipv4.ipv4)

    ofp = datapath.ofproto
    #    ofp.OFPP_IN_PORT
    ofp_parser = datapath.ofproto_parser

    cookie = cookie_mask = 0
    table_id = 0
    idle_timeout = 30
    #hard_timeout = 31
    hard_timeout = 3600
    priority = 32768
    #buffer_id = ofp.OFP_NO_BUFFER

    buffer_id = ofp.OFP_NO_BUFFER
    data = None
    if msg != None:
        buffer_id = msg.buffer_id  # ofp.OFP_NO_BUFFER
        if msg.buffer_id == ofp.OFP_NO_BUFFER:
            data = msg.data

    actions = None
    # match = None

    if datapath.id == sw_primary_header:
        # ICMP -> PushVLAN(9)
        #     LOG.debug("--- add_flow ICMP to PushVLAN(9)")
        #     s_vid = 9
        #     eth_IP = ether.ETH_TYPE_IP
        #     eth_VLAN = ether.ETH_TYPE_8021Q
        #     ip_ICMP = inet.IPPROTO_ICMP
        #     match = ofp_parser.OFPMatch()
        #     match.set_in_port(in_port)
        #     match.set_dl_type(eth_IP)
        #     match.set_ip_proto(ip_ICMP)
        #     f = ofp_parser.OFPMatchField.make(
        #         datapath.ofproto.OXM_OF_VLAN_VID, vlan_tag)
        #     actions = [datapath.ofproto_parser.OFPActionPushVlan(eth_VLAN),
        #                datapath.ofproto_parser.OFPActionSetField(f),
        #                datapath.ofproto_parser.OFPActionOutput(out_port, 0)]
        # self._add_flow(dp, match, actions)
        print "Push a VLAN tag:\n\n\n\n"
        # Push a VLAN tag:
        # Only packets without a VLAN tag:
        # match = ofp_parser.OFPMatch()
        match = OFPMatch(
            in_port=primary_input_port,
            eth_type=ether_types.ETH_TYPE_IP,  # ipv4_src=ip_pkt_src,
            ipv4_dst=ip_pkt.dst)
        # match.set_ip_proto(ipv4.inet.IPPROTO_IP)
        tag = ofp_parser.OFPMatchField.make(ofp.OXM_OF_VLAN_VID, vlan_tag)
        actions = [
            ofp_parser.OFPActionPushVlan(ether_types.ETH_TYPE_8021Q),
            ofp_parser.OFPActionSetField(vlan_vid=(0x1000 | vlan_tag))
        ]

    elif datapath.id == sw_primary_tail:
        print "Pop a VLAN tag:\n\n\n\n"
        # Pop a VLAN tag:
        match = OFPMatch(
            in_port=primary_input_port,
            vlan_vid=(0x1000 | vlan_tag),
            eth_type=ether_types.ETH_TYPE_IP,  # ipv4_src=ip_pkt_src,
            ipv4_dst=ip_pkt.dst)
        actions = [ofp_parser.OFPActionPopVlan()]

    else:
        print "Forward the VLAN tag:\n\n\n\n"
        match = ofp_parser.OFPMatch(in_port=primary_input_port,
                                    vlan_vid=(0x1000 | vlan_tag))
        actions = []

    actions = [ofp_parser.OFPActionGroup(group_id)]

    inst = [ofp_parser.OFPInstructionActions(ofp.OFPIT_APPLY_ACTIONS, actions)]

    req = ofp_parser.OFPFlowMod(
        datapath,
        cookie,
        cookie_mask,
        table_id,
        ofp.OFPFC_ADD,
        idle_timeout,
        hard_timeout,
        priority,
        buffer_id,
        #in_port, ofp.OFPG_ANY, <<<<<<<<<<<<<<<<<<<<<<<<<<<Verficar se estah ok!
        primary_output_port,
        ofp.OFPG_ANY,
        ofp.OFPFF_SEND_FLOW_REM,
        match,
        inst)
    #print "Creating flow:",datapath.id,":",in_port,out_port,req
    datapath.send_msg(req)
예제 #22
0
def create_failover_vlan_flow(datapath,
                              sw_header,
                              sw_tail,
                              vlan_tag,
                              in_port,
                              out_port,
                              ip_pkt_src,
                              ip_pkt_dst,
                              msg,
                              priority=32768,
                              modify_rule=False):

    ofp = datapath.ofproto
    #    ofp.OFPP_IN_PORT
    ofp_parser = datapath.ofproto_parser
    match = None
    cookie = cookie_mask = 0
    table_id = 0
    idle_timeout = 30
    #hard_timeout = 15
    hard_timeout = 3600
    #priority = 32768
    buffer_id = ofp.OFP_NO_BUFFER
    data = None
    if msg != None:
        buffer_id = msg.buffer_id  # ofp.OFP_NO_BUFFER
        if msg.buffer_id == ofp.OFP_NO_BUFFER:
            data = msg.data

    actions = []
    #match = None

    if datapath.id == sw_header:
        # ICMP -> PushVLAN(9)
        #     LOG.debug("--- add_flow ICMP to PushVLAN(9)")
        #     s_vid = 9
        #     eth_IP = ether.ETH_TYPE_IP
        #     eth_VLAN = ether.ETH_TYPE_8021Q
        #     ip_ICMP = inet.IPPROTO_ICMP
        #     match = ofp_parser.OFPMatch()
        #     match.set_in_port(in_port)
        #     match.set_dl_type(eth_IP)
        #     match.set_ip_proto(ip_ICMP)
        #     f = ofp_parser.OFPMatchField.make(
        #         datapath.ofproto.OXM_OF_VLAN_VID, vlan_tag)
        #     actions = [datapath.ofproto_parser.OFPActionPushVlan(eth_VLAN),
        #                datapath.ofproto_parser.OFPActionSetField(f),
        #                datapath.ofproto_parser.OFPActionOutput(out_port, 0)]
        # self._add_flow(dp, match, actions)
        print "Push a VLAN tag:\n\n\n\n"
        #Push a VLAN tag:
        #Only packets without a VLAN tag:
        #match = ofp_parser.OFPMatch()
        # match = OFPMatch(eth_type=ether_types.ETH_TYPE_IP,#ipv4_src=ip_pkt_src,
        #                    ipv4_dst=ip_pkt_dst)
        match = OFPMatch(vlan_vid=(0x1000 | vlan_tag), in_port=in_port)
        #match.set_ip_proto(ipv4.inet.IPPROTO_IP)
        tag = ofp_parser.OFPMatchField.make(ofp.OXM_OF_VLAN_VID, vlan_tag)
        # actions = [ofp_parser.OFPActionPushVlan(ether_types.ETH_TYPE_8021Q),
        #            ofp_parser.OFPActionSetField(vlan_vid=(0x1000 |vlan_tag))]

    elif datapath.id == sw_tail:
        print "Pop a VLAN tag:\n\n\n\n"
        # Pop a VLAN tag:
        match = OFPMatch(vlan_vid=(0x1000 | vlan_tag), in_port=in_port)
        # actions = [ofp_parser.OFPActionPopVlan()]

    else:
        print "Forward the VLAN tag:\n\n\n\n"
        #match = ofp_parser.OFPMatch(vlan_vid=(0x1000 | vlan_tag))
        match = OFPMatch(vlan_vid=(0x1000 | vlan_tag), in_port=in_port)
        actions = []

    #Default action:
    actions.append(ofp_parser.OFPActionOutput(port=out_port))

    inst = [ofp_parser.OFPInstructionActions(ofp.OFPIT_APPLY_ACTIONS, actions)]

    add_or_mod_a_rule = ofp.OFPFC_ADD
    if modify_rule:
        add_or_mod_a_rule = ofp.OFPFC_MODIFY

    req = ofp_parser.OFPFlowMod(
        datapath,
        cookie,
        cookie_mask,
        table_id,
        add_or_mod_a_rule,
        idle_timeout,
        hard_timeout,
        priority,
        buffer_id,
        #in_port, ofp.OFPG_ANY, <<<<<<<<<<<<<<<<<<<<<<<<<<<Verficar se estah ok!
        out_port,
        ofp.OFPG_ANY,
        ofp.OFPFF_SEND_FLOW_REM,
        match,
        inst)
    print datapath.id, ":", req
    datapath.send_msg(req)
예제 #23
0
def create_group_mod_failover_flow_with_vlan(datapaths,
                                             sw_src,
                                             instructions,
                                             group_id,
                                             create_group_id=False):
    has_backup_path = instructions[sw_src]['has_failover_path']
    failover_in_port = instructions[sw_src]['failover_in_port']
    backup_switch = instructions[sw_src]['backup_switch']
    failover_out_port = instructions[sw_src]['failover_out_port']

    primary_output_port = instructions[sw_src]['out_port']
    primary_input_port = instructions[sw_src]['in_port']
    sw_dst = instructions[sw_src]['forward_switch']
    vlan_tag = instructions[sw_src]['vlan']
    vlan_tag_failover = instructions[sw_src]['vlan_tag_failover']

    datapath = datapaths[sw_src]
    ofp = datapath.ofproto
    ofp_parser = datapath.ofproto_parser

    primary_path_actions = None
    backup_path_actions = None

    primary_path = instructions['primary_path']
    backup_path = instructions['backup_path']

    sw_primary_header = primary_path[0]
    sw_primary_tail = primary_path[-1]

    sw_backup_header = backup_path[0]
    sw_backup_tail = backup_path[-1]

    #Creating match and action for primary path
    if datapath.id == sw_primary_header:
        print "Push a VLAN tag:\n\n\n\n"
        primary_path_actions = [
            ofp_parser.OFPActionPushVlan(ether_types.ETH_TYPE_8021Q),
            ofp_parser.OFPActionSetField(vlan_vid=(0x1000 | vlan_tag))
        ]

    elif datapath.id == sw_primary_tail:
        print "Pop a VLAN tag:\n\n\n\n"
        # Pop a VLAN tag:
        primary_match = OFPMatch(vlan_vid=(
            0x1000 | vlan_tag
        ))  #, eth_type=ether_types.ETH_TYPE_IP,  # ipv4_src=ip_pkt_src,
        #ipv4_dst=ip_pkt_dst)
        primary_path_actions = [ofp_parser.OFPActionPopVlan()]

    else:
        print "Forward the VLAN tag:\n\n\n\n"
        match = ofp_parser.OFPMatch(vlan_vid=(0x1000 | vlan_tag))
        primary_path_actions = []

    # Default action:
    primary_path_actions.append(
        ofp_parser.OFPActionOutput(port=primary_output_port))

    if has_backup_path:
        if datapath.id == sw_backup_header:
            print "Push a VLAN tag:\n\n\n\n"
            backup_path_actions = [  #ofp_parser.OFPActionPushVlan(ether_types.ETH_TYPE_8021Q),
                ofp_parser.OFPActionSetField(vlan_vid=(0x1000
                                                       | vlan_tag_failover))
            ]

        elif datapath.id == sw_backup_tail:
            print "Pop a VLAN tag:\n\n\n\n"
            # Pop a VLAN tag:
            backup_match = OFPMatch(vlan_vid=(
                0x1000 | vlan_tag_failover
            ))  #, eth_type=ether_types.ETH_TYPE_IP,  # ipv4_src=ip_pkt_src,
            #ipv4_dst=ip_pkt_dst)
            backup_path_actions = [ofp_parser.OFPActionPopVlan()]

        else:
            print "Forward the VLAN tag:\n\n\n\n"
            backup_match = ofp_parser.OFPMatch(vlan_vid=(0x1000
                                                         | vlan_tag_failover))
            backup_path_actions = [  # ofp_parser.OFPActionPushVlan(ether_types.ETH_TYPE_8021Q),
                ofp_parser.OFPActionSetField(vlan_vid=(0x1000
                                                       | vlan_tag_failover))
            ]
            #backup_path_actions = []

        # Default action:
        backup_path_actions.append(
            ofp_parser.OFPActionOutput(port=failover_out_port))


#    actions = [ofp_parser.OFPActionOutput(out_port)]

    weight = 0
    #watch_port = 1 #As such, the watch port is the same as the port we will use as an output action in the bucket's actions.
    watch_port = primary_output_port
    watch_group = ofp.OFPG_ANY
    #???
    buckets = []
    if create_group_id:
        if not has_backup_path:
            buckets = [
                ofp_parser.OFPBucket(weight, watch_port, watch_group,
                                     primary_path_actions)
            ]
            #print "Aqui naooooooo!"
        else:
            #print "Aqui simmmmmmmmm!",out_port,backup_output_port
            failover_watch_port = failover_out_port
            failover_watch_group = ofp.OFPG_ANY
            #failover_actions = [ofp_parser.OFPActionOutput(backup_output_port)]
            buckets = [
                ofp_parser.OFPBucket(weight, watch_port, watch_group,
                                     primary_path_actions),
                ofp_parser.OFPBucket(weight, failover_watch_port,
                                     failover_watch_group, backup_path_actions)
            ]
        # group_id = 1
        req = ofp_parser.OFPGroupMod(datapath, ofp.OFPGC_ADD, ofp.OFPGT_FF,
                                     group_id, buckets)
        print "Creating FF group table for ", datapath.id, req
        datapath.send_msg(req)
    else:
        if not has_backup_path:
            buckets = [
                ofp_parser.OFPBucket(weight, watch_port, watch_group,
                                     primary_path_actions)
            ]
            #print "Aqui naooooooo!"
        else:
            failover_watch_port = failover_out_port
            failover_watch_group = ofp.OFPG_ANY
            #failover_actions = [ofp_parser.OFPActionOutput(backup_output_port)]
            buckets = [
                ofp_parser.OFPBucket(weight, watch_port, watch_group,
                                     primary_path_actions),
                ofp_parser.OFPBucket(weight, failover_watch_port,
                                     failover_watch_group, backup_path_actions)
            ]

        req = ofp_parser.OFPGroupMod(datapath, ofp.OFPGC_MODIFY, ofp.OFPGT_FF,
                                     group_id, buckets)
        print "Modifing FF group table:", req
        datapath.send_msg(req)
        return 0
예제 #24
0
def create_l3_failover_flow(datapath,
                            in_port,
                            out_port,
                            ip_pkt,
                            msg=None,
                            group_id=1,
                            eth_pkt=None):
    ofp = datapath.ofproto
    #    ofp.OFPP_IN_PORT
    ofp_parser = datapath.ofproto_parser

    cookie = cookie_mask = 0
    table_id = 0
    idle_timeout = 600
    #hard_timeout = 31
    hard_timeout = 3600
    priority = 32768
    buffer_id = ofp.OFP_NO_BUFFER
    data = None
    if msg != None:
        print "AQUIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII\n\n\n\n\n"
        # Send the packet-In to the host:
        _send_packet(datapath, out_port, packet.Packet(msg.data))

        buffer_id = msg.buffer_id  # ofp.OFP_NO_BUFFER
        if msg.buffer_id == ofp.OFP_NO_BUFFER:
            data = msg.data
    match = None
    if eth_pkt == None:
        match = OFPMatch(  #in_port=in_port,
            eth_type=ether_types.ETH_TYPE_IP,
            #ipv4_src=ip_pkt.src,
            ipv4_dst=ip_pkt.dst)
    else:
        match = OFPMatch(  #in_port=in_port,
            eth_type=ether_types.ETH_TYPE_IP,
            #eth_dst = eth_pkt.dst,eth_src = eth_pkt.src,
            #ipv4_src=ip_pkt.src,
            ipv4_dst=ip_pkt.dst)

    #actions = [ofp_parser.OFPActionOutput(port=out_port)]
    #group_id = 1
    actions = [ofp_parser.OFPActionGroup(group_id)]

    inst = [ofp_parser.OFPInstructionActions(ofp.OFPIT_APPLY_ACTIONS, actions)]

    req = ofp_parser.OFPFlowMod(
        datapath,
        cookie,
        cookie_mask,
        table_id,
        ofp.OFPFC_ADD,
        idle_timeout,
        hard_timeout,
        priority,
        buffer_id,
        #in_port, ofp.OFPG_ANY, <<<<<<<<<<<<<<<<<<<<<<<<<<<Verficar se estah ok!
        out_port,
        ofp.OFPG_ANY,
        ofp.OFPFF_SEND_FLOW_REM,
        match,
        inst)
    print "Creating flow:", datapath.id, ":", in_port, out_port, req
    datapath.send_msg(req)
예제 #25
0
 def ofmatch_from_dict(self, match_dict):
     ''' convert a dict/json rule to a match '''
     ## Part 2 # Part 5 ##
     return OFPMatch(**match_dict)
예제 #26
0
def to_match(m):
    return OFPMatch(**m)
예제 #27
0
    def get_semantic_flows(self):
        rq = """
        PREFIX : <http://home.eps.hw.ac.uk/~qz1/>
        select ?p ?param ?val
        where
        {
          {
            ?p rdf:type/rdfs:subClassOf* of:Flow;
            ?param ?val.
          }
          UNION
          {
          ?flow rdf:type/rdfs:subClassOf* of:Flow;
              of:hasAction ?p.
            ?p ?param ?val.
          }
          UNION
          {
          ?p rdf:type/rdfs:subClassOf* of:Flow.
            ?sw ?param ?p.
            ?sw :hasID ?val.
          }
        }
        """
        res = self.store.query(rq)
        flows = {}
        actions = {}
        ret = {}

        for (a, b, c) in res:
            a = a.replace("http://home.eps.hw.ac.uk/~qz1/", "").encode()
            b = b.replace("http://home.eps.hw.ac.uk/~qz1/", "").encode()
            c = c.replace("http://home.eps.hw.ac.uk/~qz1/", "").encode()
            LOG.debug("%s %s %s" % (a, b, c))
            if "hasAction" in b:
                actions[c] = a
            if "_action" in a:
                if a not in flows[actions[a]]["actions"]:
                    flows[actions[a]]["actions"][a] = {}
                flows[actions[a]]["actions"][a][b] = c
            else:
                if a not in flows:
                    # We need a datapath here
                    flows[a] = {"match": {}, "datapath": None, "actions": {}}

                if b in mappings:
                    if b in ["in_port"]:
                        flows[a]["match"][b] = int(
                            c)  # append_field(self.match_map[b], c)
                    else:
                        flows[a]["match"][
                            b] = c  # append_field(self.match_map[b], c)
                if "hasFlow" in b:
                    flows[a]["datapath"] = int(c)
        for a in flows:
            dpid = flows[a]["datapath"]
            if dpid not in ret:
                ret[dpid] = []
            flow_act = []
            for act in flows[a]["actions"]:
                action = flows[a]["actions"][act]
                if action[
                        "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"] == "ActionOutput":
                    flow_act.append(
                        OFPActionOutput(int(action["toPort"]), max_len=0xffff))
            flow_act = [
                OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, flow_act)
            ]
            # flows[a]["match"] = OFPMatch(**flows[a]["match"])
            ret[dpid].append({
                "actions": flow_act,
                "match": OFPMatch(**flows[a]["match"])
            })
        return ret
예제 #28
0
def I(goto=None, set_tcp=None):
    """ Short hand to generate instructions """
    i = Instructions()
    i.goto_table = goto
    if set_tcp is not None:
        i.apply_actions.append('SET_FIELD', ('TCP_DST', set_tcp))
    return i


R2M = match_from_ryu
COVISOR_RULESET = [
    Rule(
        priority=5,
        match=R2M(
            OFPMatch(  # 0
                ipv4_src=("1.0.0.0", "255.255.255.0"),
                ipv4_dst="2.0.0.1"))),
    Rule(
        priority=4,
        match=R2M(
            OFPMatch(  # 1
                ipv4_src=("1.0.0.0", "255.255.255.0"),
                ipv4_dst="2.0.0.2"))),
    Rule(
        priority=3,
        match=R2M(OFPMatch(  # 2
            ipv4_src=("1.0.0.0", "255.255.255.0")))),
    Rule(priority=2, match=R2M(OFPMatch(ipv4_dst="2.0.0.1"))),  # 3
    Rule(priority=1, match=R2M(OFPMatch(ipv4_dst="2.0.0.2"))),  # 4
    Rule(priority=0)  # 5
]
    while True:
        for port in PORT_RANGE:
            yield port


FW = []
LEARN = []
m = next_mac()
p = next_port()
for x in range(NUMBER_MACS):
    mac = m.next()
    port = p.next()
    FW.append(
        OFPFlowStats(table_id=2,
                     priority=1000,
                     match=OFPMatch(eth_dst=mac),
                     instructions=[
                         OFPInstructionActions(
                             ofproto_v1_3.OFPIT_WRITE_ACTIONS,
                             [OFPActionOutput(port)]),
                         OFPInstructionGotoTable(3)
                     ]))
    LEARN.append(
        OFPFlowStats(table_id=4,
                     priority=1000,
                     match=OFPMatch(in_port=port, eth_src=mac),
                     instructions=[]))

flows = [
    # Table 0 Mac Term
    OFPFlowStats(table_id=0,

  +------------+          +----------+
  | TCP filter |  +-->    | ETH, IP  |
  +------------+          |   etc    |
                          +----------+

The opposite of simple-ofdpa.

"""

flows = [
    # Table 0, drop all traffic
    OFPFlowStats(table_id=0,
                 priority=1000,
                 match=OFPMatch(tcp_dst=80),
                 instructions=[]),
    OFPFlowStats(table_id=0,
                 priority=1000,
                 match=OFPMatch(tcp_dst=443),
                 instructions=[]),
    OFPFlowStats(table_id=0,
                 priority=0,
                 match=OFPMatch(),
                 instructions=[OFPInstructionGotoTable(1)]),

    # Table 1, ETH, IP etc.

    # Routing
    OFPFlowStats(table_id=1,
                 priority=1008,