summaryrefslogtreecommitdiffstats
path: root/src/net/eth_slow.c
blob: baa51dbc17176d143550c53da92585841e054f3b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
 * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * You can also choose to distribute this program under the terms of
 * the Unmodified Binary Distribution Licence (as given in the file
 * COPYING.UBDL), provided that you have satisfied its requirements.
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

#include <stdlib.h>
#include <string.h>
#include <byteswap.h>
#include <errno.h>
#include <ipxe/timer.h>
#include <ipxe/iobuf.h>
#include <ipxe/netdevice.h>
#include <ipxe/if_ether.h>
#include <ipxe/ethernet.h>
#include <ipxe/eth_slow.h>

/** @file
 *
 * Ethernet slow protocols
 *
 * We implement a very simple passive LACP entity, that pretends that
 * each port is the only port on an individual system.  We avoid the
 * need for timeout logic (and retaining local state about our
 * partner) by requesting the same timeout period (1s or 30s) as our
 * partner requests, and then simply responding to every packet the
 * partner sends us.
 */

struct net_protocol eth_slow_protocol __net_protocol;

/** Slow protocols multicast address */
static const uint8_t eth_slow_address[ETH_ALEN] =
	{ 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };

/**
 * Name LACP TLV type
 *
 * @v type		LACP TLV type
 * @ret name		Name of LACP TLV type
 */
static inline __attribute__ (( always_inline )) const char *
eth_slow_lacp_tlv_name ( uint8_t type ) {
	switch ( type ) {
	case ETH_SLOW_TLV_TERMINATOR:		return "terminator";
	case ETH_SLOW_TLV_LACP_ACTOR:		return "actor";
	case ETH_SLOW_TLV_LACP_PARTNER:		return "partner";
	case ETH_SLOW_TLV_LACP_COLLECTOR:	return "collector";
	default:				return "<invalid>";
	}
}

/**
 * Name marker TLV type
 *
 * @v type		Marker TLV type
 * @ret name		Name of marker TLV type
 */
static inline __attribute__ (( always_inline )) const char *
eth_slow_marker_tlv_name ( uint8_t type ) {
	switch ( type ) {
	case ETH_SLOW_TLV_TERMINATOR:		return "terminator";
	case ETH_SLOW_TLV_MARKER_REQUEST:	return "request";
	case ETH_SLOW_TLV_MARKER_RESPONSE:	return "response";
	default:				return "<invalid>";
	}
}

/**
 * Name LACP state
 *
 * @v state		LACP state
 * @ret name		LACP state name
 */
static const char * eth_slow_lacp_state_name ( uint8_t state ) {
	static char state_chars[] = "AFGSCDLX";
	unsigned int i;

	for ( i = 0 ; i < 8 ; i++ ) {
		state_chars[i] |= 0x20;
		if ( state & ( 1 << i ) )
			state_chars[i] &= ~0x20;
	}
	return state_chars;
}

/**
 * Dump LACP packet
 *
 * @v iobuf		I/O buffer
 * @v netdev		Network device
 * @v label		"RX" or "TX"
 */
static void eth_slow_lacp_dump ( struct io_buffer *iobuf,
				 struct net_device *netdev,
				 const char *label ) {
	union eth_slow_packet *eth_slow = iobuf->data;
	struct eth_slow_lacp *lacp = &eth_slow->lacp;

	DBGC ( netdev,
	       "SLOW %s %s LACP actor (%04x,%s,%04x,%02x,%04x) [%s]\n",
	       netdev->name, label, ntohs ( lacp->actor.system_priority ),
	       eth_ntoa ( lacp->actor.system ),
	       ntohs ( lacp->actor.key ),
	       ntohs ( lacp->actor.port_priority ),
	       ntohs ( lacp->actor.port ),
	       eth_slow_lacp_state_name ( lacp->actor.state ) );
	DBGC ( netdev,
	       "SLOW %s %s LACP partner (%04x,%s,%04x,%02x,%04x) [%s]\n",
	       netdev->name, label, ntohs ( lacp->partner.system_priority ),
	       eth_ntoa ( lacp->partner.system ),
	       ntohs ( lacp->partner.key ),
	       ntohs ( lacp->partner.port_priority ),
	       ntohs ( lacp->partner.port ),
	       eth_slow_lacp_state_name ( lacp->partner.state ) );
	DBGC ( netdev, "SLOW %s %s LACP collector %04x (%d us)\n",
	       netdev->name, label, ntohs ( lacp->collector.max_delay ),
	       ( ntohs ( lacp->collector.max_delay ) * 10 ) );
	DBGC2_HDA ( netdev, 0, iobuf->data, iob_len ( iobuf ) );
}

