xref: /freebsd/sbin/nvmecontrol/disconnect.c (revision abd87254)
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 <err.h>
9 #include <libnvmf.h>
10 #include <stdlib.h>
11 #include <sysexits.h>
12 #include <unistd.h>
13 
14 #include "nvmecontrol.h"
15 
16 static struct options {
17 	const char *dev;
18 } opt = {
19 	.dev = NULL
20 };
21 
22 static const struct args args[] = {
23 	{ arg_string, &opt.dev, "controller-id|namespace-id|SubNQN" },
24 	{ arg_none, NULL, NULL },
25 };
26 
27 static void
28 disconnect(const struct cmd *f, int argc, char *argv[])
29 {
30 	int	error, fd;
31 	char	*path;
32 
33 	if (arg_parse(argc, argv, f))
34 		return;
35 	if (nvmf_nqn_valid(opt.dev)) {
36 		error = nvmf_disconnect_host(opt.dev);
37 		if (error != 0)
38 			errc(EX_IOERR, error, "failed to disconnect from %s",
39 			    opt.dev);
40 	} else {
41 		open_dev(opt.dev, &fd, 1, 1);
42 		get_nsid(fd, &path, NULL);
43 		close(fd);
44 
45 		error = nvmf_disconnect_host(path);
46 		if (error != 0)
47 			errc(EX_IOERR, error, "failed to disconnect from %s",
48 			    path);
49 	}
50 
51 	exit(0);
52 }
53 
54 static void
55 disconnect_all(const struct cmd *f __unused, int argc __unused,
56     char *argv[] __unused)
57 {
58 	int	error;
59 
60 	error = nvmf_disconnect_all();
61 	if (error != 0)
62 		errc(EX_IOERR, error,
63 		    "failed to disconnect from remote controllers");
64 
65 	exit(0);
66 }
67 
68 static struct cmd disconnect_cmd = {
69 	.name = "disconnect",
70 	.fn = disconnect,
71 	.descr = "Disconnect from a fabrics controller",
72 	.args = args,
73 };
74 
75 static struct cmd disconnect_all_cmd = {
76 	.name = "disconnect-all",
77 	.fn = disconnect_all,
78 	.descr = "Disconnect from all fabrics controllers",
79 };
80 
81 CMD_COMMAND(disconnect_cmd);
82 CMD_COMMAND(disconnect_all_cmd);
83