2 * Copyright (C) 2014-2016 Giuseppe Lettieri
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #if defined(__FreeBSD__)
30 #include <sys/cdefs.h> /* prerequisite */
32 #include <sys/types.h>
33 #include <sys/errno.h>
34 #include <sys/param.h> /* defines used in kernel.h */
35 #include <sys/kernel.h> /* types used in module initialization */
36 #include <sys/malloc.h>
39 #include <sys/rwlock.h>
40 #include <sys/selinfo.h>
41 #include <sys/sysctl.h>
42 #include <sys/socket.h> /* sockaddrs */
44 #include <net/if_var.h>
45 #include <machine/bus.h> /* bus_dmamap_* */
46 #include <sys/refcount.h>
53 #elif defined(__APPLE__)
55 #warning OSX support is only partial
63 #error Unsupported platform
65 #endif /* unsupported */
71 #include <net/netmap.h>
72 #include <dev/netmap/netmap_kern.h>
73 #include <dev/netmap/netmap_mem2.h>
77 #define NM_PIPE_MAXSLOTS 4096
79 static int netmap_default_pipes = 0; /* ignored, kept for compatibility */
81 SYSCTL_DECL(_dev_netmap);
82 SYSCTL_INT(_dev_netmap, OID_AUTO, default_pipes, CTLFLAG_RW, &netmap_default_pipes, 0 , "");
85 /* allocate the pipe array in the parent adapter */
87 nm_pipe_alloc(struct netmap_adapter *na, u_int npipes)
90 struct netmap_pipe_adapter **npa;
92 if (npipes <= na->na_max_pipes)
93 /* we already have more entries that requested */
96 if (npipes < na->na_next_pipe || npipes > NM_MAXPIPES)
99 old_len = sizeof(struct netmap_pipe_adapter *)*na->na_max_pipes;
100 len = sizeof(struct netmap_pipe_adapter *) * npipes;
101 npa = nm_os_realloc(na->na_pipes, len, old_len);
106 na->na_max_pipes = npipes;
111 /* deallocate the parent array in the parent adapter */
113 netmap_pipe_dealloc(struct netmap_adapter *na)
116 if (na->na_next_pipe > 0) {
117 D("freeing not empty pipe array for %s (%d dangling pipes)!", na->name,
120 nm_os_free(na->na_pipes);
122 na->na_max_pipes = 0;
123 na->na_next_pipe = 0;
127 /* find a pipe endpoint with the given id among the parent's pipes */
128 static struct netmap_pipe_adapter *
129 netmap_pipe_find(struct netmap_adapter *parent, u_int pipe_id)
132 struct netmap_pipe_adapter *na;
134 for (i = 0; i < parent->na_next_pipe; i++) {
135 na = parent->na_pipes[i];
136 if (na->id == pipe_id) {
143 /* add a new pipe endpoint to the parent array */
145 netmap_pipe_add(struct netmap_adapter *parent, struct netmap_pipe_adapter *na)
147 if (parent->na_next_pipe >= parent->na_max_pipes) {
148 u_int npipes = parent->na_max_pipes ? 2*parent->na_max_pipes : 2;
149 int error = nm_pipe_alloc(parent, npipes);
154 parent->na_pipes[parent->na_next_pipe] = na;
155 na->parent_slot = parent->na_next_pipe;
156 parent->na_next_pipe++;
160 /* remove the given pipe endpoint from the parent array */
162 netmap_pipe_remove(struct netmap_adapter *parent, struct netmap_pipe_adapter *na)
165 n = --parent->na_next_pipe;
166 if (n != na->parent_slot) {
167 struct netmap_pipe_adapter **p =
168 &parent->na_pipes[na->parent_slot];
169 *p = parent->na_pipes[n];
170 (*p)->parent_slot = na->parent_slot;
172 parent->na_pipes[n] = NULL;
176 netmap_pipe_txsync(struct netmap_kring *txkring, int flags)
178 struct netmap_kring *rxkring = txkring->pipe;
179 u_int limit; /* slots to transfer */
180 u_int j, k, lim_tx = txkring->nkr_num_slots - 1,
181 lim_rx = rxkring->nkr_num_slots - 1;
184 ND("%p: %s %x -> %s", txkring, txkring->name, flags, rxkring->name);
185 ND(2, "before: hwcur %d hwtail %d cur %d head %d tail %d", txkring->nr_hwcur, txkring->nr_hwtail,
186 txkring->rcur, txkring->rhead, txkring->rtail);
188 j = rxkring->nr_hwtail; /* RX */
189 k = txkring->nr_hwcur; /* TX */
190 m = txkring->rhead - txkring->nr_hwcur; /* new slots */
192 m += txkring->nkr_num_slots;
194 m = lim_rx; /* max avail space on destination */
195 busy = j - rxkring->nr_hwcur; /* busy slots */
197 busy += rxkring->nkr_num_slots;
198 m -= busy; /* subtract busy slots */
199 ND(2, "m %d limit %d", m, limit);
204 /* either the rxring is full, or nothing to send */
208 while (limit-- > 0) {
209 struct netmap_slot *rs = &rxkring->ring->slot[j];
210 struct netmap_slot *ts = &txkring->ring->slot[k];
211 struct netmap_slot tmp;
218 /* report the buffer change */
219 ts->flags |= NS_BUF_CHANGED;
220 rs->flags |= NS_BUF_CHANGED;
222 j = nm_next(j, lim_rx);
223 k = nm_next(k, lim_tx);
226 mb(); /* make sure the slots are updated before publishing them */
227 rxkring->nr_hwtail = j;
228 txkring->nr_hwcur = k;
229 txkring->nr_hwtail = nm_prev(k, lim_tx);
231 ND(2, "after: hwcur %d hwtail %d cur %d head %d tail %d j %d", txkring->nr_hwcur, txkring->nr_hwtail,
232 txkring->rcur, txkring->rhead, txkring->rtail, j);
234 mb(); /* make sure rxkring->nr_hwtail is updated before notifying */
235 rxkring->nm_notify(rxkring, 0);
241 netmap_pipe_rxsync(struct netmap_kring *rxkring, int flags)
243 struct netmap_kring *txkring = rxkring->pipe;
244 uint32_t oldhwcur = rxkring->nr_hwcur;
246 ND("%s %x <- %s", rxkring->name, flags, txkring->name);
247 rxkring->nr_hwcur = rxkring->rhead; /* recover user-relased slots */
248 ND(5, "hwcur %d hwtail %d cur %d head %d tail %d", rxkring->nr_hwcur, rxkring->nr_hwtail,
249 rxkring->rcur, rxkring->rhead, rxkring->rtail);
250 mb(); /* paired with the first mb() in txsync */
252 if (oldhwcur != rxkring->nr_hwcur) {
253 /* we have released some slots, notify the other end */
254 mb(); /* make sure nr_hwcur is updated before notifying */
255 txkring->nm_notify(txkring, 0);
260 /* Pipe endpoints are created and destroyed together, so that endopoints do not
261 * have to check for the existence of their peer at each ?xsync.
263 * To play well with the existing netmap infrastructure (refcounts etc.), we
264 * adopt the following strategy:
266 * 1) The first endpoint that is created also creates the other endpoint and
267 * grabs a reference to it.
269 * state A) user1 --> endpoint1 --> endpoint2
271 * 2) If, starting from state A, endpoint2 is then registered, endpoint1 gives
272 * its reference to the user:
274 * state B) user1 --> endpoint1 endpoint2 <--- user2
276 * 3) Assume that, starting from state B endpoint2 is closed. In the unregister
277 * callback endpoint2 notes that endpoint1 is still active and adds a reference
278 * from endpoint1 to itself. When user2 then releases her own reference,
279 * endpoint2 is not destroyed and we are back to state A. A symmetrical state
280 * would be reached if endpoint1 were released instead.
282 * 4) If, starting from state A, endpoint1 is closed, the destructor notes that
283 * it owns a reference to endpoint2 and releases it.
285 * Something similar goes on for the creation and destruction of the krings.
289 /* netmap_pipe_krings_create.
291 * There are two cases:
297 * and we are e1. We have to create both sets
304 * and we are e2. e1 is certainly registered and our
305 * krings already exist. Nothing to do.
308 netmap_pipe_krings_create(struct netmap_adapter *na)
310 struct netmap_pipe_adapter *pna =
311 (struct netmap_pipe_adapter *)na;
312 struct netmap_adapter *ona = &pna->peer->up;
320 ND("%p: case 1, create both ends", na);
321 error = netmap_krings_create(na, 0);
325 /* create the krings of the other end */
326 error = netmap_krings_create(ona, 0);
330 /* cross link the krings */
332 enum txrx r = nm_txrx_swap(t); /* swap NR_TX <-> NR_RX */
333 for (i = 0; i < nma_get_nrings(na, t); i++) {
334 NMR(na, t)[i].pipe = NMR(ona, r) + i;
335 NMR(ona, r)[i].pipe = NMR(na, t) + i;
343 netmap_krings_delete(na);
350 * There are two cases on registration (onoff==1)
356 * and we are e1. Create the needed rings of the
361 * usr1 --> e1 --> e2 <-- usr2
363 * and we are e2. Drop the ref e1 is holding.
365 * There are two additional cases on unregister (onoff==0)
371 * and we are e1. Nothing special to do, e2 will
372 * be cleaned up by the destructor of e1.
376 * usr1 --> e1 e2 <-- usr2
378 * and we are either e1 or e2. Add a ref from the
379 * other end and hide our rings.
382 netmap_pipe_reg(struct netmap_adapter *na, int onoff)
384 struct netmap_pipe_adapter *pna =
385 (struct netmap_pipe_adapter *)na;
386 struct netmap_adapter *ona = &pna->peer->up;
390 ND("%p: onoff %d", na, onoff);
393 for (i = 0; i < nma_get_nrings(na, t); i++) {
394 struct netmap_kring *kring = &NMR(na, t)[i];
396 if (nm_kring_pending_on(kring)) {
397 /* mark the peer ring as needed */
398 kring->pipe->nr_kflags |= NKR_NEEDRING;
403 /* create all missing needed rings on the other end */
404 error = netmap_mem_rings_create(ona);
408 /* In case of no error we put our rings in netmap mode */
410 for (i = 0; i < nma_get_nrings(na, t) + 1; i++) {
411 struct netmap_kring *kring = &NMR(na, t)[i];
413 if (nm_kring_pending_on(kring)) {
414 kring->nr_mode = NKR_NETMAP_ON;
418 if (na->active_fds == 0)
419 na->na_flags |= NAF_NETMAP_ON;
421 if (na->active_fds == 0)
422 na->na_flags &= ~NAF_NETMAP_ON;
424 for (i = 0; i < nma_get_nrings(na, t) + 1; i++) {
425 struct netmap_kring *kring = &NMR(na, t)[i];
427 if (nm_kring_pending_off(kring)) {
428 kring->nr_mode = NKR_NETMAP_OFF;
429 /* mark the peer ring as no longer needed by us
430 * (it may still be kept if sombody else is using it)
433 kring->pipe->nr_kflags &= ~NKR_NEEDRING;
438 /* delete all the peer rings that are no longer needed */
439 netmap_mem_rings_delete(ona);
442 if (na->active_fds) {
443 ND("active_fds %d", na->active_fds);
448 ND("%p: case 1.a or 2.a, nothing to do", na);
452 ND("%p: case 1.b, drop peer", na);
453 pna->peer->peer_ref = 0;
454 netmap_adapter_put(na);
456 ND("%p: case 2.b, grab peer", na);
457 netmap_adapter_get(na);
458 pna->peer->peer_ref = 1;
463 /* netmap_pipe_krings_delete.
465 * There are two cases:
471 * and we are e1 (e2 is not registered, so krings_delete cannot be
476 * usr1 --> e1 e2 <-- usr2
478 * and we are either e1 or e2.
480 * In the former case we have to also delete the krings of e2;
481 * in the latter case we do nothing (note that our krings
482 * have already been hidden in the unregister callback).
485 netmap_pipe_krings_delete(struct netmap_adapter *na)
487 struct netmap_pipe_adapter *pna =
488 (struct netmap_pipe_adapter *)na;
489 struct netmap_adapter *ona; /* na of the other end */
491 if (!pna->peer_ref) {
492 ND("%p: case 2, kept alive by peer", na);
496 ND("%p: case 1, deleting everything", na);
497 netmap_krings_delete(na); /* also zeroes tx_rings etc. */
498 ona = &pna->peer->up;
499 if (ona->tx_rings == NULL) {
500 /* already deleted, we must be on an
501 * cleanup-after-error path */
504 netmap_krings_delete(ona);
509 netmap_pipe_dtor(struct netmap_adapter *na)
511 struct netmap_pipe_adapter *pna =
512 (struct netmap_pipe_adapter *)na;
513 ND("%p %p", na, pna->parent_ifp);
515 ND("%p: clean up peer", na);
517 netmap_adapter_put(&pna->peer->up);
519 if (pna->role == NR_REG_PIPE_MASTER)
520 netmap_pipe_remove(pna->parent, pna);
522 if_rele(pna->parent_ifp);
523 netmap_adapter_put(pna->parent);
528 netmap_get_pipe_na(struct nmreq *nmr, struct netmap_adapter **na,
529 struct netmap_mem_d *nmd, int create)
532 struct netmap_adapter *pna; /* parent adapter */
533 struct netmap_pipe_adapter *mna, *sna, *req;
534 struct ifnet *ifp = NULL;
536 int role = nmr->nr_flags & NR_REG_MASK;
537 int error, retries = 0;
539 ND("flags %x", nmr->nr_flags);
541 if (role != NR_REG_PIPE_MASTER && role != NR_REG_PIPE_SLAVE) {
545 role = nmr->nr_flags & NR_REG_MASK;
547 /* first, try to find the parent adapter */
548 bzero(&pnmr, sizeof(pnmr));
549 memcpy(&pnmr.nr_name, nmr->nr_name, IFNAMSIZ);
550 /* pass to parent the requested number of pipes */
551 pnmr.nr_arg1 = nmr->nr_arg1;
555 error = netmap_get_na(&pnmr, &pna, &ifp, nmd, create);
558 if (error != ENXIO || retries++) {
559 ND("parent lookup failed: %d", error);
562 ND("try to create a persistent vale port");
563 /* create a persistent vale port and try again */
565 create_error = netmap_vi_create(&pnmr, 1 /* autodelete */);
567 if (create_error && create_error != EEXIST) {
568 if (create_error != EOPNOTSUPP) {
569 D("failed to create a persistent vale port: %d", create_error);
575 if (NETMAP_OWNED_BY_KERN(pna)) {
581 /* next, lookup the pipe id in the parent list */
583 pipe_id = nmr->nr_ringid & NETMAP_RING_MASK;
584 mna = netmap_pipe_find(pna, pipe_id);
586 if (mna->role == role) {
587 ND("found %d directly at %d", pipe_id, mna->parent_slot);
590 ND("found %d indirectly at %d", pipe_id, mna->parent_slot);
593 /* the pipe we have found already holds a ref to the parent,
594 * so we need to drop the one we got from netmap_get_na()
596 netmap_unget_na(pna, ifp);
599 ND("pipe %d not found, create %d", pipe_id, create);
604 /* we create both master and slave.
605 * The endpoint we were asked for holds a reference to
608 mna = nm_os_malloc(sizeof(*mna));
613 snprintf(mna->up.name, sizeof(mna->up.name), "%s{%d", pna->name, pipe_id);
616 mna->role = NR_REG_PIPE_MASTER;
618 mna->parent_ifp = ifp;
620 mna->up.nm_txsync = netmap_pipe_txsync;
621 mna->up.nm_rxsync = netmap_pipe_rxsync;
622 mna->up.nm_register = netmap_pipe_reg;
623 mna->up.nm_dtor = netmap_pipe_dtor;
624 mna->up.nm_krings_create = netmap_pipe_krings_create;
625 mna->up.nm_krings_delete = netmap_pipe_krings_delete;
626 mna->up.nm_mem = netmap_mem_get(pna->nm_mem);
627 mna->up.na_flags |= NAF_MEM_OWNER;
628 mna->up.na_lut = pna->na_lut;
630 mna->up.num_tx_rings = 1;
631 mna->up.num_rx_rings = 1;
632 mna->up.num_tx_desc = nmr->nr_tx_slots;
633 nm_bound_var(&mna->up.num_tx_desc, pna->num_tx_desc,
634 1, NM_PIPE_MAXSLOTS, NULL);
635 mna->up.num_rx_desc = nmr->nr_rx_slots;
636 nm_bound_var(&mna->up.num_rx_desc, pna->num_rx_desc,
637 1, NM_PIPE_MAXSLOTS, NULL);
638 error = netmap_attach_common(&mna->up);
641 /* register the master with the parent */
642 error = netmap_pipe_add(pna, mna);
646 /* create the slave */
647 sna = nm_os_malloc(sizeof(*mna));
652 /* most fields are the same, copy from master and then fix */
654 sna->up.nm_mem = netmap_mem_get(mna->up.nm_mem);
655 snprintf(sna->up.name, sizeof(sna->up.name), "%s}%d", pna->name, pipe_id);
656 sna->role = NR_REG_PIPE_SLAVE;
657 error = netmap_attach_common(&sna->up);
661 /* join the two endpoints */
665 /* we already have a reference to the parent, but we
666 * need another one for the other endpoint we created
668 netmap_adapter_get(pna);
669 /* likewise for the ifp, if any */
673 if (role == NR_REG_PIPE_MASTER) {
676 netmap_adapter_get(&sna->up);
680 netmap_adapter_get(&mna->up);
682 ND("created master %p and slave %p", mna, sna);
685 ND("pipe %d %s at %p", pipe_id,
686 (req->role == NR_REG_PIPE_MASTER ? "master" : "slave"), req);
688 netmap_adapter_get(*na);
690 /* keep the reference to the parent.
691 * It will be released by the req destructor
699 netmap_pipe_remove(pna, mna);
703 netmap_unget_na(pna, ifp);
708 #endif /* WITH_PIPES */