]> git.proxmox.com Git - ceph.git/blame - ceph/src/msg/async/dpdk/IPChecksum.cc
import quincy beta 17.1.0
[ceph.git] / ceph / src / msg / async / dpdk / IPChecksum.cc
CommitLineData
7c673cae
FG
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2/*
3 * This file is open source software, licensed to you under the terms
4 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
5 * distributed with this work for additional information regarding copyright
6 * ownership. You may not use this file except in compliance with the License.
7 *
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19/*
20 * Copyright (C) 2014 Cloudius Systems, Ltd.
21 */
7c673cae
FG
22
23#include <arpa/inet.h>
24#include "net.h"
25#include "IPChecksum.h"
26
27void checksummer::sum(const char* data, size_t len) {
28 auto orig_len = len;
29 if (odd) {
30 csum += uint8_t(*data++);
31 --len;
32 }
33 auto p64 = reinterpret_cast<const uint64_t*>(data);
34 while (len >= 8) {
35 csum += ntohq(*p64++);
36 len -= 8;
37 }
38 auto p16 = reinterpret_cast<const uint16_t*>(p64);
39 while (len >= 2) {
40 csum += ntohs(*p16++);
41 len -= 2;
42 }
43 auto p8 = reinterpret_cast<const uint8_t*>(p16);
44 if (len) {
45 csum += *p8++ << 8;
46 len -= 1;
47 }
48 odd ^= orig_len & 1;
49}
50
51uint16_t checksummer::get() const {
52 __int128 csum1 = (csum & 0xffffffffffffffff) + (csum >> 64);
53 uint64_t csum = (csum1 & 0xffffffffffffffff) + (csum1 >> 64);
54 csum = (csum & 0xffff) + ((csum >> 16) & 0xffff) + ((csum >> 32) & 0xffff) + (csum >> 48);
55 csum = (csum & 0xffff) + (csum >> 16);
56 csum = (csum & 0xffff) + (csum >> 16);
57 return htons(~csum);
58}
59
60void checksummer::sum(const Packet& p) {
61 for (auto&& f : p.fragments()) {
62 sum(f.base, f.size);
63 }
64}
65
66uint16_t ip_checksum(const void* data, size_t len) {
67 checksummer cksum;
68 cksum.sum(reinterpret_cast<const char*>(data), len);
69 return cksum.get();
70}