netmap_user.h revision 251138
1229970Sadrian/*
2229970Sadrian * Copyright (C) 2011 Matteo Landi, Luigi Rizzo. All rights reserved.
3229970Sadrian *
4229970Sadrian * Redistribution and use in source and binary forms, with or without
5229970Sadrian * modification, are permitted provided that the following conditions are
6229970Sadrian * met:
7229970Sadrian *
8229970Sadrian *   1. Redistributions of source code must retain the above copyright
9229970Sadrian *      notice, this list of conditions and the following disclaimer.
10229970Sadrian *
11229970Sadrian *   2. Redistributions in binary form must reproduce the above copyright
12229970Sadrian *      notice, this list of conditions and the following disclaimer in the
13229970Sadrian *      documentation and/or other materials provided with the
14229970Sadrian *      distribution.
15229970Sadrian *
16229970Sadrian *   3. Neither the name of the authors nor the names of their contributors
17229970Sadrian *      may be used to endorse or promote products derived from this
18229970Sadrian *      software without specific prior written permission.
19229970Sadrian *
20229970Sadrian * THIS SOFTWARE IS PROVIDED BY MATTEO LANDI AND CONTRIBUTORS "AS IS" AND
21229970Sadrian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22229970Sadrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23229970Sadrian * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTEO LANDI OR CONTRIBUTORS
24229970Sadrian * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25229970Sadrian * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26229970Sadrian * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27229970Sadrian * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28229970Sadrian * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29229970Sadrian * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30229970Sadrian * THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 * $FreeBSD: head/sys/net/netmap_user.h 251138 2013-05-30 13:41:19Z luigi $
35 *
36 * This header contains the macros used to manipulate netmap structures
37 * and packets in userspace. See netmap(4) for more information.
38 *
39 * The address of the struct netmap_if, say nifp, is computed from the
40 * value returned from ioctl(.., NIOCREG, ...) and the mmap region:
41 *	ioctl(fd, NIOCREG, &req);
42 *	mem = mmap(0, ... );
43 *	nifp = NETMAP_IF(mem, req.nr_nifp);
44 *		(so simple, we could just do it manually)
45 *
46 * From there:
47 *	struct netmap_ring *NETMAP_TXRING(nifp, index)
48 *	struct netmap_ring *NETMAP_RXRING(nifp, index)
49 *		we can access ring->nr_cur, ring->nr_avail, ring->nr_flags
50 *
51 *	ring->slot[i] gives us the i-th slot (we can access
52 *		directly plen, flags, bufindex)
53 *
54 *	char *buf = NETMAP_BUF(ring, x) returns a pointer to
55 *		the buffer numbered x
56 *
57 * Since rings are circular, we have macros to compute the next index
58 *	i = NETMAP_RING_NEXT(ring, i);
59 */
60
61#ifndef _NET_NETMAP_USER_H_
62#define _NET_NETMAP_USER_H_
63
64#define _NETMAP_OFFSET(type, ptr, offset) \
65	((type)(void *)((char *)(ptr) + (offset)))
66
67#define NETMAP_IF(b, o)	_NETMAP_OFFSET(struct netmap_if *, b, o)
68
69#define NETMAP_TXRING(nifp, index) _NETMAP_OFFSET(struct netmap_ring *, \
70	nifp, (nifp)->ring_ofs[index] )
71
72#define NETMAP_RXRING(nifp, index) _NETMAP_OFFSET(struct netmap_ring *,	\
73	nifp, (nifp)->ring_ofs[index + (nifp)->ni_tx_rings + 1] )
74
75#define NETMAP_BUF(ring, index)				\
76	((char *)(ring) + (ring)->buf_ofs + ((index)*(ring)->nr_buf_size))
77
78#define NETMAP_BUF_IDX(ring, buf)			\
79	( ((char *)(buf) - ((char *)(ring) + (ring)->buf_ofs) ) / \
80		(ring)->nr_buf_size )
81
82#define	NETMAP_RING_NEXT(r, i)				\
83	((i)+1 == (r)->num_slots ? 0 : (i) + 1 )
84
85#define	NETMAP_RING_FIRST_RESERVED(r)			\
86	( (r)->cur < (r)->reserved ?			\
87	  (r)->cur + (r)->num_slots - (r)->reserved :	\
88	  (r)->cur - (r)->reserved )
89
90/*
91 * Return 1 if the given tx ring is empty.
92 */
93#define NETMAP_TX_RING_EMPTY(r)	((r)->avail >= (r)->num_slots - 1)
94
95#endif /* _NET_NETMAP_USER_H_ */
96