| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
#ifdef HAVE_CONFIG_H |
| 29 |
#include "config.h" |
| 30 |
#endif |
| 31 |
|
| 32 |
#include <inttypes.h> |
| 33 |
#include <stdint.h> |
| 34 |
|
| 35 |
#include <ctype.h> |
| 36 |
#include <stdio.h> |
| 37 |
#include <string.h> |
| 38 |
#include <sys/uio.h> |
| 39 |
|
| 40 |
#include "istgt_iscsi.h" |
| 41 |
#include "istgt_crc32c.h" |
| 42 |
|
| 43 |
|
| 44 |
static uint32_t istgt_crc32c_initial = ISTGT_CRC32C_INITIAL; |
| 45 |
static uint32_t istgt_crc32c_xor = ISTGT_CRC32C_XOR; |
| 46 |
static uint32_t istgt_crc32c_polynomial = ISTGT_CRC32C_POLYNOMIAL; |
| 47 |
#ifdef ISTGT_USE_CRC32C_TABLE |
| 48 |
static uint32_t istgt_crc32c_table[256]; |
| 49 |
static int istgt_crc32c_initialized = 0; |
| 50 |
#endif |
| 51 |
|
| 52 |
static uint32_t |
| 53 |
istgt_reflect(uint32_t val, int bits) |
| 54 |
{ |
| 55 |
int i; |
| 56 |
uint32_t r; |
| 57 |
|
| 58 |
if (bits < 1 || bits > 32) |
| 59 |
return 0; |
| 60 |
r = 0; |
| 61 |
for (i = 0; i < bits; i++) { |
| 62 |
r |= ((val >> ((bits - 1) - i)) & 1) << i; |
| 63 |
} |
| 64 |
return r; |
| 65 |
} |
| 66 |
|
| 67 |
#ifdef ISTGT_USE_CRC32C_TABLE |
| 68 |
void |
| 69 |
istgt_init_crc32c_table(void) |
| 70 |
{ |
| 71 |
int i, j; |
| 72 |
uint32_t val; |
| 73 |
uint32_t reflect_polynomial; |
| 74 |
|
| 75 |
reflect_polynomial = istgt_reflect(istgt_crc32c_polynomial, 32); |
| 76 |
for (i = 0; i < 256; i++) { |
| 77 |
val = i; |
| 78 |
for (j = 0; j < 8; j++) { |
| 79 |
if (val & 1) { |
| 80 |
val = (val >> 1) ^ reflect_polynomial; |
| 81 |
} else { |
| 82 |
val = (val >> 1); |
| 83 |
} |
| 84 |
} |
| 85 |
istgt_crc32c_table[i] = val; |
| 86 |
} |
| 87 |
istgt_crc32c_initialized = 1; |
| 88 |
} |
| 89 |
#endif |
| 90 |
|
| 91 |
uint32_t |
| 92 |
istgt_update_crc32c(const uint8_t *buf, size_t len, uint32_t crc) |
| 93 |
{ |
| 94 |
size_t s; |
| 95 |
#ifndef ISTGT_USE_CRC32C_TABLE |
| 96 |
int i; |
| 97 |
uint32_t val; |
| 98 |
uint32_t reflect_polynomial; |
| 99 |
#endif |
| 100 |
|
| 101 |
#ifdef ISTGT_USE_CRC32C_TABLE |
| 102 |
#if 0 |
| 103 |
|
| 104 |
if (!istgt_crc32c_initialized) { |
| 105 |
istgt_init_crc32c_table(); |
| 106 |
} |
| 107 |
#endif |
| 108 |
#else |
| 109 |
reflect_polynomial = istgt_reflect(istgt_crc32c_polynomial, 32); |
| 110 |
#endif |
| 111 |
|
| 112 |
for (s = 0; s < len; s++) { |
| 113 |
#ifdef ISTGT_USE_CRC32C_TABLE |
| 114 |
crc = (crc >> 8) ^ istgt_crc32c_table[(crc ^ buf[s]) & 0xff]; |
| 115 |
#else |
| 116 |
val = buf[s]; |
| 117 |
for (i = 0; i < 8; i++) { |
| 118 |
if ((crc ^ val) & 1) { |
| 119 |
crc = (crc >> 1) ^ reflect_polynomial; |
| 120 |
} else { |
| 121 |
crc = (crc >> 1); |
| 122 |
} |
| 123 |
val = val >> 1; |
| 124 |
} |
| 125 |
#endif |
| 126 |
} |
| 127 |
return crc; |
| 128 |
} |
| 129 |
|
| 130 |
uint32_t |
| 131 |
istgt_fixup_crc32c(size_t total, uint32_t crc) |
| 132 |
{ |
| 133 |
uint8_t padding[ISCSI_ALIGNMENT]; |
| 134 |
size_t pad_length; |
| 135 |
size_t rest; |
| 136 |
|
| 137 |
if (total == 0) |
| 138 |
return crc; |
| 139 |
#if 0 |
| 140 |
|
| 141 |
rest = total & ~(ISCSI_ALIGNMENT - 1); |
| 142 |
#endif |
| 143 |
rest = total % ISCSI_ALIGNMENT; |
| 144 |
if (rest != 0) { |
| 145 |
pad_length = ISCSI_ALIGNMENT; |
| 146 |
pad_length -= rest; |
| 147 |
if (pad_length > 0 && pad_length < sizeof padding){ |
| 148 |
memset(padding, 0, sizeof padding); |
| 149 |
crc = istgt_update_crc32c(padding, pad_length, crc); |
| 150 |
} |
| 151 |
} |
| 152 |
return crc; |
| 153 |
} |
| 154 |
|
| 155 |
uint32_t |
| 156 |
istgt_crc32c(const uint8_t *buf, size_t len) |
| 157 |
{ |
| 158 |
uint32_t crc32c; |
| 159 |
|
| 160 |
crc32c = istgt_crc32c_initial; |
| 161 |
crc32c = istgt_update_crc32c(buf, len, crc32c); |
| 162 |
if ((len % ISCSI_ALIGNMENT) != 0) { |
| 163 |
crc32c = istgt_fixup_crc32c(len, crc32c); |
| 164 |
} |
| 165 |
crc32c = crc32c ^ istgt_crc32c_xor; |
| 166 |
return crc32c; |
| 167 |
} |
| 168 |
|
| 169 |
uint32_t |
| 170 |
istgt_iovec_crc32c(const struct iovec *iovp, int iovc, uint32_t offset, uint32_t len) |
| 171 |
{ |
| 172 |
const uint8_t *p; |
| 173 |
uint32_t total; |
| 174 |
uint32_t pos; |
| 175 |
uint32_t n; |
| 176 |
uint32_t crc32c; |
| 177 |
int i; |
| 178 |
|
| 179 |
pos = 0; |
| 180 |
total = 0; |
| 181 |
crc32c = istgt_crc32c_initial; |
| 182 |
for (i = 0; i < iovc; i++) { |
| 183 |
if (len == 0) |
| 184 |
break; |
| 185 |
if (pos + iovp[i].iov_len > offset) { |
| 186 |
p = (const uint8_t *) iovp[i].iov_base + (offset - pos); |
| 187 |
if (iovp[i].iov_len > len) { |
| 188 |
n = len; |
| 189 |
len = 0; |
| 190 |
} else { |
| 191 |
n = iovp[i].iov_len; |
| 192 |
len -= n; |
| 193 |
} |
| 194 |
crc32c = istgt_update_crc32c(p, n, crc32c); |
| 195 |
offset += n; |
| 196 |
total += n; |
| 197 |
} |
| 198 |
pos += iovp[i].iov_len; |
| 199 |
} |
| 200 |
#if 0 |
| 201 |
printf("update %d bytes\n", total); |
| 202 |
#endif |
| 203 |
crc32c = istgt_fixup_crc32c(total, crc32c); |
| 204 |
crc32c = crc32c ^ istgt_crc32c_xor; |
| 205 |
return crc32c; |
| 206 |
} |