xref: /freebsd/sys/dev/netmap/netmap_null.c (revision 95eb4b873b6a8b527c5bd78d7191975dfca38998)
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 
27 #if defined(__FreeBSD__)
28 #include <sys/cdefs.h> /* prerequisite */
29 
30 #include <sys/types.h>
31 #include <sys/errno.h>
32 #include <sys/param.h>	/* defines used in kernel.h */
33 #include <sys/kernel.h>	/* types used in module initialization */
34 #include <sys/malloc.h>
35 #include <sys/poll.h>
36 #include <sys/lock.h>
37 #include <sys/rwlock.h>
38 #include <sys/selinfo.h>
39 #include <sys/sysctl.h>
40 #include <sys/socket.h> /* sockaddrs */
41 #include <net/if.h>
42 #include <net/if_var.h>
43 #include <machine/bus.h>	/* bus_dmamap_* */
44 #include <sys/refcount.h>
45 
46 
47 #elif defined(linux)
48 
49 #include "bsd_glue.h"
50 
51 #elif defined(__APPLE__)
52 
53 #warning OSX support is only partial
54 #include "osx_glue.h"
55 
56 #elif defined(_WIN32)
57 #include "win_glue.h"
58 
59 #else
60 
61 #error	Unsupported platform
62 
63 #endif /* unsupported */
64 
65 /*
66  * common headers
67  */
68 
69 #include <net/netmap.h>
70 #include <dev/netmap/netmap_kern.h>
71 #include <dev/netmap/netmap_mem2.h>
72 
73 #ifdef WITH_NMNULL
74 
75 static int
76 netmap_null_sync(struct netmap_kring *kring, int flags)
77 {
78 	(void)kring;
79 	(void)flags;
80 	return 0;
81 }
82 
83 static int
84 netmap_null_krings_create(struct netmap_adapter *na)
85 {
86 	return netmap_krings_create(na, 0);
87 }
88 
89 static int
90 netmap_null_reg(struct netmap_adapter *na, int onoff)
91 {
92 	if (na->active_fds == 0) {
93 		if (onoff)
94 			na->na_flags |= NAF_NETMAP_ON;
95 		else
96 			na->na_flags &= ~NAF_NETMAP_ON;
97 	}
98 	return 0;
99 }
100 
101 static int
102 netmap_null_bdg_attach(const char *name, struct netmap_adapter *na,
103 		struct nm_bridge *b)
104 {
105 	(void)name;
106 	(void)na;
107 	(void)b;
108 	return EINVAL;
109 }
110 
111 int
112 netmap_get_null_na(struct nmreq_header *hdr, struct netmap_adapter **na,
113 		struct netmap_mem_d *nmd, int create)
114 {
115 	struct nmreq_register *req = (struct nmreq_register *)(uintptr_t)hdr->nr_body;
116 	struct netmap_null_adapter *nna;
117 	int error;
118 
119 	if (req->nr_mode != NR_REG_NULL) {
120 		nm_prdis("not a null port");
121 		return 0;
122 	}
123 
124 	if (!create) {
125 		nm_prerr("null ports cannot be re-opened");
126 		return EINVAL;
127 	}
128 
129 	if (nmd == NULL) {
130 		nm_prerr("null ports must use an existing allocator");
131 		return EINVAL;
132 	}
133 
134 	nna = nm_os_malloc(sizeof(*nna));
135 	if (nna == NULL) {
136 		error = ENOMEM;
137 		goto err;
138 	}
139 	snprintf(nna->up.name, sizeof(nna->up.name), "null:%s", hdr->nr_name);
140 
141 	nna->up.nm_txsync = netmap_null_sync;
142 	nna->up.nm_rxsync = netmap_null_sync;
143 	nna->up.nm_register = netmap_null_reg;
144 	nna->up.nm_krings_create = netmap_null_krings_create;
145 	nna->up.nm_krings_delete = netmap_krings_delete;
146 	nna->up.nm_bdg_attach = netmap_null_bdg_attach;
147 	nna->up.nm_mem = netmap_mem_get(nmd);
148 
149 	nna->up.num_tx_rings = req->nr_tx_rings;
150 	nna->up.num_rx_rings = req->nr_rx_rings;
151 	nna->up.num_tx_desc = req->nr_tx_slots;
152 	nna->up.num_rx_desc = req->nr_rx_slots;
153 	nna->up.na_flags = NAF_OFFSETS;
154 	error = netmap_attach_common(&nna->up);
155 	if (error)
156 		goto free_nna;
157 	*na = &nna->up;
158 	netmap_adapter_get(*na);
159 	nm_prdis("created null %s", nna->up.name);
160 
161 	return 0;
162 
163 free_nna:
164 	nm_os_free(nna);
165 err:
166 	return error;
167 }
168 
169 
170 #endif /* WITH_NMNULL */
171