From ac0c70f74a5badbebec721c2be0602ea98c0437b Mon Sep 17 00:00:00 2001 From: Andrey Konovalov Date: Thu, 27 Apr 2017 20:31:00 +0200 Subject: prog, executor: move checksum computation to executor This commit moves checksum computation to executor. This will allow to embed dynamically generated values (like TCP sequence numbers) into packets. --- executor/test_executor.cc | 168 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 168 insertions(+) (limited to 'executor/test_executor.cc') diff --git a/executor/test_executor.cc b/executor/test_executor.cc index 79b951f62..5cf404675 100644 --- a/executor/test_executor.cc +++ b/executor/test_executor.cc @@ -24,6 +24,174 @@ extern "C" int test_copyin() return 0; } +#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) + +struct csum_inet_test { + const char* data; + size_t length; + uint16_t csum; +}; + +extern "C" int test_csum_inet() +{ + struct csum_inet_test tests[] = { + {// 0 + "", + 0, + 0xffff}, + { + // 1 + "\x00", + 1, + 0xffff, + }, + { + // 2 + "\x00\x00", + 2, + 0xffff, + }, + { + // 3 + "\x00\x00\xff\xff", + 4, + 0x0000, + }, + { + // 4 + "\xfc", + 1, + 0xff03, + }, + { + // 5 + "\xfc\x12", + 2, + 0xed03, + }, + { + // 6 + "\xfc\x12\x3e", + 3, + 0xecc5, + }, + { + // 7 + "\xfc\x12\x3e\x00\xc5\xec", + 6, + 0x0000, + }, + { + // 8 + "\x42\x00\x00\x43\x44\x00\x00\x00\x45\x00\x00\x00\xba\xaa\xbb\xcc\xdd", + 17, + 0x43e1, + }, + { + // 9 + "\x42\x00\x00\x43\x44\x00\x00\x00\x45\x00\x00\x00\xba\xaa\xbb\xcc\xdd\x00", + 18, + 0x43e1, + }, + { + // 10 + "\x00\x00\x42\x00\x00\x43\x44\x00\x00\x00\x45\x00\x00\x00\xba\xaa\xbb\xcc\xdd", + 19, + 0x43e1, + }, + { + // 11 + "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\xab\xcd", + 15, + 0x5032, + }, + { + // 12 + "\x00\x00\x12\x34\x56\x78", + 6, + 0x5397, + }, + { + // 13 + "\x00\x00\x12\x34\x00\x00\x56\x78\x00\x06\x00\x04\xab\xcd", + 14, + 0x7beb, + }, + { + // 14 + "\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\xff\xee\xdd\xcc\xbb\xaa\x99\x88\x77\x66\x55\x44\x33\x22\x11\x00\x00\x00\x00\x04\x00\x00\x00\x06\x00\x00\xab\xcd", + 44, + 0x2854, + }, + { + // 15 + "\x00\x00\x12\x34\x00\x00\x56\x78\x00\x11\x00\x04\xab\xcd", + 14, + 0x70eb, + }, + { + // 16 + "\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\xff\xee\xdd\xcc\xbb\xaa\x99\x88\x77\x66\x55\x44\x33\x22\x11\x00\x00\x00\x00\x04\x00\x00\x00\x11\x00\x00\xab\xcd", + 44, + 0x1d54, + }, + { + // 17 + "\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff\xff\xee\xdd\xcc\xbb\xaa\x99\x88\x77\x66\x55\x44\x33\x22\x11\x00\x00\x00\x00\x04\x00\x00\x00\x3a\x00\x00\xab\xcd", + 44, + 0xf453, + }}; + + int i; + for (i = 0; i < ARRAY_SIZE(tests); i++) { + struct csum_inet csum; + csum_inet_init(&csum); + csum_inet_update(&csum, (const uint8_t*)tests[i].data, tests[i].length); + if (csum_inet_digest(&csum) != tests[i].csum) { + fprintf(stderr, "bad checksum in test #%d, want: %hx, got: %hx\n", i, tests[i].csum, csum_inet_digest(&csum)); + return 1; + } + } + + return 0; +} + +int randInt(int start, int end) +{ + return rand() % (end + 1 - start) + start; +} + +extern "C" int test_csum_inet_acc() +{ + uint8_t buffer[128]; + + int test; + for (test = 0; test < 256; test++) { + int size = randInt(1, 128); + int step = randInt(1, 8) * 2; + + int i; + for (i = 0; i < size; i++) + buffer[i] = randInt(0, 255); + + struct csum_inet csum_acc; + csum_inet_init(&csum_acc); + + for (i = 0; i < size / step; i++) + csum_inet_update(&csum_acc, &buffer[i * step], step); + if (size % step != 0) + csum_inet_update(&csum_acc, &buffer[size - size % step], size % step); + + struct csum_inet csum; + csum_inet_init(&csum); + csum_inet_update(&csum, &buffer[0], size); + + if (csum_inet_digest(&csum_acc) != csum_inet_digest(&csum)) + return 1; + return 0; + } +} + static unsigned host_kernel_version(); static void dump_cpu_state(int cpufd, char* vm_mem); -- cgit mrf-deployment