]>
Commit | Line | Data |
---|---|---|
518cd6b5 SS |
1 | /* SPDX-License-Identifier: LGPL-2.1+ |
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, | |
e64b31c8 | 21 | .vlan_proto = ETH_P_8021Q, |
518cd6b5 SS |
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 | 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 | ||
c3a5025d YW |
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 | ||
518cd6b5 SS |
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; | |
e64b31c8 | 195 | ivvi.vlan_proto = htobe16(sr_iov->vlan_proto); |
518cd6b5 SS |
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 sr_iov_section_verify(SRIOV *sr_iov) { | |
230 | assert(sr_iov); | |
231 | ||
232 | if (section_is_invalid(sr_iov->section)) | |
233 | return -EINVAL; | |
234 | ||
235 | if (sr_iov->vf == (uint32_t) -1) | |
236 | return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), | |
237 | "%s: [SRIOV] section without VirtualFunction= field configured. " | |
238 | "Ignoring [SRIOV] section from line %u.", | |
239 | sr_iov->section->filename, sr_iov->section->line); | |
240 | ||
241 | return 0; | |
242 | } | |
243 | ||
244 | int config_parse_sr_iov_uint32( | |
245 | const char *unit, | |
246 | const char *filename, | |
247 | unsigned line, | |
248 | const char *section, | |
249 | unsigned section_line, | |
250 | const char *lvalue, | |
251 | int ltype, | |
252 | const char *rvalue, | |
253 | void *data, | |
254 | void *userdata) { | |
255 | ||
256 | _cleanup_(sr_iov_free_or_set_invalidp) SRIOV *sr_iov = NULL; | |
257 | Network *network = data; | |
258 | uint32_t k; | |
259 | int r; | |
260 | ||
261 | assert(filename); | |
262 | assert(lvalue); | |
263 | assert(rvalue); | |
264 | assert(data); | |
265 | ||
266 | r = sr_iov_new_static(network, filename, section_line, &sr_iov); | |
267 | if (r < 0) | |
268 | return r; | |
269 | ||
270 | if (isempty(rvalue)) { | |
271 | if (streq(lvalue, "VirtualFunction")) | |
272 | sr_iov->vf = (uint32_t) -1; | |
273 | else if (streq(lvalue, "VLANId")) | |
274 | sr_iov->vlan = 0; | |
275 | else if (streq(lvalue, "QualityOfService")) | |
276 | sr_iov->qos = 0; | |
277 | else | |
278 | assert_not_reached("Invalid lvalue"); | |
279 | ||
280 | TAKE_PTR(sr_iov); | |
281 | return 0; | |
282 | } | |
283 | ||
284 | r = safe_atou32(rvalue, &k); | |
285 | if (r < 0) { | |
286 | log_syntax(unit, LOG_ERR, filename, line, r, | |
287 | "Failed to parse SR-IOV '%s=', ignoring assignment: %s", lvalue, rvalue); | |
288 | return 0; | |
289 | } | |
290 | ||
291 | if (streq(lvalue, "VLANId")) { | |
292 | if (k == 0 || k > 4095) { | |
293 | log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid SR-IOV VLANId: %d", k); | |
294 | return 0; | |
295 | } | |
296 | sr_iov->vlan = k; | |
297 | } else if (streq(lvalue, "VirtualFunction")) { | |
298 | if (k >= INT_MAX) { | |
299 | log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid SR-IOV virtual function: %d", k); | |
300 | return 0; | |
301 | } | |
302 | sr_iov->vf = k; | |
303 | } else if (streq(lvalue, "QualityOfService")) | |
304 | sr_iov->qos = k; | |
305 | else | |
306 | assert_not_reached("Invalid lvalue"); | |
307 | ||
308 | TAKE_PTR(sr_iov); | |
309 | return 0; | |
310 | } | |
311 | ||
e64b31c8 YW |
312 | int config_parse_sr_iov_vlan_proto( |
313 | const char *unit, | |
314 | const char *filename, | |
315 | unsigned line, | |
316 | const char *section, | |
317 | unsigned section_line, | |
318 | const char *lvalue, | |
319 | int ltype, | |
320 | const char *rvalue, | |
321 | void *data, | |
322 | void *userdata) { | |
323 | ||
324 | _cleanup_(sr_iov_free_or_set_invalidp) SRIOV *sr_iov = NULL; | |
325 | Network *network = data; | |
326 | int r; | |
327 | ||
328 | assert(filename); | |
329 | assert(lvalue); | |
330 | assert(rvalue); | |
331 | assert(data); | |
332 | ||
333 | r = sr_iov_new_static(network, filename, section_line, &sr_iov); | |
334 | if (r < 0) | |
335 | return r; | |
336 | ||
337 | if (isempty(rvalue) || streq(rvalue, "802.1Q")) | |
338 | sr_iov->vlan_proto = ETH_P_8021Q; | |
339 | else if (streq(rvalue, "802.1ad")) | |
340 | sr_iov->vlan_proto = ETH_P_8021AD; | |
341 | else { | |
342 | log_syntax(unit, LOG_ERR, filename, line, 0, | |
343 | "Invalid SR-IOV '%s=', ignoring assignment: %s", lvalue, rvalue); | |
344 | return 0; | |
345 | } | |
346 | ||
347 | TAKE_PTR(sr_iov); | |
348 | return 0; | |
349 | } | |
350 | ||
518cd6b5 SS |
351 | int config_parse_sr_iov_link_state( |
352 | const char *unit, | |
353 | const char *filename, | |
354 | unsigned line, | |
355 | const char *section, | |
356 | unsigned section_line, | |
357 | const char *lvalue, | |
358 | int ltype, | |
359 | const char *rvalue, | |
360 | void *data, | |
361 | void *userdata) { | |
362 | ||
363 | _cleanup_(sr_iov_free_or_set_invalidp) SRIOV *sr_iov = NULL; | |
364 | Network *network = data; | |
365 | int r; | |
366 | ||
367 | assert(filename); | |
368 | assert(lvalue); | |
369 | assert(rvalue); | |
370 | assert(data); | |
371 | ||
372 | r = sr_iov_new_static(network, filename, section_line, &sr_iov); | |
373 | if (r < 0) | |
374 | return r; | |
375 | ||
376 | /* Unfortunately, SR_IOV_LINK_STATE_DISABLE is 2, not 0. So, we cannot use | |
377 | * DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN() macro. */ | |
378 | ||
379 | if (isempty(rvalue)) { | |
380 | sr_iov->link_state = _SR_IOV_LINK_STATE_INVALID; | |
381 | TAKE_PTR(sr_iov); | |
382 | return 0; | |
383 | } | |
384 | ||
385 | if (streq(rvalue, "auto")) { | |
386 | sr_iov->link_state = SR_IOV_LINK_STATE_AUTO; | |
387 | TAKE_PTR(sr_iov); | |
388 | return 0; | |
389 | } | |
390 | ||
391 | r = parse_boolean(rvalue); | |
392 | if (r < 0) { | |
393 | log_syntax(unit, LOG_ERR, filename, line, r, | |
394 | "Failed to parse SR-IOV '%s=', ignoring assignment: %s", lvalue, rvalue); | |
395 | return 0; | |
396 | } | |
397 | ||
398 | sr_iov->link_state = r ? SR_IOV_LINK_STATE_ENABLE : SR_IOV_LINK_STATE_DISABLE; | |
399 | TAKE_PTR(sr_iov); | |
400 | return 0; | |
401 | } | |
402 | ||
403 | int config_parse_sr_iov_boolean( | |
404 | const char *unit, | |
405 | const char *filename, | |
406 | unsigned line, | |
407 | const char *section, | |
408 | unsigned section_line, | |
409 | const char *lvalue, | |
410 | int ltype, | |
411 | const char *rvalue, | |
412 | void *data, | |
413 | void *userdata) { | |
414 | ||
415 | _cleanup_(sr_iov_free_or_set_invalidp) SRIOV *sr_iov = NULL; | |
416 | Network *network = data; | |
417 | int r; | |
418 | ||
419 | assert(filename); | |
420 | assert(lvalue); | |
421 | assert(rvalue); | |
422 | assert(data); | |
423 | ||
424 | r = sr_iov_new_static(network, filename, section_line, &sr_iov); | |
425 | if (r < 0) | |
426 | return r; | |
427 | ||
428 | if (isempty(rvalue)) { | |
429 | if (streq(lvalue, "MACSpoofCheck")) | |
430 | sr_iov->vf_spoof_check_setting = -1; | |
431 | else if (streq(lvalue, "QueryReceiveSideScaling")) | |
432 | sr_iov->query_rss = -1; | |
433 | else if (streq(lvalue, "Trust")) | |
434 | sr_iov->trust = -1; | |
435 | else | |
436 | assert_not_reached("Invalid lvalue"); | |
437 | ||
438 | TAKE_PTR(sr_iov); | |
439 | return 0; | |
440 | } | |
441 | ||
442 | r = parse_boolean(rvalue); | |
443 | if (r < 0) { | |
444 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse '%s=', ignoring: %s", lvalue, rvalue); | |
445 | return 0; | |
446 | } | |
447 | ||
448 | if (streq(lvalue, "MACSpoofCheck")) | |
449 | sr_iov->vf_spoof_check_setting = r; | |
450 | else if (streq(lvalue, "QueryReceiveSideScaling")) | |
451 | sr_iov->query_rss = r; | |
452 | else if (streq(lvalue, "Trust")) | |
453 | sr_iov->trust = r; | |
454 | else | |
455 | assert_not_reached("Invalid lvalue"); | |
456 | ||
457 | TAKE_PTR(sr_iov); | |
458 | return 0; | |
459 | } | |
c3a5025d YW |
460 | |
461 | int config_parse_sr_iov_mac( | |
462 | const char *unit, | |
463 | const char *filename, | |
464 | unsigned line, | |
465 | const char *section, | |
466 | unsigned section_line, | |
467 | const char *lvalue, | |
468 | int ltype, | |
469 | const char *rvalue, | |
470 | void *data, | |
471 | void *userdata) { | |
472 | ||
473 | _cleanup_(sr_iov_free_or_set_invalidp) SRIOV *sr_iov = NULL; | |
474 | Network *network = data; | |
475 | int r; | |
476 | ||
477 | assert(filename); | |
478 | assert(lvalue); | |
479 | assert(rvalue); | |
480 | assert(data); | |
481 | ||
482 | r = sr_iov_new_static(network, filename, section_line, &sr_iov); | |
483 | if (r < 0) | |
484 | return r; | |
485 | ||
486 | if (isempty(rvalue)) { | |
487 | sr_iov->mac = ETHER_ADDR_NULL; | |
488 | TAKE_PTR(sr_iov); | |
489 | return 0; | |
490 | } | |
491 | ||
492 | r = ether_addr_from_string(rvalue, &sr_iov->mac); | |
493 | if (r < 0) { | |
494 | log_syntax(unit, LOG_ERR, filename, line, 0, | |
495 | "Failed to parse SR-IOV '%s=', ignoring assignment: %s", lvalue, rvalue); | |
496 | return 0; | |
497 | } | |
498 | ||
499 | TAKE_PTR(sr_iov); | |
500 | return 0; | |
501 | } |