/**
 * Process incoming LACP packet
 *
 * @v iobuf		I/O buffer
 * @v netdev		Network device
 * @ret rc		Return status code
 */
static int eth_slow_lacp_rx ( struct io_buffer *iobuf,
			      struct net_device *netdev ) {
	union eth_slow_packet *eth_slow = iobuf->data;
	struct eth_slow_lacp *lacp = &eth_slow->lacp;
	unsigned int interval;

	eth_slow_lacp_dump ( iobuf, netdev, "RX" );

	/* If partner is not in sync, collecting, and distributing,
	 * then block the link until after the next expected LACP
	 * packet.
	 */
	if ( ~lacp->actor.state & ( LACP_STATE_IN_SYNC |
				    LACP_STATE_COLLECTING |
				    LACP_STATE_DISTRIBUTING ) ) {
		DBGC ( netdev, "SLOW %s LACP partner is down\n", netdev->name );
		interval = ( ( lacp->actor.state & LACP_STATE_FAST ) ?
			     ( ( LACP_INTERVAL_FAST + 1 ) * TICKS_PER_SEC ) :
			     ( ( LACP_INTERVAL_SLOW + 1 ) * TICKS_PER_SEC ) );
		netdev_link_block ( netdev, interval );
	} else {
		if ( netdev_link_blocked ( netdev ) ) {
			DBGC ( netdev, "SLOW %s LACP partner is up\n",
			       netdev->name );
		}
		netdev_link_unblock ( netdev );
	}

	/* Build response */
	memset ( lacp->reserved, 0, sizeof ( lacp->reserved ) );
	memset ( &lacp->terminator, 0, sizeof ( lacp->terminator ) );
	memset ( &lacp->collector, 0, sizeof ( lacp->collector ) );
	lacp->collector.tlv.type = ETH_SLOW_TLV_LACP_COLLECTOR;
	lacp->collector.tlv.length = ETH_SLOW_TLV_LACP_COLLECTOR_LEN;
	memcpy ( &lacp->partner, &lacp->actor, sizeof ( lacp->partner ) );
	lacp->partner.tlv.type = ETH_SLOW_TLV_LACP_PARTNER;
	lacp->partner.tlv.length = ETH_SLOW_TLV_LACP_PARTNER_LEN;
	memset ( &lacp->partner.reserved, 0,
		 sizeof ( lacp->partner.reserved ) );
	memset ( &lacp->actor, 0, sizeof ( lacp->actor ) );
	lacp->actor.tlv.type = ETH_SLOW_TLV_LACP_ACTOR;
	lacp->actor.tlv.length = ETH_SLOW_TLV_LACP_ACTOR_LEN;
	lacp->actor.system_priority = htons ( LACP_SYSTEM_PRIORITY_MAX );
	memcpy ( lacp->actor.system, netdev->ll_addr,
		 sizeof ( lacp->actor.system ) );
	lacp->actor.key = htons ( 1 );
	lacp->actor.port_priority = htons ( LACP_PORT_PRIORITY_MAX );
	lacp->actor.port = htons ( 1 );
	lacp->actor.state = ( LACP_STATE_AGGREGATABLE |
			      LACP_STATE_IN_SYNC |
			      LACP_STATE_COLLECTING |
			      LACP_STATE_DISTRIBUTING |
			      ( lacp->partner.state & LACP_STATE_FAST ) );
	lacp->header.version = ETH_SLOW_LACP_VERSION;

	/* Send response */
	eth_slow_lacp_dump ( iobuf, netdev, "TX" );
	return net_tx ( iobuf, netdev, &eth_slow_protocol, eth_slow_address,
			netdev->ll_addr );
}

