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