netmap_pipe.c revision 341477
1331722Seadler/*
2341477Svmaffione * Copyright (C) 2014-2018 Giuseppe Lettieri
3341477Svmaffione * All rights reserved.
4261909Sluigi *
5261909Sluigi * Redistribution and use in source and binary forms, with or without
6261909Sluigi * modification, are permitted provided that the following conditions
7261909Sluigi * are met:
8261909Sluigi *   1. Redistributions of source code must retain the above copyright
9261909Sluigi *      notice, this list of conditions and the following disclaimer.
10261909Sluigi *   2. Redistributions in binary form must reproduce the above copyright
11261909Sluigi *      notice, this list of conditions and the following disclaimer in the
12261909Sluigi *      documentation and/or other materials provided with the distribution.
13261909Sluigi *
14261909Sluigi * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15261909Sluigi * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16261909Sluigi * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17261909Sluigi * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18261909Sluigi * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19261909Sluigi * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20261909Sluigi * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21261909Sluigi * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22261909Sluigi * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23261909Sluigi * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24261909Sluigi * SUCH DAMAGE.
25261909Sluigi */
26261909Sluigi
27261909Sluigi/* $FreeBSD: stable/11/sys/dev/netmap/netmap_pipe.c 341477 2018-12-04 17:40:56Z vmaffione $ */
28261909Sluigi
29261909Sluigi#if defined(__FreeBSD__)
30261909Sluigi#include <sys/cdefs.h> /* prerequisite */
31261909Sluigi
32261909Sluigi#include <sys/types.h>
33261909Sluigi#include <sys/errno.h>
34261909Sluigi#include <sys/param.h>	/* defines used in kernel.h */
35261909Sluigi#include <sys/kernel.h>	/* types used in module initialization */
36261909Sluigi#include <sys/malloc.h>
37261909Sluigi#include <sys/poll.h>
38261909Sluigi#include <sys/lock.h>
39261909Sluigi#include <sys/rwlock.h>
40261909Sluigi#include <sys/selinfo.h>
41261909Sluigi#include <sys/sysctl.h>
42261909Sluigi#include <sys/socket.h> /* sockaddrs */
43261909Sluigi#include <net/if.h>
44261909Sluigi#include <net/if_var.h>
45261909Sluigi#include <machine/bus.h>	/* bus_dmamap_* */
46261909Sluigi#include <sys/refcount.h>
47261909Sluigi
48261909Sluigi
49261909Sluigi#elif defined(linux)
50261909Sluigi
51261909Sluigi#include "bsd_glue.h"
52261909Sluigi
53261909Sluigi#elif defined(__APPLE__)
54261909Sluigi
55261909Sluigi#warning OSX support is only partial
56261909Sluigi#include "osx_glue.h"
57261909Sluigi
58341477Svmaffione#elif defined(_WIN32)
59341477Svmaffione#include "win_glue.h"
60341477Svmaffione
61261909Sluigi#else
62261909Sluigi
63261909Sluigi#error	Unsupported platform
64261909Sluigi
65261909Sluigi#endif /* unsupported */
66261909Sluigi
67261909Sluigi/*
68261909Sluigi * common headers
69261909Sluigi */
70261909Sluigi
71261909Sluigi#include <net/netmap.h>
72261909Sluigi#include <dev/netmap/netmap_kern.h>
73261909Sluigi#include <dev/netmap/netmap_mem2.h>
74261909Sluigi
75261909Sluigi#ifdef WITH_PIPES
76261909Sluigi
77261909Sluigi#define NM_PIPE_MAXSLOTS	4096
78341477Svmaffione#define NM_PIPE_MAXRINGS	256
79261909Sluigi
80341477Svmaffionestatic int netmap_default_pipes = 0; /* ignored, kept for compatibility */
81341477SvmaffioneSYSBEGIN(vars_pipes);
82261909SluigiSYSCTL_DECL(_dev_netmap);
83341477SvmaffioneSYSCTL_INT(_dev_netmap, OID_AUTO, default_pipes, CTLFLAG_RW,
84341477Svmaffione		&netmap_default_pipes, 0, "For compatibility only");
85341477SvmaffioneSYSEND;
86261909Sluigi
87261909Sluigi/* allocate the pipe array in the parent adapter */
88285349Sluigistatic int
89285349Sluiginm_pipe_alloc(struct netmap_adapter *na, u_int npipes)
90261909Sluigi{
91341477Svmaffione	size_t old_len, len;
92285349Sluigi	struct netmap_pipe_adapter **npa;
93261909Sluigi
94285349Sluigi	if (npipes <= na->na_max_pipes)
95285349Sluigi		/* we already have more entries that requested */
96261909Sluigi		return 0;
97341477Svmaffione
98285349Sluigi	if (npipes < na->na_next_pipe || npipes > NM_MAXPIPES)
99285349Sluigi		return EINVAL;
100261909Sluigi
101341477Svmaffione	old_len = sizeof(struct netmap_pipe_adapter *)*na->na_max_pipes;
102341477Svmaffione	len = sizeof(struct netmap_pipe_adapter *) * npipes;
103341477Svmaffione	npa = nm_os_realloc(na->na_pipes, len, old_len);
104285349Sluigi	if (npa == NULL)
105261909Sluigi		return ENOMEM;
106261909Sluigi
107285349Sluigi	na->na_pipes = npa;
108261909Sluigi	na->na_max_pipes = npipes;
109261909Sluigi
110261909Sluigi	return 0;
111261909Sluigi}
112261909Sluigi
113261909Sluigi/* deallocate the parent array in the parent adapter */
114261909Sluigivoid
115261909Sluiginetmap_pipe_dealloc(struct netmap_adapter *na)
116261909Sluigi{
117261909Sluigi	if (na->na_pipes) {
118285349Sluigi		if (na->na_next_pipe > 0) {
119285349Sluigi			D("freeing not empty pipe array for %s (%d dangling pipes)!", na->name,
120285349Sluigi					na->na_next_pipe);
121285349Sluigi		}
122341477Svmaffione		nm_os_free(na->na_pipes);
123261909Sluigi		na->na_pipes = NULL;
124261909Sluigi		na->na_max_pipes = 0;
125261909Sluigi		na->na_next_pipe = 0;
126261909Sluigi	}
127261909Sluigi}
128261909Sluigi
129261909Sluigi/* find a pipe endpoint with the given id among the parent's pipes */
130261909Sluigistatic struct netmap_pipe_adapter *
131341477Svmaffionenetmap_pipe_find(struct netmap_adapter *parent, const char *pipe_id)
132261909Sluigi{
133261909Sluigi	int i;
134261909Sluigi	struct netmap_pipe_adapter *na;
135261909Sluigi
136261909Sluigi	for (i = 0; i < parent->na_next_pipe; i++) {
137341477Svmaffione		const char *na_pipe_id;
138261909Sluigi		na = parent->na_pipes[i];
139341477Svmaffione		na_pipe_id = strrchr(na->up.name,
140341477Svmaffione			na->role == NM_PIPE_ROLE_MASTER ? '{' : '}');
141341477Svmaffione		KASSERT(na_pipe_id != NULL, ("Invalid pipe name"));
142341477Svmaffione		++na_pipe_id;
143341477Svmaffione		if (!strcmp(na_pipe_id, pipe_id)) {
144261909Sluigi			return na;
145261909Sluigi		}
146261909Sluigi	}
147261909Sluigi	return NULL;
148261909Sluigi}
149261909Sluigi
150261909Sluigi/* add a new pipe endpoint to the parent array */
151261909Sluigistatic int
152261909Sluiginetmap_pipe_add(struct netmap_adapter *parent, struct netmap_pipe_adapter *na)
153261909Sluigi{
154261909Sluigi	if (parent->na_next_pipe >= parent->na_max_pipes) {
155285349Sluigi		u_int npipes = parent->na_max_pipes ?  2*parent->na_max_pipes : 2;
156285349Sluigi		int error = nm_pipe_alloc(parent, npipes);
157285349Sluigi		if (error)
158285349Sluigi			return error;
159261909Sluigi	}
160261909Sluigi
161261909Sluigi	parent->na_pipes[parent->na_next_pipe] = na;
162261909Sluigi	na->parent_slot = parent->na_next_pipe;
163261909Sluigi	parent->na_next_pipe++;
164261909Sluigi	return 0;
165261909Sluigi}
166261909Sluigi
167261909Sluigi/* remove the given pipe endpoint from the parent array */
168261909Sluigistatic void
169261909Sluiginetmap_pipe_remove(struct netmap_adapter *parent, struct netmap_pipe_adapter *na)
170261909Sluigi{
171261909Sluigi	u_int n;
172261909Sluigi	n = --parent->na_next_pipe;
173261909Sluigi	if (n != na->parent_slot) {
174285349Sluigi		struct netmap_pipe_adapter **p =
175285349Sluigi			&parent->na_pipes[na->parent_slot];
176285349Sluigi		*p = parent->na_pipes[n];
177285349Sluigi		(*p)->parent_slot = na->parent_slot;
178261909Sluigi	}
179261909Sluigi	parent->na_pipes[n] = NULL;
180261909Sluigi}
181261909Sluigi
182341477Svmaffioneint
183270063Sluiginetmap_pipe_txsync(struct netmap_kring *txkring, int flags)
184261909Sluigi{
185341477Svmaffione	struct netmap_kring *rxkring = txkring->pipe;
186341477Svmaffione	u_int k, lim = txkring->nkr_num_slots - 1, nk;
187341477Svmaffione	int m; /* slots to transfer */
188341477Svmaffione	int complete; /* did we see a complete packet ? */
189341477Svmaffione	struct netmap_ring *txring = txkring->ring, *rxring = rxkring->ring;
190261909Sluigi
191341477Svmaffione	ND("%p: %s %x -> %s", txkring, txkring->name, flags, rxkring->name);
192341477Svmaffione	ND(20, "TX before: hwcur %d hwtail %d cur %d head %d tail %d",
193341477Svmaffione		txkring->nr_hwcur, txkring->nr_hwtail,
194341477Svmaffione		txkring->rcur, txkring->rhead, txkring->rtail);
195261909Sluigi
196341477Svmaffione	/* update the hwtail */
197341477Svmaffione	txkring->nr_hwtail = txkring->pipe_tail;
198261909Sluigi
199341477Svmaffione	m = txkring->rhead - txkring->nr_hwcur; /* new slots */
200341477Svmaffione	if (m < 0)
201341477Svmaffione		m += txkring->nkr_num_slots;
202341477Svmaffione
203341477Svmaffione	if (m == 0) {
204341477Svmaffione		/* nothing to send */
205261909Sluigi		return 0;
206261909Sluigi	}
207261909Sluigi
208341477Svmaffione	for (k = txkring->nr_hwcur, nk = lim + 1, complete = 0; m;
209341477Svmaffione			m--, k = nm_next(k, lim), nk = (complete ? k : nk)) {
210341477Svmaffione		struct netmap_slot *rs = &rxring->slot[k];
211341477Svmaffione		struct netmap_slot *ts = &txring->slot[k];
212261909Sluigi
213341477Svmaffione		*rs = *ts;
214341477Svmaffione		if (ts->flags & NS_BUF_CHANGED) {
215341477Svmaffione			ts->flags &= ~NS_BUF_CHANGED;
216341477Svmaffione		}
217341477Svmaffione		complete = !(ts->flags & NS_MOREFRAG);
218341477Svmaffione	}
219261909Sluigi
220341477Svmaffione	txkring->nr_hwcur = k;
221261909Sluigi
222341477Svmaffione	ND(20, "TX after : hwcur %d hwtail %d cur %d head %d tail %d k %d",
223341477Svmaffione		txkring->nr_hwcur, txkring->nr_hwtail,
224341477Svmaffione		txkring->rcur, txkring->rhead, txkring->rtail, k);
225261909Sluigi
226341477Svmaffione	if (likely(nk <= lim)) {
227341477Svmaffione		mb(); /* make sure the slots are updated before publishing them */
228341477Svmaffione		rxkring->pipe_tail = nk; /* only publish complete packets */
229341477Svmaffione		rxkring->nm_notify(rxkring, 0);
230341477Svmaffione	}
231261909Sluigi
232261909Sluigi	return 0;
233261909Sluigi}
234261909Sluigi
235341477Svmaffioneint
236270063Sluiginetmap_pipe_rxsync(struct netmap_kring *rxkring, int flags)
237261909Sluigi{
238341477Svmaffione	struct netmap_kring *txkring = rxkring->pipe;
239341477Svmaffione	u_int k, lim = rxkring->nkr_num_slots - 1;
240341477Svmaffione	int m; /* slots to release */
241341477Svmaffione	struct netmap_ring *txring = txkring->ring, *rxring = rxkring->ring;
242261909Sluigi
243341477Svmaffione	ND("%p: %s %x -> %s", txkring, txkring->name, flags, rxkring->name);
244341477Svmaffione	ND(20, "RX before: hwcur %d hwtail %d cur %d head %d tail %d",
245341477Svmaffione		rxkring->nr_hwcur, rxkring->nr_hwtail,
246341477Svmaffione		rxkring->rcur, rxkring->rhead, rxkring->rtail);
247261909Sluigi
248341477Svmaffione	/* update the hwtail */
249341477Svmaffione	rxkring->nr_hwtail = rxkring->pipe_tail;
250341477Svmaffione
251341477Svmaffione	m = rxkring->rhead - rxkring->nr_hwcur; /* released slots */
252341477Svmaffione	if (m < 0)
253341477Svmaffione		m += rxkring->nkr_num_slots;
254341477Svmaffione
255341477Svmaffione	if (m == 0) {
256341477Svmaffione		/* nothing to release */
257341477Svmaffione		return 0;
258261909Sluigi	}
259341477Svmaffione
260341477Svmaffione	for (k = rxkring->nr_hwcur; m; m--, k = nm_next(k, lim)) {
261341477Svmaffione		struct netmap_slot *rs = &rxring->slot[k];
262341477Svmaffione		struct netmap_slot *ts = &txring->slot[k];
263341477Svmaffione
264341477Svmaffione		if (rs->flags & NS_BUF_CHANGED) {
265341477Svmaffione			/* copy the slot and report the buffer change */
266341477Svmaffione			*ts = *rs;
267341477Svmaffione			rs->flags &= ~NS_BUF_CHANGED;
268341477Svmaffione		}
269341477Svmaffione	}
270341477Svmaffione
271341477Svmaffione	mb(); /* make sure the slots are updated before publishing them */
272341477Svmaffione	txkring->pipe_tail = nm_prev(k, lim);
273341477Svmaffione	rxkring->nr_hwcur = k;
274341477Svmaffione
275341477Svmaffione	ND(20, "RX after : hwcur %d hwtail %d cur %d head %d tail %d k %d",
276341477Svmaffione		rxkring->nr_hwcur, rxkring->nr_hwtail,
277341477Svmaffione		rxkring->rcur, rxkring->rhead, rxkring->rtail, k);
278341477Svmaffione
279341477Svmaffione	txkring->nm_notify(txkring, 0);
280341477Svmaffione
281341477Svmaffione	return 0;
282261909Sluigi}
283261909Sluigi
284261909Sluigi/* Pipe endpoints are created and destroyed together, so that endopoints do not
285261909Sluigi * have to check for the existence of their peer at each ?xsync.
286261909Sluigi *
287261909Sluigi * To play well with the existing netmap infrastructure (refcounts etc.), we
288261909Sluigi * adopt the following strategy:
289261909Sluigi *
290261909Sluigi * 1) The first endpoint that is created also creates the other endpoint and
291261909Sluigi * grabs a reference to it.
292261909Sluigi *
293261909Sluigi *    state A)  user1 --> endpoint1 --> endpoint2
294261909Sluigi *
295261909Sluigi * 2) If, starting from state A, endpoint2 is then registered, endpoint1 gives
296261909Sluigi * its reference to the user:
297261909Sluigi *
298261909Sluigi *    state B)  user1 --> endpoint1     endpoint2 <--- user2
299261909Sluigi *
300261909Sluigi * 3) Assume that, starting from state B endpoint2 is closed. In the unregister
301261909Sluigi * callback endpoint2 notes that endpoint1 is still active and adds a reference
302261909Sluigi * from endpoint1 to itself. When user2 then releases her own reference,
303261909Sluigi * endpoint2 is not destroyed and we are back to state A. A symmetrical state
304261909Sluigi * would be reached if endpoint1 were released instead.
305261909Sluigi *
306261909Sluigi * 4) If, starting from state A, endpoint1 is closed, the destructor notes that
307261909Sluigi * it owns a reference to endpoint2 and releases it.
308261909Sluigi *
309261909Sluigi * Something similar goes on for the creation and destruction of the krings.
310261909Sluigi */
311261909Sluigi
312261909Sluigi
313341477Svmaffione/* netmap_pipe_krings_create.
314261909Sluigi *
315261909Sluigi * There are two cases:
316261909Sluigi *
317261909Sluigi * 1) state is
318261909Sluigi *
319261909Sluigi *        usr1 --> e1 --> e2
320261909Sluigi *
321261909Sluigi *    and we are e1. We have to create both sets
322261909Sluigi *    of krings.
323261909Sluigi *
324261909Sluigi * 2) state is
325261909Sluigi *
326261909Sluigi *        usr1 --> e1 --> e2
327261909Sluigi *
328261909Sluigi *    and we are e2. e1 is certainly registered and our
329341477Svmaffione *    krings already exist. Nothing to do.
330261909Sluigi */
331261909Sluigistatic int
332261909Sluiginetmap_pipe_krings_create(struct netmap_adapter *na)
333261909Sluigi{
334261909Sluigi	struct netmap_pipe_adapter *pna =
335261909Sluigi		(struct netmap_pipe_adapter *)na;
336261909Sluigi	struct netmap_adapter *ona = &pna->peer->up;
337261909Sluigi	int error = 0;
338285349Sluigi	enum txrx t;
339285349Sluigi
340261909Sluigi	if (pna->peer_ref) {
341261909Sluigi		int i;
342261909Sluigi
343261909Sluigi		/* case 1) above */
344341477Svmaffione		ND("%p: case 1, create both ends", na);
345261909Sluigi		error = netmap_krings_create(na, 0);
346261909Sluigi		if (error)
347261909Sluigi			goto err;
348261909Sluigi
349341477Svmaffione		/* create the krings of the other end */
350341477Svmaffione		error = netmap_krings_create(ona, 0);
351261909Sluigi		if (error)
352261909Sluigi			goto del_krings1;
353261909Sluigi
354341477Svmaffione		/* cross link the krings and initialize the pipe_tails */
355285349Sluigi		for_rx_tx(t) {
356341477Svmaffione			enum txrx r = nm_txrx_swap(t); /* swap NR_TX <-> NR_RX */
357285349Sluigi			for (i = 0; i < nma_get_nrings(na, t); i++) {
358341477Svmaffione				struct netmap_kring *k1 = NMR(na, t)[i],
359341477Svmaffione					            *k2 = NMR(ona, r)[i];
360341477Svmaffione				k1->pipe = k2;
361341477Svmaffione				k2->pipe = k1;
362341477Svmaffione				/* mark all peer-adapter rings as fake */
363341477Svmaffione				k2->nr_kflags |= NKR_FAKERING;
364341477Svmaffione				/* init tails */
365341477Svmaffione				k1->pipe_tail = k1->nr_hwtail;
366341477Svmaffione				k2->pipe_tail = k2->nr_hwtail;
367285349Sluigi			}
368261909Sluigi		}
369341477Svmaffione
370261909Sluigi	}
371261909Sluigi	return 0;
372261909Sluigi
373261909Sluigidel_krings1:
374261909Sluigi	netmap_krings_delete(na);
375261909Sluigierr:
376261909Sluigi	return error;
377261909Sluigi}
378261909Sluigi
379261909Sluigi/* netmap_pipe_reg.
380261909Sluigi *
381261909Sluigi * There are two cases on registration (onoff==1)
382267128Sluigi *
383261909Sluigi * 1.a) state is
384261909Sluigi *
385261909Sluigi *        usr1 --> e1 --> e2
386261909Sluigi *
387341477Svmaffione *      and we are e1. Create the needed rings of the
388341477Svmaffione *      other end.
389261909Sluigi *
390261909Sluigi * 1.b) state is
391261909Sluigi *
392261909Sluigi *        usr1 --> e1 --> e2 <-- usr2
393261909Sluigi *
394261909Sluigi *      and we are e2. Drop the ref e1 is holding.
395267128Sluigi *
396261909Sluigi *  There are two additional cases on unregister (onoff==0)
397261909Sluigi *
398261909Sluigi *  2.a) state is
399261909Sluigi *
400261909Sluigi *         usr1 --> e1 --> e2
401261909Sluigi *
402261909Sluigi *       and we are e1. Nothing special to do, e2 will
403261909Sluigi *       be cleaned up by the destructor of e1.
404261909Sluigi *
405261909Sluigi *  2.b) state is
406261909Sluigi *
407261909Sluigi *         usr1 --> e1     e2 <-- usr2
408261909Sluigi *
409261909Sluigi *       and we are either e1 or e2. Add a ref from the
410341477Svmaffione *       other end.
411261909Sluigi */
412261909Sluigistatic int
413261909Sluiginetmap_pipe_reg(struct netmap_adapter *na, int onoff)
414261909Sluigi{
415261909Sluigi	struct netmap_pipe_adapter *pna =
416261909Sluigi		(struct netmap_pipe_adapter *)na;
417341477Svmaffione	struct netmap_adapter *ona = &pna->peer->up;
418341477Svmaffione	int i, error = 0;
419285349Sluigi	enum txrx t;
420285349Sluigi
421261909Sluigi	ND("%p: onoff %d", na, onoff);
422261909Sluigi	if (onoff) {
423341477Svmaffione		for_rx_tx(t) {
424341477Svmaffione			for (i = 0; i < nma_get_nrings(na, t); i++) {
425341477Svmaffione				struct netmap_kring *kring = NMR(na, t)[i];
426341477Svmaffione
427341477Svmaffione				if (nm_kring_pending_on(kring)) {
428341477Svmaffione					/* mark the peer ring as needed */
429341477Svmaffione					kring->pipe->nr_kflags |= NKR_NEEDRING;
430341477Svmaffione				}
431341477Svmaffione			}
432341477Svmaffione		}
433341477Svmaffione
434341477Svmaffione		/* create all missing needed rings on the other end.
435341477Svmaffione		 * Either our end, or the other, has been marked as
436341477Svmaffione		 * fake, so the allocation will not be done twice.
437341477Svmaffione		 */
438341477Svmaffione		error = netmap_mem_rings_create(ona);
439341477Svmaffione		if (error)
440341477Svmaffione			return error;
441341477Svmaffione
442341477Svmaffione		/* In case of no error we put our rings in netmap mode */
443341477Svmaffione		for_rx_tx(t) {
444341477Svmaffione			for (i = 0; i < nma_get_nrings(na, t) + 1; i++) {
445341477Svmaffione				struct netmap_kring *kring = NMR(na, t)[i];
446341477Svmaffione				if (nm_kring_pending_on(kring)) {
447341477Svmaffione					struct netmap_kring *sring, *dring;
448341477Svmaffione
449341477Svmaffione					kring->nr_mode = NKR_NETMAP_ON;
450341477Svmaffione					if ((kring->nr_kflags & NKR_FAKERING) &&
451341477Svmaffione					    (kring->pipe->nr_kflags & NKR_FAKERING)) {
452341477Svmaffione						/* this is a re-open of a pipe
453341477Svmaffione						 * end-point kept alive by the other end.
454341477Svmaffione						 * We need to leave everything as it is
455341477Svmaffione						 */
456341477Svmaffione						continue;
457341477Svmaffione					}
458341477Svmaffione
459341477Svmaffione					/* copy the buffers from the non-fake ring */
460341477Svmaffione					if (kring->nr_kflags & NKR_FAKERING) {
461341477Svmaffione						sring = kring->pipe;
462341477Svmaffione						dring = kring;
463341477Svmaffione					} else {
464341477Svmaffione						sring = kring;
465341477Svmaffione						dring = kring->pipe;
466341477Svmaffione					}
467341477Svmaffione					memcpy(dring->ring->slot,
468341477Svmaffione					       sring->ring->slot,
469341477Svmaffione					       sizeof(struct netmap_slot) *
470341477Svmaffione							sring->nkr_num_slots);
471341477Svmaffione					/* mark both rings as fake and needed,
472341477Svmaffione					 * so that buffers will not be
473341477Svmaffione					 * deleted by the standard machinery
474341477Svmaffione					 * (we will delete them by ourselves in
475341477Svmaffione					 * netmap_pipe_krings_delete)
476341477Svmaffione					 */
477341477Svmaffione					sring->nr_kflags |=
478341477Svmaffione						(NKR_FAKERING | NKR_NEEDRING);
479341477Svmaffione					dring->nr_kflags |=
480341477Svmaffione						(NKR_FAKERING | NKR_NEEDRING);
481341477Svmaffione					kring->nr_mode = NKR_NETMAP_ON;
482341477Svmaffione				}
483341477Svmaffione			}
484341477Svmaffione		}
485341477Svmaffione		if (na->active_fds == 0)
486341477Svmaffione			na->na_flags |= NAF_NETMAP_ON;
487261909Sluigi	} else {
488341477Svmaffione		if (na->active_fds == 0)
489341477Svmaffione			na->na_flags &= ~NAF_NETMAP_ON;
490341477Svmaffione		for_rx_tx(t) {
491341477Svmaffione			for (i = 0; i < nma_get_nrings(na, t) + 1; i++) {
492341477Svmaffione				struct netmap_kring *kring = NMR(na, t)[i];
493341477Svmaffione
494341477Svmaffione				if (nm_kring_pending_off(kring)) {
495341477Svmaffione					kring->nr_mode = NKR_NETMAP_OFF;
496341477Svmaffione				}
497341477Svmaffione			}
498341477Svmaffione		}
499261909Sluigi	}
500341477Svmaffione
501341477Svmaffione	if (na->active_fds) {
502341477Svmaffione		ND("active_fds %d", na->active_fds);
503341477Svmaffione		return 0;
504341477Svmaffione	}
505341477Svmaffione
506261909Sluigi	if (pna->peer_ref) {
507261909Sluigi		ND("%p: case 1.a or 2.a, nothing to do", na);
508261909Sluigi		return 0;
509261909Sluigi	}
510261909Sluigi	if (onoff) {
511261909Sluigi		ND("%p: case 1.b, drop peer", na);
512261909Sluigi		pna->peer->peer_ref = 0;
513261909Sluigi		netmap_adapter_put(na);
514261909Sluigi	} else {
515261909Sluigi		ND("%p: case 2.b, grab peer", na);
516261909Sluigi		netmap_adapter_get(na);
517261909Sluigi		pna->peer->peer_ref = 1;
518261909Sluigi	}
519341477Svmaffione	return error;
520261909Sluigi}
521261909Sluigi
522261909Sluigi/* netmap_pipe_krings_delete.
523261909Sluigi *
524261909Sluigi * There are two cases:
525261909Sluigi *
526261909Sluigi * 1) state is
527261909Sluigi *
528267128Sluigi *                usr1 --> e1 --> e2
529261909Sluigi *
530267128Sluigi *    and we are e1 (e2 is not registered, so krings_delete cannot be
531261909Sluigi *    called on it);
532261909Sluigi *
533261909Sluigi * 2) state is
534261909Sluigi *
535267128Sluigi *                usr1 --> e1     e2 <-- usr2
536261909Sluigi *
537261909Sluigi *    and we are either e1 or e2.
538261909Sluigi *
539261909Sluigi * In the former case we have to also delete the krings of e2;
540341477Svmaffione * in the latter case we do nothing.
541261909Sluigi */
542261909Sluigistatic void
543261909Sluiginetmap_pipe_krings_delete(struct netmap_adapter *na)
544261909Sluigi{
545261909Sluigi	struct netmap_pipe_adapter *pna =
546261909Sluigi		(struct netmap_pipe_adapter *)na;
547341477Svmaffione	struct netmap_adapter *sna, *ona; /* na of the other end */
548341477Svmaffione	enum txrx t;
549261909Sluigi	int i;
550261909Sluigi
551261909Sluigi	if (!pna->peer_ref) {
552261909Sluigi		ND("%p: case 2, kept alive by peer",  na);
553261909Sluigi		return;
554261909Sluigi	}
555341477Svmaffione	ona = &pna->peer->up;
556261909Sluigi	/* case 1) above */
557341477Svmaffione	ND("%p: case 1, deleting everything", na);
558341477Svmaffione	/* To avoid double-frees we zero-out all the buffers in the kernel part
559341477Svmaffione	 * of each ring. The reason is this: If the user is behaving correctly,
560341477Svmaffione	 * all buffers are found in exactly one slot in the userspace part of
561341477Svmaffione	 * some ring.  If the user is not behaving correctly, we cannot release
562341477Svmaffione	 * buffers cleanly anyway. In the latter case, the allocator will
563341477Svmaffione	 * return to a clean state only when all its users will close.
564341477Svmaffione	 */
565341477Svmaffione	sna = na;
566341477Svmaffionecleanup:
567341477Svmaffione	for_rx_tx(t) {
568341477Svmaffione		for (i = 0; i < nma_get_nrings(sna, t) + 1; i++) {
569341477Svmaffione			struct netmap_kring *kring = NMR(sna, t)[i];
570341477Svmaffione			struct netmap_ring *ring = kring->ring;
571341477Svmaffione			uint32_t j, lim = kring->nkr_num_slots - 1;
572341477Svmaffione
573341477Svmaffione			ND("%s ring %p hwtail %u hwcur %u",
574341477Svmaffione				kring->name, ring, kring->nr_hwtail, kring->nr_hwcur);
575341477Svmaffione
576341477Svmaffione			if (ring == NULL)
577341477Svmaffione				continue;
578341477Svmaffione
579341477Svmaffione			if (kring->tx == NR_RX)
580341477Svmaffione				ring->slot[kring->pipe_tail].buf_idx = 0;
581341477Svmaffione
582341477Svmaffione			for (j = nm_next(kring->pipe_tail, lim);
583341477Svmaffione			     j != kring->nr_hwcur;
584341477Svmaffione			     j = nm_next(j, lim))
585341477Svmaffione			{
586341477Svmaffione				ND("%s[%d] %u", kring->name, j, ring->slot[j].buf_idx);
587341477Svmaffione				ring->slot[j].buf_idx = 0;
588341477Svmaffione			}
589341477Svmaffione			kring->nr_kflags &= ~(NKR_FAKERING | NKR_NEEDRING);
590341477Svmaffione		}
591341477Svmaffione
592341477Svmaffione	}
593341477Svmaffione	if (sna != ona && ona->tx_rings) {
594341477Svmaffione		sna = ona;
595341477Svmaffione		goto cleanup;
596341477Svmaffione	}
597341477Svmaffione
598341477Svmaffione	netmap_mem_rings_delete(na);
599261909Sluigi	netmap_krings_delete(na); /* also zeroes tx_rings etc. */
600341477Svmaffione
601261909Sluigi	if (ona->tx_rings == NULL) {
602261909Sluigi		/* already deleted, we must be on an
603341477Svmaffione		 * cleanup-after-error path */
604261909Sluigi		return;
605261909Sluigi	}
606261909Sluigi	netmap_mem_rings_delete(ona);
607261909Sluigi	netmap_krings_delete(ona);
608261909Sluigi}
609261909Sluigi
610261909Sluigi
611261909Sluigistatic void
612261909Sluiginetmap_pipe_dtor(struct netmap_adapter *na)
613261909Sluigi{
614261909Sluigi	struct netmap_pipe_adapter *pna =
615261909Sluigi		(struct netmap_pipe_adapter *)na;
616341477Svmaffione	ND("%p %p", na, pna->parent_ifp);
617261909Sluigi	if (pna->peer_ref) {
618261909Sluigi		ND("%p: clean up peer", na);
619261909Sluigi		pna->peer_ref = 0;
620261909Sluigi		netmap_adapter_put(&pna->peer->up);
621261909Sluigi	}
622341477Svmaffione	if (pna->role == NM_PIPE_ROLE_MASTER)
623261909Sluigi		netmap_pipe_remove(pna->parent, pna);
624341477Svmaffione	if (pna->parent_ifp)
625341477Svmaffione		if_rele(pna->parent_ifp);
626261909Sluigi	netmap_adapter_put(pna->parent);
627261909Sluigi	pna->parent = NULL;
628261909Sluigi}
629261909Sluigi
630261909Sluigiint
631341477Svmaffionenetmap_get_pipe_na(struct nmreq_header *hdr, struct netmap_adapter **na,
632341477Svmaffione		struct netmap_mem_d *nmd, int create)
633261909Sluigi{
634341477Svmaffione	struct nmreq_register *req = (struct nmreq_register *)(uintptr_t)hdr->nr_body;
635261909Sluigi	struct netmap_adapter *pna; /* parent adapter */
636341477Svmaffione	struct netmap_pipe_adapter *mna, *sna, *reqna;
637341477Svmaffione	struct ifnet *ifp = NULL;
638341477Svmaffione	const char *pipe_id = NULL;
639341477Svmaffione	int role = 0;
640341477Svmaffione	int error, retries = 0;
641341477Svmaffione	char *cbra;
642261909Sluigi
643341477Svmaffione	/* Try to parse the pipe syntax 'xx{yy' or 'xx}yy'. */
644341477Svmaffione	cbra = strrchr(hdr->nr_name, '{');
645341477Svmaffione	if (cbra != NULL) {
646341477Svmaffione		role = NM_PIPE_ROLE_MASTER;
647341477Svmaffione	} else {
648341477Svmaffione		cbra = strrchr(hdr->nr_name, '}');
649341477Svmaffione		if (cbra != NULL) {
650341477Svmaffione			role = NM_PIPE_ROLE_SLAVE;
651341477Svmaffione		} else {
652341477Svmaffione			ND("not a pipe");
653341477Svmaffione			return 0;
654341477Svmaffione		}
655341477Svmaffione	}
656341477Svmaffione	pipe_id = cbra + 1;
657341477Svmaffione	if (*pipe_id == '\0' || cbra == hdr->nr_name) {
658341477Svmaffione		/* Bracket is the last character, so pipe name is missing;
659341477Svmaffione		 * or bracket is the first character, so base port name
660341477Svmaffione		 * is missing. */
661341477Svmaffione		return EINVAL;
662341477Svmaffione	}
663261909Sluigi
664341477Svmaffione	if (req->nr_mode != NR_REG_ALL_NIC && req->nr_mode != NR_REG_ONE_NIC) {
665341477Svmaffione		/* We only accept modes involving hardware rings. */
666341477Svmaffione		return EINVAL;
667261909Sluigi	}
668261909Sluigi
669261909Sluigi	/* first, try to find the parent adapter */
670341477Svmaffione	for (;;) {
671341477Svmaffione		char nr_name_orig[NETMAP_REQ_IFNAMSIZ];
672341477Svmaffione		int create_error;
673341477Svmaffione
674341477Svmaffione		/* Temporarily remove the pipe suffix. */
675341477Svmaffione		strncpy(nr_name_orig, hdr->nr_name, sizeof(nr_name_orig));
676341477Svmaffione		*cbra = '\0';
677341477Svmaffione		error = netmap_get_na(hdr, &pna, &ifp, nmd, create);
678341477Svmaffione		/* Restore the pipe suffix. */
679341477Svmaffione		strncpy(hdr->nr_name, nr_name_orig, sizeof(hdr->nr_name));
680341477Svmaffione		if (!error)
681341477Svmaffione			break;
682341477Svmaffione		if (error != ENXIO || retries++) {
683341477Svmaffione			ND("parent lookup failed: %d", error);
684341477Svmaffione			return error;
685341477Svmaffione		}
686341477Svmaffione		ND("try to create a persistent vale port");
687341477Svmaffione		/* create a persistent vale port and try again */
688341477Svmaffione		*cbra = '\0';
689341477Svmaffione		NMG_UNLOCK();
690341477Svmaffione		create_error = netmap_vi_create(hdr, 1 /* autodelete */);
691341477Svmaffione		NMG_LOCK();
692341477Svmaffione		strncpy(hdr->nr_name, nr_name_orig, sizeof(hdr->nr_name));
693341477Svmaffione		if (create_error && create_error != EEXIST) {
694341477Svmaffione			if (create_error != EOPNOTSUPP) {
695341477Svmaffione				D("failed to create a persistent vale port: %d", create_error);
696341477Svmaffione			}
697341477Svmaffione			return error;
698341477Svmaffione		}
699261909Sluigi	}
700261909Sluigi
701261909Sluigi	if (NETMAP_OWNED_BY_KERN(pna)) {
702261909Sluigi		ND("parent busy");
703261909Sluigi		error = EBUSY;
704261909Sluigi		goto put_out;
705261909Sluigi	}
706261909Sluigi
707261909Sluigi	/* next, lookup the pipe id in the parent list */
708341477Svmaffione	reqna = NULL;
709261909Sluigi	mna = netmap_pipe_find(pna, pipe_id);
710261909Sluigi	if (mna) {
711261909Sluigi		if (mna->role == role) {
712341477Svmaffione			ND("found %s directly at %d", pipe_id, mna->parent_slot);
713341477Svmaffione			reqna = mna;
714261909Sluigi		} else {
715341477Svmaffione			ND("found %s indirectly at %d", pipe_id, mna->parent_slot);
716341477Svmaffione			reqna = mna->peer;
717261909Sluigi		}
718261909Sluigi		/* the pipe we have found already holds a ref to the parent,
719341477Svmaffione		 * so we need to drop the one we got from netmap_get_na()
720341477Svmaffione		 */
721341477Svmaffione		netmap_unget_na(pna, ifp);
722261909Sluigi		goto found;
723261909Sluigi	}
724341477Svmaffione	ND("pipe %s not found, create %d", pipe_id, create);
725261909Sluigi	if (!create) {
726261909Sluigi		error = ENODEV;
727261909Sluigi		goto put_out;
728261909Sluigi	}
729267128Sluigi	/* we create both master and slave.
730341477Svmaffione	 * The endpoint we were asked for holds a reference to
731341477Svmaffione	 * the other one.
732341477Svmaffione	 */
733341477Svmaffione	mna = nm_os_malloc(sizeof(*mna));
734261909Sluigi	if (mna == NULL) {
735261909Sluigi		error = ENOMEM;
736270063Sluigi		goto put_out;
737261909Sluigi	}
738341477Svmaffione	snprintf(mna->up.name, sizeof(mna->up.name), "%s{%s", pna->name, pipe_id);
739261909Sluigi
740341477Svmaffione	mna->role = NM_PIPE_ROLE_MASTER;
741261909Sluigi	mna->parent = pna;
742341477Svmaffione	mna->parent_ifp = ifp;
743261909Sluigi
744261909Sluigi	mna->up.nm_txsync = netmap_pipe_txsync;
745261909Sluigi	mna->up.nm_rxsync = netmap_pipe_rxsync;
746261909Sluigi	mna->up.nm_register = netmap_pipe_reg;
747261909Sluigi	mna->up.nm_dtor = netmap_pipe_dtor;
748261909Sluigi	mna->up.nm_krings_create = netmap_pipe_krings_create;
749261909Sluigi	mna->up.nm_krings_delete = netmap_pipe_krings_delete;
750341477Svmaffione	mna->up.nm_mem = netmap_mem_get(pna->nm_mem);
751341477Svmaffione	mna->up.na_flags |= NAF_MEM_OWNER;
752261909Sluigi	mna->up.na_lut = pna->na_lut;
753261909Sluigi
754341477Svmaffione	mna->up.num_tx_rings = req->nr_tx_rings;
755341477Svmaffione	nm_bound_var(&mna->up.num_tx_rings, 1,
756341477Svmaffione			1, NM_PIPE_MAXRINGS, NULL);
757341477Svmaffione	mna->up.num_rx_rings = req->nr_rx_rings;
758341477Svmaffione	nm_bound_var(&mna->up.num_rx_rings, 1,
759341477Svmaffione			1, NM_PIPE_MAXRINGS, NULL);
760341477Svmaffione	mna->up.num_tx_desc = req->nr_tx_slots;
761261909Sluigi	nm_bound_var(&mna->up.num_tx_desc, pna->num_tx_desc,
762261909Sluigi			1, NM_PIPE_MAXSLOTS, NULL);
763341477Svmaffione	mna->up.num_rx_desc = req->nr_rx_slots;
764261909Sluigi	nm_bound_var(&mna->up.num_rx_desc, pna->num_rx_desc,
765261909Sluigi			1, NM_PIPE_MAXSLOTS, NULL);
766261909Sluigi	error = netmap_attach_common(&mna->up);
767261909Sluigi	if (error)
768270063Sluigi		goto free_mna;
769261909Sluigi	/* register the master with the parent */
770261909Sluigi	error = netmap_pipe_add(pna, mna);
771261909Sluigi	if (error)
772261909Sluigi		goto free_mna;
773261909Sluigi
774261909Sluigi	/* create the slave */
775341477Svmaffione	sna = nm_os_malloc(sizeof(*mna));
776261909Sluigi	if (sna == NULL) {
777261909Sluigi		error = ENOMEM;
778285697Sluigi		goto unregister_mna;
779261909Sluigi	}
780261909Sluigi	/* most fields are the same, copy from master and then fix */
781261909Sluigi	*sna = *mna;
782341477Svmaffione	sna->up.nm_mem = netmap_mem_get(mna->up.nm_mem);
783341477Svmaffione	/* swap the number of tx/rx rings */
784341477Svmaffione	sna->up.num_tx_rings = mna->up.num_rx_rings;
785341477Svmaffione	sna->up.num_rx_rings = mna->up.num_tx_rings;
786341477Svmaffione	snprintf(sna->up.name, sizeof(sna->up.name), "%s}%s", pna->name, pipe_id);
787341477Svmaffione	sna->role = NM_PIPE_ROLE_SLAVE;
788261909Sluigi	error = netmap_attach_common(&sna->up);
789261909Sluigi	if (error)
790261909Sluigi		goto free_sna;
791261909Sluigi
792261909Sluigi	/* join the two endpoints */
793261909Sluigi	mna->peer = sna;
794261909Sluigi	sna->peer = mna;
795261909Sluigi
796261909Sluigi	/* we already have a reference to the parent, but we
797341477Svmaffione	 * need another one for the other endpoint we created
798341477Svmaffione	 */
799261909Sluigi	netmap_adapter_get(pna);
800341477Svmaffione	/* likewise for the ifp, if any */
801341477Svmaffione	if (ifp)
802341477Svmaffione		if_ref(ifp);
803261909Sluigi
804341477Svmaffione	if (role == NM_PIPE_ROLE_MASTER) {
805341477Svmaffione		reqna = mna;
806261909Sluigi		mna->peer_ref = 1;
807261909Sluigi		netmap_adapter_get(&sna->up);
808261909Sluigi	} else {
809341477Svmaffione		reqna = sna;
810261909Sluigi		sna->peer_ref = 1;
811261909Sluigi		netmap_adapter_get(&mna->up);
812261909Sluigi	}
813261909Sluigi	ND("created master %p and slave %p", mna, sna);
814261909Sluigifound:
815261909Sluigi
816341477Svmaffione	ND("pipe %s %s at %p", pipe_id,
817341477Svmaffione		(reqna->role == NM_PIPE_ROLE_MASTER ? "master" : "slave"), reqna);
818341477Svmaffione	*na = &reqna->up;
819261909Sluigi	netmap_adapter_get(*na);
820261909Sluigi
821261909Sluigi	/* keep the reference to the parent.
822341477Svmaffione	 * It will be released by the req destructor
823341477Svmaffione	 */
824261909Sluigi
825261909Sluigi	return 0;
826261909Sluigi
827261909Sluigifree_sna:
828341477Svmaffione	nm_os_free(sna);
829285697Sluigiunregister_mna:
830285697Sluigi	netmap_pipe_remove(pna, mna);
831261909Sluigifree_mna:
832341477Svmaffione	nm_os_free(mna);
833261909Sluigiput_out:
834341477Svmaffione	netmap_unget_na(pna, ifp);
835261909Sluigi	return error;
836261909Sluigi}
837261909Sluigi
838261909Sluigi
839261909Sluigi#endif /* WITH_PIPES */
840