xref: /src/sbin/nvmecontrol/telemetry.c (revision cbb7441245cf2b22efed17dec3c88587c980c691)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (C) 2024 Netflix, Inc
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/param.h>
29 #include <sys/ioccom.h>
30 
31 #include <ctype.h>
32 #include <err.h>
33 #include <fcntl.h>
34 #include <stdbool.h>
35 #include <stddef.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sysexits.h>
40 #include <unistd.h>
41 #include <sys/endian.h>
42 
43 #include "nvmecontrol.h"
44 
45 /* Tables for command line parsing */
46 
47 static cmd_fn_t telemetry_log;
48 
49 #define NONE 0xffffffffu
50 static struct options {
51 	const char *outfn;
52 	const char *dev;
53 	uint8_t da;
54 	bool verbose;
55 } opt = {
56 	.outfn = NULL,
57 	.dev = NULL,
58 	.da = 3,
59 };
60 
61 static const struct opts telemetry_log_opts[] = {
62 #define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc }
63 	OPT("output-file", 'O', arg_string, opt, outfn,
64 	    "output file for telemetry data"),
65 	OPT("data-area", 'd', arg_uint8, opt, da,
66 	    "output file for telemetry data"),
67 	OPT("verbose", 'v', arg_none, opt, verbose,
68 	    "Be verbose about process"),
69 	{ NULL, 0, arg_none, NULL, NULL }
70 };
71 #undef OPT
72 
73 static const struct args telemetry_log_args[] = {
74 	{ arg_string, &opt.dev, "<controller id|namespace id>" },
75 	{ arg_none, NULL, NULL },
76 };
77 
78 static struct cmd telemetry_log_cmd = {
79 	.name = "telemetry-log",
80 	.fn = telemetry_log,
81 	.descr = "Retrieves telemetry log pages from drive",
82 	.ctx_size = sizeof(opt),
83 	.opts = telemetry_log_opts,
84 	.args = telemetry_log_args,
85 };
86 
87 CMD_COMMAND(telemetry_log_cmd);
88 
89 /* End of tables for command line parsing */
90 
91 /*
92  * Note: Even though this is a logpage, it's variable size and tricky
93  * to get with some weird options, so it's its own command.
94  */
95 
96 static void
telemetry_log(const struct cmd * f,int argc,char * argv[])97 telemetry_log(const struct cmd *f, int argc, char *argv[])
98 {
99 	int				fd, fdout;
100 	char				*path;
101 	uint32_t			nsid;
102 	ssize_t				size, blocks;
103 	uint64_t			off;
104 	ssize_t				chunk;
105 	struct nvme_controller_data	cdata;
106 	bool				can_telemetry;
107 	struct nvme_telemetry_log_page  tlp, buf;
108 
109 	if (arg_parse(argc, argv, f))
110 		return;
111 	if (opt.da < 1 || opt.da > 3)
112 		errx(EX_USAGE, "Data area %d is not in the range 1-3\n", opt.da);
113 	if (opt.outfn == NULL)
114 		errx(EX_USAGE, "No output file specified");
115 
116 	open_dev(opt.dev, &fd, 0, 1);
117 	get_nsid(fd, &path, &nsid);
118 	if (nsid == 0) {
119 		nsid = NVME_GLOBAL_NAMESPACE_TAG;
120 	} else {
121 		close(fd);
122 		open_dev(path, &fd, 0, 1);
123 	}
124 	free(path);
125 
126 	if (read_controller_data(fd, &cdata))
127 		errx(EX_IOERR, "Identify request failed");
128 
129 	can_telemetry = NVMEV(NVME_CTRLR_DATA_LPA_TELEMETRY, cdata.lpa);
130 	if (!can_telemetry)
131 		errx(EX_UNAVAILABLE, "Drive does not support telemetry");
132 	if (nsid != NVME_GLOBAL_NAMESPACE_TAG)
133 		errx(EX_UNAVAILABLE, "Cannot operate on namespace");
134 
135 	fdout = open(opt.outfn, O_WRONLY | O_CREAT, 0664);
136 	if (fdout == -1)
137 		err(EX_IOERR, "Can't create %s", opt.outfn);
138 
139 	/* Read the log page */
140 	size = sizeof(tlp);
141 	off = 0;
142 	read_logpage(fd, NVME_LOG_TELEMETRY_HOST_INITIATED, nsid, 0, 0, 1,
143 	    off, 0, 0, 0, &tlp, size);
144 	switch(opt.da) {
145 	case 1:
146 		size = letoh(tlp.da1_last);
147 		break;
148 	case 2:
149 		size = letoh(tlp.da2_last);
150 		break;
151 	case 3:
152 		size = letoh(tlp.da3_last);
153 		break;
154 	default:
155 		errx(EX_USAGE, "Impossible data area %d", opt.da);
156 	}
157 	blocks = size + 1;
158 	size = blocks * 512; /* The count of additional pages */
159 	chunk = 4096;
160 
161 	if (opt.verbose)
162 		printf("Extracting %llu bytes %llu blocks\n", (unsigned long long)size,
163 		    (unsigned long long)size / 512);
164 	else
165 		printf("Extracting %llu bytes\n", (unsigned long long)size);
166 	do {
167 		if (chunk > size)
168 			chunk = size;
169 		if (opt.verbose && off % 10240 == 0) {
170 			printf("%s: %llu / %llu\r", opt.dev, (unsigned long long)off / 512,
171 			    (unsigned long long)blocks);
172 			fflush(stdout);
173 		}
174 		read_logpage(fd, NVME_LOG_TELEMETRY_HOST_INITIATED, nsid, 0, 0, 1,
175 		    off, 0, 0, 0, &buf, chunk);
176 		if (write(fdout, &buf, chunk) != chunk)
177 			err(EX_IOERR, "Error writing %s", opt.outfn);
178 		off += chunk;
179 		size -= chunk;
180 	} while (size > 0);
181 	if (opt.verbose) {
182 		printf("%s: %llu / %llu\n", opt.dev, (unsigned long long)off / 512,
183 		    (unsigned long long)blocks);
184 		fflush(stdout);
185 	}
186 
187 	close(fdout);
188 	close(fd);
189 	exit(0);
190 }
191