xref: /freebsd/sys/dev/netmap/netmap_null.c (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
1 /*
2  * Copyright (C) 2018 Giuseppe Lettieri
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *   1. Redistributions of source code must retain the above copyright
9  *      notice, this list of conditions and the following disclaimer.
10  *   2. Redistributions in binary form must reproduce the above copyright
11  *      notice, this list of conditions and the following disclaimer in the
12  *      documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 /* $FreeBSD$ */
27 
28 #if defined(__FreeBSD__)
29 #include <sys/cdefs.h> /* prerequisite */
30 
31 #include <sys/types.h>
32 #include <sys/errno.h>
33 #include <sys/param.h>	/* defines used in kernel.h */
34 #include <sys/kernel.h>	/* types used in module initialization */
35 #include <sys/malloc.h>
36 #include <sys/poll.h>
37 #include <sys/lock.h>
38 #include <sys/rwlock.h>
39 #include <sys/selinfo.h>
40 #include <sys/sysctl.h>
41 #include <sys/socket.h> /* sockaddrs */
42 #include <net/if.h>
43 #include <net/if_var.h>
44 #include <machine/bus.h>	/* bus_dmamap_* */
45 #include <sys/refcount.h>
46 
47 
48 #elif defined(linux)
49 
50 #include "bsd_glue.h"
51 
52 #elif defined(__APPLE__)
53 
54 #warning OSX support is only partial
55 #include "osx_glue.h"
56 
57 #elif defined(_WIN32)
58 #include "win_glue.h"
59 
60 #else
61 
62 #error	Unsupported platform
63 
64 #endif /* unsupported */
65 
66 /*
67  * common headers
68  */
69 
70 #include <net/netmap.h>
71 #include <dev/netmap/netmap_kern.h>
72 #include <dev/netmap/netmap_mem2.h>
73 
74 #ifdef WITH_NMNULL
75 
76 static int
77 netmap_null_sync(struct netmap_kring *kring, int flags)
78 {
79 	(void)kring;
80 	(void)flags;
81 	return 0;
82 }
83 
84 static int
85 netmap_null_krings_create(struct netmap_adapter *na)
86 {
87 	return netmap_krings_create(na, 0);
88 }
89 
90 static int
91 netmap_null_reg(struct netmap_adapter *na, int onoff)
92 {
93 	if (na->active_fds == 0) {
94 		if (onoff)
95 			na->na_flags |= NAF_NETMAP_ON;
96 		else
97 			na->na_flags &= ~NAF_NETMAP_ON;
98 	}
99 	return 0;
100 }
101 
102 static int
103 netmap_null_bdg_attach(const char *name, struct netmap_adapter *na,
104 		struct nm_bridge *b)
105 {
106 	(void)name;
107 	(void)na;
108 	(void)b;
109 	return EINVAL;
110 }
111 
112 int
113 netmap_get_null_na(struct nmreq_header *hdr, struct netmap_adapter **na,
114 		struct netmap_mem_d *nmd, int create)
115 {
116 	struct nmreq_register *req = (struct nmreq_register *)(uintptr_t)hdr->nr_body;
117 	struct netmap_null_adapter *nna;
118 	int error;
119 
120 	if (req->nr_mode != NR_REG_NULL) {
121 		nm_prdis("not a null port");
122 		return 0;
123 	}
124 
125 	if (!create) {
126 		nm_prerr("null ports cannot be re-opened");
127 		return EINVAL;
128 	}
129 
130 	if (nmd == NULL) {
131 		nm_prerr("null ports must use an existing allocator");
132 		return EINVAL;
133 	}
134 
135 	nna = nm_os_malloc(sizeof(*nna));
136 	if (nna == NULL) {
137 		error = ENOMEM;
138 		goto err;
139 	}
140 	snprintf(nna->up.name, sizeof(nna->up.name), "null:%s", hdr->nr_name);
141 
142 	nna->up.nm_txsync = netmap_null_sync;
143 	nna->up.nm_rxsync = netmap_null_sync;
144 	nna->up.nm_register = netmap_null_reg;
145 	nna->up.nm_krings_create = netmap_null_krings_create;
146 	nna->up.nm_krings_delete = netmap_krings_delete;
147 	nna->up.nm_bdg_attach = netmap_null_bdg_attach;
148 	nna->up.nm_mem = netmap_mem_get(nmd);
149 
150 	nna->up.num_tx_rings = req->nr_tx_rings;
151 	nna->up.num_rx_rings = req->nr_rx_rings;
152 	nna->up.num_tx_desc = req->nr_tx_slots;
153 	nna->up.num_rx_desc = req->nr_rx_slots;
154 	nna->up.na_flags = NAF_OFFSETS;
155 	error = netmap_attach_common(&nna->up);
156 	if (error)
157 		goto free_nna;
158 	*na = &nna->up;
159 	netmap_adapter_get(*na);
160 	nm_prdis("created null %s", nna->up.name);
161 
162 	return 0;
163 
164 free_nna:
165 	nm_os_free(nna);
166 err:
167 	return error;
168 }
169 
170 
171 #endif /* WITH_NMNULL */
172