]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/dev/netmap/netmap_kern.h
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / dev / netmap / netmap_kern.h
1 /*
2  * Copyright (C) 2011-2013 Matteo Landi, Luigi Rizzo. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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
23  * SUCH DAMAGE.
24  */
25
26 /*
27  * $FreeBSD$
28  *
29  * The header contains the definitions of constants and function
30  * prototypes used only in kernelspace.
31  */
32
33 #ifndef _NET_NETMAP_KERN_H_
34 #define _NET_NETMAP_KERN_H_
35
36 #if defined(__FreeBSD__)
37
38 #define likely(x)       __builtin_expect(!!(x), 1)
39 #define unlikely(x)     __builtin_expect(!!(x), 0)
40
41 #define NM_LOCK_T       struct mtx
42 #define NM_SELINFO_T    struct selinfo
43 #define MBUF_LEN(m)     ((m)->m_pkthdr.len)
44 #define NM_SEND_UP(ifp, m)      ((ifp)->if_input)(ifp, m)
45
46 #elif defined (linux)
47
48 #define NM_LOCK_T       safe_spinlock_t // see bsd_glue.h
49 #define NM_SELINFO_T    wait_queue_head_t
50 #define MBUF_LEN(m)     ((m)->len)
51 #define NM_SEND_UP(ifp, m)      netif_rx(m)
52
53 #ifndef DEV_NETMAP
54 #define DEV_NETMAP
55 #endif
56
57 /*
58  * IFCAP_NETMAP goes into net_device's priv_flags (if_capenable).
59  * This was 16 bits up to linux 2.6.36, so we need a 16 bit value on older
60  * platforms and tolerate the clash with IFF_DYNAMIC and IFF_BRIDGE_PORT.
61  * For the 32-bit value, 0x100000 has no clashes until at least 3.5.1
62  */
63 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
64 #define IFCAP_NETMAP    0x8000
65 #else
66 #define IFCAP_NETMAP    0x100000
67 #endif
68
69 #elif defined (__APPLE__)
70
71 #warning apple support is incomplete.
72 #define likely(x)       __builtin_expect(!!(x), 1)
73 #define unlikely(x)     __builtin_expect(!!(x), 0)
74 #define NM_LOCK_T       IOLock *
75 #define NM_SELINFO_T    struct selinfo
76 #define MBUF_LEN(m)     ((m)->m_pkthdr.len)
77 #define NM_SEND_UP(ifp, m)      ((ifp)->if_input)(ifp, m)
78
79 #else
80
81 #error unsupported platform
82
83 #endif /* end - platform-specific code */
84
85 #define ND(format, ...)
86 #define D(format, ...)                                          \
87         do {                                                    \
88                 struct timeval __xxts;                          \
89                 microtime(&__xxts);                             \
90                 printf("%03d.%06d %s [%d] " format "\n",        \
91                 (int)__xxts.tv_sec % 1000, (int)__xxts.tv_usec, \
92                 __FUNCTION__, __LINE__, ##__VA_ARGS__);         \
93         } while (0)
94
95 /* rate limited, lps indicates how many per second */
96 #define RD(lps, format, ...)                                    \
97         do {                                                    \
98                 static int t0, __cnt;                           \
99                 if (t0 != time_second) {                        \
100                         t0 = time_second;                       \
101                         __cnt = 0;                              \
102                 }                                               \
103                 if (__cnt++ < lps)                              \
104                         D(format, ##__VA_ARGS__);               \
105         } while (0)
106
107 struct netmap_adapter;
108
109 /*
110  * private, kernel view of a ring. Keeps track of the status of
111  * a ring across system calls.
112  *
113  *      nr_hwcur        index of the next buffer to refill.
114  *                      It corresponds to ring->cur - ring->reserved
115  *
116  *      nr_hwavail      the number of slots "owned" by userspace.
117  *                      nr_hwavail =:= ring->avail + ring->reserved
118  *
119  * The indexes in the NIC and netmap rings are offset by nkr_hwofs slots.
120  * This is so that, on a reset, buffers owned by userspace are not
121  * modified by the kernel. In particular:
122  * RX rings: the next empty buffer (hwcur + hwavail + hwofs) coincides with
123  *      the next empty buffer as known by the hardware (next_to_check or so).
124  * TX rings: hwcur + hwofs coincides with next_to_send
125  *
126  * For received packets, slot->flags is set to nkr_slot_flags
127  * so we can provide a proper initial value (e.g. set NS_FORWARD
128  * when operating in 'transparent' mode).
129  */
130 struct netmap_kring {
131         struct netmap_ring *ring;
132         u_int nr_hwcur;
133         int nr_hwavail;
134         u_int nr_kflags;        /* private driver flags */
135 #define NKR_PENDINTR    0x1     // Pending interrupt.
136         u_int nkr_num_slots;
137
138         uint16_t        nkr_slot_flags; /* initial value for flags */
139         int     nkr_hwofs;      /* offset between NIC and netmap ring */
140         struct netmap_adapter *na;
141         NM_SELINFO_T si;        /* poll/select wait queue */
142         NM_LOCK_T q_lock;       /* used if no device lock available */
143 } __attribute__((__aligned__(64)));
144
145 /*
146  * This struct extends the 'struct adapter' (or
147  * equivalent) device descriptor. It contains all fields needed to
148  * support netmap operation.
149  */
150 struct netmap_adapter {
151         /*
152          * On linux we do not have a good way to tell if an interface
153          * is netmap-capable. So we use the following trick:
154          * NA(ifp) points here, and the first entry (which hopefully
155          * always exists and is at least 32 bits) contains a magic
156          * value which we can use to detect that the interface is good.
157          */
158         uint32_t magic;
159         uint32_t na_flags;      /* future place for IFCAP_NETMAP */
160 #define NAF_SKIP_INTR   1       /* use the regular interrupt handler.
161                                  * useful during initialization
162                                  */
163         int refcount; /* number of user-space descriptors using this
164                          interface, which is equal to the number of
165                          struct netmap_if objs in the mapped region. */
166         /*
167          * The selwakeup in the interrupt thread can use per-ring
168          * and/or global wait queues. We track how many clients
169          * of each type we have so we can optimize the drivers,
170          * and especially avoid huge contention on the locks.
171          */
172         int na_single;  /* threads attached to a single hw queue */
173         int na_multi;   /* threads attached to multiple hw queues */
174
175         int separate_locks; /* set if the interface suports different
176                                locks for rx, tx and core. */
177
178         u_int num_rx_rings; /* number of adapter receive rings */
179         u_int num_tx_rings; /* number of adapter transmit rings */
180
181         u_int num_tx_desc; /* number of descriptor in each queue */
182         u_int num_rx_desc;
183
184         /* tx_rings and rx_rings are private but allocated
185          * as a contiguous chunk of memory. Each array has
186          * N+1 entries, for the adapter queues and for the host queue.
187          */
188         struct netmap_kring *tx_rings; /* array of TX rings. */
189         struct netmap_kring *rx_rings; /* array of RX rings. */
190
191         NM_SELINFO_T tx_si, rx_si;      /* global wait queues */
192
193         /* copy of if_qflush and if_transmit pointers, to intercept
194          * packets from the network stack when netmap is active.
195          */
196         int     (*if_transmit)(struct ifnet *, struct mbuf *);
197
198         /* references to the ifnet and device routines, used by
199          * the generic netmap functions.
200          */
201         struct ifnet *ifp; /* adapter is ifp->if_softc */
202
203         NM_LOCK_T core_lock;    /* used if no device lock available */
204
205         int (*nm_register)(struct ifnet *, int onoff);
206         void (*nm_lock)(struct ifnet *, int what, u_int ringid);
207         int (*nm_txsync)(struct ifnet *, u_int ring, int lock);
208         int (*nm_rxsync)(struct ifnet *, u_int ring, int lock);
209         /* return configuration information */
210         int (*nm_config)(struct ifnet *, u_int *txr, u_int *txd,
211                                         u_int *rxr, u_int *rxd);
212
213         /*
214          * Bridge support:
215          *
216          * bdg_port is the port number used in the bridge;
217          * na_bdg_refcount is a refcount used for bridge ports,
218          *      when it goes to 0 we can detach+free this port
219          *      (a bridge port is always attached if it exists;
220          *      it is not always registered)
221          */
222         int bdg_port;
223         int na_bdg_refcount;
224
225 #ifdef linux
226         struct net_device_ops nm_ndo;
227 #endif /* linux */
228 };
229
230 /*
231  * The combination of "enable" (ifp->if_capenable & IFCAP_NETMAP)
232  * and refcount gives the status of the interface, namely:
233  *
234  *      enable  refcount        Status
235  *
236  *      FALSE   0               normal operation
237  *      FALSE   != 0            -- (impossible)
238  *      TRUE    1               netmap mode
239  *      TRUE    0               being deleted.
240  */
241
242 #define NETMAP_DELETING(_na)  (  ((_na)->refcount == 0) &&      \
243         ( (_na)->ifp->if_capenable & IFCAP_NETMAP) )
244
245 /*
246  * parameters for (*nm_lock)(adapter, what, index)
247  */
248 enum {
249         NETMAP_NO_LOCK = 0,
250         NETMAP_CORE_LOCK, NETMAP_CORE_UNLOCK,
251         NETMAP_TX_LOCK, NETMAP_TX_UNLOCK,
252         NETMAP_RX_LOCK, NETMAP_RX_UNLOCK,
253 #ifdef __FreeBSD__
254 #define NETMAP_REG_LOCK         NETMAP_CORE_LOCK
255 #define NETMAP_REG_UNLOCK       NETMAP_CORE_UNLOCK
256 #else
257         NETMAP_REG_LOCK, NETMAP_REG_UNLOCK
258 #endif
259 };
260
261 /* How to handle locking support in netmap_rx_irq/netmap_tx_irq */
262 #define NETMAP_LOCKED_ENTER     0x10000000      /* already locked on enter */
263 #define NETMAP_LOCKED_EXIT      0x20000000      /* keep locked on exit */
264
265 /*
266  * The following are support routines used by individual drivers to
267  * support netmap operation.
268  *
269  * netmap_attach() initializes a struct netmap_adapter, allocating the
270  *      struct netmap_ring's and the struct selinfo.
271  *
272  * netmap_detach() frees the memory allocated by netmap_attach().
273  *
274  * netmap_start() replaces the if_transmit routine of the interface,
275  *      and is used to intercept packets coming from the stack.
276  *
277  * netmap_load_map/netmap_reload_map are helper routines to set/reset
278  *      the dmamap for a packet buffer
279  *
280  * netmap_reset() is a helper routine to be called in the driver
281  *      when reinitializing a ring.
282  */
283 int netmap_attach(struct netmap_adapter *, int);
284 void netmap_detach(struct ifnet *);
285 int netmap_start(struct ifnet *, struct mbuf *);
286 enum txrx { NR_RX = 0, NR_TX = 1 };
287 struct netmap_slot *netmap_reset(struct netmap_adapter *na,
288         enum txrx tx, int n, u_int new_cur);
289 int netmap_ring_reinit(struct netmap_kring *);
290
291 extern u_int netmap_buf_size;
292 #define NETMAP_BUF_SIZE netmap_buf_size // XXX remove
293 extern int netmap_mitigate;
294 extern int netmap_no_pendintr;
295 extern u_int netmap_total_buffers;
296 extern char *netmap_buffer_base;
297 extern int netmap_verbose;      // XXX debugging
298 enum {                                  /* verbose flags */
299         NM_VERB_ON = 1,                 /* generic verbose */
300         NM_VERB_HOST = 0x2,             /* verbose host stack */
301         NM_VERB_RXSYNC = 0x10,          /* verbose on rxsync/txsync */
302         NM_VERB_TXSYNC = 0x20,
303         NM_VERB_RXINTR = 0x100,         /* verbose on rx/tx intr (driver) */
304         NM_VERB_TXINTR = 0x200,
305         NM_VERB_NIC_RXSYNC = 0x1000,    /* verbose on rx/tx intr (driver) */
306         NM_VERB_NIC_TXSYNC = 0x2000,
307 };
308
309 /*
310  * NA returns a pointer to the struct netmap adapter from the ifp,
311  * WNA is used to write it.
312  */
313 #ifndef WNA
314 #define WNA(_ifp)       (_ifp)->if_pspare[0]
315 #endif
316 #define NA(_ifp)        ((struct netmap_adapter *)WNA(_ifp))
317
318 /*
319  * Macros to determine if an interface is netmap capable or netmap enabled.
320  * See the magic field in struct netmap_adapter.
321  */
322 #ifdef __FreeBSD__
323 /*
324  * on FreeBSD just use if_capabilities and if_capenable.
325  */
326 #define NETMAP_CAPABLE(ifp)     (NA(ifp) &&             \
327         (ifp)->if_capabilities & IFCAP_NETMAP )
328
329 #define NETMAP_SET_CAPABLE(ifp)                         \
330         (ifp)->if_capabilities |= IFCAP_NETMAP
331
332 #else   /* linux */
333
334 /*
335  * on linux:
336  * we check if NA(ifp) is set and its first element has a related
337  * magic value. The capenable is within the struct netmap_adapter.
338  */
339 #define NETMAP_MAGIC    0x52697a7a
340
341 #define NETMAP_CAPABLE(ifp)     (NA(ifp) &&             \
342         ((uint32_t)(uintptr_t)NA(ifp) ^ NA(ifp)->magic) == NETMAP_MAGIC )
343
344 #define NETMAP_SET_CAPABLE(ifp)                         \
345         NA(ifp)->magic = ((uint32_t)(uintptr_t)NA(ifp)) ^ NETMAP_MAGIC
346
347 #endif  /* linux */
348
349 #ifdef __FreeBSD__
350 /* Callback invoked by the dma machinery after a successfull dmamap_load */
351 static void netmap_dmamap_cb(__unused void *arg,
352     __unused bus_dma_segment_t * segs, __unused int nseg, __unused int error)
353 {
354 }
355
356 /* bus_dmamap_load wrapper: call aforementioned function if map != NULL.
357  * XXX can we do it without a callback ?
358  */
359 static inline void
360 netmap_load_map(bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
361 {
362         if (map)
363                 bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE,
364                     netmap_dmamap_cb, NULL, BUS_DMA_NOWAIT);
365 }
366
367 /* update the map when a buffer changes. */
368 static inline void
369 netmap_reload_map(bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
370 {
371         if (map) {
372                 bus_dmamap_unload(tag, map);
373                 bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE,
374                     netmap_dmamap_cb, NULL, BUS_DMA_NOWAIT);
375         }
376 }
377 #else /* linux */
378
379 /*
380  * XXX How do we redefine these functions:
381  *
382  * on linux we need
383  *      dma_map_single(&pdev->dev, virt_addr, len, direction)
384  *      dma_unmap_single(&adapter->pdev->dev, phys_addr, len, direction
385  * The len can be implicit (on netmap it is NETMAP_BUF_SIZE)
386  * unfortunately the direction is not, so we need to change
387  * something to have a cross API
388  */
389 #define netmap_load_map(_t, _m, _b)
390 #define netmap_reload_map(_t, _m, _b)
391 #if 0
392         struct e1000_buffer *buffer_info =  &tx_ring->buffer_info[l];
393         /* set time_stamp *before* dma to help avoid a possible race */
394         buffer_info->time_stamp = jiffies;
395         buffer_info->mapped_as_page = false;
396         buffer_info->length = len;
397         //buffer_info->next_to_watch = l;
398         /* reload dma map */
399         dma_unmap_single(&adapter->pdev->dev, buffer_info->dma,
400                         NETMAP_BUF_SIZE, DMA_TO_DEVICE);
401         buffer_info->dma = dma_map_single(&adapter->pdev->dev,
402                         addr, NETMAP_BUF_SIZE, DMA_TO_DEVICE);
403
404         if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)) {
405                 D("dma mapping error");
406                 /* goto dma_error; See e1000_put_txbuf() */
407                 /* XXX reset */
408         }
409         tx_desc->buffer_addr = htole64(buffer_info->dma); //XXX
410
411 #endif
412
413 /*
414  * The bus_dmamap_sync() can be one of wmb() or rmb() depending on direction.
415  */
416 #define bus_dmamap_sync(_a, _b, _c)
417
418 #endif /* linux */
419
420 /*
421  * functions to map NIC to KRING indexes (n2k) and vice versa (k2n)
422  */
423 static inline int
424 netmap_idx_n2k(struct netmap_kring *kr, int idx)
425 {
426         int n = kr->nkr_num_slots;
427         idx += kr->nkr_hwofs;
428         if (idx < 0)
429                 return idx + n;
430         else if (idx < n)
431                 return idx;
432         else
433                 return idx - n;
434 }
435
436
437 static inline int
438 netmap_idx_k2n(struct netmap_kring *kr, int idx)
439 {
440         int n = kr->nkr_num_slots;
441         idx -= kr->nkr_hwofs;
442         if (idx < 0)
443                 return idx + n;
444         else if (idx < n)
445                 return idx;
446         else
447                 return idx - n;
448 }
449
450
451 /* Entries of the look-up table. */
452 struct lut_entry {
453         void *vaddr;            /* virtual address. */
454         vm_paddr_t paddr;       /* physical address. */
455 };
456
457 struct netmap_obj_pool;
458 extern struct lut_entry *netmap_buffer_lut;
459 #define NMB_VA(i)       (netmap_buffer_lut[i].vaddr)
460 #define NMB_PA(i)       (netmap_buffer_lut[i].paddr)
461
462 /*
463  * NMB return the virtual address of a buffer (buffer 0 on bad index)
464  * PNMB also fills the physical address
465  */
466 static inline void *
467 NMB(struct netmap_slot *slot)
468 {
469         uint32_t i = slot->buf_idx;
470         return (unlikely(i >= netmap_total_buffers)) ?  NMB_VA(0) : NMB_VA(i);
471 }
472
473 static inline void *
474 PNMB(struct netmap_slot *slot, uint64_t *pp)
475 {
476         uint32_t i = slot->buf_idx;
477         void *ret = (i >= netmap_total_buffers) ? NMB_VA(0) : NMB_VA(i);
478
479         *pp = (i >= netmap_total_buffers) ? NMB_PA(0) : NMB_PA(i);
480         return ret;
481 }
482
483 /* default functions to handle rx/tx interrupts */
484 int netmap_rx_irq(struct ifnet *, int, int *);
485 #define netmap_tx_irq(_n, _q) netmap_rx_irq(_n, _q, NULL)
486
487 #endif /* _NET_NETMAP_KERN_H_ */