1495db6fbSLori Alt /* 2495db6fbSLori Alt * CDDL HEADER START 3495db6fbSLori Alt * 4495db6fbSLori Alt * The contents of this file are subject to the terms of the 5495db6fbSLori Alt * Common Development and Distribution License (the "License"). 6495db6fbSLori Alt * You may not use this file except in compliance with the License. 7495db6fbSLori Alt * 8495db6fbSLori Alt * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9495db6fbSLori Alt * or http://www.opensolaris.org/os/licensing. 10495db6fbSLori Alt * See the License for the specific language governing permissions 11495db6fbSLori Alt * and limitations under the License. 12495db6fbSLori Alt * 13495db6fbSLori Alt * When distributing Covered Code, include this CDDL HEADER in each 14495db6fbSLori Alt * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15495db6fbSLori Alt * If applicable, add the following below this CDDL HEADER, with the 16495db6fbSLori Alt * fields enclosed by brackets "[]" replaced with your own identifying 17495db6fbSLori Alt * information: Portions Copyright [yyyy] [name of copyright owner] 18495db6fbSLori Alt * 19495db6fbSLori Alt * CDDL HEADER END 20495db6fbSLori Alt */ 21495db6fbSLori Alt /* 22495db6fbSLori Alt * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23495db6fbSLori Alt * Use is subject to license terms. 24495db6fbSLori Alt */ 25495db6fbSLori Alt 26495db6fbSLori Alt /* 27495db6fbSLori Alt * Fletcher Checksums 28495db6fbSLori Alt * ------------------ 29495db6fbSLori Alt * 30495db6fbSLori Alt * ZFS's 2nd and 4th order Fletcher checksums are defined by the following 31495db6fbSLori Alt * recurrence relations: 32495db6fbSLori Alt * 33495db6fbSLori Alt * a = a + f 34495db6fbSLori Alt * i i-1 i-1 35495db6fbSLori Alt * 36495db6fbSLori Alt * b = b + a 37495db6fbSLori Alt * i i-1 i 38495db6fbSLori Alt * 39495db6fbSLori Alt * c = c + b (fletcher-4 only) 40495db6fbSLori Alt * i i-1 i 41495db6fbSLori Alt * 42495db6fbSLori Alt * d = d + c (fletcher-4 only) 43495db6fbSLori Alt * i i-1 i 44495db6fbSLori Alt * 45495db6fbSLori Alt * Where 46495db6fbSLori Alt * a_0 = b_0 = c_0 = d_0 = 0 47495db6fbSLori Alt * and 48495db6fbSLori Alt * f_0 .. f_(n-1) are the input data. 49495db6fbSLori Alt * 50495db6fbSLori Alt * Using standard techniques, these translate into the following series: 51495db6fbSLori Alt * 52495db6fbSLori Alt * __n_ __n_ 53495db6fbSLori Alt * \ | \ | 54495db6fbSLori Alt * a = > f b = > i * f 55495db6fbSLori Alt * n /___| n - i n /___| n - i 56495db6fbSLori Alt * i = 1 i = 1 57495db6fbSLori Alt * 58495db6fbSLori Alt * 59495db6fbSLori Alt * __n_ __n_ 60495db6fbSLori Alt * \ | i*(i+1) \ | i*(i+1)*(i+2) 61495db6fbSLori Alt * c = > ------- f d = > ------------- f 62495db6fbSLori Alt * n /___| 2 n - i n /___| 6 n - i 63495db6fbSLori Alt * i = 1 i = 1 64495db6fbSLori Alt * 65495db6fbSLori Alt * For fletcher-2, the f_is are 64-bit, and [ab]_i are 64-bit accumulators. 66495db6fbSLori Alt * Since the additions are done mod (2^64), errors in the high bits may not 67495db6fbSLori Alt * be noticed. For this reason, fletcher-2 is deprecated. 68495db6fbSLori Alt * 69495db6fbSLori Alt * For fletcher-4, the f_is are 32-bit, and [abcd]_i are 64-bit accumulators. 70495db6fbSLori Alt * A conservative estimate of how big the buffer can get before we overflow 71495db6fbSLori Alt * can be estimated using f_i = 0xffffffff for all i: 72495db6fbSLori Alt * 73495db6fbSLori Alt * % bc 74495db6fbSLori Alt * f=2^32-1;d=0; for (i = 1; d<2^64; i++) { d += f*i*(i+1)*(i+2)/6 }; (i-1)*4 75495db6fbSLori Alt * 2264 76495db6fbSLori Alt * quit 77495db6fbSLori Alt * % 78495db6fbSLori Alt * 79495db6fbSLori Alt * So blocks of up to 2k will not overflow. Our largest block size is 80495db6fbSLori Alt * 128k, which has 32k 4-byte words, so we can compute the largest possible 81495db6fbSLori Alt * accumulators, then divide by 2^64 to figure the max amount of overflow: 82495db6fbSLori Alt * 83495db6fbSLori Alt * % bc 84495db6fbSLori Alt * a=b=c=d=0; f=2^32-1; for (i=1; i<=32*1024; i++) { a+=f; b+=a; c+=b; d+=c } 85495db6fbSLori Alt * a/2^64;b/2^64;c/2^64;d/2^64 86495db6fbSLori Alt * 0 87495db6fbSLori Alt * 0 88495db6fbSLori Alt * 1365 89495db6fbSLori Alt * 11186858 90495db6fbSLori Alt * quit 91495db6fbSLori Alt * % 92495db6fbSLori Alt * 93495db6fbSLori Alt * So a and b cannot overflow. To make sure each bit of input has some 94495db6fbSLori Alt * effect on the contents of c and d, we can look at what the factors of 95495db6fbSLori Alt * the coefficients in the equations for c_n and d_n are. The number of 2s 96495db6fbSLori Alt * in the factors determines the lowest set bit in the multiplier. Running 97495db6fbSLori Alt * through the cases for n*(n+1)/2 reveals that the highest power of 2 is 98495db6fbSLori Alt * 2^14, and for n*(n+1)*(n+2)/6 it is 2^15. So while some data may overflow 99495db6fbSLori Alt * the 64-bit accumulators, every bit of every f_i effects every accumulator, 100495db6fbSLori Alt * even for 128k blocks. 101495db6fbSLori Alt * 102495db6fbSLori Alt * If we wanted to make a stronger version of fletcher4 (fletcher4c?), 103495db6fbSLori Alt * we could do our calculations mod (2^32 - 1) by adding in the carries 104495db6fbSLori Alt * periodically, and store the number of carries in the top 32-bits. 105495db6fbSLori Alt * 106495db6fbSLori Alt * -------------------- 107495db6fbSLori Alt * Checksum Performance 108495db6fbSLori Alt * -------------------- 109495db6fbSLori Alt * 110495db6fbSLori Alt * There are two interesting components to checksum performance: cached and 111495db6fbSLori Alt * uncached performance. With cached data, fletcher-2 is about four times 112495db6fbSLori Alt * faster than fletcher-4. With uncached data, the performance difference is 113495db6fbSLori Alt * negligible, since the cost of a cache fill dominates the processing time. 114495db6fbSLori Alt * Even though fletcher-4 is slower than fletcher-2, it is still a pretty 115495db6fbSLori Alt * efficient pass over the data. 116495db6fbSLori Alt * 117495db6fbSLori Alt * In normal operation, the data which is being checksummed is in a buffer 118495db6fbSLori Alt * which has been filled either by: 119495db6fbSLori Alt * 120495db6fbSLori Alt * 1. a compression step, which will be mostly cached, or 121495db6fbSLori Alt * 2. a bcopy() or copyin(), which will be uncached (because the 122495db6fbSLori Alt * copy is cache-bypassing). 123495db6fbSLori Alt * 124495db6fbSLori Alt * For both cached and uncached data, both fletcher checksums are much faster 125495db6fbSLori Alt * than sha-256, and slower than 'off', which doesn't touch the data at all. 126495db6fbSLori Alt */ 127495db6fbSLori Alt 128495db6fbSLori Alt #include <sys/types.h> 129495db6fbSLori Alt #include <sys/sysmacros.h> 130495db6fbSLori Alt #include <sys/byteorder.h> 131*b24ab676SJeff Bonwick #include <sys/zio.h> 132495db6fbSLori Alt #include <sys/spa.h> 133495db6fbSLori Alt 134495db6fbSLori Alt void 135495db6fbSLori Alt fletcher_2_native(const void *buf, uint64_t size, zio_cksum_t *zcp) 136495db6fbSLori Alt { 137495db6fbSLori Alt const uint64_t *ip = buf; 138495db6fbSLori Alt const uint64_t *ipend = ip + (size / sizeof (uint64_t)); 139495db6fbSLori Alt uint64_t a0, b0, a1, b1; 140495db6fbSLori Alt 141495db6fbSLori Alt for (a0 = b0 = a1 = b1 = 0; ip < ipend; ip += 2) { 142495db6fbSLori Alt a0 += ip[0]; 143495db6fbSLori Alt a1 += ip[1]; 144495db6fbSLori Alt b0 += a0; 145495db6fbSLori Alt b1 += a1; 146495db6fbSLori Alt } 147495db6fbSLori Alt 148495db6fbSLori Alt ZIO_SET_CHECKSUM(zcp, a0, a1, b0, b1); 149495db6fbSLori Alt } 150495db6fbSLori Alt 151495db6fbSLori Alt void 152495db6fbSLori Alt fletcher_2_byteswap(const void *buf, uint64_t size, zio_cksum_t *zcp) 153495db6fbSLori Alt { 154495db6fbSLori Alt const uint64_t *ip = buf; 155495db6fbSLori Alt const uint64_t *ipend = ip + (size / sizeof (uint64_t)); 156495db6fbSLori Alt uint64_t a0, b0, a1, b1; 157495db6fbSLori Alt 158495db6fbSLori Alt for (a0 = b0 = a1 = b1 = 0; ip < ipend; ip += 2) { 159495db6fbSLori Alt a0 += BSWAP_64(ip[0]); 160495db6fbSLori Alt a1 += BSWAP_64(ip[1]); 161495db6fbSLori Alt b0 += a0; 162495db6fbSLori Alt b1 += a1; 163495db6fbSLori Alt } 164495db6fbSLori Alt 165495db6fbSLori Alt ZIO_SET_CHECKSUM(zcp, a0, a1, b0, b1); 166495db6fbSLori Alt } 167495db6fbSLori Alt 168495db6fbSLori Alt void 169495db6fbSLori Alt fletcher_4_native(const void *buf, uint64_t size, zio_cksum_t *zcp) 170495db6fbSLori Alt { 171495db6fbSLori Alt const uint32_t *ip = buf; 172495db6fbSLori Alt const uint32_t *ipend = ip + (size / sizeof (uint32_t)); 173495db6fbSLori Alt uint64_t a, b, c, d; 174495db6fbSLori Alt 175495db6fbSLori Alt for (a = b = c = d = 0; ip < ipend; ip++) { 176495db6fbSLori Alt a += ip[0]; 177495db6fbSLori Alt b += a; 178495db6fbSLori Alt c += b; 179495db6fbSLori Alt d += c; 180495db6fbSLori Alt } 181495db6fbSLori Alt 182495db6fbSLori Alt ZIO_SET_CHECKSUM(zcp, a, b, c, d); 183495db6fbSLori Alt } 184495db6fbSLori Alt 185495db6fbSLori Alt void 186495db6fbSLori Alt fletcher_4_byteswap(const void *buf, uint64_t size, zio_cksum_t *zcp) 187495db6fbSLori Alt { 188495db6fbSLori Alt const uint32_t *ip = buf; 189495db6fbSLori Alt const uint32_t *ipend = ip + (size / sizeof (uint32_t)); 190495db6fbSLori Alt uint64_t a, b, c, d; 191495db6fbSLori Alt 192495db6fbSLori Alt for (a = b = c = d = 0; ip < ipend; ip++) { 193495db6fbSLori Alt a += BSWAP_32(ip[0]); 194495db6fbSLori Alt b += a; 195495db6fbSLori Alt c += b; 196495db6fbSLori Alt d += c; 197495db6fbSLori Alt } 198495db6fbSLori Alt 199495db6fbSLori Alt ZIO_SET_CHECKSUM(zcp, a, b, c, d); 200495db6fbSLori Alt } 201495db6fbSLori Alt 202495db6fbSLori Alt void 203495db6fbSLori Alt fletcher_4_incremental_native(const void *buf, uint64_t size, 204495db6fbSLori Alt zio_cksum_t *zcp) 205495db6fbSLori Alt { 206495db6fbSLori Alt const uint32_t *ip = buf; 207495db6fbSLori Alt const uint32_t *ipend = ip + (size / sizeof (uint32_t)); 208495db6fbSLori Alt uint64_t a, b, c, d; 209495db6fbSLori Alt 210495db6fbSLori Alt a = zcp->zc_word[0]; 211495db6fbSLori Alt b = zcp->zc_word[1]; 212495db6fbSLori Alt c = zcp->zc_word[2]; 213495db6fbSLori Alt d = zcp->zc_word[3]; 214495db6fbSLori Alt 215495db6fbSLori Alt for (; ip < ipend; ip++) { 216495db6fbSLori Alt a += ip[0]; 217495db6fbSLori Alt b += a; 218495db6fbSLori Alt c += b; 219495db6fbSLori Alt d += c; 220495db6fbSLori Alt } 221495db6fbSLori Alt 222495db6fbSLori Alt ZIO_SET_CHECKSUM(zcp, a, b, c, d); 223495db6fbSLori Alt } 224495db6fbSLori Alt 225495db6fbSLori Alt void 226495db6fbSLori Alt fletcher_4_incremental_byteswap(const void *buf, uint64_t size, 227495db6fbSLori Alt zio_cksum_t *zcp) 228495db6fbSLori Alt { 229495db6fbSLori Alt const uint32_t *ip = buf; 230495db6fbSLori Alt const uint32_t *ipend = ip + (size / sizeof (uint32_t)); 231495db6fbSLori Alt uint64_t a, b, c, d; 232495db6fbSLori Alt 233495db6fbSLori Alt a = zcp->zc_word[0]; 234495db6fbSLori Alt b = zcp->zc_word[1]; 235495db6fbSLori Alt c = zcp->zc_word[2]; 236495db6fbSLori Alt d = zcp->zc_word[3]; 237495db6fbSLori Alt 238495db6fbSLori Alt for (; ip < ipend; ip++) { 239495db6fbSLori Alt a += BSWAP_32(ip[0]); 240495db6fbSLori Alt b += a; 241495db6fbSLori Alt c += b; 242495db6fbSLori Alt d += c; 243495db6fbSLori Alt } 244495db6fbSLori Alt 245495db6fbSLori Alt ZIO_SET_CHECKSUM(zcp, a, b, c, d); 246495db6fbSLori Alt } 247