Skip to contentSkip to conversation

add checksum selftest

PATCH In review Mike Frysinger 4 participants 9 messages Jun 24, 2009 – Jul 6, 2009 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 add checksum selftest

Start a checksum internal testsuite for arch porters and people mucking about in the checksum code -- regressions are bad.

---
Arnd: i've tested this on my Blackfin and it seems to work ... can you check for any places i missed little endian swapping ?

 lib/Kconfig.debug       |    9 ++
 lib/Makefile            |    1 +
 lib/checksum-selftest.c |  292 +++++++++++++++++++++++++++++++++++++++++++++++
 lib/checksum.c          |    2 +-
 4 files changed, 303 insertions(+), 1 deletions(-)
 create mode 100644 lib/checksum-selftest.c
lib/Kconfig.debug +9 -0
index 4c32b1a..c5cb167 100644
@@ -631,6 +631,15 @@ config DEBUG_SG
 
 	  If unsure, say N.
 
+config CHECKSUM_SELFTEST
+	bool "Checksum boot-time self-tests"
+	depends on DEBUG_KERNEL
+	help
+	  Say Y here if you want the kernel to run a short self-test at boot.
+	  The self-test checks whether the networking checksum functions are
+	  operating as expected.  Useful if you are trying to implement your
+	  own optimized replacements for the generic code.
+
 config DEBUG_NOTIFIERS
 	bool "Debug notifier call chains"
 	depends on DEBUG_KERNEL
lib/Makefile +1 -0
index b6d1857..fde861c 100644
@@ -94,6 +94,7 @@ obj-$(CONFIG_NLATTR) += nlattr.o
 obj-$(CONFIG_DMA_API_DEBUG) += dma-debug.o
 
 obj-$(CONFIG_GENERIC_CSUM) += checksum.o
+obj-$(CONFIG_CHECKSUM_SELFTEST) += checksum-selftest.o
 
 obj-$(CONFIG_GENERIC_ATOMIC64) += atomic64.o
 
lib/checksum-selftest.c new file +292 -0

292 lines not on this page · raw message

lib/checksum.c +1 -1
index 45c9c93..bebfab2 100644
@@ -46,7 +46,7 @@ static inline unsigned short from32to16(unsigned long x)
 	return x;
 }
 
