Example #1
0
    def run(self):
        """
        Main execute function for running the switch
        """

        logging.basicConfig(filename="", level=logging.DEBUG)
        self.logger.info("Switch thread running")
        host = self.config.controller_ip
        if self.config.passive_connect:
            host = None
        self.controller = ControllerInterface(host=host,
                                              port=self.config.controller_port)
        self.dataplane = dataplane.DataPlane()
        self.logger.info("Dataplane started")
        self.pipeline = FlowPipeline(self, self.config.n_tables)
        self.pipeline.controller_set(self.controller)
        self.pipeline.start()
        self.logger.info("Pipeline started")
        link_status = ofp.OFPPF_1GB_FD  #@todo dynamically infer this from the interface status
        for of_port, ifname in self.config.port_map.items():          
            self.dataplane.port_add(ifname, of_port)
            port = ofp.ofp_port()
            port.port_no = of_port
            port.name = ifname
            port.max_speed = 9999999
            port.curr_speed = 9999999
            mac = netutils.get_if_hwaddr(port.name)
            self.logger.info("Added port %s (ind=%d) with mac %x:%x:%x:%x:%x:%x" % ((ifname, of_port) + mac))
            port.hw_addr = list(mac)    # stupid frickin' python; need to convert a tuple to a list
            port.config = 0
            port.state = 0 #@todo infer if link is up/down and set OFPPS_LINK_DOWN
            port.advertised = link_status
            port.supported = link_status
            port.curr = link_status
            port.peer = link_status
            self.ports[of_port]=port
        # Register to receive all controller packets
        self.controller.register("all", self.ctrl_pkt_handler, calling_obj=self)
        self.controller.start()
        self.logger.info("Controller started")

        # Process packets when they arrive
        self.logger.info("Entering packet processing loop")
        while True:
            (of_port, data, recv_time) = self.dataplane.poll(timeout=5)
            if not self.controller.isAlive():
                # @todo Implement fail open/closed
                self.logger.error("Controller dead\n")
                break
            if not self.pipeline.isAlive():
                # @todo Implement fail open/closed
                self.logger.error("Pipeline dead\n")
                break
            if data is None:
                self.logger.debug("No packet for 5 seconds\n")
                continue
            self.logger.debug("Packet len " + str(len(data)) +
                              " in on port " + str(of_port))
            packet = Packet(in_port=of_port, data=data)
            self.pipeline.apply_pipeline(self, packet)

        self.logger.error("Exiting OFSwitch thread")
        self.pipeline.kill()
        self.dataplane.kill()
        self.pipeline.join()
        self.controller.join()
Example #2
0
    def run(self):
        """
        Main execute function for running the switch
        """

        logging.basicConfig(filename="", level=logging.DEBUG)
        self.logger.info("Switch thread running")
        host = self.config.controller_ip
        if self.config.passive_connect:
            host = None
        self.controller = ControllerInterface(host=host,
                                              port=self.config.controller_port)
        self.dataplane = dataplane.DataPlane()
        self.logger.info("Dataplane started")
        self.pipeline = FlowPipeline(self, self.config.n_tables)
        self.pipeline.controller_set(self.controller)
        self.pipeline.start()
        self.logger.info("Pipeline started")
        link_status = ofp.OFPPF_1GB_FD  #@todo dynamically infer this from the interface status
        for of_port, ifname in self.config.port_map.items():
            self.dataplane.port_add(ifname, of_port)
            port = ofp.ofp_port()
            port.port_no = of_port
            port.name = ifname
            port.max_speed = 9999999
            port.curr_speed = 9999999
            mac = netutils.get_if_hwaddr(port.name)
            self.logger.info(
                "Added port %s (ind=%d) with mac %x:%x:%x:%x:%x:%x" %
                ((ifname, of_port) + mac))
            port.hw_addr = list(
                mac
            )  # stupid frickin' python; need to convert a tuple to a list
            port.config = 0
            port.state = 0  #@todo infer if link is up/down and set OFPPS_LINK_DOWN
            port.advertised = link_status
            port.supported = link_status
            port.curr = link_status
            port.peer = link_status
            self.ports[of_port] = port
        # Register to receive all controller packets
        self.controller.register("all",
                                 self.ctrl_pkt_handler,
                                 calling_obj=self)
        self.controller.start()
        self.logger.info("Controller started")

        # Process packets when they arrive
        self.logger.info("Entering packet processing loop")
        while True:
            (of_port, data, recv_time) = self.dataplane.poll(timeout=5)
            if not self.controller.isAlive():
                # @todo Implement fail open/closed
                self.logger.error("Controller dead\n")
                break
            if not self.pipeline.isAlive():
                # @todo Implement fail open/closed
                self.logger.error("Pipeline dead\n")
                break
            if data is None:
                self.logger.debug("No packet for 5 seconds\n")
                continue
            self.logger.debug("Packet len " + str(len(data)) + " in on port " +
                              str(of_port))
            packet = Packet(in_port=of_port, data=data)
            self.pipeline.apply_pipeline(self, packet)

        self.logger.error("Exiting OFSwitch thread")
        self.pipeline.kill()
        self.dataplane.kill()
        self.pipeline.join()
        self.controller.join()
