def test_VLAN_unpack(self): """Test unpack method of VLAN class.""" raw = b'\x81\x00\xa0{' expected = VLAN(pcp=5, vid=123) unpacked = VLAN() unpacked.unpack(raw) self.assertEqual(unpacked, expected)
def test_Tagged_Ethernet_pack(self): """Test pack method of Ethernet class including VLAN tag.""" ethernet = Ethernet(destination='00:1f:3a:3e:9a:cf', source='00:15:af:d5:38:98', vlans=[VLAN(vid=200)], ether_type=0x800, data=b'testdata') packed = ethernet.pack() expected = b'\x00\x1f:>\x9a\xcf\x00\x15\xaf\xd58' expected += b'\x98\x81\x00\x00\xc8\x08\x00testdata' self.assertEqual(packed, expected)
def test_Tagged_Ethernet_unpack(self): """Test pack method of Ethernet class including VLAN tag.""" raw = b'\x00\x15\xaf\xd58\x98\x00\x1f:>' raw += b'\x9a\xcf\x81\x00!^\x08\x00testdata' expected = Ethernet(destination='00:15:af:d5:38:98', source='00:1f:3a:3e:9a:cf', vlans=[VLAN(pcp=1, vid=350)], ether_type=0x800, data=b'testdata') expected.pack() unpacked = Ethernet() unpacked.unpack(raw) self.assertEqual(unpacked, expected)
def _create_ethernet_frame(trace_entries, color): """ Create an Ethernet frame using TraceEntries and color (dl_src) Args: trace_entries: TraceEntries provided by user or collected from PacketIn color: field and value that indicate the color Returns: ethernet frame """ ethernet = Ethernet() ethernet.ether_type = trace_entries.dl_type ethernet.source = color['color_value'] ethernet.destination = trace_entries.dl_dst vlan = VLAN(vid=trace_entries.dl_vlan, pcp=trace_entries.dl_vlan_pcp) ethernet.vlans.append(vlan) return ethernet
def test_unpack_wrong_tpid(self): """Raise UnpackException if the tpid is not VLAN_TPID.""" raw = b'\x12\x34\xa0{' vlan = VLAN() with self.assertRaises(UnpackException): vlan.unpack(raw)
def test_VLAN_pack(self): """Test pack method of VLAN class.""" vlan = VLAN(pcp=3, vid=20) packed = vlan.pack() expected = b'\x81\x00`\x14' self.assertEqual(packed, expected)
def execute(self): """Send LLDP Packets every 'POLLING_TIME' seconds to all switches.""" switches = list(self.controller.switches.values()) for switch in switches: try: of_version = switch.connection.protocol.version except AttributeError: of_version = None if not switch.is_connected(): continue if of_version == 0x01: port_type = UBInt16 local_port = Port10.OFPP_LOCAL elif of_version == 0x04: port_type = UBInt32 local_port = Port13.OFPP_LOCAL else: # skip the current switch with unsupported OF version continue interfaces = list(switch.interfaces.values()) for interface in interfaces: # Avoid the interface that connects to the controller. if interface.port_number == local_port: continue lldp = LLDP() lldp.chassis_id.sub_value = DPID(switch.dpid) lldp.port_id.sub_value = port_type(interface.port_number) ethernet = Ethernet() ethernet.ether_type = EtherType.LLDP ethernet.source = interface.address ethernet.destination = constants.LLDP_MULTICAST_MAC ethernet.data = lldp.pack() # self.vlan_id == None will result in a packet with no VLAN. ethernet.vlans.append(VLAN(vid=self.vlan_id)) packet_out = self._build_lldp_packet_out( of_version, interface.port_number, ethernet.pack()) if packet_out is not None: event_out = KytosEvent( name='kytos/of_lldp.messages.out.ofpt_packet_out', content={ 'destination': switch.connection, 'message': packet_out }) self.controller.buffers.msg_out.put(event_out) log.debug("Sending a LLDP PacketOut to the switch %s", switch.dpid) msg = '\n' msg += 'Switch: %s (%s)\n' msg += ' Interfaces: %s\n' msg += ' -- LLDP PacketOut --\n' msg += ' Ethernet: eth_type (%s) | src (%s) | dst (%s)\n' msg += ' LLDP: Switch (%s) | port (%s)' log.debug(msg, switch.connection.address, switch.dpid, switch.interfaces, ethernet.ether_type, ethernet.source, ethernet.destination, switch.dpid, interface.port_number)
def generate_trace_pkt(entries, color, r_id, my_domain): """ Receives the REST/PUT to generate a PacketOut data needs to be serialized. Args: entries: color: result from Coloring Napp for a specific DPID r_id: my_domain: Returns: in_port: pkt: """ trace = {} switch = {} eth = {} ip = {} tp = {} # TODO Validate for dl_vlan. If empty, return error. # Default values dpid, in_port = 0, 65532 if color['color_field'] == 'dl_src': dl_src = color['color_value'] else: dl_src = '0e:55:05:0e:55:05' dl_dst = "ca:fe:ca:fe:ca:fe" dl_vlan, dl_type, dl_vlan_pcp = 100, 2048, 0 nw_src, nw_dst, nw_tos = '127.0.0.1', '127.0.0.1', 0 tp_src, tp_dst = 1, 1 try: trace = entries['trace'] switch = trace['switch'] eth = trace['eth'] except: pass try: ip = trace['ip'] except: pass try: tp = trace['tp'] except: pass if len(switch) > 0: dpid, in_port = prepare_switch(switch, dpid, in_port) if len(eth) > 0: dl_src, dl_dst, dl_vlan, dl_type, dl_vlan_pcp = prepare_ethernet( eth, dl_src, dl_dst, dl_vlan, dl_type, dl_vlan_pcp) # if len(ip) > 0: nw_src, nw_dst, nw_tos = prepare_ip(ip, nw_src, nw_dst, nw_tos) # if len(tp) > 0: tp_src, tp_dst = prepare_tp(tp, tp_src, tp_dst) kethernet = Ethernet() kethernet.ether_type = int(dl_type) kethernet.source = dl_src kethernet.destination = dl_dst kvlan = VLAN(vid=int(dl_vlan), pcp=dl_vlan_pcp) kethernet.vlan = kvlan msg = TraceMsg(r_id, my_domain) if int(dl_type) == constants.IPv4: kip = IPv4() kip.destination = nw_dst kip.source = nw_src # ip.protocol = 6 kip.data = dill.dumps(msg) kethernet.data = kip.pack() else: kethernet.data = dill.dumps(msg) pkt = kethernet.pack() return in_port, pkt