bonding.h 15 KB

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