def __init__ (self, *args, **kw): """ Create a switch instance Additional options over superclass: log_level (default to default_log_level) is level for this instance """ log_level = kw.pop('log_level', self.default_log_level) self.q = Queue() self.t = Thread(target=self._consumer_threadproc) core.addListeners(self) super(PCapSwitch,self).__init__(*args,**kw) self.px = {} for p in self.ports.values(): px = pxpcap.PCap(p.name, callback = self._pcap_rx, start = False) px.port_no = p.port_no self.px[p.port_no] = px self.log.setLevel(log_level) for px in self.px.itervalues(): px.start() self.t.start()
def __init__(self, host_device, filter_string): self.read_queue = Queue.Queue() import pox.lib.pxpcap as pxpcap if not pxpcap.enabled: raise RuntimeError( '''You need to compile POX's pxpcap library:\n''' '''$ (cd pox/pox/lib/pxcap/pxcap_c && python setup.py build)''' ) pcap = pxpcap.PCap(start=False, filter=filter_string, callback=self._pcap_callback) pcap.open(device=host_device, promiscuous=True) pcap.start(addListeners=False) self._pcap = pcap
def add_interface(self, name, port_no=-1, on_error=None, start=False): if on_error is None: on_error = log.error devs = pxpcap.PCap.get_devices() if name not in devs: on_error("Device %s not available -- ignoring", name) return dev = devs[name] if dev.get('addrs', {}).get('ethernet', {}).get('addr') is None: on_error("Device %s has no ethernet address -- ignoring", name) return if dev.get('addrs', {}).get('AF_INET') != None: on_error("Device %s has an IP address -- ignoring", name) return for no, p in self.px.iteritems(): if p.device == name: on_error("Device %s already added", name) if port_no == -1: while True: port_no = self._next_port self._next_port += 1 if port_no not in self.ports: break if port_no in self.ports: on_error("Port %s already exists -- ignoring", port_no) return phy = of.ofp_phy_port() phy.port_no = port_no phy.hw_addr = dev['addrs']['ethernet']['addr'] phy.name = name # Fill in features sort of arbitrarily phy.curr = of.OFPPF_10MB_HD phy.advertised = of.OFPPF_10MB_HD phy.supported = of.OFPPF_10MB_HD phy.peer = of.OFPPF_10MB_HD self.add_port(phy) px = pxpcap.PCap(name, callback=self._pcap_rx, start=False) px.port_no = phy.port_no self.px[phy.port_no] = px if start: px.start() return px
def add_interface(self, name, port_no=-1, on_error=None, start=False, virtual=False): """ Add an interface This is usually a PCap interface, unless virtual is set. If virtual is True, this creates a virtual port which isn't connected to any channel. If it's a string, it's the channel name. """ if self.magic_virtual_port_names: if name.startswith("@"): name = name[1:] virtual = True if "@" in name: name, virtual = name.split("@", 1) if on_error is None: on_error = log.error phy = of.ofp_phy_port() phy.name = name # Fill in features sort of arbitrarily phy.curr = of.OFPPF_10MB_HD phy.advertised = of.OFPPF_10MB_HD phy.supported = of.OFPPF_10MB_HD phy.peer = of.OFPPF_10MB_HD if virtual: px = VirtualPort(self, phy) if isinstance(virtual, str): px.channel = virtual else: devs = pxpcap.PCap.get_devices() if name not in devs: on_error("Device %s not available -- ignoring", name) return dev = devs[name] if dev.get('addrs', {}).get('ethernet', {}).get('addr') is None: on_error("Device %s has no ethernet address -- ignoring", name) return if dev.get('addrs', {}).get('AF_INET') != None: on_error("Device %s has an IP address -- ignoring", name) return for no, p in self.px.iteritems(): if p.device == name: on_error("Device %s already added", name) phy.hw_addr = dev['addrs']['ethernet']['addr'] px = pxpcap.PCap(name, callback=self._pcap_rx, start=False) if port_no == -1: while True: port_no = self._next_port self._next_port += 1 if port_no not in self.ports: break if port_no in self.ports: on_error("Port %s already exists -- ignoring", port_no) return phy.port_no = port_no self.px[phy.port_no] = px if virtual: # We create the MAC based on the port_no, so we have to do it here # and not earlier. phy.hw_addr = self._gen_ethaddr(phy.port_no) self.add_port(phy) if start: px.start() return px