2 * Copyright (C) 2011-2013 Matteo Landi, Luigi Rizzo. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * The header contains the definitions of constants and function
30 * prototypes used only in kernelspace.
33 #ifndef _NET_NETMAP_KERN_H_
34 #define _NET_NETMAP_KERN_H_
36 #if defined(__FreeBSD__)
38 #define likely(x) __builtin_expect(!!(x), 1)
39 #define unlikely(x) __builtin_expect(!!(x), 0)
41 #define NM_LOCK_T struct mtx
42 #define NM_RWLOCK_T struct rwlock
43 #define NM_SELINFO_T struct selinfo
44 #define MBUF_LEN(m) ((m)->m_pkthdr.len)
45 #define NM_SEND_UP(ifp, m) ((ifp)->if_input)(ifp, m)
49 #define NM_LOCK_T safe_spinlock_t // see bsd_glue.h
50 #define NM_RWLOCK_T safe_spinlock_t // see bsd_glue.h
51 #define NM_SELINFO_T wait_queue_head_t
52 #define MBUF_LEN(m) ((m)->len)
53 #define NM_SEND_UP(ifp, m) netif_rx(m)
60 * IFCAP_NETMAP goes into net_device's priv_flags (if_capenable).
61 * This was 16 bits up to linux 2.6.36, so we need a 16 bit value on older
62 * platforms and tolerate the clash with IFF_DYNAMIC and IFF_BRIDGE_PORT.
63 * For the 32-bit value, 0x100000 has no clashes until at least 3.5.1
65 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
66 #define IFCAP_NETMAP 0x8000
68 #define IFCAP_NETMAP 0x200000
71 #elif defined (__APPLE__)
73 #warning apple support is incomplete.
74 #define likely(x) __builtin_expect(!!(x), 1)
75 #define unlikely(x) __builtin_expect(!!(x), 0)
76 #define NM_LOCK_T IOLock *
77 #define NM_SELINFO_T struct selinfo
78 #define MBUF_LEN(m) ((m)->m_pkthdr.len)
79 #define NM_SEND_UP(ifp, m) ((ifp)->if_input)(ifp, m)
83 #error unsupported platform
85 #endif /* end - platform-specific code */
87 #define ND(format, ...)
88 #define D(format, ...) \
90 struct timeval __xxts; \
92 printf("%03d.%06d %s [%d] " format "\n", \
93 (int)__xxts.tv_sec % 1000, (int)__xxts.tv_usec, \
94 __FUNCTION__, __LINE__, ##__VA_ARGS__); \
97 /* rate limited, lps indicates how many per second */
98 #define RD(lps, format, ...) \
100 static int t0, __cnt; \
101 if (t0 != time_second) { \
106 D(format, ##__VA_ARGS__); \
109 struct netmap_adapter;
112 struct netmap_priv_d;
115 * private, kernel view of a ring. Keeps track of the status of
116 * a ring across system calls.
118 * nr_hwcur index of the next buffer to refill.
119 * It corresponds to ring->cur - ring->reserved
121 * nr_hwavail the number of slots "owned" by userspace.
122 * nr_hwavail =:= ring->avail + ring->reserved
124 * The indexes in the NIC and netmap rings are offset by nkr_hwofs slots.
125 * This is so that, on a reset, buffers owned by userspace are not
126 * modified by the kernel. In particular:
127 * RX rings: the next empty buffer (hwcur + hwavail + hwofs) coincides with
128 * the next empty buffer as known by the hardware (next_to_check or so).
129 * TX rings: hwcur + hwofs coincides with next_to_send
131 * For received packets, slot->flags is set to nkr_slot_flags
132 * so we can provide a proper initial value (e.g. set NS_FORWARD
133 * when operating in 'transparent' mode).
135 struct netmap_kring {
136 struct netmap_ring *ring;
139 u_int nr_kflags; /* private driver flags */
140 #define NKR_PENDINTR 0x1 // Pending interrupt.
143 uint16_t nkr_slot_flags; /* initial value for flags */
144 int nkr_hwofs; /* offset between NIC and netmap ring */
145 struct netmap_adapter *na;
146 struct nm_bdg_fwd *nkr_ft;
147 NM_SELINFO_T si; /* poll/select wait queue */
148 NM_LOCK_T q_lock; /* used if no device lock available */
149 } __attribute__((__aligned__(64)));
152 * This struct extends the 'struct adapter' (or
153 * equivalent) device descriptor. It contains all fields needed to
154 * support netmap operation.
156 struct netmap_adapter {
158 * On linux we do not have a good way to tell if an interface
159 * is netmap-capable. So we use the following trick:
160 * NA(ifp) points here, and the first entry (which hopefully
161 * always exists and is at least 32 bits) contains a magic
162 * value which we can use to detect that the interface is good.
165 uint32_t na_flags; /* future place for IFCAP_NETMAP */
166 #define NAF_SKIP_INTR 1 /* use the regular interrupt handler.
167 * useful during initialization
169 #define NAF_SW_ONLY 2 /* forward packets only to sw adapter */
170 int refcount; /* number of user-space descriptors using this
171 interface, which is equal to the number of
172 struct netmap_if objs in the mapped region. */
174 * The selwakeup in the interrupt thread can use per-ring
175 * and/or global wait queues. We track how many clients
176 * of each type we have so we can optimize the drivers,
177 * and especially avoid huge contention on the locks.
179 int na_single; /* threads attached to a single hw queue */
180 int na_multi; /* threads attached to multiple hw queues */
182 int separate_locks; /* set if the interface suports different
183 locks for rx, tx and core. */
185 u_int num_rx_rings; /* number of adapter receive rings */
186 u_int num_tx_rings; /* number of adapter transmit rings */
188 u_int num_tx_desc; /* number of descriptor in each queue */
191 /* tx_rings and rx_rings are private but allocated
192 * as a contiguous chunk of memory. Each array has
193 * N+1 entries, for the adapter queues and for the host queue.
195 struct netmap_kring *tx_rings; /* array of TX rings. */
196 struct netmap_kring *rx_rings; /* array of RX rings. */
198 NM_SELINFO_T tx_si, rx_si; /* global wait queues */
200 /* copy of if_qflush and if_transmit pointers, to intercept
201 * packets from the network stack when netmap is active.
203 int (*if_transmit)(struct ifnet *, struct mbuf *);
205 /* references to the ifnet and device routines, used by
206 * the generic netmap functions.
208 struct ifnet *ifp; /* adapter is ifp->if_softc */
210 NM_LOCK_T core_lock; /* used if no device lock available */
212 int (*nm_register)(struct ifnet *, int onoff);
213 void (*nm_lock)(struct ifnet *, int what, u_int ringid);
214 int (*nm_txsync)(struct ifnet *, u_int ring, int lock);
215 int (*nm_rxsync)(struct ifnet *, u_int ring, int lock);
216 /* return configuration information */
217 int (*nm_config)(struct ifnet *, u_int *txr, u_int *txd,
218 u_int *rxr, u_int *rxd);
223 * bdg_port is the port number used in the bridge;
224 * na_bdg_refcount is a refcount used for bridge ports,
225 * when it goes to 0 we can detach+free this port
226 * (a bridge port is always attached if it exists;
227 * it is not always registered)
228 * na_bdg points to the bridge this NA is attached to.
232 struct nm_bridge *na_bdg;
233 /* When we attach a physical interface to the bridge, we
234 * allow the controlling process to terminate, so we need
235 * a place to store the netmap_priv_d data structure.
236 * This is only done when physical interfaces are attached to a bridge.
238 struct netmap_priv_d *na_kpriv;
240 struct net_device_ops nm_ndo;
245 * The combination of "enable" (ifp->if_capenable & IFCAP_NETMAP)
246 * and refcount gives the status of the interface, namely:
248 * enable refcount Status
250 * FALSE 0 normal operation
251 * FALSE != 0 -- (impossible)
253 * TRUE 0 being deleted.
256 #define NETMAP_DELETING(_na) ( ((_na)->refcount == 0) && \
257 ( (_na)->ifp->if_capenable & IFCAP_NETMAP) )
260 * parameters for (*nm_lock)(adapter, what, index)
264 NETMAP_CORE_LOCK, NETMAP_CORE_UNLOCK,
265 NETMAP_TX_LOCK, NETMAP_TX_UNLOCK,
266 NETMAP_RX_LOCK, NETMAP_RX_UNLOCK,
268 #define NETMAP_REG_LOCK NETMAP_CORE_LOCK
269 #define NETMAP_REG_UNLOCK NETMAP_CORE_UNLOCK
271 NETMAP_REG_LOCK, NETMAP_REG_UNLOCK
275 /* How to handle locking support in netmap_rx_irq/netmap_tx_irq */
276 #define NETMAP_LOCKED_ENTER 0x10000000 /* already locked on enter */
277 #define NETMAP_LOCKED_EXIT 0x20000000 /* keep locked on exit */
280 * The following are support routines used by individual drivers to
281 * support netmap operation.
283 * netmap_attach() initializes a struct netmap_adapter, allocating the
284 * struct netmap_ring's and the struct selinfo.
286 * netmap_detach() frees the memory allocated by netmap_attach().
288 * netmap_start() replaces the if_transmit routine of the interface,
289 * and is used to intercept packets coming from the stack.
291 * netmap_load_map/netmap_reload_map are helper routines to set/reset
292 * the dmamap for a packet buffer
294 * netmap_reset() is a helper routine to be called in the driver
295 * when reinitializing a ring.
297 int netmap_attach(struct netmap_adapter *, int);
298 void netmap_detach(struct ifnet *);
299 int netmap_start(struct ifnet *, struct mbuf *);
300 enum txrx { NR_RX = 0, NR_TX = 1 };
301 struct netmap_slot *netmap_reset(struct netmap_adapter *na,
302 enum txrx tx, int n, u_int new_cur);
303 int netmap_ring_reinit(struct netmap_kring *);
306 * The following bridge-related interfaces are used by other kernel modules
307 * In the version that only supports unicast or broadcast, the lookup
308 * function can return 0 .. NM_BDG_MAXPORTS-1 for regular ports,
309 * NM_BDG_MAXPORTS for broadcast, NM_BDG_MAXPORTS+1 for unknown.
310 * XXX in practice "unknown" might be handled same as broadcast.
312 typedef u_int (*bdg_lookup_fn_t)(char *buf, u_int len, uint8_t *ring_nr,
313 struct netmap_adapter *);
314 int netmap_bdg_ctl(struct nmreq *nmr, bdg_lookup_fn_t func);
315 u_int netmap_bdg_learning(char *, u_int, uint8_t *, struct netmap_adapter *);
316 #define NM_NAME "vale" /* prefix for the bridge port name */
317 #define NM_BDG_MAXPORTS 254 /* up to 32 for bitmap, 254 ok otherwise */
318 #define NM_BDG_BROADCAST NM_BDG_MAXPORTS
319 #define NM_BDG_NOPORT (NM_BDG_MAXPORTS+1)
321 extern u_int netmap_buf_size;
322 #define NETMAP_BUF_SIZE netmap_buf_size // XXX remove
323 extern int netmap_mitigate;
324 extern int netmap_no_pendintr;
325 extern u_int netmap_total_buffers;
326 extern char *netmap_buffer_base;
327 extern int netmap_verbose; // XXX debugging
328 enum { /* verbose flags */
329 NM_VERB_ON = 1, /* generic verbose */
330 NM_VERB_HOST = 0x2, /* verbose host stack */
331 NM_VERB_RXSYNC = 0x10, /* verbose on rxsync/txsync */
332 NM_VERB_TXSYNC = 0x20,
333 NM_VERB_RXINTR = 0x100, /* verbose on rx/tx intr (driver) */
334 NM_VERB_TXINTR = 0x200,
335 NM_VERB_NIC_RXSYNC = 0x1000, /* verbose on rx/tx intr (driver) */
336 NM_VERB_NIC_TXSYNC = 0x2000,
340 * NA returns a pointer to the struct netmap adapter from the ifp,
341 * WNA is used to write it.
342 * SWNA() is used for the "host stack" endpoint associated
343 * to an interface. It is allocated together with the main NA(),
344 * as an array of two objects.
347 #define WNA(_ifp) (_ifp)->if_pspare[0]
349 #define NA(_ifp) ((struct netmap_adapter *)WNA(_ifp))
350 #define SWNA(_ifp) (NA(_ifp) + 1)
353 * Macros to determine if an interface is netmap capable or netmap enabled.
354 * See the magic field in struct netmap_adapter.
358 * on FreeBSD just use if_capabilities and if_capenable.
360 #define NETMAP_CAPABLE(ifp) (NA(ifp) && \
361 (ifp)->if_capabilities & IFCAP_NETMAP )
363 #define NETMAP_SET_CAPABLE(ifp) \
364 (ifp)->if_capabilities |= IFCAP_NETMAP
370 * we check if NA(ifp) is set and its first element has a related
371 * magic value. The capenable is within the struct netmap_adapter.
373 #define NETMAP_MAGIC 0x52697a7a
375 #define NETMAP_CAPABLE(ifp) (NA(ifp) && \
376 ((uint32_t)(uintptr_t)NA(ifp) ^ NA(ifp)->magic) == NETMAP_MAGIC )
378 #define NETMAP_SET_CAPABLE(ifp) \
379 NA(ifp)->magic = ((uint32_t)(uintptr_t)NA(ifp)) ^ NETMAP_MAGIC
384 /* Callback invoked by the dma machinery after a successfull dmamap_load */
385 static void netmap_dmamap_cb(__unused void *arg,
386 __unused bus_dma_segment_t * segs, __unused int nseg, __unused int error)
390 /* bus_dmamap_load wrapper: call aforementioned function if map != NULL.
391 * XXX can we do it without a callback ?
394 netmap_load_map(bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
397 bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE,
398 netmap_dmamap_cb, NULL, BUS_DMA_NOWAIT);
401 /* update the map when a buffer changes. */
403 netmap_reload_map(bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
406 bus_dmamap_unload(tag, map);
407 bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE,
408 netmap_dmamap_cb, NULL, BUS_DMA_NOWAIT);
414 * XXX How do we redefine these functions:
417 * dma_map_single(&pdev->dev, virt_addr, len, direction)
418 * dma_unmap_single(&adapter->pdev->dev, phys_addr, len, direction
419 * The len can be implicit (on netmap it is NETMAP_BUF_SIZE)
420 * unfortunately the direction is not, so we need to change
421 * something to have a cross API
423 #define netmap_load_map(_t, _m, _b)
424 #define netmap_reload_map(_t, _m, _b)
426 struct e1000_buffer *buffer_info = &tx_ring->buffer_info[l];
427 /* set time_stamp *before* dma to help avoid a possible race */
428 buffer_info->time_stamp = jiffies;
429 buffer_info->mapped_as_page = false;
430 buffer_info->length = len;
431 //buffer_info->next_to_watch = l;
433 dma_unmap_single(&adapter->pdev->dev, buffer_info->dma,
434 NETMAP_BUF_SIZE, DMA_TO_DEVICE);
435 buffer_info->dma = dma_map_single(&adapter->pdev->dev,
436 addr, NETMAP_BUF_SIZE, DMA_TO_DEVICE);
438 if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)) {
439 D("dma mapping error");
440 /* goto dma_error; See e1000_put_txbuf() */
443 tx_desc->buffer_addr = htole64(buffer_info->dma); //XXX
448 * The bus_dmamap_sync() can be one of wmb() or rmb() depending on direction.
450 #define bus_dmamap_sync(_a, _b, _c)
455 * functions to map NIC to KRING indexes (n2k) and vice versa (k2n)
458 netmap_idx_n2k(struct netmap_kring *kr, int idx)
460 int n = kr->nkr_num_slots;
461 idx += kr->nkr_hwofs;
472 netmap_idx_k2n(struct netmap_kring *kr, int idx)
474 int n = kr->nkr_num_slots;
475 idx -= kr->nkr_hwofs;
485 /* Entries of the look-up table. */
487 void *vaddr; /* virtual address. */
488 vm_paddr_t paddr; /* physical address. */
491 struct netmap_obj_pool;
492 extern struct lut_entry *netmap_buffer_lut;
493 #define NMB_VA(i) (netmap_buffer_lut[i].vaddr)
494 #define NMB_PA(i) (netmap_buffer_lut[i].paddr)
497 * NMB return the virtual address of a buffer (buffer 0 on bad index)
498 * PNMB also fills the physical address
501 NMB(struct netmap_slot *slot)
503 uint32_t i = slot->buf_idx;
504 return (unlikely(i >= netmap_total_buffers)) ? NMB_VA(0) : NMB_VA(i);
508 PNMB(struct netmap_slot *slot, uint64_t *pp)
510 uint32_t i = slot->buf_idx;
511 void *ret = (i >= netmap_total_buffers) ? NMB_VA(0) : NMB_VA(i);
513 *pp = (i >= netmap_total_buffers) ? NMB_PA(0) : NMB_PA(i);
517 /* default functions to handle rx/tx interrupts */
518 int netmap_rx_irq(struct ifnet *, int, int *);
519 #define netmap_tx_irq(_n, _q) netmap_rx_irq(_n, _q, NULL)
521 #endif /* _NET_NETMAP_KERN_H_ */