def request(): while True: if self.sendRequest: self.sendRequest = False yield self.streamChannelRequest else: yield p4runtime_pb2.StreamMessageRequest()
def send_packet_out(self, packet): packet_out_req = p4runtime_pb2.StreamMessageRequest() packet_out_req.packet.CopyFrom(packet) if self.generate_tv: tvutils.add_packet_out_operation(self.tc, packet) else: self.stream_out_q.put(packet_out_req)
def main(): global dev_id client = P4RuntimeClient() streamC = client.StreamChannel() switch_count = 50 for i in range(0, switch_count): dst_addr = "0800000001%s" % (toHex(i + 3)) while (client.sendRequest): sleep(0.000001) # Packet out. client.streamChannelRequest = p4runtime_pb2.StreamMessageRequest() client.streamChannelRequest.packet.payload = ( dst_addr + "08000000010181000001000008004A000032000100003C1162B80A0001010A0002021F24000200000002000000000000000100000FA004D210E1000A82F07034" ).decode("hex") metadata = client.streamChannelRequest.packet.metadata.add() metadata.metadata_id = 1 metadata.value = struct.pack("B", 1) client.sendRequest = True print "Packet-out: %s" % dst_addr sleep(10)
def PacketOut(self, pl_arr, meta_arr): request = p4runtime_pb2.StreamMessageRequest() # Create request list via self.packet_out_msg # print (debug) # using self.requests_stream.put() to append request # Get response from self.stream_msg_resp # print out packet_out () for response in self.stream_msg_resp: if response.WhichOneof("update") is "packet": print( "=============================================================" ) print("Received packet-in payload %s" % (response.packet.payload)) print("Received packet-in metadata: ") for metadata in response.packet.metadata: # uint32 print("\tmetadata id: %s" % s(metadata.metadata_id)) # bytes print("\tmetadata value: %s" % s(metadata.value)) print( "=============================================================" )
def ReadPacketIn(self, dry_run=False, **kwargs): request = p4runtime_pb2.StreamMessageRequest() if dry_run: print "P4 Runtime ReadPacketIn: ", request else: self.requests_stream.put(request) for item in self.stream_msg_resp: return item
def SendPacketOut(self, payload, dry_run=False): req = p4runtime_pb2.StreamMessageRequest() req.packet.payload = payload if dry_run: print "Sending PacketOut:", req else: self.requests_stream.put(req)
def IdleTimeoutNotification(self, dry_run=False, **kwargs): request = p4runtime_pb2.StreamMessageRequest() if dry_run: print "Idle Timeout Notification Response: ", request else: self.requests_stream.put(request) for item in self.stream_msg_resp: return item
def master_arbitration_update(self): logger.info('Master arbitration update on switch - [%s]', self.grpc_addr) request = p4runtime_pb2.StreamMessageRequest() request.arbitration.device_id = self.device_id request.arbitration.election_id.high = 0 request.arbitration.election_id.low = 1 self.requests_stream.put(request)
def WritePacketOut(self, payload): if not self.active: return request = p4runtime_pb2.StreamMessageRequest() request.packet.payload = payload self.stream_out_q.put(request)
def DigestList(self, dry_run=False, **kwargs): request = p4runtime_pb2.StreamMessageRequest() if dry_run: print "P4 Runtime DigestList Response: ", request else: self.requests_stream.put(request) for item in self.stream_msg_resp: return item
def PacketOut(self, packet, dry_run=False, **kwargs): request = p4runtime_pb2.StreamMessageRequest() request.packet.CopyFrom(packet) if dry_run: print "P4 Runtime WritePacketOut: ", request else: self.requests_stream.put(request) for item in self.stream_msg_resp: return item
def DigestListAck(self, digest_ack, dry_run=False, **kwargs): request = p4runtime_pb2.StreamMessageRequest() request.digest_ack.CopyFrom(digest_ack) if dry_run: print "P4 Runtime DigestListAck: ", request else: self.requests_stream.put(request) for item in self.stream_msg_resp: return item
def MasterArbitrationUpdate(self, dry_run=False, **kwargs): request = p4runtime_pb2.StreamMessageRequest() request.arbitration.device_id = self.device_id request.arbitration.election_id.high = 0 request.arbitration.election_id.low = 1 if dry_run: print "P4Runtime MasterArbitrationUpdate: ", request else: self.requests_stream.put(request)
def send_packet_out_multiple(self, payloads): new_requests = [] for payload in payloads: request = p4runtime_pb2.StreamMessageRequest() request.packet.payload = payload new_requests.append(request) self.request_lock.acquire() self.requests.extend(new_requests) self.request_lock.release()
def StreamDigestMessages(self, digest_id, dry_run=False): # now read the digests stream_request = p4runtime_pb2.StreamMessageRequest() # stream_request.digest_ack.digest_id = digest_id if dry_run: print "P4Runtime Read stream digest message: ", stream_request else: self.requests_stream.put(stream_request) for item in self.stream_msg_resp: if item.WhichOneof('update') == 'digest': yield item.digest
def sendPacketOutLabel(self, payload, flowID): sleep(self.controlPlaneDelay) #print "sending packetout to switch %s" % self.name flowIDbyte = ('%%0%dx' % (4 << 1) % flowID).decode('hex')[-4:] #flowIDbyte += bytearray(flowID) packet_out_req = p4runtime_pb2.PacketOut() packet_out_req.payload = bytes('\24\063') + bytes(flowIDbyte) + bytes( payload) req = p4runtime_pb2.StreamMessageRequest() req.packet.CopyFrom(packet_out_req) self.stream_out_q.put(req)
def buildMetadataBasedPacketOut(self, clabFlag, linkID, bitmask, level_to_link_id_store_index , port = 255): ''' port_num_t egress_port; bit<7> _pad; //Previous all fields are not necessary for CLB. TODO at sometime we will trey to clean up them. But at this moment we are not focusing on that bit<8> clb_flags; //Here we will keep various falgs for CLB //--------bit-7--------|| If this bit is set then reet the counter //--------bit-6--------|| If this bit is set then this is a port delete packet //--------bit-5--------|| If this bit is set then this is a port insert packet //--------bit-4--------|| Other bits are ununsed at this moment //--------bit-3--------|| //--------bit-2--------|| //--------bit-1--------|| //--------bit-0--------|| bit<32> link_id; bit<32> bitmask; //Here we are keeping all 32 bit to avoid compile time configuration complexity. At apply blo0ck we will slice necesssary bits. bit<32> level_to_link_id_store_index; // ''' rawPktContent = (255).to_bytes(2,'big') # first 2 byte egressport and padding rawPktContent = rawPktContent + (clabFlag).to_bytes(1,'big') rawPktContent = rawPktContent + (linkID).to_bytes(4,'big') rawPktContent = rawPktContent + (bitmask).to_bytes(4,'big') rawPktContent = rawPktContent + (level_to_link_id_store_index).to_bytes(4,'big') packet_out_req = p4runtime_pb2.StreamMessageRequest() port_hex = port.to_bytes(length=2, byteorder="big") packet_out = p4runtime_pb2.PacketOut() egress_physical_port = packet_out.metadata.add() egress_physical_port.metadata_id = 1 egress_physical_port.value = port_hex clb_flag_metadata_field = packet_out.metadata.add() clb_flag_metadata_field.metadata_id = 3 clb_flag_metadata_field.value = (clabFlag).to_bytes(1,'big') linkID_metadata_field = packet_out.metadata.add() linkID_metadata_field.metadata_id = 4 linkID_metadata_field.value = (linkID).to_bytes(4,'big') bitmask_metadata_field = packet_out.metadata.add() bitmask_metadata_field.metadata_id = 5 bitmask_metadata_field.value = (bitmask).to_bytes(4,'big') level_to_link_id_store_index_metadata_field = packet_out.metadata.add() level_to_link_id_store_index_metadata_field.metadata_id = 6 level_to_link_id_store_index_metadata_field.value = (level_to_link_id_store_index).to_bytes(4,'big') packet_out.payload = rawPktContent packet_out_req.packet.CopyFrom(packet_out) return packet_out_req
def sendPacketOutRaw(self, pkt): sleep(self.controlPlaneDelay) #print "sending packetout to switch %s" % self.name packet_out_req = p4runtime_pb2.PacketOut() packet_out_req.payload = pkt req = p4runtime_pb2.StreamMessageRequest() req.packet.CopyFrom(packet_out_req) #print "sending packet %s to switch %s" % (req.packet.payload.encode('hex'), self.name) self.stream_out_q.put(req)
def WriteDigestAck(self, digest_id, list_id, dry_run=False, **kwargs): request = p4runtime_pb2.StreamMessageRequest() request.digest_ack.digest_id = digest_id request.digest_ack.list_id = list_id if dry_run: print "P4 Runtime WriteDigestAck:", request else: self.requests_stream.put(request) print "Write Ack Success" for item in self.stream_msg_resp: return item
def handshake(self): req = p4runtime_pb2.StreamMessageRequest() arbitration = req.arbitration arbitration.device_id = self.device_id election_id = arbitration.election_id election_id.high = 0 election_id.low = self.election_id self.stream_out_q.put(req) rep = self.get_stream_packet("arbitration", timeout=2) if rep is None: self.fail("Failed to establish handshake")
def send_packet_out(self, pkt, port): packet_out_req = p4runtime_pb2.StreamMessageRequest() port_hex = stringify(port, 2) packet_out = p4runtime_pb2.PacketOut() packet_out.payload = str(pkt) egress_physical_port = packet_out.metadata.add() egress_physical_port.metadata_id = 1 egress_physical_port.value = port_hex packet_out_req.packet.CopyFrom(packet_out) self.stream_out_q.put(packet_out_req)
def MasterArbitrationUpdate(self, dry_run=False, **kwargs): request = p4runtime_pb2.StreamMessageRequest() request.arbitration.device_id = self.device_id request.arbitration.election_id.high = 0 request.arbitration.election_id.low = 1 if dry_run: print("P4Runtime MasterArbitrationUpdate: ", request) else: self.requests_stream.put(request) for item in self.stream_msg_resp: return item # just one
def sendDiscpkt(s, port): pkt = dpkt.ethernet.Ethernet() pkt.src = '\377\377\377\377\377\377' pkt.dst = '\377\377\377\377\377\377' pkt.type = 0xffff pkt.data = '\000' + str(unichr(port)) + s.name packet_out_req = p4runtime_pb2.PacketOut() # send a disc packet to the destination packet_out_req.payload = bytes(pkt) req = p4runtime_pb2.StreamMessageRequest() req.packet.CopyFrom(packet_out_req) s.stream_out_q.put(req)
def MasterArbitrationUpdate(self): request = p4runtime_pb2.StreamMessageRequest() request.arbitration.device_id = self.device_id request.arbitration.election_id.high = 0 request.arbitration.election_id.low = 1 self.stream_out_q.put(request) rep = self.get_stream_packet("arbitration", timeout=5) if rep is None: return False else: return True
def handshake(self): req = p4runtime_pb2.StreamMessageRequest() arbitration = req.arbitration arbitration.device_id = self.device_id # TODO(antonin): we currently allow 0 as the election id in P4Runtime; # if this changes we will need to use an election id > 0 and update the # Write message to include the election id # election_id = arbitration.election_id # election_id.high = 0 # election_id.low = 1 self.stream_out_q.put(req) rep = self.get_stream_packet("arbitration", timeout=2) if rep is None: self.fail("Failed to establish handshake")
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 send_packet_out(self, pkt, port, clnt): self.packetOutLock.acquire(blocking=True) packet_out_req = p4runtime_pb2.StreamMessageRequest() # port_hex = stringify(port, 2) port_hex = port.to_bytes(length=2, byteorder="big") packet_out = p4runtime_pb2.PacketOut() packet_out.payload = pkt.encode() egress_physical_port = packet_out.metadata.add() egress_physical_port.metadata_id = 1 egress_physical_port.value = port_hex packet_out_req.packet.CopyFrom(packet_out) clnt.stream_out_q.put(packet_out_req) self.packetOutLock.release()
def SendLLDP(self, dry_run=False, **kwargs): request = p4runtime_pb2.StreamMessageRequest() # request.packet.payload = "\000" if dry_run: print "P4 Runtime WritePacketOut: ", request else: self.requests_stream.put(request) # stream_msg_resp = self.client_stub.StreamChannel(request) print "hi, in switch" print self.stream_msg_resp for item in self.stream_msg_resp: # print item return item
def __init__(self): # Configure the host and the port to which the client should connect to. self.host = ServerConfig.HOST self.server_port = ServerConfig.SERVER_PORT with open(ServerConfig.SERVER_CERTIFICATE, "rb") as file: trusted_certs = file.read() # Instantiate a communication channel and bind the client to the server. self.credentials = grpc.ssl_channel_credentials(root_certificates=trusted_certs) self.channel = grpc.secure_channel("{}:{}".format(self.host, self.server_port), self.credentials) self.stub = p4runtime_pb2_grpc.P4RuntimeStub(self.channel) self.streamChannelRequest = p4runtime_pb2.StreamMessageRequest() self.sendRequest = True
def handshake(self, roleconfig=None): req = p4runtime_pb2.StreamMessageRequest() arbitration = req.arbitration arbitration.device_id = self.device_id role = arbitration.role role.id = self.role_id if roleconfig is not None: role.config.Pack(roleconfig) election_id = arbitration.election_id election_id.high = 0 election_id.low = self.election_id self.stream_out_q.put(req) rep = self.get_stream_packet("arbitration", timeout=2) if rep is None: print("Failed to establish handshake")