bonding.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. #ifndef _LINUX_BONDING_H
  15. #define _LINUX_BONDING_H
  16. #include <linux/timer.h>
  17. #include <linux/proc_fs.h>
  18. #include <linux/if_bonding.h>
  19. #include <linux/cpumask.h>
  20. #include <linux/in6.h>
  21. #include <linux/netpoll.h>
  22. #include <linux/inetdevice.h>
  23. #include <linux/etherdevice.h>
  24. #include "bond_3ad.h"
  25. #include "bond_alb.h"
  26. #define DRV_VERSION "3.7.1"
  27. #define DRV_RELDATE "April 27, 2011"
  28. #define DRV_NAME "bonding"
  29. #define DRV_DESCRIPTION "Ethernet Channel Bonding Driver"
  30. #define bond_version DRV_DESCRIPTION ": v" DRV_VERSION " (" DRV_RELDATE ")\n"
  31. #define BOND_MAX_ARP_TARGETS 16
  32. #define IS_UP(dev) \
  33. ((((dev)->flags & IFF_UP) == IFF_UP) && \
  34. netif_running(dev) && \
  35. netif_carrier_ok(dev))
  36. /*
  37. * Checks whether slave is ready for transmit.
  38. */
  39. #define SLAVE_IS_OK(slave) \
  40. (((slave)->dev->flags & IFF_UP) && \
  41. netif_running((slave)->dev) && \
  42. ((slave)->link == BOND_LINK_UP) && \
  43. bond_is_active_slave(slave))
  44. #define USES_PRIMARY(mode) \
  45. (((mode) == BOND_MODE_ACTIVEBACKUP) || \
  46. ((mode) == BOND_MODE_TLB) || \
  47. ((mode) == BOND_MODE_ALB))
  48. #define TX_QUEUE_OVERRIDE(mode) \
  49. (((mode) == BOND_MODE_ACTIVEBACKUP) || \
  50. ((mode) == BOND_MODE_ROUNDROBIN))
  51. /*
  52. * Less bad way to call ioctl from within the kernel; this needs to be
  53. * done some other way to get the call out of interrupt context.
  54. * Needs "ioctl" variable to be supplied by calling context.
  55. */
  56. #define IOCTL(dev, arg, cmd) ({ \
  57. int res = 0; \
  58. mm_segment_t fs = get_fs(); \
  59. set_fs(get_ds()); \
  60. res = ioctl(dev, arg, cmd); \
  61. set_fs(fs); \
  62. res; })
  63. /* slave list primitives */
  64. #define bond_to_slave(ptr) list_entry(ptr, struct slave, list)
  65. /* IMPORTANT: bond_first/last_slave can return NULL in case of an empty list */
  66. #define bond_first_slave(bond) \
  67. list_first_entry_or_null(&(bond)->slave_list, struct slave, list)
  68. #define bond_last_slave(bond) \
  69. (list_empty(&(bond)->slave_list) ? NULL : \
  70. bond_to_slave((bond)->slave_list.prev))
  71. #define bond_is_first_slave(bond, pos) ((pos)->list.prev == &(bond)->slave_list)
  72. #define bond_is_last_slave(bond, pos) ((pos)->list.next == &(bond)->slave_list)
  73. /* Since bond_first/last_slave can return NULL, these can return NULL too */
  74. #define bond_next_slave(bond, pos) \
  75. (bond_is_last_slave(bond, pos) ? bond_first_slave(bond) : \
  76. bond_to_slave((pos)->list.next))
  77. #define bond_prev_slave(bond, pos) \
  78. (bond_is_first_slave(bond, pos) ? bond_last_slave(bond) : \
  79. bond_to_slave((pos)->list.prev))
  80. /**
  81. * bond_for_each_slave_from - iterate the slaves list from a starting point
  82. * @bond: the bond holding this list.
  83. * @pos: current slave.
  84. * @cnt: counter for max number of moves
  85. * @start: starting point.
  86. *
  87. * Caller must hold bond->lock
  88. */
  89. #define bond_for_each_slave_from(bond, pos, cnt, start) \
  90. for (cnt = 0, pos = start; pos && cnt < (bond)->slave_cnt; \
  91. cnt++, pos = bond_next_slave(bond, pos))
  92. /**
  93. * bond_for_each_slave - iterate over all slaves
  94. * @bond: the bond holding this list
  95. * @pos: current slave
  96. *
  97. * Caller must hold bond->lock
  98. */
  99. #define bond_for_each_slave(bond, pos) \
  100. list_for_each_entry(pos, &(bond)->slave_list, list)
  101. /* Caller must have rcu_read_lock */
  102. #define bond_for_each_slave_rcu(bond, pos) \
  103. list_for_each_entry_rcu(pos, &(bond)->slave_list, list)
  104. /**
  105. * bond_for_each_slave_reverse - iterate in reverse from a given position
  106. * @bond: the bond holding this list
  107. * @pos: slave to continue from
  108. *
  109. * Caller must hold bond->lock
  110. */
  111. #define bond_for_each_slave_continue_reverse(bond, pos) \
  112. list_for_each_entry_continue_reverse(pos, &(bond)->slave_list, list)
  113. #ifdef CONFIG_NET_POLL_CONTROLLER
  114. extern atomic_t netpoll_block_tx;
  115. static inline void block_netpoll_tx(void)
  116. {
  117. atomic_inc(&netpoll_block_tx);
  118. }
  119. static inline void unblock_netpoll_tx(void)
  120. {
  121. atomic_dec(&netpoll_block_tx);
  122. }
  123. static inline int is_netpoll_tx_blocked(struct net_device *dev)
  124. {
  125. if (unlikely(netpoll_tx_running(dev)))
  126. return atomic_read(&netpoll_block_tx);
  127. return 0;
  128. }
  129. #else
  130. #define block_netpoll_tx()
  131. #define unblock_netpoll_tx()
  132. #define is_netpoll_tx_blocked(dev) (0)
  133. #endif
  134. struct bond_params {
  135. int mode;
  136. int xmit_policy;
  137. int miimon;
  138. u8 num_peer_notif;
  139. int arp_interval;
  140. int arp_validate;
  141. int arp_all_targets;
  142. int use_carrier;
  143. int fail_over_mac;
  144. int updelay;
  145. int downdelay;
  146. int lacp_fast;
  147. unsigned int min_links;
  148. int ad_select;
  149. char primary[IFNAMSIZ];
  150. int primary_reselect;
  151. __be32 arp_targets[BOND_MAX_ARP_TARGETS];
  152. int tx_queues;
  153. int all_slaves_active;
  154. int resend_igmp;
  155. };
  156. struct bond_parm_tbl {
  157. char *modename;
  158. int mode;
  159. };
  160. #define BOND_MAX_MODENAME_LEN 20
  161. struct vlan_entry {
  162. struct list_head vlan_list;
  163. unsigned short vlan_id;
  164. };
  165. struct slave {
  166. struct net_device *dev; /* first - useful for panic debug */
  167. struct list_head list;
  168. struct bonding *bond; /* our master */
  169. int delay;
  170. unsigned long jiffies;
  171. unsigned long last_arp_rx;
  172. unsigned long target_last_arp_rx[BOND_MAX_ARP_TARGETS];
  173. s8 link; /* one of BOND_LINK_XXXX */
  174. s8 new_link;
  175. u8 backup:1, /* indicates backup slave. Value corresponds with
  176. BOND_STATE_ACTIVE and BOND_STATE_BACKUP */
  177. inactive:1; /* indicates inactive slave */
  178. u8 duplex;
  179. u32 original_mtu;
  180. u32 link_failure_count;
  181. u32 speed;
  182. u16 queue_id;
  183. u8 perm_hwaddr[ETH_ALEN];
  184. struct ad_slave_info ad_info; /* HUGE - better to dynamically alloc */
  185. struct tlb_slave_info tlb_info;
  186. #ifdef CONFIG_NET_POLL_CONTROLLER
  187. struct netpoll *np;
  188. #endif
  189. };
  190. /*
  191. * Link pseudo-state only used internally by monitors
  192. */
  193. #define BOND_LINK_NOCHANGE -1
  194. /*
  195. * Here are the locking policies for the two bonding locks:
  196. *
  197. * 1) Get bond->lock when reading/writing slave list.
  198. * 2) Get bond->curr_slave_lock when reading/writing bond->curr_active_slave.
  199. * (It is unnecessary when the write-lock is put with bond->lock.)
  200. * 3) When we lock with bond->curr_slave_lock, we must lock with bond->lock
  201. * beforehand.
  202. */
  203. struct bonding {
  204. struct net_device *dev; /* first - useful for panic debug */
  205. struct list_head slave_list;
  206. struct slave *curr_active_slave;
  207. struct slave *current_arp_slave;
  208. struct slave *primary_slave;
  209. bool force_primary;
  210. s32 slave_cnt; /* never change this value outside the attach/detach wrappers */
  211. int (*recv_probe)(const struct sk_buff *, struct bonding *,
  212. struct slave *);
  213. rwlock_t lock;
  214. rwlock_t curr_slave_lock;
  215. u8 send_peer_notif;
  216. u8 igmp_retrans;
  217. #ifdef CONFIG_PROC_FS
  218. struct proc_dir_entry *proc_entry;
  219. char proc_file_name[IFNAMSIZ];
  220. #endif /* CONFIG_PROC_FS */
  221. struct list_head bond_list;
  222. int (*xmit_hash_policy)(struct sk_buff *, int);
  223. u16 rr_tx_counter;
  224. struct ad_bond_info ad_info;
  225. struct alb_bond_info alb_info;
  226. struct bond_params params;
  227. struct list_head vlan_list;
  228. struct workqueue_struct *wq;
  229. struct delayed_work mii_work;
  230. struct delayed_work arp_work;
  231. struct delayed_work alb_work;
  232. struct delayed_work ad_work;
  233. struct delayed_work mcast_work;
  234. #ifdef CONFIG_DEBUG_FS
  235. /* debugging support via debugfs */
  236. struct dentry *debug_dir;
  237. #endif /* CONFIG_DEBUG_FS */
  238. };
  239. static inline bool bond_vlan_used(struct bonding *bond)
  240. {
  241. return !list_empty(&bond->vlan_list);
  242. }
  243. #define bond_slave_get_rcu(dev) \
  244. ((struct slave *) rcu_dereference(dev->rx_handler_data))
  245. #define bond_slave_get_rtnl(dev) \
  246. ((struct slave *) rtnl_dereference(dev->rx_handler_data))
  247. /**
  248. * Returns NULL if the net_device does not belong to any of the bond's slaves
  249. *
  250. * Caller must hold bond lock for read
  251. */
  252. static inline struct slave *bond_get_slave_by_dev(struct bonding *bond,
  253. struct net_device *slave_dev)
  254. {
  255. struct slave *slave = NULL;
  256. bond_for_each_slave(bond, slave)
  257. if (slave->dev == slave_dev)
  258. return slave;
  259. return NULL;
  260. }
  261. static inline struct bonding *bond_get_bond_by_slave(struct slave *slave)
  262. {
  263. if (!slave || !slave->bond)
  264. return NULL;
  265. return slave->bond;
  266. }
  267. static inline bool bond_is_lb(const struct bonding *bond)
  268. {
  269. return (bond->params.mode == BOND_MODE_TLB ||
  270. bond->params.mode == BOND_MODE_ALB);
  271. }
  272. static inline void bond_set_active_slave(struct slave *slave)
  273. {
  274. slave->backup = 0;
  275. }
  276. static inline void bond_set_backup_slave(struct slave *slave)
  277. {
  278. slave->backup = 1;
  279. }
  280. static inline int bond_slave_state(struct slave *slave)
  281. {
  282. return slave->backup;
  283. }
  284. static inline bool bond_is_active_slave(struct slave *slave)
  285. {
  286. return !bond_slave_state(slave);
  287. }
  288. #define BOND_PRI_RESELECT_ALWAYS 0
  289. #define BOND_PRI_RESELECT_BETTER 1
  290. #define BOND_PRI_RESELECT_FAILURE 2
  291. #define BOND_FOM_NONE 0
  292. #define BOND_FOM_ACTIVE 1
  293. #define BOND_FOM_FOLLOW 2
  294. #define BOND_ARP_TARGETS_ANY 0
  295. #define BOND_ARP_TARGETS_ALL 1
  296. #define BOND_ARP_VALIDATE_NONE 0
  297. #define BOND_ARP_VALIDATE_ACTIVE (1 << BOND_STATE_ACTIVE)
  298. #define BOND_ARP_VALIDATE_BACKUP (1 << BOND_STATE_BACKUP)
  299. #define BOND_ARP_VALIDATE_ALL (BOND_ARP_VALIDATE_ACTIVE | \
  300. BOND_ARP_VALIDATE_BACKUP)
  301. static inline int slave_do_arp_validate(struct bonding *bond,
  302. struct slave *slave)
  303. {
  304. return bond->params.arp_validate & (1 << bond_slave_state(slave));
  305. }
  306. /* Get the oldest arp which we've received on this slave for bond's
  307. * arp_targets.
  308. */
  309. static inline unsigned long slave_oldest_target_arp_rx(struct bonding *bond,
  310. struct slave *slave)
  311. {
  312. int i = 1;
  313. unsigned long ret = slave->target_last_arp_rx[0];
  314. for (; (i < BOND_MAX_ARP_TARGETS) && bond->params.arp_targets[i]; i++)
  315. if (time_before(slave->target_last_arp_rx[i], ret))
  316. ret = slave->target_last_arp_rx[i];
  317. return ret;
  318. }
  319. static inline unsigned long slave_last_rx(struct bonding *bond,
  320. struct slave *slave)
  321. {
  322. if (slave_do_arp_validate(bond, slave)) {
  323. if (bond->params.arp_all_targets == BOND_ARP_TARGETS_ALL)
  324. return slave_oldest_target_arp_rx(bond, slave);
  325. else
  326. return slave->last_arp_rx;
  327. }
  328. return slave->dev->last_rx;
  329. }
  330. #ifdef CONFIG_NET_POLL_CONTROLLER
  331. static inline void bond_netpoll_send_skb(const struct slave *slave,
  332. struct sk_buff *skb)
  333. {
  334. struct netpoll *np = slave->np;
  335. if (np)
  336. netpoll_send_skb(np, skb);
  337. }
  338. #else
  339. static inline void bond_netpoll_send_skb(const struct slave *slave,
  340. struct sk_buff *skb)
  341. {
  342. }
  343. #endif
  344. static inline void bond_set_slave_inactive_flags(struct slave *slave)
  345. {
  346. if (!bond_is_lb(slave->bond))
  347. bond_set_backup_slave(slave);
  348. if (!slave->bond->params.all_slaves_active)
  349. slave->inactive = 1;
  350. }
  351. static inline void bond_set_slave_active_flags(struct slave *slave)
  352. {
  353. bond_set_active_slave(slave);
  354. slave->inactive = 0;
  355. }
  356. static inline bool bond_is_slave_inactive(struct slave *slave)
  357. {
  358. return slave->inactive;
  359. }
  360. static inline __be32 bond_confirm_addr(struct net_device *dev, __be32 dst, __be32 local)
  361. {
  362. struct in_device *in_dev;
  363. __be32 addr = 0;
  364. rcu_read_lock();
  365. in_dev = __in_dev_get_rcu(dev);
  366. if (in_dev)
  367. addr = inet_confirm_addr(in_dev, dst, local, RT_SCOPE_HOST);
  368. rcu_read_unlock();
  369. return addr;
  370. }
  371. static inline bool slave_can_tx(struct slave *slave)
  372. {
  373. if (IS_UP(slave->dev) && slave->link == BOND_LINK_UP &&
  374. bond_is_active_slave(slave))
  375. return true;
  376. else
  377. return false;
  378. }
  379. struct bond_net;
  380. struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr);
  381. int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb, struct net_device *slave_dev);
  382. void bond_xmit_slave_id(struct bonding *bond, struct sk_buff *skb, int slave_id);
  383. int bond_create(struct net *net, const char *name);
  384. int bond_create_sysfs(struct bond_net *net);
  385. void bond_destroy_sysfs(struct bond_net *net);
  386. void bond_prepare_sysfs_group(struct bonding *bond);
  387. int bond_create_slave_symlinks(struct net_device *master, struct net_device *slave);
  388. void bond_destroy_slave_symlinks(struct net_device *master, struct net_device *slave);
  389. int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev);
  390. int bond_release(struct net_device *bond_dev, struct net_device *slave_dev);
  391. void bond_mii_monitor(struct work_struct *);
  392. void bond_loadbalance_arp_mon(struct work_struct *);
  393. void bond_activebackup_arp_mon(struct work_struct *);
  394. void bond_set_mode_ops(struct bonding *bond, int mode);
  395. int bond_parse_parm(const char *mode_arg, const struct bond_parm_tbl *tbl);
  396. void bond_select_active_slave(struct bonding *bond);
  397. void bond_change_active_slave(struct bonding *bond, struct slave *new_active);
  398. void bond_create_debugfs(void);
  399. void bond_destroy_debugfs(void);
  400. void bond_debug_register(struct bonding *bond);
  401. void bond_debug_unregister(struct bonding *bond);
  402. void bond_debug_reregister(struct bonding *bond);
  403. const char *bond_mode_name(int mode);
  404. struct bond_net {
  405. struct net * net; /* Associated network namespace */
  406. struct list_head dev_list;
  407. #ifdef CONFIG_PROC_FS
  408. struct proc_dir_entry * proc_dir;
  409. #endif
  410. struct class_attribute class_attr_bonding_masters;
  411. };
  412. #ifdef CONFIG_PROC_FS
  413. void bond_create_proc_entry(struct bonding *bond);
  414. void bond_remove_proc_entry(struct bonding *bond);
  415. void bond_create_proc_dir(struct bond_net *bn);
  416. void bond_destroy_proc_dir(struct bond_net *bn);
  417. #else
  418. static inline void bond_create_proc_entry(struct bonding *bond)
  419. {
  420. }
  421. static inline void bond_remove_proc_entry(struct bonding *bond)
  422. {
  423. }
  424. static inline void bond_create_proc_dir(struct bond_net *bn)
  425. {
  426. }
  427. static inline void bond_destroy_proc_dir(struct bond_net *bn)
  428. {
  429. }
  430. #endif
  431. static inline struct slave *bond_slave_has_mac(struct bonding *bond,
  432. const u8 *mac)
  433. {
  434. struct slave *tmp;
  435. bond_for_each_slave(bond, tmp)
  436. if (ether_addr_equal_64bits(mac, tmp->dev->dev_addr))
  437. return tmp;
  438. return NULL;
  439. }
  440. /* Check if the ip is present in arp ip list, or first free slot if ip == 0
  441. * Returns -1 if not found, index if found
  442. */
  443. static inline int bond_get_targets_ip(__be32 *targets, __be32 ip)
  444. {
  445. int i;
  446. for (i = 0; i < BOND_MAX_ARP_TARGETS; i++)
  447. if (targets[i] == ip)
  448. return i;
  449. else if (targets[i] == 0)
  450. break;
  451. return -1;
  452. }
  453. /* exported from bond_main.c */
  454. extern int bond_net_id;
  455. extern const struct bond_parm_tbl bond_lacp_tbl[];
  456. extern const struct bond_parm_tbl bond_mode_tbl[];
  457. extern const struct bond_parm_tbl xmit_hashtype_tbl[];
  458. extern const struct bond_parm_tbl arp_validate_tbl[];
  459. extern const struct bond_parm_tbl arp_all_targets_tbl[];
  460. extern const struct bond_parm_tbl fail_over_mac_tbl[];
  461. extern const struct bond_parm_tbl pri_reselect_tbl[];
  462. extern struct bond_parm_tbl ad_select_tbl[];
  463. #endif /* _LINUX_BONDING_H */