summaryrefslogtreecommitdiff
path: root/bonding.c
blob: f4005de059b4d3532a90bed841b47a906f24758d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
/*
 * netifd - network interface daemon
 * Copyright (C) 2021 Felix Fietkau <nbd@nbd.name>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include "netifd.h"
#include "device.h"
#include "system.h"

struct bonding_device {
	struct device dev;
	device_state_cb set_state;

	struct blob_attr *port_list;
	struct vlist_tree ports;
	int n_present;
	int n_failed;

	struct bonding_port *primary_port;
	struct uloop_timeout retry;

	struct bonding_config config;
	struct blob_attr *config_data;
	bool has_macaddr;
	bool force_active;
	bool active;
};

struct bonding_port {
	struct vlist_node node;
	struct bonding_device *bdev;
	struct device_user dev;
	bool set_primary;
	bool present;
	bool active;
	char name[];
};

enum {
	BOND_ATTR_PORTS,

	BOND_ATTR_POLICY,
	BOND_ATTR_XMIT_HASH_POLICY,
	BOND_ATTR_ALL_PORTS_ACTIVE,

	BOND_ATTR_MIN_LINKS,
	BOND_ATTR_AD_ACTOR_SYSTEM,
	BOND_ATTR_AD_ACTOR_SYS_PRIO,
	BOND_ATTR_AD_SELECT,
	BOND_ATTR_LACP_RATE,

	BOND_ATTR_PACKETS_PER_PORT,
	BOND_ATTR_LP_INTERVAL,
	BOND_ATTR_DYNAMIC_LB,
	BOND_ATTR_RESEND_IGMP,

	BOND_ATTR_NUM_PEER_NOTIF,
	BOND_ATTR_PRIMARY,
	BOND_ATTR_PRIMARY_RESELECT,
	BOND_ATTR_FAILOVER_MAC,

	BOND_ATTR_MON_MODE,
	BOND_ATTR_MON_INTERVAL,
	BOND_ATTR_ARP_TARGET,
	BOND_ATTR_ARP_ALL_TARGETS,
	BOND_ATTR_ARP_VALIDATE,
	BOND_ATTR_USE_CARRIER,
	BOND_ATTR_UPDELAY,
	BOND_ATTR_DOWNDELAY,

	__BOND_ATTR_MAX,
};

static const struct blobmsg_policy bonding_attrs[__BOND_ATTR_MAX] = {
	[BOND_ATTR_PORTS] = { "ports", BLOBMSG_TYPE_ARRAY },
	[BOND_ATTR_POLICY] = { "policy", BLOBMSG_TYPE_STRING },
	[BOND_ATTR_XMIT_HASH_POLICY] = { "xmit_hash_policy", BLOBMSG_TYPE_STRING },
	[BOND_ATTR_ALL_PORTS_ACTIVE] = { "all_ports_active", BLOBMSG_TYPE_BOOL },
	[BOND_ATTR_MIN_LINKS] = { "min_links", BLOBMSG_TYPE_INT32 },
	[BOND_ATTR_AD_ACTOR_SYSTEM] = { "ad_actor_system", BLOBMSG_TYPE_STRING },
	[BOND_ATTR_AD_ACTOR_SYS_PRIO] = { "ad_actor_sys_prio", BLOBMSG_TYPE_INT32 },
	[BOND_ATTR_AD_SELECT] = { "ad_select", BLOBMSG_TYPE_STRING },
	[BOND_ATTR_LACP_RATE] = { "lacp_rate", BLOBMSG_TYPE_STRING },
	[BOND_ATTR_PACKETS_PER_PORT] = { "packets_per_port", BLOBMSG_TYPE_INT32 },
	[BOND_ATTR_LP_INTERVAL] = { "lp_interval", BLOBMSG_TYPE_INT32 },
	[BOND_ATTR_DYNAMIC_LB] = { "dynamic_lb", BLOBMSG_TYPE_BOOL },
	[BOND_ATTR_RESEND_IGMP] = { "resend_igmp", BLOBMSG_TYPE_INT32 },
	[BOND_ATTR_NUM_PEER_NOTIF] = { "num_peer_notif", BLOBMSG_TYPE_INT32 },
	[BOND_ATTR_PRIMARY] = { "primary", BLOBMSG_TYPE_STRING },
	[BOND_ATTR_PRIMARY_RESELECT] = { "primary_reselect", BLOBMSG_TYPE_STRING },
	[BOND_ATTR_FAILOVER_MAC] = { "failover_mac", BLOBMSG_TYPE_STRING },
	[BOND_ATTR_MON_MODE] = { "monitor_mode", BLOBMSG_TYPE_STRING },
	[BOND_ATTR_MON_INTERVAL] = { "monitor_interval", BLOBMSG_TYPE_INT32 },
	[BOND_ATTR_ARP_TARGET] = { "arp_target", BLOBMSG_TYPE_ARRAY },
	[BOND_ATTR_ARP_ALL_TARGETS] = { "arp_all_targets", BLOBMSG_TYPE_BOOL },
	[BOND_ATTR_ARP_VALIDATE] = { "arp_validate", BLOBMSG_TYPE_STRING },
	[BOND_ATTR_USE_CARRIER] = { "use_carrier", BLOBMSG_TYPE_BOOL },
	[BOND_ATTR_UPDELAY] = { "updelay", BLOBMSG_TYPE_INT32 },
	[BOND_ATTR_DOWNDELAY] = { "downdelay", BLOBMSG_TYPE_INT32 },
};

static const struct uci_blob_param_info bonding_attr_info[__BOND_ATTR_MAX] = {
	[BOND_ATTR_PORTS] = { .type = BLOBMSG_TYPE_STRING },
	[BOND_ATTR_ARP_TARGET] = { .type = BLOBMSG_TYPE_STRING },
};

static const struct uci_blob_param_list bonding_attr_list = {
	.n_params = __BOND_ATTR_MAX,
	.params = bonding_attrs,
	.info = bonding_attr_info,

	.n_next = 1,
	.next = { &device_attr_list },
};

static void
bonding_reset_primary(struct bonding_device *bdev)
{
	struct bonding_port *bp;

	bdev->primary_port = NULL;
	if (!bdev->has_macaddr)
		bdev->dev.settings.flags &= ~DEV_OPT_MACADDR;

	vlist_for_each_element(&bdev->ports, bp, node) {
		uint8_t *macaddr;

		if (!bp->present)
			continue;

		if (bdev->primary_port && !bp->set_primary)
			continue;

		bdev->primary_port = bp;
		if (bdev->has_macaddr)
			continue;

		if (bp->dev.dev->settings.flags & DEV_OPT_MACADDR)
			macaddr = bp->dev.dev->settings.macaddr;
		else
			macaddr = bp->dev.dev->orig_settings.macaddr;
		memcpy(bdev->dev.settings.macaddr, macaddr, 6);
		bdev->dev.settings.flags |= DEV_OPT_MACADDR;
	}
}

static int
bonding_disable_port(struct bonding_port *bp, bool keep_dev)
{
	struct bonding_device *bdev = bp->bdev;

	if (!bp->present || !bp->active)
		return 0;

	bp->active = false;

	system_bonding_set_port(&bdev->dev, bp->dev.dev, false, bp->set_primary);
	if (!keep_dev)
		device_release(&bp->dev);

	return 0;
}

static void
bonding_remove_port(struct bonding_port *bp)
{
	struct bonding_device *bdev = bp->bdev;

	if (!bp->present)
		return;

	if (bdev->dev.active)
		bonding_disable_port(bp, false);

	bp->present = false;
	bp->bdev->n_present--;

	if (bp == bdev->primary_port)
		bonding_reset_primary(bdev);

	bdev->force_active = false;
	if (bdev->n_present == 0)
		device_set_present(&bdev->dev, false);
}

static int
bonding_set_active(struct bonding_device *bdev, bool active)
{
	int ret;

	if (bdev->active == active)
		return 0;

	ret = system_bonding_set_device(&bdev->dev, active ? &bdev->config : NULL);
	if (ret < 0)
		return ret;

	bdev->active = active;
	return 0;
}

static int
bonding_enable_port(struct bonding_port *bp)
{
	struct bonding_device *bdev = bp->bdev;
	struct device *dev;
	int ret;

	if (!bp->present)
		return 0;

	/* Disable IPv6 for bonding ports */
	if (!(bp->dev.dev->settings.flags & DEV_OPT_IPV6)) {
		bp->dev.dev->settings.ipv6 = 0;
		bp->dev.dev->settings.flags |= DEV_OPT_IPV6;
	}

	ret = device_claim(&bp->dev);
	if (ret < 0)
		return ret;

	ret = bonding_set_active(bdev, true);
	if (ret)
		goto release;

	dev = bp->dev.dev;
	if (dev->settings.auth && !dev->auth_status)
		return -1;

	if (bp->active)
		return 0;

	ret = system_bonding_set_port(&bdev->dev, bp->dev.dev, true, bp->set_primary);
	if (ret < 0) {
		D(DEVICE, "Bonding port %s could not be added\n", bp->dev.dev->ifname);
		goto error;
	}

	bp->active = true;
	device_set_present(&bdev->dev, true);

	return 0;

