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.5KB

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