-static unsigned int do_csum(const unsigned char *buff, int len)
+unsigned int do_csum(const unsigned char *buff, int len)
 {
 	int odd, count;
 	unsigned long result = 0;
Signature · 1 line
1.6.3.3
#1 lore raw
Quoted 7 lines · Mike Frysinger

Start a checksum internal testsuite for arch porters and people mucking about in the checksum code -- regressions are bad.

---
Arnd: i've tested this on my Blackfin and it seems to work ... can you check for any places i missed little endian swapping ?

Endian swapping looks correct, and I like the patch a lot in general, but:

lib/checksum-selftest.c line 23

Quoted 6 lines
lib/checksum-selftest.c new file +6 -0
+
+/*
+ * The do_csum() interface is "internal" to the generic checksum code.
+ * Do not require it if the arch has not switched over.
+ */
+extern unsigned short do_csum(const unsigned char *buff, int len);

Just nitpicking: this prototype should to into asm-generic/checksum.h, extern declarations have no place in .c files.

lib/checksum-selftest.c line 44

Quoted 9 lines
lib/checksum-selftest.c new file +9 -0
+static unsigned char __initdata do_csum_data1[] = {
+	0x20,
+};
+static unsigned char __initdata do_csum_data2[] = {
+	0x0d, 0x0a,
+};
+static unsigned char __initdata do_csum_data3[] = {
+	0xff, 0xfb, 0x01,
+};

You define separate test vectors for each of the three cases, which looks like it could be optimized by reusing the same test vectors for each case.

lib/checksum-selftest.c line 192

Quoted 9 lines
lib/checksum-selftest.c new file +9 -0
+static struct csum_partial_data __initdata csum_partial_data[] = {
+	CSUM_PARTIAL_DATA(1,  0x00000074, 0x0),
+	CSUM_PARTIAL_DATA(2,  0x00000a0d, 0x0),
+	CSUM_PARTIAL_DATA(3,  0x0000fe00, 0x0),
+	CSUM_PARTIAL_DATA(5,  0x00005084, 0x0),
+	CSUM_PARTIAL_DATA(8,  0x1101eefe, 0x11016a80),
+	CSUM_PARTIAL_DATA(8b, 0x00008781, 0x847e),
+	CSUM_PARTIAL_DATA(9,  0x1101eefe, 0x11016b80),
+};

For partial checksums, the result has to be folded into a 16-bit number using csum_fold(), because csum_partial and other functions return a 32-bit __wsum that can take many equivalent values taht are all correct.

Quoted 13 lines
lib/checksum-selftest.c new file +13 -0
+static int __init csum_tcpudp_nofold_selftest(void)
+{
+	int i, ret;
+	unsigned short tret, eret;
+
+	ret = 0;
+	for (i = 0; i < ARRAY_SIZE(csum_tcpudp_nofold_data); ++i) {
+		eret = le16_to_cpu(csum_tcpudp_nofold_data[i].ret);
+		tret = csum_tcpudp_nofold(
+			csum_tcpudp_nofold_data[i].saddr,
+			csum_tcpudp_nofold_data[i].daddr,
+			csum_tcpudp_nofold_data[i].len,
+			csum_tcpudp_nofold_data[i].proto,

lib/checksum-selftest.c line 277

Quoted 10 lines
lib/checksum-selftest.c new file +10 -0
+			csum_tcpudp_nofold_data[i].sum);
+		if (tret != eret) {
+			pr_err("%s: test %i: %#x != %#x: FAIL\n",
+				__func__, i, tret, eret);
+			ret = 1;
+		}
+	}
+
+	return ret;
+}

same here, but you can easily use csum_tcpudp_magic() instead of csum_tcpudp_nofold here.

Thanks,

	Arnd <><
#2 lore raw
Quoted 9 lines · Arnd Bergmann

lib/checksum-selftest.c line 23

Quoted 5 lines · Mike Frysinger
lib/checksum-selftest.c new file +5 -0
+/*
+ * The do_csum() interface is "internal" to the generic checksum code.
+ * Do not require it if the arch has not switched over.
+ */
+extern unsigned short do_csum(const unsigned char *buff, int len);

Just nitpicking: this prototype should to into asm-generic/checksum.h, extern declarations have no place in .c files.

yeah, i meant to move that after the last discussion, but forgot

Quoted 5 lines
Quoted 5 lines
lib/checksum-selftest.c new file +5 -0
+static unsigned char __initdata do_csum_data1[] = {
+     0x20,
+};
+static unsigned char __initdata do_csum_data2[] = {
+     0x0d, 0x0a,
Quoted 8 lines

lib/checksum-selftest.c line 44

Quoted 4 lines
lib/checksum-selftest.c new file +4 -0
+};
+static unsigned char __initdata do_csum_data3[] = {
+     0xff, 0xfb, 0x01,
+};

You define separate test vectors for each of the three cases, which looks like it could be optimized by reusing the same test vectors for each case.

i'm not really familiar with the interfaces to figure out how to do this ... i just added some printks to dump arguments/buffers and then copied & pasted ones that looked pretty different

Quoted 6 lines
Quoted 6 lines

+static struct csum_partial_data __initdata csum_partial_data[] = {
+     CSUM_PARTIAL_DATA(1,  0x00000074, 0x0),
+     CSUM_PARTIAL_DATA(2,  0x00000a0d, 0x0),
+     CSUM_PARTIAL_DATA(3,  0x0000fe00, 0x0),
+     CSUM_PARTIAL_DATA(5,  0x00005084, 0x0),
+     CSUM_PARTIAL_DATA(8,  0x1101eefe, 0x11016a80),

Quoted 8 lines
Quoted 3 lines

+     CSUM_PARTIAL_DATA(8b, 0x00008781, 0x847e),
+     CSUM_PARTIAL_DATA(9,  0x1101eefe, 0x11016b80), +};

For partial checksums, the result has to be folded into a 16-bit number using csum_fold(), because csum_partial and other functions return a 32-bit __wsum that can take many equivalent values taht are all correct.

i hear your words, but i understand them not ;)

