• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/netifd/bonding.c

  1 /*
  2  * netifd - network interface daemon
  3  * Copyright (C) 2021 Felix Fietkau <nbd@nbd.name>
  4  *
  5  * This program is free software; you can redistribute it and/or modify
  6  * it under the terms of the GNU General Public License version 2
  7  * as published by the Free Software Foundation
  8  *
  9  * This program is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12  * GNU General Public License for more details.
 13  */
 14 #include <string.h>
 15 #include <stdlib.h>
 16 #include <stdio.h>
 17 
 18 #include "netifd.h"
 19 #include "device.h"
 20 #include "system.h"
 21 
 22 struct bonding_device {
 23         struct device dev;
 24         device_state_cb set_state;
 25 
 26         struct blob_attr *port_list;
 27         struct vlist_tree ports;
 28         int n_present;
 29         int n_failed;
 30 
 31         struct bonding_port *primary_port;
 32         struct uloop_timeout retry;
 33 
 34         struct bonding_config config;
 35         struct blob_attr *config_data;
 36         bool has_macaddr;
 37         bool force_active;
 38         bool reset_primary;
 39         bool active;
 40 };
 41 
 42 struct bonding_port {
 43         struct vlist_node node;
 44         struct bonding_device *bdev;
 45         struct device_user dev;
 46         bool set_primary;
 47         bool present;
 48         bool active;
 49         char name[];
 50 };
 51 
 52 enum {
 53         BOND_ATTR_PORTS,
 54 
 55         BOND_ATTR_POLICY,
 56         BOND_ATTR_XMIT_HASH_POLICY,
 57         BOND_ATTR_ALL_PORTS_ACTIVE,
 58 
 59         BOND_ATTR_MIN_LINKS,
 60         BOND_ATTR_AD_ACTOR_SYSTEM,
 61         BOND_ATTR_AD_ACTOR_SYS_PRIO,
 62         BOND_ATTR_AD_SELECT,
 63         BOND_ATTR_LACP_RATE,
 64 
 65         BOND_ATTR_PACKETS_PER_PORT,
 66         BOND_ATTR_LP_INTERVAL,
 67         BOND_ATTR_DYNAMIC_LB,
 68         BOND_ATTR_RESEND_IGMP,
 69 
 70         BOND_ATTR_NUM_PEER_NOTIF,
 71         BOND_ATTR_PRIMARY,
 72         BOND_ATTR_PRIMARY_RESELECT,
 73         BOND_ATTR_FAILOVER_MAC,
 74 
 75         BOND_ATTR_MON_MODE,
 76         BOND_ATTR_MON_INTERVAL,
 77         BOND_ATTR_ARP_TARGET,
 78         BOND_ATTR_ARP_ALL_TARGETS,
 79         BOND_ATTR_ARP_VALIDATE,
 80         BOND_ATTR_USE_CARRIER,
 81         BOND_ATTR_UPDELAY,
 82         BOND_ATTR_DOWNDELAY,
 83 
 84         __BOND_ATTR_MAX,
 85 };
 86 
 87 static const struct blobmsg_policy bonding_attrs[__BOND_ATTR_MAX] = {
 88         [BOND_ATTR_PORTS] = { "ports", BLOBMSG_TYPE_ARRAY },
 89         [BOND_ATTR_POLICY] = { "policy", BLOBMSG_TYPE_STRING },
 90         [BOND_ATTR_XMIT_HASH_POLICY] = { "xmit_hash_policy", BLOBMSG_TYPE_STRING },
 91         [BOND_ATTR_ALL_PORTS_ACTIVE] = { "all_ports_active", BLOBMSG_TYPE_BOOL },
 92         [BOND_ATTR_MIN_LINKS] = { "min_links", BLOBMSG_TYPE_INT32 },
 93         [BOND_ATTR_AD_ACTOR_SYSTEM] = { "ad_actor_system", BLOBMSG_TYPE_STRING },
 94         [BOND_ATTR_AD_ACTOR_SYS_PRIO] = { "ad_actor_sys_prio", BLOBMSG_TYPE_INT32 },
 95         [BOND_ATTR_AD_SELECT] = { "ad_select", BLOBMSG_TYPE_STRING },
 96         [BOND_ATTR_LACP_RATE] = { "lacp_rate", BLOBMSG_TYPE_STRING },
 97         [BOND_ATTR_PACKETS_PER_PORT] = { "packets_per_port", BLOBMSG_TYPE_INT32 },
 98         [BOND_ATTR_LP_INTERVAL] = { "lp_interval", BLOBMSG_TYPE_INT32 },
 99         [BOND_ATTR_DYNAMIC_LB] = { "dynamic_lb", BLOBMSG_TYPE_BOOL },
100         [BOND_ATTR_RESEND_IGMP] = { "resend_igmp", BLOBMSG_TYPE_INT32 },
101         [BOND_ATTR_NUM_PEER_NOTIF] = { "num_peer_notif", BLOBMSG_TYPE_INT32 },
102         [BOND_ATTR_PRIMARY] = { "primary", BLOBMSG_TYPE_STRING },
103         [BOND_ATTR_PRIMARY_RESELECT] = { "primary_reselect", BLOBMSG_TYPE_STRING },
104         [BOND_ATTR_FAILOVER_MAC] = { "failover_mac", BLOBMSG_TYPE_STRING },
105         [BOND_ATTR_MON_MODE] = { "monitor_mode", BLOBMSG_TYPE_STRING },
106         [BOND_ATTR_MON_INTERVAL] = { "monitor_interval", BLOBMSG_TYPE_INT32 },
107         [BOND_ATTR_ARP_TARGET] = { "arp_target", BLOBMSG_TYPE_ARRAY },
108         [BOND_ATTR_ARP_ALL_TARGETS] = { "arp_all_targets", BLOBMSG_TYPE_BOOL },
109         [BOND_ATTR_ARP_VALIDATE] = { "arp_validate", BLOBMSG_TYPE_STRING },
110         [BOND_ATTR_USE_CARRIER] = { "use_carrier", BLOBMSG_TYPE_BOOL },
111         [BOND_ATTR_UPDELAY] = { "updelay", BLOBMSG_TYPE_INT32 },
112         [BOND_ATTR_DOWNDELAY] = { "downdelay", BLOBMSG_TYPE_INT32 },
113 };
114 
115 static const struct uci_blob_param_info bonding_attr_info[__BOND_ATTR_MAX] = {
116         [BOND_ATTR_PORTS] = { .type = BLOBMSG_TYPE_STRING },
117         [BOND_ATTR_ARP_TARGET] = { .type = BLOBMSG_TYPE_STRING },
118 };
119 
120 static const struct uci_blob_param_list bonding_attr_list = {
121         .n_params = __BOND_ATTR_MAX,
122         .params = bonding_attrs,
123         .info = bonding_attr_info,
124 
125         .n_next = 1,
126         .next = { &device_attr_list },
127 };
128 
129 static void
130 bonding_reset_primary(struct bonding_device *bdev)
131 {
132         struct bonding_port *bp;
133 
134         bdev->primary_port = NULL;
135         if (!bdev->has_macaddr)
136                 bdev->dev.settings.flags &= ~DEV_OPT_MACADDR;
137 
138         vlist_for_each_element(&bdev->ports, bp, node) {
139                 uint8_t *macaddr;
140 
141                 if (!bp->present)
142                         continue;
143 
144                 if (bdev->primary_port && !bp->set_primary)
145                         continue;
146 
147                 bdev->primary_port = bp;
148                 if (bdev->has_macaddr)
149                         continue;
150 
151                 if (bp->dev.dev->settings.flags & DEV_OPT_MACADDR)
152                         macaddr = bp->dev.dev->settings.macaddr;
153                 else
154                         macaddr = bp->dev.dev->orig_settings.macaddr;
155                 memcpy(bdev->dev.settings.macaddr, macaddr, 6);
156                 bdev->dev.settings.flags |= DEV_OPT_MACADDR;
157         }
158 }
159 
160 static int
161 bonding_disable_port(struct bonding_port *bp, bool keep_dev)
162 {
163         struct bonding_device *bdev = bp->bdev;
164 
165         if (!bp->present || !bp->active)
166                 return 0;
167 
168         bp->active = false;
169 
170         system_bonding_set_port(&bdev->dev, bp->dev.dev, false, bp->set_primary);
171         if (!keep_dev)
172                 device_release(&bp->dev);
173 
174         if (bp->dev.dev->settings.flags & DEV_OPT_IPV6) {
175                 bp->dev.dev->settings.ipv6 = 1;
176                 bp->dev.dev->settings.flags &= ~DEV_OPT_IPV6;
177         }
178 
179         return 0;
180 }
181 
182 static void
183 bonding_remove_port(struct bonding_port *bp)
184 {
185         struct bonding_device *bdev = bp->bdev;
186 
187         if (!bp->present)
188                 return;
189 
190         if (bdev->dev.active)
191                 bonding_disable_port(bp, false);
192 
193         bp->present = false;
194         bp->bdev->n_present--;
195 
196         if (bp == bdev->primary_port)
197                 bonding_reset_primary(bdev);
198 
199         bdev->force_active = false;
200         if (bdev->n_present == 0)
201                 device_set_present(&bdev->dev, false);
202 }
203 
204 static int
205 bonding_set_active(struct bonding_device *bdev, bool active)
206 {
207         int ret;
208 
209         if (bdev->active == active)
210                 return 0;
211 
212         ret = system_bonding_set_device(&bdev->dev, active ? &bdev->config : NULL);
213         if (ret < 0)
214                 return ret;
215 
216         bdev->active = active;
217         return 0;
218 }
219 
220 static int
221 bonding_enable_port(struct bonding_port *bp)
222 {
223         struct bonding_device *bdev = bp->bdev;
224         struct device *dev;
225         int ret;
226 
227         if (!bp->present)
228                 return 0;
229 
230         /* Disable IPv6 for bonding ports */
231         if (!(bp->dev.dev->settings.flags & DEV_OPT_IPV6)) {
232                 bp->dev.dev->settings.ipv6 = 0;
233                 bp->dev.dev->settings.flags |= DEV_OPT_IPV6;
234         }
235 
236         ret = device_claim(&bp->dev);
237         if (ret < 0)
238                 return ret;
239 
240         ret = bonding_set_active(bdev, true);
241         if (ret)
242                 goto release;
243 
244         dev = bp->dev.dev;
245         if (dev->settings.auth && !dev->auth_status)
246                 return -1;
247 
248         if (bp->active)
249                 return 0;
250 
251         ret = system_bonding_set_port(&bdev->dev, bp->dev.dev, true, bp->set_primary);
252         if (ret < 0) {
253                 D(DEVICE, "Bonding port %s could not be added", bp->dev.dev->ifname);
254                 goto error;
255         }
256 
257         bp->active = true;
258         device_set_present(&bdev->dev, true);
259 
260         return 0;
261 
262 error:
263         bdev->n_failed++;
264         bp->present = false;
265         bdev->n_present--;
266 release:
267         device_release(&bp->dev);
268 
269         return ret;
270 }
271 
272 static void
273 bonding_port_cb(struct device_user *dep, enum device_event ev)
274 {
275         struct bonding_port *bp = container_of(dep, struct bonding_port, dev);
276         struct bonding_device *bdev = bp->bdev;
277         struct device *dev = dep->dev;
278 
279         switch (ev) {
280         case DEV_EVENT_ADD:
281                 if (bp->present)
282                         break;
283 
284                 bp->present = true;
285                 bdev->n_present++;
286 
287                 if (bdev->n_present == 1)
288                         device_set_present(&bdev->dev, true);
289                 fallthrough;
290         case DEV_EVENT_AUTH_UP:
291                 if (!bdev->dev.active)
292                         break;
293 
294                 if (bonding_enable_port(bp))
295                         break;
296 
297                 /*
298                  * Adding a bonding port can overwrite the bonding device mtu
299                  * in the kernel, apply the bonding settings in case the
300                  * bonding device mtu is set
301                  */
302                 system_if_apply_settings(&bdev->dev, &bdev->dev.settings,
303                                          DEV_OPT_MTU | DEV_OPT_MTU6);
304                 break;
305         case DEV_EVENT_LINK_DOWN:
306                 if (!dev->settings.auth)
307                         break;
308 
309                 bonding_disable_port(bp, true);
310                 break;
311         case DEV_EVENT_REMOVE:
312                 if (dep->hotplug && !dev->sys_present) {
313                         vlist_delete(&bdev->ports, &bp->node);
314                         return;
315                 }
316 
317                 if (bp->present)
318                         bonding_remove_port(bp);
319 
320                 break;
321         default:
322                 return;
323         }
324 }
325 
326 static struct bonding_port *
327 bonding_create_port(struct bonding_device *bdev, const char *name,
328                     struct device *dev, bool hotplug)
329 {
330         struct bonding_port *bp;
331         struct bonding_config *cfg = &bdev->config;
332 
333         bp = calloc(1, sizeof(*bp) + strlen(name) + 1);
334         if (!bp)
335                 return NULL;
336 
337         bp->bdev = bdev;
338         bp->dev.cb = bonding_port_cb;
339         bp->dev.hotplug = hotplug;
340         strcpy(bp->name, name);
341         bp->dev.dev = dev;
342 
343         if (cfg->primary != NULL) {
344                 bp->set_primary = strcmp(cfg->primary, name) == 0;
345         }
346 
347         vlist_add(&bdev->ports, &bp->node, bp->name);
348         /*
349          * Need to look up the bonding port again as the above
350          * created pointer will be freed in case the bonding port
351          * already existed
352          */
353         if (!hotplug)
354                 return bp;
355 
356         bp = vlist_find(&bdev->ports, name, bp, node);
357         if (bp)
358                 bp->node.version = -1;
359 
360         return bp;
361 }
362 
363 static void
364 bonding_config_init(struct device *dev)
365 {
366         struct bonding_device *bdev;
367         struct blob_attr *cur;
368         size_t rem;
369 
370         bdev = container_of(dev, struct bonding_device, dev);
371 
372         bdev->n_failed = 0;
373 
374         vlist_update(&bdev->ports);
375         blobmsg_for_each_attr(cur, bdev->port_list, rem) {
376                 const char *name = blobmsg_get_string(cur);
377 
378                 dev = device_get(name, true);
379                 if (!dev)
380                         continue;
381 
382                 bonding_create_port(bdev, name, dev, false);
383         }
384         vlist_flush(&bdev->ports);
385 
386         if (bdev->reset_primary) {
387                 bonding_reset_primary(bdev);
388                 bdev->reset_primary = false;
389         }
390 
391         if (bdev->n_failed)
392                 uloop_timeout_set(&bdev->retry, 100);
393 }
394 
395 static void
396 bonding_apply_settings(struct bonding_device *bdev, struct blob_attr **tb)
397 {
398         struct bonding_config *cfg = &bdev->config;
399         struct blob_attr *cur;
400 
401         /* defaults */
402         memset(cfg, 0, sizeof(*cfg));
403         cfg->resend_igmp = 1;
404         cfg->ad_actor_sys_prio = 65535;
405         cfg->lp_interval = 1;
406         cfg->num_peer_notif = 1;
407 
408 #define cfg_item(_type, _field, _attr)                          \
409         do {                                                    \
410                 if ((cur = tb[BOND_ATTR_##_attr]) != NULL)      \
411                         cfg->_field = blobmsg_get_##_type(cur); \
412         } while (0)
413 
414         if ((cur = tb[BOND_ATTR_POLICY]) != NULL) {
415                 const char *policy = blobmsg_get_string(cur);
416                 size_t i;
417 
418                 for (i = 0; i < ARRAY_SIZE(bonding_policy_str); i++) {
419                         if (strcmp(policy, bonding_policy_str[i]) != 0)
420                                 continue;
421 
422                         cfg->policy = i;
423                         break;
424                 }
425         }
426 
427         cfg_item(string, xmit_hash_policy, XMIT_HASH_POLICY);
428         cfg_item(bool, all_ports_active, ALL_PORTS_ACTIVE);
429         cfg_item(u32, min_links, MIN_LINKS);
430         cfg_item(string, ad_actor_system, AD_ACTOR_SYSTEM);
431         cfg_item(u32, ad_actor_sys_prio, AD_ACTOR_SYS_PRIO);
432         cfg_item(string, ad_select, AD_SELECT);
433         cfg_item(string, lacp_rate, LACP_RATE);
434         cfg_item(u32, packets_per_port, PACKETS_PER_PORT);
435         cfg_item(u32, lp_interval, LP_INTERVAL);
436         cfg_item(bool, dynamic_lb, DYNAMIC_LB);
437         cfg_item(u32, resend_igmp, RESEND_IGMP);
438         cfg_item(u32, num_peer_notif, NUM_PEER_NOTIF);
439         cfg_item(string, primary, PRIMARY);
440         cfg_item(string, primary_reselect, PRIMARY_RESELECT);
441         cfg_item(string, failover_mac, FAILOVER_MAC);
442         cfg_item(u32, monitor_interval, MON_INTERVAL);
443         cfg_item(bool, arp_all_targets, ARP_ALL_TARGETS);
444         cfg_item(string, arp_validate, ARP_VALIDATE);
445         cfg_item(bool, use_carrier, USE_CARRIER);
446         cfg_item(u32, updelay, UPDELAY);
447         cfg_item(u32, downdelay, DOWNDELAY);
448 
449         if ((cur = tb[BOND_ATTR_MON_MODE]) != NULL &&
450             !strcmp(blobmsg_get_string(cur), "arp"))
451                 cfg->monitor_arp = true;
452         cfg->arp_target = tb[BOND_ATTR_ARP_TARGET];
453 #undef cfg_item
454 }
455 
456 static enum dev_change_type
457 bonding_reload(struct device *dev, struct blob_attr *attr)
458 {
459         struct blob_attr *tb_dev[__DEV_ATTR_MAX];
460         struct blob_attr *tb_b[__BOND_ATTR_MAX];
461         enum dev_change_type ret = DEV_CONFIG_APPLIED;
462         unsigned long diff[2] = {};
463         struct bonding_device *bdev;
464 
465         BUILD_BUG_ON(sizeof(diff[0]) < __BOND_ATTR_MAX / 8);
466         BUILD_BUG_ON(sizeof(diff) < __DEV_ATTR_MAX / 8);
467 
468         bdev = container_of(dev, struct bonding_device, dev);
469         attr = blob_memdup(attr);
470 
471         blobmsg_parse_attr(device_attr_list.params, __DEV_ATTR_MAX, tb_dev, attr);
472         blobmsg_parse_attr(bonding_attrs, __BOND_ATTR_MAX, tb_b, attr);
473 
474         bdev->has_macaddr = tb_dev[DEV_ATTR_MACADDR];
475         if (bdev->primary_port && !bdev->primary_port->set_primary &&
476             tb_dev[DEV_ATTR_MACADDR])
477                 bdev->primary_port = NULL;
478 
479         bdev->port_list = tb_b[BOND_ATTR_PORTS];
480         device_init_settings(dev, tb_dev);
481         bonding_apply_settings(bdev, tb_b);
482 
483         if (bdev->config_data) {
484                 struct blob_attr *otb_dev[__DEV_ATTR_MAX];
485                 struct blob_attr *otb_b[__BOND_ATTR_MAX];
486 
487                 blobmsg_parse_attr(device_attr_list.params, __DEV_ATTR_MAX, otb_dev,
488                                    bdev->config_data);
489 
490                 uci_blob_diff(tb_dev, otb_dev, &device_attr_list, diff);
491                 if (diff[0] | diff[1])
492                     ret = DEV_CONFIG_RESTART;
493 
494                 blobmsg_parse_attr(bonding_attrs, __BOND_ATTR_MAX, otb_b,
495                                    bdev->config_data);
496 
497                 diff[0] = 0;
498                 uci_blob_diff(tb_b, otb_b, &bonding_attr_list, diff);
499                 if (diff[0] & ~(1 << BOND_ATTR_PORTS))
500                     ret = DEV_CONFIG_RESTART;
501 
502                 bonding_config_init(dev);
503         }
504 
505         free(bdev->config_data);
506         bdev->config_data = attr;
507 
508         return ret;
509 }
510 
511 static int
512 bonding_hotplug_add(struct device *dev, struct device *port, struct blob_attr *vlan)
513 {
514         struct bonding_device *bdev = container_of(dev, struct bonding_device, dev);
515         struct bonding_port *bp;
516 
517         bp = vlist_find(&bdev->ports, port->ifname, bp, node);
518         if (!bp)
519                 bonding_create_port(bdev, port->ifname, port, true);
520 
521         return 0;
522 }
523 
524 static int
525 bonding_hotplug_del(struct device *dev, struct device *port, struct blob_attr *vlan)
526 {
527         struct bonding_device *bdev = container_of(dev, struct bonding_device, dev);
528         struct bonding_port *bp;
529 
530         bp = vlist_find(&bdev->ports, port->ifname, bp, node);
531         if (!bp)
532                 return UBUS_STATUS_NOT_FOUND;
533 
534         if (bp->dev.hotplug)
535                 vlist_delete(&bdev->ports, &bp->node);
536 
537         return 0;
538 }
539 
540 static int
541 bonding_hotplug_prepare(struct device *dev, struct device **bonding_dev)
542 {
543         struct bonding_device *bdev;
544 
545         if (bonding_dev)
546                 *bonding_dev = dev;
547 
548         bdev = container_of(dev, struct bonding_device, dev);
549         bdev->force_active = true;
550         device_set_present(&bdev->dev, true);
551 
552         return 0;
553 }
554 
555 static void
556 bonding_retry_ports(struct uloop_timeout *timeout)
557 {
558         struct bonding_device *bdev = container_of(timeout, struct bonding_device, retry);
559         struct bonding_port *bp;
560 
561         bdev->n_failed = 0;
562         vlist_for_each_element(&bdev->ports, bp, node) {
563                 if (bp->present)
564                         continue;
565 
566                 if (!bp->dev.dev->present)
567                         continue;
568 
569                 bp->present = true;
570                 bdev->n_present++;
571                 bonding_enable_port(bp);
572         }
573 }
574 
575 
576 static void
577 bonding_free_port(struct bonding_port *bp)
578 {
579         struct device *dev = bp->dev.dev;
580 
581         bonding_remove_port(bp);
582 
583         device_remove_user(&bp->dev);
584 
585         /*
586          * When reloading the config and moving a device from one master to
587          * another, the other master may have tried to claim this device
588          * before it was removed here.
589          * Ensure that claiming the device is retried by toggling its present
590          * state
591          */
592         if (dev->present) {
593                 device_set_present(dev, false);
594                 device_set_present(dev, true);
595         }
596 
597         free(bp);
598 }
599 
600 static void
601 bonding_port_update(struct vlist_tree *tree, struct vlist_node *node_new,
602                      struct vlist_node *node_old)
603 {
604         struct bonding_port *bp;
605         struct device *dev;
606         struct bonding_device *bdev = container_of(tree, struct bonding_device, ports);
607 
608         if (node_new) {
609                 bp = container_of(node_new, struct bonding_port, node);
610 
611                 if (node_old) {
612                         struct bonding_port *bp_old;
613 
614                         bp_old = container_of(node_old, struct bonding_port, node);
615                         if (bp_old->set_primary != bp->set_primary) {
616                                 bp_old->set_primary = bp->set_primary;
617                                 bdev->reset_primary = true;
618                         }
619 
620                         free(bp);
621                         return;
622                 }
623 
624                 dev = bp->dev.dev;
625                 bp->dev.dev = NULL;
626                 device_add_user(&bp->dev, dev);
627         }
628 
629 
630         if (node_old) {
631                 bp = container_of(node_old, struct bonding_port, node);
632                 bonding_free_port(bp);
633         }
634 }
635 
636 static int
637 bonding_set_down(struct bonding_device *bdev)
638 {
639         struct bonding_port *bp;
640 
641         bdev->set_state(&bdev->dev, false);
642 
643         vlist_for_each_element(&bdev->ports, bp, node)
644                 bonding_disable_port(bp, false);
645 
646         bonding_set_active(bdev, false);
647 
648         return 0;
649 }
650 
651 static int
652 bonding_set_up(struct bonding_device *bdev)
653 {
654         struct bonding_port *bp;
655         int ret;
656 
657         if (!bdev->n_present) {
658                 if (!bdev->force_active)
659                         return -ENOENT;
660 
661                 ret = bonding_set_active(bdev, true);
662                 if (ret)
663                         return ret;
664         }
665 
666         bdev->n_failed = 0;
667         vlist_for_each_element(&bdev->ports, bp, node)
668                 bonding_enable_port(bp);
669         if (bdev->n_failed)
670                 uloop_timeout_set(&bdev->retry, 100);
671 
672         if (!bdev->force_active && !bdev->n_present) {
673                 /* initialization of all port interfaces failed */
674                 bonding_set_active(bdev, false);
675                 device_set_present(&bdev->dev, false);
676                 return -ENOENT;
677         }
678 
679         bonding_reset_primary(bdev);
680         ret = bdev->set_state(&bdev->dev, true);
681         if (ret < 0)
682                 bonding_set_down(bdev);
683 
684         return ret;
685 }
686 
687 static int
688 bonding_set_state(struct device *dev, bool up)
689 {
690         struct bonding_device *bdev;
691 
692         bdev = container_of(dev, struct bonding_device, dev);
693 
694         if (up)
695                 return bonding_set_up(bdev);
696         else
697                 return bonding_set_down(bdev);
698 }
699 
700 static struct device *
701 bonding_create(const char *name, struct device_type *devtype,
702         struct blob_attr *attr)
703 {
704         static const struct device_hotplug_ops bonding_ops = {
705                 .prepare = bonding_hotplug_prepare,
706                 .add = bonding_hotplug_add,
707                 .del = bonding_hotplug_del
708         };
709         struct bonding_device *bdev;
710         struct device *dev = NULL;
711 
712         bdev = calloc(1, sizeof(*bdev));
713         if (!bdev)
714                 return NULL;
715 
716         dev = &bdev->dev;
717 
718         if (device_init(dev, devtype, name) < 0) {
719                 device_cleanup(dev);
720                 free(bdev);
721                 return NULL;
722         }
723 
724         dev->config_pending = true;
725         bdev->retry.cb = bonding_retry_ports;
726 
727         bdev->set_state = dev->set_state;
728         dev->set_state = bonding_set_state;
729 
730         dev->hotplug_ops = &bonding_ops;
731 
732         vlist_init(&bdev->ports, avl_strcmp, bonding_port_update);
733         bdev->ports.keep_old = true;
734 
735         bonding_reload(dev, attr);
736 
737         return dev;
738 }
739 
740 static void
741 bonding_free(struct device *dev)
742 {
743         struct bonding_device *bdev;
744 
745         bdev = container_of(dev, struct bonding_device, dev);
746         vlist_flush_all(&bdev->ports);
747         free(bdev->config_data);
748         free(bdev);
749 }
750 
751 static struct device_type bonding_device_type = {
752         .name = "bonding",
753         .config_params = &bonding_attr_list,
754 
755         .bridge_capability = true,
756 
757         .create = bonding_create,
758         .config_init = bonding_config_init,
759         .reload = bonding_reload,
760         .free = bonding_free,
761 };
762 
763 static void __init bonding_device_type_init(void)
764 {
765         device_type_add(&bonding_device_type);
766 }
767 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt