xref: /freebsd/sys/dev/netmap/netmap_pipe.c (revision 71625ec9ad2a9bc8c09784fbd23b759830e0ee5f)
1718cf2ccSPedro F. Giffuni /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
42a7db7a6SVincenzo Maffione  * Copyright (C) 2014-2018 Giuseppe Lettieri
537e3a6d3SLuigi Rizzo  * All rights reserved.
6f0ea3689SLuigi Rizzo  *
7f0ea3689SLuigi Rizzo  * Redistribution and use in source and binary forms, with or without
8f0ea3689SLuigi Rizzo  * modification, are permitted provided that the following conditions
9f0ea3689SLuigi Rizzo  * are met:
10f0ea3689SLuigi Rizzo  *   1. Redistributions of source code must retain the above copyright
11f0ea3689SLuigi Rizzo  *      notice, this list of conditions and the following disclaimer.
12f0ea3689SLuigi Rizzo  *   2. Redistributions in binary form must reproduce the above copyright
13f0ea3689SLuigi Rizzo  *      notice, this list of conditions and the following disclaimer in the
14f0ea3689SLuigi Rizzo  *      documentation and/or other materials provided with the distribution.
15f0ea3689SLuigi Rizzo  *
16f0ea3689SLuigi Rizzo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17f0ea3689SLuigi Rizzo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18f0ea3689SLuigi Rizzo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19f0ea3689SLuigi Rizzo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20f0ea3689SLuigi Rizzo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21f0ea3689SLuigi Rizzo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22f0ea3689SLuigi Rizzo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23f0ea3689SLuigi Rizzo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24f0ea3689SLuigi Rizzo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25f0ea3689SLuigi Rizzo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26f0ea3689SLuigi Rizzo  * SUCH DAMAGE.
27f0ea3689SLuigi Rizzo  */
28f0ea3689SLuigi Rizzo 
29f0ea3689SLuigi Rizzo 
30f0ea3689SLuigi Rizzo #if defined(__FreeBSD__)
31f0ea3689SLuigi Rizzo #include <sys/cdefs.h> /* prerequisite */
32f0ea3689SLuigi Rizzo 
33f0ea3689SLuigi Rizzo #include <sys/types.h>
34f0ea3689SLuigi Rizzo #include <sys/errno.h>
35f0ea3689SLuigi Rizzo #include <sys/param.h>	/* defines used in kernel.h */
36f0ea3689SLuigi Rizzo #include <sys/kernel.h>	/* types used in module initialization */
37f0ea3689SLuigi Rizzo #include <sys/malloc.h>
38f0ea3689SLuigi Rizzo #include <sys/poll.h>
39f0ea3689SLuigi Rizzo #include <sys/lock.h>
40f0ea3689SLuigi Rizzo #include <sys/rwlock.h>
41f0ea3689SLuigi Rizzo #include <sys/selinfo.h>
42f0ea3689SLuigi Rizzo #include <sys/sysctl.h>
43f0ea3689SLuigi Rizzo #include <sys/socket.h> /* sockaddrs */
44f0ea3689SLuigi Rizzo #include <net/if.h>
45f0ea3689SLuigi Rizzo #include <net/if_var.h>
46f0ea3689SLuigi Rizzo #include <machine/bus.h>	/* bus_dmamap_* */
47f0ea3689SLuigi Rizzo #include <sys/refcount.h>
48f0ea3689SLuigi Rizzo 
49f0ea3689SLuigi Rizzo 
50f0ea3689SLuigi Rizzo #elif defined(linux)
51f0ea3689SLuigi Rizzo 
52f0ea3689SLuigi Rizzo #include "bsd_glue.h"
53f0ea3689SLuigi Rizzo 
54f0ea3689SLuigi Rizzo #elif defined(__APPLE__)
55f0ea3689SLuigi Rizzo 
56f0ea3689SLuigi Rizzo #warning OSX support is only partial
57f0ea3689SLuigi Rizzo #include "osx_glue.h"
58f0ea3689SLuigi Rizzo 
5937e3a6d3SLuigi Rizzo #elif defined(_WIN32)
6037e3a6d3SLuigi Rizzo #include "win_glue.h"
6137e3a6d3SLuigi Rizzo 
62f0ea3689SLuigi Rizzo #else
63f0ea3689SLuigi Rizzo 
64f0ea3689SLuigi Rizzo #error	Unsupported platform
65f0ea3689SLuigi Rizzo 
66f0ea3689SLuigi Rizzo #endif /* unsupported */
67f0ea3689SLuigi Rizzo 
68f0ea3689SLuigi Rizzo /*
69f0ea3689SLuigi Rizzo  * common headers
70f0ea3689SLuigi Rizzo  */
71f0ea3689SLuigi Rizzo 
72f0ea3689SLuigi Rizzo #include <net/netmap.h>
73f0ea3689SLuigi Rizzo #include <dev/netmap/netmap_kern.h>
74f0ea3689SLuigi Rizzo #include <dev/netmap/netmap_mem2.h>
75f0ea3689SLuigi Rizzo 
76f0ea3689SLuigi Rizzo #ifdef WITH_PIPES
77f0ea3689SLuigi Rizzo 
78f0ea3689SLuigi Rizzo #define NM_PIPE_MAXSLOTS	4096
792ff91c17SVincenzo Maffione #define NM_PIPE_MAXRINGS	256
80f0ea3689SLuigi Rizzo 
8137e3a6d3SLuigi Rizzo static int netmap_default_pipes = 0; /* ignored, kept for compatibility */
8237e3a6d3SLuigi Rizzo SYSBEGIN(vars_pipes);
83f0ea3689SLuigi Rizzo SYSCTL_DECL(_dev_netmap);
844f80b14cSVincenzo Maffione SYSCTL_INT(_dev_netmap, OID_AUTO, default_pipes, CTLFLAG_RW,
854f80b14cSVincenzo Maffione 		&netmap_default_pipes, 0, "For compatibility only");
8637e3a6d3SLuigi Rizzo SYSEND;
87f0ea3689SLuigi Rizzo 
88f0ea3689SLuigi Rizzo /* allocate the pipe array in the parent adapter */
89847bf383SLuigi Rizzo static int
nm_pipe_alloc(struct netmap_adapter * na,u_int npipes)90847bf383SLuigi Rizzo nm_pipe_alloc(struct netmap_adapter *na, u_int npipes)
91f0ea3689SLuigi Rizzo {
92c3e9b4dbSLuiz Otavio O Souza 	size_t old_len, len;
93847bf383SLuigi Rizzo 	struct netmap_pipe_adapter **npa;
94f0ea3689SLuigi Rizzo 
95847bf383SLuigi Rizzo 	if (npipes <= na->na_max_pipes)
96847bf383SLuigi Rizzo 		/* we already have more entries that requested */
97f0ea3689SLuigi Rizzo 		return 0;
98f0ea3689SLuigi Rizzo 
99847bf383SLuigi Rizzo 	if (npipes < na->na_next_pipe || npipes > NM_MAXPIPES)
100847bf383SLuigi Rizzo 		return EINVAL;
101f0ea3689SLuigi Rizzo 
102c3e9b4dbSLuiz Otavio O Souza 	old_len = sizeof(struct netmap_pipe_adapter *)*na->na_max_pipes;
103f0ea3689SLuigi Rizzo 	len = sizeof(struct netmap_pipe_adapter *) * npipes;
104c3e9b4dbSLuiz Otavio O Souza 	npa = nm_os_realloc(na->na_pipes, len, old_len);
105847bf383SLuigi Rizzo 	if (npa == NULL)
106f0ea3689SLuigi Rizzo 		return ENOMEM;
107f0ea3689SLuigi Rizzo 
108847bf383SLuigi Rizzo 	na->na_pipes = npa;
109f0ea3689SLuigi Rizzo 	na->na_max_pipes = npipes;
110f0ea3689SLuigi Rizzo 
111f0ea3689SLuigi Rizzo 	return 0;
112f0ea3689SLuigi Rizzo }
113f0ea3689SLuigi Rizzo 
114f0ea3689SLuigi Rizzo /* deallocate the parent array in the parent adapter */
115f0ea3689SLuigi Rizzo void
netmap_pipe_dealloc(struct netmap_adapter * na)116f0ea3689SLuigi Rizzo netmap_pipe_dealloc(struct netmap_adapter *na)
117f0ea3689SLuigi Rizzo {
118f0ea3689SLuigi Rizzo 	if (na->na_pipes) {
119847bf383SLuigi Rizzo 		if (na->na_next_pipe > 0) {
12075f4f3edSVincenzo Maffione 			nm_prerr("freeing not empty pipe array for %s (%d dangling pipes)!",
12175f4f3edSVincenzo Maffione 			    na->name, na->na_next_pipe);
122847bf383SLuigi Rizzo 		}
123c3e9b4dbSLuiz Otavio O Souza 		nm_os_free(na->na_pipes);
124f0ea3689SLuigi Rizzo 		na->na_pipes = NULL;
125f0ea3689SLuigi Rizzo 		na->na_max_pipes = 0;
126f0ea3689SLuigi Rizzo 		na->na_next_pipe = 0;
127f0ea3689SLuigi Rizzo 	}
128f0ea3689SLuigi Rizzo }
129f0ea3689SLuigi Rizzo 
130f0ea3689SLuigi Rizzo /* find a pipe endpoint with the given id among the parent's pipes */
131f0ea3689SLuigi Rizzo static struct netmap_pipe_adapter *
netmap_pipe_find(struct netmap_adapter * parent,const char * pipe_id)1322ff91c17SVincenzo Maffione netmap_pipe_find(struct netmap_adapter *parent, const char *pipe_id)
133f0ea3689SLuigi Rizzo {
134f0ea3689SLuigi Rizzo 	int i;
135f0ea3689SLuigi Rizzo 	struct netmap_pipe_adapter *na;
136f0ea3689SLuigi Rizzo 
137f0ea3689SLuigi Rizzo 	for (i = 0; i < parent->na_next_pipe; i++) {
1382ff91c17SVincenzo Maffione 		const char *na_pipe_id;
139f0ea3689SLuigi Rizzo 		na = parent->na_pipes[i];
1402ff91c17SVincenzo Maffione 		na_pipe_id = strrchr(na->up.name,
1412ff91c17SVincenzo Maffione 			na->role == NM_PIPE_ROLE_MASTER ? '{' : '}');
1422ff91c17SVincenzo Maffione 		KASSERT(na_pipe_id != NULL, ("Invalid pipe name"));
1432ff91c17SVincenzo Maffione 		++na_pipe_id;
1442ff91c17SVincenzo Maffione 		if (!strcmp(na_pipe_id, pipe_id)) {
145f0ea3689SLuigi Rizzo 			return na;
146f0ea3689SLuigi Rizzo 		}
147f0ea3689SLuigi Rizzo 	}
148f0ea3689SLuigi Rizzo 	return NULL;
149f0ea3689SLuigi Rizzo }
150f0ea3689SLuigi Rizzo 
151f0ea3689SLuigi Rizzo /* add a new pipe endpoint to the parent array */
152f0ea3689SLuigi Rizzo static int
netmap_pipe_add(struct netmap_adapter * parent,struct netmap_pipe_adapter * na)153f0ea3689SLuigi Rizzo netmap_pipe_add(struct netmap_adapter *parent, struct netmap_pipe_adapter *na)
154f0ea3689SLuigi Rizzo {
155f0ea3689SLuigi Rizzo 	if (parent->na_next_pipe >= parent->na_max_pipes) {
156847bf383SLuigi Rizzo 		u_int npipes = parent->na_max_pipes ?  2*parent->na_max_pipes : 2;
157847bf383SLuigi Rizzo 		int error = nm_pipe_alloc(parent, npipes);
158847bf383SLuigi Rizzo 		if (error)
159847bf383SLuigi Rizzo 			return error;
160f0ea3689SLuigi Rizzo 	}
161f0ea3689SLuigi Rizzo 
162f0ea3689SLuigi Rizzo 	parent->na_pipes[parent->na_next_pipe] = na;
163f0ea3689SLuigi Rizzo 	na->parent_slot = parent->na_next_pipe;
164f0ea3689SLuigi Rizzo 	parent->na_next_pipe++;
165f0ea3689SLuigi Rizzo 	return 0;
166f0ea3689SLuigi Rizzo }
167f0ea3689SLuigi Rizzo 
168f0ea3689SLuigi Rizzo /* remove the given pipe endpoint from the parent array */
169f0ea3689SLuigi Rizzo static void
netmap_pipe_remove(struct netmap_adapter * parent,struct netmap_pipe_adapter * na)170f0ea3689SLuigi Rizzo netmap_pipe_remove(struct netmap_adapter *parent, struct netmap_pipe_adapter *na)
171f0ea3689SLuigi Rizzo {
172f0ea3689SLuigi Rizzo 	u_int n;
173f0ea3689SLuigi Rizzo 	n = --parent->na_next_pipe;
174f0ea3689SLuigi Rizzo 	if (n != na->parent_slot) {
175847bf383SLuigi Rizzo 		struct netmap_pipe_adapter **p =
176847bf383SLuigi Rizzo 			&parent->na_pipes[na->parent_slot];
177847bf383SLuigi Rizzo 		*p = parent->na_pipes[n];
178847bf383SLuigi Rizzo 		(*p)->parent_slot = na->parent_slot;
179f0ea3689SLuigi Rizzo 	}
180f0ea3689SLuigi Rizzo 	parent->na_pipes[n] = NULL;
181f0ea3689SLuigi Rizzo }
182f0ea3689SLuigi Rizzo 
183c3e9b4dbSLuiz Otavio O Souza int
netmap_pipe_txsync(struct netmap_kring * txkring,int flags)1844bf50f18SLuigi Rizzo netmap_pipe_txsync(struct netmap_kring *txkring, int flags)
185f0ea3689SLuigi Rizzo {
1864bf50f18SLuigi Rizzo 	struct netmap_kring *rxkring = txkring->pipe;
1872a7db7a6SVincenzo Maffione 	u_int k, lim = txkring->nkr_num_slots - 1, nk;
1882ff91c17SVincenzo Maffione 	int m; /* slots to transfer */
1892a7db7a6SVincenzo Maffione 	int complete; /* did we see a complete packet ? */
1904f80b14cSVincenzo Maffione 	struct netmap_ring *txring = txkring->ring, *rxring = rxkring->ring;
191f0ea3689SLuigi Rizzo 
19275f4f3edSVincenzo Maffione 	nm_prdis("%p: %s %x -> %s", txkring, txkring->name, flags, rxkring->name);
19375f4f3edSVincenzo Maffione 	nm_prdis(20, "TX before: hwcur %d hwtail %d cur %d head %d tail %d",
1942ff91c17SVincenzo Maffione 		txkring->nr_hwcur, txkring->nr_hwtail,
195f0ea3689SLuigi Rizzo 		txkring->rcur, txkring->rhead, txkring->rtail);
196f0ea3689SLuigi Rizzo 
1972a7db7a6SVincenzo Maffione 	/* update the hwtail */
1982a7db7a6SVincenzo Maffione 	txkring->nr_hwtail = txkring->pipe_tail;
1992a7db7a6SVincenzo Maffione 
200f0ea3689SLuigi Rizzo 	m = txkring->rhead - txkring->nr_hwcur; /* new slots */
201f0ea3689SLuigi Rizzo 	if (m < 0)
202f0ea3689SLuigi Rizzo 		m += txkring->nkr_num_slots;
203f0ea3689SLuigi Rizzo 
2042ff91c17SVincenzo Maffione 	if (m == 0) {
2052ff91c17SVincenzo Maffione 		/* nothing to send */
206f0ea3689SLuigi Rizzo 		return 0;
207f0ea3689SLuigi Rizzo 	}
208f0ea3689SLuigi Rizzo 
2092a7db7a6SVincenzo Maffione 	for (k = txkring->nr_hwcur, nk = lim + 1, complete = 0; m;
2102a7db7a6SVincenzo Maffione 			m--, k = nm_next(k, lim), nk = (complete ? k : nk)) {
2112ff91c17SVincenzo Maffione 		struct netmap_slot *rs = &rxring->slot[k];
2124f80b14cSVincenzo Maffione 		struct netmap_slot *ts = &txring->slot[k];
213a6d768d8SVincenzo Maffione 		uint64_t off = nm_get_offset(rxkring, rs);
214f0ea3689SLuigi Rizzo 
2152a7db7a6SVincenzo Maffione 		*rs = *ts;
216a6d768d8SVincenzo Maffione 		if (nm_get_offset(rxkring, rs) < off) {
217a6d768d8SVincenzo Maffione 			nm_write_offset(rxkring, rs, off);
218a6d768d8SVincenzo Maffione 		}
2192ff91c17SVincenzo Maffione 		if (ts->flags & NS_BUF_CHANGED) {
2202ff91c17SVincenzo Maffione 			ts->flags &= ~NS_BUF_CHANGED;
2212ff91c17SVincenzo Maffione 		}
2222a7db7a6SVincenzo Maffione 		complete = !(ts->flags & NS_MOREFRAG);
223f0ea3689SLuigi Rizzo 	}
224f0ea3689SLuigi Rizzo 
225f0ea3689SLuigi Rizzo 	txkring->nr_hwcur = k;
226f0ea3689SLuigi Rizzo 
22775f4f3edSVincenzo Maffione 	nm_prdis(20, "TX after : hwcur %d hwtail %d cur %d head %d tail %d k %d",
2282ff91c17SVincenzo Maffione 		txkring->nr_hwcur, txkring->nr_hwtail,
2292ff91c17SVincenzo Maffione 		txkring->rcur, txkring->rhead, txkring->rtail, k);
230f0ea3689SLuigi Rizzo 
2312a7db7a6SVincenzo Maffione 	if (likely(nk <= lim)) {
2322a7db7a6SVincenzo Maffione 		mb(); /* make sure the slots are updated before publishing them */
2332a7db7a6SVincenzo Maffione 		rxkring->pipe_tail = nk; /* only publish complete packets */
234847bf383SLuigi Rizzo 		rxkring->nm_notify(rxkring, 0);
2352a7db7a6SVincenzo Maffione 	}
236f0ea3689SLuigi Rizzo 
237f0ea3689SLuigi Rizzo 	return 0;
238f0ea3689SLuigi Rizzo }
239f0ea3689SLuigi Rizzo 
240c3e9b4dbSLuiz Otavio O Souza int
netmap_pipe_rxsync(struct netmap_kring * rxkring,int flags)2414bf50f18SLuigi Rizzo netmap_pipe_rxsync(struct netmap_kring *rxkring, int flags)
242f0ea3689SLuigi Rizzo {
2434bf50f18SLuigi Rizzo 	struct netmap_kring *txkring = rxkring->pipe;
2442ff91c17SVincenzo Maffione 	u_int k, lim = rxkring->nkr_num_slots - 1;
2452ff91c17SVincenzo Maffione 	int m; /* slots to release */
2462ff91c17SVincenzo Maffione 	struct netmap_ring *txring = txkring->ring, *rxring = rxkring->ring;
247f0ea3689SLuigi Rizzo 
24875f4f3edSVincenzo Maffione 	nm_prdis("%p: %s %x -> %s", txkring, txkring->name, flags, rxkring->name);
24975f4f3edSVincenzo Maffione 	nm_prdis(20, "RX before: hwcur %d hwtail %d cur %d head %d tail %d",
2502ff91c17SVincenzo Maffione 		rxkring->nr_hwcur, rxkring->nr_hwtail,
251f0ea3689SLuigi Rizzo 		rxkring->rcur, rxkring->rhead, rxkring->rtail);
252f0ea3689SLuigi Rizzo 
2532a7db7a6SVincenzo Maffione 	/* update the hwtail */
2542a7db7a6SVincenzo Maffione 	rxkring->nr_hwtail = rxkring->pipe_tail;
2552a7db7a6SVincenzo Maffione 
2562ff91c17SVincenzo Maffione 	m = rxkring->rhead - rxkring->nr_hwcur; /* released slots */
2572ff91c17SVincenzo Maffione 	if (m < 0)
2582ff91c17SVincenzo Maffione 		m += rxkring->nkr_num_slots;
2592ff91c17SVincenzo Maffione 
2602ff91c17SVincenzo Maffione 	if (m == 0) {
2612ff91c17SVincenzo Maffione 		/* nothing to release */
2622ff91c17SVincenzo Maffione 		return 0;
263f0ea3689SLuigi Rizzo 	}
2642ff91c17SVincenzo Maffione 
2652ff91c17SVincenzo Maffione 	for (k = rxkring->nr_hwcur; m; m--, k = nm_next(k, lim)) {
2662ff91c17SVincenzo Maffione 		struct netmap_slot *rs = &rxring->slot[k];
2672ff91c17SVincenzo Maffione 		struct netmap_slot *ts = &txring->slot[k];
2682ff91c17SVincenzo Maffione 
269a6d768d8SVincenzo Maffione 		/* copy the slot. This also propagates any offset */
2702ff91c17SVincenzo Maffione 		*ts = *rs;
271a6d768d8SVincenzo Maffione 		if (rs->flags & NS_BUF_CHANGED) {
2722ff91c17SVincenzo Maffione 			rs->flags &= ~NS_BUF_CHANGED;
2732ff91c17SVincenzo Maffione 		}
2742ff91c17SVincenzo Maffione 	}
2752ff91c17SVincenzo Maffione 
2762ff91c17SVincenzo Maffione 	mb(); /* make sure the slots are updated before publishing them */
2772a7db7a6SVincenzo Maffione 	txkring->pipe_tail = nm_prev(k, lim);
2782ff91c17SVincenzo Maffione 	rxkring->nr_hwcur = k;
2792ff91c17SVincenzo Maffione 
28075f4f3edSVincenzo Maffione 	nm_prdis(20, "RX after : hwcur %d hwtail %d cur %d head %d tail %d k %d",
2812ff91c17SVincenzo Maffione 		rxkring->nr_hwcur, rxkring->nr_hwtail,
2822ff91c17SVincenzo Maffione 		rxkring->rcur, rxkring->rhead, rxkring->rtail, k);
2832ff91c17SVincenzo Maffione 
2842ff91c17SVincenzo Maffione 	txkring->nm_notify(txkring, 0);
2852ff91c17SVincenzo Maffione 
286f0ea3689SLuigi Rizzo 	return 0;
287f0ea3689SLuigi Rizzo }
288f0ea3689SLuigi Rizzo 
289f0ea3689SLuigi Rizzo /* Pipe endpoints are created and destroyed together, so that endopoints do not
290f0ea3689SLuigi Rizzo  * have to check for the existence of their peer at each ?xsync.
291f0ea3689SLuigi Rizzo  *
292f0ea3689SLuigi Rizzo  * To play well with the existing netmap infrastructure (refcounts etc.), we
293f0ea3689SLuigi Rizzo  * adopt the following strategy:
294f0ea3689SLuigi Rizzo  *
295f0ea3689SLuigi Rizzo  * 1) The first endpoint that is created also creates the other endpoint and
296f0ea3689SLuigi Rizzo  * grabs a reference to it.
297f0ea3689SLuigi Rizzo  *
298f0ea3689SLuigi Rizzo  *    state A)  user1 --> endpoint1 --> endpoint2
299f0ea3689SLuigi Rizzo  *
300f0ea3689SLuigi Rizzo  * 2) If, starting from state A, endpoint2 is then registered, endpoint1 gives
301f0ea3689SLuigi Rizzo  * its reference to the user:
302f0ea3689SLuigi Rizzo  *
303f0ea3689SLuigi Rizzo  *    state B)  user1 --> endpoint1     endpoint2 <--- user2
304f0ea3689SLuigi Rizzo  *
305f0ea3689SLuigi Rizzo  * 3) Assume that, starting from state B endpoint2 is closed. In the unregister
306f0ea3689SLuigi Rizzo  * callback endpoint2 notes that endpoint1 is still active and adds a reference
307f0ea3689SLuigi Rizzo  * from endpoint1 to itself. When user2 then releases her own reference,
308f0ea3689SLuigi Rizzo  * endpoint2 is not destroyed and we are back to state A. A symmetrical state
309f0ea3689SLuigi Rizzo  * would be reached if endpoint1 were released instead.
310f0ea3689SLuigi Rizzo  *
311f0ea3689SLuigi Rizzo  * 4) If, starting from state A, endpoint1 is closed, the destructor notes that
312f0ea3689SLuigi Rizzo  * it owns a reference to endpoint2 and releases it.
313f0ea3689SLuigi Rizzo  *
314f0ea3689SLuigi Rizzo  * Something similar goes on for the creation and destruction of the krings.
315f0ea3689SLuigi Rizzo  */
316f0ea3689SLuigi Rizzo 
317f0ea3689SLuigi Rizzo 
netmap_pipe_krings_create_both(struct netmap_adapter * na,struct netmap_adapter * ona)31875f4f3edSVincenzo Maffione int netmap_pipe_krings_create_both(struct netmap_adapter *na,
31975f4f3edSVincenzo Maffione 				  struct netmap_adapter *ona)
32075f4f3edSVincenzo Maffione {
32175f4f3edSVincenzo Maffione 	enum txrx t;
32275f4f3edSVincenzo Maffione 	int error;
32375f4f3edSVincenzo Maffione 	int i;
32475f4f3edSVincenzo Maffione 
32575f4f3edSVincenzo Maffione 	/* case 1) below */
32675f4f3edSVincenzo Maffione 	nm_prdis("%p: case 1, create both ends", na);
32775f4f3edSVincenzo Maffione 	error = netmap_krings_create(na, 0);
32875f4f3edSVincenzo Maffione 	if (error)
32975f4f3edSVincenzo Maffione 		return error;
33075f4f3edSVincenzo Maffione 
33175f4f3edSVincenzo Maffione 	/* create the krings of the other end */
33275f4f3edSVincenzo Maffione 	error = netmap_krings_create(ona, 0);
33375f4f3edSVincenzo Maffione 	if (error)
33475f4f3edSVincenzo Maffione 		goto del_krings1;
33575f4f3edSVincenzo Maffione 
33675f4f3edSVincenzo Maffione 	/* cross link the krings and initialize the pipe_tails */
33775f4f3edSVincenzo Maffione 	for_rx_tx(t) {
33875f4f3edSVincenzo Maffione 		enum txrx r = nm_txrx_swap(t); /* swap NR_TX <-> NR_RX */
33975f4f3edSVincenzo Maffione 		for (i = 0; i < nma_get_nrings(na, t); i++) {
34075f4f3edSVincenzo Maffione 			struct netmap_kring *k1 = NMR(na, t)[i],
34175f4f3edSVincenzo Maffione 					    *k2 = NMR(ona, r)[i];
34275f4f3edSVincenzo Maffione 			k1->pipe = k2;
34375f4f3edSVincenzo Maffione 			k2->pipe = k1;
34475f4f3edSVincenzo Maffione 			/* mark all peer-adapter rings as fake */
34575f4f3edSVincenzo Maffione 			k2->nr_kflags |= NKR_FAKERING;
34675f4f3edSVincenzo Maffione 			/* init tails */
34775f4f3edSVincenzo Maffione 			k1->pipe_tail = k1->nr_hwtail;
34875f4f3edSVincenzo Maffione 			k2->pipe_tail = k2->nr_hwtail;
34975f4f3edSVincenzo Maffione 		}
35075f4f3edSVincenzo Maffione 	}
35175f4f3edSVincenzo Maffione 
35275f4f3edSVincenzo Maffione 	return 0;
35375f4f3edSVincenzo Maffione 
35475f4f3edSVincenzo Maffione del_krings1:
35575f4f3edSVincenzo Maffione 	netmap_krings_delete(na);
35675f4f3edSVincenzo Maffione 	return error;
35775f4f3edSVincenzo Maffione }
35875f4f3edSVincenzo Maffione 
359c3e9b4dbSLuiz Otavio O Souza /* netmap_pipe_krings_create.
360f0ea3689SLuigi Rizzo  *
361f0ea3689SLuigi Rizzo  * There are two cases:
362f0ea3689SLuigi Rizzo  *
363f0ea3689SLuigi Rizzo  * 1) state is
364f0ea3689SLuigi Rizzo  *
365f0ea3689SLuigi Rizzo  *        usr1 --> e1 --> e2
366f0ea3689SLuigi Rizzo  *
367f0ea3689SLuigi Rizzo  *    and we are e1. We have to create both sets
368f0ea3689SLuigi Rizzo  *    of krings.
369f0ea3689SLuigi Rizzo  *
370f0ea3689SLuigi Rizzo  * 2) state is
371f0ea3689SLuigi Rizzo  *
372f0ea3689SLuigi Rizzo  *        usr1 --> e1 --> e2
373f0ea3689SLuigi Rizzo  *
374f0ea3689SLuigi Rizzo  *    and we are e2. e1 is certainly registered and our
37537e3a6d3SLuigi Rizzo  *    krings already exist. Nothing to do.
376f0ea3689SLuigi Rizzo  */
377f0ea3689SLuigi Rizzo static int
netmap_pipe_krings_create(struct netmap_adapter * na)378f0ea3689SLuigi Rizzo netmap_pipe_krings_create(struct netmap_adapter *na)
379f0ea3689SLuigi Rizzo {
380f0ea3689SLuigi Rizzo 	struct netmap_pipe_adapter *pna =
381f0ea3689SLuigi Rizzo 		(struct netmap_pipe_adapter *)na;
382f0ea3689SLuigi Rizzo 	struct netmap_adapter *ona = &pna->peer->up;
383847bf383SLuigi Rizzo 
38475f4f3edSVincenzo Maffione 	if (pna->peer_ref)
38575f4f3edSVincenzo Maffione 		return netmap_pipe_krings_create_both(na, ona);
386f0ea3689SLuigi Rizzo 
387f0ea3689SLuigi Rizzo 	return 0;
388f0ea3689SLuigi Rizzo }
389f0ea3689SLuigi Rizzo 
39075f4f3edSVincenzo Maffione int
netmap_pipe_reg_both(struct netmap_adapter * na,struct netmap_adapter * ona)39175f4f3edSVincenzo Maffione netmap_pipe_reg_both(struct netmap_adapter *na, struct netmap_adapter *ona)
392f0ea3689SLuigi Rizzo {
39337e3a6d3SLuigi Rizzo 	int i, error = 0;
394847bf383SLuigi Rizzo 	enum txrx t;
395847bf383SLuigi Rizzo 
39637e3a6d3SLuigi Rizzo 	for_rx_tx(t) {
397c3e9b4dbSLuiz Otavio O Souza 		for (i = 0; i < nma_get_nrings(na, t); i++) {
3982ff91c17SVincenzo Maffione 			struct netmap_kring *kring = NMR(na, t)[i];
39937e3a6d3SLuigi Rizzo 
40037e3a6d3SLuigi Rizzo 			if (nm_kring_pending_on(kring)) {
401c3e9b4dbSLuiz Otavio O Souza 				/* mark the peer ring as needed */
40237e3a6d3SLuigi Rizzo 				kring->pipe->nr_kflags |= NKR_NEEDRING;
40337e3a6d3SLuigi Rizzo 			}
40437e3a6d3SLuigi Rizzo 		}
40537e3a6d3SLuigi Rizzo 	}
40637e3a6d3SLuigi Rizzo 
4072ff91c17SVincenzo Maffione 	/* create all missing needed rings on the other end.
4082ff91c17SVincenzo Maffione 	 * Either our end, or the other, has been marked as
4092ff91c17SVincenzo Maffione 	 * fake, so the allocation will not be done twice.
4102ff91c17SVincenzo Maffione 	 */
41137e3a6d3SLuigi Rizzo 	error = netmap_mem_rings_create(ona);
41237e3a6d3SLuigi Rizzo 	if (error)
41337e3a6d3SLuigi Rizzo 		return error;
41437e3a6d3SLuigi Rizzo 
41537e3a6d3SLuigi Rizzo 	/* In case of no error we put our rings in netmap mode */
41637e3a6d3SLuigi Rizzo 	for_rx_tx(t) {
417b6e66be2SVincenzo Maffione 		for (i = 0; i < nma_get_nrings(na, t); i++) {
4182ff91c17SVincenzo Maffione 			struct netmap_kring *kring = NMR(na, t)[i];
41937e3a6d3SLuigi Rizzo 			if (nm_kring_pending_on(kring)) {
4202ff91c17SVincenzo Maffione 
4212a7db7a6SVincenzo Maffione 				kring->nr_mode = NKR_NETMAP_ON;
4222a7db7a6SVincenzo Maffione 				if ((kring->nr_kflags & NKR_FAKERING) &&
4232a7db7a6SVincenzo Maffione 				    (kring->pipe->nr_kflags & NKR_FAKERING)) {
4242a7db7a6SVincenzo Maffione 					/* this is a re-open of a pipe
4252a7db7a6SVincenzo Maffione 					 * end-point kept alive by the other end.
4262a7db7a6SVincenzo Maffione 					 * We need to leave everything as it is
4272a7db7a6SVincenzo Maffione 					 */
4282a7db7a6SVincenzo Maffione 					continue;
4292a7db7a6SVincenzo Maffione 				}
4302a7db7a6SVincenzo Maffione 
431a6d768d8SVincenzo Maffione 				/* copy the buffers from the non-fake ring
432a6d768d8SVincenzo Maffione 				 * (this also propagates any initial offset)
433a6d768d8SVincenzo Maffione 				 */
434a6d768d8SVincenzo Maffione 				memcpy(kring->pipe->ring->slot,
435a6d768d8SVincenzo Maffione 				       kring->ring->slot,
4362ff91c17SVincenzo Maffione 				       sizeof(struct netmap_slot) *
437a6d768d8SVincenzo Maffione 						kring->nkr_num_slots);
438a6d768d8SVincenzo Maffione 				/* copy the offset-related fields */
439a6d768d8SVincenzo Maffione 				*(uint64_t *)(uintptr_t)&kring->pipe->ring->offset_mask =
440a6d768d8SVincenzo Maffione 					kring->ring->offset_mask;
441a6d768d8SVincenzo Maffione 				*(uint64_t *)(uintptr_t)&kring->pipe->ring->buf_align =
442a6d768d8SVincenzo Maffione 					kring->ring->buf_align;
4432ff91c17SVincenzo Maffione 				/* mark both rings as fake and needed,
4442ff91c17SVincenzo Maffione 				 * so that buffers will not be
4452ff91c17SVincenzo Maffione 				 * deleted by the standard machinery
4462ff91c17SVincenzo Maffione 				 * (we will delete them by ourselves in
4472ff91c17SVincenzo Maffione 				 * netmap_pipe_krings_delete)
4482ff91c17SVincenzo Maffione 				 */
449a6d768d8SVincenzo Maffione 				kring->nr_kflags |=
4502ff91c17SVincenzo Maffione 					(NKR_FAKERING | NKR_NEEDRING);
45137e3a6d3SLuigi Rizzo 				kring->nr_mode = NKR_NETMAP_ON;
45237e3a6d3SLuigi Rizzo 			}
45337e3a6d3SLuigi Rizzo 		}
45437e3a6d3SLuigi Rizzo 	}
45575f4f3edSVincenzo Maffione 
45675f4f3edSVincenzo Maffione 	return 0;
45775f4f3edSVincenzo Maffione }
45875f4f3edSVincenzo Maffione 
45975f4f3edSVincenzo Maffione /* netmap_pipe_reg.
46075f4f3edSVincenzo Maffione  *
46175f4f3edSVincenzo Maffione  * There are two cases on registration (onoff==1)
46275f4f3edSVincenzo Maffione  *
46375f4f3edSVincenzo Maffione  * 1.a) state is
46475f4f3edSVincenzo Maffione  *
46575f4f3edSVincenzo Maffione  *        usr1 --> e1 --> e2
46675f4f3edSVincenzo Maffione  *
46775f4f3edSVincenzo Maffione  *      and we are e1. Create the needed rings of the
46875f4f3edSVincenzo Maffione  *      other end.
46975f4f3edSVincenzo Maffione  *
47075f4f3edSVincenzo Maffione  * 1.b) state is
47175f4f3edSVincenzo Maffione  *
47275f4f3edSVincenzo Maffione  *        usr1 --> e1 --> e2 <-- usr2
47375f4f3edSVincenzo Maffione  *
47475f4f3edSVincenzo Maffione  *      and we are e2. Drop the ref e1 is holding.
47575f4f3edSVincenzo Maffione  *
47675f4f3edSVincenzo Maffione  *  There are two additional cases on unregister (onoff==0)
47775f4f3edSVincenzo Maffione  *
47875f4f3edSVincenzo Maffione  *  2.a) state is
47975f4f3edSVincenzo Maffione  *
48075f4f3edSVincenzo Maffione  *         usr1 --> e1 --> e2
48175f4f3edSVincenzo Maffione  *
48275f4f3edSVincenzo Maffione  *       and we are e1. Nothing special to do, e2 will
48375f4f3edSVincenzo Maffione  *       be cleaned up by the destructor of e1.
48475f4f3edSVincenzo Maffione  *
48575f4f3edSVincenzo Maffione  *  2.b) state is
48675f4f3edSVincenzo Maffione  *
48775f4f3edSVincenzo Maffione  *         usr1 --> e1     e2 <-- usr2
48875f4f3edSVincenzo Maffione  *
48975f4f3edSVincenzo Maffione  *       and we are either e1 or e2. Add a ref from the
49075f4f3edSVincenzo Maffione  *       other end.
49175f4f3edSVincenzo Maffione  */
49275f4f3edSVincenzo Maffione static int
netmap_pipe_reg(struct netmap_adapter * na,int onoff)49375f4f3edSVincenzo Maffione netmap_pipe_reg(struct netmap_adapter *na, int onoff)
49475f4f3edSVincenzo Maffione {
49575f4f3edSVincenzo Maffione 	struct netmap_pipe_adapter *pna =
49675f4f3edSVincenzo Maffione 		(struct netmap_pipe_adapter *)na;
49775f4f3edSVincenzo Maffione 	struct netmap_adapter *ona = &pna->peer->up;
49875f4f3edSVincenzo Maffione 	int error = 0;
49975f4f3edSVincenzo Maffione 
50075f4f3edSVincenzo Maffione 	nm_prdis("%p: onoff %d", na, onoff);
50175f4f3edSVincenzo Maffione 	if (onoff) {
50275f4f3edSVincenzo Maffione 		error = netmap_pipe_reg_both(na, ona);
50375f4f3edSVincenzo Maffione 		if (error) {
50475f4f3edSVincenzo Maffione 			return error;
50575f4f3edSVincenzo Maffione 		}
50637e3a6d3SLuigi Rizzo 		if (na->active_fds == 0)
5074bf50f18SLuigi Rizzo 			na->na_flags |= NAF_NETMAP_ON;
508f0ea3689SLuigi Rizzo 	} else {
50937e3a6d3SLuigi Rizzo 		if (na->active_fds == 0)
5104bf50f18SLuigi Rizzo 			na->na_flags &= ~NAF_NETMAP_ON;
51175f4f3edSVincenzo Maffione 		netmap_krings_mode_commit(na, onoff);
512c3e9b4dbSLuiz Otavio O Souza 	}
51337e3a6d3SLuigi Rizzo 
51437e3a6d3SLuigi Rizzo 	if (na->active_fds) {
51575f4f3edSVincenzo Maffione 		nm_prdis("active_fds %d", na->active_fds);
51637e3a6d3SLuigi Rizzo 		return 0;
51737e3a6d3SLuigi Rizzo 	}
51837e3a6d3SLuigi Rizzo 
519f0ea3689SLuigi Rizzo 	if (pna->peer_ref) {
52075f4f3edSVincenzo Maffione 		nm_prdis("%p: case 1.a or 2.a, nothing to do", na);
521f0ea3689SLuigi Rizzo 		return 0;
522f0ea3689SLuigi Rizzo 	}
523f0ea3689SLuigi Rizzo 	if (onoff) {
52475f4f3edSVincenzo Maffione 		nm_prdis("%p: case 1.b, drop peer", na);
525f0ea3689SLuigi Rizzo 		pna->peer->peer_ref = 0;
526f0ea3689SLuigi Rizzo 		netmap_adapter_put(na);
527f0ea3689SLuigi Rizzo 	} else {
52875f4f3edSVincenzo Maffione 		nm_prdis("%p: case 2.b, grab peer", na);
529f0ea3689SLuigi Rizzo 		netmap_adapter_get(na);
530f0ea3689SLuigi Rizzo 		pna->peer->peer_ref = 1;
531f0ea3689SLuigi Rizzo 	}
53237e3a6d3SLuigi Rizzo 	return error;
533f0ea3689SLuigi Rizzo }
534f0ea3689SLuigi Rizzo 
53575f4f3edSVincenzo Maffione void
netmap_pipe_krings_delete_both(struct netmap_adapter * na,struct netmap_adapter * ona)53675f4f3edSVincenzo Maffione netmap_pipe_krings_delete_both(struct netmap_adapter *na,
53775f4f3edSVincenzo Maffione 			       struct netmap_adapter *ona)
53875f4f3edSVincenzo Maffione {
53975f4f3edSVincenzo Maffione 	struct netmap_adapter *sna;
54075f4f3edSVincenzo Maffione 	enum txrx t;
54175f4f3edSVincenzo Maffione 	int i;
54275f4f3edSVincenzo Maffione 
54375f4f3edSVincenzo Maffione 	/* case 1) below */
54475f4f3edSVincenzo Maffione 	nm_prdis("%p: case 1, deleting everything", na);
54575f4f3edSVincenzo Maffione 	/* To avoid double-frees we zero-out all the buffers in the kernel part
54675f4f3edSVincenzo Maffione 	 * of each ring. The reason is this: If the user is behaving correctly,
54775f4f3edSVincenzo Maffione 	 * all buffers are found in exactly one slot in the userspace part of
54875f4f3edSVincenzo Maffione 	 * some ring.  If the user is not behaving correctly, we cannot release
54975f4f3edSVincenzo Maffione 	 * buffers cleanly anyway. In the latter case, the allocator will
55075f4f3edSVincenzo Maffione 	 * return to a clean state only when all its users will close.
55175f4f3edSVincenzo Maffione 	 */
55275f4f3edSVincenzo Maffione 	sna = na;
55375f4f3edSVincenzo Maffione cleanup:
55475f4f3edSVincenzo Maffione 	for_rx_tx(t) {
55575f4f3edSVincenzo Maffione 		for (i = 0; i < nma_get_nrings(sna, t); i++) {
55675f4f3edSVincenzo Maffione 			struct netmap_kring *kring = NMR(sna, t)[i];
55775f4f3edSVincenzo Maffione 			struct netmap_ring *ring = kring->ring;
55875f4f3edSVincenzo Maffione 			uint32_t j, lim = kring->nkr_num_slots - 1;
55975f4f3edSVincenzo Maffione 
56075f4f3edSVincenzo Maffione 			nm_prdis("%s ring %p hwtail %u hwcur %u",
56175f4f3edSVincenzo Maffione 				kring->name, ring, kring->nr_hwtail, kring->nr_hwcur);
56275f4f3edSVincenzo Maffione 
56375f4f3edSVincenzo Maffione 			if (ring == NULL)
56475f4f3edSVincenzo Maffione 				continue;
56575f4f3edSVincenzo Maffione 
56675f4f3edSVincenzo Maffione 			if (kring->tx == NR_RX)
56775f4f3edSVincenzo Maffione 				ring->slot[kring->pipe_tail].buf_idx = 0;
56875f4f3edSVincenzo Maffione 
56975f4f3edSVincenzo Maffione 			for (j = nm_next(kring->pipe_tail, lim);
57075f4f3edSVincenzo Maffione 			     j != kring->nr_hwcur;
57175f4f3edSVincenzo Maffione 			     j = nm_next(j, lim))
57275f4f3edSVincenzo Maffione 			{
57375f4f3edSVincenzo Maffione 				nm_prdis("%s[%d] %u", kring->name, j, ring->slot[j].buf_idx);
57475f4f3edSVincenzo Maffione 				ring->slot[j].buf_idx = 0;
57575f4f3edSVincenzo Maffione 			}
57675f4f3edSVincenzo Maffione 			kring->nr_kflags &= ~(NKR_FAKERING | NKR_NEEDRING);
57775f4f3edSVincenzo Maffione 		}
57875f4f3edSVincenzo Maffione 
57975f4f3edSVincenzo Maffione 	}
58075f4f3edSVincenzo Maffione 	if (sna != ona && ona->tx_rings) {
58175f4f3edSVincenzo Maffione 		sna = ona;
58275f4f3edSVincenzo Maffione 		goto cleanup;
58375f4f3edSVincenzo Maffione 	}
58475f4f3edSVincenzo Maffione 
58575f4f3edSVincenzo Maffione 	netmap_mem_rings_delete(na);
58675f4f3edSVincenzo Maffione 	netmap_krings_delete(na); /* also zeroes tx_rings etc. */
58775f4f3edSVincenzo Maffione 
58875f4f3edSVincenzo Maffione 	if (ona->tx_rings == NULL) {
58975f4f3edSVincenzo Maffione 		/* already deleted, we must be on an
59075f4f3edSVincenzo Maffione 		 * cleanup-after-error path */
59175f4f3edSVincenzo Maffione 		return;
59275f4f3edSVincenzo Maffione 	}
59375f4f3edSVincenzo Maffione 	netmap_mem_rings_delete(ona);
59475f4f3edSVincenzo Maffione 	netmap_krings_delete(ona);
59575f4f3edSVincenzo Maffione }
59675f4f3edSVincenzo Maffione 
597f0ea3689SLuigi Rizzo /* netmap_pipe_krings_delete.
598f0ea3689SLuigi Rizzo  *
599f0ea3689SLuigi Rizzo  * There are two cases:
600f0ea3689SLuigi Rizzo  *
601f0ea3689SLuigi Rizzo  * 1) state is
602f0ea3689SLuigi Rizzo  *
603f0ea3689SLuigi Rizzo  *                usr1 --> e1 --> e2
604f0ea3689SLuigi Rizzo  *
605f0ea3689SLuigi Rizzo  *    and we are e1 (e2 is not registered, so krings_delete cannot be
606f0ea3689SLuigi Rizzo  *    called on it);
607f0ea3689SLuigi Rizzo  *
608f0ea3689SLuigi Rizzo  * 2) state is
609f0ea3689SLuigi Rizzo  *
610f0ea3689SLuigi Rizzo  *                usr1 --> e1     e2 <-- usr2
611f0ea3689SLuigi Rizzo  *
612f0ea3689SLuigi Rizzo  *    and we are either e1 or e2.
613f0ea3689SLuigi Rizzo  *
614f0ea3689SLuigi Rizzo  * In the former case we have to also delete the krings of e2;
6152ff91c17SVincenzo Maffione  * in the latter case we do nothing.
616f0ea3689SLuigi Rizzo  */
617f0ea3689SLuigi Rizzo static void
netmap_pipe_krings_delete(struct netmap_adapter * na)618f0ea3689SLuigi Rizzo netmap_pipe_krings_delete(struct netmap_adapter *na)
619f0ea3689SLuigi Rizzo {
620f0ea3689SLuigi Rizzo 	struct netmap_pipe_adapter *pna =
621f0ea3689SLuigi Rizzo 		(struct netmap_pipe_adapter *)na;
62275f4f3edSVincenzo Maffione 	struct netmap_adapter *ona; /* na of the other end */
623f0ea3689SLuigi Rizzo 
624f0ea3689SLuigi Rizzo 	if (!pna->peer_ref) {
62575f4f3edSVincenzo Maffione 		nm_prdis("%p: case 2, kept alive by peer",  na);
626f0ea3689SLuigi Rizzo 		return;
627f0ea3689SLuigi Rizzo 	}
6282ff91c17SVincenzo Maffione 	ona = &pna->peer->up;
62975f4f3edSVincenzo Maffione 	netmap_pipe_krings_delete_both(na, ona);
630f0ea3689SLuigi Rizzo }
631f0ea3689SLuigi Rizzo 
632f0ea3689SLuigi Rizzo 
633f0ea3689SLuigi Rizzo static void
netmap_pipe_dtor(struct netmap_adapter * na)634f0ea3689SLuigi Rizzo netmap_pipe_dtor(struct netmap_adapter *na)
635f0ea3689SLuigi Rizzo {
636f0ea3689SLuigi Rizzo 	struct netmap_pipe_adapter *pna =
637f0ea3689SLuigi Rizzo 		(struct netmap_pipe_adapter *)na;
63875f4f3edSVincenzo Maffione 	nm_prdis("%p %p", na, pna->parent_ifp);
639f0ea3689SLuigi Rizzo 	if (pna->peer_ref) {
64075f4f3edSVincenzo Maffione 		nm_prdis("%p: clean up peer", na);
641f0ea3689SLuigi Rizzo 		pna->peer_ref = 0;
642f0ea3689SLuigi Rizzo 		netmap_adapter_put(&pna->peer->up);
643f0ea3689SLuigi Rizzo 	}
6442ff91c17SVincenzo Maffione 	if (pna->role == NM_PIPE_ROLE_MASTER)
645f0ea3689SLuigi Rizzo 		netmap_pipe_remove(pna->parent, pna);
646c3e9b4dbSLuiz Otavio O Souza 	if (pna->parent_ifp)
647c3e9b4dbSLuiz Otavio O Souza 		if_rele(pna->parent_ifp);
648f0ea3689SLuigi Rizzo 	netmap_adapter_put(pna->parent);
649f0ea3689SLuigi Rizzo 	pna->parent = NULL;
650f0ea3689SLuigi Rizzo }
651f0ea3689SLuigi Rizzo 
652f0ea3689SLuigi Rizzo int
netmap_get_pipe_na(struct nmreq_header * hdr,struct netmap_adapter ** na,struct netmap_mem_d * nmd,int create)6532ff91c17SVincenzo Maffione netmap_get_pipe_na(struct nmreq_header *hdr, struct netmap_adapter **na,
654c3e9b4dbSLuiz Otavio O Souza 		struct netmap_mem_d *nmd, int create)
655f0ea3689SLuigi Rizzo {
656cfa866f6SMatt Macy 	struct nmreq_register *req = (struct nmreq_register *)(uintptr_t)hdr->nr_body;
657f0ea3689SLuigi Rizzo 	struct netmap_adapter *pna; /* parent adapter */
6582ff91c17SVincenzo Maffione 	struct netmap_pipe_adapter *mna, *sna, *reqna;
659e330262fSJustin Hibbits 	if_t ifp = NULL;
6602ff91c17SVincenzo Maffione 	const char *pipe_id = NULL;
6612ff91c17SVincenzo Maffione 	int role = 0;
662c3e9b4dbSLuiz Otavio O Souza 	int error, retries = 0;
663a6d768d8SVincenzo Maffione 	char *cbra, pipe_char;
664f0ea3689SLuigi Rizzo 
6652ff91c17SVincenzo Maffione 	/* Try to parse the pipe syntax 'xx{yy' or 'xx}yy'. */
6662ff91c17SVincenzo Maffione 	cbra = strrchr(hdr->nr_name, '{');
6672ff91c17SVincenzo Maffione 	if (cbra != NULL) {
6682ff91c17SVincenzo Maffione 		role = NM_PIPE_ROLE_MASTER;
6692ff91c17SVincenzo Maffione 	} else {
6702ff91c17SVincenzo Maffione 		cbra = strrchr(hdr->nr_name, '}');
6712ff91c17SVincenzo Maffione 		if (cbra != NULL) {
6722ff91c17SVincenzo Maffione 			role = NM_PIPE_ROLE_SLAVE;
6732ff91c17SVincenzo Maffione 		} else {
67475f4f3edSVincenzo Maffione 			nm_prdis("not a pipe");
675f0ea3689SLuigi Rizzo 			return 0;
676f0ea3689SLuigi Rizzo 		}
6772ff91c17SVincenzo Maffione 	}
678a6d768d8SVincenzo Maffione 	pipe_char = *cbra;
6792ff91c17SVincenzo Maffione 	pipe_id = cbra + 1;
6802ff91c17SVincenzo Maffione 	if (*pipe_id == '\0' || cbra == hdr->nr_name) {
6812ff91c17SVincenzo Maffione 		/* Bracket is the last character, so pipe name is missing;
6822ff91c17SVincenzo Maffione 		 * or bracket is the first character, so base port name
6832ff91c17SVincenzo Maffione 		 * is missing. */
6842ff91c17SVincenzo Maffione 		return EINVAL;
6852ff91c17SVincenzo Maffione 	}
6862ff91c17SVincenzo Maffione 
6872ff91c17SVincenzo Maffione 	if (req->nr_mode != NR_REG_ALL_NIC && req->nr_mode != NR_REG_ONE_NIC) {
6882ff91c17SVincenzo Maffione 		/* We only accept modes involving hardware rings. */
6892ff91c17SVincenzo Maffione 		return EINVAL;
6902ff91c17SVincenzo Maffione 	}
691f0ea3689SLuigi Rizzo 
692f0ea3689SLuigi Rizzo 	/* first, try to find the parent adapter */
693c3e9b4dbSLuiz Otavio O Souza 	for (;;) {
694c3e9b4dbSLuiz Otavio O Souza 		int create_error;
695c3e9b4dbSLuiz Otavio O Souza 
6962ff91c17SVincenzo Maffione 		/* Temporarily remove the pipe suffix. */
6972ff91c17SVincenzo Maffione 		*cbra = '\0';
6982ff91c17SVincenzo Maffione 		error = netmap_get_na(hdr, &pna, &ifp, nmd, create);
6992ff91c17SVincenzo Maffione 		/* Restore the pipe suffix. */
700a6d768d8SVincenzo Maffione 		*cbra = pipe_char;
701c3e9b4dbSLuiz Otavio O Souza 		if (!error)
702c3e9b4dbSLuiz Otavio O Souza 			break;
703c3e9b4dbSLuiz Otavio O Souza 		if (error != ENXIO || retries++) {
70475f4f3edSVincenzo Maffione 			nm_prdis("parent lookup failed: %d", error);
705f0ea3689SLuigi Rizzo 			return error;
706f0ea3689SLuigi Rizzo 		}
70775f4f3edSVincenzo Maffione 		nm_prdis("try to create a persistent vale port");
708c3e9b4dbSLuiz Otavio O Souza 		/* create a persistent vale port and try again */
7092ff91c17SVincenzo Maffione 		*cbra = '\0';
710c3e9b4dbSLuiz Otavio O Souza 		NMG_UNLOCK();
7112ff91c17SVincenzo Maffione 		create_error = netmap_vi_create(hdr, 1 /* autodelete */);
712c3e9b4dbSLuiz Otavio O Souza 		NMG_LOCK();
713a6d768d8SVincenzo Maffione 		*cbra = pipe_char;
714c3e9b4dbSLuiz Otavio O Souza 		if (create_error && create_error != EEXIST) {
715c3e9b4dbSLuiz Otavio O Souza 			if (create_error != EOPNOTSUPP) {
71675f4f3edSVincenzo Maffione 				nm_prerr("failed to create a persistent vale port: %d",
71775f4f3edSVincenzo Maffione 				    create_error);
718c3e9b4dbSLuiz Otavio O Souza 			}
719c3e9b4dbSLuiz Otavio O Souza 			return error;
720c3e9b4dbSLuiz Otavio O Souza 		}
721c3e9b4dbSLuiz Otavio O Souza 	}
722f0ea3689SLuigi Rizzo 
723f0ea3689SLuigi Rizzo 	if (NETMAP_OWNED_BY_KERN(pna)) {
72475f4f3edSVincenzo Maffione 		nm_prdis("parent busy");
725f0ea3689SLuigi Rizzo 		error = EBUSY;
726f0ea3689SLuigi Rizzo 		goto put_out;
727f0ea3689SLuigi Rizzo 	}
728f0ea3689SLuigi Rizzo 
729f0ea3689SLuigi Rizzo 	/* next, lookup the pipe id in the parent list */
7302ff91c17SVincenzo Maffione 	reqna = NULL;
731f0ea3689SLuigi Rizzo 	mna = netmap_pipe_find(pna, pipe_id);
732f0ea3689SLuigi Rizzo 	if (mna) {
733f0ea3689SLuigi Rizzo 		if (mna->role == role) {
73475f4f3edSVincenzo Maffione 			nm_prdis("found %s directly at %d", pipe_id, mna->parent_slot);
7352ff91c17SVincenzo Maffione 			reqna = mna;
736f0ea3689SLuigi Rizzo 		} else {
73775f4f3edSVincenzo Maffione 			nm_prdis("found %s indirectly at %d", pipe_id, mna->parent_slot);
7382ff91c17SVincenzo Maffione 			reqna = mna->peer;
739f0ea3689SLuigi Rizzo 		}
740f0ea3689SLuigi Rizzo 		/* the pipe we have found already holds a ref to the parent,
741f0ea3689SLuigi Rizzo 		 * so we need to drop the one we got from netmap_get_na()
742f0ea3689SLuigi Rizzo 		 */
743c3e9b4dbSLuiz Otavio O Souza 		netmap_unget_na(pna, ifp);
744f0ea3689SLuigi Rizzo 		goto found;
745f0ea3689SLuigi Rizzo 	}
74675f4f3edSVincenzo Maffione 	nm_prdis("pipe %s not found, create %d", pipe_id, create);
747f0ea3689SLuigi Rizzo 	if (!create) {
748f0ea3689SLuigi Rizzo 		error = ENODEV;
749f0ea3689SLuigi Rizzo 		goto put_out;
750f0ea3689SLuigi Rizzo 	}
751f0ea3689SLuigi Rizzo 	/* we create both master and slave.
752f0ea3689SLuigi Rizzo 	 * The endpoint we were asked for holds a reference to
753f0ea3689SLuigi Rizzo 	 * the other one.
754f0ea3689SLuigi Rizzo 	 */
755c3e9b4dbSLuiz Otavio O Souza 	mna = nm_os_malloc(sizeof(*mna));
756f0ea3689SLuigi Rizzo 	if (mna == NULL) {
757f0ea3689SLuigi Rizzo 		error = ENOMEM;
7584bf50f18SLuigi Rizzo 		goto put_out;
759f0ea3689SLuigi Rizzo 	}
7602ff91c17SVincenzo Maffione 	snprintf(mna->up.name, sizeof(mna->up.name), "%s{%s", pna->name, pipe_id);
761f0ea3689SLuigi Rizzo 
7622ff91c17SVincenzo Maffione 	mna->role = NM_PIPE_ROLE_MASTER;
763f0ea3689SLuigi Rizzo 	mna->parent = pna;
764c3e9b4dbSLuiz Otavio O Souza 	mna->parent_ifp = ifp;
765f0ea3689SLuigi Rizzo 
766f0ea3689SLuigi Rizzo 	mna->up.nm_txsync = netmap_pipe_txsync;
767f0ea3689SLuigi Rizzo 	mna->up.nm_rxsync = netmap_pipe_rxsync;
768f0ea3689SLuigi Rizzo 	mna->up.nm_register = netmap_pipe_reg;
769f0ea3689SLuigi Rizzo 	mna->up.nm_dtor = netmap_pipe_dtor;
770f0ea3689SLuigi Rizzo 	mna->up.nm_krings_create = netmap_pipe_krings_create;
771f0ea3689SLuigi Rizzo 	mna->up.nm_krings_delete = netmap_pipe_krings_delete;
772c3e9b4dbSLuiz Otavio O Souza 	mna->up.nm_mem = netmap_mem_get(pna->nm_mem);
773a6d768d8SVincenzo Maffione 	mna->up.na_flags |= NAF_MEM_OWNER | NAF_OFFSETS;
774f0ea3689SLuigi Rizzo 	mna->up.na_lut = pna->na_lut;
775f0ea3689SLuigi Rizzo 
7762ff91c17SVincenzo Maffione 	mna->up.num_tx_rings = req->nr_tx_rings;
7772ff91c17SVincenzo Maffione 	nm_bound_var(&mna->up.num_tx_rings, 1,
7782ff91c17SVincenzo Maffione 			1, NM_PIPE_MAXRINGS, NULL);
7792ff91c17SVincenzo Maffione 	mna->up.num_rx_rings = req->nr_rx_rings;
7802ff91c17SVincenzo Maffione 	nm_bound_var(&mna->up.num_rx_rings, 1,
7812ff91c17SVincenzo Maffione 			1, NM_PIPE_MAXRINGS, NULL);
7822ff91c17SVincenzo Maffione 	mna->up.num_tx_desc = req->nr_tx_slots;
783f0ea3689SLuigi Rizzo 	nm_bound_var(&mna->up.num_tx_desc, pna->num_tx_desc,
784f0ea3689SLuigi Rizzo 			1, NM_PIPE_MAXSLOTS, NULL);
7852ff91c17SVincenzo Maffione 	mna->up.num_rx_desc = req->nr_rx_slots;
786f0ea3689SLuigi Rizzo 	nm_bound_var(&mna->up.num_rx_desc, pna->num_rx_desc,
787f0ea3689SLuigi Rizzo 			1, NM_PIPE_MAXSLOTS, NULL);
788f0ea3689SLuigi Rizzo 	error = netmap_attach_common(&mna->up);
789f0ea3689SLuigi Rizzo 	if (error)
7904bf50f18SLuigi Rizzo 		goto free_mna;
791f0ea3689SLuigi Rizzo 	/* register the master with the parent */
792f0ea3689SLuigi Rizzo 	error = netmap_pipe_add(pna, mna);
793f0ea3689SLuigi Rizzo 	if (error)
794f0ea3689SLuigi Rizzo 		goto free_mna;
795f0ea3689SLuigi Rizzo 
796f0ea3689SLuigi Rizzo 	/* create the slave */
797c3e9b4dbSLuiz Otavio O Souza 	sna = nm_os_malloc(sizeof(*mna));
798f0ea3689SLuigi Rizzo 	if (sna == NULL) {
799f0ea3689SLuigi Rizzo 		error = ENOMEM;
8009694aad3SLuigi Rizzo 		goto unregister_mna;
801f0ea3689SLuigi Rizzo 	}
802f0ea3689SLuigi Rizzo 	/* most fields are the same, copy from master and then fix */
803f0ea3689SLuigi Rizzo 	*sna = *mna;
804c3e9b4dbSLuiz Otavio O Souza 	sna->up.nm_mem = netmap_mem_get(mna->up.nm_mem);
805c52382bdSVincenzo Maffione 	/* swap the number of tx/rx rings and slots */
8062ff91c17SVincenzo Maffione 	sna->up.num_tx_rings = mna->up.num_rx_rings;
807c52382bdSVincenzo Maffione 	sna->up.num_tx_desc  = mna->up.num_rx_desc;
8082ff91c17SVincenzo Maffione 	sna->up.num_rx_rings = mna->up.num_tx_rings;
809c52382bdSVincenzo Maffione 	sna->up.num_rx_desc  = mna->up.num_tx_desc;
8102ff91c17SVincenzo Maffione 	snprintf(sna->up.name, sizeof(sna->up.name), "%s}%s", pna->name, pipe_id);
8112ff91c17SVincenzo Maffione 	sna->role = NM_PIPE_ROLE_SLAVE;
812f0ea3689SLuigi Rizzo 	error = netmap_attach_common(&sna->up);
813f0ea3689SLuigi Rizzo 	if (error)
814f0ea3689SLuigi Rizzo 		goto free_sna;
815f0ea3689SLuigi Rizzo 
816f0ea3689SLuigi Rizzo 	/* join the two endpoints */
817f0ea3689SLuigi Rizzo 	mna->peer = sna;
818f0ea3689SLuigi Rizzo 	sna->peer = mna;
819f0ea3689SLuigi Rizzo 
820f0ea3689SLuigi Rizzo 	/* we already have a reference to the parent, but we
821f0ea3689SLuigi Rizzo 	 * need another one for the other endpoint we created
822f0ea3689SLuigi Rizzo 	 */
823f0ea3689SLuigi Rizzo 	netmap_adapter_get(pna);
824c3e9b4dbSLuiz Otavio O Souza 	/* likewise for the ifp, if any */
825c3e9b4dbSLuiz Otavio O Souza 	if (ifp)
826c3e9b4dbSLuiz Otavio O Souza 		if_ref(ifp);
827f0ea3689SLuigi Rizzo 
8282ff91c17SVincenzo Maffione 	if (role == NM_PIPE_ROLE_MASTER) {
8292ff91c17SVincenzo Maffione 		reqna = mna;
830f0ea3689SLuigi Rizzo 		mna->peer_ref = 1;
831f0ea3689SLuigi Rizzo 		netmap_adapter_get(&sna->up);
832f0ea3689SLuigi Rizzo 	} else {
8332ff91c17SVincenzo Maffione 		reqna = sna;
834f0ea3689SLuigi Rizzo 		sna->peer_ref = 1;
835f0ea3689SLuigi Rizzo 		netmap_adapter_get(&mna->up);
836f0ea3689SLuigi Rizzo 	}
83775f4f3edSVincenzo Maffione 	nm_prdis("created master %p and slave %p", mna, sna);
838f0ea3689SLuigi Rizzo found:
839f0ea3689SLuigi Rizzo 
84075f4f3edSVincenzo Maffione 	nm_prdis("pipe %s %s at %p", pipe_id,
8412ff91c17SVincenzo Maffione 		(reqna->role == NM_PIPE_ROLE_MASTER ? "master" : "slave"), reqna);
8422ff91c17SVincenzo Maffione 	*na = &reqna->up;
843f0ea3689SLuigi Rizzo 	netmap_adapter_get(*na);
844f0ea3689SLuigi Rizzo 
845f0ea3689SLuigi Rizzo 	/* keep the reference to the parent.
846f0ea3689SLuigi Rizzo 	 * It will be released by the req destructor
847f0ea3689SLuigi Rizzo 	 */
848f0ea3689SLuigi Rizzo 
849f0ea3689SLuigi Rizzo 	return 0;
850f0ea3689SLuigi Rizzo 
851f0ea3689SLuigi Rizzo free_sna:
852c3e9b4dbSLuiz Otavio O Souza 	nm_os_free(sna);
8539694aad3SLuigi Rizzo unregister_mna:
8549694aad3SLuigi Rizzo 	netmap_pipe_remove(pna, mna);
855f0ea3689SLuigi Rizzo free_mna:
856c3e9b4dbSLuiz Otavio O Souza 	nm_os_free(mna);
857f0ea3689SLuigi Rizzo put_out:
85837e3a6d3SLuigi Rizzo 	netmap_unget_na(pna, ifp);
859f0ea3689SLuigi Rizzo 	return error;
860f0ea3689SLuigi Rizzo }
861f0ea3689SLuigi Rizzo 
862f0ea3689SLuigi Rizzo 
863f0ea3689SLuigi Rizzo #endif /* WITH_PIPES */
864