Debian Codegraph @

examples/telemetry-listen.c

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /**
3 * This file is part of libnvme.
4 * Copyright (c) 2020 Western Digital Corporation or its affiliates.
5 *
6 * Authors: Keith Busch <[email protected]>
7 */
8
9 /**
10 * Open all nvme controller's uevent and listen for changes. If NVME_AEN event
11 * is observed with controller telemetry data, read the log and save it to a
12 * file in /var/log/ with the device's unique name and epoch timestamp.
13 */
14 #include <fcntl.h>
15 #include <stdbool.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include <time.h>
21 #include <libnvme.h>
22 #include <sys/stat.h>
23
24 #include <ccan/endian/endian.h>
25
26 struct events {
27 nvme_ctrl_t c;
28 int uevent_fd;
29 };
30
31 static int open_uevent(nvme_ctrl_t c)
32 {
33 char buf[0x1000];
34 if (snprintf(buf, sizeof(buf), "%s/uevent", nvme_ctrl_get_sysfs_dir(c)) < 0)
35 return -1;
36 return open(buf, O_RDONLY);
37 }
38
39 static void save_telemetry(nvme_ctrl_t c)
40 {
41 char buf[0x1000];
42 size_t log_size;
43 int ret, fd;
44 struct nvme_telemetry_log *log;
45 time_t s;
46
47 /* Clear the log (rae == false) at the end to see new telemetry events later */
48 ret = nvme_get_ctrl_telemetry(nvme_ctrl_get_fd(c), false, &log, NVME_TELEMETRY_DA_3, &log_size);
49 if (ret)
50 return;
51
52 s = time(NULL);
53 ret = snprintf(buf, sizeof(buf), "/var/log/%s-telemetry-%llu",
54 nvme_ctrl_get_subsysnqn(c), (unsigned long long)s);
55 if (ret < 0) {
56 free(log);
57 return;
58 }
59
60 fd = open(buf, O_CREAT|O_WRONLY, S_IRUSR|S_IRGRP);
61 if (fd < 0) {
62 free(log);
63 return;
64 }
65
66 ret = write(fd, log, log_size);
67 if (ret < 0)
68 printf("failed to write telemetry log\n");
69 else
70 printf("telemetry log save as %s, wrote:%d size:%zd\n", buf,
71 ret, log_size);
72 close(fd);
73 free(log);
74 }
75
76 static void check_telemetry(nvme_ctrl_t c, int ufd)
77 {
78 char buf[0x1000] = { 0 };
79 char *p, *ptr;
80 int len;
81
82 len = read(ufd, buf, sizeof(buf) - 1);
83 if (len < 0)
84 return;
85
86 ptr = buf;
87 while ((p = strsep(&ptr, "\n")) != NULL) {
88 __u32 aen, type, info, lid;
89
90 if (sscanf(p, "NVME_AEN=0x%08x", &aen) != 1)
91 continue;
92
93 type = aen & 0x07;
94 info = (aen >> 8) & 0xff;
95 lid = (aen >> 16) & 0xff;
96
97 printf("%s: aen type:%x info:%x lid:%d\n",
98 nvme_ctrl_get_name(c), type, info, lid);
99 if (type == NVME_AER_NOTICE &&
100 info == NVME_AER_NOTICE_TELEMETRY)
101 save_telemetry(c);
102 }
103 }
104
105 static void wait_events(fd_set *fds, struct events *e, int nr)
106 {
107 int ret, i;
108
109 for (i = 0; i < nr; i++)
110 check_telemetry(e[i].c, e[i].uevent_fd);
111
112 while (1) {
113 ret = select(nr, fds, NULL, NULL, NULL);
114 if (ret < 0)
115 return;
116
117 for (i = 0; i < nr; i++) {
118 if (!FD_ISSET(e[i].uevent_fd, fds))
119 continue;
120 check_telemetry(e[i].c, e[i].uevent_fd);
121 }
122 }
123 }
124
125 int main()
126 {
127 struct events *e;
128 fd_set fds;
129 int i = 0;
130
131 nvme_subsystem_t s;
132 nvme_ctrl_t c;
133 nvme_host_t h;
134 nvme_root_t r;
135
136 r = nvme_scan(NULL);
137 if (!r)
138 return EXIT_FAILURE;
139
140 nvme_for_each_host(r, h)
141 nvme_for_each_subsystem(h, s)
142 nvme_subsystem_for_each_ctrl(s, c)
143 i++;
144
145 e = calloc(i, sizeof(struct events));
146 FD_ZERO(&fds);
147 i = 0;
148
149 nvme_for_each_host(r, h) {
150 nvme_for_each_subsystem(h, s) {
151 nvme_subsystem_for_each_ctrl(s, c) {
152 int fd = open_uevent(c);
153
154 if (fd < 0)
155 continue;
156 FD_SET(fd, &fds);
157 e[i].uevent_fd = fd;
158 e[i].c = c;
159 i++;
160 }
161 }
162 }
163
164 wait_events(&fds, e, i);
165 nvme_free_tree(r);
166 free(e);
167
168 return EXIT_SUCCESS;
169 }