/**
 * Dump marker packet
 *
 * @v iobuf		I/O buffer
 * @v netdev		Network device
 * @v label		"RX" or "TX"
 */
static void eth_slow_marker_dump ( struct io_buffer *iobuf,
				   struct net_device *netdev,
				   const char *label ) {
	union eth_slow_packet *eth_slow = iobuf->data;
	struct eth_slow_marker *marker = &eth_slow->marker;

	DBGC ( netdev, "SLOW %s %s marker %s port %04x system %s xact %08x\n",
	       netdev->name, label,
	       eth_slow_marker_tlv_name ( marker->marker.tlv.type ),
	       ntohs ( marker->marker.port ),
	       eth_ntoa ( marker->marker.system ),
	       ntohl ( marker->marker.xact ) );
	DBGC2_HDA ( netdev, 0, iobuf->data, iob_len ( iobuf ) );
}

/**
 * Process incoming marker packet
 *
 * @v iobuf		I/O buffer
 * @v netdev		Network device
 * @ret rc		Return status code
 */
static int eth_slow_marker_rx ( struct io_buffer *iobuf,
				struct net_device *netdev ) {
	union eth_slow_packet *eth_slow = iobuf->data;
	struct eth_slow_marker *marker = &eth_slow->marker;

	eth_slow_marker_dump ( iobuf, netdev, "RX" );

	if ( marker->marker.tlv.type == ETH_SLOW_TLV_MARKER_REQUEST ) {
		/* Send marker response */
		marker->marker.tlv.type = ETH_SLOW_TLV_MARKER_RESPONSE;
		eth_slow_marker_dump ( iobuf, netdev, "TX" );
		return net_tx ( iobuf, netdev, &eth_slow_protocol,
				eth_slow_address, netdev->ll_addr );
	} else {
		/* Discard all other marker packets */
		free_iob ( iobuf );
		return -EINVAL;
	}
}

/**
 * Process incoming slow packet
 *
 * @v iobuf		I/O buffer
 * @v netdev		Network device
 * @v ll_dest		Link-layer destination address
 * @v ll_source		Link-layer source address
 * @v flags		Packet flags
 * @ret rc		Return status code
 */
static int eth_slow_rx ( struct io_buffer *iobuf,
			 struct net_device *netdev,
			 const void *ll_dest __unused,
			 const void *ll_source __unused,
			 unsigned int flags __unused ) {
	union eth_slow_packet *eth_slow = iobuf->data;

	/* Sanity checks */
	if ( iob_len ( iobuf ) < sizeof ( *eth_slow ) ) {
		free_iob ( iobuf );
		return -EINVAL;
	}

	/* Handle according to subtype */
	switch ( eth_slow->header.subtype ) {
	case ETH_SLOW_SUBTYPE_LACP:
		return eth_slow_lacp_rx ( iobuf, netdev );
	case ETH_SLOW_SUBTYPE_MARKER:
		return eth_slow_marker_rx ( iobuf, netdev );
	default:
		DBGC ( netdev, "SLOW %s RX unknown subtype %02x\n",
		       netdev->name, eth_slow->header.subtype );
		free_iob ( iobuf );
		return -EINVAL;
	}
}

/** Slow protocol */
struct net_protocol eth_slow_protocol __net_protocol = {
	.name = "Slow",
	.net_proto = htons ( ETH_P_SLOW ),
	.rx = eth_slow_rx,
};