aboutsummaryrefslogtreecommitdiff
path: root/sbin/nvmecontrol/reconnect.c
blob: c8a010c038d0e52bd93c84c22d75fc506d6af348 (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
/*-
 * 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 "nvmecontrol.h"
#include "fabrics.h"

/*
 * See comment about other possible settings in connect.c.
 */

static struct options {
	const char	*dev;
	const char	*transport;
	const char	*address;
	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 = {
	.dev = NULL,
	.transport = "tcp",
	.address = 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
reconnect_nvm_controller(int fd, enum nvmf_trtype trtype, int adrfam,
    const char *address, const char *port)
{
	struct nvme_controller_data cdata;
	struct nvmf_association_params aparams;
	struct nvmf_reconnect_params rparams;
	struct nvmf_qpair *admin, **io;
	int error;

	error = nvmf_reconnect_params(fd, &rparams);
	if (error != 0) {
		warnc(error, "Failed to fetch reconnect parameters");
		return (EX_IOERR);
	}

	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,
	    rparams.cntlid, rparams.subnqn, opt.hostnqn, opt.kato, &admin, io,
	    opt.num_io_queues, opt.queue_size, &cdata);
	if (error != 0)
		return (error);

	error = nvmf_reconnect_host(fd, 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
reconnect_fn(const struct cmd *f, int argc, char *argv[])
{
	enum nvmf_trtype trtype;
	const char *address, *port;
	char *tofree;
	int error, fd;

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

	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);

	open_dev(opt.dev, &fd, 1, 1);
	if (port == NULL)
		errx(EX_USAGE, "Explicit port required");

	error = reconnect_nvm_controller(fd, trtype, AF_UNSPEC, address, port);
	if (error != 0)
		exit(error);

	close(fd);
	free(tofree);
}

static const struct opts reconnect_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("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 reconnect_args[] = {
	{ arg_string, &opt.dev, "controller-id" },
	{ arg_string, &opt.address, "address" },
	{ arg_none, NULL, NULL },
};

static struct cmd reconnect_cmd = {
	.name = "reconnect",
	.fn = reconnect_fn,
	.descr = "Reconnect to a fabrics controller",
	.ctx_size = sizeof(opt),
	.opts = reconnect_opts,
	.args = reconnect_args,
};

CMD_COMMAND(reconnect_cmd);