예제 #1
0
    def deploy_flow_entry(self, subnet, outport, dstport):
        """
            translate the routing information into flow entry format
            and send FlowMod.
        """
        if outport is None:
            logger.warning('fail to deploy flow entry, cant find output port for %s', str(subnet))
            return

        # match by destination IP address
        match = ofctl_v1_0.to_match(self.dp, {'nw_dst': str(subnet), 'dl_type': '2048', 'nw_proto': '1'})
        
        # rewrite source MAC address with gateway's MAC address
        # rewrite destination MAC address with host's MAC address
        # set output port
        actions = []
        actions.append(self.dp.ofproto_parser.OFPActionSetDlSrc(outport.hw_addr.packed))
        actions.append(self.dp.ofproto_parser.OFPActionSetDlDst(dstport.hw_addr.packed))
        actions.append(self.dp.ofproto_parser.OFPActionOutput(outport.port_no))

        mod = self.dp.ofproto_parser.OFPFlowMod(
                    datapath = self.dp, match = match,
                    priority = 1, cookie = 0, actions = actions,
                    idle_timeout = FLOW_IDLE_TIMEOUT,
                    hard_timeout = FLOW_HARD_TIMEOUT,
                    command = self.dp.ofproto.OFPFC_MODIFY)

        # send FlowMod
        self.dp.send_msg(mod)
예제 #2
0
    def remove_ofrule(self, match):

        mod = self._ofproto_parser.OFPFlowMod(
            datapath=self._dp,
            match=to_match(self._dp, match),
            cookie=0,
            command=self._ofproto.OFPFC_DELETE)

        self._dp.send_msg(mod)
예제 #3
0
    def remove_ofrule(self, match):

        mod = self._ofproto_parser.OFPFlowMod(
            datapath=self._dp,
            match=to_match(self._dp, match),
            cookie=0,
            command=self._ofproto.OFPFC_DELETE)

        self._dp.send_msg(mod)
예제 #4
0
    def deliver_to_host(self, msg, pkt, outport_no):
        """
            deliver packet to host if the switch owns that subnet.
            (1) find ARP entry for destination IP address.
                if failed, send ARP request and buffer the packet
            (2) create a FlowMod packet.
            (3) send FlowMod and PacketOut.
        """
        ip_layer = self.find_packet(pkt, 'ipv4')
        dp = msg.datapath
        switch = self.switches[dp.id]
        ipDestAddr = netaddr.IPAddress(ip_layer.dst)

        logger.info('final switch arrived, try to deliver to %s (dpid=%s)', str(ipDestAddr), dpid_lib.dpid_to_str(msg.datapath.id))

        try:
            mac_addr = switch.ip_to_mac[ipDestAddr][0]
        except KeyError:
            logger.info('no ARP entry for %s, packet buffered', str(ipDestAddr))
            self.send_arp_request(msg.datapath, outport_no, ipDestAddr)
            switch.msg_buffer.append((msg, pkt, outport_no))
            return False

        # match by destination IP address
        match = ofctl_v1_0.to_match(dp, {'nw_dst': str(ipDestAddr), 'dl_type': '2048', 'nw_proto': '1'})
        
        # rewrite source MAC address with gateway's MAC address
        # rewrite destination MAC address with host's MAC address
        # set output port
        actions = []
        actions.append(dp.ofproto_parser.OFPActionSetDlSrc(switch.ports[outport_no].hw_addr.packed))
        actions.append(dp.ofproto_parser.OFPActionSetDlDst(mac_addr.packed))
        actions.append(dp.ofproto_parser.OFPActionOutput(outport_no))

        mod = dp.ofproto_parser.OFPFlowMod(
                    datapath = dp, match = match,
                    priority = 1, cookie = 0, actions = actions,
                    idle_timeout = FLOW_IDLE_TIMEOUT,
                    hard_timeout = FLOW_HARD_TIMEOUT,
                    command = dp.ofproto.OFPFC_MODIFY)

        out = dp.ofproto_parser.OFPPacketOut(
            datapath = dp, buffer_id = msg.buffer_id,
            in_port = msg.in_port, actions = actions)

        dp.send_msg(mod)
        dp.send_msg(out)

        logger.info('FlowMod and PacketOut sent, packet delivered to %s', str(ipDestAddr))
        return True
예제 #5
0
    def add_ofrule(self, match, actions, priority):

        mod = self._ofproto_parser.OFPFlowMod(
            datapath=self._dp,
            match=to_match(self._dp, match),
            cookie=0,
            command=self._ofproto.OFPFC_ADD,
            idle_timeout=0,
            hard_timeout=0,
            priority=priority,
            flags=self._ofproto.OFPFF_SEND_FLOW_REM,
            actions=to_actions(self._dp, actions))

        self._dp.send_msg(mod)
