Пример #1
0
    def installTestStarFlowEntries(self):
        """
        Code written for specific test on start topology. (Measure duplication and tagging time).
        :return:
        """

        datapath1 = self.id_to_datapath[1]
        datapath2 = self.id_to_datapath[2]

        # Install the flow entries on the MP.
        Match = ofproto_v1_3_parser.OFPMatch(
            in_port=1,
            eth_type=ExtraLayers.typeForTestingTaggingAndDuplicationTime)
        Actions = [
            ofproto_v1_3_parser.OFPActionOutput(0xfffffffd),
            ofproto_v1_3_parser.OFPActionOutput(0xfffffffd),
            ofproto_v1_3_parser.OFPActionPopVlan(),
            ofproto_v1_3_parser.OFPActionSetField(
                eth_type=ExtraLayers.dirBackwardsThreeFlowEntries),
            ofproto_v1_3_parser.OFPActionPushVlan(
                ethertype=ExtraLayers.VLAN_ID_ETHERTYPE_QINQ),
            ofproto_v1_3_parser.OFPActionSetField(vlan_vid=1),
            ofproto_v1_3_parser.OFPActionOutput(0xfffffffd)
        ]
        Inst = [
            ofproto_v1_3_parser.OFPInstructionActions(
                datapath1.ofproto.OFPIT_APPLY_ACTIONS, Actions)
        ]
        mod = ofproto_v1_3_parser.OFPFlowMod(
            datapath=datapath1,
            priority=NORMAL_SWITCH_FORWARDING_PRIO,
            match=Match,
            instructions=Inst)
        datapath1.send_msg(mod)
Пример #2
0
    def add_lb_flow(self, datapath, mac):
        global inc

        match = datapath.ofproto_parser.OFPMatch(in_port=4,
                                                 eth_type=0x800,
                                                 ip_proto=6,
                                                 ipv4_dst='10.0.0.1',
                                                 eth_dst='00:00:00:00:00:01')
        ip = "10.0.0." + str(self.mac_port[mac])
        eth = "00:00:00:00:00:0" + str(self.mac_port[mac])
        actions = [
            ofparser.OFPActionSetField(ipv4_dst=ip),
            ofparser.OFPActionSetField(eth_dst=eth),
            ofparser.OFPActionSetField(tcp_dst=80),
            ofparser.OFPActionOutput(port=self.mac_port[mac])
        ]
        print "redirected to port ", self.mac_port[mac]

        inst = [
            ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
                                           actions)
        ]
        mod = ofparser.OFPFlowMod(datapath=datapath,
                                  table_id=0,
                                  priority=100,
                                  match=match,
                                  instructions=inst,
                                  hard_timeout=2)
        datapath.send_msg(mod)
Пример #3
0
    def getReturnAndTagActions(self, switchID):
        '''
        Get the return and tag actions
        :param switchID: ID of current switch
        :return: return and tag flow entries to be installed
        '''
        actions = []

        #In 4 flow entries, the packet contains only tagging on last switch ID (inner tagging) need to add
        #the first switch ID (outer tagging) and change the ethertype to be dirReturnNoTag.
        # Set type of packet
        actions.append(
            ofproto_v1_3_parser.OFPActionSetField(
                eth_src=ExtraLayers.ReturnNoTagMAC))
        #push VLAN header
        actions.append(ofproto_v1_3_parser.OFPActionPushVlan())
        #set the DirectionReturn field to be dirReturnNoTag

        #tag cuurent switch ID
        actions.append(
            ofproto_v1_3_parser.OFPActionSetField(
                vlan_vid=retMaskedVlanID(switchID)))
        # Send to ingress port.
        actions.append(
            ofproto_v1_3_parser.OFPActionOutput(
                (self.switch_to_parent_port[switchID])))

        #return the actions list
        return actions
Пример #4
0
    def getDoNotDistributeActions(self, switchID):
        '''
        Get the do not distribute actions to the given switch and current number of flow entries.
        :param switchID: ID of current switch.
        :return: Do not distribute flow entries to be installed.
        '''

        #initialize the actions set to be empty.
        actions = []

        # Create the return packet - Set return And tag direaction and set the current switch ID with solid flag FALSE.

        # set vlan ID to be current switch(No solid flag).
        actions.append(
            ofproto_v1_3_parser.OFPActionSetField(
                eth_src=ExtraLayers.ReturnAndTagMAC))
        # set vlan ID to be current switch with solid flag.
        actions.append(
            ofproto_v1_3_parser.OFPActionSetField(
                vlan_vid=retMaskedVlanID(switchID)))
        # Send to ingress port.
        actions.append(ofproto_v1_3_parser.OFPActionOutput(OF_IN_PORT))

        #return the actions list
        return actions
Пример #5
0
    def getDistributionActions(self, switchID):
        '''
        Get the distribution actions to the given switch and current number of flow entries.
        :param switchID: ID of current switch.
        :return: distribution flow entries to be installed
        '''
        #initialize the actions set to be empty
        actions = []

        #Distribution in 4 flow entries  - because returning the packet required extra processing and distribution requires none, first distribute the original packet and then return
        #First- send the original packet to all the distribution switches
        #Then - add info about the current switch ID (inner tag) and change the direction to be return and tag direction
        #packet arrive with inner vlan id of prev switch and dummy switch- change return direction ,set
        #the outer vlan to be curr switch and return via same port

        #if there are port to distribute to, should  add the distribution actions (send the packet to distribution as is)
        if (0 != len(self.switch_to_distribution_port[switchID])):

            #send to every switch in distribution port all the distribution ports
            for dist_port in self.switch_to_distribution_port[switchID]:
                actions.append(ofproto_v1_3_parser.OFPActionOutput(dist_port))

        #Now add the info about the current switch by popping both vlan and adding current switch tag.
        actions.append(
            ofproto_v1_3_parser.OFPActionSetField(
                eth_src=ExtraLayers.ReturnAndTagMAC))
        # set vlan ID to be current switch with solid flag.
        actions.append(
            ofproto_v1_3_parser.OFPActionSetField(
                vlan_vid=(retMaskedVlanID(switchID) | ExtraLayers.solidFlag)))
        # Send to ingress port.
        actions.append(ofproto_v1_3_parser.OFPActionOutput(OF_IN_PORT))

        # if the there is no any RTP start in the current switch, exit
        if switchID not in self.rtp_switch_to_start_points:
            return actions

        # add the RTP for each RTP ID
        actions.append(
            ofproto_v1_3_parser.OFPActionSetField(
                vlan_vid=retMaskedVlanID(ExtraLayers.NULL_ID)))

        entryNum = 0
        for entry in self.rtp_switch_to_start_points[switchID]:
            actions.append(ofproto_v1_3_parser.OFPActionPushVlan())
            actions.append(
                ofproto_v1_3_parser.OFPActionSetField(
                    eth_src=ExtraLayers.RTPMAC))
            actions.append(
                ofproto_v1_3_parser.OFPActionSetField(
                    vlan_vid=retMaskedVlanID(entry['id'])))
            actions.append(
                ofproto_v1_3_parser.OFPActionOutput(entry['outPort']))
            #Do no pop the vlan header from the last packet send
            entryNum = entryNum + 1
            if entryNum < len(self.rtp_switch_to_start_points[switchID]):
                actions.append(ofproto_v1_3_parser.OFPActionPopVlan())

        #return the actions list
        return actions
Пример #6
0
    def switch_features_handler(self, ev):
        msg = ev.msg
        datapath = msg.datapath
        print "thread start"
        z = Thread(target=self.ctr_check, args=(datapath, ))
        z.start()
        #print "thread block passed"

        #ARP packets flooding
        match = ofparser.OFPMatch(eth_type=0x0806)
        actions = [ofparser.OFPActionOutput(port=ofproto.OFPP_FLOOD)]
        self.add_flow1(datapath=datapath,
                       table_id=0,
                       priority=100,
                       match=match,
                       actions=actions)

        # reverse path flow
        for i in range(1, 4):
            ip = "10.0.0." + str(i)
            eth = "00:00:00:00:00:0" + str(i)

            match = ofparser.OFPMatch(in_port=i,
                                      eth_type=0x800,
                                      ip_proto=6,
                                      tcp_src=80,
                                      ipv4_src=ip,
                                      eth_src=eth,
                                      ipv4_dst='10.0.0.4')
            actions = [
                ofparser.OFPActionSetField(ipv4_src="10.0.0.1"),
                ofparser.OFPActionSetField(eth_src="00:00:00:00:00:01"),
                ofparser.OFPActionSetField(tcp_src=80),
                ofparser.OFPActionOutput(port=4, max_len=0)
            ]
            self.add_flow1(datapath=datapath,
                           table_id=0,
                           priority=100,
                           match=match,
                           actions=actions)

            # reverse ARP path flow

            match_ = ofparser.OFPMatch(in_port=i,
                                       eth_type=ether_types.ETH_TYPE_ARP,
                                       ipv4_src=ip,
                                       eth_src=eth,
                                       eth_dst="00:00:00:00:00:04")
            actions_ = [
                ofparser.OFPActionSetField(ipv4_src="10.0.0.1"),
                ofparser.OFPActionSetField(eth_src="00:00:00:00:00:01"),
                ofparser.OFPActionOutput(port=4, max_len=0)
            ]
            self.add_flow1(datapath=datapath,
                           table_id=0,
                           priority=100,
                           match=match_,
                           actions=actions_)
Пример #7
0
    def switch_features_handler(self, ev):
        msg = ev.msg
        datapath = msg.datapath

        #ARP packets flooding
        match = ofparser.OFPMatch(eth_type=0x0806)
        actions = [ofparser.OFPActionOutput(port=ofproto.OFPP_FLOOD)]
        self.add_flow1(datapath=datapath,
                       table_id=0,
                       priority=100,
                       match=match,
                       actions=actions)

        # reverse path flow
        for i in range(1, 4):
            ip = "10.0.0." + str(i)
            eth = "00:00:00:00:00:0" + str(i)
            match = ofparser.OFPMatch(in_port=i,
                                      eth_type=0x800,
                                      ip_proto=6,
                                      ipv4_src=ip,
                                      eth_src=eth)
            actions = [
                ofparser.OFPActionSetField(ipv4_src="10.0.0.1"),
                ofparser.OFPActionSetField(eth_src="00:00:00:00:00:01"),
                ofparser.OFPActionSetField(tcp_src=80),
                ofparser.OFPActionOutput(port=4, max_len=0)
            ]
            self.add_flow1(datapath=datapath,
                           table_id=0,
                           priority=100,
                           match=match,
                           actions=actions)

        # reverse ARP path flow
        for i in range(1, 4):
            ip = "10.0.0." + str(i)
            eth = "00:00:00:00:00:0" + str(i)
            match = ofparser.OFPMatch(in_port=i,
                                      eth_type=ether_types.ETH_TYPE_ARP,
                                      ipv4_src=ip,
                                      eth_src=eth)
            actions = [
                ofparser.OFPActionSetField(ipv4_src="10.0.0.1"),
                ofparser.OFPActionSetField(eth_src="00:00:00:00:00:01"),
                ofparser.OFPActionOutput(port=4, max_len=0)
            ]
            self.add_flow1(datapath=datapath,
                           table_id=0,
                           priority=100,
                           match=match,
                           actions=actions)
