def test_packet_in(self): # In this tests we will send a packet-in message from the servicer and check if # packet_in.sniff method works msg = p4runtime_pb2.StreamMessageResponse() msg.packet.payload = b'Random packet-in payload' md = p4runtime_pb2.PacketMetadata() md.metadata_id = 1 md.value = b'\x00\x01' msg.packet.metadata.append(md) # Have to sniff the packet in another thread since this blocks the thread packet_in = sh.PacketIn() captured_packet = [] def _sniff_packet(captured_packet): captured_packet += packet_in.sniff(timeout=1) _t = Thread(target=_sniff_packet, args=(captured_packet, )) _t.start() # TODO: modify the servicer to send stream message? sh.client.stream_in_q["packet"].put(msg) _t.join() self.assertEqual(len(captured_packet), 1) self.assertEqual(captured_packet[0], msg)
def test_packet_out(self): expected_msg = p4runtime_pb2.StreamMessageRequest() expected_msg.packet.payload = b'Random packet-out payload' md = p4runtime_pb2.PacketMetadata() md.metadata_id = 1 md.value = b'\x00\x01' expected_msg.packet.metadata.append(md) packet_out = sh.PacketOut() packet_out.payload = b'Random packet-out payload' packet_out.metadata['egress_port'] = '1' packet_out.send() actual_msg = self.servicer.stored_packet_out.get(block=True, timeout=1) self.assertEqual(actual_msg, expected_msg)
def get_metadata_pb(self, metadata_id, value): p4runtime_metadata = p4runtime_pb2.PacketMetadata() p4runtime_metadata.metadata_id = metadata_id p4runtime_metadata.value = value return p4runtime_metadata
def get_packetout_metadata_pb(self, metadata_name, value): p4info_meta = self.get_packetout_meta("packet_out", metadata_name) p4runtime_metadata = p4runtime_pb2.PacketMetadata() p4runtime_metadata.metadata_id = p4info_meta.id p4runtime_metadata.value = encode(value, p4info_meta.bitwidth) return p4runtime_metadata