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.

eth_slow.c 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <byteswap.h>
  27. #include <errno.h>
  28. #include <ipxe/timer.h>
  29. #include <ipxe/iobuf.h>
  30. #include <ipxe/netdevice.h>
  31. #include <ipxe/if_ether.h>
  32. #include <ipxe/ethernet.h>
  33. #include <ipxe/eth_slow.h>
  34. /** @file
  35. *
  36. * Ethernet slow protocols
  37. *
  38. * We implement a very simple passive LACP entity, that pretends that
  39. * each port is the only port on an individual system. We avoid the
  40. * need for timeout logic (and retaining local state about our
  41. * partner) by requesting the same timeout period (1s or 30s) as our
  42. * partner requests, and then simply responding to every packet the
  43. * partner sends us.
  44. */
  45. struct net_protocol eth_slow_protocol __net_protocol;
  46. /** Slow protocols multicast address */
  47. static const uint8_t eth_slow_address[ETH_ALEN] =
  48. { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
  49. /**
  50. * Name LACP TLV type
  51. *
  52. * @v type LACP TLV type
  53. * @ret name Name of LACP TLV type
  54. */
  55. static inline __attribute__ (( always_inline )) const char *
  56. eth_slow_lacp_tlv_name ( uint8_t type ) {
  57. switch ( type ) {
  58. case ETH_SLOW_TLV_TERMINATOR: return "terminator";
  59. case ETH_SLOW_TLV_LACP_ACTOR: return "actor";
  60. case ETH_SLOW_TLV_LACP_PARTNER: return "partner";
  61. case ETH_SLOW_TLV_LACP_COLLECTOR: return "collector";
  62. default: return "<invalid>";
  63. }
  64. }
  65. /**
  66. * Name marker TLV type
  67. *
  68. * @v type Marker TLV type
  69. * @ret name Name of marker TLV type
  70. */
  71. static inline __attribute__ (( always_inline )) const char *
  72. eth_slow_marker_tlv_name ( uint8_t type ) {
  73. switch ( type ) {
  74. case ETH_SLOW_TLV_TERMINATOR: return "terminator";
  75. case ETH_SLOW_TLV_MARKER_REQUEST: return "request";
  76. case ETH_SLOW_TLV_MARKER_RESPONSE: return "response";
  77. default: return "<invalid>";
  78. }
  79. }
  80. /**
  81. * Name LACP state
  82. *
  83. * @v state LACP state
  84. * @ret name LACP state name
  85. */
  86. static const char * eth_slow_lacp_state_name ( uint8_t state ) {
  87. static char state_chars[] = "AFGSCDLX";
  88. unsigned int i;
  89. for ( i = 0 ; i < 8 ; i++ ) {
  90. state_chars[i] |= 0x20;
  91. if ( state & ( 1 << i ) )
  92. state_chars[i] &= ~0x20;
  93. }
  94. return state_chars;
  95. }
  96. /**
  97. * Dump LACP packet
  98. *
  99. * @v iobuf I/O buffer
  100. * @v netdev Network device
  101. * @v label "RX" or "TX"
  102. */
  103. static void eth_slow_lacp_dump ( struct io_buffer *iobuf,
  104. struct net_device *netdev,
  105. const char *label ) {
  106. union eth_slow_packet *eth_slow = iobuf->data;
  107. struct eth_slow_lacp *lacp = &eth_slow->lacp;
  108. DBGC ( netdev,
  109. "SLOW %s %s LACP actor (%04x,%s,%04x,%02x,%04x) [%s]\n",
  110. netdev->name, label, ntohs ( lacp->actor.system_priority ),
  111. eth_ntoa ( lacp->actor.system ),
  112. ntohs ( lacp->actor.key ),
  113. ntohs ( lacp->actor.port_priority ),
  114. ntohs ( lacp->actor.port ),
  115. eth_slow_lacp_state_name ( lacp->actor.state ) );
  116. DBGC ( netdev,
  117. "SLOW %s %s LACP partner (%04x,%s,%04x,%02x,%04x) [%s]\n",
  118. netdev->name, label, ntohs ( lacp->partner.system_priority ),
  119. eth_ntoa ( lacp->partner.system ),
  120. ntohs ( lacp->partner.key ),
  121. ntohs ( lacp->partner.port_priority ),
  122. ntohs ( lacp->partner.port ),
  123. eth_slow_lacp_state_name ( lacp->partner.state ) );
  124. DBGC ( netdev, "SLOW %s %s LACP collector %04x (%d us)\n",
  125. netdev->name, label, ntohs ( lacp->collector.max_delay ),
  126. ( ntohs ( lacp->collector.max_delay ) * 10 ) );
  127. DBGC2_HDA ( netdev, 0, iobuf->data, iob_len ( iobuf ) );
  128. }
  129. /**
  130. * Process incoming LACP packet
  131. *
  132. * @v iobuf I/O buffer
  133. * @v netdev Network device
  134. * @ret rc Return status code
  135. */
  136. static int eth_slow_lacp_rx ( struct io_buffer *iobuf,
  137. struct net_device *netdev ) {
  138. union eth_slow_packet *eth_slow = iobuf->data;
  139. struct eth_slow_lacp *lacp = &eth_slow->lacp;
  140. unsigned int interval;
  141. eth_slow_lacp_dump ( iobuf, netdev, "RX" );
  142. /* If partner is not in sync, collecting, and distributing,
  143. * then block the link until after the next expected LACP
  144. * packet.
  145. */
  146. if ( ~lacp->actor.state & ( LACP_STATE_IN_SYNC |
  147. LACP_STATE_COLLECTING |
  148. LACP_STATE_DISTRIBUTING ) ) {
  149. DBGC ( netdev, "SLOW %s LACP partner is down\n", netdev->name );
  150. interval = ( ( lacp->actor.state & LACP_STATE_FAST ) ?
  151. ( ( LACP_INTERVAL_FAST + 1 ) * TICKS_PER_SEC ) :
  152. ( ( LACP_INTERVAL_SLOW + 1 ) * TICKS_PER_SEC ) );
  153. netdev_link_block ( netdev, interval );
  154. } else {
  155. if ( netdev_link_blocked ( netdev ) ) {
  156. DBGC ( netdev, "SLOW %s LACP partner is up\n",
  157. netdev->name );
  158. }
  159. netdev_link_unblock ( netdev );
  160. }
  161. /* Build response */
  162. memset ( lacp->reserved, 0, sizeof ( lacp->reserved ) );
  163. memset ( &lacp->terminator, 0, sizeof ( lacp->terminator ) );
  164. memset ( &lacp->collector, 0, sizeof ( lacp->collector ) );
  165. lacp->collector.tlv.type = ETH_SLOW_TLV_LACP_COLLECTOR;
  166. lacp->collector.tlv.length = ETH_SLOW_TLV_LACP_COLLECTOR_LEN;
  167. memcpy ( &lacp->partner, &lacp->actor, sizeof ( lacp->partner ) );
  168. lacp->partner.tlv.type = ETH_SLOW_TLV_LACP_PARTNER;
  169. lacp->partner.tlv.length = ETH_SLOW_TLV_LACP_PARTNER_LEN;
  170. memset ( &lacp->partner.reserved, 0,
  171. sizeof ( lacp->partner.reserved ) );
  172. memset ( &lacp->actor, 0, sizeof ( lacp->actor ) );
  173. lacp->actor.tlv.type = ETH_SLOW_TLV_LACP_ACTOR;
  174. lacp->actor.tlv.length = ETH_SLOW_TLV_LACP_ACTOR_LEN;
  175. lacp->actor.system_priority = htons ( LACP_SYSTEM_PRIORITY_MAX );
  176. memcpy ( lacp->actor.system, netdev->ll_addr,
  177. sizeof ( lacp->actor.system ) );
  178. lacp->actor.key = htons ( 1 );
  179. lacp->actor.port_priority = htons ( LACP_PORT_PRIORITY_MAX );
  180. lacp->actor.port = htons ( 1 );
  181. lacp->actor.state = ( LACP_STATE_AGGREGATABLE |
  182. LACP_STATE_IN_SYNC |
  183. LACP_STATE_COLLECTING |
  184. LACP_STATE_DISTRIBUTING |
  185. ( lacp->partner.state & LACP_STATE_FAST ) );
  186. lacp->header.version = ETH_SLOW_LACP_VERSION;
  187. /* Send response */
  188. eth_slow_lacp_dump ( iobuf, netdev, "TX" );
  189. return net_tx ( iobuf, netdev, &eth_slow_protocol, eth_slow_address,
  190. netdev->ll_addr );
  191. }
  192. /**
  193. * Dump marker packet
  194. *
  195. * @v iobuf I/O buffer
  196. * @v netdev Network device
  197. * @v label "RX" or "TX"
  198. */
  199. static void eth_slow_marker_dump ( struct io_buffer *iobuf,
  200. struct net_device *netdev,
  201. const char *label ) {
  202. union eth_slow_packet *eth_slow = iobuf->data;
  203. struct eth_slow_marker *marker = &eth_slow->marker;
  204. DBGC ( netdev, "SLOW %s %s marker %s port %04x system %s xact %08x\n",
  205. netdev->name, label,
  206. eth_slow_marker_tlv_name ( marker->marker.tlv.type ),
  207. ntohs ( marker->marker.port ),
  208. eth_ntoa ( marker->marker.system ),
  209. ntohl ( marker->marker.xact ) );
  210. DBGC2_HDA ( netdev, 0, iobuf->data, iob_len ( iobuf ) );
  211. }
  212. /**
  213. * Process incoming marker packet
  214. *
  215. * @v iobuf I/O buffer
  216. * @v netdev Network device
  217. * @ret rc Return status code
  218. */
  219. static int eth_slow_marker_rx ( struct io_buffer *iobuf,
  220. struct net_device *netdev ) {
  221. union eth_slow_packet *eth_slow = iobuf->data;
  222. struct eth_slow_marker *marker = &eth_slow->marker;
  223. eth_slow_marker_dump ( iobuf, netdev, "RX" );
  224. if ( marker->marker.tlv.type == ETH_SLOW_TLV_MARKER_REQUEST ) {
  225. /* Send marker response */
  226. marker->marker.tlv.type = ETH_SLOW_TLV_MARKER_RESPONSE;
  227. eth_slow_marker_dump ( iobuf, netdev, "TX" );
  228. return net_tx ( iobuf, netdev, &eth_slow_protocol,
  229. eth_slow_address, netdev->ll_addr );
  230. } else {
  231. /* Discard all other marker packets */
  232. free_iob ( iobuf );
  233. return -EINVAL;
  234. }
  235. }
  236. /**
  237. * Process incoming slow packet
  238. *
  239. * @v iobuf I/O buffer
  240. * @v netdev Network device
  241. * @v ll_dest Link-layer destination address
  242. * @v ll_source Link-layer source address
  243. * @v flags Packet flags
  244. * @ret rc Return status code
  245. */
  246. static int eth_slow_rx ( struct io_buffer *iobuf,
  247. struct net_device *netdev,
  248. const void *ll_dest __unused,
  249. const void *ll_source __unused,
  250. unsigned int flags __unused ) {
  251. union eth_slow_packet *eth_slow = iobuf->data;
  252. /* Sanity checks */
  253. if ( iob_len ( iobuf ) < sizeof ( *eth_slow ) ) {
  254. free_iob ( iobuf );
  255. return -EINVAL;
  256. }
  257. /* Handle according to subtype */
  258. switch ( eth_slow->header.subtype ) {
  259. case ETH_SLOW_SUBTYPE_LACP:
  260. return eth_slow_lacp_rx ( iobuf, netdev );
  261. case ETH_SLOW_SUBTYPE_MARKER:
  262. return eth_slow_marker_rx ( iobuf, netdev );
  263. default:
  264. DBGC ( netdev, "SLOW %s RX unknown subtype %02x\n",
  265. netdev->name, eth_slow->header.subtype );
  266. free_iob ( iobuf );
  267. return -EINVAL;
  268. }
  269. }
  270. /** Slow protocol */
  271. struct net_protocol eth_slow_protocol __net_protocol = {
  272. .name = "Slow",
  273. .net_proto = htons ( ETH_P_SLOW ),
  274. .rx = eth_slow_rx,
  275. };