Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / tests / tcpip_test.c
1 /*
2  * Copyright (C) 2012 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 (at your option) 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
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 /** @file
23  *
24  * TCP/IP self-tests
25  *
26  */
27
28 /* Forcibly enable assertions */
29 #undef NDEBUG
30
31 #include <stdint.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <assert.h>
35 #include <ipxe/test.h>
36 #include <ipxe/profile.h>
37 #include <ipxe/tcpip.h>
38
39 /** Number of sample iterations for profiling */
40 #define PROFILE_COUNT 16
41
42 /** A TCP/IP fixed-data test */
43 struct tcpip_test {
44         /** Data */
45         const void *data;
46         /** Length of data */
47         size_t len;
48 };
49
50 /** A TCP/IP pseudorandom-data test */
51 struct tcpip_random_test {
52         /** Seed */
53         unsigned int seed;
54         /** Length of data */
55         size_t len;
56         /** Alignment offset */
57         size_t offset;
58 };
59
60 /** Define inline data */
61 #define DATA(...) { __VA_ARGS__ }
62
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 ),                        \
70         }
71
72 /** Define a TCP/IP pseudorandom-data test */
73 #define TCPIP_RANDOM_TEST( name, SEED, LEN, OFFSET )                    \
74         static struct tcpip_random_test name = {                        \
75                 .seed = SEED,                                           \
76                 .len = LEN,                                             \
77                 .offset = OFFSET,                                       \
78         }
79
80 /** Buffer for pseudorandom-data tests */
81 static uint8_t __attribute__ (( aligned ( 16 ) ))
82         tcpip_data[ 4096 + 7 /* offset */ ];
83
84 /** Empty data */
85 TCPIP_TEST ( empty, DATA() );
86
87 /** Single byte */
88 TCPIP_TEST ( one_byte, DATA ( 0xeb ) );
89
90 /** Double byte */
91 TCPIP_TEST ( two_bytes, DATA ( 0xba, 0xbe ) );
92
93 /** Final wrap-around carry (big-endian) */
94 TCPIP_TEST ( final_carry_big,
95              DATA ( 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 ) );
96
97 /** Final wrap-around carry (little-endian) */
98 TCPIP_TEST ( final_carry_little,
99              DATA ( 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00 ) );
100
101 /** Random data (aligned) */
102 TCPIP_RANDOM_TEST ( random_aligned, 0x12345678UL, 4096, 0 );
103
104 /** Random data (unaligned, +1) */
105 TCPIP_RANDOM_TEST ( random_unaligned_1, 0x12345678UL, 4096, 1 );
106
107 /** Random data (unaligned, +2) */
108 TCPIP_RANDOM_TEST ( random_unaligned_2, 0x12345678UL, 4096, 2 );
109
110 /** Random data (aligned, truncated) */
111 TCPIP_RANDOM_TEST ( random_aligned_truncated, 0x12345678UL, 4095, 0 );
112
113 /** Random data (unaligned start and finish) */
114 TCPIP_RANDOM_TEST ( partial, 0xcafebabe, 121, 5 );
115
116 /**
117  * Calculate TCP/IP checksum
118  *
119  * @v data              Data to sum
120  * @v len               Length of data
121  * @ret cksum           Checksum
122  *
123  * This is a reference implementation taken from RFC1071 (and modified
124  * to fix compilation without warnings under gcc).
125  */
126 static uint16_t rfc_tcpip_chksum ( const void *data, size_t len ) {
127         unsigned long sum = 0;
128
129         while ( len > 1 )  {
130                 sum += *( ( uint16_t * ) data );
131                 data += 2;
132                 len -= 2;
133         }
134
135         if ( len > 0 )
136                 sum += *( ( uint8_t * ) data );
137
138         while ( sum >> 16 )
139                 sum = ( ( sum & 0xffff ) + ( sum >> 16 ) );
140
141         return ~sum;
142 }
143
144 /**
145  * Report TCP/IP fixed-data test result
146  *
147  * @v test              TCP/IP test
148  * @v file              Test code file
149  * @v line              Test code line
150  */
151 static void tcpip_okx ( struct tcpip_test *test, const char *file,
152                         unsigned int line ) {
153         uint16_t expected;
154         uint16_t generic_sum;
155         uint16_t sum;
156
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 );
162
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 );
166 }
167 #define tcpip_ok( test ) tcpip_okx ( test, __FILE__, __LINE__ )
168
169 /**
170  * Report TCP/IP pseudorandom-data test result
171  *
172  * @v test              TCP/IP test
173  * @v file              Test code file
174  * @v line              Test code line
175  */
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;
180         uint16_t expected;
181         uint16_t generic_sum;
182         uint16_t sum;
183         unsigned int i;
184
185         /* Sanity check */
186         assert ( ( test->len + test->offset ) <= sizeof ( tcpip_data ) );
187
188         /* Generate random data */
189         srandom ( test->seed );
190         for ( i = 0 ; i < test->len ; i++ )
191                 data[i] = random();
192
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,
196                                                       data, test->len );
197         okx ( generic_sum == expected, file, line );
198
199         /* Verify optimised tcpip_continue_chksum() result */
200         sum = tcpip_continue_chksum ( TCPIP_EMPTY_CSUM, data, test->len );
201         okx ( sum == expected, file, line );
202
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,
208                                               test->len );
209                 profile_stop ( &profiler );
210         }
211         DBG ( "TCPIP checksummed %zd bytes (+%zd) in %ld +/- %ld ticks\n",
212               test->len, test->offset, profile_mean ( &profiler ),
213               profile_stddev ( &profiler ) );
214 }
215 #define tcpip_random_ok( test ) tcpip_random_okx ( test, __FILE__, __LINE__ )
216
217 /**
218  * Perform TCP/IP self-tests
219  *
220  */
221 static void tcpip_test_exec ( void ) {
222
223         tcpip_ok ( &empty );
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 );
233 }
234
235 /** TCP/IP self-test */
236 struct self_test tcpip_test __self_test = {
237         .name = "tcpip",
238         .exec = tcpip_test_exec,
239 };