1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (C) 2011-2014 Matteo Landi, Luigi Rizzo
5 * Copyright (C) 2013-2016 Universita` di Pisa
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 /*
31 * $FreeBSD: stable/12/sys/dev/netmap/netmap_kern.h 372831 2022-12-31 12:30:32Z vmaffione $
32 *
33 * The header contains the definitions of constants and function
34 * prototypes used only in kernelspace.
35 */
36
37 #ifndef _NET_NETMAP_KERN_H_
38 #define _NET_NETMAP_KERN_H_
39
40 #if defined(linux)
41
42 #if defined(CONFIG_NETMAP_EXTMEM)
43 #define WITH_EXTMEM
44 #endif
45 #if defined(CONFIG_NETMAP_VALE)
46 #define WITH_VALE
47 #endif
48 #if defined(CONFIG_NETMAP_PIPE)
49 #define WITH_PIPES
50 #endif
51 #if defined(CONFIG_NETMAP_MONITOR)
52 #define WITH_MONITOR
53 #endif
54 #if defined(CONFIG_NETMAP_GENERIC)
55 #define WITH_GENERIC
56 #endif
57 #if defined(CONFIG_NETMAP_PTNETMAP)
58 #define WITH_PTNETMAP
59 #endif
60 #if defined(CONFIG_NETMAP_SINK)
61 #define WITH_SINK
62 #endif
63 #if defined(CONFIG_NETMAP_NULL)
64 #define WITH_NMNULL
65 #endif
66
67 #elif defined (_WIN32)
68 #define WITH_VALE // comment out to disable VALE support
69 #define WITH_PIPES
70 #define WITH_MONITOR
71 #define WITH_GENERIC
72 #define WITH_NMNULL
73
74 #else /* neither linux nor windows */
75 #define WITH_VALE // comment out to disable VALE support
76 #define WITH_PIPES
77 #define WITH_MONITOR
78 #define WITH_GENERIC
79 #define WITH_EXTMEM
80 #define WITH_NMNULL
81 #endif
82
83 #if defined(__FreeBSD__)
84 #include <sys/selinfo.h>
85
86 #define likely(x) __builtin_expect((long)!!(x), 1L)
87 #define unlikely(x) __builtin_expect((long)!!(x), 0L)
88 #define __user
89
90 #define NM_LOCK_T struct mtx /* low level spinlock, used to protect queues */
91
92 #define NM_MTX_T struct sx /* OS-specific mutex (sleepable) */
93 #define NM_MTX_INIT(m) sx_init(&(m), #m)
94 #define NM_MTX_DESTROY(m) sx_destroy(&(m))
95 #define NM_MTX_LOCK(m) sx_xlock(&(m))
96 #define NM_MTX_SPINLOCK(m) while (!sx_try_xlock(&(m))) ;
97 #define NM_MTX_UNLOCK(m) sx_xunlock(&(m))
98 #define NM_MTX_ASSERT(m) sx_assert(&(m), SA_XLOCKED)
99
100 #define NM_SELINFO_T struct nm_selinfo
101 #define NM_SELRECORD_T struct thread
102 #define MBUF_LEN(m) ((m)->m_pkthdr.len)
103 #define MBUF_TXQ(m) ((m)->m_pkthdr.flowid)
104 #define MBUF_TRANSMIT(na, ifp, m) ((na)->if_transmit(ifp, m))
105 #define GEN_TX_MBUF_IFP(m) ((m)->m_pkthdr.rcvif)
106
107 #define NM_ATOMIC_T volatile int /* required by atomic/bitops.h */
108 /* atomic operations */
109 #include <machine/atomic.h>
110 #define NM_ATOMIC_TEST_AND_SET(p) (!atomic_cmpset_acq_int((p), 0, 1))
111 #define NM_ATOMIC_CLEAR(p) atomic_store_rel_int((p), 0)
112
113 #define WNA(_ifp) (_ifp)->if_netmap
114
115 struct netmap_adapter *netmap_getna(if_t ifp);
116
117 #define MBUF_REFCNT(m) ((m)->m_ext.ext_count)
118 #define SET_MBUF_REFCNT(m, x) (m)->m_ext.ext_count = x
119
120 #define MBUF_QUEUED(m) 1
121
122 struct nm_selinfo {
123 /* Support for select(2) and poll(2). */
124 struct selinfo si;
125 /* Support for kqueue(9). See comments in netmap_freebsd.c */
126 struct taskqueue *ntfytq;
127 struct task ntfytask;
128 struct mtx m;
129 char mtxname[32];
130 int kqueue_users;
131 };
132
133
134 struct hrtimer {
135 /* Not used in FreeBSD. */
136 };
137
138 #define NM_BNS_GET(b)
139 #define NM_BNS_PUT(b)
140
141 #elif defined (linux)
142
143 #define NM_LOCK_T safe_spinlock_t // see bsd_glue.h
144 #define NM_SELINFO_T wait_queue_head_t
145 #define MBUF_LEN(m) ((m)->len)
146 #define MBUF_TRANSMIT(na, ifp, m) \
147 ({ \
148 /* Avoid infinite recursion with generic. */ \
149 m->priority = NM_MAGIC_PRIORITY_TX; \
150 (((struct net_device_ops *)(na)->if_transmit)->ndo_start_xmit(m, ifp)); \
151 0; \
152 })
153
154 /* See explanation in nm_os_generic_xmit_frame. */
155 #define GEN_TX_MBUF_IFP(m) ((struct ifnet *)skb_shinfo(m)->destructor_arg)
156
157 #define NM_ATOMIC_T volatile long unsigned int
158
159 #define NM_MTX_T struct mutex /* OS-specific sleepable lock */
160 #define NM_MTX_INIT(m) mutex_init(&(m))
161 #define NM_MTX_DESTROY(m) do { (void)(m); } while (0)
162 #define NM_MTX_LOCK(m) mutex_lock(&(m))
163 #define NM_MTX_UNLOCK(m) mutex_unlock(&(m))
164 #define NM_MTX_ASSERT(m) mutex_is_locked(&(m))
165
166 #ifndef DEV_NETMAP
167 #define DEV_NETMAP
168 #endif /* DEV_NETMAP */
169
170 #elif defined (__APPLE__)
171
172 #warning apple support is incomplete.
173 #define likely(x) __builtin_expect(!!(x), 1)
174 #define unlikely(x) __builtin_expect(!!(x), 0)
175 #define NM_LOCK_T IOLock *
176 #define NM_SELINFO_T struct selinfo
177 #define MBUF_LEN(m) ((m)->m_pkthdr.len)
178
179 #elif defined (_WIN32)
180 #include "../../../WINDOWS/win_glue.h"
181
182 #define NM_SELRECORD_T IO_STACK_LOCATION
183 #define NM_SELINFO_T win_SELINFO // see win_glue.h
184 #define NM_LOCK_T win_spinlock_t // see win_glue.h
185 #define NM_MTX_T KGUARDED_MUTEX /* OS-specific mutex (sleepable) */
186
187 #define NM_MTX_INIT(m) KeInitializeGuardedMutex(&m);
188 #define NM_MTX_DESTROY(m) do { (void)(m); } while (0)
189 #define NM_MTX_LOCK(m) KeAcquireGuardedMutex(&(m))
190 #define NM_MTX_UNLOCK(m) KeReleaseGuardedMutex(&(m))
191 #define NM_MTX_ASSERT(m) assert(&m.Count>0)
192
193 //These linknames are for the NDIS driver
194 #define NETMAP_NDIS_LINKNAME_STRING L"\\DosDevices\\NMAPNDIS"
195 #define NETMAP_NDIS_NTDEVICE_STRING L"\\Device\\NMAPNDIS"
196
197 //Definition of internal driver-to-driver ioctl codes
198 #define NETMAP_KERNEL_XCHANGE_POINTERS _IO('i', 180)
199 #define NETMAP_KERNEL_SEND_SHUTDOWN_SIGNAL _IO_direct('i', 195)
200
201 typedef struct hrtimer{
202 KTIMER timer;
203 BOOLEAN active;
204 KDPC deferred_proc;
205 };
206
207 /* MSVC does not have likely/unlikely support */
208 #ifdef _MSC_VER
209 #define likely(x) (x)
210 #define unlikely(x) (x)
211 #else
212 #define likely(x) __builtin_expect((long)!!(x), 1L)
213 #define unlikely(x) __builtin_expect((long)!!(x), 0L)
214 #endif //_MSC_VER
215
216 #else
217
218 #error unsupported platform
219
220 #endif /* end - platform-specific code */
221
222 #ifndef _WIN32 /* support for emulated sysctl */
223 #define SYSBEGIN(x)
224 #define SYSEND
225 #endif /* _WIN32 */
226
227 #define NM_ACCESS_ONCE(x) (*(volatile __typeof__(x) *)&(x))
228
229 #define NMG_LOCK_T NM_MTX_T
230 #define NMG_LOCK_INIT() NM_MTX_INIT(netmap_global_lock)
231 #define NMG_LOCK_DESTROY() NM_MTX_DESTROY(netmap_global_lock)
232 #define NMG_LOCK() NM_MTX_LOCK(netmap_global_lock)
233 #define NMG_UNLOCK() NM_MTX_UNLOCK(netmap_global_lock)
234 #define NMG_LOCK_ASSERT() NM_MTX_ASSERT(netmap_global_lock)
235
236 #if defined(__FreeBSD__)
237 #define nm_prerr_int printf
238 #define nm_prinf_int printf
239 #elif defined (_WIN32)
240 #define nm_prerr_int DbgPrint
241 #define nm_prinf_int DbgPrint
242 #elif defined(linux)
243 #define nm_prerr_int(fmt, arg...) printk(KERN_ERR fmt, ##arg)
244 #define nm_prinf_int(fmt, arg...) printk(KERN_INFO fmt, ##arg)
245 #endif
246
247 #define nm_prinf(format, ...) \
248 do { \
249 struct timeval __xxts; \
250 microtime(&__xxts); \
251 nm_prinf_int("%03d.%06d [%4d] %-25s " format "\n",\
252 (int)__xxts.tv_sec % 1000, (int)__xxts.tv_usec, \
253 __LINE__, __FUNCTION__, ##__VA_ARGS__); \
254 } while (0)
255
256 #define nm_prerr(format, ...) \
257 do { \
258 struct timeval __xxts; \
259 microtime(&__xxts); \
260 nm_prerr_int("%03d.%06d [%4d] %-25s " format "\n",\
261 (int)__xxts.tv_sec % 1000, (int)__xxts.tv_usec, \
262 __LINE__, __FUNCTION__, ##__VA_ARGS__); \
263 } while (0)
264
265 /* Disabled printf (used to be nm_prdis). */
266 #define nm_prdis(format, ...)
267
268 /* Rate limited, lps indicates how many per second. */
269 #define nm_prlim(lps, format, ...) \
270 do { \
271 static int t0, __cnt; \
272 if (t0 != time_second) { \
273 t0 = time_second; \
274 __cnt = 0; \
275 } \
276 if (__cnt++ < lps) \
277 nm_prinf(format, ##__VA_ARGS__); \
278 } while (0)
279
280 struct netmap_adapter;
281 struct nm_bdg_fwd;
282 struct nm_bridge;
283 struct netmap_priv_d;
284 struct nm_bdg_args;
285
286 /* os-specific NM_SELINFO_T initialzation/destruction functions */
287 int nm_os_selinfo_init(NM_SELINFO_T *, const char *name);
288 void nm_os_selinfo_uninit(NM_SELINFO_T *);
289
290 const char *nm_dump_buf(char *p, int len, int lim, char *dst);
291
292 void nm_os_selwakeup(NM_SELINFO_T *si);
293 void nm_os_selrecord(NM_SELRECORD_T *sr, NM_SELINFO_T *si);
294
295 int nm_os_ifnet_init(void);
296 void nm_os_ifnet_fini(void);
297 void nm_os_ifnet_lock(void);
298 void nm_os_ifnet_unlock(void);
299
300 unsigned nm_os_ifnet_mtu(struct ifnet *ifp);
301
302 void nm_os_get_module(void);
303 void nm_os_put_module(void);
304
305 void netmap_make_zombie(struct ifnet *);
306 void netmap_undo_zombie(struct ifnet *);
307
308 /* os independent alloc/realloc/free */
309 void *nm_os_malloc(size_t);
310 void *nm_os_vmalloc(size_t);
311 void *nm_os_realloc(void *, size_t new_size, size_t old_size);
312 void nm_os_free(void *);
313 void nm_os_vfree(void *);
314
315 /* os specific attach/detach enter/exit-netmap-mode routines */
316 void nm_os_onattach(struct ifnet *);
317 void nm_os_ondetach(struct ifnet *);
318 void nm_os_onenter(struct ifnet *);
319 void nm_os_onexit(struct ifnet *);
320
321 /* passes a packet up to the host stack.
322 * If the packet is sent (or dropped) immediately it returns NULL,
323 * otherwise it links the packet to prev and returns m.
324 * In this case, a final call with m=NULL and prev != NULL will send up
325 * the entire chain to the host stack.
326 */
327 void *nm_os_send_up(struct ifnet *, struct mbuf *m, struct mbuf *prev);
328
329 int nm_os_mbuf_has_seg_offld(struct mbuf *m);
330 int nm_os_mbuf_has_csum_offld(struct mbuf *m);
331
332 #include "netmap_mbq.h"
333
334 extern NMG_LOCK_T netmap_global_lock;
335
336 enum txrx { NR_RX = 0, NR_TX = 1, NR_TXRX };
337
338 static __inline const char*
nm_txrx2str(enum txrx t)339 nm_txrx2str(enum txrx t)
340 {
341 return (t== NR_RX ? "RX" : "TX");
342 }
343
344 static __inline enum txrx
nm_txrx_swap(enum txrx t)345 nm_txrx_swap(enum txrx t)
346 {
347 return (t== NR_RX ? NR_TX : NR_RX);
348 }
349
350 #define for_rx_tx(t) for ((t) = 0; (t) < NR_TXRX; (t)++)
351
352 #ifdef WITH_MONITOR
353 struct netmap_zmon_list {
354 struct netmap_kring *next;
355 struct netmap_kring *prev;
356 };
357 #endif /* WITH_MONITOR */
358
359 /*
360 * private, kernel view of a ring. Keeps track of the status of
361 * a ring across system calls.
362 *
363 * nr_hwcur index of the next buffer to refill.
364 * It corresponds to ring->head
365 * at the time the system call returns.
366 *
367 * nr_hwtail index of the first buffer owned by the kernel.
368 * On RX, hwcur->hwtail are receive buffers
369 * not yet released. hwcur is advanced following
370 * ring->head, hwtail is advanced on incoming packets,
371 * and a wakeup is generated when hwtail passes ring->cur
372 * On TX, hwcur->rcur have been filled by the sender
373 * but not sent yet to the NIC; rcur->hwtail are available
374 * for new transmissions, and hwtail->hwcur-1 are pending
375 * transmissions not yet acknowledged.
376 *
377 * The indexes in the NIC and netmap rings are offset by nkr_hwofs slots.
378 * This is so that, on a reset, buffers owned by userspace are not
379 * modified by the kernel. In particular:
380 * RX rings: the next empty buffer (hwtail + hwofs) coincides with
381 * the next empty buffer as known by the hardware (next_to_check or so).
382 * TX rings: hwcur + hwofs coincides with next_to_send
383 *
384 * The following fields are used to implement lock-free copy of packets
385 * from input to output ports in VALE switch:
386 * nkr_hwlease buffer after the last one being copied.
387 * A writer in nm_bdg_flush reserves N buffers
388 * from nr_hwlease, advances it, then does the
389 * copy outside the lock.
390 * In RX rings (used for VALE ports),
391 * nkr_hwtail <= nkr_hwlease < nkr_hwcur+N-1
392 * In TX rings (used for NIC or host stack ports)
393 * nkr_hwcur <= nkr_hwlease < nkr_hwtail
394 * nkr_leases array of nkr_num_slots where writers can report
395 * completion of their block. NR_NOSLOT (~0) indicates
396 * that the writer has not finished yet
397 * nkr_lease_idx index of next free slot in nr_leases, to be assigned
398 *
399 * The kring is manipulated by txsync/rxsync and generic netmap function.
400 *
401 * Concurrent rxsync or txsync on the same ring are prevented through
402 * by nm_kr_(try)lock() which in turn uses nr_busy. This is all we need
403 * for NIC rings, and for TX rings attached to the host stack.
404 *
405 * RX rings attached to the host stack use an mbq (rx_queue) on both
406 * rxsync_from_host() and netmap_transmit(). The mbq is protected
407 * by its internal lock.
408 *
409 * RX rings attached to the VALE switch are accessed by both senders
410 * and receiver. They are protected through the q_lock on the RX ring.
411 */
412 struct netmap_kring {
413 struct netmap_ring *ring;
414
415 uint32_t nr_hwcur; /* should be nr_hwhead */
416 uint32_t nr_hwtail;
417
418 /*
419 * Copies of values in user rings, so we do not need to look
420 * at the ring (which could be modified). These are set in the
421 * *sync_prologue()/finalize() routines.
422 */
423 uint32_t rhead;
424 uint32_t rcur;
425 uint32_t rtail;
426
427 uint32_t nr_kflags; /* private driver flags */
428 #define NKR_PENDINTR 0x1 // Pending interrupt.
429 #define NKR_EXCLUSIVE 0x2 /* exclusive binding */
430 #define NKR_FORWARD 0x4 /* (host ring only) there are
431 packets to forward
432 */
433 #define NKR_NEEDRING 0x8 /* ring needed even if users==0
434 * (used internally by pipes and
435 * by ptnetmap host ports)
436 */
437 #define NKR_NOINTR 0x10 /* don't use interrupts on this ring */
438 #define NKR_FAKERING 0x20 /* don't allocate/free buffers */
439
440 uint32_t nr_mode;
441 uint32_t nr_pending_mode;
442 #define NKR_NETMAP_OFF 0x0
443 #define NKR_NETMAP_ON 0x1
444
445 uint32_t nkr_num_slots;
446
447 /*
448 * On a NIC reset, the NIC ring indexes may be reset but the
449 * indexes in the netmap rings remain the same. nkr_hwofs
450 * keeps track of the offset between the two.
451 */
452 int32_t nkr_hwofs;
453
454 /* last_reclaim is opaque marker to help reduce the frequency
455 * of operations such as reclaiming tx buffers. A possible use
456 * is set it to ticks and do the reclaim only once per tick.
457 */
458 uint64_t last_reclaim;
459
460
461 NM_SELINFO_T si; /* poll/select wait queue */
462 NM_LOCK_T q_lock; /* protects kring and ring. */
463 NM_ATOMIC_T nr_busy; /* prevent concurrent syscalls */
464
465 /* the adapter the owns this kring */
466 struct netmap_adapter *na;
467
468 /* the adapter that wants to be notified when this kring has
469 * new slots avaialable. This is usually the same as the above,
470 * but wrappers may let it point to themselves
471 */
472 struct netmap_adapter *notify_na;
473
474 /* The following fields are for VALE switch support */
475 struct nm_bdg_fwd *nkr_ft;
476 uint32_t *nkr_leases;
477 #define NR_NOSLOT ((uint32_t)~0) /* used in nkr_*lease* */
478 uint32_t nkr_hwlease;
479 uint32_t nkr_lease_idx;
480
481 /* while nkr_stopped is set, no new [tr]xsync operations can
482 * be started on this kring.
483 * This is used by netmap_disable_all_rings()
484 * to find a synchronization point where critical data
485 * structures pointed to by the kring can be added or removed
486 */
487 volatile int nkr_stopped;
488
489 /* Support for adapters without native netmap support.
490 * On tx rings we preallocate an array of tx buffers
491 * (same size as the netmap ring), on rx rings we
492 * store incoming mbufs in a queue that is drained by
493 * a rxsync.
494 */
495 struct mbuf **tx_pool;
496 struct mbuf *tx_event; /* TX event used as a notification */
497 NM_LOCK_T tx_event_lock; /* protects the tx_event mbuf */
498 struct mbq rx_queue; /* intercepted rx mbufs. */
499
500 uint32_t users; /* existing bindings for this ring */
501
502 uint32_t ring_id; /* kring identifier */
503 enum txrx tx; /* kind of ring (tx or rx) */
504 char name[64]; /* diagnostic */
505
506 /* [tx]sync callback for this kring.
507 * The default nm_kring_create callback (netmap_krings_create)
508 * sets the nm_sync callback of each hardware tx(rx) kring to
509 * the corresponding nm_txsync(nm_rxsync) taken from the
510 * netmap_adapter; moreover, it sets the sync callback
511 * of the host tx(rx) ring to netmap_txsync_to_host
512 * (netmap_rxsync_from_host).
513 *
514 * Overrides: the above configuration is not changed by
515 * any of the nm_krings_create callbacks.
516 */
517 int (*nm_sync)(struct netmap_kring *kring, int flags);
518 int (*nm_notify)(struct netmap_kring *kring, int flags);
519
520 #ifdef WITH_PIPES
521 struct netmap_kring *pipe; /* if this is a pipe ring,
522 * pointer to the other end
523 */
524 uint32_t pipe_tail; /* hwtail updated by the other end */
525 #endif /* WITH_PIPES */
526
527 int (*save_notify)(struct netmap_kring *kring, int flags);
528
529 #ifdef WITH_MONITOR
530 /* array of krings that are monitoring this kring */
531 struct netmap_kring **monitors;
532 uint32_t max_monitors; /* current size of the monitors array */
533 uint32_t n_monitors; /* next unused entry in the monitor array */
534 uint32_t mon_pos[NR_TXRX]; /* index of this ring in the monitored ring array */
535 uint32_t mon_tail; /* last seen slot on rx */
536
537 /* circular list of zero-copy monitors */
538 struct netmap_zmon_list zmon_list[NR_TXRX];
539
540 /*
541 * Monitors work by intercepting the sync and notify callbacks of the
542 * monitored krings. This is implemented by replacing the pointers
543 * above and saving the previous ones in mon_* pointers below
544 */
545 int (*mon_sync)(struct netmap_kring *kring, int flags);
546 int (*mon_notify)(struct netmap_kring *kring, int flags);
547
548 #endif
549 }
550 #ifdef _WIN32
551 __declspec(align(64));
552 #else
553 __attribute__((__aligned__(64)));
554 #endif
555
556 /* return 1 iff the kring needs to be turned on */
557 static inline int
nm_kring_pending_on(struct netmap_kring * kring)558 nm_kring_pending_on(struct netmap_kring *kring)
559 {
560 return kring->nr_pending_mode == NKR_NETMAP_ON &&
561 kring->nr_mode == NKR_NETMAP_OFF;
562 }
563
564 /* return 1 iff the kring needs to be turned off */
565 static inline int
nm_kring_pending_off(struct netmap_kring * kring)566 nm_kring_pending_off(struct netmap_kring *kring)
567 {
568 return kring->nr_pending_mode == NKR_NETMAP_OFF &&
569 kring->nr_mode == NKR_NETMAP_ON;
570 }
571
572 /* return the next index, with wraparound */
573 static inline uint32_t
nm_next(uint32_t i,uint32_t lim)574 nm_next(uint32_t i, uint32_t lim)
575 {
576 return unlikely (i == lim) ? 0 : i + 1;
577 }
578
579
580 /* return the previous index, with wraparound */
581 static inline uint32_t
nm_prev(uint32_t i,uint32_t lim)582 nm_prev(uint32_t i, uint32_t lim)
583 {
584 return unlikely (i == 0) ? lim : i - 1;
585 }
586
587
588 /*
589 *
590 * Here is the layout for the Rx and Tx rings.
591
592 RxRING TxRING
593
594 +-----------------+ +-----------------+
595 | | | |
596 | free | | free |
597 +-----------------+ +-----------------+
598 head->| owned by user |<-hwcur | not sent to nic |<-hwcur
599 | | | yet |
600 +-----------------+ | |
601 cur->| available to | | |
602 | user, not read | +-----------------+
603 | yet | cur->| (being |
604 | | | prepared) |
605 | | | |
606 +-----------------+ + ------ +
607 tail->| |<-hwtail | |<-hwlease
608 | (being | ... | | ...
609 | prepared) | ... | | ...
610 +-----------------+ ... | | ...
611 | |<-hwlease +-----------------+
612 | | tail->| |<-hwtail
613 | | | |
614 | | | |
615 | | | |
616 +-----------------+ +-----------------+
617
618 * The cur/tail (user view) and hwcur/hwtail (kernel view)
619 * are used in the normal operation of the card.
620 *
621 * When a ring is the output of a switch port (Rx ring for
622 * a VALE port, Tx ring for the host stack or NIC), slots
623 * are reserved in blocks through 'hwlease' which points
624 * to the next unused slot.
625 * On an Rx ring, hwlease is always after hwtail,
626 * and completions cause hwtail to advance.
627 * On a Tx ring, hwlease is always between cur and hwtail,
628 * and completions cause cur to advance.
629 *
630 * nm_kr_space() returns the maximum number of slots that
631 * can be assigned.
632 * nm_kr_lease() reserves the required number of buffers,
633 * advances nkr_hwlease and also returns an entry in
634 * a circular array where completions should be reported.
635 */
636
637 struct lut_entry;
638 #ifdef __FreeBSD__
639 #define plut_entry lut_entry
640 #endif
641
642 struct netmap_lut {
643 struct lut_entry *lut;
644 struct plut_entry *plut;
645 uint32_t objtotal; /* max buffer index */
646 uint32_t objsize; /* buffer size */
647 };
648
649 struct netmap_vp_adapter; // forward
650 struct nm_bridge;
651
652 /* Struct to be filled by nm_config callbacks. */
653 struct nm_config_info {
654 unsigned num_tx_rings;
655 unsigned num_rx_rings;
656 unsigned num_tx_descs;
657 unsigned num_rx_descs;
658 unsigned rx_buf_maxsize;
659 };
660
661 /*
662 * default type for the magic field.
663 * May be overriden in glue code.
664 */
665 #ifndef NM_OS_MAGIC
666 #define NM_OS_MAGIC uint32_t
667 #endif /* !NM_OS_MAGIC */
668
669 /*
670 * The "struct netmap_adapter" extends the "struct adapter"
671 * (or equivalent) device descriptor.
672 * It contains all base fields needed to support netmap operation.
673 * There are in fact different types of netmap adapters
674 * (native, generic, VALE switch...) so a netmap_adapter is
675 * just the first field in the derived type.
676 */
677 struct netmap_adapter {
678 /*
679 * On linux we do not have a good way to tell if an interface
680 * is netmap-capable. So we always use the following trick:
681 * NA(ifp) points here, and the first entry (which hopefully
682 * always exists and is at least 32 bits) contains a magic
683 * value which we can use to detect that the interface is good.
684 */
685 NM_OS_MAGIC magic;
686 uint32_t na_flags; /* enabled, and other flags */
687 #define NAF_SKIP_INTR 1 /* use the regular interrupt handler.
688 * useful during initialization
689 */
690 #define NAF_SW_ONLY 2 /* forward packets only to sw adapter */
691 #define NAF_BDG_MAYSLEEP 4 /* the bridge is allowed to sleep when
692 * forwarding packets coming from this
693 * interface
694 */
695 #define NAF_MEM_OWNER 8 /* the adapter uses its own memory area
696 * that cannot be changed
697 */
698 #define NAF_NATIVE 16 /* the adapter is native.
699 * Virtual ports (non persistent vale ports,
700 * pipes, monitors...) should never use
701 * this flag.
702 */
703 #define NAF_NETMAP_ON 32 /* netmap is active (either native or
704 * emulated). Where possible (e.g. FreeBSD)
705 * IFCAP_NETMAP also mirrors this flag.
706 */
707 #define NAF_HOST_RINGS 64 /* the adapter supports the host rings */
708 #define NAF_FORCE_NATIVE 128 /* the adapter is always NATIVE */
709 /* free */
710 #define NAF_MOREFRAG 512 /* the adapter supports NS_MOREFRAG */
711 #define NAF_ZOMBIE (1U<<30) /* the nic driver has been unloaded */
712 #define NAF_BUSY (1U<<31) /* the adapter is used internally and
713 * cannot be registered from userspace
714 */
715 int active_fds; /* number of user-space descriptors using this
716 interface, which is equal to the number of
717 struct netmap_if objs in the mapped region. */
718
719 u_int num_rx_rings; /* number of adapter receive rings */
720 u_int num_tx_rings; /* number of adapter transmit rings */
721 u_int num_host_rx_rings; /* number of host receive rings */
722 u_int num_host_tx_rings; /* number of host transmit rings */
723
724 u_int num_tx_desc; /* number of descriptor in each queue */
725 u_int num_rx_desc;
726
727 /* tx_rings and rx_rings are private but allocated as a
728 * contiguous chunk of memory. Each array has N+K entries,
729 * N for the hardware rings and K for the host rings.
730 */
731 struct netmap_kring **tx_rings; /* array of TX rings. */
732 struct netmap_kring **rx_rings; /* array of RX rings. */
733
734 void *tailroom; /* space below the rings array */
735 /* (used for leases) */
736
737
738 NM_SELINFO_T si[NR_TXRX]; /* global wait queues */
739
740 /* count users of the global wait queues */
741 int si_users[NR_TXRX];
742
743 void *pdev; /* used to store pci device */
744
745 /* copy of if_qflush and if_transmit pointers, to intercept
746 * packets from the network stack when netmap is active.
747 */
748 int (*if_transmit)(struct ifnet *, struct mbuf *);
749
750 /* copy of if_input for netmap_send_up() */
751 void (*if_input)(struct ifnet *, struct mbuf *);
752
753 /* Back reference to the parent ifnet struct. Used for
754 * hardware ports (emulated netmap included). */
755 struct ifnet *ifp; /* adapter is ifp->if_softc */
756
757 /*---- callbacks for this netmap adapter -----*/
758 /*
759 * nm_dtor() is the cleanup routine called when destroying
760 * the adapter.
761 * Called with NMG_LOCK held.
762 *
763 * nm_register() is called on NIOCREGIF and close() to enter
764 * or exit netmap mode on the NIC
765 * Called with NNG_LOCK held.
766 *
767 * nm_txsync() pushes packets to the underlying hw/switch
768 *
769 * nm_rxsync() collects packets from the underlying hw/switch
770 *
771 * nm_config() returns configuration information from the OS
772 * Called with NMG_LOCK held.
773 *
774 * nm_krings_create() create and init the tx_rings and
775 * rx_rings arrays of kring structures. In particular,
776 * set the nm_sync callbacks for each ring.
777 * There is no need to also allocate the corresponding
778 * netmap_rings, since netmap_mem_rings_create() will always
779 * be called to provide the missing ones.
780 * Called with NNG_LOCK held.
781 *
782 * nm_krings_delete() cleanup and delete the tx_rings and rx_rings
783 * arrays
784 * Called with NMG_LOCK held.
785 *
786 * nm_notify() is used to act after data have become available
787 * (or the stopped state of the ring has changed)
788 * For hw devices this is typically a selwakeup(),
789 * but for NIC/host ports attached to a switch (or vice-versa)
790 * we also need to invoke the 'txsync' code downstream.
791 * This callback pointer is actually used only to initialize
792 * kring->nm_notify.
793 * Return values are the same as for netmap_rx_irq().
794 */
795 void (*nm_dtor)(struct netmap_adapter *);
796
797 int (*nm_register)(struct netmap_adapter *, int onoff);
798 void (*nm_intr)(struct netmap_adapter *, int onoff);
799
800 int (*nm_txsync)(struct netmap_kring *kring, int flags);
801 int (*nm_rxsync)(struct netmap_kring *kring, int flags);
802 int (*nm_notify)(struct netmap_kring *kring, int flags);
803 #define NAF_FORCE_READ 1
804 #define NAF_FORCE_RECLAIM 2
805 #define NAF_CAN_FORWARD_DOWN 4
806 /* return configuration information */
807 int (*nm_config)(struct netmap_adapter *, struct nm_config_info *info);
808 int (*nm_krings_create)(struct netmap_adapter *);
809 void (*nm_krings_delete)(struct netmap_adapter *);
810 /*
811 * nm_bdg_attach() initializes the na_vp field to point
812 * to an adapter that can be attached to a VALE switch. If the
813 * current adapter is already a VALE port, na_vp is simply a cast;
814 * otherwise, na_vp points to a netmap_bwrap_adapter.
815 * If applicable, this callback also initializes na_hostvp,
816 * that can be used to connect the adapter host rings to the
817 * switch.
818 * Called with NMG_LOCK held.
819 *
820 * nm_bdg_ctl() is called on the actual attach/detach to/from
821 * to/from the switch, to perform adapter-specific
822 * initializations
823 * Called with NMG_LOCK held.
824 */
825 int (*nm_bdg_attach)(const char *bdg_name, struct netmap_adapter *,
826 struct nm_bridge *);
827 int (*nm_bdg_ctl)(struct nmreq_header *, struct netmap_adapter *);
828
829 /* adapter used to attach this adapter to a VALE switch (if any) */
830 struct netmap_vp_adapter *na_vp;
831 /* adapter used to attach the host rings of this adapter
832 * to a VALE switch (if any) */
833 struct netmap_vp_adapter *na_hostvp;
834
835 /* standard refcount to control the lifetime of the adapter
836 * (it should be equal to the lifetime of the corresponding ifp)
837 */
838 int na_refcount;
839
840 /* memory allocator (opaque)
841 * We also cache a pointer to the lut_entry for translating
842 * buffer addresses, the total number of buffers and the buffer size.
843 */
844 struct netmap_mem_d *nm_mem;
845 struct netmap_mem_d *nm_mem_prev;
846 struct netmap_lut na_lut;
847
848 /* additional information attached to this adapter
849 * by other netmap subsystems. Currently used by
850 * bwrap, LINUX/v1000 and ptnetmap
851 */
852 void *na_private;
853
854 /* array of pipes that have this adapter as a parent */
855 struct netmap_pipe_adapter **na_pipes;
856 int na_next_pipe; /* next free slot in the array */
857 int na_max_pipes; /* size of the array */
858
859 /* Offset of ethernet header for each packet. */
860 u_int virt_hdr_len;
861
862 /* Max number of bytes that the NIC can store in the buffer
863 * referenced by each RX descriptor. This translates to the maximum
864 * bytes that a single netmap slot can reference. Larger packets
865 * require NS_MOREFRAG support. */
866 unsigned rx_buf_maxsize;
867
868 char name[NETMAP_REQ_IFNAMSIZ]; /* used at least by pipes */
869
870 #ifdef WITH_MONITOR
871 unsigned long monitor_id; /* debugging */
872 #endif
873 };
874
875 static __inline u_int
nma_get_ndesc(struct netmap_adapter * na,enum txrx t)876 nma_get_ndesc(struct netmap_adapter *na, enum txrx t)
877 {
878 return (t == NR_TX ? na->num_tx_desc : na->num_rx_desc);
879 }
880
881 static __inline void
nma_set_ndesc(struct netmap_adapter * na,enum txrx t,u_int v)882 nma_set_ndesc(struct netmap_adapter *na, enum txrx t, u_int v)
883 {
884 if (t == NR_TX)
885 na->num_tx_desc = v;
886 else
887 na->num_rx_desc = v;
888 }
889
890 static __inline u_int
nma_get_nrings(struct netmap_adapter * na,enum txrx t)891 nma_get_nrings(struct netmap_adapter *na, enum txrx t)
892 {
893 return (t == NR_TX ? na->num_tx_rings : na->num_rx_rings);
894 }
895
896 static __inline u_int
nma_get_host_nrings(struct netmap_adapter * na,enum txrx t)897 nma_get_host_nrings(struct netmap_adapter *na, enum txrx t)
898 {
899 return (t == NR_TX ? na->num_host_tx_rings : na->num_host_rx_rings);
900 }
901
902 static __inline void
nma_set_nrings(struct netmap_adapter * na,enum txrx t,u_int v)903 nma_set_nrings(struct netmap_adapter *na, enum txrx t, u_int v)
904 {
905 if (t == NR_TX)
906 na->num_tx_rings = v;
907 else
908 na->num_rx_rings = v;
909 }
910
911 static __inline void
nma_set_host_nrings(struct netmap_adapter * na,enum txrx t,u_int v)912 nma_set_host_nrings(struct netmap_adapter *na, enum txrx t, u_int v)
913 {
914 if (t == NR_TX)
915 na->num_host_tx_rings = v;
916 else
917 na->num_host_rx_rings = v;
918 }
919
920 static __inline struct netmap_kring**
NMR(struct netmap_adapter * na,enum txrx t)921 NMR(struct netmap_adapter *na, enum txrx t)
922 {
923 return (t == NR_TX ? na->tx_rings : na->rx_rings);
924 }
925
926 int nma_intr_enable(struct netmap_adapter *na, int onoff);
927
928 /*
929 * If the NIC is owned by the kernel
930 * (i.e., bridge), neither another bridge nor user can use it;
931 * if the NIC is owned by a user, only users can share it.
932 * Evaluation must be done under NMG_LOCK().
933 */
934 #define NETMAP_OWNED_BY_KERN(na) ((na)->na_flags & NAF_BUSY)
935 #define NETMAP_OWNED_BY_ANY(na) \
936 (NETMAP_OWNED_BY_KERN(na) || ((na)->active_fds > 0))
937
938 /*
939 * derived netmap adapters for various types of ports
940 */
941 struct netmap_vp_adapter { /* VALE software port */
942 struct netmap_adapter up;
943
944 /*
945 * Bridge support:
946 *
947 * bdg_port is the port number used in the bridge;
948 * na_bdg points to the bridge this NA is attached to.
949 */
950 int bdg_port;
951 struct nm_bridge *na_bdg;
952 int retry;
953 int autodelete; /* remove the ifp on last reference */
954
955 /* Maximum Frame Size, used in bdg_mismatch_datapath() */
956 u_int mfs;
957 /* Last source MAC on this port */
958 uint64_t last_smac;
959 };
960
961
962 struct netmap_hw_adapter { /* physical device */
963 struct netmap_adapter up;
964
965 #ifdef linux
966 struct net_device_ops nm_ndo;
967 struct ethtool_ops nm_eto;
968 #endif
969 const struct ethtool_ops* save_ethtool;
970
971 int (*nm_hw_register)(struct netmap_adapter *, int onoff);
972 };
973
974 #ifdef WITH_GENERIC
975 /* Mitigation support. */
976 struct nm_generic_mit {
977 struct hrtimer mit_timer;
978 int mit_pending;
979 int mit_ring_idx; /* index of the ring being mitigated */
980 struct netmap_adapter *mit_na; /* backpointer */
981 };
982
983 struct netmap_generic_adapter { /* emulated device */
984 struct netmap_hw_adapter up;
985
986 /* Pointer to a previously used netmap adapter. */
987 struct netmap_adapter *prev;
988
989 /* Emulated netmap adapters support:
990 * - save_if_input saves the if_input hook (FreeBSD);
991 * - mit implements rx interrupt mitigation;
992 */
993 void (*save_if_input)(struct ifnet *, struct mbuf *);
994
995 struct nm_generic_mit *mit;
996 #ifdef linux
997 netdev_tx_t (*save_start_xmit)(struct mbuf *, struct ifnet *);
998 #endif
999 /* Is the adapter able to use multiple RX slots to scatter
1000 * each packet pushed up by the driver? */
1001 int rxsg;
1002
1003 /* Is the transmission path controlled by a netmap-aware
1004 * device queue (i.e. qdisc on linux)? */
1005 int txqdisc;
1006 };
1007 #endif /* WITH_GENERIC */
1008
1009 static __inline u_int
netmap_real_rings(struct netmap_adapter * na,enum txrx t)1010 netmap_real_rings(struct netmap_adapter *na, enum txrx t)
1011 {
1012 return nma_get_nrings(na, t) +
1013 !!(na->na_flags & NAF_HOST_RINGS) * nma_get_host_nrings(na, t);
1014 }
1015
1016 /* account for fake rings */
1017 static __inline u_int
netmap_all_rings(struct netmap_adapter * na,enum txrx t)1018 netmap_all_rings(struct netmap_adapter *na, enum txrx t)
1019 {
1020 return max(nma_get_nrings(na, t) + 1, netmap_real_rings(na, t));
1021 }
1022
1023 int netmap_default_bdg_attach(const char *name, struct netmap_adapter *na,
1024 struct nm_bridge *);
1025 struct nm_bdg_polling_state;
1026 /*
1027 * Bridge wrapper for non VALE ports attached to a VALE switch.
1028 *
1029 * The real device must already have its own netmap adapter (hwna).
1030 * The bridge wrapper and the hwna adapter share the same set of
1031 * netmap rings and buffers, but they have two separate sets of
1032 * krings descriptors, with tx/rx meanings swapped:
1033 *
1034 * netmap
1035 * bwrap krings rings krings hwna
1036 * +------+ +------+ +-----+ +------+ +------+
1037 * |tx_rings->| |\ /| |----| |<-tx_rings|
1038 * | | +------+ \ / +-----+ +------+ | |
1039 * | | X | |
1040 * | | / \ | |
1041 * | | +------+/ \+-----+ +------+ | |
1042 * |rx_rings->| | | |----| |<-rx_rings|
1043 * | | +------+ +-----+ +------+ | |
1044 * +------+ +------+
1045 *
1046 * - packets coming from the bridge go to the brwap rx rings,
1047 * which are also the hwna tx rings. The bwrap notify callback
1048 * will then complete the hwna tx (see netmap_bwrap_notify).
1049 *
1050 * - packets coming from the outside go to the hwna rx rings,
1051 * which are also the bwrap tx rings. The (overwritten) hwna
1052 * notify method will then complete the bridge tx
1053 * (see netmap_bwrap_intr_notify).
1054 *
1055 * The bridge wrapper may optionally connect the hwna 'host' rings
1056 * to the bridge. This is done by using a second port in the
1057 * bridge and connecting it to the 'host' netmap_vp_adapter
1058 * contained in the netmap_bwrap_adapter. The brwap host adapter
1059 * cross-links the hwna host rings in the same way as shown above.
1060 *
1061 * - packets coming from the bridge and directed to the host stack
1062 * are handled by the bwrap host notify callback
1063 * (see netmap_bwrap_host_notify)
1064 *
1065 * - packets coming from the host stack are still handled by the
1066 * overwritten hwna notify callback (netmap_bwrap_intr_notify),
1067 * but are diverted to the host adapter depending on the ring number.
1068 *
1069 */
1070 struct netmap_bwrap_adapter {
1071 struct netmap_vp_adapter up;
1072 struct netmap_vp_adapter host; /* for host rings */
1073 struct netmap_adapter *hwna; /* the underlying device */
1074
1075 /*
1076 * When we attach a physical interface to the bridge, we
1077 * allow the controlling process to terminate, so we need
1078 * a place to store the n_detmap_priv_d data structure.
1079 * This is only done when physical interfaces
1080 * are attached to a bridge.
1081 */
1082 struct netmap_priv_d *na_kpriv;
1083 struct nm_bdg_polling_state *na_polling_state;
1084 /* we overwrite the hwna->na_vp pointer, so we save
1085 * here its original value, to be restored at detach
1086 */
1087 struct netmap_vp_adapter *saved_na_vp;
1088 };
1089 int nm_is_bwrap(struct netmap_adapter *na);
1090 int nm_bdg_polling(struct nmreq_header *hdr);
1091
1092 #ifdef WITH_VALE
1093 int netmap_vale_attach(struct nmreq_header *hdr, void *auth_token);
1094 int netmap_vale_detach(struct nmreq_header *hdr, void *auth_token);
1095 int netmap_vale_list(struct nmreq_header *hdr);
1096 int netmap_vi_create(struct nmreq_header *hdr, int);
1097 int nm_vi_create(struct nmreq_header *);
1098 int nm_vi_destroy(const char *name);
1099 #else /* !WITH_VALE */
1100 #define netmap_vi_create(hdr, a) (EOPNOTSUPP)
1101 #endif /* WITH_VALE */
1102
1103 #ifdef WITH_PIPES
1104
1105 #define NM_MAXPIPES 64 /* max number of pipes per adapter */
1106
1107 struct netmap_pipe_adapter {
1108 /* pipe identifier is up.name */
1109 struct netmap_adapter up;
1110
1111 #define NM_PIPE_ROLE_MASTER 0x1
1112 #define NM_PIPE_ROLE_SLAVE 0x2
1113 int role; /* either NM_PIPE_ROLE_MASTER or NM_PIPE_ROLE_SLAVE */
1114
1115 struct netmap_adapter *parent; /* adapter that owns the memory */
1116 struct netmap_pipe_adapter *peer; /* the other end of the pipe */
1117 int peer_ref; /* 1 iff we are holding a ref to the peer */
1118 struct ifnet *parent_ifp; /* maybe null */
1119
1120 u_int parent_slot; /* index in the parent pipe array */
1121 };
1122
1123 #endif /* WITH_PIPES */
1124
1125 #ifdef WITH_NMNULL
1126 struct netmap_null_adapter {
1127 struct netmap_adapter up;
1128 };
1129 #endif /* WITH_NMNULL */
1130
1131
1132 /* return slots reserved to rx clients; used in drivers */
1133 static inline uint32_t
nm_kr_rxspace(struct netmap_kring * k)1134 nm_kr_rxspace(struct netmap_kring *k)
1135 {
1136 int space = k->nr_hwtail - k->nr_hwcur;
1137 if (space < 0)
1138 space += k->nkr_num_slots;
1139 nm_prdis("preserving %d rx slots %d -> %d", space, k->nr_hwcur, k->nr_hwtail);
1140
1141 return space;
1142 }
1143
1144 /* return slots reserved to tx clients */
1145 #define nm_kr_txspace(_k) nm_kr_rxspace(_k)
1146
1147
1148 /* True if no space in the tx ring, only valid after txsync_prologue */
1149 static inline int
nm_kr_txempty(struct netmap_kring * kring)1150 nm_kr_txempty(struct netmap_kring *kring)
1151 {
1152 return kring->rhead == kring->nr_hwtail;
1153 }
1154
1155 /* True if no more completed slots in the rx ring, only valid after
1156 * rxsync_prologue */
1157 #define nm_kr_rxempty(_k) nm_kr_txempty(_k)
1158
1159 /* True if the application needs to wait for more space on the ring
1160 * (more received packets or more free tx slots).
1161 * Only valid after *xsync_prologue. */
1162 static inline int
nm_kr_wouldblock(struct netmap_kring * kring)1163 nm_kr_wouldblock(struct netmap_kring *kring)
1164 {
1165 return kring->rcur == kring->nr_hwtail;
1166 }
1167
1168 /*
1169 * protect against multiple threads using the same ring.
1170 * also check that the ring has not been stopped or locked
1171 */
1172 #define NM_KR_BUSY 1 /* some other thread is syncing the ring */
1173 #define NM_KR_STOPPED 2 /* unbounded stop (ifconfig down or driver unload) */
1174 #define NM_KR_LOCKED 3 /* bounded, brief stop for mutual exclusion */
1175
1176
1177 /* release the previously acquired right to use the *sync() methods of the ring */
nm_kr_put(struct netmap_kring * kr)1178 static __inline void nm_kr_put(struct netmap_kring *kr)
1179 {
1180 NM_ATOMIC_CLEAR(&kr->nr_busy);
1181 }
1182
1183
1184 /* true if the ifp that backed the adapter has disappeared (e.g., the
1185 * driver has been unloaded)
1186 */
1187 static inline int nm_iszombie(struct netmap_adapter *na);
1188
1189 /* try to obtain exclusive right to issue the *sync() operations on the ring.
1190 * The right is obtained and must be later relinquished via nm_kr_put() if and
1191 * only if nm_kr_tryget() returns 0.
1192 * If can_sleep is 1 there are only two other possible outcomes:
1193 * - the function returns NM_KR_BUSY
1194 * - the function returns NM_KR_STOPPED and sets the POLLERR bit in *perr
1195 * (if non-null)
1196 * In both cases the caller will typically skip the ring, possibly collecting
1197 * errors along the way.
1198 * If the calling context does not allow sleeping, the caller must pass 0 in can_sleep.
1199 * In the latter case, the function may also return NM_KR_LOCKED and leave *perr
1200 * untouched: ideally, the caller should try again at a later time.
1201 */
nm_kr_tryget(struct netmap_kring * kr,int can_sleep,int * perr)1202 static __inline int nm_kr_tryget(struct netmap_kring *kr, int can_sleep, int *perr)
1203 {
1204 int busy = 1, stopped;
1205 /* check a first time without taking the lock
1206 * to avoid starvation for nm_kr_get()
1207 */
1208 retry:
1209 stopped = kr->nkr_stopped;
1210 if (unlikely(stopped)) {
1211 goto stop;
1212 }
1213 busy = NM_ATOMIC_TEST_AND_SET(&kr->nr_busy);
1214 /* we should not return NM_KR_BUSY if the ring was
1215 * actually stopped, so check another time after
1216 * the barrier provided by the atomic operation
1217 */
1218 stopped = kr->nkr_stopped;
1219 if (unlikely(stopped)) {
1220 goto stop;
1221 }
1222
1223 if (unlikely(nm_iszombie(kr->na))) {
1224 stopped = NM_KR_STOPPED;
1225 goto stop;
1226 }
1227
1228 return unlikely(busy) ? NM_KR_BUSY : 0;
1229
1230 stop:
1231 if (!busy)
1232 nm_kr_put(kr);
1233 if (stopped == NM_KR_STOPPED) {
1234 /* if POLLERR is defined we want to use it to simplify netmap_poll().
1235 * Otherwise, any non-zero value will do.
1236 */
1237 #ifdef POLLERR
1238 #define NM_POLLERR POLLERR
1239 #else
1240 #define NM_POLLERR 1
1241 #endif /* POLLERR */
1242 if (perr)
1243 *perr |= NM_POLLERR;
1244 #undef NM_POLLERR
1245 } else if (can_sleep) {
1246 tsleep(kr, 0, "NM_KR_TRYGET", 4);
1247 goto retry;
1248 }
1249 return stopped;
1250 }
1251
1252 /* put the ring in the 'stopped' state and wait for the current user (if any) to
1253 * notice. stopped must be either NM_KR_STOPPED or NM_KR_LOCKED
1254 */
nm_kr_stop(struct netmap_kring * kr,int stopped)1255 static __inline void nm_kr_stop(struct netmap_kring *kr, int stopped)
1256 {
1257 kr->nkr_stopped = stopped;
1258 while (NM_ATOMIC_TEST_AND_SET(&kr->nr_busy))
1259 tsleep(kr, 0, "NM_KR_GET", 4);
1260 }
1261
1262 /* restart a ring after a stop */
nm_kr_start(struct netmap_kring * kr)1263 static __inline void nm_kr_start(struct netmap_kring *kr)
1264 {
1265 kr->nkr_stopped = 0;
1266 nm_kr_put(kr);
1267 }
1268
1269
1270 /*
1271 * The following functions are used by individual drivers to
1272 * support netmap operation.
1273 *
1274 * netmap_attach() initializes a struct netmap_adapter, allocating the
1275 * struct netmap_ring's and the struct selinfo.
1276 *
1277 * netmap_detach() frees the memory allocated by netmap_attach().
1278 *
1279 * netmap_transmit() replaces the if_transmit routine of the interface,
1280 * and is used to intercept packets coming from the stack.
1281 *
1282 * netmap_load_map/netmap_reload_map are helper routines to set/reset
1283 * the dmamap for a packet buffer
1284 *
1285 * netmap_reset() is a helper routine to be called in the hw driver
1286 * when reinitializing a ring. It should not be called by
1287 * virtual ports (vale, pipes, monitor)
1288 */
1289 int netmap_attach(struct netmap_adapter *);
1290 int netmap_attach_ext(struct netmap_adapter *, size_t size, int override_reg);
1291 void netmap_detach(struct ifnet *);
1292 int netmap_transmit(struct ifnet *, struct mbuf *);
1293 struct netmap_slot *netmap_reset(struct netmap_adapter *na,
1294 enum txrx tx, u_int n, u_int new_cur);
1295 int netmap_ring_reinit(struct netmap_kring *);
1296 int netmap_rings_config_get(struct netmap_adapter *, struct nm_config_info *);
1297
1298 /* Return codes for netmap_*x_irq. */
1299 enum {
1300 /* Driver should do normal interrupt processing, e.g. because
1301 * the interface is not in netmap mode. */
1302 NM_IRQ_PASS = 0,
1303 /* Port is in netmap mode, and the interrupt work has been
1304 * completed. The driver does not have to notify netmap
1305 * again before the next interrupt. */
1306 NM_IRQ_COMPLETED = -1,
1307 /* Port is in netmap mode, but the interrupt work has not been
1308 * completed. The driver has to make sure netmap will be
1309 * notified again soon, even if no more interrupts come (e.g.
1310 * on Linux the driver should not call napi_complete()). */
1311 NM_IRQ_RESCHED = -2,
1312 };
1313
1314 /* default functions to handle rx/tx interrupts */
1315 int netmap_rx_irq(struct ifnet *, u_int, u_int *);
1316 #define netmap_tx_irq(_n, _q) netmap_rx_irq(_n, _q, NULL)
1317 int netmap_common_irq(struct netmap_adapter *, u_int, u_int *work_done);
1318
1319
1320 #ifdef WITH_VALE
1321 /* functions used by external modules to interface with VALE */
1322 #define netmap_vp_to_ifp(_vp) ((_vp)->up.ifp)
1323 #define netmap_ifp_to_vp(_ifp) (NA(_ifp)->na_vp)
1324 #define netmap_ifp_to_host_vp(_ifp) (NA(_ifp)->na_hostvp)
1325 #define netmap_bdg_idx(_vp) ((_vp)->bdg_port)
1326 const char *netmap_bdg_name(struct netmap_vp_adapter *);
1327 #else /* !WITH_VALE */
1328 #define netmap_vp_to_ifp(_vp) NULL
1329 #define netmap_ifp_to_vp(_ifp) NULL
1330 #define netmap_ifp_to_host_vp(_ifp) NULL
1331 #define netmap_bdg_idx(_vp) -1
1332 #endif /* WITH_VALE */
1333
1334 static inline int
nm_netmap_on(struct netmap_adapter * na)1335 nm_netmap_on(struct netmap_adapter *na)
1336 {
1337 return na && na->na_flags & NAF_NETMAP_ON;
1338 }
1339
1340 static inline int
nm_native_on(struct netmap_adapter * na)1341 nm_native_on(struct netmap_adapter *na)
1342 {
1343 return nm_netmap_on(na) && (na->na_flags & NAF_NATIVE);
1344 }
1345
1346 static inline struct netmap_kring *
netmap_kring_on(struct netmap_adapter * na,u_int q,enum txrx t)1347 netmap_kring_on(struct netmap_adapter *na, u_int q, enum txrx t)
1348 {
1349 struct netmap_kring *kring = NULL;
1350
1351 if (!nm_native_on(na))
1352 return NULL;
1353
1354 if (t == NR_RX && q < na->num_rx_rings)
1355 kring = na->rx_rings[q];
1356 else if (t == NR_TX && q < na->num_tx_rings)
1357 kring = na->tx_rings[q];
1358 else
1359 return NULL;
1360
1361 return (kring->nr_mode == NKR_NETMAP_ON) ? kring : NULL;
1362 }
1363
1364 static inline int
nm_iszombie(struct netmap_adapter * na)1365 nm_iszombie(struct netmap_adapter *na)
1366 {
1367 return na == NULL || (na->na_flags & NAF_ZOMBIE);
1368 }
1369
1370 static inline void
nm_update_hostrings_mode(struct netmap_adapter * na)1371 nm_update_hostrings_mode(struct netmap_adapter *na)
1372 {
1373 /* Process nr_mode and nr_pending_mode for host rings. */
1374 na->tx_rings[na->num_tx_rings]->nr_mode =
1375 na->tx_rings[na->num_tx_rings]->nr_pending_mode;
1376 na->rx_rings[na->num_rx_rings]->nr_mode =
1377 na->rx_rings[na->num_rx_rings]->nr_pending_mode;
1378 }
1379
1380 void nm_set_native_flags(struct netmap_adapter *);
1381 void nm_clear_native_flags(struct netmap_adapter *);
1382
1383 void netmap_krings_mode_commit(struct netmap_adapter *na, int onoff);
1384
1385 /*
1386 * nm_*sync_prologue() functions are used in ioctl/poll and ptnetmap
1387 * kthreads.
1388 * We need netmap_ring* parameter, because in ptnetmap it is decoupled
1389 * from host kring.
1390 * The user-space ring pointers (head/cur/tail) are shared through
1391 * CSB between host and guest.
1392 */
1393
1394 /*
1395 * validates parameters in the ring/kring, returns a value for head
1396 * If any error, returns ring_size to force a reinit.
1397 */
1398 uint32_t nm_txsync_prologue(struct netmap_kring *, struct netmap_ring *);
1399
1400
1401 /*
1402 * validates parameters in the ring/kring, returns a value for head
1403 * If any error, returns ring_size lim to force a reinit.
1404 */
1405 uint32_t nm_rxsync_prologue(struct netmap_kring *, struct netmap_ring *);
1406
1407
1408 /* check/fix address and len in tx rings */
1409 #if 1 /* debug version */
1410 #define NM_CHECK_ADDR_LEN(_na, _a, _l) do { \
1411 if (_a == NETMAP_BUF_BASE(_na) || _l > NETMAP_BUF_SIZE(_na)) { \
1412 nm_prlim(5, "bad addr/len ring %d slot %d idx %d len %d", \
1413 kring->ring_id, nm_i, slot->buf_idx, len); \
1414 if (_l > NETMAP_BUF_SIZE(_na)) \
1415 _l = NETMAP_BUF_SIZE(_na); \
1416 } } while (0)
1417 #else /* no debug version */
1418 #define NM_CHECK_ADDR_LEN(_na, _a, _l) do { \
1419 if (_l > NETMAP_BUF_SIZE(_na)) \
1420 _l = NETMAP_BUF_SIZE(_na); \
1421 } while (0)
1422 #endif
1423
1424
1425 /*---------------------------------------------------------------*/
1426 /*
1427 * Support routines used by netmap subsystems
1428 * (native drivers, VALE, generic, pipes, monitors, ...)
1429 */
1430
1431
1432 /* common routine for all functions that create a netmap adapter. It performs
1433 * two main tasks:
1434 * - if the na points to an ifp, mark the ifp as netmap capable
1435 * using na as its native adapter;
1436 * - provide defaults for the setup callbacks and the memory allocator
1437 */
1438 int netmap_attach_common(struct netmap_adapter *);
1439 /* fill priv->np_[tr]xq{first,last} using the ringid and flags information
1440 * coming from a struct nmreq_register
1441 */
1442 int netmap_interp_ringid(struct netmap_priv_d *priv, struct nmreq_header *hdr);
1443 /* update the ring parameters (number and size of tx and rx rings).
1444 * It calls the nm_config callback, if available.
1445 */
1446 int netmap_update_config(struct netmap_adapter *na);
1447 /* create and initialize the common fields of the krings array.
1448 * using the information that must be already available in the na.
1449 * tailroom can be used to request the allocation of additional
1450 * tailroom bytes after the krings array. This is used by
1451 * netmap_vp_adapter's (i.e., VALE ports) to make room for
1452 * leasing-related data structures
1453 */
1454 int netmap_krings_create(struct netmap_adapter *na, u_int tailroom);
1455 /* deletes the kring array of the adapter. The array must have
1456 * been created using netmap_krings_create
1457 */
1458 void netmap_krings_delete(struct netmap_adapter *na);
1459
1460 int netmap_hw_krings_create(struct netmap_adapter *na);
1461 void netmap_hw_krings_delete(struct netmap_adapter *na);
1462
1463 /* set the stopped/enabled status of ring
1464 * When stopping, they also wait for all current activity on the ring to
1465 * terminate. The status change is then notified using the na nm_notify
1466 * callback.
1467 */
1468 void netmap_set_ring(struct netmap_adapter *, u_int ring_id, enum txrx, int stopped);
1469 /* set the stopped/enabled status of all rings of the adapter. */
1470 void netmap_set_all_rings(struct netmap_adapter *, int stopped);
1471 /* convenience wrappers for netmap_set_all_rings */
1472 void netmap_disable_all_rings(struct ifnet *);
1473 void netmap_enable_all_rings(struct ifnet *);
1474
1475 int netmap_buf_size_validate(const struct netmap_adapter *na, unsigned mtu);
1476 int netmap_do_regif(struct netmap_priv_d *priv, struct netmap_adapter *na,
1477 struct nmreq_header *);
1478 void netmap_do_unregif(struct netmap_priv_d *priv);
1479
1480 u_int nm_bound_var(u_int *v, u_int dflt, u_int lo, u_int hi, const char *msg);
1481 int netmap_get_na(struct nmreq_header *hdr, struct netmap_adapter **na,
1482 struct ifnet **ifp, struct netmap_mem_d *nmd, int create);
1483 void netmap_unget_na(struct netmap_adapter *na, struct ifnet *ifp);
1484 int netmap_get_hw_na(struct ifnet *ifp,
1485 struct netmap_mem_d *nmd, struct netmap_adapter **na);
1486
1487 #ifdef WITH_VALE
1488 uint32_t netmap_vale_learning(struct nm_bdg_fwd *ft, uint8_t *dst_ring,
1489 struct netmap_vp_adapter *, void *private_data);
1490
1491 /* these are redefined in case of no VALE support */
1492 int netmap_get_vale_na(struct nmreq_header *hdr, struct netmap_adapter **na,
1493 struct netmap_mem_d *nmd, int create);
1494 void *netmap_vale_create(const char *bdg_name, int *return_status);
1495 int netmap_vale_destroy(const char *bdg_name, void *auth_token);
1496
1497 extern unsigned int vale_max_bridges;
1498
1499 #else /* !WITH_VALE */
1500 #define netmap_bdg_learning(_1, _2, _3, _4) 0
1501 #define netmap_get_vale_na(_1, _2, _3, _4) 0
1502 #define netmap_bdg_create(_1, _2) NULL
1503 #define netmap_bdg_destroy(_1, _2) 0
1504 #define vale_max_bridges 1
1505 #endif /* !WITH_VALE */
1506
1507 #ifdef WITH_PIPES
1508 /* max number of pipes per device */
1509 #define NM_MAXPIPES 64 /* XXX this should probably be a sysctl */
1510 void netmap_pipe_dealloc(struct netmap_adapter *);
1511 int netmap_get_pipe_na(struct nmreq_header *hdr, struct netmap_adapter **na,
1512 struct netmap_mem_d *nmd, int create);
1513 #else /* !WITH_PIPES */
1514 #define NM_MAXPIPES 0
1515 #define netmap_pipe_alloc(_1, _2) 0
1516 #define netmap_pipe_dealloc(_1)
1517 #define netmap_get_pipe_na(hdr, _2, _3, _4) \
1518 ((strchr(hdr->nr_name, '{') != NULL || strchr(hdr->nr_name, '}') != NULL) ? EOPNOTSUPP : 0)
1519 #endif
1520
1521 #ifdef WITH_MONITOR
1522 int netmap_get_monitor_na(struct nmreq_header *hdr, struct netmap_adapter **na,
1523 struct netmap_mem_d *nmd, int create);
1524 void netmap_monitor_stop(struct netmap_adapter *na);
1525 #else
1526 #define netmap_get_monitor_na(hdr, _2, _3, _4) \
1527 (((struct nmreq_register *)(uintptr_t)hdr->nr_body)->nr_flags & (NR_MONITOR_TX | NR_MONITOR_RX) ? EOPNOTSUPP : 0)
1528 #endif
1529
1530 #ifdef WITH_NMNULL
1531 int netmap_get_null_na(struct nmreq_header *hdr, struct netmap_adapter **na,
1532 struct netmap_mem_d *nmd, int create);
1533 #else /* !WITH_NMNULL */
1534 #define netmap_get_null_na(hdr, _2, _3, _4) \
1535 (((struct nmreq_register *)(uintptr_t)hdr->nr_body)->nr_flags & (NR_MONITOR_TX | NR_MONITOR_RX) ? EOPNOTSUPP : 0)
1536 #endif /* WITH_NMNULL */
1537
1538 #ifdef CONFIG_NET_NS
1539 struct net *netmap_bns_get(void);
1540 void netmap_bns_put(struct net *);
1541 void netmap_bns_getbridges(struct nm_bridge **, u_int *);
1542 #else
1543 extern struct nm_bridge *nm_bridges;
1544 #define netmap_bns_get()
1545 #define netmap_bns_put(_1)
1546 #define netmap_bns_getbridges(b, n) \
1547 do { *b = nm_bridges; *n = vale_max_bridges; } while (0)
1548 #endif
1549
1550 /* Various prototypes */
1551 int netmap_poll(struct netmap_priv_d *, int events, NM_SELRECORD_T *td);
1552 int netmap_init(void);
1553 void netmap_fini(void);
1554 int netmap_get_memory(struct netmap_priv_d* p);
1555 void netmap_dtor(void *data);
1556
1557 int netmap_ioctl(struct netmap_priv_d *priv, u_long cmd, caddr_t data,
1558 struct thread *, int nr_body_is_user);
1559 int netmap_ioctl_legacy(struct netmap_priv_d *priv, u_long cmd, caddr_t data,
1560 struct thread *td);
1561 size_t nmreq_size_by_type(uint16_t nr_reqtype);
1562
1563 /* netmap_adapter creation/destruction */
1564
1565 // #define NM_DEBUG_PUTGET 1
1566
1567 #ifdef NM_DEBUG_PUTGET
1568
1569 #define NM_DBG(f) __##f
1570
1571 void __netmap_adapter_get(struct netmap_adapter *na);
1572
1573 #define netmap_adapter_get(na) \
1574 do { \
1575 struct netmap_adapter *__na = na; \
1576 __netmap_adapter_get(__na); \
1577 nm_prinf("getting %p:%s -> %d", __na, (__na)->name, (__na)->na_refcount); \
1578 } while (0)
1579
1580 int __netmap_adapter_put(struct netmap_adapter *na);
1581
1582 #define netmap_adapter_put(na) \
1583 ({ \
1584 struct netmap_adapter *__na = na; \
1585 if (__na == NULL) \
1586 nm_prinf("putting NULL"); \
1587 else \
1588 nm_prinf("putting %p:%s -> %d", __na, (__na)->name, (__na)->na_refcount - 1); \
1589 __netmap_adapter_put(__na); \
1590 })
1591
1592 #else /* !NM_DEBUG_PUTGET */
1593
1594 #define NM_DBG(f) f
1595 void netmap_adapter_get(struct netmap_adapter *na);
1596 int netmap_adapter_put(struct netmap_adapter *na);
1597
1598 #endif /* !NM_DEBUG_PUTGET */
1599
1600
1601 /*
1602 * module variables
1603 */
1604 #define NETMAP_BUF_BASE(_na) ((_na)->na_lut.lut[0].vaddr)
1605 #define NETMAP_BUF_SIZE(_na) ((_na)->na_lut.objsize)
1606 extern int netmap_no_pendintr;
1607 extern int netmap_verbose;
1608 #ifdef CONFIG_NETMAP_DEBUG
1609 extern int netmap_debug; /* for debugging */
1610 #else /* !CONFIG_NETMAP_DEBUG */
1611 #define netmap_debug (0)
1612 #endif /* !CONFIG_NETMAP_DEBUG */
1613 enum { /* debug flags */
1614 NM_DEBUG_ON = 1, /* generic debug messsages */
1615 NM_DEBUG_HOST = 0x2, /* debug host stack */
1616 NM_DEBUG_RXSYNC = 0x10, /* debug on rxsync/txsync */
1617 NM_DEBUG_TXSYNC = 0x20,
1618 NM_DEBUG_RXINTR = 0x100, /* debug on rx/tx intr (driver) */
1619 NM_DEBUG_TXINTR = 0x200,
1620 NM_DEBUG_NIC_RXSYNC = 0x1000, /* debug on rx/tx intr (driver) */
1621 NM_DEBUG_NIC_TXSYNC = 0x2000,
1622 NM_DEBUG_MEM = 0x4000, /* verbose memory allocations/deallocations */
1623 NM_DEBUG_VALE = 0x8000, /* debug messages from memory allocators */
1624 NM_DEBUG_BDG = NM_DEBUG_VALE,
1625 };
1626
1627 extern int netmap_txsync_retry;
1628 extern int netmap_generic_hwcsum;
1629 extern int netmap_generic_mit;
1630 extern int netmap_generic_ringsize;
1631 extern int netmap_generic_rings;
1632 #ifdef linux
1633 extern int netmap_generic_txqdisc;
1634 #endif
1635
1636 /*
1637 * NA returns a pointer to the struct netmap adapter from the ifp.
1638 * WNA is os-specific and must be defined in glue code.
1639 */
1640 #define NA(_ifp) ((struct netmap_adapter *)WNA(_ifp))
1641
1642 /*
1643 * we provide a default implementation of NM_ATTACH_NA/NM_DETACH_NA
1644 * based on the WNA field.
1645 * Glue code may override this by defining its own NM_ATTACH_NA
1646 */
1647 #ifndef NM_ATTACH_NA
1648 /*
1649 * On old versions of FreeBSD, NA(ifp) is a pspare. On linux we
1650 * overload another pointer in the netdev.
1651 *
1652 * We check if NA(ifp) is set and its first element has a related
1653 * magic value. The capenable is within the struct netmap_adapter.
1654 */
1655 #define NETMAP_MAGIC 0x52697a7a
1656
1657 #define NM_NA_VALID(ifp) (NA(ifp) && \
1658 ((uint32_t)(uintptr_t)NA(ifp) ^ NA(ifp)->magic) == NETMAP_MAGIC )
1659
1660 #define NM_ATTACH_NA(ifp, na) do { \
1661 WNA(ifp) = na; \
1662 if (NA(ifp)) \
1663 NA(ifp)->magic = \
1664 ((uint32_t)(uintptr_t)NA(ifp)) ^ NETMAP_MAGIC; \
1665 } while(0)
1666 #define NM_RESTORE_NA(ifp, na) WNA(ifp) = na;
1667
1668 #define NM_DETACH_NA(ifp) do { WNA(ifp) = NULL; } while (0)
1669 #define NM_NA_CLASH(ifp) (NA(ifp) && !NM_NA_VALID(ifp))
1670 #endif /* !NM_ATTACH_NA */
1671
1672
1673 #define NM_IS_NATIVE(ifp) (NM_NA_VALID(ifp) && NA(ifp)->nm_dtor == netmap_hw_dtor)
1674
1675 #if defined(__FreeBSD__)
1676
1677 /* Assigns the device IOMMU domain to an allocator.
1678 * Returns -ENOMEM in case the domain is different */
1679 #define nm_iommu_group_id(dev) (0)
1680
1681 /* Callback invoked by the dma machinery after a successful dmamap_load */
netmap_dmamap_cb(__unused void * arg,__unused bus_dma_segment_t * segs,__unused int nseg,__unused int error)1682 static void netmap_dmamap_cb(__unused void *arg,
1683 __unused bus_dma_segment_t * segs, __unused int nseg, __unused int error)
1684 {
1685 }
1686
1687 /* bus_dmamap_load wrapper: call aforementioned function if map != NULL.
1688 * XXX can we do it without a callback ?
1689 */
1690 static inline int
netmap_load_map(struct netmap_adapter * na,bus_dma_tag_t tag,bus_dmamap_t map,void * buf)1691 netmap_load_map(struct netmap_adapter *na,
1692 bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
1693 {
1694 if (map)
1695 bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE(na),
1696 netmap_dmamap_cb, NULL, BUS_DMA_NOWAIT);
1697 return 0;
1698 }
1699
1700 static inline void
netmap_unload_map(struct netmap_adapter * na,bus_dma_tag_t tag,bus_dmamap_t map)1701 netmap_unload_map(struct netmap_adapter *na,
1702 bus_dma_tag_t tag, bus_dmamap_t map)
1703 {
1704 if (map)
1705 bus_dmamap_unload(tag, map);
1706 }
1707
1708 #define netmap_sync_map(na, tag, map, sz, t)
1709
1710 /* update the map when a buffer changes. */
1711 static inline void
netmap_reload_map(struct netmap_adapter * na,bus_dma_tag_t tag,bus_dmamap_t map,void * buf)1712 netmap_reload_map(struct netmap_adapter *na,
1713 bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
1714 {
1715 if (map) {
1716 bus_dmamap_unload(tag, map);
1717 bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE(na),
1718 netmap_dmamap_cb, NULL, BUS_DMA_NOWAIT);
1719 }
1720 }
1721
1722 #elif defined(_WIN32)
1723
1724 #else /* linux */
1725
1726 int nm_iommu_group_id(bus_dma_tag_t dev);
1727 #include <linux/dma-mapping.h>
1728
1729 /*
1730 * on linux we need
1731 * dma_map_single(&pdev->dev, virt_addr, len, direction)
1732 * dma_unmap_single(&adapter->pdev->dev, phys_addr, len, direction)
1733 */
1734 #if 0
1735 struct e1000_buffer *buffer_info = &tx_ring->buffer_info[l];
1736 /* set time_stamp *before* dma to help avoid a possible race */
1737 buffer_info->time_stamp = jiffies;
1738 buffer_info->mapped_as_page = false;
1739 buffer_info->length = len;
1740 //buffer_info->next_to_watch = l;
1741 /* reload dma map */
1742 dma_unmap_single(&adapter->pdev->dev, buffer_info->dma,
1743 NETMAP_BUF_SIZE, DMA_TO_DEVICE);
1744 buffer_info->dma = dma_map_single(&adapter->pdev->dev,
1745 addr, NETMAP_BUF_SIZE, DMA_TO_DEVICE);
1746
1747 if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)) {
1748 nm_prerr("dma mapping error");
1749 /* goto dma_error; See e1000_put_txbuf() */
1750 /* XXX reset */
1751 }
1752 tx_desc->buffer_addr = htole64(buffer_info->dma); //XXX
1753
1754 #endif
1755
1756 static inline int
netmap_load_map(struct netmap_adapter * na,bus_dma_tag_t tag,bus_dmamap_t map,void * buf,u_int size)1757 netmap_load_map(struct netmap_adapter *na,
1758 bus_dma_tag_t tag, bus_dmamap_t map, void *buf, u_int size)
1759 {
1760 if (map) {
1761 *map = dma_map_single(na->pdev, buf, size,
1762 DMA_BIDIRECTIONAL);
1763 if (dma_mapping_error(na->pdev, *map)) {
1764 *map = 0;
1765 return ENOMEM;
1766 }
1767 }
1768 return 0;
1769 }
1770
1771 static inline void
netmap_unload_map(struct netmap_adapter * na,bus_dma_tag_t tag,bus_dmamap_t map,u_int sz)1772 netmap_unload_map(struct netmap_adapter *na,
1773 bus_dma_tag_t tag, bus_dmamap_t map, u_int sz)
1774 {
1775 if (*map) {
1776 dma_unmap_single(na->pdev, *map, sz,
1777 DMA_BIDIRECTIONAL);
1778 }
1779 }
1780
1781 #ifdef NETMAP_LINUX_HAVE_DMASYNC
1782 static inline void
netmap_sync_map_cpu(struct netmap_adapter * na,bus_dma_tag_t tag,bus_dmamap_t map,u_int sz,enum txrx t)1783 netmap_sync_map_cpu(struct netmap_adapter *na,
1784 bus_dma_tag_t tag, bus_dmamap_t map, u_int sz, enum txrx t)
1785 {
1786 if (*map) {
1787 dma_sync_single_for_cpu(na->pdev, *map, sz,
1788 (t == NR_TX ? DMA_TO_DEVICE : DMA_FROM_DEVICE));
1789 }
1790 }
1791
1792 static inline void
netmap_sync_map_dev(struct netmap_adapter * na,bus_dma_tag_t tag,bus_dmamap_t map,u_int sz,enum txrx t)1793 netmap_sync_map_dev(struct netmap_adapter *na,
1794 bus_dma_tag_t tag, bus_dmamap_t map, u_int sz, enum txrx t)
1795 {
1796 if (*map) {
1797 dma_sync_single_for_device(na->pdev, *map, sz,
1798 (t == NR_TX ? DMA_TO_DEVICE : DMA_FROM_DEVICE));
1799 }
1800 }
1801
1802 static inline void
netmap_reload_map(struct netmap_adapter * na,bus_dma_tag_t tag,bus_dmamap_t map,void * buf)1803 netmap_reload_map(struct netmap_adapter *na,
1804 bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
1805 {
1806 u_int sz = NETMAP_BUF_SIZE(na);
1807
1808 if (*map) {
1809 dma_unmap_single(na->pdev, *map, sz,
1810 DMA_BIDIRECTIONAL);
1811 }
1812
1813 *map = dma_map_single(na->pdev, buf, sz,
1814 DMA_BIDIRECTIONAL);
1815 }
1816 #else /* !NETMAP_LINUX_HAVE_DMASYNC */
1817 #define netmap_sync_map_cpu(na, tag, map, sz, t)
1818 #define netmap_sync_map_dev(na, tag, map, sz, t)
1819 #endif /* NETMAP_LINUX_HAVE_DMASYNC */
1820
1821 #endif /* linux */
1822
1823
1824 /*
1825 * functions to map NIC to KRING indexes (n2k) and vice versa (k2n)
1826 */
1827 static inline int
netmap_idx_n2k(struct netmap_kring * kr,int idx)1828 netmap_idx_n2k(struct netmap_kring *kr, int idx)
1829 {
1830 int n = kr->nkr_num_slots;
1831
1832 if (likely(kr->nkr_hwofs == 0)) {
1833 return idx;
1834 }
1835
1836 idx += kr->nkr_hwofs;
1837 if (idx < 0)
1838 return idx + n;
1839 else if (idx < n)
1840 return idx;
1841 else
1842 return idx - n;
1843 }
1844
1845
1846 static inline int
netmap_idx_k2n(struct netmap_kring * kr,int idx)1847 netmap_idx_k2n(struct netmap_kring *kr, int idx)
1848 {
1849 int n = kr->nkr_num_slots;
1850
1851 if (likely(kr->nkr_hwofs == 0)) {
1852 return idx;
1853 }
1854
1855 idx -= kr->nkr_hwofs;
1856 if (idx < 0)
1857 return idx + n;
1858 else if (idx < n)
1859 return idx;
1860 else
1861 return idx - n;
1862 }
1863
1864
1865 /* Entries of the look-up table. */
1866 #ifdef __FreeBSD__
1867 struct lut_entry {
1868 void *vaddr; /* virtual address. */
1869 vm_paddr_t paddr; /* physical address. */
1870 };
1871 #else /* linux & _WIN32 */
1872 /* dma-mapping in linux can assign a buffer a different address
1873 * depending on the device, so we need to have a separate
1874 * physical-address look-up table for each na.
1875 * We can still share the vaddrs, though, therefore we split
1876 * the lut_entry structure.
1877 */
1878 struct lut_entry {
1879 void *vaddr; /* virtual address. */
1880 };
1881
1882 struct plut_entry {
1883 vm_paddr_t paddr; /* physical address. */
1884 };
1885 #endif /* linux & _WIN32 */
1886
1887 struct netmap_obj_pool;
1888
1889 /*
1890 * NMB return the virtual address of a buffer (buffer 0 on bad index)
1891 * PNMB also fills the physical address
1892 */
1893 static inline void *
NMB(struct netmap_adapter * na,struct netmap_slot * slot)1894 NMB(struct netmap_adapter *na, struct netmap_slot *slot)
1895 {
1896 struct lut_entry *lut = na->na_lut.lut;
1897 uint32_t i = slot->buf_idx;
1898 return (unlikely(i >= na->na_lut.objtotal)) ?
1899 lut[0].vaddr : lut[i].vaddr;
1900 }
1901
1902 static inline void *
PNMB(struct netmap_adapter * na,struct netmap_slot * slot,uint64_t * pp)1903 PNMB(struct netmap_adapter *na, struct netmap_slot *slot, uint64_t *pp)
1904 {
1905 uint32_t i = slot->buf_idx;
1906 struct lut_entry *lut = na->na_lut.lut;
1907 struct plut_entry *plut = na->na_lut.plut;
1908 void *ret = (i >= na->na_lut.objtotal) ? lut[0].vaddr : lut[i].vaddr;
1909
1910 #ifdef _WIN32
1911 *pp = (i >= na->na_lut.objtotal) ? (uint64_t)plut[0].paddr.QuadPart : (uint64_t)plut[i].paddr.QuadPart;
1912 #else
1913 *pp = (i >= na->na_lut.objtotal) ? plut[0].paddr : plut[i].paddr;
1914 #endif
1915 return ret;
1916 }
1917
1918
1919 /*
1920 * Structure associated to each netmap file descriptor.
1921 * It is created on open and left unbound (np_nifp == NULL).
1922 * A successful NIOCREGIF will set np_nifp and the first few fields;
1923 * this is protected by a global lock (NMG_LOCK) due to low contention.
1924 *
1925 * np_refs counts the number of references to the structure: one for the fd,
1926 * plus (on FreeBSD) one for each active mmap which we track ourselves
1927 * (linux automatically tracks them, but FreeBSD does not).
1928 * np_refs is protected by NMG_LOCK.
1929 *
1930 * Read access to the structure is lock free, because ni_nifp once set
1931 * can only go to 0 when nobody is using the entry anymore. Readers
1932 * must check that np_nifp != NULL before using the other fields.
1933 */
1934 struct netmap_priv_d {
1935 struct netmap_if * volatile np_nifp; /* netmap if descriptor. */
1936
1937 struct netmap_adapter *np_na;
1938 struct ifnet *np_ifp;
1939 uint32_t np_flags; /* from the ioctl */
1940 u_int np_qfirst[NR_TXRX],
1941 np_qlast[NR_TXRX]; /* range of tx/rx rings to scan */
1942 uint16_t np_txpoll;
1943 uint16_t np_kloop_state; /* use with NMG_LOCK held */
1944 #define NM_SYNC_KLOOP_RUNNING (1 << 0)
1945 #define NM_SYNC_KLOOP_STOPPING (1 << 1)
1946 int np_sync_flags; /* to be passed to nm_sync */
1947
1948 int np_refs; /* use with NMG_LOCK held */
1949
1950 /* pointers to the selinfo to be used for selrecord.
1951 * Either the local or the global one depending on the
1952 * number of rings.
1953 */
1954 NM_SELINFO_T *np_si[NR_TXRX];
1955
1956 /* In the optional CSB mode, the user must specify the start address
1957 * of two arrays of Communication Status Block (CSB) entries, for the
1958 * two directions (kernel read application write, and kernel write
1959 * application read).
1960 * The number of entries must agree with the number of rings bound to
1961 * the netmap file descriptor. The entries corresponding to the TX
1962 * rings are laid out before the ones corresponding to the RX rings.
1963 *
1964 * Array of CSB entries for application --> kernel communication
1965 * (N entries). */
1966 struct nm_csb_atok *np_csb_atok_base;
1967 /* Array of CSB entries for kernel --> application communication
1968 * (N entries). */
1969 struct nm_csb_ktoa *np_csb_ktoa_base;
1970
1971 #ifdef linux
1972 struct file *np_filp; /* used by sync kloop */
1973 #endif /* linux */
1974 };
1975
1976 struct netmap_priv_d *netmap_priv_new(void);
1977 void netmap_priv_delete(struct netmap_priv_d *);
1978
nm_kring_pending(struct netmap_priv_d * np)1979 static inline int nm_kring_pending(struct netmap_priv_d *np)
1980 {
1981 struct netmap_adapter *na = np->np_na;
1982 enum txrx t;
1983 int i;
1984
1985 for_rx_tx(t) {
1986 for (i = np->np_qfirst[t]; i < np->np_qlast[t]; i++) {
1987 struct netmap_kring *kring = NMR(na, t)[i];
1988 if (kring->nr_mode != kring->nr_pending_mode) {
1989 return 1;
1990 }
1991 }
1992 }
1993 return 0;
1994 }
1995
1996 /* call with NMG_LOCK held */
1997 static __inline int
nm_si_user(struct netmap_priv_d * priv,enum txrx t)1998 nm_si_user(struct netmap_priv_d *priv, enum txrx t)
1999 {
2000 return (priv->np_na != NULL &&
2001 (priv->np_qlast[t] - priv->np_qfirst[t] > 1));
2002 }
2003
2004 #ifdef WITH_PIPES
2005 int netmap_pipe_txsync(struct netmap_kring *txkring, int flags);
2006 int netmap_pipe_rxsync(struct netmap_kring *rxkring, int flags);
2007 int netmap_pipe_krings_create_both(struct netmap_adapter *na,
2008 struct netmap_adapter *ona);
2009 void netmap_pipe_krings_delete_both(struct netmap_adapter *na,
2010 struct netmap_adapter *ona);
2011 int netmap_pipe_reg_both(struct netmap_adapter *na,
2012 struct netmap_adapter *ona);
2013 #endif /* WITH_PIPES */
2014
2015 #ifdef WITH_MONITOR
2016
2017 struct netmap_monitor_adapter {
2018 struct netmap_adapter up;
2019
2020 struct netmap_priv_d priv;
2021 uint32_t flags;
2022 };
2023
2024 #endif /* WITH_MONITOR */
2025
2026
2027 #ifdef WITH_GENERIC
2028 /*
2029 * generic netmap emulation for devices that do not have
2030 * native netmap support.
2031 */
2032 int generic_netmap_attach(struct ifnet *ifp);
2033 int generic_rx_handler(struct ifnet *ifp, struct mbuf *m);;
2034
2035 int nm_os_catch_rx(struct netmap_generic_adapter *gna, int intercept);
2036 int nm_os_catch_tx(struct netmap_generic_adapter *gna, int intercept);
2037
2038 int na_is_generic(struct netmap_adapter *na);
2039
2040 /*
2041 * the generic transmit routine is passed a structure to optionally
2042 * build a queue of descriptors, in an OS-specific way.
2043 * The payload is at addr, if non-null, and the routine should send or queue
2044 * the packet, returning 0 if successful, 1 on failure.
2045 *
2046 * At the end, if head is non-null, there will be an additional call
2047 * to the function with addr = NULL; this should tell the OS-specific
2048 * routine to send the queue and free any resources. Failure is ignored.
2049 */
2050 struct nm_os_gen_arg {
2051 struct ifnet *ifp;
2052 void *m; /* os-specific mbuf-like object */
2053 void *head, *tail; /* tailq, if the OS-specific routine needs to build one */
2054 void *addr; /* payload of current packet */
2055 u_int len; /* packet length */
2056 u_int ring_nr; /* packet length */
2057 u_int qevent; /* in txqdisc mode, place an event on this mbuf */
2058 };
2059
2060 int nm_os_generic_xmit_frame(struct nm_os_gen_arg *);
2061 int nm_os_generic_find_num_desc(struct ifnet *ifp, u_int *tx, u_int *rx);
2062 void nm_os_generic_find_num_queues(struct ifnet *ifp, u_int *txq, u_int *rxq);
2063 void nm_os_generic_set_features(struct netmap_generic_adapter *gna);
2064
2065 static inline struct ifnet*
netmap_generic_getifp(struct netmap_generic_adapter * gna)2066 netmap_generic_getifp(struct netmap_generic_adapter *gna)
2067 {
2068 if (gna->prev)
2069 return gna->prev->ifp;
2070
2071 return gna->up.up.ifp;
2072 }
2073
2074 void netmap_generic_irq(struct netmap_adapter *na, u_int q, u_int *work_done);
2075
2076 //#define RATE_GENERIC /* Enables communication statistics for generic. */
2077 #ifdef RATE_GENERIC
2078 void generic_rate(int txp, int txs, int txi, int rxp, int rxs, int rxi);
2079 #else
2080 #define generic_rate(txp, txs, txi, rxp, rxs, rxi)
2081 #endif
2082
2083 /*
2084 * netmap_mitigation API. This is used by the generic adapter
2085 * to reduce the number of interrupt requests/selwakeup
2086 * to clients on incoming packets.
2087 */
2088 void nm_os_mitigation_init(struct nm_generic_mit *mit, int idx,
2089 struct netmap_adapter *na);
2090 void nm_os_mitigation_start(struct nm_generic_mit *mit);
2091 void nm_os_mitigation_restart(struct nm_generic_mit *mit);
2092 int nm_os_mitigation_active(struct nm_generic_mit *mit);
2093 void nm_os_mitigation_cleanup(struct nm_generic_mit *mit);
2094 #else /* !WITH_GENERIC */
2095 #define generic_netmap_attach(ifp) (EOPNOTSUPP)
2096 #define na_is_generic(na) (0)
2097 #endif /* WITH_GENERIC */
2098
2099 /* Shared declarations for the VALE switch. */
2100
2101 /*
2102 * Each transmit queue accumulates a batch of packets into
2103 * a structure before forwarding. Packets to the same
2104 * destination are put in a list using ft_next as a link field.
2105 * ft_frags and ft_next are valid only on the first fragment.
2106 */
2107 struct nm_bdg_fwd { /* forwarding entry for a bridge */
2108 void *ft_buf; /* netmap or indirect buffer */
2109 uint8_t ft_frags; /* how many fragments (only on 1st frag) */
2110 uint16_t ft_offset; /* dst port (unused) */
2111 uint16_t ft_flags; /* flags, e.g. indirect */
2112 uint16_t ft_len; /* src fragment len */
2113 uint16_t ft_next; /* next packet to same destination */
2114 };
2115
2116 /* struct 'virtio_net_hdr' from linux. */
2117 struct nm_vnet_hdr {
2118 #define VIRTIO_NET_HDR_F_NEEDS_CSUM 1 /* Use csum_start, csum_offset */
2119 #define VIRTIO_NET_HDR_F_DATA_VALID 2 /* Csum is valid */
2120 uint8_t flags;
2121 #define VIRTIO_NET_HDR_GSO_NONE 0 /* Not a GSO frame */
2122 #define VIRTIO_NET_HDR_GSO_TCPV4 1 /* GSO frame, IPv4 TCP (TSO) */
2123 #define VIRTIO_NET_HDR_GSO_UDP 3 /* GSO frame, IPv4 UDP (UFO) */
2124 #define VIRTIO_NET_HDR_GSO_TCPV6 4 /* GSO frame, IPv6 TCP */
2125 #define VIRTIO_NET_HDR_GSO_ECN 0x80 /* TCP has ECN set */
2126 uint8_t gso_type;
2127 uint16_t hdr_len;
2128 uint16_t gso_size;
2129 uint16_t csum_start;
2130 uint16_t csum_offset;
2131 };
2132
2133 #define WORST_CASE_GSO_HEADER (14+40+60) /* IPv6 + TCP */
2134
2135 /* Private definitions for IPv4, IPv6, UDP and TCP headers. */
2136
2137 struct nm_iphdr {
2138 uint8_t version_ihl;
2139 uint8_t tos;
2140 uint16_t tot_len;
2141 uint16_t id;
2142 uint16_t frag_off;
2143 uint8_t ttl;
2144 uint8_t protocol;
2145 uint16_t check;
2146 uint32_t saddr;
2147 uint32_t daddr;
2148 /*The options start here. */
2149 };
2150
2151 struct nm_tcphdr {
2152 uint16_t source;
2153 uint16_t dest;
2154 uint32_t seq;
2155 uint32_t ack_seq;
2156 uint8_t doff; /* Data offset + Reserved */
2157 uint8_t flags;
2158 uint16_t window;
2159 uint16_t check;
2160 uint16_t urg_ptr;
2161 };
2162
2163 struct nm_udphdr {
2164 uint16_t source;
2165 uint16_t dest;
2166 uint16_t len;
2167 uint16_t check;
2168 };
2169
2170 struct nm_ipv6hdr {
2171 uint8_t priority_version;
2172 uint8_t flow_lbl[3];
2173
2174 uint16_t payload_len;
2175 uint8_t nexthdr;
2176 uint8_t hop_limit;
2177
2178 uint8_t saddr[16];
2179 uint8_t daddr[16];
2180 };
2181
2182 /* Type used to store a checksum (in host byte order) that hasn't been
2183 * folded yet.
2184 */
2185 #define rawsum_t uint32_t
2186
2187 rawsum_t nm_os_csum_raw(uint8_t *data, size_t len, rawsum_t cur_sum);
2188 uint16_t nm_os_csum_ipv4(struct nm_iphdr *iph);
2189 void nm_os_csum_tcpudp_ipv4(struct nm_iphdr *iph, void *data,
2190 size_t datalen, uint16_t *check);
2191 void nm_os_csum_tcpudp_ipv6(struct nm_ipv6hdr *ip6h, void *data,
2192 size_t datalen, uint16_t *check);
2193 uint16_t nm_os_csum_fold(rawsum_t cur_sum);
2194
2195 void bdg_mismatch_datapath(struct netmap_vp_adapter *na,
2196 struct netmap_vp_adapter *dst_na,
2197 const struct nm_bdg_fwd *ft_p,
2198 struct netmap_ring *dst_ring,
2199 u_int *j, u_int lim, u_int *howmany);
2200
2201 /* persistent virtual port routines */
2202 int nm_os_vi_persist(const char *, struct ifnet **);
2203 void nm_os_vi_detach(struct ifnet *);
2204 void nm_os_vi_init_index(void);
2205
2206 /*
2207 * kernel thread routines
2208 */
2209 struct nm_kctx; /* OS-specific kernel context - opaque */
2210 typedef void (*nm_kctx_worker_fn_t)(void *data);
2211
2212 /* kthread configuration */
2213 struct nm_kctx_cfg {
2214 long type; /* kthread type/identifier */
2215 nm_kctx_worker_fn_t worker_fn; /* worker function */
2216 void *worker_private;/* worker parameter */
2217 int attach_user; /* attach kthread to user process */
2218 };
2219 /* kthread configuration */
2220 struct nm_kctx *nm_os_kctx_create(struct nm_kctx_cfg *cfg,
2221 void *opaque);
2222 int nm_os_kctx_worker_start(struct nm_kctx *);
2223 void nm_os_kctx_worker_stop(struct nm_kctx *);
2224 void nm_os_kctx_destroy(struct nm_kctx *);
2225 void nm_os_kctx_worker_setaff(struct nm_kctx *, int);
2226 u_int nm_os_ncpus(void);
2227
2228 int netmap_sync_kloop(struct netmap_priv_d *priv,
2229 struct nmreq_header *hdr);
2230 int netmap_sync_kloop_stop(struct netmap_priv_d *priv);
2231
2232 #ifdef WITH_PTNETMAP
2233 /* ptnetmap guest routines */
2234
2235 /*
2236 * ptnetmap_memdev routines used to talk with ptnetmap_memdev device driver
2237 */
2238 struct ptnetmap_memdev;
2239 int nm_os_pt_memdev_iomap(struct ptnetmap_memdev *, vm_paddr_t *, void **,
2240 uint64_t *);
2241 void nm_os_pt_memdev_iounmap(struct ptnetmap_memdev *);
2242 uint32_t nm_os_pt_memdev_ioread(struct ptnetmap_memdev *, unsigned int);
2243
2244 /*
2245 * netmap adapter for guest ptnetmap ports
2246 */
2247 struct netmap_pt_guest_adapter {
2248 /* The netmap adapter to be used by netmap applications.
2249 * This field must be the first, to allow upcast. */
2250 struct netmap_hw_adapter hwup;
2251
2252 /* The netmap adapter to be used by the driver. */
2253 struct netmap_hw_adapter dr;
2254
2255 /* Reference counter to track users of backend netmap port: the
2256 * network stack and netmap clients.
2257 * Used to decide when we need (de)allocate krings/rings and
2258 * start (stop) ptnetmap kthreads. */
2259 int backend_users;
2260
2261 };
2262
2263 int netmap_pt_guest_attach(struct netmap_adapter *na,
2264 unsigned int nifp_offset,
2265 unsigned int memid);
2266 bool netmap_pt_guest_txsync(struct nm_csb_atok *atok,
2267 struct nm_csb_ktoa *ktoa,
2268 struct netmap_kring *kring, int flags);
2269 bool netmap_pt_guest_rxsync(struct nm_csb_atok *atok,
2270 struct nm_csb_ktoa *ktoa,
2271 struct netmap_kring *kring, int flags);
2272 int ptnet_nm_krings_create(struct netmap_adapter *na);
2273 void ptnet_nm_krings_delete(struct netmap_adapter *na);
2274 void ptnet_nm_dtor(struct netmap_adapter *na);
2275
2276 /* Helper function wrapping nm_sync_kloop_appl_read(). */
2277 static inline void
ptnet_sync_tail(struct nm_csb_ktoa * ktoa,struct netmap_kring * kring)2278 ptnet_sync_tail(struct nm_csb_ktoa *ktoa, struct netmap_kring *kring)
2279 {
2280 struct netmap_ring *ring = kring->ring;
2281
2282 /* Update hwcur and hwtail as known by the host. */
2283 nm_sync_kloop_appl_read(ktoa, &kring->nr_hwtail, &kring->nr_hwcur);
2284
2285 /* nm_sync_finalize */
2286 ring->tail = kring->rtail = kring->nr_hwtail;
2287 }
2288 #endif /* WITH_PTNETMAP */
2289
2290 #ifdef __FreeBSD__
2291 /*
2292 * FreeBSD mbuf allocator/deallocator in emulation mode:
2293 *
2294 * We allocate mbufs with m_gethdr(), since the mbuf header is needed
2295 * by the driver. We also attach a customly-provided external storage,
2296 * which in this case is a netmap buffer. When calling m_extadd(), however
2297 * we pass a NULL address, since the real address (and length) will be
2298 * filled in by nm_os_generic_xmit_frame() right before calling
2299 * if_transmit().
2300 *
2301 * The dtor function does nothing, however we need it since mb_free_ext()
2302 * has a KASSERT(), checking that the mbuf dtor function is not NULL.
2303 */
2304
void_mbuf_dtor(struct mbuf * m)2305 static void void_mbuf_dtor(struct mbuf *m) { }
2306
2307 #define SET_MBUF_DESTRUCTOR(m, fn) do { \
2308 (m)->m_ext.ext_free = (fn != NULL) ? \
2309 (void *)fn : (void *)void_mbuf_dtor; \
2310 } while (0)
2311
2312 static inline struct mbuf *
nm_os_get_mbuf(struct ifnet * ifp,int len)2313 nm_os_get_mbuf(struct ifnet *ifp, int len)
2314 {
2315 struct mbuf *m;
2316
2317 (void)ifp;
2318 (void)len;
2319
2320 m = m_gethdr(M_NOWAIT, MT_DATA);
2321 if (m == NULL) {
2322 return m;
2323 }
2324
2325 m_extadd(m, NULL /* buf */, 0 /* size */, void_mbuf_dtor,
2326 NULL, NULL, 0, EXT_NET_DRV);
2327
2328 return m;
2329 }
2330
2331 #endif /* __FreeBSD__ */
2332
2333 struct nmreq_option * nmreq_getoption(struct nmreq_header *, uint16_t);
2334
2335 int netmap_init_bridges(void);
2336 void netmap_uninit_bridges(void);
2337
2338 /* Functions to read and write CSB fields from the kernel. */
2339 #if defined (linux)
2340 #define CSB_READ(csb, field, r) (get_user(r, &csb->field))
2341 #define CSB_WRITE(csb, field, v) (put_user(v, &csb->field))
2342 #else /* ! linux */
2343 #define CSB_READ(csb, field, r) (r = fuword32(&csb->field))
2344 #define CSB_WRITE(csb, field, v) (suword32(&csb->field, v))
2345 #endif /* ! linux */
2346
2347 #endif /* _NET_NETMAP_KERN_H_ */
2348