#ifdef __KERNEL__
#ifndef AUTOCONF_INCLUDED
#include <linux/config.h>
+#include <asm/cpufeature.h>
+#include <asm/processor.h>
#endif
#include <linux/seq_file.h>
#include <linux/module.h>
typedef enum {
OBD_CKSUM_CRC32 = 0x00000001,
OBD_CKSUM_ADLER = 0x00000002,
+ OBD_CKSUM_CRC32C= 0x00000004,
} cksum_type_t;
/*
OBD_FL_SRVLOCK = 0x00000800, /* delegate DLM locking to server */
OBD_FL_CKSUM_CRC32 = 0x00001000, /* CRC32 checksum type */
OBD_FL_CKSUM_ADLER = 0x00002000, /* ADLER checksum type */
- OBD_FL_CKSUM_RSVD1 = 0x00004000, /* for future cksum types */
+ OBD_FL_CKSUM_CRC32C = 0x00004000, /* CRC32C checksum type */
OBD_FL_CKSUM_RSVD2 = 0x00008000, /* for future cksum types */
OBD_FL_CKSUM_RSVD3 = 0x00010000, /* for future cksum types */
OBD_FL_SHRINK_GRANT = 0x00020000, /* object shrink the grant */
OBD_FL_RECOV_RESEND = 0x00080000, /* recoverable resent */
OBD_FL_NOSPC_BLK = 0x00100000, /* no more block space on OST */
- OBD_FL_CKSUM_ALL = OBD_FL_CKSUM_CRC32 | OBD_FL_CKSUM_ADLER,
+ /* Note that while these checksum values are currently separate bits,
+ * in 2.x we can actually allow all values from 1-31 if we wanted. */
+ OBD_FL_CKSUM_ALL = OBD_FL_CKSUM_CRC32 | OBD_FL_CKSUM_ADLER |
+ OBD_FL_CKSUM_CRC32C,
/* mask for local-only flag, which won't be sent over network */
OBD_FL_LOCAL_MASK = 0xF0000000,
return crc;
}
#endif
+
+#ifdef HAVE_ADLER
+/* Adler-32 is supported */
+#define CHECKSUM_ADLER OBD_CKSUM_ADLER
+#else
+#define CHECKSUM_ADLER 0
+#endif
+
+#ifdef X86_FEATURE_XMM4_2
+/* Call Nehalem+ CRC32C harware acceleration instruction on individual bytes. */
+static inline __u32 crc32c_hw_byte(__u32 crc, unsigned char const *p,
+ size_t bytes)
+{
+ while (bytes--) {
+ __asm__ __volatile__ (
+ ".byte 0xf2, 0xf, 0x38, 0xf0, 0xf1"
+ : "=S"(crc)
+ : "0"(crc), "c"(*p)
+ );
+ p++;
+ }
+
+ return crc;
+}
+
+#if BITS_PER_LONG > 32
+#define WORD_SHIFT 3
+#define WORD_MASK 7
+#define REX "0x48, "
+#else
+#define WORD_SHIFT 2
+#define WORD_MASK 3
+#define REX ""
+#endif
+
+/* Do we need to worry about unaligned input data here? */
+static inline __u32 crc32c_hw(__u32 crc, unsigned char const *p, size_t len)
+{
+ unsigned int words = len >> WORD_SHIFT;
+ unsigned int bytes = len & WORD_MASK;
+ long *ptmp = (long *)p;
+
+ while (words--) {
+ __asm__ __volatile__(
+ ".byte 0xf2, " REX "0xf, 0x38, 0xf1, 0xf1;"
+ : "=S"(crc)
+ : "0"(crc), "c"(*ptmp)
+ );
+ ptmp++;
+ }
+
+ if (bytes)
+ crc = crc32c_hw_byte(crc, (unsigned char *)ptmp, bytes);
+
+ return crc;
+}
+#else
+/* We should never call this unless the CPU has previously been detected to
+ * support this instruction in the SSE4.2 feature set. b=23549 */
+static inline __u32 crc32c_hw(__u32 crc, unsigned char const *p,size_t len)
+{
+ LBUG();
+}
+#endif
static inline __u32 init_checksum(cksum_type_t cksum_type)
{
switch(cksum_type) {
- case OBD_CKSUM_CRC32:
+ case OBD_CKSUM_CRC32C:
return ~0U;
#ifdef HAVE_ADLER
case OBD_CKSUM_ADLER:
return 1U;
#endif
+ case OBD_CKSUM_CRC32:
+ return ~0U;
default:
CERROR("Unknown checksum type (%x)!!!\n", cksum_type);
LBUG();
size_t len, cksum_type_t cksum_type)
{
switch(cksum_type) {
- case OBD_CKSUM_CRC32:
- return crc32_le(cksum, p, len);
+ case OBD_CKSUM_CRC32C:
+ return crc32c_hw(cksum, p, len);
#ifdef HAVE_ADLER
case OBD_CKSUM_ADLER:
return adler32(cksum, p, len);
#endif
+ case OBD_CKSUM_CRC32:
+ return crc32_le(cksum, p, len);
default:
CERROR("Unknown checksum type (%x)!!!\n", cksum_type);
LBUG();
return 0;
}
+/* 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 CRC32 (even though
+ * it isn't very fast) because that is supported by all clients
+ * checksums, since 1.6.5 (or earlier via patches).
+ *
+ * These flags should be listed in order of descending performance, so that
+ * 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 OBD_FL_CKSUM_CRC32;
+ if (cksum_type & OBD_CKSUM_CRC32C)
+ return OBD_FL_CKSUM_CRC32C;
#ifdef HAVE_ADLER
- case OBD_CKSUM_ADLER:
+ if (cksum_type & OBD_CKSUM_ADLER)
return OBD_FL_CKSUM_ADLER;
#endif
- default:
+ if (unlikely(cksum_type && !(cksum_type & OBD_CKSUM_CRC32)))
CWARN("unknown cksum type %x\n", cksum_type);
- }
+
return OBD_FL_CKSUM_CRC32;
}
static inline cksum_type_t cksum_type_unpack(obd_flag o_flags)
{
- 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)
+ switch (o_flags & OBD_FL_CKSUM_ALL) {
+ case OBD_FL_CKSUM_CRC32C:
+ return OBD_CKSUM_CRC32C;
+ case 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);
+ break;
#endif
+ default:
+ break;
+ }
+
+ /* 1.6.4- only supported CRC32 and didn't set o_flags */
return OBD_CKSUM_CRC32;
}
+/* Return a bitmask of the checksum types supported on this system.
+ *
+ * CRC32 is a required for compatibility (starting with 1.6.5),
+ * after which we could move to Adler as the base checksum type.
+ *
+ * If hardware crc32c support is not available, it is slower than Adler,
+ * so don't include it, even if it could be emulated in software. b=23549 */
+static inline cksum_type_t cksum_types_supported(void)
+{
+ cksum_type_t ret = OBD_CKSUM_CRC32;
+
+#ifdef X86_FEATURE_XMM4_2
+ if (cpu_has_xmm4_2)
+ ret |= OBD_CKSUM_CRC32C;
+#endif
#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
+ ret |= OBD_CKSUM_ADLER;
#endif
+ return ret;
+}
-#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 ordering, but in the future we might want to
+ * determine this based on benchmarking the different algorithms quickly.
+ * 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 */
if (OBD_FAIL_CHECK(OBD_FAIL_OSC_CKSUM_ADLER_ONLY))
data->ocd_cksum_types = OBD_CKSUM_ADLER;
else
- /* send the list of supported checksum types */
- data->ocd_cksum_types = OBD_CKSUM_ALL;
+ data->ocd_cksum_types = cksum_types_supported();
}
#ifdef HAVE_LRU_RESIZE_SUPPORT
/* send max bytes per rpc */
data->ocd_brw_size = PTLRPC_MAX_BRW_PAGES << CFS_PAGE_SHIFT;
/* send the list of supported checksum types */
- data->ocd_cksum_types = OBD_CKSUM_ALL;
+ data->ocd_cksum_types = cksum_types_supported();
/* NB: lov_connect() needs to fill in .ocd_index for each OST */
rc = obd_connect(NULL, &mds->mds_lov_exp, mds->mds_lov_obd, &obd->obd_uuid, data, NULL);
OBD_FREE(data, sizeof(*data));
/* The client set in ocd_cksum_types the checksum types it
* supports. We have to mask off the algorithms that we don't
* support */
- if (cksum_types & OBD_CKSUM_ALL)
- data->ocd_cksum_types &= OBD_CKSUM_ALL;
- else
+ data->ocd_cksum_types &= cksum_types_supported();
+
+ /* 1.6.4- only support CRC32 and didn't set ocd_cksum_types */
+ if (unlikely(data->ocd_cksum_types == 0))
data->ocd_cksum_types = OBD_CKSUM_CRC32;
CDEBUG(D_RPCTRACE, "%s: cli %s supports cksum type %x, return "
if (oa->o_valid & OBD_MD_FLFLAGS && oa->o_flags & OBD_FL_MMAP)
return 1;
- if (oa->o_valid & OBD_MD_FLFLAGS)
- cksum_type = cksum_type_unpack(oa->o_flags);
- else
- cksum_type = OBD_CKSUM_CRC32;
-
+ cksum_type = cksum_type_unpack(oa->o_valid & OBD_MD_FLFLAGS ?
+ oa->o_flags : 0);
new_cksum = osc_checksum_bulk(nob, page_count, pga, OST_WRITE,
cksum_type);
char *router;
cksum_type_t cksum_type;
- if (body->oa.o_valid & OBD_MD_FLFLAGS)
- cksum_type = cksum_type_unpack(body->oa.o_flags);
- else
- cksum_type = OBD_CKSUM_CRC32;
+ cksum_type = cksum_type_unpack(body->oa.o_valid &OBD_MD_FLFLAGS?
+ body->oa.o_flags : 0);
client_cksum = osc_checksum_bulk(rc, aa->aa_page_count,
aa->aa_ppga, OST_READ,
cksum_type);
}
if (body->oa.o_valid & OBD_MD_FLCKSUM) {
- cksum_type_t cksum_type = OBD_CKSUM_CRC32;
-
- if (body->oa.o_valid & OBD_MD_FLFLAGS)
- cksum_type = cksum_type_unpack(body->oa.o_flags);
+ cksum_type_t cksum_type =
+ cksum_type_unpack(body->oa.o_valid & OBD_MD_FLFLAGS ?
+ body->oa.o_flags : 0);
body->oa.o_flags = cksum_type_pack(cksum_type);
body->oa.o_valid = OBD_MD_FLCKSUM | OBD_MD_FLFLAGS;
- body->oa.o_cksum = ost_checksum_bulk(desc, OST_READ, cksum_type);
+ body->oa.o_cksum = ost_checksum_bulk(desc, OST_READ,cksum_type);
CDEBUG(D_PAGE,"checksum at read origin: %x\n",body->oa.o_cksum);
} else {
body->oa.o_valid = 0;
/* We sent to the server ocd_cksum_types with bits set
* for algorithms we understand. The server masked off
* the checksum types it doesn't support */
- if ((ocd->ocd_cksum_types & OBD_CKSUM_ALL) == 0) {
+ if ((ocd->ocd_cksum_types & cksum_types_supported()) == 0) {
LCONSOLE_WARN("The negotiation of the checksum "
"alogrithm to use with server %s "
"failed (%x/%x), disabling "
"checksums\n",
obd2cli_tgt(imp->imp_obd),
ocd->ocd_cksum_types,
- OBD_CKSUM_ALL);
+ cksum_types_supported());
cli->cl_checksum = 0;
cli->cl_supp_cksum_types = OBD_CKSUM_CRC32;
- cli->cl_cksum_type = OBD_CKSUM_CRC32;
} else {
cli->cl_supp_cksum_types = ocd->ocd_cksum_types;
-
- if (ocd->ocd_cksum_types & OSC_DEFAULT_CKSUM)
- cli->cl_cksum_type = OSC_DEFAULT_CKSUM;
- else if (ocd->ocd_cksum_types & OBD_CKSUM_ADLER)
- cli->cl_cksum_type = OBD_CKSUM_ADLER;
- else
- cli->cl_cksum_type = OBD_CKSUM_CRC32;
}
} else {
/* The server does not support OBD_CONNECT_CKSUM.
* Enforce CRC32 for backward compatibility*/
cli->cl_supp_cksum_types = OBD_CKSUM_CRC32;
- cli->cl_cksum_type = OBD_CKSUM_CRC32;
}
+ cli->cl_cksum_type =cksum_type_select(cli->cl_supp_cksum_types);
if (ocd->ocd_connect_flags & OBD_CONNECT_BRW_SIZE)
cli->cl_max_pages_per_rpc =
CLASSERT(OBD_FL_SRVLOCK == 2048);
CLASSERT(OBD_FL_CKSUM_CRC32 == 4096);
CLASSERT(OBD_FL_CKSUM_ADLER == 8192);
+ CLASSERT(OBD_FL_CKSUM_CRC32C == 16384);
CLASSERT(OBD_FL_SHRINK_GRANT == 131072);
CLASSERT(OBD_FL_MMAP == (0x00040000));
CLASSERT(OBD_FL_RECOV_RESEND == (0x00080000));
CLASSERT(OBD_CKSUM_CRC32 == 1);
CLASSERT(OBD_CKSUM_ADLER == 2);
+ CLASSERT(OBD_CKSUM_CRC32C == 4);
/* Checks for struct lov_mds_md_v1 */
LASSERTF((int)sizeof(struct lov_mds_md_v1) == 32, " found %lld\n",
return 0
}
-export ORIG_CSUM_TYPE=""
+export ORIG_CSUM_TYPE="`lctl get_param -n osc/*osc-[^mM]*/checksum_type |
+ sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
+[ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
set_checksum_type()
{
- [ "$ORIG_CSUM_TYPE" ] || \
- ORIG_CSUM_TYPE=`lctl get_param -n osc/*osc-[^mM]*/checksum_type |
- sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`
lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
log "set checksum type to $1"
return 0
CHECK_CVALUE(OBD_FL_SRVLOCK);
CHECK_CVALUE(OBD_FL_CKSUM_CRC32);
CHECK_CVALUE(OBD_FL_CKSUM_ADLER);
+ CHECK_CVALUE(OBD_FL_CKSUM_CRC32C);
CHECK_CVALUE(OBD_FL_SHRINK_GRANT);
CHECK_CVALUE(OBD_FL_MMAP);
CHECK_CVALUE(OBD_FL_RECOV_RESEND);
CHECK_CVALUE(OBD_CKSUM_CRC32);
CHECK_CVALUE(OBD_CKSUM_ADLER);
+ CHECK_CVALUE(OBD_CKSUM_CRC32C);
}
static void
CLASSERT(OBD_FL_SRVLOCK == 2048);
CLASSERT(OBD_FL_CKSUM_CRC32 == 4096);
CLASSERT(OBD_FL_CKSUM_ADLER == 8192);
+ CLASSERT(OBD_FL_CKSUM_CRC32C == 16384);
CLASSERT(OBD_FL_SHRINK_GRANT == 131072);
CLASSERT(OBD_FL_MMAP == (0x00040000));
CLASSERT(OBD_FL_RECOV_RESEND == (0x00080000));
CLASSERT(OBD_CKSUM_CRC32 == 1);
CLASSERT(OBD_CKSUM_ADLER == 2);
+ CLASSERT(OBD_CKSUM_CRC32C == 4);
/* Checks for struct lov_mds_md_v1 */
LASSERTF((int)sizeof(struct lov_mds_md_v1) == 32, " found %lld\n",