Пример #8
0
 def nat_actions(self, eth_type, nfvip, nat_offset):
     ip_ver = nfvip.ip.version
     ip_src_nxm = 'ipv%u_src_nxm' % ip_ver
     ip_dst_nxm = 'ipv%u_dst_nxm' % ip_ver
     ipbits = nfvip.ip.max_prefixlen
     # pylint: disable=no-member
     return [
         parser.NXActionRegMove(src_field='ipv%u_src' % ip_ver, dst_field=self.AREG, n_bits=nat_offset, src_ofs=0, dst_ofs=nat_offset),
         parser.NXActionRegMove(src_field='ipv%u_dst' % ip_ver, dst_field=self.AREG, n_bits=nat_offset, src_ofs=0, dst_ofs=0),
         # we have to load output port numbers into reg1 and reg2 because NXFlowSpecOutput() won't take a literal.
         parser.NXActionRegLoad(value=self.FAKEPORT, dst=self.FAKEPORTREG, ofs_nbits=nicira_ext.ofs_nbits(0, 15)),
         parser.NXActionRegLoad(value=self.COPROPORT, dst=self.COPROPORTREG, ofs_nbits=nicira_ext.ofs_nbits(0, 15)),
         # now program an inbound flow to perform NAT.
         parser.NXActionLearn(
             table_id=self.FROM_COPRO_TABLE,
             priority=2,
             hard_timeout=self.IDLE,
             specs=[
                 parser.NXFlowSpecMatch(src=eth_type, dst=('eth_type_nxm', 0), n_bits=16),
                 parser.NXFlowSpecMatch(src=(ip_src_nxm, 0), dst=(ip_src_nxm, 0), n_bits=ipbits),
                 parser.NXFlowSpecMatch(src=(ip_dst_nxm, 0), dst=(ip_dst_nxm, 0), n_bits=ipbits),
                 parser.NXFlowSpecLoad(src=int(self.FAKECLIENTMAC), dst=('eth_src_nxm', 0), n_bits=48),
                 parser.NXFlowSpecLoad(src=int(self.FAKESERVERMAC), dst=('eth_dst_nxm', 0), n_bits=48),
             ] + self.reg_copy(self.AREG, ip_src_nxm, ipbits) + [
                 parser.NXFlowSpecLoad(src=int(nfvip.ip), dst=(ip_dst_nxm, 0), n_bits=ipbits),
                 parser.NXFlowSpecOutput(src=(self.FAKEPORTREG, 0), dst='', n_bits=16),
             ]),
         # now program outbound an outbound flow.
         parser.NXActionLearn(
             table_id=self.TO_COPRO_TABLE,
             priority=2,
             idle_timeout=self.IDLE,
             specs=[
                 parser.NXFlowSpecMatch(src=eth_type, dst=('eth_type_nxm', 0), n_bits=16),
                 parser.NXFlowSpecMatch(src=int(nfvip.ip), dst=(ip_src_nxm, 0), n_bits=ipbits),
             ] + self.reg_copy(self.AREG, ip_dst_nxm, ipbits) + [
                 parser.NXFlowSpecLoad(src=('eth_dst_nxm', 0), dst=('eth_src_nxm', 0), n_bits=48),
                 parser.NXFlowSpecLoad(src=('eth_src_nxm', 0), dst=('eth_dst_nxm', 0), n_bits=48),
                 parser.NXFlowSpecLoad(src=(ip_dst_nxm, 0), dst=(ip_src_nxm, 0), n_bits=ipbits),
                 parser.NXFlowSpecLoad(src=(ip_src_nxm, 0), dst=(ip_dst_nxm, 0), n_bits=ipbits),
                 parser.NXFlowSpecOutput(src=(self.COPROPORTREG, 0), dst='', n_bits=16),
             ]),
         # now that future flows are programmed, handle the packet we have.
         parser.OFPActionSetField(eth_src=self.FAKECLIENTMAC),
         parser.OFPActionSetField(eth_dst=self.FAKESERVERMAC),
         parser.NXActionRegMove(src_field=self.AREG, dst_field=('ipv%u_src' % ip_ver), n_bits=ipbits, src_ofs=0, dst_ofs=0),
         parser.OFPActionSetField(**{'ipv%u_dst' % ip_ver: str(nfvip.ip)}),
         parser.OFPActionOutput(self.FAKEPORT)
     ]
Пример #9
0
    def packet_in_handler(self, ev):
        global inc
        msg = ev.msg
        datapath = msg.datapath
        ofproto = datapath.ofproto

        pkt = packet.Packet(msg.data)
        eth = pkt.get_protocol(ethernet.ethernet)
        _ipv4 = pkt.get_protocol(ipv4.ipv4)
        _icmp = pkt.get_protocol(icmp.icmp)

        if _icmp:
            self.logger.info("%r", _icmp)

        if _ipv4:
            self.logger.info("%r", _ipv4)

        if eth:
            self.logger.info("%r", eth)

        dpid = msg.match['in_port']
        #self.mac_to_port.setdefault(dpid,{})
        #self.logger.info(pkt)
        if dpid == 4:

            match = datapath.ofproto_parser.OFPMatch(
                in_port=dpid,
                eth_type=0x800,
                ip_proto=6,
                tcp_dst=80,
                ipv4_dst='10.0.0.1',
                eth_dst='00:00:00:00:00:01')
            inc = self.inc
            ip = "10.0.0." + str(inc)
            eth = "00:00:00:00:00:0" + str(inc)
            actions = [
                ofparser.OFPActionSetField(ipv4_dst=ip),
                ofparser.OFPActionSetField(eth_dst=eth),
                ofparser.OFPActionSetField(tcp_dst=80),
                ofparser.OFPActionOutput(port=inc)
            ]
            self.add_flow(datapath=datapath,
                          table_id=0,
                          priority=100,
                          match=match,
                          actions=actions)
            print "redirected to port ", inc
            self.inc = (inc % 3) + 1
Пример #10
0
 def common_reply_actions(self):
     # pylint: disable=no-member
     return [
         parser.NXActionRegMove(src_field='eth_src', dst_field='eth_dst', n_bits=48, src_ofs=0, dst_ofs=0),
         parser.OFPActionSetField(eth_src=self.FAKECLIENTMAC),
         parser.OFPActionOutput(ofp.OFPP_IN_PORT)
     ]
Пример #11
0
 def arp_reply_actions(self):
     # pylint: disable=no-member
     common_reply = self.common_reply_actions()
     return self.apply_actions([
         parser.NXActionRegLoad(value=arp.ARP_REPLY,
                                dst='arp_op',
                                ofs_nbits=nicira_ext.ofs_nbits(0, 2)),
         parser.NXActionRegMove(src_field='arp_sha',
                                dst_field='arp_tha',
                                n_bits=48,
                                src_ofs=0,
                                dst_ofs=0),
         parser.NXActionRegMove(src_field='arp_tpa',
                                dst_field=self.AREG,
                                n_bits=32,
                                src_ofs=0,
                                dst_ofs=0),
         parser.NXActionRegMove(src_field='arp_spa',
                                dst_field='arp_tpa',
                                n_bits=32,
                                src_ofs=0,
                                dst_ofs=0),
         parser.NXActionRegMove(src_field=self.AREG,
                                dst_field='arp_spa',
                                n_bits=32,
                                src_ofs=0,
                                dst_ofs=0),
         parser.OFPActionSetField(arp_sha=FAKECLIENTMAC),
     ] + common_reply)
Пример #12
0
    def _get_ue_ul_of_rule(self, ue, path, anchor):
        """
		Write the OpenFlow rules along the computed routing path 
		"""
        ofrs = {}
        try:
            # Forwarding based on destination MAC unicast addr
            in_port = [
                self.switches[path[0].link.src.dpid].ap_conf.port.port_no
            ]
            for p in path:
                for inp in in_port:
                    out_port = p.link.src.port_no
                    if inp == out_port:
                        out_port = ofproto.OFPP_IN_PORT

                    match = ofp_parser.OFPMatch(
                        in_port=inp,
                        eth_type=ether.ETH_TYPE_IPV6,
                        ipv6_src=(':'.join(anchor.nw_prefix),
                                  ipv6_utils.ipv6_mask_from_cidr(
                                      anchor.nw_prefix_len)))
                    actions = [
                        ofp_parser.OFPActionSetField(
                            eth_src=self.switches[p.link.src.dpid].hw_addr),
                        ofp_parser.OFPActionSetField(
                            eth_dst=self.switches[p.link.dst.dpid].hw_addr),
                        ofp_parser.OFPActionOutput(out_port)
                    ]
                    instructions = [
                        ofp_parser.OFPInstructionActions(
                            ofproto.OFPIT_APPLY_ACTIONS, actions)
                    ]

                    ofr = OFRule(self.switches[p.link.src.dpid],
                                 match,
                                 actions,
                                 instructions,
                                 table_id=OF_TABLE_UES)
                    ofrs[ofr.key] = ofr

                    in_port = [p.link.dst.port_no]

        except IndexError:
            self.logger.debug("Index error in _get_ue_ul_of_rule")

        return ofrs
Пример #13
0
	def _get_neigh_of_rule(self, node):
		"""
		Write the OpenFlow rule for forwarding the packet to the node
		(ipv6, hw_addr) lookup and eth_dst address translation
		"""
		ofrs = {}

		match = ofp_parser.OFPMatch(eth_type = ether.ETH_TYPE_IPV6,
									ipv6_dst = node.ipv6_addr)
		actions = [ofp_parser.OFPActionSetField(eth_src = node.attachment.switch.hw_addr),
					ofp_parser.OFPActionSetField(eth_dst = node.hw_addr),
					ofp_parser.OFPActionOutput(node.attachment.port.port_no)]
		instructions = [ofp_parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions)]
		ofr = OFRule(node.attachment.switch, match, actions, instructions, table_id = OF_TABLE_NEIGH)

		ofrs[ofr.key] = ofr

		return ofrs
Пример #14
0
def set_vlan_vid(vlan_vid):
    """Set VLAN VID with VID_PRESENT flag set.

    Args:
        vid (int): VLAN VID
    Returns:
        ryu.ofproto.ofproto_v1_3_parser.OFPActionSetField: set VID with VID_PRESENT.
    """
    return parser.OFPActionSetField(vlan_vid=vid_present(vlan_vid))
Пример #15
0
def set_eth_src(eth_src):
    """Return action to set source Ethernet MAC address.

    Args:
        eth_src (str): source Ethernet MAC address.
    Returns:
        ryu.ofproto.ofproto_v1_3_parser.OFPActionSetField: set field action.
    """
    return parser.OFPActionSetField(eth_src=eth_src)
Пример #16
0
def set_eth_dst(eth_dst):
    """Return action to set destination Ethernet MAC address.

    Args:
        eth_src (str): destination Ethernet MAC address.
    Returns:
        ryu.ofproto.ofproto_v1_3_parser.OFPActionSetField: set field action.
    """
    return parser.OFPActionSetField(eth_dst=eth_dst)
Пример #17
0
def set_field(**kwds):
    """Return action to set any field.

    Args:
        kwds (dict): exactly one field to set
    Returns:
        ryu.ofproto.ofproto_v1_3_parser.OFPActionSetField: set field action.
    """
    return parser.OFPActionSetField(**kwds)
