iovctl.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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/param.h>
  27. #include <sys/iov.h>
  28. #include <sys/dnv.h>
  29. #include <sys/nv.h>
  30. #include <err.h>
  31. #include <errno.h>
  32. #include <fcntl.h>
  33. #include <regex.h>
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <unistd.h>
  38. #include "iovctl.h"
  39. static void config_action(const char *filename, int dryrun);
  40. static void delete_action(const char *device, int dryrun);
  41. static void print_schema(const char *device);
  42. /*
  43. * Fetch the config schema from the kernel via ioctl. This function has to
  44. * call the ioctl twice: the first returns the amount of memory that we need
  45. * to allocate for the schema, and the second actually fetches the schema.
  46. */
  47. static nvlist_t *
  48. get_schema(int fd)
  49. {
  50. struct pci_iov_schema arg;
  51. nvlist_t *schema;
  52. int error;
  53. /* Do the ioctl() once to fetch the size of the schema. */
  54. arg.schema = NULL;
  55. arg.len = 0;
  56. arg.error = 0;
  57. error = ioctl(fd, IOV_GET_SCHEMA, &arg);
  58. if (error != 0)
  59. err(1, "Could not fetch size of config schema");
  60. arg.schema = malloc(arg.len);
  61. if (arg.schema == NULL)
  62. err(1, "Could not allocate %zu bytes for schema",
  63. arg.len);
  64. /* Now do the ioctl() for real to get the schema. */
  65. error = ioctl(fd, IOV_GET_SCHEMA, &arg);
  66. if (error != 0 || arg.error != 0) {
  67. if (arg.error != 0)
  68. errno = arg.error;
  69. err(1, "Could not fetch config schema");
  70. }
  71. schema = nvlist_unpack(arg.schema, arg.len, NV_FLAG_IGNORE_CASE);
  72. if (schema == NULL)
  73. err(1, "Could not unpack schema");
  74. free(arg.schema);
  75. return (schema);
  76. }
  77. /*
  78. * Call the ioctl that activates SR-IOV and creates the VFs.
  79. */
  80. static void
  81. config_iov(int fd, const char *dev_name, const nvlist_t *config, int dryrun)
  82. {
  83. struct pci_iov_arg arg;
  84. int error;
  85. arg.config = nvlist_pack(config, &arg.len);
  86. if (arg.config == NULL)
  87. err(1, "Could not pack configuration");
  88. if (dryrun) {
  89. printf("Would enable SR-IOV on device '%s'.\n", dev_name);
  90. printf(
  91. "The following configuration parameters would be used:\n");
  92. nvlist_fdump(config, stdout);
  93. printf(
  94. "The configuration parameters consume %zu bytes when packed.\n",
  95. arg.len);
  96. } else {
  97. error = ioctl(fd, IOV_CONFIG, &arg);
  98. if (error != 0)
  99. err(1, "Failed to configure SR-IOV");
  100. }
  101. free(arg.config);
  102. }
  103. static int
  104. open_device(const char *dev_name)
  105. {
  106. char *dev;
  107. int fd;
  108. size_t copied, size;
  109. long path_max;
  110. path_max = pathconf("/dev", _PC_PATH_MAX);
  111. if (path_max < 0)
  112. err(1, "Could not get maximum path length");
  113. size = path_max;
  114. dev = malloc(size);
  115. if (dev == NULL)
  116. err(1, "Could not allocate memory for device path");
  117. if (dev_name[0] == '/')
  118. copied = strlcpy(dev, dev_name, size);
  119. else
  120. copied = snprintf(dev, size, "/dev/iov/%s", dev_name);
  121. /* >= to account for null terminator. */
  122. if (copied >= size)
  123. errx(1, "Provided file name too long");
  124. fd = open(dev, O_RDWR);
  125. if (fd < 0)
  126. err(1, "Could not open device '%s'", dev);
  127. free(dev);
  128. return (fd);
  129. }
  130. static void
  131. usage(void)
  132. {
  133. warnx("Usage: iovctl -C -f <config file> [-n]");
  134. warnx(" iovctl -D [-d <PF device> | -f <config file>] [-n]");
  135. warnx(" iovctl -S [-d <PF device> | -f <config file>]");
  136. exit(1);
  137. }
  138. enum main_action {
  139. NONE,
  140. CONFIG,
  141. DELETE,
  142. PRINT_SCHEMA,
  143. };
  144. int
  145. main(int argc, char **argv)
  146. {
  147. char *device;
  148. const char *filename;
  149. int ch, dryrun;
  150. enum main_action action;
  151. device = NULL;
  152. filename = NULL;
  153. dryrun = 0;
  154. action = NONE;
  155. while ((ch = getopt(argc, argv, "Cd:Df:nS")) != -1) {
  156. switch (ch) {
  157. case 'C':
  158. if (action != NONE) {
  159. warnx(
  160. "Only one of -C, -D or -S may be specified");
  161. usage();
  162. }
  163. action = CONFIG;
  164. break;
  165. case 'd':
  166. device = strdup(optarg);
  167. break;
  168. case 'D':
  169. if (action != NONE) {
  170. warnx(
  171. "Only one of -C, -D or -S may be specified");
  172. usage();
  173. }
  174. action = DELETE;
  175. break;
  176. case 'f':
  177. filename = optarg;
  178. break;
  179. case 'n':
  180. dryrun = 1;
  181. break;
  182. case 'S':
  183. if (action != NONE) {
  184. warnx(
  185. "Only one of -C, -D or -S may be specified");
  186. usage();
  187. }
  188. action = PRINT_SCHEMA;
  189. break;
  190. case '?':
  191. warnx("Unrecognized argument '-%c'\n", optopt);
  192. usage();
  193. break;
  194. }
  195. }
  196. if (device != NULL && filename != NULL) {
  197. warnx("Only one of the -d and -f flags may be specified");
  198. usage();
  199. }
  200. if (device == NULL && filename == NULL && action != CONFIG) {
  201. warnx("Either the -d or -f flag must be specified");
  202. usage();
  203. }
  204. switch (action) {
  205. case CONFIG:
  206. if (device != NULL) {
  207. warnx("-d flag cannot be used with the -C flag");
  208. usage();
  209. }
  210. if (filename == NULL) {
  211. warnx("The -f flag must be specified");
  212. usage();
  213. }
  214. config_action(filename, dryrun);
  215. break;
  216. case DELETE:
  217. if (device == NULL)
  218. device = find_device(filename);
  219. delete_action(device, dryrun);
  220. free(device);
  221. break;
  222. case PRINT_SCHEMA:
  223. if (dryrun) {
  224. warnx("-n flag cannot be used with the -S flag");
  225. usage();
  226. }
  227. if (device == NULL)
  228. device = find_device(filename);
  229. print_schema(device);
  230. free(device);
  231. break;
  232. default:
  233. usage();
  234. break;
  235. }
  236. exit(0);
  237. }
  238. static void
  239. config_action(const char *filename, int dryrun)
  240. {
  241. char *dev;
  242. nvlist_t *schema, *config;
  243. int fd;
  244. dev = find_device(filename);
  245. fd = open(dev, O_RDWR);
  246. if (fd < 0)
  247. err(1, "Could not open device '%s'", dev);
  248. schema = get_schema(fd);
  249. config = parse_config_file(filename, schema);
  250. if (config == NULL)
  251. errx(1, "Could not parse config");
  252. config_iov(fd, dev, config, dryrun);
  253. nvlist_destroy(config);
  254. nvlist_destroy(schema);
  255. free(dev);
  256. close(fd);
  257. }
  258. static void
  259. delete_action(const char *dev_name, int dryrun)
  260. {
  261. int fd, error;
  262. fd = open_device(dev_name);
  263. if (dryrun)
  264. printf("Would attempt to delete all VF children of '%s'\n",
  265. dev_name);
  266. else {
  267. error = ioctl(fd, IOV_DELETE);
  268. if (error != 0)
  269. err(1, "Failed to delete VFs");
  270. }
  271. close(fd);
  272. }
  273. static void
  274. print_default_value(const nvlist_t *parameter, const char *type)
  275. {
  276. const uint8_t *mac;
  277. size_t size;
  278. if (strcasecmp(type, "bool") == 0)
  279. printf(" (default = %s)",
  280. nvlist_get_bool(parameter, DEFAULT_SCHEMA_NAME) ? "true" :
  281. "false");
  282. else if (strcasecmp(type, "string") == 0)
  283. printf(" (default = %s)",
  284. nvlist_get_string(parameter, DEFAULT_SCHEMA_NAME));
  285. else if (strcasecmp(type, "uint8_t") == 0)
  286. printf(" (default = %ju)",
  287. (uintmax_t)nvlist_get_number(parameter,
  288. DEFAULT_SCHEMA_NAME));
  289. else if (strcasecmp(type, "uint16_t") == 0)
  290. printf(" (default = %ju)",
  291. (uintmax_t)nvlist_get_number(parameter,
  292. DEFAULT_SCHEMA_NAME));
  293. else if (strcasecmp(type, "uint32_t") == 0)
  294. printf(" (default = %ju)",
  295. (uintmax_t)nvlist_get_number(parameter,
  296. DEFAULT_SCHEMA_NAME));
  297. else if (strcasecmp(type, "uint64_t") == 0)
  298. printf(" (default = %ju)",
  299. (uintmax_t)nvlist_get_number(parameter,
  300. DEFAULT_SCHEMA_NAME));
  301. else if (strcasecmp(type, "unicast-mac") == 0) {
  302. mac = nvlist_get_binary(parameter, DEFAULT_SCHEMA_NAME, &size);
  303. printf(" (default = %02x:%02x:%02x:%02x:%02x:%02x)", mac[0],
  304. mac[1], mac[2], mac[3], mac[4], mac[5]);
  305. } else if (strcasecmp(type, "vlan") == 0) {
  306. uint16_t vlan = nvlist_get_number(parameter, DEFAULT_SCHEMA_NAME);
  307. if (vlan == VF_VLAN_TRUNK)
  308. printf(" (default = trunk)");
  309. else
  310. printf(" (default = %d)", vlan);
  311. } else
  312. errx(1, "Unexpected type in schema: '%s'", type);
  313. }
  314. static void
  315. print_subsystem_schema(const nvlist_t * subsystem_schema)
  316. {
  317. const char *name, *type;
  318. const nvlist_t *parameter;
  319. void *it;
  320. int nvtype;
  321. it = NULL;
  322. while ((name = nvlist_next(subsystem_schema, &nvtype, &it)) != NULL) {
  323. parameter = nvlist_get_nvlist(subsystem_schema, name);
  324. type = nvlist_get_string(parameter, TYPE_SCHEMA_NAME);
  325. printf("\t%s : %s", name, type);
  326. if (dnvlist_get_bool(parameter, REQUIRED_SCHEMA_NAME, false))
  327. printf(" (required)");
  328. else if (nvlist_exists(parameter, DEFAULT_SCHEMA_NAME))
  329. print_default_value(parameter, type);
  330. else
  331. printf(" (optional)");
  332. printf("\n");
  333. }
  334. }
  335. static void
  336. print_schema(const char *dev_name)
  337. {
  338. nvlist_t *schema;
  339. const nvlist_t *iov_schema, *driver_schema, *pf_schema, *vf_schema;
  340. int fd;
  341. fd = open_device(dev_name);
  342. schema = get_schema(fd);
  343. pf_schema = nvlist_get_nvlist(schema, PF_CONFIG_NAME);
  344. iov_schema = nvlist_get_nvlist(pf_schema, IOV_CONFIG_NAME);
  345. driver_schema = nvlist_get_nvlist(pf_schema, DRIVER_CONFIG_NAME);
  346. printf(
  347. "The following configuration parameters may be configured on the PF:\n");
  348. print_subsystem_schema(iov_schema);
  349. print_subsystem_schema(driver_schema);
  350. vf_schema = nvlist_get_nvlist(schema, VF_SCHEMA_NAME);
  351. iov_schema = nvlist_get_nvlist(vf_schema, IOV_CONFIG_NAME);
  352. driver_schema = nvlist_get_nvlist(vf_schema, DRIVER_CONFIG_NAME);
  353. printf(
  354. "\nThe following configuration parameters may be configured on a VF:\n");
  355. print_subsystem_schema(iov_schema);
  356. print_subsystem_schema(driver_schema);
  357. nvlist_destroy(schema);
  358. close(fd);
  359. }