示例#1
0
文件: iovctl.c 项目: fengsi/freebsd
/*
 * Call the ioctl that activates SR-IOV and creates the VFs.
 */
static void
config_iov(int fd, const char *dev_name, const nvlist_t *config, int dryrun)
{
	struct pci_iov_arg arg;
	int error;

	arg.config = nvlist_pack(config, &arg.len);
	if (arg.config == NULL)
		err(1, "Could not pack configuration");

	if (dryrun) {
		printf("Would enable SR-IOV on device '%s'.\n", dev_name);
		printf(
		    "The following configuration parameters would be used:\n");
		nvlist_fdump(config, stdout);
		printf(
		"The configuration parameters consume %zu bytes when packed.\n",
		    arg.len);
	} else {
		error = ioctl(fd, IOV_CONFIG, &arg);
		if (error != 0)
			err(1, "Failed to configure SR-IOV");
	}

	free(arg.config);
}
示例#2
0
int
main(int argc, char **argv)
{
	nvlist_t *nvl;
	char *buf;
	int ret;
	size_t size;
	FILE *pfile;
	bool print;

	ret = 1;
	nvl = NULL;
	buf = NULL;
	pfile = NULL;
	print = false;
	if (argc != 2 && argc != 3) {
		fprintf(stderr, "Usage: %s [filename] [print]\n", argv[0]);
		return (1);
	}
	if (argc == 3 && strcmp(argv[2], "print"))
		print = true;

	pfile = fopen(argv[1], "r");
	if (pfile == NULL) {
		fprintf(stderr, "Unable to open file %s.\n", argv[1]);
		return (1);
	}

	fseek(pfile, 0, SEEK_END);
	size = ftell(pfile);
	fseek(pfile, 0, SEEK_SET);

	buf = malloc(size);
	if (buf == NULL) {
		fprintf(stderr, "Unable to read file %s.\n", argv[1]);
		goto out;
	}

	if (fread(buf, 1, size, pfile) != size) {
		fprintf(stderr, "Unable to read full file %s.\n", argv[1]);
		goto out;
	}

	nvl = nvlist_unpack(buf, size, 0);
	if (nvl == NULL || nvlist_error(nvl) != 0)
		printf("Failed to unpack.\n");
	if (print)
		nvlist_fdump(nvl, stdout);

	ret = 0;
out:
	if (pfile != NULL)
		fclose(pfile);
	if (nvl != NULL)
		nvlist_destroy(nvl);
	free(buf);

	return (ret);
}
示例#3
0
void
service_message(struct service *service, struct service_connection *sconn)
{
	nvlist_t *nvlin, *nvlout;
	const char *cmd;
	int error;

	nvlin = cap_recv_nvlist(service_connection_get_chan(sconn));
	if (nvlin == NULL) {
		if (errno == ENOTCONN) {
			pjdlog_debug(1, "Connection closed by the client.");
		} else {
			pjdlog_errno(LOG_ERR,
			    "Unable to receive message from client");
		}
		service_connection_remove(service, sconn);
		return;
	}

	error = EDOOFUS;
	nvlout = nvlist_create(0);

	cmd = nvlist_get_string(nvlin, "cmd");
	pjdlog_debug(1, "Command received from client: %s.", cmd);
	if (pjdlog_debug_get() >= 2)
		nvlist_fdump(nvlin, stderr);
	if (strcmp(cmd, "limit_set") == 0) {
		nvlist_t *nvllim;

		nvllim = nvlist_take_nvlist(nvlin, "limits");
		error = service->s_limit(service_connection_get_limits(sconn),
		    nvllim);
		if (error == 0) {
			service_connection_set_limits(sconn, nvllim);
			/* Function consumes nvllim. */
		} else {
			nvlist_destroy(nvllim);
		}
	} else if (strcmp(cmd, "limit_get") == 0) {
		const nvlist_t *nvllim;

		nvllim = service_connection_get_limits(sconn);
		if (nvllim != NULL)
			nvlist_add_nvlist(nvlout, "limits", nvllim);
		else
			nvlist_add_null(nvlout, "limits");
		error = 0;
	} else if (strcmp(cmd, "clone") == 0) {
		int sock;

		sock = service_connection_clone(service, sconn);
		if (sock == -1) {
			error = errno;
		} else {
			nvlist_add_descriptor(nvlout, "sock", sock);
			error = 0;
		}
	} else {
		nvlout = nvlist_create(0);
		error = service->s_command(cmd,
		    service_connection_get_limits(sconn), nvlin, nvlout);
	}

	nvlist_destroy(nvlin);
	nvlist_add_number(nvlout, "error", (uint64_t)error);
	pjdlog_debug(1, "Sending reply to client (error=%d).", error);
	if (pjdlog_debug_get() >= 2)
		nvlist_fdump(nvlout, stderr);

	if (cap_send_nvlist(service_connection_get_chan(sconn), nvlout) == -1) {
		pjdlog_errno(LOG_ERR, "Unable to send message to client");
		service_connection_remove(service, sconn);
		return;
	}
}