bonding.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. bond_is_active_slave(slave))
  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. u8 backup:1, /* indicates backup slave. Value corresponds with
  172. BOND_STATE_ACTIVE and BOND_STATE_BACKUP */
  173. inactive:1; /* indicates inactive slave */
  174. u32 original_mtu;
  175. u32 link_failure_count;
  176. u8 perm_hwaddr[ETH_ALEN];
  177. u16 speed;
  178. u8 duplex;
  179. u16 queue_id;
  180. struct ad_slave_info ad_info; /* HUGE - better to dynamically alloc */
  181. struct tlb_slave_info tlb_info;
  182. #ifdef CONFIG_NET_POLL_CONTROLLER
  183. struct netpoll *np;
  184. #endif
  185. };
  186. /*
  187. * Link pseudo-state only used internally by monitors
  188. */
  189. #define BOND_LINK_NOCHANGE -1
  190. /*
  191. * Here are the locking policies for the two bonding locks:
  192. *
  193. * 1) Get bond->lock when reading/writing slave list.
  194. * 2) Get bond->curr_slave_lock when reading/writing bond->curr_active_slave.
  195. * (It is unnecessary when the write-lock is put with bond->lock.)
  196. * 3) When we lock with bond->curr_slave_lock, we must lock with bond->lock
  197. * beforehand.
  198. */
  199. struct bonding {
  200. struct net_device *dev; /* first - useful for panic debug */
  201. struct slave *first_slave;
  202. struct slave *curr_active_slave;
  203. struct slave *current_arp_slave;
  204. struct slave *primary_slave;
  205. bool force_primary;
  206. s32 slave_cnt; /* never change this value outside the attach/detach wrappers */
  207. rwlock_t lock;
  208. rwlock_t curr_slave_lock;
  209. s8 kill_timers;
  210. s8 send_grat_arp;
  211. s8 send_unsol_na;
  212. s8 setup_by_slave;
  213. s8 igmp_retrans;
  214. #ifdef CONFIG_PROC_FS
  215. struct proc_dir_entry *proc_entry;
  216. char proc_file_name[IFNAMSIZ];
  217. #endif /* CONFIG_PROC_FS */
  218. struct list_head bond_list;
  219. struct netdev_hw_addr_list mc_list;
  220. int (*xmit_hash_policy)(struct sk_buff *, int);
  221. __be32 master_ip;
  222. u16 flags;
  223. u16 rr_tx_counter;
  224. struct ad_bond_info ad_info;
  225. struct alb_bond_info alb_info;
  226. struct bond_params params;
  227. struct list_head vlan_list;
  228. struct vlan_group *vlgrp;
  229. struct packet_type arp_mon_pt;
  230. struct workqueue_struct *wq;
  231. struct delayed_work mii_work;
  232. struct delayed_work arp_work;
  233. struct delayed_work alb_work;
  234. struct delayed_work ad_work;
  235. struct delayed_work mcast_work;
  236. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  237. struct in6_addr master_ipv6;
  238. #endif
  239. #ifdef CONFIG_DEBUG_FS
  240. /* debugging suport via debugfs */
  241. struct dentry *debug_dir;
  242. #endif /* CONFIG_DEBUG_FS */
  243. };
  244. #define bond_slave_get_rcu(dev) \
  245. ((struct slave *) rcu_dereference(dev->rx_handler_data))
  246. /**
  247. * Returns NULL if the net_device does not belong to any of the bond's slaves
  248. *
  249. * Caller must hold bond lock for read
  250. */
  251. static inline struct slave *bond_get_slave_by_dev(struct bonding *bond,
  252. struct net_device *slave_dev)
  253. {
  254. struct slave *slave = NULL;
  255. int i;
  256. bond_for_each_slave(bond, slave, i) {
  257. if (slave->dev == slave_dev) {
  258. return slave;
  259. }
  260. }
  261. return NULL;
  262. }
  263. static inline struct bonding *bond_get_bond_by_slave(struct slave *slave)
  264. {
  265. if (!slave || !slave->dev->master) {
  266. return NULL;
  267. }
  268. return netdev_priv(slave->dev->master);
  269. }
  270. static inline bool bond_is_lb(const struct bonding *bond)
  271. {
  272. return (bond->params.mode == BOND_MODE_TLB ||
  273. bond->params.mode == BOND_MODE_ALB);
  274. }
  275. static inline void bond_set_active_slave(struct slave *slave)
  276. {
  277. slave->backup = 0;
  278. }
  279. static inline void bond_set_backup_slave(struct slave *slave)
  280. {
  281. slave->backup = 1;
  282. }
  283. static inline int bond_slave_state(struct slave *slave)
  284. {
  285. return slave->backup;
  286. }
  287. static inline bool bond_is_active_slave(struct slave *slave)
  288. {
  289. return !bond_slave_state(slave);
  290. }
  291. #define BOND_PRI_RESELECT_ALWAYS 0
  292. #define BOND_PRI_RESELECT_BETTER 1
  293. #define BOND_PRI_RESELECT_FAILURE 2
  294. #define BOND_FOM_NONE 0
  295. #define BOND_FOM_ACTIVE 1
  296. #define BOND_FOM_FOLLOW 2
  297. #define BOND_ARP_VALIDATE_NONE 0
  298. #define BOND_ARP_VALIDATE_ACTIVE (1 << BOND_STATE_ACTIVE)
  299. #define BOND_ARP_VALIDATE_BACKUP (1 << BOND_STATE_BACKUP)
  300. #define BOND_ARP_VALIDATE_ALL (BOND_ARP_VALIDATE_ACTIVE | \
  301. BOND_ARP_VALIDATE_BACKUP)
  302. static inline int slave_do_arp_validate(struct bonding *bond,
  303. struct slave *slave)
  304. {
  305. return bond->params.arp_validate & (1 << bond_slave_state(slave));
  306. }
  307. static inline unsigned long slave_last_rx(struct bonding *bond,
  308. struct slave *slave)
  309. {
  310. if (slave_do_arp_validate(bond, slave))
  311. return slave->last_arp_rx;
  312. return slave->dev->last_rx;
  313. }
  314. #ifdef CONFIG_NET_POLL_CONTROLLER
  315. static inline void bond_netpoll_send_skb(const struct slave *slave,
  316. struct sk_buff *skb)
  317. {
  318. struct netpoll *np = slave->np;
  319. if (np)
  320. netpoll_send_skb(np, skb);
  321. }
  322. #else
  323. static inline void bond_netpoll_send_skb(const struct slave *slave,
  324. struct sk_buff *skb)
  325. {
  326. }
  327. #endif
  328. static inline void bond_set_slave_inactive_flags(struct slave *slave)
  329. {
  330. struct bonding *bond = netdev_priv(slave->dev->master);
  331. if (!bond_is_lb(bond))
  332. bond_set_backup_slave(slave);
  333. if (!bond->params.all_slaves_active)
  334. slave->inactive = 1;
  335. }
  336. static inline void bond_set_slave_active_flags(struct slave *slave)
  337. {
  338. bond_set_active_slave(slave);
  339. slave->inactive = 0;
  340. }
  341. static inline bool bond_is_slave_inactive(struct slave *slave)
  342. {
  343. return slave->inactive;
  344. }
  345. struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr);
  346. int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb, struct net_device *slave_dev);
  347. int bond_create(struct net *net, const char *name);
  348. int bond_create_sysfs(void);
  349. void bond_destroy_sysfs(void);
  350. void bond_prepare_sysfs_group(struct bonding *bond);
  351. int bond_create_slave_symlinks(struct net_device *master, struct net_device *slave);
  352. void bond_destroy_slave_symlinks(struct net_device *master, struct net_device *slave);
  353. int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev);
  354. int bond_release(struct net_device *bond_dev, struct net_device *slave_dev);
  355. void bond_mii_monitor(struct work_struct *);
  356. void bond_loadbalance_arp_mon(struct work_struct *);
  357. void bond_activebackup_arp_mon(struct work_struct *);
  358. void bond_set_mode_ops(struct bonding *bond, int mode);
  359. int bond_parse_parm(const char *mode_arg, const struct bond_parm_tbl *tbl);
  360. void bond_select_active_slave(struct bonding *bond);
  361. void bond_change_active_slave(struct bonding *bond, struct slave *new_active);
  362. void bond_register_arp(struct bonding *);
  363. void bond_unregister_arp(struct bonding *);
  364. void bond_create_debugfs(void);
  365. void bond_destroy_debugfs(void);
  366. void bond_debug_register(struct bonding *bond);
  367. void bond_debug_unregister(struct bonding *bond);
  368. void bond_debug_reregister(struct bonding *bond);
  369. struct bond_net {
  370. struct net * net; /* Associated network namespace */
  371. struct list_head dev_list;
  372. #ifdef CONFIG_PROC_FS
  373. struct proc_dir_entry * proc_dir;
  374. #endif
  375. };
  376. #ifdef CONFIG_PROC_FS
  377. void bond_create_proc_entry(struct bonding *bond);
  378. void bond_remove_proc_entry(struct bonding *bond);
  379. void bond_create_proc_dir(struct bond_net *bn);
  380. void bond_destroy_proc_dir(struct bond_net *bn);
  381. #else
  382. static inline void bond_create_proc_entry(struct bonding *bond)
  383. {
  384. }
  385. static inline void bond_remove_proc_entry(struct bonding *bond)
  386. {
  387. }
  388. static inline void bond_create_proc_dir(struct bond_net *bn)
  389. {
  390. }
  391. static inline void bond_destroy_proc_dir(struct bond_net *bn)
  392. {
  393. }
  394. #endif
  395. /* exported from bond_main.c */
  396. extern int bond_net_id;
  397. extern const struct bond_parm_tbl bond_lacp_tbl[];
  398. extern const struct bond_parm_tbl bond_mode_tbl[];
  399. extern const struct bond_parm_tbl xmit_hashtype_tbl[];
  400. extern const struct bond_parm_tbl arp_validate_tbl[];
  401. extern const struct bond_parm_tbl fail_over_mac_tbl[];
  402. extern const struct bond_parm_tbl pri_reselect_tbl[];
  403. extern struct bond_parm_tbl ad_select_tbl[];
  404. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  405. void bond_send_unsolicited_na(struct bonding *bond);
  406. void bond_register_ipv6_notifier(void);
  407. void bond_unregister_ipv6_notifier(void);
  408. #else
  409. static inline void bond_send_unsolicited_na(struct bonding *bond)
  410. {
  411. return;
  412. }
  413. static inline void bond_register_ipv6_notifier(void)
  414. {
  415. return;
  416. }
  417. static inline void bond_unregister_ipv6_notifier(void)
  418. {
  419. return;
  420. }
  421. #endif
  422. #endif /* _LINUX_BONDING_H */