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 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <byteswap.h>
  22. #include <errno.h>
  23. #include <ipxe/iobuf.h>
  24. #include <ipxe/netdevice.h>
  25. #include <ipxe/if_ether.h>
  26. #include <ipxe/ethernet.h>
  27. #include <ipxe/eth_slow.h>
  28. /** @file
  29. *
  30. * Ethernet slow protocols
  31. *
  32. * We implement a very simple passive LACP entity, that pretends that
  33. * each port is the only port on an individual system. We avoid the
  34. * need for timeout logic (and retaining local state about our
  35. * partner) by requesting the same timeout period (1s or 30s) as our
  36. * partner requests, and then simply responding to every packet the
  37. * partner sends us.
  38. */
  39. struct net_protocol eth_slow_protocol __net_protocol;
  40. /** Slow protocols multicast address */
  41. static const uint8_t eth_slow_address[ETH_ALEN] =
  42. { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
  43. /**
  44. * Name LACP TLV type
  45. *
  46. * @v type LACP TLV type
  47. * @ret name Name of LACP TLV type
  48. */
  49. static inline __attribute__ (( always_inline )) const char *
  50. eth_slow_lacp_tlv_name ( uint8_t type ) {
  51. switch ( type ) {
  52. case ETH_SLOW_TLV_TERMINATOR: return "terminator";
  53. case ETH_SLOW_TLV_LACP_ACTOR: return "actor";
  54. case ETH_SLOW_TLV_LACP_PARTNER: return "partner";
  55. case ETH_SLOW_TLV_LACP_COLLECTOR: return "collector";
  56. default: return "<invalid>";
  57. }
  58. }
  59. /**
  60. * Name marker TLV type
  61. *
  62. * @v type Marker TLV type
  63. * @ret name Name of marker TLV type
  64. */
  65. static inline __attribute__ (( always_inline )) const char *
  66. eth_slow_marker_tlv_name ( uint8_t type ) {
  67. switch ( type ) {
  68. case ETH_SLOW_TLV_TERMINATOR: return "terminator";
  69. case ETH_SLOW_TLV_MARKER_REQUEST: return "request";
  70. case ETH_SLOW_TLV_MARKER_RESPONSE: return "response";
  71. default: return "<invalid>";
  72. }
  73. }
  74. /**
  75. * Name LACP state
  76. *
  77. * @v state LACP state
  78. * @ret name LACP state name
  79. */
  80. static const char * eth_slow_lacp_state_name ( uint8_t state ) {
  81. static char state_chars[] = "AFGSRTLX";
  82. unsigned int i;
  83. for ( i = 0 ; i < 8 ; i++ ) {
  84. state_chars[i] |= 0x20;
  85. if ( state & ( 1 << i ) )
  86. state_chars[i] &= ~0x20;
  87. }
  88. return state_chars;
  89. }
  90. /**
  91. * Dump LACP packet
  92. *
  93. * @v iobuf I/O buffer
  94. * @v netdev Network device
  95. * @v label "RX" or "TX"
  96. */
  97. static void eth_slow_lacp_dump ( struct io_buffer *iobuf,
  98. struct net_device *netdev,
  99. const char *label ) {
  100. union eth_slow_packet *eth_slow = iobuf->data;
  101. struct eth_slow_lacp *lacp = &eth_slow->lacp;
  102. DBGC ( netdev,
  103. "SLOW %s %s LACP actor (%04x,%s,%04x,%02x,%04x) [%s]\n",
  104. netdev->name, label, ntohs ( lacp->actor.system_priority ),
  105. eth_ntoa ( lacp->actor.system ),
  106. ntohs ( lacp->actor.key ),
  107. ntohs ( lacp->actor.port_priority ),
  108. ntohs ( lacp->actor.port ),
  109. eth_slow_lacp_state_name ( lacp->actor.state ) );
  110. DBGC ( netdev,
  111. "SLOW %s %s LACP partner (%04x,%s,%04x,%02x,%04x) [%s]\n",
  112. netdev->name, label, ntohs ( lacp->partner.system_priority ),
  113. eth_ntoa ( lacp->partner.system ),
  114. ntohs ( lacp->partner.key ),
  115. ntohs ( lacp->partner.port_priority ),
  116. ntohs ( lacp->partner.port ),
  117. eth_slow_lacp_state_name ( lacp->partner.state ) );
  118. DBGC ( netdev, "SLOW %s %s LACP collector %04x (%d us)\n",
  119. netdev->name, label, ntohs ( lacp->collector.max_delay ),
  120. ( ntohs ( lacp->collector.max_delay ) * 10 ) );
  121. DBGC2_HDA ( netdev, 0, iobuf->data, iob_len ( iobuf ) );
  122. }
  123. /**
  124. * Process incoming LACP packet
  125. *
  126. * @v iobuf I/O buffer
  127. * @v netdev Network device
  128. * @ret rc Return status code
  129. */
  130. static int eth_slow_lacp_rx ( struct io_buffer *iobuf,
  131. struct net_device *netdev ) {
  132. union eth_slow_packet *eth_slow = iobuf->data;
  133. struct eth_slow_lacp *lacp = &eth_slow->lacp;
  134. eth_slow_lacp_dump ( iobuf, netdev, "RX" );
  135. /* Build response */
  136. memset ( lacp->reserved, 0, sizeof ( lacp->reserved ) );
  137. memset ( &lacp->terminator, 0, sizeof ( lacp->terminator ) );
  138. memset ( &lacp->collector, 0, sizeof ( lacp->collector ) );
  139. lacp->collector.tlv.type = ETH_SLOW_TLV_LACP_COLLECTOR;
  140. lacp->collector.tlv.length = ETH_SLOW_TLV_LACP_COLLECTOR_LEN;
  141. memcpy ( &lacp->partner, &lacp->actor, sizeof ( lacp->partner ) );
  142. lacp->partner.tlv.type = ETH_SLOW_TLV_LACP_PARTNER;
  143. lacp->partner.tlv.length = ETH_SLOW_TLV_LACP_PARTNER_LEN;
  144. memset ( &lacp->partner.reserved, 0,
  145. sizeof ( lacp->partner.reserved ) );
  146. memset ( &lacp->actor, 0, sizeof ( lacp->actor ) );
  147. lacp->actor.tlv.type = ETH_SLOW_TLV_LACP_ACTOR;
  148. lacp->actor.tlv.length = ETH_SLOW_TLV_LACP_ACTOR_LEN;
  149. lacp->actor.system_priority = htons ( LACP_SYSTEM_PRIORITY_MAX );
  150. memcpy ( lacp->actor.system, netdev->ll_addr,
  151. sizeof ( lacp->actor.system ) );
  152. lacp->actor.key = htons ( 1 );
  153. lacp->actor.port_priority = htons ( LACP_PORT_PRIORITY_MAX );
  154. lacp->actor.port = htons ( 1 );
  155. lacp->actor.state = ( LACP_STATE_IN_SYNC |
  156. LACP_STATE_COLLECTING |
  157. LACP_STATE_DISTRIBUTING |
  158. ( lacp->partner.state & LACP_STATE_FAST ) );
  159. lacp->header.version = ETH_SLOW_LACP_VERSION;
  160. /* Send response */
  161. eth_slow_lacp_dump ( iobuf, netdev, "TX" );
  162. return net_tx ( iobuf, netdev, &eth_slow_protocol, eth_slow_address,
  163. netdev->ll_addr );
  164. }
  165. /**
  166. * Dump marker packet
  167. *
  168. * @v iobuf I/O buffer
  169. * @v netdev Network device
  170. * @v label "RX" or "TX"
  171. */
  172. static void eth_slow_marker_dump ( struct io_buffer *iobuf,
  173. struct net_device *netdev,
  174. const char *label ) {
  175. union eth_slow_packet *eth_slow = iobuf->data;
  176. struct eth_slow_marker *marker = &eth_slow->marker;
  177. DBGC ( netdev, "SLOW %s %s marker %s port %04x system %s xact %08x\n",
  178. netdev->name, label,
  179. eth_slow_marker_tlv_name ( marker->marker.tlv.type ),
  180. ntohs ( marker->marker.port ),
  181. eth_ntoa ( marker->marker.system ),
  182. ntohl ( marker->marker.xact ) );
  183. DBGC2_HDA ( netdev, 0, iobuf->data, iob_len ( iobuf ) );
  184. }
  185. /**
  186. * Process incoming marker packet
  187. *
  188. * @v iobuf I/O buffer
  189. * @v netdev Network device
  190. * @ret rc Return status code
  191. */
  192. static int eth_slow_marker_rx ( struct io_buffer *iobuf,
  193. struct net_device *netdev ) {
  194. union eth_slow_packet *eth_slow = iobuf->data;
  195. struct eth_slow_marker *marker = &eth_slow->marker;
  196. eth_slow_marker_dump ( iobuf, netdev, "RX" );
  197. if ( marker->marker.tlv.type == ETH_SLOW_TLV_MARKER_REQUEST ) {
  198. /* Send marker response */
  199. marker->marker.tlv.type = ETH_SLOW_TLV_MARKER_RESPONSE;
  200. eth_slow_marker_dump ( iobuf, netdev, "TX" );
  201. return net_tx ( iobuf, netdev, &eth_slow_protocol,
  202. eth_slow_address, netdev->ll_addr );
  203. } else {
  204. /* Discard all other marker packets */
  205. free_iob ( iobuf );
  206. return -EINVAL;
  207. }
  208. }
  209. /**
  210. * Process incoming slow packet
  211. *
  212. * @v iobuf I/O buffer
  213. * @v netdev Network device
  214. * @v ll_dest Link-layer destination address
  215. * @v ll_source Link-layer source address
  216. * @v flags Packet flags
  217. * @ret rc Return status code
  218. */
  219. static int eth_slow_rx ( struct io_buffer *iobuf,
  220. struct net_device *netdev,
  221. const void *ll_dest __unused,
  222. const void *ll_source __unused,
  223. unsigned int flags __unused ) {
  224. union eth_slow_packet *eth_slow = iobuf->data;
  225. /* Sanity checks */
  226. if ( iob_len ( iobuf ) < sizeof ( *eth_slow ) ) {
  227. free_iob ( iobuf );
  228. return -EINVAL;
  229. }
  230. /* Handle according to subtype */
  231. switch ( eth_slow->header.subtype ) {
  232. case ETH_SLOW_SUBTYPE_LACP:
  233. return eth_slow_lacp_rx ( iobuf, netdev );
  234. case ETH_SLOW_SUBTYPE_MARKER:
  235. return eth_slow_marker_rx ( iobuf, netdev );
  236. default:
  237. DBGC ( netdev, "SLOW %s RX unknown subtype %02x\n",
  238. netdev->name, eth_slow->header.subtype );
  239. free_iob ( iobuf );
  240. return -EINVAL;
  241. }
  242. }
  243. /** Slow protocol */
  244. struct net_protocol eth_slow_protocol __net_protocol = {
  245. .name = "Slow",
  246. .net_proto = htons ( ETH_P_SLOW ),
  247. .rx = eth_slow_rx,
  248. };