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.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 packet_type arp_mon_pt;
  219. struct workqueue_struct *wq;
  220. struct delayed_work mii_work;
  221. struct delayed_work arp_work;
  222. struct delayed_work alb_work;
  223. struct delayed_work ad_work;
  224. struct delayed_work mcast_work;
  225. #ifdef CONFIG_DEBUG_FS
  226. /* debugging suport via debugfs */
  227. struct dentry *debug_dir;
  228. #endif /* CONFIG_DEBUG_FS */
  229. };
  230. #define bond_slave_get_rcu(dev) \
  231. ((struct slave *) rcu_dereference(dev->rx_handler_data))
  232. /**
  233. * Returns NULL if the net_device does not belong to any of the bond's slaves
  234. *
  235. * Caller must hold bond lock for read
  236. */
  237. static inline struct slave *bond_get_slave_by_dev(struct bonding *bond,
  238. struct net_device *slave_dev)
  239. {
  240. struct slave *slave = NULL;
  241. int i;
  242. bond_for_each_slave(bond, slave, i) {
  243. if (slave->dev == slave_dev) {
  244. return slave;
  245. }
  246. }
  247. return NULL;
  248. }
  249. static inline struct bonding *bond_get_bond_by_slave(struct slave *slave)
  250. {
  251. if (!slave || !slave->dev->master) {
  252. return NULL;
  253. }
  254. return netdev_priv(slave->dev->master);
  255. }
  256. static inline bool bond_is_lb(const struct bonding *bond)
  257. {
  258. return (bond->params.mode == BOND_MODE_TLB ||
  259. bond->params.mode == BOND_MODE_ALB);
  260. }
  261. static inline void bond_set_active_slave(struct slave *slave)
  262. {
  263. slave->backup = 0;
  264. }
  265. static inline void bond_set_backup_slave(struct slave *slave)
  266. {
  267. slave->backup = 1;
  268. }
  269. static inline int bond_slave_state(struct slave *slave)
  270. {
  271. return slave->backup;
  272. }
  273. static inline bool bond_is_active_slave(struct slave *slave)
  274. {
  275. return !bond_slave_state(slave);
  276. }
  277. #define BOND_PRI_RESELECT_ALWAYS 0
  278. #define BOND_PRI_RESELECT_BETTER 1
  279. #define BOND_PRI_RESELECT_FAILURE 2
  280. #define BOND_FOM_NONE 0
  281. #define BOND_FOM_ACTIVE 1
  282. #define BOND_FOM_FOLLOW 2
  283. #define BOND_ARP_VALIDATE_NONE 0
  284. #define BOND_ARP_VALIDATE_ACTIVE (1 << BOND_STATE_ACTIVE)
  285. #define BOND_ARP_VALIDATE_BACKUP (1 << BOND_STATE_BACKUP)
  286. #define BOND_ARP_VALIDATE_ALL (BOND_ARP_VALIDATE_ACTIVE | \
  287. BOND_ARP_VALIDATE_BACKUP)
  288. static inline int slave_do_arp_validate(struct bonding *bond,
  289. struct slave *slave)
  290. {
  291. return bond->params.arp_validate & (1 << bond_slave_state(slave));
  292. }
  293. static inline unsigned long slave_last_rx(struct bonding *bond,
  294. struct slave *slave)
  295. {
  296. if (slave_do_arp_validate(bond, slave))
  297. return slave->last_arp_rx;
  298. return slave->dev->last_rx;
  299. }
  300. #ifdef CONFIG_NET_POLL_CONTROLLER
  301. static inline void bond_netpoll_send_skb(const struct slave *slave,
  302. struct sk_buff *skb)
  303. {
  304. struct netpoll *np = slave->np;
  305. if (np)
  306. netpoll_send_skb(np, skb);
  307. }
  308. #else
  309. static inline void bond_netpoll_send_skb(const struct slave *slave,
  310. struct sk_buff *skb)
  311. {
  312. }
  313. #endif
  314. static inline void bond_set_slave_inactive_flags(struct slave *slave)
  315. {
  316. struct bonding *bond = netdev_priv(slave->dev->master);
  317. if (!bond_is_lb(bond))
  318. bond_set_backup_slave(slave);
  319. if (!bond->params.all_slaves_active)
  320. slave->inactive = 1;
  321. }
  322. static inline void bond_set_slave_active_flags(struct slave *slave)
  323. {
  324. bond_set_active_slave(slave);
  325. slave->inactive = 0;
  326. }
  327. static inline bool bond_is_slave_inactive(struct slave *slave)
  328. {
  329. return slave->inactive;
  330. }
  331. struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr);
  332. int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb, struct net_device *slave_dev);
  333. int bond_create(struct net *net, const char *name);
  334. int bond_create_sysfs(void);
  335. void bond_destroy_sysfs(void);
  336. void bond_prepare_sysfs_group(struct bonding *bond);
  337. int bond_create_slave_symlinks(struct net_device *master, struct net_device *slave);
  338. void bond_destroy_slave_symlinks(struct net_device *master, struct net_device *slave);
  339. int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev);
  340. int bond_release(struct net_device *bond_dev, struct net_device *slave_dev);
  341. void bond_mii_monitor(struct work_struct *);
  342. void bond_loadbalance_arp_mon(struct work_struct *);
  343. void bond_activebackup_arp_mon(struct work_struct *);
  344. void bond_set_mode_ops(struct bonding *bond, int mode);
  345. int bond_parse_parm(const char *mode_arg, const struct bond_parm_tbl *tbl);
  346. void bond_select_active_slave(struct bonding *bond);
  347. void bond_change_active_slave(struct bonding *bond, struct slave *new_active);
  348. void bond_create_debugfs(void);
  349. void bond_destroy_debugfs(void);
  350. void bond_debug_register(struct bonding *bond);
  351. void bond_debug_unregister(struct bonding *bond);
  352. void bond_debug_reregister(struct bonding *bond);
  353. const char *bond_mode_name(int mode);
  354. struct bond_net {
  355. struct net * net; /* Associated network namespace */
  356. struct list_head dev_list;
  357. #ifdef CONFIG_PROC_FS
  358. struct proc_dir_entry * proc_dir;
  359. #endif
  360. };
  361. #ifdef CONFIG_PROC_FS
  362. void bond_create_proc_entry(struct bonding *bond);
  363. void bond_remove_proc_entry(struct bonding *bond);
  364. void bond_create_proc_dir(struct bond_net *bn);
  365. void bond_destroy_proc_dir(struct bond_net *bn);
  366. #else
  367. static inline void bond_create_proc_entry(struct bonding *bond)
  368. {
  369. }
  370. static inline void bond_remove_proc_entry(struct bonding *bond)
  371. {
  372. }
  373. static inline void bond_create_proc_dir(struct bond_net *bn)
  374. {
  375. }
  376. static inline void bond_destroy_proc_dir(struct bond_net *bn)
  377. {
  378. }
  379. #endif
  380. /* exported from bond_main.c */
  381. extern int bond_net_id;
  382. extern const struct bond_parm_tbl bond_lacp_tbl[];
  383. extern const struct bond_parm_tbl bond_mode_tbl[];
  384. extern const struct bond_parm_tbl xmit_hashtype_tbl[];
  385. extern const struct bond_parm_tbl arp_validate_tbl[];
  386. extern const struct bond_parm_tbl fail_over_mac_tbl[];
  387. extern const struct bond_parm_tbl pri_reselect_tbl[];
  388. extern struct bond_parm_tbl ad_select_tbl[];
  389. #endif /* _LINUX_BONDING_H */