aboutsummaryrefslogtreecommitdiff
path: root/sbin/nvmecontrol/connect.c
blob: afb78725a3c7317f4d83ed35b57dae648c404765 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*-
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2023-2024 Chelsio Communications, Inc.
 * Written by: John Baldwin <jhb@FreeBSD.org>
 */

#include <sys/socket.h>
#include <err.h>
#include <libnvmf.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>

#include "comnd.h"
#include "fabrics.h"

/*
 * Settings that are currently hardcoded but could be exposed to the
 * user via additional command line options:
 *
 * - ADMIN queue entries
 * - MaxR2T
 */

static struct options {
	const char	*transport;
	const char	*address;
	const char	*cntlid;
	const char	*subnqn;
	const char	*hostnqn;
	uint32_t	kato;
	uint16_t	num_io_queues;
	uint16_t	queue_size;
	bool		data_digests;
	bool		flow_control;
	bool		header_digests;
} opt = {
	.transport = "tcp",
	.address = NULL,
	.cntlid = "dynamic",
	.subnqn = NULL,
	.hostnqn = NULL,
	.kato = NVMF_KATO_DEFAULT / 1000,
	.num_io_queues = 1,
	.queue_size = 0,
	.data_digests = false,
	.flow_control = false,
	.header_digests = false,
};

static void
tcp_association_params(struct nvmf_association_params *params)
{
	params->tcp.pda = 0;
	params->tcp.header_digests = opt.header_digests;
	params->tcp.data_digests = opt.data_digests;
	/* XXX */
	params->tcp.maxr2t = 1;
}

static int
connect_nvm_controller(enum nvmf_trtype trtype, int adrfam, const char *address,
    const char *port, uint16_t cntlid, const char *subnqn)
{
	struct nvme_controller_data cdata;
	struct nvmf_association_params aparams;
	struct nvmf_qpair *admin, **io;
	int error;

	memset(&aparams, 0, sizeof(aparams));
	aparams.sq_flow_control = opt.flow_control;
	switch (trtype) {
	case NVMF_TRTYPE_TCP:
		tcp_association_params(&aparams);
		break;
	default:
		warnx("Unsupported transport %s", nvmf_transport_type(trtype));
		return (EX_UNAVAILABLE);
	}

	io = calloc(opt.num_io_queues, sizeof(*io));
	error = connect_nvm_queues(&aparams, trtype, adrfam, address, port,
	    cntlid, subnqn, opt.hostnqn, opt.kato, &admin, io,
	    opt.num_io_queues, opt.queue_size, &cdata);
	if (error != 0)
		return (error);

	error = nvmf_handoff_host(admin, opt.num_io_queues, io, &cdata);
	if (error != 0) {
		warnc(error, "Failed to handoff queues to kernel");
		return (EX_IOERR);
	}
	free(io);
	return (0);
}

static void
connect_discovery_entry(struct nvme_discovery_log_entry *entry)
{
	int adrfam;

	switch (entry->trtype) {
	case NVMF_TRTYPE_TCP:
		switch (entry->adrfam) {
		case NVMF_ADRFAM_IPV4:
			adrfam = AF_INET;
			break;
		case NVMF_ADRFAM_IPV6:
			adrfam = AF_INET6;
			break;
		default:
			warnx("Skipping unsupported address family for %s",
			    entry->subnqn);
			return;
		}
		switch (entry->tsas.tcp.sectype) {
		case NVME_TCP_SECURITY_NONE:
			break;
		default:
			warnx("Skipping unsupported TCP security type for %s",
			    entry->subnqn);
			return;
		}
		break;
	default:
		warnx("Skipping unsupported transport %s for %s",
		    nvmf_transport_type(entry->trtype), entry->subnqn);
		return;
	}

	/*
	 * XXX: Track portids and avoid duplicate connections for a
	 * given (subnqn,portid)?
	 */

	/* XXX: Should this make use of entry->aqsz in some way? */
	connect_nvm_controller(entry->trtype, adrfam, entry->traddr,
	    entry->trsvcid, entry->cntlid, entry->subnqn);
}

