Date: Sun, 2 Dec 2018 23:10:56 +0000 (UTC) From: Warner Losh <imp@FreeBSD.org> To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r341407 - head/sbin/nvmecontrol Message-ID: <201812022310.wB2NAuHc034019@repo.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: imp Date: Sun Dec 2 23:10:55 2018 New Revision: 341407 URL: https://svnweb.freebsd.org/changeset/base/341407 Log: Move nvmecontrol to using linker sets for commands More commands will be added to nvmecontrol. Also, there will be a few more vendor commands (some of which may need to remain private to companies writing them). The first step on that journey is to move to using linker sets to dispatch commands. The next step will be using dlopen to bring in the .so's that have the command that might need to remain private for seamless integration. Similar changes to this will be needed for vendor specific log pages. Sponsored by: Netflix Differential Revision: https://reviews.freebsd.org/D18403 Modified: head/sbin/nvmecontrol/devlist.c head/sbin/nvmecontrol/firmware.c head/sbin/nvmecontrol/format.c head/sbin/nvmecontrol/identify.c head/sbin/nvmecontrol/logpage.c head/sbin/nvmecontrol/ns.c head/sbin/nvmecontrol/nvmecontrol.c head/sbin/nvmecontrol/nvmecontrol.h head/sbin/nvmecontrol/perftest.c head/sbin/nvmecontrol/power.c head/sbin/nvmecontrol/reset.c head/sbin/nvmecontrol/wdc.c Modified: head/sbin/nvmecontrol/devlist.c ============================================================================== --- head/sbin/nvmecontrol/devlist.c Sun Dec 2 21:58:36 2018 (r341406) +++ head/sbin/nvmecontrol/devlist.c Sun Dec 2 23:10:55 2018 (r341407) @@ -43,6 +43,9 @@ __FBSDID("$FreeBSD$"); #include "nvmecontrol.h" +#define DEVLIST_USAGE \ +" nvmecontrol devlist\n" + static void devlist_usage(void) { @@ -64,7 +67,7 @@ ns_get_sector_size(struct nvme_namespace_data *nsdata) return (1 << lbads); } -void +static void devlist(int argc, char *argv[]) { struct nvme_controller_data cdata; @@ -124,3 +127,5 @@ devlist(int argc, char *argv[]) exit(1); } + +NVME_COMMAND(top, devlist, devlist, DEVLIST_USAGE); Modified: head/sbin/nvmecontrol/firmware.c ============================================================================== --- head/sbin/nvmecontrol/firmware.c Sun Dec 2 21:58:36 2018 (r341406) +++ head/sbin/nvmecontrol/firmware.c Sun Dec 2 23:10:55 2018 (r341407) @@ -50,6 +50,10 @@ __FBSDID("$FreeBSD$"); #include "nvmecontrol.h" +#define FIRMWARE_USAGE \ +" nvmecontrol firmware [-s slot] [-f path_to_firmware] [-a] <controller id>\n" + + static int slot_has_valid_firmware(int fd, int slot) { @@ -178,7 +182,7 @@ firmware_usage(void) exit(1); } -void +static void firmware(int argc, char *argv[]) { int fd = -1, slot = 0; @@ -338,3 +342,5 @@ firmware(int argc, char *argv[]) close(fd); exit(0); } + +NVME_COMMAND(top, firmware, firmware, FIRMWARE_USAGE); Modified: head/sbin/nvmecontrol/format.c ============================================================================== --- head/sbin/nvmecontrol/format.c Sun Dec 2 21:58:36 2018 (r341406) +++ head/sbin/nvmecontrol/format.c Sun Dec 2 23:10:55 2018 (r341407) @@ -43,6 +43,9 @@ __FBSDID("$FreeBSD$"); #include "nvmecontrol.h" +#define FORMAT_USAGE \ +" nvmecontrol format [-f fmt] [-m mset] [-p pi] [-l pil] [-E] [-C] <controller id|namespace id>\n" + static void format_usage(void) { @@ -51,7 +54,7 @@ format_usage(void) exit(1); } -void +static void format(int argc, char *argv[]) { struct nvme_controller_data cd; @@ -185,3 +188,5 @@ format(int argc, char *argv[]) close(fd); exit(0); } + +NVME_COMMAND(top, format, format, FORMAT_USAGE); Modified: head/sbin/nvmecontrol/identify.c ============================================================================== --- head/sbin/nvmecontrol/identify.c Sun Dec 2 21:58:36 2018 (r341406) +++ head/sbin/nvmecontrol/identify.c Sun Dec 2 23:10:55 2018 (r341407) @@ -43,6 +43,9 @@ __FBSDID("$FreeBSD$"); #include "nvmecontrol.h" #include "nvmecontrol_ext.h" +#define IDENTIFY_USAGE \ +" nvmecontrol identify [-x [-v]] <controller id|namespace id>\n" + static void print_namespace(struct nvme_namespace_data *nsdata) { @@ -265,7 +268,7 @@ identify_ns(int argc, char *argv[]) exit(0); } -void +static void identify(int argc, char *argv[]) { char *target; @@ -293,3 +296,5 @@ identify(int argc, char *argv[]) else identify_ns(argc, argv); } + +NVME_COMMAND(top, identify, identify, IDENTIFY_USAGE); Modified: head/sbin/nvmecontrol/logpage.c ============================================================================== --- head/sbin/nvmecontrol/logpage.c Sun Dec 2 21:58:36 2018 (r341406) +++ head/sbin/nvmecontrol/logpage.c Sun Dec 2 23:10:55 2018 (r341407) @@ -48,6 +48,9 @@ __FBSDID("$FreeBSD$"); #include "nvmecontrol.h" +#define LOGPAGE_USAGE \ +" nvmecontrol logpage <-p page_id> [-b] [-v vendor] [-x] <controller id|namespace id>\n" \ + #define DEFAULT_SIZE (4096) #define MAX_FW_SLOTS (7) @@ -908,7 +911,7 @@ logpage_help(void) exit(1); } -void +static void logpage(int argc, char *argv[]) { int fd; @@ -1031,3 +1034,5 @@ logpage(int argc, char *argv[]) close(fd); exit(0); } + +NVME_COMMAND(top, logpage, logpage, LOGPAGE_USAGE); Modified: head/sbin/nvmecontrol/ns.c ============================================================================== --- head/sbin/nvmecontrol/ns.c Sun Dec 2 21:58:36 2018 (r341406) +++ head/sbin/nvmecontrol/ns.c Sun Dec 2 23:10:55 2018 (r341407) @@ -41,6 +41,11 @@ __FBSDID("$FreeBSD$"); #include "nvmecontrol.h" +SET_DECLARE(ns, struct nvme_function); + +#define NS_USAGE \ +" nvmecontrol ns (create|delete|attach|detach)\n" + /* handles NVME_OPC_NAMESPACE_MANAGEMENT and ATTACHMENT admin cmds */ #define NSCREATE_USAGE \ @@ -60,13 +65,10 @@ void nsdelete(int argc, char *argv[]); void nsattach(int argc, char *argv[]); void nsdetach(int argc, char *argv[]); -static struct nvme_function ns_funcs[] = { - {"create", nscreate, NSCREATE_USAGE}, - {"delete", nsdelete, NSDELETE_USAGE}, - {"attach", nsattach, NSATTACH_USAGE}, - {"detach", nsdetach, NSDETACH_USAGE}, - {NULL, NULL, NULL}, -}; +NVME_COMMAND(ns, create, nscreate, NSCREATE_USAGE); +NVME_COMMAND(ns, delete, nsdelete, NSDELETE_USAGE); +NVME_COMMAND(ns, attach, nsattach, NSATTACH_USAGE); +NVME_COMMAND(ns, detach, nsdetach, NSDETACH_USAGE); static void nscreate_usage(void) @@ -466,9 +468,11 @@ nsdetach(int argc, char *argv[]) exit(0); } -void +static void ns(int argc, char *argv[]) { - dispatch(argc, argv, ns_funcs); + DISPATCH(argc, argv, ns); } + +NVME_COMMAND(top, ns, ns, NS_USAGE); Modified: head/sbin/nvmecontrol/nvmecontrol.c ============================================================================== --- head/sbin/nvmecontrol/nvmecontrol.c Sun Dec 2 21:58:36 2018 (r341406) +++ head/sbin/nvmecontrol/nvmecontrol.c Sun Dec 2 23:10:55 2018 (r341407) @@ -47,51 +47,39 @@ __FBSDID("$FreeBSD$"); #include "nvmecontrol.h" +SET_DECLARE(top, struct nvme_function); -static struct nvme_function funcs[] = { - {"devlist", devlist, DEVLIST_USAGE}, - {"identify", identify, IDENTIFY_USAGE}, - {"perftest", perftest, PERFTEST_USAGE}, - {"reset", reset, RESET_USAGE}, - {"logpage", logpage, LOGPAGE_USAGE}, - {"firmware", firmware, FIRMWARE_USAGE}, - {"format", format, FORMAT_USAGE}, - {"power", power, POWER_USAGE}, - {"wdc", wdc, WDC_USAGE}, - {"ns", ns, NS_USAGE}, - {NULL, NULL, NULL}, -}; - -void -gen_usage(struct nvme_function *f) +static void +gen_usage_set(struct nvme_function **f, struct nvme_function **flimit) { fprintf(stderr, "usage:\n"); - while (f->name != NULL) { - fprintf(stderr, "%s", f->usage); + while (f < flimit) { + fprintf(stderr, "%s", (*f)->usage); f++; } exit(1); } void -dispatch(int argc, char *argv[], struct nvme_function *tbl) +dispatch_set(int argc, char *argv[], struct nvme_function **tbl, + struct nvme_function **tbl_limit) { - struct nvme_function *f = tbl; + struct nvme_function **f = tbl; if (argv[1] == NULL) { - gen_usage(tbl); + gen_usage_set(tbl, tbl_limit); return; } - while (f->name != NULL) { - if (strcmp(argv[1], f->name) == 0) - f->fn(argc-1, &argv[1]); + while (f < tbl_limit) { + if (strcmp(argv[1], (*f)->name) == 0) + (*f)->fn(argc-1, &argv[1]); f++; } fprintf(stderr, "Unknown command: %s\n", argv[1]); - gen_usage(tbl); + gen_usage_set(tbl, tbl_limit); } static void @@ -243,9 +231,9 @@ main(int argc, char *argv[]) { if (argc < 2) - gen_usage(funcs); + gen_usage_set(SET_BEGIN(top), SET_LIMIT(top)); - dispatch(argc, argv, funcs); + DISPATCH(argc, argv, top); return (0); } Modified: head/sbin/nvmecontrol/nvmecontrol.h ============================================================================== --- head/sbin/nvmecontrol/nvmecontrol.h Sun Dec 2 21:58:36 2018 (r341406) +++ head/sbin/nvmecontrol/nvmecontrol.h Sun Dec 2 23:10:55 2018 (r341407) @@ -31,6 +31,7 @@ #ifndef __NVMECONTROL_H__ #define __NVMECONTROL_H__ +#include <sys/linker_set.h> #include <dev/nvme/nvme.h> typedef void (*nvme_fn_t)(int argc, char *argv[]); @@ -41,53 +42,15 @@ struct nvme_function { const char *usage; }; +#define NVME_CMDSET(set, sym) DATA_SET(set, sym) +#define NVME_COMMAND(set, nam, function, usage_str) \ + static struct nvme_function function ## _nvme_cmd = \ + { .name = #nam, .fn = function, .usage = usage_str }; \ + NVME_CMDSET(set, function ## _nvme_cmd) + #define NVME_CTRLR_PREFIX "nvme" #define NVME_NS_PREFIX "ns" -#define DEVLIST_USAGE \ -" nvmecontrol devlist\n" - -#define IDENTIFY_USAGE \ -" nvmecontrol identify [-x [-v]] <controller id|namespace id>\n" - -#define PERFTEST_USAGE \ -" nvmecontrol perftest <-n num_threads> <-o read|write>\n" \ -" <-s size_in_bytes> <-t time_in_seconds>\n" \ -" <-i intr|wait> [-f refthread] [-p]\n" \ -" <namespace id>\n" - -#define RESET_USAGE \ -" nvmecontrol reset <controller id>\n" - -#define LOGPAGE_USAGE \ -" nvmecontrol logpage <-p page_id> [-b] [-v vendor] [-x] <controller id|namespace id>\n" \ - -#define FIRMWARE_USAGE \ -" nvmecontrol firmware [-s slot] [-f path_to_firmware] [-a] <controller id>\n" - -#define FORMAT_USAGE \ -" nvmecontrol format [-f fmt] [-m mset] [-p pi] [-l pil] [-E] [-C] <controller id|namespace id>\n" - -#define POWER_USAGE \ -" nvmecontrol power [-l] [-p new-state [-w workload-hint]] <controller id>\n" - -#define WDC_USAGE \ -" nvmecontrol wdc (cap-diag|drive-log|get-crash-dump|purge|purge-montior)\n" - -#define NS_USAGE \ -" nvmecontrol ns (create|delete|attach|detach)\n" - -void devlist(int argc, char *argv[]); -void identify(int argc, char *argv[]); -void perftest(int argc, char *argv[]); -void reset(int argc, char *argv[]); -void logpage(int argc, char *argv[]); -void firmware(int argc, char *argv[]); -void format(int argc, char *argv[]); -void power(int argc, char *argv[]); -void wdc(int argc, char *argv[]); -void ns(int argc, char *argv[]); - int open_dev(const char *str, int *fd, int show_error, int exit_on_error); void parse_ns_str(const char *ns_str, char *ctrlr_str, uint32_t *nsid); void read_controller_data(int fd, struct nvme_controller_data *cdata); @@ -95,8 +58,12 @@ void read_namespace_data(int fd, uint32_t nsid, struct void print_hex(void *data, uint32_t length); void read_logpage(int fd, uint8_t log_page, uint32_t nsid, void *payload, uint32_t payload_size); -void gen_usage(struct nvme_function *); -void dispatch(int argc, char *argv[], struct nvme_function *f); + +void dispatch_set(int argc, char *argv[], struct nvme_function **tbl, + struct nvme_function **tbl_limit); + +#define DISPATCH(argc, argv, set) \ + dispatch_set(argc, argv, SET_BEGIN(set), SET_LIMIT(set)) /* Utility Routines */ /* Modified: head/sbin/nvmecontrol/perftest.c ============================================================================== --- head/sbin/nvmecontrol/perftest.c Sun Dec 2 21:58:36 2018 (r341406) +++ head/sbin/nvmecontrol/perftest.c Sun Dec 2 23:10:55 2018 (r341407) @@ -45,6 +45,12 @@ __FBSDID("$FreeBSD$"); #include "nvmecontrol.h" +#define PERFTEST_USAGE \ +" nvmecontrol perftest <-n num_threads> <-o read|write>\n" \ +" <-s size_in_bytes> <-t time_in_seconds>\n" \ +" <-i intr|wait> [-f refthread] [-p]\n" \ +" <namespace id>\n" + static void print_perftest(struct nvme_io_test *io_test, bool perthread) { @@ -76,7 +82,7 @@ perftest_usage(void) exit(1); } -void +static void perftest(int argc, char *argv[]) { struct nvme_io_test io_test; @@ -176,3 +182,5 @@ perftest(int argc, char *argv[]) print_perftest(&io_test, perthread); exit(0); } + +NVME_COMMAND(top, perftest, perftest, PERFTEST_USAGE); Modified: head/sbin/nvmecontrol/power.c ============================================================================== --- head/sbin/nvmecontrol/power.c Sun Dec 2 21:58:36 2018 (r341406) +++ head/sbin/nvmecontrol/power.c Sun Dec 2 23:10:55 2018 (r341407) @@ -44,6 +44,9 @@ __FBSDID("$FreeBSD$"); _Static_assert(sizeof(struct nvme_power_state) == 256 / NBBY, "nvme_power_state size wrong"); +#define POWER_USAGE \ +" nvmecontrol power [-l] [-p new-state [-w workload-hint]] <controller id>\n" + static void power_usage(void) { @@ -133,7 +136,7 @@ power_show(int fd) printf("Current Power Mode is %d\n", pt.cpl.cdw0); } -void +static void power(int argc, char *argv[]) { struct nvme_controller_data cdata; @@ -193,3 +196,5 @@ out: close(fd); exit(0); } + +NVME_COMMAND(top, power, power, POWER_USAGE); Modified: head/sbin/nvmecontrol/reset.c ============================================================================== --- head/sbin/nvmecontrol/reset.c Sun Dec 2 21:58:36 2018 (r341406) +++ head/sbin/nvmecontrol/reset.c Sun Dec 2 23:10:55 2018 (r341407) @@ -41,6 +41,9 @@ __FBSDID("$FreeBSD$"); #include "nvmecontrol.h" +#define RESET_USAGE \ +" nvmecontrol reset <controller id>\n" + static void reset_usage(void) { @@ -49,7 +52,7 @@ reset_usage(void) exit(1); } -void +static void reset(int argc, char *argv[]) { int ch, fd; @@ -71,3 +74,5 @@ reset(int argc, char *argv[]) exit(0); } + +NVME_COMMAND(top, reset, reset, RESET_USAGE); Modified: head/sbin/nvmecontrol/wdc.c ============================================================================== --- head/sbin/nvmecontrol/wdc.c Sun Dec 2 21:58:36 2018 (r341406) +++ head/sbin/nvmecontrol/wdc.c Sun Dec 2 23:10:55 2018 (r341407) @@ -42,6 +42,11 @@ __FBSDID("$FreeBSD$"); #include "nvmecontrol.h" +#define WDC_USAGE \ +" nvmecontrol wdc (cap-diag|drive-log|get-crash-dump|purge|purge-montior)\n" + +SET_DECLARE(wdc, struct nvme_function); + #define WDC_NVME_TOC_SIZE 8 #define WDC_NVME_CAP_DIAG_OPCODE 0xe6 @@ -51,10 +56,7 @@ static void wdc_cap_diag(int argc, char *argv[]); #define WDC_CAP_DIAG_USAGE "\tnvmecontrol wdc cap-diag [-o path-template]\n" -static struct nvme_function wdc_funcs[] = { - {"cap-diag", wdc_cap_diag, WDC_CAP_DIAG_USAGE}, - {NULL, NULL, NULL}, -}; +NVME_COMMAND(wdc, cap-diag, wdc_cap_diag, WDC_CAP_DIAG_USAGE); static void wdc_append_serial_name(int fd, char *buf, size_t len, const char *suffix) @@ -188,9 +190,11 @@ wdc_cap_diag(int argc, char *argv[]) exit(1); } -void +static void wdc(int argc, char *argv[]) { - dispatch(argc, argv, wdc_funcs); + DISPATCH(argc, argv, wdc); } + +NVME_COMMAND(top, wdc, wdc, WDC_USAGE);
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201812022310.wB2NAuHc034019>