def setUp(self): BaseTest.setUp(self) self.device_id = 0 # Setting up PTF dataplane self.dataplane = ptf.dataplane_instance self.dataplane.flush() self._swports = [] for device, port, ifname in config["interfaces"]: self._swports.append(port) grpc_addr = testutils.test_param_get("grpcaddr") if grpc_addr is None: grpc_addr = 'localhost:50051' self.channel = grpc.insecure_channel(grpc_addr) self.stub = p4runtime_pb2.P4RuntimeStub(self.channel) proto_txt_path = testutils.test_param_get("p4info") print "Importing p4info proto from", proto_txt_path self.p4info = p4info_pb2.P4Info() with open(proto_txt_path, "rb") as fin: google.protobuf.text_format.Merge(fin.read(), self.p4info) # used to store write requests sent to the P4Runtime server, useful for # autocleanup of tests (see definition of autocleanup decorator below) self._reqs = [] self.set_up_stream()
def __init__(self, name, address='127.0.0.1:50051', device_id=0): self.name = name self.address = address self.device_id = device_id self.p4info = None self.channel = grpc.insecure_channel(self.address) self.client_stub = p4runtime_pb2.P4RuntimeStub(self.channel)
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 __init__(self, name, address='127.0.0.1:50051', device_id=0): self.name = name self.address = address self.device_id = device_id self.p4info = None self.channel = grpc.insecure_channel(self.address) # TODO Do want to do a better job managing stub? self.client_stub = p4runtime_pb2.P4RuntimeStub(self.channel)
def connect_p4runtime(self): self.channel = grpc.insecure_channel(self.p4runtime_address) if self.proto_dump_file is not None: interceptor = GrpcRequestLogger(self.proto_dump_file) self.channel = grpc.intercept_channel(self.channel, interceptor) self.client_stub = p4runtime_pb2.P4RuntimeStub(self.channel) self.requests_stream = IterableQueue() self.stream_msg_resp = self.client_stub.StreamChannel( iter(self.requests_stream)) self.MasterArbitrationUpdate()
def __init__(self, name, address='127.0.0.1:50051', device_id=1, proto_dump_file=None): self.name = name self.address = address self.device_id = device_id self.p4info = None self.channel = grpc.insecure_channel(self.address) self.client_stub = p4runtime_pb2.P4RuntimeStub(self.channel) self.requests_stream = IterableQueue() self.stream_msg_resp = self.client_stub.StreamChannel( iter(self.requests_stream)) self.proto_dump_file = proto_dump_file
def __init__(self, name=None, address='127.0.0.1:50051', device_id=0, proto_dump_file=None): self.name = name self.address = address self.device_id = device_id self.p4info = None self.channel = grpc.insecure_channel(self.address) if proto_dump_file is not None: interceptor = GrpcRequestLogger(proto_dump_file) self.channel = grpc.intercept_channel(self.channel, interceptor) self.client_stub = p4runtime_pb2.P4RuntimeStub(self.channel) self.requests_stream = IterableQueue() self.stream_msg_resp = self.client_stub.StreamChannel(iter(self.requests_stream)) self.proto_dump_file = proto_dump_file connections.append(self)
def setUp(self, proto_bin_path): BaseTest.setUp(self) self.target = testutils.test_param_get('target') if not self.target: self.target = "bmv2" elif self.target not in {"bmv2"}: print "Unsupported target", self.target sys.exit(1) self.device_id = 0 # Setting up PTF dataplane self.dataplane = ptf.dataplane_instance self.dataplane.flush() self.device_id = 0 self.channel = grpc.insecure_channel('localhost:50051') self.stub = p4runtime_pb2.P4RuntimeStub(self.channel) ConfigRequest = p4runtime_pb2.SetForwardingPipelineConfigRequest req = ConfigRequest() print "Connecting to device" req.action = ConfigRequest().VERIFY_AND_COMMIT config = req.configs.add() config.device_id = self.device_id print "Importing p4info proto from", proto_bin_path with open(proto_bin_path, "rb") as fin: config.p4info.ParseFromString(fin.read()) # we save p4info for name -> id lookups self.p4info = config.p4info if self.target == "bmv2": device_config = p4config_pb2.P4DeviceConfig() extras = device_config.extras extras.kv["port"] = "9090" extras.kv["notifications"] = "ipc:///tmp/bmv2-0-notifications.ipc" config.p4_device_config = device_config.SerializeToString() rep = self.stub.SetForwardingPipelineConfig(req)
def update_config(config_path, p4info_path, grpc_addr, device_id): ''' Performs a SetForwardingPipelineConfig on the device with provided P4Info and binary device config ''' channel = grpc.insecure_channel(grpc_addr) stub = p4runtime_pb2.P4RuntimeStub(channel) info("Sending P4 config") request = p4runtime_pb2.SetForwardingPipelineConfigRequest() request.device_id = device_id config = request.config with open(p4info_path, 'r') as p4info_f: google.protobuf.text_format.Merge(p4info_f.read(), config.p4info) with open(config_path, 'rb') as config_f: config.p4_device_config = config_f.read() request.action = p4runtime_pb2.SetForwardingPipelineConfigRequest.VERIFY_AND_COMMIT try: response = stub.SetForwardingPipelineConfig(request) except Exception as e: error("Error during SetForwardingPipelineConfig") error(str(e)) return False return True