Debian Codegraph @

src/shared/netif-sriov.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-device.h"
4 #include "sd-netlink.h"
5
6 #include "alloc-util.h"
7 #include "conf-parser.h"
8 #include "device-private.h"
9 #include "device-util.h"
10 #include "ether-addr-util.h"
11 #include "netif-sriov.h"
12 #include "parse-util.h"
13 #include "set.h"
14 #include "siphash24.h"
15 #include "stdio-util.h"
16 #include "string-table.h"
17 #include "string-util.h"
18
19 static SRIOV* sr_iov_free(SRIOV *sr_iov) {
20 if (!sr_iov)
21 return NULL;
22
23 if (sr_iov->sr_iov_by_section && sr_iov->section)
24 ordered_hashmap_remove(sr_iov->sr_iov_by_section, sr_iov->section);
25
26 config_section_free(sr_iov->section);
27
28 return mfree(sr_iov);
29 }
30
31 DEFINE_SECTION_CLEANUP_FUNCTIONS(SRIOV, sr_iov_free);
32
33 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
34 sr_iov_hash_ops_by_section,
35 ConfigSection, config_section_hash_func, config_section_compare_func,
36 SRIOV, sr_iov_free);
37
38 static int sr_iov_new(SRIOV **ret) {
39 SRIOV *sr_iov;
40
41 assert(ret);
42
43 sr_iov = new(SRIOV, 1);
44 if (!sr_iov)
45 return -ENOMEM;
46
47 *sr_iov = (SRIOV) {
48 .vf = UINT32_MAX,
49 .vlan_proto = ETH_P_8021Q,
50 .vf_spoof_check_setting = -1,
51 .trust = -1,
52 .query_rss = -1,
53 .link_state = _SR_IOV_LINK_STATE_INVALID,
54 };
55
56 *ret = TAKE_PTR(sr_iov);
57
58 return 0;
59 }
60
61 static int sr_iov_new_static(OrderedHashmap **sr_iov_by_section, const char *filename, unsigned section_line, SRIOV **ret) {
62 _cleanup_(config_section_freep) ConfigSection *n = NULL;
63 _cleanup_(sr_iov_freep) SRIOV *sr_iov = NULL;
64 SRIOV *existing = NULL;
65 int r;
66
67 assert(sr_iov_by_section);
68 assert(filename);
69 assert(section_line > 0);
70 assert(ret);
71
72 r = config_section_new(filename, section_line, &n);
73 if (r < 0)
74 return r;
75
76 existing = ordered_hashmap_get(*sr_iov_by_section, n);
77 if (existing) {
78 *ret = existing;
79 return 0;
80 }
81
82 r = sr_iov_new(&sr_iov);
83 if (r < 0)
84 return r;
85
86 r = ordered_hashmap_ensure_put(sr_iov_by_section, &sr_iov_hash_ops_by_section, n, sr_iov);
87 if (r < 0)
88 return r;
89
90 sr_iov->section = TAKE_PTR(n);
91 sr_iov->sr_iov_by_section = *sr_iov_by_section;
92
93 *ret = TAKE_PTR(sr_iov);
94 return 0;
95 }
96
97 void sr_iov_hash_func(const SRIOV *sr_iov, struct siphash *state) {
98 assert(sr_iov);
99 assert(state);
100
101 siphash24_compress_typesafe(sr_iov->vf, state);
102 }
103
104 int sr_iov_compare_func(const SRIOV *s1, const SRIOV *s2) {
105 assert(s1);
106 assert(s2);
107
108 return CMP(s1->vf, s2->vf);
109 }
110
111 DEFINE_PRIVATE_HASH_OPS(
112 sr_iov_hash_ops,
113 SRIOV,
114 sr_iov_hash_func,
115 sr_iov_compare_func);
116
117 static const char * const sr_iov_attribute_table[_SR_IOV_ATTRIBUTE_MAX] = {
118 [SR_IOV_VF_MAC] = "MAC address",
119 [SR_IOV_VF_SPOOFCHK] = "spoof check",
120 [SR_IOV_VF_RSS_QUERY_EN] = "RSS query",
121 [SR_IOV_VF_TRUST] = "trust",
122 [SR_IOV_VF_LINK_STATE] = "link state",
123 [SR_IOV_VF_VLAN_LIST] = "vlan list",
124 };
125
126 DEFINE_STRING_TABLE_LOOKUP_TO_STRING(sr_iov_attribute, SRIOVAttribute);
127
128 bool sr_iov_has_config(SRIOV *sr_iov, SRIOVAttribute attr) {
129 assert(sr_iov);
130
131 switch (attr) {
132 case SR_IOV_VF_MAC:
133 return !ether_addr_is_null(&sr_iov->mac);
134
135 case SR_IOV_VF_SPOOFCHK:
136 return sr_iov->vf_spoof_check_setting >= 0;
137
138 case SR_IOV_VF_RSS_QUERY_EN:
139 return sr_iov->query_rss >= 0;
140
141 case SR_IOV_VF_TRUST:
142 return sr_iov->trust >= 0;
143
144 case SR_IOV_VF_LINK_STATE:
145 return sr_iov->link_state >= 0;
146
147 case SR_IOV_VF_VLAN_LIST:
148 return sr_iov->vlan > 0;
149
150 default:
151 assert_not_reached();
152 }
153 }
154
155 int sr_iov_set_netlink_message(SRIOV *sr_iov, SRIOVAttribute attr, sd_netlink_message *req) {
156
157 int r;
158
159 assert(sr_iov);
160 assert(req);
161
162 r = sd_netlink_message_open_container(req, IFLA_VFINFO_LIST);
163 if (r < 0)
164 return r;
165
166 r = sd_netlink_message_open_container(req, IFLA_VF_INFO);
167 if (r < 0)
168 return r;
169
170 switch (attr) {
171 case SR_IOV_VF_MAC: {
172 if (ether_addr_is_null(&sr_iov->mac))
173 return -ENODATA;
174
175 struct ifla_vf_mac ivm = {
176 .vf = sr_iov->vf,
177 };
178
179 memcpy(ivm.mac, &sr_iov->mac, ETH_ALEN);
180 r = sd_netlink_message_append_data(req, IFLA_VF_MAC, &ivm, sizeof(struct ifla_vf_mac));
181 break;
182 }
183 case SR_IOV_VF_SPOOFCHK: {
184 if (sr_iov->vf_spoof_check_setting < 0)
185 return -ENODATA;
186
187 struct ifla_vf_spoofchk ivs = {
188 .vf = sr_iov->vf,
189 .setting = sr_iov->vf_spoof_check_setting,
190 };
191
192 r = sd_netlink_message_append_data(req, IFLA_VF_SPOOFCHK, &ivs, sizeof(struct ifla_vf_spoofchk));
193 break;
194 }
195 case SR_IOV_VF_RSS_QUERY_EN: {
196 if (sr_iov->query_rss < 0)
197 return -ENODATA;
198
199 struct ifla_vf_rss_query_en ivs = {
200 .vf = sr_iov->vf,
201 .setting = sr_iov->query_rss,
202 };
203
204 r = sd_netlink_message_append_data(req, IFLA_VF_RSS_QUERY_EN, &ivs, sizeof(struct ifla_vf_rss_query_en));
205 break;
206 }
207 case SR_IOV_VF_TRUST: {
208 if (sr_iov->trust < 0)
209 return -ENODATA;
210
211 struct ifla_vf_trust ivt = {
212 .vf = sr_iov->vf,
213 .setting = sr_iov->trust,
214 };
215
216 r = sd_netlink_message_append_data(req, IFLA_VF_TRUST, &ivt, sizeof(struct ifla_vf_trust));
217 break;
218 }
219 case SR_IOV_VF_LINK_STATE: {
220 if (sr_iov->link_state < 0)
221 return -ENODATA;
222
223 struct ifla_vf_link_state ivl = {
224 .vf = sr_iov->vf,
225 .link_state = sr_iov->link_state,
226 };
227
228 r = sd_netlink_message_append_data(req, IFLA_VF_LINK_STATE, &ivl, sizeof(struct ifla_vf_link_state));
229 break;
230 }
231 case SR_IOV_VF_VLAN_LIST: {
232 if (sr_iov->vlan <= 0)
233 return -ENODATA;
234
235 /* Because of padding, first the buffer must be initialized with 0. */
236 struct ifla_vf_vlan_info ivvi = {};
237 ivvi.vf = sr_iov->vf;
238 ivvi.vlan = sr_iov->vlan;
239 ivvi.qos = sr_iov->qos;
240 ivvi.vlan_proto = htobe16(sr_iov->vlan_proto);
241
242 r = sd_netlink_message_open_container(req, IFLA_VF_VLAN_LIST);
243 if (r < 0)
244 return r;
245
246 r = sd_netlink_message_append_data(req, IFLA_VF_VLAN_INFO, &ivvi, sizeof(struct ifla_vf_vlan_info));
247 if (r < 0)
248 return r;
249
250 r = sd_netlink_message_close_container(req);
251 break;
252 }
253 default:
254 assert_not_reached();
255 }
256 if (r < 0)
257 return r;
258
259 r = sd_netlink_message_close_container(req);
260 if (r < 0)
261 return r;
262
263 r = sd_netlink_message_close_container(req);
264 if (r < 0)
265 return r;
266
267 return 0;
268 }
269
270 int sr_iov_get_num_vfs(sd_device *device, uint32_t *ret) {
271 return device_get_sysattr_u32(device, "device/sriov_numvfs", ret);
272 }
273
274 int sr_iov_set_num_vfs(sd_device *device, uint32_t num_vfs, OrderedHashmap *sr_iov_by_section) {
275 char val[DECIMAL_STR_MAX(uint32_t)];
276 int r;
277
278 assert(device);
279
280 if (num_vfs == UINT32_MAX) {
281 uint32_t current_num_vfs;
282 SRIOV *sr_iov;
283
284 /* If the number of virtual functions is not specified, then use the maximum number of VF + 1. */
285
286 num_vfs = 0;
287 ORDERED_HASHMAP_FOREACH(sr_iov, sr_iov_by_section)
288 num_vfs = MAX(num_vfs, sr_iov->vf + 1);
289
290 if (num_vfs == 0) /* No VF is configured. */
291 return 0;
292
293 r = sr_iov_get_num_vfs(device, &current_num_vfs);
294 if (r < 0)
295 return log_device_debug_errno(device, r, "Failed to get the current number of SR-IOV virtual functions: %m");
296
297 /* Enough VFs already exist. */
298 if (num_vfs <= current_num_vfs)
299 return 0;
300
301 } else if (num_vfs == 0) {
302 r = sd_device_set_sysattr_value(device, "device/sriov_numvfs", "0");
303 if (r < 0)
304 log_device_debug_errno(device, r, "Failed to write device/sriov_numvfs sysfs attribute, ignoring: %m");
305
306 /* Gracefully handle the error in disabling VFs when the interface does not support SR-IOV. */
307 return r == -ENOENT ? 0 : r;
308 }
309
310 /* So, the interface does not have enough VFs. Before increasing the number of VFs, check the
311 * maximum allowed number of VFs from the sriov_totalvfs sysattr. Note that the sysattr
312 * currently exists only for PCI drivers. Hence, ignore -ENOENT.
313 * TODO: netdevsim provides the information in debugfs. */
314 uint32_t max_num_vfs;
315 r = device_get_sysattr_u32(device, "device/sriov_totalvfs", &max_num_vfs);
316 if (r >= 0) {
317 if (num_vfs > max_num_vfs)
318 return log_device_debug_errno(device, SYNTHETIC_ERRNO(ERANGE),
319 "Specified number of virtual functions is out of range. "
320 "The maximum allowed value is %"PRIu32".",
321 max_num_vfs);
322
323 } else if (r != -ENOENT) /* Currently, only PCI driver has the attribute. */
324 return log_device_debug_errno(device, r, "Failed to read device/sriov_totalvfs sysfs attribute: %m");
325
326 xsprintf(val, "%"PRIu32, num_vfs);
327 r = sd_device_set_sysattr_value(device, "device/sriov_numvfs", val);
328 if (r == -EBUSY) {
329 /* Some devices e.g. netdevsim refuse to set sriov_numvfs if it has non-zero value. */
330 r = sd_device_set_sysattr_value(device, "device/sriov_numvfs", "0");
331 if (r >= 0)
332 r = sd_device_set_sysattr_value(device, "device/sriov_numvfs", val);
333 }
334 if (r < 0)
335 return log_device_debug_errno(device, r, "Failed to write device/sriov_numvfs sysfs attribute: %m");
336
337 log_device_debug(device, "device/sriov_numvfs sysfs attribute set to '%s'.", val);
338 return 0;
339 }
340
341 static int sr_iov_section_verify(uint32_t num_vfs, SRIOV *sr_iov) {
342 assert(sr_iov);
343
344 if (section_is_invalid(sr_iov->section))
345 return -EINVAL;
346
347 if (sr_iov->vf == UINT32_MAX)
348 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
349 "%s: [SR-IOV] section without VirtualFunction= field configured. "
350 "Ignoring [SR-IOV] section from line %u.",
351 sr_iov->section->filename, sr_iov->section->line);
352
353 if (sr_iov->vf >= num_vfs)
354 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
355 "%s: VirtualFunction= must be smaller than the value specified in SR-IOVVirtualFunctions=. "
356 "Ignoring [SR-IOV] section from line %u.",
357 sr_iov->section->filename, sr_iov->section->line);
358
359 return 0;
360 }
361
362 int sr_iov_drop_invalid_sections(uint32_t num_vfs, OrderedHashmap *sr_iov_by_section) {
363 _cleanup_set_free_ Set *set = NULL;
364 SRIOV *sr_iov;
365 int r;
366
367 ORDERED_HASHMAP_FOREACH(sr_iov, sr_iov_by_section) {
368 SRIOV *dup;
369
370 if (sr_iov_section_verify(num_vfs, sr_iov) < 0) {
371 sr_iov_free(sr_iov);
372 continue;
373 }
374
375 dup = set_remove(set, sr_iov);
376 if (dup) {
377 log_warning("%s: Conflicting [SR-IOV] section is specified at line %u and %u, "
378 "dropping the [SR-IOV] section specified at line %u.",
379 dup->section->filename, sr_iov->section->line,
380 dup->section->line, dup->section->line);
381 sr_iov_free(dup);
382 }
383
384 r = set_ensure_put(&set, &sr_iov_hash_ops, sr_iov);
385 if (r < 0)
386 return log_oom();
387 assert(r > 0);
388 }
389
390 return 0;
391 }
392
393 int config_parse_sr_iov_uint32(
394 const char *unit,
395 const char *filename,
396 unsigned line,
397 const char *section,
398 unsigned section_line,
399 const char *lvalue,
400 int ltype,
401 const char *rvalue,
402 void *data,
403 void *userdata) {
404
405 _cleanup_(sr_iov_free_or_set_invalidp) SRIOV *sr_iov = NULL;
406 OrderedHashmap **sr_iov_by_section = ASSERT_PTR(data);
407 uint32_t k;
408 int r;
409
410 assert(filename);
411 assert(lvalue);
412 assert(rvalue);
413
414 r = sr_iov_new_static(sr_iov_by_section, filename, section_line, &sr_iov);
415 if (r < 0)
416 return r;
417
418 if (isempty(rvalue)) {
419 if (streq(lvalue, "VirtualFunction"))
420 sr_iov->vf = UINT32_MAX;
421 else if (streq(lvalue, "VLANId"))
422 sr_iov->vlan = 0;
423 else if (streq(lvalue, "QualityOfService"))
424 sr_iov->qos = 0;
425 else
426 assert_not_reached();
427
428 TAKE_PTR(sr_iov);
429 return 0;
430 }
431
432 r = safe_atou32(rvalue, &k);
433 if (r < 0) {
434 log_syntax(unit, LOG_WARNING, filename, line, r,
435 "Failed to parse SR-IOV '%s=', ignoring assignment: %s", lvalue, rvalue);
436 return 0;
437 }
438
439 if (streq(lvalue, "VLANId")) {
440 if (k == 0 || k > 4095) {
441 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid SR-IOV VLANId: %u", k);
442 return 0;
443 }
444 sr_iov->vlan = k;
445 } else if (streq(lvalue, "VirtualFunction")) {
446 if (k >= INT_MAX) {
447 log_syntax(unit, LOG_WARNING, filename, line, 0, "Invalid SR-IOV virtual function: %u", k);
448 return 0;
449 }
450 sr_iov->vf = k;
451 } else if (streq(lvalue, "QualityOfService"))
452 sr_iov->qos = k;
453 else
454 assert_not_reached();
455
456 TAKE_PTR(sr_iov);
457 return 0;
458 }
459
460 int config_parse_sr_iov_vlan_proto(
461 const char *unit,
462 const char *filename,
463 unsigned line,
464 const char *section,
465 unsigned section_line,
466 const char *lvalue,
467 int ltype,
468 const char *rvalue,
469 void *data,
470 void *userdata) {
471
472 _cleanup_(sr_iov_free_or_set_invalidp) SRIOV *sr_iov = NULL;
473 OrderedHashmap **sr_iov_by_section = ASSERT_PTR(data);
474 int r;
475
476 assert(filename);
477 assert(lvalue);
478 assert(rvalue);
479
480 r = sr_iov_new_static(sr_iov_by_section, filename, section_line, &sr_iov);
481 if (r < 0)
482 return r;
483
484 if (isempty(rvalue) || streq(rvalue, "802.1Q"))
485 sr_iov->vlan_proto = ETH_P_8021Q;
486 else if (streq(rvalue, "802.1ad"))
487 sr_iov->vlan_proto = ETH_P_8021AD;
488 else {
489 log_syntax(unit, LOG_WARNING, filename, line, 0,
490 "Invalid SR-IOV '%s=', ignoring assignment: %s", lvalue, rvalue);
491 return 0;
492 }
493
494 TAKE_PTR(sr_iov);
495 return 0;
496 }
497
498 int config_parse_sr_iov_link_state(
499 const char *unit,
500 const char *filename,
501 unsigned line,
502 const char *section,
503 unsigned section_line,
504 const char *lvalue,
505 int ltype,
506 const char *rvalue,
507 void *data,
508 void *userdata) {
509
510 _cleanup_(sr_iov_free_or_set_invalidp) SRIOV *sr_iov = NULL;
511 OrderedHashmap **sr_iov_by_section = ASSERT_PTR(data);
512 int r;
513
514 assert(filename);
515 assert(lvalue);
516 assert(rvalue);
517
518 r = sr_iov_new_static(sr_iov_by_section, filename, section_line, &sr_iov);
519 if (r < 0)
520 return r;
521
522 /* Unfortunately, SR_IOV_LINK_STATE_DISABLE is 2, not 0. So, we cannot use
523 * DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN() macro. */
524
525 if (isempty(rvalue)) {
526 sr_iov->link_state = _SR_IOV_LINK_STATE_INVALID;
527 TAKE_PTR(sr_iov);
528 return 0;
529 }
530
531 if (streq(rvalue, "auto")) {
532 sr_iov->link_state = SR_IOV_LINK_STATE_AUTO;
533 TAKE_PTR(sr_iov);
534 return 0;
535 }
536
537 r = parse_boolean(rvalue);
538 if (r < 0) {
539 log_syntax(unit, LOG_WARNING, filename, line, r,
540 "Failed to parse SR-IOV '%s=', ignoring assignment: %s", lvalue, rvalue);
541 return 0;
542 }
543
544 sr_iov->link_state = r ? SR_IOV_LINK_STATE_ENABLE : SR_IOV_LINK_STATE_DISABLE;
545 TAKE_PTR(sr_iov);
546 return 0;
547 }
548
549 int config_parse_sr_iov_boolean(
550 const char *unit,
551 const char *filename,
552 unsigned line,
553 const char *section,
554 unsigned section_line,
555 const char *lvalue,
556 int ltype,
557 const char *rvalue,
558 void *data,
559 void *userdata) {
560
561 _cleanup_(sr_iov_free_or_set_invalidp) SRIOV *sr_iov = NULL;
562 OrderedHashmap **sr_iov_by_section = ASSERT_PTR(data);
563 int r;
564
565 assert(filename);
566 assert(lvalue);
567 assert(rvalue);
568
569 r = sr_iov_new_static(sr_iov_by_section, filename, section_line, &sr_iov);
570 if (r < 0)
571 return r;
572
573 if (isempty(rvalue)) {
574 if (streq(lvalue, "MACSpoofCheck"))
575 sr_iov->vf_spoof_check_setting = -1;
576 else if (streq(lvalue, "QueryReceiveSideScaling"))
577 sr_iov->query_rss = -1;
578 else if (streq(lvalue, "Trust"))
579 sr_iov->trust = -1;
580 else
581 assert_not_reached();
582
583 TAKE_PTR(sr_iov);
584 return 0;
585 }
586
587 r = parse_boolean(rvalue);
588 if (r < 0) {
589 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse '%s=', ignoring: %s", lvalue, rvalue);
590 return 0;
591 }
592
593 if (streq(lvalue, "MACSpoofCheck"))
594 sr_iov->vf_spoof_check_setting = r;
595 else if (streq(lvalue, "QueryReceiveSideScaling"))
596 sr_iov->query_rss = r;
597 else if (streq(lvalue, "Trust"))
598 sr_iov->trust = r;
599 else
600 assert_not_reached();
601
602 TAKE_PTR(sr_iov);
603 return 0;
604 }
605
606 int config_parse_sr_iov_mac(
607 const char *unit,
608 const char *filename,
609 unsigned line,
610 const char *section,
611 unsigned section_line,
612 const char *lvalue,
613 int ltype,
614 const char *rvalue,
615 void *data,
616 void *userdata) {
617
618 _cleanup_(sr_iov_free_or_set_invalidp) SRIOV *sr_iov = NULL;
619 OrderedHashmap **sr_iov_by_section = ASSERT_PTR(data);
620 int r;
621
622 assert(filename);
623 assert(lvalue);
624 assert(rvalue);
625
626 r = sr_iov_new_static(sr_iov_by_section, filename, section_line, &sr_iov);
627 if (r < 0)
628 return r;
629
630 if (isempty(rvalue)) {
631 sr_iov->mac = ETHER_ADDR_NULL;
632 TAKE_PTR(sr_iov);
633 return 0;
634 }
635
636 r = parse_ether_addr(rvalue, &sr_iov->mac);
637 if (r < 0) {
638 log_syntax(unit, LOG_WARNING, filename, line, r,
639 "Failed to parse SR-IOV '%s=', ignoring assignment: %s", lvalue, rvalue);
640 return 0;
641 }
642
643 TAKE_PTR(sr_iov);
644 return 0;
645 }
646
647 int config_parse_sr_iov_num_vfs(
648 const char *unit,
649 const char *filename,
650 unsigned line,
651 const char *section,
652 unsigned section_line,
653 const char *lvalue,
654 int ltype,
655 const char *rvalue,
656 void *data,
657 void *userdata) {
658
659 uint32_t n, *num_vfs = ASSERT_PTR(data);
660 int r;
661
662 assert(filename);
663 assert(lvalue);
664 assert(rvalue);
665
666 if (isempty(rvalue)) {
667 *num_vfs = UINT32_MAX;
668 return 0;
669 }
670
671 r = safe_atou32(rvalue, &n);
672 if (r < 0) {
673 log_syntax(unit, LOG_WARNING, filename, line, r,
674 "Failed to parse %s=, ignoring assignment: %s", lvalue, rvalue);
675 return 0;
676 }
677
678 if (n > INT_MAX) {
679 log_syntax(unit, LOG_WARNING, filename, line, 0,
680 "The number of SR-IOV virtual functions is too large. It must be equal to "
681 "or smaller than 2147483647. Ignoring assignment: %"PRIu32, n);
682 return 0;
683 }
684
685 *num_vfs = n;
686 return 0;
687 }