bonding.h 12 KB

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