1*68b8534bSLuigi Rizzo /* 2*68b8534bSLuigi Rizzo * Copyright (C) 2011 Matteo Landi, Luigi Rizzo. All rights reserved. 3*68b8534bSLuigi Rizzo * 4*68b8534bSLuigi Rizzo * Redistribution and use in source and binary forms, with or without 5*68b8534bSLuigi Rizzo * modification, are permitted provided that the following conditions are 6*68b8534bSLuigi Rizzo * met: 7*68b8534bSLuigi Rizzo * 8*68b8534bSLuigi Rizzo * 1. Redistributions of source code must retain the above copyright 9*68b8534bSLuigi Rizzo * notice, this list of conditions and the following disclaimer. 10*68b8534bSLuigi Rizzo * 11*68b8534bSLuigi Rizzo * 2. Redistributions in binary form must reproduce the above copyright 12*68b8534bSLuigi Rizzo * notice, this list of conditions and the following disclaimer in the 13*68b8534bSLuigi Rizzo * documentation and/or other materials provided with the 14*68b8534bSLuigi Rizzo * distribution. 15*68b8534bSLuigi Rizzo * 16*68b8534bSLuigi Rizzo * 3. Neither the name of the authors nor the names of their contributors 17*68b8534bSLuigi Rizzo * may be used to endorse or promote products derived from this 18*68b8534bSLuigi Rizzo * software without specific prior written permission. 19*68b8534bSLuigi Rizzo * 20*68b8534bSLuigi Rizzo * THIS SOFTWARE IS PROVIDED BY MATTEO LANDI AND CONTRIBUTORS "AS IS" AND 21*68b8534bSLuigi Rizzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22*68b8534bSLuigi Rizzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23*68b8534bSLuigi Rizzo * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTEO LANDI OR CONTRIBUTORS 24*68b8534bSLuigi Rizzo * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25*68b8534bSLuigi Rizzo * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26*68b8534bSLuigi Rizzo * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27*68b8534bSLuigi Rizzo * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28*68b8534bSLuigi Rizzo * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29*68b8534bSLuigi Rizzo * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 30*68b8534bSLuigi Rizzo * THE POSSIBILITY OF SUCH DAMAGE. 31*68b8534bSLuigi Rizzo */ 32*68b8534bSLuigi Rizzo 33*68b8534bSLuigi Rizzo /* 34*68b8534bSLuigi Rizzo * $FreeBSD$ 35*68b8534bSLuigi Rizzo * $Id: netmap_user.h 9495 2011-10-18 15:28:23Z luigi $ 36*68b8534bSLuigi Rizzo * 37*68b8534bSLuigi Rizzo * This header contains the macros used to manipulate netmap structures 38*68b8534bSLuigi Rizzo * and packets in userspace. See netmap(4) for more information. 39*68b8534bSLuigi Rizzo * 40*68b8534bSLuigi Rizzo * The address of the struct netmap_if, say nifp, is determined 41*68b8534bSLuigi Rizzo * by the value returned from ioctl(.., NIOCREG, ...) and the mmap 42*68b8534bSLuigi Rizzo * region: 43*68b8534bSLuigi Rizzo * ioctl(fd, NIOCREG, &req); 44*68b8534bSLuigi Rizzo * mem = mmap(0, ... ); 45*68b8534bSLuigi Rizzo * nifp = NETMAP_IF(mem, req.nr_nifp); 46*68b8534bSLuigi Rizzo * (so simple, we could just do it manually) 47*68b8534bSLuigi Rizzo * 48*68b8534bSLuigi Rizzo * From there: 49*68b8534bSLuigi Rizzo * struct netmap_ring *NETMAP_TXRING(nifp, index) 50*68b8534bSLuigi Rizzo * struct netmap_ring *NETMAP_RXRING(nifp, index) 51*68b8534bSLuigi Rizzo * we can access ring->nr_cur, ring->nr_avail, ring->nr_flags 52*68b8534bSLuigi Rizzo * 53*68b8534bSLuigi Rizzo * ring->slot[i] gives us the i-th slot (we can access 54*68b8534bSLuigi Rizzo * directly plen, flags, bufindex) 55*68b8534bSLuigi Rizzo * 56*68b8534bSLuigi Rizzo * char *buf = NETMAP_BUF(ring, index) returns a pointer to 57*68b8534bSLuigi Rizzo * the i-th buffer 58*68b8534bSLuigi Rizzo * 59*68b8534bSLuigi Rizzo * Since rings are circular, we have macros to compute the next index 60*68b8534bSLuigi Rizzo * i = NETMAP_RING_NEXT(ring, i); 61*68b8534bSLuigi Rizzo */ 62*68b8534bSLuigi Rizzo 63*68b8534bSLuigi Rizzo #ifndef _NET_NETMAP_USER_H_ 64*68b8534bSLuigi Rizzo #define _NET_NETMAP_USER_H_ 65*68b8534bSLuigi Rizzo 66*68b8534bSLuigi Rizzo #define NETMAP_IF(b, o) (struct netmap_if *)((char *)(b) + (o)) 67*68b8534bSLuigi Rizzo 68*68b8534bSLuigi Rizzo #define NETMAP_TXRING(nifp, index) \ 69*68b8534bSLuigi Rizzo ((struct netmap_ring *)((char *)(nifp) + \ 70*68b8534bSLuigi Rizzo (nifp)->ring_ofs[index] ) ) 71*68b8534bSLuigi Rizzo 72*68b8534bSLuigi Rizzo #define NETMAP_RXRING(nifp, index) \ 73*68b8534bSLuigi Rizzo ((struct netmap_ring *)((char *)(nifp) + \ 74*68b8534bSLuigi Rizzo (nifp)->ring_ofs[index + (nifp)->ni_num_queues+1] ) ) 75*68b8534bSLuigi Rizzo 76*68b8534bSLuigi Rizzo #if NETMAP_BUF_SIZE != 2048 77*68b8534bSLuigi Rizzo #error cannot handle odd size 78*68b8534bSLuigi Rizzo #define NETMAP_BUF(ring, index) \ 79*68b8534bSLuigi Rizzo ((char *)(ring) + (ring)->buf_ofs + ((index)*NETMAP_BUF_SIZE)) 80*68b8534bSLuigi Rizzo #else 81*68b8534bSLuigi Rizzo #define NETMAP_BUF(ring, index) \ 82*68b8534bSLuigi Rizzo ((char *)(ring) + (ring)->buf_ofs + ((index)<<11)) 83*68b8534bSLuigi Rizzo #endif 84*68b8534bSLuigi Rizzo 85*68b8534bSLuigi Rizzo #define NETMAP_RING_NEXT(r, i) \ 86*68b8534bSLuigi Rizzo ((i)+1 == (r)->num_slots ? 0 : (i) + 1 ) 87*68b8534bSLuigi Rizzo 88*68b8534bSLuigi Rizzo /* 89*68b8534bSLuigi Rizzo * Return 1 if the given tx ring is empty. 90*68b8534bSLuigi Rizzo * 91*68b8534bSLuigi Rizzo * @r netmap_ring descriptor pointer. 92*68b8534bSLuigi Rizzo * Special case, a negative value in hwavail indicates that the 93*68b8534bSLuigi Rizzo * transmit queue is idle. 94*68b8534bSLuigi Rizzo * XXX revise 95*68b8534bSLuigi Rizzo */ 96*68b8534bSLuigi Rizzo #define NETMAP_TX_RING_EMPTY(r) ((r)->avail >= (r)->num_slots - 1) 97*68b8534bSLuigi Rizzo 98*68b8534bSLuigi Rizzo #endif /* _NET_NETMAP_USER_H_ */ 99