Debian Codegraph @

examples/mi-conf.c

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /**
3 * This file is part of libnvme.
4 * Copyright (c) 2022 Code Construct Pty Ltd.
5 *
6 * Authors: Jeremy Kerr <[email protected]>
7 */
8
9 /**
10 * mi-conf: query a device for optimal MTU and set for both the local MCTP
11 * route (through dbus to mctpd) and the device itself (through NVMe-MI
12 * configuration commands)
13 */
14
15 #include <err.h>
16 #include <stdbool.h>
17 #include <stdint.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20
21 #include <libnvme-mi.h>
22
23 #include <ccan/array_size/array_size.h>
24 #include <ccan/endian/endian.h>
25
26 #include <dbus/dbus.h>
27
28 #define MCTP_DBUS_NAME "au.com.codeconstruct.MCTP1"
29 #define MCTP_DBUS_PATH "/au/com/codeconstruct/mctp1"
30 #define MCTP_DBUS_EP_IFACE "au.com.codeconstruct.MCTP.Endpoint1"
31
32 static int parse_mctp(const char *devstr, unsigned int *net, uint8_t *eid)
33 {
34 int rc;
35
36 rc = sscanf(devstr, "mctp:%u,%hhu", net, eid);
37 if (rc != 2)
38 return -1;
39
40 return 0;
41 }
42
43 int find_port(nvme_mi_ep_t ep, uint8_t *portp, uint16_t *mtup)
44 {
45 struct nvme_mi_read_nvm_ss_info ss_info;
46 struct nvme_mi_read_port_info port_info;
47 uint8_t port;
48 bool found;
49 int rc;
50
51 /* query number of ports */
52 rc = nvme_mi_mi_read_mi_data_subsys(ep, &ss_info);
53 if (rc) {
54 warn("Failed reading subsystem info");
55 return -1;
56 }
57
58 found = false;
59 for (port = 0; port <= ss_info.nump; port++) {
60 rc = nvme_mi_mi_read_mi_data_port(ep, port, &port_info);
61 if (rc) {
62 warn("Failed reading port info for port %ud", port);
63 return -1;
64 }
65
66 /* skip non-SMBus ports */
67 if (port_info.portt != 0x2)
68 continue;
69
70 if (found) {
71 warn("Mutliple SMBus ports; skipping duplicate");
72 } else {
73 *portp = port;
74 *mtup = port_info.mmctptus;
75 found = true;
76 }
77 }
78
79 return found ? 0 : 1;
80 }
81
82 int set_local_mtu(DBusConnection *bus, unsigned int net, uint8_t eid,
83 uint32_t mtu)
84 {
85 DBusMessage *msg, *resp;
86 DBusError berr;
87 char *ep_path;
88 int rc;
89
90 rc = asprintf(&ep_path, "%s/networks/%u/endpoints/%hhu", MCTP_DBUS_PATH,
91 net, eid);
92 if (rc < 0) {
93 warn("Failed to create dbus path");
94 return -1;
95 }
96
97 /* The NVMe-MI interfaces refer to their MTU as *not* including the
98 * 4-byte MCTP header, whereas the MCTP specs *do* include it. When
99 * we're setting the route MTU, we're using to the MCTP-style MTU,
100 * which needs the extra four bytes included
101 */
102 mtu += 4;
103
104 rc = -1;
105 dbus_error_init(&berr);
106 msg = dbus_message_new_method_call(MCTP_DBUS_NAME, ep_path,
107 MCTP_DBUS_EP_IFACE, "SetMTU");
108 if (!msg) {
109 warnx("Can't create D-Bus message");
110 goto out;
111 }
112
113 rc = dbus_message_append_args(msg,
114 DBUS_TYPE_UINT32, &mtu,
115 DBUS_TYPE_INVALID);
116 if (!rc) {
117 warnx("Can't construct D-Bus message arguments");
118 goto out_free_msg;
119 }
120
121 resp = dbus_connection_send_with_reply_and_block(bus, msg,
122 2000, &berr);
123 if (!resp) {
124 warnx("Failed to set local MTU: %s (%s)", berr.message,
125 berr.name);
126 } else {
127 dbus_message_unref(resp);
128 rc = 0;
129 }
130
131 out_free_msg:
132 dbus_message_unref(msg);
133 out:
134 dbus_error_free(&berr);
135 return rc;
136 }
137
138 int main(int argc, char **argv)
139 {
140 uint16_t cur_mtu, mtu;
141 DBusConnection *bus;
142 const char *devstr;
143 uint8_t eid, port;
144 nvme_root_t root;
145 unsigned int net;
146 nvme_mi_ep_t ep;
147 DBusError berr;
148 int rc;
149
150 if (argc != 2) {
151 fprintf(stderr, "usage: %s mctp:<net>,<eid>\n", argv[0]);
152 return EXIT_FAILURE;
153 }
154
155 devstr = argv[1];
156 rc = parse_mctp(devstr, &net, &eid);
157 if (rc)
158 errx(EXIT_FAILURE, "can't parse MI device string '%s'", devstr);
159
160 root = nvme_mi_create_root(stderr, DEFAULT_LOGLEVEL);
161 if (!root)
162 err(EXIT_FAILURE, "can't create NVMe root");
163
164 ep = nvme_mi_open_mctp(root, net, eid);
165 if (!ep) {
166 warnx("can't open MCTP endpoint %d:%d", net, eid);
167 goto out_free_root;
168 }
169
170 dbus_error_init(&berr);
171 bus = dbus_bus_get(DBUS_BUS_SYSTEM, &berr);
172 if (!bus) {
173 warnx("Failed opening D-Bus: %s (%s)\n",
174 berr.message, berr.name);
175 rc = -1;
176 goto out_close_ep;
177 }
178
179 rc = find_port(ep, &port, &mtu);
180 if (rc) {
181 warnx("Can't find SMBus port information");
182 goto out_close_bus;
183 }
184
185 rc = nvme_mi_mi_config_get_mctp_mtu(ep, port, &cur_mtu);
186 if (rc) {
187 cur_mtu = 0;
188 warn("Can't query current MTU; no way to revert on failure");
189 }
190
191 if (mtu == cur_mtu) {
192 printf("Current MTU (%d) is already at max\n", cur_mtu);
193 goto out_close_bus;
194 }
195
196 rc = nvme_mi_mi_config_set_mctp_mtu(ep, port, mtu);
197 if (rc) {
198 warn("Can't set MCTP MTU");
199 goto out_close_bus;
200 }
201
202 rc = set_local_mtu(bus, net, eid, mtu);
203 if (rc) {
204 /* revert if we have an old setting */
205 if (cur_mtu) {
206 rc = nvme_mi_mi_config_set_mctp_mtu(ep, port, cur_mtu);
207 if (rc)
208 warn("Failed to restore previous MTU!");
209 rc = -1;
210 }
211 } else {
212 printf("MTU for port %u set to %d (was %d)\n",
213 port, mtu, cur_mtu);
214 }
215
216 out_close_bus:
217 dbus_connection_unref(bus);
218 out_close_ep:
219 dbus_error_free(&berr);
220 nvme_mi_close(ep);
221 out_free_root:
222 nvme_mi_free_root(root);
223
224 return rc ? EXIT_FAILURE : EXIT_SUCCESS;
225 }
226