bonding.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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/kobject.h>
  20. #include "bond_3ad.h"
  21. #include "bond_alb.h"
  22. #define DRV_VERSION "3.2.3"
  23. #define DRV_RELDATE "December 6, 2007"
  24. #define DRV_NAME "bonding"
  25. #define DRV_DESCRIPTION "Ethernet Channel Bonding Driver"
  26. #define BOND_MAX_ARP_TARGETS 16
  27. #ifdef BONDING_DEBUG
  28. #define dprintk(fmt, args...) \
  29. printk(KERN_DEBUG \
  30. DRV_NAME ": %s() %d: " fmt, __FUNCTION__, __LINE__ , ## args )
  31. #else
  32. #define dprintk(fmt, args...)
  33. #endif /* BONDING_DEBUG */
  34. #define IS_UP(dev) \
  35. ((((dev)->flags & IFF_UP) == IFF_UP) && \
  36. netif_running(dev) && \
  37. netif_carrier_ok(dev))
  38. /*
  39. * Checks whether bond is ready for transmit.
  40. *
  41. * Caller must hold bond->lock
  42. */
  43. #define BOND_IS_OK(bond) \
  44. (((bond)->dev->flags & IFF_UP) && \
  45. netif_running((bond)->dev) && \
  46. ((bond)->slave_cnt > 0))
  47. /*
  48. * Checks whether slave is ready for transmit.
  49. */
  50. #define SLAVE_IS_OK(slave) \
  51. (((slave)->dev->flags & IFF_UP) && \
  52. netif_running((slave)->dev) && \
  53. ((slave)->link == BOND_LINK_UP) && \
  54. ((slave)->state == BOND_STATE_ACTIVE))
  55. #define USES_PRIMARY(mode) \
  56. (((mode) == BOND_MODE_ACTIVEBACKUP) || \
  57. ((mode) == BOND_MODE_TLB) || \
  58. ((mode) == BOND_MODE_ALB))
  59. /*
  60. * Less bad way to call ioctl from within the kernel; this needs to be
  61. * done some other way to get the call out of interrupt context.
  62. * Needs "ioctl" variable to be supplied by calling context.
  63. */
  64. #define IOCTL(dev, arg, cmd) ({ \
  65. int res = 0; \
  66. mm_segment_t fs = get_fs(); \
  67. set_fs(get_ds()); \
  68. res = ioctl(dev, arg, cmd); \
  69. set_fs(fs); \
  70. res; })
  71. /**
  72. * bond_for_each_slave_from - iterate the slaves list from a starting point
  73. * @bond: the bond holding this list.
  74. * @pos: current slave.
  75. * @cnt: counter for max number of moves
  76. * @start: starting point.
  77. *
  78. * Caller must hold bond->lock
  79. */
  80. #define bond_for_each_slave_from(bond, pos, cnt, start) \
  81. for (cnt = 0, pos = start; \
  82. cnt < (bond)->slave_cnt; \
  83. cnt++, pos = (pos)->next)
  84. /**
  85. * bond_for_each_slave_from_to - iterate the slaves list from start point to stop point
  86. * @bond: the bond holding this list.
  87. * @pos: current slave.
  88. * @cnt: counter for number max of moves
  89. * @start: start point.
  90. * @stop: stop point.
  91. *
  92. * Caller must hold bond->lock
  93. */
  94. #define bond_for_each_slave_from_to(bond, pos, cnt, start, stop) \
  95. for (cnt = 0, pos = start; \
  96. ((cnt < (bond)->slave_cnt) && (pos != (stop)->next)); \
  97. cnt++, pos = (pos)->next)
  98. /**
  99. * bond_for_each_slave - iterate the slaves list from head
  100. * @bond: the bond holding this list.
  101. * @pos: current slave.
  102. * @cnt: counter for max number of moves
  103. *
  104. * Caller must hold bond->lock
  105. */
  106. #define bond_for_each_slave(bond, pos, cnt) \
  107. bond_for_each_slave_from(bond, pos, cnt, (bond)->first_slave)
  108. struct bond_params {
  109. int mode;
  110. int xmit_policy;
  111. int miimon;
  112. int arp_interval;
  113. int arp_validate;
  114. int use_carrier;
  115. int fail_over_mac;
  116. int updelay;
  117. int downdelay;
  118. int lacp_fast;
  119. char primary[IFNAMSIZ];
  120. __be32 arp_targets[BOND_MAX_ARP_TARGETS];
  121. };
  122. struct bond_parm_tbl {
  123. char *modename;
  124. int mode;
  125. };
  126. #define BOND_MAX_MODENAME_LEN 20
  127. struct vlan_entry {
  128. struct list_head vlan_list;
  129. __be32 vlan_ip;
  130. unsigned short vlan_id;
  131. };
  132. struct slave {
  133. struct net_device *dev; /* first - useful for panic debug */
  134. struct slave *next;
  135. struct slave *prev;
  136. int delay;
  137. unsigned long jiffies;
  138. unsigned long last_arp_rx;
  139. s8 link; /* one of BOND_LINK_XXXX */
  140. s8 state; /* one of BOND_STATE_XXXX */
  141. u32 original_flags;
  142. u32 original_mtu;
  143. u32 link_failure_count;
  144. u16 speed;
  145. u8 duplex;
  146. u8 perm_hwaddr[ETH_ALEN];
  147. struct ad_slave_info ad_info; /* HUGE - better to dynamically alloc */
  148. struct tlb_slave_info tlb_info;
  149. };
  150. /*
  151. * Here are the locking policies for the two bonding locks:
  152. *
  153. * 1) Get bond->lock when reading/writing slave list.
  154. * 2) Get bond->curr_slave_lock when reading/writing bond->curr_active_slave.
  155. * (It is unnecessary when the write-lock is put with bond->lock.)
  156. * 3) When we lock with bond->curr_slave_lock, we must lock with bond->lock
  157. * beforehand.
  158. */
  159. struct bonding {
  160. struct net_device *dev; /* first - useful for panic debug */
  161. struct slave *first_slave;
  162. struct slave *curr_active_slave;
  163. struct slave *current_arp_slave;
  164. struct slave *primary_slave;
  165. s32 slave_cnt; /* never change this value outside the attach/detach wrappers */
  166. rwlock_t lock;
  167. rwlock_t curr_slave_lock;
  168. s8 kill_timers;
  169. s8 send_grat_arp;
  170. s8 setup_by_slave;
  171. struct net_device_stats stats;
  172. #ifdef CONFIG_PROC_FS
  173. struct proc_dir_entry *proc_entry;
  174. char proc_file_name[IFNAMSIZ];
  175. #endif /* CONFIG_PROC_FS */
  176. struct list_head bond_list;
  177. struct dev_mc_list *mc_list;
  178. int (*xmit_hash_policy)(struct sk_buff *, struct net_device *, int);
  179. __be32 master_ip;
  180. u16 flags;
  181. u16 rr_tx_counter;
  182. struct ad_bond_info ad_info;
  183. struct alb_bond_info alb_info;
  184. struct bond_params params;
  185. struct list_head vlan_list;
  186. struct vlan_group *vlgrp;
  187. struct packet_type arp_mon_pt;
  188. struct workqueue_struct *wq;
  189. struct delayed_work mii_work;
  190. struct delayed_work arp_work;
  191. struct delayed_work alb_work;
  192. struct delayed_work ad_work;
  193. };
  194. /**
  195. * Returns NULL if the net_device does not belong to any of the bond's slaves
  196. *
  197. * Caller must hold bond lock for read
  198. */
  199. static inline struct slave *bond_get_slave_by_dev(struct bonding *bond, struct net_device *slave_dev)
  200. {
  201. struct slave *slave = NULL;
  202. int i;
  203. bond_for_each_slave(bond, slave, i) {
  204. if (slave->dev == slave_dev) {
  205. break;
  206. }
  207. }
  208. return slave;
  209. }
  210. static inline struct bonding *bond_get_bond_by_slave(struct slave *slave)
  211. {
  212. if (!slave || !slave->dev->master) {
  213. return NULL;
  214. }
  215. return (struct bonding *)slave->dev->master->priv;
  216. }
  217. #define BOND_ARP_VALIDATE_NONE 0
  218. #define BOND_ARP_VALIDATE_ACTIVE (1 << BOND_STATE_ACTIVE)
  219. #define BOND_ARP_VALIDATE_BACKUP (1 << BOND_STATE_BACKUP)
  220. #define BOND_ARP_VALIDATE_ALL (BOND_ARP_VALIDATE_ACTIVE | \
  221. BOND_ARP_VALIDATE_BACKUP)
  222. static inline int slave_do_arp_validate(struct bonding *bond,
  223. struct slave *slave)
  224. {
  225. return bond->params.arp_validate & (1 << slave->state);
  226. }
  227. static inline unsigned long slave_last_rx(struct bonding *bond,
  228. struct slave *slave)
  229. {
  230. if (slave_do_arp_validate(bond, slave))
  231. return slave->last_arp_rx;
  232. return slave->dev->last_rx;
  233. }
  234. static inline void bond_set_slave_inactive_flags(struct slave *slave)
  235. {
  236. struct bonding *bond = slave->dev->master->priv;
  237. if (bond->params.mode != BOND_MODE_TLB &&
  238. bond->params.mode != BOND_MODE_ALB)
  239. slave->state = BOND_STATE_BACKUP;
  240. slave->dev->priv_flags |= IFF_SLAVE_INACTIVE;
  241. if (slave_do_arp_validate(bond, slave))
  242. slave->dev->priv_flags |= IFF_SLAVE_NEEDARP;
  243. }
  244. static inline void bond_set_slave_active_flags(struct slave *slave)
  245. {
  246. slave->state = BOND_STATE_ACTIVE;
  247. slave->dev->priv_flags &= ~(IFF_SLAVE_INACTIVE | IFF_SLAVE_NEEDARP);
  248. }
  249. static inline void bond_set_master_3ad_flags(struct bonding *bond)
  250. {
  251. bond->dev->priv_flags |= IFF_MASTER_8023AD;
  252. }
  253. static inline void bond_unset_master_3ad_flags(struct bonding *bond)
  254. {
  255. bond->dev->priv_flags &= ~IFF_MASTER_8023AD;
  256. }
  257. static inline void bond_set_master_alb_flags(struct bonding *bond)
  258. {
  259. bond->dev->priv_flags |= IFF_MASTER_ALB;
  260. }
  261. static inline void bond_unset_master_alb_flags(struct bonding *bond)
  262. {
  263. bond->dev->priv_flags &= ~IFF_MASTER_ALB;
  264. }
  265. struct vlan_entry *bond_next_vlan(struct bonding *bond, struct vlan_entry *curr);
  266. int bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb, struct net_device *slave_dev);
  267. int bond_create(char *name, struct bond_params *params, struct bonding **newbond);
  268. void bond_destroy(struct bonding *bond);
  269. int bond_release_and_destroy(struct net_device *bond_dev, struct net_device *slave_dev);
  270. int bond_create_sysfs(void);
  271. void bond_destroy_sysfs(void);
  272. void bond_destroy_sysfs_entry(struct bonding *bond);
  273. int bond_create_sysfs_entry(struct bonding *bond);
  274. int bond_create_slave_symlinks(struct net_device *master, struct net_device *slave);
  275. void bond_destroy_slave_symlinks(struct net_device *master, struct net_device *slave);
  276. int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev);
  277. int bond_release(struct net_device *bond_dev, struct net_device *slave_dev);
  278. void bond_mii_monitor(struct work_struct *);
  279. void bond_loadbalance_arp_mon(struct work_struct *);
  280. void bond_activebackup_arp_mon(struct work_struct *);
  281. void bond_set_mode_ops(struct bonding *bond, int mode);
  282. int bond_parse_parm(const char *mode_arg, struct bond_parm_tbl *tbl);
  283. void bond_select_active_slave(struct bonding *bond);
  284. void bond_change_active_slave(struct bonding *bond, struct slave *new_active);
  285. void bond_register_arp(struct bonding *);
  286. void bond_unregister_arp(struct bonding *);
  287. #endif /* _LINUX_BONDING_H */