]>
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 | ||
134 | if (sr_iov->vf_spoof_check_setting >= 0) { | |
135 | struct ifla_vf_spoofchk ivs = { | |
136 | .vf = sr_iov->vf, | |
137 | .setting = sr_iov->vf_spoof_check_setting, | |
138 | }; | |
139 | ||
140 | r = sd_netlink_message_append_data(req, IFLA_VF_SPOOFCHK, &ivs, sizeof(struct ifla_vf_spoofchk)); | |
141 | if (r < 0) | |
142 | return log_link_error_errno(link, r, "Could not append IFLA_VF_SPOOFCHK: %m"); | |
143 | } | |
144 | ||
145 | if (sr_iov->query_rss >= 0) { | |
146 | struct ifla_vf_rss_query_en ivs = { | |
147 | .vf = sr_iov->vf, | |
148 | .setting = sr_iov->query_rss, | |
149 | }; | |
150 | ||
151 | r = sd_netlink_message_append_data(req, IFLA_VF_RSS_QUERY_EN, &ivs, sizeof(struct ifla_vf_rss_query_en)); | |
152 | if (r < 0) | |
153 | return log_link_error_errno(link, r, "Could not append IFLA_VF_RSS_QUERY_EN: %m"); | |
154 | } | |
155 | ||
156 | if (sr_iov->trust >= 0) { | |
157 | struct ifla_vf_trust ivt = { | |
158 | .vf = sr_iov->vf, | |
159 | .setting = sr_iov->trust, | |
160 | }; | |
161 | ||
162 | r = sd_netlink_message_append_data(req, IFLA_VF_TRUST, &ivt, sizeof(struct ifla_vf_trust)); | |
163 | if (r < 0) | |
164 | return log_link_error_errno(link, r, "Could not append IFLA_VF_TRUST: %m"); | |
165 | } | |
166 | ||
167 | if (sr_iov->link_state >= 0) { | |
168 | struct ifla_vf_link_state ivl = { | |
169 | .vf = sr_iov->vf, | |
170 | .link_state = sr_iov->link_state, | |
171 | }; | |
172 | ||
173 | r = sd_netlink_message_append_data(req, IFLA_VF_LINK_STATE, &ivl, sizeof(struct ifla_vf_link_state)); | |
174 | if (r < 0) | |
175 | return log_link_error_errno(link, r, "Could not append IFLA_VF_LINK_STATE: %m"); | |
176 | } | |
177 | ||
178 | if (sr_iov->vlan > 0) { | |
179 | /* Because of padding, first the buffer must be initialized with 0. */ | |
180 | struct ifla_vf_vlan_info ivvi = {}; | |
181 | ivvi.vf = sr_iov->vf; | |
182 | ivvi.vlan = sr_iov->vlan; | |
183 | ivvi.qos = sr_iov->qos; | |
e64b31c8 | 184 | ivvi.vlan_proto = htobe16(sr_iov->vlan_proto); |
518cd6b5 SS |
185 | |
186 | r = sd_netlink_message_open_container(req, IFLA_VF_VLAN_LIST); | |
187 | if (r < 0) | |
188 | return log_link_error_errno(link, r, "Could not open IFLA_VF_VLAN_LIST container: %m"); | |
189 | ||
190 | r = sd_netlink_message_append_data(req, IFLA_VF_VLAN_INFO, &ivvi, sizeof(struct ifla_vf_vlan_info)); | |
191 | if (r < 0) | |
192 | return log_link_error_errno(link, r, "Could not append IFLA_VF_VLAN_INFO: %m"); | |
193 | ||
194 | r = sd_netlink_message_close_container(req); | |
195 | if (r < 0) | |
196 | return log_link_error_errno(link, r, "Could not close IFLA_VF_VLAN_LIST container: %m"); | |
197 | } | |
198 | ||
199 | r = sd_netlink_message_close_container(req); | |
200 | if (r < 0) | |
201 | return log_link_error_errno(link, r, "Could not close IFLA_VF_INFO container: %m"); | |
202 | ||
203 | r = sd_netlink_message_close_container(req); | |
204 | if (r < 0) | |
205 | return log_link_error_errno(link, r, "Could not close IFLA_VFINFO_LIST container: %m"); | |
206 | ||
207 | r = netlink_call_async(link->manager->rtnl, NULL, req, sr_iov_handler, | |
208 | link_netlink_destroy_callback, link); | |
209 | if (r < 0) | |
210 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
211 | ||
212 | link_ref(link); | |
213 | link->sr_iov_messages++; | |
214 | ||
215 | return 0; | |
216 | } | |
217 | ||
218 | int sr_iov_section_verify(SRIOV *sr_iov) { | |
219 | assert(sr_iov); | |
220 | ||
221 | if (section_is_invalid(sr_iov->section)) | |
222 | return -EINVAL; | |
223 | ||
224 | if (sr_iov->vf == (uint32_t) -1) | |
225 | return log_warning_errno(SYNTHETIC_ERRNO(EINVAL), | |
226 | "%s: [SRIOV] section without VirtualFunction= field configured. " | |
227 | "Ignoring [SRIOV] section from line %u.", | |
228 | sr_iov->section->filename, sr_iov->section->line); | |
229 | ||
230 | return 0; | |
231 | } | |
232 | ||
233 | int config_parse_sr_iov_uint32( | |
234 | const char *unit, | |
235 | const char *filename, | |
236 | unsigned line, | |
237 | const char *section, | |
238 | unsigned section_line, | |
239 | const char *lvalue, | |
240 | int ltype, | |
241 | const char *rvalue, | |
242 | void *data, | |
243 | void *userdata) { | |
244 | ||
245 | _cleanup_(sr_iov_free_or_set_invalidp) SRIOV *sr_iov = NULL; | |
246 | Network *network = data; | |
247 | uint32_t k; | |
248 | int r; | |
249 | ||
250 | assert(filename); | |
251 | assert(lvalue); | |
252 | assert(rvalue); | |
253 | assert(data); | |
254 | ||
255 | r = sr_iov_new_static(network, filename, section_line, &sr_iov); | |
256 | if (r < 0) | |
257 | return r; | |
258 | ||
259 | if (isempty(rvalue)) { | |
260 | if (streq(lvalue, "VirtualFunction")) | |
261 | sr_iov->vf = (uint32_t) -1; | |
262 | else if (streq(lvalue, "VLANId")) | |
263 | sr_iov->vlan = 0; | |
264 | else if (streq(lvalue, "QualityOfService")) | |
265 | sr_iov->qos = 0; | |
266 | else | |
267 | assert_not_reached("Invalid lvalue"); | |
268 | ||
269 | TAKE_PTR(sr_iov); | |
270 | return 0; | |
271 | } | |
272 | ||
273 | r = safe_atou32(rvalue, &k); | |
274 | if (r < 0) { | |
275 | log_syntax(unit, LOG_ERR, filename, line, r, | |
276 | "Failed to parse SR-IOV '%s=', ignoring assignment: %s", lvalue, rvalue); | |
277 | return 0; | |
278 | } | |
279 | ||
280 | if (streq(lvalue, "VLANId")) { | |
281 | if (k == 0 || k > 4095) { | |
282 | log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid SR-IOV VLANId: %d", k); | |
283 | return 0; | |
284 | } | |
285 | sr_iov->vlan = k; | |
286 | } else if (streq(lvalue, "VirtualFunction")) { | |
287 | if (k >= INT_MAX) { | |
288 | log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid SR-IOV virtual function: %d", k); | |
289 | return 0; | |
290 | } | |
291 | sr_iov->vf = k; | |
292 | } else if (streq(lvalue, "QualityOfService")) | |
293 | sr_iov->qos = k; | |
294 | else | |
295 | assert_not_reached("Invalid lvalue"); | |
296 | ||
297 | TAKE_PTR(sr_iov); | |
298 | return 0; | |
299 | } | |
300 | ||
e64b31c8 YW |
301 | int config_parse_sr_iov_vlan_proto( |
302 | const char *unit, | |
303 | const char *filename, | |
304 | unsigned line, | |
305 | const char *section, | |
306 | unsigned section_line, | |
307 | const char *lvalue, | |
308 | int ltype, | |
309 | const char *rvalue, | |
310 | void *data, | |
311 | void *userdata) { | |
312 | ||
313 | _cleanup_(sr_iov_free_or_set_invalidp) SRIOV *sr_iov = NULL; | |
314 | Network *network = data; | |
315 | int r; | |
316 | ||
317 | assert(filename); | |
318 | assert(lvalue); | |
319 | assert(rvalue); | |
320 | assert(data); | |
321 | ||
322 | r = sr_iov_new_static(network, filename, section_line, &sr_iov); | |
323 | if (r < 0) | |
324 | return r; | |
325 | ||
326 | if (isempty(rvalue) || streq(rvalue, "802.1Q")) | |
327 | sr_iov->vlan_proto = ETH_P_8021Q; | |
328 | else if (streq(rvalue, "802.1ad")) | |
329 | sr_iov->vlan_proto = ETH_P_8021AD; | |
330 | else { | |
331 | log_syntax(unit, LOG_ERR, filename, line, 0, | |
332 | "Invalid SR-IOV '%s=', ignoring assignment: %s", lvalue, rvalue); | |
333 | return 0; | |
334 | } | |
335 | ||
336 | TAKE_PTR(sr_iov); | |
337 | return 0; | |
338 | } | |
339 | ||
518cd6b5 SS |
340 | int config_parse_sr_iov_link_state( |
341 | const char *unit, | |
342 | const char *filename, | |
343 | unsigned line, | |
344 | const char *section, | |
345 | unsigned section_line, | |
346 | const char *lvalue, | |
347 | int ltype, | |
348 | const char *rvalue, | |
349 | void *data, | |
350 | void *userdata) { | |
351 | ||
352 | _cleanup_(sr_iov_free_or_set_invalidp) SRIOV *sr_iov = NULL; | |
353 | Network *network = data; | |
354 | int r; | |
355 | ||
356 | assert(filename); | |
357 | assert(lvalue); | |
358 | assert(rvalue); | |
359 | assert(data); | |
360 | ||
361 | r = sr_iov_new_static(network, filename, section_line, &sr_iov); | |
362 | if (r < 0) | |
363 | return r; | |
364 | ||
365 | /* Unfortunately, SR_IOV_LINK_STATE_DISABLE is 2, not 0. So, we cannot use | |
366 | * DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN() macro. */ | |
367 | ||
368 | if (isempty(rvalue)) { | |
369 | sr_iov->link_state = _SR_IOV_LINK_STATE_INVALID; | |
370 | TAKE_PTR(sr_iov); | |
371 | return 0; | |
372 | } | |
373 | ||
374 | if (streq(rvalue, "auto")) { | |
375 | sr_iov->link_state = SR_IOV_LINK_STATE_AUTO; | |
376 | TAKE_PTR(sr_iov); | |
377 | return 0; | |
378 | } | |
379 | ||
380 | r = parse_boolean(rvalue); | |
381 | if (r < 0) { | |
382 | log_syntax(unit, LOG_ERR, filename, line, r, | |
383 | "Failed to parse SR-IOV '%s=', ignoring assignment: %s", lvalue, rvalue); | |
384 | return 0; | |
385 | } | |
386 | ||
387 | sr_iov->link_state = r ? SR_IOV_LINK_STATE_ENABLE : SR_IOV_LINK_STATE_DISABLE; | |
388 | TAKE_PTR(sr_iov); | |
389 | return 0; | |
390 | } | |
391 | ||
392 | int config_parse_sr_iov_boolean( | |
393 | const char *unit, | |
394 | const char *filename, | |
395 | unsigned line, | |
396 | const char *section, | |
397 | unsigned section_line, | |
398 | const char *lvalue, | |
399 | int ltype, | |
400 | const char *rvalue, | |
401 | void *data, | |
402 | void *userdata) { | |
403 | ||
404 | _cleanup_(sr_iov_free_or_set_invalidp) SRIOV *sr_iov = NULL; | |
405 | Network *network = data; | |
406 | int r; | |
407 | ||
408 | assert(filename); | |
409 | assert(lvalue); | |
410 | assert(rvalue); | |
411 | assert(data); | |
412 | ||
413 | r = sr_iov_new_static(network, filename, section_line, &sr_iov); | |
414 | if (r < 0) | |
415 | return r; | |
416 | ||
417 | if (isempty(rvalue)) { | |
418 | if (streq(lvalue, "MACSpoofCheck")) | |
419 | sr_iov->vf_spoof_check_setting = -1; | |
420 | else if (streq(lvalue, "QueryReceiveSideScaling")) | |
421 | sr_iov->query_rss = -1; | |
422 | else if (streq(lvalue, "Trust")) | |
423 | sr_iov->trust = -1; | |
424 | else | |
425 | assert_not_reached("Invalid lvalue"); | |
426 | ||
427 | TAKE_PTR(sr_iov); | |
428 | return 0; | |
429 | } | |
430 | ||
431 | r = parse_boolean(rvalue); | |
432 | if (r < 0) { | |
433 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse '%s=', ignoring: %s", lvalue, rvalue); | |
434 | return 0; | |
435 | } | |
436 | ||
437 | if (streq(lvalue, "MACSpoofCheck")) | |
438 | sr_iov->vf_spoof_check_setting = r; | |
439 | else if (streq(lvalue, "QueryReceiveSideScaling")) | |
440 | sr_iov->query_rss = r; | |
441 | else if (streq(lvalue, "Trust")) | |
442 | sr_iov->trust = r; | |
443 | else | |
444 | assert_not_reached("Invalid lvalue"); | |
445 | ||
446 | TAKE_PTR(sr_iov); | |
447 | return 0; | |
448 | } |