Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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