2 * Bond several ethernet interfaces into a Cisco, running 'Etherchannel'.
4 * Portions are (c) Copyright 1995 Simon "Guru Aleph-Null" Janes
5 * NCM: Network and Communications Management, Inc.
7 * BUT, I'm the one who modified it for ethernet, so:
8 * (c) Copyright 1999, Thomas Davis, tadavis@lbl.gov
10 * This software may be used and distributed according to the terms
11 * of the GNU Public License, incorporated herein by reference.
15 #ifndef _LINUX_BONDING_H
16 #define _LINUX_BONDING_H
18 #include <linux/timer.h>
19 #include <linux/proc_fs.h>
20 #include <linux/if_bonding.h>
21 #include <linux/cpumask.h>
22 #include <linux/in6.h>
23 #include <linux/netpoll.h>
24 #include <linux/inetdevice.h>
25 #include <linux/etherdevice.h>
26 #include <linux/reciprocal_div.h>
30 #include "bond_options.h"
32 #define DRV_VERSION "3.7.1"
33 #define DRV_RELDATE "April 27, 2011"
34 #define DRV_NAME "bonding"
35 #define DRV_DESCRIPTION "Ethernet Channel Bonding Driver"
37 #define bond_version DRV_DESCRIPTION ": v" DRV_VERSION " (" DRV_RELDATE ")\n"
39 #define BOND_MAX_ARP_TARGETS 16
41 #define BOND_DEFAULT_MIIMON 100
44 ((((dev)->flags & IFF_UP) == IFF_UP) && \
45 netif_running(dev) && \
46 netif_carrier_ok(dev))
49 * Checks whether slave is ready for transmit.
51 #define SLAVE_IS_OK(slave) \
52 (((slave)->dev->flags & IFF_UP) && \
53 netif_running((slave)->dev) && \
54 ((slave)->link == BOND_LINK_UP) && \
55 bond_is_active_slave(slave))
58 #define USES_PRIMARY(mode) \
59 (((mode) == BOND_MODE_ACTIVEBACKUP) || \
60 ((mode) == BOND_MODE_TLB) || \
61 ((mode) == BOND_MODE_ALB))
63 #define BOND_NO_USES_ARP(mode) \
64 (((mode) == BOND_MODE_8023AD) || \
65 ((mode) == BOND_MODE_TLB) || \
66 ((mode) == BOND_MODE_ALB))
68 #define TX_QUEUE_OVERRIDE(mode) \
69 (((mode) == BOND_MODE_ACTIVEBACKUP) || \
70 ((mode) == BOND_MODE_ROUNDROBIN))
72 #define IS_IP_TARGET_UNUSABLE_ADDRESS(a) \
73 ((htonl(INADDR_BROADCAST) == a) || \
76 * Less bad way to call ioctl from within the kernel; this needs to be
77 * done some other way to get the call out of interrupt context.
78 * Needs "ioctl" variable to be supplied by calling context.
80 #define IOCTL(dev, arg, cmd) ({ \
82 mm_segment_t fs = get_fs(); \
84 res = ioctl(dev, arg, cmd); \
88 /* slave list primitives */
89 #define bond_slave_list(bond) (&(bond)->dev->adj_list.lower)
91 #define bond_has_slaves(bond) !list_empty(bond_slave_list(bond))
93 /* IMPORTANT: bond_first/last_slave can return NULL in case of an empty list */
94 #define bond_first_slave(bond) \
95 (bond_has_slaves(bond) ? \
96 netdev_adjacent_get_private(bond_slave_list(bond)->next) : \
98 #define bond_last_slave(bond) \
99 (bond_has_slaves(bond) ? \
100 netdev_adjacent_get_private(bond_slave_list(bond)->prev) : \
103 /* Caller must have rcu_read_lock */
104 #define bond_first_slave_rcu(bond) \
105 netdev_lower_get_first_private_rcu(bond->dev)
107 #define bond_is_first_slave(bond, pos) (pos == bond_first_slave(bond))
108 #define bond_is_last_slave(bond, pos) (pos == bond_last_slave(bond))
111 * bond_for_each_slave - iterate over all slaves
112 * @bond: the bond holding this list
113 * @pos: current slave
114 * @iter: list_head * iterator
116 * Caller must hold bond->lock
118 #define bond_for_each_slave(bond, pos, iter) \
119 netdev_for_each_lower_private((bond)->dev, pos, iter)
121 /* Caller must have rcu_read_lock */
122 #define bond_for_each_slave_rcu(bond, pos, iter) \
123 netdev_for_each_lower_private_rcu((bond)->dev, pos, iter)
125 #ifdef CONFIG_NET_POLL_CONTROLLER
126 extern atomic_t netpoll_block_tx;
128 static inline void block_netpoll_tx(void)
130 atomic_inc(&netpoll_block_tx);
133 static inline void unblock_netpoll_tx(void)
135 atomic_dec(&netpoll_block_tx);
138 static inline int is_netpoll_tx_blocked(struct net_device *dev)
140 if (unlikely(netpoll_tx_running(dev)))
141 return atomic_read(&netpoll_block_tx);
145 #define block_netpoll_tx()
146 #define unblock_netpoll_tx()
147 #define is_netpoll_tx_blocked(dev) (0)
163 unsigned int min_links;
165 char primary[IFNAMSIZ];
166 int primary_reselect;
167 __be32 arp_targets[BOND_MAX_ARP_TARGETS];
169 int all_slaves_active;
172 int packets_per_slave;
174 struct reciprocal_value reciprocal_packets_per_slave;
177 struct bond_parm_tbl {
183 struct net_device *dev; /* first - useful for panic debug */
184 struct bonding *bond; /* our master */
186 /* all three in jiffies */
187 unsigned long last_link_up;
188 unsigned long last_rx;
189 unsigned long target_last_arp_rx[BOND_MAX_ARP_TARGETS];
190 s8 link; /* one of BOND_LINK_XXXX */
192 u8 backup:1, /* indicates backup slave. Value corresponds with
193 BOND_STATE_ACTIVE and BOND_STATE_BACKUP */
194 inactive:1, /* indicates inactive slave */
195 should_notify:1; /* indicateds whether the state changed */
198 u32 link_failure_count;
201 u8 perm_hwaddr[ETH_ALEN];
202 struct ad_slave_info *ad_info;
203 struct tlb_slave_info tlb_info;
204 #ifdef CONFIG_NET_POLL_CONTROLLER
211 * Link pseudo-state only used internally by monitors
213 #define BOND_LINK_NOCHANGE -1
216 * Here are the locking policies for the two bonding locks:
218 * 1) Get bond->lock when reading/writing slave list.
219 * 2) Get bond->curr_slave_lock when reading/writing bond->curr_active_slave.
220 * (It is unnecessary when the write-lock is put with bond->lock.)
221 * 3) When we lock with bond->curr_slave_lock, we must lock with bond->lock
225 struct net_device *dev; /* first - useful for panic debug */
226 struct slave *curr_active_slave;
227 struct slave *current_arp_slave;
228 struct slave *primary_slave;
230 s32 slave_cnt; /* never change this value outside the attach/detach wrappers */
231 int (*recv_probe)(const struct sk_buff *, struct bonding *,
234 rwlock_t curr_slave_lock;
237 #ifdef CONFIG_PROC_FS
238 struct proc_dir_entry *proc_entry;
239 char proc_file_name[IFNAMSIZ];
240 #endif /* CONFIG_PROC_FS */
241 struct list_head bond_list;
243 struct ad_bond_info ad_info;
244 struct alb_bond_info alb_info;
245 struct bond_params params;
246 struct workqueue_struct *wq;
247 struct delayed_work mii_work;
248 struct delayed_work arp_work;
249 struct delayed_work alb_work;
250 struct delayed_work ad_work;
251 struct delayed_work mcast_work;
252 #ifdef CONFIG_DEBUG_FS
253 /* debugging support via debugfs */
254 struct dentry *debug_dir;
255 #endif /* CONFIG_DEBUG_FS */
258 #define bond_slave_get_rcu(dev) \
259 ((struct slave *) rcu_dereference(dev->rx_handler_data))
261 #define bond_slave_get_rtnl(dev) \
262 ((struct slave *) rtnl_dereference(dev->rx_handler_data))
264 struct bond_vlan_tag {
266 unsigned short vlan_id;
270 * Returns NULL if the net_device does not belong to any of the bond's slaves
272 * Caller must hold bond lock for read
274 static inline struct slave *bond_get_slave_by_dev(struct bonding *bond,
275 struct net_device *slave_dev)
277 return netdev_lower_dev_get_private(bond->dev, slave_dev);
280 static inline struct bonding *bond_get_bond_by_slave(struct slave *slave)
282 if (!slave || !slave->bond)
287 static inline bool bond_is_lb(const struct bonding *bond)
289 return bond->params.mode == BOND_MODE_TLB ||
290 bond->params.mode == BOND_MODE_ALB;
293 static inline void bond_set_active_slave(struct slave *slave)
297 rtmsg_ifinfo(RTM_NEWLINK, slave->dev, 0, GFP_ATOMIC);
301 static inline void bond_set_backup_slave(struct slave *slave)
303 if (!slave->backup) {
305 rtmsg_ifinfo(RTM_NEWLINK, slave->dev, 0, GFP_ATOMIC);
309 static inline void bond_set_slave_state(struct slave *slave,
310 int slave_state, bool notify)
312 if (slave->backup == slave_state)
315 slave->backup = slave_state;
317 rtmsg_ifinfo(RTM_NEWLINK, slave->dev, 0, GFP_ATOMIC);
318 slave->should_notify = 0;
320 if (slave->should_notify)
321 slave->should_notify = 0;
323 slave->should_notify = 1;
327 static inline void bond_slave_state_change(struct bonding *bond)
329 struct list_head *iter;
332 bond_for_each_slave(bond, tmp, iter) {
333 if (tmp->link == BOND_LINK_UP)
334 bond_set_active_slave(tmp);
335 else if (tmp->link == BOND_LINK_DOWN)
336 bond_set_backup_slave(tmp);
340 static inline void bond_slave_state_notify(struct bonding *bond)
342 struct list_head *iter;
345 bond_for_each_slave(bond, tmp, iter) {
346 if (tmp->should_notify) {
347 rtmsg_ifinfo(RTM_NEWLINK, tmp->dev, 0, GFP_ATOMIC);
348 tmp->should_notify = 0;
353 static inline int bond_slave_state(struct slave *slave)
355 return slave->backup;
358 static inline bool bond_is_active_slave(struct slave *slave)
360 return !bond_slave_state(slave);
363 #define BOND_PRI_RESELECT_ALWAYS 0
364 #define BOND_PRI_RESELECT_BETTER 1
365 #define BOND_PRI_RESELECT_FAILURE 2
367 #define BOND_FOM_NONE 0
368 #define BOND_FOM_ACTIVE 1
369 #define BOND_FOM_FOLLOW 2
371 #define BOND_ARP_TARGETS_ANY 0
372 #define BOND_ARP_TARGETS_ALL 1
374 #define BOND_ARP_VALIDATE_NONE 0
375 #define BOND_ARP_VALIDATE_ACTIVE (1 << BOND_STATE_ACTIVE)
376 #define BOND_ARP_VALIDATE_BACKUP (1 << BOND_STATE_BACKUP)
377 #define BOND_ARP_VALIDATE_ALL (BOND_ARP_VALIDATE_ACTIVE | \
378 BOND_ARP_VALIDATE_BACKUP)
379 #define BOND_ARP_FILTER (BOND_ARP_VALIDATE_ALL + 1)
380 #define BOND_ARP_FILTER_ACTIVE (BOND_ARP_VALIDATE_ACTIVE | \
382 #define BOND_ARP_FILTER_BACKUP (BOND_ARP_VALIDATE_BACKUP | \
385 #define BOND_SLAVE_NOTIFY_NOW true
386 #define BOND_SLAVE_NOTIFY_LATER false
388 static inline int slave_do_arp_validate(struct bonding *bond,
391 return bond->params.arp_validate & (1 << bond_slave_state(slave));
394 static inline int slave_do_arp_validate_only(struct bonding *bond)
396 return bond->params.arp_validate & BOND_ARP_FILTER;
399 /* Get the oldest arp which we've received on this slave for bond's
402 static inline unsigned long slave_oldest_target_arp_rx(struct bonding *bond,
406 unsigned long ret = slave->target_last_arp_rx[0];
408 for (; (i < BOND_MAX_ARP_TARGETS) && bond->params.arp_targets[i]; i++)
409 if (time_before(slave->target_last_arp_rx[i], ret))
410 ret = slave->target_last_arp_rx[i];
415 static inline unsigned long slave_last_rx(struct bonding *bond,
418 if (bond->params.arp_all_targets == BOND_ARP_TARGETS_ALL)
419 return slave_oldest_target_arp_rx(bond, slave);
421 return slave->last_rx;
424 #ifdef CONFIG_NET_POLL_CONTROLLER
425 static inline void bond_netpoll_send_skb(const struct slave *slave,
428 struct netpoll *np = slave->np;
431 netpoll_send_skb(np, skb);
434 static inline void bond_netpoll_send_skb(const struct slave *slave,
440 static inline void bond_set_slave_inactive_flags(struct slave *slave,
443 if (!bond_is_lb(slave->bond))
444 bond_set_slave_state(slave, BOND_STATE_BACKUP, notify);
445 if (!slave->bond->params.all_slaves_active)
449 static inline void bond_set_slave_active_flags(struct slave *slave,
452 bond_set_slave_state(slave, BOND_STATE_ACTIVE, notify);
456 static inline bool bond_is_slave_inactive(struct slave *slave)
458 return slave->inactive;
461 static inline __be32 bond_confirm_addr(struct net_device *dev, __be32 dst, __be32 local)
463 struct in_device *in_dev;
467 in_dev = __in_dev_get_rcu(dev);
470 addr = inet_confirm_addr(dev_net(dev), in_dev, dst, local,
476 static inline bool slave_can_tx(struct slave *slave)
478 if (IS_UP(slave->dev) && slave->link == BOND_LINK_UP &&
479 bond_is_active_slave(slave))
486 struct net *net; /* Associated network namespace */
487 struct list_head dev_list;
488 #ifdef CONFIG_PROC_FS
489 struct proc_dir_entry *proc_dir;
491 struct class_attribute class_attr_bonding_masters;
494 int bond_arp_rcv(const struct sk_buff *skb, struct bonding *bond, struct slave *slave);
495 void bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb, struct net_device *slave_dev);
496 int bond_create(struct net *net, const char *name);
497 int bond_create_sysfs(struct bond_net *net);
498 void bond_destroy_sysfs(struct bond_net *net);
499 void bond_prepare_sysfs_group(struct bonding *bond);
500 int bond_sysfs_slave_add(struct slave *slave);
501 void bond_sysfs_slave_del(struct slave *slave);
502 int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev);
503 int bond_release(struct net_device *bond_dev, struct net_device *slave_dev);
504 u32 bond_xmit_hash(struct bonding *bond, struct sk_buff *skb);
505 void bond_select_active_slave(struct bonding *bond);
506 void bond_change_active_slave(struct bonding *bond, struct slave *new_active);
507 void bond_create_debugfs(void);
508 void bond_destroy_debugfs(void);
509 void bond_debug_register(struct bonding *bond);
510 void bond_debug_unregister(struct bonding *bond);
511 void bond_debug_reregister(struct bonding *bond);
512 const char *bond_mode_name(int mode);
513 void bond_setup(struct net_device *bond_dev);
514 unsigned int bond_get_num_tx_queues(void);
515 int bond_netlink_init(void);
516 void bond_netlink_fini(void);
517 struct net_device *bond_option_active_slave_get_rcu(struct bonding *bond);
518 struct net_device *bond_option_active_slave_get(struct bonding *bond);
519 const char *bond_slave_link_status(s8 link);
521 #ifdef CONFIG_PROC_FS
522 void bond_create_proc_entry(struct bonding *bond);
523 void bond_remove_proc_entry(struct bonding *bond);
524 void bond_create_proc_dir(struct bond_net *bn);
525 void bond_destroy_proc_dir(struct bond_net *bn);
527 static inline void bond_create_proc_entry(struct bonding *bond)
531 static inline void bond_remove_proc_entry(struct bonding *bond)
535 static inline void bond_create_proc_dir(struct bond_net *bn)
539 static inline void bond_destroy_proc_dir(struct bond_net *bn)
544 static inline struct slave *bond_slave_has_mac(struct bonding *bond,
547 struct list_head *iter;
550 bond_for_each_slave(bond, tmp, iter)
551 if (ether_addr_equal_64bits(mac, tmp->dev->dev_addr))
557 /* Caller must hold rcu_read_lock() for read */
558 static inline struct slave *bond_slave_has_mac_rcu(struct bonding *bond,
561 struct list_head *iter;
564 bond_for_each_slave_rcu(bond, tmp, iter)
565 if (ether_addr_equal_64bits(mac, tmp->dev->dev_addr))
571 /* Check if the ip is present in arp ip list, or first free slot if ip == 0
572 * Returns -1 if not found, index if found
574 static inline int bond_get_targets_ip(__be32 *targets, __be32 ip)
578 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++)
579 if (targets[i] == ip)
581 else if (targets[i] == 0)
587 /* exported from bond_main.c */
588 extern int bond_net_id;
589 extern const struct bond_parm_tbl bond_lacp_tbl[];
590 extern const struct bond_parm_tbl xmit_hashtype_tbl[];
591 extern const struct bond_parm_tbl arp_validate_tbl[];
592 extern const struct bond_parm_tbl arp_all_targets_tbl[];
593 extern const struct bond_parm_tbl fail_over_mac_tbl[];
594 extern const struct bond_parm_tbl pri_reselect_tbl[];
595 extern struct bond_parm_tbl ad_select_tbl[];
597 /* exported from bond_netlink.c */
598 extern struct rtnl_link_ops bond_link_ops;
600 #endif /* _LINUX_BONDING_H */