168b8534bSLuigi Rizzo /* 268b8534bSLuigi Rizzo * Copyright (C) 2011 Matteo Landi, Luigi Rizzo. All rights reserved. 368b8534bSLuigi Rizzo * 468b8534bSLuigi Rizzo * Redistribution and use in source and binary forms, with or without 568b8534bSLuigi Rizzo * modification, are permitted provided that the following conditions are 668b8534bSLuigi Rizzo * met: 768b8534bSLuigi Rizzo * 868b8534bSLuigi Rizzo * 1. Redistributions of source code must retain the above copyright 968b8534bSLuigi Rizzo * notice, this list of conditions and the following disclaimer. 1068b8534bSLuigi Rizzo * 1168b8534bSLuigi Rizzo * 2. Redistributions in binary form must reproduce the above copyright 1268b8534bSLuigi Rizzo * notice, this list of conditions and the following disclaimer in the 1368b8534bSLuigi Rizzo * documentation and/or other materials provided with the 1468b8534bSLuigi Rizzo * distribution. 1568b8534bSLuigi Rizzo * 1668b8534bSLuigi Rizzo * 3. Neither the name of the authors nor the names of their contributors 1768b8534bSLuigi Rizzo * may be used to endorse or promote products derived from this 1868b8534bSLuigi Rizzo * software without specific prior written permission. 1968b8534bSLuigi Rizzo * 2068b8534bSLuigi Rizzo * THIS SOFTWARE IS PROVIDED BY MATTEO LANDI AND CONTRIBUTORS "AS IS" AND 2168b8534bSLuigi Rizzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2268b8534bSLuigi Rizzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2368b8534bSLuigi Rizzo * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTEO LANDI OR CONTRIBUTORS 2468b8534bSLuigi Rizzo * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2568b8534bSLuigi Rizzo * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2668b8534bSLuigi Rizzo * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2768b8534bSLuigi Rizzo * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2868b8534bSLuigi Rizzo * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2968b8534bSLuigi Rizzo * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 3068b8534bSLuigi Rizzo * THE POSSIBILITY OF SUCH DAMAGE. 3168b8534bSLuigi Rizzo */ 3268b8534bSLuigi Rizzo 3368b8534bSLuigi Rizzo /* 3468b8534bSLuigi Rizzo * $FreeBSD$ 3568b8534bSLuigi Rizzo * $Id: netmap_user.h 9495 2011-10-18 15:28:23Z luigi $ 3668b8534bSLuigi Rizzo * 3768b8534bSLuigi Rizzo * This header contains the macros used to manipulate netmap structures 3868b8534bSLuigi Rizzo * and packets in userspace. See netmap(4) for more information. 3968b8534bSLuigi Rizzo * 4068b8534bSLuigi Rizzo * The address of the struct netmap_if, say nifp, is determined 4168b8534bSLuigi Rizzo * by the value returned from ioctl(.., NIOCREG, ...) and the mmap 4268b8534bSLuigi Rizzo * region: 4368b8534bSLuigi Rizzo * ioctl(fd, NIOCREG, &req); 4468b8534bSLuigi Rizzo * mem = mmap(0, ... ); 4568b8534bSLuigi Rizzo * nifp = NETMAP_IF(mem, req.nr_nifp); 4668b8534bSLuigi Rizzo * (so simple, we could just do it manually) 4768b8534bSLuigi Rizzo * 4868b8534bSLuigi Rizzo * From there: 4968b8534bSLuigi Rizzo * struct netmap_ring *NETMAP_TXRING(nifp, index) 5068b8534bSLuigi Rizzo * struct netmap_ring *NETMAP_RXRING(nifp, index) 5168b8534bSLuigi Rizzo * we can access ring->nr_cur, ring->nr_avail, ring->nr_flags 5268b8534bSLuigi Rizzo * 5368b8534bSLuigi Rizzo * ring->slot[i] gives us the i-th slot (we can access 5468b8534bSLuigi Rizzo * directly plen, flags, bufindex) 5568b8534bSLuigi Rizzo * 5668b8534bSLuigi Rizzo * char *buf = NETMAP_BUF(ring, index) returns a pointer to 5768b8534bSLuigi Rizzo * the i-th buffer 5868b8534bSLuigi Rizzo * 5968b8534bSLuigi Rizzo * Since rings are circular, we have macros to compute the next index 6068b8534bSLuigi Rizzo * i = NETMAP_RING_NEXT(ring, i); 6168b8534bSLuigi Rizzo */ 6268b8534bSLuigi Rizzo 6368b8534bSLuigi Rizzo #ifndef _NET_NETMAP_USER_H_ 6468b8534bSLuigi Rizzo #define _NET_NETMAP_USER_H_ 6568b8534bSLuigi Rizzo 6668b8534bSLuigi Rizzo #define NETMAP_IF(b, o) (struct netmap_if *)((char *)(b) + (o)) 6768b8534bSLuigi Rizzo 6868b8534bSLuigi Rizzo #define NETMAP_TXRING(nifp, index) \ 6968b8534bSLuigi Rizzo ((struct netmap_ring *)((char *)(nifp) + \ 7068b8534bSLuigi Rizzo (nifp)->ring_ofs[index] ) ) 7168b8534bSLuigi Rizzo 7268b8534bSLuigi Rizzo #define NETMAP_RXRING(nifp, index) \ 7368b8534bSLuigi Rizzo ((struct netmap_ring *)((char *)(nifp) + \ 7468b8534bSLuigi Rizzo (nifp)->ring_ofs[index + (nifp)->ni_num_queues+1] ) ) 7568b8534bSLuigi Rizzo 7668b8534bSLuigi Rizzo #define NETMAP_BUF(ring, index) \ 77*5819da83SLuigi Rizzo ((char *)(ring) + (ring)->buf_ofs + ((index)*(ring)->nr_buf_size)) 7868b8534bSLuigi Rizzo 7968b8534bSLuigi Rizzo #define NETMAP_RING_NEXT(r, i) \ 8068b8534bSLuigi Rizzo ((i)+1 == (r)->num_slots ? 0 : (i) + 1 ) 8168b8534bSLuigi Rizzo 8268b8534bSLuigi Rizzo /* 8368b8534bSLuigi Rizzo * Return 1 if the given tx ring is empty. 8468b8534bSLuigi Rizzo * 8568b8534bSLuigi Rizzo * @r netmap_ring descriptor pointer. 8668b8534bSLuigi Rizzo * Special case, a negative value in hwavail indicates that the 8768b8534bSLuigi Rizzo * transmit queue is idle. 8868b8534bSLuigi Rizzo * XXX revise 8968b8534bSLuigi Rizzo */ 9068b8534bSLuigi Rizzo #define NETMAP_TX_RING_EMPTY(r) ((r)->avail >= (r)->num_slots - 1) 9168b8534bSLuigi Rizzo 9268b8534bSLuigi Rizzo #endif /* _NET_NETMAP_USER_H_ */ 93