1341516Svmaffione/*
2341516Svmaffione * Copyright (C) 2018 Giuseppe Lettieri
3341516Svmaffione * All rights reserved.
4341516Svmaffione *
5341516Svmaffione * Redistribution and use in source and binary forms, with or without
6341516Svmaffione * modification, are permitted provided that the following conditions
7341516Svmaffione * are met:
8341516Svmaffione *   1. Redistributions of source code must retain the above copyright
9341516Svmaffione *      notice, this list of conditions and the following disclaimer.
10341516Svmaffione *   2. Redistributions in binary form must reproduce the above copyright
11341516Svmaffione *      notice, this list of conditions and the following disclaimer in the
12341516Svmaffione *      documentation and/or other materials provided with the distribution.
13341516Svmaffione *
14341516Svmaffione * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15341516Svmaffione * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16341516Svmaffione * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17341516Svmaffione * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18341516Svmaffione * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19341516Svmaffione * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20341516Svmaffione * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21341516Svmaffione * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22341516Svmaffione * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23341516Svmaffione * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24341516Svmaffione * SUCH DAMAGE.
25341516Svmaffione */
26341516Svmaffione/* $FreeBSD: stable/11/sys/dev/netmap/netmap_null.c 344047 2019-02-12 09:26:05Z vmaffione $ */
27341516Svmaffione
28341516Svmaffione#if defined(__FreeBSD__)
29341516Svmaffione#include <sys/cdefs.h> /* prerequisite */
30341516Svmaffione
31341516Svmaffione#include <sys/types.h>
32341516Svmaffione#include <sys/errno.h>
33341516Svmaffione#include <sys/param.h>	/* defines used in kernel.h */
34341516Svmaffione#include <sys/kernel.h>	/* types used in module initialization */
35341516Svmaffione#include <sys/malloc.h>
36341516Svmaffione#include <sys/poll.h>
37341516Svmaffione#include <sys/lock.h>
38341516Svmaffione#include <sys/rwlock.h>
39341516Svmaffione#include <sys/selinfo.h>
40341516Svmaffione#include <sys/sysctl.h>
41341516Svmaffione#include <sys/socket.h> /* sockaddrs */
42341516Svmaffione#include <net/if.h>
43341516Svmaffione#include <net/if_var.h>
44341516Svmaffione#include <machine/bus.h>	/* bus_dmamap_* */
45341516Svmaffione#include <sys/refcount.h>
46341516Svmaffione
47341516Svmaffione
48341516Svmaffione#elif defined(linux)
49341516Svmaffione
50341516Svmaffione#include "bsd_glue.h"
51341516Svmaffione
52341516Svmaffione#elif defined(__APPLE__)
53341516Svmaffione
54341516Svmaffione#warning OSX support is only partial
55341516Svmaffione#include "osx_glue.h"
56341516Svmaffione
57341516Svmaffione#elif defined(_WIN32)
58341516Svmaffione#include "win_glue.h"
59341516Svmaffione
60341516Svmaffione#else
61341516Svmaffione
62341516Svmaffione#error	Unsupported platform
63341516Svmaffione
64341516Svmaffione#endif /* unsupported */
65341516Svmaffione
66341516Svmaffione/*
67341516Svmaffione * common headers
68341516Svmaffione */
69341516Svmaffione
70341516Svmaffione#include <net/netmap.h>
71341516Svmaffione#include <dev/netmap/netmap_kern.h>
72341516Svmaffione#include <dev/netmap/netmap_mem2.h>
73341516Svmaffione
74341516Svmaffione#ifdef WITH_NMNULL
75341516Svmaffione
76341516Svmaffionestatic int
77344047Svmaffionenetmap_null_sync(struct netmap_kring *kring, int flags)
78341516Svmaffione{
79341516Svmaffione	(void)kring;
80341516Svmaffione	(void)flags;
81341516Svmaffione	return 0;
82341516Svmaffione}
83341516Svmaffione
84341516Svmaffionestatic int
85341516Svmaffionenetmap_null_krings_create(struct netmap_adapter *na)
86341516Svmaffione{
87341516Svmaffione	return netmap_krings_create(na, 0);
88341516Svmaffione}
89341516Svmaffione
90341516Svmaffionestatic int
91341516Svmaffionenetmap_null_reg(struct netmap_adapter *na, int onoff)
92341516Svmaffione{
93341516Svmaffione	if (na->active_fds == 0) {
94341516Svmaffione		if (onoff)
95341516Svmaffione			na->na_flags |= NAF_NETMAP_ON;
96341516Svmaffione		else
97341516Svmaffione			na->na_flags &= ~NAF_NETMAP_ON;
98341516Svmaffione	}
99341516Svmaffione	return 0;
100341516Svmaffione}
101341516Svmaffione
102341516Svmaffionestatic int
103341516Svmaffionenetmap_null_bdg_attach(const char *name, struct netmap_adapter *na,
104341516Svmaffione		struct nm_bridge *b)
105341516Svmaffione{
106341516Svmaffione	(void)name;
107341516Svmaffione	(void)na;
108341516Svmaffione	(void)b;
109341516Svmaffione	return EINVAL;
110341516Svmaffione}
111341516Svmaffione
112341516Svmaffioneint
113341516Svmaffionenetmap_get_null_na(struct nmreq_header *hdr, struct netmap_adapter **na,
114341516Svmaffione		struct netmap_mem_d *nmd, int create)
115341516Svmaffione{
116341516Svmaffione	struct nmreq_register *req = (struct nmreq_register *)(uintptr_t)hdr->nr_body;
117341516Svmaffione	struct netmap_null_adapter *nna;
118341516Svmaffione	int error;
119341516Svmaffione
120341516Svmaffione	if (req->nr_mode != NR_REG_NULL) {
121341516Svmaffione		nm_prdis("not a null port");
122341516Svmaffione		return 0;
123341516Svmaffione	}
124341516Svmaffione
125341516Svmaffione	if (!create) {
126341516Svmaffione		nm_prerr("null ports cannot be re-opened");
127341516Svmaffione		return EINVAL;
128341516Svmaffione	}
129341516Svmaffione
130341516Svmaffione	if (nmd == NULL) {
131341516Svmaffione		nm_prerr("null ports must use an existing allocator");
132341516Svmaffione		return EINVAL;
133341516Svmaffione	}
134341516Svmaffione
135341516Svmaffione	nna = nm_os_malloc(sizeof(*nna));
136341516Svmaffione	if (nna == NULL) {
137341516Svmaffione		error = ENOMEM;
138341516Svmaffione		goto err;
139341516Svmaffione	}
140341516Svmaffione	snprintf(nna->up.name, sizeof(nna->up.name), "null:%s", hdr->nr_name);
141341516Svmaffione
142344047Svmaffione	nna->up.nm_txsync = netmap_null_sync;
143344047Svmaffione	nna->up.nm_rxsync = netmap_null_sync;
144341516Svmaffione	nna->up.nm_register = netmap_null_reg;
145341516Svmaffione	nna->up.nm_krings_create = netmap_null_krings_create;
146344047Svmaffione	nna->up.nm_krings_delete = netmap_krings_delete;
147341516Svmaffione	nna->up.nm_bdg_attach = netmap_null_bdg_attach;
148341516Svmaffione	nna->up.nm_mem = netmap_mem_get(nmd);
149341516Svmaffione
150341516Svmaffione	nna->up.num_tx_rings = req->nr_tx_rings;
151341516Svmaffione	nna->up.num_rx_rings = req->nr_rx_rings;
152341516Svmaffione	nna->up.num_tx_desc = req->nr_tx_slots;
153341516Svmaffione	nna->up.num_rx_desc = req->nr_rx_slots;
154341516Svmaffione	error = netmap_attach_common(&nna->up);
155341516Svmaffione	if (error)
156341516Svmaffione		goto free_nna;
157341516Svmaffione	*na = &nna->up;
158341516Svmaffione	netmap_adapter_get(*na);
159341516Svmaffione	nm_prdis("created null %s", nna->up.name);
160341516Svmaffione
161341516Svmaffione	return 0;
162341516Svmaffione
163341516Svmaffionefree_nna:
164341516Svmaffione	nm_os_free(nna);
165341516Svmaffioneerr:
166341516Svmaffione	return error;
167341516Svmaffione}
168341516Svmaffione
169341516Svmaffione
170341516Svmaffione#endif /* WITH_NMNULL */
171