xref: /freebsd/sbin/nvmecontrol/connect.c (revision 96190b4fef3b4a0cc3ca0606b0c4e3e69a5e6717)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2023-2024 Chelsio Communications, Inc.
5  * Written by: John Baldwin <jhb@FreeBSD.org>
6  */
7 
8 #include <sys/socket.h>
9 #include <err.h>
10 #include <libnvmf.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <sysexits.h>
14 #include <unistd.h>
15 
16 #include "comnd.h"
17 #include "fabrics.h"
18 
19 /*
20  * Settings that are currently hardcoded but could be exposed to the
21  * user via additional command line options:
22  *
23  * - ADMIN queue entries
24  * - MaxR2T
25  */
26 
27 static struct options {
28 	const char	*transport;
29 	const char	*address;
30 	const char	*cntlid;
31 	const char	*subnqn;
32 	const char	*hostnqn;
33 	uint32_t	kato;
34 	uint16_t	num_io_queues;
35 	uint16_t	queue_size;
36 	bool		data_digests;
37 	bool		flow_control;
38 	bool		header_digests;
39 } opt = {
40 	.transport = "tcp",
41 	.address = NULL,
42 	.cntlid = "dynamic",
43 	.subnqn = NULL,
44 	.hostnqn = NULL,
45 	.kato = NVMF_KATO_DEFAULT / 1000,
46 	.num_io_queues = 1,
47 	.queue_size = 0,
48 	.data_digests = false,
49 	.flow_control = false,
50 	.header_digests = false,
51 };
52 
53 static void
54 tcp_association_params(struct nvmf_association_params *params)
55 {
56 	params->tcp.pda = 0;
57 	params->tcp.header_digests = opt.header_digests;
58 	params->tcp.data_digests = opt.data_digests;
59 	/* XXX */
60 	params->tcp.maxr2t = 1;
61 }
62 
63 static int
64 connect_nvm_controller(enum nvmf_trtype trtype, int adrfam, const char *address,
65     const char *port, uint16_t cntlid, const char *subnqn)
66 {
67 	struct nvme_controller_data cdata;
68 	struct nvmf_association_params aparams;
69 	struct nvmf_qpair *admin, **io;
70 	int error;
71 
72 	memset(&aparams, 0, sizeof(aparams));
73 	aparams.sq_flow_control = opt.flow_control;
74 	switch (trtype) {
75 	case NVMF_TRTYPE_TCP:
76 		tcp_association_params(&aparams);
77 		break;
78 	default:
79 		warnx("Unsupported transport %s", nvmf_transport_type(trtype));
80 		return (EX_UNAVAILABLE);
81 	}
82 
83 	io = calloc(opt.num_io_queues, sizeof(*io));
84 	error = connect_nvm_queues(&aparams, trtype, adrfam, address, port,
85 	    cntlid, subnqn, opt.hostnqn, opt.kato * 1000, &admin, io,
86 	    opt.num_io_queues, opt.queue_size, &cdata);
87 	if (error != 0) {
88 		free(io);
89 		return (error);
90 	}
91 
92 	error = nvmf_handoff_host(admin, opt.num_io_queues, io, &cdata);
93 	if (error != 0) {
94 		warnc(error, "Failed to handoff queues to kernel");
95 		free(io);
96 		return (EX_IOERR);
97 	}
98 	free(io);
99 	return (0);
100 }
101 
102 static void
103 connect_discovery_entry(struct nvme_discovery_log_entry *entry)
104 {
105 	int adrfam;
106 
107 	switch (entry->trtype) {
108 	case NVMF_TRTYPE_TCP:
109 		switch (entry->adrfam) {
110 		case NVMF_ADRFAM_IPV4:
111 			adrfam = AF_INET;
112 			break;
113 		case NVMF_ADRFAM_IPV6:
114 			adrfam = AF_INET6;
115 			break;
116 		default:
117 			warnx("Skipping unsupported address family for %s",
118 			    entry->subnqn);
119 			return;
120 		}
121 		switch (entry->tsas.tcp.sectype) {
122 		case NVME_TCP_SECURITY_NONE:
123 			break;
124 		default:
125 			warnx("Skipping unsupported TCP security type for %s",
126 			    entry->subnqn);
127 			return;
128 		}
129 		break;
130 	default:
131 		warnx("Skipping unsupported transport %s for %s",
132 		    nvmf_transport_type(entry->trtype), entry->subnqn);
133 		return;
134 	}
135 
136 	/*
137 	 * XXX: Track portids and avoid duplicate connections for a
138 	 * given (subnqn,portid)?
139 	 */
140 
141 	/* XXX: Should this make use of entry->aqsz in some way? */
142 	connect_nvm_controller(entry->trtype, adrfam, entry->traddr,
143 	    entry->trsvcid, entry->cntlid, entry->subnqn);
144 }
145 
146 static void
147 connect_discovery_log_page(struct nvmf_qpair *qp)
148 {
149 	struct nvme_discovery_log *log;
150 	int error;
151 
152 	error = nvmf_host_fetch_discovery_log_page(qp, &log);
153 	if (error != 0)
154 		errc(EX_IOERR, error, "Failed to fetch discovery log page");
155 
156 	for (u_int i = 0; i < log->numrec; i++)
157 		connect_discovery_entry(&log->entries[i]);
158 	free(log);
159 }
160 
161 static void
162 discover_controllers(enum nvmf_trtype trtype, const char *address,
163     const char *port)
164 {
165 	struct nvmf_qpair *qp;
166 
167 	qp = connect_discovery_adminq(trtype, address, port, opt.hostnqn);
168 
169 	connect_discovery_log_page(qp);
170 
171 	nvmf_free_qpair(qp);
172 }
173 
174 static void
175 connect_fn(const struct cmd *f, int argc, char *argv[])
176 {
177 	enum nvmf_trtype trtype;
178 	const char *address, *port;
179 	char *tofree;
180 	u_long cntlid;
181 	int error;
182 
183 	if (arg_parse(argc, argv, f))
184 		return;
185 
186 	if (opt.num_io_queues <= 0)
187 		errx(EX_USAGE, "Invalid number of I/O queues");
188 
189 	if (strcasecmp(opt.transport, "tcp") == 0) {
190 		trtype = NVMF_TRTYPE_TCP;
191 	} else
192 		errx(EX_USAGE, "Unsupported or invalid transport");
193 
194 	nvmf_parse_address(opt.address, &address, &port, &tofree);
195 	if (port == NULL)
196 		errx(EX_USAGE, "Explicit port required");
197 
198 	cntlid = nvmf_parse_cntlid(opt.cntlid);
199 
200 	error = connect_nvm_controller(trtype, AF_UNSPEC, address, port, cntlid,
201 	    opt.subnqn);
202 	if (error != 0)
203 		exit(error);
204 
205 	free(tofree);
206 }
207 
208 static void
209 connect_all_fn(const struct cmd *f, int argc, char *argv[])
210 {
211 	enum nvmf_trtype trtype;
212 	const char *address, *port;
213 	char *tofree;
214 
215 	if (arg_parse(argc, argv, f))
216 		return;
217 
218 	if (opt.num_io_queues <= 0)
219 		errx(EX_USAGE, "Invalid number of I/O queues");
220 
221 	if (strcasecmp(opt.transport, "tcp") == 0) {
222 		trtype = NVMF_TRTYPE_TCP;
223 	} else
224 		errx(EX_USAGE, "Unsupported or invalid transport");
225 
226 	nvmf_parse_address(opt.address, &address, &port, &tofree);
227 	discover_controllers(trtype, address, port);
228 
229 	free(tofree);
230 }
231 
232 static const struct opts connect_opts[] = {
233 #define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc }
234 	OPT("transport", 't', arg_string, opt, transport,
235 	    "Transport type"),
236 	OPT("cntlid", 'c', arg_string, opt, cntlid,
237 	    "Controller ID"),
238 	OPT("nr-io-queues", 'i', arg_uint16, opt, num_io_queues,
239 	    "Number of I/O queues"),
240 	OPT("queue-size", 'Q', arg_uint16, opt, queue_size,
241 	    "Number of entries in each I/O queue"),
242 	OPT("keep-alive-tmo", 'k', arg_uint32, opt, kato,
243 	    "Keep Alive timeout (in seconds)"),
244 	OPT("hostnqn", 'q', arg_string, opt, hostnqn,
245 	    "Host NQN"),
246 	OPT("flow_control", 'F', arg_none, opt, flow_control,
247 	    "Request SQ flow control"),
248 	OPT("hdr_digests", 'g', arg_none, opt, header_digests,
249 	    "Enable TCP PDU header digests"),
250 	OPT("data_digests", 'G', arg_none, opt, data_digests,
251 	    "Enable TCP PDU data digests"),
252 	{ NULL, 0, arg_none, NULL, NULL }
253 };
254 #undef OPT
255 
256 static const struct args connect_args[] = {
257 	{ arg_string, &opt.address, "address" },
258 	{ arg_string, &opt.subnqn, "SubNQN" },
259 	{ arg_none, NULL, NULL },
260 };
261 
262 static const struct args connect_all_args[] = {
263 	{ arg_string, &opt.address, "address" },
264 	{ arg_none, NULL, NULL },
265 };
266 
267 static struct cmd connect_cmd = {
268 	.name = "connect",
269 	.fn = connect_fn,
270 	.descr = "Connect to a fabrics controller",
271 	.ctx_size = sizeof(opt),
272 	.opts = connect_opts,
273 	.args = connect_args,
274 };
275 
276 static struct cmd connect_all_cmd = {
277 	.name = "connect-all",
278 	.fn = connect_all_fn,
279 	.descr = "Discover and connect to fabrics controllers",
280 	.ctx_size = sizeof(opt),
281 	.opts = connect_opts,
282 	.args = connect_all_args,
283 };
284 
285 CMD_COMMAND(connect_cmd);
286 CMD_COMMAND(connect_all_cmd);
287