You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

proto_eth_slow.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /* Copyright 2004 Linux Networx */
  2. #ifdef PROTO_LACP
  3. #if 0
  4. #include "etherboot.h"
  5. #include "nic.h"
  6. #include "timer.h"
  7. #endif
  8. #define LACP_DEBUG 0
  9. /* Structure definitions originally taken from the linux bond_3ad driver */
  10. #define SLOW_DST_MAC "\x01\x80\xc2\x00\x00\x02"
  11. static const char slow_dest[] = SLOW_DST_MAC;
  12. #define SLOW_SUBTYPE_LACP 1
  13. #define SLOW_SUBTYPE_MARKER 2
  14. struct slow_header {
  15. uint8_t subtype;
  16. };
  17. struct lacp_info {
  18. uint16_t system_priority;
  19. uint8_t system[ETH_ALEN];
  20. uint16_t key;
  21. uint16_t port_priority;
  22. uint16_t port;
  23. uint8_t state;
  24. uint8_t reserved[3];
  25. } PACKED;
  26. #define LACP_CMP_LEN (2 + 6 + 2 + 2 + 2)
  27. #define LACP_CP_LEN (2 + 6 + 2 + 2 + 2 + 1)
  28. /* Link Aggregation Control Protocol(LACP) data unit structure(43.4.2.2 in the 802.3ad standard) */
  29. struct slow_lacp {
  30. uint8_t subtype; /* = LACP(= 0x01) */
  31. uint8_t version_number;
  32. uint8_t tlv_type_actor_info; /* = actor information(type/length/value) */
  33. #define LACP_TLV_TERMINATOR 0
  34. #define LACP_TLV_ACTOR 1
  35. #define LACP_TLV_PARTNER 2
  36. #define LACP_TLV_COLLECTOR 3
  37. uint8_t actor_information_length; /* = 20 */
  38. struct lacp_info actor;
  39. uint8_t tlv_type_partner_info; /* = partner information */
  40. uint8_t partner_information_length; /* = 20 */
  41. struct lacp_info partner;
  42. uint8_t tlv_type_collector_info; /* = collector information */
  43. uint8_t collector_information_length; /* = 16 */
  44. uint16_t collector_max_delay;
  45. uint8_t reserved_12[12];
  46. uint8_t tlv_type_terminator; /* = terminator */
  47. uint8_t terminator_length; /* = 0 */
  48. uint8_t reserved_50[50]; /* = 0 */
  49. } PACKED;
  50. /* Marker Protocol Data Unit(PDU) structure(43.5.3.2 in the 802.3ad standard) */
  51. struct slow_marker {
  52. uint8_t subtype; /* = 0x02 (marker PDU) */
  53. uint8_t version_number; /* = 0x01 */
  54. uint8_t tlv_type;
  55. #define MARKER_TLV_TERMINATOR 0 /* marker terminator */
  56. #define MARKER_TLV_INFO 1 /* marker information */
  57. #define MARKER_TLV_RESPONSE 2 /* marker response information */
  58. uint8_t marker_length; /* = 0x16 */
  59. uint16_t requester_port; /* The number assigned to the port by the requester */
  60. uint8_t requester_system[ETH_ALEN]; /* The requester's system id */
  61. uint32_t requester_transaction_id; /* The transaction id allocated by the requester, */
  62. uint16_t pad; /* = 0 */
  63. uint8_t tlv_type_terminator; /* = 0x00 */
  64. uint8_t terminator_length; /* = 0x00 */
  65. uint8_t reserved_90[90]; /* = 0 */
  66. } PACKED;
  67. union slow_union {
  68. struct slow_header header;
  69. struct slow_lacp lacp;
  70. struct slow_marker marker;
  71. };
  72. #define FAST_PERIODIC_TIME (1*TICKS_PER_SEC)
  73. #define SLOW_PERIODIC_TIME (30*TICKS_PER_SEC)
  74. #define SHORT_TIMEOUT_TIME (3*FAST_PERIODIC_TIME)
  75. #define LONG_TIMEOUT_TIME (3*SLOW_PERIODIC_TIME)
  76. #define CHURN_DETECTION_TIME (60*TICKS_PER_SEC)
  77. #define AGGREGATE_WAIT_TIME (2*TICKS_PER_SEC)
  78. #define LACP_ACTIVITY (1 << 0)
  79. #define LACP_TIMEOUT (1 << 1)
  80. #define LACP_AGGREGATION (1 << 2)
  81. #define LACP_SYNCHRONIZATION (1 << 3)
  82. #define LACP_COLLECTING (1 << 4)
  83. #define LACP_DISTRIBUTING (1 << 5)
  84. #define LACP_DEFAULTED (1 << 6)
  85. #define LACP_EXPIRED (1 << 7)
  86. #define UNSELECTED 0
  87. #define STANDBY 1
  88. #define SELECTED 2
  89. struct lacp_state {
  90. struct slow_lacp pkt;
  91. unsigned long current_while_timer; /* Time when the LACP information expires */
  92. unsigned long periodic_timer; /* Time when I need to send my partner an update */
  93. };
  94. static struct lacp_state lacp;
  95. #if LACP_DEBUG > 0
  96. static void print_lacp_state(uint8_t state)
  97. {
  98. printf("%hhx", state);
  99. if (state & LACP_ACTIVITY) {
  100. printf(" Activity");
  101. }
  102. if (state & LACP_TIMEOUT) {
  103. printf(" Timeout");
  104. }
  105. if (state & LACP_AGGREGATION) {
  106. printf(" Aggregation");
  107. }
  108. if (state & LACP_SYNCHRONIZATION) {
  109. printf(" Syncronization");
  110. }
  111. if (state & LACP_COLLECTING) {
  112. printf(" Collecting");
  113. }
  114. if (state & LACP_DISTRIBUTING) {
  115. printf(" Distributing");
  116. }
  117. if (state & LACP_DEFAULTED) {
  118. printf(" Defaulted");
  119. }
  120. if (state & LACP_EXPIRED) {
  121. printf(" Expired");
  122. }
  123. printf("\n");
  124. }
  125. static inline void print_lacpdu(struct slow_lacp *pkt)
  126. {
  127. printf("subtype version: %hhx %hhx\n",
  128. pkt->subtype, pkt->version_number);
  129. printf("actor_tlv %hhx", pkt->tlv_type_actor_info);
  130. printf(" len: %hhx (\n", pkt->actor_information_length);
  131. printf(" sys_pri: %hx", ntohs(pkt->actor.system_priority));
  132. printf(" mac: %!", pkt->actor.system);
  133. printf(" key: %hx", ntohs(pkt->actor.key));
  134. printf(" port_pri: %hx", ntohs(pkt->actor.port_priority));
  135. printf(" port: %hx\n", ntohs(pkt->actor.port));
  136. printf(" state: ");
  137. print_lacp_state(pkt->actor.state);
  138. #if LACP_DEBUG > 1
  139. printf(" reserved: %hhx %hhx %hhx\n",
  140. pkt->actor.reserved[0], pkt->actor.reserved[1], pkt->actor.reserved[2]);
  141. #endif
  142. printf(")\n");
  143. printf("partner_tlv: %hhx", pkt->tlv_type_partner_info);
  144. printf(" len: %hhx (\n", pkt->partner_information_length);
  145. printf(" sys_pri: %hx", ntohs(pkt->partner.system_priority));
  146. printf(" mac: %!", pkt->partner.system);
  147. printf(" key: %hx", ntohs(pkt->partner.key));
  148. printf(" port_pri: %hx", ntohs(pkt->partner.port_priority));
  149. printf(" port: %hx\n", ntohs(pkt->partner.port));
  150. printf(" state: ");
  151. print_lacp_state(pkt->partner.state);
  152. #if LACP_DEBUG > 1
  153. printf(" reserved: %hhx %hhx %hhx\n",
  154. pkt->partner.reserved[0], pkt->partner.reserved[1], pkt->partner.reserved[2]);
  155. #endif
  156. printf(")\n");
  157. printf("collector_tlv: %hhx ", pkt->tlv_type_collector_info);
  158. printf(" len: %hhx (", pkt->collector_information_length);
  159. printf(" max_delay: %hx", ntohs(pkt->collector_max_delay));
  160. #if LACP_DEBUG > 1
  161. printf("reserved_12: %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx\n",
  162. pkt->reserved_12[0], pkt->reserved_12[1], pkt->reserved_12[2],
  163. pkt->reserved_12[3], pkt->reserved_12[4], pkt->reserved_12[5],
  164. pkt->reserved_12[6], pkt->reserved_12[7], pkt->reserved_12[8],
  165. pkt->reserved_12[9], pkt->reserved_12[10], pkt->reserved_12[11]);
  166. #endif
  167. printf(" )\n");
  168. printf("terminator_tlv: %hhx", pkt->tlv_type_terminator);
  169. printf(" len: %hhx ()\n", pkt->terminator_length);
  170. }
  171. static inline unsigned long lacp_timer_val(unsigned long now, unsigned long when)
  172. {
  173. return when?(when - now)/TICKS_PER_SEC : 0;
  174. }
  175. static void print_lacp(const char *which, struct slow_lacp *pkt, unsigned long now)
  176. {
  177. printf("%s\n", which);
  178. print_lacpdu(pkt);
  179. printf("timers: c %ds p %ds\n",
  180. lacp_timer_val(now, lacp.current_while_timer),
  181. lacp_timer_val(now, lacp.periodic_timer)
  182. );
  183. printf("\n");
  184. }
  185. #else /* LACP_DEBUG */
  186. #define print_lacp(which, pkt, now) do {} while(0)
  187. #endif /* LACP_DEBUG */
  188. static void lacp_init_state(const uint8_t *mac)
  189. {
  190. memset(&lacp, 0, sizeof(lacp));
  191. /* Initialize the packet constants */
  192. lacp.pkt.subtype = 1;
  193. lacp.pkt.version_number = 1;
  194. /* The default state of my interface */
  195. lacp.pkt.tlv_type_actor_info = LACP_TLV_ACTOR;
  196. lacp.pkt.actor_information_length = 0x14;
  197. lacp.pkt.actor.system_priority = htons(1);
  198. memcpy(lacp.pkt.actor.system, mac, ETH_ALEN);
  199. lacp.pkt.actor.key = htons(1);
  200. lacp.pkt.actor.port = htons(1);
  201. lacp.pkt.actor.port_priority = htons(1);
  202. lacp.pkt.actor.state =
  203. LACP_SYNCHRONIZATION |
  204. LACP_COLLECTING |
  205. LACP_DISTRIBUTING |
  206. LACP_DEFAULTED;
  207. /* Set my partner defaults */
  208. lacp.pkt.tlv_type_partner_info = LACP_TLV_PARTNER;
  209. lacp.pkt.partner_information_length = 0x14;
  210. lacp.pkt.partner.system_priority = htons(1);
  211. /* memset(lacp.pkt.parnter_system, 0, ETH_ALEN); */
  212. lacp.pkt.partner.key = htons(1);
  213. lacp.pkt.partner.port = htons(1);
  214. lacp.pkt.partner.port_priority = htons(1);
  215. lacp.pkt.partner.state =
  216. LACP_ACTIVITY |
  217. LACP_SYNCHRONIZATION |
  218. LACP_COLLECTING |
  219. LACP_DISTRIBUTING |
  220. LACP_DEFAULTED;
  221. lacp.pkt.tlv_type_collector_info = LACP_TLV_COLLECTOR;
  222. lacp.pkt.collector_information_length = 0x10;
  223. lacp.pkt.collector_max_delay = htons(0x8000); /* ???? */
  224. lacp.pkt.tlv_type_terminator = LACP_TLV_TERMINATOR;
  225. lacp.pkt.terminator_length = 0;
  226. }
  227. #define LACP_NTT_MASK (LACP_ACTIVITY | LACP_TIMEOUT | \
  228. LACP_SYNCHRONIZATION | LACP_AGGREGATION)
  229. static inline int lacp_update_ntt(struct slow_lacp *pkt)
  230. {
  231. int ntt = 0;
  232. if ((memcmp(&pkt->partner, &lacp.pkt.actor, LACP_CMP_LEN) != 0) ||
  233. ((pkt->partner.state & LACP_NTT_MASK) !=
  234. (lacp.pkt.actor.state & LACP_NTT_MASK)))
  235. {
  236. ntt = 1;
  237. }
  238. return ntt;
  239. }
  240. static inline void lacp_record_pdu(struct slow_lacp *pkt)
  241. {
  242. memcpy(&lacp.pkt.partner, &pkt->actor, LACP_CP_LEN);
  243. lacp.pkt.actor.state &= ~LACP_DEFAULTED;
  244. lacp.pkt.partner.state &= ~LACP_SYNCHRONIZATION;
  245. if ((memcmp(&pkt->partner, &lacp.pkt.actor, LACP_CMP_LEN) == 0) &&
  246. ((pkt->partner.state & LACP_AGGREGATION) ==
  247. (lacp.pkt.actor.state & LACP_AGGREGATION)))
  248. {
  249. lacp.pkt.partner.state |= LACP_SYNCHRONIZATION;
  250. }
  251. if (!(pkt->actor.state & LACP_AGGREGATION)) {
  252. lacp.pkt.partner.state |= LACP_SYNCHRONIZATION;
  253. }
  254. /* ACTIVITY? */
  255. }
  256. static inline int lacp_timer_expired(unsigned long now, unsigned long when)
  257. {
  258. return when && (now > when);
  259. }
  260. static inline void lacp_start_periodic_timer(unsigned long now)
  261. {
  262. if ((lacp.pkt.partner.state & LACP_ACTIVITY) ||
  263. (lacp.pkt.actor.state & LACP_ACTIVITY)) {
  264. lacp.periodic_timer = now +
  265. (((lacp.pkt.partner.state & LACP_TIMEOUT)?
  266. FAST_PERIODIC_TIME : SLOW_PERIODIC_TIME));
  267. }
  268. }
  269. static inline void lacp_start_current_while_timer(unsigned long now)
  270. {
  271. lacp.current_while_timer = now +
  272. ((lacp.pkt.actor.state & LACP_TIMEOUT) ?
  273. SHORT_TIMEOUT_TIME : LONG_TIMEOUT_TIME);
  274. lacp.pkt.actor.state &= ~LACP_EXPIRED;
  275. }
  276. static void send_lacp_reports(unsigned long now, int ntt)
  277. {
  278. if (memcmp(nic.node_addr, lacp.pkt.actor.system, ETH_ALEN) != 0) {
  279. lacp_init_state(nic.node_addr);
  280. }
  281. /* If the remote information has expired I need to take action */
  282. if (lacp_timer_expired(now, lacp.current_while_timer)) {
  283. if (!(lacp.pkt.actor.state & LACP_EXPIRED)) {
  284. lacp.pkt.partner.state &= ~LACP_SYNCHRONIZATION;
  285. lacp.pkt.partner.state |= LACP_TIMEOUT;
  286. lacp.pkt.actor.state |= LACP_EXPIRED;
  287. lacp.current_while_timer = now + SHORT_TIMEOUT_TIME;
  288. ntt = 1;
  289. }
  290. else {
  291. lacp_init_state(nic.node_addr);
  292. }
  293. }
  294. /* If the periodic timer has expired I need to transmit */
  295. if (lacp_timer_expired(now, lacp.periodic_timer)) {
  296. ntt = 1;
  297. /* Reset by lacp_start_periodic_timer */
  298. }
  299. if (ntt) {
  300. eth_transmit(slow_dest, ETH_P_SLOW, sizeof(lacp.pkt), &lacp.pkt);
  301. /* Restart the periodic timer */
  302. lacp_start_periodic_timer(now);
  303. print_lacp("Trasmitted", &lacp.pkt, now);
  304. }
  305. }
  306. static inline void send_eth_slow_reports(unsigned long now)
  307. {
  308. send_lacp_reports(now, 0);
  309. }
  310. static inline void process_eth_slow(unsigned short ptype, unsigned long now)
  311. {
  312. union slow_union *pkt;
  313. if ((ptype != ETH_P_SLOW) ||
  314. (nic.packetlen < (ETH_HLEN + sizeof(pkt->header)))) {
  315. return;
  316. }
  317. pkt = (union slow_union *)&nic.packet[ETH_HLEN];
  318. if ((pkt->header.subtype == SLOW_SUBTYPE_LACP) &&
  319. (nic.packetlen >= ETH_HLEN + sizeof(pkt->lacp))) {
  320. int ntt;
  321. if (memcmp(nic.node_addr, lacp.pkt.actor.system, ETH_ALEN) != 0) {
  322. lacp_init_state(nic.node_addr);
  323. }
  324. /* As long as nic.packet is 2 byte aligned all is good */
  325. print_lacp("Received", &pkt->lacp, now);
  326. /* I don't actually implement the MUX or SELECT
  327. * machines.
  328. *
  329. * What logically happens when the client and I
  330. * disagree about an aggregator is the current
  331. * aggregtator is unselected. The MUX machine places
  332. * me in DETACHED. The SELECT machine runs and
  333. * reslects the same aggregator. If I go through
  334. * these steps fast enough an outside observer can not
  335. * notice this.
  336. *
  337. * Since the process will not generate any noticeable
  338. * effect it does not need an implmenetation. This
  339. * keeps the code simple and the code and binary
  340. * size down.
  341. */
  342. /* lacp_update_selected(&pkt->lacp); */
  343. ntt = lacp_update_ntt(&pkt->lacp);
  344. lacp_record_pdu(&pkt->lacp);
  345. lacp_start_current_while_timer(now);
  346. send_lacp_reports(now, ntt);
  347. }
  348. /* If we receive a marker information packet return it */
  349. else if ((pkt->header.subtype == SLOW_SUBTYPE_MARKER) &&
  350. (nic.packetlen >= ETH_HLEN + sizeof(pkt->marker)) &&
  351. (pkt->marker.tlv_type == MARKER_TLV_INFO) &&
  352. (pkt->marker.marker_length == 0x16))
  353. {
  354. pkt->marker.tlv_type = MARKER_TLV_RESPONSE;
  355. eth_transmit(slow_dest, ETH_P_SLOW,
  356. sizeof(pkt->marker), &pkt->marker);
  357. }
  358. }
  359. #else
  360. #define send_eth_slow_reports(now) do {} while(0)
  361. #define process_eth_slow(ptype, now) do {} while(0)
  362. #endif