error:
	bdev->n_failed++;
	bp->present = false;
	bdev->n_present--;
release:
	device_release(&bp->dev);

	return ret;
}

static void
bonding_port_cb(struct device_user *dep, enum device_event ev)
{
	struct bonding_port *bp = container_of(dep, struct bonding_port, dev);
	struct bonding_device *bdev = bp->bdev;
	struct device *dev = dep->dev;

	switch (ev) {
	case DEV_EVENT_ADD:
		if (bp->present)
			break;

		bp->present = true;
		bdev->n_present++;

		if (bdev->n_present == 1)
			device_set_present(&bdev->dev, true);
		fallthrough;
	case DEV_EVENT_AUTH_UP:
		if (!bdev->dev.active)
			break;

		if (bonding_enable_port(bp))
			break;

		/*
		 * Adding a bonding port can overwrite the bonding device mtu
		 * in the kernel, apply the bonding settings in case the
		 * bonding device mtu is set
		 */
		system_if_apply_settings(&bdev->dev, &bdev->dev.settings,
					 DEV_OPT_MTU | DEV_OPT_MTU6);
		break;
	case DEV_EVENT_LINK_DOWN:
		if (!dev->settings.auth)
			break;

		bonding_disable_port(bp, true);
		break;
	case DEV_EVENT_REMOVE:
		if (dep->hotplug) {
			vlist_delete(&bdev->ports, &bp->node);
			return;
		}

		if (bp->present)
			bonding_remove_port(bp);

		break;
	default:
		return;
	}
}

