xref: /freebsd-src/sys/dev/netmap/netmap_vale.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
1718cf2ccSPedro F. Giffuni /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
437e3a6d3SLuigi Rizzo  * Copyright (C) 2013-2016 Universita` di Pisa
537e3a6d3SLuigi Rizzo  * All rights reserved.
6f9790aebSLuigi Rizzo  *
7f9790aebSLuigi Rizzo  * Redistribution and use in source and binary forms, with or without
8f9790aebSLuigi Rizzo  * modification, are permitted provided that the following conditions
9f9790aebSLuigi Rizzo  * are met:
10f9790aebSLuigi Rizzo  *   1. Redistributions of source code must retain the above copyright
11f9790aebSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer.
12f9790aebSLuigi Rizzo  *   2. Redistributions in binary form must reproduce the above copyright
13f9790aebSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer in the
14f9790aebSLuigi Rizzo  *      documentation and/or other materials provided with the distribution.
15f9790aebSLuigi Rizzo  *
16f9790aebSLuigi Rizzo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17f9790aebSLuigi Rizzo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18f9790aebSLuigi Rizzo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19f9790aebSLuigi Rizzo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20f9790aebSLuigi Rizzo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21f9790aebSLuigi Rizzo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22f9790aebSLuigi Rizzo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23f9790aebSLuigi Rizzo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24f9790aebSLuigi Rizzo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25f9790aebSLuigi Rizzo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26f9790aebSLuigi Rizzo  * SUCH DAMAGE.
27f9790aebSLuigi Rizzo  */
28f9790aebSLuigi Rizzo 
29f9790aebSLuigi Rizzo 
30f9790aebSLuigi Rizzo #if defined(__FreeBSD__)
31f9790aebSLuigi Rizzo #include <sys/cdefs.h> /* prerequisite */
32f9790aebSLuigi Rizzo #include <sys/types.h>
33f9790aebSLuigi Rizzo #include <sys/errno.h>
34f9790aebSLuigi Rizzo #include <sys/param.h>	/* defines used in kernel.h */
35f9790aebSLuigi Rizzo #include <sys/kernel.h>	/* types used in module initialization */
36f9790aebSLuigi Rizzo #include <sys/conf.h>	/* cdevsw struct, UID, GID */
37f9790aebSLuigi Rizzo #include <sys/sockio.h>
38f9790aebSLuigi Rizzo #include <sys/socketvar.h>	/* struct socket */
39f9790aebSLuigi Rizzo #include <sys/malloc.h>
40f9790aebSLuigi Rizzo #include <sys/poll.h>
41f9790aebSLuigi Rizzo #include <sys/rwlock.h>
42f9790aebSLuigi Rizzo #include <sys/socket.h> /* sockaddrs */
43f9790aebSLuigi Rizzo #include <sys/selinfo.h>
44f9790aebSLuigi Rizzo #include <sys/sysctl.h>
45f9790aebSLuigi Rizzo #include <net/if.h>
46f9790aebSLuigi Rizzo #include <net/if_var.h>
47f9790aebSLuigi Rizzo #include <net/bpf.h>		/* BIOCIMMEDIATE */
48f9790aebSLuigi Rizzo #include <machine/bus.h>	/* bus_dmamap_* */
49f9790aebSLuigi Rizzo #include <sys/endian.h>
50f9790aebSLuigi Rizzo #include <sys/refcount.h>
512a7db7a6SVincenzo Maffione #include <sys/smp.h>
52f9790aebSLuigi Rizzo 
53f9790aebSLuigi Rizzo 
54f9790aebSLuigi Rizzo #elif defined(linux)
55f9790aebSLuigi Rizzo 
56f9790aebSLuigi Rizzo #include "bsd_glue.h"
57f9790aebSLuigi Rizzo 
58f9790aebSLuigi Rizzo #elif defined(__APPLE__)
59f9790aebSLuigi Rizzo 
60f9790aebSLuigi Rizzo #warning OSX support is only partial
61f9790aebSLuigi Rizzo #include "osx_glue.h"
62f9790aebSLuigi Rizzo 
6337e3a6d3SLuigi Rizzo #elif defined(_WIN32)
6437e3a6d3SLuigi Rizzo #include "win_glue.h"
6537e3a6d3SLuigi Rizzo 
66f9790aebSLuigi Rizzo #else
67f9790aebSLuigi Rizzo 
68f9790aebSLuigi Rizzo #error	Unsupported platform
69f9790aebSLuigi Rizzo 
70f9790aebSLuigi Rizzo #endif /* unsupported */
71f9790aebSLuigi Rizzo 
72f9790aebSLuigi Rizzo /*
73f9790aebSLuigi Rizzo  * common headers
74f9790aebSLuigi Rizzo  */
75f9790aebSLuigi Rizzo 
76f9790aebSLuigi Rizzo #include <net/netmap.h>
77f9790aebSLuigi Rizzo #include <dev/netmap/netmap_kern.h>
78f9790aebSLuigi Rizzo #include <dev/netmap/netmap_mem2.h>
792a7db7a6SVincenzo Maffione #include <dev/netmap/netmap_bdg.h>
80f9790aebSLuigi Rizzo 
81f9790aebSLuigi Rizzo #ifdef WITH_VALE
82f9790aebSLuigi Rizzo 
83f9790aebSLuigi Rizzo /*
84f9790aebSLuigi Rizzo  * system parameters (most of them in netmap_kern.h)
8537e3a6d3SLuigi Rizzo  * NM_BDG_NAME		prefix for switch port names, default "vale"
86f9790aebSLuigi Rizzo  * NM_BDG_MAXPORTS	number of ports
87f9790aebSLuigi Rizzo  * NM_BRIDGES		max number of switches in the system.
88f9790aebSLuigi Rizzo  *
89f9790aebSLuigi Rizzo  * Switch ports are named valeX:Y where X is the switch name and Y
90f9790aebSLuigi Rizzo  * is the port. If Y matches a physical interface name, the port is
91f9790aebSLuigi Rizzo  * connected to a physical device.
92f9790aebSLuigi Rizzo  *
93f9790aebSLuigi Rizzo  * Unlike physical interfaces, switch ports use their own memory region
94f9790aebSLuigi Rizzo  * for rings and buffers.
95f9790aebSLuigi Rizzo  * The virtual interfaces use per-queue lock instead of core lock.
96f9790aebSLuigi Rizzo  * In the tx loop, we aggregate traffic in batches to make all operations
97f9790aebSLuigi Rizzo  * faster. The batch size is bridge_batch.
98f9790aebSLuigi Rizzo  */
99a6d768d8SVincenzo Maffione #define NM_BDG_MAXRINGS		16	/* XXX unclear how many (must be a pow of 2). */
100f9790aebSLuigi Rizzo #define NM_BDG_MAXSLOTS		4096	/* XXX same as above */
101f9790aebSLuigi Rizzo #define NM_BRIDGE_RINGSIZE	1024	/* in the device */
102f9790aebSLuigi Rizzo #define NM_BDG_BATCH		1024	/* entries in the forwarding buffer */
103f9790aebSLuigi Rizzo /* actual size of the tables */
1042a7db7a6SVincenzo Maffione #define NM_BDG_BATCH_MAX	(NM_BDG_BATCH + NETMAP_MAX_FRAGS)
105f9790aebSLuigi Rizzo /* NM_FT_NULL terminates a list of slots in the ft */
106f9790aebSLuigi Rizzo #define NM_FT_NULL		NM_BDG_BATCH_MAX
107f9790aebSLuigi Rizzo 
108f9790aebSLuigi Rizzo 
109f9790aebSLuigi Rizzo /*
110f9790aebSLuigi Rizzo  * bridge_batch is set via sysctl to the max batch size to be
111f9790aebSLuigi Rizzo  * used in the bridge. The actual value may be larger as the
112f9790aebSLuigi Rizzo  * last packet in the block may overflow the size.
113f9790aebSLuigi Rizzo  */
11437e3a6d3SLuigi Rizzo static int bridge_batch = NM_BDG_BATCH; /* bridge batch size */
115dd6ab49aSVincenzo Maffione 
116dd6ab49aSVincenzo Maffione /* Max number of vale bridges (loader tunable). */
117dd6ab49aSVincenzo Maffione unsigned int vale_max_bridges = NM_BRIDGES;
118dd6ab49aSVincenzo Maffione 
11937e3a6d3SLuigi Rizzo SYSBEGIN(vars_vale);
120f9790aebSLuigi Rizzo SYSCTL_DECL(_dev_netmap);
1214f80b14cSVincenzo Maffione SYSCTL_INT(_dev_netmap, OID_AUTO, bridge_batch, CTLFLAG_RW, &bridge_batch, 0,
1224f80b14cSVincenzo Maffione 		"Max batch size to be used in the bridge");
123dd6ab49aSVincenzo Maffione SYSCTL_UINT(_dev_netmap, OID_AUTO, max_bridges, CTLFLAG_RDTUN, &vale_max_bridges, 0,
124dd6ab49aSVincenzo Maffione 		"Max number of vale bridges");
12537e3a6d3SLuigi Rizzo SYSEND;
126f9790aebSLuigi Rizzo 
127e330262fSJustin Hibbits static int netmap_vale_vp_create(struct nmreq_header *hdr, if_t,
128c3e9b4dbSLuiz Otavio O Souza 		struct netmap_mem_d *nmd, struct netmap_vp_adapter **);
129b6e66be2SVincenzo Maffione static int netmap_vale_vp_bdg_attach(const char *, struct netmap_adapter *,
1302a7db7a6SVincenzo Maffione 		struct nm_bridge *);
1312a7db7a6SVincenzo Maffione static int netmap_vale_bwrap_attach(const char *, struct netmap_adapter *);
132f9790aebSLuigi Rizzo 
133f9790aebSLuigi Rizzo /*
134b6e66be2SVincenzo Maffione  * For each output interface, nm_vale_q is used to construct a list.
135f9790aebSLuigi Rizzo  * bq_len is the number of output buffers (we can have coalescing
136f9790aebSLuigi Rizzo  * during the copy).
137f9790aebSLuigi Rizzo  */
138b6e66be2SVincenzo Maffione struct nm_vale_q {
139f9790aebSLuigi Rizzo 	uint16_t bq_head;
140f9790aebSLuigi Rizzo 	uint16_t bq_tail;
141f9790aebSLuigi Rizzo 	uint32_t bq_len;	/* number of buffers */
142f9790aebSLuigi Rizzo };
143f9790aebSLuigi Rizzo 
1442ff91c17SVincenzo Maffione /* Holds the default callbacks */
1452a7db7a6SVincenzo Maffione struct netmap_bdg_ops vale_bdg_ops = {
146b6e66be2SVincenzo Maffione 	.lookup = netmap_vale_learning,
1472a7db7a6SVincenzo Maffione 	.config = NULL,
1482a7db7a6SVincenzo Maffione 	.dtor = NULL,
149b6e66be2SVincenzo Maffione 	.vp_create = netmap_vale_vp_create,
1502a7db7a6SVincenzo Maffione 	.bwrap_attach = netmap_vale_bwrap_attach,
1512a7db7a6SVincenzo Maffione 	.name = NM_BDG_NAME,
152f9790aebSLuigi Rizzo };
153f9790aebSLuigi Rizzo 
154f9790aebSLuigi Rizzo /*
155f9790aebSLuigi Rizzo  * this is a slightly optimized copy routine which rounds
156f9790aebSLuigi Rizzo  * to multiple of 64 bytes and is often faster than dealing
157f9790aebSLuigi Rizzo  * with other odd sizes. We assume there is enough room
158f9790aebSLuigi Rizzo  * in the source and destination buffers.
159f9790aebSLuigi Rizzo  *
160a6d768d8SVincenzo Maffione  * XXX only for multiples of NM_BUF_ALIGN bytes, non overlapped.
161f9790aebSLuigi Rizzo  */
162a6d768d8SVincenzo Maffione 
163f9790aebSLuigi Rizzo static inline void
pkt_copy(void * _src,void * _dst,int l)164f9790aebSLuigi Rizzo pkt_copy(void *_src, void *_dst, int l)
165f9790aebSLuigi Rizzo {
166f9790aebSLuigi Rizzo 	uint64_t *src = _src;
167f9790aebSLuigi Rizzo 	uint64_t *dst = _dst;
168f9790aebSLuigi Rizzo 	if (unlikely(l >= 1024)) {
169f9790aebSLuigi Rizzo 		memcpy(dst, src, l);
170f9790aebSLuigi Rizzo 		return;
171f9790aebSLuigi Rizzo 	}
172a6d768d8SVincenzo Maffione 	for (; likely(l > 0); l -= NM_BUF_ALIGN) {
173a6d768d8SVincenzo Maffione 		/* XXX NM_BUF_ALIGN/sizeof(uint64_t) statements */
174f9790aebSLuigi Rizzo 		*dst++ = *src++;
175f9790aebSLuigi Rizzo 		*dst++ = *src++;
176f9790aebSLuigi Rizzo 		*dst++ = *src++;
177f9790aebSLuigi Rizzo 		*dst++ = *src++;
178f9790aebSLuigi Rizzo 		*dst++ = *src++;
179f9790aebSLuigi Rizzo 		*dst++ = *src++;
180f9790aebSLuigi Rizzo 		*dst++ = *src++;
181f9790aebSLuigi Rizzo 		*dst++ = *src++;
182f9790aebSLuigi Rizzo 	}
183f9790aebSLuigi Rizzo }
184f9790aebSLuigi Rizzo 
185f9790aebSLuigi Rizzo 
186f9790aebSLuigi Rizzo /*
187f9790aebSLuigi Rizzo  * Free the forwarding tables for rings attached to switch ports.
188f9790aebSLuigi Rizzo  */
189f9790aebSLuigi Rizzo static void
nm_free_bdgfwd(struct netmap_adapter * na)190f9790aebSLuigi Rizzo nm_free_bdgfwd(struct netmap_adapter *na)
191f9790aebSLuigi Rizzo {
192f9790aebSLuigi Rizzo 	int nrings, i;
1932ff91c17SVincenzo Maffione 	struct netmap_kring **kring;
194f9790aebSLuigi Rizzo 
195f9790aebSLuigi Rizzo 	NMG_LOCK_ASSERT();
19617885a7bSLuigi Rizzo 	nrings = na->num_tx_rings;
19717885a7bSLuigi Rizzo 	kring = na->tx_rings;
198f9790aebSLuigi Rizzo 	for (i = 0; i < nrings; i++) {
1992ff91c17SVincenzo Maffione 		if (kring[i]->nkr_ft) {
2002ff91c17SVincenzo Maffione 			nm_os_free(kring[i]->nkr_ft);
2012ff91c17SVincenzo Maffione 			kring[i]->nkr_ft = NULL; /* protect from freeing twice */
202f9790aebSLuigi Rizzo 		}
203f9790aebSLuigi Rizzo 	}
204f9790aebSLuigi Rizzo }
205f9790aebSLuigi Rizzo 
206f9790aebSLuigi Rizzo 
207f9790aebSLuigi Rizzo /*
208f9790aebSLuigi Rizzo  * Allocate the forwarding tables for the rings attached to the bridge ports.
209f9790aebSLuigi Rizzo  */
210f9790aebSLuigi Rizzo static int
nm_alloc_bdgfwd(struct netmap_adapter * na)211f9790aebSLuigi Rizzo nm_alloc_bdgfwd(struct netmap_adapter *na)
212f9790aebSLuigi Rizzo {
213f9790aebSLuigi Rizzo 	int nrings, l, i, num_dstq;
2142ff91c17SVincenzo Maffione 	struct netmap_kring **kring;
215f9790aebSLuigi Rizzo 
216f9790aebSLuigi Rizzo 	NMG_LOCK_ASSERT();
217f9790aebSLuigi Rizzo 	/* all port:rings + broadcast */
218f9790aebSLuigi Rizzo 	num_dstq = NM_BDG_MAXPORTS * NM_BDG_MAXRINGS + 1;
219f9790aebSLuigi Rizzo 	l = sizeof(struct nm_bdg_fwd) * NM_BDG_BATCH_MAX;
220b6e66be2SVincenzo Maffione 	l += sizeof(struct nm_vale_q) * num_dstq;
221f9790aebSLuigi Rizzo 	l += sizeof(uint16_t) * NM_BDG_BATCH_MAX;
222f9790aebSLuigi Rizzo 
223847bf383SLuigi Rizzo 	nrings = netmap_real_rings(na, NR_TX);
224f9790aebSLuigi Rizzo 	kring = na->tx_rings;
225f9790aebSLuigi Rizzo 	for (i = 0; i < nrings; i++) {
226f9790aebSLuigi Rizzo 		struct nm_bdg_fwd *ft;
227b6e66be2SVincenzo Maffione 		struct nm_vale_q *dstq;
228f9790aebSLuigi Rizzo 		int j;
229f9790aebSLuigi Rizzo 
230c3e9b4dbSLuiz Otavio O Souza 		ft = nm_os_malloc(l);
231f9790aebSLuigi Rizzo 		if (!ft) {
232f9790aebSLuigi Rizzo 			nm_free_bdgfwd(na);
233f9790aebSLuigi Rizzo 			return ENOMEM;
234f9790aebSLuigi Rizzo 		}
235b6e66be2SVincenzo Maffione 		dstq = (struct nm_vale_q *)(ft + NM_BDG_BATCH_MAX);
236f9790aebSLuigi Rizzo 		for (j = 0; j < num_dstq; j++) {
237f9790aebSLuigi Rizzo 			dstq[j].bq_head = dstq[j].bq_tail = NM_FT_NULL;
238f9790aebSLuigi Rizzo 			dstq[j].bq_len = 0;
239f9790aebSLuigi Rizzo 		}
2402ff91c17SVincenzo Maffione 		kring[i]->nkr_ft = ft;
241f9790aebSLuigi Rizzo 	}
242f9790aebSLuigi Rizzo 	return 0;
243f9790aebSLuigi Rizzo }
244f9790aebSLuigi Rizzo 
2452ff91c17SVincenzo Maffione /* Allows external modules to create bridges in exclusive mode,
2462ff91c17SVincenzo Maffione  * returns an authentication token that the external module will need
2472ff91c17SVincenzo Maffione  * to provide during nm_bdg_ctl_{attach, detach}(), netmap_bdg_regops(),
2482ff91c17SVincenzo Maffione  * and nm_bdg_update_private_data() operations.
2492ff91c17SVincenzo Maffione  * Successfully executed if ret != NULL and *return_status == 0.
2502ff91c17SVincenzo Maffione  */
2512ff91c17SVincenzo Maffione void *
netmap_vale_create(const char * bdg_name,int * return_status)2522a7db7a6SVincenzo Maffione netmap_vale_create(const char *bdg_name, int *return_status)
2532ff91c17SVincenzo Maffione {
2542ff91c17SVincenzo Maffione 	struct nm_bridge *b = NULL;
2552ff91c17SVincenzo Maffione 	void *ret = NULL;
2562ff91c17SVincenzo Maffione 
2572ff91c17SVincenzo Maffione 	NMG_LOCK();
2582a7db7a6SVincenzo Maffione 	b = nm_find_bridge(bdg_name, 0 /* don't create */, NULL);
2592ff91c17SVincenzo Maffione 	if (b) {
2602ff91c17SVincenzo Maffione 		*return_status = EEXIST;
2612ff91c17SVincenzo Maffione 		goto unlock_bdg_create;
2622ff91c17SVincenzo Maffione 	}
2632ff91c17SVincenzo Maffione 
2642a7db7a6SVincenzo Maffione 	b = nm_find_bridge(bdg_name, 1 /* create */, &vale_bdg_ops);
2652ff91c17SVincenzo Maffione 	if (!b) {
2662ff91c17SVincenzo Maffione 		*return_status = ENOMEM;
2672ff91c17SVincenzo Maffione 		goto unlock_bdg_create;
2682ff91c17SVincenzo Maffione 	}
2692ff91c17SVincenzo Maffione 
2702ff91c17SVincenzo Maffione 	b->bdg_flags |= NM_BDG_ACTIVE | NM_BDG_EXCLUSIVE;
2712ff91c17SVincenzo Maffione 	ret = nm_bdg_get_auth_token(b);
2722ff91c17SVincenzo Maffione 	*return_status = 0;
2732ff91c17SVincenzo Maffione 
2742ff91c17SVincenzo Maffione unlock_bdg_create:
2752ff91c17SVincenzo Maffione 	NMG_UNLOCK();
2762ff91c17SVincenzo Maffione 	return ret;
2772ff91c17SVincenzo Maffione }
2782ff91c17SVincenzo Maffione 
2792ff91c17SVincenzo Maffione /* Allows external modules to destroy a bridge created through
2802ff91c17SVincenzo Maffione  * netmap_bdg_create(), the bridge must be empty.
2812ff91c17SVincenzo Maffione  */
2822ff91c17SVincenzo Maffione int
netmap_vale_destroy(const char * bdg_name,void * auth_token)2832a7db7a6SVincenzo Maffione netmap_vale_destroy(const char *bdg_name, void *auth_token)
2842ff91c17SVincenzo Maffione {
2852ff91c17SVincenzo Maffione 	struct nm_bridge *b = NULL;
2862ff91c17SVincenzo Maffione 	int ret = 0;
2872ff91c17SVincenzo Maffione 
2882ff91c17SVincenzo Maffione 	NMG_LOCK();
2892a7db7a6SVincenzo Maffione 	b = nm_find_bridge(bdg_name, 0 /* don't create */, NULL);
2902ff91c17SVincenzo Maffione 	if (!b) {
2912ff91c17SVincenzo Maffione 		ret = ENXIO;
2922ff91c17SVincenzo Maffione 		goto unlock_bdg_free;
2932ff91c17SVincenzo Maffione 	}
2942ff91c17SVincenzo Maffione 
2952ff91c17SVincenzo Maffione 	if (!nm_bdg_valid_auth_token(b, auth_token)) {
2962ff91c17SVincenzo Maffione 		ret = EACCES;
2972ff91c17SVincenzo Maffione 		goto unlock_bdg_free;
2982ff91c17SVincenzo Maffione 	}
2992ff91c17SVincenzo Maffione 	if (!(b->bdg_flags & NM_BDG_EXCLUSIVE)) {
3002ff91c17SVincenzo Maffione 		ret = EINVAL;
3012ff91c17SVincenzo Maffione 		goto unlock_bdg_free;
3022ff91c17SVincenzo Maffione 	}
3032ff91c17SVincenzo Maffione 
3042ff91c17SVincenzo Maffione 	b->bdg_flags &= ~(NM_BDG_EXCLUSIVE | NM_BDG_ACTIVE);
3052ff91c17SVincenzo Maffione 	ret = netmap_bdg_free(b);
3062ff91c17SVincenzo Maffione 	if (ret) {
3072ff91c17SVincenzo Maffione 		b->bdg_flags |= NM_BDG_EXCLUSIVE | NM_BDG_ACTIVE;
3082ff91c17SVincenzo Maffione 	}
3092ff91c17SVincenzo Maffione 
3102ff91c17SVincenzo Maffione unlock_bdg_free:
3112ff91c17SVincenzo Maffione 	NMG_UNLOCK();
3122ff91c17SVincenzo Maffione 	return ret;
3132ff91c17SVincenzo Maffione }
3142ff91c17SVincenzo Maffione 
315b6e66be2SVincenzo Maffione /* Process NETMAP_REQ_VALE_LIST. */
316b6e66be2SVincenzo Maffione int
netmap_vale_list(struct nmreq_header * hdr)317b6e66be2SVincenzo Maffione netmap_vale_list(struct nmreq_header *hdr)
318b6e66be2SVincenzo Maffione {
319b6e66be2SVincenzo Maffione 	struct nmreq_vale_list *req =
320b6e66be2SVincenzo Maffione 		(struct nmreq_vale_list *)(uintptr_t)hdr->nr_body;
321b6e66be2SVincenzo Maffione 	int namelen = strlen(hdr->nr_name);
322b6e66be2SVincenzo Maffione 	struct nm_bridge *b, *bridges;
323b6e66be2SVincenzo Maffione 	struct netmap_vp_adapter *vpna;
324b6e66be2SVincenzo Maffione 	int error = 0, i, j;
325b6e66be2SVincenzo Maffione 	u_int num_bridges;
326b6e66be2SVincenzo Maffione 
327b6e66be2SVincenzo Maffione 	netmap_bns_getbridges(&bridges, &num_bridges);
328b6e66be2SVincenzo Maffione 
329b6e66be2SVincenzo Maffione 	/* this is used to enumerate bridges and ports */
330b6e66be2SVincenzo Maffione 	if (namelen) { /* look up indexes of bridge and port */
331b6e66be2SVincenzo Maffione 		if (strncmp(hdr->nr_name, NM_BDG_NAME,
332b6e66be2SVincenzo Maffione 					strlen(NM_BDG_NAME))) {
333b6e66be2SVincenzo Maffione 			return EINVAL;
334b6e66be2SVincenzo Maffione 		}
335b6e66be2SVincenzo Maffione 		NMG_LOCK();
336b6e66be2SVincenzo Maffione 		b = nm_find_bridge(hdr->nr_name, 0 /* don't create */, NULL);
337b6e66be2SVincenzo Maffione 		if (!b) {
338b6e66be2SVincenzo Maffione 			NMG_UNLOCK();
339b6e66be2SVincenzo Maffione 			return ENOENT;
340b6e66be2SVincenzo Maffione 		}
341b6e66be2SVincenzo Maffione 
342b6e66be2SVincenzo Maffione 		req->nr_bridge_idx = b - bridges; /* bridge index */
343b6e66be2SVincenzo Maffione 		req->nr_port_idx = NM_BDG_NOPORT;
344b6e66be2SVincenzo Maffione 		for (j = 0; j < b->bdg_active_ports; j++) {
345b6e66be2SVincenzo Maffione 			i = b->bdg_port_index[j];
346b6e66be2SVincenzo Maffione 			vpna = b->bdg_ports[i];
347b6e66be2SVincenzo Maffione 			if (vpna == NULL) {
348b6e66be2SVincenzo Maffione 				nm_prerr("This should not happen");
349b6e66be2SVincenzo Maffione 				continue;
350b6e66be2SVincenzo Maffione 			}
351b6e66be2SVincenzo Maffione 			/* the former and the latter identify a
352b6e66be2SVincenzo Maffione 			 * virtual port and a NIC, respectively
353b6e66be2SVincenzo Maffione 			 */
354b6e66be2SVincenzo Maffione 			if (!strcmp(vpna->up.name, hdr->nr_name)) {
355b6e66be2SVincenzo Maffione 				req->nr_port_idx = i; /* port index */
356b6e66be2SVincenzo Maffione 				break;
357b6e66be2SVincenzo Maffione 			}
358b6e66be2SVincenzo Maffione 		}
359b6e66be2SVincenzo Maffione 		NMG_UNLOCK();
360b6e66be2SVincenzo Maffione 	} else {
361b6e66be2SVincenzo Maffione 		/* return the first non-empty entry starting from
362b6e66be2SVincenzo Maffione 		 * bridge nr_arg1 and port nr_arg2.
363b6e66be2SVincenzo Maffione 		 *
364b6e66be2SVincenzo Maffione 		 * Users can detect the end of the same bridge by
365b6e66be2SVincenzo Maffione 		 * seeing the new and old value of nr_arg1, and can
366b6e66be2SVincenzo Maffione 		 * detect the end of all the bridge by error != 0
367b6e66be2SVincenzo Maffione 		 */
368b6e66be2SVincenzo Maffione 		i = req->nr_bridge_idx;
369b6e66be2SVincenzo Maffione 		j = req->nr_port_idx;
370b6e66be2SVincenzo Maffione 
371b6e66be2SVincenzo Maffione 		NMG_LOCK();
372dd6ab49aSVincenzo Maffione 		for (error = ENOENT; i < vale_max_bridges; i++) {
373b6e66be2SVincenzo Maffione 			b = bridges + i;
374b6e66be2SVincenzo Maffione 			for ( ; j < NM_BDG_MAXPORTS; j++) {
375b6e66be2SVincenzo Maffione 				if (b->bdg_ports[j] == NULL)
376b6e66be2SVincenzo Maffione 					continue;
377b6e66be2SVincenzo Maffione 				vpna = b->bdg_ports[j];
378b6e66be2SVincenzo Maffione 				/* write back the VALE switch name */
379b6e66be2SVincenzo Maffione 				strlcpy(hdr->nr_name, vpna->up.name,
380b6e66be2SVincenzo Maffione 					sizeof(hdr->nr_name));
381b6e66be2SVincenzo Maffione 				error = 0;
382b6e66be2SVincenzo Maffione 				goto out;
383b6e66be2SVincenzo Maffione 			}
384b6e66be2SVincenzo Maffione 			j = 0; /* following bridges scan from 0 */
385b6e66be2SVincenzo Maffione 		}
386b6e66be2SVincenzo Maffione 	out:
387b6e66be2SVincenzo Maffione 		req->nr_bridge_idx = i;
388b6e66be2SVincenzo Maffione 		req->nr_port_idx = j;
389b6e66be2SVincenzo Maffione 		NMG_UNLOCK();
390b6e66be2SVincenzo Maffione 	}
391b6e66be2SVincenzo Maffione 
392b6e66be2SVincenzo Maffione 	return error;
393b6e66be2SVincenzo Maffione }
394b6e66be2SVincenzo Maffione 
3952ff91c17SVincenzo Maffione 
3964bf50f18SLuigi Rizzo /* nm_dtor callback for ephemeral VALE ports */
3974bf50f18SLuigi Rizzo static void
netmap_vale_vp_dtor(struct netmap_adapter * na)398b6e66be2SVincenzo Maffione netmap_vale_vp_dtor(struct netmap_adapter *na)
3994bf50f18SLuigi Rizzo {
4004bf50f18SLuigi Rizzo 	struct netmap_vp_adapter *vpna = (struct netmap_vp_adapter*)na;
4014bf50f18SLuigi Rizzo 	struct nm_bridge *b = vpna->na_bdg;
4024bf50f18SLuigi Rizzo 
40375f4f3edSVincenzo Maffione 	nm_prdis("%s has %d references", na->name, na->na_refcount);
404f9790aebSLuigi Rizzo 
405f9790aebSLuigi Rizzo 	if (b) {
406f9790aebSLuigi Rizzo 		netmap_bdg_detach_common(b, vpna->bdg_port, -1);
407f9790aebSLuigi Rizzo 	}
408c3e9b4dbSLuiz Otavio O Souza 
4094f80b14cSVincenzo Maffione 	if (na->ifp != NULL && !nm_iszombie(na)) {
4102a7db7a6SVincenzo Maffione 		NM_DETACH_NA(na->ifp);
4114f80b14cSVincenzo Maffione 		if (vpna->autodelete) {
412e330262fSJustin Hibbits 			nm_prdis("releasing %s", if_name(na->ifp));
413c3e9b4dbSLuiz Otavio O Souza 			NMG_UNLOCK();
414c3e9b4dbSLuiz Otavio O Souza 			nm_os_vi_detach(na->ifp);
415c3e9b4dbSLuiz Otavio O Souza 			NMG_LOCK();
416c3e9b4dbSLuiz Otavio O Souza 		}
417f9790aebSLuigi Rizzo 	}
4184f80b14cSVincenzo Maffione }
419f9790aebSLuigi Rizzo 
4202ff91c17SVincenzo Maffione 
4214bf50f18SLuigi Rizzo 
4224bf50f18SLuigi Rizzo /* nm_krings_create callback for VALE ports.
4234bf50f18SLuigi Rizzo  * Calls the standard netmap_krings_create, then adds leases on rx
4244bf50f18SLuigi Rizzo  * rings and bdgfwd on tx rings.
4254bf50f18SLuigi Rizzo  */
426f9790aebSLuigi Rizzo static int
netmap_vale_vp_krings_create(struct netmap_adapter * na)427b6e66be2SVincenzo Maffione netmap_vale_vp_krings_create(struct netmap_adapter *na)
428f9790aebSLuigi Rizzo {
429f0ea3689SLuigi Rizzo 	u_int tailroom;
430f9790aebSLuigi Rizzo 	int error, i;
431f9790aebSLuigi Rizzo 	uint32_t *leases;
432847bf383SLuigi Rizzo 	u_int nrx = netmap_real_rings(na, NR_RX);
433f9790aebSLuigi Rizzo 
434f9790aebSLuigi Rizzo 	/*
435f9790aebSLuigi Rizzo 	 * Leases are attached to RX rings on vale ports
436f9790aebSLuigi Rizzo 	 */
437f9790aebSLuigi Rizzo 	tailroom = sizeof(uint32_t) * na->num_rx_desc * nrx;
438f9790aebSLuigi Rizzo 
439f0ea3689SLuigi Rizzo 	error = netmap_krings_create(na, tailroom);
440f9790aebSLuigi Rizzo 	if (error)
441f9790aebSLuigi Rizzo 		return error;
442f9790aebSLuigi Rizzo 
443f9790aebSLuigi Rizzo 	leases = na->tailroom;
444f9790aebSLuigi Rizzo 
445f9790aebSLuigi Rizzo 	for (i = 0; i < nrx; i++) { /* Receive rings */
4462ff91c17SVincenzo Maffione 		na->rx_rings[i]->nkr_leases = leases;
447f9790aebSLuigi Rizzo 		leases += na->num_rx_desc;
448f9790aebSLuigi Rizzo 	}
449f9790aebSLuigi Rizzo 
450f9790aebSLuigi Rizzo 	error = nm_alloc_bdgfwd(na);
451f9790aebSLuigi Rizzo 	if (error) {
452f9790aebSLuigi Rizzo 		netmap_krings_delete(na);
453f9790aebSLuigi Rizzo 		return error;
454f9790aebSLuigi Rizzo 	}
455f9790aebSLuigi Rizzo 
456f9790aebSLuigi Rizzo 	return 0;
457f9790aebSLuigi Rizzo }
458f9790aebSLuigi Rizzo 
45917885a7bSLuigi Rizzo 
4604bf50f18SLuigi Rizzo /* nm_krings_delete callback for VALE ports. */
461f9790aebSLuigi Rizzo static void
netmap_vale_vp_krings_delete(struct netmap_adapter * na)462b6e66be2SVincenzo Maffione netmap_vale_vp_krings_delete(struct netmap_adapter *na)
463f9790aebSLuigi Rizzo {
464f9790aebSLuigi Rizzo 	nm_free_bdgfwd(na);
465f9790aebSLuigi Rizzo 	netmap_krings_delete(na);
466f9790aebSLuigi Rizzo }
467f9790aebSLuigi Rizzo 
468f9790aebSLuigi Rizzo 
469f9790aebSLuigi Rizzo static int
470b6e66be2SVincenzo Maffione nm_vale_flush(struct nm_bdg_fwd *ft, u_int n,
471f9790aebSLuigi Rizzo 	struct netmap_vp_adapter *na, u_int ring_nr);
472f9790aebSLuigi Rizzo 
473f9790aebSLuigi Rizzo 
474f9790aebSLuigi Rizzo /*
4754bf50f18SLuigi Rizzo  * main dispatch routine for the bridge.
476f9790aebSLuigi Rizzo  * Grab packets from a kring, move them into the ft structure
477f9790aebSLuigi Rizzo  * associated to the tx (input) port. Max one instance per port,
478f9790aebSLuigi Rizzo  * filtered on input (ioctl, poll or XXX).
479f9790aebSLuigi Rizzo  * Returns the next position in the ring.
480f9790aebSLuigi Rizzo  */
481f9790aebSLuigi Rizzo static int
nm_vale_preflush(struct netmap_kring * kring,u_int end)482b6e66be2SVincenzo Maffione nm_vale_preflush(struct netmap_kring *kring, u_int end)
483f9790aebSLuigi Rizzo {
4844bf50f18SLuigi Rizzo 	struct netmap_vp_adapter *na =
4854bf50f18SLuigi Rizzo 		(struct netmap_vp_adapter*)kring->na;
486f9790aebSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
487f9790aebSLuigi Rizzo 	struct nm_bdg_fwd *ft;
4884bf50f18SLuigi Rizzo 	u_int ring_nr = kring->ring_id;
489f9790aebSLuigi Rizzo 	u_int j = kring->nr_hwcur, lim = kring->nkr_num_slots - 1;
490f9790aebSLuigi Rizzo 	u_int ft_i = 0;	/* start from 0 */
491f9790aebSLuigi Rizzo 	u_int frags = 1; /* how many frags ? */
492f9790aebSLuigi Rizzo 	struct nm_bridge *b = na->na_bdg;
493f9790aebSLuigi Rizzo 
494f9790aebSLuigi Rizzo 	/* To protect against modifications to the bridge we acquire a
495f9790aebSLuigi Rizzo 	 * shared lock, waiting if we can sleep (if the source port is
496f9790aebSLuigi Rizzo 	 * attached to a user process) or with a trylock otherwise (NICs).
497f9790aebSLuigi Rizzo 	 */
49875f4f3edSVincenzo Maffione 	nm_prdis("wait rlock for %d packets", ((j > end ? lim+1 : 0) + end) - j);
499f9790aebSLuigi Rizzo 	if (na->up.na_flags & NAF_BDG_MAYSLEEP)
500f9790aebSLuigi Rizzo 		BDG_RLOCK(b);
501f9790aebSLuigi Rizzo 	else if (!BDG_RTRYLOCK(b))
502c3e9b4dbSLuiz Otavio O Souza 		return j;
50375f4f3edSVincenzo Maffione 	nm_prdis(5, "rlock acquired for %d packets", ((j > end ? lim+1 : 0) + end) - j);
504f9790aebSLuigi Rizzo 	ft = kring->nkr_ft;
505f9790aebSLuigi Rizzo 
506f9790aebSLuigi Rizzo 	for (; likely(j != end); j = nm_next(j, lim)) {
507f9790aebSLuigi Rizzo 		struct netmap_slot *slot = &ring->slot[j];
508f9790aebSLuigi Rizzo 		char *buf;
509f9790aebSLuigi Rizzo 
510f9790aebSLuigi Rizzo 		ft[ft_i].ft_len = slot->len;
511f9790aebSLuigi Rizzo 		ft[ft_i].ft_flags = slot->flags;
5122ff91c17SVincenzo Maffione 		ft[ft_i].ft_offset = 0;
513f9790aebSLuigi Rizzo 
51475f4f3edSVincenzo Maffione 		nm_prdis("flags is 0x%x", slot->flags);
515847bf383SLuigi Rizzo 		/* we do not use the buf changed flag, but we still need to reset it */
516847bf383SLuigi Rizzo 		slot->flags &= ~NS_BUF_CHANGED;
517847bf383SLuigi Rizzo 
518f9790aebSLuigi Rizzo 		/* this slot goes into a list so initialize the link field */
519f9790aebSLuigi Rizzo 		ft[ft_i].ft_next = NM_FT_NULL;
520f9790aebSLuigi Rizzo 		buf = ft[ft_i].ft_buf = (slot->flags & NS_INDIRECT) ?
521a6d768d8SVincenzo Maffione 			(void *)(uintptr_t)slot->ptr : NMB_O(kring, slot);
522a6d768d8SVincenzo Maffione 		if (unlikely(buf == NULL ||
523a6d768d8SVincenzo Maffione 		     slot->len > NETMAP_BUF_SIZE(&na->up) - nm_get_offset(kring, slot))) {
524b6e66be2SVincenzo Maffione 			nm_prlim(5, "NULL %s buffer pointer from %s slot %d len %d",
525e31c6ec7SLuigi Rizzo 				(slot->flags & NS_INDIRECT) ? "INDIRECT" : "DIRECT",
526e31c6ec7SLuigi Rizzo 				kring->name, j, ft[ft_i].ft_len);
5274bf50f18SLuigi Rizzo 			buf = ft[ft_i].ft_buf = NETMAP_BUF_BASE(&na->up);
528e31c6ec7SLuigi Rizzo 			ft[ft_i].ft_len = 0;
529e31c6ec7SLuigi Rizzo 			ft[ft_i].ft_flags = 0;
530e31c6ec7SLuigi Rizzo 		}
5312e159ef0SLuigi Rizzo 		__builtin_prefetch(buf);
532f9790aebSLuigi Rizzo 		++ft_i;
533f9790aebSLuigi Rizzo 		if (slot->flags & NS_MOREFRAG) {
534f9790aebSLuigi Rizzo 			frags++;
535f9790aebSLuigi Rizzo 			continue;
536f9790aebSLuigi Rizzo 		}
537f9790aebSLuigi Rizzo 		if (unlikely(netmap_verbose && frags > 1))
53875f4f3edSVincenzo Maffione 			nm_prlim(5, "%d frags at %d", frags, ft_i - frags);
539f9790aebSLuigi Rizzo 		ft[ft_i - frags].ft_frags = frags;
540f9790aebSLuigi Rizzo 		frags = 1;
541f9790aebSLuigi Rizzo 		if (unlikely((int)ft_i >= bridge_batch))
542b6e66be2SVincenzo Maffione 			ft_i = nm_vale_flush(ft, ft_i, na, ring_nr);
543f9790aebSLuigi Rizzo 	}
544f9790aebSLuigi Rizzo 	if (frags > 1) {
54537e3a6d3SLuigi Rizzo 		/* Here ft_i > 0, ft[ft_i-1].flags has NS_MOREFRAG, and we
54637e3a6d3SLuigi Rizzo 		 * have to fix frags count. */
54737e3a6d3SLuigi Rizzo 		frags--;
54837e3a6d3SLuigi Rizzo 		ft[ft_i - 1].ft_flags &= ~NS_MOREFRAG;
54937e3a6d3SLuigi Rizzo 		ft[ft_i - frags].ft_frags = frags;
550b6e66be2SVincenzo Maffione 		nm_prlim(5, "Truncate incomplete fragment at %d (%d frags)", ft_i, frags);
551f9790aebSLuigi Rizzo 	}
552f9790aebSLuigi Rizzo 	if (ft_i)
553b6e66be2SVincenzo Maffione 		ft_i = nm_vale_flush(ft, ft_i, na, ring_nr);
554f9790aebSLuigi Rizzo 	BDG_RUNLOCK(b);
555f9790aebSLuigi Rizzo 	return j;
556f9790aebSLuigi Rizzo }
557f9790aebSLuigi Rizzo 
558f9790aebSLuigi Rizzo 
559f9790aebSLuigi Rizzo /* ----- FreeBSD if_bridge hash function ------- */
560f9790aebSLuigi Rizzo 
561f9790aebSLuigi Rizzo /*
562f9790aebSLuigi Rizzo  * The following hash function is adapted from "Hash Functions" by Bob Jenkins
563f9790aebSLuigi Rizzo  * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
564f9790aebSLuigi Rizzo  *
565f9790aebSLuigi Rizzo  * http://www.burtleburtle.net/bob/hash/spooky.html
566f9790aebSLuigi Rizzo  */
567f9790aebSLuigi Rizzo #define mix(a, b, c)                                                    \
568f9790aebSLuigi Rizzo do {                                                                    \
569f9790aebSLuigi Rizzo 	a -= b; a -= c; a ^= (c >> 13);                                 \
570f9790aebSLuigi Rizzo 	b -= c; b -= a; b ^= (a << 8);                                  \
571f9790aebSLuigi Rizzo 	c -= a; c -= b; c ^= (b >> 13);                                 \
572f9790aebSLuigi Rizzo 	a -= b; a -= c; a ^= (c >> 12);                                 \
573f9790aebSLuigi Rizzo 	b -= c; b -= a; b ^= (a << 16);                                 \
574f9790aebSLuigi Rizzo 	c -= a; c -= b; c ^= (b >> 5);                                  \
575f9790aebSLuigi Rizzo 	a -= b; a -= c; a ^= (c >> 3);                                  \
576f9790aebSLuigi Rizzo 	b -= c; b -= a; b ^= (a << 10);                                 \
577f9790aebSLuigi Rizzo 	c -= a; c -= b; c ^= (b >> 15);                                 \
578f9790aebSLuigi Rizzo } while (/*CONSTCOND*/0)
579f9790aebSLuigi Rizzo 
58017885a7bSLuigi Rizzo 
581f9790aebSLuigi Rizzo static __inline uint32_t
nm_vale_rthash(const uint8_t * addr)582b6e66be2SVincenzo Maffione nm_vale_rthash(const uint8_t *addr)
583f9790aebSLuigi Rizzo {
58445c67e8fSVincenzo Maffione 	uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = 0; // hash key
585f9790aebSLuigi Rizzo 
586f9790aebSLuigi Rizzo 	b += addr[5] << 8;
587f9790aebSLuigi Rizzo 	b += addr[4];
588f9790aebSLuigi Rizzo 	a += addr[3] << 24;
589f9790aebSLuigi Rizzo 	a += addr[2] << 16;
590f9790aebSLuigi Rizzo 	a += addr[1] << 8;
591f9790aebSLuigi Rizzo 	a += addr[0];
592f9790aebSLuigi Rizzo 
593f9790aebSLuigi Rizzo 	mix(a, b, c);
594f9790aebSLuigi Rizzo #define BRIDGE_RTHASH_MASK	(NM_BDG_HASH-1)
595f9790aebSLuigi Rizzo 	return (c & BRIDGE_RTHASH_MASK);
596f9790aebSLuigi Rizzo }
597f9790aebSLuigi Rizzo 
598f9790aebSLuigi Rizzo #undef mix
599f9790aebSLuigi Rizzo 
600f9790aebSLuigi Rizzo 
601f9790aebSLuigi Rizzo /*
602f9790aebSLuigi Rizzo  * Lookup function for a learning bridge.
603f9790aebSLuigi Rizzo  * Update the hash table with the source address,
604f9790aebSLuigi Rizzo  * and then returns the destination port index, and the
605f9790aebSLuigi Rizzo  * ring in *dst_ring (at the moment, always use ring 0)
606f9790aebSLuigi Rizzo  */
6072ff91c17SVincenzo Maffione uint32_t
netmap_vale_learning(struct nm_bdg_fwd * ft,uint8_t * dst_ring,struct netmap_vp_adapter * na,void * private_data)608b6e66be2SVincenzo Maffione netmap_vale_learning(struct nm_bdg_fwd *ft, uint8_t *dst_ring,
6092ff91c17SVincenzo Maffione 		struct netmap_vp_adapter *na, void *private_data)
610f9790aebSLuigi Rizzo {
6112ff91c17SVincenzo Maffione 	uint8_t *buf = ((uint8_t *)ft->ft_buf) + ft->ft_offset;
6122ff91c17SVincenzo Maffione 	u_int buf_len = ft->ft_len - ft->ft_offset;
6132ff91c17SVincenzo Maffione 	struct nm_hash_ent *ht = private_data;
614f9790aebSLuigi Rizzo 	uint32_t sh, dh;
615f9790aebSLuigi Rizzo 	u_int dst, mysrc = na->bdg_port;
616f9790aebSLuigi Rizzo 	uint64_t smac, dmac;
61737e3a6d3SLuigi Rizzo 	uint8_t indbuf[12];
618f9790aebSLuigi Rizzo 
6192ff91c17SVincenzo Maffione 	if (buf_len < 14) {
620f9790aebSLuigi Rizzo 		return NM_BDG_NOPORT;
621f9790aebSLuigi Rizzo 	}
62237e3a6d3SLuigi Rizzo 
62337e3a6d3SLuigi Rizzo 	if (ft->ft_flags & NS_INDIRECT) {
62437e3a6d3SLuigi Rizzo 		if (copyin(buf, indbuf, sizeof(indbuf))) {
62537e3a6d3SLuigi Rizzo 			return NM_BDG_NOPORT;
62637e3a6d3SLuigi Rizzo 		}
62737e3a6d3SLuigi Rizzo 		buf = indbuf;
62837e3a6d3SLuigi Rizzo 	}
62937e3a6d3SLuigi Rizzo 
630f9790aebSLuigi Rizzo 	dmac = le64toh(*(uint64_t *)(buf)) & 0xffffffffffff;
631f9790aebSLuigi Rizzo 	smac = le64toh(*(uint64_t *)(buf + 4));
632f9790aebSLuigi Rizzo 	smac >>= 16;
633f9790aebSLuigi Rizzo 
634f9790aebSLuigi Rizzo 	/*
635f9790aebSLuigi Rizzo 	 * The hash is somewhat expensive, there might be some
636f9790aebSLuigi Rizzo 	 * worthwhile optimizations here.
637f9790aebSLuigi Rizzo 	 */
638847bf383SLuigi Rizzo 	if (((buf[6] & 1) == 0) && (na->last_smac != smac)) { /* valid src */
639f9790aebSLuigi Rizzo 		uint8_t *s = buf+6;
640b6e66be2SVincenzo Maffione 		sh = nm_vale_rthash(s); /* hash of source */
641f9790aebSLuigi Rizzo 		/* update source port forwarding entry */
642847bf383SLuigi Rizzo 		na->last_smac = ht[sh].mac = smac;	/* XXX expire ? */
643f9790aebSLuigi Rizzo 		ht[sh].ports = mysrc;
644b6e66be2SVincenzo Maffione 		if (netmap_debug & NM_DEBUG_VALE)
645b6e66be2SVincenzo Maffione 		    nm_prinf("src %02x:%02x:%02x:%02x:%02x:%02x on port %d",
646f9790aebSLuigi Rizzo 			s[0], s[1], s[2], s[3], s[4], s[5], mysrc);
647f9790aebSLuigi Rizzo 	}
648f9790aebSLuigi Rizzo 	dst = NM_BDG_BROADCAST;
649f9790aebSLuigi Rizzo 	if ((buf[0] & 1) == 0) { /* unicast */
650b6e66be2SVincenzo Maffione 		dh = nm_vale_rthash(buf); /* hash of dst */
651f9790aebSLuigi Rizzo 		if (ht[dh].mac == dmac) {	/* found dst */
652f9790aebSLuigi Rizzo 			dst = ht[dh].ports;
653f9790aebSLuigi Rizzo 		}
654f9790aebSLuigi Rizzo 	}
655f9790aebSLuigi Rizzo 	return dst;
656f9790aebSLuigi Rizzo }
657f9790aebSLuigi Rizzo 
658f9790aebSLuigi Rizzo 
659f9790aebSLuigi Rizzo /*
66017885a7bSLuigi Rizzo  * Available space in the ring. Only used in VALE code
66117885a7bSLuigi Rizzo  * and only with is_rx = 1
66217885a7bSLuigi Rizzo  */
66317885a7bSLuigi Rizzo static inline uint32_t
nm_kr_space(struct netmap_kring * k,int is_rx)66417885a7bSLuigi Rizzo nm_kr_space(struct netmap_kring *k, int is_rx)
66517885a7bSLuigi Rizzo {
66617885a7bSLuigi Rizzo 	int space;
66717885a7bSLuigi Rizzo 
66817885a7bSLuigi Rizzo 	if (is_rx) {
66917885a7bSLuigi Rizzo 		int busy = k->nkr_hwlease - k->nr_hwcur;
67017885a7bSLuigi Rizzo 		if (busy < 0)
67117885a7bSLuigi Rizzo 			busy += k->nkr_num_slots;
67217885a7bSLuigi Rizzo 		space = k->nkr_num_slots - 1 - busy;
67317885a7bSLuigi Rizzo 	} else {
67417885a7bSLuigi Rizzo 		/* XXX never used in this branch */
67517885a7bSLuigi Rizzo 		space = k->nr_hwtail - k->nkr_hwlease;
67617885a7bSLuigi Rizzo 		if (space < 0)
67717885a7bSLuigi Rizzo 			space += k->nkr_num_slots;
67817885a7bSLuigi Rizzo 	}
67917885a7bSLuigi Rizzo #if 0
68017885a7bSLuigi Rizzo 	// sanity check
68117885a7bSLuigi Rizzo 	if (k->nkr_hwlease >= k->nkr_num_slots ||
68217885a7bSLuigi Rizzo 		k->nr_hwcur >= k->nkr_num_slots ||
68317885a7bSLuigi Rizzo 		k->nr_tail >= k->nkr_num_slots ||
68417885a7bSLuigi Rizzo 		busy < 0 ||
68517885a7bSLuigi Rizzo 		busy >= k->nkr_num_slots) {
68675f4f3edSVincenzo Maffione 		nm_prerr("invalid kring, cur %d tail %d lease %d lease_idx %d lim %d",
68775f4f3edSVincenzo Maffione 		    k->nr_hwcur, k->nr_hwtail, k->nkr_hwlease,
68817885a7bSLuigi Rizzo 		    k->nkr_lease_idx, k->nkr_num_slots);
68917885a7bSLuigi Rizzo 	}
69017885a7bSLuigi Rizzo #endif
69117885a7bSLuigi Rizzo 	return space;
69217885a7bSLuigi Rizzo }
69317885a7bSLuigi Rizzo 
69417885a7bSLuigi Rizzo 
69517885a7bSLuigi Rizzo 
69617885a7bSLuigi Rizzo 
69717885a7bSLuigi Rizzo /* make a lease on the kring for N positions. return the
69817885a7bSLuigi Rizzo  * lease index
69917885a7bSLuigi Rizzo  * XXX only used in VALE code and with is_rx = 1
70017885a7bSLuigi Rizzo  */
70117885a7bSLuigi Rizzo static inline uint32_t
nm_kr_lease(struct netmap_kring * k,u_int n,int is_rx)70217885a7bSLuigi Rizzo nm_kr_lease(struct netmap_kring *k, u_int n, int is_rx)
70317885a7bSLuigi Rizzo {
70417885a7bSLuigi Rizzo 	uint32_t lim = k->nkr_num_slots - 1;
70517885a7bSLuigi Rizzo 	uint32_t lease_idx = k->nkr_lease_idx;
70617885a7bSLuigi Rizzo 
70717885a7bSLuigi Rizzo 	k->nkr_leases[lease_idx] = NR_NOSLOT;
70817885a7bSLuigi Rizzo 	k->nkr_lease_idx = nm_next(lease_idx, lim);
70917885a7bSLuigi Rizzo 
710b6e66be2SVincenzo Maffione #ifdef CONFIG_NETMAP_DEBUG
71117885a7bSLuigi Rizzo 	if (n > nm_kr_space(k, is_rx)) {
712b6e66be2SVincenzo Maffione 		nm_prerr("invalid request for %d slots", n);
71317885a7bSLuigi Rizzo 		panic("x");
71417885a7bSLuigi Rizzo 	}
715b6e66be2SVincenzo Maffione #endif /* CONFIG NETMAP_DEBUG */
71617885a7bSLuigi Rizzo 	/* XXX verify that there are n slots */
71717885a7bSLuigi Rizzo 	k->nkr_hwlease += n;
71817885a7bSLuigi Rizzo 	if (k->nkr_hwlease > lim)
71917885a7bSLuigi Rizzo 		k->nkr_hwlease -= lim + 1;
72017885a7bSLuigi Rizzo 
721b6e66be2SVincenzo Maffione #ifdef CONFIG_NETMAP_DEBUG
72217885a7bSLuigi Rizzo 	if (k->nkr_hwlease >= k->nkr_num_slots ||
72317885a7bSLuigi Rizzo 		k->nr_hwcur >= k->nkr_num_slots ||
72417885a7bSLuigi Rizzo 		k->nr_hwtail >= k->nkr_num_slots ||
72517885a7bSLuigi Rizzo 		k->nkr_lease_idx >= k->nkr_num_slots) {
726b6e66be2SVincenzo Maffione 		nm_prerr("invalid kring %s, cur %d tail %d lease %d lease_idx %d lim %d",
7274bf50f18SLuigi Rizzo 			k->na->name,
72817885a7bSLuigi Rizzo 			k->nr_hwcur, k->nr_hwtail, k->nkr_hwlease,
72917885a7bSLuigi Rizzo 			k->nkr_lease_idx, k->nkr_num_slots);
73017885a7bSLuigi Rizzo 	}
731b6e66be2SVincenzo Maffione #endif /* CONFIG_NETMAP_DEBUG */
73217885a7bSLuigi Rizzo 	return lease_idx;
73317885a7bSLuigi Rizzo }
73417885a7bSLuigi Rizzo 
73517885a7bSLuigi Rizzo /*
7364bf50f18SLuigi Rizzo  *
737f9790aebSLuigi Rizzo  * This flush routine supports only unicast and broadcast but a large
738f9790aebSLuigi Rizzo  * number of ports, and lets us replace the learn and dispatch functions.
739f9790aebSLuigi Rizzo  */
740f9790aebSLuigi Rizzo int
nm_vale_flush(struct nm_bdg_fwd * ft,u_int n,struct netmap_vp_adapter * na,u_int ring_nr)741b6e66be2SVincenzo Maffione nm_vale_flush(struct nm_bdg_fwd *ft, u_int n, struct netmap_vp_adapter *na,
742f9790aebSLuigi Rizzo 		u_int ring_nr)
743f9790aebSLuigi Rizzo {
744b6e66be2SVincenzo Maffione 	struct nm_vale_q *dst_ents, *brddst;
745f9790aebSLuigi Rizzo 	uint16_t num_dsts = 0, *dsts;
746f9790aebSLuigi Rizzo 	struct nm_bridge *b = na->na_bdg;
74737e3a6d3SLuigi Rizzo 	u_int i, me = na->bdg_port;
748f9790aebSLuigi Rizzo 
749f9790aebSLuigi Rizzo 	/*
750f9790aebSLuigi Rizzo 	 * The work area (pointed by ft) is followed by an array of
751f9790aebSLuigi Rizzo 	 * pointers to queues , dst_ents; there are NM_BDG_MAXRINGS
752f9790aebSLuigi Rizzo 	 * queues per port plus one for the broadcast traffic.
753f9790aebSLuigi Rizzo 	 * Then we have an array of destination indexes.
754f9790aebSLuigi Rizzo 	 */
755b6e66be2SVincenzo Maffione 	dst_ents = (struct nm_vale_q *)(ft + NM_BDG_BATCH_MAX);
756f9790aebSLuigi Rizzo 	dsts = (uint16_t *)(dst_ents + NM_BDG_MAXPORTS * NM_BDG_MAXRINGS + 1);
757f9790aebSLuigi Rizzo 
758f9790aebSLuigi Rizzo 	/* first pass: find a destination for each packet in the batch */
759f9790aebSLuigi Rizzo 	for (i = 0; likely(i < n); i += ft[i].ft_frags) {
760f9790aebSLuigi Rizzo 		uint8_t dst_ring = ring_nr; /* default, same ring as origin */
761f9790aebSLuigi Rizzo 		uint16_t dst_port, d_i;
762b6e66be2SVincenzo Maffione 		struct nm_vale_q *d;
7632ff91c17SVincenzo Maffione 		struct nm_bdg_fwd *start_ft = NULL;
764f9790aebSLuigi Rizzo 
76575f4f3edSVincenzo Maffione 		nm_prdis("slot %d frags %d", i, ft[i].ft_frags);
7662ff91c17SVincenzo Maffione 
7672ff91c17SVincenzo Maffione 		if (na->up.virt_hdr_len < ft[i].ft_len) {
7682ff91c17SVincenzo Maffione 			ft[i].ft_offset = na->up.virt_hdr_len;
7692ff91c17SVincenzo Maffione 			start_ft = &ft[i];
7702ff91c17SVincenzo Maffione 		} else if (na->up.virt_hdr_len == ft[i].ft_len && ft[i].ft_flags & NS_MOREFRAG) {
7712ff91c17SVincenzo Maffione 			ft[i].ft_offset = ft[i].ft_len;
7722ff91c17SVincenzo Maffione 			start_ft = &ft[i+1];
7732ff91c17SVincenzo Maffione 		} else {
774f0ea3689SLuigi Rizzo 			/* Drop the packet if the virtio-net header is not into the first
7752ff91c17SVincenzo Maffione 			 * fragment nor at the very beginning of the second.
7762ff91c17SVincenzo Maffione 			 */
777f9790aebSLuigi Rizzo 			continue;
7782ff91c17SVincenzo Maffione 		}
779b6e66be2SVincenzo Maffione 		dst_port = b->bdg_ops.lookup(start_ft, &dst_ring, na, b->private_data);
780f9790aebSLuigi Rizzo 		if (netmap_verbose > 255)
78175f4f3edSVincenzo Maffione 			nm_prlim(5, "slot %d port %d -> %d", i, me, dst_port);
7824f80b14cSVincenzo Maffione 		if (dst_port >= NM_BDG_NOPORT)
783f9790aebSLuigi Rizzo 			continue; /* this packet is identified to be dropped */
784f9790aebSLuigi Rizzo 		else if (dst_port == NM_BDG_BROADCAST)
785f9790aebSLuigi Rizzo 			dst_ring = 0; /* broadcasts always go to ring 0 */
786f9790aebSLuigi Rizzo 		else if (unlikely(dst_port == me ||
787f9790aebSLuigi Rizzo 		    !b->bdg_ports[dst_port]))
788f9790aebSLuigi Rizzo 			continue;
789f9790aebSLuigi Rizzo 
790f9790aebSLuigi Rizzo 		/* get a position in the scratch pad */
791f9790aebSLuigi Rizzo 		d_i = dst_port * NM_BDG_MAXRINGS + dst_ring;
792f9790aebSLuigi Rizzo 		d = dst_ents + d_i;
793f9790aebSLuigi Rizzo 
794f9790aebSLuigi Rizzo 		/* append the first fragment to the list */
795f9790aebSLuigi Rizzo 		if (d->bq_head == NM_FT_NULL) { /* new destination */
796f9790aebSLuigi Rizzo 			d->bq_head = d->bq_tail = i;
797f9790aebSLuigi Rizzo 			/* remember this position to be scanned later */
798f9790aebSLuigi Rizzo 			if (dst_port != NM_BDG_BROADCAST)
799f9790aebSLuigi Rizzo 				dsts[num_dsts++] = d_i;
800f9790aebSLuigi Rizzo 		} else {
801f9790aebSLuigi Rizzo 			ft[d->bq_tail].ft_next = i;
802f9790aebSLuigi Rizzo 			d->bq_tail = i;
803f9790aebSLuigi Rizzo 		}
804f9790aebSLuigi Rizzo 		d->bq_len += ft[i].ft_frags;
805f9790aebSLuigi Rizzo 	}
806f9790aebSLuigi Rizzo 
807f9790aebSLuigi Rizzo 	/*
808f9790aebSLuigi Rizzo 	 * Broadcast traffic goes to ring 0 on all destinations.
809f9790aebSLuigi Rizzo 	 * So we need to add these rings to the list of ports to scan.
810f9790aebSLuigi Rizzo 	 */
811f9790aebSLuigi Rizzo 	brddst = dst_ents + NM_BDG_BROADCAST * NM_BDG_MAXRINGS;
812f9790aebSLuigi Rizzo 	if (brddst->bq_head != NM_FT_NULL) {
81337e3a6d3SLuigi Rizzo 		u_int j;
814f9790aebSLuigi Rizzo 		for (j = 0; likely(j < b->bdg_active_ports); j++) {
815f9790aebSLuigi Rizzo 			uint16_t d_i;
816f9790aebSLuigi Rizzo 			i = b->bdg_port_index[j];
817f9790aebSLuigi Rizzo 			if (unlikely(i == me))
818f9790aebSLuigi Rizzo 				continue;
819f9790aebSLuigi Rizzo 			d_i = i * NM_BDG_MAXRINGS;
820f9790aebSLuigi Rizzo 			if (dst_ents[d_i].bq_head == NM_FT_NULL)
821f9790aebSLuigi Rizzo 				dsts[num_dsts++] = d_i;
822f9790aebSLuigi Rizzo 		}
823f9790aebSLuigi Rizzo 	}
824f9790aebSLuigi Rizzo 
82575f4f3edSVincenzo Maffione 	nm_prdis(5, "pass 1 done %d pkts %d dsts", n, num_dsts);
8264bf50f18SLuigi Rizzo 	/* second pass: scan destinations */
827f9790aebSLuigi Rizzo 	for (i = 0; i < num_dsts; i++) {
828f9790aebSLuigi Rizzo 		struct netmap_vp_adapter *dst_na;
829f9790aebSLuigi Rizzo 		struct netmap_kring *kring;
830f9790aebSLuigi Rizzo 		struct netmap_ring *ring;
831f0ea3689SLuigi Rizzo 		u_int dst_nr, lim, j, d_i, next, brd_next;
832f9790aebSLuigi Rizzo 		u_int needed, howmany;
833f9790aebSLuigi Rizzo 		int retry = netmap_txsync_retry;
834b6e66be2SVincenzo Maffione 		struct nm_vale_q *d;
835f9790aebSLuigi Rizzo 		uint32_t my_start = 0, lease_idx = 0;
836f9790aebSLuigi Rizzo 		int nrings;
837f0ea3689SLuigi Rizzo 		int virt_hdr_mismatch = 0;
838f9790aebSLuigi Rizzo 
839f9790aebSLuigi Rizzo 		d_i = dsts[i];
84075f4f3edSVincenzo Maffione 		nm_prdis("second pass %d port %d", i, d_i);
841f9790aebSLuigi Rizzo 		d = dst_ents + d_i;
842f9790aebSLuigi Rizzo 		// XXX fix the division
843f9790aebSLuigi Rizzo 		dst_na = b->bdg_ports[d_i/NM_BDG_MAXRINGS];
844f9790aebSLuigi Rizzo 		/* protect from the lookup function returning an inactive
845f9790aebSLuigi Rizzo 		 * destination port
846f9790aebSLuigi Rizzo 		 */
847f9790aebSLuigi Rizzo 		if (unlikely(dst_na == NULL))
848f9790aebSLuigi Rizzo 			goto cleanup;
849f9790aebSLuigi Rizzo 		if (dst_na->up.na_flags & NAF_SW_ONLY)
850f9790aebSLuigi Rizzo 			goto cleanup;
851f9790aebSLuigi Rizzo 		/*
852f9790aebSLuigi Rizzo 		 * The interface may be in !netmap mode in two cases:
853f9790aebSLuigi Rizzo 		 * - when na is attached but not activated yet;
854f9790aebSLuigi Rizzo 		 * - when na is being deactivated but is still attached.
855f9790aebSLuigi Rizzo 		 */
8564bf50f18SLuigi Rizzo 		if (unlikely(!nm_netmap_on(&dst_na->up))) {
85775f4f3edSVincenzo Maffione 			nm_prdis("not in netmap mode!");
858f9790aebSLuigi Rizzo 			goto cleanup;
859f9790aebSLuigi Rizzo 		}
860f9790aebSLuigi Rizzo 
861f9790aebSLuigi Rizzo 		/* there is at least one either unicast or broadcast packet */
862f9790aebSLuigi Rizzo 		brd_next = brddst->bq_head;
863f9790aebSLuigi Rizzo 		next = d->bq_head;
864f9790aebSLuigi Rizzo 		/* we need to reserve this many slots. If fewer are
865f9790aebSLuigi Rizzo 		 * available, some packets will be dropped.
866a6d768d8SVincenzo Maffione 		 * Packets may have multiple fragments, so
867f9790aebSLuigi Rizzo 		 * there is a chance that we may not use all of the slots
868f9790aebSLuigi Rizzo 		 * we have claimed, so we will need to handle the leftover
869f9790aebSLuigi Rizzo 		 * ones when we regain the lock.
870f9790aebSLuigi Rizzo 		 */
871f9790aebSLuigi Rizzo 		needed = d->bq_len + brddst->bq_len;
872f9790aebSLuigi Rizzo 
87337e3a6d3SLuigi Rizzo 		if (unlikely(dst_na->up.virt_hdr_len != na->up.virt_hdr_len)) {
874c3e9b4dbSLuiz Otavio O Souza 			if (netmap_verbose) {
87575f4f3edSVincenzo Maffione 				nm_prlim(3, "virt_hdr_mismatch, src %d dst %d", na->up.virt_hdr_len,
87637e3a6d3SLuigi Rizzo 						dst_na->up.virt_hdr_len);
877c3e9b4dbSLuiz Otavio O Souza 			}
878f0ea3689SLuigi Rizzo 			/* There is a virtio-net header/offloadings mismatch between
879f0ea3689SLuigi Rizzo 			 * source and destination. The slower mismatch datapath will
880f0ea3689SLuigi Rizzo 			 * be used to cope with all the mismatches.
881f0ea3689SLuigi Rizzo 			 */
882f0ea3689SLuigi Rizzo 			virt_hdr_mismatch = 1;
883f0ea3689SLuigi Rizzo 			if (dst_na->mfs < na->mfs) {
884f0ea3689SLuigi Rizzo 				/* We may need to do segmentation offloadings, and so
885f0ea3689SLuigi Rizzo 				 * we may need a number of destination slots greater
886f0ea3689SLuigi Rizzo 				 * than the number of input slots ('needed').
887f0ea3689SLuigi Rizzo 				 * We look for the smallest integer 'x' which satisfies:
888f0ea3689SLuigi Rizzo 				 *	needed * na->mfs + x * H <= x * na->mfs
889f0ea3689SLuigi Rizzo 				 * where 'H' is the length of the longest header that may
890f0ea3689SLuigi Rizzo 				 * be replicated in the segmentation process (e.g. for
891f0ea3689SLuigi Rizzo 				 * TCPv4 we must account for ethernet header, IP header
892f0ea3689SLuigi Rizzo 				 * and TCPv4 header).
893f0ea3689SLuigi Rizzo 				 */
8944f80b14cSVincenzo Maffione 				KASSERT(dst_na->mfs > 0, ("vpna->mfs is 0"));
895f0ea3689SLuigi Rizzo 				needed = (needed * na->mfs) /
896f0ea3689SLuigi Rizzo 						(dst_na->mfs - WORST_CASE_GSO_HEADER) + 1;
89775f4f3edSVincenzo Maffione 				nm_prdis(3, "srcmtu=%u, dstmtu=%u, x=%u", na->mfs, dst_na->mfs, needed);
898f0ea3689SLuigi Rizzo 			}
899f0ea3689SLuigi Rizzo 		}
900f0ea3689SLuigi Rizzo 
90175f4f3edSVincenzo Maffione 		nm_prdis(5, "pass 2 dst %d is %x %s",
90206f6997eSVincenzo Maffione 			i, d_i, nm_is_bwrap(&dst_na->up) ? "nic/host" : "virtual");
903f9790aebSLuigi Rizzo 		dst_nr = d_i & (NM_BDG_MAXRINGS-1);
904f9790aebSLuigi Rizzo 		nrings = dst_na->up.num_rx_rings;
905f9790aebSLuigi Rizzo 		if (dst_nr >= nrings)
906f9790aebSLuigi Rizzo 			dst_nr = dst_nr % nrings;
9072ff91c17SVincenzo Maffione 		kring = dst_na->up.rx_rings[dst_nr];
908f9790aebSLuigi Rizzo 		ring = kring->ring;
9094f80b14cSVincenzo Maffione 		/* the destination ring may have not been opened for RX */
9104f80b14cSVincenzo Maffione 		if (unlikely(ring == NULL || kring->nr_mode != NKR_NETMAP_ON))
9114f80b14cSVincenzo Maffione 			goto cleanup;
912f9790aebSLuigi Rizzo 		lim = kring->nkr_num_slots - 1;
913f9790aebSLuigi Rizzo 
914f9790aebSLuigi Rizzo retry:
915f9790aebSLuigi Rizzo 
916f0ea3689SLuigi Rizzo 		if (dst_na->retry && retry) {
917f0ea3689SLuigi Rizzo 			/* try to get some free slot from the previous run */
918b6e66be2SVincenzo Maffione 			kring->nm_notify(kring, NAF_FORCE_RECLAIM);
9194bf50f18SLuigi Rizzo 			/* actually useful only for bwraps, since there
9204bf50f18SLuigi Rizzo 			 * the notify will trigger a txsync on the hwna. VALE ports
9214bf50f18SLuigi Rizzo 			 * have dst_na->retry == 0
9224bf50f18SLuigi Rizzo 			 */
923f0ea3689SLuigi Rizzo 		}
924f9790aebSLuigi Rizzo 		/* reserve the buffers in the queue and an entry
925f9790aebSLuigi Rizzo 		 * to report completion, and drop lock.
926f9790aebSLuigi Rizzo 		 * XXX this might become a helper function.
927f9790aebSLuigi Rizzo 		 */
928f9790aebSLuigi Rizzo 		mtx_lock(&kring->q_lock);
929f9790aebSLuigi Rizzo 		if (kring->nkr_stopped) {
930f9790aebSLuigi Rizzo 			mtx_unlock(&kring->q_lock);
931f9790aebSLuigi Rizzo 			goto cleanup;
932f9790aebSLuigi Rizzo 		}
933f9790aebSLuigi Rizzo 		my_start = j = kring->nkr_hwlease;
934f9790aebSLuigi Rizzo 		howmany = nm_kr_space(kring, 1);
935f9790aebSLuigi Rizzo 		if (needed < howmany)
936f9790aebSLuigi Rizzo 			howmany = needed;
937f9790aebSLuigi Rizzo 		lease_idx = nm_kr_lease(kring, howmany, 1);
938f9790aebSLuigi Rizzo 		mtx_unlock(&kring->q_lock);
939f9790aebSLuigi Rizzo 
940f9790aebSLuigi Rizzo 		/* only retry if we need more than available slots */
941f9790aebSLuigi Rizzo 		if (retry && needed <= howmany)
942f9790aebSLuigi Rizzo 			retry = 0;
943f9790aebSLuigi Rizzo 
944f9790aebSLuigi Rizzo 		/* copy to the destination queue */
945f9790aebSLuigi Rizzo 		while (howmany > 0) {
946f9790aebSLuigi Rizzo 			struct netmap_slot *slot;
947f9790aebSLuigi Rizzo 			struct nm_bdg_fwd *ft_p, *ft_end;
948f9790aebSLuigi Rizzo 			u_int cnt;
949f9790aebSLuigi Rizzo 
950f9790aebSLuigi Rizzo 			/* find the queue from which we pick next packet.
951f9790aebSLuigi Rizzo 			 * NM_FT_NULL is always higher than valid indexes
952f9790aebSLuigi Rizzo 			 * so we never dereference it if the other list
953f9790aebSLuigi Rizzo 			 * has packets (and if both are empty we never
954f9790aebSLuigi Rizzo 			 * get here).
955f9790aebSLuigi Rizzo 			 */
956f9790aebSLuigi Rizzo 			if (next < brd_next) {
957f9790aebSLuigi Rizzo 				ft_p = ft + next;
958f9790aebSLuigi Rizzo 				next = ft_p->ft_next;
959f9790aebSLuigi Rizzo 			} else { /* insert broadcast */
960f9790aebSLuigi Rizzo 				ft_p = ft + brd_next;
961f9790aebSLuigi Rizzo 				brd_next = ft_p->ft_next;
962f9790aebSLuigi Rizzo 			}
963f9790aebSLuigi Rizzo 			cnt = ft_p->ft_frags; // cnt > 0
964f9790aebSLuigi Rizzo 			if (unlikely(cnt > howmany))
965f9790aebSLuigi Rizzo 			    break; /* no more space */
966f9790aebSLuigi Rizzo 			if (netmap_verbose && cnt > 1)
96775f4f3edSVincenzo Maffione 				nm_prlim(5, "rx %d frags to %d", cnt, j);
968f9790aebSLuigi Rizzo 			ft_end = ft_p + cnt;
969f0ea3689SLuigi Rizzo 			if (unlikely(virt_hdr_mismatch)) {
970f0ea3689SLuigi Rizzo 				bdg_mismatch_datapath(na, dst_na, ft_p, ring, &j, lim, &howmany);
971f0ea3689SLuigi Rizzo 			} else {
972f0ea3689SLuigi Rizzo 				howmany -= cnt;
973f9790aebSLuigi Rizzo 				do {
974f9790aebSLuigi Rizzo 					char *dst, *src = ft_p->ft_buf;
975f9790aebSLuigi Rizzo 					size_t copy_len = ft_p->ft_len, dst_len = copy_len;
976a6d768d8SVincenzo Maffione 					uintptr_t src_cb;
977a6d768d8SVincenzo Maffione 					uint64_t dstoff, dstoff_cb;
978a6d768d8SVincenzo Maffione 					int src_co, dst_co;
979a6d768d8SVincenzo Maffione 					const uintptr_t mask = NM_BUF_ALIGN - 1;
980f9790aebSLuigi Rizzo 
981f9790aebSLuigi Rizzo 					slot = &ring->slot[j];
9824bf50f18SLuigi Rizzo 					dst = NMB(&dst_na->up, slot);
983a6d768d8SVincenzo Maffione 					dstoff = nm_get_offset(kring, slot);
984a6d768d8SVincenzo Maffione 					dstoff_cb = dstoff & ~mask;
985a6d768d8SVincenzo Maffione 					src_cb = ((uintptr_t)src) & ~mask;
986a6d768d8SVincenzo Maffione 					src_co = ((uintptr_t)src) & mask;
987a6d768d8SVincenzo Maffione 					dst_co = ((uintptr_t)(dst + dstoff)) & mask;
988a6d768d8SVincenzo Maffione 					if (dst_co < src_co) {
989a6d768d8SVincenzo Maffione 						dstoff_cb += NM_BUF_ALIGN;
990a6d768d8SVincenzo Maffione 					}
991a6d768d8SVincenzo Maffione 					dstoff = dstoff_cb + src_co;
992a6d768d8SVincenzo Maffione 					copy_len += src_co;
993f9790aebSLuigi Rizzo 
99475f4f3edSVincenzo Maffione 					nm_prdis("send [%d] %d(%d) bytes at %s:%d",
99517885a7bSLuigi Rizzo 							i, (int)copy_len, (int)dst_len,
996a6d768d8SVincenzo Maffione 							NM_IFPNAME(dst_ifp), j);
997f9790aebSLuigi Rizzo 
998a6d768d8SVincenzo Maffione 					if (unlikely(dstoff > NETMAP_BUF_SIZE(&dst_na->up) ||
999a6d768d8SVincenzo Maffione 				                     dst_len > NETMAP_BUF_SIZE(&dst_na->up) - dstoff)) {
1000a6d768d8SVincenzo Maffione 						nm_prlim(5, "dropping packet/fragment of len %zu, dest offset %llu",
1001a6d768d8SVincenzo Maffione 								dst_len, (unsigned long long)dstoff);
1002a6d768d8SVincenzo Maffione 						copy_len = dst_len = 0;
1003a6d768d8SVincenzo Maffione 						dstoff = nm_get_offset(kring, slot);
1004e31c6ec7SLuigi Rizzo 					}
1005a6d768d8SVincenzo Maffione 
1006f9790aebSLuigi Rizzo 					if (ft_p->ft_flags & NS_INDIRECT) {
1007f9790aebSLuigi Rizzo 						if (copyin(src, dst, copy_len)) {
1008f9790aebSLuigi Rizzo 							// invalid user pointer, pretend len is 0
1009f9790aebSLuigi Rizzo 							dst_len = 0;
1010f9790aebSLuigi Rizzo 						}
1011f9790aebSLuigi Rizzo 					} else {
1012f9790aebSLuigi Rizzo 						//memcpy(dst, src, copy_len);
1013a6d768d8SVincenzo Maffione 						pkt_copy((char *)src_cb, dst + dstoff_cb, (int)copy_len);
1014f9790aebSLuigi Rizzo 					}
1015f9790aebSLuigi Rizzo 					slot->len = dst_len;
1016f9790aebSLuigi Rizzo 					slot->flags = (cnt << 8)| NS_MOREFRAG;
1017a6d768d8SVincenzo Maffione 					nm_write_offset(kring, slot, dstoff);
1018f9790aebSLuigi Rizzo 					j = nm_next(j, lim);
1019f0ea3689SLuigi Rizzo 					needed--;
1020f9790aebSLuigi Rizzo 					ft_p++;
1021f9790aebSLuigi Rizzo 				} while (ft_p != ft_end);
1022f9790aebSLuigi Rizzo 				slot->flags = (cnt << 8); /* clear flag on last entry */
1023f0ea3689SLuigi Rizzo 			}
1024f9790aebSLuigi Rizzo 			/* are we done ? */
1025f9790aebSLuigi Rizzo 			if (next == NM_FT_NULL && brd_next == NM_FT_NULL)
1026f9790aebSLuigi Rizzo 				break;
1027f9790aebSLuigi Rizzo 		}
1028f9790aebSLuigi Rizzo 		{
1029f9790aebSLuigi Rizzo 		    /* current position */
1030f9790aebSLuigi Rizzo 		    uint32_t *p = kring->nkr_leases; /* shorthand */
1031f9790aebSLuigi Rizzo 		    uint32_t update_pos;
1032f9790aebSLuigi Rizzo 		    int still_locked = 1;
1033f9790aebSLuigi Rizzo 
1034f9790aebSLuigi Rizzo 		    mtx_lock(&kring->q_lock);
1035f9790aebSLuigi Rizzo 		    if (unlikely(howmany > 0)) {
1036f9790aebSLuigi Rizzo 			/* not used all bufs. If i am the last one
1037f9790aebSLuigi Rizzo 			 * i can recover the slots, otherwise must
1038f9790aebSLuigi Rizzo 			 * fill them with 0 to mark empty packets.
1039f9790aebSLuigi Rizzo 			 */
104075f4f3edSVincenzo Maffione 			nm_prdis("leftover %d bufs", howmany);
1041f9790aebSLuigi Rizzo 			if (nm_next(lease_idx, lim) == kring->nkr_lease_idx) {
1042f9790aebSLuigi Rizzo 			    /* yes i am the last one */
104375f4f3edSVincenzo Maffione 			    nm_prdis("roll back nkr_hwlease to %d", j);
1044f9790aebSLuigi Rizzo 			    kring->nkr_hwlease = j;
1045f9790aebSLuigi Rizzo 			} else {
1046f9790aebSLuigi Rizzo 			    while (howmany-- > 0) {
1047f9790aebSLuigi Rizzo 				ring->slot[j].len = 0;
1048f9790aebSLuigi Rizzo 				ring->slot[j].flags = 0;
1049f9790aebSLuigi Rizzo 				j = nm_next(j, lim);
1050f9790aebSLuigi Rizzo 			    }
1051f9790aebSLuigi Rizzo 			}
1052f9790aebSLuigi Rizzo 		    }
1053f9790aebSLuigi Rizzo 		    p[lease_idx] = j; /* report I am done */
1054f9790aebSLuigi Rizzo 
105517885a7bSLuigi Rizzo 		    update_pos = kring->nr_hwtail;
1056f9790aebSLuigi Rizzo 
1057f9790aebSLuigi Rizzo 		    if (my_start == update_pos) {
1058f9790aebSLuigi Rizzo 			/* all slots before my_start have been reported,
1059f9790aebSLuigi Rizzo 			 * so scan subsequent leases to see if other ranges
1060f9790aebSLuigi Rizzo 			 * have been completed, and to a selwakeup or txsync.
1061f9790aebSLuigi Rizzo 		         */
1062f9790aebSLuigi Rizzo 			while (lease_idx != kring->nkr_lease_idx &&
1063f9790aebSLuigi Rizzo 				p[lease_idx] != NR_NOSLOT) {
1064f9790aebSLuigi Rizzo 			    j = p[lease_idx];
1065f9790aebSLuigi Rizzo 			    p[lease_idx] = NR_NOSLOT;
1066f9790aebSLuigi Rizzo 			    lease_idx = nm_next(lease_idx, lim);
1067f9790aebSLuigi Rizzo 			}
1068f9790aebSLuigi Rizzo 			/* j is the new 'write' position. j != my_start
1069f9790aebSLuigi Rizzo 			 * means there are new buffers to report
1070f9790aebSLuigi Rizzo 			 */
1071f9790aebSLuigi Rizzo 			if (likely(j != my_start)) {
107217885a7bSLuigi Rizzo 				kring->nr_hwtail = j;
1073f9790aebSLuigi Rizzo 				still_locked = 0;
1074f9790aebSLuigi Rizzo 				mtx_unlock(&kring->q_lock);
1075847bf383SLuigi Rizzo 				kring->nm_notify(kring, 0);
10764bf50f18SLuigi Rizzo 				/* this is netmap_notify for VALE ports and
10774bf50f18SLuigi Rizzo 				 * netmap_bwrap_notify for bwrap. The latter will
10784bf50f18SLuigi Rizzo 				 * trigger a txsync on the underlying hwna
10794bf50f18SLuigi Rizzo 				 */
10804bf50f18SLuigi Rizzo 				if (dst_na->retry && retry--) {
10814bf50f18SLuigi Rizzo 					/* XXX this is going to call nm_notify again.
10824bf50f18SLuigi Rizzo 					 * Only useful for bwrap in virtual machines
10834bf50f18SLuigi Rizzo 					 */
1084f9790aebSLuigi Rizzo 					goto retry;
1085f9790aebSLuigi Rizzo 				}
1086f9790aebSLuigi Rizzo 			}
10874bf50f18SLuigi Rizzo 		    }
1088f9790aebSLuigi Rizzo 		    if (still_locked)
1089f9790aebSLuigi Rizzo 			mtx_unlock(&kring->q_lock);
1090f9790aebSLuigi Rizzo 		}
1091f9790aebSLuigi Rizzo cleanup:
1092f9790aebSLuigi Rizzo 		d->bq_head = d->bq_tail = NM_FT_NULL; /* cleanup */
1093f9790aebSLuigi Rizzo 		d->bq_len = 0;
1094f9790aebSLuigi Rizzo 	}
1095f9790aebSLuigi Rizzo 	brddst->bq_head = brddst->bq_tail = NM_FT_NULL; /* cleanup */
1096f9790aebSLuigi Rizzo 	brddst->bq_len = 0;
1097f9790aebSLuigi Rizzo 	return 0;
1098f9790aebSLuigi Rizzo }
1099f9790aebSLuigi Rizzo 
11004bf50f18SLuigi Rizzo /* nm_txsync callback for VALE ports */
1101f9790aebSLuigi Rizzo static int
netmap_vale_vp_txsync(struct netmap_kring * kring,int flags)1102b6e66be2SVincenzo Maffione netmap_vale_vp_txsync(struct netmap_kring *kring, int flags)
1103f9790aebSLuigi Rizzo {
11044bf50f18SLuigi Rizzo 	struct netmap_vp_adapter *na =
11054bf50f18SLuigi Rizzo 		(struct netmap_vp_adapter *)kring->na;
110617885a7bSLuigi Rizzo 	u_int done;
110717885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
1108847bf383SLuigi Rizzo 	u_int const head = kring->rhead;
1109f9790aebSLuigi Rizzo 
1110f9790aebSLuigi Rizzo 	if (bridge_batch <= 0) { /* testing only */
1111847bf383SLuigi Rizzo 		done = head; // used all
1112f9790aebSLuigi Rizzo 		goto done;
1113f9790aebSLuigi Rizzo 	}
11144bf50f18SLuigi Rizzo 	if (!na->na_bdg) {
1115847bf383SLuigi Rizzo 		done = head;
11164bf50f18SLuigi Rizzo 		goto done;
11174bf50f18SLuigi Rizzo 	}
1118f9790aebSLuigi Rizzo 	if (bridge_batch > NM_BDG_BATCH)
1119f9790aebSLuigi Rizzo 		bridge_batch = NM_BDG_BATCH;
1120f9790aebSLuigi Rizzo 
1121b6e66be2SVincenzo Maffione 	done = nm_vale_preflush(kring, head);
1122f9790aebSLuigi Rizzo done:
1123847bf383SLuigi Rizzo 	if (done != head)
1124b6e66be2SVincenzo Maffione 		nm_prerr("early break at %d/ %d, tail %d", done, head, kring->nr_hwtail);
112517885a7bSLuigi Rizzo 	/*
112617885a7bSLuigi Rizzo 	 * packets between 'done' and 'cur' are left unsent.
112717885a7bSLuigi Rizzo 	 */
112817885a7bSLuigi Rizzo 	kring->nr_hwcur = done;
112917885a7bSLuigi Rizzo 	kring->nr_hwtail = nm_prev(done, lim);
1130b6e66be2SVincenzo Maffione 	if (netmap_debug & NM_DEBUG_TXSYNC)
1131b6e66be2SVincenzo Maffione 		nm_prinf("%s ring %d flags %d", na->up.name, kring->ring_id, flags);
1132f9790aebSLuigi Rizzo 	return 0;
1133f9790aebSLuigi Rizzo }
1134f9790aebSLuigi Rizzo 
1135f9790aebSLuigi Rizzo 
11364bf50f18SLuigi Rizzo /* create a netmap_vp_adapter that describes a VALE port.
11374bf50f18SLuigi Rizzo  * Only persistent VALE ports have a non-null ifp.
11384bf50f18SLuigi Rizzo  */
11394bf50f18SLuigi Rizzo static int
netmap_vale_vp_create(struct nmreq_header * hdr,if_t ifp,struct netmap_mem_d * nmd,struct netmap_vp_adapter ** ret)1140e330262fSJustin Hibbits netmap_vale_vp_create(struct nmreq_header *hdr, if_t ifp,
11412ff91c17SVincenzo Maffione 		struct netmap_mem_d *nmd, struct netmap_vp_adapter **ret)
1142f9790aebSLuigi Rizzo {
1143cfa866f6SMatt Macy 	struct nmreq_register *req = (struct nmreq_register *)(uintptr_t)hdr->nr_body;
1144f9790aebSLuigi Rizzo 	struct netmap_vp_adapter *vpna;
1145f9790aebSLuigi Rizzo 	struct netmap_adapter *na;
1146c3e9b4dbSLuiz Otavio O Souza 	int error = 0;
1147f0ea3689SLuigi Rizzo 	u_int npipes = 0;
11482ff91c17SVincenzo Maffione 	u_int extrabufs = 0;
11492ff91c17SVincenzo Maffione 
11502ff91c17SVincenzo Maffione 	if (hdr->nr_reqtype != NETMAP_REQ_REGISTER) {
11512ff91c17SVincenzo Maffione 		return EINVAL;
11522ff91c17SVincenzo Maffione 	}
1153f9790aebSLuigi Rizzo 
1154c3e9b4dbSLuiz Otavio O Souza 	vpna = nm_os_malloc(sizeof(*vpna));
1155f9790aebSLuigi Rizzo 	if (vpna == NULL)
1156f9790aebSLuigi Rizzo 		return ENOMEM;
1157f9790aebSLuigi Rizzo 
1158f9790aebSLuigi Rizzo  	na = &vpna->up;
1159f9790aebSLuigi Rizzo 
1160f9790aebSLuigi Rizzo 	na->ifp = ifp;
1161b6e66be2SVincenzo Maffione 	strlcpy(na->name, hdr->nr_name, sizeof(na->name));
1162f9790aebSLuigi Rizzo 
1163f9790aebSLuigi Rizzo 	/* bound checking */
11642ff91c17SVincenzo Maffione 	na->num_tx_rings = req->nr_tx_rings;
1165f9790aebSLuigi Rizzo 	nm_bound_var(&na->num_tx_rings, 1, 1, NM_BDG_MAXRINGS, NULL);
11662ff91c17SVincenzo Maffione 	req->nr_tx_rings = na->num_tx_rings; /* write back */
11672ff91c17SVincenzo Maffione 	na->num_rx_rings = req->nr_rx_rings;
1168f9790aebSLuigi Rizzo 	nm_bound_var(&na->num_rx_rings, 1, 1, NM_BDG_MAXRINGS, NULL);
11692ff91c17SVincenzo Maffione 	req->nr_rx_rings = na->num_rx_rings; /* write back */
11702ff91c17SVincenzo Maffione 	nm_bound_var(&req->nr_tx_slots, NM_BRIDGE_RINGSIZE,
1171f9790aebSLuigi Rizzo 			1, NM_BDG_MAXSLOTS, NULL);
11722ff91c17SVincenzo Maffione 	na->num_tx_desc = req->nr_tx_slots;
11732ff91c17SVincenzo Maffione 	nm_bound_var(&req->nr_rx_slots, NM_BRIDGE_RINGSIZE,
1174f9790aebSLuigi Rizzo 			1, NM_BDG_MAXSLOTS, NULL);
1175f0ea3689SLuigi Rizzo 	/* validate number of pipes. We want at least 1,
1176f0ea3689SLuigi Rizzo 	 * but probably can do with some more.
1177f0ea3689SLuigi Rizzo 	 * So let's use 2 as default (when 0 is supplied)
1178f0ea3689SLuigi Rizzo 	 */
1179f0ea3689SLuigi Rizzo 	nm_bound_var(&npipes, 2, 1, NM_MAXPIPES, NULL);
1180f0ea3689SLuigi Rizzo 	/* validate extra bufs */
1181b6e66be2SVincenzo Maffione 	extrabufs = req->nr_extra_bufs;
11822ff91c17SVincenzo Maffione 	nm_bound_var(&extrabufs, 0, 0,
1183f0ea3689SLuigi Rizzo 			128*NM_BDG_MAXSLOTS, NULL);
11842ff91c17SVincenzo Maffione 	req->nr_extra_bufs = extrabufs; /* write back */
11852ff91c17SVincenzo Maffione 	na->num_rx_desc = req->nr_rx_slots;
11864f80b14cSVincenzo Maffione 	/* Set the mfs to a default value, as it is needed on the VALE
11874f80b14cSVincenzo Maffione 	 * mismatch datapath. XXX We should set it according to the MTU
11884f80b14cSVincenzo Maffione 	 * known to the kernel. */
11894f80b14cSVincenzo Maffione 	vpna->mfs = NM_BDG_MFS_DEFAULT;
1190847bf383SLuigi Rizzo 	vpna->last_smac = ~0llu;
1191f0ea3689SLuigi Rizzo 	/*if (vpna->mfs > netmap_buf_size)  TODO netmap_buf_size is zero??
1192f0ea3689SLuigi Rizzo 		vpna->mfs = netmap_buf_size; */
1193f0ea3689SLuigi Rizzo 	if (netmap_verbose)
1194b6e66be2SVincenzo Maffione 		nm_prinf("max frame size %u", vpna->mfs);
1195f9790aebSLuigi Rizzo 
1196a6d768d8SVincenzo Maffione 	na->na_flags |= (NAF_BDG_MAYSLEEP | NAF_OFFSETS);
119710b8ef3dSLuigi Rizzo 	/* persistent VALE ports look like hw devices
119810b8ef3dSLuigi Rizzo 	 * with a native netmap adapter
119910b8ef3dSLuigi Rizzo 	 */
120010b8ef3dSLuigi Rizzo 	if (ifp)
120110b8ef3dSLuigi Rizzo 		na->na_flags |= NAF_NATIVE;
1202b6e66be2SVincenzo Maffione 	na->nm_txsync = netmap_vale_vp_txsync;
1203b6e66be2SVincenzo Maffione 	na->nm_rxsync = netmap_vp_rxsync; /* use the one provided by bdg */
1204b6e66be2SVincenzo Maffione 	na->nm_register = netmap_vp_reg;  /* use the one provided by bdg */
1205b6e66be2SVincenzo Maffione 	na->nm_krings_create = netmap_vale_vp_krings_create;
1206b6e66be2SVincenzo Maffione 	na->nm_krings_delete = netmap_vale_vp_krings_delete;
1207b6e66be2SVincenzo Maffione 	na->nm_dtor = netmap_vale_vp_dtor;
120875f4f3edSVincenzo Maffione 	nm_prdis("nr_mem_id %d", req->nr_mem_id);
1209c3e9b4dbSLuiz Otavio O Souza 	na->nm_mem = nmd ?
1210c3e9b4dbSLuiz Otavio O Souza 		netmap_mem_get(nmd):
1211c3e9b4dbSLuiz Otavio O Souza 		netmap_mem_private_new(
1212f9790aebSLuigi Rizzo 			na->num_tx_rings, na->num_tx_desc,
1213f0ea3689SLuigi Rizzo 			na->num_rx_rings, na->num_rx_desc,
12142ff91c17SVincenzo Maffione 			req->nr_extra_bufs, npipes, &error);
1215f0ea3689SLuigi Rizzo 	if (na->nm_mem == NULL)
1216f0ea3689SLuigi Rizzo 		goto err;
1217b6e66be2SVincenzo Maffione 	na->nm_bdg_attach = netmap_vale_vp_bdg_attach;
1218f9790aebSLuigi Rizzo 	/* other nmd fields are set in the common routine */
1219f9790aebSLuigi Rizzo 	error = netmap_attach_common(na);
1220f0ea3689SLuigi Rizzo 	if (error)
1221f0ea3689SLuigi Rizzo 		goto err;
12224bf50f18SLuigi Rizzo 	*ret = vpna;
1223f0ea3689SLuigi Rizzo 	return 0;
1224f0ea3689SLuigi Rizzo 
1225f0ea3689SLuigi Rizzo err:
1226f0ea3689SLuigi Rizzo 	if (na->nm_mem != NULL)
1227c3e9b4dbSLuiz Otavio O Souza 		netmap_mem_put(na->nm_mem);
1228c3e9b4dbSLuiz Otavio O Souza 	nm_os_free(vpna);
1229f9790aebSLuigi Rizzo 	return error;
1230f9790aebSLuigi Rizzo }
1231f9790aebSLuigi Rizzo 
12322a7db7a6SVincenzo Maffione /* nm_bdg_attach callback for VALE ports
12332a7db7a6SVincenzo Maffione  * The na_vp port is this same netmap_adapter. There is no host port.
1234f9790aebSLuigi Rizzo  */
1235f9790aebSLuigi Rizzo static int
netmap_vale_vp_bdg_attach(const char * name,struct netmap_adapter * na,struct nm_bridge * b)1236b6e66be2SVincenzo Maffione netmap_vale_vp_bdg_attach(const char *name, struct netmap_adapter *na,
12372a7db7a6SVincenzo Maffione 		struct nm_bridge *b)
1238f9790aebSLuigi Rizzo {
12392a7db7a6SVincenzo Maffione 	struct netmap_vp_adapter *vpna = (struct netmap_vp_adapter *)na;
1240f9790aebSLuigi Rizzo 
1241b6e66be2SVincenzo Maffione 	if ((b->bdg_flags & NM_BDG_NEED_BWRAP) || vpna->na_bdg) {
12422a7db7a6SVincenzo Maffione 		return NM_NEED_BWRAP;
1243f9790aebSLuigi Rizzo 	}
12442a7db7a6SVincenzo Maffione 	na->na_vp = vpna;
1245b6e66be2SVincenzo Maffione 	strlcpy(na->name, name, sizeof(na->name));
12462a7db7a6SVincenzo Maffione 	na->na_hostvp = NULL;
1247f9790aebSLuigi Rizzo 	return 0;
1248f9790aebSLuigi Rizzo }
1249f9790aebSLuigi Rizzo 
1250f9790aebSLuigi Rizzo static int
netmap_vale_bwrap_krings_create(struct netmap_adapter * na)12512a7db7a6SVincenzo Maffione netmap_vale_bwrap_krings_create(struct netmap_adapter *na)
1252f9790aebSLuigi Rizzo {
1253cfa866f6SMatt Macy 	int error;
1254f9790aebSLuigi Rizzo 
12554bf50f18SLuigi Rizzo 	/* impersonate a netmap_vp_adapter */
1256b6e66be2SVincenzo Maffione 	error = netmap_vale_vp_krings_create(na);
1257f9790aebSLuigi Rizzo 	if (error)
1258f9790aebSLuigi Rizzo 		return error;
12592a7db7a6SVincenzo Maffione 	error = netmap_bwrap_krings_create_common(na);
1260f9790aebSLuigi Rizzo 	if (error) {
1261b6e66be2SVincenzo Maffione 		netmap_vale_vp_krings_delete(na);
12622a7db7a6SVincenzo Maffione 	}
126337e3a6d3SLuigi Rizzo 	return error;
1264f9790aebSLuigi Rizzo }
1265f9790aebSLuigi Rizzo 
1266f9790aebSLuigi Rizzo static void
netmap_vale_bwrap_krings_delete(struct netmap_adapter * na)12672a7db7a6SVincenzo Maffione netmap_vale_bwrap_krings_delete(struct netmap_adapter *na)
1268f9790aebSLuigi Rizzo {
12692a7db7a6SVincenzo Maffione 	netmap_bwrap_krings_delete_common(na);
1270b6e66be2SVincenzo Maffione 	netmap_vale_vp_krings_delete(na);
1271f9790aebSLuigi Rizzo }
1272f9790aebSLuigi Rizzo 
1273f9790aebSLuigi Rizzo static int
netmap_vale_bwrap_attach(const char * nr_name,struct netmap_adapter * hwna)12742a7db7a6SVincenzo Maffione netmap_vale_bwrap_attach(const char *nr_name, struct netmap_adapter *hwna)
1275f9790aebSLuigi Rizzo {
1276f9790aebSLuigi Rizzo 	struct netmap_bwrap_adapter *bna;
12774bf50f18SLuigi Rizzo 	struct netmap_adapter *na = NULL;
12784bf50f18SLuigi Rizzo 	struct netmap_adapter *hostna = NULL;
12792a7db7a6SVincenzo Maffione 	int error;
1280f9790aebSLuigi Rizzo 
1281c3e9b4dbSLuiz Otavio O Souza 	bna = nm_os_malloc(sizeof(*bna));
12824bf50f18SLuigi Rizzo 	if (bna == NULL) {
1283f9790aebSLuigi Rizzo 		return ENOMEM;
12844bf50f18SLuigi Rizzo 	}
1285f9790aebSLuigi Rizzo 	na = &bna->up.up;
1286b6e66be2SVincenzo Maffione 	strlcpy(na->name, nr_name, sizeof(na->name));
128737e3a6d3SLuigi Rizzo 	na->nm_register = netmap_bwrap_reg;
1288b6e66be2SVincenzo Maffione 	na->nm_txsync = netmap_vale_vp_txsync;
1289f9790aebSLuigi Rizzo 	// na->nm_rxsync = netmap_bwrap_rxsync;
12902a7db7a6SVincenzo Maffione 	na->nm_krings_create = netmap_vale_bwrap_krings_create;
12912a7db7a6SVincenzo Maffione 	na->nm_krings_delete = netmap_vale_bwrap_krings_delete;
1292f9790aebSLuigi Rizzo 	na->nm_notify = netmap_bwrap_notify;
1293a6d768d8SVincenzo Maffione 	bna->nm_intr_notify = netmap_bwrap_intr_notify;
1294f9790aebSLuigi Rizzo 	bna->up.retry = 1; /* XXX maybe this should depend on the hwna */
12954f80b14cSVincenzo Maffione 	/* Set the mfs, needed on the VALE mismatch datapath. */
12964f80b14cSVincenzo Maffione 	bna->up.mfs = NM_BDG_MFS_DEFAULT;
1297f9790aebSLuigi Rizzo 
1298f0ea3689SLuigi Rizzo 	if (hwna->na_flags & NAF_HOST_RINGS) {
1299f9790aebSLuigi Rizzo 		hostna = &bna->host.up;
1300847bf383SLuigi Rizzo 		hostna->nm_notify = netmap_bwrap_notify;
13014f80b14cSVincenzo Maffione 		bna->host.mfs = NM_BDG_MFS_DEFAULT;
1302f0ea3689SLuigi Rizzo 	}
1303f9790aebSLuigi Rizzo 
13042a7db7a6SVincenzo Maffione 	error = netmap_bwrap_attach_common(na, hwna);
1305f9790aebSLuigi Rizzo 	if (error) {
1306c3e9b4dbSLuiz Otavio O Souza 		nm_os_free(bna);
13072a7db7a6SVincenzo Maffione 	}
1308f9790aebSLuigi Rizzo 	return error;
1309847bf383SLuigi Rizzo }
1310847bf383SLuigi Rizzo 
1311847bf383SLuigi Rizzo int
netmap_get_vale_na(struct nmreq_header * hdr,struct netmap_adapter ** na,struct netmap_mem_d * nmd,int create)13122a7db7a6SVincenzo Maffione netmap_get_vale_na(struct nmreq_header *hdr, struct netmap_adapter **na,
13132a7db7a6SVincenzo Maffione 		struct netmap_mem_d *nmd, int create)
1314847bf383SLuigi Rizzo {
13152a7db7a6SVincenzo Maffione 	return netmap_get_bdg_na(hdr, na, nmd, create, &vale_bdg_ops);
1316847bf383SLuigi Rizzo }
1317847bf383SLuigi Rizzo 
13182a7db7a6SVincenzo Maffione 
13192a7db7a6SVincenzo Maffione /* creates a persistent VALE port */
13202a7db7a6SVincenzo Maffione int
nm_vi_create(struct nmreq_header * hdr)13212a7db7a6SVincenzo Maffione nm_vi_create(struct nmreq_header *hdr)
1322847bf383SLuigi Rizzo {
13232a7db7a6SVincenzo Maffione 	struct nmreq_vale_newif *req =
13242a7db7a6SVincenzo Maffione 		(struct nmreq_vale_newif *)(uintptr_t)hdr->nr_body;
13252a7db7a6SVincenzo Maffione 	int error = 0;
13262a7db7a6SVincenzo Maffione 	/* Build a nmreq_register out of the nmreq_vale_newif,
13272a7db7a6SVincenzo Maffione 	 * so that we can call netmap_get_bdg_na(). */
13282a7db7a6SVincenzo Maffione 	struct nmreq_register regreq;
13292a7db7a6SVincenzo Maffione 	bzero(&regreq, sizeof(regreq));
13302a7db7a6SVincenzo Maffione 	regreq.nr_tx_slots = req->nr_tx_slots;
13312a7db7a6SVincenzo Maffione 	regreq.nr_rx_slots = req->nr_rx_slots;
13322a7db7a6SVincenzo Maffione 	regreq.nr_tx_rings = req->nr_tx_rings;
13332a7db7a6SVincenzo Maffione 	regreq.nr_rx_rings = req->nr_rx_rings;
13342a7db7a6SVincenzo Maffione 	regreq.nr_mem_id = req->nr_mem_id;
13352a7db7a6SVincenzo Maffione 	hdr->nr_reqtype = NETMAP_REQ_REGISTER;
13362a7db7a6SVincenzo Maffione 	hdr->nr_body = (uintptr_t)&regreq;
13372a7db7a6SVincenzo Maffione 	error = netmap_vi_create(hdr, 0 /* no autodelete */);
13382a7db7a6SVincenzo Maffione 	hdr->nr_reqtype = NETMAP_REQ_VALE_NEWIF;
13392a7db7a6SVincenzo Maffione 	hdr->nr_body = (uintptr_t)req;
13402a7db7a6SVincenzo Maffione 	/* Write back to the original struct. */
13412a7db7a6SVincenzo Maffione 	req->nr_tx_slots = regreq.nr_tx_slots;
13422a7db7a6SVincenzo Maffione 	req->nr_rx_slots = regreq.nr_rx_slots;
13432a7db7a6SVincenzo Maffione 	req->nr_tx_rings = regreq.nr_tx_rings;
13442a7db7a6SVincenzo Maffione 	req->nr_rx_rings = regreq.nr_rx_rings;
13452a7db7a6SVincenzo Maffione 	req->nr_mem_id = regreq.nr_mem_id;
13462a7db7a6SVincenzo Maffione 	return error;
1347f9790aebSLuigi Rizzo }
13482a7db7a6SVincenzo Maffione 
13492a7db7a6SVincenzo Maffione /* remove a persistent VALE port from the system */
13502a7db7a6SVincenzo Maffione int
nm_vi_destroy(const char * name)13512a7db7a6SVincenzo Maffione nm_vi_destroy(const char *name)
13522a7db7a6SVincenzo Maffione {
1353e330262fSJustin Hibbits 	if_t ifp;
13542a7db7a6SVincenzo Maffione 	struct netmap_vp_adapter *vpna;
13552a7db7a6SVincenzo Maffione 	int error;
13562a7db7a6SVincenzo Maffione 
13572a7db7a6SVincenzo Maffione 	ifp = ifunit_ref(name);
13582a7db7a6SVincenzo Maffione 	if (!ifp)
13592a7db7a6SVincenzo Maffione 		return ENXIO;
13602a7db7a6SVincenzo Maffione 	NMG_LOCK();
13612a7db7a6SVincenzo Maffione 	/* make sure this is actually a VALE port */
13622a7db7a6SVincenzo Maffione 	if (!NM_NA_VALID(ifp) || NA(ifp)->nm_register != netmap_vp_reg) {
13632a7db7a6SVincenzo Maffione 		error = EINVAL;
13642a7db7a6SVincenzo Maffione 		goto err;
13652a7db7a6SVincenzo Maffione 	}
13662a7db7a6SVincenzo Maffione 
13672a7db7a6SVincenzo Maffione 	vpna = (struct netmap_vp_adapter *)NA(ifp);
13682a7db7a6SVincenzo Maffione 
13692a7db7a6SVincenzo Maffione 	/* we can only destroy ports that were created via NETMAP_BDG_NEWIF */
13702a7db7a6SVincenzo Maffione 	if (vpna->autodelete) {
13712a7db7a6SVincenzo Maffione 		error = EINVAL;
13722a7db7a6SVincenzo Maffione 		goto err;
13732a7db7a6SVincenzo Maffione 	}
13742a7db7a6SVincenzo Maffione 
137545c67e8fSVincenzo Maffione 	/* also make sure that nobody is using the interface */
13762a7db7a6SVincenzo Maffione 	if (NETMAP_OWNED_BY_ANY(&vpna->up) ||
13772a7db7a6SVincenzo Maffione 	    vpna->up.na_refcount > 1 /* any ref besides the one in nm_vi_create()? */) {
13782a7db7a6SVincenzo Maffione 		error = EBUSY;
13792a7db7a6SVincenzo Maffione 		goto err;
13802a7db7a6SVincenzo Maffione 	}
13812a7db7a6SVincenzo Maffione 
13822a7db7a6SVincenzo Maffione 	NMG_UNLOCK();
13832a7db7a6SVincenzo Maffione 
1384b6e66be2SVincenzo Maffione 	if (netmap_verbose)
1385e330262fSJustin Hibbits 		nm_prinf("destroying a persistent vale interface %s", if_name(ifp));
13862a7db7a6SVincenzo Maffione 	/* Linux requires all the references are released
13872a7db7a6SVincenzo Maffione 	 * before unregister
13882a7db7a6SVincenzo Maffione 	 */
13892a7db7a6SVincenzo Maffione 	netmap_detach(ifp);
13902a7db7a6SVincenzo Maffione 	if_rele(ifp);
13912a7db7a6SVincenzo Maffione 	nm_os_vi_detach(ifp);
13922a7db7a6SVincenzo Maffione 	return 0;
13932a7db7a6SVincenzo Maffione 
13942a7db7a6SVincenzo Maffione err:
13952a7db7a6SVincenzo Maffione 	NMG_UNLOCK();
13962a7db7a6SVincenzo Maffione 	if_rele(ifp);
13972a7db7a6SVincenzo Maffione 	return error;
13982a7db7a6SVincenzo Maffione }
13992a7db7a6SVincenzo Maffione 
14002a7db7a6SVincenzo Maffione static int
nm_update_info(struct nmreq_register * req,struct netmap_adapter * na)14012a7db7a6SVincenzo Maffione nm_update_info(struct nmreq_register *req, struct netmap_adapter *na)
14022a7db7a6SVincenzo Maffione {
14032a7db7a6SVincenzo Maffione 	req->nr_rx_rings = na->num_rx_rings;
14042a7db7a6SVincenzo Maffione 	req->nr_tx_rings = na->num_tx_rings;
14052a7db7a6SVincenzo Maffione 	req->nr_rx_slots = na->num_rx_desc;
14062a7db7a6SVincenzo Maffione 	req->nr_tx_slots = na->num_tx_desc;
14072a7db7a6SVincenzo Maffione 	return netmap_mem_get_info(na->nm_mem, &req->nr_memsize, NULL,
14082a7db7a6SVincenzo Maffione 					&req->nr_mem_id);
14092a7db7a6SVincenzo Maffione }
14102a7db7a6SVincenzo Maffione 
14112a7db7a6SVincenzo Maffione 
14122a7db7a6SVincenzo Maffione /*
14132a7db7a6SVincenzo Maffione  * Create a virtual interface registered to the system.
14142a7db7a6SVincenzo Maffione  * The interface will be attached to a bridge later.
14152a7db7a6SVincenzo Maffione  */
14162a7db7a6SVincenzo Maffione int
netmap_vi_create(struct nmreq_header * hdr,int autodelete)14172a7db7a6SVincenzo Maffione netmap_vi_create(struct nmreq_header *hdr, int autodelete)
14182a7db7a6SVincenzo Maffione {
14192a7db7a6SVincenzo Maffione 	struct nmreq_register *req = (struct nmreq_register *)(uintptr_t)hdr->nr_body;
1420e330262fSJustin Hibbits 	if_t ifp;
14212a7db7a6SVincenzo Maffione 	struct netmap_vp_adapter *vpna;
14222a7db7a6SVincenzo Maffione 	struct netmap_mem_d *nmd = NULL;
14232a7db7a6SVincenzo Maffione 	int error;
14242a7db7a6SVincenzo Maffione 
14252a7db7a6SVincenzo Maffione 	if (hdr->nr_reqtype != NETMAP_REQ_REGISTER) {
14262a7db7a6SVincenzo Maffione 		return EINVAL;
14272a7db7a6SVincenzo Maffione 	}
14282a7db7a6SVincenzo Maffione 
14292a7db7a6SVincenzo Maffione 	/* don't include VALE prefix */
14302a7db7a6SVincenzo Maffione 	if (!strncmp(hdr->nr_name, NM_BDG_NAME, strlen(NM_BDG_NAME)))
14312a7db7a6SVincenzo Maffione 		return EINVAL;
14322a7db7a6SVincenzo Maffione 	if (strlen(hdr->nr_name) >= IFNAMSIZ) {
14332a7db7a6SVincenzo Maffione 		return EINVAL;
14342a7db7a6SVincenzo Maffione 	}
14352a7db7a6SVincenzo Maffione 	ifp = ifunit_ref(hdr->nr_name);
14362a7db7a6SVincenzo Maffione 	if (ifp) { /* already exist, cannot create new one */
14372a7db7a6SVincenzo Maffione 		error = EEXIST;
14382a7db7a6SVincenzo Maffione 		NMG_LOCK();
14392a7db7a6SVincenzo Maffione 		if (NM_NA_VALID(ifp)) {
14402a7db7a6SVincenzo Maffione 			int update_err = nm_update_info(req, NA(ifp));
14412a7db7a6SVincenzo Maffione 			if (update_err)
14422a7db7a6SVincenzo Maffione 				error = update_err;
14432a7db7a6SVincenzo Maffione 		}
14442a7db7a6SVincenzo Maffione 		NMG_UNLOCK();
14452a7db7a6SVincenzo Maffione 		if_rele(ifp);
14462a7db7a6SVincenzo Maffione 		return error;
14472a7db7a6SVincenzo Maffione 	}
14482a7db7a6SVincenzo Maffione 	error = nm_os_vi_persist(hdr->nr_name, &ifp);
14492a7db7a6SVincenzo Maffione 	if (error)
14502a7db7a6SVincenzo Maffione 		return error;
14512a7db7a6SVincenzo Maffione 
14522a7db7a6SVincenzo Maffione 	NMG_LOCK();
14532a7db7a6SVincenzo Maffione 	if (req->nr_mem_id) {
14542a7db7a6SVincenzo Maffione 		nmd = netmap_mem_find(req->nr_mem_id);
14552a7db7a6SVincenzo Maffione 		if (nmd == NULL) {
14562a7db7a6SVincenzo Maffione 			error = EINVAL;
14572a7db7a6SVincenzo Maffione 			goto err_1;
14582a7db7a6SVincenzo Maffione 		}
14592a7db7a6SVincenzo Maffione 	}
14602a7db7a6SVincenzo Maffione 	/* netmap_vp_create creates a struct netmap_vp_adapter */
1461b6e66be2SVincenzo Maffione 	error = netmap_vale_vp_create(hdr, ifp, nmd, &vpna);
14622a7db7a6SVincenzo Maffione 	if (error) {
1463b6e66be2SVincenzo Maffione 		if (netmap_debug & NM_DEBUG_VALE)
1464b6e66be2SVincenzo Maffione 			nm_prerr("error %d", error);
14652a7db7a6SVincenzo Maffione 		goto err_1;
14662a7db7a6SVincenzo Maffione 	}
14672a7db7a6SVincenzo Maffione 	/* persist-specific routines */
14682a7db7a6SVincenzo Maffione 	vpna->up.nm_bdg_ctl = netmap_vp_bdg_ctl;
14692a7db7a6SVincenzo Maffione 	if (!autodelete) {
14702a7db7a6SVincenzo Maffione 		netmap_adapter_get(&vpna->up);
14712a7db7a6SVincenzo Maffione 	} else {
14722a7db7a6SVincenzo Maffione 		vpna->autodelete = 1;
14732a7db7a6SVincenzo Maffione 	}
14742a7db7a6SVincenzo Maffione 	NM_ATTACH_NA(ifp, &vpna->up);
14752a7db7a6SVincenzo Maffione 	/* return the updated info */
14762a7db7a6SVincenzo Maffione 	error = nm_update_info(req, &vpna->up);
14772a7db7a6SVincenzo Maffione 	if (error) {
14782a7db7a6SVincenzo Maffione 		goto err_2;
14792a7db7a6SVincenzo Maffione 	}
148075f4f3edSVincenzo Maffione 	nm_prdis("returning nr_mem_id %d", req->nr_mem_id);
14812a7db7a6SVincenzo Maffione 	if (nmd)
14822a7db7a6SVincenzo Maffione 		netmap_mem_put(nmd);
14832a7db7a6SVincenzo Maffione 	NMG_UNLOCK();
1484e330262fSJustin Hibbits 	nm_prdis("created %s", if_name(ifp));
14852a7db7a6SVincenzo Maffione 	return 0;
14862a7db7a6SVincenzo Maffione 
14872a7db7a6SVincenzo Maffione err_2:
14882a7db7a6SVincenzo Maffione 	netmap_detach(ifp);
14892a7db7a6SVincenzo Maffione err_1:
14902a7db7a6SVincenzo Maffione 	if (nmd)
14912a7db7a6SVincenzo Maffione 		netmap_mem_put(nmd);
14922a7db7a6SVincenzo Maffione 	NMG_UNLOCK();
14932a7db7a6SVincenzo Maffione 	nm_os_vi_detach(ifp);
14942a7db7a6SVincenzo Maffione 
14952a7db7a6SVincenzo Maffione 	return error;
14962a7db7a6SVincenzo Maffione }
14972a7db7a6SVincenzo Maffione 
1498f9790aebSLuigi Rizzo #endif /* WITH_VALE */
1499