bpf_zerocopy.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2007 Seccuris Inc.
  5. * All rights reserved.
  6. *
  7. * This software was developed by Robert N. M. Watson under contract to
  8. * Seccuris Inc.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. */
  31. #include <sys/cdefs.h>
  32. #include "opt_bpf.h"
  33. #include <sys/param.h>
  34. #include <sys/lock.h>
  35. #include <sys/malloc.h>
  36. #include <sys/mbuf.h>
  37. #include <sys/mutex.h>
  38. #include <sys/proc.h>
  39. #include <sys/sf_buf.h>
  40. #include <sys/socket.h>
  41. #include <sys/uio.h>
  42. #include <machine/atomic.h>
  43. #include <net/if.h>
  44. #include <net/bpf.h>
  45. #include <net/bpf_zerocopy.h>
  46. #include <net/bpfdesc.h>
  47. #include <vm/vm.h>
  48. #include <vm/vm_param.h>
  49. #include <vm/pmap.h>
  50. #include <vm/vm_extern.h>
  51. #include <vm/vm_map.h>
  52. #include <vm/vm_page.h>
  53. /*
  54. * Zero-copy buffer scheme for BPF: user space "donates" two buffers, which
  55. * are mapped into the kernel address space using sf_bufs and used directly
  56. * by BPF. Memory is wired since page faults cannot be tolerated in the
  57. * contexts where the buffers are copied to (locks held, interrupt context,
  58. * etc). Access to shared memory buffers is synchronized using a header on
  59. * each buffer, allowing the number of system calls to go to zero as BPF
  60. * reaches saturation (buffers filled as fast as they can be drained by the
  61. * user process). Full details of the protocol for communicating between the
  62. * user process and BPF may be found in bpf(4).
  63. */
  64. /*
  65. * Maximum number of pages per buffer. Since all BPF devices use two, the
  66. * maximum per device is 2*BPF_MAX_PAGES. Resource limits on the number of
  67. * sf_bufs may be an issue, so do not set this too high. On older systems,
  68. * kernel address space limits may also be an issue.
  69. */
  70. #define BPF_MAX_PAGES 512
  71. /*
  72. * struct zbuf describes a memory buffer loaned by a user process to the
  73. * kernel. We represent this as a series of pages managed using an array of
  74. * sf_bufs. Even though the memory is contiguous in user space, it may not
  75. * be mapped contiguously in the kernel (i.e., a set of physically
  76. * non-contiguous pages in the direct map region) so we must implement
  77. * scatter-gather copying. One significant mitigating factor is that on
  78. * systems with a direct memory map, we can avoid TLB misses.
  79. *
  80. * At the front of the shared memory region is a bpf_zbuf_header, which
  81. * contains shared control data to allow user space and the kernel to
  82. * synchronize; this is included in zb_size, but not bpf_bufsize, so that BPF
  83. * knows that the space is not available.
  84. */
  85. struct zbuf {
  86. vm_offset_t zb_uaddr; /* User address at time of setup. */
  87. size_t zb_size; /* Size of buffer, incl. header. */
  88. u_int zb_numpages; /* Number of pages. */
  89. int zb_flags; /* Flags on zbuf. */
  90. struct sf_buf **zb_pages; /* Pages themselves. */
  91. struct bpf_zbuf_header *zb_header; /* Shared header. */
  92. };
  93. /*
  94. * When a buffer has been assigned to userspace, flag it as such, as the
  95. * buffer may remain in the store position as a result of the user process
  96. * not yet having acknowledged the buffer in the hold position yet.
  97. */
  98. #define ZBUF_FLAG_ASSIGNED 0x00000001 /* Set when owned by user. */
  99. /*
  100. * Release a page we've previously wired.
  101. */
  102. static void
  103. zbuf_page_free(vm_page_t pp)
  104. {
  105. vm_page_unwire(pp, PQ_INACTIVE);
  106. }
  107. /*
  108. * Free an sf_buf with attached page.
  109. */
  110. static void
  111. zbuf_sfbuf_free(struct sf_buf *sf)
  112. {
  113. vm_page_t pp;
  114. pp = sf_buf_page(sf);
  115. sf_buf_free(sf);
  116. zbuf_page_free(pp);
  117. }
  118. /*
  119. * Free a zbuf, including its page array, sbufs, and pages. Allow partially
  120. * allocated zbufs to be freed so that it may be used even during a zbuf
  121. * setup.
  122. */
  123. static void
  124. zbuf_free(struct zbuf *zb)
  125. {
  126. int i;
  127. for (i = 0; i < zb->zb_numpages; i++) {
  128. if (zb->zb_pages[i] != NULL)
  129. zbuf_sfbuf_free(zb->zb_pages[i]);
  130. }
  131. free(zb->zb_pages, M_BPF);
  132. free(zb, M_BPF);
  133. }
  134. /*
  135. * Given a user pointer to a page of user memory, return an sf_buf for the
  136. * page. Because we may be requesting quite a few sf_bufs, prefer failure to
  137. * deadlock and use SFB_NOWAIT.
  138. */
  139. static struct sf_buf *
  140. zbuf_sfbuf_get(struct vm_map *map, vm_offset_t uaddr)
  141. {
  142. struct sf_buf *sf;
  143. vm_page_t pp;
  144. if (vm_fault_quick_hold_pages(map, uaddr, PAGE_SIZE, VM_PROT_READ |
  145. VM_PROT_WRITE, &pp, 1) < 0)
  146. return (NULL);
  147. sf = sf_buf_alloc(pp, SFB_NOWAIT);
  148. if (sf == NULL) {
  149. zbuf_page_free(pp);
  150. return (NULL);
  151. }
  152. return (sf);
  153. }
  154. /*
  155. * Create a zbuf describing a range of user address space memory. Validate
  156. * page alignment, size requirements, etc.
  157. */
  158. static int
  159. zbuf_setup(struct thread *td, vm_offset_t uaddr, size_t len,
  160. struct zbuf **zbp)
  161. {
  162. struct zbuf *zb;
  163. struct vm_map *map;
  164. int error, i;
  165. *zbp = NULL;
  166. /*
  167. * User address must be page-aligned.
  168. */
  169. if (uaddr & PAGE_MASK)
  170. return (EINVAL);
  171. /*
  172. * Length must be an integer number of full pages.
  173. */
  174. if (len & PAGE_MASK)
  175. return (EINVAL);
  176. /*
  177. * Length must not exceed per-buffer resource limit.
  178. */
  179. if ((len / PAGE_SIZE) > BPF_MAX_PAGES)
  180. return (EINVAL);
  181. /*
  182. * Allocate the buffer and set up each page with is own sf_buf.
  183. */
  184. error = 0;
  185. zb = malloc(sizeof(*zb), M_BPF, M_ZERO | M_WAITOK);
  186. zb->zb_uaddr = uaddr;
  187. zb->zb_size = len;
  188. zb->zb_numpages = len / PAGE_SIZE;
  189. zb->zb_pages = malloc(sizeof(struct sf_buf *) *
  190. zb->zb_numpages, M_BPF, M_ZERO | M_WAITOK);
  191. map = &td->td_proc->p_vmspace->vm_map;
  192. for (i = 0; i < zb->zb_numpages; i++) {
  193. zb->zb_pages[i] = zbuf_sfbuf_get(map,
  194. uaddr + (i * PAGE_SIZE));
  195. if (zb->zb_pages[i] == NULL) {
  196. error = EFAULT;
  197. goto error;
  198. }
  199. }
  200. zb->zb_header =
  201. (struct bpf_zbuf_header *)sf_buf_kva(zb->zb_pages[0]);
  202. bzero(zb->zb_header, sizeof(*zb->zb_header));
  203. *zbp = zb;
  204. return (0);
  205. error:
  206. zbuf_free(zb);
  207. return (error);
  208. }
  209. /*
  210. * Copy bytes from a source into the specified zbuf. The caller is
  211. * responsible for performing bounds checking, etc.
  212. */
  213. void
  214. bpf_zerocopy_append_bytes(struct bpf_d *d, caddr_t buf, u_int offset,
  215. void *src, u_int len)
  216. {
  217. u_int count, page, poffset;
  218. u_char *src_bytes;
  219. struct zbuf *zb;
  220. KASSERT(d->bd_bufmode == BPF_BUFMODE_ZBUF,
  221. ("bpf_zerocopy_append_bytes: not in zbuf mode"));
  222. KASSERT(buf != NULL, ("bpf_zerocopy_append_bytes: NULL buf"));
  223. src_bytes = (u_char *)src;
  224. zb = (struct zbuf *)buf;
  225. KASSERT((zb->zb_flags & ZBUF_FLAG_ASSIGNED) == 0,
  226. ("bpf_zerocopy_append_bytes: ZBUF_FLAG_ASSIGNED"));
  227. /*
  228. * Scatter-gather copy to user pages mapped into kernel address space
  229. * using sf_bufs: copy up to a page at a time.
  230. */
  231. offset += sizeof(struct bpf_zbuf_header);
  232. page = offset / PAGE_SIZE;
  233. poffset = offset % PAGE_SIZE;
  234. while (len > 0) {
  235. KASSERT(page < zb->zb_numpages, ("bpf_zerocopy_append_bytes:"
  236. " page overflow (%d p %d np)\n", page, zb->zb_numpages));
  237. count = min(len, PAGE_SIZE - poffset);
  238. bcopy(src_bytes, ((u_char *)sf_buf_kva(zb->zb_pages[page])) +
  239. poffset, count);
  240. poffset += count;
  241. if (poffset == PAGE_SIZE) {
  242. poffset = 0;
  243. page++;
  244. }
  245. KASSERT(poffset < PAGE_SIZE,
  246. ("bpf_zerocopy_append_bytes: page offset overflow (%d)",
  247. poffset));
  248. len -= count;
  249. src_bytes += count;
  250. }
  251. }
  252. /*
  253. * Copy bytes from an mbuf chain to the specified zbuf: copying will be
  254. * scatter-gather both from mbufs, which may be fragmented over memory, and
  255. * to pages, which may not be contiguously mapped in kernel address space.
  256. * As with bpf_zerocopy_append_bytes(), the caller is responsible for
  257. * checking that this will not exceed the buffer limit.
  258. */
  259. void
  260. bpf_zerocopy_append_mbuf(struct bpf_d *d, caddr_t buf, u_int offset,
  261. void *src, u_int len)
  262. {
  263. u_int count, moffset, page, poffset;
  264. const struct mbuf *m;
  265. struct zbuf *zb;
  266. KASSERT(d->bd_bufmode == BPF_BUFMODE_ZBUF,
  267. ("bpf_zerocopy_append_mbuf not in zbuf mode"));
  268. KASSERT(buf != NULL, ("bpf_zerocopy_append_mbuf: NULL buf"));
  269. m = (struct mbuf *)src;
  270. zb = (struct zbuf *)buf;
  271. KASSERT((zb->zb_flags & ZBUF_FLAG_ASSIGNED) == 0,
  272. ("bpf_zerocopy_append_mbuf: ZBUF_FLAG_ASSIGNED"));
  273. /*
  274. * Scatter gather both from an mbuf chain and to a user page set
  275. * mapped into kernel address space using sf_bufs. If we're lucky,
  276. * each mbuf requires one copy operation, but if page alignment and
  277. * mbuf alignment work out less well, we'll be doing two copies per
  278. * mbuf.
  279. */
  280. offset += sizeof(struct bpf_zbuf_header);
  281. page = offset / PAGE_SIZE;
  282. poffset = offset % PAGE_SIZE;
  283. moffset = 0;
  284. while (len > 0) {
  285. KASSERT(page < zb->zb_numpages,
  286. ("bpf_zerocopy_append_mbuf: page overflow (%d p %d "
  287. "np)\n", page, zb->zb_numpages));
  288. KASSERT(m != NULL,
  289. ("bpf_zerocopy_append_mbuf: end of mbuf chain"));
  290. count = min(m->m_len - moffset, len);
  291. count = min(count, PAGE_SIZE - poffset);
  292. bcopy(mtod(m, u_char *) + moffset,
  293. ((u_char *)sf_buf_kva(zb->zb_pages[page])) + poffset,
  294. count);
  295. poffset += count;
  296. if (poffset == PAGE_SIZE) {
  297. poffset = 0;
  298. page++;
  299. }
  300. KASSERT(poffset < PAGE_SIZE,
  301. ("bpf_zerocopy_append_mbuf: page offset overflow (%d)",
  302. poffset));
  303. moffset += count;
  304. if (moffset == m->m_len) {
  305. m = m->m_next;
  306. moffset = 0;
  307. }
  308. len -= count;
  309. }
  310. }
  311. /*
  312. * Notification from the BPF framework that a buffer in the store position is
  313. * rejecting packets and may be considered full. We mark the buffer as
  314. * immutable and assign to userspace so that it is immediately available for
  315. * the user process to access.
  316. */
  317. void
  318. bpf_zerocopy_buffull(struct bpf_d *d)
  319. {
  320. struct zbuf *zb;
  321. KASSERT(d->bd_bufmode == BPF_BUFMODE_ZBUF,
  322. ("bpf_zerocopy_buffull: not in zbuf mode"));
  323. zb = (struct zbuf *)d->bd_sbuf;
  324. KASSERT(zb != NULL, ("bpf_zerocopy_buffull: zb == NULL"));
  325. if ((zb->zb_flags & ZBUF_FLAG_ASSIGNED) == 0) {
  326. zb->zb_flags |= ZBUF_FLAG_ASSIGNED;
  327. zb->zb_header->bzh_kernel_len = d->bd_slen;
  328. atomic_add_rel_int(&zb->zb_header->bzh_kernel_gen, 1);
  329. }
  330. }
  331. /*
  332. * Notification from the BPF framework that a buffer has moved into the held
  333. * slot on a descriptor. Zero-copy BPF will update the shared page to let
  334. * the user process know and flag the buffer as assigned if it hasn't already
  335. * been marked assigned due to filling while it was in the store position.
  336. *
  337. * Note: identical logic as in bpf_zerocopy_buffull(), except that we operate
  338. * on bd_hbuf and bd_hlen.
  339. */
  340. void
  341. bpf_zerocopy_bufheld(struct bpf_d *d)
  342. {
  343. struct zbuf *zb;
  344. KASSERT(d->bd_bufmode == BPF_BUFMODE_ZBUF,
  345. ("bpf_zerocopy_bufheld: not in zbuf mode"));
  346. zb = (struct zbuf *)d->bd_hbuf;
  347. KASSERT(zb != NULL, ("bpf_zerocopy_bufheld: zb == NULL"));
  348. if ((zb->zb_flags & ZBUF_FLAG_ASSIGNED) == 0) {
  349. zb->zb_flags |= ZBUF_FLAG_ASSIGNED;
  350. zb->zb_header->bzh_kernel_len = d->bd_hlen;
  351. atomic_add_rel_int(&zb->zb_header->bzh_kernel_gen, 1);
  352. }
  353. }
  354. /*
  355. * Notification from the BPF framework that the free buffer has been been
  356. * rotated out of the held position to the free position. This happens when
  357. * the user acknowledges the held buffer.
  358. */
  359. void
  360. bpf_zerocopy_buf_reclaimed(struct bpf_d *d)
  361. {
  362. struct zbuf *zb;
  363. KASSERT(d->bd_bufmode == BPF_BUFMODE_ZBUF,
  364. ("bpf_zerocopy_reclaim_buf: not in zbuf mode"));
  365. KASSERT(d->bd_fbuf != NULL,
  366. ("bpf_zerocopy_buf_reclaimed: NULL free buf"));
  367. zb = (struct zbuf *)d->bd_fbuf;
  368. zb->zb_flags &= ~ZBUF_FLAG_ASSIGNED;
  369. }
  370. /*
  371. * Query from the BPF framework regarding whether the buffer currently in the
  372. * held position can be moved to the free position, which can be indicated by
  373. * the user process making their generation number equal to the kernel
  374. * generation number.
  375. */
  376. int
  377. bpf_zerocopy_canfreebuf(struct bpf_d *d)
  378. {
  379. struct zbuf *zb;
  380. KASSERT(d->bd_bufmode == BPF_BUFMODE_ZBUF,
  381. ("bpf_zerocopy_canfreebuf: not in zbuf mode"));
  382. zb = (struct zbuf *)d->bd_hbuf;
  383. if (zb == NULL)
  384. return (0);
  385. if (zb->zb_header->bzh_kernel_gen ==
  386. atomic_load_acq_int(&zb->zb_header->bzh_user_gen))
  387. return (1);
  388. return (0);
  389. }
  390. /*
  391. * Query from the BPF framework as to whether or not the buffer current in
  392. * the store position can actually be written to. This may return false if
  393. * the store buffer is assigned to userspace before the hold buffer is
  394. * acknowledged.
  395. */
  396. int
  397. bpf_zerocopy_canwritebuf(struct bpf_d *d)
  398. {
  399. struct zbuf *zb;
  400. KASSERT(d->bd_bufmode == BPF_BUFMODE_ZBUF,
  401. ("bpf_zerocopy_canwritebuf: not in zbuf mode"));
  402. zb = (struct zbuf *)d->bd_sbuf;
  403. KASSERT(zb != NULL, ("bpf_zerocopy_canwritebuf: bd_sbuf NULL"));
  404. if (zb->zb_flags & ZBUF_FLAG_ASSIGNED)
  405. return (0);
  406. return (1);
  407. }
  408. /*
  409. * Free zero copy buffers at request of descriptor.
  410. */
  411. void
  412. bpf_zerocopy_free(struct bpf_d *d)
  413. {
  414. struct zbuf *zb;
  415. KASSERT(d->bd_bufmode == BPF_BUFMODE_ZBUF,
  416. ("bpf_zerocopy_free: not in zbuf mode"));
  417. zb = (struct zbuf *)d->bd_sbuf;
  418. if (zb != NULL)
  419. zbuf_free(zb);
  420. zb = (struct zbuf *)d->bd_hbuf;
  421. if (zb != NULL)
  422. zbuf_free(zb);
  423. zb = (struct zbuf *)d->bd_fbuf;
  424. if (zb != NULL)
  425. zbuf_free(zb);
  426. }
  427. /*
  428. * Ioctl to return the maximum buffer size.
  429. */
  430. int
  431. bpf_zerocopy_ioctl_getzmax(struct thread *td, struct bpf_d *d, size_t *i)
  432. {
  433. KASSERT(d->bd_bufmode == BPF_BUFMODE_ZBUF,
  434. ("bpf_zerocopy_ioctl_getzmax: not in zbuf mode"));
  435. *i = BPF_MAX_PAGES * PAGE_SIZE;
  436. return (0);
  437. }
  438. /*
  439. * Ioctl to force rotation of the two buffers, if there's any data available.
  440. * This can be used by user space to implement timeouts when waiting for a
  441. * buffer to fill.
  442. */
  443. int
  444. bpf_zerocopy_ioctl_rotzbuf(struct thread *td, struct bpf_d *d,
  445. struct bpf_zbuf *bz)
  446. {
  447. struct zbuf *bzh;
  448. bzero(bz, sizeof(*bz));
  449. BPFD_LOCK(d);
  450. if (d->bd_hbuf == NULL && d->bd_slen != 0) {
  451. ROTATE_BUFFERS(d);
  452. bzh = (struct zbuf *)d->bd_hbuf;
  453. bz->bz_bufa = (void *)bzh->zb_uaddr;
  454. bz->bz_buflen = d->bd_hlen;
  455. }
  456. BPFD_UNLOCK(d);
  457. return (0);
  458. }
  459. /*
  460. * Ioctl to configure zero-copy buffers -- may be done only once.
  461. */
  462. int
  463. bpf_zerocopy_ioctl_setzbuf(struct thread *td, struct bpf_d *d,
  464. struct bpf_zbuf *bz)
  465. {
  466. struct zbuf *zba, *zbb;
  467. int error;
  468. KASSERT(d->bd_bufmode == BPF_BUFMODE_ZBUF,
  469. ("bpf_zerocopy_ioctl_setzbuf: not in zbuf mode"));
  470. /*
  471. * Must set both buffers. Cannot clear them.
  472. */
  473. if (bz->bz_bufa == NULL || bz->bz_bufb == NULL)
  474. return (EINVAL);
  475. /*
  476. * Buffers must have a size greater than 0. Alignment and other size
  477. * validity checking is done in zbuf_setup().
  478. */
  479. if (bz->bz_buflen == 0)
  480. return (EINVAL);
  481. /*
  482. * Allocate new buffers.
  483. */
  484. error = zbuf_setup(td, (vm_offset_t)bz->bz_bufa, bz->bz_buflen,
  485. &zba);
  486. if (error)
  487. return (error);
  488. error = zbuf_setup(td, (vm_offset_t)bz->bz_bufb, bz->bz_buflen,
  489. &zbb);
  490. if (error) {
  491. zbuf_free(zba);
  492. return (error);
  493. }
  494. /*
  495. * We only allow buffers to be installed once, so atomically check
  496. * that no buffers are currently installed and install new buffers.
  497. */
  498. BPFD_LOCK(d);
  499. if (d->bd_hbuf != NULL || d->bd_sbuf != NULL || d->bd_fbuf != NULL ||
  500. d->bd_bif != NULL) {
  501. BPFD_UNLOCK(d);
  502. zbuf_free(zba);
  503. zbuf_free(zbb);
  504. return (EINVAL);
  505. }
  506. /*
  507. * Point BPF descriptor at buffers; initialize sbuf as zba so that
  508. * it is always filled first in the sequence, per bpf(4).
  509. */
  510. d->bd_fbuf = (caddr_t)zbb;
  511. d->bd_sbuf = (caddr_t)zba;
  512. d->bd_slen = 0;
  513. d->bd_hlen = 0;
  514. /*
  515. * We expose only the space left in the buffer after the size of the
  516. * shared management region.
  517. */
  518. d->bd_bufsize = bz->bz_buflen - sizeof(struct bpf_zbuf_header);
  519. BPFD_UNLOCK(d);
  520. return (0);
  521. }