File: checksum.c

package info (click to toggle)
kernel-source-2.0.32 2.0.32-5
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 29,648 kB
  • ctags: 86,850
  • sloc: ansic: 542,141; asm: 26,201; makefile: 3,423; sh: 1,195; perl: 727; tcl: 408; cpp: 277; lisp: 211; awk: 134
file content (148 lines) | stat: -rw-r--r-- 2,813 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * INET		An implementation of the TCP/IP protocol suite for the LINUX
 *		operating system.  INET is implemented using the  BSD Socket
 *		interface as the means of communication with the user level.
 *
 *		MIPS specific IP/TCP/UDP checksumming routines
 *
 * Authors:	Ralf Baechle, <ralf@waldorf-gmbh.de>
 *		Lots of code moved from tcp.c and ip.c; see those files
 *		for more names.
 *
 *		This program is free software; you can redistribute it and/or
 *		modify it under the terms of the GNU General Public License
 *		as published by the Free Software Foundation; either version
 *		2 of the License, or (at your option) any later version.
 */
#include <net/checksum.h>
#include <asm/string.h>

/*
 * computes a partial checksum, e.g. for TCP/UDP fragments
 */

unsigned int csum_partial(const unsigned char *buff, int len, unsigned int sum)
{
	unsigned long	scratch1;
	unsigned long	scratch2;

	/*
	 * The GCC generated code for handling carry bits makes
	 * it strongly desirable to do this in assembler!
	 */
    __asm__("
	.set	noreorder
	.set	noat
	andi	$1,%5,2		# Check alignment
	beqz	$1,2f		# Branch if ok
	subu	$1,%4,2		# delay slot, Alignment uses up two bytes
	bgez	$1,1f		# Jump if we had at least two bytes
	move	%4,$1		# delay slot
	j	4f
	addiu	%4,2		# delay slot; len was < 2.  Deal with it

1:	lw	%2,(%5)
	addiu	%4,2
	addu	%0,%2
	sltu	$1,%0,%2
	addu	%0,$1

2:	move	%1,%4
	srl	%1,%1,5
	beqz	%1,2f
	sll	%1,%1,5		# delay slot

	addu	%1,%5
1:	lw	%2,0(%5)
	addu	%5,32
	addu	%0,%2
	sltu	$1,%0,%2

	lw	%2,-28(%5)
	addu	%0,$1
	addu	%0,%2
	sltu	$1,%0,%2

	lw	%2,-24(%5)
	addu	%0,$1
	addu	%0,%2
	sltu	$1,%0,%2

	lw	%2,-20(%5)
	addu	%0,$1
	addu	%0,%2
	sltu	$1,%0,%2

	lw	%2,-16(%5)
	addu	%0,$1
	addu	%0,%2
	sltu	$1,%0,%2

	lw	%2,-12(%5)
	addu	%0,$1
	addu	%0,%2
	sltu	$1,%0,%2

	lw	%2,-8(%5)
	addu	%0,$1
	addu	%0,%2
	sltu	$1,%0,%2

	lw	%2,-4(%5)
	addu	%0,$1
	addu	%0,%2
	sltu	$1,%0,%2

	bne	%5,%1,1b
	addu	%0,$1		# delay slot

2:	andi	%1,%4,0x1c
	srl	%1,%1,2
	beqz	%1,4f
	addu	%1,%5		# delay slot
3:	lw	%2,0(%5)
	addu	%5,4
	addu	%0,%2
	sltu	$1,%0,%2
	bne	%5,%1,3b
	addu	%0,$1		# delay slot

4:	andi	$1,%3,2
	beqz	$1,5f
	move	%2,$0		# delay slot
	lhu	%2,(%5)
	addiu	%5,2

5:	andi	$1,%3,1
	beqz	$1,6f
	sll	%1,16		# delay slot
	lbu	%1,(%5)
	nop			# NOP ALERT (spit, gasp)
6:	or	%2,%1
	addu	%0,%2
	sltu	$1,%0,%2
	addu	%0,$1
7:	.set	at
	.set	reorder"
	: "=r"(sum), "=r" (scratch1), "=r" (scratch2)
	: "0"(sum), "r"(len), "r"(buff)
	: "$1");

	return sum;
}

/*
 * copy from fs while checksumming, otherwise like csum_partial
 */
unsigned int csum_partial_copy(const char *src, char *dst, 
				  int len, int sum)
{
	/*
	 * It's 2:30 am and I don't feel like doing it real ...
	 * This is lots slower than the real thing (tm)
	 */
	sum = csum_partial(src, len, sum);
	memcpy(dst, src, len);

	return sum;
}