Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc2ad45.
[cascardo/ovs.git] / lib / csum.c
1 /*
2  * Copyright (c) 2008 Nicira Networks.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include <config.h>
18 #include "csum.h"
19
20 /* Returns the IP checksum of the 'n' bytes in 'data'. */
21 uint16_t
22 csum(const void *data, size_t n)
23 {
24     return csum_finish(csum_continue(0, data, n));
25 }
26
27 /* Adds the 16 bits in 'new' to the partial IP checksum 'partial' and returns
28  * the updated checksum.  (To start a new checksum, pass 0 for 'partial'.  To
29  * obtain the finished checksum, pass the return value to csum_finish().) */
30 uint32_t
31 csum_add16(uint32_t partial, uint16_t new)
32 {
33     return partial + new;
34 }
35
36 /* Adds the 32 bits in 'new' to the partial IP checksum 'partial' and returns
37  * the updated checksum.  (To start a new checksum, pass 0 for 'partial'.  To
38  * obtain the finished checksum, pass the return value to csum_finish().) */
39 uint32_t
40 csum_add32(uint32_t partial, uint32_t new)
41 {
42     return partial + (new >> 16) + (new & 0xffff);
43 }
44
45
46 /* Adds the 'n' bytes in 'data' to the partial IP checksum 'partial' and
47  * returns the updated checksum.  (To start a new checksum, pass 0 for
48  * 'partial'.  To obtain the finished checksum, pass the return value to
49  * csum_finish().) */
50 uint32_t
51 csum_continue(uint32_t partial, const void *data_, size_t n)
52 {
53     const uint16_t *data = data_;
54
55     for (; n > 1; n -= 2) {
56         partial = csum_add16(partial, *data++);
57     }
58     if (n) {
59         partial += *(uint8_t *) data;
60     }
61     return partial;
62 }
63
64 /* Returns the IP checksum corresponding to 'partial', which is a value updated
65  * by some combination of csum_add16(), csum_add32(), and csum_continue(). */
66 uint16_t
67 csum_finish(uint32_t partial)
68 {
69     return ~((partial & 0xffff) + (partial >> 16));
70 }
71
72 /* Returns the new checksum for a packet in which the checksum field previously
73  * contained 'old_csum' and in which a field that contained 'old_u16' was
74  * changed to contain 'new_u16'. */
75 uint16_t
76 recalc_csum16(uint16_t old_csum, uint16_t old_u16, uint16_t new_u16)
77 {
78     /* Ones-complement arithmetic is endian-independent, so this code does not
79      * use htons() or ntohs().
80      *
81      * See RFC 1624 for formula and explanation. */
82     uint16_t hc_complement = ~old_csum;
83     uint16_t m_complement = ~old_u16;
84     uint16_t m_prime = new_u16;
85     uint32_t sum = hc_complement + m_complement + m_prime;
86     uint16_t hc_prime_complement = sum + (sum >> 16);
87     return ~hc_prime_complement;
88 }
89
90 /* Returns the new checksum for a packet in which the checksum field previously
91  * contained 'old_csum' and in which a field that contained 'old_u32' was
92  * changed to contain 'new_u32'. */
93 uint16_t
94 recalc_csum32(uint16_t old_csum, uint32_t old_u32, uint32_t new_u32)
95 {
96     return recalc_csum16(recalc_csum16(old_csum, old_u32, new_u32),
97                          old_u32 >> 16, new_u32 >> 16);
98 }