Whamcloud - gitweb
LU-8066 obdclass: move lustre sysctl to sysfs
[fs/lustre-release.git] / lustre / include / obd_cksum.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2014, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #ifndef __OBD_CKSUM
34 #define __OBD_CKSUM
35 #include <libcfs/libcfs.h>
36 #include <libcfs/libcfs_crypto.h>
37 #include <lustre/lustre_idl.h>
38
39 static inline unsigned char cksum_obd2cfs(cksum_type_t cksum_type)
40 {
41         switch (cksum_type) {
42         case OBD_CKSUM_CRC32:
43                 return CFS_HASH_ALG_CRC32;
44         case OBD_CKSUM_ADLER:
45                 return CFS_HASH_ALG_ADLER32;
46         case OBD_CKSUM_CRC32C:
47                 return CFS_HASH_ALG_CRC32C;
48         default:
49                 CERROR("Unknown checksum type (%x)!!!\n", cksum_type);
50                 LBUG();
51         }
52         return 0;
53 }
54
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.
57  *
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.
61  *
62  * In case of an unsupported types/flags we fall back to ADLER
63  * because that is supported by all clients since 1.8
64  *
65  * In case multiple algorithms are supported the best one is used. */
66 static inline u32 cksum_type_pack(cksum_type_t cksum_type)
67 {
68         unsigned int    performance = 0, tmp;
69         u32             flag = OBD_FL_CKSUM_ADLER;
70
71         if (cksum_type & OBD_CKSUM_CRC32) {
72                 tmp = cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32));
73                 if (tmp > performance) {
74                         performance = tmp;
75                         flag = OBD_FL_CKSUM_CRC32;
76                 }
77         }
78         if (cksum_type & OBD_CKSUM_CRC32C) {
79                 tmp = cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32C));
80                 if (tmp > performance) {
81                         performance = tmp;
82                         flag = OBD_FL_CKSUM_CRC32C;
83                 }
84         }
85         if (cksum_type & OBD_CKSUM_ADLER) {
86                 tmp = cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_ADLER));
87                 if (tmp > performance) {
88                         performance = tmp;
89                         flag = OBD_FL_CKSUM_ADLER;
90                 }
91         }
92         if (unlikely(cksum_type && !(cksum_type & (OBD_CKSUM_CRC32C |
93                                                    OBD_CKSUM_CRC32 |
94                                                    OBD_CKSUM_ADLER))))
95                 CWARN("unknown cksum type %x\n", cksum_type);
96
97         return flag;
98 }
99
100 static inline cksum_type_t cksum_type_unpack(u32 o_flags)
101 {
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;
107         default:
108                 break;
109         }
110
111         return OBD_CKSUM_ADLER;
112 }
113
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
117  */
118 static inline cksum_type_t cksum_types_supported_client(void)
119 {
120         cksum_type_t ret = OBD_CKSUM_ADLER;
121
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)));
126
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;
131
132         return ret;
133 }
134
135 /* Server uses algos that perform at 50% or better of the Adler */
136 static inline cksum_type_t cksum_types_supported_server(void)
137 {
138         int          base_speed;
139         cksum_type_t    ret = OBD_CKSUM_ADLER;
140
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)));
145
146         base_speed = cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_ADLER)) / 2;
147
148         if (cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32C)) >=
149             base_speed)
150                 ret |= OBD_CKSUM_CRC32C;
151         if (cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32)) >=
152             base_speed)
153                 ret |= OBD_CKSUM_CRC32;
154
155         return ret;
156 }
157
158
159 /* Select the best checksum algorithm among those supplied in the cksum_types
160  * input.
161  *
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)
167 {
168         return cksum_type_unpack(cksum_type_pack(cksum_types));
169 }
170
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"}
174
175 #endif /* __OBD_H */