Whamcloud - gitweb
LU-1347 build: remove the vim/emacs modelines
[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 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  */
34
35 #ifndef __OBD_CKSUM
36 #define __OBD_CKSUM
37
38 #if defined(__linux__)
39 #include <linux/obd_cksum.h>
40 #elif defined(__APPLE__)
41 #include <darwin/obd_chksum.h>
42 #elif defined(__WINNT__)
43 #include <winnt/obd_cksum.h>
44 #else
45 #error Unsupported operating system.
46 #endif
47
48 #include <lustre/lustre_idl.h>
49
50 /*
51  * Checksums
52  */
53
54 #ifndef HAVE_ARCH_CRC32
55 /* crc32_le lifted from the Linux kernel, which had the following to say:
56  *
57  * This code is in the public domain; copyright abandoned.
58  * Liability for non-performance of this code is limited to the amount
59  * you paid for it.  Since it is distributed for free, your refund will
60  * be very very small.  If it breaks, you get to keep both pieces.
61  */
62 #define CRCPOLY_LE 0xedb88320
63 /**
64  * crc32_le() - Calculate bitwise little-endian Ethernet AUTODIN II CRC32
65  * \param crc  seed value for computation.  ~0 for Ethernet, sometimes 0 for
66  *             other uses, or the previous crc32 value if computing incrementally.
67  * \param p  - pointer to buffer over which CRC is run
68  * \param len- length of buffer \a p
69  */
70 static inline __u32 crc32_le(__u32 crc, unsigned char const *p, size_t len)
71 {
72         int i;
73         while (len--) {
74                 crc ^= *p++;
75                 for (i = 0; i < 8; i++)
76                         crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
77         }
78         return crc;
79 }
80 #endif
81  
82 #ifdef HAVE_ADLER
83 /* Adler-32 is supported */
84 #define CHECKSUM_ADLER OBD_CKSUM_ADLER
85 #else
86 #define CHECKSUM_ADLER 0
87 #endif
88
89 #ifdef X86_FEATURE_XMM4_2
90 /* Call Nehalem+ CRC32C harware acceleration instruction on individual bytes. */
91 static inline __u32 crc32c_hw_byte(__u32 crc, unsigned char const *p,
92                                    size_t bytes)
93 {
94         while (bytes--) {
95                 __asm__ __volatile__ (
96                         ".byte 0xf2, 0xf, 0x38, 0xf0, 0xf1"
97                         : "=S"(crc)
98                         : "0"(crc), "c"(*p)
99                 );
100                 p++;
101         }
102
103         return crc;
104 }
105
106 #if BITS_PER_LONG > 32
107 #define WORD_SHIFT 3
108 #define WORD_MASK  7
109 #define REX "0x48, "
110 #else
111 #define WORD_SHIFT 2
112 #define WORD_MASK  3
113 #define REX ""
114 #endif
115
116 /* Do we need to worry about unaligned input data here? */
117 static inline __u32 crc32c_hw(__u32 crc, unsigned char const *p, size_t len)
118 {
119         unsigned int words = len >> WORD_SHIFT;
120         unsigned int bytes = len &  WORD_MASK;
121         long *ptmp = (long *)p;
122
123         while (words--) {
124                 __asm__ __volatile__(
125                         ".byte 0xf2, " REX "0xf, 0x38, 0xf1, 0xf1;"
126                         : "=S"(crc)
127                         : "0"(crc), "c"(*ptmp)
128                 );
129                 ptmp++;
130         }
131
132         if (bytes)
133                 crc = crc32c_hw_byte(crc, (unsigned char *)ptmp, bytes);
134
135         return crc;
136 }
137 #else
138 /* We should never call this unless the CPU has previously been detected to
139  * support this instruction in the SSE4.2 feature set. b=23549  */
140 static inline __u32 crc32c_hw(__u32 crc, unsigned char const *p,size_t len)
141 {
142         LBUG();
143 }
144 #endif
145
146 static inline __u32 init_checksum(cksum_type_t cksum_type)
147 {
148         switch(cksum_type) {
149         case OBD_CKSUM_CRC32C:
150                 return ~0U;
151 #ifdef HAVE_ADLER
152         case OBD_CKSUM_ADLER:
153                 return 1U;
154 #endif
155         case OBD_CKSUM_CRC32:
156                 return ~0U;
157         default:
158                 CERROR("Unknown checksum type (%x)!!!\n", cksum_type);
159                 LBUG();
160         }
161         return 0;
162 }
163
164 static inline __u32 fini_checksum(__u32 cksum, cksum_type_t cksum_type)
165 {
166         if (cksum_type == OBD_CKSUM_CRC32C)
167                 return ~cksum;
168         return cksum;
169 }
170
171 static inline __u32 compute_checksum(__u32 cksum, unsigned char const *p,
172                                      size_t len, cksum_type_t cksum_type)
173 {
174         switch(cksum_type) {
175         case OBD_CKSUM_CRC32C:
176                 return crc32c_hw(cksum, p, len);
177 #ifdef HAVE_ADLER
178         case OBD_CKSUM_ADLER:
179                 return adler32(cksum, p, len);
180 #endif
181         case OBD_CKSUM_CRC32:
182                 return crc32_le(cksum, p, len);
183         default:
184                 CERROR("Unknown checksum type (%x)!!!\n", cksum_type);
185                 LBUG();
186         }
187         return 0;
188 }
189
190 /* The OBD_FL_CKSUM_* flags is packed into 5 bits of o_flags, since there can
191  * only be a single checksum type per RPC.
192  *
193  * The OBD_CHECKSUM_* type bits passed in ocd_cksum_types are a 32-bit bitmask
194  * since they need to represent the full range of checksum algorithms that
195  * both the client and server can understand.
196  *
197  * In case of an unsupported types/flags we fall back to CRC32 (even though
198  * it isn't very fast) because that is supported by all clients
199  * checksums, since 1.6.5 (or earlier via patches).
200  *
201  * These flags should be listed in order of descending performance, so that
202  * in case multiple algorithms are supported the best one is used. */
203 static inline obd_flag cksum_type_pack(cksum_type_t cksum_type)
204 {
205         if (cksum_type & OBD_CKSUM_CRC32C)
206                 return OBD_FL_CKSUM_CRC32C;
207 #ifdef HAVE_ADLER
208         if (cksum_type & OBD_CKSUM_ADLER)
209                 return OBD_FL_CKSUM_ADLER;
210 #endif
211         if (unlikely(cksum_type && !(cksum_type & OBD_CKSUM_CRC32)))
212                 CWARN("unknown cksum type %x\n", cksum_type);
213
214         return OBD_FL_CKSUM_CRC32;
215 }
216
217 static inline cksum_type_t cksum_type_unpack(obd_flag o_flags)
218 {
219         switch (o_flags & OBD_FL_CKSUM_ALL) {
220         case OBD_FL_CKSUM_CRC32C:
221                 return OBD_CKSUM_CRC32C;
222         case OBD_FL_CKSUM_ADLER:
223 #ifdef HAVE_ADLER
224                 return OBD_CKSUM_ADLER;
225 #else
226                 CWARN("checksum type is set to adler32, but adler32 is not "
227                       "supported (%x)\n", o_flags);
228                 break;
229 #endif
230         default:
231                 break;
232         }
233
234         /* 1.6.4- only supported CRC32 and didn't set o_flags */
235         return OBD_CKSUM_CRC32;
236 }
237
238 /* Return a bitmask of the checksum types supported on this system.
239  *
240  * CRC32 is a required for compatibility (starting with 1.6.5),
241  * after which we could move to Adler as the base checksum type.
242  *
243  * If hardware crc32c support is not available, it is slower than Adler,
244  * so don't include it, even if it could be emulated in software. b=23549 */
245 static inline cksum_type_t cksum_types_supported(void)
246 {
247         cksum_type_t ret = OBD_CKSUM_CRC32;
248
249 #ifdef X86_FEATURE_XMM4_2
250         if (cpu_has_xmm4_2)
251                 ret |= OBD_CKSUM_CRC32C;
252 #endif
253 #ifdef HAVE_ADLER
254         ret |= OBD_CKSUM_ADLER;
255 #endif
256         return ret;
257 }
258
259 /* Select the best checksum algorithm among those supplied in the cksum_types
260  * input.
261  *
262  * Currently, calling cksum_type_pack() with a mask will return the fastest
263  * checksum type due to its ordering, but in the future we might want to
264  * determine this based on benchmarking the different algorithms quickly.
265  * Caution is advised, however, since what is fastest on a single client may
266  * not be the fastest or most efficient algorithm on the server.  */
267 static inline cksum_type_t cksum_type_select(cksum_type_t cksum_types)
268 {
269         return cksum_type_unpack(cksum_type_pack(cksum_types));
270 }
271
272 /* Checksum algorithm names. Must be defined in the same order as the
273  * OBD_CKSUM_* flags. */
274 #define DECLARE_CKSUM_NAME char *cksum_name[] = {"crc32", "adler", "crc32c"}
275
276 #endif /* __OBD_H */