|
@@ -0,0 +1,210 @@
|
|
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
|
+
|
|
19
|
+FILE_LICENCE ( GPL2_OR_LATER );
|
|
20
|
+
|
|
21
|
+#include <stdint.h>
|
|
22
|
+#include <stdlib.h>
|
|
23
|
+#include <string.h>
|
|
24
|
+#include <stdio.h>
|
|
25
|
+#include <errno.h>
|
|
26
|
+#include <byteswap.h>
|
|
27
|
+#include <ipxe/iobuf.h>
|
|
28
|
+#include <ipxe/netdevice.h>
|
|
29
|
+#include <ipxe/if_ether.h>
|
|
30
|
+#include <ipxe/keys.h>
|
|
31
|
+#include <console.h>
|
|
32
|
+#include <usr/ifmgmt.h>
|
|
33
|
+#include <usr/lotest.h>
|
|
34
|
+
|
|
35
|
+/** @file
|
|
36
|
+ *
|
|
37
|
+ * Loopback testing
|
|
38
|
+ *
|
|
39
|
+ */
|
|
40
|
+
|
|
41
|
+#define LINK_WAIT_MS 15000
|
|
42
|
+
|
|
43
|
+/**
|
|
44
|
+ * Process received packet
|
|
45
|
+ *
|
|
46
|
+ * @v iobuf I/O buffer
|
|
47
|
+ * @v netdev Network device
|
|
48
|
+ * @v ll_source Link-layer source address
|
|
49
|
+ * @ret rc Return status code
|
|
50
|
+ */
|
|
51
|
+static int lotest_rx ( struct io_buffer *iobuf,
|
|
52
|
+ struct net_device *netdev __unused,
|
|
53
|
+ const void *ll_source __unused ) {
|
|
54
|
+ free_iob ( iobuf );
|
|
55
|
+ return -ENOTSUP;
|
|
56
|
+}
|
|
57
|
+
|
|
58
|
+/**
|
|
59
|
+ * Transcribe network-layer address
|
|
60
|
+ *
|
|
61
|
+ * @v net_addr Network-layer address
|
|
62
|
+ * @ret string Human-readable transcription of address
|
|
63
|
+ */
|
|
64
|
+static const char * lotest_ntoa ( const void *net_addr __unused ) {
|
|
65
|
+ return "<INVALID>";
|
|
66
|
+}
|
|
67
|
+
|
|
68
|
+/**
|
|
69
|
+ * Loopback test network-layer protocol
|
|
70
|
+ *
|
|
71
|
+ * Using a dedicated network-layer protocol avoids problems caused by
|
|
72
|
+ * cards supporting features such as IPv4 checksum offload trying to
|
|
73
|
+ * interpret the (randomly generated) network-layer content.
|
|
74
|
+ */
|
|
75
|
+static struct net_protocol lotest_protocol __net_protocol = {
|
|
76
|
+ .name = "LOTEST",
|
|
77
|
+ .rx = lotest_rx,
|
|
78
|
+ .ntoa = lotest_ntoa,
|
|
79
|
+ .net_proto = htons ( 0x6950 ), /* Not a genuine protocol number */
|
|
80
|
+ .net_addr_len = 0,
|
|
81
|
+};
|
|
82
|
+
|
|
83
|
+/**
|
|
84
|
+ * Perform loopback test between two network devices
|
|
85
|
+ *
|
|
86
|
+ * @v sender Sending network device
|
|
87
|
+ * @v receiver Received network device
|
|
88
|
+ * @v mtu Packet size (excluding link-layer headers)
|
|
89
|
+ * @ret rc Return status code
|
|
90
|
+ */
|
|
91
|
+int loopback_test ( struct net_device *sender, struct net_device *receiver,
|
|
92
|
+ size_t mtu ) {
|
|
93
|
+ uint8_t buf[mtu];
|
|
94
|
+ struct io_buffer *iobuf;
|
|
95
|
+ const void *ll_dest;
|
|
96
|
+ const void *ll_source;
|
|
97
|
+ uint16_t net_proto;
|
|
98
|
+ unsigned int i;
|
|
99
|
+ unsigned int successes;
|
|
100
|
+ int rc;
|
|
101
|
+
|
|
102
|
+ /* Open network devices */
|
|
103
|
+ if ( ( rc = ifopen ( sender ) ) != 0 )
|
|
104
|
+ return rc;
|
|
105
|
+ if ( ( rc = ifopen ( receiver ) ) != 0 )
|
|
106
|
+ return rc;
|
|
107
|
+
|
|
108
|
+ /* Wait for link-up */
|
|
109
|
+ if ( ( rc = iflinkwait ( sender, LINK_WAIT_MS ) ) != 0 )
|
|
110
|
+ return rc;
|
|
111
|
+ if ( ( rc = iflinkwait ( receiver, LINK_WAIT_MS ) ) != 0 )
|
|
112
|
+ return rc;
|
|
113
|
+
|
|
114
|
+ /* Print initial statistics */
|
|
115
|
+ printf ( "Performing loopback test from %s to %s with %zd byte MTU\n",
|
|
116
|
+ sender->name, receiver->name, mtu );
|
|
117
|
+ ifstat ( sender );
|
|
118
|
+ ifstat ( receiver );
|
|
119
|
+
|
|
120
|
+ /* Perform loopback test */
|
|
121
|
+ for ( successes = 0 ; ; successes++ ) {
|
|
122
|
+
|
|
123
|
+ /* Print running total */
|
|
124
|
+ printf ( "\r%d", successes );
|
|
125
|
+
|
|
126
|
+ /* Generate random packet */
|
|
127
|
+ for ( i = 0 ; i < sizeof ( buf ) ; i++ )
|
|
128
|
+ buf[i] = random();
|
|
129
|
+ iobuf = alloc_iob ( MAX_LL_HEADER_LEN + sizeof ( buf ) );
|
|
130
|
+ if ( ! iobuf ) {
|
|
131
|
+ printf ( "\nFailed to allocate I/O buffer" );
|
|
132
|
+ rc = -ENOMEM;
|
|
133
|
+ goto done;
|
|
134
|
+ }
|
|
135
|
+ iob_reserve ( iobuf, MAX_LL_HEADER_LEN );
|
|
136
|
+ memcpy ( iob_put ( iobuf, sizeof ( buf ) ),
|
|
137
|
+ buf, sizeof ( buf ) );
|
|
138
|
+
|
|
139
|
+ /* Transmit packet */
|
|
140
|
+ if ( ( rc = net_tx ( iob_disown ( iobuf ), sender,
|
|
141
|
+ &lotest_protocol,
|
|
142
|
+ receiver->ll_addr ) ) != 0 ) {
|
|
143
|
+ printf ( "\nFailed to transmit packet: %s",
|
|
144
|
+ strerror ( rc ) );
|
|
145
|
+ goto done;
|
|
146
|
+ }
|
|
147
|
+
|
|
148
|
+ /* Poll until packet arrives */
|
|
149
|
+ do {
|
|
150
|
+ /* Check for cancellation */
|
|
151
|
+ if ( iskey() && ( getchar() == CTRL_C ) ) {
|
|
152
|
+ rc = -ECANCELED;
|
|
153
|
+ goto done;
|
|
154
|
+ }
|
|
155
|
+ /* Poll network devices */
|
|
156
|
+ netdev_poll ( sender );
|
|
157
|
+ netdev_poll ( receiver );
|
|
158
|
+ } while ( ( iobuf = netdev_rx_dequeue ( receiver ) ) == NULL );
|
|
159
|
+
|
|
160
|
+ /* Check received packet */
|
|
161
|
+ if ( ( rc = receiver->ll_protocol->pull ( receiver, iobuf,
|
|
162
|
+ &ll_dest, &ll_source,
|
|
163
|
+ &net_proto ) ) != 0 ){
|
|
164
|
+ printf ( "\nFailed to strip link-layer header: %s",
|
|
165
|
+ strerror ( rc ) );
|
|
166
|
+ goto done;
|
|
167
|
+ }
|
|
168
|
+ if ( net_proto == lotest_protocol.net_proto ) {
|
|
169
|
+ if ( iob_len ( iobuf ) != sizeof ( buf ) ) {
|
|
170
|
+ printf ( "\nLength mismatch: sent %zd, "
|
|
171
|
+ "received %zd",
|
|
172
|
+ sizeof ( buf ), iob_len ( iobuf ) );
|
|
173
|
+ DBG ( "\nSent:\n" );
|
|
174
|
+ DBG_HDA ( 0, buf, sizeof ( buf ) );
|
|
175
|
+ DBG ( "Received:\n" );
|
|
176
|
+ DBG_HDA ( 0, iobuf->data, iob_len ( iobuf ) );
|
|
177
|
+ rc = -EINVAL;
|
|
178
|
+ goto done;
|
|
179
|
+ }
|
|
180
|
+ if ( memcmp ( iobuf->data, buf, sizeof ( buf ) ) != 0){
|
|
181
|
+ printf ( "\nContent mismatch" );
|
|
182
|
+ DBG ( "\nSent:\n" );
|
|
183
|
+ DBG_HDA ( 0, buf, sizeof ( buf ) );
|
|
184
|
+ DBG ( "Received:\n" );
|
|
185
|
+ DBG_HDA ( 0, iobuf->data, iob_len ( iobuf ) );
|
|
186
|
+ rc = -EINVAL;
|
|
187
|
+ goto done;
|
|
188
|
+ }
|
|
189
|
+ } else {
|
|
190
|
+ printf ( "\nReceived spurious packet type %04x\n",
|
|
191
|
+ net_proto );
|
|
192
|
+ /* Continue; this allows for the fact that
|
|
193
|
+ * there may have been packets outstanding on
|
|
194
|
+ * the wire when we started the test.
|
|
195
|
+ */
|
|
196
|
+ }
|
|
197
|
+
|
|
198
|
+ free_iob ( iob_disown ( iobuf ) );
|
|
199
|
+ }
|
|
200
|
+
|
|
201
|
+ done:
|
|
202
|
+ printf ( "\n");
|
|
203
|
+ free_iob ( iobuf );
|
|
204
|
+
|
|
205
|
+ /* Dump final statistics */
|
|
206
|
+ ifstat ( sender );
|
|
207
|
+ ifstat ( receiver );
|
|
208
|
+
|
|
209
|
+ return 0;
|
|
210
|
+}
|