110602SLori.Alt@Sun.COM /*
210602SLori.Alt@Sun.COM * CDDL HEADER START
310602SLori.Alt@Sun.COM *
410602SLori.Alt@Sun.COM * The contents of this file are subject to the terms of the
510602SLori.Alt@Sun.COM * Common Development and Distribution License (the "License").
610602SLori.Alt@Sun.COM * You may not use this file except in compliance with the License.
710602SLori.Alt@Sun.COM *
810602SLori.Alt@Sun.COM * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
910602SLori.Alt@Sun.COM * or http://www.opensolaris.org/os/licensing.
1010602SLori.Alt@Sun.COM * See the License for the specific language governing permissions
1110602SLori.Alt@Sun.COM * and limitations under the License.
1210602SLori.Alt@Sun.COM *
1310602SLori.Alt@Sun.COM * When distributing Covered Code, include this CDDL HEADER in each
1410602SLori.Alt@Sun.COM * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1510602SLori.Alt@Sun.COM * If applicable, add the following below this CDDL HEADER, with the
1610602SLori.Alt@Sun.COM * fields enclosed by brackets "[]" replaced with your own identifying
1710602SLori.Alt@Sun.COM * information: Portions Copyright [yyyy] [name of copyright owner]
1810602SLori.Alt@Sun.COM *
1910602SLori.Alt@Sun.COM * CDDL HEADER END
2010602SLori.Alt@Sun.COM */
2110602SLori.Alt@Sun.COM /*
2210602SLori.Alt@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
2310602SLori.Alt@Sun.COM * Use is subject to license terms.
2410602SLori.Alt@Sun.COM */
2510602SLori.Alt@Sun.COM
2610602SLori.Alt@Sun.COM /*
2710602SLori.Alt@Sun.COM * Fletcher Checksums
2810602SLori.Alt@Sun.COM * ------------------
2910602SLori.Alt@Sun.COM *
3010602SLori.Alt@Sun.COM * ZFS's 2nd and 4th order Fletcher checksums are defined by the following
3110602SLori.Alt@Sun.COM * recurrence relations:
3210602SLori.Alt@Sun.COM *
3310602SLori.Alt@Sun.COM * a = a + f
3410602SLori.Alt@Sun.COM * i i-1 i-1
3510602SLori.Alt@Sun.COM *
3610602SLori.Alt@Sun.COM * b = b + a
3710602SLori.Alt@Sun.COM * i i-1 i
3810602SLori.Alt@Sun.COM *
3910602SLori.Alt@Sun.COM * c = c + b (fletcher-4 only)
4010602SLori.Alt@Sun.COM * i i-1 i
4110602SLori.Alt@Sun.COM *
4210602SLori.Alt@Sun.COM * d = d + c (fletcher-4 only)
4310602SLori.Alt@Sun.COM * i i-1 i
4410602SLori.Alt@Sun.COM *
4510602SLori.Alt@Sun.COM * Where
4610602SLori.Alt@Sun.COM * a_0 = b_0 = c_0 = d_0 = 0
4710602SLori.Alt@Sun.COM * and
4810602SLori.Alt@Sun.COM * f_0 .. f_(n-1) are the input data.
4910602SLori.Alt@Sun.COM *
5010602SLori.Alt@Sun.COM * Using standard techniques, these translate into the following series:
5110602SLori.Alt@Sun.COM *
5210602SLori.Alt@Sun.COM * __n_ __n_
5310602SLori.Alt@Sun.COM * \ | \ |
5410602SLori.Alt@Sun.COM * a = > f b = > i * f
5510602SLori.Alt@Sun.COM * n /___| n - i n /___| n - i
5610602SLori.Alt@Sun.COM * i = 1 i = 1
5710602SLori.Alt@Sun.COM *
5810602SLori.Alt@Sun.COM *
5910602SLori.Alt@Sun.COM * __n_ __n_
6010602SLori.Alt@Sun.COM * \ | i*(i+1) \ | i*(i+1)*(i+2)
6110602SLori.Alt@Sun.COM * c = > ------- f d = > ------------- f
6210602SLori.Alt@Sun.COM * n /___| 2 n - i n /___| 6 n - i
6310602SLori.Alt@Sun.COM * i = 1 i = 1
6410602SLori.Alt@Sun.COM *
6510602SLori.Alt@Sun.COM * For fletcher-2, the f_is are 64-bit, and [ab]_i are 64-bit accumulators.
6610602SLori.Alt@Sun.COM * Since the additions are done mod (2^64), errors in the high bits may not
6710602SLori.Alt@Sun.COM * be noticed. For this reason, fletcher-2 is deprecated.
6810602SLori.Alt@Sun.COM *
6910602SLori.Alt@Sun.COM * For fletcher-4, the f_is are 32-bit, and [abcd]_i are 64-bit accumulators.
7010602SLori.Alt@Sun.COM * A conservative estimate of how big the buffer can get before we overflow
7110602SLori.Alt@Sun.COM * can be estimated using f_i = 0xffffffff for all i:
7210602SLori.Alt@Sun.COM *
7310602SLori.Alt@Sun.COM * % bc
7410602SLori.Alt@Sun.COM * f=2^32-1;d=0; for (i = 1; d<2^64; i++) { d += f*i*(i+1)*(i+2)/6 }; (i-1)*4
7510602SLori.Alt@Sun.COM * 2264
7610602SLori.Alt@Sun.COM * quit
7710602SLori.Alt@Sun.COM * %
7810602SLori.Alt@Sun.COM *
7910602SLori.Alt@Sun.COM * So blocks of up to 2k will not overflow. Our largest block size is
8010602SLori.Alt@Sun.COM * 128k, which has 32k 4-byte words, so we can compute the largest possible
8110602SLori.Alt@Sun.COM * accumulators, then divide by 2^64 to figure the max amount of overflow:
8210602SLori.Alt@Sun.COM *
8310602SLori.Alt@Sun.COM * % bc
8410602SLori.Alt@Sun.COM * a=b=c=d=0; f=2^32-1; for (i=1; i<=32*1024; i++) { a+=f; b+=a; c+=b; d+=c }
8510602SLori.Alt@Sun.COM * a/2^64;b/2^64;c/2^64;d/2^64
8610602SLori.Alt@Sun.COM * 0
8710602SLori.Alt@Sun.COM * 0
8810602SLori.Alt@Sun.COM * 1365
8910602SLori.Alt@Sun.COM * 11186858
9010602SLori.Alt@Sun.COM * quit
9110602SLori.Alt@Sun.COM * %
9210602SLori.Alt@Sun.COM *
9310602SLori.Alt@Sun.COM * So a and b cannot overflow. To make sure each bit of input has some
9410602SLori.Alt@Sun.COM * effect on the contents of c and d, we can look at what the factors of
9510602SLori.Alt@Sun.COM * the coefficients in the equations for c_n and d_n are. The number of 2s
9610602SLori.Alt@Sun.COM * in the factors determines the lowest set bit in the multiplier. Running
9710602SLori.Alt@Sun.COM * through the cases for n*(n+1)/2 reveals that the highest power of 2 is
9810602SLori.Alt@Sun.COM * 2^14, and for n*(n+1)*(n+2)/6 it is 2^15. So while some data may overflow
9910602SLori.Alt@Sun.COM * the 64-bit accumulators, every bit of every f_i effects every accumulator,
10010602SLori.Alt@Sun.COM * even for 128k blocks.
10110602SLori.Alt@Sun.COM *
10210602SLori.Alt@Sun.COM * If we wanted to make a stronger version of fletcher4 (fletcher4c?),
10310602SLori.Alt@Sun.COM * we could do our calculations mod (2^32 - 1) by adding in the carries
10410602SLori.Alt@Sun.COM * periodically, and store the number of carries in the top 32-bits.
10510602SLori.Alt@Sun.COM *
10610602SLori.Alt@Sun.COM * --------------------
10710602SLori.Alt@Sun.COM * Checksum Performance
10810602SLori.Alt@Sun.COM * --------------------
10910602SLori.Alt@Sun.COM *
11010602SLori.Alt@Sun.COM * There are two interesting components to checksum performance: cached and
11110602SLori.Alt@Sun.COM * uncached performance. With cached data, fletcher-2 is about four times
11210602SLori.Alt@Sun.COM * faster than fletcher-4. With uncached data, the performance difference is
11310602SLori.Alt@Sun.COM * negligible, since the cost of a cache fill dominates the processing time.
11410602SLori.Alt@Sun.COM * Even though fletcher-4 is slower than fletcher-2, it is still a pretty
11510602SLori.Alt@Sun.COM * efficient pass over the data.
11610602SLori.Alt@Sun.COM *
11710602SLori.Alt@Sun.COM * In normal operation, the data which is being checksummed is in a buffer
11810602SLori.Alt@Sun.COM * which has been filled either by:
11910602SLori.Alt@Sun.COM *
12010602SLori.Alt@Sun.COM * 1. a compression step, which will be mostly cached, or
12110602SLori.Alt@Sun.COM * 2. a bcopy() or copyin(), which will be uncached (because the
12210602SLori.Alt@Sun.COM * copy is cache-bypassing).
12310602SLori.Alt@Sun.COM *
12410602SLori.Alt@Sun.COM * For both cached and uncached data, both fletcher checksums are much faster
12510602SLori.Alt@Sun.COM * than sha-256, and slower than 'off', which doesn't touch the data at all.
12610602SLori.Alt@Sun.COM */
12710602SLori.Alt@Sun.COM
12810602SLori.Alt@Sun.COM #include <sys/types.h>
12910602SLori.Alt@Sun.COM #include <sys/sysmacros.h>
13010602SLori.Alt@Sun.COM #include <sys/byteorder.h>
131*10922SJeff.Bonwick@Sun.COM #include <sys/zio.h>
13210602SLori.Alt@Sun.COM #include <sys/spa.h>
13310602SLori.Alt@Sun.COM
13410602SLori.Alt@Sun.COM void
fletcher_2_native(const void * buf,uint64_t size,zio_cksum_t * zcp)13510602SLori.Alt@Sun.COM fletcher_2_native(const void *buf, uint64_t size, zio_cksum_t *zcp)
13610602SLori.Alt@Sun.COM {
13710602SLori.Alt@Sun.COM const uint64_t *ip = buf;
13810602SLori.Alt@Sun.COM const uint64_t *ipend = ip + (size / sizeof (uint64_t));
13910602SLori.Alt@Sun.COM uint64_t a0, b0, a1, b1;
14010602SLori.Alt@Sun.COM
14110602SLori.Alt@Sun.COM for (a0 = b0 = a1 = b1 = 0; ip < ipend; ip += 2) {
14210602SLori.Alt@Sun.COM a0 += ip[0];
14310602SLori.Alt@Sun.COM a1 += ip[1];
14410602SLori.Alt@Sun.COM b0 += a0;
14510602SLori.Alt@Sun.COM b1 += a1;
14610602SLori.Alt@Sun.COM }
14710602SLori.Alt@Sun.COM
14810602SLori.Alt@Sun.COM ZIO_SET_CHECKSUM(zcp, a0, a1, b0, b1);
14910602SLori.Alt@Sun.COM }
15010602SLori.Alt@Sun.COM
15110602SLori.Alt@Sun.COM void
fletcher_2_byteswap(const void * buf,uint64_t size,zio_cksum_t * zcp)15210602SLori.Alt@Sun.COM fletcher_2_byteswap(const void *buf, uint64_t size, zio_cksum_t *zcp)
15310602SLori.Alt@Sun.COM {
15410602SLori.Alt@Sun.COM const uint64_t *ip = buf;
15510602SLori.Alt@Sun.COM const uint64_t *ipend = ip + (size / sizeof (uint64_t));
15610602SLori.Alt@Sun.COM uint64_t a0, b0, a1, b1;
15710602SLori.Alt@Sun.COM
15810602SLori.Alt@Sun.COM for (a0 = b0 = a1 = b1 = 0; ip < ipend; ip += 2) {
15910602SLori.Alt@Sun.COM a0 += BSWAP_64(ip[0]);
16010602SLori.Alt@Sun.COM a1 += BSWAP_64(ip[1]);
16110602SLori.Alt@Sun.COM b0 += a0;
16210602SLori.Alt@Sun.COM b1 += a1;
16310602SLori.Alt@Sun.COM }
16410602SLori.Alt@Sun.COM
16510602SLori.Alt@Sun.COM ZIO_SET_CHECKSUM(zcp, a0, a1, b0, b1);
16610602SLori.Alt@Sun.COM }
16710602SLori.Alt@Sun.COM
16810602SLori.Alt@Sun.COM void
fletcher_4_native(const void * buf,uint64_t size,zio_cksum_t * zcp)16910602SLori.Alt@Sun.COM fletcher_4_native(const void *buf, uint64_t size, zio_cksum_t *zcp)
17010602SLori.Alt@Sun.COM {
17110602SLori.Alt@Sun.COM const uint32_t *ip = buf;
17210602SLori.Alt@Sun.COM const uint32_t *ipend = ip + (size / sizeof (uint32_t));
17310602SLori.Alt@Sun.COM uint64_t a, b, c, d;
17410602SLori.Alt@Sun.COM
17510602SLori.Alt@Sun.COM for (a = b = c = d = 0; ip < ipend; ip++) {
17610602SLori.Alt@Sun.COM a += ip[0];
17710602SLori.Alt@Sun.COM b += a;
17810602SLori.Alt@Sun.COM c += b;
17910602SLori.Alt@Sun.COM d += c;
18010602SLori.Alt@Sun.COM }
18110602SLori.Alt@Sun.COM
18210602SLori.Alt@Sun.COM ZIO_SET_CHECKSUM(zcp, a, b, c, d);
18310602SLori.Alt@Sun.COM }
18410602SLori.Alt@Sun.COM
18510602SLori.Alt@Sun.COM void
fletcher_4_byteswap(const void * buf,uint64_t size,zio_cksum_t * zcp)18610602SLori.Alt@Sun.COM fletcher_4_byteswap(const void *buf, uint64_t size, zio_cksum_t *zcp)
18710602SLori.Alt@Sun.COM {
18810602SLori.Alt@Sun.COM const uint32_t *ip = buf;
18910602SLori.Alt@Sun.COM const uint32_t *ipend = ip + (size / sizeof (uint32_t));
19010602SLori.Alt@Sun.COM uint64_t a, b, c, d;
19110602SLori.Alt@Sun.COM
19210602SLori.Alt@Sun.COM for (a = b = c = d = 0; ip < ipend; ip++) {
19310602SLori.Alt@Sun.COM a += BSWAP_32(ip[0]);
19410602SLori.Alt@Sun.COM b += a;
19510602SLori.Alt@Sun.COM c += b;
19610602SLori.Alt@Sun.COM d += c;
19710602SLori.Alt@Sun.COM }
19810602SLori.Alt@Sun.COM
19910602SLori.Alt@Sun.COM ZIO_SET_CHECKSUM(zcp, a, b, c, d);
20010602SLori.Alt@Sun.COM }
20110602SLori.Alt@Sun.COM
20210602SLori.Alt@Sun.COM void
fletcher_4_incremental_native(const void * buf,uint64_t size,zio_cksum_t * zcp)20310602SLori.Alt@Sun.COM fletcher_4_incremental_native(const void *buf, uint64_t size,
20410602SLori.Alt@Sun.COM zio_cksum_t *zcp)
20510602SLori.Alt@Sun.COM {
20610602SLori.Alt@Sun.COM const uint32_t *ip = buf;
20710602SLori.Alt@Sun.COM const uint32_t *ipend = ip + (size / sizeof (uint32_t));
20810602SLori.Alt@Sun.COM uint64_t a, b, c, d;
20910602SLori.Alt@Sun.COM
21010602SLori.Alt@Sun.COM a = zcp->zc_word[0];
21110602SLori.Alt@Sun.COM b = zcp->zc_word[1];
21210602SLori.Alt@Sun.COM c = zcp->zc_word[2];
21310602SLori.Alt@Sun.COM d = zcp->zc_word[3];
21410602SLori.Alt@Sun.COM
21510602SLori.Alt@Sun.COM for (; ip < ipend; ip++) {
21610602SLori.Alt@Sun.COM a += ip[0];
21710602SLori.Alt@Sun.COM b += a;
21810602SLori.Alt@Sun.COM c += b;
21910602SLori.Alt@Sun.COM d += c;
22010602SLori.Alt@Sun.COM }
22110602SLori.Alt@Sun.COM
22210602SLori.Alt@Sun.COM ZIO_SET_CHECKSUM(zcp, a, b, c, d);
22310602SLori.Alt@Sun.COM }
22410602SLori.Alt@Sun.COM
22510602SLori.Alt@Sun.COM void
fletcher_4_incremental_byteswap(const void * buf,uint64_t size,zio_cksum_t * zcp)22610602SLori.Alt@Sun.COM fletcher_4_incremental_byteswap(const void *buf, uint64_t size,
22710602SLori.Alt@Sun.COM zio_cksum_t *zcp)
22810602SLori.Alt@Sun.COM {
22910602SLori.Alt@Sun.COM const uint32_t *ip = buf;
23010602SLori.Alt@Sun.COM const uint32_t *ipend = ip + (size / sizeof (uint32_t));
23110602SLori.Alt@Sun.COM uint64_t a, b, c, d;
23210602SLori.Alt@Sun.COM
23310602SLori.Alt@Sun.COM a = zcp->zc_word[0];
23410602SLori.Alt@Sun.COM b = zcp->zc_word[1];
23510602SLori.Alt@Sun.COM c = zcp->zc_word[2];
23610602SLori.Alt@Sun.COM d = zcp->zc_word[3];
23710602SLori.Alt@Sun.COM
23810602SLori.Alt@Sun.COM for (; ip < ipend; ip++) {
23910602SLori.Alt@Sun.COM a += BSWAP_32(ip[0]);
24010602SLori.Alt@Sun.COM b += a;
24110602SLori.Alt@Sun.COM c += b;
24210602SLori.Alt@Sun.COM d += c;
24310602SLori.Alt@Sun.COM }
24410602SLori.Alt@Sun.COM
24510602SLori.Alt@Sun.COM ZIO_SET_CHECKSUM(zcp, a, b, c, d);
24610602SLori.Alt@Sun.COM }
247