]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-sriov.c
Merge pull request #17921 from yuwata/network-drop-assertion-17920
[thirdparty/systemd.git] / src / network / networkd-sriov.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later
2 * Copyright © 2020 VMware, Inc. */
3
4 #include "alloc-util.h"
5 #include "netlink-util.h"
6 #include "networkd-manager.h"
7 #include "networkd-sriov.h"
8 #include "parse-util.h"
9 #include "set.h"
10 #include "string-util.h"
11
12 static int sr_iov_new(SRIOV **ret) {
13 SRIOV *sr_iov;
14
15 sr_iov = new(SRIOV, 1);
16 if (!sr_iov)
17 return -ENOMEM;
18
19 *sr_iov = (SRIOV) {
20 .vf = (uint32_t) -1,
21 .vlan_proto = ETH_P_8021Q,
22 .vf_spoof_check_setting = -1,
23 .trust = -1,
24 .query_rss = -1,
25 .link_state = _SR_IOV_LINK_STATE_INVALID,
26 };
27
28 *ret = TAKE_PTR(sr_iov);
29
30 return 0;
31 }
32
33 static int sr_iov_new_static(Network *network, const char *filename, unsigned section_line, SRIOV **ret) {
34 _cleanup_(network_config_section_freep) NetworkConfigSection *n = NULL;
35 _cleanup_(sr_iov_freep) SRIOV *sr_iov = NULL;
36 SRIOV *existing = NULL;
37 int r;
38
39 assert(network);
40 assert(ret);
41 assert(filename);
42 assert(section_line > 0);
43
44 r = network_config_section_new(filename, section_line, &n);
45 if (r < 0)
46 return r;
47
48 existing = ordered_hashmap_get(network->sr_iov_by_section, n);
49 if (existing) {
50 *ret = existing;
51 return 0;
52 }
53
54 r = sr_iov_new(&sr_iov);
55 if (r < 0)
56 return r;
57
58 sr_iov->network = network;
59 sr_iov->section = TAKE_PTR(n);
60
61 r = ordered_hashmap_ensure_allocated(&network->sr_iov_by_section, &network_config_hash_ops);
62 if (r < 0)
63 return r;
64
65 r = ordered_hashmap_put(network->sr_iov_by_section, sr_iov->section, sr_iov);
66 if (r < 0)
67 return r;
68
69 *ret = TAKE_PTR(sr_iov);
70 return 0;
71 }
72
73 SRIOV *sr_iov_free(SRIOV *sr_iov) {
74 if (!sr_iov)
75 return NULL;
76
77 if (sr_iov->network && sr_iov->section)
78 ordered_hashmap_remove(sr_iov->network->sr_iov_by_section, sr_iov->section);
79
80 network_config_section_free(sr_iov->section);
81
82 return mfree(sr_iov);
83 }
84
85 static int sr_iov_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
86 int r;
87
88 assert(link);
89 assert(link->sr_iov_messages > 0);
90 link->sr_iov_messages--;
91
92 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
93 return 1;
94
95 r = sd_netlink_message_get_errno(m);
96 if (r < 0 && r != -EEXIST) {
97 log_link_message_error_errno(link, m, r, "Could not set up SR-IOV");
98 link_enter_failed(link);
99 return 1;
100 }
101
102 if (link->sr_iov_messages == 0) {
103 log_link_debug(link, "SR-IOV configured");
104 link->sr_iov_configured = true;
105 link_check_ready(link);
106 }
107
108 return 1;
109 }
110
111 static int sr_iov_configure(Link *link, SRIOV *sr_iov) {
112 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
113 int r;
114
115 assert(link);
116 assert(link->manager);
117 assert(link->manager->rtnl);
118 assert(link->ifindex > 0);
119
120 log_link_debug(link, "Setting SR-IOV virtual function %"PRIu32, sr_iov->vf);
121
122 r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex);
123 if (r < 0)
124 return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m");
125
126 r = sd_netlink_message_open_container(req, IFLA_VFINFO_LIST);
127 if (r < 0)
128 return log_link_error_errno(link, r, "Could not open IFLA_VFINFO_LIST container: %m");
129
130 r = sd_netlink_message_open_container(req, IFLA_VF_INFO);
131 if (r < 0)
132 return log_link_error_errno(link, r, "Could not open IFLA_VF_INFO container: %m");
133
134 if (!ether_addr_is_null(&sr_iov->mac)) {
135 struct ifla_vf_mac ivm = {
136 .vf = sr_iov->vf,
137 };
138
139 memcpy(ivm.mac, &sr_iov->mac, ETH_ALEN);
140 r = sd_netlink_message_append_data(req, IFLA_VF_MAC, &ivm, sizeof(struct ifla_vf_mac));
141 if (r < 0)
142 return log_link_error_errno(link, r, "Could not append IFLA_VF_MAC: %m");
143 }
144
145 if (sr_iov->vf_spoof_check_setting >= 0) {
146 struct ifla_vf_spoofchk ivs = {
147 .vf = sr_iov->vf,
148 .setting = sr_iov->vf_spoof_check_setting,
149 };
150
151 r = sd_netlink_message_append_data(req, IFLA_VF_SPOOFCHK, &ivs, sizeof(struct ifla_vf_spoofchk));
152 if (r < 0)
153 return log_link_error_errno(link, r, "Could not append IFLA_VF_SPOOFCHK: %m");
154 }
155
156 if (sr_iov->query_rss >= 0) {
157 struct ifla_vf_rss_query_en ivs = {
158 .vf = sr_iov->vf,
159 .setting = sr_iov->query_rss,
160 };
161
162 r = sd_netlink_message_append_data(req, IFLA_VF_RSS_QUERY_EN, &ivs, sizeof(struct ifla_vf_rss_query_en));
163 if (r < 0)
164 return log_link_error_errno(link, r, "Could not append IFLA_VF_RSS_QUERY_EN: %m");
165 }
166
167 if (sr_iov->trust >= 0) {
168 struct ifla_vf_trust ivt = {
169 .vf = sr_iov->vf,
170 .setting = sr_iov->trust,
171 };
172
173 r = sd_netlink_message_append_data(req, IFLA_VF_TRUST, &ivt, sizeof(struct ifla_vf_trust));
174 if (r < 0)
175 return log_link_error_errno(link, r, "Could not append IFLA_VF_TRUST: %m");
176 }
177
178 if (sr_iov->link_state >= 0) {
179 struct ifla_vf_link_state ivl = {
180 .vf = sr_iov->vf,
181 .link_state = sr_iov->link_state,
182 };
183
184 r = sd_netlink_message_append_data(req, IFLA_VF_LINK_STATE, &ivl, sizeof(struct ifla_vf_link_state));
185 if (r < 0)
186 return log_link_error_errno(link, r, "Could not append IFLA_VF_LINK_STATE: %m");
187 }
188
189 if (sr_iov->vlan > 0) {
190 /* Because of padding, first the buffer must be initialized with 0. */
191 struct ifla_vf_vlan_info ivvi = {};
192 ivvi.vf = sr_iov->vf;
193 ivvi.vlan = sr_iov->vlan;
194 ivvi.qos = sr_iov->qos;
195 ivvi.vlan_proto = htobe16(sr_iov->vlan_proto);
196
197 r = sd_netlink_message_open_container(req, IFLA_VF_VLAN_LIST);
198 if (r < 0)
199 return log_link_error_errno(link, r, "Could not open IFLA_VF_VLAN_LIST container: %m");
200
201 r = sd_netlink_message_append_data(req, IFLA_VF_VLAN_INFO, &ivvi, sizeof(struct ifla_vf_vlan_info));
202 if (r < 0)
203 return log_link_error_errno(link, r, "Could not append IFLA_VF_VLAN_INFO: %m");
204
205 r = sd_netlink_message_close_container(req);
206 if (r < 0)
207 return log_link_error_errno(link, r, "Could not close IFLA_VF_VLAN_LIST container: %m");
208 }
209
210 r = sd_netlink_message_close_container(req);
211 if (r < 0)
212 return log_link_error_errno(link, r, "Could not close IFLA_VF_INFO container: %m");
213
214 r = sd_netlink_message_close_container(req);
215 if (r < 0)
216 return log_link_error_errno(link, r, "Could not close IFLA_VFINFO_LIST container: %m");
217
218 r = netlink_call_async(link->manager->rtnl, NULL, req, sr_iov_handler,
219 link_netlink_destroy_callback, link);
220 if (r < 0)
221 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
222
223 link_ref(link);
224 link->sr_iov_messages++;
225
226 return 0;
227 }
228
229 int link_configure_sr_iov(Link *link) {
230 SRIOV *sr_iov;
231 int r;
232
233 assert(link);
234 assert(link->network);
235
236 if (link->sr_iov_messages != 0) {
237 log_link_debug(link, "SR-IOV is configuring.");
238 return 0;
239 }
240
241 link->sr_iov_configured = false;
242
243 ORDERED_HASHMAP_FOREACH(sr_iov, link->network->sr_iov_by_section) {
244 r = sr_iov_configure(link, sr_iov);
245 if (r < 0)
246 return r;
247 }
248
249 if (link->sr_iov_messages == 0)
250 link->sr_iov_configured = true;
251 else
252 log_link_debug(link, "Configuring SR-IOV");
253
254 return 0;
255 }
256
257 static int sr_iov_section_verify(SRIOV *sr_iov) {
258 assert(sr_iov);
259
260 if (section_is_invalid(sr_iov->section))
261 return -EINVAL;
262
263 if (sr_iov->vf == (uint32_t) -1)
264 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
265 "%s: [SRIOV] section without VirtualFunction= field configured. "
266 "Ignoring [SRIOV] section from line %u.",
267 sr_iov->section->filename, sr_iov->section->line);
268
269 return 0;
270 }
271
272 void network_drop_invalid_sr_iov(Network *network) {
273 SRIOV *sr_iov;
274
275 assert(network);
276
277 ORDERED_HASHMAP_FOREACH(sr_iov, network->sr_iov_by_section)
278 if (sr_iov_section_verify(sr_iov) < 0)
279 sr_iov_free(sr_iov);
280 }
281
282 int config_parse_sr_iov_uint32(
283 const char *unit,
284 const char *filename,
285 unsigned line,
286 const char *section,
287 unsigned section_line,
288 const char *lvalue,
289 int ltype,
290 const char *rvalue,
291 void *data,
292 void *userdata) {
293
294 _cleanup_(sr_iov_free_or_set_invalidp) SRIOV *sr_iov = NULL;
295 Network *network = data;
296 uint32_t k;
297 int r;
298
299 assert(filename);
300 assert(lvalue);
301 assert(rvalue);
302 assert(data);
303
304 r = sr_iov_new_static(network, filename, section_line, &sr_iov);
305 if (r < 0)
306 return r;
307
308 if (isempty(rvalue)) {
309 if (streq(lvalue, "VirtualFunction"))
310 sr_iov->vf = (uint32_t) -1;
311 else if (streq(lvalue, "VLANId"))
312 sr_iov->vlan = 0;
313 else if (streq(lvalue, "QualityOfService"))
314 sr_iov->qos = 0;
315 else
316 assert_not_reached("Invalid lvalue");
317
318 TAKE_PTR(sr_iov);
319 return 0;
320 }
321
322 r = safe_atou32(rvalue, &k);
323 if (r < 0) {
324 log_syntax(unit, LOG_WARNING, filename, line, r,
325 "Failed to parse SR-IOV '%s=', ignoring assignment: %s", lvalue, rvalue);
326 return 0;
327 }
328
329 if (streq(lvalue, "VLANId")) {
330 if (k == 0 || k > 4095) {
331 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid SR-IOV VLANId: %d", k);
332 return 0;
333 }
334 sr_iov->vlan = k;
335 } else if (streq(lvalue, "VirtualFunction")) {
336 if (k >= INT_MAX) {
337 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid SR-IOV virtual function: %d", k);
338 return 0;
339 }
340 sr_iov->vf = k;
341 } else if (streq(lvalue, "QualityOfService"))
342 sr_iov->qos = k;
343 else
344 assert_not_reached("Invalid lvalue");
345
346 TAKE_PTR(sr_iov);
347 return 0;
348 }
349
350 int config_parse_sr_iov_vlan_proto(
351 const char *unit,
352 const char *filename,
353 unsigned line,
354 const char *section,
355 unsigned section_line,
356 const char *lvalue,
357 int ltype,
358 const char *rvalue,
359 void *data,
360 void *userdata) {
361
362 _cleanup_(sr_iov_free_or_set_invalidp) SRIOV *sr_iov = NULL;
363 Network *network = data;
364 int r;
365
366 assert(filename);
367 assert(lvalue);
368 assert(rvalue);
369 assert(data);
370
371 r = sr_iov_new_static(network, filename, section_line, &sr_iov);
372 if (r < 0)
373 return r;
374
375 if (isempty(rvalue) || streq(rvalue, "802.1Q"))
376 sr_iov->vlan_proto = ETH_P_8021Q;
377 else if (streq(rvalue, "802.1ad"))
378 sr_iov->vlan_proto = ETH_P_8021AD;
379 else {
380 log_syntax(unit, LOG_WARNING, filename, line, 0,
381 "Invalid SR-IOV '%s=', ignoring assignment: %s", lvalue, rvalue);
382 return 0;
383 }
384
385 TAKE_PTR(sr_iov);
386 return 0;
387 }
388
389 int config_parse_sr_iov_link_state(
390 const char *unit,
391 const char *filename,
392 unsigned line,
393 const char *section,
394 unsigned section_line,
395 const char *lvalue,
396 int ltype,
397 const char *rvalue,
398 void *data,
399 void *userdata) {
400
401 _cleanup_(sr_iov_free_or_set_invalidp) SRIOV *sr_iov = NULL;
402 Network *network = data;
403 int r;
404
405 assert(filename);
406 assert(lvalue);
407 assert(rvalue);
408 assert(data);
409
410 r = sr_iov_new_static(network, filename, section_line, &sr_iov);
411 if (r < 0)
412 return r;
413
414 /* Unfortunately, SR_IOV_LINK_STATE_DISABLE is 2, not 0. So, we cannot use
415 * DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN() macro. */
416
417 if (isempty(rvalue)) {
418 sr_iov->link_state = _SR_IOV_LINK_STATE_INVALID;
419 TAKE_PTR(sr_iov);
420 return 0;
421 }
422
423 if (streq(rvalue, "auto")) {
424 sr_iov->link_state = SR_IOV_LINK_STATE_AUTO;
425 TAKE_PTR(sr_iov);
426 return 0;
427 }
428
429 r = parse_boolean(rvalue);
430 if (r < 0) {
431 log_syntax(unit, LOG_WARNING, filename, line, r,
432 "Failed to parse SR-IOV '%s=', ignoring assignment: %s", lvalue, rvalue);
433 return 0;
434 }
435
436 sr_iov->link_state = r ? SR_IOV_LINK_STATE_ENABLE : SR_IOV_LINK_STATE_DISABLE;
437 TAKE_PTR(sr_iov);
438 return 0;
439 }
440
441 int config_parse_sr_iov_boolean(
442 const char *unit,
443 const char *filename,
444 unsigned line,
445 const char *section,
446 unsigned section_line,
447 const char *lvalue,
448 int ltype,
449 const char *rvalue,
450 void *data,
451 void *userdata) {
452
453 _cleanup_(sr_iov_free_or_set_invalidp) SRIOV *sr_iov = NULL;
454 Network *network = data;
455 int r;
456
457 assert(filename);
458 assert(lvalue);
459 assert(rvalue);
460 assert(data);
461
462 r = sr_iov_new_static(network, filename, section_line, &sr_iov);
463 if (r < 0)
464 return r;
465
466 if (isempty(rvalue)) {
467 if (streq(lvalue, "MACSpoofCheck"))
468 sr_iov->vf_spoof_check_setting = -1;
469 else if (streq(lvalue, "QueryReceiveSideScaling"))
470 sr_iov->query_rss = -1;
471 else if (streq(lvalue, "Trust"))
472 sr_iov->trust = -1;
473 else
474 assert_not_reached("Invalid lvalue");
475
476 TAKE_PTR(sr_iov);
477 return 0;
478 }
479
480 r = parse_boolean(rvalue);
481 if (r < 0) {
482 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse '%s=', ignoring: %s", lvalue, rvalue);
483 return 0;
484 }
485
486 if (streq(lvalue, "MACSpoofCheck"))
487 sr_iov->vf_spoof_check_setting = r;
488 else if (streq(lvalue, "QueryReceiveSideScaling"))
489 sr_iov->query_rss = r;
490 else if (streq(lvalue, "Trust"))
491 sr_iov->trust = r;
492 else
493 assert_not_reached("Invalid lvalue");
494
495 TAKE_PTR(sr_iov);
496 return 0;
497 }
498
499 int config_parse_sr_iov_mac(
500 const char *unit,
501 const char *filename,
502 unsigned line,
503 const char *section,
504 unsigned section_line,
505 const char *lvalue,
506 int ltype,
507 const char *rvalue,
508 void *data,
509 void *userdata) {
510
511 _cleanup_(sr_iov_free_or_set_invalidp) SRIOV *sr_iov = NULL;
512 Network *network = data;
513 int r;
514
515 assert(filename);
516 assert(lvalue);
517 assert(rvalue);
518 assert(data);
519
520 r = sr_iov_new_static(network, filename, section_line, &sr_iov);
521 if (r < 0)
522 return r;
523
524 if (isempty(rvalue)) {
525 sr_iov->mac = ETHER_ADDR_NULL;
526 TAKE_PTR(sr_iov);
527 return 0;
528 }
529
530 r = ether_addr_from_string(rvalue, &sr_iov->mac);
531 if (r < 0) {
532 log_syntax(unit, LOG_WARNING, filename, line, r,
533 "Failed to parse SR-IOV '%s=', ignoring assignment: %s", lvalue, rvalue);
534 return 0;
535 }
536
537 TAKE_PTR(sr_iov);
538 return 0;
539 }