Quoted 18 lines
Quoted 18 lines
lib/checksum-selftest.c new file +18 -0
+static int __init csum_tcpudp_nofold_selftest(void)
+{
+     int i, ret;
+     unsigned short tret, eret;
+
+     ret = 0;
+     for (i = 0; i < ARRAY_SIZE(csum_tcpudp_nofold_data); ++i) {
+             eret = le16_to_cpu(csum_tcpudp_nofold_data[i].ret);
+             tret = csum_tcpudp_nofold(
+                     csum_tcpudp_nofold_data[i].saddr,
+                     csum_tcpudp_nofold_data[i].daddr,
+                     csum_tcpudp_nofold_data[i].len,
+                     csum_tcpudp_nofold_data[i].proto,
+                     csum_tcpudp_nofold_data[i].sum);
+             if (tret != eret) {
+                     pr_err("%s: test %i: %#x != %#x: FAIL\n",
+                             __func__, i, tret, eret);
+                     ret = 1;
Quoted 8 lines

lib/checksum-selftest.c line 147

Quoted 5 lines

+             }
+     }

lib/checksum-selftest.c new file +3 -0
+
+     return ret;
+}

same here, but you can easily use csum_tcpudp_magic() instead of csum_tcpudp_nofold here.

this introduces redirection that would be annoying for people who implement just csum_tcpudp_nofold() (like the Blackfin arch). i dont have a problem extending the test to also cover csum_tcpudp_magic(), but i'd like to keep the csum_tcpudp_nofold() invocation as well. -mike

#3 lore raw
Quoted 14 lines · Mike Frysinger
Quoted 5 lines
Quoted 5 lines
lib/checksum-selftest.c new file +5 -0
+static unsigned char __initdata do_csum_data1[] = {
+     0x20,
+};
+static unsigned char __initdata do_csum_data2[] = {
+     0x0d, 0x0a,
Quoted 8 lines

lib/checksum-selftest.c line 44

Quoted 4 lines
lib/checksum-selftest.c new file +4 -0
+};
+static unsigned char __initdata do_csum_data3[] = {
+     0xff, 0xfb, 0x01,
+};

You define separate test vectors for each of the three cases, which looks like it could be optimized by reusing the same test vectors for each case.

Quoted 3 lines

i'm not really familiar with the interfaces to figure out how to do this ... i just added some printks to dump arguments/buffers and then copied & pasted ones that looked pretty different

I just mean you can consolidate

