You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tcpip_test.c 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /** @file
  25. *
  26. * TCP/IP self-tests
  27. *
  28. */
  29. /* Forcibly enable assertions */
  30. #undef NDEBUG
  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. /** Number of sample iterations for profiling */
  39. #define PROFILE_COUNT 16
  40. /** A TCP/IP fixed-data test */
  41. struct tcpip_test {
  42. /** Data */
  43. const void *data;
  44. /** Length of data */
  45. size_t len;
  46. };
  47. /** A TCP/IP pseudorandom-data test */
  48. struct tcpip_random_test {
  49. /** Seed */
  50. unsigned int seed;
  51. /** Length of data */
  52. size_t len;
  53. /** Alignment offset */
  54. size_t offset;
  55. };
  56. /** Define inline data */
  57. #define DATA(...) { __VA_ARGS__ }
  58. /** Define a TCP/IP fixed-data test */
  59. #define TCPIP_TEST( name, DATA ) \
  60. static const uint8_t __attribute__ (( aligned ( 16 ) )) \
  61. name ## _data[] = DATA; \
  62. static struct tcpip_test name = { \
  63. .data = name ## _data, \
  64. .len = sizeof ( name ## _data ), \
  65. }
  66. /** Define a TCP/IP pseudorandom-data test */
  67. #define TCPIP_RANDOM_TEST( name, SEED, LEN, OFFSET ) \
  68. static struct tcpip_random_test name = { \
  69. .seed = SEED, \
  70. .len = LEN, \
  71. .offset = OFFSET, \
  72. }
  73. /** Buffer for pseudorandom-data tests */
  74. static uint8_t __attribute__ (( aligned ( 16 ) ))
  75. tcpip_data[ 4096 + 7 /* offset */ ];
  76. /** Empty data */
  77. TCPIP_TEST ( empty, DATA() );
  78. /** Single byte */
  79. TCPIP_TEST ( one_byte, DATA ( 0xeb ) );
  80. /** Double byte */
  81. TCPIP_TEST ( two_bytes, DATA ( 0xba, 0xbe ) );
  82. /** Final wrap-around carry (big-endian) */
  83. TCPIP_TEST ( final_carry_big,
  84. DATA ( 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 ) );
  85. /** Final wrap-around carry (little-endian) */
  86. TCPIP_TEST ( final_carry_little,
  87. DATA ( 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00 ) );
  88. /** Random data (aligned) */
  89. TCPIP_RANDOM_TEST ( random_aligned, 0x12345678UL, 4096, 0 );
  90. /** Random data (unaligned, +1) */
  91. TCPIP_RANDOM_TEST ( random_unaligned_1, 0x12345678UL, 4096, 1 );
  92. /** Random data (unaligned, +2) */
  93. TCPIP_RANDOM_TEST ( random_unaligned_2, 0x12345678UL, 4096, 2 );
  94. /** Random data (aligned, truncated) */
  95. TCPIP_RANDOM_TEST ( random_aligned_truncated, 0x12345678UL, 4095, 0 );
  96. /** Random data (unaligned start and finish) */
  97. TCPIP_RANDOM_TEST ( partial, 0xcafebabe, 121, 5 );
  98. /**
  99. * Calculate TCP/IP checksum
  100. *
  101. * @v data Data to sum
  102. * @v len Length of data
  103. * @ret cksum Checksum
  104. *
  105. * This is a reference implementation taken from RFC1071 (and modified
  106. * to fix compilation without warnings under gcc).
  107. */
  108. static uint16_t rfc_tcpip_chksum ( const void *data, size_t len ) {
  109. unsigned long sum = 0;
  110. while ( len > 1 ) {
  111. sum += *( ( uint16_t * ) data );
  112. data += 2;
  113. len -= 2;
  114. }
  115. if ( len > 0 )
  116. sum += *( ( uint8_t * ) data );
  117. while ( sum >> 16 )
  118. sum = ( ( sum & 0xffff ) + ( sum >> 16 ) );
  119. return ~sum;
  120. }
  121. /**
  122. * Report TCP/IP fixed-data test result
  123. *
  124. * @v test TCP/IP test
  125. * @v file Test code file
  126. * @v line Test code line
  127. */
  128. static void tcpip_okx ( struct tcpip_test *test, const char *file,
  129. unsigned int line ) {
  130. uint16_t expected;
  131. uint16_t generic_sum;
  132. uint16_t sum;
  133. /* Verify generic_tcpip_continue_chksum() result */
  134. expected = rfc_tcpip_chksum ( test->data, test->len );
  135. generic_sum = generic_tcpip_continue_chksum ( TCPIP_EMPTY_CSUM,
  136. test->data, test->len );
  137. okx ( generic_sum == expected, file, line );
  138. /* Verify optimised tcpip_continue_chksum() result */
  139. sum = tcpip_continue_chksum ( TCPIP_EMPTY_CSUM, test->data, test->len );
  140. okx ( sum == expected, file, line );
  141. }
  142. #define tcpip_ok( test ) tcpip_okx ( test, __FILE__, __LINE__ )
  143. /**
  144. * Report TCP/IP pseudorandom-data test result
  145. *
  146. * @v test TCP/IP test
  147. * @v file Test code file
  148. * @v line Test code line
  149. */
  150. static void tcpip_random_okx ( struct tcpip_random_test *test,
  151. const char *file, unsigned int line ) {
  152. uint8_t *data = ( tcpip_data + test->offset );
  153. struct profiler profiler;
  154. uint16_t expected;
  155. uint16_t generic_sum;
  156. uint16_t sum;
  157. unsigned int i;
  158. /* Sanity check */
  159. assert ( ( test->len + test->offset ) <= sizeof ( tcpip_data ) );
  160. /* Generate random data */
  161. srandom ( test->seed );
  162. for ( i = 0 ; i < test->len ; i++ )
  163. data[i] = random();
  164. /* Verify generic_tcpip_continue_chksum() result */
  165. expected = rfc_tcpip_chksum ( data, test->len );
  166. generic_sum = generic_tcpip_continue_chksum ( TCPIP_EMPTY_CSUM,
  167. data, test->len );
  168. okx ( generic_sum == expected, file, line );
  169. /* Verify optimised tcpip_continue_chksum() result */
  170. sum = tcpip_continue_chksum ( TCPIP_EMPTY_CSUM, data, test->len );
  171. okx ( sum == expected, file, line );
  172. /* Profile optimised calculation */
  173. memset ( &profiler, 0, sizeof ( profiler ) );
  174. for ( i = 0 ; i < PROFILE_COUNT ; i++ ) {
  175. profile_start ( &profiler );
  176. sum = tcpip_continue_chksum ( TCPIP_EMPTY_CSUM, data,
  177. test->len );
  178. profile_stop ( &profiler );
  179. }
  180. DBG ( "TCPIP checksummed %zd bytes (+%zd) in %ld +/- %ld ticks\n",
  181. test->len, test->offset, profile_mean ( &profiler ),
  182. profile_stddev ( &profiler ) );
  183. }
  184. #define tcpip_random_ok( test ) tcpip_random_okx ( test, __FILE__, __LINE__ )
  185. /**
  186. * Perform TCP/IP self-tests
  187. *
  188. */
  189. static void tcpip_test_exec ( void ) {
  190. tcpip_ok ( &empty );
  191. tcpip_ok ( &one_byte );
  192. tcpip_ok ( &two_bytes );
  193. tcpip_ok ( &final_carry_big );
  194. tcpip_ok ( &final_carry_little );
  195. tcpip_random_ok ( &random_aligned );
  196. tcpip_random_ok ( &random_unaligned_1 );
  197. tcpip_random_ok ( &random_unaligned_2 );
  198. tcpip_random_ok ( &random_aligned_truncated );
  199. tcpip_random_ok ( &partial );
  200. }
  201. /** TCP/IP self-test */
  202. struct self_test tcpip_test __self_test = {
  203. .name = "tcpip",
  204. .exec = tcpip_test_exec,
  205. };