PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/usr.sbin/iovctl/iovctl.c

https://bitbucket.org/freebsd/freebsd-base
C | 407 lines | 319 code | 52 blank | 36 comment | 86 complexity | b4263a2f7d9d98a7a263a3c09ce8d5c7 MD5 | raw file
  1. /*-
  2. * Copyright (c) 2013-2015 Sandvine Inc.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. * SUCH DAMAGE.
  25. */
  26. #include <sys/cdefs.h>
  27. __FBSDID("$FreeBSD$");
  28. #include <sys/param.h>
  29. #include <sys/iov.h>
  30. #include <sys/dnv.h>
  31. #include <sys/nv.h>
  32. #include <err.h>
  33. #include <errno.h>
  34. #include <fcntl.h>
  35. #include <regex.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <unistd.h>
  40. #include "iovctl.h"
  41. static void config_action(const char *filename, int dryrun);
  42. static void delete_action(const char *device, int dryrun);
  43. static void print_schema(const char *device);
  44. /*
  45. * Fetch the config schema from the kernel via ioctl. This function has to
  46. * call the ioctl twice: the first returns the amount of memory that we need
  47. * to allocate for the schema, and the second actually fetches the schema.
  48. */
  49. static nvlist_t *
  50. get_schema(int fd)
  51. {
  52. struct pci_iov_schema arg;
  53. nvlist_t *schema;
  54. int error;
  55. /* Do the ioctl() once to fetch the size of the schema. */
  56. arg.schema = NULL;
  57. arg.len = 0;
  58. arg.error = 0;
  59. error = ioctl(fd, IOV_GET_SCHEMA, &arg);
  60. if (error != 0)
  61. err(1, "Could not fetch size of config schema");
  62. arg.schema = malloc(arg.len);
  63. if (arg.schema == NULL)
  64. err(1, "Could not allocate %zu bytes for schema",
  65. arg.len);
  66. /* Now do the ioctl() for real to get the schema. */
  67. error = ioctl(fd, IOV_GET_SCHEMA, &arg);
  68. if (error != 0 || arg.error != 0) {
  69. if (arg.error != 0)
  70. errno = arg.error;
  71. err(1, "Could not fetch config schema");
  72. }
  73. schema = nvlist_unpack(arg.schema, arg.len, NV_FLAG_IGNORE_CASE);
  74. if (schema == NULL)
  75. err(1, "Could not unpack schema");
  76. free(arg.schema);
  77. return (schema);
  78. }
  79. /*
  80. * Call the ioctl that activates SR-IOV and creates the VFs.
  81. */
  82. static void
  83. config_iov(int fd, const char *dev_name, const nvlist_t *config, int dryrun)
  84. {
  85. struct pci_iov_arg arg;
  86. int error;
  87. arg.config = nvlist_pack(config, &arg.len);
  88. if (arg.config == NULL)
  89. err(1, "Could not pack configuration");
  90. if (dryrun) {
  91. printf("Would enable SR-IOV on device '%s'.\n", dev_name);
  92. printf(
  93. "The following configuration parameters would be used:\n");
  94. nvlist_fdump(config, stdout);
  95. printf(
  96. "The configuration parameters consume %zu bytes when packed.\n",
  97. arg.len);
  98. } else {
  99. error = ioctl(fd, IOV_CONFIG, &arg);
  100. if (error != 0)
  101. err(1, "Failed to configure SR-IOV");
  102. }
  103. free(arg.config);
  104. }
  105. static int
  106. open_device(const char *dev_name)
  107. {
  108. char *dev;
  109. int fd;
  110. size_t copied, size;
  111. long path_max;
  112. path_max = pathconf("/dev", _PC_PATH_MAX);
  113. if (path_max < 0)
  114. err(1, "Could not get maximum path length");
  115. size = path_max;
  116. dev = malloc(size);
  117. if (dev == NULL)
  118. err(1, "Could not allocate memory for device path");
  119. if (dev_name[0] == '/')
  120. copied = strlcpy(dev, dev_name, size);
  121. else
  122. copied = snprintf(dev, size, "/dev/iov/%s", dev_name);
  123. /* >= to account for null terminator. */
  124. if (copied >= size)
  125. errx(1, "Provided file name too long");
  126. fd = open(dev, O_RDWR);
  127. if (fd < 0)
  128. err(1, "Could not open device '%s'", dev);
  129. free(dev);
  130. return (fd);
  131. }
  132. static void
  133. usage(void)
  134. {
  135. warnx("Usage: iovctl -C -f <config file> [-n]");
  136. warnx(" iovctl -D [-d <PF device> | -f <config file>] [-n]");
  137. warnx(" iovctl -S [-d <PF device> | -f <config file>]");
  138. exit(1);
  139. }
  140. enum main_action {
  141. NONE,
  142. CONFIG,
  143. DELETE,
  144. PRINT_SCHEMA,
  145. };
  146. int
  147. main(int argc, char **argv)
  148. {
  149. char *device;
  150. const char *filename;
  151. int ch, dryrun;
  152. enum main_action action;
  153. device = NULL;
  154. filename = NULL;
  155. dryrun = 0;
  156. action = NONE;
  157. while ((ch = getopt(argc, argv, "Cd:Df:nS")) != -1) {
  158. switch (ch) {
  159. case 'C':
  160. if (action != NONE) {
  161. warnx(
  162. "Only one of -C, -D or -S may be specified");
  163. usage();
  164. }
  165. action = CONFIG;
  166. break;
  167. case 'd':
  168. device = strdup(optarg);
  169. break;
  170. case 'D':
  171. if (action != NONE) {
  172. warnx(
  173. "Only one of -C, -D or -S may be specified");
  174. usage();
  175. }
  176. action = DELETE;
  177. break;
  178. case 'f':
  179. filename = optarg;
  180. break;
  181. case 'n':
  182. dryrun = 1;
  183. break;
  184. case 'S':
  185. if (action != NONE) {
  186. warnx(
  187. "Only one of -C, -D or -S may be specified");
  188. usage();
  189. }
  190. action = PRINT_SCHEMA;
  191. break;
  192. case '?':
  193. warnx("Unrecognized argument '-%c'\n", optopt);
  194. usage();
  195. break;
  196. }
  197. }
  198. if (device != NULL && filename != NULL) {
  199. warnx("Only one of the -d and -f flags may be specified");
  200. usage();
  201. }
  202. if (device == NULL && filename == NULL && action != CONFIG) {
  203. warnx("Either the -d or -f flag must be specified");
  204. usage();
  205. }
  206. switch (action) {
  207. case CONFIG:
  208. if (device != NULL) {
  209. warnx("-d flag cannot be used with the -C flag");
  210. usage();
  211. }
  212. if (filename == NULL) {
  213. warnx("The -f flag must be specified");
  214. usage();
  215. }
  216. config_action(filename, dryrun);
  217. break;
  218. case DELETE:
  219. if (device == NULL)
  220. device = find_device(filename);
  221. delete_action(device, dryrun);
  222. free(device);
  223. break;
  224. case PRINT_SCHEMA:
  225. if (dryrun) {
  226. warnx("-n flag cannot be used with the -S flag");
  227. usage();
  228. }
  229. if (device == NULL)
  230. device = find_device(filename);
  231. print_schema(device);
  232. free(device);
  233. break;
  234. default:
  235. usage();
  236. break;
  237. }
  238. exit(0);
  239. }
  240. static void
  241. config_action(const char *filename, int dryrun)
  242. {
  243. char *dev;
  244. nvlist_t *schema, *config;
  245. int fd;
  246. dev = find_device(filename);
  247. fd = open(dev, O_RDWR);
  248. if (fd < 0)
  249. err(1, "Could not open device '%s'", dev);
  250. schema = get_schema(fd);
  251. config = parse_config_file(filename, schema);
  252. if (config == NULL)
  253. errx(1, "Could not parse config");
  254. config_iov(fd, dev, config, dryrun);
  255. nvlist_destroy(config);
  256. nvlist_destroy(schema);
  257. free(dev);
  258. close(fd);
  259. }
  260. static void
  261. delete_action(const char *dev_name, int dryrun)
  262. {
  263. int fd, error;
  264. fd = open_device(dev_name);
  265. if (dryrun)
  266. printf("Would attempt to delete all VF children of '%s'\n",
  267. dev_name);
  268. else {
  269. error = ioctl(fd, IOV_DELETE);
  270. if (error != 0)
  271. err(1, "Failed to delete VFs");
  272. }
  273. close(fd);
  274. }
  275. static void
  276. print_default_value(const nvlist_t *parameter, const char *type)
  277. {
  278. const uint8_t *mac;
  279. size_t size;
  280. if (strcasecmp(type, "bool") == 0)
  281. printf(" (default = %s)",
  282. nvlist_get_bool(parameter, DEFAULT_SCHEMA_NAME) ? "true" :
  283. "false");
  284. else if (strcasecmp(type, "string") == 0)
  285. printf(" (default = %s)",
  286. nvlist_get_string(parameter, DEFAULT_SCHEMA_NAME));
  287. else if (strcasecmp(type, "uint8_t") == 0)
  288. printf(" (default = %ju)",
  289. (uintmax_t)nvlist_get_number(parameter,
  290. DEFAULT_SCHEMA_NAME));
  291. else if (strcasecmp(type, "uint16_t") == 0)
  292. printf(" (default = %ju)",
  293. (uintmax_t)nvlist_get_number(parameter,
  294. DEFAULT_SCHEMA_NAME));
  295. else if (strcasecmp(type, "uint32_t") == 0)
  296. printf(" (default = %ju)",
  297. (uintmax_t)nvlist_get_number(parameter,
  298. DEFAULT_SCHEMA_NAME));
  299. else if (strcasecmp(type, "uint64_t") == 0)
  300. printf(" (default = %ju)",
  301. (uintmax_t)nvlist_get_number(parameter,
  302. DEFAULT_SCHEMA_NAME));
  303. else if (strcasecmp(type, "unicast-mac") == 0) {
  304. mac = nvlist_get_binary(parameter, DEFAULT_SCHEMA_NAME, &size);
  305. printf(" (default = %02x:%02x:%02x:%02x:%02x:%02x)", mac[0],
  306. mac[1], mac[2], mac[3], mac[4], mac[5]);
  307. } else
  308. errx(1, "Unexpected type in schema: '%s'", type);
  309. }
  310. static void
  311. print_subsystem_schema(const nvlist_t * subsystem_schema)
  312. {
  313. const char *name, *type;
  314. const nvlist_t *parameter;
  315. void *it;
  316. int nvtype;
  317. it = NULL;
  318. while ((name = nvlist_next(subsystem_schema, &nvtype, &it)) != NULL) {
  319. parameter = nvlist_get_nvlist(subsystem_schema, name);
  320. type = nvlist_get_string(parameter, TYPE_SCHEMA_NAME);
  321. printf("\t%s : %s", name, type);
  322. if (dnvlist_get_bool(parameter, REQUIRED_SCHEMA_NAME, false))
  323. printf(" (required)");
  324. else if (nvlist_exists(parameter, DEFAULT_SCHEMA_NAME))
  325. print_default_value(parameter, type);
  326. else
  327. printf(" (optional)");
  328. printf("\n");
  329. }
  330. }
  331. static void
  332. print_schema(const char *dev_name)
  333. {
  334. nvlist_t *schema;
  335. const nvlist_t *iov_schema, *driver_schema, *pf_schema, *vf_schema;
  336. int fd;
  337. fd = open_device(dev_name);
  338. schema = get_schema(fd);
  339. pf_schema = nvlist_get_nvlist(schema, PF_CONFIG_NAME);
  340. iov_schema = nvlist_get_nvlist(pf_schema, IOV_CONFIG_NAME);
  341. driver_schema = nvlist_get_nvlist(pf_schema, DRIVER_CONFIG_NAME);
  342. printf(
  343. "The following configuration parameters may be configured on the PF:\n");
  344. print_subsystem_schema(iov_schema);
  345. print_subsystem_schema(driver_schema);
  346. vf_schema = nvlist_get_nvlist(schema, VF_SCHEMA_NAME);
  347. iov_schema = nvlist_get_nvlist(vf_schema, IOV_CONFIG_NAME);
  348. driver_schema = nvlist_get_nvlist(vf_schema, DRIVER_CONFIG_NAME);
  349. printf(
  350. "\nThe following configuration parameters may be configured on a VF:\n");
  351. print_subsystem_schema(iov_schema);
  352. print_subsystem_schema(driver_schema);
  353. nvlist_destroy(schema);
  354. close(fd);
  355. }