+struct do_csum_data {

+       unsigned short ret;
+       unsigned char *buff;
+       int len;
+};
+#define DO_CSUM_DATA(_num, _ret) \
+{ \
+       .ret = _ret, \
+       .buff = do_csum_data##_num, \
+       .len = ARRAY_SIZE(do_csum_data##_num), \

+}

and

+struct csum_partial_data {

+       __wsum ret;
+       const void *buff;
+       int len;
+       __wsum wsum;
+};
+#define CSUM_PARTIAL_DATA(_num, _ret, _wsum) \
+{ \
+       .ret = _ret, \
+       .buff = csum_partial_data##_num, \
+       .len = ARRAY_SIZE(csum_partial_data##_num), \
+       .wsum = _wsum, \

+}

into something like

struct csum_combined_check_data {
	const char *buff;
	int len;
	unsigned short do_csum_ret;
	__wsum wsum;
	unsigned short csum_partial_ret;
};
#define CSUM_COMBINED_TEST_DATA(_num, _do_csum_ret,	\
			 _csum_partial_ret, _wsum)	\
{							\
	.buff = csum_partial_data##_num,		\
	.len = ARRAY_SIZE(csum_partial_data##_num),	\
	.do_csum_ret = _do_csum_ret,			\
	.wsum = _wsum,					\
	.csum_partial_ret = csum_partial_ret,		\
};

This could cut down the length of the module significantly, without changing any of the semantics.

Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
lib/checksum-selftest.c new file +6 -0
+static struct csum_partial_data __initdata csum_partial_data[] = {
+     CSUM_PARTIAL_DATA(1,  0x00000074, 0x0),
+     CSUM_PARTIAL_DATA(2,  0x00000a0d, 0x0),
+     CSUM_PARTIAL_DATA(3,  0x0000fe00, 0x0),
+     CSUM_PARTIAL_DATA(5,  0x00005084, 0x0),
+     CSUM_PARTIAL_DATA(8,  0x1101eefe, 0x11016a80),
Quoted 8 lines

lib/checksum-selftest.c line 192

Quoted 3 lines
lib/checksum-selftest.c new file +3 -0
+     CSUM_PARTIAL_DATA(8b, 0x00008781, 0x847e),
+     CSUM_PARTIAL_DATA(9,  0x1101eefe, 0x11016b80),
+};

For partial checksums, the result has to be folded into a 16-bit number using csum_fold(), because csum_partial and other functions return a 32-bit __wsum that can take many equivalent values taht are all correct.

Quoted 1 line

i hear your words, but i understand them not ;)

The problem is that IP checksumming is only defined for 16-bit words. We use __wsum (32 bits) as an intermediate in the networking stack so we can consolidate the folding in one place. If you have a test vector that results in checksum 0xffff (as a well-formed packet should), the __wsum could be one of 0x0000ffff, 0xffff0000, 0xffffffff, 0x1234edcb, for any other value x where
(((x >> 16) + (x & 0xffff)) >> 16 + ((x >> 16) + (x & 0xffff))) & 0xffff = 0xffff. The specific __wsum returned by csum_partial() is implementation specific, so you cannot compare it to a precomputed value unless sending it through csum_fold().

	Arnd <><
#4 lore raw
Quoted 15 lines · Arnd Bergmann
Quoted 14 lines · Mike Frysinger
Quoted 13 lines

lib/checksum-selftest.c line 44

Quoted 9 lines
lib/checksum-selftest.c new file +9 -0
+static unsigned char __initdata do_csum_data1[] = {
+     0x20,
+};
+static unsigned char __initdata do_csum_data2[] = {
+     0x0d, 0x0a,
+};
+static unsigned char __initdata do_csum_data3[] = {
+     0xff, 0xfb, 0x01,
+};

You define separate test vectors for each of the three cases, which looks like it could be optimized by reusing the same test vectors for each case.

Quoted 5 lines
Quoted 3 lines

i'm not really familiar with the interfaces to figure out how to do this ... i just added some printks to dump arguments/buffers and then copied & pasted ones that looked pretty different

I just mean you can consolidate

ok, i'll take a stab at that

Quoted 17 lines
Quoted 15 lines
Quoted 6 lines
Quoted 6 lines

+static struct csum_partial_data __initdata csum_partial_data[] = {
+     CSUM_PARTIAL_DATA(1,  0x00000074, 0x0),
+     CSUM_PARTIAL_DATA(2,  0x00000a0d, 0x0),
+     CSUM_PARTIAL_DATA(3,  0x0000fe00, 0x0),
+     CSUM_PARTIAL_DATA(5,  0x00005084, 0x0),
+     CSUM_PARTIAL_DATA(8,  0x1101eefe, 0x11016a80),

Quoted 8 lines
Quoted 3 lines

+     CSUM_PARTIAL_DATA(8b, 0x00008781, 0x847e),
+     CSUM_PARTIAL_DATA(9,  0x1101eefe, 0x11016b80), +};

For partial checksums, the result has to be folded into a 16-bit number using csum_fold(), because csum_partial and other functions return a 32-bit __wsum that can take many equivalent values taht are all correct.

Quoted 1 line

i hear your words, but i understand them not ;)

