tcp_mmap.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2018 Google Inc.
  4. * Author: Eric Dumazet (edumazet@google.com)
  5. *
  6. * Reference program demonstrating tcp mmap() usage,
  7. * and SO_RCVLOWAT hints for receiver.
  8. *
  9. * Note : NIC with header split is needed to use mmap() on TCP :
  10. * Each incoming frame must be a multiple of PAGE_SIZE bytes of TCP payload.
  11. *
  12. * How to use on loopback interface :
  13. *
  14. * ifconfig lo mtu 61512 # 15*4096 + 40 (ipv6 header) + 32 (TCP with TS option header)
  15. * tcp_mmap -s -z &
  16. * tcp_mmap -H ::1 -z
  17. *
  18. * Or leave default lo mtu, but use -M option to set TCP_MAXSEG option to (4096 + 12)
  19. * (4096 : page size on x86, 12: TCP TS option length)
  20. * tcp_mmap -s -z -M $((4096+12)) &
  21. * tcp_mmap -H ::1 -z -M $((4096+12))
  22. *
  23. * Note: -z option on sender uses MSG_ZEROCOPY, which forces a copy when packets go through loopback interface.
  24. * We might use sendfile() instead, but really this test program is about mmap(), for receivers ;)
  25. *
  26. * $ ./tcp_mmap -s & # Without mmap()
  27. * $ for i in {1..4}; do ./tcp_mmap -H ::1 -z ; done
  28. * received 32768 MB (0 % mmap'ed) in 14.1157 s, 19.4732 Gbit
  29. * cpu usage user:0.057 sys:7.815, 240.234 usec per MB, 65531 c-switches
  30. * received 32768 MB (0 % mmap'ed) in 14.6833 s, 18.7204 Gbit
  31. * cpu usage user:0.043 sys:8.103, 248.596 usec per MB, 65524 c-switches
  32. * received 32768 MB (0 % mmap'ed) in 11.143 s, 24.6682 Gbit
  33. * cpu usage user:0.044 sys:6.576, 202.026 usec per MB, 65519 c-switches
  34. * received 32768 MB (0 % mmap'ed) in 14.9056 s, 18.4413 Gbit
  35. * cpu usage user:0.036 sys:8.193, 251.129 usec per MB, 65530 c-switches
  36. * $ kill %1 # kill tcp_mmap server
  37. *
  38. * $ ./tcp_mmap -s -z & # With mmap()
  39. * $ for i in {1..4}; do ./tcp_mmap -H ::1 -z ; done
  40. * received 32768 MB (99.9939 % mmap'ed) in 6.73792 s, 40.7956 Gbit
  41. * cpu usage user:0.045 sys:2.827, 87.6465 usec per MB, 65532 c-switches
  42. * received 32768 MB (99.9939 % mmap'ed) in 7.26732 s, 37.8238 Gbit
  43. * cpu usage user:0.037 sys:3.087, 95.3369 usec per MB, 65532 c-switches
  44. * received 32768 MB (99.9939 % mmap'ed) in 7.61661 s, 36.0893 Gbit
  45. * cpu usage user:0.046 sys:3.559, 110.016 usec per MB, 65529 c-switches
  46. * received 32768 MB (99.9939 % mmap'ed) in 7.43764 s, 36.9577 Gbit
  47. * cpu usage user:0.035 sys:3.467, 106.873 usec per MB, 65530 c-switches
  48. */
  49. #define _GNU_SOURCE
  50. #include <pthread.h>
  51. #include <sys/types.h>
  52. #include <fcntl.h>
  53. #include <error.h>
  54. #include <sys/socket.h>
  55. #include <sys/mman.h>
  56. #include <sys/resource.h>
  57. #include <unistd.h>
  58. #include <string.h>
  59. #include <stdlib.h>
  60. #include <stdio.h>
  61. #include <errno.h>
  62. #include <time.h>
  63. #include <sys/time.h>
  64. #include <netinet/in.h>
  65. #include <arpa/inet.h>
  66. #include <poll.h>
  67. #include <linux/tcp.h>
  68. #include <assert.h>
  69. #include <openssl/pem.h>
  70. #ifndef MSG_ZEROCOPY
  71. #define MSG_ZEROCOPY 0x4000000
  72. #endif
  73. #ifndef min
  74. #define min(a, b) ((a) < (b) ? (a) : (b))
  75. #endif
  76. #define FILE_SZ (1ULL << 35)
  77. static int cfg_family = AF_INET6;
  78. static socklen_t cfg_alen = sizeof(struct sockaddr_in6);
  79. static int cfg_port = 8787;
  80. static int rcvbuf; /* Default: autotuning. Can be set with -r <integer> option */
  81. static int sndbuf; /* Default: autotuning. Can be set with -w <integer> option */
  82. static int zflg; /* zero copy option. (MSG_ZEROCOPY for sender, mmap() for receiver */
  83. static int xflg; /* hash received data (simple xor) (-h option) */
  84. static int keepflag; /* -k option: receiver shall keep all received file in memory (no munmap() calls) */
  85. static int integrity; /* -i option: sender and receiver compute sha256 over the data.*/
  86. static size_t chunk_size = 512*1024;
  87. static size_t map_align;
  88. unsigned long htotal;
  89. unsigned int digest_len;
  90. static inline void prefetch(const void *x)
  91. {
  92. #if defined(__x86_64__)
  93. asm volatile("prefetcht0 %P0" : : "m" (*(const char *)x));
  94. #endif
  95. }
  96. void hash_zone(void *zone, unsigned int length)
  97. {
  98. unsigned long temp = htotal;
  99. while (length >= 8*sizeof(long)) {
  100. prefetch(zone + 384);
  101. temp ^= *(unsigned long *)zone;
  102. temp ^= *(unsigned long *)(zone + sizeof(long));
  103. temp ^= *(unsigned long *)(zone + 2*sizeof(long));
  104. temp ^= *(unsigned long *)(zone + 3*sizeof(long));
  105. temp ^= *(unsigned long *)(zone + 4*sizeof(long));
  106. temp ^= *(unsigned long *)(zone + 5*sizeof(long));
  107. temp ^= *(unsigned long *)(zone + 6*sizeof(long));
  108. temp ^= *(unsigned long *)(zone + 7*sizeof(long));
  109. zone += 8*sizeof(long);
  110. length -= 8*sizeof(long);
  111. }
  112. while (length >= 1) {
  113. temp ^= *(unsigned char *)zone;
  114. zone += 1;
  115. length--;
  116. }
  117. htotal = temp;
  118. }
  119. #define ALIGN_UP(x, align_to) (((x) + ((align_to)-1)) & ~((align_to)-1))
  120. #define ALIGN_PTR_UP(p, ptr_align_to) ((typeof(p))ALIGN_UP((unsigned long)(p), ptr_align_to))
  121. static void *mmap_large_buffer(size_t need, size_t *allocated)
  122. {
  123. void *buffer;
  124. size_t sz;
  125. /* Attempt to use huge pages if possible. */
  126. sz = ALIGN_UP(need, map_align);
  127. buffer = mmap(NULL, sz, PROT_READ | PROT_WRITE,
  128. MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, -1, 0);
  129. if (buffer == (void *)-1) {
  130. sz = need;
  131. buffer = mmap(NULL, sz, PROT_READ | PROT_WRITE,
  132. MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE,
  133. -1, 0);
  134. if (buffer != (void *)-1)
  135. fprintf(stderr, "MAP_HUGETLB attempt failed, look at /sys/kernel/mm/hugepages for optimal performance\n");
  136. }
  137. *allocated = sz;
  138. return buffer;
  139. }
  140. static uint32_t tcp_info_get_rcv_mss(int fd)
  141. {
  142. socklen_t sz = sizeof(struct tcp_info);
  143. struct tcp_info info;
  144. if (getsockopt(fd, IPPROTO_TCP, TCP_INFO, &info, &sz)) {
  145. fprintf(stderr, "Error fetching TCP_INFO\n");
  146. return 0;
  147. }
  148. return info.tcpi_rcv_mss;
  149. }
  150. void *child_thread(void *arg)
  151. {
  152. unsigned char digest[SHA256_DIGEST_LENGTH];
  153. unsigned long total_mmap = 0, total = 0;
  154. struct tcp_zerocopy_receive zc;
  155. unsigned char *buffer = NULL;
  156. unsigned long delta_usec;
  157. EVP_MD_CTX *ctx = NULL;
  158. int flags = MAP_SHARED;
  159. struct timeval t0, t1;
  160. void *raddr = NULL;
  161. void *addr = NULL;
  162. double throughput;
  163. struct rusage ru;
  164. size_t buffer_sz;
  165. int lu, fd;
  166. fd = (int)(unsigned long)arg;
  167. gettimeofday(&t0, NULL);
  168. fcntl(fd, F_SETFL, O_NDELAY);
  169. buffer = mmap_large_buffer(chunk_size, &buffer_sz);
  170. if (buffer == (void *)-1) {
  171. perror("mmap");
  172. goto error;
  173. }
  174. if (zflg) {
  175. raddr = mmap(NULL, chunk_size + map_align, PROT_READ, flags, fd, 0);
  176. if (raddr == (void *)-1) {
  177. perror("mmap");
  178. zflg = 0;
  179. } else {
  180. addr = ALIGN_PTR_UP(raddr, map_align);
  181. }
  182. }
  183. if (integrity) {
  184. ctx = EVP_MD_CTX_new();
  185. if (!ctx) {
  186. perror("cannot enable SHA computing");
  187. goto error;
  188. }
  189. EVP_DigestInit_ex(ctx, EVP_sha256(), NULL);
  190. }
  191. while (1) {
  192. struct pollfd pfd = { .fd = fd, .events = POLLIN, };
  193. int sub;
  194. poll(&pfd, 1, 10000);
  195. if (zflg) {
  196. socklen_t zc_len = sizeof(zc);
  197. int res;
  198. memset(&zc, 0, sizeof(zc));
  199. zc.address = (__u64)((unsigned long)addr);
  200. zc.length = min(chunk_size, FILE_SZ - total);
  201. res = getsockopt(fd, IPPROTO_TCP, TCP_ZEROCOPY_RECEIVE,
  202. &zc, &zc_len);
  203. if (res == -1)
  204. break;
  205. if (zc.length) {
  206. assert(zc.length <= chunk_size);
  207. if (integrity)
  208. EVP_DigestUpdate(ctx, addr, zc.length);
  209. total_mmap += zc.length;
  210. if (xflg)
  211. hash_zone(addr, zc.length);
  212. /* It is more efficient to unmap the pages right now,
  213. * instead of doing this in next TCP_ZEROCOPY_RECEIVE.
  214. */
  215. madvise(addr, zc.length, MADV_DONTNEED);
  216. total += zc.length;
  217. }
  218. if (zc.recv_skip_hint) {
  219. assert(zc.recv_skip_hint <= chunk_size);
  220. lu = read(fd, buffer, min(zc.recv_skip_hint,
  221. FILE_SZ - total));
  222. if (lu > 0) {
  223. if (integrity)
  224. EVP_DigestUpdate(ctx, buffer, lu);
  225. if (xflg)
  226. hash_zone(buffer, lu);
  227. total += lu;
  228. }
  229. if (lu == 0)
  230. goto end;
  231. }
  232. continue;
  233. }
  234. sub = 0;
  235. while (sub < chunk_size) {
  236. lu = read(fd, buffer + sub, min(chunk_size - sub,
  237. FILE_SZ - total));
  238. if (lu == 0)
  239. goto end;
  240. if (lu < 0)
  241. break;
  242. if (integrity)
  243. EVP_DigestUpdate(ctx, buffer + sub, lu);
  244. if (xflg)
  245. hash_zone(buffer + sub, lu);
  246. total += lu;
  247. sub += lu;
  248. }
  249. }
  250. end:
  251. gettimeofday(&t1, NULL);
  252. delta_usec = (t1.tv_sec - t0.tv_sec) * 1000000 + t1.tv_usec - t0.tv_usec;
  253. if (integrity) {
  254. fcntl(fd, F_SETFL, 0);
  255. EVP_DigestFinal_ex(ctx, digest, &digest_len);
  256. lu = read(fd, buffer, SHA256_DIGEST_LENGTH);
  257. if (lu != SHA256_DIGEST_LENGTH)
  258. perror("Error: Cannot read SHA256\n");
  259. if (memcmp(digest, buffer,
  260. SHA256_DIGEST_LENGTH))
  261. fprintf(stderr, "Error: SHA256 of the data is not right\n");
  262. else
  263. printf("\nSHA256 is correct\n");
  264. }
  265. throughput = 0;
  266. if (delta_usec)
  267. throughput = total * 8.0 / (double)delta_usec / 1000.0;
  268. getrusage(RUSAGE_THREAD, &ru);
  269. if (total > 1024*1024) {
  270. unsigned long total_usec;
  271. unsigned long mb = total >> 20;
  272. total_usec = 1000000*ru.ru_utime.tv_sec + ru.ru_utime.tv_usec +
  273. 1000000*ru.ru_stime.tv_sec + ru.ru_stime.tv_usec;
  274. printf("received %lg MB (%lg %% mmap'ed) in %lg s, %lg Gbit\n"
  275. " cpu usage user:%lg sys:%lg, %lg usec per MB, %lu c-switches, rcv_mss %u\n",
  276. total / (1024.0 * 1024.0),
  277. 100.0*total_mmap/total,
  278. (double)delta_usec / 1000000.0,
  279. throughput,
  280. (double)ru.ru_utime.tv_sec + (double)ru.ru_utime.tv_usec / 1000000.0,
  281. (double)ru.ru_stime.tv_sec + (double)ru.ru_stime.tv_usec / 1000000.0,
  282. (double)total_usec/mb,
  283. ru.ru_nvcsw,
  284. tcp_info_get_rcv_mss(fd));
  285. }
  286. error:
  287. munmap(buffer, buffer_sz);
  288. close(fd);
  289. if (zflg)
  290. munmap(raddr, chunk_size + map_align);
  291. pthread_exit(0);
  292. }
  293. static void apply_rcvsnd_buf(int fd)
  294. {
  295. if (rcvbuf && setsockopt(fd, SOL_SOCKET,
  296. SO_RCVBUF, &rcvbuf, sizeof(rcvbuf)) == -1) {
  297. perror("setsockopt SO_RCVBUF");
  298. }
  299. if (sndbuf && setsockopt(fd, SOL_SOCKET,
  300. SO_SNDBUF, &sndbuf, sizeof(sndbuf)) == -1) {
  301. perror("setsockopt SO_SNDBUF");
  302. }
  303. }
  304. static void setup_sockaddr(int domain, const char *str_addr,
  305. struct sockaddr_storage *sockaddr)
  306. {
  307. struct sockaddr_in6 *addr6 = (void *) sockaddr;
  308. struct sockaddr_in *addr4 = (void *) sockaddr;
  309. switch (domain) {
  310. case PF_INET:
  311. memset(addr4, 0, sizeof(*addr4));
  312. addr4->sin_family = AF_INET;
  313. addr4->sin_port = htons(cfg_port);
  314. if (str_addr &&
  315. inet_pton(AF_INET, str_addr, &(addr4->sin_addr)) != 1)
  316. error(1, 0, "ipv4 parse error: %s", str_addr);
  317. break;
  318. case PF_INET6:
  319. memset(addr6, 0, sizeof(*addr6));
  320. addr6->sin6_family = AF_INET6;
  321. addr6->sin6_port = htons(cfg_port);
  322. if (str_addr &&
  323. inet_pton(AF_INET6, str_addr, &(addr6->sin6_addr)) != 1)
  324. error(1, 0, "ipv6 parse error: %s", str_addr);
  325. break;
  326. default:
  327. error(1, 0, "illegal domain");
  328. }
  329. }
  330. static void do_accept(int fdlisten)
  331. {
  332. pthread_attr_t attr;
  333. int rcvlowat;
  334. pthread_attr_init(&attr);
  335. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  336. rcvlowat = chunk_size;
  337. if (setsockopt(fdlisten, SOL_SOCKET, SO_RCVLOWAT,
  338. &rcvlowat, sizeof(rcvlowat)) == -1) {
  339. perror("setsockopt SO_RCVLOWAT");
  340. }
  341. apply_rcvsnd_buf(fdlisten);
  342. while (1) {
  343. struct sockaddr_in addr;
  344. socklen_t addrlen = sizeof(addr);
  345. pthread_t th;
  346. int fd, res;
  347. fd = accept(fdlisten, (struct sockaddr *)&addr, &addrlen);
  348. if (fd == -1) {
  349. perror("accept");
  350. continue;
  351. }
  352. res = pthread_create(&th, &attr, child_thread,
  353. (void *)(unsigned long)fd);
  354. if (res) {
  355. errno = res;
  356. perror("pthread_create");
  357. close(fd);
  358. }
  359. }
  360. }
  361. /* Each thread should reserve a big enough vma to avoid
  362. * spinlock collisions in ptl locks.
  363. * This size is 2MB on x86_64, and is exported in /proc/meminfo.
  364. */
  365. static unsigned long default_huge_page_size(void)
  366. {
  367. FILE *f = fopen("/proc/meminfo", "r");
  368. unsigned long hps = 0;
  369. size_t linelen = 0;
  370. char *line = NULL;
  371. if (!f)
  372. return 0;
  373. while (getline(&line, &linelen, f) > 0) {
  374. if (sscanf(line, "Hugepagesize: %lu kB", &hps) == 1) {
  375. hps <<= 10;
  376. break;
  377. }
  378. }
  379. free(line);
  380. fclose(f);
  381. return hps;
  382. }
  383. static void randomize(void *target, size_t count)
  384. {
  385. static int urandom = -1;
  386. ssize_t got;
  387. urandom = open("/dev/urandom", O_RDONLY);
  388. if (urandom < 0) {
  389. perror("open /dev/urandom");
  390. exit(1);
  391. }
  392. got = read(urandom, target, count);
  393. if (got != count) {
  394. perror("read /dev/urandom");
  395. exit(1);
  396. }
  397. }
  398. int main(int argc, char *argv[])
  399. {
  400. unsigned char digest[SHA256_DIGEST_LENGTH];
  401. struct sockaddr_storage listenaddr, addr;
  402. unsigned int max_pacing_rate = 0;
  403. EVP_MD_CTX *ctx = NULL;
  404. unsigned char *buffer;
  405. uint64_t total = 0;
  406. char *host = NULL;
  407. int fd, c, on = 1;
  408. size_t buffer_sz;
  409. int sflg = 0;
  410. int mss = 0;
  411. while ((c = getopt(argc, argv, "46p:svr:w:H:zxkP:M:C:a:i")) != -1) {
  412. switch (c) {
  413. case '4':
  414. cfg_family = PF_INET;
  415. cfg_alen = sizeof(struct sockaddr_in);
  416. break;
  417. case '6':
  418. cfg_family = PF_INET6;
  419. cfg_alen = sizeof(struct sockaddr_in6);
  420. break;
  421. case 'p':
  422. cfg_port = atoi(optarg);
  423. break;
  424. case 'H':
  425. host = optarg;
  426. break;
  427. case 's': /* server : listen for incoming connections */
  428. sflg++;
  429. break;
  430. case 'r':
  431. rcvbuf = atoi(optarg);
  432. break;
  433. case 'w':
  434. sndbuf = atoi(optarg);
  435. break;
  436. case 'z':
  437. zflg = 1;
  438. break;
  439. case 'M':
  440. mss = atoi(optarg);
  441. break;
  442. case 'x':
  443. xflg = 1;
  444. break;
  445. case 'k':
  446. keepflag = 1;
  447. break;
  448. case 'P':
  449. max_pacing_rate = atoi(optarg) ;
  450. break;
  451. case 'C':
  452. chunk_size = atol(optarg);
  453. break;
  454. case 'a':
  455. map_align = atol(optarg);
  456. break;
  457. case 'i':
  458. integrity = 1;
  459. break;
  460. default:
  461. exit(1);
  462. }
  463. }
  464. if (!map_align) {
  465. map_align = default_huge_page_size();
  466. /* if really /proc/meminfo is not helping,
  467. * we use the default x86_64 hugepagesize.
  468. */
  469. if (!map_align)
  470. map_align = 2*1024*1024;
  471. }
  472. if (sflg) {
  473. int fdlisten = socket(cfg_family, SOCK_STREAM, 0);
  474. if (fdlisten == -1) {
  475. perror("socket");
  476. exit(1);
  477. }
  478. apply_rcvsnd_buf(fdlisten);
  479. setsockopt(fdlisten, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
  480. setup_sockaddr(cfg_family, host, &listenaddr);
  481. if (mss &&
  482. setsockopt(fdlisten, IPPROTO_TCP, TCP_MAXSEG,
  483. &mss, sizeof(mss)) == -1) {
  484. perror("setsockopt TCP_MAXSEG");
  485. exit(1);
  486. }
  487. if (bind(fdlisten, (const struct sockaddr *)&listenaddr, cfg_alen) == -1) {
  488. perror("bind");
  489. exit(1);
  490. }
  491. if (listen(fdlisten, 128) == -1) {
  492. perror("listen");
  493. exit(1);
  494. }
  495. do_accept(fdlisten);
  496. }
  497. buffer = mmap_large_buffer(chunk_size, &buffer_sz);
  498. if (buffer == (unsigned char *)-1) {
  499. perror("mmap");
  500. exit(1);
  501. }
  502. fd = socket(cfg_family, SOCK_STREAM, 0);
  503. if (fd == -1) {
  504. perror("socket");
  505. exit(1);
  506. }
  507. apply_rcvsnd_buf(fd);
  508. setup_sockaddr(cfg_family, host, &addr);
  509. if (mss &&
  510. setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &mss, sizeof(mss)) == -1) {
  511. perror("setsockopt TCP_MAXSEG");
  512. exit(1);
  513. }
  514. if (connect(fd, (const struct sockaddr *)&addr, cfg_alen) == -1) {
  515. perror("connect");
  516. exit(1);
  517. }
  518. if (max_pacing_rate &&
  519. setsockopt(fd, SOL_SOCKET, SO_MAX_PACING_RATE,
  520. &max_pacing_rate, sizeof(max_pacing_rate)) == -1)
  521. perror("setsockopt SO_MAX_PACING_RATE");
  522. if (zflg && setsockopt(fd, SOL_SOCKET, SO_ZEROCOPY,
  523. &on, sizeof(on)) == -1) {
  524. perror("setsockopt SO_ZEROCOPY, (-z option disabled)");
  525. zflg = 0;
  526. }
  527. if (integrity) {
  528. randomize(buffer, buffer_sz);
  529. ctx = EVP_MD_CTX_new();
  530. if (!ctx) {
  531. perror("cannot enable SHA computing");
  532. exit(1);
  533. }
  534. EVP_DigestInit_ex(ctx, EVP_sha256(), NULL);
  535. }
  536. while (total < FILE_SZ) {
  537. size_t offset = total % chunk_size;
  538. int64_t wr = FILE_SZ - total;
  539. if (wr > chunk_size - offset)
  540. wr = chunk_size - offset;
  541. /* Note : we just want to fill the pipe with random bytes */
  542. wr = send(fd, buffer + offset,
  543. (size_t)wr, zflg ? MSG_ZEROCOPY : 0);
  544. if (wr <= 0)
  545. break;
  546. if (integrity)
  547. EVP_DigestUpdate(ctx, buffer + offset, wr);
  548. total += wr;
  549. }
  550. if (integrity && total == FILE_SZ) {
  551. EVP_DigestFinal_ex(ctx, digest, &digest_len);
  552. send(fd, digest, (size_t)SHA256_DIGEST_LENGTH, 0);
  553. }
  554. close(fd);
  555. munmap(buffer, buffer_sz);
  556. return 0;
  557. }