Mark McLoughlin | 7200ac3 | 2009-10-22 17:49:03 +0100 | [diff] [blame] | 1 | /* |
| 2 | * IP checksumming functions. |
| 3 | * (c) 2008 Gerd Hoffmann <kraxel@redhat.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; under version 2 of the License. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #ifndef QEMU_NET_CHECKSUM_H |
| 19 | #define QEMU_NET_CHECKSUM_H |
| 20 | |
Dmitry Fleytman | 0478d1d | 2016-06-01 11:23:36 +0300 | [diff] [blame] | 21 | #include "qemu/bswap.h" |
Michael Tokarev | 2624717 | 2014-02-07 12:22:18 +0400 | [diff] [blame] | 22 | struct iovec; |
Mark McLoughlin | 7200ac3 | 2009-10-22 17:49:03 +0100 | [diff] [blame] | 23 | |
Bin Meng | f574633 | 2020-12-11 17:35:12 +0800 | [diff] [blame] | 24 | #define CSUM_IP 0x01 |
| 25 | #define CSUM_TCP 0x02 |
| 26 | #define CSUM_UDP 0x04 |
| 27 | #define CSUM_ALL (CSUM_IP | CSUM_TCP | CSUM_UDP) |
| 28 | |
Dmitry Fleytman | 5acf5ea | 2013-03-09 11:21:02 +0200 | [diff] [blame] | 29 | uint32_t net_checksum_add_cont(int len, uint8_t *buf, int seq); |
Mark McLoughlin | 7200ac3 | 2009-10-22 17:49:03 +0100 | [diff] [blame] | 30 | uint16_t net_checksum_finish(uint32_t sum); |
| 31 | uint16_t net_checksum_tcpudp(uint16_t length, uint16_t proto, |
| 32 | uint8_t *addrs, uint8_t *buf); |
Bin Meng | f574633 | 2020-12-11 17:35:12 +0800 | [diff] [blame] | 33 | void net_checksum_calculate(uint8_t *data, int length, int csum_flag); |
Mark McLoughlin | 7200ac3 | 2009-10-22 17:49:03 +0100 | [diff] [blame] | 34 | |
Dmitry Fleytman | 5acf5ea | 2013-03-09 11:21:02 +0200 | [diff] [blame] | 35 | static inline uint32_t |
| 36 | net_checksum_add(int len, uint8_t *buf) |
| 37 | { |
| 38 | return net_checksum_add_cont(len, buf, 0); |
| 39 | } |
| 40 | |
| 41 | static inline uint16_t |
Ed Swierk | 0dacea9 | 2017-11-16 06:06:06 -0800 | [diff] [blame] | 42 | net_checksum_finish_nozero(uint32_t sum) |
| 43 | { |
| 44 | return net_checksum_finish(sum) ?: 0xFFFF; |
| 45 | } |
| 46 | |
| 47 | static inline uint16_t |
Dmitry Fleytman | 5acf5ea | 2013-03-09 11:21:02 +0200 | [diff] [blame] | 48 | net_raw_checksum(uint8_t *data, int length) |
| 49 | { |
| 50 | return net_checksum_finish(net_checksum_add(length, data)); |
| 51 | } |
| 52 | |
Dmitry Fleytman | 8402630 | 2013-03-09 11:21:03 +0200 | [diff] [blame] | 53 | /** |
| 54 | * net_checksum_add_iov: scatter-gather vector checksumming |
| 55 | * |
| 56 | * @iov: input scatter-gather array |
| 57 | * @iov_cnt: number of array elements |
| 58 | * @iov_off: starting iov offset for checksumming |
| 59 | * @size: length of data to be checksummed |
Dmitry Fleytman | eb70002 | 2016-06-01 11:23:41 +0300 | [diff] [blame] | 60 | * @csum_offset: offset of the checksum chunk |
Dmitry Fleytman | 8402630 | 2013-03-09 11:21:03 +0200 | [diff] [blame] | 61 | */ |
| 62 | uint32_t net_checksum_add_iov(const struct iovec *iov, |
| 63 | const unsigned int iov_cnt, |
Dmitry Fleytman | eb70002 | 2016-06-01 11:23:41 +0300 | [diff] [blame] | 64 | uint32_t iov_off, uint32_t size, |
| 65 | uint32_t csum_offset); |
Dmitry Fleytman | 8402630 | 2013-03-09 11:21:03 +0200 | [diff] [blame] | 66 | |
Dmitry Fleytman | 0478d1d | 2016-06-01 11:23:36 +0300 | [diff] [blame] | 67 | typedef struct toeplitz_key_st { |
| 68 | uint32_t leftmost_32_bits; |
| 69 | uint8_t *next_byte; |
| 70 | } net_toeplitz_key; |
| 71 | |
| 72 | static inline |
| 73 | void net_toeplitz_key_init(net_toeplitz_key *key, uint8_t *key_bytes) |
| 74 | { |
| 75 | key->leftmost_32_bits = be32_to_cpu(*(uint32_t *)key_bytes); |
| 76 | key->next_byte = key_bytes + sizeof(uint32_t); |
| 77 | } |
| 78 | |
| 79 | static inline |
| 80 | void net_toeplitz_add(uint32_t *result, |
| 81 | uint8_t *input, |
| 82 | uint32_t len, |
| 83 | net_toeplitz_key *key) |
| 84 | { |
| 85 | register uint32_t accumulator = *result; |
| 86 | register uint32_t leftmost_32_bits = key->leftmost_32_bits; |
| 87 | register uint32_t byte; |
| 88 | |
| 89 | for (byte = 0; byte < len; byte++) { |
| 90 | register uint8_t input_byte = input[byte]; |
| 91 | register uint8_t key_byte = *(key->next_byte++); |
| 92 | register uint8_t bit; |
| 93 | |
| 94 | for (bit = 0; bit < 8; bit++) { |
| 95 | if (input_byte & (1 << 7)) { |
| 96 | accumulator ^= leftmost_32_bits; |
| 97 | } |
| 98 | |
| 99 | leftmost_32_bits = |
| 100 | (leftmost_32_bits << 1) | ((key_byte & (1 << 7)) >> 7); |
| 101 | |
| 102 | input_byte <<= 1; |
| 103 | key_byte <<= 1; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | key->leftmost_32_bits = leftmost_32_bits; |
| 108 | *result = accumulator; |
| 109 | } |
| 110 | |
Mark McLoughlin | 7200ac3 | 2009-10-22 17:49:03 +0100 | [diff] [blame] | 111 | #endif /* QEMU_NET_CHECKSUM_H */ |