static struct bonding_port *
bonding_create_port(struct bonding_device *bdev, const char *name,
		    struct device *dev, bool hotplug)
{
	struct bonding_port *bp;

	bp = calloc(1, sizeof(*bp) + strlen(name) + 1);
	if (!bp)
		return NULL;

	bp->bdev = bdev;
	bp->dev.cb = bonding_port_cb;
	bp->dev.hotplug = hotplug;
	strcpy(bp->name, name);
	bp->dev.dev = dev;
	vlist_add(&bdev->ports, &bp->node, bp->name);
	/*
	 * Need to look up the bonding port again as the above
	 * created pointer will be freed in case the bonding port
	 * already existed
	 */
	if (!hotplug)
		return bp;

	bp = vlist_find(&bdev->ports, name, bp, node);
	if (bp)
		bp->node.version = -1;

	return bp;
}

static void
bonding_config_init(struct device *dev)
{
	struct bonding_device *bdev;
	struct blob_attr *cur;
	int rem;

	bdev = container_of(dev, struct bonding_device, dev);

	bdev->n_failed = 0;

	vlist_update(&bdev->ports);
	blobmsg_for_each_attr(cur, bdev->port_list, rem) {
		const char *name = blobmsg_get_string(cur);

		dev = device_get(name, true);
		if (!dev)
			continue;

		bonding_create_port(bdev, name, dev, false);
	}
	vlist_flush(&bdev->ports);

	if (bdev->n_failed)
		uloop_timeout_set(&bdev->retry, 100);
}

