def start(self): self.runFlag = True self.channel = grpc.insecure_channel(self.ip + ':' + self.port) self.client_stub = p4runtime_pb2.P4RuntimeStub(self.channel) stream = self.client_stub.StreamChannel(self.__stream_req_iterator()) self.stream_recv_thread = threading.Thread(target=self.__stream_recv, args=(stream, )) self.stream_recv_thread.start() self.ipcThread = threading.Thread(target=self._runIpcListener) self.ipcThread.start() stream_message_request = p4runtime_pb2.StreamMessageRequest() arb = stream_message_request.arbitration arb.device_id = self.device_id self.election_id = arb.election_id self.election_id.high = 0 self.election_id.low = 1 self.stream_out_queue.put(stream_message_request) rep = self.get_stream_packet("arbitration", timeout=100) if rep is None: print("Failed to handshake with switch") self.stream_out_queue.put(None) self.stream_recv_thread.join(1) exit(1) else: print("####Handshake with switch complete, now master") print("####Setting Forwarding Config") self.set_forwarding_pipeline() self.started = True return
def PacketIn(self, dry_run=False, **kwargs): request = p4runtime_pb2.StreamMessageRequest() if dry_run: print "P4 Runtime PacketIn: ", 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 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 MasterArbitrationUpdate(self, dry_run=False, **kwargs): # set device id 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 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 send_packet_out(self, packet): packet_out_req = p4runtime_pb2.StreamMessageRequest() packet_out_req.packet.CopyFrom(packet) self.stream_out_q.put(packet_out_req)