4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2014, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
35 #include <libcfs/libcfs.h>
36 #include <libcfs/libcfs_crypto.h>
37 #include <lustre/lustre_idl.h>
39 static inline unsigned char cksum_obd2cfs(cksum_type_t cksum_type)
43 return CFS_HASH_ALG_CRC32;
45 return CFS_HASH_ALG_ADLER32;
46 case OBD_CKSUM_CRC32C:
47 return CFS_HASH_ALG_CRC32C;
49 CERROR("Unknown checksum type (%x)!!!\n", cksum_type);
55 /* The OBD_FL_CKSUM_* flags is packed into 5 bits of o_flags, since there can
56 * only be a single checksum type per RPC.
58 * The OBD_CHECKSUM_* type bits passed in ocd_cksum_types are a 32-bit bitmask
59 * since they need to represent the full range of checksum algorithms that
60 * both the client and server can understand.
62 * In case of an unsupported types/flags we fall back to ADLER
63 * because that is supported by all clients since 1.8
65 * In case multiple algorithms are supported the best one is used. */
66 static inline u32 cksum_type_pack(cksum_type_t cksum_type)
68 unsigned int performance = 0, tmp;
69 u32 flag = OBD_FL_CKSUM_ADLER;
71 if (cksum_type & OBD_CKSUM_CRC32) {
72 tmp = cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32));
73 if (tmp > performance) {
75 flag = OBD_FL_CKSUM_CRC32;
78 if (cksum_type & OBD_CKSUM_CRC32C) {
79 tmp = cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32C));
80 if (tmp > performance) {
82 flag = OBD_FL_CKSUM_CRC32C;
85 if (cksum_type & OBD_CKSUM_ADLER) {
86 tmp = cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_ADLER));
87 if (tmp > performance) {
89 flag = OBD_FL_CKSUM_ADLER;
92 if (unlikely(cksum_type && !(cksum_type & (OBD_CKSUM_CRC32C |
95 CWARN("unknown cksum type %x\n", cksum_type);
100 static inline cksum_type_t cksum_type_unpack(u32 o_flags)
102 switch (o_flags & OBD_FL_CKSUM_ALL) {
103 case OBD_FL_CKSUM_CRC32C:
104 return OBD_CKSUM_CRC32C;
105 case OBD_FL_CKSUM_CRC32:
106 return OBD_CKSUM_CRC32;
111 return OBD_CKSUM_ADLER;
114 /* Return a bitmask of the checksum types supported on this system.
115 * 1.8 supported ADLER it is base and not depend on hw
116 * Client uses all available local algos
118 static inline cksum_type_t cksum_types_supported_client(void)
120 cksum_type_t ret = OBD_CKSUM_ADLER;
122 CDEBUG(D_INFO, "Crypto hash speed: crc %d, crc32c %d, adler %d\n",
123 cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32)),
124 cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32C)),
125 cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_ADLER)));
127 if (cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32C)) > 0)
128 ret |= OBD_CKSUM_CRC32C;
129 if (cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32)) > 0)
130 ret |= OBD_CKSUM_CRC32;
135 /* Server uses algos that perform at 50% or better of the Adler */
136 static inline enum cksum_types cksum_types_supported_server(void)
138 enum cksum_types ret = OBD_CKSUM_ADLER;
141 CDEBUG(D_INFO, "Crypto hash speed: crc %d, crc32c %d, adler %d\n",
142 cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32)),
143 cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32C)),
144 cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_ADLER)));
146 base_speed = cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_ADLER)) / 2;
148 if (cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32C)) >=
150 ret |= OBD_CKSUM_CRC32C;
151 if (cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32)) >=
153 ret |= OBD_CKSUM_CRC32;
159 /* Select the best checksum algorithm among those supplied in the cksum_types
162 * Currently, calling cksum_type_pack() with a mask will return the fastest
163 * checksum type due to its benchmarking at libcfs module load.
164 * Caution is advised, however, since what is fastest on a single client may
165 * not be the fastest or most efficient algorithm on the server. */
166 static inline cksum_type_t cksum_type_select(cksum_type_t cksum_types)
168 return cksum_type_unpack(cksum_type_pack(cksum_types));
171 /* Checksum algorithm names. Must be defined in the same order as the
172 * OBD_CKSUM_* flags. */
173 #define DECLARE_CKSUM_NAME char *cksum_name[] = {"crc32", "adler", "crc32c"}