Example #3
0
class OFSwitch(Thread):
    """
    Top level class for the ofps implementation
    Components:
       A set of dataplane ports in a DataPlane object
       A controller connection and ofp stack
       A flow table object
    The switch is responsible for:
       Plumbing the packets from the dataplane to the flow table
       Executing actions as specified by the output from the flow table
       Processing the controller messages
    The main thread processes dataplane packets and control packets
    """

    VERSION = "OFPS version 0.1"

    # @todo Support fail open/closed
    def __init__(self):
        """
        Constructor for base class
        """
        super(OFSwitch, self).__init__()
        self.setDaemon(True)
        self.config = OFSwitchConfig()
        self.logger = logging.getLogger("switch")
        self.groups = GroupTable()
        self.ports = {}         # hash of ports[index]=ofp.ofp_port
    def config_set(self, config):
        """
        Set the configuration for the switch.
        Contents:
            Controller IP address and port
            Fail open/closed
            Passive listener port
            Number of tables to support
        """
        self.config = config
        
    def ctrl_pkt_handler(self, cookie, msg, rawmsg):
        """
        Handle a message from the controller
        @todo Use a queue so messages can be processed in the main thread
        """
        try:
            callable = getattr(ctrl_msg, msg.__class__.__name__)
            self.logger.debug("Calling ctrl_msg.%s" % msg.__class__.__name__)
            callable(self, msg, rawmsg)
        except KeyError:
            self.logger.error("Could not execute controller fn (%s)" %
                                str(msg.__class__.__name__))
            sys.exit(1)

        return True
    
    def getConfig(self,element):
        """ 
        Return the element from the config 
        @param  element:  a string
        """
        return self.config.getConfig(element)

    def run(self):
        """
        Main execute function for running the switch
        """

        logging.basicConfig(filename="", level=logging.DEBUG)
        self.logger.info("Switch thread running")
        host = self.config.controller_ip
        if self.config.passive_connect:
            host = None
        self.controller = ControllerInterface(host=host,
                                              port=self.config.controller_port)
        self.dataplane = dataplane.DataPlane()
        self.logger.info("Dataplane started")
        self.pipeline = FlowPipeline(self, self.config.n_tables)
        self.pipeline.controller_set(self.controller)
        self.pipeline.start()
        self.logger.info("Pipeline started")
        link_status = ofp.OFPPF_1GB_FD  #@todo dynamically infer this from the interface status
        for of_port, ifname in self.config.port_map.items():          
            self.dataplane.port_add(ifname, of_port)
            port = ofp.ofp_port()
            port.port_no = of_port
            port.name = ifname
            port.max_speed = 9999999
            port.curr_speed = 9999999
            mac = netutils.get_if_hwaddr(port.name)
            self.logger.info("Added port %s (ind=%d) with mac %x:%x:%x:%x:%x:%x" % ((ifname, of_port) + mac))
            port.hw_addr = list(mac)    # stupid frickin' python; need to convert a tuple to a list
            port.config = 0
            port.state = 0 #@todo infer if link is up/down and set OFPPS_LINK_DOWN
            port.advertised = link_status
            port.supported = link_status
            port.curr = link_status
            port.peer = link_status
            self.ports[of_port]=port
        # Register to receive all controller packets
        self.controller.register("all", self.ctrl_pkt_handler, calling_obj=self)
        self.controller.start()
        self.logger.info("Controller started")

        # Process packets when they arrive
        self.logger.info("Entering packet processing loop")
        while True:
            (of_port, data, recv_time) = self.dataplane.poll(timeout=5)
            if not self.controller.isAlive():
                # @todo Implement fail open/closed
                self.logger.error("Controller dead\n")
                break
            if not self.pipeline.isAlive():
                # @todo Implement fail open/closed
                self.logger.error("Pipeline dead\n")
                break
            if data is None:
                self.logger.debug("No packet for 5 seconds\n")
                continue
            self.logger.debug("Packet len " + str(len(data)) +
                              " in on port " + str(of_port))
            packet = Packet(in_port=of_port, data=data)
            self.pipeline.apply_pipeline(self, packet)

        self.logger.error("Exiting OFSwitch thread")
        self.pipeline.kill()
        self.dataplane.kill()
        self.pipeline.join()
        self.controller.join()
    
    def __str__(self):
        str  = "OFPS:: OpenFlow Python Switch\n"
        str += "    datapath_id = %s\n" % (self.config.datapath_id2str(
                self.config.getConfig('datapath_id')))
        for key, val in self.config.port_map.iteritems():
            str += "    interface %d = %s\n" % (key, val)
        return str 
    
    def version(self):
        return OFSwitch.VERSION