static void
bonding_apply_settings(struct bonding_device *bdev, struct blob_attr **tb)
{
	struct bonding_config *cfg = &bdev->config;
	struct blob_attr *cur;

	/* defaults */
	memset(cfg, 0, sizeof(*cfg));
	cfg->resend_igmp = 1;
	cfg->ad_actor_sys_prio = 65535;
	cfg->lp_interval = 1;
	cfg->num_peer_notif = 1;

#define cfg_item(_type, _field, _attr)				\
	do {							\
		if ((cur = tb[BOND_ATTR_##_attr]) != NULL)	\
			cfg->_field = blobmsg_get_##_type(cur);	\
	} while (0)

	if ((cur = tb[BOND_ATTR_POLICY]) != NULL) {
		const char *policy = blobmsg_get_string(cur);
		size_t i;

		for (i = 0; i < ARRAY_SIZE(bonding_policy_str); i++) {
			if (strcmp(policy, bonding_policy_str[i]) != 0)
				continue;

			cfg->policy = i;
			break;
		}
	}

	cfg_item(string, xmit_hash_policy, XMIT_HASH_POLICY);
	cfg_item(bool, all_ports_active, ALL_PORTS_ACTIVE);
	cfg_item(u32, min_links, MIN_LINKS);
	cfg_item(string, ad_actor_system, AD_ACTOR_SYSTEM);
	cfg_item(u32, ad_actor_sys_prio, AD_ACTOR_SYS_PRIO);
	cfg_item(string, ad_select, AD_SELECT);
	cfg_item(string, lacp_rate, LACP_RATE);
	cfg_item(u32, packets_per_port, PACKETS_PER_PORT);
	cfg_item(u32, lp_interval, LP_INTERVAL);
	cfg_item(bool, dynamic_lb, DYNAMIC_LB);
	cfg_item(u32, resend_igmp, RESEND_IGMP);
	cfg_item(u32, num_peer_notif, NUM_PEER_NOTIF);
	cfg_item(string, primary, PRIMARY);
	cfg_item(string, primary_reselect, PRIMARY_RESELECT);
	cfg_item(string, failover_mac, FAILOVER_MAC);
	cfg_item(u32, monitor_interval, MON_INTERVAL);
	cfg_item(bool, arp_all_targets, ARP_ALL_TARGETS);
	cfg_item(string, arp_validate, ARP_VALIDATE);
	cfg_item(bool, use_carrier, USE_CARRIER);
	cfg_item(u32, updelay, UPDELAY);
	cfg_item(u32, downdelay, DOWNDELAY);

	if ((cur = tb[BOND_ATTR_MON_MODE]) != NULL &&
	    !strcmp(blobmsg_get_string(cur), "arp"))
		cfg->monitor_arp = true;
	cfg->arp_target = tb[BOND_ATTR_ARP_TARGET];
#undef cfg_item
}

static enum dev_change_type
bonding_reload(struct device *dev, struct blob_attr *attr)
{
	struct blob_attr *tb_dev[__DEV_ATTR_MAX];
	struct blob_attr *tb_b[__BOND_ATTR_MAX];
	enum dev_change_type ret = DEV_CONFIG_APPLIED;
	unsigned long diff;
	struct bonding_device *bdev;

	BUILD_BUG_ON(sizeof(diff) < __BOND_ATTR_MAX / 8);
	BUILD_BUG_ON(sizeof(diff) < __DEV_ATTR_MAX / 8);

	bdev = container_of(dev, struct bonding_device, dev);
	attr = blob_memdup(attr);

	blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, tb_dev,
		blob_data(attr), blob_len(attr));
	blobmsg_parse(bonding_attrs, __BOND_ATTR_MAX, tb_b,
		blob_data(attr), blob_len(attr));

	bdev->has_macaddr = tb_dev[DEV_ATTR_MACADDR];
	if (bdev->primary_port && !bdev->primary_port->set_primary &&
	    tb_dev[DEV_ATTR_MACADDR])
		bdev->primary_port = NULL;

	bdev->port_list = tb_b[BOND_ATTR_PORTS];
	device_init_settings(dev, tb_dev);
	bonding_apply_settings(bdev, tb_b);

	if (bdev->config_data) {
		struct blob_attr *otb_dev[__DEV_ATTR_MAX];
		struct blob_attr *otb_b[__BOND_ATTR_MAX];

		blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb_dev,
			blob_data(bdev->config_data), blob_len(bdev->config_data));

		diff = 0;
		uci_blob_diff(tb_dev, otb_dev, &device_attr_list, &diff);
		if (diff)
		    ret = DEV_CONFIG_RESTART;

		blobmsg_parse(bonding_attrs, __BOND_ATTR_MAX, otb_b,
			blob_data(bdev->config_data), blob_len(bdev->config_data));

		diff = 0;
		uci_blob_diff(tb_b, otb_b, &bonding_attr_list, &diff);
		if (diff & ~(1 << BOND_ATTR_PORTS))
		    ret = DEV_CONFIG_RESTART;

		bonding_config_init(dev);
	}

	free(bdev->config_data);
	bdev->config_data = attr;

	return ret;
}

static int
bonding_hotplug_add(struct device *dev, struct device *port, struct blob_attr *vlan)
{
	struct bonding_device *bdev = container_of(dev, struct bonding_device, dev);
	struct bonding_port *bp;

	bp = vlist_find(&bdev->ports, port->ifname, bp, node);
	if (!bp)
		bonding_create_port(bdev, port->ifname, port, true);

	return 0;
}

static int
bonding_hotplug_del(struct device *dev, struct device *port, struct blob_attr *vlan)
{
	struct bonding_device *bdev = container_of(dev, struct bonding_device, dev);
	struct bonding_port *bp;

	bp = vlist_find(&bdev->ports, port->ifname, bp, node);
	if (!bp)
		return UBUS_STATUS_NOT_FOUND;

	if (bp->dev.hotplug)
		vlist_delete(&bdev->ports, &bp->node);

	return 0;
}

static int
bonding_hotplug_prepare(struct device *dev, struct device **bonding_dev)
{
	struct bonding_device *bdev;

	if (bonding_dev)
		*bonding_dev = dev;

	bdev = container_of(dev, struct bonding_device, dev);
	bdev->force_active = true;
	device_set_present(&bdev->dev, true);

	return 0;
}

static void
bonding_retry_ports(struct uloop_timeout *timeout)
{
	struct bonding_device *bdev = container_of(timeout, struct bonding_device, retry);
	struct bonding_port *bp;

	bdev->n_failed = 0;
	vlist_for_each_element(&bdev->ports, bp, node) {
		if (bp->present)
			continue;

		if (!bp->dev.dev->present)
			continue;

		bp->present = true;
		bdev->n_present++;
		bonding_enable_port(bp);
	}
}


static void
bonding_free_port(struct bonding_port *bp)
{
	struct device *dev = bp->dev.dev;

	bonding_remove_port(bp);

	device_remove_user(&bp->dev);

	/*
	 * When reloading the config and moving a device from one master to
	 * another, the other master may have tried to claim this device
	 * before it was removed here.
	 * Ensure that claiming the device is retried by toggling its present
	 * state
	 */
	if (dev->present) {
		device_set_present(dev, false);
		device_set_present(dev, true);
	}

	free(bp);
}

static void
bonding_port_update(struct vlist_tree *tree, struct vlist_node *node_new,
		     struct vlist_node *node_old)
{
	struct bonding_port *bp;
	struct device *dev;

	if (node_new) {
		bp = container_of(node_new, struct bonding_port, node);

		if (node_old) {
			free(bp);
			return;
		}

		dev = bp->dev.dev;
		bp->dev.dev = NULL;
		device_add_user(&bp->dev, dev);
	}


	if (node_old) {
		bp = container_of(node_old, struct bonding_port, node);
		bonding_free_port(bp);
	}
}

static int
bonding_set_down(struct bonding_device *bdev)
{
	struct bonding_port *bp;

	bdev->set_state(&bdev->dev, false);

	vlist_for_each_element(&bdev->ports, bp, node)
		bonding_disable_port(bp, false);

	bonding_set_active(bdev, false);

	return 0;
}

static int
bonding_set_up(struct bonding_device *bdev)
{
	struct bonding_port *bp;
	int ret;

	if (!bdev->n_present) {
		if (!bdev->force_active)
			return -ENOENT;

		ret = bonding_set_active(bdev, true);
		if (ret)
			return ret;
	}

	bdev->n_failed = 0;
	vlist_for_each_element(&bdev->ports, bp, node)
		bonding_enable_port(bp);
	if (bdev->n_failed)
		uloop_timeout_set(&bdev->retry, 100);

	if (!bdev->force_active && !bdev->n_present) {
		/* initialization of all port interfaces failed */
		bonding_set_active(bdev, false);
		device_set_present(&bdev->dev, false);
		return -ENOENT;
	}

	bonding_reset_primary(bdev);
	ret = bdev->set_state(&bdev->dev, true);
	if (ret < 0)
		bonding_set_down(bdev);

	return ret;
}

static int
bonding_set_state(struct device *dev, bool up)
{
	struct bonding_device *bdev;

	bdev = container_of(dev, struct bonding_device, dev);

	if (up)
		return bonding_set_up(bdev);
	else
		return bonding_set_down(bdev);
}

static struct device *
bonding_create(const char *name, struct device_type *devtype,
	struct blob_attr *attr)
{
	static const struct device_hotplug_ops bonding_ops = {
		.prepare = bonding_hotplug_prepare,
		.add = bonding_hotplug_add,
		.del = bonding_hotplug_del
	};
	struct bonding_device *bdev;
	struct device *dev = NULL;

	bdev = calloc(1, sizeof(*bdev));
	if (!bdev)
		return NULL;

	dev = &bdev->dev;

	if (device_init(dev, devtype, name) < 0) {
		device_cleanup(dev);
		free(bdev);
		return NULL;
	}

	dev->config_pending = true;
	bdev->retry.cb = bonding_retry_ports;

	bdev->set_state = dev->set_state;
	dev->set_state = bonding_set_state;

	dev->hotplug_ops = &bonding_ops;

	vlist_init(&bdev->ports, avl_strcmp, bonding_port_update);
	bdev->ports.keep_old = true;

	bonding_reload(dev, attr);

	return dev;
}

static void
bonding_free(struct device *dev)
{
	struct bonding_device *bdev;

	bdev = container_of(dev, struct bonding_device, dev);
	vlist_flush_all(&bdev->ports);
	free(bdev->config_data);
	free(bdev);
}

static struct device_type bonding_device_type = {
	.name = "bonding",
	.config_params = &bonding_attr_list,

	.bridge_capability = true,

	.create = bonding_create,
	.config_init = bonding_config_init,
	.reload = bonding_reload,
	.free = bonding_free,
};

static void __init bonding_device_type_init(void)
{
	device_type_add(&bonding_device_type);
}