bonding.h 12 KB

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