Whamcloud - gitweb
LU-14742 socklnd: detect link state to set fatal error on ni
[fs/lustre-release.git] / lustre / obdclass / obd_cksum.c
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) 2018, DataDirect Networks Storage.
24  * Author: Li Xi.
25  *
26  * Checksum functions
27  */
28 #include <obd_class.h>
29 #include <obd_cksum.h>
30
31 /* Server uses algos that perform at 50% or better of the Adler */
32 enum cksum_types obd_cksum_types_supported_server(const char *obd_name)
33 {
34         enum cksum_types ret = OBD_CKSUM_ADLER;
35         int base_speed;
36
37         CDEBUG(D_INFO, "%s: checksum speed: crc %d, crc32c %d, adler %d, "
38                "t10ip512 %d, t10ip4k %d, t10crc512 %d, t10crc4k %d\n",
39                obd_name,
40                cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32)),
41                cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32C)),
42                cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_ADLER)),
43                obd_t10_cksum_speed(obd_name, OBD_CKSUM_T10IP512),
44                obd_t10_cksum_speed(obd_name, OBD_CKSUM_T10IP4K),
45                obd_t10_cksum_speed(obd_name, OBD_CKSUM_T10CRC512),
46                obd_t10_cksum_speed(obd_name, OBD_CKSUM_T10CRC4K));
47
48         base_speed = cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_ADLER)) / 2;
49
50         if (cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32C)) >=
51             base_speed)
52                 ret |= OBD_CKSUM_CRC32C;
53
54         if (cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32)) >=
55             base_speed)
56                 ret |= OBD_CKSUM_CRC32;
57
58         if (obd_t10_cksum_speed(obd_name, OBD_CKSUM_T10IP512) >= base_speed)
59                 ret |= OBD_CKSUM_T10IP512;
60
61         if (obd_t10_cksum_speed(obd_name, OBD_CKSUM_T10IP4K) >= base_speed)
62                 ret |= OBD_CKSUM_T10IP4K;
63
64         if (obd_t10_cksum_speed(obd_name, OBD_CKSUM_T10CRC512) >= base_speed)
65                 ret |= OBD_CKSUM_T10CRC512;
66
67         if (obd_t10_cksum_speed(obd_name, OBD_CKSUM_T10CRC4K) >= base_speed)
68                 ret |= OBD_CKSUM_T10CRC4K;
69
70         return ret;
71 }
72 EXPORT_SYMBOL(obd_cksum_types_supported_server);
73
74 /* The OBD_FL_CKSUM_* flags is packed into 5 bits of o_flags, since there can
75  * only be a single checksum type per RPC.
76  *
77  * The OBD_CKSUM_* type bits passed in ocd_cksum_types are a 32-bit bitmask
78  * since they need to represent the full range of checksum algorithms that
79  * both the client and server can understand.
80  *
81  * In case of an unsupported types/flags we fall back to ADLER
82  * because that is supported by all clients since 1.8
83  *
84  * In case multiple algorithms are supported the best one is used. */
85 u32 obd_cksum_type_pack(const char *obd_name, enum cksum_types cksum_type)
86 {
87         unsigned int performance = 0, tmp;
88         u32 flag = OBD_FL_CKSUM_ADLER;
89
90         if (cksum_type & OBD_CKSUM_CRC32) {
91                 tmp = cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32));
92                 if (tmp > performance) {
93                         performance = tmp;
94                         flag = OBD_FL_CKSUM_CRC32;
95                 }
96         }
97         if (cksum_type & OBD_CKSUM_CRC32C) {
98                 tmp = cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32C));
99                 if (tmp > performance) {
100                         performance = tmp;
101                         flag = OBD_FL_CKSUM_CRC32C;
102                 }
103         }
104         if (cksum_type & OBD_CKSUM_ADLER) {
105                 tmp = cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_ADLER));
106                 if (tmp > performance) {
107                         performance = tmp;
108                         flag = OBD_FL_CKSUM_ADLER;
109                 }
110         }
111
112         if (cksum_type & OBD_CKSUM_T10IP512) {
113                 tmp = obd_t10_cksum_speed(obd_name, OBD_CKSUM_T10IP512);
114                 if (tmp > performance) {
115                         performance = tmp;
116                         flag = OBD_FL_CKSUM_T10IP512;
117                 }
118         }
119
120         if (cksum_type & OBD_CKSUM_T10IP4K) {
121                 tmp = obd_t10_cksum_speed(obd_name, OBD_CKSUM_T10IP4K);
122                 if (tmp > performance) {
123                         performance = tmp;
124                         flag = OBD_FL_CKSUM_T10IP4K;
125                 }
126         }
127
128         if (cksum_type & OBD_CKSUM_T10CRC512) {
129                 tmp = obd_t10_cksum_speed(obd_name, OBD_CKSUM_T10CRC512);
130                 if (tmp > performance) {
131                         performance = tmp;
132                         flag = OBD_FL_CKSUM_T10CRC512;
133                 }
134         }
135
136         if (cksum_type & OBD_CKSUM_T10CRC4K) {
137                 tmp = obd_t10_cksum_speed(obd_name, OBD_CKSUM_T10CRC4K);
138                 if (tmp > performance) {
139                         performance = tmp;
140                         flag = OBD_FL_CKSUM_T10CRC4K;
141                 }
142         }
143
144         if (unlikely(cksum_type && !(cksum_type & OBD_CKSUM_ALL)))
145                 CWARN("%s: unknown cksum type %x\n", obd_name, cksum_type);
146
147         return flag;
148 }
149 EXPORT_SYMBOL(obd_cksum_type_pack);