netmap_pipe.c revision 342033
1/*
2 * Copyright (C) 2014-2018 Giuseppe Lettieri
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *   1. Redistributions of source code must retain the above copyright
9 *      notice, this list of conditions and the following disclaimer.
10 *   2. Redistributions in binary form must reproduce the above copyright
11 *      notice, this list of conditions and the following disclaimer in the
12 *      documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27/* $FreeBSD: stable/11/sys/dev/netmap/netmap_pipe.c 342033 2018-12-13 10:13:29Z vmaffione $ */
28
29#if defined(__FreeBSD__)
30#include <sys/cdefs.h> /* prerequisite */
31
32#include <sys/types.h>
33#include <sys/errno.h>
34#include <sys/param.h>	/* defines used in kernel.h */
35#include <sys/kernel.h>	/* types used in module initialization */
36#include <sys/malloc.h>
37#include <sys/poll.h>
38#include <sys/lock.h>
39#include <sys/rwlock.h>
40#include <sys/selinfo.h>
41#include <sys/sysctl.h>
42#include <sys/socket.h> /* sockaddrs */
43#include <net/if.h>
44#include <net/if_var.h>
45#include <machine/bus.h>	/* bus_dmamap_* */
46#include <sys/refcount.h>
47
48
49#elif defined(linux)
50
51#include "bsd_glue.h"
52
53#elif defined(__APPLE__)
54
55#warning OSX support is only partial
56#include "osx_glue.h"
57
58#elif defined(_WIN32)
59#include "win_glue.h"
60
61#else
62
63#error	Unsupported platform
64
65#endif /* unsupported */
66
67/*
68 * common headers
69 */
70
71#include <net/netmap.h>
72#include <dev/netmap/netmap_kern.h>
73#include <dev/netmap/netmap_mem2.h>
74
75#ifdef WITH_PIPES
76
77#define NM_PIPE_MAXSLOTS	4096
78#define NM_PIPE_MAXRINGS	256
79
80static int netmap_default_pipes = 0; /* ignored, kept for compatibility */
81SYSBEGIN(vars_pipes);
82SYSCTL_DECL(_dev_netmap);
83SYSCTL_INT(_dev_netmap, OID_AUTO, default_pipes, CTLFLAG_RW,
84		&netmap_default_pipes, 0, "For compatibility only");
85SYSEND;
86
87/* allocate the pipe array in the parent adapter */
88static int
89nm_pipe_alloc(struct netmap_adapter *na, u_int npipes)
90{
91	size_t old_len, len;
92	struct netmap_pipe_adapter **npa;
93
94	if (npipes <= na->na_max_pipes)
95		/* we already have more entries that requested */
96		return 0;
97
98	if (npipes < na->na_next_pipe || npipes > NM_MAXPIPES)
99		return EINVAL;
100
101	old_len = sizeof(struct netmap_pipe_adapter *)*na->na_max_pipes;
102	len = sizeof(struct netmap_pipe_adapter *) * npipes;
103	npa = nm_os_realloc(na->na_pipes, len, old_len);
104	if (npa == NULL)
105		return ENOMEM;
106
107	na->na_pipes = npa;
108	na->na_max_pipes = npipes;
109
110	return 0;
111}
112
113/* deallocate the parent array in the parent adapter */
114void
115netmap_pipe_dealloc(struct netmap_adapter *na)
116{
117	if (na->na_pipes) {
118		if (na->na_next_pipe > 0) {
119			D("freeing not empty pipe array for %s (%d dangling pipes)!", na->name,
120					na->na_next_pipe);
121		}
122		nm_os_free(na->na_pipes);
123		na->na_pipes = NULL;
124		na->na_max_pipes = 0;
125		na->na_next_pipe = 0;
126	}
127}
128
129/* find a pipe endpoint with the given id among the parent's pipes */
130static struct netmap_pipe_adapter *
131netmap_pipe_find(struct netmap_adapter *parent, const char *pipe_id)
132{
133	int i;
134	struct netmap_pipe_adapter *na;
135
136	for (i = 0; i < parent->na_next_pipe; i++) {
137		const char *na_pipe_id;
138		na = parent->na_pipes[i];
139		na_pipe_id = strrchr(na->up.name,
140			na->role == NM_PIPE_ROLE_MASTER ? '{' : '}');
141		KASSERT(na_pipe_id != NULL, ("Invalid pipe name"));
142		++na_pipe_id;
143		if (!strcmp(na_pipe_id, pipe_id)) {
144			return na;
145		}
146	}
147	return NULL;
148}
149
150/* add a new pipe endpoint to the parent array */
151static int
152netmap_pipe_add(struct netmap_adapter *parent, struct netmap_pipe_adapter *na)
153{
154	if (parent->na_next_pipe >= parent->na_max_pipes) {
155		u_int npipes = parent->na_max_pipes ?  2*parent->na_max_pipes : 2;
156		int error = nm_pipe_alloc(parent, npipes);
157		if (error)
158			return error;
159	}
160
161	parent->na_pipes[parent->na_next_pipe] = na;
162	na->parent_slot = parent->na_next_pipe;
163	parent->na_next_pipe++;
164	return 0;
165}
166
167/* remove the given pipe endpoint from the parent array */
168static void
169netmap_pipe_remove(struct netmap_adapter *parent, struct netmap_pipe_adapter *na)
170{
171	u_int n;
172	n = --parent->na_next_pipe;
173	if (n != na->parent_slot) {
174		struct netmap_pipe_adapter **p =
175			&parent->na_pipes[na->parent_slot];
176		*p = parent->na_pipes[n];
177		(*p)->parent_slot = na->parent_slot;
178	}
179	parent->na_pipes[n] = NULL;
180}
181
182int
183netmap_pipe_txsync(struct netmap_kring *txkring, int flags)
184{
185	struct netmap_kring *rxkring = txkring->pipe;
186	u_int k, lim = txkring->nkr_num_slots - 1, nk;
187	int m; /* slots to transfer */
188	int complete; /* did we see a complete packet ? */
189	struct netmap_ring *txring = txkring->ring, *rxring = rxkring->ring;
190
191	ND("%p: %s %x -> %s", txkring, txkring->name, flags, rxkring->name);
192	ND(20, "TX before: hwcur %d hwtail %d cur %d head %d tail %d",
193		txkring->nr_hwcur, txkring->nr_hwtail,
194		txkring->rcur, txkring->rhead, txkring->rtail);
195
196	/* update the hwtail */
197	txkring->nr_hwtail = txkring->pipe_tail;
198
199	m = txkring->rhead - txkring->nr_hwcur; /* new slots */
200	if (m < 0)
201		m += txkring->nkr_num_slots;
202
203	if (m == 0) {
204		/* nothing to send */
205		return 0;
206	}
207
208	for (k = txkring->nr_hwcur, nk = lim + 1, complete = 0; m;
209			m--, k = nm_next(k, lim), nk = (complete ? k : nk)) {
210		struct netmap_slot *rs = &rxring->slot[k];
211		struct netmap_slot *ts = &txring->slot[k];
212
213		*rs = *ts;
214		if (ts->flags & NS_BUF_CHANGED) {
215			ts->flags &= ~NS_BUF_CHANGED;
216		}
217		complete = !(ts->flags & NS_MOREFRAG);
218	}
219
220	txkring->nr_hwcur = k;
221
222	ND(20, "TX after : hwcur %d hwtail %d cur %d head %d tail %d k %d",
223		txkring->nr_hwcur, txkring->nr_hwtail,
224		txkring->rcur, txkring->rhead, txkring->rtail, k);
225
226	if (likely(nk <= lim)) {
227		mb(); /* make sure the slots are updated before publishing them */
228		rxkring->pipe_tail = nk; /* only publish complete packets */
229		rxkring->nm_notify(rxkring, 0);
230	}
231
232	return 0;
233}
234
235int
236netmap_pipe_rxsync(struct netmap_kring *rxkring, int flags)
237{
238	struct netmap_kring *txkring = rxkring->pipe;
239	u_int k, lim = rxkring->nkr_num_slots - 1;
240	int m; /* slots to release */
241	struct netmap_ring *txring = txkring->ring, *rxring = rxkring->ring;
242
243	ND("%p: %s %x -> %s", txkring, txkring->name, flags, rxkring->name);
244	ND(20, "RX before: hwcur %d hwtail %d cur %d head %d tail %d",
245		rxkring->nr_hwcur, rxkring->nr_hwtail,
246		rxkring->rcur, rxkring->rhead, rxkring->rtail);
247
248	/* update the hwtail */
249	rxkring->nr_hwtail = rxkring->pipe_tail;
250
251	m = rxkring->rhead - rxkring->nr_hwcur; /* released slots */
252	if (m < 0)
253		m += rxkring->nkr_num_slots;
254
255	if (m == 0) {
256		/* nothing to release */
257		return 0;
258	}
259
260	for (k = rxkring->nr_hwcur; m; m--, k = nm_next(k, lim)) {
261		struct netmap_slot *rs = &rxring->slot[k];
262		struct netmap_slot *ts = &txring->slot[k];
263
264		if (rs->flags & NS_BUF_CHANGED) {
265			/* copy the slot and report the buffer change */
266			*ts = *rs;
267			rs->flags &= ~NS_BUF_CHANGED;
268		}
269	}
270
271	mb(); /* make sure the slots are updated before publishing them */
272	txkring->pipe_tail = nm_prev(k, lim);
273	rxkring->nr_hwcur = k;
274
275	ND(20, "RX after : hwcur %d hwtail %d cur %d head %d tail %d k %d",
276		rxkring->nr_hwcur, rxkring->nr_hwtail,
277		rxkring->rcur, rxkring->rhead, rxkring->rtail, k);
278
279	txkring->nm_notify(txkring, 0);
280
281	return 0;
282}
283
284/* Pipe endpoints are created and destroyed together, so that endopoints do not
285 * have to check for the existence of their peer at each ?xsync.
286 *
287 * To play well with the existing netmap infrastructure (refcounts etc.), we
288 * adopt the following strategy:
289 *
290 * 1) The first endpoint that is created also creates the other endpoint and
291 * grabs a reference to it.
292 *
293 *    state A)  user1 --> endpoint1 --> endpoint2
294 *
295 * 2) If, starting from state A, endpoint2 is then registered, endpoint1 gives
296 * its reference to the user:
297 *
298 *    state B)  user1 --> endpoint1     endpoint2 <--- user2
299 *
300 * 3) Assume that, starting from state B endpoint2 is closed. In the unregister
301 * callback endpoint2 notes that endpoint1 is still active and adds a reference
302 * from endpoint1 to itself. When user2 then releases her own reference,
303 * endpoint2 is not destroyed and we are back to state A. A symmetrical state
304 * would be reached if endpoint1 were released instead.
305 *
306 * 4) If, starting from state A, endpoint1 is closed, the destructor notes that
307 * it owns a reference to endpoint2 and releases it.
308 *
309 * Something similar goes on for the creation and destruction of the krings.
310 */
311
312
313/* netmap_pipe_krings_create.
314 *
315 * There are two cases:
316 *
317 * 1) state is
318 *
319 *        usr1 --> e1 --> e2
320 *
321 *    and we are e1. We have to create both sets
322 *    of krings.
323 *
324 * 2) state is
325 *
326 *        usr1 --> e1 --> e2
327 *
328 *    and we are e2. e1 is certainly registered and our
329 *    krings already exist. Nothing to do.
330 */
331static int
332netmap_pipe_krings_create(struct netmap_adapter *na)
333{
334	struct netmap_pipe_adapter *pna =
335		(struct netmap_pipe_adapter *)na;
336	struct netmap_adapter *ona = &pna->peer->up;
337	int error = 0;
338	enum txrx t;
339
340	if (pna->peer_ref) {
341		int i;
342
343		/* case 1) above */
344		ND("%p: case 1, create both ends", na);
345		error = netmap_krings_create(na, 0);
346		if (error)
347			goto err;
348
349		/* create the krings of the other end */
350		error = netmap_krings_create(ona, 0);
351		if (error)
352			goto del_krings1;
353
354		/* cross link the krings and initialize the pipe_tails */
355		for_rx_tx(t) {
356			enum txrx r = nm_txrx_swap(t); /* swap NR_TX <-> NR_RX */
357			for (i = 0; i < nma_get_nrings(na, t); i++) {
358				struct netmap_kring *k1 = NMR(na, t)[i],
359					            *k2 = NMR(ona, r)[i];
360				k1->pipe = k2;
361				k2->pipe = k1;
362				/* mark all peer-adapter rings as fake */
363				k2->nr_kflags |= NKR_FAKERING;
364				/* init tails */
365				k1->pipe_tail = k1->nr_hwtail;
366				k2->pipe_tail = k2->nr_hwtail;
367			}
368		}
369
370	}
371	return 0;
372
373del_krings1:
374	netmap_krings_delete(na);
375err:
376	return error;
377}
378
379/* netmap_pipe_reg.
380 *
381 * There are two cases on registration (onoff==1)
382 *
383 * 1.a) state is
384 *
385 *        usr1 --> e1 --> e2
386 *
387 *      and we are e1. Create the needed rings of the
388 *      other end.
389 *
390 * 1.b) state is
391 *
392 *        usr1 --> e1 --> e2 <-- usr2
393 *
394 *      and we are e2. Drop the ref e1 is holding.
395 *
396 *  There are two additional cases on unregister (onoff==0)
397 *
398 *  2.a) state is
399 *
400 *         usr1 --> e1 --> e2
401 *
402 *       and we are e1. Nothing special to do, e2 will
403 *       be cleaned up by the destructor of e1.
404 *
405 *  2.b) state is
406 *
407 *         usr1 --> e1     e2 <-- usr2
408 *
409 *       and we are either e1 or e2. Add a ref from the
410 *       other end.
411 */
412static int
413netmap_pipe_reg(struct netmap_adapter *na, int onoff)
414{
415	struct netmap_pipe_adapter *pna =
416		(struct netmap_pipe_adapter *)na;
417	struct netmap_adapter *ona = &pna->peer->up;
418	int i, error = 0;
419	enum txrx t;
420
421	ND("%p: onoff %d", na, onoff);
422	if (onoff) {
423		for_rx_tx(t) {
424			for (i = 0; i < nma_get_nrings(na, t); i++) {
425				struct netmap_kring *kring = NMR(na, t)[i];
426
427				if (nm_kring_pending_on(kring)) {
428					/* mark the peer ring as needed */
429					kring->pipe->nr_kflags |= NKR_NEEDRING;
430				}
431			}
432		}
433
434		/* create all missing needed rings on the other end.
435		 * Either our end, or the other, has been marked as
436		 * fake, so the allocation will not be done twice.
437		 */
438		error = netmap_mem_rings_create(ona);
439		if (error)
440			return error;
441
442		/* In case of no error we put our rings in netmap mode */
443		for_rx_tx(t) {
444			for (i = 0; i < nma_get_nrings(na, t); i++) {
445				struct netmap_kring *kring = NMR(na, t)[i];
446				if (nm_kring_pending_on(kring)) {
447					struct netmap_kring *sring, *dring;
448
449					kring->nr_mode = NKR_NETMAP_ON;
450					if ((kring->nr_kflags & NKR_FAKERING) &&
451					    (kring->pipe->nr_kflags & NKR_FAKERING)) {
452						/* this is a re-open of a pipe
453						 * end-point kept alive by the other end.
454						 * We need to leave everything as it is
455						 */
456						continue;
457					}
458
459					/* copy the buffers from the non-fake ring */
460					if (kring->nr_kflags & NKR_FAKERING) {
461						sring = kring->pipe;
462						dring = kring;
463					} else {
464						sring = kring;
465						dring = kring->pipe;
466					}
467					memcpy(dring->ring->slot,
468					       sring->ring->slot,
469					       sizeof(struct netmap_slot) *
470							sring->nkr_num_slots);
471					/* mark both rings as fake and needed,
472					 * so that buffers will not be
473					 * deleted by the standard machinery
474					 * (we will delete them by ourselves in
475					 * netmap_pipe_krings_delete)
476					 */
477					sring->nr_kflags |=
478						(NKR_FAKERING | NKR_NEEDRING);
479					dring->nr_kflags |=
480						(NKR_FAKERING | NKR_NEEDRING);
481					kring->nr_mode = NKR_NETMAP_ON;
482				}
483			}
484		}
485		if (na->active_fds == 0)
486			na->na_flags |= NAF_NETMAP_ON;
487	} else {
488		if (na->active_fds == 0)
489			na->na_flags &= ~NAF_NETMAP_ON;
490		for_rx_tx(t) {
491			for (i = 0; i < nma_get_nrings(na, t); i++) {
492				struct netmap_kring *kring = NMR(na, t)[i];
493
494				if (nm_kring_pending_off(kring)) {
495					kring->nr_mode = NKR_NETMAP_OFF;
496				}
497			}
498		}
499	}
500
501	if (na->active_fds) {
502		ND("active_fds %d", na->active_fds);
503		return 0;
504	}
505
506	if (pna->peer_ref) {
507		ND("%p: case 1.a or 2.a, nothing to do", na);
508		return 0;
509	}
510	if (onoff) {
511		ND("%p: case 1.b, drop peer", na);
512		pna->peer->peer_ref = 0;
513		netmap_adapter_put(na);
514	} else {
515		ND("%p: case 2.b, grab peer", na);
516		netmap_adapter_get(na);
517		pna->peer->peer_ref = 1;
518	}
519	return error;
520}
521
522/* netmap_pipe_krings_delete.
523 *
524 * There are two cases:
525 *
526 * 1) state is
527 *
528 *                usr1 --> e1 --> e2
529 *
530 *    and we are e1 (e2 is not registered, so krings_delete cannot be
531 *    called on it);
532 *
533 * 2) state is
534 *
535 *                usr1 --> e1     e2 <-- usr2
536 *
537 *    and we are either e1 or e2.
538 *
539 * In the former case we have to also delete the krings of e2;
540 * in the latter case we do nothing.
541 */
542static void
543netmap_pipe_krings_delete(struct netmap_adapter *na)
544{
545	struct netmap_pipe_adapter *pna =
546		(struct netmap_pipe_adapter *)na;
547	struct netmap_adapter *sna, *ona; /* na of the other end */
548	enum txrx t;
549	int i;
550
551	if (!pna->peer_ref) {
552		ND("%p: case 2, kept alive by peer",  na);
553		return;
554	}
555	ona = &pna->peer->up;
556	/* case 1) above */
557	ND("%p: case 1, deleting everything", na);
558	/* To avoid double-frees we zero-out all the buffers in the kernel part
559	 * of each ring. The reason is this: If the user is behaving correctly,
560	 * all buffers are found in exactly one slot in the userspace part of
561	 * some ring.  If the user is not behaving correctly, we cannot release
562	 * buffers cleanly anyway. In the latter case, the allocator will
563	 * return to a clean state only when all its users will close.
564	 */
565	sna = na;
566cleanup:
567	for_rx_tx(t) {
568		for (i = 0; i < nma_get_nrings(sna, t); i++) {
569			struct netmap_kring *kring = NMR(sna, t)[i];
570			struct netmap_ring *ring = kring->ring;
571			uint32_t j, lim = kring->nkr_num_slots - 1;
572
573			ND("%s ring %p hwtail %u hwcur %u",
574				kring->name, ring, kring->nr_hwtail, kring->nr_hwcur);
575
576			if (ring == NULL)
577				continue;
578
579			if (kring->tx == NR_RX)
580				ring->slot[kring->pipe_tail].buf_idx = 0;
581
582			for (j = nm_next(kring->pipe_tail, lim);
583			     j != kring->nr_hwcur;
584			     j = nm_next(j, lim))
585			{
586				ND("%s[%d] %u", kring->name, j, ring->slot[j].buf_idx);
587				ring->slot[j].buf_idx = 0;
588			}
589			kring->nr_kflags &= ~(NKR_FAKERING | NKR_NEEDRING);
590		}
591
592	}
593	if (sna != ona && ona->tx_rings) {
594		sna = ona;
595		goto cleanup;
596	}
597
598	netmap_mem_rings_delete(na);
599	netmap_krings_delete(na); /* also zeroes tx_rings etc. */
600
601	if (ona->tx_rings == NULL) {
602		/* already deleted, we must be on an
603		 * cleanup-after-error path */
604		return;
605	}
606	netmap_mem_rings_delete(ona);
607	netmap_krings_delete(ona);
608}
609
610
611static void
612netmap_pipe_dtor(struct netmap_adapter *na)
613{
614	struct netmap_pipe_adapter *pna =
615		(struct netmap_pipe_adapter *)na;
616	ND("%p %p", na, pna->parent_ifp);
617	if (pna->peer_ref) {
618		ND("%p: clean up peer", na);
619		pna->peer_ref = 0;
620		netmap_adapter_put(&pna->peer->up);
621	}
622	if (pna->role == NM_PIPE_ROLE_MASTER)
623		netmap_pipe_remove(pna->parent, pna);
624	if (pna->parent_ifp)
625		if_rele(pna->parent_ifp);
626	netmap_adapter_put(pna->parent);
627	pna->parent = NULL;
628}
629
630int
631netmap_get_pipe_na(struct nmreq_header *hdr, struct netmap_adapter **na,
632		struct netmap_mem_d *nmd, int create)
633{
634	struct nmreq_register *req = (struct nmreq_register *)(uintptr_t)hdr->nr_body;
635	struct netmap_adapter *pna; /* parent adapter */
636	struct netmap_pipe_adapter *mna, *sna, *reqna;
637	struct ifnet *ifp = NULL;
638	const char *pipe_id = NULL;
639	int role = 0;
640	int error, retries = 0;
641	char *cbra;
642
643	/* Try to parse the pipe syntax 'xx{yy' or 'xx}yy'. */
644	cbra = strrchr(hdr->nr_name, '{');
645	if (cbra != NULL) {
646		role = NM_PIPE_ROLE_MASTER;
647	} else {
648		cbra = strrchr(hdr->nr_name, '}');
649		if (cbra != NULL) {
650			role = NM_PIPE_ROLE_SLAVE;
651		} else {
652			ND("not a pipe");
653			return 0;
654		}
655	}
656	pipe_id = cbra + 1;
657	if (*pipe_id == '\0' || cbra == hdr->nr_name) {
658		/* Bracket is the last character, so pipe name is missing;
659		 * or bracket is the first character, so base port name
660		 * is missing. */
661		return EINVAL;
662	}
663
664	if (req->nr_mode != NR_REG_ALL_NIC && req->nr_mode != NR_REG_ONE_NIC) {
665		/* We only accept modes involving hardware rings. */
666		return EINVAL;
667	}
668
669	/* first, try to find the parent adapter */
670	for (;;) {
671		char nr_name_orig[NETMAP_REQ_IFNAMSIZ];
672		int create_error;
673
674		/* Temporarily remove the pipe suffix. */
675		strlcpy(nr_name_orig, hdr->nr_name, sizeof(nr_name_orig));
676		*cbra = '\0';
677		error = netmap_get_na(hdr, &pna, &ifp, nmd, create);
678		/* Restore the pipe suffix. */
679		strlcpy(hdr->nr_name, nr_name_orig, sizeof(hdr->nr_name));
680		if (!error)
681			break;
682		if (error != ENXIO || retries++) {
683			ND("parent lookup failed: %d", error);
684			return error;
685		}
686		ND("try to create a persistent vale port");
687		/* create a persistent vale port and try again */
688		*cbra = '\0';
689		NMG_UNLOCK();
690		create_error = netmap_vi_create(hdr, 1 /* autodelete */);
691		NMG_LOCK();
692		strlcpy(hdr->nr_name, nr_name_orig, sizeof(hdr->nr_name));
693		if (create_error && create_error != EEXIST) {
694			if (create_error != EOPNOTSUPP) {
695				D("failed to create a persistent vale port: %d", create_error);
696			}
697			return error;
698		}
699	}
700
701	if (NETMAP_OWNED_BY_KERN(pna)) {
702		ND("parent busy");
703		error = EBUSY;
704		goto put_out;
705	}
706
707	/* next, lookup the pipe id in the parent list */
708	reqna = NULL;
709	mna = netmap_pipe_find(pna, pipe_id);
710	if (mna) {
711		if (mna->role == role) {
712			ND("found %s directly at %d", pipe_id, mna->parent_slot);
713			reqna = mna;
714		} else {
715			ND("found %s indirectly at %d", pipe_id, mna->parent_slot);
716			reqna = mna->peer;
717		}
718		/* the pipe we have found already holds a ref to the parent,
719		 * so we need to drop the one we got from netmap_get_na()
720		 */
721		netmap_unget_na(pna, ifp);
722		goto found;
723	}
724	ND("pipe %s not found, create %d", pipe_id, create);
725	if (!create) {
726		error = ENODEV;
727		goto put_out;
728	}
729	/* we create both master and slave.
730	 * The endpoint we were asked for holds a reference to
731	 * the other one.
732	 */
733	mna = nm_os_malloc(sizeof(*mna));
734	if (mna == NULL) {
735		error = ENOMEM;
736		goto put_out;
737	}
738	snprintf(mna->up.name, sizeof(mna->up.name), "%s{%s", pna->name, pipe_id);
739
740	mna->role = NM_PIPE_ROLE_MASTER;
741	mna->parent = pna;
742	mna->parent_ifp = ifp;
743
744	mna->up.nm_txsync = netmap_pipe_txsync;
745	mna->up.nm_rxsync = netmap_pipe_rxsync;
746	mna->up.nm_register = netmap_pipe_reg;
747	mna->up.nm_dtor = netmap_pipe_dtor;
748	mna->up.nm_krings_create = netmap_pipe_krings_create;
749	mna->up.nm_krings_delete = netmap_pipe_krings_delete;
750	mna->up.nm_mem = netmap_mem_get(pna->nm_mem);
751	mna->up.na_flags |= NAF_MEM_OWNER;
752	mna->up.na_lut = pna->na_lut;
753
754	mna->up.num_tx_rings = req->nr_tx_rings;
755	nm_bound_var(&mna->up.num_tx_rings, 1,
756			1, NM_PIPE_MAXRINGS, NULL);
757	mna->up.num_rx_rings = req->nr_rx_rings;
758	nm_bound_var(&mna->up.num_rx_rings, 1,
759			1, NM_PIPE_MAXRINGS, NULL);
760	mna->up.num_tx_desc = req->nr_tx_slots;
761	nm_bound_var(&mna->up.num_tx_desc, pna->num_tx_desc,
762			1, NM_PIPE_MAXSLOTS, NULL);
763	mna->up.num_rx_desc = req->nr_rx_slots;
764	nm_bound_var(&mna->up.num_rx_desc, pna->num_rx_desc,
765			1, NM_PIPE_MAXSLOTS, NULL);
766	error = netmap_attach_common(&mna->up);
767	if (error)
768		goto free_mna;
769	/* register the master with the parent */
770	error = netmap_pipe_add(pna, mna);
771	if (error)
772		goto free_mna;
773
774	/* create the slave */
775	sna = nm_os_malloc(sizeof(*mna));
776	if (sna == NULL) {
777		error = ENOMEM;
778		goto unregister_mna;
779	}
780	/* most fields are the same, copy from master and then fix */
781	*sna = *mna;
782	sna->up.nm_mem = netmap_mem_get(mna->up.nm_mem);
783	/* swap the number of tx/rx rings */
784	sna->up.num_tx_rings = mna->up.num_rx_rings;
785	sna->up.num_rx_rings = mna->up.num_tx_rings;
786	snprintf(sna->up.name, sizeof(sna->up.name), "%s}%s", pna->name, pipe_id);
787	sna->role = NM_PIPE_ROLE_SLAVE;
788	error = netmap_attach_common(&sna->up);
789	if (error)
790		goto free_sna;
791
792	/* join the two endpoints */
793	mna->peer = sna;
794	sna->peer = mna;
795
796	/* we already have a reference to the parent, but we
797	 * need another one for the other endpoint we created
798	 */
799	netmap_adapter_get(pna);
800	/* likewise for the ifp, if any */
801	if (ifp)
802		if_ref(ifp);
803
804	if (role == NM_PIPE_ROLE_MASTER) {
805		reqna = mna;
806		mna->peer_ref = 1;
807		netmap_adapter_get(&sna->up);
808	} else {
809		reqna = sna;
810		sna->peer_ref = 1;
811		netmap_adapter_get(&mna->up);
812	}
813	ND("created master %p and slave %p", mna, sna);
814found:
815
816	ND("pipe %s %s at %p", pipe_id,
817		(reqna->role == NM_PIPE_ROLE_MASTER ? "master" : "slave"), reqna);
818	*na = &reqna->up;
819	netmap_adapter_get(*na);
820
821	/* keep the reference to the parent.
822	 * It will be released by the req destructor
823	 */
824
825	return 0;
826
827free_sna:
828	nm_os_free(sna);
829unregister_mna:
830	netmap_pipe_remove(pna, mna);
831free_mna:
832	nm_os_free(mna);
833put_out:
834	netmap_unget_na(pna, ifp);
835	return error;
836}
837
838
839#endif /* WITH_PIPES */
840