aboutsummaryrefslogtreecommitdiff
path: root/src/usr/lotest.c
blob: c4b0b4413f9bd94a6daa398090802e08eafea6c0 (plain)
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
/*
 * 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.
 */

FILE_LICENCE ( GPL2_OR_LATER );

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <byteswap.h>
#include <ipxe/iobuf.h>
#include <ipxe/netdevice.h>
#include <ipxe/if_ether.h>
#include <ipxe/keys.h>
#include <ipxe/console.h>
#include <usr/ifmgmt.h>
#include <usr/lotest.h>

/** @file
 *
 * Loopback testing
 *
 */

#define LINK_WAIT_MS 15000

/**
 * Process received 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 lotest_rx ( struct io_buffer *iobuf,
		       struct net_device *netdev __unused,
		       const void *ll_dest __unused,
		       const void *ll_source __unused,
		       unsigned int flags __unused ) {
	free_iob ( iobuf );
	return -ENOTSUP;
}

/**
 * Transcribe network-layer address
 *
 * @v net_addr		Network-layer address
 * @ret string		Human-readable transcription of address
 */
static const char * lotest_ntoa ( const void *net_addr __unused ) {
	return "<INVALID>";
}

/**
 * Loopback test network-layer protocol
 *
 * Using a dedicated network-layer protocol avoids problems caused by
 * cards supporting features such as IPv4 checksum offload trying to
 * interpret the (randomly generated) network-layer content.
 */
static struct net_protocol lotest_protocol __net_protocol = {
	.name = "LOTEST",
	.rx = lotest_rx,
	.ntoa = lotest_ntoa,
	.net_proto = htons ( 0x6950 ), /* Not a genuine protocol number */
	.net_addr_len = 0,
};

/**
 * Wait for packet to be received
 *
 * @v receiver		Receiving network device*
 * @v data		Expected data
 * @v len		Expected data length
 * @ret rc		Return status code
 */
static int loopback_wait ( struct net_device *receiver, void *data,
			   size_t len ) {
	struct ll_protocol *ll_protocol = receiver->ll_protocol;
	struct io_buffer *iobuf;
	const void *ll_dest;
	const void *ll_source;
	uint16_t net_proto;
	unsigned int flags;
	int rc;

	/* Poll until packet arrives */
	while ( 1 ) {

		/* Check for cancellation */
		if ( iskey() && ( getchar() == CTRL_C ) )
			return -ECANCELED;

		/* Poll network devices */
		net_poll();

		/* Dequeue packet, if available */
		iobuf = netdev_rx_dequeue ( receiver );
		if ( ! iobuf )
			continue;

		/* Strip link-layer header */
		if ( ( rc = ll_protocol->pull ( receiver, iobuf, &ll_dest,
						&ll_source, &net_proto,
						&flags ) ) != 0 ) {
			printf ( "\nFailed to strip link-layer header: %s",
				 strerror ( rc ) );
			free_iob ( iob_disown ( iobuf ) );
			return rc;
		}

		/* Ignore non-loopback packets */
		if ( net_proto != lotest_protocol.net_proto ) {
			printf ( "\nReceived spurious packet type %04x\n",
				 ntohs ( net_proto ) );
			free_iob ( iob_disown ( iobuf ) );
			continue;
		}

		/* Check packet length */
		if ( iob_len ( iobuf ) != len ) {
			printf ( "\nLength mismatch: sent %zd, received %zd",
				 len, iob_len ( iobuf ) );
			DBG ( "\nSent:\n" );
			DBG_HDA ( 0, data, len );
			DBG ( "Received:\n" );
			DBG_HDA ( 0, iobuf->data, iob_len ( iobuf ) );
			free_iob ( iob_disown ( iobuf ) );
			return -EINVAL;
		}

		/* Check packet content */
		if ( memcmp ( iobuf->data, data, len ) != 0 ) {
			printf ( "\nContent mismatch" );
			DBG ( "\nSent:\n" );
			DBG_HDA ( 0, data, len );
			DBG ( "Received:\n" );
			DBG_HDA ( 0, iobuf->data, iob_len ( iobuf ) );
			free_iob ( iob_disown ( iobuf ) );
			return -EINVAL;
		}

		/* Discard packet and return */
		free_iob ( iob_disown ( iobuf ) );
		return 0;
	}
}

/**
 * Perform loopback test between two network devices
 *
 * @v sender		Sending network device
 * @v receiver		Received network device
 * @v mtu		Packet size (excluding link-layer headers)
 * @ret rc		Return status code
 */
int loopback_test ( struct net_device *sender, struct net_device *receiver,
		    size_t mtu ) {
	uint8_t buf[mtu];
	struct io_buffer *iobuf;
	unsigned int i;
	unsigned int successes;
	int rc;

	/* Open network devices */
	if ( ( rc = ifopen ( sender ) ) != 0 )
		return rc;
	if ( ( rc = ifopen ( receiver ) ) != 0 )
		return rc;

	/* Wait for link-up */
	if ( ( rc = iflinkwait ( sender, LINK_WAIT_MS ) ) != 0 )
		return rc;
	if ( ( rc = iflinkwait ( receiver, LINK_WAIT_MS ) ) != 0 )
		return rc;

	/* Print initial statistics */
	printf ( "Performing loopback test from %s to %s with %zd byte MTU\n",
		 sender->name, receiver->name, mtu );
	ifstat ( sender );
	ifstat ( receiver );

	/* Freeze receive queue processing on the receiver, so that we
	 * can extract all received packets.
	 */
	netdev_rx_freeze ( receiver );

	/* Perform loopback test */
	for ( successes = 0 ; ; successes++ ) {

		/* Print running total */
		printf ( "\r%d", successes );

		/* Generate random packet */
		for ( i = 0 ; i < sizeof ( buf ) ; i++ )
			buf[i] = random();
		iobuf = alloc_iob ( MAX_LL_HEADER_LEN + sizeof ( buf ) );
		if ( ! iobuf ) {
			printf ( "\nFailed to allocate I/O buffer" );
			rc = -ENOMEM;
			break;
		}
		iob_reserve ( iobuf, MAX_LL_HEADER_LEN );
		memcpy ( iob_put ( iobuf, sizeof ( buf ) ),
			 buf, sizeof ( buf ) );

		/* Transmit packet */
		if ( ( rc = net_tx ( iob_disown ( iobuf ), sender,
				     &lotest_protocol, receiver->ll_addr,
				     sender->ll_addr ) ) != 0 ) {
			printf ( "\nFailed to transmit packet: %s",
				 strerror ( rc ) );
			break;
		}

		/* Wait for received packet */
		if ( ( rc = loopback_wait ( receiver, buf,
					    sizeof ( buf ) ) ) != 0 ) {
			break;
		}
	}

	printf ( "\n");
	netdev_rx_unfreeze ( receiver );

	/* Dump final statistics */
	ifstat ( sender );
	ifstat ( receiver );

	return 0;
}