summaryrefslogtreecommitdiffstats
path: root/src/common/Checksummer.h
blob: a42f5b682a139e95db39204003745d1f0895a495 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#ifndef CEPH_OS_BLUESTORE_CHECKSUMMER
#define CEPH_OS_BLUESTORE_CHECKSUMMER

#include "include/buffer.h"
#include "include/byteorder.h"
#include "include/ceph_assert.h"

#include "xxHash/xxhash.h"

class Checksummer {
public:
  enum CSumType {
    CSUM_NONE = 1,	//intentionally set to 1 to be aligned with OSDMnitor's pool_opts_t handling - it treats 0 as unset while we need to distinguish none and unset cases
    CSUM_XXHASH32 = 2,
    CSUM_XXHASH64 = 3,
    CSUM_CRC32C = 4,
    CSUM_CRC32C_16 = 5, // low 16 bits of crc32c
    CSUM_CRC32C_8 = 6,  // low 8 bits of crc32c
    CSUM_MAX,
  };
  static const char *get_csum_type_string(unsigned t) {
    switch (t) {
    case CSUM_NONE: return "none";
    case CSUM_XXHASH32: return "xxhash32";
    case CSUM_XXHASH64: return "xxhash64";
    case CSUM_CRC32C: return "crc32c";
    case CSUM_CRC32C_16: return "crc32c_16";
    case CSUM_CRC32C_8: return "crc32c_8";
    default: return "???";
    }
  }
  static int get_csum_string_type(const std::string &s) {
    if (s == "none")
      return CSUM_NONE;
    if (s == "xxhash32")
      return CSUM_XXHASH32;
    if (s == "xxhash64")
      return CSUM_XXHASH64;
    if (s == "crc32c")
      return CSUM_CRC32C;
    if (s == "crc32c_16")
      return CSUM_CRC32C_16;
    if (s == "crc32c_8")
      return CSUM_CRC32C_8;
    return -EINVAL;
  }

  static size_t get_csum_init_value_size(int csum_type) {
    switch (csum_type) {
    case CSUM_NONE: return 0;
    case CSUM_XXHASH32: return sizeof(xxhash32::init_value_t);
    case CSUM_XXHASH64: return sizeof(xxhash64::init_value_t);
    case CSUM_CRC32C: return sizeof(crc32c::init_value_t);
    case CSUM_CRC32C_16: return sizeof(crc32c_16::init_value_t);
    case CSUM_CRC32C_8: return sizeof(crc32c_8::init_value_t);
    default: return 0;
    }
  }
  static size_t get_csum_value_size(int csum_type) {
    switch (csum_type) {
    case CSUM_NONE: return 0;
    case CSUM_XXHASH32: return 4;
    case CSUM_XXHASH64: return 8;
    case CSUM_CRC32C: return 4;
    case CSUM_CRC32C_16: return 2;
    case CSUM_CRC32C_8: return 1;
    default: return 0;
    }
  }

  struct crc32c {
    typedef uint32_t init_value_t;
    typedef ceph_le32 value_t;

    // we have no execution context/state.
    typedef int state_t;
    static void init(state_t *state) {
    }
    static void fini(state_t *state) {
    }

    static init_value_t calc(
      state_t state,
      init_value_t init_value,
      size_t len,
      ceph::buffer::list::const_iterator& p
      ) {
      return p.crc32c(len, init_value);
    }
  };

  struct crc32c_16 {
    typedef uint32_t init_value_t;
    typedef ceph_le16 value_t;

    // we have no execution context/state.
    typedef int state_t;
    static void init(state_t *state) {
    }
    static void fini(state_t *state) {
    }

    static init_value_t calc(
      state_t state,
      init_value_t init_value,
      size_t len,
      ceph::buffer::list::const_iterator& p
      ) {
      return p.crc32c(len, init_value) & 0xffff;
    }
  };

  struct crc32c_8 {
    typedef uint32_t init_value_t;
    typedef __u8 value_t;

    // we have no execution context/state.
    typedef int state_t;
    static void init(state_t *state) {
    }
    static void fini(state_t *state) {
    }

