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$ 3564ae02c3SLuigi Rizzo * $Id: netmap_user.h 10597 2012-02-21 05:08:32Z 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 * 4064ae02c3SLuigi Rizzo * The address of the struct netmap_if, say nifp, is computed from the 4164ae02c3SLuigi Rizzo * value returned from ioctl(.., NIOCREG, ...) and the mmap region: 4268b8534bSLuigi Rizzo * ioctl(fd, NIOCREG, &req); 4368b8534bSLuigi Rizzo * mem = mmap(0, ... ); 4468b8534bSLuigi Rizzo * nifp = NETMAP_IF(mem, req.nr_nifp); 4568b8534bSLuigi Rizzo * (so simple, we could just do it manually) 4668b8534bSLuigi Rizzo * 4768b8534bSLuigi Rizzo * From there: 4868b8534bSLuigi Rizzo * struct netmap_ring *NETMAP_TXRING(nifp, index) 4968b8534bSLuigi Rizzo * struct netmap_ring *NETMAP_RXRING(nifp, index) 5068b8534bSLuigi Rizzo * we can access ring->nr_cur, ring->nr_avail, ring->nr_flags 5168b8534bSLuigi Rizzo * 5268b8534bSLuigi Rizzo * ring->slot[i] gives us the i-th slot (we can access 5368b8534bSLuigi Rizzo * directly plen, flags, bufindex) 5468b8534bSLuigi Rizzo * 5568b8534bSLuigi Rizzo * char *buf = NETMAP_BUF(ring, index) returns a pointer to 5668b8534bSLuigi Rizzo * the i-th buffer 5768b8534bSLuigi Rizzo * 5868b8534bSLuigi Rizzo * Since rings are circular, we have macros to compute the next index 5968b8534bSLuigi Rizzo * i = NETMAP_RING_NEXT(ring, i); 6068b8534bSLuigi Rizzo */ 6168b8534bSLuigi Rizzo 6268b8534bSLuigi Rizzo #ifndef _NET_NETMAP_USER_H_ 6368b8534bSLuigi Rizzo #define _NET_NETMAP_USER_H_ 6468b8534bSLuigi Rizzo 6568b8534bSLuigi Rizzo #define NETMAP_IF(b, o) (struct netmap_if *)((char *)(b) + (o)) 6668b8534bSLuigi Rizzo 6768b8534bSLuigi Rizzo #define NETMAP_TXRING(nifp, index) \ 6868b8534bSLuigi Rizzo ((struct netmap_ring *)((char *)(nifp) + \ 6968b8534bSLuigi Rizzo (nifp)->ring_ofs[index] ) ) 7068b8534bSLuigi Rizzo 7168b8534bSLuigi Rizzo #define NETMAP_RXRING(nifp, index) \ 7268b8534bSLuigi Rizzo ((struct netmap_ring *)((char *)(nifp) + \ 7364ae02c3SLuigi Rizzo (nifp)->ring_ofs[index + (nifp)->ni_tx_queues+1] ) ) 7468b8534bSLuigi Rizzo 7568b8534bSLuigi Rizzo #define NETMAP_BUF(ring, index) \ 765819da83SLuigi Rizzo ((char *)(ring) + (ring)->buf_ofs + ((index)*(ring)->nr_buf_size)) 7768b8534bSLuigi Rizzo 7864ae02c3SLuigi Rizzo #define NETMAP_BUF_IDX(ring, buf) \ 7964ae02c3SLuigi Rizzo ( ((char *)(buf) - ((char *)(ring) + (ring)->buf_ofs) ) / \ 80*a7250582SLuigi Rizzo (ring)->nr_buf_size ) 8164ae02c3SLuigi Rizzo 8268b8534bSLuigi Rizzo #define NETMAP_RING_NEXT(r, i) \ 8368b8534bSLuigi Rizzo ((i)+1 == (r)->num_slots ? 0 : (i) + 1 ) 8468b8534bSLuigi Rizzo 85*a7250582SLuigi Rizzo #define NETMAP_RING_FIRST_RESERVED(r) \ 86*a7250582SLuigi Rizzo ( (r)->cur < (r)->reserved ? \ 87*a7250582SLuigi Rizzo (r)->cur + (r)->num_slots - (r)->reserved : \ 88*a7250582SLuigi Rizzo (r)->cur - (r)->reserved ) 89*a7250582SLuigi Rizzo 9068b8534bSLuigi Rizzo /* 9168b8534bSLuigi Rizzo * Return 1 if the given tx ring is empty. 9268b8534bSLuigi Rizzo */ 9368b8534bSLuigi Rizzo #define NETMAP_TX_RING_EMPTY(r) ((r)->avail >= (r)->num_slots - 1) 9468b8534bSLuigi Rizzo 9568b8534bSLuigi Rizzo #endif /* _NET_NETMAP_USER_H_ */ 96