1 // SPDX-License-Identifier: GPL-2.0
3 * arch/alpha/lib/checksum.c
5 * This file contains network checksum routines that are better done
6 * in an architecture-specific manner due to speed..
7 * Comments in other versions indicate that the algorithms are from RFC1071
9 * accelerated versions (and 21264 assembly versions ) contributed by
10 * Rick Gorton <rick.gorton@alpha-processor.com>
13 #include <linux/module.h>
14 #include <linux/string.h>
16 #include <asm/byteorder.h>
18 static inline unsigned short from64to16(unsigned long x
)
20 /* Using extract instructions is a bit more efficient
21 than the original shift/bitmask version. */
30 tmp_v
.ul
= (unsigned long) in_v
.ui
[0] + (unsigned long) in_v
.ui
[1];
32 /* Since the bits of tmp_v.sh[3] are going to always be zero,
33 we don't have to bother to add that in. */
34 out_v
.ul
= (unsigned long) tmp_v
.us
[0] + (unsigned long) tmp_v
.us
[1]
35 + (unsigned long) tmp_v
.us
[2];
37 /* Similarly, out_v.us[2] is always zero for the final add. */
38 return out_v
.us
[0] + out_v
.us
[1];
42 * computes the checksum of the TCP/UDP pseudo-header
43 * returns a 16-bit checksum, already complemented.
45 __sum16
csum_tcpudp_magic(__be32 saddr
, __be32 daddr
,
46 __u32 len
, __u8 proto
, __wsum sum
)
48 return (__force __sum16
)~from64to16(
49 (__force u64
)saddr
+ (__force u64
)daddr
+
50 (__force u64
)sum
+ ((len
+ proto
) << 8));
52 EXPORT_SYMBOL(csum_tcpudp_magic
);
54 __wsum
csum_tcpudp_nofold(__be32 saddr
, __be32 daddr
,
55 __u32 len
, __u8 proto
, __wsum sum
)
59 result
= (__force u64
)saddr
+ (__force u64
)daddr
+
60 (__force u64
)sum
+ ((len
+ proto
) << 8);
62 /* Fold down to 32-bits so we don't lose in the typedef-less
65 result
= (result
& 0xffffffff) + (result
>> 32);
67 result
= (result
& 0xffffffff) + (result
>> 32);
68 return (__force __wsum
)result
;
70 EXPORT_SYMBOL(csum_tcpudp_nofold
);
73 * Do a 64-bit checksum on an arbitrary memory area..
75 * This isn't a great routine, but it's not _horrible_ either. The
76 * inner loop could be unrolled a bit further, and there are better
77 * ways to do the carry, but this is reasonable.
79 static inline unsigned long do_csum(const unsigned char * buff
, int len
)
82 unsigned long result
= 0;
86 odd
= 1 & (unsigned long) buff
;
92 count
= len
>> 1; /* nr of 16-bit words.. */
94 if (2 & (unsigned long) buff
) {
95 result
+= *(unsigned short *) buff
;
100 count
>>= 1; /* nr of 32-bit words.. */
102 if (4 & (unsigned long) buff
) {
103 result
+= *(unsigned int *) buff
;
108 count
>>= 1; /* nr of 64-bit words.. */
110 unsigned long carry
= 0;
112 unsigned long w
= *(unsigned long *) buff
;
117 carry
= (w
> result
);
120 result
= (result
& 0xffffffff) + (result
>> 32);
123 result
+= *(unsigned int *) buff
;
128 result
+= *(unsigned short *) buff
;
134 result
= from64to16(result
);
136 result
= ((result
>> 8) & 0xff) | ((result
& 0xff) << 8);
142 * This is a version of ip_compute_csum() optimized for IP headers,
143 * which always checksum on 4 octet boundaries.
145 __sum16
ip_fast_csum(const void *iph
, unsigned int ihl
)
147 return (__force __sum16
)~do_csum(iph
,ihl
*4);
149 EXPORT_SYMBOL(ip_fast_csum
);
152 * computes the checksum of a memory block at buff, length len,
153 * and adds in "sum" (32-bit)
155 * returns a 32-bit number suitable for feeding into itself
156 * or csum_tcpudp_magic
158 * this function must be called with even lengths, except
159 * for the last fragment, which may be odd
161 * it's best to have buff aligned on a 32-bit boundary
163 __wsum
csum_partial(const void *buff
, int len
, __wsum sum
)
165 unsigned long result
= do_csum(buff
, len
);
167 /* add in old sum, and carry.. */
168 result
+= (__force u32
)sum
;
169 /* 32+c bits -> 32 bits */
170 result
= (result
& 0xffffffff) + (result
>> 32);
171 return (__force __wsum
)result
;
174 EXPORT_SYMBOL(csum_partial
);
177 * this routine is used for miscellaneous IP-like checksums, mainly
180 __sum16
ip_compute_csum(const void *buff
, int len
)
182 return (__force __sum16
)~from64to16(do_csum(buff
,len
));
184 EXPORT_SYMBOL(ip_compute_csum
);