Line data Source code
1 : /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2 :
3 : /***
4 : This file is part of systemd.
5 :
6 : Copyright 2014 Tom Gundersen <teg@jklm.no>
7 : Copyright 2014 Susant Sahani
8 :
9 : systemd is free software; you can redistribute it and/or modify it
10 : under the terms of the GNU Lesser General Public License as published by
11 : the Free Software Foundation; either version 2.1 of the License, or
12 : (at your option) any later version.
13 :
14 : systemd is distributed in the hope that it will be useful, but
15 : WITHOUT ANY WARRANTY; without even the implied warranty of
16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 : Lesser General Public License for more details.
18 :
19 : You should have received a copy of the GNU Lesser General Public License
20 : along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 : ***/
22 :
23 : #include <netinet/ether.h>
24 : #include <linux/if_bonding.h>
25 :
26 : #include "conf-parser.h"
27 : #include "sd-netlink.h"
28 : #include "networkd-netdev-bond.h"
29 : #include "missing.h"
30 :
31 : /*
32 : * Number of seconds between instances where the bonding
33 : * driver sends learning packets to each slaves peer switch
34 : */
35 : #define LEARNING_PACKETS_INTERVAL_MIN_SEC (1 * USEC_PER_SEC)
36 : #define LEARNING_PACKETS_INTERVAL_MAX_SEC (0x7fffffff * USEC_PER_SEC)
37 :
38 : /* Number of IGMP membership reports to be issued after
39 : * a failover event.
40 : */
41 : #define RESEND_IGMP_MIN 0
42 : #define RESEND_IGMP_MAX 255
43 : #define RESEND_IGMP_DEFAULT 1
44 :
45 : /*
46 : * Number of packets to transmit through a slave before
47 : * moving to the next one.
48 : */
49 : #define PACKETS_PER_SLAVE_MIN 0
50 : #define PACKETS_PER_SLAVE_MAX 65535
51 : #define PACKETS_PER_SLAVE_DEFAULT 1
52 :
53 : /*
54 : * Number of peer notifications (gratuitous ARPs and
55 : * unsolicited IPv6 Neighbor Advertisements) to be issued after a
56 : * failover event.
57 : */
58 : #define GRATUITOUS_ARP_MIN 0
59 : #define GRATUITOUS_ARP_MAX 255
60 : #define GRATUITOUS_ARP_DEFAULT 1
61 :
62 : static const char* const bond_mode_table[_NETDEV_BOND_MODE_MAX] = {
63 : [NETDEV_BOND_MODE_BALANCE_RR] = "balance-rr",
64 : [NETDEV_BOND_MODE_ACTIVE_BACKUP] = "active-backup",
65 : [NETDEV_BOND_MODE_BALANCE_XOR] = "balance-xor",
66 : [NETDEV_BOND_MODE_BROADCAST] = "broadcast",
67 : [NETDEV_BOND_MODE_802_3AD] = "802.3ad",
68 : [NETDEV_BOND_MODE_BALANCE_TLB] = "balance-tlb",
69 : [NETDEV_BOND_MODE_BALANCE_ALB] = "balance-alb",
70 : };
71 :
72 18 : DEFINE_STRING_TABLE_LOOKUP(bond_mode, BondMode);
73 0 : DEFINE_CONFIG_PARSE_ENUM(config_parse_bond_mode, bond_mode, BondMode, "Failed to parse bond mode");
74 :
75 : static const char* const bond_xmit_hash_policy_table[_NETDEV_BOND_XMIT_HASH_POLICY_MAX] = {
76 : [NETDEV_BOND_XMIT_HASH_POLICY_LAYER2] = "layer2",
77 : [NETDEV_BOND_XMIT_HASH_POLICY_LAYER34] = "layer3+4",
78 : [NETDEV_BOND_XMIT_HASH_POLICY_LAYER23] = "layer2+3",
79 : [NETDEV_BOND_XMIT_HASH_POLICY_ENCAP23] = "encap2+3",
80 : [NETDEV_BOND_XMIT_HASH_POLICY_ENCAP34] = "encap3+4",
81 : };
82 :
83 0 : DEFINE_STRING_TABLE_LOOKUP(bond_xmit_hash_policy, BondXmitHashPolicy);
84 0 : DEFINE_CONFIG_PARSE_ENUM(config_parse_bond_xmit_hash_policy,
85 : bond_xmit_hash_policy,
86 : BondXmitHashPolicy,
87 : "Failed to parse bond transmit hash policy")
88 :
89 : static const char* const bond_lacp_rate_table[_NETDEV_BOND_LACP_RATE_MAX] = {
90 : [NETDEV_BOND_LACP_RATE_SLOW] = "slow",
91 : [NETDEV_BOND_LACP_RATE_FAST] = "fast",
92 : };
93 :
94 0 : DEFINE_STRING_TABLE_LOOKUP(bond_lacp_rate, BondLacpRate);
95 0 : DEFINE_CONFIG_PARSE_ENUM(config_parse_bond_lacp_rate, bond_lacp_rate, BondLacpRate, "Failed to parse bond lacp rate")
96 :
97 : static const char* const bond_ad_select_table[_NETDEV_BOND_AD_SELECT_MAX] = {
98 : [NETDEV_BOND_AD_SELECT_STABLE] = "stable",
99 : [NETDEV_BOND_AD_SELECT_BANDWIDTH] = "bandwidth",
100 : [NETDEV_BOND_AD_SELECT_COUNT] = "count",
101 : };
102 :
103 0 : DEFINE_STRING_TABLE_LOOKUP(bond_ad_select, BondAdSelect);
104 0 : DEFINE_CONFIG_PARSE_ENUM(config_parse_bond_ad_select, bond_ad_select, BondAdSelect, "Failed to parse bond AD select");
105 :
106 : static const char* const bond_fail_over_mac_table[_NETDEV_BOND_FAIL_OVER_MAC_MAX] = {
107 : [NETDEV_BOND_FAIL_OVER_MAC_NONE] = "none",
108 : [NETDEV_BOND_FAIL_OVER_MAC_ACTIVE] = "active",
109 : [NETDEV_BOND_FAIL_OVER_MAC_FOLLOW] = "follow",
110 : };
111 :
112 0 : DEFINE_STRING_TABLE_LOOKUP(bond_fail_over_mac, BondFailOverMac);
113 0 : DEFINE_CONFIG_PARSE_ENUM(config_parse_bond_fail_over_mac, bond_fail_over_mac, BondFailOverMac, "Failed to parse bond fail over MAC");
114 :
115 : static const char *const bond_arp_validate_table[_NETDEV_BOND_ARP_VALIDATE_MAX] = {
116 : [NETDEV_BOND_ARP_VALIDATE_NONE] = "none",
117 : [NETDEV_BOND_ARP_VALIDATE_ACTIVE]= "active",
118 : [NETDEV_BOND_ARP_VALIDATE_BACKUP]= "backup",
119 : [NETDEV_BOND_ARP_VALIDATE_ALL]= "all",
120 : };
121 :
122 0 : DEFINE_STRING_TABLE_LOOKUP(bond_arp_validate, BondArpValidate);
123 0 : DEFINE_CONFIG_PARSE_ENUM(config_parse_bond_arp_validate, bond_arp_validate, BondArpValidate, "Failed to parse bond arp validate");
124 :
125 : static const char *const bond_arp_all_targets_table[_NETDEV_BOND_ARP_ALL_TARGETS_MAX] = {
126 : [NETDEV_BOND_ARP_ALL_TARGETS_ANY] = "any",
127 : [NETDEV_BOND_ARP_ALL_TARGETS_ALL] = "all",
128 : };
129 :
130 0 : DEFINE_STRING_TABLE_LOOKUP(bond_arp_all_targets, BondArpAllTargets);
131 0 : DEFINE_CONFIG_PARSE_ENUM(config_parse_bond_arp_all_targets, bond_arp_all_targets, BondArpAllTargets, "Failed to parse bond Arp all targets");
132 :
133 : static const char *bond_primary_reselect_table[_NETDEV_BOND_PRIMARY_RESELECT_MAX] = {
134 : [NETDEV_BOND_PRIMARY_RESELECT_ALWAYS] = "always",
135 : [NETDEV_BOND_PRIMARY_RESELECT_BETTER]= "better",
136 : [NETDEV_BOND_PRIMARY_RESELECT_FAILURE]= "failure",
137 : };
138 :
139 0 : DEFINE_STRING_TABLE_LOOKUP(bond_primary_reselect, BondPrimaryReselect);
140 0 : DEFINE_CONFIG_PARSE_ENUM(config_parse_bond_primary_reselect, bond_primary_reselect, BondPrimaryReselect, "Failed to parse bond primary reselect");
141 :
142 0 : static uint8_t bond_mode_to_kernel(BondMode mode) {
143 0 : switch (mode) {
144 : case NETDEV_BOND_MODE_BALANCE_RR:
145 0 : return BOND_MODE_ROUNDROBIN;
146 : case NETDEV_BOND_MODE_ACTIVE_BACKUP:
147 0 : return BOND_MODE_ACTIVEBACKUP;
148 : case NETDEV_BOND_MODE_BALANCE_XOR:
149 0 : return BOND_MODE_XOR;
150 : case NETDEV_BOND_MODE_BROADCAST:
151 0 : return BOND_MODE_BROADCAST;
152 : case NETDEV_BOND_MODE_802_3AD:
153 0 : return BOND_MODE_8023AD;
154 : case NETDEV_BOND_MODE_BALANCE_TLB:
155 0 : return BOND_MODE_TLB;
156 : case NETDEV_BOND_MODE_BALANCE_ALB:
157 0 : return BOND_MODE_ALB;
158 : default:
159 0 : return (uint8_t) -1;
160 : }
161 : }
162 :
163 0 : static uint8_t bond_xmit_hash_policy_to_kernel(BondXmitHashPolicy policy) {
164 0 : switch (policy) {
165 : case NETDEV_BOND_XMIT_HASH_POLICY_LAYER2:
166 0 : return BOND_XMIT_POLICY_LAYER2;
167 : case NETDEV_BOND_XMIT_HASH_POLICY_LAYER34:
168 0 : return BOND_XMIT_POLICY_LAYER34;
169 : case NETDEV_BOND_XMIT_HASH_POLICY_LAYER23:
170 0 : return BOND_XMIT_POLICY_LAYER23;
171 : case NETDEV_BOND_XMIT_HASH_POLICY_ENCAP23:
172 0 : return BOND_XMIT_POLICY_ENCAP23;
173 : case NETDEV_BOND_XMIT_HASH_POLICY_ENCAP34:
174 0 : return BOND_XMIT_POLICY_ENCAP34;
175 : default:
176 0 : return (uint8_t) -1;
177 : }
178 : }
179 :
180 0 : static int netdev_bond_fill_message_create(NetDev *netdev, Link *link, sd_netlink_message *m) {
181 0 : Bond *b = BOND(netdev);
182 0 : ArpIpTarget *target = NULL;
183 0 : int r, i = 0;
184 :
185 0 : assert(netdev);
186 0 : assert(!link);
187 0 : assert(b);
188 0 : assert(m);
189 :
190 0 : if (b->mode != _NETDEV_BOND_MODE_INVALID) {
191 0 : r = sd_netlink_message_append_u8(m, IFLA_BOND_MODE,
192 0 : bond_mode_to_kernel(b->mode));
193 0 : if (r < 0)
194 0 : return log_netdev_error_errno(netdev, r, "Could not append IFLA_BOND_MODE attribute: %m");
195 : }
196 :
197 0 : if (b->xmit_hash_policy != _NETDEV_BOND_XMIT_HASH_POLICY_INVALID) {
198 0 : r = sd_netlink_message_append_u8(m, IFLA_BOND_XMIT_HASH_POLICY,
199 0 : bond_xmit_hash_policy_to_kernel(b->xmit_hash_policy));
200 0 : if (r < 0)
201 0 : return log_netdev_error_errno(netdev, r, "Could not append IFLA_BOND_XMIT_HASH_POLICY attribute: %m");
202 : }
203 :
204 0 : if (b->lacp_rate != _NETDEV_BOND_LACP_RATE_INVALID &&
205 0 : b->mode == NETDEV_BOND_MODE_802_3AD) {
206 0 : r = sd_netlink_message_append_u8(m, IFLA_BOND_AD_LACP_RATE, b->lacp_rate );
207 0 : if (r < 0) {
208 0 : return log_netdev_error_errno(netdev, r, "Could not append IFLA_BOND_AD_LACP_RATE attribute: %m");
209 : }
210 : }
211 :
212 0 : if (b->miimon != 0) {
213 0 : r = sd_netlink_message_append_u32(m, IFLA_BOND_MIIMON, b->miimon / USEC_PER_MSEC);
214 0 : if (r < 0)
215 0 : log_netdev_error_errno(netdev, r, "Could not append IFLA_BOND_BOND_MIIMON attribute: %m");
216 : }
217 :
218 0 : if (b->downdelay != 0) {
219 0 : r = sd_netlink_message_append_u32(m, IFLA_BOND_DOWNDELAY, b->downdelay / USEC_PER_MSEC);
220 0 : if (r < 0)
221 0 : return log_netdev_error_errno(netdev, r, "Could not append IFLA_BOND_DOWNDELAY attribute: %m");
222 : }
223 :
224 0 : if (b->updelay != 0) {
225 0 : r = sd_netlink_message_append_u32(m, IFLA_BOND_UPDELAY, b->updelay / USEC_PER_MSEC);
226 0 : if (r < 0)
227 0 : return log_netdev_error_errno(netdev, r, "Could not append IFLA_BOND_UPDELAY attribute: %m");
228 : }
229 :
230 0 : if (b->arp_interval != 0) {
231 0 : r = sd_netlink_message_append_u32(m, IFLA_BOND_ARP_INTERVAL, b->arp_interval / USEC_PER_MSEC);
232 0 : if (r < 0)
233 0 : return log_netdev_error_errno(netdev, r, "Could not append IFLA_BOND_ARP_INTERVAL attribute: %m");
234 :
235 0 : if ((b->lp_interval >= LEARNING_PACKETS_INTERVAL_MIN_SEC) &&
236 0 : (b->lp_interval <= LEARNING_PACKETS_INTERVAL_MAX_SEC)) {
237 0 : r = sd_netlink_message_append_u32(m, IFLA_BOND_LP_INTERVAL, b->lp_interval / USEC_PER_SEC);
238 0 : if (r < 0)
239 0 : return log_netdev_error_errno(netdev, r, "Could not append IFLA_BOND_LP_INTERVAL attribute: %m");
240 : }
241 : }
242 :
243 0 : if (b->ad_select != _NETDEV_BOND_AD_SELECT_INVALID &&
244 0 : b->mode == NETDEV_BOND_MODE_802_3AD) {
245 0 : r = sd_netlink_message_append_u8(m, IFLA_BOND_AD_SELECT, b->ad_select);
246 0 : if (r < 0)
247 0 : return log_netdev_error_errno(netdev, r, "Could not append IFLA_BOND_AD_SELECT attribute: %m");
248 : }
249 :
250 0 : if (b->fail_over_mac != _NETDEV_BOND_FAIL_OVER_MAC_INVALID &&
251 0 : b->mode == NETDEV_BOND_MODE_ACTIVE_BACKUP) {
252 0 : r = sd_netlink_message_append_u8(m, IFLA_BOND_FAIL_OVER_MAC, b->fail_over_mac);
253 0 : if (r < 0)
254 0 : return log_netdev_error_errno(netdev, r, "Could not append IFLA_BOND_FAIL_OVER_MAC attribute: %m");
255 : }
256 :
257 0 : if (b->arp_validate != _NETDEV_BOND_ARP_VALIDATE_INVALID) {
258 0 : r = sd_netlink_message_append_u32(m, IFLA_BOND_ARP_VALIDATE, b->arp_validate);
259 0 : if (r < 0)
260 0 : return log_netdev_error_errno(netdev, r, "Could not append IFLA_BOND_ARP_VALIDATE attribute: %m");
261 : }
262 :
263 0 : if (b->arp_all_targets != _NETDEV_BOND_ARP_ALL_TARGETS_INVALID) {
264 0 : r = sd_netlink_message_append_u32(m, IFLA_BOND_ARP_ALL_TARGETS, b->arp_all_targets);
265 0 : if (r < 0)
266 0 : return log_netdev_error_errno(netdev, r, "Could not append IFLA_BOND_ARP_VALIDATE attribute: %m");
267 : }
268 :
269 0 : if (b->primary_reselect != _NETDEV_BOND_PRIMARY_RESELECT_INVALID) {
270 0 : r = sd_netlink_message_append_u32(m, IFLA_BOND_ARP_ALL_TARGETS, b->primary_reselect);
271 0 : if (r < 0)
272 0 : return log_netdev_error_errno(netdev, r, "Could not append IFLA_BOND_ARP_ALL_TARGETS attribute: %m");
273 : }
274 :
275 0 : if (b->resend_igmp <= RESEND_IGMP_MAX) {
276 0 : r = sd_netlink_message_append_u32(m, IFLA_BOND_RESEND_IGMP, b->resend_igmp);
277 0 : if (r < 0)
278 0 : return log_netdev_error_errno(netdev, r, "Could not append IFLA_BOND_RESEND_IGMP attribute: %m");
279 : }
280 :
281 0 : if (b->packets_per_slave <= PACKETS_PER_SLAVE_MAX &&
282 0 : b->mode == NETDEV_BOND_MODE_BALANCE_RR) {
283 0 : r = sd_netlink_message_append_u32(m, IFLA_BOND_PACKETS_PER_SLAVE, b->packets_per_slave);
284 0 : if (r < 0)
285 0 : return log_netdev_error_errno(netdev, r, "Could not append IFLA_BOND_PACKETS_PER_SLAVE attribute: %m");
286 : }
287 :
288 0 : if (b->num_grat_arp <= GRATUITOUS_ARP_MAX) {
289 0 : r = sd_netlink_message_append_u8(m, IFLA_BOND_NUM_PEER_NOTIF, b->num_grat_arp);
290 0 : if (r < 0)
291 0 : return log_netdev_error_errno(netdev, r, "Could not append IFLA_BOND_NUM_PEER_NOTIF attribute: %m");
292 : }
293 :
294 0 : if (b->min_links != 0) {
295 0 : r = sd_netlink_message_append_u32(m, IFLA_BOND_MIN_LINKS, b->min_links);
296 0 : if (r < 0)
297 0 : return log_netdev_error_errno(netdev, r, "Could not append IFLA_BOND_MIN_LINKS attribute: %m");
298 : }
299 :
300 0 : r = sd_netlink_message_append_u8(m, IFLA_BOND_ALL_SLAVES_ACTIVE, b->all_slaves_active);
301 0 : if (r < 0)
302 0 : return log_netdev_error_errno(netdev, r, "Could not append IFLA_BOND_ALL_SLAVES_ACTIVE attribute: %m");
303 :
304 0 : if (b->arp_interval > 0) {
305 0 : if (b->n_arp_ip_targets > 0) {
306 :
307 0 : r = sd_netlink_message_open_container(m, IFLA_BOND_ARP_IP_TARGET);
308 0 : if (r < 0)
309 0 : return log_netdev_error_errno(netdev, r, "Could not open contaniner IFLA_BOND_ARP_IP_TARGET : %m");
310 :
311 0 : LIST_FOREACH(arp_ip_target, target, b->arp_ip_targets) {
312 0 : r = sd_netlink_message_append_u32(m, i++, target->ip.in.s_addr);
313 0 : if (r < 0)
314 0 : return log_netdev_error_errno(netdev, r, "Could not append IFLA_BOND_ARP_ALL_TARGETS attribute: %m");
315 : }
316 :
317 0 : r = sd_netlink_message_close_container(m);
318 0 : if (r < 0)
319 0 : return log_netdev_error_errno(netdev, r, "Could not close contaniner IFLA_BOND_ARP_IP_TARGET : %m");
320 : }
321 : }
322 :
323 0 : return 0;
324 : }
325 :
326 0 : int config_parse_arp_ip_target_address(const char *unit,
327 : const char *filename,
328 : unsigned line,
329 : const char *section,
330 : unsigned section_line,
331 : const char *lvalue,
332 : int ltype,
333 : const char *rvalue,
334 : void *data,
335 : void *userdata) {
336 0 : Bond *b = userdata;
337 : const char *word, *state;
338 : size_t l;
339 : int r;
340 :
341 0 : assert(filename);
342 0 : assert(lvalue);
343 0 : assert(rvalue);
344 0 : assert(data);
345 :
346 0 : FOREACH_WORD_QUOTED(word, l, rvalue, state) {
347 0 : _cleanup_free_ ArpIpTarget *buffer = NULL;
348 0 : _cleanup_free_ char *n = NULL;
349 : int f;
350 :
351 0 : n = strndup(word, l);
352 0 : if (!n)
353 0 : return -ENOMEM;
354 :
355 0 : buffer = new0(ArpIpTarget, 1);
356 0 : if (!buffer)
357 0 : return -ENOMEM;
358 :
359 0 : r = in_addr_from_string_auto(n, &f, &buffer->ip);
360 0 : if (r < 0) {
361 0 : log_syntax(unit, LOG_ERR, filename, line, EINVAL, "Bond ARP ip target address is invalid, ignoring assignment: %s", n);
362 0 : return 0;
363 : }
364 :
365 0 : if (f != AF_INET) {
366 0 : log_syntax(unit, LOG_ERR, filename, line, EINVAL, "Bond ARP ip target address is invalid, ignoring assignment: %s", n);
367 0 : return 0;
368 : }
369 :
370 0 : LIST_PREPEND(arp_ip_target, b->arp_ip_targets, buffer);
371 0 : b->n_arp_ip_targets ++;
372 :
373 0 : buffer = NULL;
374 : }
375 :
376 0 : if (b->n_arp_ip_targets > NETDEV_BOND_ARP_TARGETS_MAX)
377 0 : log_syntax(unit, LOG_WARNING, filename, line, EINVAL, "More than the maximum number of kernel-supported ARP ip targets specified: %d > %d", b->n_arp_ip_targets, NETDEV_BOND_ARP_TARGETS_MAX);
378 :
379 0 : return 0;
380 : }
381 :
382 0 : static void bond_done(NetDev *netdev) {
383 0 : ArpIpTarget *t = NULL, *n = NULL;
384 0 : Bond *b = BOND(netdev);
385 :
386 0 : assert(netdev);
387 0 : assert(b);
388 :
389 0 : LIST_FOREACH_SAFE(arp_ip_target, t, n, b->arp_ip_targets)
390 0 : free(t);
391 :
392 0 : b->arp_ip_targets = NULL;
393 0 : }
394 :
395 0 : static void bond_init(NetDev *netdev) {
396 0 : Bond *b = BOND(netdev);
397 :
398 0 : assert(netdev);
399 0 : assert(b);
400 :
401 0 : b->mode = _NETDEV_BOND_MODE_INVALID;
402 0 : b->xmit_hash_policy = _NETDEV_BOND_XMIT_HASH_POLICY_INVALID;
403 0 : b->lacp_rate = _NETDEV_BOND_LACP_RATE_INVALID;
404 0 : b->ad_select = _NETDEV_BOND_AD_SELECT_INVALID;
405 0 : b->fail_over_mac = _NETDEV_BOND_FAIL_OVER_MAC_INVALID;
406 0 : b->arp_validate = _NETDEV_BOND_ARP_VALIDATE_INVALID;
407 0 : b->arp_all_targets = _NETDEV_BOND_ARP_ALL_TARGETS_INVALID;
408 0 : b->primary_reselect = _NETDEV_BOND_PRIMARY_RESELECT_INVALID;
409 :
410 0 : b->all_slaves_active = false;
411 :
412 0 : b->resend_igmp = RESEND_IGMP_DEFAULT;
413 0 : b->packets_per_slave = PACKETS_PER_SLAVE_DEFAULT;
414 0 : b->num_grat_arp = GRATUITOUS_ARP_DEFAULT;
415 0 : b->lp_interval = LEARNING_PACKETS_INTERVAL_MIN_SEC;
416 :
417 0 : LIST_HEAD_INIT(b->arp_ip_targets);
418 0 : b->n_arp_ip_targets = 0;
419 0 : }
420 :
421 : const NetDevVTable bond_vtable = {
422 : .object_size = sizeof(Bond),
423 : .init = bond_init,
424 : .done = bond_done,
425 : .sections = "Match\0NetDev\0Bond\0",
426 : .fill_message_create = netdev_bond_fill_message_create,
427 : .create_type = NETDEV_CREATE_MASTER,
428 : };
|