Пример #18
0
def actions_to_ryu(actions, rule):
    """ Converts a list of actions to a list of ryu actions

        This returns both a instruction list and any extra messages that
        are required to install the instructions such as group mod messages.
        Currently this is not smart about reusing groups.

        actions: A iterable list actions such as ActionSet, ActionList,
                 or Bucket.
        rule: The rule being converted
        return: A tuple ([actions], [extra messages])
    """
    ret = []
    extra_messages = []
    for action in actions:
        if action[0] == 'OUTPUT':
            ret.append(parser.OFPActionOutput(action[1]))
        elif action[0] == 'COPY_TTL_OUT':
            ret.append(parser.OFPActionCopyTtlOut())
        elif action[0] == 'COPY_TTL_IN':
            ret.append(parser.OFPActionCopyTtlIn())
        elif action[0] == 'SET_MPLS_TTL':
            ret.append(parser.OFPActionSetMplsTtl(action[1]))
        elif action[0] == 'DEC_MPLS_TTL':
            ret.append(parser.OFPActionDecMplsTtl())
        elif action[0] == 'PUSH_VLAN':
            ret.append(parser.OFPActionPushVlan(action[1]))
        elif action[0] == 'POP_VLAN':
            ret.append(parser.OFPActionPopVlan())
        elif action[0] == 'PUSH_MPLS':
            ret.append(parser.OFPActionPushMpls(action[1]))
        elif action[0] == 'POP_MPLS':
            ret.append(parser.OFPActionPopMpls(action[1]))
        elif action[0] == 'SET_QUEUE':
            ret.append(parser.OFPActionSetQueue(action[1]))
        elif action[0] == 'GROUP':
            if isinstance(action[1], Group):
                group_id, extra = group_to_ryu(action[1], rule)
                ret.append(parser.OFPActionGroup(group_id))
                extra_messages += extra
            else:
                ret.append(parser.OFPActionGroup(action[1]))
        elif action[0] == 'SET_NW_TTL':
            ret.append(parser.OFPActionSetNwTtl(action[1]))
        elif action[0] == 'DEC_NW_TTL':
            ret.append(parser.OFPActionDecNwTtl())
        elif action[0] == 'SET_FIELD':
            set_field = {action[1][0].lower(): action[1][1]}
            ret.append(parser.OFPActionSetField(**set_field))
        elif action[0] == 'PUSH_PBB':
            ret.append(parser.OFPActionPushPbb(action[1]))
        elif action[0] == 'POP_PBB':
            ret.append(parser.OFPActionPopPbb())
        else:
            assert not "GGRR"
    return (ret, extra_messages)
Пример #19
0
    def getReturnAndTagActions(self, switchID, numberOfFlowEntries):
        '''
        Get the return and tag actions (only return actions in 3 flow entries case or the first return action for 4 flow entries case)
        :param switchID: ID of current switch
        :param numberOfFlowEntries: 3 or 4 flow entries mode
        :return: return and tag flow entries to be installed
        '''
        #initialize the actions set to be empty
        actions = []
        #3 flow entries case
        if (3 == numberOfFlowEntries):
            #transfer to "parent" port as is (the packet already contains the switch ID's tagging)
            actions.append(
                ofproto_v1_3_parser.OFPActionOutput(
                    (self.switch_to_parent_port[switchID])))

        #4 flow entries case
        else:
            #In 4 flow entries, the packet contains only tagging on last switch ID (inner tagging) need to add
            #the first switch ID (outer tagging) and change the ethertype to be dirReturnNoTag.
            # Pop vlan
            actions.append(ofproto_v1_3_parser.OFPActionPopVlan())
            #set the DirectionReturn field to be dirReturnNoTag
            actions.append(
                ofproto_v1_3_parser.OFPActionSetField(
                    eth_type=ExtraLayers.dirReturnNoTag))
            # push a new VLAN with ethertype of vlan
            actions.append(
                ofproto_v1_3_parser.OFPActionPushVlan(
                    ethertype=ExtraLayers.VLAN_ID_ETHERTYPE_QINQ))
            # set vlan ID to be current switch (last switch field)
            actions.append(
                ofproto_v1_3_parser.OFPActionSetField(
                    vlan_vid=retMaskedVlanID(switchID)))
            #transfer to "parent" port
            actions.append(
                ofproto_v1_3_parser.OFPActionOutput(
                    (self.switch_to_parent_port[switchID])))

        #return the actions list
        return actions
Пример #20
0
def push_vlan_act(vlan_vid):
    """Return OpenFlow action list to push Ethernet 802.1Q header with VLAN VID.

    Args:
        vid (int): VLAN VID
    Returns:
        list: actions to push 802.1Q header with VLAN VID set.
    """
    return [
        parser.OFPActionPushVlan(ether.ETH_TYPE_8021Q),
        parser.OFPActionSetField(vlan_vid=vid_present(vlan_vid))
    ]
Пример #21
0
 def port_add_vlan_untagged(self, port, vlan, forwarding_table, mirror_act):
     ofmsgs = []
     ofmsgs.extend(self.add_controller_ips(vlan.controller_ips, vlan))
     push_vlan_act = mirror_act + [
         parser.OFPActionPushVlan(ether.ETH_TYPE_8021Q),
         parser.OFPActionSetField(vlan_vid=vlan.vid|ofp.OFPVID_PRESENT)]
     push_vlan_inst = [
         self.apply_actions(push_vlan_act),
         self.goto_table(forwarding_table)
     ]
     null_vlan = namedtuple('null_vlan', 'vid')
     null_vlan.vid = ofp.OFPVID_NONE
     ofmsgs.append(self.valve_flowmod(
         self.dp.vlan_table,
         self.valve_in_match(in_port=port.number, vlan=null_vlan),
         priority=self.dp.low_priority,
         inst=push_vlan_inst))
     ofmsgs.extend(self.build_flood_rules(vlan))
     return ofmsgs
Пример #22
0
    def getRTPReturnActions(self, switchID, inPort):
        '''
        Get the return rtp packet to the given switch.
        :param switchID: ID of current switch.
        :param switchID: in port of switch.
        :return: distribution flow entries to be installed
        '''
        # set as reutrn packet
        actions = []
        actions.append(
            ofproto_v1_3_parser.OFPActionSetField(
                eth_src=ExtraLayers.ReturnNoTagMAC))

        #return the packet in the in port
        returnOutPort = self.switch_to_parent_port[switchID]
        if returnOutPort == inPort:
            returnOutPort = OF_IN_PORT

        actions.append(ofproto_v1_3_parser.OFPActionOutput(returnOutPort))
        return actions
Пример #23
0
    def install_probing(self, datapath):
        '''Redirect node rules'''
        if datapath.id == 2:
            '''Packet duplication group entry'''
            buckets = []
            actions = [
                ofparser.OFPActionSetField(mpls_label=17),
                ofparser.OFPActionOutput(port=3)
            ]
            buckets.append(ofparser.OFPBucket(actions=actions))
            actions = [
                ofparser.OFPActionSetField(mpls_label=100),
                ofparser.OFPActionOutput(port=1)
            ]
            buckets.append(ofparser.OFPBucket(actions=actions))

            req = ofparser.OFPGroupMod(datapath=datapath,
                                       type_=ofproto.OFPGT_ALL,
                                       group_id=0,
                                       buckets=buckets)
            datapath.send_msg(req)
            '''Probing rule: packet duplicated in both primary and detour path'''
            match = ofparser.OFPMatch(in_port=2,
                                      state=100,
                                      eth_dst="00:00:00:00:00:06",
                                      eth_src="00:00:00:00:00:01",
                                      eth_type=0x8847)
            actions = [
                osparser.OFPExpActionSetState(state=17,
                                              table_id=0,
                                              hard_timeout=10,
                                              hard_rollback=100),
                ofparser.OFPActionGroup(0)
            ]
            self.add_flow(datapath=datapath,
                          table_id=0,
                          priority=10,
                          match=match,
                          actions=actions)
            '''Match on probe packet: switch back to the primary path'''
            match = ofparser.OFPMatch(in_port=1,
                                      mpls_label=100,
                                      eth_dst="00:00:00:00:00:06",
                                      eth_src="00:00:00:00:00:01",
                                      eth_type=0x8847)
            actions = [osparser.OFPExpActionSetState(state=0, table_id=0)]
            self.add_flow(datapath=datapath,
                          table_id=0,
                          priority=10,
                          match=match,
                          actions=actions)
            '''Failure: switch on the detour path and set probing timeout'''
            match = ofparser.OFPMatch(in_port=1,
                                      mpls_label=17,
                                      eth_dst="00:00:00:00:00:06",
                                      eth_src="00:00:00:00:00:01",
                                      eth_type=0x8847)
            actions = [
                osparser.OFPExpActionSetState(state=17,
                                              table_id=0,
                                              hard_timeout=10,
                                              hard_rollback=100),
                ofparser.OFPActionOutput(port=3)
            ]
            self.add_flow(datapath=datapath,
                          table_id=0,
                          priority=10,
                          match=match,
                          actions=actions,
                          command=ofproto.OFPFC_MODIFY)
        '''Detect node rules'''
        if datapath.id == 3:
            '''Probe handler group entry'''
            buckets = []
            actions = [ofparser.OFPActionOutput(port=2)]
            buckets.append(ofparser.OFPBucket(watch_port=2, actions=actions))
            actions = []
            buckets.append(ofparser.OFPBucket(watch_port=1, actions=actions))
            req = ofparser.OFPGroupMod(datapath=datapath,
                                       type_=ofproto.OFPGT_FF,
                                       group_id=0,
                                       buckets=buckets)
            datapath.send_msg(req)
            '''Probe handler: if the link is up, the probe packet will be forwarded toward the next node, otherwise drop'''
            match = ofparser.OFPMatch(in_port=1,
                                      state=0,
                                      mpls_label=100,
                                      eth_dst="00:00:00:00:00:06",
                                      eth_src="00:00:00:00:00:01",
                                      eth_type=0x8847)
            actions = [ofparser.OFPActionGroup(group_id=0)]
            self.add_flow(datapath=datapath,
                          table_id=0,
                          priority=100,
                          match=match,
                          actions=actions)
            '''It sends back a probe packet coming from the no more "unreachable node"'''
            match = ofparser.OFPMatch(in_port=2,
                                      state=0,
                                      mpls_label=100,
                                      eth_dst="00:00:00:00:00:06",
                                      eth_src="00:00:00:00:00:01",
                                      eth_type=0x8847)
            actions = [ofparser.OFPActionOutput(1)]
            self.add_flow(datapath=datapath,
                          table_id=0,
                          priority=10,
                          match=match,
                          actions=actions)
        '''Unreachable node'''
        if datapath.id == 4:
            '''probe handler: it sends back a probe message coming from the previous node'''
            match = ofparser.OFPMatch(in_port=1,
                                      state=0,
                                      mpls_label=100,
                                      eth_dst="00:00:00:00:00:06",
                                      eth_src="00:00:00:00:00:01",
                                      eth_type=0x8847)
            actions = [ofparser.OFPActionOutput(ofproto.OFPP_IN_PORT)]
            self.add_flow(datapath=datapath,
                          table_id=0,
                          priority=100,
                          match=match,
                          actions=actions)
