bonding.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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 "bond_3ad.h"
  23. #include "bond_alb.h"
  24. #define DRV_VERSION "3.7.0"
  25. #define DRV_RELDATE "June 2, 2010"
  26. #define DRV_NAME "bonding"
  27. #define DRV_DESCRIPTION "Ethernet Channel Bonding Driver"
  28. #define bond_version DRV_DESCRIPTION ": v" DRV_VERSION " (" DRV_RELDATE ")\n"
  29. #define BOND_MAX_ARP_TARGETS 16
  30. #define IS_UP(dev) \
  31. ((((dev)->flags & IFF_UP) == IFF_UP) && \
  32. netif_running(dev) && \
  33. netif_carrier_ok(dev))
  34. /*
  35. * Checks whether bond is ready for transmit.
  36. *
  37. * Caller must hold bond->lock
  38. */
  39. #define BOND_IS_OK(bond) \
  40. (((bond)->dev->flags & IFF_UP) && \
  41. netif_running((bond)->dev) && \
  42. ((bond)->slave_cnt > 0))
  43. /*
  44. * Checks whether slave is ready for transmit.
  45. */
  46. #define SLAVE_IS_OK(slave) \
  47. (((slave)->dev->flags & IFF_UP) && \
  48. netif_running((slave)->dev) && \
  49. ((slave)->link == BOND_LINK_UP) && \
  50. ((slave)->state == BOND_STATE_ACTIVE))
  51. #define USES_PRIMARY(mode) \
  52. (((mode) == BOND_MODE_ACTIVEBACKUP) || \
  53. ((mode) == BOND_MODE_TLB) || \
  54. ((mode) == BOND_MODE_ALB))
  55. #define TX_QUEUE_OVERRIDE(mode) \
  56. (((mode) == BOND_MODE_ACTIVEBACKUP) || \
  57. ((mode) == BOND_MODE_ROUNDROBIN))
  58. /*
  59. * Less bad way to call ioctl from within the kernel; this needs to be
  60. * done some other way to get the call out of interrupt context.
  61. * Needs "ioctl" variable to be supplied by calling context.
  62. */
  63. #define IOCTL(dev, arg, cmd) ({ \
  64. int res = 0; \
  65. mm_segment_t fs = get_fs(); \
  66. set_fs(get_ds()); \
  67. res = ioctl(dev, arg, cmd); \
  68. set_fs(fs); \
  69. res; })
  70. /**
  71. * bond_for_each_slave_from - iterate the slaves list from a starting point
  72. * @bond: the bond holding this list.
  73. * @pos: current slave.
  74. * @cnt: counter for max number of moves
  75. * @start: starting point.
  76. *
  77. * Caller must hold bond->lock
  78. */
  79. #define bond_for_each_slave_from(bond, pos, cnt, start) \
  80. for (cnt = 0, pos = start; \
  81. cnt < (bond)->slave_cnt; \
  82. cnt++, pos = (pos)->next)
  83. /**
  84. * bond_for_each_slave_from_to - iterate the slaves list from start point to stop point
  85. * @bond: the bond holding this list.
  86. * @pos: current slave.
  87. * @cnt: counter for number max of moves
  88. * @start: start point.
  89. * @stop: stop point.
  90. *
  91. * Caller must hold bond->lock
  92. */
  93. #define bond_for_each_slave_from_to(bond, pos, cnt, start, stop) \
  94. for (cnt = 0, pos = start; \
  95. ((cnt < (bond)->slave_cnt) && (pos != (stop)->next)); \
  96. cnt++, pos = (pos)->next)
  97. /**
  98. * bond_for_each_slave - iterate the slaves list from head
  99. * @bond: the bond holding this list.
  100. * @pos: current slave.
  101. * @cnt: counter for max number of moves
  102. *
  103. * Caller must hold bond->lock
  104. */
  105. #define bond_for_each_slave(bond, pos, cnt) \
  106. bond_for_each_slave_from(bond, pos, cnt, (bond)->first_slave)
  107. #ifdef CONFIG_NET_POLL_CONTROLLER
  108. extern atomic_t netpoll_block_tx;
  109. static inline void block_netpoll_tx(void)
  110. {
  111. atomic_inc(&netpoll_block_tx);
  112. }
  113. static inline void unblock_netpoll_tx(void)
  114. {
  115. atomic_dec(&netpoll_block_tx);
  116. }
  117. static inline int is_netpoll_tx_blocked(struct net_device *dev)
  118. {
  119. if (unlikely(netpoll_tx_running(dev)))
  120. return atomic_read(&netpoll_block_tx);
  121. return 0;
  122. }
  123. #else
  124. #define block_netpoll_tx()
  125. #define unblock_netpoll_tx()
  126. #define is_netpoll_tx_blocked(dev) (0)
  127. #endif
  128. struct bond_params {
  129. int mode;
  130. int xmit_policy;
  131. int miimon;
  132. int num_grat_arp;
  133. int num_unsol_na;
  134. int arp_interval;
  135. int arp_validate;
  136. int use_carrier;
  137. int fail_over_mac;
  138. int updelay;
  139. int downdelay;
  140. int lacp_fast;
  141. int ad_select;
  142. char primary[IFNAMSIZ];
  143. int primary_reselect;
  144. __be32 arp_targets[BOND_MAX_ARP_TARGETS];
  145. int tx_queues;
  146. int all_slaves_active;
  147. int resend_igmp;
  148. };
  149. struct bond_parm_tbl {
  150. char *modename;
  151. int mode;
  152. };
  153. #define BOND_MAX_MODENAME_LEN 20
  154. struct vlan_entry {
  155. struct list_head vlan_list;
  156. __be32 vlan_ip;
  157. unsigned short vlan_id;
  158. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  159. struct in6_addr vlan_ipv6;
  160. #endif
  161. };
  162. struct slave {
  163. struct net_device *dev; /* first - useful for panic debug */
  164. struct slave *next;
  165. struct slave *prev;
  166. int delay;
  167. unsigned long jiffies;
  168. unsigned long last_arp_rx;
  169. s8 link; /* one of BOND_LINK_XXXX */
  170. s8 new_link;
  171. s8 state; /* one of BOND_STATE_XXXX */
  172. u32 original_mtu;
  173. u32 link_failure_count;
  174. u8 perm_hwaddr[ETH_ALEN];
  175. u16 speed;
  176. u8 duplex;
  177. u16 queue_id;
  178. struct ad_slave_info ad_info; /* HUGE - better to dynamically alloc */
  179. struct tlb_slave_info tlb_info;
  180. #ifdef CONFIG_NET_POLL_CONTROLLER
  181. struct netpoll *np;
  182. #endif
  183. };
  184. /*
  185. * Link pseudo-state only used internally by monitors
  186. */
  187. #define BOND_LINK_NOCHANGE -1
  188. /*
  189. * Here are the locking policies for the two bonding locks:
  190. *
  191. * 1) Get bond->lock when reading/writing slave list.
  192. * 2) Get bond->curr_slave_lock when reading/writing bond->curr_active_slave.
  193. * (It is unnecessary when the write-lock is put with bond->lock.)
  194. * 3) When we lock with bond->curr_slave_lock, we must lock with bond->lock
  195. * beforehand.
  196. */
  197. struct bonding {
  198. struct net_device *dev; /* first - useful for panic debug */
  199. struct slave *first_slave;
  200. struct slave *curr_active_slave;
  201. struct slave *current_arp_slave;
  202. struct slave *primary_slave;
  203. bool force_primary;
  204. s32 slave_cnt; /* never change this value outside the attach/detach wrappers */
  205. rwlock_t lock;
  206. rwlock_t curr_slave_lock;
  207. s8 kill_timers;
  208. s8 send_grat_arp;
  209. s8 send_unsol_na;
  210. s8 setup_by_slave;
  211. s8 igmp_retrans;
  212. #ifdef CONFIG_PROC_FS
  213. struct proc_dir_entry *proc_entry;
  214. char proc_file_name[IFNAMSIZ];
  215. #endif /* CONFIG_PROC_FS */
  216. struct list_head bond_list;
  217. struct netdev_hw_addr_list mc_list;
  218. int (*xmit_hash_policy)(struct sk_buff *, int);
  219. __be32 master_ip;
  220. u16 flags;
  221. u16 rr_tx_counter;
  222. struct ad_bond_info ad_info;
  223. struct alb_bond_info alb_info;
  224. struct bond_params params;
  225. struct list_head vlan_list;
  226. struct vlan_group *vlgrp;
  227. struct packet_type arp_mon_pt;
  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. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  235. struct in6_addr master_ipv6;
  236. #endif
  237. #ifdef CONFIG_DEBUG_FS
  238. /* debugging suport via debugfs */
  239. struct dentry *debug_dir;
  240. #endif /* CONFIG_DEBUG_FS */
  241. };
  242. /**
  243. * Returns NULL if the net_device does not belong to any of the bond's slaves
  244. *
  245. * Caller must hold bond lock for read
  246. */
  247. static inline struct slave *bond_get_slave_by_dev(struct bonding *bond,
  248. struct net_device *slave_dev)
  249. {
  250. struct slave *slave = NULL;
  251. int i;
  252. bond_for_each_slave(bond, slave, i) {
  253. if (slave->dev == slave_dev) {
  254. return slave;
  255. }
  256. }
  257. return NULL;
  258. }
  259. static inline struct bonding *bond_get_bond_by_slave(struct slave *slave)
  260. {
  261. if (!slave || !slave->dev->master) {
  262. return NULL;
  263. }
  264. return netdev_priv(slave->dev->master);
  265. }
  266. static inline bool bond_is_lb(const struct bonding *bond)
  267. {
  268. return (bond->params.mode == BOND_MODE_TLB ||
  269. bond->params.mode == BOND_MODE_ALB);
  270. }
  271. #define BOND_PRI_RESELECT_ALWAYS 0
  272. #define BOND_PRI_RESELECT_BETTER 1
  273. #define BOND_PRI_RESELECT_FAILURE 2
  274. #define BOND_FOM_NONE 0
  275. #define BOND_FOM_ACTIVE 1
  276. #define BOND_FOM_FOLLOW 2
  277. #define BOND_ARP_VALIDATE_NONE 0
  278. #define BOND_ARP_VALIDATE_ACTIVE (1 << BOND_STATE_ACTIVE)
  279. #define BOND_ARP_VALIDATE_BACKUP (1 << BOND_STATE_BACKUP)
  280. #define BOND_ARP_VALIDATE_ALL (BOND_ARP_VALIDATE_ACTIVE | \
  281. BOND_ARP_VALIDATE_BACKUP)
  282. static inline int slave_do_arp_validate(struct bonding *bond,
  283. struct slave *slave)
  284. {
  285. return bond->params.arp_validate & (1 << slave->state);
  286. }
  287. static inline unsigned long slave_last_rx(struct bonding *bond,
  288. struct slave *slave)
  289. {
  290. if (slave_do_arp_validate(bond, slave))
  291. return slave->last_arp_rx;
  292. return slave->dev->last_rx;
  293. }
  294. #ifdef CONFIG_NET_POLL_CONTROLLER
  295. static inline void bond_netpoll_send_skb(const struct slave *slave,
  296. struct sk_buff *skb)
  297. {
  298. struct netpoll *np = slave->np;
  299. if (np)
  300. netpoll_send_skb(np, skb);
  301. }
  302. #else
  303. static inline void bond_netpoll_send_skb(const struct slave *slave,
  304. struct sk_buff *skb)
  305. {
  306. }
  307. #endif
  308. static inline void bond_set_slave_inactive_flags(struct slave *slave)
  309. {
  310. struct bonding *bond = netdev_priv(slave->dev->master);
  311. if (!bond_is_lb(bond))
  312. slave->state = BOND_STATE_BACKUP;
  313. if (!bond->params.all_slaves_active)
  314. slave->dev->priv_flags |= IFF_SLAVE_INACTIVE;
  315. if (slave_do_arp_validate(bond, slave))
  316. slave->dev->priv_flags |= IFF_SLAVE_NEEDARP;
  317. }
  318. static inline void bond_set_slave_active_flags(struct slave *slave)
  319. {
  320. slave->state = BOND_STATE_ACTIVE;
  321. slave->dev->priv_flags &= ~(IFF_SLAVE_INACTIVE | IFF_SLAVE_NEEDARP);
  322. }
  323. static inline void bond_set_master_3ad_flags(struct bonding *bond)
  324. {
  325. bond->dev->priv_flags |= IFF_MASTER_8023AD;
  326. }
  327. static inline void bond_unset_master_3ad_flags(struct bonding *bond)
  328. {
  329. bond->dev->priv_flags &= ~IFF_MASTER_8023AD;
  330. }
  331. static inline void bond_set_master_alb_flags(struct bonding *bond)
  332. {
  333. bond->dev->priv_flags |= IFF_MASTER_ALB;
  334. }
  335. static inline void bond_unset_master_alb_flags(struct bonding *bond)
  336. {
  337. bond->dev->priv_flags &= ~IFF_MASTER_ALB;
  338. }
  339. struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr);
  340. int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb, struct net_device *slave_dev);
  341. int bond_create(struct net *net, const char *name);
  342. int bond_create_sysfs(void);
  343. void bond_destroy_sysfs(void);
  344. void bond_prepare_sysfs_group(struct bonding *bond);
  345. int bond_create_slave_symlinks(struct net_device *master, struct net_device *slave);
  346. void bond_destroy_slave_symlinks(struct net_device *master, struct net_device *slave);
  347. int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev);
  348. int bond_release(struct net_device *bond_dev, struct net_device *slave_dev);
  349. void bond_mii_monitor(struct work_struct *);
  350. void bond_loadbalance_arp_mon(struct work_struct *);
  351. void bond_activebackup_arp_mon(struct work_struct *);
  352. void bond_set_mode_ops(struct bonding *bond, int mode);
  353. int bond_parse_parm(const char *mode_arg, const struct bond_parm_tbl *tbl);
  354. void bond_select_active_slave(struct bonding *bond);
  355. void bond_change_active_slave(struct bonding *bond, struct slave *new_active);
  356. void bond_register_arp(struct bonding *);
  357. void bond_unregister_arp(struct bonding *);
  358. void bond_create_debugfs(void);
  359. void bond_destroy_debugfs(void);
  360. void bond_debug_register(struct bonding *bond);
  361. void bond_debug_unregister(struct bonding *bond);
  362. void bond_debug_reregister(struct bonding *bond);
  363. struct bond_net {
  364. struct net * net; /* Associated network namespace */
  365. struct list_head dev_list;
  366. #ifdef CONFIG_PROC_FS
  367. struct proc_dir_entry * proc_dir;
  368. #endif
  369. };
  370. #ifdef CONFIG_PROC_FS
  371. void bond_create_proc_entry(struct bonding *bond);
  372. void bond_remove_proc_entry(struct bonding *bond);
  373. void bond_create_proc_dir(struct bond_net *bn);
  374. void bond_destroy_proc_dir(struct bond_net *bn);
  375. #else
  376. static inline void bond_create_proc_entry(struct bonding *bond)
  377. {
  378. }
  379. static inline void bond_remove_proc_entry(struct bonding *bond)
  380. {
  381. }
  382. static inline void bond_create_proc_dir(struct bond_net *bn)
  383. {
  384. }
  385. static inline void bond_destroy_proc_dir(struct bond_net *bn)
  386. {
  387. }
  388. #endif
  389. /* exported from bond_main.c */
  390. extern int bond_net_id;
  391. extern const struct bond_parm_tbl bond_lacp_tbl[];
  392. extern const struct bond_parm_tbl bond_mode_tbl[];
  393. extern const struct bond_parm_tbl xmit_hashtype_tbl[];
  394. extern const struct bond_parm_tbl arp_validate_tbl[];
  395. extern const struct bond_parm_tbl fail_over_mac_tbl[];
  396. extern const struct bond_parm_tbl pri_reselect_tbl[];
  397. extern struct bond_parm_tbl ad_select_tbl[];
  398. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  399. void bond_send_unsolicited_na(struct bonding *bond);
  400. void bond_register_ipv6_notifier(void);
  401. void bond_unregister_ipv6_notifier(void);
  402. #else
  403. static inline void bond_send_unsolicited_na(struct bonding *bond)
  404. {
  405. return;
  406. }
  407. static inline void bond_register_ipv6_notifier(void)
  408. {
  409. return;
  410. }
  411. static inline void bond_unregister_ipv6_notifier(void)
  412. {
  413. return;
  414. }
  415. #endif
  416. #endif /* _LINUX_BONDING_H */