bonding.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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_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 atomic_t netpoll_block_tx;
  108. static inline void block_netpoll_tx(void)
  109. {
  110. atomic_inc(&netpoll_block_tx);
  111. }
  112. static inline void unblock_netpoll_tx(void)
  113. {
  114. atomic_dec(&netpoll_block_tx);
  115. }
  116. static inline int is_netpoll_tx_blocked(struct net_device *dev)
  117. {
  118. if (unlikely(netpoll_tx_running(dev)))
  119. return atomic_read(&netpoll_block_tx);
  120. return 0;
  121. }
  122. #else
  123. #define block_netpoll_tx()
  124. #define unblock_netpoll_tx()
  125. #define is_netpoll_tx_blocked(dev) (0)
  126. #endif
  127. struct bond_params {
  128. int mode;
  129. int xmit_policy;
  130. int miimon;
  131. int num_grat_arp;
  132. int num_unsol_na;
  133. int arp_interval;
  134. int arp_validate;
  135. int use_carrier;
  136. int fail_over_mac;
  137. int updelay;
  138. int downdelay;
  139. int lacp_fast;
  140. int ad_select;
  141. char primary[IFNAMSIZ];
  142. int primary_reselect;
  143. __be32 arp_targets[BOND_MAX_ARP_TARGETS];
  144. int tx_queues;
  145. int all_slaves_active;
  146. int resend_igmp;
  147. };
  148. struct bond_parm_tbl {
  149. char *modename;
  150. int mode;
  151. };
  152. #define BOND_MAX_MODENAME_LEN 20
  153. struct vlan_entry {
  154. struct list_head vlan_list;
  155. __be32 vlan_ip;
  156. unsigned short vlan_id;
  157. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  158. struct in6_addr vlan_ipv6;
  159. #endif
  160. };
  161. struct slave {
  162. struct net_device *dev; /* first - useful for panic debug */
  163. struct slave *next;
  164. struct slave *prev;
  165. int delay;
  166. unsigned long jiffies;
  167. unsigned long last_arp_rx;
  168. s8 link; /* one of BOND_LINK_XXXX */
  169. s8 new_link;
  170. s8 state; /* one of BOND_STATE_XXXX */
  171. u32 original_mtu;
  172. u32 link_failure_count;
  173. u8 perm_hwaddr[ETH_ALEN];
  174. u16 speed;
  175. u8 duplex;
  176. u16 queue_id;
  177. struct ad_slave_info ad_info; /* HUGE - better to dynamically alloc */
  178. struct tlb_slave_info tlb_info;
  179. #ifdef CONFIG_NET_POLL_CONTROLLER
  180. struct netpoll *np;
  181. #endif
  182. };
  183. /*
  184. * Link pseudo-state only used internally by monitors
  185. */
  186. #define BOND_LINK_NOCHANGE -1
  187. /*
  188. * Here are the locking policies for the two bonding locks:
  189. *
  190. * 1) Get bond->lock when reading/writing slave list.
  191. * 2) Get bond->curr_slave_lock when reading/writing bond->curr_active_slave.
  192. * (It is unnecessary when the write-lock is put with bond->lock.)
  193. * 3) When we lock with bond->curr_slave_lock, we must lock with bond->lock
  194. * beforehand.
  195. */
  196. struct bonding {
  197. struct net_device *dev; /* first - useful for panic debug */
  198. struct slave *first_slave;
  199. struct slave *curr_active_slave;
  200. struct slave *current_arp_slave;
  201. struct slave *primary_slave;
  202. bool force_primary;
  203. s32 slave_cnt; /* never change this value outside the attach/detach wrappers */
  204. rwlock_t lock;
  205. rwlock_t curr_slave_lock;
  206. s8 kill_timers;
  207. s8 send_grat_arp;
  208. s8 send_unsol_na;
  209. s8 setup_by_slave;
  210. s8 igmp_retrans;
  211. #ifdef CONFIG_PROC_FS
  212. struct proc_dir_entry *proc_entry;
  213. char proc_file_name[IFNAMSIZ];
  214. #endif /* CONFIG_PROC_FS */
  215. struct list_head bond_list;
  216. struct netdev_hw_addr_list mc_list;
  217. int (*xmit_hash_policy)(struct sk_buff *, int);
  218. __be32 master_ip;
  219. u16 flags;
  220. u16 rr_tx_counter;
  221. struct ad_bond_info ad_info;
  222. struct alb_bond_info alb_info;
  223. struct bond_params params;
  224. struct list_head vlan_list;
  225. struct vlan_group *vlgrp;
  226. struct packet_type arp_mon_pt;
  227. struct workqueue_struct *wq;
  228. struct delayed_work mii_work;
  229. struct delayed_work arp_work;
  230. struct delayed_work alb_work;
  231. struct delayed_work ad_work;
  232. struct delayed_work mcast_work;
  233. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  234. struct in6_addr master_ipv6;
  235. #endif
  236. #ifdef CONFIG_DEBUG_FS
  237. /* debugging suport via debugfs */
  238. struct dentry *debug_dir;
  239. #endif /* CONFIG_DEBUG_FS */
  240. };
  241. /**
  242. * Returns NULL if the net_device does not belong to any of the bond's slaves
  243. *
  244. * Caller must hold bond lock for read
  245. */
  246. static inline struct slave *bond_get_slave_by_dev(struct bonding *bond,
  247. struct net_device *slave_dev)
  248. {
  249. struct slave *slave = NULL;
  250. int i;
  251. bond_for_each_slave(bond, slave, i) {
  252. if (slave->dev == slave_dev) {
  253. return slave;
  254. }
  255. }
  256. return NULL;
  257. }
  258. static inline struct bonding *bond_get_bond_by_slave(struct slave *slave)
  259. {
  260. if (!slave || !slave->dev->master) {
  261. return NULL;
  262. }
  263. return netdev_priv(slave->dev->master);
  264. }
  265. static inline bool bond_is_lb(const struct bonding *bond)
  266. {
  267. return (bond->params.mode == BOND_MODE_TLB ||
  268. bond->params.mode == BOND_MODE_ALB);
  269. }
  270. #define BOND_PRI_RESELECT_ALWAYS 0
  271. #define BOND_PRI_RESELECT_BETTER 1
  272. #define BOND_PRI_RESELECT_FAILURE 2
  273. #define BOND_FOM_NONE 0
  274. #define BOND_FOM_ACTIVE 1
  275. #define BOND_FOM_FOLLOW 2
  276. #define BOND_ARP_VALIDATE_NONE 0
  277. #define BOND_ARP_VALIDATE_ACTIVE (1 << BOND_STATE_ACTIVE)
  278. #define BOND_ARP_VALIDATE_BACKUP (1 << BOND_STATE_BACKUP)
  279. #define BOND_ARP_VALIDATE_ALL (BOND_ARP_VALIDATE_ACTIVE | \
  280. BOND_ARP_VALIDATE_BACKUP)
  281. static inline int slave_do_arp_validate(struct bonding *bond,
  282. struct slave *slave)
  283. {
  284. return bond->params.arp_validate & (1 << slave->state);
  285. }
  286. static inline unsigned long slave_last_rx(struct bonding *bond,
  287. struct slave *slave)
  288. {
  289. if (slave_do_arp_validate(bond, slave))
  290. return slave->last_arp_rx;
  291. return slave->dev->last_rx;
  292. }
  293. #ifdef CONFIG_NET_POLL_CONTROLLER
  294. static inline void bond_netpoll_send_skb(const struct slave *slave,
  295. struct sk_buff *skb)
  296. {
  297. struct netpoll *np = slave->np;
  298. if (np)
  299. netpoll_send_skb(np, skb);
  300. }
  301. #else
  302. static inline void bond_netpoll_send_skb(const struct slave *slave,
  303. struct sk_buff *skb)
  304. {
  305. }
  306. #endif
  307. static inline void bond_set_slave_inactive_flags(struct slave *slave)
  308. {
  309. struct bonding *bond = netdev_priv(slave->dev->master);
  310. if (!bond_is_lb(bond))
  311. slave->state = BOND_STATE_BACKUP;
  312. if (!bond->params.all_slaves_active)
  313. slave->dev->priv_flags |= IFF_SLAVE_INACTIVE;
  314. if (slave_do_arp_validate(bond, slave))
  315. slave->dev->priv_flags |= IFF_SLAVE_NEEDARP;
  316. }
  317. static inline void bond_set_slave_active_flags(struct slave *slave)
  318. {
  319. slave->state = BOND_STATE_ACTIVE;
  320. slave->dev->priv_flags &= ~(IFF_SLAVE_INACTIVE | IFF_SLAVE_NEEDARP);
  321. }
  322. static inline void bond_set_master_3ad_flags(struct bonding *bond)
  323. {
  324. bond->dev->priv_flags |= IFF_MASTER_8023AD;
  325. }
  326. static inline void bond_unset_master_3ad_flags(struct bonding *bond)
  327. {
  328. bond->dev->priv_flags &= ~IFF_MASTER_8023AD;
  329. }
  330. static inline void bond_set_master_alb_flags(struct bonding *bond)
  331. {
  332. bond->dev->priv_flags |= IFF_MASTER_ALB;
  333. }
  334. static inline void bond_unset_master_alb_flags(struct bonding *bond)
  335. {
  336. bond->dev->priv_flags &= ~IFF_MASTER_ALB;
  337. }
  338. struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr);
  339. int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb, struct net_device *slave_dev);
  340. int bond_create(struct net *net, const char *name);
  341. int bond_create_sysfs(void);
  342. void bond_destroy_sysfs(void);
  343. void bond_prepare_sysfs_group(struct bonding *bond);
  344. int bond_create_slave_symlinks(struct net_device *master, struct net_device *slave);
  345. void bond_destroy_slave_symlinks(struct net_device *master, struct net_device *slave);
  346. int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev);
  347. int bond_release(struct net_device *bond_dev, struct net_device *slave_dev);
  348. void bond_mii_monitor(struct work_struct *);
  349. void bond_loadbalance_arp_mon(struct work_struct *);
  350. void bond_activebackup_arp_mon(struct work_struct *);
  351. void bond_set_mode_ops(struct bonding *bond, int mode);
  352. int bond_parse_parm(const char *mode_arg, const struct bond_parm_tbl *tbl);
  353. void bond_select_active_slave(struct bonding *bond);
  354. void bond_change_active_slave(struct bonding *bond, struct slave *new_active);
  355. void bond_register_arp(struct bonding *);
  356. void bond_unregister_arp(struct bonding *);
  357. void bond_create_debugfs(void);
  358. void bond_destroy_debugfs(void);
  359. void bond_debug_register(struct bonding *bond);
  360. void bond_debug_unregister(struct bonding *bond);
  361. void bond_debug_reregister(struct bonding *bond);
  362. struct bond_net {
  363. struct net * net; /* Associated network namespace */
  364. struct list_head dev_list;
  365. #ifdef CONFIG_PROC_FS
  366. struct proc_dir_entry * proc_dir;
  367. #endif
  368. };
  369. /* exported from bond_main.c */
  370. extern int bond_net_id;
  371. extern const struct bond_parm_tbl bond_lacp_tbl[];
  372. extern const struct bond_parm_tbl bond_mode_tbl[];
  373. extern const struct bond_parm_tbl xmit_hashtype_tbl[];
  374. extern const struct bond_parm_tbl arp_validate_tbl[];
  375. extern const struct bond_parm_tbl fail_over_mac_tbl[];
  376. extern const struct bond_parm_tbl pri_reselect_tbl[];
  377. extern struct bond_parm_tbl ad_select_tbl[];
  378. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  379. void bond_send_unsolicited_na(struct bonding *bond);
  380. void bond_register_ipv6_notifier(void);
  381. void bond_unregister_ipv6_notifier(void);
  382. #else
  383. static inline void bond_send_unsolicited_na(struct bonding *bond)
  384. {
  385. return;
  386. }
  387. static inline void bond_register_ipv6_notifier(void)
  388. {
  389. return;
  390. }
  391. static inline void bond_unregister_ipv6_notifier(void)
  392. {
  393. return;
  394. }
  395. #endif
  396. #endif /* _LINUX_BONDING_H */