Пример #24
0
    def function_dynamic_nat(self, datapath):
        """ Set table 1 as stateful """
        req = bebaparser.OFPExpMsgConfigureStatefulTable(datapath=datapath,
                                                         table_id=1,
                                                         stateful=1)
        datapath.send_msg(req)
        """ Set table 2 restore """
        req = bebaparser.OFPExpMsgConfigureStatefulTable(datapath=datapath,
                                                         table_id=2,
                                                         stateful=1)
        datapath.send_msg(req)

        ############################### LOOKUP/UPDATE ###################################
        """ Tab1 """
        """ Set lookup extractor = {OXM_OF_METADATA} """
        req = bebaparser.OFPExpMsgKeyExtract(
            datapath=datapath,
            command=bebaproto.OFPSC_EXP_SET_L_EXTRACTOR,
            fields=[ofproto.OXM_OF_METADATA],
            table_id=1)
        datapath.send_msg(req)
        """ Set update extractor = {OXM_OF_METADATA}  """
        req = bebaparser.OFPExpMsgKeyExtract(
            datapath=datapath,
            command=bebaproto.OFPSC_EXP_SET_U_EXTRACTOR,
            fields=[ofproto.OXM_OF_METADATA],
            table_id=1)
        datapath.send_msg(req)
        """ Tab2 """
        """ Set lookup extractor = {OXM_OF_IPV4_SRC, OXM_OF_IP_PROTO, OXM_OF_TCP_SRC} """
        req = bebaparser.OFPExpMsgKeyExtract(
            datapath=datapath,
            command=bebaproto.OFPSC_EXP_SET_L_EXTRACTOR,
            fields=[
                ofproto.OXM_OF_IPV4_SRC, ofproto.OXM_OF_IP_PROTO,
                ofproto.OXM_OF_TCP_SRC
            ],
            table_id=2)
        datapath.send_msg(req)
        """ Set lookup extractor = {OXM_OF_IPV4_DST, OXM_OF_IP_PROTO, OXM_OF_TCP_DST} """
        req = bebaparser.OFPExpMsgKeyExtract(
            datapath=datapath,
            command=bebaproto.OFPSC_EXP_SET_U_EXTRACTOR,
            fields=[
                ofproto.OXM_OF_IPV4_DST, ofproto.OXM_OF_IP_PROTO,
                ofproto.OXM_OF_TCP_DST
            ],
            table_id=2)
        datapath.send_msg(req)
        """ Tab3 """

        ########################### SET STATE TABLE 1 ############################################

        for stateVal in range(1, 21):
            state = bebaparser.OFPExpMsgSetFlowState(
                datapath=datapath,
                state=2000 + stateVal,
                keys=[stateVal, 0, 0, 0, 0, 0, 0, 0],
                table_id=1)
            datapath.send_msg(state)

        ########################### SET HF DATA VARIABLE TAB 1 ############################################
        ''' GD[0] = state_label'''
        req = bebaparser.OFPExpMsgsSetGlobalDataVariable(
            datapath=datapath, table_id=1, global_data_variable_id=0, value=0)
        datapath.send_msg(req)
        ''' HF[0] = OXM_EXP_STATE [state_label] '''
        req = bebaparser.OFPExpMsgHeaderFieldExtract(
            datapath=datapath,
            table_id=1,
            extractor_id=0,
            field=bebaproto.OXM_EXP_STATE)
        datapath.send_msg(req)

        ########################### SET HF DATA VARIABLE TAB 2 ############################################
        ''' HF[0] = OXM_OF_IPV4_SRC [id_pkt] '''
        req = bebaparser.OFPExpMsgHeaderFieldExtract(
            datapath=datapath,
            table_id=2,
            extractor_id=0,
            field=ofproto.OXM_OF_IPV4_SRC)
        datapath.send_msg(req)
        ''' HF[1] = OXM_OF_TCP_SRC [id_pkt] '''
        req = bebaparser.OFPExpMsgHeaderFieldExtract(
            datapath=datapath,
            table_id=2,
            extractor_id=1,
            field=ofproto.OXM_OF_TCP_SRC)
        datapath.send_msg(req)

        ########################### SET HF DATA VARIABLE TAB 3 ############################################
        ''' HF[0] = OXM_OF_METADATA [metadata] '''
        req = bebaparser.OFPExpMsgHeaderFieldExtract(
            datapath=datapath,
            table_id=3,
            extractor_id=0,
            field=ofproto.OXM_OF_METADATA)
        datapath.send_msg(req)

        ################################# REGOLE ############################################
        ''' #######################  TAB 0  '''

        match = ofparser.OFPMatch(state=0, in_port=LAN_PORT)
        actions = [
            bebaparser.OFPExpActionSetState(state=1, table_id=0),
            bebaparser.OFPExpActionSetDataVariable(table_id=0,
                                                   opcode=bebaproto.OPCODE_SUM,
                                                   output_gd_id=1,
                                                   operand_1_gd_id=1,
                                                   operand_2_cost=1),
            bebaparser.OFPExpActionWriteContextToField(
                src_type=bebaproto.SOURCE_TYPE_GLOBAL_DATA_VAR,
                src_id=1,
                dst_field=ofproto.OXM_OF_METADATA)
        ]
        inst = [
            ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
                                           actions),
            ofparser.OFPInstructionGotoTable(1)
        ]
        mod = ofparser.OFPFlowMod(datapath=datapath,
                                  table_id=0,
                                  priority=89,
                                  match=match,
                                  instructions=inst)
        datapath.send_msg(mod)

        # Line 12
        match = ofparser.OFPMatch(state=1, in_port=LAN_PORT)
        actions = []
        inst = [
            ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
                                           actions),
            ofparser.OFPInstructionGotoTable(3)
        ]
        mod = ofparser.OFPFlowMod(datapath=datapath,
                                  table_id=0,
                                  priority=88,
                                  match=match,
                                  instructions=inst)
        datapath.send_msg(mod)

        # Line 13
        match = ofparser.OFPMatch(state=0,
                                  in_port=INTERNET_PORT,
                                  eth_type=0x0800,
                                  ipv4_dst='1.0.0.1')
        actions = []
        inst = [
            ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
                                           actions),
            ofparser.OFPInstructionGotoTable(2)
        ]
        mod = ofparser.OFPFlowMod(datapath=datapath,
                                  table_id=0,
                                  priority=87,
                                  match=match,
                                  instructions=inst)
        datapath.send_msg(mod)
        ''' #######################  TAB 1  '''

        # Line 0
        # HF[0] = state_label
        # GD[0] = state_label + 0 => GD[0] + HF[0]
        # state_label -> metadata => GD[0] = HF[0]
        match = ofparser.OFPMatch(in_port=3)
        actions = [
            bebaparser.OFPExpActionSetDataVariable(table_id=1,
                                                   opcode=bebaproto.OPCODE_SUM,
                                                   output_gd_id=0,
                                                   operand_1_hf_id=0,
                                                   operand_2_cost=0),
            bebaparser.OFPExpActionWriteContextToField(
                src_type=bebaproto.SOURCE_TYPE_GLOBAL_DATA_VAR,
                src_id=0,
                dst_field=ofproto.OXM_OF_METADATA)
        ]
        inst = [
            ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
                                           actions),
            ofparser.OFPInstructionGotoTable(2)
        ]
        mod = ofparser.OFPFlowMod(datapath=datapath,
                                  table_id=1,
                                  priority=100,
                                  match=match,
                                  instructions=inst)
        datapath.send_msg(mod)
        ''' #######################  TAB 2 restore'''

        # Line 0
        # ip.src -> R0 => HF[0] -> FD[0] => FD[0] = HF[0] + 0
        # tcp.src -> R1 => HF[1] -> FD[1] => FD[1] = HF[1] + 0
        match = ofparser.OFPMatch(state=0, in_port=LAN_PORT)
        actions = [
            bebaparser.OFPExpActionSetState(state=1, table_id=2),
            bebaparser.OFPExpActionSetDataVariable(table_id=2,
                                                   opcode=bebaproto.OPCODE_SUM,
                                                   output_fd_id=0,
                                                   operand_1_hf_id=0,
                                                   operand_2_cost=0),
            bebaparser.OFPExpActionSetDataVariable(table_id=2,
                                                   opcode=bebaproto.OPCODE_SUM,
                                                   output_fd_id=1,
                                                   operand_1_hf_id=1,
                                                   operand_2_cost=0)
        ]
        inst = [
            ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
                                           actions),
            ofparser.OFPInstructionGotoTable(3)
        ]
        mod = ofparser.OFPFlowMod(datapath=datapath,
                                  table_id=2,
                                  priority=100,
                                  match=match,
                                  instructions=inst)
        datapath.send_msg(mod)

        # Line 1
        # ip.dst = R0 => IPV4_DST = FD[0]
        # tcp.dst = R1 => TCP_DST = FD[1]
        match = ofparser.OFPMatch(state=1, in_port=INTERNET_PORT)
        actions = [
            bebaparser.OFPExpActionSetState(state=1, table_id=2),
            bebaparser.OFPExpActionWriteContextToField(
                src_type=bebaproto.SOURCE_TYPE_FLOW_DATA_VAR,
                src_id=0,
                dst_field=ofproto.OXM_OF_IPV4_DST),
            bebaparser.OFPExpActionWriteContextToField(
                src_type=bebaproto.SOURCE_TYPE_FLOW_DATA_VAR,
                src_id=1,
                dst_field=ofproto.OXM_OF_TCP_DST)
        ]
        inst = [
            ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
                                           actions),
            ofparser.OFPInstructionGotoTable(4)
        ]
        mod = ofparser.OFPFlowMod(datapath=datapath,
                                  table_id=2,
                                  priority=99,
                                  match=match,
                                  instructions=inst)
        datapath.send_msg(mod)
        ''' #######################  TAB 3 translate '''

        # Line 3
        # metadata(b16,b31) -> R1 => metadata(b16,b31) -> FD[1] => FD[1] = HF[0]
        # ip.src = 10.0.0.1
        # tcp.src = R1 => TCP_SRC = FD[1]
        match = ofparser.OFPMatch(state=0, in_port=LAN_PORT, eth_type=0x0800)
        actions = [
            bebaparser.OFPExpActionSetState(state=1, table_id=3),
            bebaparser.OFPExpActionSetDataVariable(table_id=3,
                                                   opcode=bebaproto.OPCODE_SUM,
                                                   output_fd_id=1,
                                                   operand_1_hf_id=0,
                                                   operand_2_cost=0),
            ofparser.OFPActionSetField(ipv4_src="1.0.0.1"),
            bebaparser.OFPExpActionWriteContextToField(
                src_type=bebaproto.SOURCE_TYPE_FLOW_DATA_VAR,
                src_id=1,
                dst_field=ofproto.OXM_OF_TCP_SRC)
        ]
        inst = [
            ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
                                           actions),
            ofparser.OFPInstructionGotoTable(4)
        ]
        mod = ofparser.OFPFlowMod(datapath=datapath,
                                  table_id=3,
                                  priority=97,
                                  match=match,
                                  instructions=inst)
        datapath.send_msg(mod)

        # Line 4
        # ip.src = 10.0.0.1
        # tcp.src = R1 => TCP_SRC = FD[1]
        match = ofparser.OFPMatch(state=1, in_port=LAN_PORT, eth_type=0x0800)
        actions = [
            bebaparser.OFPExpActionSetState(state=1, table_id=3),
            ofparser.OFPActionSetField(ipv4_src='1.0.0.1'),
            bebaparser.OFPExpActionWriteContextToField(
                src_type=bebaproto.SOURCE_TYPE_FLOW_DATA_VAR,
                src_id=1,
                dst_field=ofproto.OXM_OF_TCP_SRC)
        ]
        inst = [
            ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
                                           actions),
            ofparser.OFPInstructionGotoTable(4)
        ]
        mod = ofparser.OFPFlowMod(datapath=datapath,
                                  table_id=3,
                                  priority=96,
                                  match=match,
                                  instructions=inst)
        datapath.send_msg(mod)
