Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / msg / async / dpdk / IPChecksum.cc
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  */
22 /*
23  * Ceph - scalable distributed file system
24  *
25  * Copyright (C) 2015 XSky <haomai@xsky.com>
26  *
27  * Author: Haomai Wang <haomaiwang@gmail.com>
28  *
29  * This is free software; you can redistribute it and/or
30  * modify it under the terms of the GNU Lesser General Public
31  * License version 2.1, as published by the Free Software
32  * Foundation.  See file COPYING.
33  *
34  */
35
36 #include <arpa/inet.h>
37 #include "net.h"
38 #include "IPChecksum.h"
39
40 void checksummer::sum(const char* data, size_t len) {
41   auto orig_len = len;
42   if (odd) {
43     csum += uint8_t(*data++);
44     --len;
45   }
46   auto p64 = reinterpret_cast<const uint64_t*>(data);
47   while (len >= 8) {
48     csum += ntohq(*p64++);
49     len -= 8;
50   }
51   auto p16 = reinterpret_cast<const uint16_t*>(p64);
52   while (len >= 2) {
53     csum += ntohs(*p16++);
54     len -= 2;
55   }
56   auto p8 = reinterpret_cast<const uint8_t*>(p16);
57   if (len) {
58     csum += *p8++ << 8;
59     len -= 1;
60   }
61   odd ^= orig_len & 1;
62 }
63
64 uint16_t checksummer::get() const {
65   __int128 csum1 = (csum & 0xffffffffffffffff) + (csum >> 64);
66   uint64_t csum = (csum1 & 0xffffffffffffffff) + (csum1 >> 64);
67   csum = (csum & 0xffff) + ((csum >> 16) & 0xffff) + ((csum >> 32) & 0xffff) + (csum >> 48);
68   csum = (csum & 0xffff) + (csum >> 16);
69   csum = (csum & 0xffff) + (csum >> 16);
70   return htons(~csum);
71 }
72
73 void checksummer::sum(const Packet& p) {
74   for (auto&& f : p.fragments()) {
75     sum(f.base, f.size);
76   }
77 }
78
79 uint16_t ip_checksum(const void* data, size_t len) {
80   checksummer cksum;
81   cksum.sum(reinterpret_cast<const char*>(data), len);
82   return cksum.get();
83 }