xref: /illumos-gate/usr/src/test/nvme-tests/tests/libnvme/libnvme_test_common.c (revision 533affcbc7fc4d0c8132976ea454aaa715fe2307)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2024 Oxide Computer Company
14  */
15 
16 /*
17  * Common utilities for libnvme tests.
18  */
19 
20 #include <err.h>
21 #include <stdlib.h>
22 
23 #include "libnvme_test_common.h"
24 
25 /*
26  * For any test linked against libumem, ensure that umem debugging is enabled by
27  * default. Many tests use umem_setmtbf() and we need to make sure there is no
28  * per-thread cache.
29  */
30 const char *
_umem_debug_init(void)31 _umem_debug_init(void)
32 {
33 	return ("default,verbose");
34 }
35 
36 const char *
_umem_logging_init(void)37 _umem_logging_init(void)
38 {
39 	return ("fail,contents");
40 }
41 
42 static void
libnvme_test_hdl_vwarn(nvme_t * nvme,const char * fmt,va_list ap)43 libnvme_test_hdl_vwarn(nvme_t *nvme, const char *fmt, va_list ap)
44 {
45 	(void) fprintf(stderr, "TEST FAILED: ");
46 	(void) vfprintf(stderr, fmt, ap);
47 	(void) fprintf(stderr, ": %s: %s (libnvme: 0x%x, sys: %d)\n",
48 	    nvme_errmsg(nvme), nvme_errtostr(nvme, nvme_err(nvme)),
49 	    nvme_err(nvme), nvme_syserr(nvme));
50 }
51 
52 static void
libnvme_test_ctrl_vwarn(nvme_ctrl_t * ctrl,const char * fmt,va_list ap)53 libnvme_test_ctrl_vwarn(nvme_ctrl_t *ctrl, const char *fmt, va_list ap)
54 {
55 	(void) fprintf(stderr, "TEST FAILED: ");
56 	(void) vfprintf(stderr, fmt, ap);
57 	(void) fprintf(stderr, ": %s: %s (libnvme: 0x%x, sys: %d)\n",
58 	    nvme_ctrl_errmsg(ctrl), nvme_ctrl_errtostr(ctrl,
59 	    nvme_ctrl_err(ctrl)), nvme_ctrl_err(ctrl), nvme_ctrl_syserr(ctrl));
60 }
61 
62 static void
libnvme_test_ctrl_info_vwarn(nvme_ctrl_info_t * info,const char * fmt,va_list ap)63 libnvme_test_ctrl_info_vwarn(nvme_ctrl_info_t *info, const char *fmt,
64     va_list ap)
65 {
66 	(void) fprintf(stderr, "TEST FAILED: ");
67 	(void) vfprintf(stderr, fmt, ap);
68 	(void) fprintf(stderr, ": %s: %s (libnvme info: 0x%x, sys: %d)\n",
69 	    nvme_ctrl_info_errmsg(info), nvme_ctrl_info_errtostr(info,
70 	    nvme_ctrl_info_err(info)), nvme_ctrl_info_err(info),
71 	    nvme_ctrl_info_syserr(info));
72 }
73 
74 static void
libnvme_test_ns_info_vwarn(nvme_ns_info_t * info,const char * fmt,va_list ap)75 libnvme_test_ns_info_vwarn(nvme_ns_info_t *info, const char *fmt,
76     va_list ap)
77 {
78 	(void) fprintf(stderr, "TEST FAILED: ");
79 	(void) vfprintf(stderr, fmt, ap);
80 	(void) fprintf(stderr, ": %s: %s (libnvme info: 0x%x, sys: %d)\n",
81 	    nvme_ns_info_errmsg(info), nvme_ns_info_errtostr(info,
82 	    nvme_ns_info_err(info)), nvme_ns_info_err(info),
83 	    nvme_ns_info_syserr(info));
84 }
85 
86 void
libnvme_test_hdl_warn(nvme_t * nvme,const char * fmt,...)87 libnvme_test_hdl_warn(nvme_t *nvme, const char *fmt, ...)
88 {
89 	va_list ap;
90 
91 	va_start(ap, fmt);
92 	libnvme_test_hdl_vwarn(nvme, fmt, ap);
93 	va_end(ap);
94 }
95 
96 void __NORETURN
libnvme_test_hdl_fatal(nvme_t * nvme,const char * fmt,...)97 libnvme_test_hdl_fatal(nvme_t *nvme, const char *fmt, ...)
98 {
99 	va_list ap;
100 
101 	va_start(ap, fmt);
102 	libnvme_test_hdl_vwarn(nvme, fmt, ap);
103 	va_end(ap);
104 
105 	exit(EXIT_FAILURE);
106 }
107 
108 void
libnvme_test_ctrl_warn(nvme_ctrl_t * ctrl,const char * fmt,...)109 libnvme_test_ctrl_warn(nvme_ctrl_t *ctrl, const char *fmt, ...)
110 {
111 	va_list ap;
112 
113 	va_start(ap, fmt);
114 	libnvme_test_ctrl_vwarn(ctrl, fmt, ap);
115 	va_end(ap);
116 }
117 
118 void __NORETURN
libnvme_test_ctrl_fatal(nvme_ctrl_t * ctrl,const char * fmt,...)119 libnvme_test_ctrl_fatal(nvme_ctrl_t *ctrl, const char *fmt, ...)
120 {
121 	va_list ap;
122 
123 	va_start(ap, fmt);
124 	libnvme_test_ctrl_vwarn(ctrl, fmt, ap);
125 	va_end(ap);
126 
127 	exit(EXIT_FAILURE);
128 }
129 
130 void
libnvme_test_ctrl_info_warn(nvme_ctrl_info_t * info,const char * fmt,...)131 libnvme_test_ctrl_info_warn(nvme_ctrl_info_t *info, const char *fmt, ...)
132 {
133 	va_list ap;
134 
135 	va_start(ap, fmt);
136 	libnvme_test_ctrl_info_vwarn(info, fmt, ap);
137 	va_end(ap);
138 }
139 
140 void
libnvme_test_ns_info_warn(nvme_ns_info_t * info,const char * fmt,...)141 libnvme_test_ns_info_warn(nvme_ns_info_t *info, const char *fmt, ...)
142 {
143 	va_list ap;
144 
145 	va_start(ap, fmt);
146 	libnvme_test_ns_info_vwarn(info, fmt, ap);
147 	va_end(ap);
148 }
149 
150 void __NORETURN
libnvme_test_ctrl_info_fatal(nvme_ctrl_info_t * info,const char * fmt,...)151 libnvme_test_ctrl_info_fatal(nvme_ctrl_info_t *info, const char *fmt, ...)
152 {
153 	va_list ap;
154 
155 	va_start(ap, fmt);
156 	libnvme_test_ctrl_info_vwarn(info, fmt, ap);
157 	va_end(ap);
158 
159 	exit(EXIT_FAILURE);
160 }
161 
162 void
libnvme_test_init(nvme_t ** nvmep,nvme_ctrl_t ** ctrlp)163 libnvme_test_init(nvme_t **nvmep, nvme_ctrl_t **ctrlp)
164 {
165 	nvme_t *nvme;
166 	nvme_ctrl_t *ctrl;
167 	const char *dev;
168 
169 	nvme = nvme_init();
170 	if (nvme == NULL) {
171 		err(EXIT_FAILURE, "failed to create libnvme handle");
172 	}
173 
174 	dev = getenv(NVME_TEST_DEV_ENVVAR);
175 	if (dev == NULL) {
176 		errx(EXIT_FAILURE, "cannot run test, missing required NVMe "
177 		    "device, please set the %s environment variable",
178 		    NVME_TEST_DEV_ENVVAR);
179 	}
180 
181 	if (!nvme_ctrl_ns_init(nvme, dev, &ctrl, NULL)) {
182 		libnvme_test_hdl_fatal(nvme, "failed to open %s", dev);
183 	}
184 
185 	*nvmep = nvme;
186 	*ctrlp = ctrl;
187 }
188