1fb578518SFranco Fichtner /*
2fb578518SFranco Fichtner * Copyright (C) 2011-2013 Matteo Landi, Luigi Rizzo. All rights reserved.
3fb578518SFranco Fichtner * Copyright (C) 2013 Universita` di Pisa. All rights reserved.
4fb578518SFranco Fichtner *
5fb578518SFranco Fichtner * Redistribution and use in source and binary forms, with or without
6fb578518SFranco Fichtner * modification, are permitted provided that the following conditions
7fb578518SFranco Fichtner * are met:
8fb578518SFranco Fichtner * 1. Redistributions of source code must retain the above copyright
9fb578518SFranco Fichtner * notice, this list of conditions and the following disclaimer.
10fb578518SFranco Fichtner * 2. Redistributions in binary form must reproduce the above copyright
11fb578518SFranco Fichtner * notice, this list of conditions and the following disclaimer in the
12fb578518SFranco Fichtner * documentation and/or other materials provided with the distribution.
13fb578518SFranco Fichtner *
14fb578518SFranco Fichtner * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15fb578518SFranco Fichtner * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16fb578518SFranco Fichtner * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17fb578518SFranco Fichtner * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18fb578518SFranco Fichtner * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19fb578518SFranco Fichtner * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20fb578518SFranco Fichtner * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21fb578518SFranco Fichtner * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22fb578518SFranco Fichtner * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23fb578518SFranco Fichtner * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24fb578518SFranco Fichtner * SUCH DAMAGE.
25fb578518SFranco Fichtner */
26fb578518SFranco Fichtner
27fb578518SFranco Fichtner /*
28fb578518SFranco Fichtner * $FreeBSD: head/sys/dev/netmap/netmap_kern.h 238985 2012-08-02 11:59:43Z luigi $
29fb578518SFranco Fichtner *
30fb578518SFranco Fichtner * The header contains the definitions of constants and function
31fb578518SFranco Fichtner * prototypes used only in kernelspace.
32fb578518SFranco Fichtner */
33fb578518SFranco Fichtner
34fb578518SFranco Fichtner #ifndef _NET_NETMAP_KERN_H_
35fb578518SFranco Fichtner #define _NET_NETMAP_KERN_H_
36fb578518SFranco Fichtner
37fb578518SFranco Fichtner #define WITH_VALE // comment out to disable VALE support
38fb578518SFranco Fichtner
39fb578518SFranco Fichtner #define likely(x) __builtin_expect((long)!!(x), 1L)
40fb578518SFranco Fichtner #define unlikely(x) __builtin_expect((long)!!(x), 0L)
41fb578518SFranco Fichtner
42ed9bd855SFranco Fichtner #define NM_LOCK_T struct lock
43ed9bd855SFranco Fichtner #define NMG_LOCK_T struct lock
44ed9bd855SFranco Fichtner #define NMG_LOCK_INIT() lockinit(&netmap_global_lock, \
45bf9f7c16SFranco Fichtner "netmap global lock", 0, LK_CANRECURSE)
46ed9bd855SFranco Fichtner #define NMG_LOCK_DESTROY() lockuninit(&netmap_global_lock)
47ed9bd855SFranco Fichtner #define NMG_LOCK() lockmgr(&netmap_global_lock, LK_EXCLUSIVE)
48ed9bd855SFranco Fichtner #define NMG_UNLOCK() lockmgr(&netmap_global_lock, LK_RELEASE)
49bf9f7c16SFranco Fichtner #define NMG_LOCK_ASSERT() KKASSERT(lockstatus(&netmap_global_lock, NULL) != 0)
50fb578518SFranco Fichtner
51bf9f7c16SFranco Fichtner #define NM_SELINFO_T struct kqinfo
52fb578518SFranco Fichtner #define MBUF_LEN(m) ((m)->m_pkthdr.len)
53fb578518SFranco Fichtner #define MBUF_IFP(m) ((m)->m_pkthdr.rcvif)
5473029d08SFranco Fichtner #define NM_SEND_UP(ifp, m) ((ifp)->if_input(ifp, m, NULL, -1))
55fb578518SFranco Fichtner
56fb578518SFranco Fichtner #define NM_ATOMIC_T volatile int // XXX ?
57fb578518SFranco Fichtner /* atomic operations */
58fb578518SFranco Fichtner #include <machine/atomic.h>
59fb578518SFranco Fichtner #define NM_ATOMIC_TEST_AND_SET(p) (!atomic_cmpset_acq_int((p), 0, 1))
60fb578518SFranco Fichtner #define NM_ATOMIC_CLEAR(p) atomic_store_rel_int((p), 0)
61fb578518SFranco Fichtner
62fb578518SFranco Fichtner #define prefetch(x) __builtin_prefetch(x)
63fb578518SFranco Fichtner
64bf9f7c16SFranco Fichtner #define mb() cpu_mfence()
65bf9f7c16SFranco Fichtner #define rmb() cpu_lfence()
66bf9f7c16SFranco Fichtner #define wmb() cpu_sfence()
67bf9f7c16SFranco Fichtner
68*805c8e8eSzrj #ifdef MALLOC_DECLARE
69fb578518SFranco Fichtner MALLOC_DECLARE(M_NETMAP);
70*805c8e8eSzrj #endif
71fb578518SFranco Fichtner
72fb578518SFranco Fichtner // XXX linux struct, not used in FreeBSD
73fb578518SFranco Fichtner struct net_device_ops {
74fb578518SFranco Fichtner };
75fb578518SFranco Fichtner struct hrtimer {
76fb578518SFranco Fichtner };
77fb578518SFranco Fichtner
78ed9bd855SFranco Fichtner #define IFCAP_NETMAP 0x8000 /* XXX move to <net/if.h> */
79fb578518SFranco Fichtner
80fb578518SFranco Fichtner #define ND(format, ...)
81fb578518SFranco Fichtner #define D(format, ...) \
82fb578518SFranco Fichtner do { \
83fb578518SFranco Fichtner struct timeval __xxts; \
84fb578518SFranco Fichtner microtime(&__xxts); \
85ed9bd855SFranco Fichtner kprintf("%03d.%06d %s [%d] " format "\n", \
86fb578518SFranco Fichtner (int)__xxts.tv_sec % 1000, (int)__xxts.tv_usec, \
87fb578518SFranco Fichtner __FUNCTION__, __LINE__, ##__VA_ARGS__); \
88fb578518SFranco Fichtner } while (0)
89fb578518SFranco Fichtner
90fb578518SFranco Fichtner /* rate limited, lps indicates how many per second */
91fb578518SFranco Fichtner #define RD(lps, format, ...) \
92fb578518SFranco Fichtner do { \
93fb578518SFranco Fichtner static int t0, __cnt; \
94fb578518SFranco Fichtner if (t0 != time_second) { \
95fb578518SFranco Fichtner t0 = time_second; \
96fb578518SFranco Fichtner __cnt = 0; \
97fb578518SFranco Fichtner } \
98fb578518SFranco Fichtner if (__cnt++ < lps) \
99fb578518SFranco Fichtner D(format, ##__VA_ARGS__); \
100fb578518SFranco Fichtner } while (0)
101fb578518SFranco Fichtner
102fb578518SFranco Fichtner struct netmap_adapter;
103fb578518SFranco Fichtner struct nm_bdg_fwd;
104fb578518SFranco Fichtner struct nm_bridge;
105fb578518SFranco Fichtner struct netmap_priv_d;
106fb578518SFranco Fichtner
107fb578518SFranco Fichtner const char *nm_dump_buf(char *p, int len, int lim, char *dst);
108fb578518SFranco Fichtner
109b3f97fadSFranco Fichtner #include <net/netmap/netmap_mbq.h>
110fb578518SFranco Fichtner
111fb578518SFranco Fichtner extern NMG_LOCK_T netmap_global_lock;
112fb578518SFranco Fichtner
113fb578518SFranco Fichtner /*
114fb578518SFranco Fichtner * private, kernel view of a ring. Keeps track of the status of
115fb578518SFranco Fichtner * a ring across system calls.
116fb578518SFranco Fichtner *
117fb578518SFranco Fichtner * nr_hwcur index of the next buffer to refill.
118fb578518SFranco Fichtner * It corresponds to ring->cur - ring->reserved
119fb578518SFranco Fichtner *
120fb578518SFranco Fichtner * nr_hwavail the number of slots "owned" by userspace.
121fb578518SFranco Fichtner * nr_hwavail =:= ring->avail + ring->reserved
122fb578518SFranco Fichtner *
123fb578518SFranco Fichtner * The indexes in the NIC and netmap rings are offset by nkr_hwofs slots.
124fb578518SFranco Fichtner * This is so that, on a reset, buffers owned by userspace are not
125fb578518SFranco Fichtner * modified by the kernel. In particular:
126fb578518SFranco Fichtner * RX rings: the next empty buffer (hwcur + hwavail + hwofs) coincides with
127fb578518SFranco Fichtner * the next empty buffer as known by the hardware (next_to_check or so).
128fb578518SFranco Fichtner * TX rings: hwcur + hwofs coincides with next_to_send
129fb578518SFranco Fichtner *
130fb578518SFranco Fichtner * Clients cannot issue concurrent syscall on a ring. The system
131fb578518SFranco Fichtner * detects this and reports an error using two flags,
132fb578518SFranco Fichtner * NKR_WBUSY and NKR_RBUSY
133fb578518SFranco Fichtner * For received packets, slot->flags is set to nkr_slot_flags
134fb578518SFranco Fichtner * so we can provide a proper initial value (e.g. set NS_FORWARD
135fb578518SFranco Fichtner * when operating in 'transparent' mode).
136fb578518SFranco Fichtner *
137fb578518SFranco Fichtner * The following fields are used to implement lock-free copy of packets
138fb578518SFranco Fichtner * from input to output ports in VALE switch:
139fb578518SFranco Fichtner * nkr_hwlease buffer after the last one being copied.
140fb578518SFranco Fichtner * A writer in nm_bdg_flush reserves N buffers
141fb578518SFranco Fichtner * from nr_hwlease, advances it, then does the
142fb578518SFranco Fichtner * copy outside the lock.
143fb578518SFranco Fichtner * In RX rings (used for VALE ports),
144fb578518SFranco Fichtner * nkr_hwcur + nkr_hwavail <= nkr_hwlease < nkr_hwcur+N-1
145fb578518SFranco Fichtner * In TX rings (used for NIC or host stack ports)
146fb578518SFranco Fichtner * nkr_hwcur <= nkr_hwlease < nkr_hwcur+ nkr_hwavail
147fb578518SFranco Fichtner * nkr_leases array of nkr_num_slots where writers can report
148fb578518SFranco Fichtner * completion of their block. NR_NOSLOT (~0) indicates
149fb578518SFranco Fichtner * that the writer has not finished yet
150fb578518SFranco Fichtner * nkr_lease_idx index of next free slot in nr_leases, to be assigned
151fb578518SFranco Fichtner *
152fb578518SFranco Fichtner * The kring is manipulated by txsync/rxsync and generic netmap function.
153fb578518SFranco Fichtner * q_lock is used to arbitrate access to the kring from within the netmap
154fb578518SFranco Fichtner * code, and this and other protections guarantee that there is never
155fb578518SFranco Fichtner * more than 1 concurrent call to txsync or rxsync. So we are free
156fb578518SFranco Fichtner * to manipulate the kring from within txsync/rxsync without any extra
157fb578518SFranco Fichtner * locks.
158fb578518SFranco Fichtner */
159fb578518SFranco Fichtner struct netmap_kring {
160fb578518SFranco Fichtner struct netmap_ring *ring;
161fb578518SFranco Fichtner uint32_t nr_hwcur;
162fb578518SFranco Fichtner uint32_t nr_hwavail;
163fb578518SFranco Fichtner uint32_t nr_kflags; /* private driver flags */
164fb578518SFranco Fichtner int32_t nr_hwreserved;
165fb578518SFranco Fichtner #define NKR_PENDINTR 0x1 // Pending interrupt.
166fb578518SFranco Fichtner uint32_t nkr_num_slots;
167fb578518SFranco Fichtner int32_t nkr_hwofs; /* offset between NIC and netmap ring */
168fb578518SFranco Fichtner
169fb578518SFranco Fichtner uint16_t nkr_slot_flags; /* initial value for flags */
170fb578518SFranco Fichtner struct netmap_adapter *na;
171fb578518SFranco Fichtner struct nm_bdg_fwd *nkr_ft;
172fb578518SFranco Fichtner uint32_t *nkr_leases;
173fb578518SFranco Fichtner #define NR_NOSLOT ((uint32_t)~0)
174fb578518SFranco Fichtner uint32_t nkr_hwlease;
175fb578518SFranco Fichtner uint32_t nkr_lease_idx;
176fb578518SFranco Fichtner
177fb578518SFranco Fichtner NM_SELINFO_T si; /* poll/select wait queue */
178fb578518SFranco Fichtner NM_LOCK_T q_lock; /* protects kring and ring. */
179fb578518SFranco Fichtner NM_ATOMIC_T nr_busy; /* prevent concurrent syscalls */
180fb578518SFranco Fichtner
181fb578518SFranco Fichtner volatile int nkr_stopped;
182fb578518SFranco Fichtner
183fb578518SFranco Fichtner /* support for adapters without native netmap support.
184fb578518SFranco Fichtner * On tx rings we preallocate an array of tx buffers
185fb578518SFranco Fichtner * (same size as the netmap ring), on rx rings we
186fb578518SFranco Fichtner * store incoming packets in a queue.
187fb578518SFranco Fichtner * XXX who writes to the rx queue ?
188fb578518SFranco Fichtner */
189fb578518SFranco Fichtner struct mbuf **tx_pool;
190fb578518SFranco Fichtner u_int nr_ntc; /* Emulation of a next-to-clean RX ring pointer. */
191fb578518SFranco Fichtner struct mbq rx_queue; /* A queue for intercepted rx mbufs. */
192fb578518SFranco Fichtner
193fb578518SFranco Fichtner } __attribute__((__aligned__(64)));
194fb578518SFranco Fichtner
195fb578518SFranco Fichtner
196fb578518SFranco Fichtner /* return the next index, with wraparound */
197fb578518SFranco Fichtner static inline uint32_t
nm_next(uint32_t i,uint32_t lim)198fb578518SFranco Fichtner nm_next(uint32_t i, uint32_t lim)
199fb578518SFranco Fichtner {
200fb578518SFranco Fichtner return unlikely (i == lim) ? 0 : i + 1;
201fb578518SFranco Fichtner }
202fb578518SFranco Fichtner
203fb578518SFranco Fichtner /*
204fb578518SFranco Fichtner *
205fb578518SFranco Fichtner * Here is the layout for the Rx and Tx rings.
206fb578518SFranco Fichtner
207fb578518SFranco Fichtner RxRING TxRING
208fb578518SFranco Fichtner
209fb578518SFranco Fichtner +-----------------+ +-----------------+
210fb578518SFranco Fichtner | | | |
211fb578518SFranco Fichtner |XXX free slot XXX| |XXX free slot XXX|
212fb578518SFranco Fichtner +-----------------+ +-----------------+
213fb578518SFranco Fichtner | |<-hwcur | |<-hwcur
214fb578518SFranco Fichtner | reserved h | | (ready |
215fb578518SFranco Fichtner +----------- w -+ | to be |
216fb578518SFranco Fichtner cur->| a | | sent) h |
217fb578518SFranco Fichtner | v | +---------- w |
218fb578518SFranco Fichtner | a | cur->| (being a |
219fb578518SFranco Fichtner | i | | prepared) v |
220fb578518SFranco Fichtner | avail l | | a |
221fb578518SFranco Fichtner +-----------------+ + a ------ i +
222fb578518SFranco Fichtner | | ... | v l |<-hwlease
223fb578518SFranco Fichtner | (being | ... | a | ...
224fb578518SFranco Fichtner | prepared) | ... | i | ...
225fb578518SFranco Fichtner +-----------------+ ... | l | ...
226fb578518SFranco Fichtner | |<-hwlease +-----------------+
227fb578518SFranco Fichtner | | | |
228fb578518SFranco Fichtner | | | |
229fb578518SFranco Fichtner | | | |
230fb578518SFranco Fichtner | | | |
231fb578518SFranco Fichtner +-----------------+ +-----------------+
232fb578518SFranco Fichtner
233fb578518SFranco Fichtner * The cur/avail (user view) and hwcur/hwavail (kernel view)
234fb578518SFranco Fichtner * are used in the normal operation of the card.
235fb578518SFranco Fichtner *
236fb578518SFranco Fichtner * When a ring is the output of a switch port (Rx ring for
237fb578518SFranco Fichtner * a VALE port, Tx ring for the host stack or NIC), slots
238fb578518SFranco Fichtner * are reserved in blocks through 'hwlease' which points
239fb578518SFranco Fichtner * to the next unused slot.
240fb578518SFranco Fichtner * On an Rx ring, hwlease is always after hwavail,
241fb578518SFranco Fichtner * and completions cause avail to advance.
242fb578518SFranco Fichtner * On a Tx ring, hwlease is always between cur and hwavail,
243fb578518SFranco Fichtner * and completions cause cur to advance.
244fb578518SFranco Fichtner *
245fb578518SFranco Fichtner * nm_kr_space() returns the maximum number of slots that
246fb578518SFranco Fichtner * can be assigned.
247fb578518SFranco Fichtner * nm_kr_lease() reserves the required number of buffers,
248fb578518SFranco Fichtner * advances nkr_hwlease and also returns an entry in
249fb578518SFranco Fichtner * a circular array where completions should be reported.
250fb578518SFranco Fichtner */
251fb578518SFranco Fichtner
252fb578518SFranco Fichtner
253fb578518SFranco Fichtner
254fb578518SFranco Fichtner
255fb578518SFranco Fichtner enum txrx { NR_RX = 0, NR_TX = 1 };
256fb578518SFranco Fichtner
257fb578518SFranco Fichtner /*
258fb578518SFranco Fichtner * The "struct netmap_adapter" extends the "struct adapter"
259fb578518SFranco Fichtner * (or equivalent) device descriptor.
260fb578518SFranco Fichtner * It contains all base fields needed to support netmap operation.
261fb578518SFranco Fichtner * There are in fact different types of netmap adapters
262fb578518SFranco Fichtner * (native, generic, VALE switch...) so a netmap_adapter is
263fb578518SFranco Fichtner * just the first field in the derived type.
264fb578518SFranco Fichtner */
265fb578518SFranco Fichtner struct netmap_adapter {
266fb578518SFranco Fichtner /*
267fb578518SFranco Fichtner * On linux we do not have a good way to tell if an interface
268fb578518SFranco Fichtner * is netmap-capable. So we use the following trick:
269fb578518SFranco Fichtner * NA(ifp) points here, and the first entry (which hopefully
270fb578518SFranco Fichtner * always exists and is at least 32 bits) contains a magic
271fb578518SFranco Fichtner * value which we can use to detect that the interface is good.
272fb578518SFranco Fichtner */
273fb578518SFranco Fichtner uint32_t magic;
274fb578518SFranco Fichtner uint32_t na_flags; /* future place for IFCAP_NETMAP */
275fb578518SFranco Fichtner #define NAF_SKIP_INTR 1 /* use the regular interrupt handler.
276fb578518SFranco Fichtner * useful during initialization
277fb578518SFranco Fichtner */
278fb578518SFranco Fichtner #define NAF_SW_ONLY 2 /* forward packets only to sw adapter */
279fb578518SFranco Fichtner #define NAF_BDG_MAYSLEEP 4 /* the bridge is allowed to sleep when
280fb578518SFranco Fichtner * forwarding packets coming from this
281fb578518SFranco Fichtner * interface
282fb578518SFranco Fichtner */
283fb578518SFranco Fichtner #define NAF_MEM_OWNER 8 /* the adapter is responsible for the
284fb578518SFranco Fichtner * deallocation of the memory allocator
285fb578518SFranco Fichtner */
286fb578518SFranco Fichtner #define NAF_NATIVE_ON 16 /* the adapter is native and the attached
287fb578518SFranco Fichtner * interface is in netmap mode
288fb578518SFranco Fichtner */
289fb578518SFranco Fichtner int active_fds; /* number of user-space descriptors using this
290fb578518SFranco Fichtner interface, which is equal to the number of
291fb578518SFranco Fichtner struct netmap_if objs in the mapped region. */
292fb578518SFranco Fichtner
293fb578518SFranco Fichtner u_int num_rx_rings; /* number of adapter receive rings */
294fb578518SFranco Fichtner u_int num_tx_rings; /* number of adapter transmit rings */
295fb578518SFranco Fichtner
296fb578518SFranco Fichtner u_int num_tx_desc; /* number of descriptor in each queue */
297fb578518SFranco Fichtner u_int num_rx_desc;
298fb578518SFranco Fichtner
299fb578518SFranco Fichtner /* tx_rings and rx_rings are private but allocated
300fb578518SFranco Fichtner * as a contiguous chunk of memory. Each array has
301fb578518SFranco Fichtner * N+1 entries, for the adapter queues and for the host queue.
302fb578518SFranco Fichtner */
303fb578518SFranco Fichtner struct netmap_kring *tx_rings; /* array of TX rings. */
304fb578518SFranco Fichtner struct netmap_kring *rx_rings; /* array of RX rings. */
305fb578518SFranco Fichtner void *tailroom; /* space below the rings array */
306fb578518SFranco Fichtner /* (used for leases) */
307fb578518SFranco Fichtner
308785c7ee6SFranco Fichtner
30913431b3eSFranco Fichtner NM_SELINFO_T tx_si, rx_si; /* global wait queues */
310fb578518SFranco Fichtner
311fb578518SFranco Fichtner /* copy of if_qflush and if_transmit pointers, to intercept
312fb578518SFranco Fichtner * packets from the network stack when netmap is active.
313fb578518SFranco Fichtner */
314fb578518SFranco Fichtner int (*if_transmit)(struct ifnet *, struct mbuf *);
315fb578518SFranco Fichtner
316fb578518SFranco Fichtner /* references to the ifnet and device routines, used by
317fb578518SFranco Fichtner * the generic netmap functions.
318fb578518SFranco Fichtner */
319fb578518SFranco Fichtner struct ifnet *ifp; /* adapter is ifp->if_softc */
320fb578518SFranco Fichtner
321fb578518SFranco Fichtner /* private cleanup */
322fb578518SFranco Fichtner void (*nm_dtor)(struct netmap_adapter *);
323fb578518SFranco Fichtner
324fb578518SFranco Fichtner int (*nm_register)(struct netmap_adapter *, int onoff);
325fb578518SFranco Fichtner
326fb578518SFranco Fichtner int (*nm_txsync)(struct netmap_adapter *, u_int ring, int flags);
327fb578518SFranco Fichtner int (*nm_rxsync)(struct netmap_adapter *, u_int ring, int flags);
328fb578518SFranco Fichtner #define NAF_FORCE_READ 1
329fb578518SFranco Fichtner #define NAF_FORCE_RECLAIM 2
330fb578518SFranco Fichtner /* return configuration information */
331fb578518SFranco Fichtner int (*nm_config)(struct netmap_adapter *,
332fb578518SFranco Fichtner u_int *txr, u_int *txd, u_int *rxr, u_int *rxd);
333fb578518SFranco Fichtner int (*nm_krings_create)(struct netmap_adapter *);
334fb578518SFranco Fichtner void (*nm_krings_delete)(struct netmap_adapter *);
335fb578518SFranco Fichtner int (*nm_notify)(struct netmap_adapter *,
336fb578518SFranco Fichtner u_int ring, enum txrx, int flags);
337fb578518SFranco Fichtner #define NAF_GLOBAL_NOTIFY 4
338fb578518SFranco Fichtner #define NAF_DISABLE_NOTIFY 8
339fb578518SFranco Fichtner
340fb578518SFranco Fichtner /* standard refcount to control the lifetime of the adapter
341fb578518SFranco Fichtner * (it should be equal to the lifetime of the corresponding ifp)
342fb578518SFranco Fichtner */
343fb578518SFranco Fichtner int na_refcount;
344fb578518SFranco Fichtner
345fb578518SFranco Fichtner /* memory allocator (opaque)
346fb578518SFranco Fichtner * We also cache a pointer to the lut_entry for translating
347fb578518SFranco Fichtner * buffer addresses, and the total number of buffers.
348fb578518SFranco Fichtner */
349fb578518SFranco Fichtner struct netmap_mem_d *nm_mem;
350fb578518SFranco Fichtner struct lut_entry *na_lut;
351fb578518SFranco Fichtner uint32_t na_lut_objtotal; /* max buffer index */
352fb578518SFranco Fichtner
353fb578518SFranco Fichtner /* used internally. If non-null, the interface cannot be bound
354fb578518SFranco Fichtner * from userspace
355fb578518SFranco Fichtner */
356fb578518SFranco Fichtner void *na_private;
357fb578518SFranco Fichtner };
358fb578518SFranco Fichtner
359fb578518SFranco Fichtner /*
360fb578518SFranco Fichtner * If the NIC is owned by the kernel
361fb578518SFranco Fichtner * (i.e., bridge), neither another bridge nor user can use it;
362fb578518SFranco Fichtner * if the NIC is owned by a user, only users can share it.
363fb578518SFranco Fichtner * Evaluation must be done under NMG_LOCK().
364fb578518SFranco Fichtner */
365fb578518SFranco Fichtner #define NETMAP_OWNED_BY_KERN(na) (na->na_private)
366fb578518SFranco Fichtner #define NETMAP_OWNED_BY_ANY(na) \
367fb578518SFranco Fichtner (NETMAP_OWNED_BY_KERN(na) || (na->active_fds > 0))
368fb578518SFranco Fichtner
369fb578518SFranco Fichtner
370fb578518SFranco Fichtner /*
371fb578518SFranco Fichtner * derived netmap adapters for various types of ports
372fb578518SFranco Fichtner */
373fb578518SFranco Fichtner struct netmap_vp_adapter { /* VALE software port */
374fb578518SFranco Fichtner struct netmap_adapter up;
375fb578518SFranco Fichtner
376fb578518SFranco Fichtner /*
377fb578518SFranco Fichtner * Bridge support:
378fb578518SFranco Fichtner *
379fb578518SFranco Fichtner * bdg_port is the port number used in the bridge;
380fb578518SFranco Fichtner * na_bdg points to the bridge this NA is attached to.
381fb578518SFranco Fichtner */
382fb578518SFranco Fichtner int bdg_port;
383fb578518SFranco Fichtner struct nm_bridge *na_bdg;
384fb578518SFranco Fichtner int retry;
385fb578518SFranco Fichtner };
386fb578518SFranco Fichtner
387fb578518SFranco Fichtner struct netmap_hw_adapter { /* physical device */
388fb578518SFranco Fichtner struct netmap_adapter up;
389fb578518SFranco Fichtner
390fb578518SFranco Fichtner struct net_device_ops nm_ndo; // XXX linux only
391fb578518SFranco Fichtner };
392fb578518SFranco Fichtner
393fb578518SFranco Fichtner struct netmap_generic_adapter { /* non-native device */
394fb578518SFranco Fichtner struct netmap_hw_adapter up;
395fb578518SFranco Fichtner
396fb578518SFranco Fichtner /* Pointer to a previously used netmap adapter. */
397fb578518SFranco Fichtner struct netmap_adapter *prev;
398fb578518SFranco Fichtner
399fb578518SFranco Fichtner /* generic netmap adapters support:
400fb578518SFranco Fichtner * a net_device_ops struct overrides ndo_select_queue(),
401fb578518SFranco Fichtner * save_if_input saves the if_input hook (FreeBSD),
402fb578518SFranco Fichtner * mit_timer and mit_pending implement rx interrupt mitigation,
403fb578518SFranco Fichtner */
404fb578518SFranco Fichtner struct net_device_ops generic_ndo;
40573029d08SFranco Fichtner void (*save_if_input)(struct ifnet *, struct mbuf *,
40673029d08SFranco Fichtner const struct pktinfo *, int);
407fb578518SFranco Fichtner
408fb578518SFranco Fichtner struct hrtimer mit_timer;
409fb578518SFranco Fichtner int mit_pending;
410fb578518SFranco Fichtner };
411fb578518SFranco Fichtner
412fb578518SFranco Fichtner #ifdef WITH_VALE
413fb578518SFranco Fichtner
414fb578518SFranco Fichtner /* bridge wrapper for non VALE ports. It is used to connect real devices to the bridge.
415fb578518SFranco Fichtner *
416fb578518SFranco Fichtner * The real device must already have its own netmap adapter (hwna). The
417fb578518SFranco Fichtner * bridge wrapper and the hwna adapter share the same set of netmap rings and
418fb578518SFranco Fichtner * buffers, but they have two separate sets of krings descriptors, with tx/rx
419fb578518SFranco Fichtner * meanings swapped:
420fb578518SFranco Fichtner *
421fb578518SFranco Fichtner * netmap
422fb578518SFranco Fichtner * bwrap krings rings krings hwna
423fb578518SFranco Fichtner * +------+ +------+ +-----+ +------+ +------+
424fb578518SFranco Fichtner * |tx_rings->| |\ /| |----| |<-tx_rings|
425fb578518SFranco Fichtner * | | +------+ \ / +-----+ +------+ | |
426fb578518SFranco Fichtner * | | X | |
427fb578518SFranco Fichtner * | | / \ | |
428fb578518SFranco Fichtner * | | +------+/ \+-----+ +------+ | |
429fb578518SFranco Fichtner * |rx_rings->| | | |----| |<-rx_rings|
430fb578518SFranco Fichtner * | | +------+ +-----+ +------+ | |
431fb578518SFranco Fichtner * +------+ +------+
432fb578518SFranco Fichtner *
433fb578518SFranco Fichtner * - packets coming from the bridge go to the brwap rx rings, which are also the
434fb578518SFranco Fichtner * hwna tx rings. The bwrap notify callback will then complete the hwna tx
435fb578518SFranco Fichtner * (see netmap_bwrap_notify).
436fb578518SFranco Fichtner * - packets coming from the outside go to the hwna rx rings, which are also the
437fb578518SFranco Fichtner * bwrap tx rings. The (overwritten) hwna notify method will then complete
438fb578518SFranco Fichtner * the bridge tx (see netmap_bwrap_intr_notify).
439fb578518SFranco Fichtner *
440fb578518SFranco Fichtner * The bridge wrapper may optionally connect the hwna 'host' rings to the
441fb578518SFranco Fichtner * bridge. This is done by using a second port in the bridge and connecting it
442fb578518SFranco Fichtner * to the 'host' netmap_vp_adapter contained in the netmap_bwrap_adapter.
443fb578518SFranco Fichtner * The brwap host adapter cross-links the hwna host rings in the same way as shown above.
444fb578518SFranco Fichtner *
445fb578518SFranco Fichtner * - packets coming from the bridge and directed to host stack are handled by the
446fb578518SFranco Fichtner * bwrap host notify callback (see netmap_bwrap_host_notify)
447fb578518SFranco Fichtner * - packets coming from the host stack are still handled by the overwritten
448fb578518SFranco Fichtner * hwna notify callback (netmap_bwrap_intr_notify), but are diverted to the
449fb578518SFranco Fichtner * host adapter depending on the ring number.
450fb578518SFranco Fichtner *
451fb578518SFranco Fichtner */
452fb578518SFranco Fichtner struct netmap_bwrap_adapter {
453fb578518SFranco Fichtner struct netmap_vp_adapter up;
454fb578518SFranco Fichtner struct netmap_vp_adapter host; /* for host rings */
455fb578518SFranco Fichtner struct netmap_adapter *hwna; /* the underlying device */
456fb578518SFranco Fichtner
457fb578518SFranco Fichtner /* backup of the hwna notify callback */
458fb578518SFranco Fichtner int (*save_notify)(struct netmap_adapter *,
459fb578518SFranco Fichtner u_int ring, enum txrx, int flags);
460fb578518SFranco Fichtner /* When we attach a physical interface to the bridge, we
461fb578518SFranco Fichtner * allow the controlling process to terminate, so we need
462fb578518SFranco Fichtner * a place to store the netmap_priv_d data structure.
463fb578518SFranco Fichtner * This is only done when physical interfaces are attached to a bridge.
464fb578518SFranco Fichtner */
465fb578518SFranco Fichtner struct netmap_priv_d *na_kpriv;
466fb578518SFranco Fichtner };
467fb578518SFranco Fichtner
468fb578518SFranco Fichtner
469fb578518SFranco Fichtner /*
470fb578518SFranco Fichtner * Available space in the ring. Only used in VALE code
471fb578518SFranco Fichtner */
472fb578518SFranco Fichtner static inline uint32_t
nm_kr_space(struct netmap_kring * k,int is_rx)473fb578518SFranco Fichtner nm_kr_space(struct netmap_kring *k, int is_rx)
474fb578518SFranco Fichtner {
475fb578518SFranco Fichtner int space;
476fb578518SFranco Fichtner
477fb578518SFranco Fichtner if (is_rx) {
478fb578518SFranco Fichtner int busy = k->nkr_hwlease - k->nr_hwcur + k->nr_hwreserved;
479fb578518SFranco Fichtner if (busy < 0)
480fb578518SFranco Fichtner busy += k->nkr_num_slots;
481fb578518SFranco Fichtner space = k->nkr_num_slots - 1 - busy;
482fb578518SFranco Fichtner } else {
483fb578518SFranco Fichtner space = k->nr_hwcur + k->nr_hwavail - k->nkr_hwlease;
484fb578518SFranco Fichtner if (space < 0)
485fb578518SFranco Fichtner space += k->nkr_num_slots;
486fb578518SFranco Fichtner }
487fb578518SFranco Fichtner #if 0
488fb578518SFranco Fichtner // sanity check
489fb578518SFranco Fichtner if (k->nkr_hwlease >= k->nkr_num_slots ||
490fb578518SFranco Fichtner k->nr_hwcur >= k->nkr_num_slots ||
491fb578518SFranco Fichtner k->nr_hwavail >= k->nkr_num_slots ||
492fb578518SFranco Fichtner busy < 0 ||
493fb578518SFranco Fichtner busy >= k->nkr_num_slots) {
494fb578518SFranco Fichtner D("invalid kring, cur %d avail %d lease %d lease_idx %d lim %d", k->nr_hwcur, k->nr_hwavail, k->nkr_hwlease,
495fb578518SFranco Fichtner k->nkr_lease_idx, k->nkr_num_slots);
496fb578518SFranco Fichtner }
497fb578518SFranco Fichtner #endif
498fb578518SFranco Fichtner return space;
499fb578518SFranco Fichtner }
500fb578518SFranco Fichtner
501fb578518SFranco Fichtner
502fb578518SFranco Fichtner
503fb578518SFranco Fichtner
504fb578518SFranco Fichtner /* make a lease on the kring for N positions. return the
505fb578518SFranco Fichtner * lease index
506fb578518SFranco Fichtner */
507fb578518SFranco Fichtner static inline uint32_t
nm_kr_lease(struct netmap_kring * k,u_int n,int is_rx)508fb578518SFranco Fichtner nm_kr_lease(struct netmap_kring *k, u_int n, int is_rx)
509fb578518SFranco Fichtner {
510fb578518SFranco Fichtner uint32_t lim = k->nkr_num_slots - 1;
511fb578518SFranco Fichtner uint32_t lease_idx = k->nkr_lease_idx;
512fb578518SFranco Fichtner
513fb578518SFranco Fichtner k->nkr_leases[lease_idx] = NR_NOSLOT;
514fb578518SFranco Fichtner k->nkr_lease_idx = nm_next(lease_idx, lim);
515fb578518SFranco Fichtner
516fb578518SFranco Fichtner if (n > nm_kr_space(k, is_rx)) {
517fb578518SFranco Fichtner D("invalid request for %d slots", n);
518fb578518SFranco Fichtner panic("x");
519fb578518SFranco Fichtner }
520fb578518SFranco Fichtner /* XXX verify that there are n slots */
521fb578518SFranco Fichtner k->nkr_hwlease += n;
522fb578518SFranco Fichtner if (k->nkr_hwlease > lim)
523fb578518SFranco Fichtner k->nkr_hwlease -= lim + 1;
524fb578518SFranco Fichtner
525fb578518SFranco Fichtner if (k->nkr_hwlease >= k->nkr_num_slots ||
526fb578518SFranco Fichtner k->nr_hwcur >= k->nkr_num_slots ||
527fb578518SFranco Fichtner k->nr_hwavail >= k->nkr_num_slots ||
528fb578518SFranco Fichtner k->nkr_lease_idx >= k->nkr_num_slots) {
529fb578518SFranco Fichtner D("invalid kring %s, cur %d avail %d lease %d lease_idx %d lim %d",
530fb578518SFranco Fichtner k->na->ifp->if_xname,
531fb578518SFranco Fichtner k->nr_hwcur, k->nr_hwavail, k->nkr_hwlease,
532fb578518SFranco Fichtner k->nkr_lease_idx, k->nkr_num_slots);
533fb578518SFranco Fichtner }
534fb578518SFranco Fichtner return lease_idx;
535fb578518SFranco Fichtner }
536fb578518SFranco Fichtner
537fb578518SFranco Fichtner #endif /* WITH_VALE */
538fb578518SFranco Fichtner
539fb578518SFranco Fichtner /* return update position */
540fb578518SFranco Fichtner static inline uint32_t
nm_kr_rxpos(struct netmap_kring * k)541fb578518SFranco Fichtner nm_kr_rxpos(struct netmap_kring *k)
542fb578518SFranco Fichtner {
543fb578518SFranco Fichtner uint32_t pos = k->nr_hwcur + k->nr_hwavail;
544fb578518SFranco Fichtner if (pos >= k->nkr_num_slots)
545fb578518SFranco Fichtner pos -= k->nkr_num_slots;
546fb578518SFranco Fichtner #if 0
547fb578518SFranco Fichtner if (pos >= k->nkr_num_slots ||
548fb578518SFranco Fichtner k->nkr_hwlease >= k->nkr_num_slots ||
549fb578518SFranco Fichtner k->nr_hwcur >= k->nkr_num_slots ||
550fb578518SFranco Fichtner k->nr_hwavail >= k->nkr_num_slots ||
551fb578518SFranco Fichtner k->nkr_lease_idx >= k->nkr_num_slots) {
552fb578518SFranco Fichtner D("invalid kring, cur %d avail %d lease %d lease_idx %d lim %d", k->nr_hwcur, k->nr_hwavail, k->nkr_hwlease,
553fb578518SFranco Fichtner k->nkr_lease_idx, k->nkr_num_slots);
554fb578518SFranco Fichtner }
555fb578518SFranco Fichtner #endif
556fb578518SFranco Fichtner return pos;
557fb578518SFranco Fichtner }
558fb578518SFranco Fichtner
559fb578518SFranco Fichtner
560fb578518SFranco Fichtner /*
561fb578518SFranco Fichtner * protect against multiple threads using the same ring.
562fb578518SFranco Fichtner * also check that the ring has not been stopped.
563fb578518SFranco Fichtner * We only care for 0 or !=0 as a return code.
564fb578518SFranco Fichtner */
565fb578518SFranco Fichtner #define NM_KR_BUSY 1
566fb578518SFranco Fichtner #define NM_KR_STOPPED 2
567fb578518SFranco Fichtner
nm_kr_put(struct netmap_kring * kr)568fb578518SFranco Fichtner static __inline void nm_kr_put(struct netmap_kring *kr)
569fb578518SFranco Fichtner {
570fb578518SFranco Fichtner NM_ATOMIC_CLEAR(&kr->nr_busy);
571fb578518SFranco Fichtner }
572fb578518SFranco Fichtner
nm_kr_tryget(struct netmap_kring * kr)573fb578518SFranco Fichtner static __inline int nm_kr_tryget(struct netmap_kring *kr)
574fb578518SFranco Fichtner {
575fb578518SFranco Fichtner /* check a first time without taking the lock
576fb578518SFranco Fichtner * to avoid starvation for nm_kr_get()
577fb578518SFranco Fichtner */
578fb578518SFranco Fichtner if (unlikely(kr->nkr_stopped)) {
579fb578518SFranco Fichtner ND("ring %p stopped (%d)", kr, kr->nkr_stopped);
580fb578518SFranco Fichtner return NM_KR_STOPPED;
581fb578518SFranco Fichtner }
582fb578518SFranco Fichtner if (unlikely(NM_ATOMIC_TEST_AND_SET(&kr->nr_busy)))
583fb578518SFranco Fichtner return NM_KR_BUSY;
584fb578518SFranco Fichtner /* check a second time with lock held */
585fb578518SFranco Fichtner if (unlikely(kr->nkr_stopped)) {
586fb578518SFranco Fichtner ND("ring %p stopped (%d)", kr, kr->nkr_stopped);
587fb578518SFranco Fichtner nm_kr_put(kr);
588fb578518SFranco Fichtner return NM_KR_STOPPED;
589fb578518SFranco Fichtner }
590fb578518SFranco Fichtner return 0;
591fb578518SFranco Fichtner }
592fb578518SFranco Fichtner
593fb578518SFranco Fichtner
594fb578518SFranco Fichtner /*
595fb578518SFranco Fichtner * The following are support routines used by individual drivers to
596fb578518SFranco Fichtner * support netmap operation.
597fb578518SFranco Fichtner *
598fb578518SFranco Fichtner * netmap_attach() initializes a struct netmap_adapter, allocating the
599fb578518SFranco Fichtner * struct netmap_ring's and the struct selinfo.
600fb578518SFranco Fichtner *
601fb578518SFranco Fichtner * netmap_detach() frees the memory allocated by netmap_attach().
602fb578518SFranco Fichtner *
603fb578518SFranco Fichtner * netmap_transmit() replaces the if_transmit routine of the interface,
604fb578518SFranco Fichtner * and is used to intercept packets coming from the stack.
605fb578518SFranco Fichtner *
606fb578518SFranco Fichtner * netmap_load_map/netmap_reload_map are helper routines to set/reset
607fb578518SFranco Fichtner * the dmamap for a packet buffer
608fb578518SFranco Fichtner *
609fb578518SFranco Fichtner * netmap_reset() is a helper routine to be called in the driver
610fb578518SFranco Fichtner * when reinitializing a ring.
611fb578518SFranco Fichtner */
612fb578518SFranco Fichtner int netmap_attach(struct netmap_adapter *);
613fb578518SFranco Fichtner int netmap_attach_common(struct netmap_adapter *);
614fb578518SFranco Fichtner void netmap_detach_common(struct netmap_adapter *na);
615fb578518SFranco Fichtner void netmap_detach(struct ifnet *);
616fb578518SFranco Fichtner int netmap_transmit(struct ifnet *, struct mbuf *);
617fb578518SFranco Fichtner struct netmap_slot *netmap_reset(struct netmap_adapter *na,
618fb578518SFranco Fichtner enum txrx tx, u_int n, u_int new_cur);
619fb578518SFranco Fichtner int netmap_ring_reinit(struct netmap_kring *);
620fb578518SFranco Fichtner
621fb578518SFranco Fichtner
622fb578518SFranco Fichtner /*
623fb578518SFranco Fichtner * Support routines to be used with the VALE switch
624fb578518SFranco Fichtner */
625fb578518SFranco Fichtner int netmap_update_config(struct netmap_adapter *na);
626fb578518SFranco Fichtner int netmap_krings_create(struct netmap_adapter *na, u_int ntx, u_int nrx, u_int tailroom);
627fb578518SFranco Fichtner void netmap_krings_delete(struct netmap_adapter *na);
628fb578518SFranco Fichtner
629fb578518SFranco Fichtner struct netmap_if *
630fb578518SFranco Fichtner netmap_do_regif(struct netmap_priv_d *priv, struct netmap_adapter *na,
631fb578518SFranco Fichtner uint16_t ringid, int *err);
632fb578518SFranco Fichtner
633fb578518SFranco Fichtner
634fb578518SFranco Fichtner
635fb578518SFranco Fichtner u_int nm_bound_var(u_int *v, u_int dflt, u_int lo, u_int hi, const char *msg);
636fb578518SFranco Fichtner int netmap_get_na(struct nmreq *nmr, struct netmap_adapter **na, int create);
637fb578518SFranco Fichtner int netmap_get_hw_na(struct ifnet *ifp, struct netmap_adapter **na);
638fb578518SFranco Fichtner
639fb578518SFranco Fichtner #ifdef WITH_VALE
640fb578518SFranco Fichtner /*
641fb578518SFranco Fichtner * The following bridge-related interfaces are used by other kernel modules
642fb578518SFranco Fichtner * In the version that only supports unicast or broadcast, the lookup
643fb578518SFranco Fichtner * function can return 0 .. NM_BDG_MAXPORTS-1 for regular ports,
644fb578518SFranco Fichtner * NM_BDG_MAXPORTS for broadcast, NM_BDG_MAXPORTS+1 for unknown.
645fb578518SFranco Fichtner * XXX in practice "unknown" might be handled same as broadcast.
646fb578518SFranco Fichtner */
647fb578518SFranco Fichtner typedef u_int (*bdg_lookup_fn_t)(char *buf, u_int len,
648fb578518SFranco Fichtner uint8_t *ring_nr, struct netmap_vp_adapter *);
649fb578518SFranco Fichtner u_int netmap_bdg_learning(char *, u_int, uint8_t *,
650fb578518SFranco Fichtner struct netmap_vp_adapter *);
651fb578518SFranco Fichtner
652fb578518SFranco Fichtner #define NM_BDG_MAXPORTS 254 /* up to 254 */
653fb578518SFranco Fichtner #define NM_BDG_BROADCAST NM_BDG_MAXPORTS
654fb578518SFranco Fichtner #define NM_BDG_NOPORT (NM_BDG_MAXPORTS+1)
655fb578518SFranco Fichtner
656fb578518SFranco Fichtner #define NM_NAME "vale" /* prefix for bridge port name */
657fb578518SFranco Fichtner
658fb578518SFranco Fichtner
659fb578518SFranco Fichtner /* these are redefined in case of no VALE support */
660fb578518SFranco Fichtner int netmap_get_bdg_na(struct nmreq *nmr, struct netmap_adapter **na, int create);
661fb578518SFranco Fichtner void netmap_init_bridges(void);
662fb578518SFranco Fichtner int netmap_bdg_ctl(struct nmreq *nmr, bdg_lookup_fn_t func);
663fb578518SFranco Fichtner
664fb578518SFranco Fichtner #else /* !WITH_VALE */
665fb578518SFranco Fichtner #define netmap_get_bdg_na(_1, _2, _3) 0
666fb578518SFranco Fichtner #define netmap_init_bridges(_1)
667fb578518SFranco Fichtner #define netmap_bdg_ctl(_1, _2) EINVAL
668fb578518SFranco Fichtner #endif /* !WITH_VALE */
669fb578518SFranco Fichtner
670fb578518SFranco Fichtner /* Various prototypes */
671b3f97fadSFranco Fichtner struct dev_kqfilter_args; /* XXX this shouldn't be here */
672f27ed164SFranco Fichtner int netmap_kqfilter(struct dev_kqfilter_args *ap);
673fb578518SFranco Fichtner
674fb578518SFranco Fichtner
675fb578518SFranco Fichtner int netmap_init(void);
676fb578518SFranco Fichtner void netmap_fini(void);
677fb578518SFranco Fichtner int netmap_get_memory(struct netmap_priv_d* p);
678fb578518SFranco Fichtner void netmap_dtor(void *data);
679fb578518SFranco Fichtner int netmap_dtor_locked(struct netmap_priv_d *priv);
680fb578518SFranco Fichtner
68113431b3eSFranco Fichtner struct dev_ioctl_args; /* XXX this shouldn't be here */
68213431b3eSFranco Fichtner int netmap_ioctl(struct dev_ioctl_args *ap);
683fb578518SFranco Fichtner
684fb578518SFranco Fichtner /* netmap_adapter creation/destruction */
685fb578518SFranco Fichtner #define NM_IFPNAME(ifp) ((ifp) ? (ifp)->if_xname : "zombie")
686fb578518SFranco Fichtner #define NM_DEBUG_PUTGET 1
687fb578518SFranco Fichtner
688fb578518SFranco Fichtner #ifdef NM_DEBUG_PUTGET
689fb578518SFranco Fichtner
690fb578518SFranco Fichtner #define NM_DBG(f) __##f
691fb578518SFranco Fichtner
692fb578518SFranco Fichtner void __netmap_adapter_get(struct netmap_adapter *na);
693fb578518SFranco Fichtner
694fb578518SFranco Fichtner #define netmap_adapter_get(na) \
695fb578518SFranco Fichtner do { \
696fb578518SFranco Fichtner struct netmap_adapter *__na = na; \
697fb578518SFranco Fichtner D("getting %p:%s (%d)", __na, NM_IFPNAME(__na->ifp), __na->na_refcount); \
698fb578518SFranco Fichtner __netmap_adapter_get(__na); \
699fb578518SFranco Fichtner } while (0)
700fb578518SFranco Fichtner
701fb578518SFranco Fichtner int __netmap_adapter_put(struct netmap_adapter *na);
702fb578518SFranco Fichtner
703fb578518SFranco Fichtner #define netmap_adapter_put(na) \
704fb578518SFranco Fichtner do { \
705fb578518SFranco Fichtner struct netmap_adapter *__na = na; \
706fb578518SFranco Fichtner D("putting %p:%s (%d)", __na, NM_IFPNAME(__na->ifp), __na->na_refcount); \
707fb578518SFranco Fichtner __netmap_adapter_put(__na); \
708fb578518SFranco Fichtner } while (0)
709fb578518SFranco Fichtner
710fb578518SFranco Fichtner #else /* !NM_DEBUG_PUTGET */
711fb578518SFranco Fichtner
712fb578518SFranco Fichtner #define NM_DBG(f) f
713fb578518SFranco Fichtner void netmap_adapter_get(struct netmap_adapter *na);
714fb578518SFranco Fichtner int netmap_adapter_put(struct netmap_adapter *na);
715fb578518SFranco Fichtner
716fb578518SFranco Fichtner #endif /* !NM_DEBUG_PUTGET */
717fb578518SFranco Fichtner
718fb578518SFranco Fichtner
719fb578518SFranco Fichtner
720fb578518SFranco Fichtner extern u_int netmap_buf_size;
721fb578518SFranco Fichtner #define NETMAP_BUF_SIZE netmap_buf_size // XXX remove
722fb578518SFranco Fichtner extern int netmap_mitigate;
723fb578518SFranco Fichtner extern int netmap_no_pendintr;
724fb578518SFranco Fichtner extern u_int netmap_total_buffers;
725fb578518SFranco Fichtner extern char *netmap_buffer_base;
726fb578518SFranco Fichtner extern int netmap_verbose; // XXX debugging
727fb578518SFranco Fichtner enum { /* verbose flags */
728fb578518SFranco Fichtner NM_VERB_ON = 1, /* generic verbose */
729fb578518SFranco Fichtner NM_VERB_HOST = 0x2, /* verbose host stack */
730fb578518SFranco Fichtner NM_VERB_RXSYNC = 0x10, /* verbose on rxsync/txsync */
731fb578518SFranco Fichtner NM_VERB_TXSYNC = 0x20,
732fb578518SFranco Fichtner NM_VERB_RXINTR = 0x100, /* verbose on rx/tx intr (driver) */
733fb578518SFranco Fichtner NM_VERB_TXINTR = 0x200,
734fb578518SFranco Fichtner NM_VERB_NIC_RXSYNC = 0x1000, /* verbose on rx/tx intr (driver) */
735fb578518SFranco Fichtner NM_VERB_NIC_TXSYNC = 0x2000,
736fb578518SFranco Fichtner };
737fb578518SFranco Fichtner
738fb578518SFranco Fichtner extern int netmap_txsync_retry;
739fb578518SFranco Fichtner extern int netmap_generic_mit;
740fb578518SFranco Fichtner extern int netmap_generic_ringsize;
741fb578518SFranco Fichtner
742fb578518SFranco Fichtner /*
743fb578518SFranco Fichtner * NA returns a pointer to the struct netmap adapter from the ifp,
744fb578518SFranco Fichtner * WNA is used to write it.
745fb578518SFranco Fichtner */
746fb578518SFranco Fichtner #ifndef WNA
747ed9bd855SFranco Fichtner #define WNA(_ifp) (_ifp)->if_unused7 /* XXX better name ;) */
748fb578518SFranco Fichtner #endif
749fb578518SFranco Fichtner #define NA(_ifp) ((struct netmap_adapter *)WNA(_ifp))
750fb578518SFranco Fichtner
751fb578518SFranco Fichtner /*
752fb578518SFranco Fichtner * Macros to determine if an interface is netmap capable or netmap enabled.
753fb578518SFranco Fichtner * See the magic field in struct netmap_adapter.
754fb578518SFranco Fichtner */
755fb578518SFranco Fichtner /*
756fb578518SFranco Fichtner * on FreeBSD just use if_capabilities and if_capenable.
757fb578518SFranco Fichtner */
758fb578518SFranco Fichtner #define NETMAP_CAPABLE(ifp) (NA(ifp) && \
759fb578518SFranco Fichtner (ifp)->if_capabilities & IFCAP_NETMAP )
760fb578518SFranco Fichtner
761fb578518SFranco Fichtner #define NETMAP_SET_CAPABLE(ifp) \
762fb578518SFranco Fichtner (ifp)->if_capabilities |= IFCAP_NETMAP
763fb578518SFranco Fichtner
764fb578518SFranco Fichtner /* Callback invoked by the dma machinery after a successfull dmamap_load */
netmap_dmamap_cb(__unused void * arg,__unused bus_dma_segment_t * segs,__unused int nseg,__unused int error)765fb578518SFranco Fichtner static void netmap_dmamap_cb(__unused void *arg,
766fb578518SFranco Fichtner __unused bus_dma_segment_t * segs, __unused int nseg, __unused int error)
767fb578518SFranco Fichtner {
768fb578518SFranco Fichtner }
769fb578518SFranco Fichtner
770fb578518SFranco Fichtner /* bus_dmamap_load wrapper: call aforementioned function if map != NULL.
771fb578518SFranco Fichtner * XXX can we do it without a callback ?
772fb578518SFranco Fichtner */
773fb578518SFranco Fichtner static inline void
netmap_load_map(bus_dma_tag_t tag,bus_dmamap_t map,void * buf)774fb578518SFranco Fichtner netmap_load_map(bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
775fb578518SFranco Fichtner {
776fb578518SFranco Fichtner if (map)
777fb578518SFranco Fichtner bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE,
778fb578518SFranco Fichtner netmap_dmamap_cb, NULL, BUS_DMA_NOWAIT);
779fb578518SFranco Fichtner }
780fb578518SFranco Fichtner
781fb578518SFranco Fichtner /* update the map when a buffer changes. */
782fb578518SFranco Fichtner static inline void
netmap_reload_map(bus_dma_tag_t tag,bus_dmamap_t map,void * buf)783fb578518SFranco Fichtner netmap_reload_map(bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
784fb578518SFranco Fichtner {
785fb578518SFranco Fichtner if (map) {
786fb578518SFranco Fichtner bus_dmamap_unload(tag, map);
787fb578518SFranco Fichtner bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE,
788fb578518SFranco Fichtner netmap_dmamap_cb, NULL, BUS_DMA_NOWAIT);
789fb578518SFranco Fichtner }
790fb578518SFranco Fichtner }
791fb578518SFranco Fichtner
792fb578518SFranco Fichtner /*
793fb578518SFranco Fichtner * functions to map NIC to KRING indexes (n2k) and vice versa (k2n)
794fb578518SFranco Fichtner */
795fb578518SFranco Fichtner static inline int
netmap_idx_n2k(struct netmap_kring * kr,int idx)796fb578518SFranco Fichtner netmap_idx_n2k(struct netmap_kring *kr, int idx)
797fb578518SFranco Fichtner {
798fb578518SFranco Fichtner int n = kr->nkr_num_slots;
799fb578518SFranco Fichtner idx += kr->nkr_hwofs;
800fb578518SFranco Fichtner if (idx < 0)
801fb578518SFranco Fichtner return idx + n;
802fb578518SFranco Fichtner else if (idx < n)
803fb578518SFranco Fichtner return idx;
804fb578518SFranco Fichtner else
805fb578518SFranco Fichtner return idx - n;
806fb578518SFranco Fichtner }
807fb578518SFranco Fichtner
808fb578518SFranco Fichtner
809fb578518SFranco Fichtner static inline int
netmap_idx_k2n(struct netmap_kring * kr,int idx)810fb578518SFranco Fichtner netmap_idx_k2n(struct netmap_kring *kr, int idx)
811fb578518SFranco Fichtner {
812fb578518SFranco Fichtner int n = kr->nkr_num_slots;
813fb578518SFranco Fichtner idx -= kr->nkr_hwofs;
814fb578518SFranco Fichtner if (idx < 0)
815fb578518SFranco Fichtner return idx + n;
816fb578518SFranco Fichtner else if (idx < n)
817fb578518SFranco Fichtner return idx;
818fb578518SFranco Fichtner else
819fb578518SFranco Fichtner return idx - n;
820fb578518SFranco Fichtner }
821fb578518SFranco Fichtner
822fb578518SFranco Fichtner
823fb578518SFranco Fichtner /* Entries of the look-up table. */
824fb578518SFranco Fichtner struct lut_entry {
825fb578518SFranco Fichtner void *vaddr; /* virtual address. */
826fb578518SFranco Fichtner vm_paddr_t paddr; /* physical address. */
827fb578518SFranco Fichtner };
828fb578518SFranco Fichtner
829fb578518SFranco Fichtner struct netmap_obj_pool;
830fb578518SFranco Fichtner extern struct lut_entry *netmap_buffer_lut;
831fb578518SFranco Fichtner #define NMB_VA(i) (netmap_buffer_lut[i].vaddr)
832fb578518SFranco Fichtner #define NMB_PA(i) (netmap_buffer_lut[i].paddr)
833fb578518SFranco Fichtner
834fb578518SFranco Fichtner /*
835fb578518SFranco Fichtner * NMB return the virtual address of a buffer (buffer 0 on bad index)
836fb578518SFranco Fichtner * PNMB also fills the physical address
837fb578518SFranco Fichtner */
838fb578518SFranco Fichtner static inline void *
NMB(struct netmap_slot * slot)839fb578518SFranco Fichtner NMB(struct netmap_slot *slot)
840fb578518SFranco Fichtner {
841fb578518SFranco Fichtner uint32_t i = slot->buf_idx;
842fb578518SFranco Fichtner return (unlikely(i >= netmap_total_buffers)) ? NMB_VA(0) : NMB_VA(i);
843fb578518SFranco Fichtner }
844fb578518SFranco Fichtner
845fb578518SFranco Fichtner static inline void *
PNMB(struct netmap_slot * slot,uint64_t * pp)846fb578518SFranco Fichtner PNMB(struct netmap_slot *slot, uint64_t *pp)
847fb578518SFranco Fichtner {
848fb578518SFranco Fichtner uint32_t i = slot->buf_idx;
849fb578518SFranco Fichtner void *ret = (i >= netmap_total_buffers) ? NMB_VA(0) : NMB_VA(i);
850fb578518SFranco Fichtner
851fb578518SFranco Fichtner *pp = (i >= netmap_total_buffers) ? NMB_PA(0) : NMB_PA(i);
852fb578518SFranco Fichtner return ret;
853fb578518SFranco Fichtner }
854fb578518SFranco Fichtner
855fb578518SFranco Fichtner /* Generic version of NMB, which uses device-specific memory. */
856fb578518SFranco Fichtner static inline void *
BDG_NMB(struct netmap_adapter * na,struct netmap_slot * slot)857fb578518SFranco Fichtner BDG_NMB(struct netmap_adapter *na, struct netmap_slot *slot)
858fb578518SFranco Fichtner {
859fb578518SFranco Fichtner struct lut_entry *lut = na->na_lut;
860fb578518SFranco Fichtner uint32_t i = slot->buf_idx;
861fb578518SFranco Fichtner return (unlikely(i >= na->na_lut_objtotal)) ?
862fb578518SFranco Fichtner lut[0].vaddr : lut[i].vaddr;
863fb578518SFranco Fichtner }
864fb578518SFranco Fichtner
865fb578518SFranco Fichtner /* default functions to handle rx/tx interrupts */
866fb578518SFranco Fichtner int netmap_rx_irq(struct ifnet *, u_int, u_int *);
867fb578518SFranco Fichtner #define netmap_tx_irq(_n, _q) netmap_rx_irq(_n, _q, NULL)
868fb578518SFranco Fichtner int netmap_common_irq(struct ifnet *, u_int, u_int *work_done);
869fb578518SFranco Fichtner
870fb578518SFranco Fichtner
871fb578518SFranco Fichtner void netmap_txsync_to_host(struct netmap_adapter *na);
872fb578518SFranco Fichtner void netmap_disable_all_rings(struct ifnet *);
873fb578518SFranco Fichtner void netmap_enable_all_rings(struct ifnet *);
874fb578518SFranco Fichtner void netmap_disable_ring(struct netmap_kring *kr);
875fb578518SFranco Fichtner
876fb578518SFranco Fichtner
877fb578518SFranco Fichtner /* Structure associated to each thread which registered an interface.
878fb578518SFranco Fichtner *
879fb578518SFranco Fichtner * The first 4 fields of this structure are written by NIOCREGIF and
880fb578518SFranco Fichtner * read by poll() and NIOC?XSYNC.
881fb578518SFranco Fichtner * There is low contention among writers (actually, a correct user program
882fb578518SFranco Fichtner * should have no contention among writers) and among writers and readers,
883fb578518SFranco Fichtner * so we use a single global lock to protect the structure initialization.
884fb578518SFranco Fichtner * Since initialization involves the allocation of memory, we reuse the memory
885fb578518SFranco Fichtner * allocator lock.
886fb578518SFranco Fichtner * Read access to the structure is lock free. Readers must check that
887fb578518SFranco Fichtner * np_nifp is not NULL before using the other fields.
888fb578518SFranco Fichtner * If np_nifp is NULL initialization has not been performed, so they should
889fb578518SFranco Fichtner * return an error to userlevel.
890fb578518SFranco Fichtner *
891fb578518SFranco Fichtner * The ref_done field is used to regulate access to the refcount in the
892fb578518SFranco Fichtner * memory allocator. The refcount must be incremented at most once for
893fb578518SFranco Fichtner * each open("/dev/netmap"). The increment is performed by the first
894fb578518SFranco Fichtner * function that calls netmap_get_memory() (currently called by
895fb578518SFranco Fichtner * mmap(), NIOCGINFO and NIOCREGIF).
896fb578518SFranco Fichtner * If the refcount is incremented, it is then decremented when the
897fb578518SFranco Fichtner * private structure is destroyed.
898fb578518SFranco Fichtner */
899fb578518SFranco Fichtner struct netmap_priv_d {
900fb578518SFranco Fichtner struct netmap_if * volatile np_nifp; /* netmap if descriptor. */
901fb578518SFranco Fichtner
902fb578518SFranco Fichtner struct netmap_adapter *np_na;
903fb578518SFranco Fichtner int np_ringid; /* from the ioctl */
904fb578518SFranco Fichtner u_int np_qfirst, np_qlast; /* range of rings to scan */
905fb578518SFranco Fichtner uint16_t np_txpoll;
906fb578518SFranco Fichtner
907fb578518SFranco Fichtner struct netmap_mem_d *np_mref; /* use with NMG_LOCK held */
908fb578518SFranco Fichtner /* np_refcount is only used on FreeBSD */
909fb578518SFranco Fichtner int np_refcount; /* use with NMG_LOCK held */
910fb578518SFranco Fichtner };
911fb578518SFranco Fichtner
912fb578518SFranco Fichtner
913fb578518SFranco Fichtner /*
914fb578518SFranco Fichtner * generic netmap emulation for devices that do not have
915fb578518SFranco Fichtner * native netmap support.
916fb578518SFranco Fichtner * XXX generic_netmap_register() is only exported to implement
917fb578518SFranco Fichtner * nma_is_generic().
918fb578518SFranco Fichtner */
919fb578518SFranco Fichtner int generic_netmap_register(struct netmap_adapter *na, int enable);
920fb578518SFranco Fichtner int generic_netmap_attach(struct ifnet *ifp);
921fb578518SFranco Fichtner
922fb578518SFranco Fichtner int netmap_catch_rx(struct netmap_adapter *na, int intercept);
92373029d08SFranco Fichtner void generic_rx_handler(struct ifnet *ifp, struct mbuf *m,
92473029d08SFranco Fichtner const struct pktinfo *, int);
925fb578518SFranco Fichtner void netmap_catch_packet_steering(struct netmap_generic_adapter *na, int enable);
926fb578518SFranco Fichtner int generic_xmit_frame(struct ifnet *ifp, struct mbuf *m, void *addr, u_int len, u_int ring_nr);
927fb578518SFranco Fichtner int generic_find_num_desc(struct ifnet *ifp, u_int *tx, u_int *rx);
928fb578518SFranco Fichtner void generic_find_num_queues(struct ifnet *ifp, u_int *txq, u_int *rxq);
929fb578518SFranco Fichtner
930fb578518SFranco Fichtner static __inline int
nma_is_generic(struct netmap_adapter * na)931fb578518SFranco Fichtner nma_is_generic(struct netmap_adapter *na)
932fb578518SFranco Fichtner {
933fb578518SFranco Fichtner return na->nm_register == generic_netmap_register;
934fb578518SFranco Fichtner }
935fb578518SFranco Fichtner
936fb578518SFranco Fichtner /*
937fb578518SFranco Fichtner * netmap_mitigation API. This is used by the generic adapter
938fb578518SFranco Fichtner * to reduce the number of interrupt requests/selwakeup
939fb578518SFranco Fichtner * to clients on incoming packets.
940fb578518SFranco Fichtner */
941fb578518SFranco Fichtner void netmap_mitigation_init(struct netmap_generic_adapter *na);
942fb578518SFranco Fichtner void netmap_mitigation_start(struct netmap_generic_adapter *na);
943fb578518SFranco Fichtner void netmap_mitigation_restart(struct netmap_generic_adapter *na);
944fb578518SFranco Fichtner int netmap_mitigation_active(struct netmap_generic_adapter *na);
945fb578518SFranco Fichtner void netmap_mitigation_cleanup(struct netmap_generic_adapter *na);
946fb578518SFranco Fichtner
947fb578518SFranco Fichtner // int generic_timer_handler(struct hrtimer *t);
948fb578518SFranco Fichtner
949fb578518SFranco Fichtner #endif /* _NET_NETMAP_KERN_H_ */
950