Пример #25
0
    def function_load_balancer(self, datapath):
        """ Tab0 """
        """ Set lookup extractor = {BiFlow} """
        # GIA CONFIGURATA
        """ Set table 3 translate """
        req = bebaparser.OFPExpMsgConfigureStatefulTable(datapath=datapath,
                                                         table_id=3,
                                                         stateful=1)
        datapath.send_msg(req)

        ############################### LOOKUP/UPDATE ###################################
        """ Tab3 """
        """ Set lookup extractor = {OXM_OF_IPV4_SRC, OXM_OF_TCP_SRC} """
        req = bebaparser.OFPExpMsgKeyExtract(
            datapath=datapath,
            command=bebaproto.OFPSC_EXP_SET_L_EXTRACTOR,
            fields=[ofproto.OXM_OF_IPV4_SRC, ofproto.OXM_OF_TCP_SRC],
            table_id=3)
        datapath.send_msg(req)
        """ Set update extractor = {OXM_OF_IPV4_SRC, OXM_OF_TCP_SRC} """
        req = bebaparser.OFPExpMsgKeyExtract(
            datapath=datapath,
            command=bebaproto.OFPSC_EXP_SET_U_EXTRACTOR,
            fields=[ofproto.OXM_OF_IPV4_SRC, ofproto.OXM_OF_TCP_SRC],
            table_id=3)
        datapath.send_msg(req)

        ########################### SET GD E HF DATA VARIABLE TAB 3 ############################################
        ''' GD[0] = 0'''
        req = bebaparser.OFPExpMsgsSetGlobalDataVariable(
            datapath=datapath, table_id=3, global_data_variable_id=0, value=0)
        datapath.send_msg(req)
        ''' GD[1] = LAN_DUE 10.0.0.2 hexadecimal'''
        req = bebaparser.OFPExpMsgsSetGlobalDataVariable(
            datapath=datapath,
            table_id=3,
            global_data_variable_id=1,
            value=0x0200000a)
        datapath.send_msg(req)
        ''' GD[2] = LAN_TRE 10.0.0.3 hexadecimal'''
        req = bebaparser.OFPExpMsgsSetGlobalDataVariable(
            datapath=datapath,
            table_id=3,
            global_data_variable_id=2,
            value=0x0300000a)
        datapath.send_msg(req)
        ''' GD[3] = PORT 80 '''
        req = bebaparser.OFPExpMsgsSetGlobalDataVariable(
            datapath=datapath,
            table_id=3,
            global_data_variable_id=3,
            value=0x5000)
        datapath.send_msg(req)

        ################################# RULES ############################################
        ''' #######################  TAB 0  '''

        # Line 7
        match = ofparser.OFPMatch(state=0,
                                  in_port=INTERNET_PORT,
                                  eth_type=0x0800,
                                  ipv4_dst='1.0.0.1',
                                  ip_proto=6,
                                  tcp_dst=80)
        actions = [
            bebaparser.OFPExpActionSetState(state=1, table_id=0),
            bebaparser.OFPExpActionSetDataVariable(table_id=0,
                                                   opcode=bebaproto.OPCODE_SUM,
                                                   output_gd_id=0,
                                                   operand_1_gd_id=0,
                                                   operand_2_cost=1),
            bebaparser.OFPExpActionWriteContextToField(
                src_type=bebaproto.SOURCE_TYPE_GLOBAL_DATA_VAR,
                src_id=0,
                dst_field=ofproto.OXM_OF_METADATA)
        ]
        inst = [
            ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
                                           actions),
            ofparser.OFPInstructionGotoTable(3)
        ]
        mod = ofparser.OFPFlowMod(datapath=datapath,
                                  table_id=0,
                                  priority=93,
                                  match=match,
                                  instructions=inst)
        datapath.send_msg(mod)

        # Line 8
        match = ofparser.OFPMatch(state=1,
                                  in_port=INTERNET_PORT,
                                  eth_type=0x0800,
                                  ipv4_dst='1.0.0.1',
                                  ip_proto=6,
                                  tcp_dst=80)
        actions = []
        inst = [
            ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
                                           actions),
            ofparser.OFPInstructionGotoTable(3)
        ]
        mod = ofparser.OFPFlowMod(datapath=datapath,
                                  table_id=0,
                                  priority=92,
                                  match=match,
                                  instructions=inst)
        datapath.send_msg(mod)

        # Line 9
        match = ofparser.OFPMatch(state=0,
                                  in_port=LAN_PORT,
                                  eth_type=0x0800,
                                  ipv4_src='10.0.0.2',
                                  ip_proto=6,
                                  tcp_src=80)
        actions = []
        inst = [
            ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
                                           actions),
            ofparser.OFPInstructionGotoTable(4)
        ]
        mod = ofparser.OFPFlowMod(datapath=datapath,
                                  table_id=0,
                                  priority=91,
                                  match=match,
                                  instructions=inst)
        datapath.send_msg(mod)

        # Line 10
        match = ofparser.OFPMatch(state=0,
                                  in_port=LAN_PORT,
                                  eth_type=0x0800,
                                  ipv4_src='10.0.0.3',
                                  ip_proto=6,
                                  tcp_src=80)
        actions = []
        inst = [
            ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
                                           actions),
            ofparser.OFPInstructionGotoTable(4)
        ]
        mod = ofparser.OFPFlowMod(datapath=datapath,
                                  table_id=0,
                                  priority=90,
                                  match=match,
                                  instructions=inst)
        datapath.send_msg(mod)
        ''' #######################  TAB 3 translate '''

        # Line 0
        # ip.dst = 10.0.0.2
        # tcp.dst = 80
        # 10.0.0.2 -> R0 => 10.0.0.2 -> FD[0] => FD[0] = GD[0] + 10.0.0.2 => 0 + 10.0.0.2
        # 80 -> R1 		 => 80 -> FD[1] 	  => GD[0] + 80 			  => 0 + 80
        match = ofparser.OFPMatch(state=0,
                                  in_port=INTERNET_PORT,
                                  metadata=(0, 0x000000001),
                                  eth_type=0x0800,
                                  ip_proto=6)
        actions = [
            bebaparser.OFPExpActionSetState(state=1, table_id=3),
            ofparser.OFPActionSetField(ipv4_dst='10.0.0.2'),
            ofparser.OFPActionSetField(tcp_dst=80),
            # tolto per test
            bebaparser.OFPExpActionSetDataVariable(table_id=3,
                                                   opcode=bebaproto.OPCODE_SUM,
                                                   output_fd_id=0,
                                                   operand_1_gd_id=1,
                                                   operand_2_cost=0),
            bebaparser.OFPExpActionSetDataVariable(table_id=3,
                                                   opcode=bebaproto.OPCODE_SUM,
                                                   output_fd_id=1,
                                                   operand_1_gd_id=0,
                                                   operand_2_cost=80)
        ]
        inst = [
            ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
                                           actions),
            ofparser.OFPInstructionGotoTable(4)
        ]
        mod = ofparser.OFPFlowMod(datapath=datapath,
                                  table_id=3,
                                  priority=100,
                                  match=match,
                                  instructions=inst)
        datapath.send_msg(mod)

        # Line 1
        # ip.dst = 10.0.0.3
        # tcp.dst = 80
        # 10.0.0.3 -> R0 => 10.0.0.3 -> FD[0] => FD[0] = GD[0] + 10.0.0.3 => 0 + 10.0.0.3
        # 80 -> R1 		 => 80 -> FD[1] 	  => GD[0] + 80 			  => 0 + 80
        match = ofparser.OFPMatch(state=0,
                                  in_port=INTERNET_PORT,
                                  metadata=(1, 0x000000001),
                                  eth_type=0x0800,
                                  ip_proto=6)
        actions = [
            bebaparser.OFPExpActionSetState(state=1, table_id=3),
            ofparser.OFPActionSetField(ipv4_dst='10.0.0.3'),
            ofparser.OFPActionSetField(tcp_dst=80),
            # tolto per test
            bebaparser.OFPExpActionSetDataVariable(table_id=3,
                                                   opcode=bebaproto.OPCODE_SUM,
                                                   output_fd_id=0,
                                                   operand_1_gd_id=2,
                                                   operand_2_cost=0),
            bebaparser.OFPExpActionSetDataVariable(table_id=3,
                                                   opcode=bebaproto.OPCODE_SUM,
                                                   output_fd_id=1,
                                                   operand_1_gd_id=0,
                                                   operand_2_cost=80)
        ]
        inst = [
            ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
                                           actions),
            ofparser.OFPInstructionGotoTable(4)
        ]
        mod = ofparser.OFPFlowMod(datapath=datapath,
                                  table_id=3,
                                  priority=99,
                                  match=match,
                                  instructions=inst)
        datapath.send_msg(mod)

        # Line 2
        # ip.dst = R0 => IPV4_DST = FD[0]
        # tcp.dst = R1 => TCP_DST = FD[1]
        match = ofparser.OFPMatch(state=1, in_port=INTERNET_PORT)
        actions = [
            bebaparser.OFPExpActionWriteContextToField(
                src_type=bebaproto.SOURCE_TYPE_FLOW_DATA_VAR,
                src_id=0,
                dst_field=ofproto.OXM_OF_IPV4_DST),
            bebaparser.OFPExpActionWriteContextToField(
                src_type=bebaproto.SOURCE_TYPE_FLOW_DATA_VAR,
                src_id=1,
                dst_field=ofproto.OXM_OF_TCP_DST)
        ]
        inst = [
            ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
                                           actions),
            ofparser.OFPInstructionGotoTable(4)
        ]
        mod = ofparser.OFPFlowMod(datapath=datapath,
                                  table_id=3,
                                  priority=98,
                                  match=match,
                                  instructions=inst)
        datapath.send_msg(mod)
        ''' #######################  TAB 4 forward '''

        # Line 4
        match = ofparser.OFPMatch(in_port=LAN_PORT,
                                  eth_type=0x0800,
                                  ipv4_src='10.0.0.2',
                                  ip_proto=6,
                                  tcp_src=80)
        actions = [
            ofparser.OFPActionSetField(ipv4_src='1.0.0.1'),
            ofparser.OFPActionSetField(tcp_src=80),
            ofparser.OFPActionSetField(eth_dst="00:00:00:00:00:01"),
            ofparser.OFPActionOutput(INTERNET_PORT)
        ]
        self.add_flow(datapath=datapath,
                      table_id=4,
                      priority=96,
                      match=match,
                      actions=actions)

        # Line 5
        match = ofparser.OFPMatch(in_port=LAN_PORT,
                                  eth_type=0x0800,
                                  ipv4_src='10.0.0.3',
                                  ip_proto=6,
                                  tcp_src=80)
        actions = [
            ofparser.OFPActionSetField(ipv4_src='1.0.0.1'),
            ofparser.OFPActionSetField(tcp_src=80),
            ofparser.OFPActionSetField(eth_dst="00:00:00:00:00:01"),
            ofparser.OFPActionOutput(INTERNET_PORT)
        ]
        self.add_flow(datapath=datapath,
                      table_id=4,
                      priority=95,
                      match=match,
                      actions=actions)

        # Line 6
        match = ofparser.OFPMatch()
        actions = [
            ofparser.OFPActionSetField(eth_dst="00:00:00:00:00:01"),
            ofparser.OFPActionOutput(INTERNET_PORT)
        ]
        self.add_flow(datapath=datapath,
                      table_id=4,
                      priority=94,
                      match=match,
                      actions=actions)
