Whamcloud - gitweb
LU-1201 checksum: add libcfs crypto hash
[fs/lustre-release.git] / lustre / include / obd_cksum.h
index 7d0729e..d546bb8 100644 (file)
@@ -1,6 +1,4 @@
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
- *
+/*
  * GPL HEADER START
  *
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
@@ -26,7 +24,7 @@
  * GPL HEADER END
  */
 /*
- * Copyright  2008 Sun Microsystems, Inc. All rights reserved
+ * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  */
 /*
 
 #ifndef __OBD_CKSUM
 #define __OBD_CKSUM
-
-#if defined(__linux__)
-#include <linux/obd_cksum.h>
-#elif defined(__APPLE__)
-#include <darwin/obd_chksum.h>
-#elif defined(__WINNT__)
-#include <winnt/obd_cksum.h>
-#else
-#error Unsupported operating system.
-#endif
-
+#include <libcfs/libcfs.h>
 #include <lustre/lustre_idl.h>
 
-/*
- * Checksums
- */
-
-#ifndef HAVE_ARCH_CRC32
-/* crc32_le lifted from the Linux kernel, which had the following to say:
- *
- * This code is in the public domain; copyright abandoned.
- * Liability for non-performance of this code is limited to the amount
- * you paid for it.  Since it is distributed for free, your refund will
- * be very very small.  If it breaks, you get to keep both pieces.
- */
-#define CRCPOLY_LE 0xedb88320
-/**
- * crc32_le() - Calculate bitwise little-endian Ethernet AUTODIN II CRC32
- * \param crc  seed value for computation.  ~0 for Ethernet, sometimes 0 for
- *             other uses, or the previous crc32 value if computing incrementally.
- * \param p  - pointer to buffer over which CRC is run
- * \param len- length of buffer \a p
- */
-static inline __u32 crc32_le(__u32 crc, unsigned char const *p, size_t len)
+static inline unsigned char cksum_obd2cfs(cksum_type_t cksum_type)
 {
-        int i;
-        while (len--) {
-                crc ^= *p++;
-                for (i = 0; i < 8; i++)
-                        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
-        }
-        return crc;
+       switch (cksum_type) {
+       case OBD_CKSUM_CRC32:
+               return CFS_HASH_ALG_CRC32;
+       case OBD_CKSUM_ADLER:
+               return CFS_HASH_ALG_ADLER32;
+       case OBD_CKSUM_CRC32C:
+               return CFS_HASH_ALG_CRC32C;
+       default:
+               CERROR("Unknown checksum type (%x)!!!\n", cksum_type);
+               LBUG();
+       }
+       return 0;
 }
-#endif
 
-static inline __u32 init_checksum(cksum_type_t cksum_type)
+/* The OBD_FL_CKSUM_* flags is packed into 5 bits of o_flags, since there can
+ * only be a single checksum type per RPC.
+ *
+ * The OBD_CHECKSUM_* type bits passed in ocd_cksum_types are a 32-bit bitmask
+ * since they need to represent the full range of checksum algorithms that
+ * both the client and server can understand.
+ *
+ * In case of an unsupported types/flags we fall back to ADLER
+ * because that is supported by all clients since 1.8
+ *
+ * In case multiple algorithms are supported the best one is used. */
+static inline obd_flag cksum_type_pack(cksum_type_t cksum_type)
 {
-        switch(cksum_type) {
-        case OBD_CKSUM_CRC32:
-                return ~0U;
-#ifdef HAVE_ADLER
-        case OBD_CKSUM_ADLER:
-                return 1U;
-#endif
-        default:
-                CERROR("Unknown checksum type (%x)!!!\n", cksum_type);
-                LBUG();
-        }
-        return 0;
+       unsigned int    performance = 0, tmp;
+       obd_flag        flag = OBD_FL_CKSUM_ADLER;
+
+       if (cksum_type & OBD_CKSUM_CRC32) {
+               tmp = cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32));
+               if (tmp > performance) {
+                       performance = tmp;
+                       flag = OBD_FL_CKSUM_CRC32;
+               }
+       }
+       if (cksum_type & OBD_CKSUM_CRC32C) {
+               tmp = cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32C));
+               if (tmp > performance) {
+                       performance = tmp;
+                       flag = OBD_FL_CKSUM_CRC32C;
+               }
+       }
+       if (cksum_type & OBD_CKSUM_ADLER) {
+               tmp = cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_ADLER));
+               if (tmp > performance) {
+                       performance = tmp;
+                       flag = OBD_FL_CKSUM_ADLER;
+               }
+       }
+       if (unlikely(cksum_type && !(cksum_type & (OBD_CKSUM_CRC32C |
+                                                  OBD_CKSUM_CRC32 |
+                                                  OBD_CKSUM_ADLER))))
+               CWARN("unknown cksum type %x\n", cksum_type);
+
+       return flag;
 }
 
