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