From de37109767b6b415778f34cbac196c8418f7e371 Mon Sep 17 00:00:00 2001 From: Julius Werner Date: Tue, 30 Jan 2024 16:51:05 -0800 Subject: lib: Move IP checksum to commonlib This patch moves the IP checksum algorithm into commonlib to prepare for it being shared with libpayload. The current implementation is ancient and pretty hard to read (and does some unnecessary questionable things like the type-punning stuff which leads to suboptimal code generation), so this reimplements it from scratch (that also helps with the licensing). This algorithm is prepared to take in a pre-calculated "wide" checksum in a machine-register-sized data type which is then narrowed down to 16 bits (see RFC 1071 for why that's valid). This isn't used yet (and the code will get optimized out), but will be used later in this patch series for architecture-specific optimization. Change-Id: Ic04c714c00439a17fc04a8a6e730cc2aa19b8e68 Signed-off-by: Julius Werner Reviewed-on: https://review.coreboot.org/c/coreboot/+/80251 Reviewed-by: Yidi Lin Tested-by: build bot (Jenkins) Reviewed-by: Jakub Czapiga --- tests/commonlib/bsd/Makefile.mk | 4 ++ tests/commonlib/bsd/ipchksum-test.c | 99 ++++++++++++++++++++++++++++++++++++ tests/lib/Makefile.mk | 6 +-- tests/lib/compute_ip_checksum-test.c | 99 ------------------------------------ 4 files changed, 104 insertions(+), 104 deletions(-) create mode 100644 tests/commonlib/bsd/ipchksum-test.c delete mode 100644 tests/lib/compute_ip_checksum-test.c (limited to 'tests') diff --git a/tests/commonlib/bsd/Makefile.mk b/tests/commonlib/bsd/Makefile.mk index bf17b6d56dbb..3de223e70738 100644 --- a/tests/commonlib/bsd/Makefile.mk +++ b/tests/commonlib/bsd/Makefile.mk @@ -2,8 +2,12 @@ tests-y += helpers-test tests-y += gcd-test +tests-y += ipchksum-test helpers-test-srcs += tests/commonlib/bsd/helpers-test.c gcd-test-srcs += tests/commonlib/bsd/gcd-test.c gcd-test-srcs += src/commonlib/bsd/gcd.c + +ipchksum-test-srcs += tests/commonlib/bsd/ipchksum-test.c +ipchksum-test-srcs += src/commonlib/bsd/ipchksum.c diff --git a/tests/commonlib/bsd/ipchksum-test.c b/tests/commonlib/bsd/ipchksum-test.c new file mode 100644 index 000000000000..2aed94c0cf2d --- /dev/null +++ b/tests/commonlib/bsd/ipchksum-test.c @@ -0,0 +1,99 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include +#include +#include +#include +#include + +static const uint8_t test_data_simple[] = { + 0x64, 0x3b, 0x33, 0x17, 0x34, 0x74, 0x62, 0x30, 0x75, 0x73, 0xf3, 0x11, 0x30, 0x2c, + 0x34, 0x35, 0x6d, 0x39, 0x69, 0x32, 0x23, 0x24, 0x76, 0x71, 0x77, 0x30, 0x39, 0x75, + 0x76, 0x35, 0x71, 0x32, 0x40, 0x46, 0x34, 0x34, 0xBB, 0x03, 0x66, 0x52}; +static const size_t test_data_simple_sz = ARRAY_SIZE(test_data_simple); +static const uint16_t test_data_simple_checksum = 0x4267; + +static uint8_t test_data_zeros[1024]; +static const size_t test_data_zeros_sz = ARRAY_SIZE(test_data_zeros); +static const uint16_t test_data_zeros_checksum = 0xFFFF; + +static int setup_test_group(void **state) +{ + memset(test_data_zeros, 0, test_data_zeros_sz); + + return 0; +} + +static void test_ipchksum_zero_length(void **state) +{ + uint16_t res = ipchksum(test_data_simple, 0); + + /* Expect checksum to be in initial state as there are were no data provided. */ + assert_int_equal(0xFFFF, res); +} + +static void test_ipchksum_zero_buffer(void **state) +{ + uint16_t res = ipchksum(test_data_zeros, test_data_zeros_sz); + assert_int_equal(test_data_zeros_checksum, res); +} + +static void test_ipchksum_simple_data(void **state) +{ + uint16_t res; + uint16_t check_res; + const size_t helper_buffer_size = sizeof(uint8_t) * (test_data_simple_sz + 2); + char *helper_buffer = malloc(helper_buffer_size); + + /* Self test */ + assert_non_null(helper_buffer); + + /* Expect function to generate the same checksum as stored in */ + res = ipchksum(test_data_simple, test_data_simple_sz); + assert_int_equal(test_data_simple_checksum, res); + + /* Copy test data and checksum to new buffer. Expect computed checksum to be zero, + as it proves the data and the checksum are correct. */ + memcpy(helper_buffer, test_data_simple, test_data_simple_sz); + helper_buffer[helper_buffer_size - 2] = res & 0xFF; + helper_buffer[helper_buffer_size - 1] = (res >> 8) & 0xFF; + check_res = ipchksum(helper_buffer, helper_buffer_size); + assert_int_equal(0, check_res); + + free(helper_buffer); +} + +static void test_ipchksum_add_empty_values(void **state) +{ + uint16_t res; + + res = ipchksum_add(0, 0xFFFF, 0xFFFF); + assert_int_equal(0xFFFF, res); + + res = ipchksum_add(1, 0xFFFF, 0xFFFF); + assert_int_equal(0xFFFF, res); +} + +static void test_ipchksum_add(void **state) +{ + uint16_t res_1 = ipchksum(test_data_simple, test_data_simple_sz / 2); + uint16_t res_2 = ipchksum(test_data_simple + test_data_simple_sz / 2, + test_data_simple_sz / 2); + uint16_t res_sum = ipchksum_add(test_data_simple_sz / 2, res_1, res_2); + + assert_int_equal(test_data_simple_checksum, res_sum); +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_ipchksum_zero_length), + cmocka_unit_test(test_ipchksum_zero_buffer), + cmocka_unit_test(test_ipchksum_simple_data), + + cmocka_unit_test(test_ipchksum_add_empty_values), + cmocka_unit_test(test_ipchksum_add), + }; + + return cb_run_group_tests(tests, setup_test_group, NULL); +} diff --git a/tests/lib/Makefile.mk b/tests/lib/Makefile.mk index 0cab1ba266b6..b8c4c71eaf81 100644 --- a/tests/lib/Makefile.mk +++ b/tests/lib/Makefile.mk @@ -22,7 +22,6 @@ tests-y += memcpy-test tests-y += malloc-test tests-y += memmove-test tests-y += crc_byte-test -tests-y += compute_ip_checksum-test tests-y += memrange-test tests-y += uuid-test tests-y += bootmem-test @@ -123,9 +122,6 @@ memmove-test-srcs += tests/lib/memmove-test.c crc_byte-test-srcs += tests/lib/crc_byte-test.c crc_byte-test-srcs += src/lib/crc_byte.c -compute_ip_checksum-test-srcs += tests/lib/compute_ip_checksum-test.c -compute_ip_checksum-test-srcs += src/lib/compute_ip_checksum.c - memrange-test-srcs += tests/lib/memrange-test.c memrange-test-srcs += src/lib/memrange.c memrange-test-srcs += tests/stubs/console.c @@ -148,7 +144,7 @@ dimm_info_util-test-srcs += tests/stubs/console.c coreboot_table-test-srcs += tests/lib/coreboot_table-test.c coreboot_table-test-srcs += tests/stubs/console.c -coreboot_table-test-srcs += src/lib/compute_ip_checksum.c +coreboot_table-test-srcs += src/commonlib/bsd/ipchksum.c coreboot_table-test-srcs += src/lib/coreboot_table.c coreboot_table-test-srcs += src/lib/imd_cbmem.c coreboot_table-test-srcs += src/lib/imd.c diff --git a/tests/lib/compute_ip_checksum-test.c b/tests/lib/compute_ip_checksum-test.c deleted file mode 100644 index d465bfc4d0f6..000000000000 --- a/tests/lib/compute_ip_checksum-test.c +++ /dev/null @@ -1,99 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-only */ - -#include -#include -#include -#include -#include - -static const uint8_t test_data_simple[] = { - 0x64, 0x3b, 0x33, 0x17, 0x34, 0x74, 0x62, 0x30, 0x75, 0x73, 0xf3, 0x11, 0x30, 0x2c, - 0x34, 0x35, 0x6d, 0x39, 0x69, 0x32, 0x23, 0x24, 0x76, 0x71, 0x77, 0x30, 0x39, 0x75, - 0x76, 0x35, 0x71, 0x32, 0x40, 0x46, 0x34, 0x34, 0xBB, 0x03, 0x66, 0x52}; -static const size_t test_data_simple_sz = ARRAY_SIZE(test_data_simple); -static const unsigned long test_data_simple_checksum = 0x4267; - -static uint8_t test_data_zeros[1024]; -static const size_t test_data_zeros_sz = ARRAY_SIZE(test_data_zeros); -static const unsigned long test_data_zeros_checksum = 0xFFFF; - -static int setup_test_group(void **state) -{ - memset(test_data_zeros, 0, test_data_zeros_sz); - - return 0; -} - -static void test_compute_ip_checksum_zero_length(void **state) -{ - unsigned long res = compute_ip_checksum(test_data_simple, 0); - - /* Expect checksum to be in initial state as there are were no data provided. */ - assert_int_equal(0xFFFF, res); -} - -static void test_compute_ip_checksum_zero_buffer(void **state) -{ - unsigned long res = compute_ip_checksum(test_data_zeros, test_data_zeros_sz); - assert_int_equal(test_data_zeros_checksum, res); -} - -static void test_compute_ip_checksum_simple_data(void **state) -{ - unsigned long res; - unsigned long check_res; - const size_t helper_buffer_size = sizeof(uint8_t) * (test_data_simple_sz + 2); - char *helper_buffer = malloc(helper_buffer_size); - - /* Self test */ - assert_non_null(helper_buffer); - - /* Expect function to generate the same checksum as stored in */ - res = compute_ip_checksum(test_data_simple, test_data_simple_sz); - assert_int_equal(test_data_simple_checksum, res); - - /* Copy test data and checksum to new buffer. Expect computed checksum to be zero, - as it proves the data and the checksum are correct. */ - memcpy(helper_buffer, test_data_simple, test_data_simple_sz); - helper_buffer[helper_buffer_size - 2] = res & 0xFF; - helper_buffer[helper_buffer_size - 1] = (res >> 8) & 0xFF; - check_res = compute_ip_checksum(helper_buffer, helper_buffer_size); - assert_int_equal(0, check_res); - - free(helper_buffer); -} - -static void test_add_ip_checksums_empty_values(void **state) -{ - unsigned long res; - - res = add_ip_checksums(0, 0xFFFF, 0xFFFF); - assert_int_equal(0xFFFF, res); - - res = add_ip_checksums(1, 0xFFFF, 0xFFFF); - assert_int_equal(0xFFFF, res); -} - -static void test_add_ip_checksums(void **state) -{ - unsigned long res_1 = compute_ip_checksum(test_data_simple, test_data_simple_sz / 2); - unsigned long res_2 = compute_ip_checksum(test_data_simple + test_data_simple_sz / 2, - test_data_simple_sz / 2); - unsigned long res_sum = add_ip_checksums(test_data_simple_sz / 2, res_1, res_2); - - assert_int_equal(test_data_simple_checksum, res_sum); -} - -int main(void) -{ - const struct CMUnitTest tests[] = { - cmocka_unit_test(test_compute_ip_checksum_zero_length), - cmocka_unit_test(test_compute_ip_checksum_zero_buffer), - cmocka_unit_test(test_compute_ip_checksum_simple_data), - - cmocka_unit_test(test_add_ip_checksums_empty_values), - cmocka_unit_test(test_add_ip_checksums), - }; - - return cb_run_group_tests(tests, setup_test_group, NULL); -} -- cgit v1.2.3