static void
connect_discovery_log_page(struct nvmf_qpair *qp)
{
	struct nvme_discovery_log *log;
	int error;

	error = nvmf_host_fetch_discovery_log_page(qp, &log);
	if (error != 0)
		errc(EX_IOERR, error, "Failed to fetch discovery log page");

	for (u_int i = 0; i < log->numrec; i++)
		connect_discovery_entry(&log->entries[i]);
	free(log);
}

static void
discover_controllers(enum nvmf_trtype trtype, const char *address,
    const char *port)
{
	struct nvmf_qpair *qp;

	qp = connect_discovery_adminq(trtype, address, port, opt.hostnqn);

	connect_discovery_log_page(qp);

	nvmf_free_qpair(qp);
}

static void
connect_fn(const struct cmd *f, int argc, char *argv[])
{
	enum nvmf_trtype trtype;
	const char *address, *port;
	char *tofree;
	u_long cntlid;
	int error;

	if (arg_parse(argc, argv, f))
		return;

	if (opt.num_io_queues <= 0)
		errx(EX_USAGE, "Invalid number of I/O queues");

	if (strcasecmp(opt.transport, "tcp") == 0) {
		trtype = NVMF_TRTYPE_TCP;
	} else
		errx(EX_USAGE, "Unsupported or invalid transport");

	nvmf_parse_address(opt.address, &address, &port, &tofree);
	if (port == NULL)
		errx(EX_USAGE, "Explicit port required");

	cntlid = nvmf_parse_cntlid(opt.cntlid);

	error = connect_nvm_controller(trtype, AF_UNSPEC, address, port, cntlid,
	    opt.subnqn);
	if (error != 0)
		exit(error);

	free(tofree);
}

static void
connect_all_fn(const struct cmd *f, int argc, char *argv[])
{
	enum nvmf_trtype trtype;
	const char *address, *port;
	char *tofree;

	if (arg_parse(argc, argv, f))
		return;

	if (opt.num_io_queues <= 0)
		errx(EX_USAGE, "Invalid number of I/O queues");

	if (strcasecmp(opt.transport, "tcp") == 0) {
		trtype = NVMF_TRTYPE_TCP;
	} else
		errx(EX_USAGE, "Unsupported or invalid transport");

	nvmf_parse_address(opt.address, &address, &port, &tofree);
	discover_controllers(trtype, address, port);

	free(tofree);
}

static const struct opts connect_opts[] = {
#define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc }
	OPT("transport", 't', arg_string, opt, transport,
	    "Transport type"),
	OPT("cntlid", 'c', arg_string, opt, cntlid,
	    "Controller ID"),
	OPT("nr-io-queues", 'i', arg_uint16, opt, num_io_queues,
	    "Number of I/O queues"),
	OPT("queue-size", 'Q', arg_uint16, opt, queue_size,
	    "Number of entries in each I/O queue"),
	OPT("keep-alive-tmo", 'k', arg_uint32, opt, kato,
	    "Keep Alive timeout (in seconds)"),
	OPT("hostnqn", 'q', arg_string, opt, hostnqn,
	    "Host NQN"),
	OPT("flow_control", 'F', arg_none, opt, flow_control,
	    "Request SQ flow control"),
	OPT("hdr_digests", 'g', arg_none, opt, header_digests,
	    "Enable TCP PDU header digests"),
	OPT("data_digests", 'G', arg_none, opt, data_digests,
	    "Enable TCP PDU data digests"),
	{ NULL, 0, arg_none, NULL, NULL }
};
#undef OPT

static const struct args connect_args[] = {
	{ arg_string, &opt.address, "address" },
	{ arg_string, &opt.subnqn, "SubNQN" },
	{ arg_none, NULL, NULL },
};

static const struct args connect_all_args[] = {
	{ arg_string, &opt.address, "address" },
	{ arg_none, NULL, NULL },
};

static struct cmd connect_cmd = {
	.name = "connect",
	.fn = connect_fn,
	.descr = "Connect to a fabrics controller",
	.ctx_size = sizeof(opt),
	.opts = connect_opts,
	.args = connect_args,
};

static struct cmd connect_all_cmd = {
	.name = "connect-all",
	.fn = connect_all_fn,
	.descr = "Discover and connect to fabrics controllers",
	.ctx_size = sizeof(opt),
	.opts = connect_opts,
	.args = connect_all_args,
};

CMD_COMMAND(connect_cmd);
CMD_COMMAND(connect_all_cmd);