bonding.h 14 KB

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