]>
git.proxmox.com Git - ceph.git/blob - ceph/src/common/Checksummer.h
6611e8749f4d3a80b622cd8dafcc6492e265fe78
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #ifndef CEPH_OS_BLUESTORE_CHECKSUMMER
5 #define CEPH_OS_BLUESTORE_CHECKSUMMER
7 #include "include/buffer.h"
8 #include "xxHash/xxhash.h"
13 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
17 CSUM_CRC32C_16
= 5 , // low 16 bits of crc32c
18 CSUM_CRC32C_8
= 6 , // low 8 bits of crc32c
21 static const char * get_csum_type_string ( unsigned t
) {
23 case CSUM_NONE
: return "none" ;
24 case CSUM_XXHASH32
: return "xxhash32" ;
25 case CSUM_XXHASH64
: return "xxhash64" ;
26 case CSUM_CRC32C
: return "crc32c" ;
27 case CSUM_CRC32C_16
: return "crc32c_16" ;
28 case CSUM_CRC32C_8
: return "crc32c_8" ;
29 default : return "???" ;
32 static int get_csum_string_type ( const std :: string
& s
) {
42 return CSUM_CRC32C_16
;
48 static size_t get_csum_init_value_size ( int csum_type
) {
50 case CSUM_NONE
: return 0 ;
51 case CSUM_XXHASH32
: return sizeof ( xxhash32 :: init_value_t
);
52 case CSUM_XXHASH64
: return sizeof ( xxhash64 :: init_value_t
);
53 case CSUM_CRC32C
: return sizeof ( crc32c :: init_value_t
);
54 case CSUM_CRC32C_16
: return sizeof ( crc32c_16 :: init_value_t
);
55 case CSUM_CRC32C_8
: return sizeof ( crc32c_8 :: init_value_t
);
59 static size_t get_csum_value_size ( int csum_type
) {
61 case CSUM_NONE
: return 0 ;
62 case CSUM_XXHASH32
: return 4 ;
63 case CSUM_XXHASH64
: return 8 ;
64 case CSUM_CRC32C
: return 4 ;
65 case CSUM_CRC32C_16
: return 2 ;
66 case CSUM_CRC32C_8
: return 1 ;
72 typedef uint32_t init_value_t
;
73 typedef __le32 value_t
;
75 // we have no execution context/state.
77 static void init ( state_t
* state
) {
79 static void fini ( state_t
* state
) {
84 init_value_t init_value
,
86 bufferlist :: const_iterator
& p
88 return p
. crc32c ( len
, init_value
);
93 typedef uint32_t init_value_t
;
94 typedef __le16 value_t
;
96 // we have no execution context/state.
98 static void init ( state_t
* state
) {
100 static void fini ( state_t
* state
) {
105 init_value_t init_value
,
107 bufferlist :: const_iterator
& p
109 return p
. crc32c ( len
, init_value
) & 0xffff ;
114 typedef uint32_t init_value_t
;
115 typedef __u8 value_t
;
117 // we have no execution context/state.
119 static void init ( state_t
* state
) {
121 static void fini ( state_t
* state
) {
126 init_value_t init_value
,
128 bufferlist :: const_iterator
& p
130 return p
. crc32c ( len
, init_value
) & 0xff ;
135 typedef uint32_t init_value_t
;
136 typedef __le32 value_t
;
138 typedef XXH32_state_t
* state_t
;
139 static void init ( state_t
* s
) {
140 * s
= XXH32_createState ();
142 static void fini ( state_t
* s
) {
148 init_value_t init_value
,
150 bufferlist :: const_iterator
& p
152 XXH32_reset ( state
, init_value
);
155 size_t l
= p
. get_ptr_and_advance ( len
, & data
);
156 XXH32_update ( state
, data
, l
);
159 return XXH32_digest ( state
);
164 typedef uint64_t init_value_t
;
165 typedef __le64 value_t
;
167 typedef XXH64_state_t
* state_t
;
168 static void init ( state_t
* s
) {
169 * s
= XXH64_createState ();
171 static void fini ( state_t
* s
) {
177 init_value_t init_value
,
179 bufferlist :: const_iterator
& p
181 XXH64_reset ( state
, init_value
);
184 size_t l
= p
. get_ptr_and_advance ( len
, & data
);
185 XXH64_update ( state
, data
, l
);
188 return XXH64_digest ( state
);
193 static int calculate (
194 size_t csum_block_size
,
197 const bufferlist
& bl
,
200 return calculate
< Alg
>(- 1 , csum_block_size
, offset
, length
, bl
, csum_data
);
204 static int calculate (
205 typename
Alg :: init_value_t init_value
,
206 size_t csum_block_size
,
209 const bufferlist
& bl
,
210 bufferptr
* csum_data
) {
211 assert ( length
% csum_block_size
== 0 );
212 size_t blocks
= length
/ csum_block_size
;
213 bufferlist :: const_iterator p
= bl
. begin ();
214 assert ( bl
. length () >= length
);
216 typename
Alg :: state_t state
;
219 assert ( csum_data
-> length () >= ( offset
+ length
) / csum_block_size
*
220 sizeof ( typename
Alg :: value_t
));
222 typename
Alg :: value_t
* pv
=
223 reinterpret_cast < typename
Alg :: value_t
*>( csum_data
-> c_str ());
224 pv
+= offset
/ csum_block_size
;
226 * pv
= Alg :: calc ( state
, init_value
, csum_block_size
, p
);
235 size_t csum_block_size
,
238 const bufferlist
& bl
,
239 const bufferptr
& csum_data
,
242 assert ( length
% csum_block_size
== 0 );
243 bufferlist :: const_iterator p
= bl
. begin ();
244 assert ( bl
. length () >= length
);
246 typename
Alg :: state_t state
;
249 const typename
Alg :: value_t
* pv
=
250 reinterpret_cast < const typename
Alg :: value_t
*>( csum_data
. c_str ());
251 pv
+= offset
/ csum_block_size
;
254 typename
Alg :: value_t v
= Alg :: calc ( state
, - 1 , csum_block_size
, p
);
263 pos
+= csum_block_size
;
264 length
-= csum_block_size
;
267 return - 1 ; // no errors