Example #4
0
class OFSwitch(Thread):
    """
    Top level class for the ofps implementation
    Components:
       A set of dataplane ports in a DataPlane object
       A controller connection and ofp stack
       A flow table object
    The switch is responsible for:
       Plumbing the packets from the dataplane to the flow table
       Executing actions as specified by the output from the flow table
       Processing the controller messages
    The main thread processes dataplane packets and control packets
    """

    VERSION = "OFPS version 0.1"

    # @todo Support fail open/closed
    def __init__(self):
        """
        Constructor for base class
        """
        super(OFSwitch, self).__init__()
        self.setDaemon(True)
        self.config = OFSwitchConfig()
        self.logger = logging.getLogger("switch")
        self.groups = GroupTable()
        self.ports = {}  # hash of ports[index]=ofp.ofp_port

    def config_set(self, config):
        """
        Set the configuration for the switch.
        Contents:
            Controller IP address and port
            Fail open/closed
            Passive listener port
            Number of tables to support
        """
        self.config = config

    def ctrl_pkt_handler(self, cookie, msg, rawmsg):
        """
        Handle a message from the controller
        @todo Use a queue so messages can be processed in the main thread
        """
        try:
            callable = getattr(ctrl_msg, msg.__class__.__name__)
            self.logger.debug("Calling ctrl_msg.%s" % msg.__class__.__name__)
            callable(self, msg, rawmsg)
        except KeyError:
            self.logger.error("Could not execute controller fn (%s)" %
                              str(msg.__class__.__name__))
            sys.exit(1)

        return True

    def getConfig(self, element):
        """ 
        Return the element from the config 
        @param  element:  a string
        """
        return self.config.getConfig(element)

    def run(self):
        """
        Main execute function for running the switch
        """

        logging.basicConfig(filename="", level=logging.DEBUG)
        self.logger.info("Switch thread running")
        host = self.config.controller_ip
        if self.config.passive_connect:
            host = None
        self.controller = ControllerInterface(host=host,
                                              port=self.config.controller_port)
        self.dataplane = dataplane.DataPlane()
        self.logger.info("Dataplane started")
        self.pipeline = FlowPipeline(self, self.config.n_tables)
        self.pipeline.controller_set(self.controller)
        self.pipeline.start()
        self.logger.info("Pipeline started")
        link_status = ofp.OFPPF_1GB_FD  #@todo dynamically infer this from the interface status
        for of_port, ifname in self.config.port_map.items():
            self.dataplane.port_add(ifname, of_port)
            port = ofp.ofp_port()
            port.port_no = of_port
            port.name = ifname
            port.max_speed = 9999999
            port.curr_speed = 9999999
            mac = netutils.get_if_hwaddr(port.name)
            self.logger.info(
                "Added port %s (ind=%d) with mac %x:%x:%x:%x:%x:%x" %
                ((ifname, of_port) + mac))
            port.hw_addr = list(
                mac
            )  # stupid frickin' python; need to convert a tuple to a list
            port.config = 0
            port.state = 0  #@todo infer if link is up/down and set OFPPS_LINK_DOWN
            port.advertised = link_status
            port.supported = link_status
            port.curr = link_status
            port.peer = link_status
            self.ports[of_port] = port
        # Register to receive all controller packets
        self.controller.register("all",
                                 self.ctrl_pkt_handler,
                                 calling_obj=self)
        self.controller.start()
        self.logger.info("Controller started")

        # Process packets when they arrive
        self.logger.info("Entering packet processing loop")
        while True:
            (of_port, data, recv_time) = self.dataplane.poll(timeout=5)
            if not self.controller.isAlive():
                # @todo Implement fail open/closed
                self.logger.error("Controller dead\n")
                break
            if not self.pipeline.isAlive():
                # @todo Implement fail open/closed
                self.logger.error("Pipeline dead\n")
                break
            if data is None:
                self.logger.debug("No packet for 5 seconds\n")
                continue
            self.logger.debug("Packet len " + str(len(data)) + " in on port " +
                              str(of_port))
            packet = Packet(in_port=of_port, data=data)
            self.pipeline.apply_pipeline(self, packet)

        self.logger.error("Exiting OFSwitch thread")
        self.pipeline.kill()
        self.dataplane.kill()
        self.pipeline.join()
        self.controller.join()

    def __str__(self):
        str = "OFPS:: OpenFlow Python Switch\n"
        str += "    datapath_id = %s\n" % (self.config.datapath_id2str(
            self.config.getConfig('datapath_id')))
        for key, val in self.config.port_map.iteritems():
            str += "    interface %d = %s\n" % (key, val)
        return str

    def version(self):
        return OFSwitch.VERSION