Пример #26
0
    def function_lan_dmz_isolation(self, datapath):
        """ Set table 0 as stateful """
        req = bebaparser.OFPExpMsgConfigureStatefulTable(datapath=datapath,
                                                         table_id=0,
                                                         stateful=1)
        datapath.send_msg(req)

        ############################### LOOKUP/UPDATE ###################################
        """ Tab0 """
        """ Set lookup extractor = {BiFlow} """
        req = bebaparser.OFPExpMsgKeyExtract(
            datapath=datapath,
            command=bebaproto.OFPSC_EXP_SET_L_EXTRACTOR,
            fields=[
                ofproto.OXM_OF_IPV4_SRC, ofproto.OXM_OF_IPV4_DST,
                ofproto.OXM_OF_TCP_SRC, ofproto.OXM_OF_TCP_DST
            ],
            table_id=0,
            biflow=1)
        datapath.send_msg(req)
        """ Set lookup extractor = {BiFlow} """
        req = bebaparser.OFPExpMsgKeyExtract(
            datapath=datapath,
            command=bebaproto.OFPSC_EXP_SET_U_EXTRACTOR,
            fields=[
                ofproto.OXM_OF_IPV4_SRC, ofproto.OXM_OF_IPV4_DST,
                ofproto.OXM_OF_TCP_SRC, ofproto.OXM_OF_TCP_DST
            ],
            table_id=0,
            biflow=1)
        datapath.send_msg(req)
        """ Tab4 """
        """ Stateless """

        ########################### SET GD DATA VARIABLE TAB 0 ############################################
        ''' GD[0] = 0'''
        req = bebaparser.OFPExpMsgsSetGlobalDataVariable(
            datapath=datapath, table_id=0, global_data_variable_id=0, value=0)
        datapath.send_msg(req)
        ''' GD[1] = 0 '''
        req = bebaparser.OFPExpMsgsSetGlobalDataVariable(
            datapath=datapath, table_id=0, global_data_variable_id=0, value=0)
        datapath.send_msg(req)

        ################################# REGOLE ############################################

        match = ofparser.OFPMatch(eth_type=0x0806)
        actions = [ofparser.OFPActionOutput(ofproto.OFPP_FLOOD)]
        inst = [
            ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
                                           actions)
        ]
        mod = ofparser.OFPFlowMod(datapath=datapath,
                                  table_id=0,
                                  priority=100,
                                  match=match,
                                  instructions=inst)
        datapath.send_msg(mod)
        ''' #######################  TAB 0  '''
        # Line 0
        match = ofparser.OFPMatch(state=0,
                                  in_port=DMZ_PORT,
                                  eth_type=0x0800,
                                  ipv4_dst=('10.0.0.0', '255.255.255.0'))
        actions = [bebaparser.OFPExpActionSetState(state=11, table_id=0)]
        inst = [
            ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
                                           actions),
            ofparser.OFPInstructionWriteMetadata(metadata=0,
                                                 metadata_mask=0xFFFFFFFF),
            ofparser.OFPInstructionGotoTable(4)
        ]
        mod = ofparser.OFPFlowMod(datapath=datapath,
                                  table_id=0,
                                  priority=100,
                                  match=match,
                                  instructions=inst)
        datapath.send_msg(mod)

        # Line 1
        match = ofparser.OFPMatch(state=0,
                                  in_port=LAN_PORT,
                                  eth_type=0x0800,
                                  ipv4_dst=('8.0.0.0', '255.255.255.0'))
        actions = [bebaparser.OFPExpActionSetState(state=12, table_id=0)]
        inst = [
            ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
                                           actions),
            ofparser.OFPInstructionWriteMetadata(metadata=0,
                                                 metadata_mask=0xFFFFFFFF),
            ofparser.OFPInstructionGotoTable(4)
        ]
        mod = ofparser.OFPFlowMod(datapath=datapath,
                                  table_id=0,
                                  priority=99,
                                  match=match,
                                  instructions=inst)
        datapath.send_msg(mod)

        # Line 2
        match = ofparser.OFPMatch(state=11,
                                  in_port=DMZ_PORT,
                                  eth_type=0x0800,
                                  ipv4_dst=('10.0.0.0', '255.255.255.0'))
        actions = []
        inst = [
            ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
                                           actions),
            ofparser.OFPInstructionWriteMetadata(metadata=0,
                                                 metadata_mask=0xFFFFFFFF),
            ofparser.OFPInstructionGotoTable(4)
        ]
        mod = ofparser.OFPFlowMod(datapath=datapath,
                                  table_id=0,
                                  priority=98,
                                  match=match,
                                  instructions=inst)
        datapath.send_msg(mod)

        # Line 3
        match = ofparser.OFPMatch(state=11,
                                  in_port=LAN_PORT,
                                  eth_type=0x0800,
                                  ipv4_dst=('8.0.0.0', '255.255.255.0'))
        actions = [bebaparser.OFPExpActionSetState(state=2, table_id=0)]
        inst = [
            ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
                                           actions),
            ofparser.OFPInstructionWriteMetadata(metadata=1,
                                                 metadata_mask=0xFFFFFFFF),
            ofparser.OFPInstructionGotoTable(4)
        ]
        mod = ofparser.OFPFlowMod(datapath=datapath,
                                  table_id=0,
                                  priority=97,
                                  match=match,
                                  instructions=inst)
        datapath.send_msg(mod)

        # Line 4
        match = ofparser.OFPMatch(state=12,
                                  in_port=LAN_PORT,
                                  eth_type=0x0800,
                                  ipv4_dst=('8.0.0.0', '255.255.255.0'))
        actions = []
        inst = [
            ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
                                           actions),
            ofparser.OFPInstructionWriteMetadata(metadata=0,
                                                 metadata_mask=0xFFFFFFFF),
            ofparser.OFPInstructionGotoTable(4)
        ]
        mod = ofparser.OFPFlowMod(datapath=datapath,
                                  table_id=0,
                                  priority=96,
                                  match=match,
                                  instructions=inst)
        datapath.send_msg(mod)

        # Line 5
        match = ofparser.OFPMatch(state=12,
                                  in_port=DMZ_PORT,
                                  eth_type=0x0800,
                                  ipv4_dst=('10.0.0.0', '255.255.255.0'))
        actions = [bebaparser.OFPExpActionSetState(state=2, table_id=0)]
        inst = [
            ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
                                           actions),
            ofparser.OFPInstructionWriteMetadata(metadata=1,
                                                 metadata_mask=0xFFFFFFFF),
            ofparser.OFPInstructionGotoTable(4)
        ]
        mod = ofparser.OFPFlowMod(datapath=datapath,
                                  table_id=0,
                                  priority=95,
                                  match=match,
                                  instructions=inst)
        datapath.send_msg(mod)

        # Line 6
        match = ofparser.OFPMatch(state=2)
        actions = []
        inst = [
            ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
                                           actions),
            ofparser.OFPInstructionWriteMetadata(metadata=1,
                                                 metadata_mask=0xFFFFFFFF),
            ofparser.OFPInstructionGotoTable(4)
        ]
        mod = ofparser.OFPFlowMod(datapath=datapath,
                                  table_id=0,
                                  priority=94,
                                  match=match,
                                  instructions=inst)
        datapath.send_msg(mod)
        ''' #######################  TAB 1   '''
        # NOT USED IN THIS USE CASE
        ''' #######################  TAB 2 restore'''
        # NOT USED IN THIS USE CASE
        ''' #######################  TAB 3 translate '''
        # NOT USED IN THIS USE CASE
        ''' #######################  TAB 4 forward '''

        # Line 0
        match = ofparser.OFPMatch(in_port=DMZ_PORT,
                                  metadata=(0, 0x00000000F),
                                  eth_type=0x0800,
                                  ipv4_dst=('10.0.0.0', '255.255.255.0'))
        actions = []
        self.add_flow(datapath=datapath,
                      table_id=4,
                      priority=100,
                      match=match,
                      actions=actions)

        # Line 1
        match = ofparser.OFPMatch(in_port=DMZ_PORT,
                                  metadata=(1, 0x00000000F),
                                  eth_type=0x0800,
                                  ipv4_dst='10.0.0.2')
        actions = [
            ofparser.OFPActionSetField(eth_dst="00:00:00:00:00:03"),
            ofparser.OFPActionOutput(LAN_PORT)
        ]
        self.add_flow(datapath=datapath,
                      table_id=4,
                      priority=99,
                      match=match,
                      actions=actions)

        # Line 1 BIS
        match = ofparser.OFPMatch(in_port=DMZ_PORT,
                                  metadata=(1, 0x00000000F),
                                  eth_type=0x0800,
                                  ipv4_dst='10.0.0.3')
        actions = [
            ofparser.OFPActionSetField(eth_dst="00:00:00:00:00:04"),
            ofparser.OFPActionOutput(LAN_PORT)
        ]
        self.add_flow(datapath=datapath,
                      table_id=4,
                      priority=99,
                      match=match,
                      actions=actions)

        # Line 2
        match = ofparser.OFPMatch(eth_type=0x0800, ipv4_dst='8.0.0.2')
        actions = [
            ofparser.OFPActionSetField(eth_dst="00:00:00:00:00:02"),
            ofparser.OFPActionOutput(DMZ_PORT)
        ]
        self.add_flow(datapath=datapath,
                      table_id=4,
                      priority=98,
                      match=match,
                      actions=actions)

        # Line 3
        match = ofparser.OFPMatch(eth_type=0x0800, ipv4_dst='10.0.0.2')
        actions = [
            ofparser.OFPActionSetField(eth_dst="00:00:00:00:00:03"),
            ofparser.OFPActionOutput(LAN_PORT)
        ]
        self.add_flow(datapath=datapath,
                      table_id=4,
                      priority=97,
                      match=match,
                      actions=actions)

        # Line 3 BIS
        match = ofparser.OFPMatch(eth_type=0x0800, ipv4_dst='10.0.0.3')
        actions = [
            ofparser.OFPActionSetField(eth_dst="00:00:00:00:00:04"),
            ofparser.OFPActionOutput(LAN_PORT)
        ]
        self.add_flow(datapath=datapath,
                      table_id=4,
                      priority=97,
                      match=match,
                      actions=actions)
Пример #27
0
 def set_vlan(self, vlan_id):
     self.actions_fields.append(ofp.OFPActionPushVlan())
     self.actions_fields.append(ofp.OFPActionSetField(vlan_vid=(0x1000 | vlan_id)))
     return self
Пример #28
0
 def set_mac_rewrite(self, eth_src):
     self.actions_fields.append(ofp.OFPActionSetField(eth_src=eth_src))
     return self
