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

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