Update primary code license to Apache 2.0.
[cascardo/ovs.git] / lib / csum.c
1 /*
2  * Copyright (c) 2008, 2009 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include "csum.h"
19
20 /* Returns the IP checksum of the 'n' bytes in 'data'.
21  *
22  * The return value has the same endianness as the data.  That is, if 'data'
23  * consists of a packet in network byte order, then the return value is a value
24  * in network byte order, and if 'data' consists of a data structure in host
25  * byte order, then the return value is in host byte order. */
26 uint16_t
27 csum(const void *data, size_t n)
28 {
29     return csum_finish(csum_continue(0, data, n));
30 }
31
32 /* Adds the 16 bits in 'new' to the partial IP checksum 'partial' and returns
33  * the updated checksum.  (To start a new checksum, pass 0 for 'partial'.  To
34  * obtain the finished checksum, pass the return value to csum_finish().) */
35 uint32_t
36 csum_add16(uint32_t partial, uint16_t new)
37 {
38     return partial + new;
39 }
40
41 /* Adds the 32 bits in 'new' to the partial IP checksum 'partial' and returns
42  * the updated checksum.  (To start a new checksum, pass 0 for 'partial'.  To
43  * obtain the finished checksum, pass the return value to csum_finish().) */
44 uint32_t
45 csum_add32(uint32_t partial, uint32_t new)
46 {
47     return partial + (new >> 16) + (new & 0xffff);
48 }
49
50
51 /* Adds the 'n' bytes in 'data' to the partial IP checksum 'partial' and
52  * returns the updated checksum.  (To start a new checksum, pass 0 for
53  * 'partial'.  To obtain the finished checksum, pass the return value to
54  * csum_finish().) */
55 uint32_t
56 csum_continue(uint32_t partial, const void *data_, size_t n)
57 {
58     const uint16_t *data = data_;
59
60     for (; n > 1; n -= 2) {
61         partial = csum_add16(partial, *data++);
62     }
63     if (n) {
64         partial += *(uint8_t *) data;
65     }
66     return partial;
67 }
68
69 /* Returns the IP checksum corresponding to 'partial', which is a value updated
70  * by some combination of csum_add16(), csum_add32(), and csum_continue().
71  *
72  * The return value has the same endianness as the checksummed data.  That is,
73  * if the data consist of a packet in network byte order, then the return value
74  * is a value in network byte order, and if the data are a data structure in
75  * host byte order, then the return value is in host byte order. */
76 uint16_t
77 csum_finish(uint32_t partial)
78 {
79     return ~((partial & 0xffff) + (partial >> 16));
80 }
81
82 /* Returns the new checksum for a packet in which the checksum field previously
83  * contained 'old_csum' and in which a field that contained 'old_u16' was
84  * changed to contain 'new_u16'. */
85 uint16_t
86 recalc_csum16(uint16_t old_csum, uint16_t old_u16, uint16_t new_u16)
87 {
88     /* Ones-complement arithmetic is endian-independent, so this code does not
89      * use htons() or ntohs().
90      *
91      * See RFC 1624 for formula and explanation. */
92     uint16_t hc_complement = ~old_csum;
93     uint16_t m_complement = ~old_u16;
94     uint16_t m_prime = new_u16;
95     uint32_t sum = hc_complement + m_complement + m_prime;
96     uint16_t hc_prime_complement = sum + (sum >> 16);
97     return ~hc_prime_complement;
98 }
99
100 /* Returns the new checksum for a packet in which the checksum field previously
101  * contained 'old_csum' and in which a field that contained 'old_u32' was
102  * changed to contain 'new_u32'. */
103 uint16_t
104 recalc_csum32(uint16_t old_csum, uint32_t old_u32, uint32_t new_u32)
105 {
106     return recalc_csum16(recalc_csum16(old_csum, old_u32, new_u32),
107                          old_u32 >> 16, new_u32 >> 16);
108 }