bonding.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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/etherdevice.h>
  20. #include <linux/cpumask.h>
  21. #include <linux/in6.h>
  22. #include <linux/netpoll.h>
  23. #include <linux/inetdevice.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. /**
  64. * bond_for_each_slave_from - iterate the slaves list from a starting point
  65. * @bond: the bond holding this list.
  66. * @pos: current slave.
  67. * @cnt: counter for max number of moves
  68. * @start: starting point.
  69. *
  70. * Caller must hold bond->lock
  71. */
  72. #define bond_for_each_slave_from(bond, pos, cnt, start) \
  73. for (cnt = 0, pos = start; \
  74. cnt < (bond)->slave_cnt; \
  75. cnt++, pos = (pos)->next)
  76. /**
  77. * bond_for_each_slave_from_to - iterate the slaves list from start point to stop point
  78. * @bond: the bond holding this list.
  79. * @pos: current slave.
  80. * @cnt: counter for number max of moves
  81. * @start: start point.
  82. * @stop: stop point.
  83. *
  84. * Caller must hold bond->lock
  85. */
  86. #define bond_for_each_slave_from_to(bond, pos, cnt, start, stop) \
  87. for (cnt = 0, pos = start; \
  88. ((cnt < (bond)->slave_cnt) && (pos != (stop)->next)); \
  89. cnt++, pos = (pos)->next)
  90. /**
  91. * bond_for_each_slave - iterate the slaves list from head
  92. * @bond: the bond holding this list.
  93. * @pos: current slave.
  94. * @cnt: counter for max number of moves
  95. *
  96. * Caller must hold bond->lock
  97. */
  98. #define bond_for_each_slave(bond, pos, cnt) \
  99. bond_for_each_slave_from(bond, pos, cnt, (bond)->first_slave)
  100. #ifdef CONFIG_NET_POLL_CONTROLLER
  101. extern atomic_t netpoll_block_tx;
  102. static inline void block_netpoll_tx(void)
  103. {
  104. atomic_inc(&netpoll_block_tx);
  105. }
  106. static inline void unblock_netpoll_tx(void)
  107. {
  108. atomic_dec(&netpoll_block_tx);
  109. }
  110. static inline int is_netpoll_tx_blocked(struct net_device *dev)
  111. {
  112. if (unlikely(netpoll_tx_running(dev)))
  113. return atomic_read(&netpoll_block_tx);
  114. return 0;
  115. }
  116. #else
  117. #define block_netpoll_tx()
  118. #define unblock_netpoll_tx()
  119. #define is_netpoll_tx_blocked(dev) (0)
  120. #endif
  121. struct bond_params {
  122. int mode;
  123. int xmit_policy;
  124. int miimon;
  125. u8 num_peer_notif;
  126. int arp_interval;
  127. int arp_validate;
  128. int use_carrier;
  129. int fail_over_mac;
  130. int updelay;
  131. int downdelay;
  132. int lacp_fast;
  133. unsigned int min_links;
  134. int ad_select;
  135. char primary[IFNAMSIZ];
  136. int primary_reselect;
  137. __be32 arp_targets[BOND_MAX_ARP_TARGETS];
  138. int tx_queues;
  139. int all_slaves_active;
  140. int resend_igmp;
  141. };
  142. struct bond_parm_tbl {
  143. char *modename;
  144. int mode;
  145. };
  146. #define BOND_MAX_MODENAME_LEN 20
  147. struct vlan_entry {
  148. struct list_head vlan_list;
  149. unsigned short vlan_id;
  150. };
  151. struct slave {
  152. struct net_device *dev; /* first - useful for panic debug */
  153. struct slave *next;
  154. struct slave *prev;
  155. struct bonding *bond; /* our master */
  156. int delay;
  157. unsigned long jiffies;
  158. unsigned long last_arp_rx;
  159. s8 link; /* one of BOND_LINK_XXXX */
  160. s8 new_link;
  161. u8 backup:1, /* indicates backup slave. Value corresponds with
  162. BOND_STATE_ACTIVE and BOND_STATE_BACKUP */
  163. inactive:1; /* indicates inactive slave */
  164. u8 duplex;
  165. u32 original_mtu;
  166. u32 link_failure_count;
  167. u32 speed;
  168. u16 queue_id;
  169. u8 perm_hwaddr[ETH_ALEN];
  170. struct ad_slave_info ad_info; /* HUGE - better to dynamically alloc */
  171. struct tlb_slave_info tlb_info;
  172. #ifdef CONFIG_NET_POLL_CONTROLLER
  173. struct netpoll *np;
  174. #endif
  175. };
  176. /*
  177. * Link pseudo-state only used internally by monitors
  178. */
  179. #define BOND_LINK_NOCHANGE -1
  180. /*
  181. * Here are the locking policies for the two bonding locks:
  182. *
  183. * 1) Get bond->lock when reading/writing slave list.
  184. * 2) Get bond->curr_slave_lock when reading/writing bond->curr_active_slave.
  185. * (It is unnecessary when the write-lock is put with bond->lock.)
  186. * 3) When we lock with bond->curr_slave_lock, we must lock with bond->lock
  187. * beforehand.
  188. */
  189. struct bonding {
  190. struct net_device *dev; /* first - useful for panic debug */
  191. struct slave *first_slave;
  192. struct slave *curr_active_slave;
  193. struct slave *current_arp_slave;
  194. struct slave *primary_slave;
  195. bool force_primary;
  196. s32 slave_cnt; /* never change this value outside the attach/detach wrappers */
  197. int (*recv_probe)(struct sk_buff *, struct bonding *,
  198. struct slave *);
  199. rwlock_t lock;
  200. rwlock_t curr_slave_lock;
  201. u8 send_peer_notif;
  202. s8 setup_by_slave;
  203. s8 igmp_retrans;
  204. #ifdef CONFIG_PROC_FS
  205. struct proc_dir_entry *proc_entry;
  206. char proc_file_name[IFNAMSIZ];
  207. #endif /* CONFIG_PROC_FS */
  208. struct list_head bond_list;
  209. struct netdev_hw_addr_list mc_list;
  210. int (*xmit_hash_policy)(struct sk_buff *, int);
  211. u16 rr_tx_counter;
  212. struct ad_bond_info ad_info;
  213. struct alb_bond_info alb_info;
  214. struct bond_params params;
  215. struct list_head vlan_list;
  216. struct workqueue_struct *wq;
  217. struct delayed_work mii_work;
  218. struct delayed_work arp_work;
  219. struct delayed_work alb_work;
  220. struct delayed_work ad_work;
  221. struct delayed_work mcast_work;
  222. #ifdef CONFIG_DEBUG_FS
  223. /* debugging suport via debugfs */
  224. struct dentry *debug_dir;
  225. #endif /* CONFIG_DEBUG_FS */
  226. };
  227. static inline bool bond_vlan_used(struct bonding *bond)
  228. {
  229. return !list_empty(&bond->vlan_list);
  230. }
  231. #define bond_slave_get_rcu(dev) \
  232. ((struct slave *) rcu_dereference(dev->rx_handler_data))
  233. /**
  234. * Returns NULL if the net_device does not belong to any of the bond's slaves
  235. *
  236. * Caller must hold bond lock for read
  237. */
  238. static inline struct slave *bond_get_slave_by_dev(struct bonding *bond,
  239. struct net_device *slave_dev)
  240. {
  241. struct slave *slave = NULL;
  242. int i;
  243. bond_for_each_slave(bond, slave, i) {
  244. if (slave->dev == slave_dev) {
  245. return slave;
  246. }
  247. }
  248. return NULL;
  249. }
  250. static inline struct bonding *bond_get_bond_by_slave(struct slave *slave)
  251. {
  252. if (!slave || !slave->dev->master) {
  253. return NULL;
  254. }
  255. return netdev_priv(slave->dev->master);
  256. }
  257. static inline bool bond_is_lb(const struct bonding *bond)
  258. {
  259. return (bond->params.mode == BOND_MODE_TLB ||
  260. bond->params.mode == BOND_MODE_ALB);
  261. }
  262. static inline void bond_set_active_slave(struct slave *slave)
  263. {
  264. slave->backup = 0;
  265. }
  266. static inline void bond_set_backup_slave(struct slave *slave)
  267. {
  268. slave->backup = 1;
  269. }
  270. static inline int bond_slave_state(struct slave *slave)
  271. {
  272. return slave->backup;
  273. }
  274. static inline bool bond_is_active_slave(struct slave *slave)
  275. {
  276. return !bond_slave_state(slave);
  277. }
  278. #define BOND_PRI_RESELECT_ALWAYS 0
  279. #define BOND_PRI_RESELECT_BETTER 1
  280. #define BOND_PRI_RESELECT_FAILURE 2
  281. #define BOND_FOM_NONE 0
  282. #define BOND_FOM_ACTIVE 1
  283. #define BOND_FOM_FOLLOW 2
  284. #define BOND_ARP_VALIDATE_NONE 0
  285. #define BOND_ARP_VALIDATE_ACTIVE (1 << BOND_STATE_ACTIVE)
  286. #define BOND_ARP_VALIDATE_BACKUP (1 << BOND_STATE_BACKUP)
  287. #define BOND_ARP_VALIDATE_ALL (BOND_ARP_VALIDATE_ACTIVE | \
  288. BOND_ARP_VALIDATE_BACKUP)
  289. static inline int slave_do_arp_validate(struct bonding *bond,
  290. struct slave *slave)
  291. {
  292. return bond->params.arp_validate & (1 << bond_slave_state(slave));
  293. }
  294. static inline unsigned long slave_last_rx(struct bonding *bond,
  295. struct slave *slave)
  296. {
  297. if (slave_do_arp_validate(bond, slave))
  298. return slave->last_arp_rx;
  299. return slave->dev->last_rx;
  300. }
  301. #ifdef CONFIG_NET_POLL_CONTROLLER
  302. static inline void bond_netpoll_send_skb(const struct slave *slave,
  303. struct sk_buff *skb)
  304. {
  305. struct netpoll *np = slave->np;
  306. if (np)
  307. netpoll_send_skb(np, skb);
  308. }
  309. #else
  310. static inline void bond_netpoll_send_skb(const struct slave *slave,
  311. struct sk_buff *skb)
  312. {
  313. }
  314. #endif
  315. static inline void bond_set_slave_inactive_flags(struct slave *slave)
  316. {
  317. struct bonding *bond = netdev_priv(slave->dev->master);
  318. if (!bond_is_lb(bond))
  319. bond_set_backup_slave(slave);
  320. if (!bond->params.all_slaves_active)
  321. slave->inactive = 1;
  322. }
  323. static inline void bond_set_slave_active_flags(struct slave *slave)
  324. {
  325. bond_set_active_slave(slave);
  326. slave->inactive = 0;
  327. }
  328. static inline bool bond_is_slave_inactive(struct slave *slave)
  329. {
  330. return slave->inactive;
  331. }
  332. static inline __be32 bond_confirm_addr(struct net_device *dev, __be32 dst, __be32 local)
  333. {
  334. struct in_device *in_dev;
  335. __be32 addr = 0;
  336. rcu_read_lock();
  337. in_dev = __in_dev_get_rcu(dev);
  338. if (in_dev)
  339. addr = inet_confirm_addr(in_dev, dst, local, RT_SCOPE_HOST);
  340. rcu_read_unlock();
  341. return addr;
  342. }
  343. struct bond_net;
  344. struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr);
  345. int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb, struct net_device *slave_dev);
  346. int bond_create(struct net *net, const char *name);
  347. int bond_create_sysfs(struct bond_net *net);
  348. void bond_destroy_sysfs(struct bond_net *net);
  349. void bond_prepare_sysfs_group(struct bonding *bond);
  350. int bond_create_slave_symlinks(struct net_device *master, struct net_device *slave);
  351. void bond_destroy_slave_symlinks(struct net_device *master, struct net_device *slave);
  352. int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev);
  353. int bond_release(struct net_device *bond_dev, struct net_device *slave_dev);
  354. void bond_mii_monitor(struct work_struct *);
  355. void bond_loadbalance_arp_mon(struct work_struct *);
  356. void bond_activebackup_arp_mon(struct work_struct *);
  357. void bond_set_mode_ops(struct bonding *bond, int mode);
  358. int bond_parse_parm(const char *mode_arg, const struct bond_parm_tbl *tbl);
  359. void bond_select_active_slave(struct bonding *bond);
  360. void bond_change_active_slave(struct bonding *bond, struct slave *new_active);
  361. void bond_create_debugfs(void);
  362. void bond_destroy_debugfs(void);
  363. void bond_debug_register(struct bonding *bond);
  364. void bond_debug_unregister(struct bonding *bond);
  365. void bond_debug_reregister(struct bonding *bond);
  366. const char *bond_mode_name(int mode);
  367. struct bond_net {
  368. struct net * net; /* Associated network namespace */
  369. struct list_head dev_list;
  370. #ifdef CONFIG_PROC_FS
  371. struct proc_dir_entry * proc_dir;
  372. #endif
  373. struct class_attribute class_attr_bonding_masters;
  374. };
  375. #ifdef CONFIG_PROC_FS
  376. void bond_create_proc_entry(struct bonding *bond);
  377. void bond_remove_proc_entry(struct bonding *bond);
  378. void bond_create_proc_dir(struct bond_net *bn);
  379. void bond_destroy_proc_dir(struct bond_net *bn);
  380. #else
  381. static inline void bond_create_proc_entry(struct bonding *bond)
  382. {
  383. }
  384. static inline void bond_remove_proc_entry(struct bonding *bond)
  385. {
  386. }
  387. static inline void bond_create_proc_dir(struct bond_net *bn)
  388. {
  389. }
  390. static inline void bond_destroy_proc_dir(struct bond_net *bn)
  391. {
  392. }
  393. #endif
  394. static inline struct slave *bond_slave_has_mac(struct bonding *bond,
  395. const u8 *mac)
  396. {
  397. int i = 0;
  398. struct slave *tmp;
  399. bond_for_each_slave(bond, tmp, i)
  400. if (!compare_ether_addr_64bits(mac, tmp->dev->dev_addr))
  401. return tmp;
  402. return NULL;
  403. }
  404. /* exported from bond_main.c */
  405. extern int bond_net_id;
  406. extern const struct bond_parm_tbl bond_lacp_tbl[];
  407. extern const struct bond_parm_tbl bond_mode_tbl[];
  408. extern const struct bond_parm_tbl xmit_hashtype_tbl[];
  409. extern const struct bond_parm_tbl arp_validate_tbl[];
  410. extern const struct bond_parm_tbl fail_over_mac_tbl[];
  411. extern const struct bond_parm_tbl pri_reselect_tbl[];
  412. extern struct bond_parm_tbl ad_select_tbl[];
  413. #endif /* _LINUX_BONDING_H */