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#include <linux/config.h>
432#include <linux/kernel.h>
433#include <linux/module.h>
434#include <linux/sched.h>
435#include <linux/types.h>
436#include <linux/fcntl.h>
437#include <linux/interrupt.h>
438#include <linux/ptrace.h>
439#include <linux/ioport.h>
440#include <linux/in.h>
441#include <linux/ip.h>
442#include <linux/slab.h>
443#include <linux/string.h>
444#include <linux/init.h>
445#include <linux/timer.h>
446#include <linux/socket.h>
447#include <linux/ctype.h>
448#include <linux/inet.h>
449#include <asm/system.h>
450#include <asm/bitops.h>
451#include <asm/io.h>
452#include <asm/dma.h>
453#include <asm/uaccess.h>
454#include <linux/errno.h>
455
456#include <linux/netdevice.h>
457#include <linux/inetdevice.h>
458#include <linux/etherdevice.h>
459#include <linux/skbuff.h>
460#include <net/sock.h>
461#include <linux/rtnetlink.h>
462#include <linux/proc_fs.h>
463#include <linux/seq_file.h>
464
465#include <linux/if_bonding.h>
466#include <linux/smp.h>
467#include <linux/if_ether.h>
468#include <net/arp.h>
469#include <linux/mii.h>
470#include <linux/ethtool.h>
471#include "bonding.h"
472#include "bond_3ad.h"
473#include "bond_alb.h"
474
475#define DRV_VERSION "2.4.1"
476#define DRV_RELDATE "September 15, 2003"
477#define DRV_NAME "bonding"
478#define DRV_DESCRIPTION "Ethernet Channel Bonding Driver"
479
480static const char *version =
481DRV_NAME ".c:v" DRV_VERSION " (" DRV_RELDATE ")\n";
482
483
484#ifndef BOND_LINK_MON_INTERV
485#define BOND_LINK_MON_INTERV 0
486#endif
487
488#ifndef BOND_LINK_ARP_INTERV
489#define BOND_LINK_ARP_INTERV 0
490#endif
491
492#ifndef MAX_ARP_IP_TARGETS
493#define MAX_ARP_IP_TARGETS 16
494#endif
495
496#define USES_PRIMARY(mode) \
497 (((mode) == BOND_MODE_ACTIVEBACKUP) || \
498 ((mode) == BOND_MODE_TLB) || \
499 ((mode) == BOND_MODE_ALB))
500
501struct bond_parm_tbl {
502 char *modename;
503 int mode;
504};
505
506static int arp_interval = BOND_LINK_ARP_INTERV;
507static char *arp_ip_target[MAX_ARP_IP_TARGETS] = { NULL, };
508static u32 arp_target[MAX_ARP_IP_TARGETS] = { 0, } ;
509static int arp_ip_count = 0;
510static u32 my_ip = 0;
511char *arp_target_hw_addr = NULL;
512
513static char *primary= NULL;
514
515static int app_abi_ver = 0;
516static int orig_app_abi_ver = -1;
517
518
519
520
521
522
523
524static int max_bonds = BOND_DEFAULT_MAX_BONDS;
525static int miimon = BOND_LINK_MON_INTERV;
526static int use_carrier = 1;
527static int bond_mode = BOND_MODE_ROUNDROBIN;
528static int updelay = 0;
529static int downdelay = 0;
530
531static char *mode = NULL;
532
533static struct bond_parm_tbl bond_mode_tbl[] = {
534{ "balance-rr", BOND_MODE_ROUNDROBIN},
535{ "active-backup", BOND_MODE_ACTIVEBACKUP},
536{ "balance-xor", BOND_MODE_XOR},
537{ "broadcast", BOND_MODE_BROADCAST},
538{ "802.3ad", BOND_MODE_8023AD},
539{ "balance-tlb", BOND_MODE_TLB},
540{ "balance-alb", BOND_MODE_ALB},
541{ NULL, -1},
542};
543
544static int multicast_mode = BOND_MULTICAST_ALL;
545static char *multicast = NULL;
546
547static struct bond_parm_tbl bond_mc_tbl[] = {
548{ "disabled", BOND_MULTICAST_DISABLED},
549{ "active", BOND_MULTICAST_ACTIVE},
550{ "all", BOND_MULTICAST_ALL},
551{ NULL, -1},
552};
553
554static int lacp_fast = 0;
555static char *lacp_rate = NULL;
556
557static struct bond_parm_tbl bond_lacp_tbl[] = {
558{ "slow", AD_LACP_SLOW},
559{ "fast", AD_LACP_FAST},
560{ NULL, -1},
561};
562
563static LIST_HEAD(bond_dev_list);
564#ifdef CONFIG_PROC_FS
565static struct proc_dir_entry *bond_proc_dir = NULL;
566#endif
567
568MODULE_PARM(max_bonds, "i");
569MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
570MODULE_PARM(miimon, "i");
571MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
572MODULE_PARM(use_carrier, "i");
573MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; 0 for off, 1 for on (default)");
574MODULE_PARM(mode, "s");
575MODULE_PARM_DESC(mode, "Mode of operation : 0 for round robin, 1 for active-backup, 2 for xor");
576MODULE_PARM(arp_interval, "i");
577MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
578MODULE_PARM(arp_ip_target, "1-" __MODULE_STRING(MAX_ARP_IP_TARGETS) "s");
579MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
580MODULE_PARM(updelay, "i");
581MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
582MODULE_PARM(downdelay, "i");
583MODULE_PARM_DESC(downdelay, "Delay before considering link down, in milliseconds");
584MODULE_PARM(primary, "s");
585MODULE_PARM_DESC(primary, "Primary network device to use");
586MODULE_PARM(multicast, "s");
587MODULE_PARM_DESC(multicast, "Mode for multicast support : 0 for none, 1 for active slave, 2 for all slaves (default)");
588MODULE_PARM(lacp_rate, "s");
589MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner (slow/fast)");
590
591static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *dev);
592static int bond_xmit_xor(struct sk_buff *skb, struct net_device *dev);
593static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *dev);
594static struct net_device_stats *bond_get_stats(struct net_device *dev);
595static void bond_mii_monitor(struct net_device *dev);
596static void loadbalance_arp_monitor(struct net_device *dev);
597static void activebackup_arp_monitor(struct net_device *dev);
598static void bond_mc_list_destroy(struct bonding *bond);
599static void bond_mc_add(bonding_t *bond, void *addr, int alen);
600static void bond_mc_delete(bonding_t *bond, void *addr, int alen);
601static int bond_mc_list_copy (struct dev_mc_list *src, struct bonding *dst, int gpf_flag);
602static inline int dmi_same(struct dev_mc_list *dmi1, struct dev_mc_list *dmi2);
603static void bond_set_promiscuity(bonding_t *bond, int inc);
604static void bond_set_allmulti(bonding_t *bond, int inc);
605static struct dev_mc_list* bond_mc_list_find_dmi(struct dev_mc_list *dmi, struct dev_mc_list *mc_list);
606static void bond_mc_update(bonding_t *bond, slave_t *new, slave_t *old);
607static int bond_enslave(struct net_device *master, struct net_device *slave);
608static int bond_release(struct net_device *master, struct net_device *slave);
609static int bond_release_all(struct net_device *master);
610static int bond_sethwaddr(struct net_device *master, struct net_device *slave);
611static void change_active_interface(struct bonding *bond, struct slave *new);
612static void reselect_active_interface(struct bonding *bond);
613static struct slave *find_best_interface(struct bonding *bond);
614
615
616
617#ifdef BONDING_DEBUG
618#define dprintk(x...) printk(x...)
619#else
620#define dprintk(x...) do {} while (0)
621#endif
622
623
624
625static void arp_send_all(slave_t *slave)
626{
627 int i;
628
629 for (i = 0; (i<MAX_ARP_IP_TARGETS) && arp_target[i]; i++) {
630 arp_send(ARPOP_REQUEST, ETH_P_ARP, arp_target[i], slave->dev,
631 my_ip, arp_target_hw_addr, slave->dev->dev_addr,
632 arp_target_hw_addr);
633 }
634}
635
636
637static const char *
638bond_mode_name(void)
639{
640 switch (bond_mode) {
641 case BOND_MODE_ROUNDROBIN :
642 return "load balancing (round-robin)";
643 case BOND_MODE_ACTIVEBACKUP :
644 return "fault-tolerance (active-backup)";
645 case BOND_MODE_XOR :
646 return "load balancing (xor)";
647 case BOND_MODE_BROADCAST :
648 return "fault-tolerance (broadcast)";
649 case BOND_MODE_8023AD:
650 return "IEEE 802.3ad Dynamic link aggregation";
651 case BOND_MODE_TLB:
652 return "transmit load balancing";
653 case BOND_MODE_ALB:
654 return "adaptive load balancing";
655 default:
656 return "unknown";
657 }
658}
659
660static const char *
661multicast_mode_name(void)
662{
663 switch(multicast_mode) {
664 case BOND_MULTICAST_DISABLED :
665 return "disabled";
666 case BOND_MULTICAST_ACTIVE :
667 return "active slave only";
668 case BOND_MULTICAST_ALL :
669 return "all slaves";
670 default :
671 return "unknown";
672 }
673}
674
675void bond_set_slave_inactive_flags(slave_t *slave)
676{
677 slave->state = BOND_STATE_BACKUP;
678 slave->dev->flags |= IFF_NOARP;
679}
680
681void bond_set_slave_active_flags(slave_t *slave)
682{
683 slave->state = BOND_STATE_ACTIVE;
684 slave->dev->flags &= ~IFF_NOARP;
685}
686
687
688
689
690
691
692
693
694static inline void
695update_slave_cnt(bonding_t *bond, int incr)
696{
697 slave_t *slave = NULL;
698 int expect = bond->slave_cnt + incr;
699
700 bond->slave_cnt = 0;
701 for (slave = bond->prev; slave != (slave_t*)bond;
702 slave = slave->prev) {
703 bond->slave_cnt++;
704 }
705
706 if (expect != bond->slave_cnt)
707 BUG();
708}
709
710
711
712
713
714
715
716
717
718
719
720static slave_t *
721bond_detach_slave(bonding_t *bond, slave_t *slave)
722{
723 if ((bond == NULL) || (slave == NULL) ||
724 ((void *)bond == (void *)slave)) {
725 printk(KERN_ERR
726 "bond_detach_slave(): trying to detach "
727 "slave %p from bond %p\n", bond, slave);
728 return slave;
729 }
730
731 if (bond->next == slave) {
732 if (bond->prev == slave) {
733 bond->prev = bond->next = (slave_t *)bond;
734 } else {
735 bond->next = slave->next;
736 slave->next->prev = (slave_t *)bond;
737 bond->prev->next = slave->next;
738 }
739 } else {
740 slave->prev->next = slave->next;
741 if (bond->prev == slave) {
742 bond->prev = slave->prev;
743 } else {
744 slave->next->prev = slave->prev;
745 }
746 }
747
748 update_slave_cnt(bond, -1);
749
750 return slave;
751}
752
753
754
755
756
757
758static void
759bond_attach_slave(struct bonding *bond, struct slave *new_slave)
760{
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782 new_slave->prev = bond->prev;
783 new_slave->prev->next = new_slave;
784 bond->prev = new_slave;
785 new_slave->next = bond->next;
786
787 update_slave_cnt(bond, 1);
788}
789
790
791
792
793
794
795
796#define IOCTL(dev, arg, cmd) ({ \
797 int ret; \
798 mm_segment_t fs = get_fs(); \
799 set_fs(get_ds()); \
800 ret = ioctl(dev, arg, cmd); \
801 set_fs(fs); \
802 ret; })
803
804
805
806
807
808
809
810static int bond_update_speed_duplex(struct slave *slave)
811{
812 struct net_device *dev = slave->dev;
813 static int (* ioctl)(struct net_device *, struct ifreq *, int);
814 struct ifreq ifr;
815 struct ethtool_cmd etool;
816
817 ioctl = dev->do_ioctl;
818 if (ioctl) {
819 etool.cmd = ETHTOOL_GSET;
820 ifr.ifr_data = (char*)&etool;
821 if (IOCTL(dev, &ifr, SIOCETHTOOL) == 0) {
822 slave->speed = etool.speed;
823 slave->duplex = etool.duplex;
824 } else {
825 goto err_out;
826 }
827 } else {
828 goto err_out;
829 }
830
831 switch (slave->speed) {
832 case SPEED_10:
833 case SPEED_100:
834 case SPEED_1000:
835 break;
836 default:
837 goto err_out;
838 }
839
840 switch (slave->duplex) {
841 case DUPLEX_FULL:
842 case DUPLEX_HALF:
843 break;
844 default:
845 goto err_out;
846 }
847
848 return 0;
849
850err_out:
851
852 slave->speed = SPEED_100;
853 slave->duplex = DUPLEX_FULL;
854 return -1;
855}
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873static int
874bond_check_dev_link(struct net_device *dev, int reporting)
875{
876 static int (* ioctl)(struct net_device *, struct ifreq *, int);
877 struct ifreq ifr;
878 struct mii_ioctl_data *mii;
879 struct ethtool_value etool;
880
881 if (use_carrier) {
882 return netif_carrier_ok(dev) ? BMSR_LSTATUS : 0;
883 }
884
885 ioctl = dev->do_ioctl;
886 if (ioctl) {
887
888
889
890
891
892
893
894
895
896
897
898
899
900 mii = (struct mii_ioctl_data *)&ifr.ifr_data;
901 if (IOCTL(dev, &ifr, SIOCGMIIPHY) == 0) {
902 mii->reg_num = MII_BMSR;
903 if (IOCTL(dev, &ifr, SIOCGMIIREG) == 0) {
904 return mii->val_out & BMSR_LSTATUS;
905 }
906 }
907
908
909
910
911 etool.cmd = ETHTOOL_GLINK;
912 ifr.ifr_data = (char*)&etool;
913 if (IOCTL(dev, &ifr, SIOCETHTOOL) == 0) {
914 if (etool.data == 1) {
915 return BMSR_LSTATUS;
916 } else {
917#ifdef BONDING_DEBUG
918 printk(KERN_INFO
919 ":: SIOCETHTOOL shows link down \n");
920#endif
921 return 0;
922 }
923 }
924
925 }
926
927
928
929
930
931
932
933 return reporting ? -1 : BMSR_LSTATUS;
934}
935
936static u16 bond_check_mii_link(bonding_t *bond)
937{
938 int has_active_interface = 0;
939
940 read_lock_bh(&bond->lock);
941 read_lock(&bond->ptrlock);
942 has_active_interface = (bond->current_slave != NULL);
943 read_unlock(&bond->ptrlock);
944 read_unlock_bh(&bond->lock);
945
946 return (has_active_interface ? BMSR_LSTATUS : 0);
947}
948
949
950static void bond_register_lacpdu(struct bonding *bond)
951{
952 struct packet_type* pk_type = &(BOND_AD_INFO(bond).ad_pkt_type);
953
954
955 pk_type->type = PKT_TYPE_LACPDU;
956 pk_type->dev = bond->device;
957 pk_type->func = bond_3ad_lacpdu_recv;
958
959 dev_add_pack(pk_type);
960}
961
962
963static void bond_unregister_lacpdu(struct bonding *bond)
964{
965 dev_remove_pack(&(BOND_AD_INFO(bond).ad_pkt_type));
966}
967
968static int bond_open(struct net_device *dev)
969{
970 struct bonding *bond = (struct bonding *)(dev->priv);
971 struct timer_list *timer = &((struct bonding *)(dev->priv))->mii_timer;
972 struct timer_list *arp_timer = &((struct bonding *)(dev->priv))->arp_timer;
973
974 if ((bond_mode == BOND_MODE_TLB) ||
975 (bond_mode == BOND_MODE_ALB)) {
976 struct timer_list *alb_timer = &(BOND_ALB_INFO(bond).alb_timer);
977
978
979
980
981 if (bond_alb_initialize(bond, (bond_mode == BOND_MODE_ALB))) {
982
983 return -1;
984 }
985
986 init_timer(alb_timer);
987 alb_timer->expires = jiffies + 1;
988 alb_timer->data = (unsigned long)bond;
989 alb_timer->function = (void *)&bond_alb_monitor;
990 add_timer(alb_timer);
991 }
992
993 if (miimon > 0) {
994 init_timer(timer);
995 timer->expires = jiffies + (miimon * HZ / 1000);
996 timer->data = (unsigned long)dev;
997 timer->function = (void *)&bond_mii_monitor;
998 add_timer(timer);
999 }
1000
1001 if (arp_interval> 0) {
1002 init_timer(arp_timer);
1003 arp_timer->expires = jiffies + (arp_interval * HZ / 1000);
1004 arp_timer->data = (unsigned long)dev;
1005 if (bond_mode == BOND_MODE_ACTIVEBACKUP) {
1006 arp_timer->function = (void *)&activebackup_arp_monitor;
1007 } else {
1008 arp_timer->function = (void *)&loadbalance_arp_monitor;
1009 }
1010 add_timer(arp_timer);
1011 }
1012
1013 if (bond_mode == BOND_MODE_8023AD) {
1014 struct timer_list *ad_timer = &(BOND_AD_INFO(bond).ad_timer);
1015 init_timer(ad_timer);
1016 ad_timer->expires = jiffies + (AD_TIMER_INTERVAL * HZ / 1000);
1017 ad_timer->data = (unsigned long)bond;
1018 ad_timer->function = (void *)&bond_3ad_state_machine_handler;
1019 add_timer(ad_timer);
1020
1021
1022 bond_register_lacpdu(bond);
1023 }
1024
1025 return 0;
1026}
1027
1028static int bond_close(struct net_device *master)
1029{
1030 bonding_t *bond = (struct bonding *) master->priv;
1031
1032 write_lock_bh(&bond->lock);
1033
1034 if (miimon > 0) {
1035 del_timer(&bond->mii_timer);
1036 }
1037 if (arp_interval> 0) {
1038 del_timer(&bond->arp_timer);
1039 if (arp_target_hw_addr != NULL) {
1040 kfree(arp_target_hw_addr);
1041 arp_target_hw_addr = NULL;
1042 }
1043 }
1044
1045 if (bond_mode == BOND_MODE_8023AD) {
1046 del_timer_sync(&(BOND_AD_INFO(bond).ad_timer));
1047
1048
1049 bond_unregister_lacpdu(bond);
1050 }
1051
1052 bond_mc_list_destroy (bond);
1053
1054 write_unlock_bh(&bond->lock);
1055
1056
1057 bond_release_all(master);
1058
1059 if ((bond_mode == BOND_MODE_TLB) ||
1060 (bond_mode == BOND_MODE_ALB)) {
1061 del_timer_sync(&(BOND_ALB_INFO(bond).alb_timer));
1062
1063 bond_alb_deinitialize(bond);
1064 }
1065
1066 return 0;
1067}
1068
1069
1070
1071
1072static void bond_mc_list_flush(struct net_device *dev, struct net_device *flush)
1073{
1074 struct dev_mc_list *dmi;
1075
1076 for (dmi = flush->mc_list; dmi != NULL; dmi = dmi->next)
1077 dev_mc_delete(dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1078
1079 if (bond_mode == BOND_MODE_8023AD) {
1080
1081 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1082
1083 dev_mc_delete(dev, lacpdu_multicast, ETH_ALEN, 0);
1084 }
1085}
1086
1087
1088
1089
1090static void bond_mc_list_destroy(struct bonding *bond)
1091{
1092 struct dev_mc_list *dmi;
1093
1094 dmi = bond->mc_list;
1095 while (dmi) {
1096 bond->mc_list = dmi->next;
1097 kfree(dmi);
1098 dmi = bond->mc_list;
1099 }
1100}
1101
1102
1103
1104
1105static void bond_mc_add(bonding_t *bond, void *addr, int alen)
1106{
1107 slave_t *slave;
1108 switch (multicast_mode) {
1109 case BOND_MULTICAST_ACTIVE :
1110
1111 if (bond->current_slave != NULL)
1112 dev_mc_add(bond->current_slave->dev, addr, alen, 0);
1113 break;
1114 case BOND_MULTICAST_ALL :
1115 for (slave = bond->prev; slave != (slave_t*)bond; slave = slave->prev)
1116 dev_mc_add(slave->dev, addr, alen, 0);
1117 break;
1118 case BOND_MULTICAST_DISABLED :
1119 break;
1120 }
1121}
1122
1123
1124
1125
1126static void bond_mc_delete(bonding_t *bond, void *addr, int alen)
1127{
1128 slave_t *slave;
1129 switch (multicast_mode) {
1130 case BOND_MULTICAST_ACTIVE :
1131
1132 if (bond->current_slave != NULL)
1133 dev_mc_delete(bond->current_slave->dev, addr, alen, 0);
1134 break;
1135 case BOND_MULTICAST_ALL :
1136 for (slave = bond->prev; slave != (slave_t*)bond; slave = slave->prev)
1137 dev_mc_delete(slave->dev, addr, alen, 0);
1138 break;
1139 case BOND_MULTICAST_DISABLED :
1140 break;
1141 }
1142}
1143
1144
1145
1146
1147static int bond_mc_list_copy (struct dev_mc_list *src, struct bonding *dst,
1148 int gpf_flag)
1149{
1150 struct dev_mc_list *dmi, *new_dmi;
1151
1152 for (dmi = src; dmi != NULL; dmi = dmi->next) {
1153 new_dmi = kmalloc(sizeof(struct dev_mc_list), gpf_flag);
1154
1155 if (new_dmi == NULL) {
1156 return -ENOMEM;
1157 }
1158
1159 new_dmi->next = dst->mc_list;
1160 dst->mc_list = new_dmi;
1161
1162 new_dmi->dmi_addrlen = dmi->dmi_addrlen;
1163 memcpy(new_dmi->dmi_addr, dmi->dmi_addr, dmi->dmi_addrlen);
1164 new_dmi->dmi_users = dmi->dmi_users;
1165 new_dmi->dmi_gusers = dmi->dmi_gusers;
1166 }
1167 return 0;
1168}
1169
1170
1171
1172
1173static inline int dmi_same(struct dev_mc_list *dmi1, struct dev_mc_list *dmi2)
1174{
1175 return memcmp(dmi1->dmi_addr, dmi2->dmi_addr, dmi1->dmi_addrlen) == 0 &&
1176 dmi1->dmi_addrlen == dmi2->dmi_addrlen;
1177}
1178
1179
1180
1181
1182static void bond_set_promiscuity(bonding_t *bond, int inc)
1183{
1184 slave_t *slave;
1185
1186 if (USES_PRIMARY(bond_mode)) {
1187 if (bond->current_slave) {
1188 dev_set_promiscuity(bond->current_slave->dev, inc);
1189 }
1190
1191 } else {
1192 for (slave = bond->prev; slave != (slave_t*)bond;
1193 slave = slave->prev) {
1194 dev_set_promiscuity(slave->dev, inc);
1195 }
1196 }
1197}
1198
1199
1200
1201
1202static void bond_set_allmulti(bonding_t *bond, int inc)
1203{
1204 slave_t *slave;
1205 switch (multicast_mode) {
1206 case BOND_MULTICAST_ACTIVE :
1207
1208 if (bond->current_slave != NULL)
1209 dev_set_allmulti(bond->current_slave->dev, inc);
1210 break;
1211 case BOND_MULTICAST_ALL :
1212 for (slave = bond->prev; slave != (slave_t*)bond; slave = slave->prev)
1213 dev_set_allmulti(slave->dev, inc);
1214 break;
1215 case BOND_MULTICAST_DISABLED :
1216 break;
1217 }
1218}
1219
1220
1221
1222
1223static struct dev_mc_list* bond_mc_list_find_dmi(struct dev_mc_list *dmi,
1224 struct dev_mc_list *mc_list)
1225{
1226 struct dev_mc_list *idmi;
1227
1228 for (idmi = mc_list; idmi != NULL; idmi = idmi->next) {
1229 if (dmi_same(dmi, idmi)) {
1230 return idmi;
1231 }
1232 }
1233 return NULL;
1234}
1235
1236static void set_multicast_list(struct net_device *master)
1237{
1238 bonding_t *bond = master->priv;
1239 struct dev_mc_list *dmi;
1240
1241 write_lock_bh(&bond->lock);
1242
1243
1244
1245
1246 if ( (master->flags & IFF_PROMISC) && !(bond->flags & IFF_PROMISC) )
1247 bond_set_promiscuity(bond, 1);
1248
1249 if ( !(master->flags & IFF_PROMISC) && (bond->flags & IFF_PROMISC) )
1250 bond_set_promiscuity(bond, -1);
1251
1252 if (multicast_mode == BOND_MULTICAST_DISABLED) {
1253 bond->flags = master->flags;
1254 write_unlock_bh(&bond->lock);
1255 return;
1256 }
1257
1258
1259 if ( (master->flags & IFF_ALLMULTI) && !(bond->flags & IFF_ALLMULTI) )
1260 bond_set_allmulti(bond, 1);
1261
1262 if ( !(master->flags & IFF_ALLMULTI) && (bond->flags & IFF_ALLMULTI) )
1263 bond_set_allmulti(bond, -1);
1264
1265 bond->flags = master->flags;
1266
1267
1268 for (dmi = master->mc_list; dmi != NULL; dmi = dmi->next) {
1269 if (bond_mc_list_find_dmi(dmi, bond->mc_list) == NULL)
1270 bond_mc_add(bond, dmi->dmi_addr, dmi->dmi_addrlen);
1271 }
1272
1273
1274 for (dmi = bond->mc_list; dmi != NULL; dmi = dmi->next) {
1275 if (bond_mc_list_find_dmi(dmi, master->mc_list) == NULL)
1276 bond_mc_delete(bond, dmi->dmi_addr, dmi->dmi_addrlen);
1277 }
1278
1279
1280
1281 bond_mc_list_destroy (bond);
1282 bond_mc_list_copy (master->mc_list, bond, GFP_ATOMIC);
1283
1284 write_unlock_bh(&bond->lock);
1285}
1286
1287
1288
1289
1290
1291
1292static void bond_mc_update(bonding_t *bond, slave_t *new, slave_t *old)
1293{
1294 struct dev_mc_list *dmi;
1295
1296 if (USES_PRIMARY(bond_mode)) {
1297 if (bond->device->flags & IFF_PROMISC) {
1298 if (old)
1299 dev_set_promiscuity(old->dev, -1);
1300 if (new)
1301 dev_set_promiscuity(new->dev, 1);
1302 }
1303 }
1304
1305 switch(multicast_mode) {
1306 case BOND_MULTICAST_ACTIVE :
1307 if (bond->device->flags & IFF_ALLMULTI) {
1308 if (old)
1309 dev_set_allmulti(old->dev, -1);
1310 if (new)
1311 dev_set_allmulti(new->dev, 1);
1312 }
1313
1314
1315 if (old) {
1316 for (dmi = bond->device->mc_list; dmi != NULL; dmi = dmi->next)
1317 dev_mc_delete(old->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1318 }
1319 if (new) {
1320 for (dmi = bond->device->mc_list; dmi != NULL; dmi = dmi->next)
1321 dev_mc_add(new->dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1322 }
1323 break;
1324 case BOND_MULTICAST_ALL :
1325
1326 break;
1327 case BOND_MULTICAST_DISABLED :
1328 break;
1329 }
1330}
1331
1332
1333static int bond_enslave(struct net_device *master_dev,
1334 struct net_device *slave_dev)
1335{
1336 bonding_t *bond = NULL;
1337 slave_t *new_slave = NULL;
1338 unsigned long rflags = 0;
1339 int err = 0;
1340 struct dev_mc_list *dmi;
1341 struct in_ifaddr **ifap;
1342 struct in_ifaddr *ifa;
1343 int link_reporting;
1344 struct sockaddr addr;
1345
1346 if (master_dev == NULL || slave_dev == NULL) {
1347 return -ENODEV;
1348 }
1349 bond = (struct bonding *) master_dev->priv;
1350
1351 if (slave_dev->do_ioctl == NULL) {
1352 printk(KERN_DEBUG
1353 "Warning : no link monitoring support for %s\n",
1354 slave_dev->name);
1355 }
1356
1357
1358
1359 if (!(master_dev->flags & IFF_UP)) {
1360#ifdef BONDING_DEBUG
1361 printk(KERN_CRIT "Error, master_dev is not up\n");
1362#endif
1363 return -EPERM;
1364 }
1365
1366
1367 if (master_dev->flags & IFF_SLAVE || slave_dev->flags & IFF_SLAVE) {
1368#ifdef BONDING_DEBUG
1369 printk(KERN_CRIT "Error, Device was already enslaved\n");
1370#endif
1371 return -EBUSY;
1372 }
1373
1374 if (app_abi_ver >= 1) {
1375
1376
1377
1378 if ((slave_dev->flags & IFF_UP)) {
1379#ifdef BONDING_DEBUG
1380 printk(KERN_CRIT "Error, slave_dev is up\n");
1381#endif
1382 return -EPERM;
1383 }
1384
1385 if (slave_dev->set_mac_address == NULL) {
1386 printk(KERN_CRIT
1387 "The slave device you specified does not support"
1388 " setting the MAC address.\n");
1389 printk(KERN_CRIT
1390 "Your kernel likely does not support slave"
1391 " devices.\n");
1392
1393 return -EOPNOTSUPP;
1394 }
1395 } else {
1396
1397
1398
1399 if (!(slave_dev->flags & IFF_UP)) {
1400#ifdef BONDING_DEBUG
1401 printk(KERN_CRIT "Error, slave_dev is not running\n");
1402#endif
1403 return -EINVAL;
1404 }
1405
1406 if ((bond_mode == BOND_MODE_8023AD) ||
1407 (bond_mode == BOND_MODE_TLB) ||
1408 (bond_mode == BOND_MODE_ALB)) {
1409 printk(KERN_ERR
1410 "bonding: Error: to use %s mode, you must "
1411 "upgrade ifenslave.\n", bond_mode_name());
1412 return -EOPNOTSUPP;
1413 }
1414 }
1415
1416 if ((new_slave = kmalloc(sizeof(slave_t), GFP_KERNEL)) == NULL) {
1417 return -ENOMEM;
1418 }
1419 memset(new_slave, 0, sizeof(slave_t));
1420
1421
1422
1423
1424 new_slave->original_flags = slave_dev->flags;
1425
1426 if (app_abi_ver >= 1) {
1427
1428
1429
1430
1431 memcpy(new_slave->perm_hwaddr, slave_dev->dev_addr, ETH_ALEN);
1432
1433 if (bond->slave_cnt > 0) {
1434
1435
1436
1437
1438 memcpy(addr.sa_data, master_dev->dev_addr, master_dev->addr_len);
1439 addr.sa_family = slave_dev->type;
1440 err = slave_dev->set_mac_address(slave_dev, &addr);
1441 if (err) {
1442#ifdef BONDING_DEBUG
1443 printk(KERN_CRIT "Error %d calling set_mac_address\n", err);
1444#endif
1445 goto err_free;
1446 }
1447 }
1448
1449
1450 err = dev_open(slave_dev);
1451 if (err) {
1452#ifdef BONDING_DEBUG
1453 printk(KERN_CRIT "Openning slave %s failed\n", slave_dev->name);
1454#endif
1455 goto err_restore_mac;
1456 }
1457 }
1458
1459 err = netdev_set_master(slave_dev, master_dev);
1460 if (err) {
1461#ifdef BONDING_DEBUG
1462 printk(KERN_CRIT "Error %d calling netdev_set_master\n", err);
1463#endif
1464 if (app_abi_ver < 1) {
1465 goto err_free;
1466 } else {
1467 goto err_close;
1468 }
1469 }
1470
1471 new_slave->dev = slave_dev;
1472
1473 if ((bond_mode == BOND_MODE_TLB) ||
1474 (bond_mode == BOND_MODE_ALB)) {
1475
1476
1477
1478 err = bond_alb_init_slave(bond, new_slave);
1479 if (err) {
1480 goto err_unset_master;
1481 }
1482 }
1483
1484
1485 if (master_dev->flags & IFF_PROMISC) {
1486
1487
1488
1489
1490
1491 if (!USES_PRIMARY(bond_mode)) {
1492 dev_set_promiscuity(slave_dev, 1);
1493 }
1494 }
1495
1496 if (multicast_mode == BOND_MULTICAST_ALL) {
1497
1498 if (master_dev->flags & IFF_ALLMULTI)
1499 dev_set_allmulti(slave_dev, 1);
1500
1501
1502 for (dmi = master_dev->mc_list; dmi != NULL; dmi = dmi->next)
1503 dev_mc_add (slave_dev, dmi->dmi_addr, dmi->dmi_addrlen, 0);
1504 }
1505
1506 if (bond_mode == BOND_MODE_8023AD) {
1507
1508 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1509
1510 dev_mc_add(slave_dev, lacpdu_multicast, ETH_ALEN, 0);
1511 }
1512
1513 write_lock_bh(&bond->lock);
1514
1515 bond_attach_slave(bond, new_slave);
1516 new_slave->delay = 0;
1517 new_slave->link_failure_count = 0;
1518
1519 if (miimon > 0 && !use_carrier) {
1520 link_reporting = bond_check_dev_link(slave_dev, 1);
1521
1522 if ((link_reporting == -1) && (arp_interval == 0)) {
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532 printk(KERN_ERR
1533 "bond_enslave(): MII and ETHTOOL support not "
1534 "available for interface %s, and "
1535 "arp_interval/arp_ip_target module parameters "
1536 "not specified, thus bonding will not detect "
1537 "link failures! see bonding.txt for details.\n",
1538 slave_dev->name);
1539 } else if (link_reporting == -1) {
1540
1541 printk(KERN_WARNING
1542 "bond_enslave: can't get link status from "
1543 "interface %s; the network driver associated "
1544 "with this interface does not support "
1545 "MII or ETHTOOL link status reporting, thus "
1546 "miimon has no effect on this interface.\n",
1547 slave_dev->name);
1548 }
1549 }
1550
1551
1552 if ((miimon <= 0) ||
1553 (bond_check_dev_link(slave_dev, 0) == BMSR_LSTATUS)) {
1554 if (updelay) {
1555#ifdef BONDING_DEBUG
1556 printk(KERN_CRIT "Initial state of slave_dev is "
1557 "BOND_LINK_BACK\n");
1558#endif
1559 new_slave->link = BOND_LINK_BACK;
1560 new_slave->delay = updelay;
1561 }
1562 else {
1563#ifdef BONDING_DEBUG
1564 printk(KERN_DEBUG "Initial state of slave_dev is "
1565 "BOND_LINK_UP\n");
1566#endif
1567 new_slave->link = BOND_LINK_UP;
1568 }
1569 new_slave->jiffies = jiffies;
1570 }
1571 else {
1572#ifdef BONDING_DEBUG
1573 printk(KERN_CRIT "Initial state of slave_dev is "
1574 "BOND_LINK_DOWN\n");
1575#endif
1576 new_slave->link = BOND_LINK_DOWN;
1577 }
1578
1579 if (bond_update_speed_duplex(new_slave) &&
1580 (new_slave->link != BOND_LINK_DOWN)) {
1581
1582 printk(KERN_WARNING
1583 "bond_enslave(): failed to get speed/duplex from %s, "
1584 "speed forced to 100Mbps, duplex forced to Full.\n",
1585 new_slave->dev->name);
1586 if (bond_mode == BOND_MODE_8023AD) {
1587 printk(KERN_WARNING
1588 "Operation of 802.3ad mode requires ETHTOOL support "
1589 "in base driver for proper aggregator selection.\n");
1590 }
1591 }
1592
1593
1594
1595
1596
1597
1598
1599
1600 if (bond_mode == BOND_MODE_ACTIVEBACKUP) {
1601 if (((bond->current_slave == NULL)
1602 || (bond->current_slave->dev->flags & IFF_NOARP))
1603 && (new_slave->link != BOND_LINK_DOWN)) {
1604#ifdef BONDING_DEBUG
1605 printk(KERN_CRIT "This is the first active slave\n");
1606#endif
1607
1608
1609 change_active_interface(bond, new_slave);
1610 }
1611 else {
1612#ifdef BONDING_DEBUG
1613 printk(KERN_CRIT "This is just a backup slave\n");
1614#endif
1615 bond_set_slave_inactive_flags(new_slave);
1616 }
1617 if (((struct in_device *)slave_dev->ip_ptr) != NULL) {
1618 read_lock_irqsave(&(((struct in_device *)slave_dev->ip_ptr)->lock), rflags);
1619 ifap= &(((struct in_device *)slave_dev->ip_ptr)->ifa_list);
1620 ifa = *ifap;
1621 if (ifa != NULL)
1622 my_ip = ifa->ifa_address;
1623 read_unlock_irqrestore(&(((struct in_device *)slave_dev->ip_ptr)->lock), rflags);
1624 }
1625
1626
1627 if (primary != NULL) {
1628 if (strcmp(primary, new_slave->dev->name) == 0) {
1629 bond->primary_slave = new_slave;
1630 }
1631 }
1632 } else if (bond_mode == BOND_MODE_8023AD) {
1633
1634
1635
1636
1637 bond_set_slave_inactive_flags(new_slave);
1638
1639 if (new_slave == bond->next) {
1640 SLAVE_AD_INFO(new_slave).id = 1;
1641
1642
1643
1644 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL,
1645 lacp_fast);
1646 } else {
1647 SLAVE_AD_INFO(new_slave).id =
1648 SLAVE_AD_INFO(new_slave->prev).id + 1;
1649 }
1650
1651 bond_3ad_bind_slave(new_slave);
1652 } else if ((bond_mode == BOND_MODE_TLB) ||
1653 (bond_mode == BOND_MODE_ALB)) {
1654 new_slave->state = BOND_STATE_ACTIVE;
1655 if ((bond->current_slave == NULL) && (new_slave->link != BOND_LINK_DOWN)) {
1656
1657
1658
1659 change_active_interface(bond, new_slave);
1660 }
1661
1662
1663 if (primary != NULL) {
1664 if (strcmp(primary, new_slave->dev->name) == 0) {
1665 bond->primary_slave = new_slave;
1666 }
1667 }
1668 } else {
1669#ifdef BONDING_DEBUG
1670 printk(KERN_CRIT "This slave is always active in trunk mode\n");
1671#endif
1672
1673 new_slave->state = BOND_STATE_ACTIVE;
1674
1675
1676
1677
1678
1679 if (bond->current_slave == NULL)
1680 bond->current_slave = new_slave;
1681 }
1682
1683 write_unlock_bh(&bond->lock);
1684
1685 if (app_abi_ver < 1) {
1686
1687
1688
1689
1690
1691
1692
1693 int ndx = 0;
1694
1695 for (ndx = 0; ndx < slave_dev->addr_len; ndx++) {
1696#ifdef BONDING_DEBUG
1697 printk(KERN_DEBUG
1698 "Checking ndx=%d of master_dev->dev_addr\n", ndx);
1699#endif
1700 if (master_dev->dev_addr[ndx] != 0) {
1701#ifdef BONDING_DEBUG
1702 printk(KERN_DEBUG
1703 "Found non-zero byte at ndx=%d\n", ndx);
1704#endif
1705 break;
1706 }
1707 }
1708 if (ndx == slave_dev->addr_len) {
1709
1710
1711
1712
1713#ifdef BONDING_DEBUG
1714 printk(KERN_DEBUG "%s doesn't have a MAC address yet. ",
1715 master_dev->name);
1716 printk(KERN_DEBUG "Going to give assign it from %s.\n",
1717 slave_dev->name);
1718#endif
1719 bond_sethwaddr(master_dev, slave_dev);
1720 }
1721 }
1722
1723 printk (KERN_INFO "%s: enslaving %s as a%s interface with a%s link.\n",
1724 master_dev->name, slave_dev->name,
1725 new_slave->state == BOND_STATE_ACTIVE ? "n active" : " backup",
1726 new_slave->link != BOND_LINK_DOWN ? "n up" : " down");
1727
1728
1729 return 0;
1730
1731
1732err_unset_master:
1733 netdev_set_master(slave_dev, NULL);
1734
1735err_close:
1736 dev_close(slave_dev);
1737
1738err_restore_mac:
1739 memcpy(addr.sa_data, new_slave->perm_hwaddr, ETH_ALEN);
1740 addr.sa_family = slave_dev->type;
1741 slave_dev->set_mac_address(slave_dev, &addr);
1742
1743err_free:
1744 kfree(new_slave);
1745 return err;
1746}
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759static int bond_change_active(struct net_device *master_dev, struct net_device *slave_dev)
1760{
1761 bonding_t *bond;
1762 slave_t *slave;
1763 slave_t *oldactive = NULL;
1764 slave_t *newactive = NULL;
1765 int ret = 0;
1766
1767 if (master_dev == NULL || slave_dev == NULL) {
1768 return -ENODEV;
1769 }
1770
1771
1772 if (!(slave_dev->flags & IFF_SLAVE) ||
1773 (slave_dev->master != master_dev)) {
1774
1775 return -EINVAL;
1776 }
1777
1778 bond = (struct bonding *) master_dev->priv;
1779 write_lock_bh(&bond->lock);
1780 slave = (slave_t *)bond;
1781 oldactive = bond->current_slave;
1782
1783 while ((slave = slave->prev) != (slave_t *)bond) {
1784 if(slave_dev == slave->dev) {
1785 newactive = slave;
1786 break;
1787 }
1788 }
1789
1790
1791
1792
1793 if (newactive && (newactive == oldactive)) {
1794 write_unlock_bh(&bond->lock);
1795 return 0;
1796 }
1797
1798 if ((newactive != NULL)&&
1799 (oldactive != NULL)&&
1800 (newactive->link == BOND_LINK_UP)&&
1801 IS_UP(newactive->dev)) {
1802 change_active_interface(bond, newactive);
1803 } else {
1804 ret = -EINVAL;
1805 }
1806 write_unlock_bh(&bond->lock);
1807 return ret;
1808}
1809
1810
1811
1812
1813
1814
1815
1816static struct slave *find_best_interface(struct bonding *bond)
1817{
1818 struct slave *newslave, *oldslave;
1819 struct slave *bestslave = NULL;
1820 int mintime;
1821
1822 newslave = oldslave = bond->current_slave;
1823
1824 if (newslave == NULL) {
1825 if (bond->next != (slave_t *)bond) {
1826 newslave = bond->next;
1827 } else {
1828 return NULL;
1829 }
1830 }
1831
1832 mintime = updelay;
1833
1834
1835
1836
1837
1838
1839 if ((bond->primary_slave != NULL) && (arp_interval == 0)) {
1840 if (IS_UP(bond->primary_slave->dev))
1841 newslave = bond->primary_slave;
1842 }
1843
1844
1845 oldslave = newslave;
1846
1847 do {
1848 if (IS_UP(newslave->dev)) {
1849 if (newslave->link == BOND_LINK_UP) {
1850 return newslave;
1851 }
1852 else if (newslave->link == BOND_LINK_BACK) {
1853
1854 if (newslave->delay < mintime) {
1855 mintime = newslave->delay;
1856 bestslave = newslave;
1857 }
1858 }
1859 }
1860 } while ((newslave = newslave->next) != oldslave);
1861
1862 return bestslave;
1863}
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880static void change_active_interface(struct bonding *bond, struct slave *new)
1881{
1882 struct slave *old = bond->current_slave;
1883
1884 if (old == new) {
1885 return;
1886 }
1887
1888 if (new) {
1889 if (new->link == BOND_LINK_BACK) {
1890 if (USES_PRIMARY(bond_mode)) {
1891 printk (KERN_INFO
1892 "%s: making interface %s the new "
1893 "active one %d ms earlier.\n",
1894 bond->device->name, new->dev->name,
1895 (updelay - new->delay) * miimon);
1896 }
1897
1898 new->delay = 0;
1899 new->link = BOND_LINK_UP;
1900 new->jiffies = jiffies;
1901
1902 if (bond_mode == BOND_MODE_8023AD) {
1903 bond_3ad_handle_link_change(new, BOND_LINK_UP);
1904 }
1905
1906 if ((bond_mode == BOND_MODE_TLB) ||
1907 (bond_mode == BOND_MODE_ALB)) {
1908 bond_alb_handle_link_change(bond, new, BOND_LINK_UP);
1909 }
1910 } else {
1911 if (USES_PRIMARY(bond_mode)) {
1912 printk (KERN_INFO
1913 "%s: making interface %s the new active one.\n",
1914 bond->device->name, new->dev->name);
1915 }
1916 }
1917 }
1918
1919 if (bond_mode == BOND_MODE_ACTIVEBACKUP) {
1920 if (old) {
1921 bond_set_slave_inactive_flags(old);
1922 }
1923
1924 if (new) {
1925 bond_set_slave_active_flags(new);
1926 }
1927 }
1928
1929 if (USES_PRIMARY(bond_mode)) {
1930 bond_mc_update(bond, new, old);
1931 }
1932
1933 if ((bond_mode == BOND_MODE_TLB) ||
1934 (bond_mode == BOND_MODE_ALB)) {
1935 bond_alb_assign_current_slave(bond, new);
1936 } else {
1937 bond->current_slave = new;
1938 }
1939}
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952static void reselect_active_interface(struct bonding *bond)
1953{
1954 struct slave *best_slave;
1955
1956 best_slave = find_best_interface(bond);
1957
1958 if (best_slave != bond->current_slave) {
1959 change_active_interface(bond, best_slave);
1960 }
1961}
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974static int bond_release(struct net_device *master, struct net_device *slave)
1975{
1976 bonding_t *bond;
1977 slave_t *our_slave, *old_current;
1978 struct sockaddr addr;
1979
1980 if (master == NULL || slave == NULL) {
1981 return -ENODEV;
1982 }
1983
1984 bond = (struct bonding *) master->priv;
1985
1986
1987
1988 if ((master->flags & IFF_SLAVE) || !(slave->flags & IFF_SLAVE)) {
1989 printk (KERN_DEBUG "%s: cannot release %s.\n", master->name, slave->name);
1990 return -EINVAL;
1991 }
1992
1993 write_lock_bh(&bond->lock);
1994 bond->current_arp_slave = NULL;
1995 our_slave = (slave_t *)bond;
1996 old_current = bond->current_slave;
1997 while ((our_slave = our_slave->prev) != (slave_t *)bond) {
1998 if (our_slave->dev == slave) {
1999 int mac_addr_differ = memcmp(bond->device->dev_addr,
2000 our_slave->perm_hwaddr,
2001 ETH_ALEN);
2002 if (!mac_addr_differ && (bond->slave_cnt > 1)) {
2003 printk(KERN_WARNING "WARNING: the permanent HWaddr of %s "
2004 "- %02X:%02X:%02X:%02X:%02X:%02X - "
2005 "is still in use by %s. Set the HWaddr "
2006 "of %s to a different address "
2007 "to avoid conflicts.\n",
2008 slave->name,
2009 our_slave->perm_hwaddr[0],
2010 our_slave->perm_hwaddr[1],
2011 our_slave->perm_hwaddr[2],
2012 our_slave->perm_hwaddr[3],
2013 our_slave->perm_hwaddr[4],
2014 our_slave->perm_hwaddr[5],
2015 bond->device->name,
2016 slave->name);
2017 }
2018
2019
2020 if (bond_mode == BOND_MODE_8023AD) {
2021
2022
2023
2024 bond_3ad_unbind_slave(our_slave);
2025 }
2026
2027 printk (KERN_INFO "%s: releasing %s interface %s\n",
2028 master->name,
2029 (our_slave->state == BOND_STATE_ACTIVE) ? "active" : "backup",
2030 slave->name);
2031
2032
2033 bond_detach_slave(bond, our_slave);
2034
2035 if (bond->primary_slave == our_slave) {
2036 bond->primary_slave = NULL;
2037 }
2038
2039 if (bond->current_slave == our_slave) {
2040 change_active_interface(bond, NULL);
2041 reselect_active_interface(bond);
2042 }
2043
2044 if (bond->current_slave == NULL) {
2045 printk(KERN_INFO
2046 "%s: now running without any active interface !\n",
2047 master->name);
2048 }
2049
2050 if ((bond_mode == BOND_MODE_TLB) ||
2051 (bond_mode == BOND_MODE_ALB)) {
2052
2053
2054
2055
2056 bond_alb_deinit_slave(bond, our_slave);
2057 }
2058
2059 break;
2060 }
2061
2062 }
2063 write_unlock_bh(&bond->lock);
2064
2065 if (our_slave == (slave_t *)bond) {
2066
2067 printk (KERN_INFO "%s: %s not enslaved\n", master->name, slave->name);
2068 return -EINVAL;
2069 }
2070
2071
2072 if (master->flags & IFF_PROMISC) {
2073
2074
2075
2076
2077 if (!USES_PRIMARY(bond_mode)) {
2078 dev_set_promiscuity(slave, -1);
2079 }
2080 }
2081
2082
2083 if (multicast_mode == BOND_MULTICAST_ALL) {
2084
2085 bond_mc_list_flush (slave, master);
2086
2087
2088 if (master->flags & IFF_ALLMULTI)
2089 dev_set_allmulti(slave, -1);
2090 }
2091
2092 netdev_set_master(slave, NULL);
2093
2094
2095 dev_close(slave);
2096
2097 if (app_abi_ver >= 1) {
2098
2099 memcpy(addr.sa_data, our_slave->perm_hwaddr, ETH_ALEN);
2100 addr.sa_family = slave->type;
2101 slave->set_mac_address(slave, &addr);
2102 }
2103
2104
2105
2106
2107
2108 if ((our_slave->original_flags & IFF_NOARP) == 0) {
2109 slave->flags &= ~IFF_NOARP;
2110 }
2111
2112 kfree(our_slave);
2113
2114
2115
2116
2117
2118 if (bond->next == (slave_t*)bond) {
2119 memset(master->dev_addr, 0, master->addr_len);
2120 }
2121
2122 return 0;
2123}
2124
2125
2126
2127
2128static int bond_release_all(struct net_device *master)
2129{
2130 bonding_t *bond;
2131 slave_t *our_slave, *old_current;
2132 struct net_device *slave_dev;
2133 struct sockaddr addr;
2134 int err = 0;
2135
2136 if (master == NULL) {
2137 return -ENODEV;
2138 }
2139
2140 if (master->flags & IFF_SLAVE) {
2141 return -EINVAL;
2142 }
2143
2144 bond = (struct bonding *) master->priv;
2145
2146 write_lock_bh(&bond->lock);
2147 if (bond->next == (struct slave *) bond) {
2148 err = -EINVAL;
2149 goto out;
2150 }
2151
2152 old_current = bond->current_slave;
2153 change_active_interface(bond, NULL);
2154 bond->current_arp_slave = NULL;
2155 bond->primary_slave = NULL;
2156
2157 while ((our_slave = bond->prev) != (slave_t *)bond) {
2158
2159
2160
2161 if (bond_mode == BOND_MODE_8023AD) {
2162 bond_3ad_unbind_slave(our_slave);
2163 }
2164
2165 slave_dev = our_slave->dev;
2166 bond_detach_slave(bond, our_slave);
2167
2168 if ((bond_mode == BOND_MODE_TLB) ||
2169 (bond_mode == BOND_MODE_ALB)) {
2170
2171
2172
2173 bond_alb_deinit_slave(bond, our_slave);
2174 }
2175
2176
2177
2178
2179
2180 write_unlock_bh(&bond->lock);
2181
2182
2183 if (master->flags & IFF_PROMISC) {
2184 if (!USES_PRIMARY(bond_mode)) {
2185 dev_set_promiscuity(slave_dev, -1);
2186 }
2187 }
2188
2189 if (multicast_mode == BOND_MULTICAST_ALL) {
2190
2191 bond_mc_list_flush (slave_dev, master);
2192
2193
2194 if (master->flags & IFF_ALLMULTI)
2195 dev_set_allmulti(slave_dev, -1);
2196 }
2197
2198 netdev_set_master(slave_dev, NULL);
2199
2200
2201 dev_close(slave_dev);
2202
2203 if (app_abi_ver >= 1) {
2204
2205 memcpy(addr.sa_data, our_slave->perm_hwaddr, ETH_ALEN);
2206 addr.sa_family = slave_dev->type;
2207 slave_dev->set_mac_address(slave_dev, &addr);
2208 }
2209
2210
2211
2212
2213 if ((our_slave->original_flags & IFF_NOARP) == 0) {
2214 slave_dev->flags &= ~IFF_NOARP;
2215 }
2216
2217 kfree(our_slave);
2218
2219
2220 write_lock_bh(&bond->lock);
2221 }
2222
2223
2224
2225
2226
2227 memset(master->dev_addr, 0, master->addr_len);
2228
2229 printk (KERN_INFO "%s: released all slaves\n", master->name);
2230
2231out:
2232 write_unlock_bh(&bond->lock);
2233
2234 return err;
2235}
2236
2237
2238static void bond_mii_monitor(struct net_device *master)
2239{
2240 bonding_t *bond = (struct bonding *) master->priv;
2241 slave_t *slave, *oldcurrent;
2242 int slave_died = 0;
2243 int do_failover = 0;
2244
2245 read_lock(&bond->lock);
2246
2247
2248
2249
2250
2251
2252
2253 slave = (slave_t *)bond;
2254
2255 read_lock(&bond->ptrlock);
2256 oldcurrent = bond->current_slave;
2257 read_unlock(&bond->ptrlock);
2258
2259 while ((slave = slave->prev) != (slave_t *)bond) {
2260 struct net_device *dev = slave->dev;
2261 int link_state;
2262 u16 old_speed = slave->speed;
2263 u8 old_duplex = slave->duplex;
2264
2265 link_state = bond_check_dev_link(dev, 0);
2266
2267 switch (slave->link) {
2268 case BOND_LINK_UP:
2269 if (link_state == BMSR_LSTATUS) {
2270
2271 break;
2272 }
2273 else {
2274 slave->link = BOND_LINK_FAIL;
2275 slave->delay = downdelay;
2276 if (slave->link_failure_count < UINT_MAX) {
2277 slave->link_failure_count++;
2278 }
2279 if (downdelay > 0) {
2280 printk (KERN_INFO
2281 "%s: link status down for %sinterface "
2282 "%s, disabling it in %d ms.\n",
2283 master->name,
2284 IS_UP(dev)
2285 ? ((bond_mode == BOND_MODE_ACTIVEBACKUP)
2286 ? ((slave == oldcurrent)
2287 ? "active " : "backup ")
2288 : "")
2289 : "idle ",
2290 dev->name,
2291 downdelay * miimon);
2292 }
2293 }
2294
2295
2296
2297 case BOND_LINK_FAIL:
2298 if (link_state != BMSR_LSTATUS) {
2299
2300 if (slave->delay <= 0) {
2301
2302 slave->link = BOND_LINK_DOWN;
2303
2304
2305
2306 if ((bond_mode == BOND_MODE_ACTIVEBACKUP) ||
2307 (bond_mode == BOND_MODE_8023AD)) {
2308 bond_set_slave_inactive_flags(slave);
2309 }
2310
2311 printk(KERN_INFO
2312 "%s: link status definitely down "
2313 "for interface %s, disabling it",
2314 master->name,
2315 dev->name);
2316
2317
2318 if (bond_mode == BOND_MODE_8023AD) {
2319 bond_3ad_handle_link_change(slave, BOND_LINK_DOWN);
2320 }
2321
2322 if ((bond_mode == BOND_MODE_TLB) ||
2323 (bond_mode == BOND_MODE_ALB)) {
2324 bond_alb_handle_link_change(bond, slave, BOND_LINK_DOWN);
2325 }
2326
2327 if (slave == oldcurrent) {
2328 do_failover = 1;
2329 }
2330
2331 slave_died = 1;
2332 } else {
2333 slave->delay--;
2334 }
2335 } else {
2336
2337 slave->link = BOND_LINK_UP;
2338 slave->jiffies = jiffies;
2339 printk(KERN_INFO
2340 "%s: link status up again after %d ms "
2341 "for interface %s.\n",
2342 master->name,
2343 (downdelay - slave->delay) * miimon,
2344 dev->name);
2345 }
2346 break;
2347 case BOND_LINK_DOWN:
2348 if (link_state != BMSR_LSTATUS) {
2349
2350 break;
2351 } else {
2352 slave->link = BOND_LINK_BACK;
2353 slave->delay = updelay;
2354
2355 if (updelay > 0) {
2356
2357
2358 printk (KERN_INFO
2359 "%s: link status up for interface"
2360 " %s, enabling it in %d ms.\n",
2361 master->name,
2362 dev->name,
2363 updelay * miimon);
2364 }
2365 }
2366
2367
2368
2369 case BOND_LINK_BACK:
2370 if (link_state != BMSR_LSTATUS) {
2371
2372 slave->link = BOND_LINK_DOWN;
2373 printk(KERN_INFO
2374 "%s: link status down again after %d ms "
2375 "for interface %s.\n",
2376 master->name,
2377 (updelay - slave->delay) * miimon,
2378 dev->name);
2379 } else {
2380
2381 if (slave->delay == 0) {
2382
2383 slave->link = BOND_LINK_UP;
2384 slave->jiffies = jiffies;
2385
2386 if (bond_mode == BOND_MODE_8023AD) {
2387
2388 slave->state = BOND_STATE_BACKUP;
2389 }
2390 else if (bond_mode != BOND_MODE_ACTIVEBACKUP) {
2391
2392 slave->state = BOND_STATE_ACTIVE;
2393 } else if (slave != bond->primary_slave) {
2394
2395 slave->state = BOND_STATE_BACKUP;
2396 }
2397
2398 printk(KERN_INFO
2399 "%s: link status definitely up "
2400 "for interface %s.\n",
2401 master->name,
2402 dev->name);
2403
2404
2405 if (bond_mode == BOND_MODE_8023AD) {
2406 bond_3ad_handle_link_change(slave, BOND_LINK_UP);
2407 }
2408
2409 if ((bond_mode == BOND_MODE_TLB) ||
2410 (bond_mode == BOND_MODE_ALB)) {
2411 bond_alb_handle_link_change(bond, slave, BOND_LINK_UP);
2412 }
2413
2414 if ((oldcurrent == NULL) ||
2415 (slave == bond->primary_slave)) {
2416 do_failover = 1;
2417 }
2418 } else {
2419 slave->delay--;
2420 }
2421 }
2422 break;
2423 }
2424
2425 bond_update_speed_duplex(slave);
2426
2427 if (bond_mode == BOND_MODE_8023AD) {
2428 if (old_speed != slave->speed) {
2429 bond_3ad_adapter_speed_changed(slave);
2430 }
2431 if (old_duplex != slave->duplex) {
2432 bond_3ad_adapter_duplex_changed(slave);
2433 }
2434 }
2435
2436 }
2437
2438 if (do_failover) {
2439 write_lock(&bond->ptrlock);
2440
2441 reselect_active_interface(bond);
2442 if (oldcurrent && !bond->current_slave) {
2443 printk(KERN_INFO
2444 "%s: now running without any active interface !\n",
2445 master->name);
2446 }
2447
2448 write_unlock(&bond->ptrlock);
2449 }
2450
2451 read_unlock(&bond->lock);
2452
2453 mod_timer(&bond->mii_timer, jiffies + (miimon * HZ / 1000));
2454}
2455
2456
2457
2458
2459
2460
2461
2462
2463static void loadbalance_arp_monitor(struct net_device *master)
2464{
2465 bonding_t *bond;
2466 slave_t *slave, *oldcurrent;
2467 int the_delta_in_ticks = arp_interval * HZ / 1000;
2468 int next_timer = jiffies + (arp_interval * HZ / 1000);
2469 int do_failover = 0;
2470
2471 bond = (struct bonding *) master->priv;
2472 if (master->priv == NULL) {
2473 mod_timer(&bond->arp_timer, next_timer);
2474 return;
2475 }
2476
2477
2478
2479
2480 if ((!IS_UP(master)) || rtnl_shlock_nowait()) {
2481 mod_timer(&bond->arp_timer, next_timer);
2482 return;
2483 }
2484
2485 if (rtnl_exlock_nowait()) {
2486 rtnl_shunlock();
2487 mod_timer(&bond->arp_timer, next_timer);
2488 return;
2489 }
2490
2491 read_lock(&bond->lock);
2492
2493 read_lock(&bond->ptrlock);
2494 oldcurrent = bond->current_slave;
2495 read_unlock(&bond->ptrlock);
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505 slave = (slave_t *)bond;
2506 while ((slave = slave->prev) != (slave_t *)bond) {
2507
2508 if (slave->link != BOND_LINK_UP) {
2509
2510 if (((jiffies - slave->dev->trans_start) <=
2511 the_delta_in_ticks) &&
2512 ((jiffies - slave->dev->last_rx) <=
2513 the_delta_in_ticks)) {
2514
2515 slave->link = BOND_LINK_UP;
2516 slave->state = BOND_STATE_ACTIVE;
2517
2518
2519
2520
2521
2522
2523 if (oldcurrent == NULL) {
2524 printk(KERN_INFO
2525 "%s: link status definitely up "
2526 "for interface %s, ",
2527 master->name,
2528 slave->dev->name);
2529 do_failover = 1;
2530 } else {
2531 printk(KERN_INFO
2532 "%s: interface %s is now up\n",
2533 master->name,
2534 slave->dev->name);
2535 }
2536 }
2537 } else {
2538
2539
2540
2541
2542
2543
2544 if (((jiffies - slave->dev->trans_start) >=
2545 (2*the_delta_in_ticks)) ||
2546 (((jiffies - slave->dev->last_rx) >=
2547 (2*the_delta_in_ticks)) && my_ip !=0)) {
2548 slave->link = BOND_LINK_DOWN;
2549 slave->state = BOND_STATE_BACKUP;
2550 if (slave->link_failure_count < UINT_MAX) {
2551 slave->link_failure_count++;
2552 }
2553 printk(KERN_INFO
2554 "%s: interface %s is now down.\n",
2555 master->name,
2556 slave->dev->name);
2557
2558 if (slave == oldcurrent) {
2559 do_failover = 1;
2560 }
2561 }
2562 }
2563
2564
2565
2566
2567
2568
2569
2570
2571 if (IS_UP(slave->dev)) {
2572 arp_send_all(slave);
2573 }
2574 }
2575
2576 if (do_failover) {
2577 write_lock(&bond->ptrlock);
2578
2579 reselect_active_interface(bond);
2580 if (oldcurrent && !bond->current_slave) {
2581 printk(KERN_INFO
2582 "%s: now running without any active interface !\n",
2583 master->name);
2584 }
2585
2586 write_unlock(&bond->ptrlock);
2587 }
2588
2589 read_unlock(&bond->lock);
2590 rtnl_exunlock();
2591 rtnl_shunlock();
2592
2593
2594 mod_timer(&bond->arp_timer, next_timer);
2595}
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612static void activebackup_arp_monitor(struct net_device *master)
2613{
2614 bonding_t *bond;
2615 slave_t *slave;
2616 int the_delta_in_ticks = arp_interval * HZ / 1000;
2617 int next_timer = jiffies + (arp_interval * HZ / 1000);
2618
2619 bond = (struct bonding *) master->priv;
2620 if (master->priv == NULL) {
2621 mod_timer(&bond->arp_timer, next_timer);
2622 return;
2623 }
2624
2625 if (!IS_UP(master)) {
2626 mod_timer(&bond->arp_timer, next_timer);
2627 return;
2628 }
2629
2630 read_lock(&bond->lock);
2631
2632
2633
2634
2635
2636
2637 slave = (slave_t *)bond;
2638 while ((slave = slave->prev) != (slave_t *)bond) {
2639
2640 if (slave->link != BOND_LINK_UP) {
2641 if ((jiffies - slave->dev->last_rx) <=
2642 the_delta_in_ticks) {
2643
2644 slave->link = BOND_LINK_UP;
2645 write_lock(&bond->ptrlock);
2646 if ((bond->current_slave == NULL) &&
2647 ((jiffies - slave->dev->trans_start) <=
2648 the_delta_in_ticks)) {
2649 change_active_interface(bond, slave);
2650 bond->current_arp_slave = NULL;
2651 } else if (bond->current_slave != slave) {
2652
2653
2654
2655
2656
2657
2658 bond_set_slave_inactive_flags(slave);
2659 bond->current_arp_slave = NULL;
2660 }
2661
2662 if (slave == bond->current_slave) {
2663 printk(KERN_INFO
2664 "%s: %s is up and now the "
2665 "active interface\n",
2666 master->name,
2667 slave->dev->name);
2668 } else {
2669 printk(KERN_INFO
2670 "%s: backup interface %s is "
2671 "now up\n",
2672 master->name,
2673 slave->dev->name);
2674 }
2675
2676 write_unlock(&bond->ptrlock);
2677 }
2678 } else {
2679 read_lock(&bond->ptrlock);
2680 if ((slave != bond->current_slave) &&
2681 (bond->current_arp_slave == NULL) &&
2682 (((jiffies - slave->dev->last_rx) >=
2683 3*the_delta_in_ticks) && (my_ip != 0))) {
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694 read_unlock(&bond->ptrlock);
2695 slave->link = BOND_LINK_DOWN;
2696 if (slave->link_failure_count < UINT_MAX) {
2697 slave->link_failure_count++;
2698 }
2699 bond_set_slave_inactive_flags(slave);
2700 printk(KERN_INFO
2701 "%s: backup interface %s is now down\n",
2702 master->name,
2703 slave->dev->name);
2704 } else {
2705 read_unlock(&bond->ptrlock);
2706 }
2707 }
2708 }
2709
2710 read_lock(&bond->ptrlock);
2711 slave = bond->current_slave;
2712 read_unlock(&bond->ptrlock);
2713
2714 if (slave != NULL) {
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724 if ((((jiffies - slave->dev->trans_start) >=
2725 (2*the_delta_in_ticks)) ||
2726 (((jiffies - slave->dev->last_rx) >=
2727 (2*the_delta_in_ticks)) && (my_ip != 0))) &&
2728 ((jiffies - slave->jiffies) >= 2*the_delta_in_ticks)) {
2729
2730 slave->link = BOND_LINK_DOWN;
2731 if (slave->link_failure_count < UINT_MAX) {
2732 slave->link_failure_count++;
2733 }
2734 printk(KERN_INFO "%s: link status down for "
2735 "active interface %s, disabling it",
2736 master->name,
2737 slave->dev->name);
2738 write_lock(&bond->ptrlock);
2739 reselect_active_interface(bond);
2740 slave = bond->current_slave;
2741 write_unlock(&bond->ptrlock);
2742 bond->current_arp_slave = slave;
2743 if (slave != NULL) {
2744 slave->jiffies = jiffies;
2745 }
2746
2747 } else if ((bond->primary_slave != NULL) &&
2748 (bond->primary_slave != slave) &&
2749 (bond->primary_slave->link == BOND_LINK_UP)) {
2750
2751 printk(KERN_INFO
2752 "%s: changing from interface %s to primary "
2753 "interface %s\n",
2754 master->name,
2755 slave->dev->name,
2756 bond->primary_slave->dev->name);
2757
2758
2759 write_lock(&bond->ptrlock);
2760 change_active_interface(bond, bond->primary_slave);
2761 write_unlock(&bond->ptrlock);
2762 slave = bond->primary_slave;
2763 slave->jiffies = jiffies;
2764 } else {
2765 bond->current_arp_slave = NULL;
2766 }
2767
2768
2769
2770
2771 if ((slave != NULL) && (my_ip != 0)) {
2772 arp_send_all(slave);
2773 }
2774 }
2775
2776
2777
2778
2779
2780 if (slave == NULL) {
2781
2782 if ((bond->current_arp_slave == NULL) ||
2783 (bond->current_arp_slave == (slave_t *)bond)) {
2784 bond->current_arp_slave = bond->prev;
2785 }
2786
2787 if (bond->current_arp_slave != (slave_t *)bond) {
2788 bond_set_slave_inactive_flags(bond->current_arp_slave);
2789 slave = bond->current_arp_slave->next;
2790
2791
2792 do {
2793 if (IS_UP(slave->dev)) {
2794 slave->link = BOND_LINK_BACK;
2795 bond_set_slave_active_flags(slave);
2796 arp_send_all(slave);
2797 slave->jiffies = jiffies;
2798 bond->current_arp_slave = slave;
2799 break;
2800 }
2801
2802
2803
2804
2805
2806
2807
2808
2809 if (slave->link == BOND_LINK_UP) {
2810 slave->link = BOND_LINK_DOWN;
2811 if (slave->link_failure_count <
2812 UINT_MAX) {
2813 slave->link_failure_count++;
2814 }
2815
2816 bond_set_slave_inactive_flags(slave);
2817 printk(KERN_INFO
2818 "%s: backup interface "
2819 "%s is now down.\n",
2820 master->name,
2821 slave->dev->name);
2822 }
2823 } while ((slave = slave->next) !=
2824 bond->current_arp_slave->next);
2825 }
2826 }
2827
2828 read_unlock(&bond->lock);
2829 mod_timer(&bond->arp_timer, next_timer);
2830}
2831
2832static int bond_sethwaddr(struct net_device *master, struct net_device *slave)
2833{
2834#ifdef BONDING_DEBUG
2835 printk(KERN_CRIT "bond_sethwaddr: master=%x\n", (unsigned int)master);
2836 printk(KERN_CRIT "bond_sethwaddr: slave=%x\n", (unsigned int)slave);
2837 printk(KERN_CRIT "bond_sethwaddr: slave->addr_len=%d\n", slave->addr_len);
2838#endif
2839 memcpy(master->dev_addr, slave->dev_addr, slave->addr_len);
2840 return 0;
2841}
2842
2843static int bond_info_query(struct net_device *master, struct ifbond *info)
2844{
2845 bonding_t *bond = (struct bonding *) master->priv;
2846 slave_t *slave;
2847
2848 info->bond_mode = bond_mode;
2849 info->num_slaves = 0;
2850 info->miimon = miimon;
2851
2852 read_lock_bh(&bond->lock);
2853 for (slave = bond->prev; slave != (slave_t *)bond; slave = slave->prev) {
2854 info->num_slaves++;
2855 }
2856 read_unlock_bh(&bond->lock);
2857
2858 return 0;
2859}
2860
2861static int bond_slave_info_query(struct net_device *master,
2862 struct ifslave *info)
2863{
2864 bonding_t *bond = (struct bonding *) master->priv;
2865 slave_t *slave;
2866 int cur_ndx = 0;
2867
2868 if (info->slave_id < 0) {
2869 return -ENODEV;
2870 }
2871
2872 read_lock_bh(&bond->lock);
2873 for (slave = bond->prev;
2874 slave != (slave_t *)bond && cur_ndx < info->slave_id;
2875 slave = slave->prev) {
2876 cur_ndx++;
2877 }
2878 read_unlock_bh(&bond->lock);
2879
2880 if (slave != (slave_t *)bond) {
2881 strcpy(info->slave_name, slave->dev->name);
2882 info->link = slave->link;
2883 info->state = slave->state;
2884 info->link_failure_count = slave->link_failure_count;
2885 } else {
2886 return -ENODEV;
2887 }
2888
2889 return 0;
2890}
2891
2892static int bond_ethtool_ioctl(struct net_device *master_dev, struct ifreq *ifr)
2893{
2894 void *addr = ifr->ifr_data;
2895 uint32_t cmd;
2896
2897 if (get_user(cmd, (uint32_t *) addr))
2898 return -EFAULT;
2899
2900 switch (cmd) {
2901
2902 case ETHTOOL_GDRVINFO:
2903 {
2904 struct ethtool_drvinfo info;
2905 char *endptr;
2906
2907 if (copy_from_user(&info, addr, sizeof(info)))
2908 return -EFAULT;
2909
2910 if (strcmp(info.driver, "ifenslave") == 0) {
2911 int new_abi_ver;
2912
2913 new_abi_ver = simple_strtoul(info.fw_version,
2914 &endptr, 0);
2915 if (*endptr) {
2916 printk(KERN_ERR
2917 "bonding: Error: got invalid ABI"
2918 " version from application\n");
2919
2920 return -EINVAL;
2921 }
2922
2923 if (orig_app_abi_ver == -1) {
2924 orig_app_abi_ver = new_abi_ver;
2925 }
2926
2927 app_abi_ver = new_abi_ver;
2928 }
2929
2930 strncpy(info.driver, DRV_NAME, 32);
2931 strncpy(info.version, DRV_VERSION, 32);
2932 snprintf(info.fw_version, 32, "%d", BOND_ABI_VERSION);
2933
2934 if (copy_to_user(addr, &info, sizeof(info)))
2935 return -EFAULT;
2936
2937 return 0;
2938 }
2939 break;
2940 default:
2941 return -EOPNOTSUPP;
2942 }
2943}
2944
2945static int bond_ioctl(struct net_device *master_dev, struct ifreq *ifr, int cmd)
2946{
2947 struct net_device *slave_dev = NULL;
2948 struct ifbond *u_binfo = NULL, k_binfo;
2949 struct ifslave *u_sinfo = NULL, k_sinfo;
2950 struct mii_ioctl_data *mii = NULL;
2951 int prev_abi_ver = orig_app_abi_ver;
2952 int ret = 0;
2953
2954#ifdef BONDING_DEBUG
2955 printk(KERN_INFO "bond_ioctl: master=%s, cmd=%d\n",
2956 master_dev->name, cmd);
2957#endif
2958
2959 switch (cmd) {
2960 case SIOCETHTOOL:
2961 return bond_ethtool_ioctl(master_dev, ifr);
2962
2963 case SIOCGMIIPHY:
2964 mii = (struct mii_ioctl_data *)&ifr->ifr_data;
2965 if (mii == NULL) {
2966 return -EINVAL;
2967 }
2968 mii->phy_id = 0;
2969
2970 case SIOCGMIIREG:
2971
2972
2973
2974
2975 mii = (struct mii_ioctl_data *)&ifr->ifr_data;
2976 if (mii == NULL) {
2977 return -EINVAL;
2978 }
2979 if (mii->reg_num == 1) {
2980 mii->val_out = bond_check_mii_link(
2981 (struct bonding *)master_dev->priv);
2982 }
2983 return 0;
2984 case BOND_INFO_QUERY_OLD:
2985 case SIOCBONDINFOQUERY:
2986 u_binfo = (struct ifbond *)ifr->ifr_data;
2987 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond))) {
2988 return -EFAULT;
2989 }
2990 ret = bond_info_query(master_dev, &k_binfo);
2991 if (ret == 0) {
2992 if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond))) {
2993 return -EFAULT;
2994 }
2995 }
2996 return ret;
2997 case BOND_SLAVE_INFO_QUERY_OLD:
2998 case SIOCBONDSLAVEINFOQUERY:
2999 u_sinfo = (struct ifslave *)ifr->ifr_data;
3000 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave))) {
3001 return -EFAULT;
3002 }
3003 ret = bond_slave_info_query(master_dev, &k_sinfo);
3004 if (ret == 0) {
3005 if (copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave))) {
3006 return -EFAULT;
3007 }
3008 }
3009 return ret;
3010 }
3011
3012 if (!capable(CAP_NET_ADMIN)) {
3013 return -EPERM;
3014 }
3015
3016 if (orig_app_abi_ver == -1) {
3017
3018
3019
3020 orig_app_abi_ver = app_abi_ver;
3021
3022 } else if (orig_app_abi_ver != app_abi_ver) {
3023 printk(KERN_ERR
3024 "bonding: Error: already using ifenslave ABI "
3025 "version %d; to upgrade ifenslave to version %d, "
3026 "you must first reload bonding.\n",
3027 orig_app_abi_ver, app_abi_ver);
3028 return -EINVAL;
3029 }
3030
3031 slave_dev = dev_get_by_name(ifr->ifr_slave);
3032
3033#ifdef BONDING_DEBUG
3034 printk(KERN_INFO "slave_dev=%x: \n", (unsigned int)slave_dev);
3035 printk(KERN_INFO "slave_dev->name=%s: \n", slave_dev->name);
3036#endif
3037
3038 if (slave_dev == NULL) {
3039 ret = -ENODEV;
3040 } else {
3041 switch (cmd) {
3042 case BOND_ENSLAVE_OLD:
3043 case SIOCBONDENSLAVE:
3044 ret = bond_enslave(master_dev, slave_dev);
3045 break;
3046 case BOND_RELEASE_OLD:
3047 case SIOCBONDRELEASE:
3048 ret = bond_release(master_dev, slave_dev);
3049 break;
3050 case BOND_SETHWADDR_OLD:
3051 case SIOCBONDSETHWADDR:
3052 ret = bond_sethwaddr(master_dev, slave_dev);
3053 break;
3054 case BOND_CHANGE_ACTIVE_OLD:
3055 case SIOCBONDCHANGEACTIVE:
3056 if (USES_PRIMARY(bond_mode)) {
3057 ret = bond_change_active(master_dev, slave_dev);
3058 }
3059 else {
3060 ret = -EINVAL;
3061 }
3062 break;
3063 default:
3064 ret = -EOPNOTSUPP;
3065 }
3066 dev_put(slave_dev);
3067 }
3068
3069 if (ret < 0) {
3070
3071
3072
3073
3074 orig_app_abi_ver = prev_abi_ver;
3075 }
3076
3077 return ret;
3078}
3079
3080#ifdef CONFIG_NET_FASTROUTE
3081static int bond_accept_fastpath(struct net_device *dev, struct dst_entry *dst)
3082{
3083 return -1;
3084}
3085#endif
3086
3087
3088
3089
3090static int bond_xmit_broadcast(struct sk_buff *skb, struct net_device *dev)
3091{
3092 slave_t *slave, *start_at;
3093 struct bonding *bond = (struct bonding *) dev->priv;
3094 struct net_device *device_we_should_send_to = 0;
3095
3096 if (!IS_UP(dev)) {
3097 dev_kfree_skb(skb);
3098 return 0;
3099 }
3100
3101 read_lock(&bond->lock);
3102
3103 read_lock(&bond->ptrlock);
3104 slave = start_at = bond->current_slave;
3105 read_unlock(&bond->ptrlock);
3106
3107 if (slave == NULL) {
3108
3109 read_unlock(&bond->lock);
3110 dev_kfree_skb(skb);
3111 return 0;
3112 }
3113
3114 do {
3115 if (IS_UP(slave->dev)
3116 && (slave->link == BOND_LINK_UP)
3117 && (slave->state == BOND_STATE_ACTIVE)) {
3118 if (device_we_should_send_to) {
3119 struct sk_buff *skb2;
3120 if ((skb2 = skb_clone(skb, GFP_ATOMIC)) == NULL) {
3121 printk(KERN_ERR "bond_xmit_broadcast: skb_clone() failed\n");
3122 continue;
3123 }
3124
3125 skb2->dev = device_we_should_send_to;
3126 skb2->priority = 1;
3127 dev_queue_xmit(skb2);
3128 }
3129 device_we_should_send_to = slave->dev;
3130 }
3131 } while ((slave = slave->next) != start_at);
3132
3133 if (device_we_should_send_to) {
3134 skb->dev = device_we_should_send_to;
3135 skb->priority = 1;
3136 dev_queue_xmit(skb);
3137 } else
3138 dev_kfree_skb(skb);
3139
3140
3141 read_unlock(&bond->lock);
3142 return 0;
3143}
3144
3145static int bond_xmit_roundrobin(struct sk_buff *skb, struct net_device *dev)
3146{
3147 slave_t *slave, *start_at;
3148 struct bonding *bond = (struct bonding *) dev->priv;
3149
3150 if (!IS_UP(dev)) {
3151 dev_kfree_skb(skb);
3152 return 0;
3153 }
3154
3155 read_lock(&bond->lock);
3156
3157 read_lock(&bond->ptrlock);
3158 slave = start_at = bond->current_slave;
3159 read_unlock(&bond->ptrlock);
3160
3161 if (slave == NULL) {
3162
3163 dev_kfree_skb(skb);
3164 read_unlock(&bond->lock);
3165 return 0;
3166 }
3167
3168 do {
3169 if (IS_UP(slave->dev)
3170 && (slave->link == BOND_LINK_UP)
3171 && (slave->state == BOND_STATE_ACTIVE)) {
3172
3173 skb->dev = slave->dev;
3174 skb->priority = 1;
3175 dev_queue_xmit(skb);
3176
3177 write_lock(&bond->ptrlock);
3178 bond->current_slave = slave->next;
3179 write_unlock(&bond->ptrlock);
3180
3181 read_unlock(&bond->lock);
3182 return 0;
3183 }
3184 } while ((slave = slave->next) != start_at);
3185
3186
3187 dev_kfree_skb(skb);
3188 read_unlock(&bond->lock);
3189 return 0;
3190}
3191
3192
3193
3194
3195
3196
3197static int bond_xmit_xor(struct sk_buff *skb, struct net_device *dev)
3198{
3199 slave_t *slave, *start_at;
3200 struct bonding *bond = (struct bonding *) dev->priv;
3201 struct ethhdr *data = (struct ethhdr *)skb->data;
3202 int slave_no;
3203
3204 if (!IS_UP(dev)) {
3205 dev_kfree_skb(skb);
3206 return 0;
3207 }
3208
3209 read_lock(&bond->lock);
3210 slave = bond->prev;
3211
3212
3213 if (bond->slave_cnt == 0) {
3214
3215 dev_kfree_skb(skb);
3216 read_unlock(&bond->lock);
3217 return 0;
3218 }
3219
3220 slave_no = (data->h_dest[5]^slave->dev->dev_addr[5]) % bond->slave_cnt;
3221
3222 while ( (slave_no > 0) && (slave != (slave_t *)bond) ) {
3223 slave = slave->prev;
3224 slave_no--;
3225 }
3226 start_at = slave;
3227
3228 do {
3229 if (IS_UP(slave->dev)
3230 && (slave->link == BOND_LINK_UP)
3231 && (slave->state == BOND_STATE_ACTIVE)) {
3232
3233 skb->dev = slave->dev;
3234 skb->priority = 1;
3235 dev_queue_xmit(skb);
3236
3237 read_unlock(&bond->lock);
3238 return 0;
3239 }
3240 } while ((slave = slave->next) != start_at);
3241
3242
3243 dev_kfree_skb(skb);
3244 read_unlock(&bond->lock);
3245 return 0;
3246}
3247
3248
3249
3250
3251
3252static int bond_xmit_activebackup(struct sk_buff *skb, struct net_device *dev)
3253{
3254 struct bonding *bond = (struct bonding *) dev->priv;
3255 int ret;
3256
3257 if (!IS_UP(dev)) {
3258 dev_kfree_skb(skb);
3259 return 0;
3260 }
3261
3262
3263
3264 if ( (arp_interval > 0) && (my_ip == 0) &&
3265 (skb->protocol == __constant_htons(ETH_P_ARP) ) ) {
3266 char *the_ip = (((char *)skb->data))
3267 + sizeof(struct ethhdr)
3268 + sizeof(struct arphdr) +
3269 ETH_ALEN;
3270 memcpy(&my_ip, the_ip, 4);
3271 }
3272
3273
3274
3275
3276
3277
3278
3279
3280 if ( (bond_mode != BOND_MODE_ACTIVEBACKUP) &&
3281 (arp_ip_count == 1) &&
3282 (arp_interval > 0) && (arp_target_hw_addr == NULL) &&
3283 (skb->protocol == __constant_htons(ETH_P_IP) ) ) {
3284 struct ethhdr *eth_hdr =
3285 (struct ethhdr *) (((char *)skb->data));
3286 struct iphdr *ip_hdr = (struct iphdr *)(eth_hdr + 1);
3287
3288 if (arp_target[0] == ip_hdr->daddr) {
3289 arp_target_hw_addr = kmalloc(ETH_ALEN, GFP_KERNEL);
3290 if (arp_target_hw_addr != NULL)
3291 memcpy(arp_target_hw_addr, eth_hdr->h_dest, ETH_ALEN);
3292 }
3293 }
3294
3295 read_lock(&bond->lock);
3296
3297 read_lock(&bond->ptrlock);
3298 if (bond->current_slave != NULL) {
3299 skb->dev = bond->current_slave->dev;
3300 read_unlock(&bond->ptrlock);
3301 skb->priority = 1;
3302 ret = dev_queue_xmit(skb);
3303 read_unlock(&bond->lock);
3304 return 0;
3305 }
3306 else {
3307 read_unlock(&bond->ptrlock);
3308 }
3309
3310
3311#ifdef BONDING_DEBUG
3312 printk(KERN_INFO "There was no suitable interface, so we don't transmit\n");
3313#endif
3314 dev_kfree_skb(skb);
3315 read_unlock(&bond->lock);
3316 return 0;
3317}
3318
3319static struct net_device_stats *bond_get_stats(struct net_device *dev)
3320{
3321 bonding_t *bond = dev->priv;
3322 struct net_device_stats *stats = &(bond->stats), *sstats;
3323 slave_t *slave;
3324
3325 memset(stats, 0, sizeof(struct net_device_stats));
3326
3327 read_lock_bh(&bond->lock);
3328
3329 for (slave = bond->prev; slave != (slave_t *)bond; slave = slave->prev) {
3330 sstats = slave->dev->get_stats(slave->dev);
3331
3332 stats->rx_packets += sstats->rx_packets;
3333 stats->rx_bytes += sstats->rx_bytes;
3334 stats->rx_errors += sstats->rx_errors;
3335 stats->rx_dropped += sstats->rx_dropped;
3336
3337 stats->tx_packets += sstats->tx_packets;
3338 stats->tx_bytes += sstats->tx_bytes;
3339 stats->tx_errors += sstats->tx_errors;
3340 stats->tx_dropped += sstats->tx_dropped;
3341
3342 stats->multicast += sstats->multicast;
3343 stats->collisions += sstats->collisions;
3344
3345 stats->rx_length_errors += sstats->rx_length_errors;
3346 stats->rx_over_errors += sstats->rx_over_errors;
3347 stats->rx_crc_errors += sstats->rx_crc_errors;
3348 stats->rx_frame_errors += sstats->rx_frame_errors;
3349 stats->rx_fifo_errors += sstats->rx_fifo_errors;
3350 stats->rx_missed_errors += sstats->rx_missed_errors;
3351
3352 stats->tx_aborted_errors += sstats->tx_aborted_errors;
3353 stats->tx_carrier_errors += sstats->tx_carrier_errors;
3354 stats->tx_fifo_errors += sstats->tx_fifo_errors;
3355 stats->tx_heartbeat_errors += sstats->tx_heartbeat_errors;
3356 stats->tx_window_errors += sstats->tx_window_errors;
3357
3358 }
3359
3360 read_unlock_bh(&bond->lock);
3361 return stats;
3362}
3363
3364#ifdef CONFIG_PROC_FS
3365
3366#define SEQ_START_TOKEN ((void *)1)
3367
3368static void *bond_info_seq_start(struct seq_file *seq, loff_t *pos)
3369{
3370 struct bonding *bond = seq->private;
3371 loff_t off = 0;
3372 struct slave *slave;
3373
3374
3375 read_lock(&dev_base_lock);
3376 read_lock_bh(&bond->lock);
3377
3378 if (*pos == 0) {
3379 return SEQ_START_TOKEN;
3380 }
3381
3382 for (slave = bond->prev; slave != (slave_t *)bond;
3383 slave = slave->prev) {
3384
3385 if (++off == *pos) {
3386 return slave;
3387 }
3388 }
3389
3390 return NULL;
3391}
3392
3393static void *bond_info_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3394{
3395 struct bonding *bond = seq->private;
3396 struct slave *slave = v;
3397
3398 ++*pos;
3399 if (v == SEQ_START_TOKEN) {
3400 slave = bond->prev;
3401 } else {
3402 slave = slave->prev;
3403 }
3404
3405 return (slave == (struct slave *) bond) ? NULL : slave;
3406}
3407
3408static void bond_info_seq_stop(struct seq_file *seq, void *v)
3409{
3410 struct bonding *bond = seq->private;
3411
3412 read_unlock_bh(&bond->lock);
3413 read_unlock(&dev_base_lock);
3414}
3415
3416static void bond_info_show_master(struct seq_file *seq, struct bonding *bond)
3417{
3418 struct slave *curr;
3419
3420 read_lock(&bond->ptrlock);
3421 curr = bond->current_slave;
3422 read_unlock(&bond->ptrlock);
3423
3424 seq_printf(seq, "Bonding Mode: %s\n", bond_mode_name());
3425
3426 if (USES_PRIMARY(bond_mode)) {
3427 if (curr) {
3428 seq_printf(seq,
3429 "Currently Active Slave: %s\n",
3430 curr->dev->name);
3431 }
3432 }
3433
3434 seq_printf(seq, "MII Status: %s\n", (curr) ? "up" : "down");
3435 seq_printf(seq, "MII Polling Interval (ms): %d\n", miimon);
3436 seq_printf(seq, "Up Delay (ms): %d\n", updelay * miimon);
3437 seq_printf(seq, "Down Delay (ms): %d\n", downdelay * miimon);
3438 seq_printf(seq, "Multicast Mode: %s\n", multicast_mode_name());
3439
3440 if (bond_mode == BOND_MODE_8023AD) {
3441 struct ad_info ad_info;
3442
3443 seq_puts(seq, "\n802.3ad info\n");
3444
3445 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
3446 seq_printf(seq, "bond %s has no active aggregator\n",
3447 bond->device->name);
3448 } else {
3449 seq_printf(seq, "Active Aggregator Info:\n");
3450
3451 seq_printf(seq, "\tAggregator ID: %d\n",
3452 ad_info.aggregator_id);
3453 seq_printf(seq, "\tNumber of ports: %d\n",
3454 ad_info.ports);
3455 seq_printf(seq, "\tActor Key: %d\n",
3456 ad_info.actor_key);
3457 seq_printf(seq, "\tPartner Key: %d\n",
3458 ad_info.partner_key);
3459 seq_printf(seq, "\tPartner Mac Address: %02x:%02x:%02x:%02x:%02x:%02x\n",
3460 ad_info.partner_system[0],
3461 ad_info.partner_system[1],
3462 ad_info.partner_system[2],
3463 ad_info.partner_system[3],
3464 ad_info.partner_system[4],
3465 ad_info.partner_system[5]);
3466 }
3467 }
3468}
3469
3470static void bond_info_show_slave(struct seq_file *seq, const struct slave *slave)
3471{
3472 seq_printf(seq, "\nSlave Interface: %s\n", slave->dev->name);
3473 seq_printf(seq, "MII Status: %s\n",
3474 (slave->link == BOND_LINK_UP) ? "up" : "down");
3475 seq_printf(seq, "Link Failure Count: %d\n",
3476 slave->link_failure_count);
3477
3478 if (app_abi_ver >= 1) {
3479 seq_printf(seq,
3480 "Permanent HW addr: %02x:%02x:%02x:%02x:%02x:%02x\n",
3481 slave->perm_hwaddr[0],
3482 slave->perm_hwaddr[1],
3483 slave->perm_hwaddr[2],
3484 slave->perm_hwaddr[3],
3485 slave->perm_hwaddr[4],
3486 slave->perm_hwaddr[5]);
3487 }
3488
3489 if (bond_mode == BOND_MODE_8023AD) {
3490 const struct aggregator *agg
3491 = SLAVE_AD_INFO(slave).port.aggregator;
3492
3493 if (agg) {
3494 seq_printf(seq, "Aggregator ID: %d\n",
3495 agg->aggregator_identifier);
3496 } else {
3497 seq_puts(seq, "Aggregator ID: N/A\n");
3498 }
3499 }
3500}
3501
3502static int bond_info_seq_show(struct seq_file *seq, void *v)
3503{
3504 if (v == SEQ_START_TOKEN) {
3505 seq_printf(seq, "%s\n", version);
3506 bond_info_show_master(seq, seq->private);
3507 } else {
3508 bond_info_show_slave(seq, v);
3509 }
3510
3511 return 0;
3512}
3513
3514static struct seq_operations bond_info_seq_ops = {
3515 .start = bond_info_seq_start,
3516 .next = bond_info_seq_next,
3517 .stop = bond_info_seq_stop,
3518 .show = bond_info_seq_show,
3519};
3520
3521static int bond_info_open(struct inode *inode, struct file *file)
3522{
3523 struct seq_file *seq;
3524 struct proc_dir_entry *proc;
3525 int rc;
3526
3527 rc = seq_open(file, &bond_info_seq_ops);
3528 if (!rc) {
3529
3530 seq = file->private_data;
3531 proc = PDE(inode);
3532 seq->private = proc->data;
3533 }
3534 return rc;
3535}
3536
3537static struct file_operations bond_info_fops = {
3538 .owner = THIS_MODULE,
3539 .open = bond_info_open,
3540 .read = seq_read,
3541 .llseek = seq_lseek,
3542 .release = seq_release,
3543};
3544
3545static int bond_create_proc_info(struct bonding *bond)
3546{
3547 struct net_device *dev = bond->device;
3548
3549 if (bond_proc_dir) {
3550 bond->bond_proc_file = create_proc_entry(dev->name,
3551 S_IRUGO,
3552 bond_proc_dir);
3553 if (bond->bond_proc_file == NULL) {
3554 printk(KERN_WARNING
3555 "%s: Cannot create /proc/net/bonding/%s\n",
3556 dev->name, dev->name);
3557 } else {
3558 bond->bond_proc_file->data = bond;
3559 bond->bond_proc_file->proc_fops = &bond_info_fops;
3560 bond->bond_proc_file->owner = THIS_MODULE;
3561 memcpy(bond->procdir_name, dev->name, IFNAMSIZ);
3562 }
3563 }
3564
3565 return 0;
3566}
3567
3568static void bond_destroy_proc_info(struct bonding *bond)
3569{
3570 if (bond_proc_dir && bond->bond_proc_file) {
3571 remove_proc_entry(bond->procdir_name, bond_proc_dir);
3572 memset(bond->procdir_name, 0, IFNAMSIZ);
3573 bond->bond_proc_file = NULL;
3574 }
3575}
3576
3577
3578
3579
3580static void bond_create_proc_dir(void)
3581{
3582 int len = strlen(DRV_NAME);
3583
3584 for (bond_proc_dir = proc_net->subdir; bond_proc_dir;
3585 bond_proc_dir = bond_proc_dir->next) {
3586 if ((bond_proc_dir->namelen == len) &&
3587 !memcmp(bond_proc_dir->name, DRV_NAME, len)) {
3588 break;
3589 }
3590 }
3591
3592 if (!bond_proc_dir) {
3593 bond_proc_dir = proc_mkdir(DRV_NAME, proc_net);
3594 if (bond_proc_dir) {
3595 bond_proc_dir->owner = THIS_MODULE;
3596 } else {
3597 printk(KERN_WARNING DRV_NAME
3598 ": Warning: cannot create /proc/net/%s\n",
3599 DRV_NAME);
3600 }
3601 }
3602}
3603
3604
3605
3606
3607static void bond_destroy_proc_dir(void)
3608{
3609 struct proc_dir_entry *de;
3610
3611 if (!bond_proc_dir) {
3612 return;
3613 }
3614
3615
3616 for (de = bond_proc_dir->subdir; de; de = de->next) {
3617
3618 if (*(de->name) != '.') {
3619 break;
3620 }
3621 }
3622
3623 if (de) {
3624 if (bond_proc_dir->owner == THIS_MODULE) {
3625 bond_proc_dir->owner = NULL;
3626 }
3627 } else {
3628 remove_proc_entry(DRV_NAME, proc_net);
3629 bond_proc_dir = NULL;
3630 }
3631}
3632#endif
3633
3634
3635
3636
3637
3638
3639
3640
3641static inline int
3642bond_set_mac_address(struct net_device *dev, void *addr)
3643{
3644 struct bonding *bond = dev->priv;
3645 struct sockaddr *sa = addr, tmp_sa;
3646 struct slave *slave;
3647 int error;
3648
3649 dprintk(KERN_INFO "bond_set_mac_address %p %s\n", dev,
3650 dev->name);
3651
3652 if (!is_valid_ether_addr(sa->sa_data)) {
3653 return -EADDRNOTAVAIL;
3654 }
3655
3656 for (slave = bond->prev; slave != (struct slave *)bond;
3657 slave = slave->prev) {
3658 dprintk(KERN_INFO "bond_set_mac: slave %p %s\n", slave,
3659 slave->dev->name);
3660 if (slave->dev->set_mac_address == NULL) {
3661 error = -EOPNOTSUPP;
3662 dprintk(KERN_INFO "bond_set_mac EOPNOTSUPP %s\n",
3663 slave->dev->name);
3664 goto unwind;
3665 }
3666
3667 error = slave->dev->set_mac_address(slave->dev, addr);
3668 if (error) {
3669
3670
3671
3672
3673
3674
3675 dprintk(KERN_INFO "bond_set_mac err %d %s\n",
3676 error, slave->dev->name);
3677 goto unwind;
3678 }
3679 }
3680
3681
3682 memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
3683 return 0;
3684
3685unwind:
3686 memcpy(tmp_sa.sa_data, dev->dev_addr, dev->addr_len);
3687 tmp_sa.sa_family = dev->type;
3688
3689 for (slave = slave->next; slave != bond->next;
3690 slave = slave->next) {
3691 int tmp_error;
3692
3693 tmp_error = slave->dev->set_mac_address(slave->dev, &tmp_sa);
3694 if (tmp_error) {
3695 dprintk(KERN_INFO "bond_set_mac_address: "
3696 "unwind err %d dev %s\n",
3697 tmp_error, slave->dev->name);
3698 }
3699 }
3700
3701 return error;
3702}
3703
3704
3705
3706
3707static inline int
3708bond_change_mtu(struct net_device *dev, int newmtu)
3709{
3710 bonding_t *bond = dev->priv;
3711 slave_t *slave;
3712 int error;
3713
3714 dprintk(KERN_INFO "CM: b %p nm %d\n", bond, newmtu);
3715 for (slave = bond->prev; slave != (slave_t *)bond;
3716 slave = slave->prev) {
3717 dprintk(KERN_INFO "CM: s %p s->p %p c_m %p\n", slave,
3718 slave->prev, slave->dev->change_mtu);
3719 if (slave->dev->change_mtu) {
3720 error = slave->dev->change_mtu(slave->dev, newmtu);
3721 } else {
3722 slave->dev->mtu = newmtu;
3723 error = 0;
3724 }
3725
3726 if (error) {
3727
3728
3729
3730
3731
3732
3733
3734
3735 dprintk(KERN_INFO "bond_change_mtu err %d %s\n",
3736 error, slave->dev->name);
3737 goto unwind;
3738 }
3739 }
3740
3741 dev->mtu = newmtu;
3742 return 0;
3743
3744
3745unwind:
3746 for (slave = slave->next; slave != bond->next;
3747 slave = slave->next) {
3748
3749 if (slave->dev->change_mtu) {
3750 slave->dev->change_mtu(slave->dev, dev->mtu);
3751 } else {
3752 slave->dev->mtu = dev->mtu;
3753 }
3754 }
3755
3756 return error;
3757}
3758
3759
3760
3761
3762static inline int bond_event_changename(struct bonding *bond)
3763{
3764#ifdef CONFIG_PROC_FS
3765 bond_destroy_proc_info(bond);
3766 bond_create_proc_info(bond);
3767#endif
3768
3769 return NOTIFY_DONE;
3770}
3771
3772static int bond_master_netdev_event(unsigned long event, struct net_device *event_dev)
3773{
3774 struct bonding *bond, *event_bond = NULL;
3775
3776 list_for_each_entry(bond, &bond_dev_list, bond_list) {
3777 if (bond == event_dev->priv) {
3778 event_bond = bond;
3779 break;
3780 }
3781 }
3782
3783 if (event_bond == NULL) {
3784 return NOTIFY_DONE;
3785 }
3786
3787 switch (event) {
3788 case NETDEV_CHANGENAME:
3789 return bond_event_changename(event_bond);
3790 case NETDEV_UNREGISTER:
3791
3792
3793
3794 break;
3795 default:
3796 break;
3797 }
3798
3799 return NOTIFY_DONE;
3800}
3801
3802static int bond_slave_netdev_event(unsigned long event, struct net_device *event_dev)
3803{
3804 struct net_device *master = event_dev->master;
3805
3806 switch (event) {
3807 case NETDEV_UNREGISTER:
3808 if (master != NULL) {
3809 bond_release(master, event_dev);
3810 }
3811 break;
3812 case NETDEV_CHANGE:
3813
3814
3815
3816
3817
3818 break;
3819 case NETDEV_DOWN:
3820
3821
3822
3823 break;
3824 case NETDEV_CHANGEMTU:
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837 break;
3838 case NETDEV_CHANGENAME:
3839
3840
3841
3842 break;
3843 default:
3844 break;
3845 }
3846
3847 return NOTIFY_DONE;
3848}
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858static int bond_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
3859{
3860 struct net_device *event_dev = (struct net_device *)ptr;
3861 unsigned short flags;
3862 int res = NOTIFY_DONE;
3863
3864 dprintk(KERN_INFO "bond_netdev_event n_b %p ev %lx ptr %p\n",
3865 this, event, ptr);
3866
3867 flags = event_dev->flags & (IFF_MASTER | IFF_SLAVE);
3868 switch (flags) {
3869 case IFF_MASTER:
3870 res = bond_master_netdev_event(event, event_dev);
3871 break;
3872 case IFF_SLAVE:
3873 res = bond_slave_netdev_event(event, event_dev);
3874 break;
3875 default:
3876
3877 break;
3878 }
3879
3880 return res;
3881}
3882
3883static struct notifier_block bond_netdev_notifier = {
3884 .notifier_call = bond_netdev_event,
3885};
3886
3887
3888
3889
3890static inline void bond_deinit(struct net_device *dev)
3891{
3892 struct bonding *bond = dev->priv;
3893
3894 list_del(&bond->bond_list);
3895
3896#ifdef CONFIG_PROC_FS
3897 bond_destroy_proc_info(bond);
3898#endif
3899}
3900
3901
3902
3903
3904static void bond_free_all(void)
3905{
3906 struct bonding *bond, *nxt;
3907
3908 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list) {
3909 struct net_device *dev = bond->device;
3910
3911 unregister_netdevice(dev);
3912 bond_deinit(dev);
3913 free_netdev(dev);
3914 }
3915
3916#ifdef CONFIG_PROC_FS
3917 bond_destroy_proc_dir();
3918#endif
3919}
3920
3921
3922
3923
3924
3925static int __init bond_init(struct net_device *dev)
3926{
3927 struct bonding *bond;
3928 int count;
3929
3930#ifdef BONDING_DEBUG
3931 printk (KERN_INFO "Begin bond_init for %s\n", dev->name);
3932#endif
3933 bond = dev->priv;
3934
3935
3936 rwlock_init(&bond->lock);
3937 rwlock_init(&bond->ptrlock);
3938
3939
3940 bond->next = bond->prev = (slave_t *)bond;
3941 bond->current_slave = NULL;
3942 bond->current_arp_slave = NULL;
3943 bond->device = dev;
3944
3945
3946 dev->set_mac_address = bond_set_mac_address;
3947
3948 switch (bond_mode) {
3949 case BOND_MODE_ACTIVEBACKUP:
3950 dev->hard_start_xmit = bond_xmit_activebackup;
3951 break;
3952 case BOND_MODE_ROUNDROBIN:
3953 dev->hard_start_xmit = bond_xmit_roundrobin;
3954 break;
3955 case BOND_MODE_XOR:
3956 dev->hard_start_xmit = bond_xmit_xor;
3957 break;
3958 case BOND_MODE_BROADCAST:
3959 dev->hard_start_xmit = bond_xmit_broadcast;
3960 break;
3961 case BOND_MODE_8023AD:
3962 dev->hard_start_xmit = bond_3ad_xmit_xor;
3963 break;
3964 case BOND_MODE_TLB:
3965 case BOND_MODE_ALB:
3966 dev->hard_start_xmit = bond_alb_xmit;
3967 dev->set_mac_address = bond_alb_set_mac_address;
3968 break;
3969 default:
3970 printk(KERN_ERR "Unknown bonding mode %d\n", bond_mode);
3971 return -EINVAL;
3972 }
3973
3974 dev->get_stats = bond_get_stats;
3975 dev->open = bond_open;
3976 dev->stop = bond_close;
3977 dev->set_multicast_list = set_multicast_list;
3978 dev->do_ioctl = bond_ioctl;
3979 dev->change_mtu = bond_change_mtu;
3980 dev->tx_queue_len = 0;
3981 dev->flags |= IFF_MASTER|IFF_MULTICAST;
3982#ifdef CONFIG_NET_FASTROUTE
3983 dev->accept_fastpath = bond_accept_fastpath;
3984#endif
3985
3986 printk(KERN_INFO "%s registered with", dev->name);
3987 if (miimon > 0) {
3988 printk(" MII link monitoring set to %d ms", miimon);
3989 updelay /= miimon;
3990 downdelay /= miimon;
3991 } else {
3992 printk("out MII link monitoring");
3993 }
3994 printk(", in %s mode.\n", bond_mode_name());
3995
3996 printk(KERN_INFO "%s registered with", dev->name);
3997 if (arp_interval > 0) {
3998 printk(" ARP monitoring set to %d ms with %d target(s):",
3999 arp_interval, arp_ip_count);
4000 for (count=0 ; count<arp_ip_count ; count++)
4001 printk (" %s", arp_ip_target[count]);
4002 printk("\n");
4003 } else {
4004 printk("out ARP monitoring\n");
4005 }
4006
4007#ifdef CONFIG_PROC_FS
4008 bond_create_proc_info(bond);
4009#endif
4010
4011 list_add_tail(&bond->bond_list, &bond_dev_list);
4012
4013 return 0;
4014}
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028static inline int
4029bond_parse_parm(char *mode_arg, struct bond_parm_tbl *tbl)
4030{
4031 int i;
4032
4033 for (i = 0; tbl[i].modename != NULL; i++) {
4034 if ((isdigit(*mode_arg) &&
4035 tbl[i].mode == simple_strtol(mode_arg, NULL, 0)) ||
4036 (0 == strncmp(mode_arg, tbl[i].modename,
4037 strlen(tbl[i].modename)))) {
4038 return tbl[i].mode;
4039 }
4040 }
4041
4042 return -1;
4043}
4044
4045
4046static int __init bonding_init(void)
4047{
4048 int no;
4049 int err;
4050
4051 printk(KERN_INFO "%s", version);
4052
4053
4054
4055
4056 if (mode) {
4057 bond_mode = bond_parse_parm(mode, bond_mode_tbl);
4058 if (bond_mode == -1) {
4059 printk(KERN_WARNING
4060 "bonding_init(): Invalid bonding mode \"%s\"\n",
4061 mode == NULL ? "NULL" : mode);
4062 return -EINVAL;
4063 }
4064 }
4065
4066 if (USES_PRIMARY(bond_mode)) {
4067 multicast_mode = BOND_MULTICAST_ACTIVE;
4068 } else {
4069 multicast_mode = BOND_MULTICAST_ALL;
4070 }
4071
4072 if (multicast) {
4073 multicast_mode = bond_parse_parm(multicast, bond_mc_tbl);
4074 if (multicast_mode == -1) {
4075 printk(KERN_WARNING
4076 "bonding_init(): Invalid multicast mode \"%s\"\n",
4077 multicast == NULL ? "NULL" : multicast);
4078 return -EINVAL;
4079 }
4080 }
4081
4082 if (lacp_rate) {
4083 if (bond_mode != BOND_MODE_8023AD) {
4084 printk(KERN_WARNING
4085 "lacp_rate param is irrelevant in mode %s\n",
4086 bond_mode_name());
4087 } else {
4088 lacp_fast = bond_parse_parm(lacp_rate, bond_lacp_tbl);
4089 if (lacp_fast == -1) {
4090 printk(KERN_WARNING
4091 "bonding_init(): Invalid lacp rate "
4092 "\"%s\"\n",
4093 lacp_rate == NULL ? "NULL" : lacp_rate);
4094
4095 return -EINVAL;
4096 }
4097 }
4098 }
4099
4100 if (max_bonds < 1 || max_bonds > INT_MAX) {
4101 printk(KERN_WARNING
4102 "bonding_init(): max_bonds (%d) not in range %d-%d, "
4103 "so it was reset to BOND_DEFAULT_MAX_BONDS (%d)",
4104 max_bonds, 1, INT_MAX, BOND_DEFAULT_MAX_BONDS);
4105 max_bonds = BOND_DEFAULT_MAX_BONDS;
4106 }
4107
4108 if (miimon < 0) {
4109 printk(KERN_WARNING
4110 "bonding_init(): miimon module parameter (%d), "
4111 "not in range 0-%d, so it was reset to %d\n",
4112 miimon, INT_MAX, BOND_LINK_MON_INTERV);
4113 miimon = BOND_LINK_MON_INTERV;
4114 }
4115
4116 if (updelay < 0) {
4117 printk(KERN_WARNING
4118 "bonding_init(): updelay module parameter (%d), "
4119 "not in range 0-%d, so it was reset to 0\n",
4120 updelay, INT_MAX);
4121 updelay = 0;
4122 }
4123
4124 if (downdelay < 0) {
4125 printk(KERN_WARNING
4126 "bonding_init(): downdelay module parameter (%d), "
4127 "not in range 0-%d, so it was reset to 0\n",
4128 downdelay, INT_MAX);
4129 downdelay = 0;
4130 }
4131
4132
4133 if (bond_mode == BOND_MODE_8023AD) {
4134 if (arp_interval != 0) {
4135 printk(KERN_WARNING "bonding_init(): ARP monitoring"
4136 "can't be used simultaneously with 802.3ad, "
4137 "disabling ARP monitoring\n");
4138 arp_interval = 0;
4139 }
4140
4141 if (miimon == 0) {
4142 printk(KERN_ERR
4143 "bonding_init(): miimon must be specified, "
4144 "otherwise bonding will not detect link failure, "
4145 "speed and duplex which are essential "
4146 "for 802.3ad operation\n");
4147 printk(KERN_ERR "Forcing miimon to 100msec\n");
4148 miimon = 100;
4149 }
4150
4151 if (multicast_mode != BOND_MULTICAST_ALL) {
4152 printk(KERN_ERR
4153 "bonding_init(): Multicast mode must "
4154 "be set to ALL for 802.3ad\n");
4155 printk(KERN_ERR "Forcing Multicast mode to ALL\n");
4156 multicast_mode = BOND_MULTICAST_ALL;
4157 }
4158 }
4159
4160
4161 if ((bond_mode == BOND_MODE_TLB) ||
4162 (bond_mode == BOND_MODE_ALB)) {
4163 if (miimon == 0) {
4164 printk(KERN_ERR
4165 "bonding_init(): miimon must be specified, "
4166 "otherwise bonding will not detect link failure "
4167 "and link speed which are essential "
4168 "for TLB/ALB load balancing\n");
4169 printk(KERN_ERR "Forcing miimon to 100msec\n");
4170 miimon = 100;
4171 }
4172
4173 if (multicast_mode != BOND_MULTICAST_ACTIVE) {
4174 printk(KERN_ERR
4175 "bonding_init(): Multicast mode must "
4176 "be set to ACTIVE for TLB/ALB\n");
4177 printk(KERN_ERR "Forcing Multicast mode to ACTIVE\n");
4178 multicast_mode = BOND_MULTICAST_ACTIVE;
4179 }
4180 }
4181
4182 if (bond_mode == BOND_MODE_ALB) {
4183 printk(KERN_INFO
4184 "In ALB mode you might experience client disconnections"
4185 " upon reconnection of a link if the bonding module"
4186 " updelay parameter (%d msec) is incompatible with the"
4187 " forwarding delay time of the switch\n", updelay);
4188 }
4189
4190 if (miimon == 0) {
4191 if ((updelay != 0) || (downdelay != 0)) {
4192
4193
4194
4195 printk(KERN_WARNING
4196 "bonding_init(): miimon module parameter not "
4197 "set and updelay (%d) or downdelay (%d) module "
4198 "parameter is set; updelay and downdelay have "
4199 "no effect unless miimon is set\n",
4200 updelay, downdelay);
4201 }
4202 } else {
4203
4204 if (arp_interval != 0) {
4205 printk(KERN_WARNING
4206 "bonding_init(): miimon (%d) and arp_interval "
4207 "(%d) can't be used simultaneously, "
4208 "disabling ARP monitoring\n",
4209 miimon, arp_interval);
4210 arp_interval = 0;
4211 }
4212
4213 if ((updelay % miimon) != 0) {
4214
4215
4216
4217 printk(KERN_WARNING
4218 "bonding_init(): updelay (%d) is not a multiple "
4219 "of miimon (%d), updelay rounded to %d ms\n",
4220 updelay, miimon, (updelay / miimon) * miimon);
4221 }
4222
4223 if ((downdelay % miimon) != 0) {
4224
4225
4226
4227 printk(KERN_WARNING
4228 "bonding_init(): downdelay (%d) is not a "
4229 "multiple of miimon (%d), downdelay rounded "
4230 "to %d ms\n",
4231 downdelay, miimon,
4232 (downdelay / miimon) * miimon);
4233 }
4234 }
4235
4236 if (arp_interval < 0) {
4237 printk(KERN_WARNING
4238 "bonding_init(): arp_interval module parameter (%d), "
4239 "not in range 0-%d, so it was reset to %d\n",
4240 arp_interval, INT_MAX, BOND_LINK_ARP_INTERV);
4241 arp_interval = BOND_LINK_ARP_INTERV;
4242 }
4243
4244 for (arp_ip_count=0 ;
4245 (arp_ip_count < MAX_ARP_IP_TARGETS) && arp_ip_target[arp_ip_count];
4246 arp_ip_count++ ) {
4247
4248
4249 if (!isdigit(arp_ip_target[arp_ip_count][0])) {
4250 printk(KERN_WARNING
4251 "bonding_init(): bad arp_ip_target module "
4252 "parameter (%s), ARP monitoring will not be "
4253 "performed\n",
4254 arp_ip_target[arp_ip_count]);
4255 arp_interval = 0;
4256 } else {
4257 u32 ip = in_aton(arp_ip_target[arp_ip_count]);
4258 arp_target[arp_ip_count] = ip;
4259 }
4260 }
4261
4262
4263 if ( (arp_interval > 0) && (arp_ip_count==0)) {
4264
4265 printk(KERN_WARNING
4266 "bonding_init(): arp_interval module parameter "
4267 "(%d) specified without providing an arp_ip_target "
4268 "parameter, arp_interval was reset to 0\n",
4269 arp_interval);
4270 arp_interval = 0;
4271 }
4272
4273 if ((miimon == 0) && (arp_interval == 0)) {
4274
4275
4276
4277 printk(KERN_ERR
4278 "bonding_init(): either miimon or "
4279 "arp_interval and arp_ip_target module parameters "
4280 "must be specified, otherwise bonding will not detect "
4281 "link failures! see bonding.txt for details.\n");
4282 }
4283
4284 if ((primary != NULL) && !USES_PRIMARY(bond_mode)) {
4285
4286
4287
4288 printk(KERN_WARNING
4289 "bonding_init(): %s primary device specified but has "
4290 "no effect in %s mode\n",
4291 primary, bond_mode_name());
4292 primary = NULL;
4293 }
4294
4295 rtnl_lock();
4296
4297#ifdef CONFIG_PROC_FS
4298 bond_create_proc_dir();
4299#endif
4300
4301 err = 0;
4302 for (no = 0; no < max_bonds; no++) {
4303 struct net_device *dev;
4304
4305 dev = alloc_netdev(sizeof(struct bonding), "", ether_setup);
4306 if (!dev) {
4307 err = -ENOMEM;
4308 goto out_err;
4309 }
4310
4311 err = dev_alloc_name(dev, "bond%d");
4312 if (err < 0) {
4313 free_netdev(dev);
4314 goto out_err;
4315 }
4316
4317
4318
4319
4320
4321 err = bond_init(dev);
4322 if (err < 0) {
4323 free_netdev(dev);
4324 goto out_err;
4325 }
4326
4327 SET_MODULE_OWNER(dev);
4328
4329 err = register_netdevice(dev);
4330 if (err < 0) {
4331 bond_deinit(dev);
4332 free_netdev(dev);
4333 goto out_err;
4334 }
4335 }
4336
4337 rtnl_unlock();
4338 register_netdevice_notifier(&bond_netdev_notifier);
4339
4340 return 0;
4341
4342out_err:
4343
4344 bond_free_all();
4345
4346 rtnl_unlock();
4347
4348 return err;
4349}
4350
4351static void __exit bonding_exit(void)
4352{
4353 unregister_netdevice_notifier(&bond_netdev_notifier);
4354
4355 rtnl_lock();
4356 bond_free_all();
4357 rtnl_unlock();
4358}
4359
4360module_init(bonding_init);
4361module_exit(bonding_exit);
4362MODULE_LICENSE("GPL");
4363MODULE_DESCRIPTION(DRV_DESCRIPTION ", v" DRV_VERSION);
4364
4365
4366
4367
4368
4369
4370
4371
4372