    static init_value_t calc(
      state_t state,
      init_value_t init_value,
      size_t len,
      ceph::buffer::list::const_iterator& p
      ) {
      return p.crc32c(len, init_value) & 0xff;
    }
  };

  struct xxhash32 {
    typedef uint32_t init_value_t;
    typedef ceph_le32 value_t;

    typedef XXH32_state_t *state_t;
    static void init(state_t *s) {
      *s = XXH32_createState();
    }
    static void fini(state_t *s) {
      XXH32_freeState(*s);
    }

    static init_value_t calc(
      state_t state,
      init_value_t init_value,
      size_t len,
      ceph::buffer::list::const_iterator& p
      ) {
      XXH32_reset(state, init_value);
      while (len > 0) {
	const char *data;
	size_t l = p.get_ptr_and_advance(len, &data);
	XXH32_update(state, data, l);
	len -= l;
      }
      return XXH32_digest(state);
    }
  };

  struct xxhash64 {
    typedef uint64_t init_value_t;
    typedef ceph_le64 value_t;

    typedef XXH64_state_t *state_t;
    static void init(state_t *s) {
      *s = XXH64_createState();
    }
    static void fini(state_t *s) {
      XXH64_freeState(*s);
    }

    static init_value_t calc(
      state_t state,
      init_value_t init_value,
      size_t len,
      ceph::buffer::list::const_iterator& p
      ) {
      XXH64_reset(state, init_value);
      while (len > 0) {
	const char *data;
	size_t l = p.get_ptr_and_advance(len, &data);
	XXH64_update(state, data, l);
	len -= l;
      }
      return XXH64_digest(state);
    }
  };

  template<class Alg>
  static int calculate(
    size_t csum_block_size,
    size_t offset,
    size_t length,
    const ceph::buffer::list &bl,
    ceph::buffer::ptr* csum_data
    ) {
    return calculate<Alg>(-1, csum_block_size, offset, length, bl, csum_data);
  }

  template<class Alg>
  static int calculate(
      typename Alg::init_value_t init_value,
      size_t csum_block_size,
      size_t offset,
      size_t length,
      const ceph::buffer::list &bl,
      ceph::buffer::ptr* csum_data) {
    ceph_assert(length % csum_block_size == 0);
    size_t blocks = length / csum_block_size;
    ceph::buffer::list::const_iterator p = bl.begin();
    ceph_assert(bl.length() >= length);

    typename Alg::state_t state;
    Alg::init(&state);

    ceph_assert(csum_data->length() >= (offset + length) / csum_block_size *
	   sizeof(typename Alg::value_t));

    typename Alg::value_t *pv =
      reinterpret_cast<typename Alg::value_t*>(csum_data->c_str());
    pv += offset / csum_block_size;
    while (blocks--) {
      *pv = Alg::calc(state, init_value, csum_block_size, p);
      ++pv;
    }
    Alg::fini(&state);
    return 0;
  }

  template<class Alg>
  static int verify(
    size_t csum_block_size,
    size_t offset,
    size_t length,
    const ceph::buffer::list &bl,
    const ceph::buffer::ptr& csum_data,
    uint64_t *bad_csum=0
    ) {
    ceph_assert(length % csum_block_size == 0);
    ceph::buffer::list::const_iterator p = bl.begin();
    ceph_assert(bl.length() >= length);

    typename Alg::state_t state;
    Alg::init(&state);

    const typename Alg::value_t *pv =
      reinterpret_cast<const typename Alg::value_t*>(csum_data.c_str());
    pv += offset / csum_block_size;
    size_t pos = offset;
    while (length > 0) {
      typename Alg::init_value_t v = Alg::calc(state, -1, csum_block_size, p);
      if (*pv != v) {
	if (bad_csum) {
	  *bad_csum = v;
	}
	Alg::fini(&state);
	return pos;
      }
      ++pv;
      pos += csum_block_size;
      length -= csum_block_size;
    }
    Alg::fini(&state);
    return -1;  // no errors
  }
};

#endif