2 * Copyright (C) 2011 Matteo Landi, Luigi Rizzo. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the
16 * 3. Neither the name of the authors nor the names of their contributors
17 * may be used to endorse or promote products derived from this
18 * software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY MATTEO LANDI AND CONTRIBUTORS "AS IS" AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTEO LANDI OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
35 * $Id: netmap_user.h 10597 2012-02-21 05:08:32Z luigi $
37 * This header contains the macros used to manipulate netmap structures
38 * and packets in userspace. See netmap(4) for more information.
40 * The address of the struct netmap_if, say nifp, is computed from the
41 * value returned from ioctl(.., NIOCREG, ...) and the mmap region:
42 * ioctl(fd, NIOCREG, &req);
43 * mem = mmap(0, ... );
44 * nifp = NETMAP_IF(mem, req.nr_nifp);
45 * (so simple, we could just do it manually)
48 * struct netmap_ring *NETMAP_TXRING(nifp, index)
49 * struct netmap_ring *NETMAP_RXRING(nifp, index)
50 * we can access ring->nr_cur, ring->nr_avail, ring->nr_flags
52 * ring->slot[i] gives us the i-th slot (we can access
53 * directly plen, flags, bufindex)
55 * char *buf = NETMAP_BUF(ring, index) returns a pointer to
58 * Since rings are circular, we have macros to compute the next index
59 * i = NETMAP_RING_NEXT(ring, i);
62 #ifndef _NET_NETMAP_USER_H_
63 #define _NET_NETMAP_USER_H_
65 #define _NETMAP_OFFSET(type, ptr, offset) \
66 ((type)(void *)((char *)(ptr) + (offset)))
68 #define NETMAP_IF(b, o) _NETMAP_OFFSET(struct netmap_if *, b, o)
70 #define NETMAP_TXRING(nifp, index) _NETMAP_OFFSET(struct netmap_ring *, \
71 nifp, (nifp)->ring_ofs[index] )
73 #define NETMAP_RXRING(nifp, index) _NETMAP_OFFSET(struct netmap_ring *, \
74 nifp, (nifp)->ring_ofs[index + (nifp)->ni_tx_rings + 1] )
76 #define NETMAP_BUF(ring, index) \
77 ((char *)(ring) + (ring)->buf_ofs + ((index)*(ring)->nr_buf_size))
79 #define NETMAP_BUF_IDX(ring, buf) \
80 ( ((char *)(buf) - ((char *)(ring) + (ring)->buf_ofs) ) / \
83 #define NETMAP_RING_NEXT(r, i) \
84 ((i)+1 == (r)->num_slots ? 0 : (i) + 1 )
86 #define NETMAP_RING_FIRST_RESERVED(r) \
87 ( (r)->cur < (r)->reserved ? \
88 (r)->cur + (r)->num_slots - (r)->reserved : \
89 (r)->cur - (r)->reserved )
92 * Return 1 if the given tx ring is empty.
94 #define NETMAP_TX_RING_EMPTY(r) ((r)->avail >= (r)->num_slots - 1)
96 #endif /* _NET_NETMAP_USER_H_ */