def __build_device_config_tofino(self): """ Builds the device config for Tofino """ prog_name = self.sw_info['type'] bin_path = self.sw_info['bin_path'] cxt_json_path = self.sw_info['cxt_json_path'] logger.info( 'Building device configuration for program - [%s], bin_path - [%s]' ', and cxt_json_path - [%s]', prog_name, bin_path, cxt_json_path) prog_name = prog_name.encode('utf-8') device_config = p4config_pb2.P4DeviceConfig() device_config.reassign = True with open(bin_path, 'rb') as bin_f: with open(cxt_json_path, 'r') as cxt_json_f: device_config.device_data = "" device_config.device_data += struct.pack("<i", len(prog_name)) device_config.device_data += prog_name tofino_bin = bin_f.read() device_config.device_data += struct.pack("<i", len(tofino_bin)) device_config.device_data += tofino_bin cxt_json = cxt_json_f.read() device_config.device_data += struct.pack("<i", len(cxt_json)) device_config.device_data += cxt_json return device_config
def buildDeviceConfig(bmv2_json_file_path=None): "Builds the device config for BMv2" device_config = p4config_pb2.P4DeviceConfig() device_config.reassign = True with open(bmv2_json_file_path) as f: device_config.device_data = f.read().encode('utf-8') return device_config
def main(): parser = argparse.ArgumentParser( description="Invokes the p4c-bm2-ss compiler and generates a " "binary file to be used as the 'p4_device_config' field in the " "ForwardingPipelineConfig p4runtime.proto message.") parser.add_argument('src', type=str, help='Path to P4 source') parser.add_argument('--out-bin', type=str, required=True, help='Path for output binary file') parser.add_argument('--out-p4info', type=str, required=True, help='Path for output p4info text message') parser.add_argument('--compiler', type=str, default='p4c-bm2-ss', help='bmv2 compiler to use. Default is p4c-bm2-ss.') args = parser.parse_args() if not check_compiler_exec(args.compiler): print "Cannot use provided compiler or compiler binary not in PATH" sys.exit(1) if not os.path.exists(args.src): print "P4 source", args.src, "does not exist" sys.exit(1) tmp_dir = tempfile.mkdtemp() out_json = os.path.join(tmp_dir, 'dp.json') out_p4info = os.path.join(tmp_dir, 'p4info.proto.txt') cmd = [ args.compiler, '--p4-16', args.src, '-o', out_json, '--p4runtime-format', 'text', '--p4runtime-file', out_p4info ] print ' '.join(cmd) try: subprocess.check_output(cmd, stderr=subprocess.STDOUT) except subprocess.CalledProcessError: print "Error when compiling P4 program" print out shutil.rmtree(tmp_dir) sys.exit(1) except OSError: print "Fatal error when compiling P4 program" shutil.rmtree(tmp_dir) sys.exit(2) try: shutil.copyfile(out_p4info, args.out_p4info) except: print "Error when writing to", args.out_p4info sys.exit(1) with open(args.out_bin, 'wf') as f_out: with open(out_json, 'r') as f_json: device_config = p4config_pb2.P4DeviceConfig() device_config.device_data = f_json.read() f_out.write(device_config.SerializeToString()) shutil.rmtree(tmp_dir)
def buildDeviceConfig(bmv2_json_file_path=None): "Builds the device config for BMv2" device_config = p4config_pb2.P4DeviceConfig() device_config.reassign = True # set device_config.extra to default instance with open(bmv2_json_file_path) as f: device_config.device_data = f.read() return device_config
def build_device_config(self, bmv2_json_file_path=None): logger.info('Building device config for BMV2 with file - [%s]', bmv2_json_file_path) device_config = p4config_pb2.P4DeviceConfig() device_config.reassign = True with open(bmv2_json_file_path) as f: device_config.device_data = f.read() return device_config
def build_bmv2_config(self): """ Builds the device config for BMv2 """ device_config = p4config_pb2.P4DeviceConfig() device_config.reassign = True with open(self.config_path) as f: device_config.device_data = f.read() return device_config
def __build_device_config_bmv2(self): runtime_json = self.sw_info['runtime_json'] logger.info('Building device [%s] config with file - [%s]', self.grpc_addr, runtime_json) device_config = p4config_pb2.P4DeviceConfig() device_config.reassign = True with open(runtime_json) as f: file_data = f.read() device_config.device_data = bytes(file_data, 'utf-8') return device_config
def build_tofino_config(self, prog_name, bin_path, cxt_json_path): device_config = p4config_pb2.P4DeviceConfig() with open(bin_path, 'rb') as bin_f: with open(cxt_json_path, 'r') as cxt_json_f: device_config.device_data = "" device_config.device_data += struct.pack("<i", len(prog_name)) device_config.device_data += prog_name tofino_bin = bin_f.read() device_config.device_data += struct.pack("<i", len(tofino_bin)) device_config.device_data += tofino_bin cxt_json = cxt_json_f.read() device_config.device_data += struct.pack("<i", len(cxt_json)) device_config.device_data += cxt_json return device_config
def set_forwarding_pipeline(self): device_config = p4config_pb2.P4DeviceConfig() device_config.reassign = True with open(self.bmv2_json) as f: device_config.device_data = f.read() request = p4runtime_pb2.SetForwardingPipelineConfigRequest() request.election_id.CopyFrom(self.election_id) request.device_id = self.device_id config = request.config config.p4info.CopyFrom(self.p4info) config.p4_device_config = device_config.SerializeToString() request.action = p4runtime_pb2.SetForwardingPipelineConfigRequest.VERIFY_AND_COMMIT self.client_stub.SetForwardingPipelineConfig(request) return
def buildDeviceConfig(bmv2_json_file_path=None, port=None, notifications=None, cpu_iface=None): "Builds the device config for BMv2" device_config = p4config_pb2.P4DeviceConfig() device_config.reassign = True # (*kv)["port"] = "9090"; # (*kv)["notifications"] = "ipc:///tmp/bmv2-0-notifications.ipc"; # (*kv)["cpu_iface"] = "veth251"; #device_config.extras.kv["port"] = port #device_config.extras.kv["notifications"] = notifications #device_config.extras.kv["cpu_iface"] = cpu_iface #print(device_config) with open(bmv2_json_file_path) as f: device_config.device_data = f.read() return device_config
def buildDeviceConfig(prog_name="sample", tofino_bin=None, cxt_json=None): "Builds the device config for Tofino based switch" device_config = p4config_pb2.P4DeviceConfig() device_config.reassign = True with open(tofino_bin) as tofino_bin_f: with open(cxt_json) as cxt_json_f: device_config.device_data = "" device_config.device_data += struct.pack("<i", len(prog_name)) device_config.device_data += prog_name tofino_bin = tofino_bin_f.read() device_config.device_data += struct.pack("<i", len(tofino_bin)) device_config.device_data += tofino_bin cxt_json = cxt_json_f.read() device_config.device_data += struct.pack("<i", len(cxt_json)) device_config.device_data += cxt_json return device_config
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 buildDeviceConfig(self, **kwargs): return p4config_pb2.P4DeviceConfig()
def bmv2_json_to_device_config(bmv2_json_fname, bmv2_bin_fname): with open(bmv2_bin_fname, 'wf') as f_out: with open(bmv2_json_fname, 'r') as f_json: device_config = p4config_pb2.P4DeviceConfig() device_config.device_data = f_json.read() f_out.write(device_config.SerializeToString())
def buildDeviceConfig(self, **kwargs): Log.debug("before build device config") return p4config_pb2.P4DeviceConfig()