Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / msg / async / dpdk / IPChecksum.h
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 #ifndef CEPH_MSG_CHECKSUM_H_
37 #define CEPH_MSG_CHECKSUM_H_
38
39 #include <cstdint>
40 #include <cstddef>
41 #include <arpa/inet.h>
42
43 #include "Packet.h"
44
45 uint16_t ip_checksum(const void* data, size_t len);
46
47 struct checksummer {
48   __int128 csum = 0;
49   bool odd = false;
50   void sum(const char* data, size_t len);
51   void sum(const Packet& p);
52   void sum(uint8_t data) {
53     if (!odd) {
54       csum += data << 8;
55     } else {
56       csum += data;
57     }
58     odd = !odd;
59   }
60   void sum(uint16_t data) {
61     if (odd) {
62       sum(uint8_t(data >> 8));
63       sum(uint8_t(data));
64     } else {
65       csum += data;
66     }
67   }
68   void sum(uint32_t data) {
69     if (odd) {
70       sum(uint16_t(data));
71       sum(uint16_t(data >> 16));
72     } else {
73       csum += data;
74     }
75   }
76   void sum_many() {}
77   template <typename T0, typename... T>
78   void sum_many(T0 data, T... rest) {
79     sum(data);
80     sum_many(rest...);
81   }
82   uint16_t get() const;
83 };
84
85 #endif /* CEPH_MSG_CHECKSUM_H_ */