예제 #6
0
    def add_ofrule(self, match, actions, priority):

        mod = self._ofproto_parser.OFPFlowMod(
            datapath=self._dp,
            match=to_match(self._dp, match),
            cookie=0,
            command=self._ofproto.OFPFC_ADD,
            idle_timeout=0,
            hard_timeout=0,
            priority=priority,
            flags=self._ofproto.OFPFF_SEND_FLOW_REM,
            actions=to_actions(self._dp, actions))

        self._dp.send_msg(mod)
예제 #7
0
    def add_ofrule(self, match, actions, priority):

        if 'in_port' in match:
            for action in actions:
                if action['type'] != 'OUTPUT':
                    continue
                if action['port'] == match['in_port']:
                    action['port'] = ofproto_v1_0.OFPP_IN_PORT

        mod = self._ofproto_parser.OFPFlowMod(
            datapath=self._dp,
            match=to_match(self._dp, match),
            cookie=0,
            command=self._ofproto.OFPFC_ADD,
            idle_timeout=0,
            hard_timeout=0,
            priority=priority,
            flags=self._ofproto.OFPFF_SEND_FLOW_REM,
            actions=to_actions(self._dp, actions))

        self._dp.send_msg(mod)
예제 #8
0
    def deploy_flow_entry(self, subnet, outport, dstport):
        """
            translate the routing information into flow entry format
            and send FlowMod.
        """
        if outport is None:
            logger.warning(
                'fail to deploy flow entry, cant find output port for %s',
                str(subnet))
            return

        # match by destination IP address
        match = ofctl_v1_0.to_match(self.dp, {
            'nw_dst': str(subnet),
            'dl_type': '2048',
            'nw_proto': '1'
        })

        # rewrite source MAC address with gateway's MAC address
        # rewrite destination MAC address with host's MAC address
        # set output port
        actions = []
        actions.append(
            self.dp.ofproto_parser.OFPActionSetDlSrc(outport.hw_addr.packed))
        actions.append(
            self.dp.ofproto_parser.OFPActionSetDlDst(dstport.hw_addr.packed))
        actions.append(self.dp.ofproto_parser.OFPActionOutput(outport.port_no))

        mod = self.dp.ofproto_parser.OFPFlowMod(
            datapath=self.dp,
            match=match,
            priority=1,
            cookie=0,
            actions=actions,
            idle_timeout=FLOW_IDLE_TIMEOUT,
            hard_timeout=FLOW_HARD_TIMEOUT,
            command=self.dp.ofproto.OFPFC_MODIFY)

        # send FlowMod
        self.dp.send_msg(mod)
예제 #9
0
    def deliver_to_host(self, msg, pkt, outport_no):
        """
            deliver packet to host if the switch owns that subnet.
            (1) find ARP entry for destination IP address.
                if failed, send ARP request and buffer the packet
            (2) create a FlowMod packet.
            (3) send FlowMod and PacketOut.
        """
        ip_layer = self.find_packet(pkt, 'ipv4')
        dp = msg.datapath
        switch = self.switches[dp.id]
        ipDestAddr = netaddr.IPAddress(ip_layer.dst)

        logger.info('final switch arrived, try to deliver to %s (dpid=%s)',
                    str(ipDestAddr), dpid_lib.dpid_to_str(msg.datapath.id))

        try:
            mac_addr = switch.ip_to_mac[ipDestAddr][0]
        except KeyError:
            logger.info('no ARP entry for %s, packet buffered',
                        str(ipDestAddr))
            self.send_arp_request(msg.datapath, outport_no, ipDestAddr)
            switch.msg_buffer.append((msg, pkt, outport_no))
            return False

        # match by destination IP address
        match = ofctl_v1_0.to_match(dp, {
            'nw_dst': str(ipDestAddr),
            'dl_type': '2048',
            'nw_proto': '1'
        })

        # rewrite source MAC address with gateway's MAC address
        # rewrite destination MAC address with host's MAC address
        # set output port
        actions = []
        actions.append(
            dp.ofproto_parser.OFPActionSetDlSrc(
                switch.ports[outport_no].hw_addr.packed))
        actions.append(dp.ofproto_parser.OFPActionSetDlDst(mac_addr.packed))
        actions.append(dp.ofproto_parser.OFPActionOutput(outport_no))

        mod = dp.ofproto_parser.OFPFlowMod(datapath=dp,
                                           match=match,
                                           priority=1,
                                           cookie=0,
                                           actions=actions,
                                           idle_timeout=FLOW_IDLE_TIMEOUT,
                                           hard_timeout=FLOW_HARD_TIMEOUT,
                                           command=dp.ofproto.OFPFC_MODIFY)

        out = dp.ofproto_parser.OFPPacketOut(datapath=dp,
                                             buffer_id=msg.buffer_id,
                                             in_port=msg.in_port,
                                             actions=actions)

        dp.send_msg(mod)
        dp.send_msg(out)

        logger.info('FlowMod and PacketOut sent, packet delivered to %s',
                    str(ipDestAddr))
        return True
예제 #10
0
 def test_match_to_str(self):
     for attrs in self.attrs_list:
         match = ofctl_v1_0.to_match(self.dp, attrs)
         str = ofctl_v1_0.match_to_str(match)
         eq_(attrs, str)