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.sun.com/software/products/lustre/docs/GPLv2.pdf
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
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
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>
45 #error Unsupported operating system.
48 #include <lustre/lustre_idl.h>
54 #ifndef HAVE_ARCH_CRC32
55 /* crc32_le lifted from the Linux kernel, which had the following to say:
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.
62 #define CRCPOLY_LE 0xedb88320
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
70 static inline __u32 crc32_le(__u32 crc, unsigned char const *p, size_t len)
75 for (i = 0; i < 8; i++)
76 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
83 /* Adler-32 is supported */
84 #define CHECKSUM_ADLER OBD_CKSUM_ADLER
86 #define CHECKSUM_ADLER 0
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,
95 __asm__ __volatile__ (
96 ".byte 0xf2, 0xf, 0x38, 0xf0, 0xf1"
106 #if BITS_PER_LONG > 32
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)
119 unsigned int words = len >> WORD_SHIFT;
120 unsigned int bytes = len & WORD_MASK;
121 long *ptmp = (long *)p;
124 __asm__ __volatile__(
125 ".byte 0xf2, " REX "0xf, 0x38, 0xf1, 0xf1;"
127 : "0"(crc), "c"(*ptmp)
133 crc = crc32c_hw_byte(crc, (unsigned char *)ptmp, bytes);
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)
146 static inline __u32 init_checksum(cksum_type_t cksum_type)
149 case OBD_CKSUM_CRC32C:
152 case OBD_CKSUM_ADLER:
155 case OBD_CKSUM_CRC32:
158 CERROR("Unknown checksum type (%x)!!!\n", cksum_type);
164 static inline __u32 fini_checksum(__u32 cksum, cksum_type_t cksum_type)
166 if (cksum_type == OBD_CKSUM_CRC32C)
171 static inline __u32 compute_checksum(__u32 cksum, unsigned char const *p,
172 size_t len, cksum_type_t cksum_type)
175 case OBD_CKSUM_CRC32C:
176 return crc32c_hw(cksum, p, len);
178 case OBD_CKSUM_ADLER:
179 return adler32(cksum, p, len);
181 case OBD_CKSUM_CRC32:
182 return crc32_le(cksum, p, len);
184 CERROR("Unknown checksum type (%x)!!!\n", cksum_type);
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.
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.
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).
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)
205 if (cksum_type & OBD_CKSUM_CRC32C)
206 return OBD_FL_CKSUM_CRC32C;
208 if (cksum_type & OBD_CKSUM_ADLER)
209 return OBD_FL_CKSUM_ADLER;
211 if (unlikely(cksum_type && !(cksum_type & OBD_CKSUM_CRC32)))
212 CWARN("unknown cksum type %x\n", cksum_type);
214 return OBD_FL_CKSUM_CRC32;
217 static inline cksum_type_t cksum_type_unpack(obd_flag o_flags)
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:
224 return OBD_CKSUM_ADLER;
226 CWARN("checksum type is set to adler32, but adler32 is not "
227 "supported (%x)\n", o_flags);
234 /* 1.6.4- only supported CRC32 and didn't set o_flags */
235 return OBD_CKSUM_CRC32;
238 /* Return a bitmask of the checksum types supported on this system.
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.
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)
247 cksum_type_t ret = OBD_CKSUM_CRC32;
249 #ifdef X86_FEATURE_XMM4_2
251 ret |= OBD_CKSUM_CRC32C;
254 ret |= OBD_CKSUM_ADLER;
259 /* Select the best checksum algorithm among those supplied in the cksum_types
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)
269 return cksum_type_unpack(cksum_type_pack(cksum_types));
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"}