Пример #29
0
    def install_leaves(self, datapath):
        # Send probe packet to packet generation table
        req = bebaparser.OFPExpMsgAddPktTmp(datapath=datapath,
                                            pkttmp_id=0,
                                            pkt_data=pkt_raw)
        datapath.send_msg(req)

        ##################################### TABLE 0: DISPATCHING ##################################################

        ######################### TABLE 0 CONFIG ###############
        req = bebaparser.OFPExpMsgConfigureStatefulTable(datapath=datapath,
                                                         table_id=0,
                                                         stateful=1)
        datapath.send_msg(req)
        """ Set lookup extractor = {eth_dst} """
        req = bebaparser.OFPExpMsgKeyExtract(
            datapath=datapath,
            command=bebaproto.OFPSC_EXP_SET_L_EXTRACTOR,
            fields=[ofproto.OXM_OF_IN_PORT],
            table_id=0)
        datapath.send_msg(req)
        """ Set update extractor = {eth_dst}  """
        req = bebaparser.OFPExpMsgKeyExtract(
            datapath=datapath,
            command=bebaproto.OFPSC_EXP_SET_U_EXTRACTOR,
            fields=[ofproto.OXM_OF_IN_PORT],
            table_id=0)
        datapath.send_msg(req)
        """ Field extractor for mpls label """
        req = bebaparser.OFPExpMsgHeaderFieldExtract(
            datapath=datapath,
            table_id=0,
            extractor_id=0,
            field=ofproto.OXM_OF_MPLS_LABEL)
        datapath.send_msg(req)
        """ Field extractor for timestamp """
        req = bebaparser.OFPExpMsgHeaderFieldExtract(
            datapath=datapath,
            table_id=0,
            extractor_id=1,
            field=bebaproto.OXM_EXP_TIMESTAMP)
        datapath.send_msg(req)
        """ Packet counter_max for designing probe frequency """
        req = bebaparser.OFPExpMsgsSetGlobalDataVariable(
            datapath=datapath,
            table_id=0,
            global_data_variable_id=0,
            value=PROBE_FREQ)
        datapath.send_msg(req)
        """ Condition C0: if counter reaches counter_max, then trigger probe sending """
        req = bebaparser.OFPExpMsgSetCondition(
            datapath=datapath,
            table_id=0,
            condition_id=0,
            condition=bebaproto.CONDITION_GTE,
            operand_1_fd_id=0,
            operand_2_gd_id=0)
        datapath.send_msg(req)

        req = bebaparser.OFPExpMsgSetCondition(
            datapath=datapath,
            table_id=0,
            condition_id=3,
            condition=bebaproto.CONDITION_GTE,
            operand_1_fd_id=0,
            operand_2_gd_id=0)
        datapath.send_msg(req)

        ##################################### TABLE 0 FLOWS ##################################################
        """ RECEIVE PROBE ACTION """
        """ When a probe is received tab0 sends it to tab3"""
        """ match: 	MPLS """
        """ no action """
        """ instruction: goto tab3"""

        match = ofparser.OFPMatch(eth_type=0x8847)
        instructions = [ofparser.OFPInstructionGotoTable(3)]
        mod = ofparser.OFPFlowMod(datapath=datapath,
                                  table_id=0,
                                  priority=200,
                                  match=match,
                                  instructions=instructions)
        datapath.send_msg(mod)
        """ For packets from down ports (attached to hosts): go to ToR Discovery table (1) """
        for i in DOWN_PORTS:
            match = ofparser.OFPMatch(in_port=i)
            instructions = [ofparser.OFPInstructionGotoTable(1)]
            mod = ofparser.OFPFlowMod(datapath=datapath,
                                      table_id=0,
                                      priority=10,
                                      match=match,
                                      instructions=instructions)
            datapath.send_msg(mod)

        match = ofparser.OFPMatch()
        instructions = [ofparser.OFPInstructionGotoTable(2)]
        mod = ofparser.OFPFlowMod(datapath=datapath,
                                  table_id=0,
                                  priority=0,
                                  match=match,
                                  instructions=instructions)
        datapath.send_msg(mod)

        # using low bitrate flows to refresh the estimates. For testing purposes only
        for i in [0, 1]:
            match = ofparser.OFPMatch(in_port=3,
                                      eth_type=0x0800,
                                      ip_proto=6,
                                      tcp_dst=10000 + i)
            actions = [ofparser.OFPActionOutput(i + 1)]
            self.add_flow(datapath=datapath,
                          table_id=0,
                          priority=200,
                          match=match,
                          actions=actions)

            match = ofparser.OFPMatch(in_port=3,
                                      eth_type=0x0800,
                                      ip_proto=6,
                                      tcp_src=10000 + i)
            actions = [ofparser.OFPActionOutput(i + 1)]
            self.add_flow(datapath=datapath,
                          table_id=0,
                          priority=200,
                          match=match,
                          actions=actions)

        ######################## TABLE 1 ToR DISCOVERY  #########################################################

        # this cycle writes metadata specifying to which leaf belongs the packet
        for i in LEAVES:
            if i != datapath.id:
                match = ofparser.OFPMatch(eth_dst=MAC_ADDRS[i - 1])
                instructions = [
                    ofparser.OFPInstructionWriteMetadata(
                        metadata=i, metadata_mask=0xffffffff),
                    ofparser.OFPInstructionGotoTable(3)
                ]
                mod = ofparser.OFPFlowMod(datapath=datapath,
                                          table_id=1,
                                          priority=0,
                                          match=match,
                                          instructions=instructions)
                datapath.send_msg(mod)

        ######################### TABLE 2: ACTIVE PROBING ######################################################

        ################### TABLE 2 CONFIG #########

        req = bebaparser.OFPExpMsgConfigureStatefulTable(datapath=datapath,
                                                         table_id=2,
                                                         stateful=1)
        datapath.send_msg(req)
        """ Set lookup extractor = {IN_PORT} """
        req = bebaparser.OFPExpMsgKeyExtract(
            datapath=datapath,
            command=bebaproto.OFPSC_EXP_SET_L_EXTRACTOR,
            fields=[ofproto.OXM_OF_IN_PORT],
            table_id=2)
        datapath.send_msg(req)
        """ Set update extractor = {IN_PORT}  """
        req = bebaparser.OFPExpMsgKeyExtract(
            datapath=datapath,
            command=bebaproto.OFPSC_EXP_SET_U_EXTRACTOR,
            fields=[ofproto.OXM_OF_IN_PORT],
            table_id=2)
        datapath.send_msg(req)

        req = bebaparser.OFPExpMsgHeaderFieldExtract(
            datapath=datapath,
            table_id=2,
            extractor_id=1,
            field=bebaproto.OXM_EXP_TIMESTAMP)
        datapath.send_msg(req)

        req = bebaparser.OFPExpMsgHeaderFieldExtract(
            datapath=datapath,
            table_id=2,
            extractor_id=2,
            field=bebaproto.OXM_EXP_PKT_LEN)
        datapath.send_msg(req)

        req = bebaparser.OFPExpMsgSetCondition(
            datapath=datapath,
            condition=bebaproto.CONDITION_GTE,
            condition_id=0,
            table_id=2,
            operand_1_hf_id=1,
            operand_2_fd_id=4)
        datapath.send_msg(req)

        ############################### TABLE 2 FLOWS #############################

        match = ofparser.OFPMatch(state=0)
        actions = [
            bebaparser.OFPExpActionSetState(table_id=2,
                                            state=1,
                                            idle_timeout=2),
            # saves timestamp
            bebaparser.OFPExpActionSetDataVariable(table_id=2,
                                                   opcode=bebaproto.OPCODE_SUM,
                                                   output_fd_id=4,
                                                   operand_1_hf_id=1,
                                                   operand_2_cost=PROBE_FREQ),
            ofparser.OFPActionOutput(3)
        ]
        self.add_flow(datapath=datapath,
                      table_id=2,
                      priority=500,
                      match=match,
                      actions=actions)
        """ For every packet coming from spine ports, calculates ewma """
        for i in UPPER_PORTS:
            # simply ewma measuring
            match = ofparser.OFPMatch(state=1, in_port=i, condition0=1)
            actions_ewma_1 = [  # calculates deltaT: FDV[1]=HF[1]-FDV[0]=TS_NOW - TS_LAST
                bebaparser.OFPExpActionSetDataVariable(
                    table_id=2,
                    opcode=bebaproto.OPCODE_SUB,
                    output_fd_id=1,
                    operand_1_hf_id=1,
                    operand_2_fd_id=0),
                # calculates rate: R = (bytes / deltaT_ms) * 8 [kb/s]
                bebaparser.OFPExpActionSetDataVariable(
                    table_id=2,
                    opcode=bebaproto.OPCODE_MUL,
                    output_fd_id=2,
                    operand_1_fd_id=2,
                    operand_2_cost=8),
                # stores the result in FDV[3]: THE FLOW ESTIMATED RATE
                bebaparser.OFPExpActionSetDataVariable(
                    table_id=2,
                    opcode=bebaproto.OPCODE_DIV,
                    output_fd_id=2,
                    operand_1_fd_id=2,
                    operand_2_fd_id=1),
                # calculates ewma
                bebaparser.OFPExpActionSetDataVariable(
                    table_id=2,
                    opcode=bebaproto.OPCODE_EWMA,
                    output_fd_id=3,
                    operand_1_fd_id=3,
                    operand_2_cost=ALPHA,
                    operand_3_fd_id=2),
                # saves current timestamp
                bebaparser.OFPExpActionSetDataVariable(
                    table_id=2,
                    opcode=bebaproto.OPCODE_SUM,
                    output_fd_id=0,
                    operand_1_hf_id=1,
                    operand_2_cost=0),
                # saves timestamp + probe freq
                bebaparser.OFPExpActionSetDataVariable(
                    table_id=2,
                    opcode=bebaproto.OPCODE_SUM,
                    output_fd_id=4,
                    operand_1_hf_id=1,
                    operand_2_cost=PROBE_FREQ),
                # reset byte counter
                bebaparser.OFPExpActionSetDataVariable(
                    table_id=2,
                    opcode=bebaproto.OPCODE_SUB,
                    output_fd_id=2,
                    operand_1_fd_id=2,
                    operand_2_fd_id=2),
                # saves in GDV[i] the ewma
                bebaparser.OFPExpActionSetDataVariable(
                    table_id=2,
                    opcode=bebaproto.OPCODE_SUM,
                    output_gd_id=i,
                    operand_1_fd_id=3,
                    operand_2_cost=0)
            ]
            # actions for the packet
            actions = actions_ewma_1 + [ofparser.OFPActionOutput(3)]
            # action group for the probes
            probe_actions1 = [
                ofparser.OFPActionSetField(mpls_tc=datapath.id),  # the GDV[i]
                bebaparser.OFPExpActionWriteContextToField(
                    src_type=bebaproto.SOURCE_TYPE_GLOBAL_DATA_VAR,
                    src_id=i,
                    dst_field=ofproto.OXM_OF_MPLS_LABEL),
                ofparser.OFPActionOutput(i)
            ]
            probe_actions2 = [
                ofparser.OFPActionSetField(
                    mpls_tc=datapath.id),  # the GDV[other]
                bebaparser.OFPExpActionWriteContextToField(
                    src_type=bebaproto.SOURCE_TYPE_GLOBAL_DATA_VAR,
                    src_id=(1 if i == 2 else 2),
                    dst_field=ofproto.OXM_OF_MPLS_LABEL),
                ofparser.OFPActionOutput((1 if i == 2 else 2))
            ]
            # apply packet actions
            insts = [
                ofparser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
                                               actions),
                # apply packet generation
                bebaparser.OFPInstructionInSwitchPktGen(
                    pkttmp_id=0, actions=probe_actions1),
                bebaparser.OFPInstructionInSwitchPktGen(pkttmp_id=0,
                                                        actions=probe_actions2)
            ]
            mod = ofparser.OFPFlowMod(datapath=datapath,
                                      table_id=2,
                                      priority=30,
                                      match=match,
                                      instructions=insts)
            datapath.send_msg(mod)

            match = ofparser.OFPMatch(state=1, in_port=i, condition0=0)
            actions_ewma_2 = [
                bebaparser.OFPExpActionSetDataVariable(
                    table_id=2,
                    opcode=bebaproto.OPCODE_SUM,
                    output_fd_id=2,
                    operand_1_fd_id=2,
                    operand_2_hf_id=2)
            ]
            self.add_flow(datapath=datapath,
                          table_id=2,
                          priority=30,
                          match=match,
                          actions=actions_ewma_2 +
                          [ofparser.OFPActionOutput(3)])

            # using low bitrate flows to refresh the estimates. For testing purposes only
            for j in [0, 1]:
                match = ofparser.OFPMatch(in_port=i,
                                          eth_type=0x0800,
                                          ip_proto=6,
                                          tcp_dst=10000 + j,
                                          condition0=1)
                actions = actions_ewma_1 + [ofparser.OFPActionOutput(3)]
                self.add_flow(datapath=datapath,
                              table_id=2,
                              priority=150,
                              match=match,
                              actions=actions)

                match = ofparser.OFPMatch(in_port=i,
                                          eth_type=0x0800,
                                          ip_proto=6,
                                          tcp_src=10000 + j,
                                          condition0=1)
                actions = actions_ewma_1 + [ofparser.OFPActionOutput(3)]
                self.add_flow(datapath=datapath,
                              table_id=2,
                              priority=150,
                              match=match,
                              actions=actions)

                match = ofparser.OFPMatch(in_port=i,
                                          eth_type=0x0800,
                                          ip_proto=6,
                                          tcp_dst=10000 + j,
                                          condition0=0)
                actions = actions_ewma_2 + [ofparser.OFPActionOutput(3)]
                self.add_flow(datapath=datapath,
                              table_id=2,
                              priority=150,
                              match=match,
                              actions=actions)

                match = ofparser.OFPMatch(in_port=i,
                                          eth_type=0x0800,
                                          ip_proto=6,
                                          tcp_src=10000 + j,
                                          condition0=0)
                actions = actions_ewma_2 + [ofparser.OFPActionOutput(3)]
                self.add_flow(datapath=datapath,
                              table_id=2,
                              priority=150,
                              match=match,
                              actions=actions)

        ######################## TABLE 3: FORWARDING ##############################################################

        ######################## TABLE 3 CONFIG #####################################################################

        ##### GDV[1] contains path utilization to dest 1 on port 1
        ##### GDV[2] contains path utilization to dest 2 on port 1
        ##### GDV[3] contains path utilization to dest 1 on port 2
        ##### GDV[4] contains path utilization to dest 2 on port 2

        req = bebaparser.OFPExpMsgConfigureStatefulTable(datapath=datapath,
                                                         table_id=3,
                                                         stateful=1)
        datapath.send_msg(req)
        """ Set lookup extractor """
        req = bebaparser.OFPExpMsgKeyExtract(
            datapath=datapath,
            command=bebaproto.OFPSC_EXP_SET_L_EXTRACTOR,
            fields=[
                ofproto.OXM_OF_IPV4_SRC, ofproto.OXM_OF_IPV4_DST,
                ofproto.OXM_OF_TCP_SRC, ofproto.OXM_OF_TCP_DST
            ],
            table_id=3)
        datapath.send_msg(req)
        """ Set update extractor """
        req = bebaparser.OFPExpMsgKeyExtract(
            datapath=datapath,
            command=bebaproto.OFPSC_EXP_SET_U_EXTRACTOR,
            fields=[
                ofproto.OXM_OF_IPV4_SRC, ofproto.OXM_OF_IPV4_DST,
                ofproto.OXM_OF_TCP_SRC, ofproto.OXM_OF_TCP_DST
            ],
            table_id=3)
        datapath.send_msg(req)

        req = bebaparser.OFPExpMsgHeaderFieldExtract(
            datapath=datapath,
            table_id=3,
            extractor_id=0,
            field=ofproto.OXM_OF_MPLS_LABEL)
        datapath.send_msg(req)

        req = bebaparser.OFPExpMsgHeaderFieldExtract(
            datapath=datapath,
            table_id=3,
            extractor_id=1,
            field=bebaproto.OXM_EXP_TIMESTAMP)
        datapath.send_msg(req)

        req = bebaparser.OFPExpMsgHeaderFieldExtract(
            datapath=datapath,
            table_id=3,
            extractor_id=2,
            field=bebaproto.OXM_EXP_PKT_LEN)
        datapath.send_msg(req)

        #################################### TABLE 3 FLOWS ###################################

        # many conditions as the number of LEAVES-1
        # for i in LEAVES except datapath.id: create condition[i]

        # in case of 2 Spines
        for destLeaf in [1, 2]:
            # C[destLeaf]: in which port there is less utilization for that destination?
            req = bebaparser.OFPExpMsgSetCondition(
                datapath=datapath,
                condition=bebaproto.CONDITION_LTE,
                condition_id=destLeaf,
                table_id=3,
                operand_1_gd_id=destLeaf,
                operand_2_gd_id=destLeaf + 2)
            datapath.send_msg(req)

        # leaf number dependent flows
        if datapath.id == 1:
            # LEAF 1 new flows: fetch the destination leaf and check the appropriate condition
            match1true = ofparser.OFPMatch(metadata=2, condition1=1,
                                           state=0)  # dst=2, port 1
            match2true = ofparser.OFPMatch(metadata=3, condition2=1,
                                           state=0)  # dst=3, port 1
            match1false = ofparser.OFPMatch(metadata=2, condition1=0,
                                            state=0)  # dst=2, port 2
            match2false = ofparser.OFPMatch(metadata=3, condition2=0,
                                            state=0)  # dst=3, port 2
        elif datapath.id == 2:
            # LEAF 2 new flows: fetch the destination leaf and check the appropriate condition
            match1true = ofparser.OFPMatch(metadata=1, condition1=1,
                                           state=0)  # dst=1, port 1
            match2true = ofparser.OFPMatch(metadata=3, condition2=1,
                                           state=0)  # dst=3, port 1
            match1false = ofparser.OFPMatch(metadata=1, condition1=0,
                                            state=0)  # dst=1, port 2
            match2false = ofparser.OFPMatch(metadata=3, condition2=0,
                                            state=0)  # dst=3, port 2
        elif datapath.id == 3:
            # LEAF 3 new flows: fetch the destination leaf and check the appropriate condition
            match1true = ofparser.OFPMatch(metadata=1, condition1=1,
                                           state=0)  # dst=1, port 1
            match2true = ofparser.OFPMatch(metadata=2, condition2=1,
                                           state=0)  # dst=2, port 1
            match1false = ofparser.OFPMatch(metadata=1, condition1=0,
                                            state=0)  # dst=1, port 2
            match2false = ofparser.OFPMatch(metadata=2, condition2=0,
                                            state=0)  # dst=2, port 2

        # if port 1 is better, set_state(1) and output 1
        actions_true = [
            bebaparser.OFPExpActionSetState(state=1,
                                            table_id=3,
                                            idle_timeout=RTT),
            ofparser.OFPActionOutput(1)
        ]

        # if port 2 is better, set_state(2) and output 2
        actions_false = [
            bebaparser.OFPExpActionSetState(state=2,
                                            table_id=3,
                                            idle_timeout=RTT),
            ofparser.OFPActionOutput(2)
        ]

        self.add_flow(datapath=datapath,
                      table_id=3,
                      priority=20,
                      match=match1true,
                      actions=actions_true)
        self.add_flow(datapath=datapath,
                      table_id=3,
                      priority=20,
                      match=match2true,
                      actions=actions_true)
        self.add_flow(datapath=datapath,
                      table_id=3,
                      priority=20,
                      match=match1false,
                      actions=actions_false)
        self.add_flow(datapath=datapath,
                      table_id=3,
                      priority=20,
                      match=match2false,
                      actions=actions_false)
        """ extract external probes' data and store in GDVs """

        match = ofparser.OFPMatch(eth_type=0x8847, mpls_tc=datapath.id)
        self.add_flow(datapath=datapath,
                      table_id=3,
                      priority=300,
                      match=match,
                      actions=[])

        for i in UPPER_PORTS:
            for leafNo in LEAVES:
                if leafNo != datapath.id:
                    match = ofparser.OFPMatch(in_port=i,
                                              eth_type=0x8847,
                                              mpls_tc=leafNo)
                    """ actions: save in GDVs external probes' data """
                    if datapath.id == 1:
                        if leafNo == 2:
                            actions = [
                                bebaparser.OFPExpActionSetDataVariable(
                                    table_id=3,
                                    opcode=bebaproto.OPCODE_SUM,
                                    output_gd_id=(1 if i == 1 else 3),
                                    operand_1_hf_id=0,
                                    operand_2_cost=0)
                            ]
                        elif leafNo == 3:
                            actions = [
                                bebaparser.OFPExpActionSetDataVariable(
                                    table_id=3,
                                    opcode=bebaproto.OPCODE_SUM,
                                    output_gd_id=(2 if i == 1 else 4),
                                    operand_1_hf_id=0,
                                    operand_2_cost=0)
                            ]
                    elif datapath.id == 2:
                        if leafNo == 1:
                            actions = [
                                bebaparser.OFPExpActionSetDataVariable(
                                    table_id=3,
                                    opcode=bebaproto.OPCODE_SUM,
                                    output_gd_id=(1 if i == 1 else 3),
                                    operand_1_hf_id=0,
                                    operand_2_cost=0)
                            ]
                        elif leafNo == 3:
                            actions = [
                                bebaparser.OFPExpActionSetDataVariable(
                                    table_id=3,
                                    opcode=bebaproto.OPCODE_SUM,
                                    output_gd_id=(2 if i == 1 else 4),
                                    operand_1_hf_id=0,
                                    operand_2_cost=0)
                            ]
                    elif datapath.id == 3:
                        if leafNo == 1:
                            actions = [
                                bebaparser.OFPExpActionSetDataVariable(
                                    table_id=3,
                                    opcode=bebaproto.OPCODE_SUM,
                                    output_gd_id=(1 if i == 1 else 3),
                                    operand_1_hf_id=0,
                                    operand_2_cost=0)
                            ]
                        elif leafNo == 2:
                            actions = [
                                bebaparser.OFPExpActionSetDataVariable(
                                    table_id=3,
                                    opcode=bebaproto.OPCODE_SUM,
                                    output_gd_id=(2 if i == 1 else 4),
                                    operand_1_hf_id=0,
                                    operand_2_cost=0)
                            ]

                    self.add_flow(datapath=datapath,
                                  table_id=3,
                                  priority=200,
                                  match=match,
                                  actions=actions)

        for s in UPPER_PORTS:
            for metadata in LEAVES:
                # normal conditions
                match = ofparser.OFPMatch(in_port=3,
                                          state=s,
                                          metadata=metadata)  # , condition4=0)
                actions = [ofparser.OFPActionOutput(s)]
                self.add_flow(datapath=datapath,
                              table_id=3,
                              priority=30,
                              match=match,
                              actions=actions)