-static inline __u32 compute_checksum(__u32 cksum, unsigned char const *p,
-                                     size_t len, cksum_type_t cksum_type)
+static inline cksum_type_t cksum_type_unpack(obd_flag o_flags)
 {
-        switch(cksum_type) {
-        case OBD_CKSUM_CRC32:
-                return crc32_le(cksum, p, len);
-#ifdef HAVE_ADLER
-        case OBD_CKSUM_ADLER:
-                return adler32(cksum, p, len);
-#endif
-        default:
-                CERROR("Unknown checksum type (%x)!!!\n", cksum_type);
-                LBUG();
-        }
-        return 0;
+       switch (o_flags & OBD_FL_CKSUM_ALL) {
+       case OBD_FL_CKSUM_CRC32C:
+               return OBD_CKSUM_CRC32C;
+       case OBD_FL_CKSUM_CRC32:
+               return OBD_CKSUM_CRC32;
+       default:
+               break;
+       }
+
+       return OBD_CKSUM_ADLER;
 }
 
-static inline obd_flag cksum_type_pack(cksum_type_t cksum_type)
+/* Return a bitmask of the checksum types supported on this system.
+ * 1.8 supported ADLER it is base and not depend on hw
+ * Client uses all available local algos
+ */
+static inline cksum_type_t cksum_types_supported_client(void)
 {
-        switch(cksum_type) {
-        case OBD_CKSUM_CRC32:
-                return OBD_FL_CKSUM_CRC32;
-#ifdef HAVE_ADLER
-        case OBD_CKSUM_ADLER:
-                return OBD_FL_CKSUM_ADLER;
-#endif
-        default:
-                CWARN("unknown cksum type %x\n", cksum_type);
-        }
-        return OBD_FL_CKSUM_CRC32;
+       cksum_type_t ret = OBD_CKSUM_ADLER;
+
+       CDEBUG(D_INFO, "Crypto hash speed: crc %d, crc32c %d, adler %d\n",
+              cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32)),
+              cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32C)),
+              cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_ADLER)));
+
+       if (cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32C)) > 0)
+               ret |= OBD_CKSUM_CRC32C;
+       if (cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32)) > 0)
+               ret |= OBD_CKSUM_CRC32;
+
+       return ret;
 }
 
-static inline cksum_type_t cksum_type_unpack(obd_flag o_flags)
+/* Server uses algos that perform at 50% or better of the Adler */
+static inline cksum_type_t cksum_types_supported_server(void)
 {
-        o_flags &= OBD_FL_CKSUM_ALL;
-        if ((o_flags - 1) & o_flags)
-                CWARN("several checksum types are set: %x\n", o_flags);
-        if (o_flags & OBD_FL_CKSUM_ADLER)
-#ifdef HAVE_ADLER
-                return OBD_CKSUM_ADLER;
-#else
-                CWARN("checksum type is set to adler32, but adler32 is not "
-                      "supported (%x)\n", o_flags);
-#endif
-        return OBD_CKSUM_CRC32;
+       int          base_speed;
+       cksum_type_t    ret = OBD_CKSUM_ADLER;
+
+       CDEBUG(D_INFO, "Crypto hash speed: crc %d, crc32c %d, adler %d\n",
+              cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32)),
+              cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32C)),
+              cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_ADLER)));
+
+       base_speed = cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_ADLER)) / 2;
+
+       if (cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32C)) >=
+           base_speed)
+               ret |= OBD_CKSUM_CRC32C;
+       if (cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32)) >=
+           base_speed)
+               ret |= OBD_CKSUM_CRC32;
+
+       return ret;
 }
 
-#ifdef HAVE_ADLER
-/* Default preferred checksum algorithm to use (if supported by the server) */
-#define OSC_DEFAULT_CKSUM OBD_CKSUM_ADLER
-/* Adler-32 is supported */
-#define CHECKSUM_ADLER OBD_CKSUM_ADLER
-#else
-#define OSC_DEFAULT_CKSUM OBD_CKSUM_CRC32
-#define CHECKSUM_ADLER 0
-#endif
 
-#define OBD_CKSUM_ALL (OBD_CKSUM_CRC32 | CHECKSUM_ADLER)
+/* Select the best checksum algorithm among those supplied in the cksum_types
+ * input.
+ *
+ * Currently, calling cksum_type_pack() with a mask will return the fastest
+ * checksum type due to its benchmarking at libcfs module load.
+ * Caution is advised, however, since what is fastest on a single client may
+ * not be the fastest or most efficient algorithm on the server.  */
+static inline cksum_type_t cksum_type_select(cksum_type_t cksum_types)
+{
+       return cksum_type_unpack(cksum_type_pack(cksum_types));
+}
 
 /* Checksum algorithm names. Must be defined in the same order as the
  * OBD_CKSUM_* flags. */
-#define DECLARE_CKSUM_NAME char *cksum_name[] = {"crc32", "adler"}
+#define DECLARE_CKSUM_NAME char *cksum_name[] = {"crc32", "adler", "crc32c"}
 
 #endif /* __OBD_H */