bonding.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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 <linux/inetdevice.h>
  23. #include <linux/etherdevice.h>
  24. #include "bond_3ad.h"
  25. #include "bond_alb.h"
  26. #define DRV_VERSION "3.7.1"
  27. #define DRV_RELDATE "April 27, 2011"
  28. #define DRV_NAME "bonding"
  29. #define DRV_DESCRIPTION "Ethernet Channel Bonding Driver"
  30. #define bond_version DRV_DESCRIPTION ": v" DRV_VERSION " (" DRV_RELDATE ")\n"
  31. #define BOND_MAX_ARP_TARGETS 16
  32. #define IS_UP(dev) \
  33. ((((dev)->flags & IFF_UP) == IFF_UP) && \
  34. netif_running(dev) && \
  35. netif_carrier_ok(dev))
  36. /*
  37. * Checks whether slave is ready for transmit.
  38. */
  39. #define SLAVE_IS_OK(slave) \
  40. (((slave)->dev->flags & IFF_UP) && \
  41. netif_running((slave)->dev) && \
  42. ((slave)->link == BOND_LINK_UP) && \
  43. bond_is_active_slave(slave))
  44. #define USES_PRIMARY(mode) \
  45. (((mode) == BOND_MODE_ACTIVEBACKUP) || \
  46. ((mode) == BOND_MODE_TLB) || \
  47. ((mode) == BOND_MODE_ALB))
  48. #define TX_QUEUE_OVERRIDE(mode) \
  49. (((mode) == BOND_MODE_ACTIVEBACKUP) || \
  50. ((mode) == BOND_MODE_ROUNDROBIN))
  51. /*
  52. * Less bad way to call ioctl from within the kernel; this needs to be
  53. * done some other way to get the call out of interrupt context.
  54. * Needs "ioctl" variable to be supplied by calling context.
  55. */
  56. #define IOCTL(dev, arg, cmd) ({ \
  57. int res = 0; \
  58. mm_segment_t fs = get_fs(); \
  59. set_fs(get_ds()); \
  60. res = ioctl(dev, arg, cmd); \
  61. set_fs(fs); \
  62. res; })
  63. /* slave list primitives */
  64. #define bond_slave_list(bond) (&(bond)->dev->adj_list.lower)
  65. #define bond_has_slaves(bond) !list_empty(bond_slave_list(bond))
  66. /* IMPORTANT: bond_first/last_slave can return NULL in case of an empty list */
  67. #define bond_first_slave(bond) \
  68. (bond_has_slaves(bond) ? \
  69. netdev_adjacent_get_private(bond_slave_list(bond)->next) : \
  70. NULL)
  71. #define bond_last_slave(bond) \
  72. (bond_has_slaves(bond) ? \
  73. netdev_adjacent_get_private(bond_slave_list(bond)->prev) : \
  74. NULL)
  75. #define bond_is_first_slave(bond, pos) (pos == bond_first_slave(bond))
  76. #define bond_is_last_slave(bond, pos) (pos == bond_last_slave(bond))
  77. /**
  78. * bond_for_each_slave - iterate over all slaves
  79. * @bond: the bond holding this list
  80. * @pos: current slave
  81. * @iter: list_head * iterator
  82. *
  83. * Caller must hold bond->lock
  84. */
  85. #define bond_for_each_slave(bond, pos, iter) \
  86. netdev_for_each_lower_private((bond)->dev, pos, iter)
  87. /* Caller must have rcu_read_lock */
  88. #define bond_for_each_slave_rcu(bond, pos, iter) \
  89. netdev_for_each_lower_private_rcu((bond)->dev, pos, iter)
  90. #ifdef CONFIG_NET_POLL_CONTROLLER
  91. extern atomic_t netpoll_block_tx;
  92. static inline void block_netpoll_tx(void)
  93. {
  94. atomic_inc(&netpoll_block_tx);
  95. }
  96. static inline void unblock_netpoll_tx(void)
  97. {
  98. atomic_dec(&netpoll_block_tx);
  99. }
  100. static inline int is_netpoll_tx_blocked(struct net_device *dev)
  101. {
  102. if (unlikely(netpoll_tx_running(dev)))
  103. return atomic_read(&netpoll_block_tx);
  104. return 0;
  105. }
  106. #else
  107. #define block_netpoll_tx()
  108. #define unblock_netpoll_tx()
  109. #define is_netpoll_tx_blocked(dev) (0)
  110. #endif
  111. struct bond_params {
  112. int mode;
  113. int xmit_policy;
  114. int miimon;
  115. u8 num_peer_notif;
  116. int arp_interval;
  117. int arp_validate;
  118. int arp_all_targets;
  119. int use_carrier;
  120. int fail_over_mac;
  121. int updelay;
  122. int downdelay;
  123. int lacp_fast;
  124. unsigned int min_links;
  125. int ad_select;
  126. char primary[IFNAMSIZ];
  127. int primary_reselect;
  128. __be32 arp_targets[BOND_MAX_ARP_TARGETS];
  129. int tx_queues;
  130. int all_slaves_active;
  131. int resend_igmp;
  132. int lp_interval;
  133. };
  134. struct bond_parm_tbl {
  135. char *modename;
  136. int mode;
  137. };
  138. #define BOND_MAX_MODENAME_LEN 20
  139. struct slave {
  140. struct net_device *dev; /* first - useful for panic debug */
  141. struct bonding *bond; /* our master */
  142. int delay;
  143. unsigned long jiffies;
  144. unsigned long last_arp_rx;
  145. unsigned long target_last_arp_rx[BOND_MAX_ARP_TARGETS];
  146. s8 link; /* one of BOND_LINK_XXXX */
  147. s8 new_link;
  148. u8 backup:1, /* indicates backup slave. Value corresponds with
  149. BOND_STATE_ACTIVE and BOND_STATE_BACKUP */
  150. inactive:1; /* indicates inactive slave */
  151. u8 duplex;
  152. u32 original_mtu;
  153. u32 link_failure_count;
  154. u32 speed;
  155. u16 queue_id;
  156. u8 perm_hwaddr[ETH_ALEN];
  157. struct ad_slave_info ad_info; /* HUGE - better to dynamically alloc */
  158. struct tlb_slave_info tlb_info;
  159. #ifdef CONFIG_NET_POLL_CONTROLLER
  160. struct netpoll *np;
  161. #endif
  162. };
  163. /*
  164. * Link pseudo-state only used internally by monitors
  165. */
  166. #define BOND_LINK_NOCHANGE -1
  167. /*
  168. * Here are the locking policies for the two bonding locks:
  169. *
  170. * 1) Get bond->lock when reading/writing slave list.
  171. * 2) Get bond->curr_slave_lock when reading/writing bond->curr_active_slave.
  172. * (It is unnecessary when the write-lock is put with bond->lock.)
  173. * 3) When we lock with bond->curr_slave_lock, we must lock with bond->lock
  174. * beforehand.
  175. */
  176. struct bonding {
  177. struct net_device *dev; /* first - useful for panic debug */
  178. struct slave *curr_active_slave;
  179. struct slave *current_arp_slave;
  180. struct slave *primary_slave;
  181. bool force_primary;
  182. s32 slave_cnt; /* never change this value outside the attach/detach wrappers */
  183. int (*recv_probe)(const struct sk_buff *, struct bonding *,
  184. struct slave *);
  185. rwlock_t lock;
  186. rwlock_t curr_slave_lock;
  187. u8 send_peer_notif;
  188. u8 igmp_retrans;
  189. #ifdef CONFIG_PROC_FS
  190. struct proc_dir_entry *proc_entry;
  191. char proc_file_name[IFNAMSIZ];
  192. #endif /* CONFIG_PROC_FS */
  193. struct list_head bond_list;
  194. u16 rr_tx_counter;
  195. struct ad_bond_info ad_info;
  196. struct alb_bond_info alb_info;
  197. struct bond_params params;
  198. struct workqueue_struct *wq;
  199. struct delayed_work mii_work;
  200. struct delayed_work arp_work;
  201. struct delayed_work alb_work;
  202. struct delayed_work ad_work;
  203. struct delayed_work mcast_work;
  204. #ifdef CONFIG_DEBUG_FS
  205. /* debugging support via debugfs */
  206. struct dentry *debug_dir;
  207. #endif /* CONFIG_DEBUG_FS */
  208. };
  209. #define bond_slave_get_rcu(dev) \
  210. ((struct slave *) rcu_dereference(dev->rx_handler_data))
  211. #define bond_slave_get_rtnl(dev) \
  212. ((struct slave *) rtnl_dereference(dev->rx_handler_data))
  213. /**
  214. * Returns NULL if the net_device does not belong to any of the bond's slaves
  215. *
  216. * Caller must hold bond lock for read
  217. */
  218. static inline struct slave *bond_get_slave_by_dev(struct bonding *bond,
  219. struct net_device *slave_dev)
  220. {
  221. return netdev_lower_dev_get_private(bond->dev, slave_dev);
  222. }
  223. static inline struct bonding *bond_get_bond_by_slave(struct slave *slave)
  224. {
  225. if (!slave || !slave->bond)
  226. return NULL;
  227. return slave->bond;
  228. }
  229. static inline bool bond_is_lb(const struct bonding *bond)
  230. {
  231. return (bond->params.mode == BOND_MODE_TLB ||
  232. bond->params.mode == BOND_MODE_ALB);
  233. }
  234. static inline void bond_set_active_slave(struct slave *slave)
  235. {
  236. slave->backup = 0;
  237. }
  238. static inline void bond_set_backup_slave(struct slave *slave)
  239. {
  240. slave->backup = 1;
  241. }
  242. static inline int bond_slave_state(struct slave *slave)
  243. {
  244. return slave->backup;
  245. }
  246. static inline bool bond_is_active_slave(struct slave *slave)
  247. {
  248. return !bond_slave_state(slave);
  249. }
  250. #define BOND_PRI_RESELECT_ALWAYS 0
  251. #define BOND_PRI_RESELECT_BETTER 1
  252. #define BOND_PRI_RESELECT_FAILURE 2
  253. #define BOND_FOM_NONE 0
  254. #define BOND_FOM_ACTIVE 1
  255. #define BOND_FOM_FOLLOW 2
  256. #define BOND_ARP_TARGETS_ANY 0
  257. #define BOND_ARP_TARGETS_ALL 1
  258. #define BOND_ARP_VALIDATE_NONE 0
  259. #define BOND_ARP_VALIDATE_ACTIVE (1 << BOND_STATE_ACTIVE)
  260. #define BOND_ARP_VALIDATE_BACKUP (1 << BOND_STATE_BACKUP)
  261. #define BOND_ARP_VALIDATE_ALL (BOND_ARP_VALIDATE_ACTIVE | \
  262. BOND_ARP_VALIDATE_BACKUP)
  263. static inline int slave_do_arp_validate(struct bonding *bond,
  264. struct slave *slave)
  265. {
  266. return bond->params.arp_validate & (1 << bond_slave_state(slave));
  267. }
  268. /* Get the oldest arp which we've received on this slave for bond's
  269. * arp_targets.
  270. */
  271. static inline unsigned long slave_oldest_target_arp_rx(struct bonding *bond,
  272. struct slave *slave)
  273. {
  274. int i = 1;
  275. unsigned long ret = slave->target_last_arp_rx[0];
  276. for (; (i < BOND_MAX_ARP_TARGETS) && bond->params.arp_targets[i]; i++)
  277. if (time_before(slave->target_last_arp_rx[i], ret))
  278. ret = slave->target_last_arp_rx[i];
  279. return ret;
  280. }
  281. static inline unsigned long slave_last_rx(struct bonding *bond,
  282. struct slave *slave)
  283. {
  284. if (slave_do_arp_validate(bond, slave)) {
  285. if (bond->params.arp_all_targets == BOND_ARP_TARGETS_ALL)
  286. return slave_oldest_target_arp_rx(bond, slave);
  287. else
  288. return slave->last_arp_rx;
  289. }
  290. return slave->dev->last_rx;
  291. }
  292. #ifdef CONFIG_NET_POLL_CONTROLLER
  293. static inline void bond_netpoll_send_skb(const struct slave *slave,
  294. struct sk_buff *skb)
  295. {
  296. struct netpoll *np = slave->np;
  297. if (np)
  298. netpoll_send_skb(np, skb);
  299. }
  300. #else
  301. static inline void bond_netpoll_send_skb(const struct slave *slave,
  302. struct sk_buff *skb)
  303. {
  304. }
  305. #endif
  306. static inline void bond_set_slave_inactive_flags(struct slave *slave)
  307. {
  308. if (!bond_is_lb(slave->bond))
  309. bond_set_backup_slave(slave);
  310. if (!slave->bond->params.all_slaves_active)
  311. slave->inactive = 1;
  312. }
  313. static inline void bond_set_slave_active_flags(struct slave *slave)
  314. {
  315. bond_set_active_slave(slave);
  316. slave->inactive = 0;
  317. }
  318. static inline bool bond_is_slave_inactive(struct slave *slave)
  319. {
  320. return slave->inactive;
  321. }
  322. static inline __be32 bond_confirm_addr(struct net_device *dev, __be32 dst, __be32 local)
  323. {
  324. struct in_device *in_dev;
  325. __be32 addr = 0;
  326. rcu_read_lock();
  327. in_dev = __in_dev_get_rcu(dev);
  328. if (in_dev)
  329. addr = inet_confirm_addr(in_dev, dst, local, RT_SCOPE_HOST);
  330. rcu_read_unlock();
  331. return addr;
  332. }
  333. static inline bool slave_can_tx(struct slave *slave)
  334. {
  335. if (IS_UP(slave->dev) && slave->link == BOND_LINK_UP &&
  336. bond_is_active_slave(slave))
  337. return true;
  338. else
  339. return false;
  340. }
  341. struct bond_net;
  342. int bond_arp_rcv(const struct sk_buff *skb, struct bonding *bond, struct slave *slave);
  343. int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb, struct net_device *slave_dev);
  344. void bond_xmit_slave_id(struct bonding *bond, struct sk_buff *skb, int slave_id);
  345. int bond_create(struct net *net, const char *name);
  346. int bond_create_sysfs(struct bond_net *net);
  347. void bond_destroy_sysfs(struct bond_net *net);
  348. void bond_prepare_sysfs_group(struct bonding *bond);
  349. int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev);
  350. int bond_release(struct net_device *bond_dev, struct net_device *slave_dev);
  351. void bond_mii_monitor(struct work_struct *);
  352. void bond_loadbalance_arp_mon(struct work_struct *);
  353. void bond_activebackup_arp_mon(struct work_struct *);
  354. int bond_xmit_hash(struct bonding *bond, struct sk_buff *skb, int count);
  355. int bond_parse_parm(const char *mode_arg, const struct bond_parm_tbl *tbl);
  356. void bond_select_active_slave(struct bonding *bond);
  357. void bond_change_active_slave(struct bonding *bond, struct slave *new_active);
  358. void bond_create_debugfs(void);
  359. void bond_destroy_debugfs(void);
  360. void bond_debug_register(struct bonding *bond);
  361. void bond_debug_unregister(struct bonding *bond);
  362. void bond_debug_reregister(struct bonding *bond);
  363. const char *bond_mode_name(int mode);
  364. struct bond_net {
  365. struct net * net; /* Associated network namespace */
  366. struct list_head dev_list;
  367. #ifdef CONFIG_PROC_FS
  368. struct proc_dir_entry * proc_dir;
  369. #endif
  370. struct class_attribute class_attr_bonding_masters;
  371. };
  372. #ifdef CONFIG_PROC_FS
  373. void bond_create_proc_entry(struct bonding *bond);
  374. void bond_remove_proc_entry(struct bonding *bond);
  375. void bond_create_proc_dir(struct bond_net *bn);
  376. void bond_destroy_proc_dir(struct bond_net *bn);
  377. #else
  378. static inline void bond_create_proc_entry(struct bonding *bond)
  379. {
  380. }
  381. static inline void bond_remove_proc_entry(struct bonding *bond)
  382. {
  383. }
  384. static inline void bond_create_proc_dir(struct bond_net *bn)
  385. {
  386. }
  387. static inline void bond_destroy_proc_dir(struct bond_net *bn)
  388. {
  389. }
  390. #endif
  391. static inline struct slave *bond_slave_has_mac(struct bonding *bond,
  392. const u8 *mac)
  393. {
  394. struct list_head *iter;
  395. struct slave *tmp;
  396. bond_for_each_slave(bond, tmp, iter)
  397. if (ether_addr_equal_64bits(mac, tmp->dev->dev_addr))
  398. return tmp;
  399. return NULL;
  400. }
  401. /* Caller must hold rcu_read_lock() for read */
  402. static inline struct slave *bond_slave_has_mac_rcu(struct bonding *bond,
  403. const u8 *mac)
  404. {
  405. struct list_head *iter;
  406. struct slave *tmp;
  407. bond_for_each_slave_rcu(bond, tmp, iter)
  408. if (ether_addr_equal_64bits(mac, tmp->dev->dev_addr))
  409. return tmp;
  410. return NULL;
  411. }
  412. /* Check if the ip is present in arp ip list, or first free slot if ip == 0
  413. * Returns -1 if not found, index if found
  414. */
  415. static inline int bond_get_targets_ip(__be32 *targets, __be32 ip)
  416. {
  417. int i;
  418. for (i = 0; i < BOND_MAX_ARP_TARGETS; i++)
  419. if (targets[i] == ip)
  420. return i;
  421. else if (targets[i] == 0)
  422. break;
  423. return -1;
  424. }
  425. /* exported from bond_main.c */
  426. extern int bond_net_id;
  427. extern const struct bond_parm_tbl bond_lacp_tbl[];
  428. extern const struct bond_parm_tbl bond_mode_tbl[];
  429. extern const struct bond_parm_tbl xmit_hashtype_tbl[];
  430. extern const struct bond_parm_tbl arp_validate_tbl[];
  431. extern const struct bond_parm_tbl arp_all_targets_tbl[];
  432. extern const struct bond_parm_tbl fail_over_mac_tbl[];
  433. extern const struct bond_parm_tbl pri_reselect_tbl[];
  434. extern struct bond_parm_tbl ad_select_tbl[];
  435. #endif /* _LINUX_BONDING_H */