2 * Copyright (C) 2016-2018 Vincenzo Maffione
3 * Copyright (C) 2015 Stefano Garzarella
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #if defined(__FreeBSD__)
34 #include <sys/cdefs.h>
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/types.h>
38 #include <sys/selinfo.h>
39 #include <sys/socket.h>
41 #include <net/if_var.h>
42 #include <machine/bus.h>
44 #define usleep_range(_1, _2) \
45 pause_sbt("sync-kloop-sleep", SBT_1US * _1, SBT_1US * 1, C_ABSOLUTE)
49 #include <linux/file.h>
50 #include <linux/eventfd.h>
53 #include <net/netmap.h>
54 #include <dev/netmap/netmap_kern.h>
55 #include <net/netmap_virt.h>
56 #include <dev/netmap/netmap_mem2.h>
58 /* Support for eventfd-based notifications. */
60 #define SYNC_KLOOP_POLL
63 /* Write kring pointers (hwcur, hwtail) to the CSB.
64 * This routine is coupled with ptnetmap_guest_read_kring_csb(). */
66 sync_kloop_kernel_write(struct nm_csb_ktoa __user *ptr, uint32_t hwcur,
69 /* Issue a first store-store barrier to make sure writes to the
70 * netmap ring do not overcome updates on ktoa->hwcur and ktoa->hwtail. */
74 * The same scheme used in nm_sync_kloop_appl_write() applies here.
75 * We allow the application to read a value of hwcur more recent than the value
76 * of hwtail, since this would anyway result in a consistent view of the
77 * ring state (and hwcur can never wraparound hwtail, since hwcur must be
80 * The following memory barrier scheme is used to make this happen:
84 * STORE(hwcur) LOAD(hwtail)
85 * wmb() <-------------> rmb()
86 * STORE(hwtail) LOAD(hwcur)
88 CSB_WRITE(ptr, hwcur, hwcur);
90 CSB_WRITE(ptr, hwtail, hwtail);
93 /* Read kring pointers (head, cur, sync_flags) from the CSB.
94 * This routine is coupled with ptnetmap_guest_write_kring_csb(). */
96 sync_kloop_kernel_read(struct nm_csb_atok __user *ptr,
97 struct netmap_ring *shadow_ring,
101 * We place a memory barrier to make sure that the update of head never
102 * overtakes the update of cur.
103 * (see explanation in sync_kloop_kernel_write).
105 CSB_READ(ptr, head, shadow_ring->head);
107 CSB_READ(ptr, cur, shadow_ring->cur);
108 CSB_READ(ptr, sync_flags, shadow_ring->flags);
110 /* Make sure that loads from atok->head and atok->cur are not delayed
111 * after the loads from the netmap ring. */
115 /* Enable or disable application --> kernel kicks. */
117 csb_ktoa_kick_enable(struct nm_csb_ktoa __user *csb_ktoa, uint32_t val)
119 CSB_WRITE(csb_ktoa, kern_need_kick, val);
122 #ifdef SYNC_KLOOP_POLL
123 /* Are application interrupt enabled or disabled? */
124 static inline uint32_t
125 csb_atok_intr_enabled(struct nm_csb_atok __user *csb_atok)
129 CSB_READ(csb_atok, appl_need_kick, v);
133 #endif /* SYNC_KLOOP_POLL */
136 sync_kloop_kring_dump(const char *title, const struct netmap_kring *kring)
138 nm_prinf("%s, kring %s, hwcur %d, rhead %d, "
139 "rcur %d, rtail %d, hwtail %d",
140 title, kring->name, kring->nr_hwcur, kring->rhead,
141 kring->rcur, kring->rtail, kring->nr_hwtail);
144 struct sync_kloop_ring_args {
145 struct netmap_kring *kring;
146 struct nm_csb_atok *csb_atok;
147 struct nm_csb_ktoa *csb_ktoa;
148 #ifdef SYNC_KLOOP_POLL
149 struct eventfd_ctx *irq_ctx;
150 #endif /* SYNC_KLOOP_POLL */
154 netmap_sync_kloop_tx_ring(const struct sync_kloop_ring_args *a)
156 struct netmap_kring *kring = a->kring;
157 struct nm_csb_atok *csb_atok = a->csb_atok;
158 struct nm_csb_ktoa *csb_ktoa = a->csb_ktoa;
159 struct netmap_ring shadow_ring; /* shadow copy of the netmap_ring */
160 bool more_txspace = false;
164 num_slots = kring->nkr_num_slots;
166 /* Disable application --> kernel notifications. */
167 csb_ktoa_kick_enable(csb_ktoa, 0);
168 /* Copy the application kring pointers from the CSB */
169 sync_kloop_kernel_read(csb_atok, &shadow_ring, num_slots);
172 batch = shadow_ring.head - kring->nr_hwcur;
176 #ifdef PTN_TX_BATCH_LIM
177 if (batch > PTN_TX_BATCH_LIM(num_slots)) {
178 /* If application moves ahead too fast, let's cut the move so
179 * that we don't exceed our batch limit. */
180 uint32_t head_lim = kring->nr_hwcur + PTN_TX_BATCH_LIM(num_slots);
182 if (head_lim >= num_slots)
183 head_lim -= num_slots;
184 nm_prdis(1, "batch: %d head: %d head_lim: %d", batch, shadow_ring.head,
186 shadow_ring.head = head_lim;
187 batch = PTN_TX_BATCH_LIM(num_slots);
189 #endif /* PTN_TX_BATCH_LIM */
191 if (nm_kr_txspace(kring) <= (num_slots >> 1)) {
192 shadow_ring.flags |= NAF_FORCE_RECLAIM;
195 /* Netmap prologue */
196 shadow_ring.tail = kring->rtail;
197 if (unlikely(nm_txsync_prologue(kring, &shadow_ring) >= num_slots)) {
198 /* Reinit ring and enable notifications. */
199 netmap_ring_reinit(kring);
200 csb_ktoa_kick_enable(csb_ktoa, 1);
204 if (unlikely(netmap_debug & NM_DEBUG_TXSYNC)) {
205 sync_kloop_kring_dump("pre txsync", kring);
208 if (unlikely(kring->nm_sync(kring, shadow_ring.flags))) {
209 /* Reenable notifications. */
210 csb_ktoa_kick_enable(csb_ktoa, 1);
211 nm_prerr("txsync() failed");
217 * Copy kernel hwcur and hwtail into the CSB for the application sync(), and
218 * do the nm_sync_finalize.
220 sync_kloop_kernel_write(csb_ktoa, kring->nr_hwcur,
222 if (kring->rtail != kring->nr_hwtail) {
223 /* Some more room available in the parent adapter. */
224 kring->rtail = kring->nr_hwtail;
228 if (unlikely(netmap_debug & NM_DEBUG_TXSYNC)) {
229 sync_kloop_kring_dump("post txsync", kring);
232 /* Interrupt the application if needed. */
233 #ifdef SYNC_KLOOP_POLL
234 if (a->irq_ctx && more_txspace && csb_atok_intr_enabled(csb_atok)) {
235 /* Disable application kick to avoid sending unnecessary kicks */
236 eventfd_signal(a->irq_ctx, 1);
237 more_txspace = false;
239 #endif /* SYNC_KLOOP_POLL */
241 /* Read CSB to see if there is more work to do. */
242 sync_kloop_kernel_read(csb_atok, &shadow_ring, num_slots);
243 if (shadow_ring.head == kring->rhead) {
245 * No more packets to transmit. We enable notifications and
246 * go to sleep, waiting for a kick from the application when new
247 * new slots are ready for transmission.
249 /* Reenable notifications. */
250 csb_ktoa_kick_enable(csb_ktoa, 1);
251 /* Double check, with store-load memory barrier. */
253 sync_kloop_kernel_read(csb_atok, &shadow_ring, num_slots);
254 if (shadow_ring.head != kring->rhead) {
255 /* We won the race condition, there are more packets to
256 * transmit. Disable notifications and do another cycle */
257 csb_ktoa_kick_enable(csb_ktoa, 0);
263 if (nm_kr_txempty(kring)) {
264 /* No more available TX slots. We stop waiting for a notification
265 * from the backend (netmap_tx_irq). */
266 nm_prdis(1, "TX ring");
271 #ifdef SYNC_KLOOP_POLL
272 if (a->irq_ctx && more_txspace && csb_atok_intr_enabled(csb_atok)) {
273 eventfd_signal(a->irq_ctx, 1);
275 #endif /* SYNC_KLOOP_POLL */
278 /* RX cycle without receive any packets */
279 #define SYNC_LOOP_RX_DRY_CYCLES_MAX 2
282 sync_kloop_norxslots(struct netmap_kring *kring, uint32_t g_head)
284 return (NM_ACCESS_ONCE(kring->nr_hwtail) == nm_prev(g_head,
285 kring->nkr_num_slots - 1));
289 netmap_sync_kloop_rx_ring(const struct sync_kloop_ring_args *a)
292 struct netmap_kring *kring = a->kring;
293 struct nm_csb_atok *csb_atok = a->csb_atok;
294 struct nm_csb_ktoa *csb_ktoa = a->csb_ktoa;
295 struct netmap_ring shadow_ring; /* shadow copy of the netmap_ring */
297 bool some_recvd = false;
300 num_slots = kring->nkr_num_slots;
302 /* Get RX csb_atok and csb_ktoa pointers from the CSB. */
303 num_slots = kring->nkr_num_slots;
305 /* Disable notifications. */
306 csb_ktoa_kick_enable(csb_ktoa, 0);
307 /* Copy the application kring pointers from the CSB */
308 sync_kloop_kernel_read(csb_atok, &shadow_ring, num_slots);
313 /* Netmap prologue */
314 shadow_ring.tail = kring->rtail;
315 if (unlikely(nm_rxsync_prologue(kring, &shadow_ring) >= num_slots)) {
316 /* Reinit ring and enable notifications. */
317 netmap_ring_reinit(kring);
318 csb_ktoa_kick_enable(csb_ktoa, 1);
322 if (unlikely(netmap_debug & NM_DEBUG_RXSYNC)) {
323 sync_kloop_kring_dump("pre rxsync", kring);
326 if (unlikely(kring->nm_sync(kring, shadow_ring.flags))) {
327 /* Reenable notifications. */
328 csb_ktoa_kick_enable(csb_ktoa, 1);
329 nm_prerr("rxsync() failed");
335 * Copy kernel hwcur and hwtail into the CSB for the application sync()
337 hwtail = NM_ACCESS_ONCE(kring->nr_hwtail);
338 sync_kloop_kernel_write(csb_ktoa, kring->nr_hwcur, hwtail);
339 if (kring->rtail != hwtail) {
340 kring->rtail = hwtail;
347 if (unlikely(netmap_debug & NM_DEBUG_RXSYNC)) {
348 sync_kloop_kring_dump("post rxsync", kring);
351 #ifdef SYNC_KLOOP_POLL
352 /* Interrupt the application if needed. */
353 if (a->irq_ctx && some_recvd && csb_atok_intr_enabled(csb_atok)) {
354 /* Disable application kick to avoid sending unnecessary kicks */
355 eventfd_signal(a->irq_ctx, 1);
358 #endif /* SYNC_KLOOP_POLL */
360 /* Read CSB to see if there is more work to do. */
361 sync_kloop_kernel_read(csb_atok, &shadow_ring, num_slots);
362 if (sync_kloop_norxslots(kring, shadow_ring.head)) {
364 * No more slots available for reception. We enable notification and
365 * go to sleep, waiting for a kick from the application when new receive
366 * slots are available.
368 /* Reenable notifications. */
369 csb_ktoa_kick_enable(csb_ktoa, 1);
370 /* Double check, with store-load memory barrier. */
372 sync_kloop_kernel_read(csb_atok, &shadow_ring, num_slots);
373 if (!sync_kloop_norxslots(kring, shadow_ring.head)) {
374 /* We won the race condition, more slots are available. Disable
375 * notifications and do another cycle. */
376 csb_ktoa_kick_enable(csb_ktoa, 0);
382 hwtail = NM_ACCESS_ONCE(kring->nr_hwtail);
383 if (unlikely(hwtail == kring->rhead ||
384 dry_cycles >= SYNC_LOOP_RX_DRY_CYCLES_MAX)) {
385 /* No more packets to be read from the backend. We stop and
386 * wait for a notification from the backend (netmap_rx_irq). */
387 nm_prdis(1, "nr_hwtail: %d rhead: %d dry_cycles: %d",
388 hwtail, kring->rhead, dry_cycles);
395 #ifdef SYNC_KLOOP_POLL
396 /* Interrupt the application if needed. */
397 if (a->irq_ctx && some_recvd && csb_atok_intr_enabled(csb_atok)) {
398 eventfd_signal(a->irq_ctx, 1);
400 #endif /* SYNC_KLOOP_POLL */
403 #ifdef SYNC_KLOOP_POLL
404 struct sync_kloop_poll_entry {
405 /* Support for receiving notifications from
406 * a netmap ring or from the application. */
409 wait_queue_head_t *wqh;
411 /* Support for sending notifications to the application. */
412 struct eventfd_ctx *irq_ctx;
413 struct file *irq_filp;
416 struct sync_kloop_poll_ctx {
417 poll_table wait_table;
418 unsigned int next_entry;
419 unsigned int num_entries;
420 struct sync_kloop_poll_entry entries[0];
424 sync_kloop_poll_table_queue_proc(struct file *file, wait_queue_head_t *wqh,
427 struct sync_kloop_poll_ctx *poll_ctx =
428 container_of(pt, struct sync_kloop_poll_ctx, wait_table);
429 struct sync_kloop_poll_entry *entry = poll_ctx->entries +
430 poll_ctx->next_entry;
432 BUG_ON(poll_ctx->next_entry >= poll_ctx->num_entries);
435 /* Use the default wake up function. */
436 init_waitqueue_entry(&entry->wait, current);
437 add_wait_queue(wqh, &entry->wait);
438 poll_ctx->next_entry++;
440 #endif /* SYNC_KLOOP_POLL */
443 netmap_sync_kloop(struct netmap_priv_d *priv, struct nmreq_header *hdr)
445 struct nmreq_sync_kloop_start *req =
446 (struct nmreq_sync_kloop_start *)(uintptr_t)hdr->nr_body;
447 struct nmreq_opt_sync_kloop_eventfds *eventfds_opt = NULL;
448 #ifdef SYNC_KLOOP_POLL
449 struct sync_kloop_poll_ctx *poll_ctx = NULL;
450 #endif /* SYNC_KLOOP_POLL */
451 int num_rx_rings, num_tx_rings, num_rings;
452 struct sync_kloop_ring_args *args = NULL;
453 uint32_t sleep_us = req->sleep_us;
454 struct nm_csb_atok* csb_atok_base;
455 struct nm_csb_ktoa* csb_ktoa_base;
456 struct netmap_adapter *na;
457 struct nmreq_option *opt;
461 if (sleep_us > 1000000) {
462 /* We do not accept sleeping for more than a second. */
466 if (priv->np_nifp == NULL) {
469 mb(); /* make sure following reads are not from cache */
472 if (!nm_netmap_on(na)) {
477 /* Make sure the application is working in CSB mode. */
478 if (!priv->np_csb_atok_base || !priv->np_csb_ktoa_base) {
480 nm_prerr("sync-kloop on %s requires "
481 "NETMAP_REQ_OPT_CSB option", na->name);
485 csb_atok_base = priv->np_csb_atok_base;
486 csb_ktoa_base = priv->np_csb_ktoa_base;
488 /* Make sure that no kloop is currently running. */
489 if (priv->np_kloop_state & NM_SYNC_KLOOP_RUNNING) {
492 priv->np_kloop_state |= NM_SYNC_KLOOP_RUNNING;
498 num_rx_rings = priv->np_qlast[NR_RX] - priv->np_qfirst[NR_RX];
499 num_tx_rings = priv->np_qlast[NR_TX] - priv->np_qfirst[NR_TX];
500 num_rings = num_tx_rings + num_rx_rings;
502 args = nm_os_malloc(num_rings * sizeof(args[0]));
508 /* Validate notification options. */
509 opt = nmreq_findoption((struct nmreq_option *)(uintptr_t)hdr->nr_options,
510 NETMAP_REQ_OPT_SYNC_KLOOP_EVENTFDS);
512 err = nmreq_checkduplicate(opt);
514 opt->nro_status = err;
517 if (opt->nro_size != sizeof(*eventfds_opt) +
518 sizeof(eventfds_opt->eventfds[0]) * num_rings) {
519 /* Option size not consistent with the number of
521 opt->nro_status = err = EINVAL;
524 #ifdef SYNC_KLOOP_POLL
525 eventfds_opt = (struct nmreq_opt_sync_kloop_eventfds *)opt;
527 /* We need 2 poll entries for TX and RX notifications coming
528 * from the netmap adapter, plus one entries per ring for the
529 * notifications coming from the application. */
530 poll_ctx = nm_os_malloc(sizeof(*poll_ctx) +
531 (2 + num_rings) * sizeof(poll_ctx->entries[0]));
532 init_poll_funcptr(&poll_ctx->wait_table,
533 sync_kloop_poll_table_queue_proc);
534 poll_ctx->num_entries = 2 + num_rings;
535 poll_ctx->next_entry = 0;
536 /* Poll for notifications coming from the applications through
538 for (i = 0; i < num_rings; i++) {
539 struct eventfd_ctx *irq;
543 filp = eventfd_fget(eventfds_opt->eventfds[i].ioeventfd);
548 mask = filp->f_op->poll(filp, &poll_ctx->wait_table);
549 if (mask & POLLERR) {
554 filp = eventfd_fget(eventfds_opt->eventfds[i].irqfd);
559 poll_ctx->entries[i].irq_filp = filp;
560 irq = eventfd_ctx_fileget(filp);
565 poll_ctx->entries[i].irq_ctx = irq;
567 /* Poll for notifications coming from the netmap rings bound to
568 * this file descriptor. */
571 poll_wait(priv->np_filp, priv->np_si[NR_TX],
572 &poll_ctx->wait_table);
573 poll_wait(priv->np_filp, priv->np_si[NR_RX],
574 &poll_ctx->wait_table);
577 #else /* SYNC_KLOOP_POLL */
578 opt->nro_status = EOPNOTSUPP;
580 #endif /* SYNC_KLOOP_POLL */
583 /* Prepare the arguments for netmap_sync_kloop_tx_ring()
584 * and netmap_sync_kloop_rx_ring(). */
585 for (i = 0; i < num_tx_rings; i++) {
586 struct sync_kloop_ring_args *a = args + i;
588 a->kring = NMR(na, NR_TX)[i + priv->np_qfirst[NR_TX]];
589 a->csb_atok = csb_atok_base + i;
590 a->csb_ktoa = csb_ktoa_base + i;
591 #ifdef SYNC_KLOOP_POLL
593 a->irq_ctx = poll_ctx->entries[i].irq_ctx;
594 #endif /* SYNC_KLOOP_POLL */
596 for (i = 0; i < num_rx_rings; i++) {
597 struct sync_kloop_ring_args *a = args + num_tx_rings + i;
599 a->kring = NMR(na, NR_RX)[i + priv->np_qfirst[NR_RX]];
600 a->csb_atok = csb_atok_base + num_tx_rings + i;
601 a->csb_ktoa = csb_ktoa_base + num_tx_rings + i;
602 #ifdef SYNC_KLOOP_POLL
604 a->irq_ctx = poll_ctx->entries[num_tx_rings + i].irq_ctx;
605 #endif /* SYNC_KLOOP_POLL */
610 if (unlikely(NM_ACCESS_ONCE(priv->np_kloop_state) & NM_SYNC_KLOOP_STOPPING)) {
614 #ifdef SYNC_KLOOP_POLL
616 /* It is important to set the task state as
617 * interruptible before processing any TX/RX ring,
618 * so that if a notification on ring Y comes after
619 * we have processed ring Y, but before we call
620 * schedule(), we don't miss it. This is true because
621 * the wake up function will change the the task state,
622 * and therefore the schedule_timeout() call below
623 * will observe the change).
625 set_current_state(TASK_INTERRUPTIBLE);
627 #endif /* SYNC_KLOOP_POLL */
629 /* Process all the TX rings bound to this file descriptor. */
630 for (i = 0; i < num_tx_rings; i++) {
631 struct sync_kloop_ring_args *a = args + i;
633 if (unlikely(nm_kr_tryget(a->kring, 1, NULL))) {
636 netmap_sync_kloop_tx_ring(a);
640 /* Process all the RX rings bound to this file descriptor. */
641 for (i = 0; i < num_rx_rings; i++) {
642 struct sync_kloop_ring_args *a = args + num_tx_rings + i;
644 if (unlikely(nm_kr_tryget(a->kring, 1, NULL))) {
647 netmap_sync_kloop_rx_ring(a);
651 #ifdef SYNC_KLOOP_POLL
653 /* If a poll context is present, yield to the scheduler
654 * waiting for a notification to come either from
655 * netmap or the application. */
656 schedule_timeout(msecs_to_jiffies(3000));
658 #endif /* SYNC_KLOOP_POLL */
660 /* Default synchronization method: sleep for a while. */
661 usleep_range(sleep_us, sleep_us);
665 #ifdef SYNC_KLOOP_POLL
667 /* Stop polling from netmap and the eventfds, and deallocate
668 * the poll context. */
669 __set_current_state(TASK_RUNNING);
670 for (i = 0; i < poll_ctx->next_entry; i++) {
671 struct sync_kloop_poll_entry *entry =
672 poll_ctx->entries + i;
675 remove_wait_queue(entry->wqh, &entry->wait);
676 /* We did not get a reference to the eventfds, but
677 * don't do that on netmap file descriptors (since
678 * a reference was not taken. */
679 if (entry->filp && entry->filp != priv->np_filp)
682 eventfd_ctx_put(entry->irq_ctx);
684 fput(entry->irq_filp);
686 nm_os_free(poll_ctx);
689 #endif /* SYNC_KLOOP_POLL */
696 /* Reset the kloop state. */
698 priv->np_kloop_state = 0;
705 netmap_sync_kloop_stop(struct netmap_priv_d *priv)
707 struct netmap_adapter *na;
711 if (priv->np_nifp == NULL) {
714 mb(); /* make sure following reads are not from cache */
717 if (!nm_netmap_on(na)) {
721 /* Set the kloop stopping flag. */
723 priv->np_kloop_state |= NM_SYNC_KLOOP_STOPPING;
726 /* Send a notification to the kloop, in case it is blocked in
727 * schedule_timeout(). We can use either RX or TX, because the
728 * kloop is waiting on both. */
729 nm_os_selwakeup(priv->np_si[NR_RX]);
731 /* Wait for the kloop to actually terminate. */
733 usleep_range(1000, 1500);
735 running = (NM_ACCESS_ONCE(priv->np_kloop_state)
736 & NM_SYNC_KLOOP_RUNNING);
745 * Guest ptnetmap txsync()/rxsync() routines, used in ptnet device drivers.
746 * These routines are reused across the different operating systems supported
751 * Reconcile host and guest views of the transmit ring.
753 * Guest user wants to transmit packets up to the one before ring->head,
754 * and guest kernel knows tx_ring->hwcur is the first packet unsent
755 * by the host kernel.
757 * We push out as many packets as possible, and possibly
758 * reclaim buffers from previously completed transmission.
760 * Notifications from the host are enabled only if the user guest would
761 * block (no space in the ring).
764 netmap_pt_guest_txsync(struct nm_csb_atok *atok, struct nm_csb_ktoa *ktoa,
765 struct netmap_kring *kring, int flags)
769 /* Disable notifications */
770 atok->appl_need_kick = 0;
773 * First part: tell the host (updating the CSB) to process the new
776 kring->nr_hwcur = ktoa->hwcur;
777 nm_sync_kloop_appl_write(atok, kring->rcur, kring->rhead);
779 /* Ask for a kick from a guest to the host if needed. */
780 if (((kring->rhead != kring->nr_hwcur || nm_kr_txempty(kring))
781 && NM_ACCESS_ONCE(ktoa->kern_need_kick)) ||
782 (flags & NAF_FORCE_RECLAIM)) {
783 atok->sync_flags = flags;
788 * Second part: reclaim buffers for completed transmissions.
790 if (nm_kr_txempty(kring) || (flags & NAF_FORCE_RECLAIM)) {
791 nm_sync_kloop_appl_read(ktoa, &kring->nr_hwtail,
796 * No more room in the ring for new transmissions. The user thread will
797 * go to sleep and we need to be notified by the host when more free
798 * space is available.
800 if (nm_kr_txempty(kring) && !(kring->nr_kflags & NKR_NOINTR)) {
801 /* Reenable notifications. */
802 atok->appl_need_kick = 1;
803 /* Double check, with store-load memory barrier. */
805 nm_sync_kloop_appl_read(ktoa, &kring->nr_hwtail,
807 /* If there is new free space, disable notifications */
808 if (unlikely(!nm_kr_txempty(kring))) {
809 atok->appl_need_kick = 0;
813 nm_prdis(1, "%s CSB(head:%u cur:%u hwtail:%u) KRING(head:%u cur:%u tail:%u)",
814 kring->name, atok->head, atok->cur, ktoa->hwtail,
815 kring->rhead, kring->rcur, kring->nr_hwtail);
821 * Reconcile host and guest view of the receive ring.
823 * Update hwcur/hwtail from host (reading from CSB).
825 * If guest user has released buffers up to the one before ring->head, we
826 * also give them to the host.
828 * Notifications from the host are enabled only if the user guest would
829 * block (no more completed slots in the ring).
832 netmap_pt_guest_rxsync(struct nm_csb_atok *atok, struct nm_csb_ktoa *ktoa,
833 struct netmap_kring *kring, int flags)
837 /* Disable notifications */
838 atok->appl_need_kick = 0;
841 * First part: import newly received packets, by updating the kring
842 * hwtail to the hwtail known from the host (read from the CSB).
843 * This also updates the kring hwcur.
845 nm_sync_kloop_appl_read(ktoa, &kring->nr_hwtail, &kring->nr_hwcur);
846 kring->nr_kflags &= ~NKR_PENDINTR;
849 * Second part: tell the host about the slots that guest user has
850 * released, by updating cur and head in the CSB.
852 if (kring->rhead != kring->nr_hwcur) {
853 nm_sync_kloop_appl_write(atok, kring->rcur, kring->rhead);
854 /* Ask for a kick from the guest to the host if needed. */
855 if (NM_ACCESS_ONCE(ktoa->kern_need_kick)) {
856 atok->sync_flags = flags;
862 * No more completed RX slots. The user thread will go to sleep and
863 * we need to be notified by the host when more RX slots have been
866 if (nm_kr_rxempty(kring) && !(kring->nr_kflags & NKR_NOINTR)) {
867 /* Reenable notifications. */
868 atok->appl_need_kick = 1;
869 /* Double check, with store-load memory barrier. */
871 nm_sync_kloop_appl_read(ktoa, &kring->nr_hwtail,
873 /* If there are new slots, disable notifications. */
874 if (!nm_kr_rxempty(kring)) {
875 atok->appl_need_kick = 0;
879 nm_prdis(1, "%s CSB(head:%u cur:%u hwtail:%u) KRING(head:%u cur:%u tail:%u)",
880 kring->name, atok->head, atok->cur, ktoa->hwtail,
881 kring->rhead, kring->rcur, kring->nr_hwtail);
887 * Callbacks for ptnet drivers: nm_krings_create, nm_krings_delete, nm_dtor.
890 ptnet_nm_krings_create(struct netmap_adapter *na)
892 struct netmap_pt_guest_adapter *ptna =
893 (struct netmap_pt_guest_adapter *)na; /* Upcast. */
894 struct netmap_adapter *na_nm = &ptna->hwup.up;
895 struct netmap_adapter *na_dr = &ptna->dr.up;
898 if (ptna->backend_users) {
902 /* Create krings on the public netmap adapter. */
903 ret = netmap_hw_krings_create(na_nm);
908 /* Copy krings into the netmap adapter private to the driver. */
909 na_dr->tx_rings = na_nm->tx_rings;
910 na_dr->rx_rings = na_nm->rx_rings;
916 ptnet_nm_krings_delete(struct netmap_adapter *na)
918 struct netmap_pt_guest_adapter *ptna =
919 (struct netmap_pt_guest_adapter *)na; /* Upcast. */
920 struct netmap_adapter *na_nm = &ptna->hwup.up;
921 struct netmap_adapter *na_dr = &ptna->dr.up;
923 if (ptna->backend_users) {
927 na_dr->tx_rings = NULL;
928 na_dr->rx_rings = NULL;
930 netmap_hw_krings_delete(na_nm);
934 ptnet_nm_dtor(struct netmap_adapter *na)
936 struct netmap_pt_guest_adapter *ptna =
937 (struct netmap_pt_guest_adapter *)na;
939 netmap_mem_put(ptna->dr.up.nm_mem);
940 memset(&ptna->dr, 0, sizeof(ptna->dr));
941 netmap_mem_pt_guest_ifp_del(na->nm_mem, na->ifp);
945 netmap_pt_guest_attach(struct netmap_adapter *arg,
946 unsigned int nifp_offset, unsigned int memid)
948 struct netmap_pt_guest_adapter *ptna;
949 struct ifnet *ifp = arg ? arg->ifp : NULL;
953 arg->nm_mem = netmap_mem_pt_guest_new(ifp, nifp_offset, memid);
954 if (arg->nm_mem == NULL)
956 arg->na_flags |= NAF_MEM_OWNER;
957 error = netmap_attach_ext(arg, sizeof(struct netmap_pt_guest_adapter), 1);
961 /* get the netmap_pt_guest_adapter */
962 ptna = (struct netmap_pt_guest_adapter *) NA(ifp);
964 /* Initialize a separate pass-through netmap adapter that is going to
965 * be used by the ptnet driver only, and so never exposed to netmap
966 * applications. We only need a subset of the available fields. */
967 memset(&ptna->dr, 0, sizeof(ptna->dr));
968 ptna->dr.up.ifp = ifp;
969 ptna->dr.up.nm_mem = netmap_mem_get(ptna->hwup.up.nm_mem);
970 ptna->dr.up.nm_config = ptna->hwup.up.nm_config;
972 ptna->backend_users = 0;
977 #endif /* WITH_PTNETMAP */