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) self.import_p4info_names() # 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 update_config(p4info_path, bmv2_json_path, tofino_bin_path, tofino_cxt_json_path, grpc_addr, device_id): """ Performs a SetForwardingPipelineConfig on the device """ 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) if bmv2_json_path is not None: device_config = build_bmv2_config(bmv2_json_path) else: device_config = build_tofino_config("name", tofino_bin_path, tofino_cxt_json_path) config.p4_device_config = device_config.SerializeToString() request.action = p4runtime_pb2.SetForwardingPipelineConfigRequest.VERIFY_AND_COMMIT try: stub.SetForwardingPipelineConfig(request) except Exception as e: error("Error during SetForwardingPipelineConfig") error(str(e)) return False return True
def __init__(self, grpc_addr, device_id, device, election_id, role_id, config_path, p4info_path, ctx_json): self.grpc_addr = grpc_addr if self.grpc_addr is None: self.grpc_addr = 'localhost:50051' self.device_id = int(device_id) if self.device_id is None: print("Device ID is not set") self.device = device self.config_path = config_path self.p4info_path = p4info_path self.ctx_json_path = ctx_json self.channel = grpc.insecure_channel(self.grpc_addr) self.stub = p4runtime_pb2.P4RuntimeStub(self.channel) print "Importing p4info proto from", p4info_path self.p4info = p4info_pb2.P4Info() with open(p4info_path, "rb") as fin: google.protobuf.text_format.Merge(fin.read(), self.p4info) self.p4info_helper = helper.P4InfoHelper(p4info_path) self.import_p4info_names() # used to store write requests sent to the P4Runtime server, useful for # autocleanup of tests (see definition of autocleanup decorator below) self.reqs = [] self.election_id = election_id self.role_id = role_id self.set_up_stream()
def setUp(self): BaseTest.setUp(self) # 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) self.port1 = self.swports(1) self.port2 = self.swports(2) self.port3 = self.swports(3) grpc_addr = testutils.test_param_get("grpcaddr") if grpc_addr is None: grpc_addr = 'localhost:50051' self.device_id = int(testutils.test_param_get("device_id")) if self.device_id is None: self.fail("Device ID is not set") self.cpu_port = int(testutils.test_param_get("cpu_port")) if self.cpu_port is None: self.fail("CPU port is not set") pltfm = testutils.test_param_get("pltfm") if pltfm is not None and pltfm == 'hw' and getattr( self, "_skip_on_hw", False): raise SkipTest("Skipping test in HW") 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) self.helper = P4InfoHelper(proto_txt_path) # used to store write requests sent to the P4Runtime server, useful for # autocleanup of tests (see definition of autocleanup decorator below) self.reqs = [] self.election_id = 1 self.set_up_stream()
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 __init__(self, name, address='127.0.0.1:50051', device_id=0, type='bmv2', crypto_address=None, debug=False): 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.type = type self.crypto_client = None if self.type == 'tofino': self.crypto_client = TofinoCryptoClient(crypto_address) self.debug = debug self.request_lock = threading.Lock()
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) print("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: print("Error during SetForwardingPipelineConfig") print(str(e)) return False return True
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.proto_dump_file = proto_dump_file self.stream_out_q = IterableQueue() self.stream_in_q = IterableQueue() self.stream = self.client_stub.StreamChannel(self.stream_req_iterator()) self.stream_recv_thread = threading.Thread( target=self.stream_recv, args=(self.stream,)) self.active = True connections.append(self)
def update_config(p4info_path, bmv2_json_path, tofino_bin_path, tofino_cxt_json_path, grpc_addr, device_id): """ Performs a SetForwardingPipelineConfig on the device """ channel = grpc.insecure_channel(grpc_addr) stub = p4runtime_pb2.P4RuntimeStub(channel) info("Sending P4 config") # Send master arbitration via stream channel # This should go in library, to be re-used also by base_test.py. stream_out_q = Queue.Queue() stream_in_q = Queue.Queue() def stream_req_iterator(): while True: p = stream_out_q.get() if p is None: break yield p def stream_recv(stream): for p in stream: stream_in_q.put(p) def get_stream_packet(type_, timeout=1): start = time.time() try: while True: remaining = timeout - (time.time() - start) if remaining < 0: break msg = stream_in_q.get(timeout=remaining) if not msg.HasField(type_): continue return msg except: # timeout expired pass return None stream = stub.StreamChannel(stream_req_iterator()) stream_recv_thread = threading.Thread(target=stream_recv, args=(stream, )) stream_recv_thread.start() req = p4runtime_pb2.StreamMessageRequest() arbitration = req.arbitration arbitration.device_id = device_id election_id = arbitration.election_id election_id.high = 0 election_id.low = 1 stream_out_q.put(req) rep = get_stream_packet("arbitration", timeout=5) if rep is None: error("Failed to establish handshake") return False try: # Set pipeline config. request = p4runtime_pb2.SetForwardingPipelineConfigRequest() request.device_id = device_id election_id = request.election_id election_id.high = 0 election_id.low = 1 config = request.config with open(p4info_path, 'r') as p4info_f: google.protobuf.text_format.Merge(p4info_f.read(), config.p4info) if bmv2_json_path is not None: device_config = build_bmv2_config(bmv2_json_path) else: device_config = build_tofino_config("name", tofino_bin_path, tofino_cxt_json_path) config.p4_device_config = device_config.SerializeToString() request.action = p4runtime_pb2.SetForwardingPipelineConfigRequest.VERIFY_AND_COMMIT try: stub.SetForwardingPipelineConfig(request) except Exception as e: error("Error during SetForwardingPipelineConfig") error(str(e)) return False return True finally: stream_out_q.put(None) stream_recv_thread.join()
def __init__(self): self.device_id = args.device_id self.channel = grpc.insecure_channel(args.grpc_addr) self.stub = p4runtime_pb2.P4RuntimeStub(self.channel)