bonding.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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/kobject.h>
  20. #include <linux/cpumask.h>
  21. #include <linux/in6.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_MAX_ARP_TARGETS 16
  29. #define IS_UP(dev) \
  30. ((((dev)->flags & IFF_UP) == IFF_UP) && \
  31. netif_running(dev) && \
  32. netif_carrier_ok(dev))
  33. /*
  34. * Checks whether bond is ready for transmit.
  35. *
  36. * Caller must hold bond->lock
  37. */
  38. #define BOND_IS_OK(bond) \
  39. (((bond)->dev->flags & IFF_UP) && \
  40. netif_running((bond)->dev) && \
  41. ((bond)->slave_cnt > 0))
  42. /*
  43. * Checks whether slave is ready for transmit.
  44. */
  45. #define SLAVE_IS_OK(slave) \
  46. (((slave)->dev->flags & IFF_UP) && \
  47. netif_running((slave)->dev) && \
  48. ((slave)->link == BOND_LINK_UP) && \
  49. ((slave)->state == BOND_STATE_ACTIVE))
  50. #define USES_PRIMARY(mode) \
  51. (((mode) == BOND_MODE_ACTIVEBACKUP) || \
  52. ((mode) == BOND_MODE_TLB) || \
  53. ((mode) == BOND_MODE_ALB))
  54. #define TX_QUEUE_OVERRIDE(mode) \
  55. (((mode) == BOND_MODE_ACTIVEBACKUP) || \
  56. ((mode) == BOND_MODE_ROUNDROBIN))
  57. /*
  58. * Less bad way to call ioctl from within the kernel; this needs to be
  59. * done some other way to get the call out of interrupt context.
  60. * Needs "ioctl" variable to be supplied by calling context.
  61. */
  62. #define IOCTL(dev, arg, cmd) ({ \
  63. int res = 0; \
  64. mm_segment_t fs = get_fs(); \
  65. set_fs(get_ds()); \
  66. res = ioctl(dev, arg, cmd); \
  67. set_fs(fs); \
  68. res; })
  69. /**
  70. * bond_for_each_slave_from - iterate the slaves list from a starting point
  71. * @bond: the bond holding this list.
  72. * @pos: current slave.
  73. * @cnt: counter for max number of moves
  74. * @start: starting point.
  75. *
  76. * Caller must hold bond->lock
  77. */
  78. #define bond_for_each_slave_from(bond, pos, cnt, start) \
  79. for (cnt = 0, pos = start; \
  80. cnt < (bond)->slave_cnt; \
  81. cnt++, pos = (pos)->next)
  82. /**
  83. * bond_for_each_slave_from_to - iterate the slaves list from start point to stop point
  84. * @bond: the bond holding this list.
  85. * @pos: current slave.
  86. * @cnt: counter for number max of moves
  87. * @start: start point.
  88. * @stop: stop point.
  89. *
  90. * Caller must hold bond->lock
  91. */
  92. #define bond_for_each_slave_from_to(bond, pos, cnt, start, stop) \
  93. for (cnt = 0, pos = start; \
  94. ((cnt < (bond)->slave_cnt) && (pos != (stop)->next)); \
  95. cnt++, pos = (pos)->next)
  96. /**
  97. * bond_for_each_slave - iterate the slaves list from head
  98. * @bond: the bond holding this list.
  99. * @pos: current slave.
  100. * @cnt: counter for max number of moves
  101. *
  102. * Caller must hold bond->lock
  103. */
  104. #define bond_for_each_slave(bond, pos, cnt) \
  105. bond_for_each_slave_from(bond, pos, cnt, (bond)->first_slave)
  106. #ifdef CONFIG_NET_POLL_CONTROLLER
  107. extern cpumask_var_t netpoll_block_tx;
  108. static inline void block_netpoll_tx(void)
  109. {
  110. preempt_disable();
  111. BUG_ON(cpumask_test_and_set_cpu(smp_processor_id(),
  112. netpoll_block_tx));
  113. }
  114. static inline void unblock_netpoll_tx(void)
  115. {
  116. BUG_ON(!cpumask_test_and_clear_cpu(smp_processor_id(),
  117. netpoll_block_tx));
  118. preempt_enable();
  119. }
  120. static inline int is_netpoll_tx_blocked(struct net_device *dev)
  121. {
  122. if (unlikely(dev->priv_flags & IFF_IN_NETPOLL))
  123. return cpumask_test_cpu(smp_processor_id(), netpoll_block_tx);
  124. return 0;
  125. }
  126. #else
  127. #define block_netpoll_tx()
  128. #define unblock_netpoll_tx()
  129. #define is_netpoll_tx_blocked(dev) (0)
  130. #endif
  131. struct bond_params {
  132. int mode;
  133. int xmit_policy;
  134. int miimon;
  135. int num_grat_arp;
  136. int num_unsol_na;
  137. int arp_interval;
  138. int arp_validate;
  139. int use_carrier;
  140. int fail_over_mac;
  141. int updelay;
  142. int downdelay;
  143. int lacp_fast;
  144. int ad_select;
  145. char primary[IFNAMSIZ];
  146. int primary_reselect;
  147. __be32 arp_targets[BOND_MAX_ARP_TARGETS];
  148. int tx_queues;
  149. int all_slaves_active;
  150. int resend_igmp;
  151. };
  152. struct bond_parm_tbl {
  153. char *modename;
  154. int mode;
  155. };
  156. #define BOND_MAX_MODENAME_LEN 20
  157. struct vlan_entry {
  158. struct list_head vlan_list;
  159. __be32 vlan_ip;
  160. unsigned short vlan_id;
  161. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  162. struct in6_addr vlan_ipv6;
  163. #endif
  164. };
  165. struct slave {
  166. struct net_device *dev; /* first - useful for panic debug */
  167. struct slave *next;
  168. struct slave *prev;
  169. int delay;
  170. unsigned long jiffies;
  171. unsigned long last_arp_rx;
  172. s8 link; /* one of BOND_LINK_XXXX */
  173. s8 new_link;
  174. s8 state; /* one of BOND_STATE_XXXX */
  175. u32 original_mtu;
  176. u32 link_failure_count;
  177. u8 perm_hwaddr[ETH_ALEN];
  178. u16 speed;
  179. u8 duplex;
  180. u16 queue_id;
  181. struct ad_slave_info ad_info; /* HUGE - better to dynamically alloc */
  182. struct tlb_slave_info tlb_info;
  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. };
  238. /**
  239. * Returns NULL if the net_device does not belong to any of the bond's slaves
  240. *
  241. * Caller must hold bond lock for read
  242. */
  243. static inline struct slave *bond_get_slave_by_dev(struct bonding *bond, struct net_device *slave_dev)
  244. {
  245. struct slave *slave = NULL;
  246. int i;
  247. bond_for_each_slave(bond, slave, i) {
  248. if (slave->dev == slave_dev) {
  249. break;
  250. }
  251. }
  252. return slave;
  253. }
  254. static inline struct bonding *bond_get_bond_by_slave(struct slave *slave)
  255. {
  256. if (!slave || !slave->dev->master) {
  257. return NULL;
  258. }
  259. return (struct bonding *)netdev_priv(slave->dev->master);
  260. }
  261. static inline bool bond_is_lb(const struct bonding *bond)
  262. {
  263. return (bond->params.mode == BOND_MODE_TLB ||
  264. bond->params.mode == BOND_MODE_ALB);
  265. }
  266. #define BOND_PRI_RESELECT_ALWAYS 0
  267. #define BOND_PRI_RESELECT_BETTER 1
  268. #define BOND_PRI_RESELECT_FAILURE 2
  269. #define BOND_FOM_NONE 0
  270. #define BOND_FOM_ACTIVE 1
  271. #define BOND_FOM_FOLLOW 2
  272. #define BOND_ARP_VALIDATE_NONE 0
  273. #define BOND_ARP_VALIDATE_ACTIVE (1 << BOND_STATE_ACTIVE)
  274. #define BOND_ARP_VALIDATE_BACKUP (1 << BOND_STATE_BACKUP)
  275. #define BOND_ARP_VALIDATE_ALL (BOND_ARP_VALIDATE_ACTIVE | \
  276. BOND_ARP_VALIDATE_BACKUP)
  277. static inline int slave_do_arp_validate(struct bonding *bond,
  278. struct slave *slave)
  279. {
  280. return bond->params.arp_validate & (1 << slave->state);
  281. }
  282. static inline unsigned long slave_last_rx(struct bonding *bond,
  283. struct slave *slave)
  284. {
  285. if (slave_do_arp_validate(bond, slave))
  286. return slave->last_arp_rx;
  287. return slave->dev->last_rx;
  288. }
  289. static inline void bond_set_slave_inactive_flags(struct slave *slave)
  290. {
  291. struct bonding *bond = netdev_priv(slave->dev->master);
  292. if (!bond_is_lb(bond))
  293. slave->state = BOND_STATE_BACKUP;
  294. if (!bond->params.all_slaves_active)
  295. slave->dev->priv_flags |= IFF_SLAVE_INACTIVE;
  296. if (slave_do_arp_validate(bond, slave))
  297. slave->dev->priv_flags |= IFF_SLAVE_NEEDARP;
  298. }
  299. static inline void bond_set_slave_active_flags(struct slave *slave)
  300. {
  301. slave->state = BOND_STATE_ACTIVE;
  302. slave->dev->priv_flags &= ~(IFF_SLAVE_INACTIVE | IFF_SLAVE_NEEDARP);
  303. }
  304. static inline void bond_set_master_3ad_flags(struct bonding *bond)
  305. {
  306. bond->dev->priv_flags |= IFF_MASTER_8023AD;
  307. }
  308. static inline void bond_unset_master_3ad_flags(struct bonding *bond)
  309. {
  310. bond->dev->priv_flags &= ~IFF_MASTER_8023AD;
  311. }
  312. static inline void bond_set_master_alb_flags(struct bonding *bond)
  313. {
  314. bond->dev->priv_flags |= IFF_MASTER_ALB;
  315. }
  316. static inline void bond_unset_master_alb_flags(struct bonding *bond)
  317. {
  318. bond->dev->priv_flags &= ~IFF_MASTER_ALB;
  319. }
  320. struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr);
  321. int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb, struct net_device *slave_dev);
  322. int bond_create(struct net *net, const char *name);
  323. int bond_create_sysfs(void);
  324. void bond_destroy_sysfs(void);
  325. void bond_prepare_sysfs_group(struct bonding *bond);
  326. int bond_create_slave_symlinks(struct net_device *master, struct net_device *slave);
  327. void bond_destroy_slave_symlinks(struct net_device *master, struct net_device *slave);
  328. int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev);
  329. int bond_release(struct net_device *bond_dev, struct net_device *slave_dev);
  330. void bond_mii_monitor(struct work_struct *);
  331. void bond_loadbalance_arp_mon(struct work_struct *);
  332. void bond_activebackup_arp_mon(struct work_struct *);
  333. void bond_set_mode_ops(struct bonding *bond, int mode);
  334. int bond_parse_parm(const char *mode_arg, const struct bond_parm_tbl *tbl);
  335. void bond_select_active_slave(struct bonding *bond);
  336. void bond_change_active_slave(struct bonding *bond, struct slave *new_active);
  337. void bond_register_arp(struct bonding *);
  338. void bond_unregister_arp(struct bonding *);
  339. struct bond_net {
  340. struct net * net; /* Associated network namespace */
  341. struct list_head dev_list;
  342. #ifdef CONFIG_PROC_FS
  343. struct proc_dir_entry * proc_dir;
  344. #endif
  345. };
  346. /* exported from bond_main.c */
  347. extern int bond_net_id;
  348. extern const struct bond_parm_tbl bond_lacp_tbl[];
  349. extern const struct bond_parm_tbl bond_mode_tbl[];
  350. extern const struct bond_parm_tbl xmit_hashtype_tbl[];
  351. extern const struct bond_parm_tbl arp_validate_tbl[];
  352. extern const struct bond_parm_tbl fail_over_mac_tbl[];
  353. extern const struct bond_parm_tbl pri_reselect_tbl[];
  354. extern struct bond_parm_tbl ad_select_tbl[];
  355. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  356. void bond_send_unsolicited_na(struct bonding *bond);
  357. void bond_register_ipv6_notifier(void);
  358. void bond_unregister_ipv6_notifier(void);
  359. #else
  360. static inline void bond_send_unsolicited_na(struct bonding *bond)
  361. {
  362. return;
  363. }
  364. static inline void bond_register_ipv6_notifier(void)
  365. {
  366. return;
  367. }
  368. static inline void bond_unregister_ipv6_notifier(void)
  369. {
  370. return;
  371. }
  372. #endif
  373. #endif /* _LINUX_BONDING_H */