1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#ifndef QEMU_NET_CHECKSUM_H
19#define QEMU_NET_CHECKSUM_H
20
21#include "qemu/bswap.h"
22struct iovec;
23
24uint32_t net_checksum_add_cont(int len, uint8_t *buf, int seq);
25uint16_t net_checksum_finish(uint32_t sum);
26uint16_t net_checksum_tcpudp(uint16_t length, uint16_t proto,
27 uint8_t *addrs, uint8_t *buf);
28void net_checksum_calculate(uint8_t *data, int length);
29
30static inline uint32_t
31net_checksum_add(int len, uint8_t *buf)
32{
33 return net_checksum_add_cont(len, buf, 0);
34}
35
36static inline uint16_t
37net_checksum_finish_nozero(uint32_t sum)
38{
39 return net_checksum_finish(sum) ?: 0xFFFF;
40}
41
42static inline uint16_t
43net_raw_checksum(uint8_t *data, int length)
44{
45 return net_checksum_finish(net_checksum_add(length, data));
46}
47
48
49
50
51
52
53
54
55
56
57uint32_t net_checksum_add_iov(const struct iovec *iov,
58 const unsigned int iov_cnt,
59 uint32_t iov_off, uint32_t size,
60 uint32_t csum_offset);
61
62typedef struct toeplitz_key_st {
63 uint32_t leftmost_32_bits;
64 uint8_t *next_byte;
65} net_toeplitz_key;
66
67static inline
68void net_toeplitz_key_init(net_toeplitz_key *key, uint8_t *key_bytes)
69{
70 key->leftmost_32_bits = be32_to_cpu(*(uint32_t *)key_bytes);
71 key->next_byte = key_bytes + sizeof(uint32_t);
72}
73
74static inline
75void net_toeplitz_add(uint32_t *result,
76 uint8_t *input,
77 uint32_t len,
78 net_toeplitz_key *key)
79{
80 register uint32_t accumulator = *result;
81 register uint32_t leftmost_32_bits = key->leftmost_32_bits;
82 register uint32_t byte;
83
84 for (byte = 0; byte < len; byte++) {
85 register uint8_t input_byte = input[byte];
86 register uint8_t key_byte = *(key->next_byte++);
87 register uint8_t bit;
88
89 for (bit = 0; bit < 8; bit++) {
90 if (input_byte & (1 << 7)) {
91 accumulator ^= leftmost_32_bits;
92 }
93
94 leftmost_32_bits =
95 (leftmost_32_bits << 1) | ((key_byte & (1 << 7)) >> 7);
96
97 input_byte <<= 1;
98 key_byte <<= 1;
99 }
100 }
101
102 key->leftmost_32_bits = leftmost_32_bits;
103 *result = accumulator;
104}
105
106#endif
107