Home | History | Annotate | Line # | Download | only in npf
npf_mbuf.c revision 1.13.4.4
      1  1.13.4.4  skrll /*	$NetBSD: npf_mbuf.c,v 1.13.4.4 2017/02/05 13:40:58 skrll Exp $	*/
      2       1.1  rmind 
      3       1.1  rmind /*-
      4       1.9  rmind  * Copyright (c) 2009-2012 The NetBSD Foundation, Inc.
      5       1.1  rmind  * All rights reserved.
      6       1.1  rmind  *
      7       1.1  rmind  * This material is based upon work partially supported by The
      8       1.1  rmind  * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
      9       1.1  rmind  *
     10       1.1  rmind  * Redistribution and use in source and binary forms, with or without
     11       1.1  rmind  * modification, are permitted provided that the following conditions
     12       1.1  rmind  * are met:
     13       1.1  rmind  * 1. Redistributions of source code must retain the above copyright
     14       1.1  rmind  *    notice, this list of conditions and the following disclaimer.
     15       1.1  rmind  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1  rmind  *    notice, this list of conditions and the following disclaimer in the
     17       1.1  rmind  *    documentation and/or other materials provided with the distribution.
     18       1.1  rmind  *
     19       1.1  rmind  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1  rmind  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1  rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1  rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1  rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1  rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1  rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1  rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1  rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1  rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1  rmind  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1  rmind  */
     31       1.1  rmind 
     32       1.1  rmind /*
     33       1.1  rmind  * NPF network buffer management interface.
     34       1.1  rmind  *
     35       1.1  rmind  * Network buffer in NetBSD is mbuf.  Internal mbuf structures are
     36       1.1  rmind  * abstracted within this source.
     37       1.1  rmind  */
     38       1.1  rmind 
     39  1.13.4.4  skrll #ifdef _KERNEL
     40       1.1  rmind #include <sys/cdefs.h>
     41  1.13.4.4  skrll __KERNEL_RCSID(0, "$NetBSD: npf_mbuf.c,v 1.13.4.4 2017/02/05 13:40:58 skrll Exp $");
     42       1.1  rmind 
     43       1.1  rmind #include <sys/param.h>
     44       1.1  rmind #include <sys/mbuf.h>
     45  1.13.4.4  skrll #endif
     46       1.1  rmind 
     47       1.1  rmind #include "npf_impl.h"
     48       1.1  rmind 
     49  1.13.4.4  skrll #if defined(_NPF_STANDALONE)
     50  1.13.4.4  skrll #define	m_length(m)		(nbuf)->nb_mops->getchainlen(m)
     51  1.13.4.4  skrll #define	m_buflen(m)		(nbuf)->nb_mops->getlen(m)
     52  1.13.4.4  skrll #define	m_next_ptr(m)		(nbuf)->nb_mops->getnext(m)
     53  1.13.4.4  skrll #define	m_ensure_contig(m,t)	(nbuf)->nb_mops->ensure_contig((m), (t))
     54  1.13.4.4  skrll #define	m_makewritable(m,o,l,f)	(nbuf)->nb_mops->ensure_writable((m), (o+l))
     55  1.13.4.4  skrll #define	mtod(m,t)		((t)((nbuf)->nb_mops->getdata(m)))
     56  1.13.4.4  skrll #define	m_flags_p(m,f)		true
     57  1.13.4.4  skrll #else
     58  1.13.4.4  skrll #define	m_next_ptr(m)		(m)->m_next
     59  1.13.4.4  skrll #define	m_buflen(m)		(m)->m_len
     60  1.13.4.4  skrll #define	m_flags_p(m,f)		(((m)->m_flags & (f)) != 0)
     61  1.13.4.4  skrll #endif
     62  1.13.4.4  skrll 
     63       1.9  rmind #define	NBUF_ENSURE_ALIGN	(MAX(COHERENCY_UNIT, 64))
     64       1.9  rmind #define	NBUF_ENSURE_MASK	(NBUF_ENSURE_ALIGN - 1)
     65       1.9  rmind #define	NBUF_ENSURE_ROUNDUP(x)	(((x) + NBUF_ENSURE_ALIGN) & ~NBUF_ENSURE_MASK)
     66       1.9  rmind 
     67       1.9  rmind void
     68  1.13.4.4  skrll nbuf_init(npf_t *npf, nbuf_t *nbuf, struct mbuf *m, const ifnet_t *ifp)
     69       1.9  rmind {
     70  1.13.4.4  skrll 	u_int ifid = npf_ifmap_getid(npf, ifp);
     71      1.12  rmind 
     72  1.13.4.4  skrll 	KASSERT(m_flags_p(m, M_PKTHDR));
     73  1.13.4.4  skrll 	nbuf->nb_mops = npf->mbufops;
     74       1.9  rmind 
     75       1.9  rmind 	nbuf->nb_mbuf0 = m;
     76       1.9  rmind 	nbuf->nb_ifp = ifp;
     77  1.13.4.1  skrll 	nbuf->nb_ifid = ifid;
     78       1.9  rmind 	nbuf_reset(nbuf);
     79       1.9  rmind }
     80       1.9  rmind 
     81       1.9  rmind void
     82       1.9  rmind nbuf_reset(nbuf_t *nbuf)
     83       1.9  rmind {
     84       1.9  rmind 	struct mbuf *m = nbuf->nb_mbuf0;
     85       1.9  rmind 
     86       1.9  rmind 	nbuf->nb_mbuf = m;
     87       1.9  rmind 	nbuf->nb_nptr = mtod(m, void *);
     88       1.9  rmind }
     89       1.9  rmind 
     90       1.1  rmind void *
     91       1.1  rmind nbuf_dataptr(nbuf_t *nbuf)
     92       1.1  rmind {
     93       1.9  rmind 	KASSERT(nbuf->nb_nptr);
     94       1.9  rmind 	return nbuf->nb_nptr;
     95       1.9  rmind }
     96       1.9  rmind 
     97       1.9  rmind size_t
     98       1.9  rmind nbuf_offset(const nbuf_t *nbuf)
     99       1.9  rmind {
    100       1.9  rmind 	const struct mbuf *m = nbuf->nb_mbuf;
    101       1.9  rmind 	const u_int off = (uintptr_t)nbuf->nb_nptr - mtod(m, uintptr_t);
    102       1.9  rmind 	const int poff = m_length(nbuf->nb_mbuf0) - m_length(m) + off;
    103       1.9  rmind 
    104       1.9  rmind 	return poff;
    105       1.9  rmind }
    106       1.9  rmind 
    107       1.9  rmind struct mbuf *
    108       1.9  rmind nbuf_head_mbuf(nbuf_t *nbuf)
    109       1.9  rmind {
    110       1.9  rmind 	return nbuf->nb_mbuf0;
    111       1.9  rmind }
    112       1.1  rmind 
    113       1.9  rmind bool
    114       1.9  rmind nbuf_flag_p(const nbuf_t *nbuf, int flag)
    115       1.9  rmind {
    116       1.9  rmind 	return (nbuf->nb_flags & flag) != 0;
    117       1.9  rmind }
    118       1.9  rmind 
    119       1.9  rmind void
    120       1.9  rmind nbuf_unset_flag(nbuf_t *nbuf, int flag)
    121       1.9  rmind {
    122       1.9  rmind 	nbuf->nb_flags &= ~flag;
    123       1.1  rmind }
    124       1.1  rmind 
    125       1.1  rmind /*
    126       1.9  rmind  * nbuf_advance: advance in nbuf or chain by specified amount of bytes and,
    127       1.9  rmind  * if requested, ensure that the area *after* advance is contiguous.
    128       1.1  rmind  *
    129       1.9  rmind  * => Returns new pointer to data in nbuf or NULL if offset is invalid.
    130       1.9  rmind  * => Current nbuf and the offset is stored in the nbuf metadata.
    131       1.1  rmind  */
    132       1.1  rmind void *
    133       1.9  rmind nbuf_advance(nbuf_t *nbuf, size_t len, size_t ensure)
    134       1.1  rmind {
    135       1.9  rmind 	struct mbuf *m = nbuf->nb_mbuf;
    136       1.1  rmind 	u_int off, wmark;
    137       1.1  rmind 	uint8_t *d;
    138       1.1  rmind 
    139       1.1  rmind 	/* Offset with amount to advance. */
    140       1.9  rmind 	off = (uintptr_t)nbuf->nb_nptr - mtod(m, uintptr_t) + len;
    141  1.13.4.4  skrll 	wmark = m_buflen(m);
    142       1.1  rmind 
    143       1.1  rmind 	/* Find the mbuf according to offset. */
    144       1.1  rmind 	while (__predict_false(wmark <= off)) {
    145  1.13.4.4  skrll 		m = m_next_ptr(m);
    146       1.1  rmind 		if (__predict_false(m == NULL)) {
    147       1.1  rmind 			/*
    148       1.9  rmind 			 * If end of the chain, then the offset is
    149       1.1  rmind 			 * higher than packet length.
    150       1.1  rmind 			 */
    151       1.1  rmind 			return NULL;
    152       1.1  rmind 		}
    153  1.13.4.4  skrll 		wmark += m_buflen(m);
    154       1.1  rmind 	}
    155       1.9  rmind 	KASSERT(off < m_length(nbuf->nb_mbuf0));
    156       1.1  rmind 
    157       1.1  rmind 	/* Offset in mbuf data. */
    158       1.1  rmind 	d = mtod(m, uint8_t *);
    159  1.13.4.4  skrll 	KASSERT(off >= (wmark - m_buflen(m)));
    160  1.13.4.4  skrll 	d += (off - (wmark - m_buflen(m)));
    161       1.1  rmind 
    162       1.9  rmind 	nbuf->nb_mbuf = m;
    163       1.9  rmind 	nbuf->nb_nptr = d;
    164       1.9  rmind 
    165       1.9  rmind 	if (ensure) {
    166       1.9  rmind 		/* Ensure contiguousness (may change nbuf chain). */
    167       1.9  rmind 		d = nbuf_ensure_contig(nbuf, ensure);
    168       1.9  rmind 	}
    169       1.1  rmind 	return d;
    170       1.1  rmind }
    171       1.1  rmind 
    172       1.1  rmind /*
    173       1.9  rmind  * nbuf_ensure_contig: check whether the specified length from the current
    174       1.9  rmind  * point in the nbuf is contiguous.  If not, rearrange the chain to be so.
    175       1.1  rmind  *
    176       1.9  rmind  * => Returns pointer to the data at the current offset in the buffer.
    177       1.9  rmind  * => Returns NULL on failure and nbuf becomes invalid.
    178       1.1  rmind  */
    179       1.9  rmind void *
    180       1.9  rmind nbuf_ensure_contig(nbuf_t *nbuf, size_t len)
    181       1.1  rmind {
    182      1.10  rmind 	const struct mbuf * const n = nbuf->nb_mbuf;
    183      1.10  rmind 	const size_t off = (uintptr_t)nbuf->nb_nptr - mtod(n, uintptr_t);
    184       1.9  rmind 
    185  1.13.4.4  skrll 	KASSERT(off <= m_buflen(n));
    186       1.9  rmind 
    187  1.13.4.4  skrll 	if (__predict_false(m_buflen(n) < (off + len))) {
    188      1.10  rmind 		struct mbuf *m = nbuf->nb_mbuf0;
    189      1.10  rmind 		const size_t foff = nbuf_offset(nbuf);
    190      1.10  rmind 		const size_t plen = m_length(m);
    191  1.13.4.4  skrll 		const size_t mlen = m_buflen(m);
    192      1.10  rmind 		size_t target;
    193      1.10  rmind 		bool success;
    194       1.9  rmind 
    195  1.13.4.4  skrll 		//npf_stats_inc(npf, NPF_STAT_NBUF_NONCONTIG);
    196       1.9  rmind 
    197       1.9  rmind 		/* Attempt to round-up to NBUF_ENSURE_ALIGN bytes. */
    198      1.10  rmind 		if ((target = NBUF_ENSURE_ROUNDUP(foff + len)) > plen) {
    199      1.10  rmind 			target = foff + len;
    200       1.9  rmind 		}
    201       1.1  rmind 
    202       1.9  rmind 		/* Rearrange the chain to be contiguous. */
    203  1.13.4.4  skrll 		KASSERT(m_flags_p(m, M_PKTHDR));
    204      1.10  rmind 		success = m_ensure_contig(&m, target);
    205      1.10  rmind 		KASSERT(m != NULL);
    206      1.10  rmind 
    207      1.10  rmind 		/* If no change in the chain: return what we have. */
    208  1.13.4.4  skrll 		if (m == nbuf->nb_mbuf0 && m_buflen(m) == mlen) {
    209      1.10  rmind 			return success ? nbuf->nb_nptr : NULL;
    210       1.9  rmind 		}
    211       1.1  rmind 
    212       1.9  rmind 		/*
    213      1.10  rmind 		 * The mbuf chain was re-arranged.  Update the pointers
    214      1.10  rmind 		 * accordingly and indicate that the references to the data
    215      1.10  rmind 		 * might need a reset.
    216       1.9  rmind 		 */
    217  1.13.4.4  skrll 		KASSERT(m_flags_p(m, M_PKTHDR));
    218      1.10  rmind 		nbuf->nb_mbuf0 = m;
    219      1.10  rmind 		nbuf->nb_mbuf = m;
    220      1.10  rmind 
    221  1.13.4.4  skrll 		KASSERT(foff < m_buflen(m) && foff < m_length(m));
    222      1.10  rmind 		nbuf->nb_nptr = mtod(m, uint8_t *) + foff;
    223      1.10  rmind 		nbuf->nb_flags |= NBUF_DATAREF_RESET;
    224      1.10  rmind 
    225      1.10  rmind 		if (!success) {
    226  1.13.4.4  skrll 			//npf_stats_inc(npf, NPF_STAT_NBUF_CONTIG_FAIL);
    227      1.10  rmind 			return NULL;
    228       1.1  rmind 		}
    229       1.1  rmind 	}
    230       1.9  rmind 	return nbuf->nb_nptr;
    231       1.1  rmind }
    232       1.1  rmind 
    233       1.9  rmind void *
    234       1.9  rmind nbuf_ensure_writable(nbuf_t *nbuf, size_t len)
    235       1.1  rmind {
    236       1.9  rmind 	struct mbuf *m = nbuf->nb_mbuf;
    237       1.9  rmind 	const u_int off = (uintptr_t)nbuf->nb_nptr - mtod(m, uintptr_t);
    238       1.9  rmind 	const int tlen = off + len;
    239       1.9  rmind 	bool head_buf;
    240       1.1  rmind 
    241       1.9  rmind 	KASSERT(off < m_length(nbuf->nb_mbuf0));
    242       1.1  rmind 
    243       1.9  rmind 	if (!M_UNWRITABLE(m, tlen)) {
    244       1.9  rmind 		return nbuf->nb_nptr;
    245       1.9  rmind 	}
    246       1.9  rmind 	head_buf = (nbuf->nb_mbuf0 == m);
    247       1.9  rmind 	if (m_makewritable(&m, 0, tlen, M_NOWAIT)) {
    248       1.9  rmind 		memset(nbuf, 0, sizeof(nbuf_t));
    249       1.9  rmind 		return NULL;
    250       1.9  rmind 	}
    251       1.9  rmind 	if (head_buf) {
    252  1.13.4.4  skrll 		KASSERT(m_flags_p(m, M_PKTHDR));
    253       1.9  rmind 		KASSERT(off < m_length(m));
    254       1.9  rmind 		nbuf->nb_mbuf0 = m;
    255       1.9  rmind 	}
    256       1.9  rmind 	nbuf->nb_mbuf = m;
    257       1.9  rmind 	nbuf->nb_nptr = mtod(m, uint8_t *) + off;
    258       1.1  rmind 
    259       1.9  rmind 	return nbuf->nb_nptr;
    260       1.1  rmind }
    261       1.1  rmind 
    262       1.9  rmind bool
    263       1.9  rmind nbuf_cksum_barrier(nbuf_t *nbuf, int di)
    264       1.3  rmind {
    265  1.13.4.4  skrll #ifdef _KERNEL
    266       1.9  rmind 	struct mbuf *m;
    267       1.3  rmind 
    268       1.9  rmind 	if (di != PFIL_OUT) {
    269       1.9  rmind 		return false;
    270       1.5  rmind 	}
    271       1.9  rmind 	m = nbuf->nb_mbuf0;
    272  1.13.4.4  skrll 	KASSERT(m_flags_p(m, M_PKTHDR));
    273       1.8  rmind 
    274       1.8  rmind 	if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4 | M_CSUM_UDPv4)) {
    275       1.8  rmind 		in_delayed_cksum(m);
    276       1.8  rmind 		m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4 | M_CSUM_UDPv4);
    277       1.9  rmind 		return true;
    278       1.8  rmind 	}
    279  1.13.4.3  skrll #ifdef INET6
    280  1.13.4.2  skrll 	if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv6 | M_CSUM_UDPv6)) {
    281  1.13.4.2  skrll 		in6_delayed_cksum(m);
    282  1.13.4.2  skrll 		m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv6 | M_CSUM_UDPv6);
    283  1.13.4.2  skrll 		return true;
    284  1.13.4.2  skrll 	}
    285  1.13.4.3  skrll #endif
    286  1.13.4.4  skrll #else
    287  1.13.4.4  skrll 	(void)nbuf; (void)di;
    288  1.13.4.4  skrll #endif
    289       1.9  rmind 	return false;
    290       1.8  rmind }
    291       1.8  rmind 
    292       1.5  rmind /*
    293       1.1  rmind  * nbuf_add_tag: add a tag to specified network buffer.
    294       1.1  rmind  *
    295       1.9  rmind  * => Returns 0 on success or errno on failure.
    296       1.1  rmind  */
    297       1.1  rmind int
    298  1.13.4.4  skrll nbuf_add_tag(nbuf_t *nbuf, uint32_t val)
    299       1.1  rmind {
    300  1.13.4.4  skrll #ifdef _KERNEL
    301       1.9  rmind 	struct mbuf *m = nbuf->nb_mbuf0;
    302       1.1  rmind 	struct m_tag *mt;
    303       1.1  rmind 	uint32_t *dat;
    304       1.1  rmind 
    305  1.13.4.4  skrll 	KASSERT(m_flags_p(m, M_PKTHDR));
    306       1.9  rmind 
    307       1.1  rmind 	mt = m_tag_get(PACKET_TAG_NPF, sizeof(uint32_t), M_NOWAIT);
    308       1.9  rmind 	if (mt == NULL) {
    309       1.1  rmind 		return ENOMEM;
    310       1.1  rmind 	}
    311       1.1  rmind 	dat = (uint32_t *)(mt + 1);
    312       1.1  rmind 	*dat = val;
    313       1.1  rmind 	m_tag_prepend(m, mt);
    314       1.1  rmind 	return 0;
    315  1.13.4.4  skrll #else
    316  1.13.4.4  skrll 	(void)nbuf; (void)val;
    317  1.13.4.4  skrll 	return ENOTSUP;
    318  1.13.4.4  skrll #endif
    319       1.1  rmind }
    320       1.1  rmind 
    321       1.1  rmind /*
    322       1.1  rmind  * nbuf_find_tag: find a tag in specified network buffer.
    323       1.1  rmind  *
    324       1.9  rmind  * => Returns 0 on success or errno on failure.
    325       1.1  rmind  */
    326       1.1  rmind int
    327  1.13.4.4  skrll nbuf_find_tag(nbuf_t *nbuf, uint32_t *val)
    328       1.1  rmind {
    329  1.13.4.4  skrll #ifdef _KERNEL
    330       1.9  rmind 	struct mbuf *m = nbuf->nb_mbuf0;
    331       1.1  rmind 	struct m_tag *mt;
    332       1.1  rmind 
    333  1.13.4.4  skrll 	KASSERT(m_flags_p(m, M_PKTHDR));
    334       1.9  rmind 
    335       1.1  rmind 	mt = m_tag_find(m, PACKET_TAG_NPF, NULL);
    336       1.9  rmind 	if (mt == NULL) {
    337       1.1  rmind 		return EINVAL;
    338       1.1  rmind 	}
    339  1.13.4.4  skrll 	*val = *(uint32_t *)(mt + 1);
    340       1.1  rmind 	return 0;
    341  1.13.4.4  skrll #else
    342  1.13.4.4  skrll 	(void)nbuf; (void)val;
    343  1.13.4.4  skrll 	return ENOTSUP;
    344  1.13.4.4  skrll #endif
    345       1.1  rmind }
    346