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 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. /** Positive zero data */
  83. TCPIP_TEST ( positive_zero, DATA ( 0x00, 0x00 ) );
  84. /** Negative zero data */
  85. TCPIP_TEST ( negative_zero, DATA ( 0xff, 0xff ) );
  86. /** Final wrap-around carry (big-endian) */
  87. TCPIP_TEST ( final_carry_big,
  88. DATA ( 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 ) );
  89. /** Final wrap-around carry (little-endian) */
  90. TCPIP_TEST ( final_carry_little,
  91. DATA ( 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00 ) );
  92. /** Random data (aligned) */
  93. TCPIP_RANDOM_TEST ( random_aligned, 0x12345678UL, 4096, 0 );
  94. /** Random data (unaligned, +1) */
  95. TCPIP_RANDOM_TEST ( random_unaligned_1, 0x12345678UL, 4096, 1 );
  96. /** Random data (unaligned, +2) */
  97. TCPIP_RANDOM_TEST ( random_unaligned_2, 0x12345678UL, 4096, 2 );
  98. /** Random data (aligned, truncated) */
  99. TCPIP_RANDOM_TEST ( random_aligned_truncated, 0x12345678UL, 4095, 0 );
  100. /** Random data (unaligned start and finish) */
  101. TCPIP_RANDOM_TEST ( partial, 0xcafebabe, 121, 5 );
  102. /**
  103. * Calculate TCP/IP checksum
  104. *
  105. * @v data Data to sum
  106. * @v len Length of data
  107. * @ret cksum Checksum
  108. *
  109. * This is a reference implementation taken from RFC1071 (and modified
  110. * to fix compilation without warnings under gcc).
  111. *
  112. * The initial value of the one's complement @c sum is changed from
  113. * positive zero (0x0000) to negative zero (0xffff). This ensures
  114. * that the return value will always use the positive representation
  115. * of zero (0x0000). Without this change, the return value would use
  116. * negative zero (0xffff) if the input data is zero length (or all
  117. * zeros) but positive zero (0x0000) for any other data which sums to
  118. * zero.
  119. */
  120. static uint16_t rfc_tcpip_chksum ( const void *data, size_t len ) {
  121. unsigned long sum = 0xffff;
  122. while ( len > 1 ) {
  123. sum += *( ( uint16_t * ) data );
  124. data += 2;
  125. len -= 2;
  126. }
  127. if ( len > 0 )
  128. sum += *( ( uint8_t * ) data );
  129. while ( sum >> 16 )
  130. sum = ( ( sum & 0xffff ) + ( sum >> 16 ) );
  131. assert ( sum != 0x0000 );
  132. return ~sum;
  133. }
  134. /**
  135. * Report TCP/IP fixed-data test result
  136. *
  137. * @v test TCP/IP test
  138. * @v file Test code file
  139. * @v line Test code line
  140. */
  141. static void tcpip_okx ( struct tcpip_test *test, const char *file,
  142. unsigned int line ) {
  143. uint16_t expected;
  144. uint16_t generic_sum;
  145. uint16_t sum;
  146. /* Verify generic_tcpip_continue_chksum() result */
  147. expected = rfc_tcpip_chksum ( test->data, test->len );
  148. generic_sum = generic_tcpip_continue_chksum ( TCPIP_EMPTY_CSUM,
  149. test->data, test->len );
  150. okx ( generic_sum == expected, file, line );
  151. /* Verify optimised tcpip_continue_chksum() result */
  152. sum = tcpip_continue_chksum ( TCPIP_EMPTY_CSUM, test->data, test->len );
  153. okx ( sum == expected, file, line );
  154. }
  155. #define tcpip_ok( test ) tcpip_okx ( test, __FILE__, __LINE__ )
  156. /**
  157. * Report TCP/IP pseudorandom-data test result
  158. *
  159. * @v test TCP/IP test
  160. * @v file Test code file
  161. * @v line Test code line
  162. */
  163. static void tcpip_random_okx ( struct tcpip_random_test *test,
  164. const char *file, unsigned int line ) {
  165. uint8_t *data = ( tcpip_data + test->offset );
  166. struct profiler profiler;
  167. uint16_t expected;
  168. uint16_t generic_sum;
  169. uint16_t sum;
  170. unsigned int i;
  171. /* Sanity check */
  172. assert ( ( test->len + test->offset ) <= sizeof ( tcpip_data ) );
  173. /* Generate random data */
  174. srandom ( test->seed );
  175. for ( i = 0 ; i < test->len ; i++ )
  176. data[i] = random();
  177. /* Verify generic_tcpip_continue_chksum() result */
  178. expected = rfc_tcpip_chksum ( data, test->len );
  179. generic_sum = generic_tcpip_continue_chksum ( TCPIP_EMPTY_CSUM,
  180. data, test->len );
  181. okx ( generic_sum == expected, file, line );
  182. /* Verify optimised tcpip_continue_chksum() result */
  183. sum = tcpip_continue_chksum ( TCPIP_EMPTY_CSUM, data, test->len );
  184. okx ( sum == expected, file, line );
  185. /* Profile optimised calculation */
  186. memset ( &profiler, 0, sizeof ( profiler ) );
  187. for ( i = 0 ; i < PROFILE_COUNT ; i++ ) {
  188. profile_start ( &profiler );
  189. sum = tcpip_continue_chksum ( TCPIP_EMPTY_CSUM, data,
  190. test->len );
  191. profile_stop ( &profiler );
  192. }
  193. DBG ( "TCPIP checksummed %zd bytes (+%zd) in %ld +/- %ld ticks\n",
  194. test->len, test->offset, profile_mean ( &profiler ),
  195. profile_stddev ( &profiler ) );
  196. }
  197. #define tcpip_random_ok( test ) tcpip_random_okx ( test, __FILE__, __LINE__ )
  198. /**
  199. * Perform TCP/IP self-tests
  200. *
  201. */
  202. static void tcpip_test_exec ( void ) {
  203. tcpip_ok ( &empty );
  204. tcpip_ok ( &one_byte );
  205. tcpip_ok ( &two_bytes );
  206. tcpip_ok ( &positive_zero );
  207. tcpip_ok ( &negative_zero );
  208. tcpip_ok ( &final_carry_big );
  209. tcpip_ok ( &final_carry_little );
  210. tcpip_random_ok ( &random_aligned );
  211. tcpip_random_ok ( &random_unaligned_1 );
  212. tcpip_random_ok ( &random_unaligned_2 );
  213. tcpip_random_ok ( &random_aligned_truncated );
  214. tcpip_random_ok ( &partial );
  215. }
  216. /** TCP/IP self-test */
  217. struct self_test tcpip_test __self_test = {
  218. .name = "tcpip",
  219. .exec = tcpip_test_exec,
  220. };