Home | History | Annotate | Line # | Download | only in npf
npf_mbuf.c revision 1.3
      1  1.3  rmind /*	$NetBSD: npf_mbuf.c,v 1.3 2010/09/25 00:25:31 rmind Exp $	*/
      2  1.1  rmind 
      3  1.1  rmind /*-
      4  1.1  rmind  * Copyright (c) 2009-2010 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.1  rmind #ifdef _KERNEL
     40  1.1  rmind #include <sys/cdefs.h>
     41  1.3  rmind __KERNEL_RCSID(0, "$NetBSD: npf_mbuf.c,v 1.3 2010/09/25 00:25:31 rmind Exp $");
     42  1.1  rmind #endif
     43  1.1  rmind 
     44  1.1  rmind #include <sys/param.h>
     45  1.1  rmind #include <sys/mbuf.h>
     46  1.1  rmind 
     47  1.1  rmind #include "npf_impl.h"
     48  1.1  rmind 
     49  1.1  rmind /*
     50  1.1  rmind  * nbuf_dataptr: return a pointer to data in nbuf.
     51  1.1  rmind  */
     52  1.1  rmind void *
     53  1.1  rmind nbuf_dataptr(nbuf_t *nbuf)
     54  1.1  rmind {
     55  1.1  rmind 	const struct mbuf *m = nbuf;
     56  1.1  rmind 
     57  1.1  rmind 	return mtod(m, void *);
     58  1.1  rmind }
     59  1.1  rmind 
     60  1.1  rmind /*
     61  1.1  rmind  * nbuf_advance: advance in mbuf or chain by specified amount of bytes.
     62  1.1  rmind  *
     63  1.1  rmind  * => Returns new pointer to data in mbuf and NULL if offset gets invalid.
     64  1.1  rmind  * => Sets nbuf to current (after advance) mbuf in the chain.
     65  1.1  rmind  */
     66  1.1  rmind void *
     67  1.1  rmind nbuf_advance(nbuf_t **nbuf, void *n_ptr, u_int n)
     68  1.1  rmind {
     69  1.1  rmind 	struct mbuf *m = *nbuf;
     70  1.1  rmind 	u_int off, wmark;
     71  1.1  rmind 	uint8_t *d;
     72  1.1  rmind 
     73  1.1  rmind 	/* Offset with amount to advance. */
     74  1.1  rmind 	off = (uintptr_t)n_ptr - mtod(m, uintptr_t) + n;
     75  1.1  rmind 	wmark = m->m_len;
     76  1.1  rmind 
     77  1.1  rmind 	/* Find the mbuf according to offset. */
     78  1.1  rmind 	while (__predict_false(wmark <= off)) {
     79  1.1  rmind 		m = m->m_next;
     80  1.1  rmind 		if (__predict_false(m == NULL)) {
     81  1.1  rmind 			/*
     82  1.1  rmind 			 * If out of chain, then offset is
     83  1.1  rmind 			 * higher than packet length.
     84  1.1  rmind 			 */
     85  1.1  rmind 			return NULL;
     86  1.1  rmind 		}
     87  1.1  rmind 		wmark += m->m_len;
     88  1.1  rmind 	}
     89  1.1  rmind 
     90  1.1  rmind 	/* Offset in mbuf data. */
     91  1.1  rmind 	d = mtod(m, uint8_t *);
     92  1.1  rmind 	KASSERT(off >= (wmark - m->m_len));
     93  1.1  rmind 	d += (off - (wmark - m->m_len));
     94  1.1  rmind 
     95  1.1  rmind 	*nbuf = (void *)m;
     96  1.1  rmind 	return d;
     97  1.1  rmind }
     98  1.1  rmind 
     99  1.1  rmind /*
    100  1.1  rmind  * nbuf_rw_datum: read or write a datum of specified length at current
    101  1.1  rmind  * offset in the nbuf chain and copy datum into passed buffer.
    102  1.1  rmind  *
    103  1.1  rmind  * => Datum is allowed to overlap between two or more mbufs.
    104  1.1  rmind  * => Note: all data in nbuf is in network byte order.
    105  1.1  rmind  * => Returns 0 on success, error code on failure.
    106  1.1  rmind  *
    107  1.1  rmind  * Note: this function must be static inline with constant operation
    108  1.1  rmind  * parameter - we expect constant propagation.
    109  1.1  rmind  */
    110  1.1  rmind 
    111  1.1  rmind #define	NBUF_DATA_READ		0
    112  1.1  rmind #define	NBUF_DATA_WRITE		1
    113  1.1  rmind 
    114  1.1  rmind static inline int
    115  1.1  rmind nbuf_rw_datum(const int wr, nbuf_t *nbuf, void *n_ptr, size_t len, void *buf)
    116  1.1  rmind {
    117  1.1  rmind 	uint8_t *d = n_ptr, *b = buf;
    118  1.1  rmind 	struct mbuf *m = nbuf;
    119  1.1  rmind 	u_int off, wmark, end;
    120  1.1  rmind 
    121  1.1  rmind 	/* Current offset in mbuf. */
    122  1.1  rmind 	off = (uintptr_t)n_ptr - mtod(m, uintptr_t);
    123  1.1  rmind 	KASSERT(off < m->m_len);
    124  1.1  rmind 	wmark = m->m_len;
    125  1.1  rmind 
    126  1.1  rmind 	/* Is datum overlapping? */
    127  1.1  rmind 	end = off + len;
    128  1.1  rmind 	while (__predict_false(end > wmark)) {
    129  1.1  rmind 		u_int l;
    130  1.1  rmind 
    131  1.1  rmind 		/* Get the part of current mbuf. */
    132  1.1  rmind 		l = m->m_len - off;
    133  1.1  rmind 		KASSERT(l < len);
    134  1.1  rmind 		len -= l;
    135  1.2  rmind 		if (wr == NBUF_DATA_WRITE) {
    136  1.1  rmind 			while (l--)
    137  1.1  rmind 				*d++ = *b++;
    138  1.1  rmind 		} else {
    139  1.2  rmind 			KASSERT(wr == NBUF_DATA_READ);
    140  1.1  rmind 			while (l--)
    141  1.1  rmind 				*b++ = *d++;
    142  1.1  rmind 		}
    143  1.1  rmind 		KASSERT(len > 0);
    144  1.1  rmind 
    145  1.1  rmind 		/* Take next mbuf and continue. */
    146  1.1  rmind 		m = m->m_next;
    147  1.1  rmind 		if (__predict_false(m == NULL)) {
    148  1.1  rmind 			/*
    149  1.1  rmind 			 * If out of chain, then offset with datum
    150  1.1  rmind 			 * length exceed the packet length.
    151  1.1  rmind 			 */
    152  1.1  rmind 			return EINVAL;
    153  1.1  rmind 		}
    154  1.1  rmind 		wmark += m->m_len;
    155  1.1  rmind 		d = mtod(m, uint8_t *);
    156  1.1  rmind 		off = 0;
    157  1.1  rmind 	}
    158  1.1  rmind 	KASSERT(n_ptr == d || mtod(m, uint8_t *) == d);
    159  1.1  rmind 	KASSERT(len <= m->m_len);
    160  1.1  rmind 
    161  1.1  rmind 	/* Non-overlapping case: fetch the actual data. */
    162  1.2  rmind 	if (wr == NBUF_DATA_WRITE) {
    163  1.1  rmind 		while (len--)
    164  1.1  rmind 			*d++ = *b++;
    165  1.1  rmind 	} else {
    166  1.2  rmind 		KASSERT(wr == NBUF_DATA_READ);
    167  1.1  rmind 		while (len--)
    168  1.1  rmind 			*b++ = *d++;
    169  1.1  rmind 	}
    170  1.1  rmind 	return 0;
    171  1.1  rmind }
    172  1.1  rmind 
    173  1.1  rmind /*
    174  1.1  rmind  * nbuf_{fetch|store}_datum: read/write absraction calls on nbuf_rw_datum().
    175  1.1  rmind  */
    176  1.1  rmind int
    177  1.1  rmind nbuf_fetch_datum(nbuf_t *nbuf, void *n_ptr, size_t len, void *buf)
    178  1.1  rmind {
    179  1.1  rmind 
    180  1.1  rmind 	return nbuf_rw_datum(NBUF_DATA_READ, nbuf, n_ptr, len, buf);
    181  1.1  rmind }
    182  1.1  rmind 
    183  1.1  rmind int
    184  1.1  rmind nbuf_store_datum(nbuf_t *nbuf, void *n_ptr, size_t len, void *buf)
    185  1.1  rmind {
    186  1.1  rmind 
    187  1.1  rmind 	return nbuf_rw_datum(NBUF_DATA_WRITE, nbuf, n_ptr, len, buf);
    188  1.1  rmind }
    189  1.1  rmind 
    190  1.1  rmind /*
    191  1.3  rmind  * nbuf_advfetch: advance and fetch the datum.
    192  1.3  rmind  * WARNING: Values of nbuf and n_ptr are undefined on error.
    193  1.3  rmind  */
    194  1.3  rmind int
    195  1.3  rmind nbuf_advfetch(nbuf_t **nbuf, void **n_ptr, u_int n, size_t len, void *buf)
    196  1.3  rmind {
    197  1.3  rmind 
    198  1.3  rmind 	*n_ptr = nbuf_advance(nbuf, *n_ptr, n);
    199  1.3  rmind 	if (__predict_false(*n_ptr == NULL)) {
    200  1.3  rmind 		return EINVAL;
    201  1.3  rmind 	}
    202  1.3  rmind 	return nbuf_fetch_datum(*nbuf, n_ptr, len, buf);
    203  1.3  rmind }
    204  1.3  rmind 
    205  1.3  rmind /*
    206  1.1  rmind  * nbuf_add_tag: add a tag to specified network buffer.
    207  1.1  rmind  *
    208  1.1  rmind  * => Returns 0 on success, or errno on failure.
    209  1.1  rmind  */
    210  1.1  rmind int
    211  1.1  rmind nbuf_add_tag(nbuf_t *nbuf, uint32_t key, uint32_t val)
    212  1.1  rmind {
    213  1.1  rmind 	struct mbuf *m = nbuf;
    214  1.1  rmind 	struct m_tag *mt;
    215  1.1  rmind 	uint32_t *dat;
    216  1.1  rmind 
    217  1.1  rmind 	mt = m_tag_get(PACKET_TAG_NPF, sizeof(uint32_t), M_NOWAIT);
    218  1.1  rmind 	if (__predict_false(mt == NULL)) {
    219  1.1  rmind 		return ENOMEM;
    220  1.1  rmind 	}
    221  1.1  rmind 	dat = (uint32_t *)(mt + 1);
    222  1.1  rmind 	*dat = val;
    223  1.1  rmind 	m_tag_prepend(m, mt);
    224  1.1  rmind 	return 0;
    225  1.1  rmind }
    226  1.1  rmind 
    227  1.1  rmind /*
    228  1.1  rmind  * nbuf_find_tag: find a tag in specified network buffer.
    229  1.1  rmind  *
    230  1.1  rmind  * => Returns 0 on success, or errno on failure.
    231  1.1  rmind  */
    232  1.1  rmind int
    233  1.1  rmind nbuf_find_tag(nbuf_t *nbuf, uint32_t key, void **data)
    234  1.1  rmind {
    235  1.1  rmind 	struct mbuf *m = nbuf;
    236  1.1  rmind 	struct m_tag *mt;
    237  1.1  rmind 
    238  1.1  rmind 	mt = m_tag_find(m, PACKET_TAG_NPF, NULL);
    239  1.1  rmind 	if (__predict_false(mt == NULL)) {
    240  1.1  rmind 		return EINVAL;
    241  1.1  rmind 	}
    242  1.1  rmind 	*data = (void *)(mt + 1);
    243  1.1  rmind 	return 0;
    244  1.1  rmind }
    245