1f0ea3689SLuigi Rizzo /* 2f0ea3689SLuigi Rizzo * Copyright (C) 2014 Giuseppe Lettieri. All rights reserved. 3f0ea3689SLuigi Rizzo * 4f0ea3689SLuigi Rizzo * Redistribution and use in source and binary forms, with or without 5f0ea3689SLuigi Rizzo * modification, are permitted provided that the following conditions 6f0ea3689SLuigi Rizzo * are met: 7f0ea3689SLuigi Rizzo * 1. Redistributions of source code must retain the above copyright 8f0ea3689SLuigi Rizzo * notice, this list of conditions and the following disclaimer. 9f0ea3689SLuigi Rizzo * 2. Redistributions in binary form must reproduce the above copyright 10f0ea3689SLuigi Rizzo * notice, this list of conditions and the following disclaimer in the 11f0ea3689SLuigi Rizzo * documentation and/or other materials provided with the distribution. 12f0ea3689SLuigi Rizzo * 13f0ea3689SLuigi Rizzo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14f0ea3689SLuigi Rizzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15f0ea3689SLuigi Rizzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16f0ea3689SLuigi Rizzo * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17f0ea3689SLuigi Rizzo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18f0ea3689SLuigi Rizzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19f0ea3689SLuigi Rizzo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20f0ea3689SLuigi Rizzo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21f0ea3689SLuigi Rizzo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22f0ea3689SLuigi Rizzo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23f0ea3689SLuigi Rizzo * SUCH DAMAGE. 24f0ea3689SLuigi Rizzo */ 25f0ea3689SLuigi Rizzo 26f0ea3689SLuigi Rizzo /* $FreeBSD$ */ 27f0ea3689SLuigi Rizzo 28f0ea3689SLuigi Rizzo #if defined(__FreeBSD__) 29f0ea3689SLuigi Rizzo #include <sys/cdefs.h> /* prerequisite */ 30f0ea3689SLuigi Rizzo 31f0ea3689SLuigi Rizzo #include <sys/types.h> 32f0ea3689SLuigi Rizzo #include <sys/errno.h> 33f0ea3689SLuigi Rizzo #include <sys/param.h> /* defines used in kernel.h */ 34f0ea3689SLuigi Rizzo #include <sys/kernel.h> /* types used in module initialization */ 35f0ea3689SLuigi Rizzo #include <sys/malloc.h> 36f0ea3689SLuigi Rizzo #include <sys/poll.h> 37f0ea3689SLuigi Rizzo #include <sys/lock.h> 38f0ea3689SLuigi Rizzo #include <sys/rwlock.h> 39f0ea3689SLuigi Rizzo #include <sys/selinfo.h> 40f0ea3689SLuigi Rizzo #include <sys/sysctl.h> 41f0ea3689SLuigi Rizzo #include <sys/socket.h> /* sockaddrs */ 42f0ea3689SLuigi Rizzo #include <net/if.h> 43f0ea3689SLuigi Rizzo #include <net/if_var.h> 44f0ea3689SLuigi Rizzo #include <machine/bus.h> /* bus_dmamap_* */ 45f0ea3689SLuigi Rizzo #include <sys/refcount.h> 46f0ea3689SLuigi Rizzo 47f0ea3689SLuigi Rizzo 48f0ea3689SLuigi Rizzo #elif defined(linux) 49f0ea3689SLuigi Rizzo 50f0ea3689SLuigi Rizzo #include "bsd_glue.h" 51f0ea3689SLuigi Rizzo 52f0ea3689SLuigi Rizzo #elif defined(__APPLE__) 53f0ea3689SLuigi Rizzo 54f0ea3689SLuigi Rizzo #warning OSX support is only partial 55f0ea3689SLuigi Rizzo #include "osx_glue.h" 56f0ea3689SLuigi Rizzo 57f0ea3689SLuigi Rizzo #else 58f0ea3689SLuigi Rizzo 59f0ea3689SLuigi Rizzo #error Unsupported platform 60f0ea3689SLuigi Rizzo 61f0ea3689SLuigi Rizzo #endif /* unsupported */ 62f0ea3689SLuigi Rizzo 63f0ea3689SLuigi Rizzo /* 64f0ea3689SLuigi Rizzo * common headers 65f0ea3689SLuigi Rizzo */ 66f0ea3689SLuigi Rizzo 67f0ea3689SLuigi Rizzo #include <net/netmap.h> 68f0ea3689SLuigi Rizzo #include <dev/netmap/netmap_kern.h> 69f0ea3689SLuigi Rizzo #include <dev/netmap/netmap_mem2.h> 70f0ea3689SLuigi Rizzo 71f0ea3689SLuigi Rizzo #ifdef WITH_PIPES 72f0ea3689SLuigi Rizzo 73f0ea3689SLuigi Rizzo #define NM_PIPE_MAXSLOTS 4096 74f0ea3689SLuigi Rizzo 75*847bf383SLuigi Rizzo int netmap_default_pipes = 0; /* ignored, kept for compatibility */ 76f0ea3689SLuigi Rizzo SYSCTL_DECL(_dev_netmap); 77f0ea3689SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, default_pipes, CTLFLAG_RW, &netmap_default_pipes, 0 , ""); 78f0ea3689SLuigi Rizzo 79f0ea3689SLuigi Rizzo /* allocate the pipe array in the parent adapter */ 80*847bf383SLuigi Rizzo static int 81*847bf383SLuigi Rizzo nm_pipe_alloc(struct netmap_adapter *na, u_int npipes) 82f0ea3689SLuigi Rizzo { 83f0ea3689SLuigi Rizzo size_t len; 84*847bf383SLuigi Rizzo struct netmap_pipe_adapter **npa; 85f0ea3689SLuigi Rizzo 86*847bf383SLuigi Rizzo if (npipes <= na->na_max_pipes) 87*847bf383SLuigi Rizzo /* we already have more entries that requested */ 88f0ea3689SLuigi Rizzo return 0; 89f0ea3689SLuigi Rizzo 90*847bf383SLuigi Rizzo if (npipes < na->na_next_pipe || npipes > NM_MAXPIPES) 91*847bf383SLuigi Rizzo return EINVAL; 92f0ea3689SLuigi Rizzo 93f0ea3689SLuigi Rizzo len = sizeof(struct netmap_pipe_adapter *) * npipes; 94*847bf383SLuigi Rizzo npa = realloc(na->na_pipes, len, M_DEVBUF, M_NOWAIT | M_ZERO); 95*847bf383SLuigi Rizzo if (npa == NULL) 96f0ea3689SLuigi Rizzo return ENOMEM; 97f0ea3689SLuigi Rizzo 98*847bf383SLuigi Rizzo na->na_pipes = npa; 99f0ea3689SLuigi Rizzo na->na_max_pipes = npipes; 100f0ea3689SLuigi Rizzo 101f0ea3689SLuigi Rizzo return 0; 102f0ea3689SLuigi Rizzo } 103f0ea3689SLuigi Rizzo 104f0ea3689SLuigi Rizzo /* deallocate the parent array in the parent adapter */ 105f0ea3689SLuigi Rizzo void 106f0ea3689SLuigi Rizzo netmap_pipe_dealloc(struct netmap_adapter *na) 107f0ea3689SLuigi Rizzo { 108f0ea3689SLuigi Rizzo if (na->na_pipes) { 109*847bf383SLuigi Rizzo if (na->na_next_pipe > 0) { 110*847bf383SLuigi Rizzo D("freeing not empty pipe array for %s (%d dangling pipes)!", na->name, 111*847bf383SLuigi Rizzo na->na_next_pipe); 112*847bf383SLuigi Rizzo } 113f0ea3689SLuigi Rizzo free(na->na_pipes, M_DEVBUF); 114f0ea3689SLuigi Rizzo na->na_pipes = NULL; 115f0ea3689SLuigi Rizzo na->na_max_pipes = 0; 116f0ea3689SLuigi Rizzo na->na_next_pipe = 0; 117f0ea3689SLuigi Rizzo } 118f0ea3689SLuigi Rizzo } 119f0ea3689SLuigi Rizzo 120f0ea3689SLuigi Rizzo /* find a pipe endpoint with the given id among the parent's pipes */ 121f0ea3689SLuigi Rizzo static struct netmap_pipe_adapter * 122f0ea3689SLuigi Rizzo netmap_pipe_find(struct netmap_adapter *parent, u_int pipe_id) 123f0ea3689SLuigi Rizzo { 124f0ea3689SLuigi Rizzo int i; 125f0ea3689SLuigi Rizzo struct netmap_pipe_adapter *na; 126f0ea3689SLuigi Rizzo 127f0ea3689SLuigi Rizzo for (i = 0; i < parent->na_next_pipe; i++) { 128f0ea3689SLuigi Rizzo na = parent->na_pipes[i]; 129f0ea3689SLuigi Rizzo if (na->id == pipe_id) { 130f0ea3689SLuigi Rizzo return na; 131f0ea3689SLuigi Rizzo } 132f0ea3689SLuigi Rizzo } 133f0ea3689SLuigi Rizzo return NULL; 134f0ea3689SLuigi Rizzo } 135f0ea3689SLuigi Rizzo 136f0ea3689SLuigi Rizzo /* add a new pipe endpoint to the parent array */ 137f0ea3689SLuigi Rizzo static int 138f0ea3689SLuigi Rizzo netmap_pipe_add(struct netmap_adapter *parent, struct netmap_pipe_adapter *na) 139f0ea3689SLuigi Rizzo { 140f0ea3689SLuigi Rizzo if (parent->na_next_pipe >= parent->na_max_pipes) { 141*847bf383SLuigi Rizzo u_int npipes = parent->na_max_pipes ? 2*parent->na_max_pipes : 2; 142*847bf383SLuigi Rizzo int error = nm_pipe_alloc(parent, npipes); 143*847bf383SLuigi Rizzo if (error) 144*847bf383SLuigi Rizzo return error; 145f0ea3689SLuigi Rizzo } 146f0ea3689SLuigi Rizzo 147f0ea3689SLuigi Rizzo parent->na_pipes[parent->na_next_pipe] = na; 148f0ea3689SLuigi Rizzo na->parent_slot = parent->na_next_pipe; 149f0ea3689SLuigi Rizzo parent->na_next_pipe++; 150f0ea3689SLuigi Rizzo return 0; 151f0ea3689SLuigi Rizzo } 152f0ea3689SLuigi Rizzo 153f0ea3689SLuigi Rizzo /* remove the given pipe endpoint from the parent array */ 154f0ea3689SLuigi Rizzo static void 155f0ea3689SLuigi Rizzo netmap_pipe_remove(struct netmap_adapter *parent, struct netmap_pipe_adapter *na) 156f0ea3689SLuigi Rizzo { 157f0ea3689SLuigi Rizzo u_int n; 158f0ea3689SLuigi Rizzo n = --parent->na_next_pipe; 159f0ea3689SLuigi Rizzo if (n != na->parent_slot) { 160*847bf383SLuigi Rizzo struct netmap_pipe_adapter **p = 161*847bf383SLuigi Rizzo &parent->na_pipes[na->parent_slot]; 162*847bf383SLuigi Rizzo *p = parent->na_pipes[n]; 163*847bf383SLuigi Rizzo (*p)->parent_slot = na->parent_slot; 164f0ea3689SLuigi Rizzo } 165f0ea3689SLuigi Rizzo parent->na_pipes[n] = NULL; 166f0ea3689SLuigi Rizzo } 167f0ea3689SLuigi Rizzo 168f0ea3689SLuigi Rizzo static int 1694bf50f18SLuigi Rizzo netmap_pipe_txsync(struct netmap_kring *txkring, int flags) 170f0ea3689SLuigi Rizzo { 1714bf50f18SLuigi Rizzo struct netmap_kring *rxkring = txkring->pipe; 172f0ea3689SLuigi Rizzo u_int limit; /* slots to transfer */ 173f0ea3689SLuigi Rizzo u_int j, k, lim_tx = txkring->nkr_num_slots - 1, 174f0ea3689SLuigi Rizzo lim_rx = rxkring->nkr_num_slots - 1; 175f0ea3689SLuigi Rizzo int m, busy; 176f0ea3689SLuigi Rizzo 177f0ea3689SLuigi Rizzo ND("%p: %s %x -> %s", txkring, txkring->name, flags, rxkring->name); 178f0ea3689SLuigi Rizzo ND(2, "before: hwcur %d hwtail %d cur %d head %d tail %d", txkring->nr_hwcur, txkring->nr_hwtail, 179f0ea3689SLuigi Rizzo txkring->rcur, txkring->rhead, txkring->rtail); 180f0ea3689SLuigi Rizzo 181f0ea3689SLuigi Rizzo j = rxkring->nr_hwtail; /* RX */ 182f0ea3689SLuigi Rizzo k = txkring->nr_hwcur; /* TX */ 183f0ea3689SLuigi Rizzo m = txkring->rhead - txkring->nr_hwcur; /* new slots */ 184f0ea3689SLuigi Rizzo if (m < 0) 185f0ea3689SLuigi Rizzo m += txkring->nkr_num_slots; 186f0ea3689SLuigi Rizzo limit = m; 187204f91ddSLuigi Rizzo m = lim_rx; /* max avail space on destination */ 188f0ea3689SLuigi Rizzo busy = j - rxkring->nr_hwcur; /* busy slots */ 189f0ea3689SLuigi Rizzo if (busy < 0) 190204f91ddSLuigi Rizzo busy += rxkring->nkr_num_slots; 191f0ea3689SLuigi Rizzo m -= busy; /* subtract busy slots */ 192f0ea3689SLuigi Rizzo ND(2, "m %d limit %d", m, limit); 193f0ea3689SLuigi Rizzo if (m < limit) 194f0ea3689SLuigi Rizzo limit = m; 195f0ea3689SLuigi Rizzo 196f0ea3689SLuigi Rizzo if (limit == 0) { 197f0ea3689SLuigi Rizzo /* either the rxring is full, or nothing to send */ 198f0ea3689SLuigi Rizzo return 0; 199f0ea3689SLuigi Rizzo } 200f0ea3689SLuigi Rizzo 201f0ea3689SLuigi Rizzo while (limit-- > 0) { 202f0ea3689SLuigi Rizzo struct netmap_slot *rs = &rxkring->save_ring->slot[j]; 203f0ea3689SLuigi Rizzo struct netmap_slot *ts = &txkring->ring->slot[k]; 204f0ea3689SLuigi Rizzo struct netmap_slot tmp; 205f0ea3689SLuigi Rizzo 206f0ea3689SLuigi Rizzo /* swap the slots */ 207f0ea3689SLuigi Rizzo tmp = *rs; 208f0ea3689SLuigi Rizzo *rs = *ts; 209f0ea3689SLuigi Rizzo *ts = tmp; 210f0ea3689SLuigi Rizzo 211*847bf383SLuigi Rizzo /* report the buffer change */ 212*847bf383SLuigi Rizzo ts->flags |= NS_BUF_CHANGED; 213*847bf383SLuigi Rizzo rs->flags |= NS_BUF_CHANGED; 214f0ea3689SLuigi Rizzo 215f0ea3689SLuigi Rizzo j = nm_next(j, lim_rx); 216f0ea3689SLuigi Rizzo k = nm_next(k, lim_tx); 217f0ea3689SLuigi Rizzo } 218f0ea3689SLuigi Rizzo 219ad15cc59SLuigi Rizzo mb(); /* make sure the slots are updated before publishing them */ 220f0ea3689SLuigi Rizzo rxkring->nr_hwtail = j; 221f0ea3689SLuigi Rizzo txkring->nr_hwcur = k; 222f0ea3689SLuigi Rizzo txkring->nr_hwtail = nm_prev(k, lim_tx); 223f0ea3689SLuigi Rizzo 224f0ea3689SLuigi Rizzo ND(2, "after: hwcur %d hwtail %d cur %d head %d tail %d j %d", txkring->nr_hwcur, txkring->nr_hwtail, 225f0ea3689SLuigi Rizzo txkring->rcur, txkring->rhead, txkring->rtail, j); 226f0ea3689SLuigi Rizzo 227ad15cc59SLuigi Rizzo mb(); /* make sure rxkring->nr_hwtail is updated before notifying */ 228*847bf383SLuigi Rizzo rxkring->nm_notify(rxkring, 0); 229f0ea3689SLuigi Rizzo 230f0ea3689SLuigi Rizzo return 0; 231f0ea3689SLuigi Rizzo } 232f0ea3689SLuigi Rizzo 233f0ea3689SLuigi Rizzo static int 2344bf50f18SLuigi Rizzo netmap_pipe_rxsync(struct netmap_kring *rxkring, int flags) 235f0ea3689SLuigi Rizzo { 2364bf50f18SLuigi Rizzo struct netmap_kring *txkring = rxkring->pipe; 237f0ea3689SLuigi Rizzo uint32_t oldhwcur = rxkring->nr_hwcur; 238f0ea3689SLuigi Rizzo 239f0ea3689SLuigi Rizzo ND("%s %x <- %s", rxkring->name, flags, txkring->name); 240f0ea3689SLuigi Rizzo rxkring->nr_hwcur = rxkring->rhead; /* recover user-relased slots */ 241f0ea3689SLuigi Rizzo ND(5, "hwcur %d hwtail %d cur %d head %d tail %d", rxkring->nr_hwcur, rxkring->nr_hwtail, 242f0ea3689SLuigi Rizzo rxkring->rcur, rxkring->rhead, rxkring->rtail); 243ad15cc59SLuigi Rizzo mb(); /* paired with the first mb() in txsync */ 244f0ea3689SLuigi Rizzo 245f0ea3689SLuigi Rizzo if (oldhwcur != rxkring->nr_hwcur) { 246f0ea3689SLuigi Rizzo /* we have released some slots, notify the other end */ 247ad15cc59SLuigi Rizzo mb(); /* make sure nr_hwcur is updated before notifying */ 248*847bf383SLuigi Rizzo txkring->nm_notify(txkring, 0); 249f0ea3689SLuigi Rizzo } 250f0ea3689SLuigi Rizzo return 0; 251f0ea3689SLuigi Rizzo } 252f0ea3689SLuigi Rizzo 253f0ea3689SLuigi Rizzo /* Pipe endpoints are created and destroyed together, so that endopoints do not 254f0ea3689SLuigi Rizzo * have to check for the existence of their peer at each ?xsync. 255f0ea3689SLuigi Rizzo * 256f0ea3689SLuigi Rizzo * To play well with the existing netmap infrastructure (refcounts etc.), we 257f0ea3689SLuigi Rizzo * adopt the following strategy: 258f0ea3689SLuigi Rizzo * 259f0ea3689SLuigi Rizzo * 1) The first endpoint that is created also creates the other endpoint and 260f0ea3689SLuigi Rizzo * grabs a reference to it. 261f0ea3689SLuigi Rizzo * 262f0ea3689SLuigi Rizzo * state A) user1 --> endpoint1 --> endpoint2 263f0ea3689SLuigi Rizzo * 264f0ea3689SLuigi Rizzo * 2) If, starting from state A, endpoint2 is then registered, endpoint1 gives 265f0ea3689SLuigi Rizzo * its reference to the user: 266f0ea3689SLuigi Rizzo * 267f0ea3689SLuigi Rizzo * state B) user1 --> endpoint1 endpoint2 <--- user2 268f0ea3689SLuigi Rizzo * 269f0ea3689SLuigi Rizzo * 3) Assume that, starting from state B endpoint2 is closed. In the unregister 270f0ea3689SLuigi Rizzo * callback endpoint2 notes that endpoint1 is still active and adds a reference 271f0ea3689SLuigi Rizzo * from endpoint1 to itself. When user2 then releases her own reference, 272f0ea3689SLuigi Rizzo * endpoint2 is not destroyed and we are back to state A. A symmetrical state 273f0ea3689SLuigi Rizzo * would be reached if endpoint1 were released instead. 274f0ea3689SLuigi Rizzo * 275f0ea3689SLuigi Rizzo * 4) If, starting from state A, endpoint1 is closed, the destructor notes that 276f0ea3689SLuigi Rizzo * it owns a reference to endpoint2 and releases it. 277f0ea3689SLuigi Rizzo * 278f0ea3689SLuigi Rizzo * Something similar goes on for the creation and destruction of the krings. 279f0ea3689SLuigi Rizzo */ 280f0ea3689SLuigi Rizzo 281f0ea3689SLuigi Rizzo 282f0ea3689SLuigi Rizzo /* netmap_pipe_krings_delete. 283f0ea3689SLuigi Rizzo * 284f0ea3689SLuigi Rizzo * There are two cases: 285f0ea3689SLuigi Rizzo * 286f0ea3689SLuigi Rizzo * 1) state is 287f0ea3689SLuigi Rizzo * 288f0ea3689SLuigi Rizzo * usr1 --> e1 --> e2 289f0ea3689SLuigi Rizzo * 290f0ea3689SLuigi Rizzo * and we are e1. We have to create both sets 291f0ea3689SLuigi Rizzo * of krings. 292f0ea3689SLuigi Rizzo * 293f0ea3689SLuigi Rizzo * 2) state is 294f0ea3689SLuigi Rizzo * 295f0ea3689SLuigi Rizzo * usr1 --> e1 --> e2 296f0ea3689SLuigi Rizzo * 297f0ea3689SLuigi Rizzo * and we are e2. e1 is certainly registered and our 298f0ea3689SLuigi Rizzo * krings already exist, but they may be hidden. 299f0ea3689SLuigi Rizzo */ 300f0ea3689SLuigi Rizzo static int 301f0ea3689SLuigi Rizzo netmap_pipe_krings_create(struct netmap_adapter *na) 302f0ea3689SLuigi Rizzo { 303f0ea3689SLuigi Rizzo struct netmap_pipe_adapter *pna = 304f0ea3689SLuigi Rizzo (struct netmap_pipe_adapter *)na; 305f0ea3689SLuigi Rizzo struct netmap_adapter *ona = &pna->peer->up; 306f0ea3689SLuigi Rizzo int error = 0; 307*847bf383SLuigi Rizzo enum txrx t; 308*847bf383SLuigi Rizzo 309f0ea3689SLuigi Rizzo if (pna->peer_ref) { 310f0ea3689SLuigi Rizzo int i; 311f0ea3689SLuigi Rizzo 312f0ea3689SLuigi Rizzo /* case 1) above */ 313*847bf383SLuigi Rizzo ND("%p: case 1, create everything", na); 314f0ea3689SLuigi Rizzo error = netmap_krings_create(na, 0); 315f0ea3689SLuigi Rizzo if (error) 316f0ea3689SLuigi Rizzo goto err; 317f0ea3689SLuigi Rizzo 318f0ea3689SLuigi Rizzo /* we also create all the rings, since we need to 319f0ea3689SLuigi Rizzo * update the save_ring pointers. 320f0ea3689SLuigi Rizzo * netmap_mem_rings_create (called by our caller) 321f0ea3689SLuigi Rizzo * will not create the rings again 322f0ea3689SLuigi Rizzo */ 323f0ea3689SLuigi Rizzo 324f0ea3689SLuigi Rizzo error = netmap_mem_rings_create(na); 325f0ea3689SLuigi Rizzo if (error) 326f0ea3689SLuigi Rizzo goto del_krings1; 327f0ea3689SLuigi Rizzo 328f0ea3689SLuigi Rizzo /* update our hidden ring pointers */ 329*847bf383SLuigi Rizzo for_rx_tx(t) { 330*847bf383SLuigi Rizzo for (i = 0; i < nma_get_nrings(na, t) + 1; i++) 331*847bf383SLuigi Rizzo NMR(na, t)[i].save_ring = NMR(na, t)[i].ring; 332*847bf383SLuigi Rizzo } 333f0ea3689SLuigi Rizzo 334f0ea3689SLuigi Rizzo /* now, create krings and rings of the other end */ 335f0ea3689SLuigi Rizzo error = netmap_krings_create(ona, 0); 336f0ea3689SLuigi Rizzo if (error) 337f0ea3689SLuigi Rizzo goto del_rings1; 338f0ea3689SLuigi Rizzo 339f0ea3689SLuigi Rizzo error = netmap_mem_rings_create(ona); 340f0ea3689SLuigi Rizzo if (error) 341f0ea3689SLuigi Rizzo goto del_krings2; 342f0ea3689SLuigi Rizzo 343*847bf383SLuigi Rizzo for_rx_tx(t) { 344*847bf383SLuigi Rizzo for (i = 0; i < nma_get_nrings(ona, t) + 1; i++) 345*847bf383SLuigi Rizzo NMR(ona, t)[i].save_ring = NMR(ona, t)[i].ring; 346*847bf383SLuigi Rizzo } 347f0ea3689SLuigi Rizzo 348f0ea3689SLuigi Rizzo /* cross link the krings */ 349*847bf383SLuigi Rizzo for_rx_tx(t) { 350*847bf383SLuigi Rizzo enum txrx r= nm_txrx_swap(t); /* swap NR_TX <-> NR_RX */ 351*847bf383SLuigi Rizzo for (i = 0; i < nma_get_nrings(na, t); i++) { 352*847bf383SLuigi Rizzo NMR(na, t)[i].pipe = NMR(&pna->peer->up, r) + i; 353*847bf383SLuigi Rizzo NMR(&pna->peer->up, r)[i].pipe = NMR(na, t) + i; 354*847bf383SLuigi Rizzo } 355f0ea3689SLuigi Rizzo } 356f0ea3689SLuigi Rizzo } else { 357f0ea3689SLuigi Rizzo int i; 358f0ea3689SLuigi Rizzo /* case 2) above */ 359f0ea3689SLuigi Rizzo /* recover the hidden rings */ 360f0ea3689SLuigi Rizzo ND("%p: case 2, hidden rings", na); 361*847bf383SLuigi Rizzo for_rx_tx(t) { 362*847bf383SLuigi Rizzo for (i = 0; i < nma_get_nrings(na, t) + 1; i++) 363*847bf383SLuigi Rizzo NMR(na, t)[i].ring = NMR(na, t)[i].save_ring; 364*847bf383SLuigi Rizzo } 365f0ea3689SLuigi Rizzo } 366f0ea3689SLuigi Rizzo return 0; 367f0ea3689SLuigi Rizzo 368f0ea3689SLuigi Rizzo del_krings2: 369f0ea3689SLuigi Rizzo netmap_krings_delete(ona); 370f0ea3689SLuigi Rizzo del_rings1: 371f0ea3689SLuigi Rizzo netmap_mem_rings_delete(na); 372f0ea3689SLuigi Rizzo del_krings1: 373f0ea3689SLuigi Rizzo netmap_krings_delete(na); 374f0ea3689SLuigi Rizzo err: 375f0ea3689SLuigi Rizzo return error; 376f0ea3689SLuigi Rizzo } 377f0ea3689SLuigi Rizzo 378f0ea3689SLuigi Rizzo /* netmap_pipe_reg. 379f0ea3689SLuigi Rizzo * 380f0ea3689SLuigi Rizzo * There are two cases on registration (onoff==1) 381f0ea3689SLuigi Rizzo * 382f0ea3689SLuigi Rizzo * 1.a) state is 383f0ea3689SLuigi Rizzo * 384f0ea3689SLuigi Rizzo * usr1 --> e1 --> e2 385f0ea3689SLuigi Rizzo * 386f0ea3689SLuigi Rizzo * and we are e1. Nothing special to do. 387f0ea3689SLuigi Rizzo * 388f0ea3689SLuigi Rizzo * 1.b) state is 389f0ea3689SLuigi Rizzo * 390f0ea3689SLuigi Rizzo * usr1 --> e1 --> e2 <-- usr2 391f0ea3689SLuigi Rizzo * 392f0ea3689SLuigi Rizzo * and we are e2. Drop the ref e1 is holding. 393f0ea3689SLuigi Rizzo * 394f0ea3689SLuigi Rizzo * There are two additional cases on unregister (onoff==0) 395f0ea3689SLuigi Rizzo * 396f0ea3689SLuigi Rizzo * 2.a) state is 397f0ea3689SLuigi Rizzo * 398f0ea3689SLuigi Rizzo * usr1 --> e1 --> e2 399f0ea3689SLuigi Rizzo * 400f0ea3689SLuigi Rizzo * and we are e1. Nothing special to do, e2 will 401f0ea3689SLuigi Rizzo * be cleaned up by the destructor of e1. 402f0ea3689SLuigi Rizzo * 403f0ea3689SLuigi Rizzo * 2.b) state is 404f0ea3689SLuigi Rizzo * 405f0ea3689SLuigi Rizzo * usr1 --> e1 e2 <-- usr2 406f0ea3689SLuigi Rizzo * 407f0ea3689SLuigi Rizzo * and we are either e1 or e2. Add a ref from the 408f0ea3689SLuigi Rizzo * other end and hide our rings. 409f0ea3689SLuigi Rizzo */ 410f0ea3689SLuigi Rizzo static int 411f0ea3689SLuigi Rizzo netmap_pipe_reg(struct netmap_adapter *na, int onoff) 412f0ea3689SLuigi Rizzo { 413f0ea3689SLuigi Rizzo struct netmap_pipe_adapter *pna = 414f0ea3689SLuigi Rizzo (struct netmap_pipe_adapter *)na; 415*847bf383SLuigi Rizzo enum txrx t; 416*847bf383SLuigi Rizzo 417f0ea3689SLuigi Rizzo ND("%p: onoff %d", na, onoff); 418f0ea3689SLuigi Rizzo if (onoff) { 4194bf50f18SLuigi Rizzo na->na_flags |= NAF_NETMAP_ON; 420f0ea3689SLuigi Rizzo } else { 4214bf50f18SLuigi Rizzo na->na_flags &= ~NAF_NETMAP_ON; 422f0ea3689SLuigi Rizzo } 423f0ea3689SLuigi Rizzo if (pna->peer_ref) { 424f0ea3689SLuigi Rizzo ND("%p: case 1.a or 2.a, nothing to do", na); 425f0ea3689SLuigi Rizzo return 0; 426f0ea3689SLuigi Rizzo } 427f0ea3689SLuigi Rizzo if (onoff) { 428f0ea3689SLuigi Rizzo ND("%p: case 1.b, drop peer", na); 429f0ea3689SLuigi Rizzo pna->peer->peer_ref = 0; 430f0ea3689SLuigi Rizzo netmap_adapter_put(na); 431f0ea3689SLuigi Rizzo } else { 432f0ea3689SLuigi Rizzo int i; 433f0ea3689SLuigi Rizzo ND("%p: case 2.b, grab peer", na); 434f0ea3689SLuigi Rizzo netmap_adapter_get(na); 435f0ea3689SLuigi Rizzo pna->peer->peer_ref = 1; 436f0ea3689SLuigi Rizzo /* hide our rings from netmap_mem_rings_delete */ 437*847bf383SLuigi Rizzo for_rx_tx(t) { 438*847bf383SLuigi Rizzo for (i = 0; i < nma_get_nrings(na, t) + 1; i++) { 439*847bf383SLuigi Rizzo NMR(na, t)[i].ring = NULL; 440f0ea3689SLuigi Rizzo } 441f0ea3689SLuigi Rizzo } 442f0ea3689SLuigi Rizzo } 443f0ea3689SLuigi Rizzo return 0; 444f0ea3689SLuigi Rizzo } 445f0ea3689SLuigi Rizzo 446f0ea3689SLuigi Rizzo /* netmap_pipe_krings_delete. 447f0ea3689SLuigi Rizzo * 448f0ea3689SLuigi Rizzo * There are two cases: 449f0ea3689SLuigi Rizzo * 450f0ea3689SLuigi Rizzo * 1) state is 451f0ea3689SLuigi Rizzo * 452f0ea3689SLuigi Rizzo * usr1 --> e1 --> e2 453f0ea3689SLuigi Rizzo * 454f0ea3689SLuigi Rizzo * and we are e1 (e2 is not registered, so krings_delete cannot be 455f0ea3689SLuigi Rizzo * called on it); 456f0ea3689SLuigi Rizzo * 457f0ea3689SLuigi Rizzo * 2) state is 458f0ea3689SLuigi Rizzo * 459f0ea3689SLuigi Rizzo * usr1 --> e1 e2 <-- usr2 460f0ea3689SLuigi Rizzo * 461f0ea3689SLuigi Rizzo * and we are either e1 or e2. 462f0ea3689SLuigi Rizzo * 463f0ea3689SLuigi Rizzo * In the former case we have to also delete the krings of e2; 464f0ea3689SLuigi Rizzo * in the latter case we do nothing (note that our krings 465f0ea3689SLuigi Rizzo * have already been hidden in the unregister callback). 466f0ea3689SLuigi Rizzo */ 467f0ea3689SLuigi Rizzo static void 468f0ea3689SLuigi Rizzo netmap_pipe_krings_delete(struct netmap_adapter *na) 469f0ea3689SLuigi Rizzo { 470f0ea3689SLuigi Rizzo struct netmap_pipe_adapter *pna = 471f0ea3689SLuigi Rizzo (struct netmap_pipe_adapter *)na; 472f0ea3689SLuigi Rizzo struct netmap_adapter *ona; /* na of the other end */ 473f0ea3689SLuigi Rizzo int i; 474*847bf383SLuigi Rizzo enum txrx t; 475f0ea3689SLuigi Rizzo 476f0ea3689SLuigi Rizzo if (!pna->peer_ref) { 477f0ea3689SLuigi Rizzo ND("%p: case 2, kept alive by peer", na); 478f0ea3689SLuigi Rizzo return; 479f0ea3689SLuigi Rizzo } 480f0ea3689SLuigi Rizzo /* case 1) above */ 481f0ea3689SLuigi Rizzo ND("%p: case 1, deleting everyhing", na); 482f0ea3689SLuigi Rizzo netmap_krings_delete(na); /* also zeroes tx_rings etc. */ 483f0ea3689SLuigi Rizzo /* restore the ring to be deleted on the peer */ 484f0ea3689SLuigi Rizzo ona = &pna->peer->up; 485f0ea3689SLuigi Rizzo if (ona->tx_rings == NULL) { 486f0ea3689SLuigi Rizzo /* already deleted, we must be on an 487f0ea3689SLuigi Rizzo * cleanup-after-error path */ 488f0ea3689SLuigi Rizzo return; 489f0ea3689SLuigi Rizzo } 490*847bf383SLuigi Rizzo for_rx_tx(t) { 491*847bf383SLuigi Rizzo for (i = 0; i < nma_get_nrings(ona, t) + 1; i++) 492*847bf383SLuigi Rizzo NMR(ona, t)[i].ring = NMR(ona, t)[i].save_ring; 493*847bf383SLuigi Rizzo } 494f0ea3689SLuigi Rizzo netmap_mem_rings_delete(ona); 495f0ea3689SLuigi Rizzo netmap_krings_delete(ona); 496f0ea3689SLuigi Rizzo } 497f0ea3689SLuigi Rizzo 498f0ea3689SLuigi Rizzo 499f0ea3689SLuigi Rizzo static void 500f0ea3689SLuigi Rizzo netmap_pipe_dtor(struct netmap_adapter *na) 501f0ea3689SLuigi Rizzo { 502f0ea3689SLuigi Rizzo struct netmap_pipe_adapter *pna = 503f0ea3689SLuigi Rizzo (struct netmap_pipe_adapter *)na; 504f0ea3689SLuigi Rizzo ND("%p", na); 505f0ea3689SLuigi Rizzo if (pna->peer_ref) { 506f0ea3689SLuigi Rizzo ND("%p: clean up peer", na); 507f0ea3689SLuigi Rizzo pna->peer_ref = 0; 508f0ea3689SLuigi Rizzo netmap_adapter_put(&pna->peer->up); 509f0ea3689SLuigi Rizzo } 510f0ea3689SLuigi Rizzo if (pna->role == NR_REG_PIPE_MASTER) 511f0ea3689SLuigi Rizzo netmap_pipe_remove(pna->parent, pna); 512f0ea3689SLuigi Rizzo netmap_adapter_put(pna->parent); 513f0ea3689SLuigi Rizzo pna->parent = NULL; 514f0ea3689SLuigi Rizzo } 515f0ea3689SLuigi Rizzo 516f0ea3689SLuigi Rizzo int 517f0ea3689SLuigi Rizzo netmap_get_pipe_na(struct nmreq *nmr, struct netmap_adapter **na, int create) 518f0ea3689SLuigi Rizzo { 519f0ea3689SLuigi Rizzo struct nmreq pnmr; 520f0ea3689SLuigi Rizzo struct netmap_adapter *pna; /* parent adapter */ 521f0ea3689SLuigi Rizzo struct netmap_pipe_adapter *mna, *sna, *req; 522f0ea3689SLuigi Rizzo u_int pipe_id; 523f0ea3689SLuigi Rizzo int role = nmr->nr_flags & NR_REG_MASK; 524f0ea3689SLuigi Rizzo int error; 525f0ea3689SLuigi Rizzo 526f0ea3689SLuigi Rizzo ND("flags %x", nmr->nr_flags); 527f0ea3689SLuigi Rizzo 528f0ea3689SLuigi Rizzo if (role != NR_REG_PIPE_MASTER && role != NR_REG_PIPE_SLAVE) { 529f0ea3689SLuigi Rizzo ND("not a pipe"); 530f0ea3689SLuigi Rizzo return 0; 531f0ea3689SLuigi Rizzo } 532f0ea3689SLuigi Rizzo role = nmr->nr_flags & NR_REG_MASK; 533f0ea3689SLuigi Rizzo 534f0ea3689SLuigi Rizzo /* first, try to find the parent adapter */ 535f0ea3689SLuigi Rizzo bzero(&pnmr, sizeof(pnmr)); 536f0ea3689SLuigi Rizzo memcpy(&pnmr.nr_name, nmr->nr_name, IFNAMSIZ); 537f0ea3689SLuigi Rizzo /* pass to parent the requested number of pipes */ 538f0ea3689SLuigi Rizzo pnmr.nr_arg1 = nmr->nr_arg1; 539f0ea3689SLuigi Rizzo error = netmap_get_na(&pnmr, &pna, create); 540f0ea3689SLuigi Rizzo if (error) { 541f0ea3689SLuigi Rizzo ND("parent lookup failed: %d", error); 542f0ea3689SLuigi Rizzo return error; 543f0ea3689SLuigi Rizzo } 5444bf50f18SLuigi Rizzo ND("found parent: %s", na->name); 545f0ea3689SLuigi Rizzo 546f0ea3689SLuigi Rizzo if (NETMAP_OWNED_BY_KERN(pna)) { 547f0ea3689SLuigi Rizzo ND("parent busy"); 548f0ea3689SLuigi Rizzo error = EBUSY; 549f0ea3689SLuigi Rizzo goto put_out; 550f0ea3689SLuigi Rizzo } 551f0ea3689SLuigi Rizzo 552f0ea3689SLuigi Rizzo /* next, lookup the pipe id in the parent list */ 553f0ea3689SLuigi Rizzo req = NULL; 554f0ea3689SLuigi Rizzo pipe_id = nmr->nr_ringid & NETMAP_RING_MASK; 555f0ea3689SLuigi Rizzo mna = netmap_pipe_find(pna, pipe_id); 556f0ea3689SLuigi Rizzo if (mna) { 557f0ea3689SLuigi Rizzo if (mna->role == role) { 558f0ea3689SLuigi Rizzo ND("found %d directly at %d", pipe_id, mna->parent_slot); 559f0ea3689SLuigi Rizzo req = mna; 560f0ea3689SLuigi Rizzo } else { 561f0ea3689SLuigi Rizzo ND("found %d indirectly at %d", pipe_id, mna->parent_slot); 562f0ea3689SLuigi Rizzo req = mna->peer; 563f0ea3689SLuigi Rizzo } 564f0ea3689SLuigi Rizzo /* the pipe we have found already holds a ref to the parent, 565f0ea3689SLuigi Rizzo * so we need to drop the one we got from netmap_get_na() 566f0ea3689SLuigi Rizzo */ 567f0ea3689SLuigi Rizzo netmap_adapter_put(pna); 568f0ea3689SLuigi Rizzo goto found; 569f0ea3689SLuigi Rizzo } 570f0ea3689SLuigi Rizzo ND("pipe %d not found, create %d", pipe_id, create); 571f0ea3689SLuigi Rizzo if (!create) { 572f0ea3689SLuigi Rizzo error = ENODEV; 573f0ea3689SLuigi Rizzo goto put_out; 574f0ea3689SLuigi Rizzo } 575f0ea3689SLuigi Rizzo /* we create both master and slave. 576f0ea3689SLuigi Rizzo * The endpoint we were asked for holds a reference to 577f0ea3689SLuigi Rizzo * the other one. 578f0ea3689SLuigi Rizzo */ 579f0ea3689SLuigi Rizzo mna = malloc(sizeof(*mna), M_DEVBUF, M_NOWAIT | M_ZERO); 580f0ea3689SLuigi Rizzo if (mna == NULL) { 581f0ea3689SLuigi Rizzo error = ENOMEM; 5824bf50f18SLuigi Rizzo goto put_out; 583f0ea3689SLuigi Rizzo } 5844bf50f18SLuigi Rizzo snprintf(mna->up.name, sizeof(mna->up.name), "%s{%d", pna->name, pipe_id); 585f0ea3689SLuigi Rizzo 586f0ea3689SLuigi Rizzo mna->id = pipe_id; 587f0ea3689SLuigi Rizzo mna->role = NR_REG_PIPE_MASTER; 588f0ea3689SLuigi Rizzo mna->parent = pna; 589f0ea3689SLuigi Rizzo 590f0ea3689SLuigi Rizzo mna->up.nm_txsync = netmap_pipe_txsync; 591f0ea3689SLuigi Rizzo mna->up.nm_rxsync = netmap_pipe_rxsync; 592f0ea3689SLuigi Rizzo mna->up.nm_register = netmap_pipe_reg; 593f0ea3689SLuigi Rizzo mna->up.nm_dtor = netmap_pipe_dtor; 594f0ea3689SLuigi Rizzo mna->up.nm_krings_create = netmap_pipe_krings_create; 595f0ea3689SLuigi Rizzo mna->up.nm_krings_delete = netmap_pipe_krings_delete; 596f0ea3689SLuigi Rizzo mna->up.nm_mem = pna->nm_mem; 597f0ea3689SLuigi Rizzo mna->up.na_lut = pna->na_lut; 598f0ea3689SLuigi Rizzo 599f0ea3689SLuigi Rizzo mna->up.num_tx_rings = 1; 600f0ea3689SLuigi Rizzo mna->up.num_rx_rings = 1; 601f0ea3689SLuigi Rizzo mna->up.num_tx_desc = nmr->nr_tx_slots; 602f0ea3689SLuigi Rizzo nm_bound_var(&mna->up.num_tx_desc, pna->num_tx_desc, 603f0ea3689SLuigi Rizzo 1, NM_PIPE_MAXSLOTS, NULL); 604f0ea3689SLuigi Rizzo mna->up.num_rx_desc = nmr->nr_rx_slots; 605f0ea3689SLuigi Rizzo nm_bound_var(&mna->up.num_rx_desc, pna->num_rx_desc, 606f0ea3689SLuigi Rizzo 1, NM_PIPE_MAXSLOTS, NULL); 607f0ea3689SLuigi Rizzo error = netmap_attach_common(&mna->up); 608f0ea3689SLuigi Rizzo if (error) 6094bf50f18SLuigi Rizzo goto free_mna; 610f0ea3689SLuigi Rizzo /* register the master with the parent */ 611f0ea3689SLuigi Rizzo error = netmap_pipe_add(pna, mna); 612f0ea3689SLuigi Rizzo if (error) 613f0ea3689SLuigi Rizzo goto free_mna; 614f0ea3689SLuigi Rizzo 615f0ea3689SLuigi Rizzo /* create the slave */ 616f0ea3689SLuigi Rizzo sna = malloc(sizeof(*mna), M_DEVBUF, M_NOWAIT | M_ZERO); 617f0ea3689SLuigi Rizzo if (sna == NULL) { 618f0ea3689SLuigi Rizzo error = ENOMEM; 6194bf50f18SLuigi Rizzo goto free_mna; 620f0ea3689SLuigi Rizzo } 621f0ea3689SLuigi Rizzo /* most fields are the same, copy from master and then fix */ 622f0ea3689SLuigi Rizzo *sna = *mna; 6234bf50f18SLuigi Rizzo snprintf(sna->up.name, sizeof(sna->up.name), "%s}%d", pna->name, pipe_id); 624f0ea3689SLuigi Rizzo sna->role = NR_REG_PIPE_SLAVE; 625f0ea3689SLuigi Rizzo error = netmap_attach_common(&sna->up); 626f0ea3689SLuigi Rizzo if (error) 627f0ea3689SLuigi Rizzo goto free_sna; 628f0ea3689SLuigi Rizzo 629f0ea3689SLuigi Rizzo /* join the two endpoints */ 630f0ea3689SLuigi Rizzo mna->peer = sna; 631f0ea3689SLuigi Rizzo sna->peer = mna; 632f0ea3689SLuigi Rizzo 633f0ea3689SLuigi Rizzo /* we already have a reference to the parent, but we 634f0ea3689SLuigi Rizzo * need another one for the other endpoint we created 635f0ea3689SLuigi Rizzo */ 636f0ea3689SLuigi Rizzo netmap_adapter_get(pna); 637f0ea3689SLuigi Rizzo 638f0ea3689SLuigi Rizzo if (role == NR_REG_PIPE_MASTER) { 639f0ea3689SLuigi Rizzo req = mna; 640f0ea3689SLuigi Rizzo mna->peer_ref = 1; 641f0ea3689SLuigi Rizzo netmap_adapter_get(&sna->up); 642f0ea3689SLuigi Rizzo } else { 643f0ea3689SLuigi Rizzo req = sna; 644f0ea3689SLuigi Rizzo sna->peer_ref = 1; 645f0ea3689SLuigi Rizzo netmap_adapter_get(&mna->up); 646f0ea3689SLuigi Rizzo } 647f0ea3689SLuigi Rizzo ND("created master %p and slave %p", mna, sna); 648f0ea3689SLuigi Rizzo found: 649f0ea3689SLuigi Rizzo 650f0ea3689SLuigi Rizzo ND("pipe %d %s at %p", pipe_id, 651f0ea3689SLuigi Rizzo (req->role == NR_REG_PIPE_MASTER ? "master" : "slave"), req); 652f0ea3689SLuigi Rizzo *na = &req->up; 653f0ea3689SLuigi Rizzo netmap_adapter_get(*na); 654f0ea3689SLuigi Rizzo 655f0ea3689SLuigi Rizzo /* write the configuration back */ 656f0ea3689SLuigi Rizzo nmr->nr_tx_rings = req->up.num_tx_rings; 657f0ea3689SLuigi Rizzo nmr->nr_rx_rings = req->up.num_rx_rings; 658f0ea3689SLuigi Rizzo nmr->nr_tx_slots = req->up.num_tx_desc; 659f0ea3689SLuigi Rizzo nmr->nr_rx_slots = req->up.num_rx_desc; 660f0ea3689SLuigi Rizzo 661f0ea3689SLuigi Rizzo /* keep the reference to the parent. 662f0ea3689SLuigi Rizzo * It will be released by the req destructor 663f0ea3689SLuigi Rizzo */ 664f0ea3689SLuigi Rizzo 665f0ea3689SLuigi Rizzo return 0; 666f0ea3689SLuigi Rizzo 667f0ea3689SLuigi Rizzo free_sna: 668f0ea3689SLuigi Rizzo free(sna, M_DEVBUF); 669f0ea3689SLuigi Rizzo free_mna: 670f0ea3689SLuigi Rizzo free(mna, M_DEVBUF); 671f0ea3689SLuigi Rizzo put_out: 672f0ea3689SLuigi Rizzo netmap_adapter_put(pna); 673f0ea3689SLuigi Rizzo return error; 674f0ea3689SLuigi Rizzo } 675f0ea3689SLuigi Rizzo 676f0ea3689SLuigi Rizzo 677f0ea3689SLuigi Rizzo #endif /* WITH_PIPES */ 678