Quoted 10 lines

The problem is that IP checksumming is only defined for 16-bit words. We use __wsum (32 bits) as an intermediate in the networking stack so we can consolidate the folding in one place. If you have a test vector that results in checksum 0xffff (as a well-formed packet should), the __wsum could be one of 0x0000ffff, 0xffff0000, 0xffffffff, 0x1234edcb, for any other value x where
(((x >> 16) + (x & 0xffff)) >> 16 + ((x >> 16) + (x & 0xffff))) & 0xffff = 0xffff. The specific __wsum returned by csum_partial() is implementation specific, so you cannot compare it to a precomputed value unless sending it through csum_fold().

ok, i can see that for the result, but how do i handle the functions that also take a __wsum ? do i just always pass them 0 to avoid implementation issues ?
-mike

#5 lore raw
Quoted 3 lines · Mike Frysinger

ok, i can see that for the result, but how do i handle the functions that also take a __wsum ? do i just always pass them 0 to avoid implementation issues ?

No, any __wsum you pass in there should be fine, because that one gets folded in in the end.

	Arnd <><
#6 lore raw
Quoted 10 lines · Arnd Bergmann
Quoted 4 lines · Mike Frysinger

ok, i can see that for the result, but how do i handle the functions that also take a __wsum ? do i just always pass them 0 to avoid implementation issues ?

No, any __wsum you pass in there should be fine, because that one gets folded in in the end.

Mike do you have any updated patch? I would like to check it on Microblaze too.

Thanks,
Michal

Quoted 7 lines
	Arnd <><

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

Signature · 3 lines
Michal Simek, Ing. (M.Eng)
PetaLogix - Linux Solutions for a Reconfigurable World
w: www.petalogix.com p: +61-7-30090663,+42-0-721842854 f: +61-7-30090663
#7 lore raw
Quoted 11 lines · Michal Simek
Quoted 7 lines · Arnd Bergmann
Quoted 3 lines · Mike Frysinger

ok, i can see that for the result, but how do i handle the functions that also take a __wsum ?  do i just always pass them 0 to avoid implementation issues ?

No, any __wsum you pass in there should be fine, because that one gets folded in in the end.

Mike do you have any updated patch? I would like to check it on Microblaze too.

the latest code i have is here:
http://git.kernel.org/?p=linux/kernel/git/vapier/blackfin.git;a=shortlog;h=asm-generic

but i havent finished integrating the tests and i think they currently fail on Blackfin due to it
-mike

#8 lore raw
Quoted 13 lines · Mike Frysinger
Quoted 11 lines · Michal Simek
Quoted 9 lines · Arnd Bergmann
Quoted 4 lines · Mike Frysinger

ok, i can see that for the result, but how do i handle the functions that also take a __wsum ? do i just always pass them 0 to avoid implementation issues ?

No, any __wsum you pass in there should be fine, because that one gets folded in in the end.

Quoted 7 lines
Quoted 3 lines

Mike do you have any updated patch? I would like to check it on Microblaze too.

the latest code i have is here:
http://git.kernel.org/?p=linux/kernel/git/vapier/blackfin.git;a=shortlog;h=asm-generic

ok. Thanks.

Quoted 3 lines

but i havent finished integrating the tests and i think they currently fail on Blackfin due to it

On Microblaze all tests failed too.

Michal

Quoted 2 lines

-mike

Signature · 3 lines
Michal Simek, Ing. (M.Eng)
PetaLogix - Linux Solutions for a Reconfigurable World
w: www.petalogix.com p: +61-7-30090663,+42-0-721842854 f: +61-7-30090663