Пример #30
0
                        priority=1000,
                        match=parser.OFPMatch(eth_dst=2),
                        instructions=[parser.OFPInstructionGotoTable(2)]),
    parser.OFPFlowStats(table_id=1,
                        priority=0,
                        match=parser.OFPMatch(),
                        instructions=[parser.OFPInstructionGotoTable(3)]),

    # Table 2
    parser.OFPFlowStats(
        table_id=2,
        priority=1008,
        match=parser.OFPMatch(ipv4_dst=("1.0.0.0", "255.0.0.0")),
        instructions=[
            parser.OFPInstructionActions(ofproto_v1_3.OFPIT_WRITE_ACTIONS, [
                parser.OFPActionSetField(eth_src=100),
                parser.OFPActionSetField(eth_dst=20),
                parser.OFPActionOutput(20)
            ])
        ]),
    parser.OFPFlowStats(
        table_id=2,
        priority=1008,
        match=parser.OFPMatch(ipv4_dst=("10.0.0.0", "255.0.0.0")),
        instructions=[
            parser.OFPInstructionActions(ofproto_v1_3.OFPIT_WRITE_ACTIONS, [
                parser.OFPActionSetField(eth_src=100),
                parser.OFPActionSetField(eth_dst=20),
                parser.OFPActionOutput(20)
            ])
        ]),