xref: /freebsd/sbin/nvmecontrol/telemetry.c (revision 3d966ae7895b40c7a4985f61ff4819c25c8618c9)
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 } opt = {
55 	.outfn = NULL,
56 	.dev = NULL,
57 	.da = 3,
58 };
59 
60 static const struct opts telemetry_log_opts[] = {
61 #define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc }
62 	OPT("output-file", 'O', arg_string, opt, outfn,
63 	    "output file for telemetry data"),
64 	OPT("data-area", 'd', arg_uint8, opt, da,
65 	    "output file for telemetry data"),
66 	{ NULL, 0, arg_none, NULL, NULL }
67 };
68 #undef OPT
69 
70 static const struct args telemetry_log_args[] = {
71 	{ arg_string, &opt.dev, "<controller id|namespace id>" },
72 	{ arg_none, NULL, NULL },
73 };
74 
75 static struct cmd telemetry_log_cmd = {
76 	.name = "telemetry-log",
77 	.fn = telemetry_log,
78 	.descr = "Retrieves telemetry log pages from drive",
79 	.ctx_size = sizeof(opt),
80 	.opts = telemetry_log_opts,
81 	.args = telemetry_log_args,
82 };
83 
84 CMD_COMMAND(telemetry_log_cmd);
85 
86 /* End of tables for command line parsing */
87 
88 /*
89  * Note: Even though this is a logpage, it's variable size and tricky
90  * to get with some weird options, so it's its own command.
91  */
92 
93 static void
telemetry_log(const struct cmd * f,int argc,char * argv[])94 telemetry_log(const struct cmd *f, int argc, char *argv[])
95 {
96 	int				fd, fdout;
97 	char				*path;
98 	uint32_t			nsid;
99 	ssize_t				size;
100 	uint64_t			off;
101 	ssize_t				chunk;
102 	struct nvme_controller_data	cdata;
103 	bool				can_telemetry;
104 	struct nvme_telemetry_log_page  tlp, buf;
105 
106 	if (arg_parse(argc, argv, f))
107 		return;
108 	if (opt.da < 1 || opt.da > 3)
109 		errx(EX_USAGE, "Data area %d is not in the range 1-3\n", opt.da);
110 	if (opt.outfn == NULL)
111 		errx(EX_USAGE, "No output file specified");
112 
113 	open_dev(opt.dev, &fd, 0, 1);
114 	get_nsid(fd, &path, &nsid);
115 	if (nsid == 0) {
116 		nsid = NVME_GLOBAL_NAMESPACE_TAG;
117 	} else {
118 		close(fd);
119 		open_dev(path, &fd, 0, 1);
120 	}
121 	free(path);
122 
123 	if (read_controller_data(fd, &cdata))
124 		errx(EX_IOERR, "Identify request failed");
125 
126 	can_telemetry = NVMEV(NVME_CTRLR_DATA_LPA_TELEMETRY, cdata.lpa);
127 	if (!can_telemetry)
128 		errx(EX_UNAVAILABLE, "Drive does not support telemetry");
129 	if (nsid != NVME_GLOBAL_NAMESPACE_TAG)
130 		errx(EX_UNAVAILABLE, "Cannot operate on namespace");
131 
132 	fdout = open(opt.outfn, O_WRONLY | O_CREAT, 0664);
133 	if (fdout == -1)
134 		err(EX_IOERR, "Can't create %s", opt.outfn);
135 
136 	/* Read the log page */
137 	size = sizeof(tlp);
138 	off = 0;
139 	read_logpage(fd, NVME_LOG_TELEMETRY_HOST_INITIATED, nsid, 0, 0, 0,
140 	    off, 0, 0, 0, &tlp, size);
141 	switch(opt.da) {
142 	case 1:
143 		size = letoh(tlp.da1_last);
144 		break;
145 	case 2:
146 		size = letoh(tlp.da2_last);
147 		break;
148 	case 3:
149 		size = letoh(tlp.da3_last);
150 		break;
151 	default:
152 		errx(EX_USAGE, "Impossible data area %d", opt.da);
153 	}
154 	size = (size + 1) * 512; /* The count of additional pages */
155 	chunk = 4096;
156 
157 	printf("Extracting %llu bytes\n", (unsigned long long)size);
158 	do {
159 		if (chunk > size)
160 			chunk = size;
161 		read_logpage(fd, NVME_LOG_TELEMETRY_HOST_INITIATED, nsid, 0, 0, 0,
162 		    off, 0, 0, 0, &buf, chunk);
163 		if (off == 0) {
164 			/*
165 			 * Sanity check to make sure that the generation number
166 			 * didn't change between the two reads.
167 			 */
168 			if (tlp.hi_gen != buf.hi_gen)
169 				warnx(
170 				    "Generation number changed from %d to %d",
171 				    tlp.hi_gen, buf.hi_gen);
172 		}
173 		if (write(fdout, &buf, chunk) != chunk)
174 			err(EX_IOERR, "Error writing %s", opt.outfn);
175 		off += chunk;
176 		size -= chunk;
177 	} while (size > 0);
178 
179 	close(fdout);
180 	close(fd);
181 	exit(0);
182 }
183