2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2007 Seccuris Inc.
7 * This software was developed by Robert N. M. Watson under contract to
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
39 #include <sys/malloc.h>
41 #include <sys/mutex.h>
43 #include <sys/sf_buf.h>
44 #include <sys/socket.h>
47 #include <machine/atomic.h>
51 #include <net/bpf_zerocopy.h>
52 #include <net/bpfdesc.h>
55 #include <vm/vm_param.h>
57 #include <vm/vm_extern.h>
58 #include <vm/vm_map.h>
59 #include <vm/vm_page.h>
62 * Zero-copy buffer scheme for BPF: user space "donates" two buffers, which
63 * are mapped into the kernel address space using sf_bufs and used directly
64 * by BPF. Memory is wired since page faults cannot be tolerated in the
65 * contexts where the buffers are copied to (locks held, interrupt context,
66 * etc). Access to shared memory buffers is synchronized using a header on
67 * each buffer, allowing the number of system calls to go to zero as BPF
68 * reaches saturation (buffers filled as fast as they can be drained by the
69 * user process). Full details of the protocol for communicating between the
70 * user process and BPF may be found in bpf(4).
74 * Maximum number of pages per buffer. Since all BPF devices use two, the
75 * maximum per device is 2*BPF_MAX_PAGES. Resource limits on the number of
76 * sf_bufs may be an issue, so do not set this too high. On older systems,
77 * kernel address space limits may also be an issue.
79 #define BPF_MAX_PAGES 512
82 * struct zbuf describes a memory buffer loaned by a user process to the
83 * kernel. We represent this as a series of pages managed using an array of
84 * sf_bufs. Even though the memory is contiguous in user space, it may not
85 * be mapped contiguously in the kernel (i.e., a set of physically
86 * non-contiguous pages in the direct map region) so we must implement
87 * scatter-gather copying. One significant mitigating factor is that on
88 * systems with a direct memory map, we can avoid TLB misses.
90 * At the front of the shared memory region is a bpf_zbuf_header, which
91 * contains shared control data to allow user space and the kernel to
92 * synchronize; this is included in zb_size, but not bpf_bufsize, so that BPF
93 * knows that the space is not available.
96 vm_offset_t zb_uaddr; /* User address at time of setup. */
97 size_t zb_size; /* Size of buffer, incl. header. */
98 u_int zb_numpages; /* Number of pages. */
99 int zb_flags; /* Flags on zbuf. */
100 struct sf_buf **zb_pages; /* Pages themselves. */
101 struct bpf_zbuf_header *zb_header; /* Shared header. */
105 * When a buffer has been assigned to userspace, flag it as such, as the
106 * buffer may remain in the store position as a result of the user process
107 * not yet having acknowledged the buffer in the hold position yet.
109 #define ZBUF_FLAG_ASSIGNED 0x00000001 /* Set when owned by user. */
112 * Release a page we've previously wired.
115 zbuf_page_free(vm_page_t pp)
119 if (vm_page_unwire(pp, PQ_INACTIVE) && pp->object == NULL)
125 * Free an sf_buf with attached page.
128 zbuf_sfbuf_free(struct sf_buf *sf)
132 pp = sf_buf_page(sf);
138 * Free a zbuf, including its page array, sbufs, and pages. Allow partially
139 * allocated zbufs to be freed so that it may be used even during a zbuf
143 zbuf_free(struct zbuf *zb)
147 for (i = 0; i < zb->zb_numpages; i++) {
148 if (zb->zb_pages[i] != NULL)
149 zbuf_sfbuf_free(zb->zb_pages[i]);
151 free(zb->zb_pages, M_BPF);
156 * Given a user pointer to a page of user memory, return an sf_buf for the
157 * page. Because we may be requesting quite a few sf_bufs, prefer failure to
158 * deadlock and use SFB_NOWAIT.
160 static struct sf_buf *
161 zbuf_sfbuf_get(struct vm_map *map, vm_offset_t uaddr)
166 if (vm_fault_quick_hold_pages(map, uaddr, PAGE_SIZE, VM_PROT_READ |
167 VM_PROT_WRITE, &pp, 1) < 0)
173 sf = sf_buf_alloc(pp, SFB_NOWAIT);
182 * Create a zbuf describing a range of user address space memory. Validate
183 * page alignment, size requirements, etc.
186 zbuf_setup(struct thread *td, vm_offset_t uaddr, size_t len,
196 * User address must be page-aligned.
198 if (uaddr & PAGE_MASK)
202 * Length must be an integer number of full pages.
208 * Length must not exceed per-buffer resource limit.
210 if ((len / PAGE_SIZE) > BPF_MAX_PAGES)
214 * Allocate the buffer and set up each page with is own sf_buf.
217 zb = malloc(sizeof(*zb), M_BPF, M_ZERO | M_WAITOK);
218 zb->zb_uaddr = uaddr;
220 zb->zb_numpages = len / PAGE_SIZE;
221 zb->zb_pages = malloc(sizeof(struct sf_buf *) *
222 zb->zb_numpages, M_BPF, M_ZERO | M_WAITOK);
223 map = &td->td_proc->p_vmspace->vm_map;
224 for (i = 0; i < zb->zb_numpages; i++) {
225 zb->zb_pages[i] = zbuf_sfbuf_get(map,
226 uaddr + (i * PAGE_SIZE));
227 if (zb->zb_pages[i] == NULL) {
233 (struct bpf_zbuf_header *)sf_buf_kva(zb->zb_pages[0]);
234 bzero(zb->zb_header, sizeof(*zb->zb_header));
244 * Copy bytes from a source into the specified zbuf. The caller is
245 * responsible for performing bounds checking, etc.
248 bpf_zerocopy_append_bytes(struct bpf_d *d, caddr_t buf, u_int offset,
249 void *src, u_int len)
251 u_int count, page, poffset;
255 KASSERT(d->bd_bufmode == BPF_BUFMODE_ZBUF,
256 ("bpf_zerocopy_append_bytes: not in zbuf mode"));
257 KASSERT(buf != NULL, ("bpf_zerocopy_append_bytes: NULL buf"));
259 src_bytes = (u_char *)src;
260 zb = (struct zbuf *)buf;
262 KASSERT((zb->zb_flags & ZBUF_FLAG_ASSIGNED) == 0,
263 ("bpf_zerocopy_append_bytes: ZBUF_FLAG_ASSIGNED"));
266 * Scatter-gather copy to user pages mapped into kernel address space
267 * using sf_bufs: copy up to a page at a time.
269 offset += sizeof(struct bpf_zbuf_header);
270 page = offset / PAGE_SIZE;
271 poffset = offset % PAGE_SIZE;
273 KASSERT(page < zb->zb_numpages, ("bpf_zerocopy_append_bytes:"
274 " page overflow (%d p %d np)\n", page, zb->zb_numpages));
276 count = min(len, PAGE_SIZE - poffset);
277 bcopy(src_bytes, ((u_char *)sf_buf_kva(zb->zb_pages[page])) +
280 if (poffset == PAGE_SIZE) {
284 KASSERT(poffset < PAGE_SIZE,
285 ("bpf_zerocopy_append_bytes: page offset overflow (%d)",
293 * Copy bytes from an mbuf chain to the specified zbuf: copying will be
294 * scatter-gather both from mbufs, which may be fragmented over memory, and
295 * to pages, which may not be contiguously mapped in kernel address space.
296 * As with bpf_zerocopy_append_bytes(), the caller is responsible for
297 * checking that this will not exceed the buffer limit.
300 bpf_zerocopy_append_mbuf(struct bpf_d *d, caddr_t buf, u_int offset,
301 void *src, u_int len)
303 u_int count, moffset, page, poffset;
304 const struct mbuf *m;
307 KASSERT(d->bd_bufmode == BPF_BUFMODE_ZBUF,
308 ("bpf_zerocopy_append_mbuf not in zbuf mode"));
309 KASSERT(buf != NULL, ("bpf_zerocopy_append_mbuf: NULL buf"));
311 m = (struct mbuf *)src;
312 zb = (struct zbuf *)buf;
314 KASSERT((zb->zb_flags & ZBUF_FLAG_ASSIGNED) == 0,
315 ("bpf_zerocopy_append_mbuf: ZBUF_FLAG_ASSIGNED"));
318 * Scatter gather both from an mbuf chain and to a user page set
319 * mapped into kernel address space using sf_bufs. If we're lucky,
320 * each mbuf requires one copy operation, but if page alignment and
321 * mbuf alignment work out less well, we'll be doing two copies per
324 offset += sizeof(struct bpf_zbuf_header);
325 page = offset / PAGE_SIZE;
326 poffset = offset % PAGE_SIZE;
329 KASSERT(page < zb->zb_numpages,
330 ("bpf_zerocopy_append_mbuf: page overflow (%d p %d "
331 "np)\n", page, zb->zb_numpages));
333 ("bpf_zerocopy_append_mbuf: end of mbuf chain"));
335 count = min(m->m_len - moffset, len);
336 count = min(count, PAGE_SIZE - poffset);
337 bcopy(mtod(m, u_char *) + moffset,
338 ((u_char *)sf_buf_kva(zb->zb_pages[page])) + poffset,
341 if (poffset == PAGE_SIZE) {
345 KASSERT(poffset < PAGE_SIZE,
346 ("bpf_zerocopy_append_mbuf: page offset overflow (%d)",
349 if (moffset == m->m_len) {
358 * Notification from the BPF framework that a buffer in the store position is
359 * rejecting packets and may be considered full. We mark the buffer as
360 * immutable and assign to userspace so that it is immediately available for
361 * the user process to access.
364 bpf_zerocopy_buffull(struct bpf_d *d)
368 KASSERT(d->bd_bufmode == BPF_BUFMODE_ZBUF,
369 ("bpf_zerocopy_buffull: not in zbuf mode"));
371 zb = (struct zbuf *)d->bd_sbuf;
372 KASSERT(zb != NULL, ("bpf_zerocopy_buffull: zb == NULL"));
374 if ((zb->zb_flags & ZBUF_FLAG_ASSIGNED) == 0) {
375 zb->zb_flags |= ZBUF_FLAG_ASSIGNED;
376 zb->zb_header->bzh_kernel_len = d->bd_slen;
377 atomic_add_rel_int(&zb->zb_header->bzh_kernel_gen, 1);
382 * Notification from the BPF framework that a buffer has moved into the held
383 * slot on a descriptor. Zero-copy BPF will update the shared page to let
384 * the user process know and flag the buffer as assigned if it hasn't already
385 * been marked assigned due to filling while it was in the store position.
387 * Note: identical logic as in bpf_zerocopy_buffull(), except that we operate
388 * on bd_hbuf and bd_hlen.
391 bpf_zerocopy_bufheld(struct bpf_d *d)
395 KASSERT(d->bd_bufmode == BPF_BUFMODE_ZBUF,
396 ("bpf_zerocopy_bufheld: not in zbuf mode"));
398 zb = (struct zbuf *)d->bd_hbuf;
399 KASSERT(zb != NULL, ("bpf_zerocopy_bufheld: zb == NULL"));
401 if ((zb->zb_flags & ZBUF_FLAG_ASSIGNED) == 0) {
402 zb->zb_flags |= ZBUF_FLAG_ASSIGNED;
403 zb->zb_header->bzh_kernel_len = d->bd_hlen;
404 atomic_add_rel_int(&zb->zb_header->bzh_kernel_gen, 1);
409 * Notification from the BPF framework that the free buffer has been been
410 * rotated out of the held position to the free position. This happens when
411 * the user acknowledges the held buffer.
414 bpf_zerocopy_buf_reclaimed(struct bpf_d *d)
418 KASSERT(d->bd_bufmode == BPF_BUFMODE_ZBUF,
419 ("bpf_zerocopy_reclaim_buf: not in zbuf mode"));
421 KASSERT(d->bd_fbuf != NULL,
422 ("bpf_zerocopy_buf_reclaimed: NULL free buf"));
423 zb = (struct zbuf *)d->bd_fbuf;
424 zb->zb_flags &= ~ZBUF_FLAG_ASSIGNED;
428 * Query from the BPF framework regarding whether the buffer currently in the
429 * held position can be moved to the free position, which can be indicated by
430 * the user process making their generation number equal to the kernel
434 bpf_zerocopy_canfreebuf(struct bpf_d *d)
438 KASSERT(d->bd_bufmode == BPF_BUFMODE_ZBUF,
439 ("bpf_zerocopy_canfreebuf: not in zbuf mode"));
441 zb = (struct zbuf *)d->bd_hbuf;
444 if (zb->zb_header->bzh_kernel_gen ==
445 atomic_load_acq_int(&zb->zb_header->bzh_user_gen))
451 * Query from the BPF framework as to whether or not the buffer current in
452 * the store position can actually be written to. This may return false if
453 * the store buffer is assigned to userspace before the hold buffer is
457 bpf_zerocopy_canwritebuf(struct bpf_d *d)
461 KASSERT(d->bd_bufmode == BPF_BUFMODE_ZBUF,
462 ("bpf_zerocopy_canwritebuf: not in zbuf mode"));
464 zb = (struct zbuf *)d->bd_sbuf;
465 KASSERT(zb != NULL, ("bpf_zerocopy_canwritebuf: bd_sbuf NULL"));
467 if (zb->zb_flags & ZBUF_FLAG_ASSIGNED)
473 * Free zero copy buffers at request of descriptor.
476 bpf_zerocopy_free(struct bpf_d *d)
480 KASSERT(d->bd_bufmode == BPF_BUFMODE_ZBUF,
481 ("bpf_zerocopy_free: not in zbuf mode"));
483 zb = (struct zbuf *)d->bd_sbuf;
486 zb = (struct zbuf *)d->bd_hbuf;
489 zb = (struct zbuf *)d->bd_fbuf;
495 * Ioctl to return the maximum buffer size.
498 bpf_zerocopy_ioctl_getzmax(struct thread *td, struct bpf_d *d, size_t *i)
501 KASSERT(d->bd_bufmode == BPF_BUFMODE_ZBUF,
502 ("bpf_zerocopy_ioctl_getzmax: not in zbuf mode"));
504 *i = BPF_MAX_PAGES * PAGE_SIZE;
509 * Ioctl to force rotation of the two buffers, if there's any data available.
510 * This can be used by user space to implement timeouts when waiting for a
514 bpf_zerocopy_ioctl_rotzbuf(struct thread *td, struct bpf_d *d,
519 bzero(bz, sizeof(*bz));
521 if (d->bd_hbuf == NULL && d->bd_slen != 0) {
523 bzh = (struct zbuf *)d->bd_hbuf;
524 bz->bz_bufa = (void *)bzh->zb_uaddr;
525 bz->bz_buflen = d->bd_hlen;
532 * Ioctl to configure zero-copy buffers -- may be done only once.
535 bpf_zerocopy_ioctl_setzbuf(struct thread *td, struct bpf_d *d,
538 struct zbuf *zba, *zbb;
541 KASSERT(d->bd_bufmode == BPF_BUFMODE_ZBUF,
542 ("bpf_zerocopy_ioctl_setzbuf: not in zbuf mode"));
545 * Must set both buffers. Cannot clear them.
547 if (bz->bz_bufa == NULL || bz->bz_bufb == NULL)
551 * Buffers must have a size greater than 0. Alignment and other size
552 * validity checking is done in zbuf_setup().
554 if (bz->bz_buflen == 0)
558 * Allocate new buffers.
560 error = zbuf_setup(td, (vm_offset_t)bz->bz_bufa, bz->bz_buflen,
564 error = zbuf_setup(td, (vm_offset_t)bz->bz_bufb, bz->bz_buflen,
572 * We only allow buffers to be installed once, so atomically check
573 * that no buffers are currently installed and install new buffers.
576 if (d->bd_hbuf != NULL || d->bd_sbuf != NULL || d->bd_fbuf != NULL ||
585 * Point BPF descriptor at buffers; initialize sbuf as zba so that
586 * it is always filled first in the sequence, per bpf(4).
588 d->bd_fbuf = (caddr_t)zbb;
589 d->bd_sbuf = (caddr_t)zba;
594 * We expose only the space left in the buffer after the size of the
595 * shared management region.
597 d->bd_bufsize = bz->bz_buflen - sizeof(struct bpf_zbuf_header);