2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
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 (at your option) any later version.
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.
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
20 FILE_LICENCE ( GPL2_OR_LATER );
28 /* Forcibly enable assertions */
35 #include <ipxe/test.h>
36 #include <ipxe/profile.h>
37 #include <ipxe/tcpip.h>
39 /** Number of sample iterations for profiling */
40 #define PROFILE_COUNT 16
42 /** A TCP/IP fixed-data test */
50 /** A TCP/IP pseudorandom-data test */
51 struct tcpip_random_test {
56 /** Alignment offset */
60 /** Define inline data */
61 #define DATA(...) { __VA_ARGS__ }
63 /** Define a TCP/IP fixed-data test */
64 #define TCPIP_TEST( name, DATA ) \
65 static const uint8_t __attribute__ (( aligned ( 16 ) )) \
66 name ## _data[] = DATA; \
67 static struct tcpip_test name = { \
68 .data = name ## _data, \
69 .len = sizeof ( name ## _data ), \
72 /** Define a TCP/IP pseudorandom-data test */
73 #define TCPIP_RANDOM_TEST( name, SEED, LEN, OFFSET ) \
74 static struct tcpip_random_test name = { \
80 /** Buffer for pseudorandom-data tests */
81 static uint8_t __attribute__ (( aligned ( 16 ) ))
82 tcpip_data[ 4096 + 7 /* offset */ ];
85 TCPIP_TEST ( empty, DATA() );
88 TCPIP_TEST ( one_byte, DATA ( 0xeb ) );
91 TCPIP_TEST ( two_bytes, DATA ( 0xba, 0xbe ) );
93 /** Final wrap-around carry (big-endian) */
94 TCPIP_TEST ( final_carry_big,
95 DATA ( 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 ) );
97 /** Final wrap-around carry (little-endian) */
98 TCPIP_TEST ( final_carry_little,
99 DATA ( 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00 ) );
101 /** Random data (aligned) */
102 TCPIP_RANDOM_TEST ( random_aligned, 0x12345678UL, 4096, 0 );
104 /** Random data (unaligned, +1) */
105 TCPIP_RANDOM_TEST ( random_unaligned_1, 0x12345678UL, 4096, 1 );
107 /** Random data (unaligned, +2) */
108 TCPIP_RANDOM_TEST ( random_unaligned_2, 0x12345678UL, 4096, 2 );
110 /** Random data (aligned, truncated) */
111 TCPIP_RANDOM_TEST ( random_aligned_truncated, 0x12345678UL, 4095, 0 );
113 /** Random data (unaligned start and finish) */
114 TCPIP_RANDOM_TEST ( partial, 0xcafebabe, 121, 5 );
117 * Calculate TCP/IP checksum
119 * @v data Data to sum
120 * @v len Length of data
121 * @ret cksum Checksum
123 * This is a reference implementation taken from RFC1071 (and modified
124 * to fix compilation without warnings under gcc).
126 static uint16_t rfc_tcpip_chksum ( const void *data, size_t len ) {
127 unsigned long sum = 0;
130 sum += *( ( uint16_t * ) data );
136 sum += *( ( uint8_t * ) data );
139 sum = ( ( sum & 0xffff ) + ( sum >> 16 ) );
145 * Report TCP/IP fixed-data test result
147 * @v test TCP/IP test
148 * @v file Test code file
149 * @v line Test code line
151 static void tcpip_okx ( struct tcpip_test *test, const char *file,
152 unsigned int line ) {
154 uint16_t generic_sum;
157 /* Verify generic_tcpip_continue_chksum() result */
158 expected = rfc_tcpip_chksum ( test->data, test->len );
159 generic_sum = generic_tcpip_continue_chksum ( TCPIP_EMPTY_CSUM,
160 test->data, test->len );
161 okx ( generic_sum == expected, file, line );
163 /* Verify optimised tcpip_continue_chksum() result */
164 sum = tcpip_continue_chksum ( TCPIP_EMPTY_CSUM, test->data, test->len );
165 okx ( sum == expected, file, line );
167 #define tcpip_ok( test ) tcpip_okx ( test, __FILE__, __LINE__ )
170 * Report TCP/IP pseudorandom-data test result
172 * @v test TCP/IP test
173 * @v file Test code file
174 * @v line Test code line
176 static void tcpip_random_okx ( struct tcpip_random_test *test,
177 const char *file, unsigned int line ) {
178 uint8_t *data = ( tcpip_data + test->offset );
179 struct profiler profiler;
181 uint16_t generic_sum;
186 assert ( ( test->len + test->offset ) <= sizeof ( tcpip_data ) );
188 /* Generate random data */
189 srandom ( test->seed );
190 for ( i = 0 ; i < test->len ; i++ )
193 /* Verify generic_tcpip_continue_chksum() result */
194 expected = rfc_tcpip_chksum ( data, test->len );
195 generic_sum = generic_tcpip_continue_chksum ( TCPIP_EMPTY_CSUM,
197 okx ( generic_sum == expected, file, line );
199 /* Verify optimised tcpip_continue_chksum() result */
200 sum = tcpip_continue_chksum ( TCPIP_EMPTY_CSUM, data, test->len );
201 okx ( sum == expected, file, line );
203 /* Profile optimised calculation */
204 memset ( &profiler, 0, sizeof ( profiler ) );
205 for ( i = 0 ; i < PROFILE_COUNT ; i++ ) {
206 profile_start ( &profiler );
207 sum = tcpip_continue_chksum ( TCPIP_EMPTY_CSUM, data,
209 profile_stop ( &profiler );
211 DBG ( "TCPIP checksummed %zd bytes (+%zd) in %ld +/- %ld ticks\n",
212 test->len, test->offset, profile_mean ( &profiler ),
213 profile_stddev ( &profiler ) );
215 #define tcpip_random_ok( test ) tcpip_random_okx ( test, __FILE__, __LINE__ )
218 * Perform TCP/IP self-tests
221 static void tcpip_test_exec ( void ) {
224 tcpip_ok ( &one_byte );
225 tcpip_ok ( &two_bytes );
226 tcpip_ok ( &final_carry_big );
227 tcpip_ok ( &final_carry_little );
228 tcpip_random_ok ( &random_aligned );
229 tcpip_random_ok ( &random_unaligned_1 );
230 tcpip_random_ok ( &random_unaligned_2 );
231 tcpip_random_ok ( &random_aligned_truncated );
232 tcpip_random_ok ( &partial );
235 /** TCP/IP self-test */
236 struct self_test tcpip_test __self_test = {
238 .exec = tcpip_test_exec,