bonding.h 16 KB

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