bonding: remove BOND_MODE_IS_LB macro
[deliverable/linux.git] / drivers / net / bonding / bonding.h
1 /*
2 * Bond several ethernet interfaces into a Cisco, running 'Etherchannel'.
3 *
4 * Portions are (c) Copyright 1995 Simon "Guru Aleph-Null" Janes
5 * NCM: Network and Communications Management, Inc.
6 *
7 * BUT, I'm the one who modified it for ethernet, so:
8 * (c) Copyright 1999, Thomas Davis, tadavis@lbl.gov
9 *
10 * This software may be used and distributed according to the terms
11 * of the GNU Public License, incorporated herein by reference.
12 *
13 */
14
15 #ifndef _LINUX_BONDING_H
16 #define _LINUX_BONDING_H
17
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>
27
28 #include "bond_3ad.h"
29 #include "bond_alb.h"
30 #include "bond_options.h"
31
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"
36
37 #define bond_version DRV_DESCRIPTION ": v" DRV_VERSION " (" DRV_RELDATE ")\n"
38
39 #define BOND_MAX_ARP_TARGETS 16
40
41 #define BOND_DEFAULT_MIIMON 100
42
43 #define IS_UP(dev) \
44 ((((dev)->flags & IFF_UP) == IFF_UP) && \
45 netif_running(dev) && \
46 netif_carrier_ok(dev))
47
48 /*
49 * Checks whether slave is ready for transmit.
50 */
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))
56
57
58 #define USES_PRIMARY(mode) \
59 (((mode) == BOND_MODE_ACTIVEBACKUP) || \
60 ((mode) == BOND_MODE_TLB) || \
61 ((mode) == BOND_MODE_ALB))
62
63 #define BOND_NO_USES_ARP(mode) \
64 (((mode) == BOND_MODE_8023AD) || \
65 ((mode) == BOND_MODE_TLB) || \
66 ((mode) == BOND_MODE_ALB))
67
68 #define TX_QUEUE_OVERRIDE(mode) \
69 (((mode) == BOND_MODE_ACTIVEBACKUP) || \
70 ((mode) == BOND_MODE_ROUNDROBIN))
71
72 #define IS_IP_TARGET_UNUSABLE_ADDRESS(a) \
73 ((htonl(INADDR_BROADCAST) == a) || \
74 ipv4_is_zeronet(a))
75 /*
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.
79 */
80 #define IOCTL(dev, arg, cmd) ({ \
81 int res = 0; \
82 mm_segment_t fs = get_fs(); \
83 set_fs(get_ds()); \
84 res = ioctl(dev, arg, cmd); \
85 set_fs(fs); \
86 res; })
87
88 /* slave list primitives */
89 #define bond_slave_list(bond) (&(bond)->dev->adj_list.lower)
90
91 #define bond_has_slaves(bond) !list_empty(bond_slave_list(bond))
92
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) : \
97 NULL)
98 #define bond_last_slave(bond) \
99 (bond_has_slaves(bond) ? \
100 netdev_adjacent_get_private(bond_slave_list(bond)->prev) : \
101 NULL)
102
103 /* Caller must have rcu_read_lock */
104 #define bond_first_slave_rcu(bond) \
105 netdev_lower_get_first_private_rcu(bond->dev)
106
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))
109
110 /**
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
115 *
116 * Caller must hold bond->lock
117 */
118 #define bond_for_each_slave(bond, pos, iter) \
119 netdev_for_each_lower_private((bond)->dev, pos, iter)
120
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)
124
125 #ifdef CONFIG_NET_POLL_CONTROLLER
126 extern atomic_t netpoll_block_tx;
127
128 static inline void block_netpoll_tx(void)
129 {
130 atomic_inc(&netpoll_block_tx);
131 }
132
133 static inline void unblock_netpoll_tx(void)
134 {
135 atomic_dec(&netpoll_block_tx);
136 }
137
138 static inline int is_netpoll_tx_blocked(struct net_device *dev)
139 {
140 if (unlikely(netpoll_tx_running(dev)))
141 return atomic_read(&netpoll_block_tx);
142 return 0;
143 }
144 #else
145 #define block_netpoll_tx()
146 #define unblock_netpoll_tx()
147 #define is_netpoll_tx_blocked(dev) (0)
148 #endif
149
150 struct bond_params {
151 int mode;
152 int xmit_policy;
153 int miimon;
154 u8 num_peer_notif;
155 int arp_interval;
156 int arp_validate;
157 int arp_all_targets;
158 int use_carrier;
159 int fail_over_mac;
160 int updelay;
161 int downdelay;
162 int lacp_fast;
163 unsigned int min_links;
164 int ad_select;
165 char primary[IFNAMSIZ];
166 int primary_reselect;
167 __be32 arp_targets[BOND_MAX_ARP_TARGETS];
168 int tx_queues;
169 int all_slaves_active;
170 int resend_igmp;
171 int lp_interval;
172 int packets_per_slave;
173 int tlb_dynamic_lb;
174 struct reciprocal_value reciprocal_packets_per_slave;
175 };
176
177 struct bond_parm_tbl {
178 char *modename;
179 int mode;
180 };
181
182 struct slave {
183 struct net_device *dev; /* first - useful for panic debug */
184 struct bonding *bond; /* our master */
185 int delay;
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 */
191 s8 new_link;
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 */
196 u8 duplex;
197 u32 original_mtu;
198 u32 link_failure_count;
199 u32 speed;
200 u16 queue_id;
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
205 struct netpoll *np;
206 #endif
207 struct kobject kobj;
208 };
209
210 /*
211 * Link pseudo-state only used internally by monitors
212 */
213 #define BOND_LINK_NOCHANGE -1
214
215 /*
216 * Here are the locking policies for the two bonding locks:
217 *
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
222 * beforehand.
223 */
224 struct bonding {
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;
229 bool force_primary;
230 s32 slave_cnt; /* never change this value outside the attach/detach wrappers */
231 int (*recv_probe)(const struct sk_buff *, struct bonding *,
232 struct slave *);
233 rwlock_t lock;
234 rwlock_t curr_slave_lock;
235 u8 send_peer_notif;
236 u8 igmp_retrans;
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;
242 u32 rr_tx_counter;
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 */
256 };
257
258 #define bond_slave_get_rcu(dev) \
259 ((struct slave *) rcu_dereference(dev->rx_handler_data))
260
261 #define bond_slave_get_rtnl(dev) \
262 ((struct slave *) rtnl_dereference(dev->rx_handler_data))
263
264 struct bond_vlan_tag {
265 __be16 vlan_proto;
266 unsigned short vlan_id;
267 };
268
269 /**
270 * Returns NULL if the net_device does not belong to any of the bond's slaves
271 *
272 * Caller must hold bond lock for read
273 */
274 static inline struct slave *bond_get_slave_by_dev(struct bonding *bond,
275 struct net_device *slave_dev)
276 {
277 return netdev_lower_dev_get_private(bond->dev, slave_dev);
278 }
279
280 static inline struct bonding *bond_get_bond_by_slave(struct slave *slave)
281 {
282 if (!slave || !slave->bond)
283 return NULL;
284 return slave->bond;
285 }
286
287 static inline bool bond_is_lb(const struct bonding *bond)
288 {
289 return bond->params.mode == BOND_MODE_TLB ||
290 bond->params.mode == BOND_MODE_ALB;
291 }
292
293 static inline void bond_set_active_slave(struct slave *slave)
294 {
295 if (slave->backup) {
296 slave->backup = 0;
297 rtmsg_ifinfo(RTM_NEWLINK, slave->dev, 0, GFP_ATOMIC);
298 }
299 }
300
301 static inline void bond_set_backup_slave(struct slave *slave)
302 {
303 if (!slave->backup) {
304 slave->backup = 1;
305 rtmsg_ifinfo(RTM_NEWLINK, slave->dev, 0, GFP_ATOMIC);
306 }
307 }
308
309 static inline void bond_set_slave_state(struct slave *slave,
310 int slave_state, bool notify)
311 {
312 if (slave->backup == slave_state)
313 return;
314
315 slave->backup = slave_state;
316 if (notify) {
317 rtmsg_ifinfo(RTM_NEWLINK, slave->dev, 0, GFP_ATOMIC);
318 slave->should_notify = 0;
319 } else {
320 if (slave->should_notify)
321 slave->should_notify = 0;
322 else
323 slave->should_notify = 1;
324 }
325 }
326
327 static inline void bond_slave_state_change(struct bonding *bond)
328 {
329 struct list_head *iter;
330 struct slave *tmp;
331
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);
337 }
338 }
339
340 static inline void bond_slave_state_notify(struct bonding *bond)
341 {
342 struct list_head *iter;
343 struct slave *tmp;
344
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;
349 }
350 }
351 }
352
353 static inline int bond_slave_state(struct slave *slave)
354 {
355 return slave->backup;
356 }
357
358 static inline bool bond_is_active_slave(struct slave *slave)
359 {
360 return !bond_slave_state(slave);
361 }
362
363 #define BOND_PRI_RESELECT_ALWAYS 0
364 #define BOND_PRI_RESELECT_BETTER 1
365 #define BOND_PRI_RESELECT_FAILURE 2
366
367 #define BOND_FOM_NONE 0
368 #define BOND_FOM_ACTIVE 1
369 #define BOND_FOM_FOLLOW 2
370
371 #define BOND_ARP_TARGETS_ANY 0
372 #define BOND_ARP_TARGETS_ALL 1
373
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 | \
381 BOND_ARP_FILTER)
382 #define BOND_ARP_FILTER_BACKUP (BOND_ARP_VALIDATE_BACKUP | \
383 BOND_ARP_FILTER)
384
385 #define BOND_SLAVE_NOTIFY_NOW true
386 #define BOND_SLAVE_NOTIFY_LATER false
387
388 static inline int slave_do_arp_validate(struct bonding *bond,
389 struct slave *slave)
390 {
391 return bond->params.arp_validate & (1 << bond_slave_state(slave));
392 }
393
394 static inline int slave_do_arp_validate_only(struct bonding *bond)
395 {
396 return bond->params.arp_validate & BOND_ARP_FILTER;
397 }
398
399 /* Get the oldest arp which we've received on this slave for bond's
400 * arp_targets.
401 */
402 static inline unsigned long slave_oldest_target_arp_rx(struct bonding *bond,
403 struct slave *slave)
404 {
405 int i = 1;
406 unsigned long ret = slave->target_last_arp_rx[0];
407
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];
411
412 return ret;
413 }
414
415 static inline unsigned long slave_last_rx(struct bonding *bond,
416 struct slave *slave)
417 {
418 if (bond->params.arp_all_targets == BOND_ARP_TARGETS_ALL)
419 return slave_oldest_target_arp_rx(bond, slave);
420
421 return slave->last_rx;
422 }
423
424 #ifdef CONFIG_NET_POLL_CONTROLLER
425 static inline void bond_netpoll_send_skb(const struct slave *slave,
426 struct sk_buff *skb)
427 {
428 struct netpoll *np = slave->np;
429
430 if (np)
431 netpoll_send_skb(np, skb);
432 }
433 #else
434 static inline void bond_netpoll_send_skb(const struct slave *slave,
435 struct sk_buff *skb)
436 {
437 }
438 #endif
439
440 static inline void bond_set_slave_inactive_flags(struct slave *slave,
441 bool notify)
442 {
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)
446 slave->inactive = 1;
447 }
448
449 static inline void bond_set_slave_active_flags(struct slave *slave,
450 bool notify)
451 {
452 bond_set_slave_state(slave, BOND_STATE_ACTIVE, notify);
453 slave->inactive = 0;
454 }
455
456 static inline bool bond_is_slave_inactive(struct slave *slave)
457 {
458 return slave->inactive;
459 }
460
461 static inline __be32 bond_confirm_addr(struct net_device *dev, __be32 dst, __be32 local)
462 {
463 struct in_device *in_dev;
464 __be32 addr = 0;
465
466 rcu_read_lock();
467 in_dev = __in_dev_get_rcu(dev);
468
469 if (in_dev)
470 addr = inet_confirm_addr(dev_net(dev), in_dev, dst, local,
471 RT_SCOPE_HOST);
472 rcu_read_unlock();
473 return addr;
474 }
475
476 static inline bool slave_can_tx(struct slave *slave)
477 {
478 if (IS_UP(slave->dev) && slave->link == BOND_LINK_UP &&
479 bond_is_active_slave(slave))
480 return true;
481 else
482 return false;
483 }
484
485 struct bond_net {
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;
490 #endif
491 struct class_attribute class_attr_bonding_masters;
492 };
493
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);
520
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);
526 #else
527 static inline void bond_create_proc_entry(struct bonding *bond)
528 {
529 }
530
531 static inline void bond_remove_proc_entry(struct bonding *bond)
532 {
533 }
534
535 static inline void bond_create_proc_dir(struct bond_net *bn)
536 {
537 }
538
539 static inline void bond_destroy_proc_dir(struct bond_net *bn)
540 {
541 }
542 #endif
543
544 static inline struct slave *bond_slave_has_mac(struct bonding *bond,
545 const u8 *mac)
546 {
547 struct list_head *iter;
548 struct slave *tmp;
549
550 bond_for_each_slave(bond, tmp, iter)
551 if (ether_addr_equal_64bits(mac, tmp->dev->dev_addr))
552 return tmp;
553
554 return NULL;
555 }
556
557 /* Caller must hold rcu_read_lock() for read */
558 static inline struct slave *bond_slave_has_mac_rcu(struct bonding *bond,
559 const u8 *mac)
560 {
561 struct list_head *iter;
562 struct slave *tmp;
563
564 bond_for_each_slave_rcu(bond, tmp, iter)
565 if (ether_addr_equal_64bits(mac, tmp->dev->dev_addr))
566 return tmp;
567
568 return NULL;
569 }
570
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
573 */
574 static inline int bond_get_targets_ip(__be32 *targets, __be32 ip)
575 {
576 int i;
577
578 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++)
579 if (targets[i] == ip)
580 return i;
581 else if (targets[i] == 0)
582 break;
583
584 return -1;
585 }
586
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[];
596
597 /* exported from bond_netlink.c */
598 extern struct rtnl_link_ops bond_link_ops;
599
600 #endif /* _LINUX_BONDING_H */
This page took 0.062145 seconds and 6 git commands to generate.