Whamcloud - gitweb
LU-3289 gss: Add two additional security flavors for sk
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #ifndef __OBD_CKSUM
38 #define __OBD_CKSUM
39 #include <libcfs/libcfs.h>
40 #include <libcfs/libcfs_crypto.h>
41 #include <lustre/lustre_idl.h>
42
43 static inline unsigned char cksum_obd2cfs(cksum_type_t cksum_type)
44 {
45         switch (cksum_type) {
46         case OBD_CKSUM_CRC32:
47                 return CFS_HASH_ALG_CRC32;
48         case OBD_CKSUM_ADLER:
49                 return CFS_HASH_ALG_ADLER32;
50         case OBD_CKSUM_CRC32C:
51                 return CFS_HASH_ALG_CRC32C;
52         default:
53                 CERROR("Unknown checksum type (%x)!!!\n", cksum_type);
54                 LBUG();
55         }
56         return 0;
57 }
58
59 /* The OBD_FL_CKSUM_* flags is packed into 5 bits of o_flags, since there can
60  * only be a single checksum type per RPC.
61  *
62  * The OBD_CHECKSUM_* type bits passed in ocd_cksum_types are a 32-bit bitmask
63  * since they need to represent the full range of checksum algorithms that
64  * both the client and server can understand.
65  *
66  * In case of an unsupported types/flags we fall back to ADLER
67  * because that is supported by all clients since 1.8
68  *
69  * In case multiple algorithms are supported the best one is used. */
70 static inline u32 cksum_type_pack(cksum_type_t cksum_type)
71 {
72         unsigned int    performance = 0, tmp;
73         u32             flag = OBD_FL_CKSUM_ADLER;
74
75         if (cksum_type & OBD_CKSUM_CRC32) {
76                 tmp = cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32));
77                 if (tmp > performance) {
78                         performance = tmp;
79                         flag = OBD_FL_CKSUM_CRC32;
80                 }
81         }
82         if (cksum_type & OBD_CKSUM_CRC32C) {
83                 tmp = cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32C));
84                 if (tmp > performance) {
85                         performance = tmp;
86                         flag = OBD_FL_CKSUM_CRC32C;
87                 }
88         }
89         if (cksum_type & OBD_CKSUM_ADLER) {
90                 tmp = cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_ADLER));
91                 if (tmp > performance) {
92                         performance = tmp;
93                         flag = OBD_FL_CKSUM_ADLER;
94                 }
95         }
96         if (unlikely(cksum_type && !(cksum_type & (OBD_CKSUM_CRC32C |
97                                                    OBD_CKSUM_CRC32 |
98                                                    OBD_CKSUM_ADLER))))
99                 CWARN("unknown cksum type %x\n", cksum_type);
100
101         return flag;
102 }
103
104 static inline cksum_type_t cksum_type_unpack(u32 o_flags)
105 {
106         switch (o_flags & OBD_FL_CKSUM_ALL) {
107         case OBD_FL_CKSUM_CRC32C:
108                 return OBD_CKSUM_CRC32C;
109         case OBD_FL_CKSUM_CRC32:
110                 return OBD_CKSUM_CRC32;
111         default:
112                 break;
113         }
114
115         return OBD_CKSUM_ADLER;
116 }
117
118 /* Return a bitmask of the checksum types supported on this system.
119  * 1.8 supported ADLER it is base and not depend on hw
120  * Client uses all available local algos
121  */
122 static inline cksum_type_t cksum_types_supported_client(void)
123 {
124         cksum_type_t ret = OBD_CKSUM_ADLER;
125
126         CDEBUG(D_INFO, "Crypto hash speed: crc %d, crc32c %d, adler %d\n",
127                cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32)),
128                cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32C)),
129                cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_ADLER)));
130
131         if (cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32C)) > 0)
132                 ret |= OBD_CKSUM_CRC32C;
133         if (cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32)) > 0)
134                 ret |= OBD_CKSUM_CRC32;
135
136         return ret;
137 }
138
139 /* Server uses algos that perform at 50% or better of the Adler */
140 static inline cksum_type_t cksum_types_supported_server(void)
141 {
142         int          base_speed;
143         cksum_type_t    ret = OBD_CKSUM_ADLER;
144
145         CDEBUG(D_INFO, "Crypto hash speed: crc %d, crc32c %d, adler %d\n",
146                cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32)),
147                cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32C)),
148                cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_ADLER)));
149
150         base_speed = cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_ADLER)) / 2;
151
152         if (cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32C)) >=
153             base_speed)
154                 ret |= OBD_CKSUM_CRC32C;
155         if (cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32)) >=
156             base_speed)
157                 ret |= OBD_CKSUM_CRC32;
158
159         return ret;
160 }
161
162
163 /* Select the best checksum algorithm among those supplied in the cksum_types
164  * input.
165  *
166  * Currently, calling cksum_type_pack() with a mask will return the fastest
167  * checksum type due to its benchmarking at libcfs module load.
168  * Caution is advised, however, since what is fastest on a single client may
169  * not be the fastest or most efficient algorithm on the server.  */
170 static inline cksum_type_t cksum_type_select(cksum_type_t cksum_types)
171 {
172         return cksum_type_unpack(cksum_type_pack(cksum_types));
173 }
174
175 /* Checksum algorithm names. Must be defined in the same order as the
176  * OBD_CKSUM_* flags. */
177 #define DECLARE_CKSUM_NAME char *cksum_name[] = {"crc32", "adler", "crc32c"}
178
179 #endif /* __OBD_H */