xref: /dragonfly/sys/net/netmap/netmap_freebsd.c (revision cfd1aba3)
1 /*
2  * Copyright (C) 2013 Universita` di Pisa. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *   1. Redistributions of source code must retain the above copyright
8  *      notice, this list of conditions and the following disclaimer.
9  *   2. Redistributions in binary form must reproduce the above copyright
10  *      notice, this list of conditions and the following disclaimer in the
11  *      documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 #include <sys/types.h>
27 #include <sys/module.h>
28 #include <sys/errno.h>
29 #include <sys/param.h>  /* defines used in kernel.h */
30 #include <sys/kernel.h> /* types used in module initialization */
31 #include <sys/conf.h>	/* DEV_MODULE */
32 
33 #include <sys/devfs.h>
34 
35 #include <vm/vm.h>      /* vtophys */
36 #include <vm/pmap.h>    /* vtophys */
37 #include <vm/vm_param.h>
38 #include <vm/vm_object.h>
39 #include <vm/vm_page.h>
40 #include <vm/vm_page2.h>
41 #include <vm/vm_pager.h>
42 
43 
44 #include <sys/malloc.h>
45 #include <sys/socket.h> /* sockaddrs */
46 #include <sys/event.h>
47 #include <net/if.h>
48 #include <net/if_var.h>
49 #include <sys/bus.h>	/* bus_dmamap_* */
50 
51 #include <net/netmap.h>
52 #include <net/netmap/netmap_kern.h>
53 #include <net/netmap/netmap_mem2.h>
54 
55 
56 /* ======================== FREEBSD-SPECIFIC ROUTINES ================== */
57 
58 /*
59  * Intercept the rx routine in the standard device driver.
60  * Second argument is non-zero to intercept, 0 to restore
61  */
62 int
63 netmap_catch_rx(struct netmap_adapter *na, int intercept)
64 {
65 	struct netmap_generic_adapter *gna = (struct netmap_generic_adapter *)na;
66 	struct ifnet *ifp = na->ifp;
67 
68 	if (intercept) {
69 		if (gna->save_if_input) {
70 			D("cannot intercept again");
71 			return EINVAL; /* already set */
72 		}
73 		gna->save_if_input = ifp->if_input;
74 		ifp->if_input = generic_rx_handler;
75 	} else {
76 		if (!gna->save_if_input){
77 			D("cannot restore");
78 			return EINVAL;  /* not saved */
79 		}
80 		ifp->if_input = gna->save_if_input;
81 		gna->save_if_input = NULL;
82 	}
83 
84 	return 0;
85 }
86 
87 /*
88  * Intercept the packet steering routine in the tx path,
89  * so that we can decide which queue is used for an mbuf.
90  * Second argument is non-zero to intercept, 0 to restore.
91  *
92  * XXX see if FreeBSD has such a mechanism
93  */
94 void
95 netmap_catch_packet_steering(struct netmap_generic_adapter *na, int enable)
96 {
97 	if (enable) {
98 	} else {
99 	}
100 }
101 
102 /* Transmit routine used by generic_netmap_txsync(). Returns 0 on success
103  * and non-zero on error (which may be packet drops or other errors).
104  * addr and len identify the netmap buffer, m is the (preallocated)
105  * mbuf to use for transmissions.
106  *
107  * We should add a reference to the mbuf so the m_freem() at the end
108  * of the transmission does not consume resources.
109  *
110  * On FreeBSD, and on multiqueue cards, we can force the queue using
111  *      if ((m->m_flags & M_FLOWID) != 0)
112  *              i = m->m_pkthdr.flowid % adapter->num_queues;
113  *      else
114  *              i = curcpu % adapter->num_queues;
115  *
116  */
117 int
118 generic_xmit_frame(struct ifnet *ifp, struct mbuf *m,
119 	void *addr, u_int len, u_int ring_nr)
120 {
121 	int ret;
122 
123 	m->m_len = m->m_pkthdr.len = 0;
124 
125 	// copy data to the mbuf
126 	m_copyback(m, 0, len, addr);
127 
128 #if 0
129 	// inc refcount. We are alone, so we can skip the atomic
130 	atomic_fetchadd_int(m->m_ext.ref_cnt, 1);
131 	m->m_flags |= M_FLOWID;
132 #endif
133 	m->m_pkthdr.hash = ring_nr;	/* XXX probably not accurate */
134 	m->m_pkthdr.rcvif = ifp; /* used for tx notification */
135 	ret = ifp->if_transmit(ifp, m);
136 	return ret;
137 }
138 
139 /*
140  * The following two functions are empty until we have a generic
141  * way to extract the info from the ifp
142  */
143 int
144 generic_find_num_desc(struct ifnet *ifp, unsigned int *tx, unsigned int *rx)
145 {
146 	D("called");
147 	return 0;
148 }
149 
150 void
151 generic_find_num_queues(struct ifnet *ifp, u_int *txq, u_int *rxq)
152 {
153 	D("called");
154 	*txq = 1;
155 	*rxq = 1;
156 }
157 
158 void netmap_mitigation_init(struct netmap_generic_adapter *na)
159 {
160 	ND("called");
161 	na->mit_pending = 0;
162 }
163 
164 
165 void netmap_mitigation_start(struct netmap_generic_adapter *na)
166 {
167 	ND("called");
168 }
169 
170 void netmap_mitigation_restart(struct netmap_generic_adapter *na)
171 {
172 	ND("called");
173 }
174 
175 int netmap_mitigation_active(struct netmap_generic_adapter *na)
176 {
177 	ND("called");
178 	return 0;
179 }
180 
181 void netmap_mitigation_cleanup(struct netmap_generic_adapter *na)
182 {
183 	ND("called");
184 }
185 
186 
187 /*
188  * In order to track whether pages are still mapped, we hook into
189  * the standard cdev_pager and intercept the constructor and
190  * destructor.
191  */
192 
193 struct netmap_vm_handle_t {
194 	struct cdev 		*dev;
195 	struct netmap_priv_d	*priv;
196 };
197 
198 static int
199 netmap_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
200     vm_ooffset_t foff, struct ucred *cred, u_short *color)
201 {
202 	struct netmap_vm_handle_t *vmh = handle;
203 	(void)vmh;
204 	D("handle %p size %jd prot %d foff %jd",
205 		handle, (intmax_t)size, prot, (intmax_t)foff);
206 #if 0
207 	dev_ref(vmh->dev);
208 #endif
209 	return 0;
210 }
211 
212 
213 static void
214 netmap_dev_pager_dtor(void *handle)
215 {
216 	struct netmap_vm_handle_t *vmh = handle;
217 	struct cdev *dev = vmh->dev;
218 	struct netmap_priv_d *priv = vmh->priv;
219 	(void)dev;
220 	D("handle %p", handle);
221 	netmap_dtor(priv);
222 	kfree(vmh, M_DEVBUF);
223 #if 0
224 	dev_rel(dev);
225 #endif
226 }
227 
228 static int
229 netmap_dev_pager_fault(vm_object_t object, vm_ooffset_t offset,
230 	int prot, vm_page_t *mres)
231 {
232 	struct netmap_vm_handle_t *vmh = object->handle;
233 	struct netmap_priv_d *priv = vmh->priv;
234 	vm_paddr_t paddr;
235 	vm_page_t page;
236 	vm_memattr_t memattr;
237 	vm_pindex_t pidx;
238 
239 	ND("object %p offset %jd prot %d mres %p",
240 			object, (intmax_t)offset, prot, mres);
241 	memattr = object->memattr;
242 	pidx = OFF_TO_IDX(offset);
243 	paddr = netmap_mem_ofstophys(priv->np_mref, offset);
244 	if (paddr == 0)
245 		return VM_PAGER_FAIL;
246 
247 	if (((*mres)->flags & PG_FICTITIOUS) != 0) {
248 		/*
249 		 * If the passed in result page is a fake page, update it with
250 		 * the new physical address.
251 		 */
252 		page = *mres;
253 		vm_page_updatefake(page, paddr, memattr);
254 	} else {
255 		/*
256 		 * Replace the passed in reqpage page with our own fake page and
257 		 * free up the all of the original pages.
258 		 */
259 #ifndef VM_OBJECT_WUNLOCK	/* FreeBSD < 10.x */
260 #define VM_OBJECT_WUNLOCK VM_OBJECT_UNLOCK
261 #define VM_OBJECT_WLOCK	VM_OBJECT_LOCK
262 #endif /* VM_OBJECT_WUNLOCK */
263 
264 		VM_OBJECT_WUNLOCK(object);
265 		page = vm_page_getfake(paddr, memattr);
266 		VM_OBJECT_WLOCK(object);
267 		vm_page_free(*mres);
268 		*mres = page;
269 		vm_page_insert(page, object, pidx);
270 	}
271 	page->valid = VM_PAGE_BITS_ALL;
272 	return (VM_PAGER_OK);
273 }
274 
275 
276 static struct cdev_pager_ops netmap_cdev_pager_ops = {
277 	.cdev_pg_ctor = netmap_dev_pager_ctor,
278 	.cdev_pg_dtor = netmap_dev_pager_dtor,
279 	.cdev_pg_fault = netmap_dev_pager_fault,
280 };
281 
282 
283 static int
284 netmap_mmap_single(struct dev_mmap_single_args *ap)
285 {
286 	int error;
287 	struct cdev *cdev = ap->a_head.a_dev;
288 	vm_ooffset_t *foff = ap->a_offset;
289 	vm_object_t *objp = ap->a_object;
290 	vm_size_t objsize = ap->a_size;
291 	struct netmap_vm_handle_t *vmh;
292 	struct netmap_priv_d *priv;
293 	int prot = ap->a_nprot;
294 	vm_object_t obj;
295 
296 	D("cdev %p foff %jd size %jd objp %p prot %d", cdev,
297 	    (intmax_t )*foff, (intmax_t )objsize, objp, prot);
298 
299 	vmh = kmalloc(sizeof(struct netmap_vm_handle_t), M_DEVBUF,
300 			      M_NOWAIT | M_ZERO);
301 	if (vmh == NULL)
302 		return ENOMEM;
303 	vmh->dev = cdev;
304 
305 	NMG_LOCK();
306 	error = devfs_get_cdevpriv(ap->a_fp, (void**)&priv);
307 	if (error)
308 		goto err_unlock;
309 	vmh->priv = priv;
310 	priv->np_refcount++;
311 	NMG_UNLOCK();
312 
313 	error = netmap_get_memory(priv);
314 	if (error)
315 		goto err_deref;
316 
317 	obj = cdev_pager_allocate(vmh, OBJT_DEVICE,
318 		&netmap_cdev_pager_ops, objsize, prot,
319 		*foff, NULL);
320 	if (obj == NULL) {
321 		D("cdev_pager_allocate failed");
322 		error = EINVAL;
323 		goto err_deref;
324 	}
325 
326 	*objp = obj;
327 	return 0;
328 
329 err_deref:
330 	NMG_LOCK();
331 	priv->np_refcount--;
332 err_unlock:
333 	NMG_UNLOCK();
334 // err:
335 	kfree(vmh, M_DEVBUF);
336 	return error;
337 }
338 
339 
340 // XXX can we remove this ?
341 static int
342 netmap_close(struct dev_close_args *ap)
343 {
344 	if (netmap_verbose)
345 		D("dev %p fflag 0x%x devtype %d",
346 			ap->a_head.a_dev, ap->a_fflag, ap->a_devtype);
347 	return 0;
348 }
349 
350 
351 static int
352 netmap_open(struct dev_open_args *ap)
353 {
354 	struct netmap_priv_d *priv;
355 	int error;
356 
357 	// XXX wait or nowait ?
358 	priv = kmalloc(sizeof(struct netmap_priv_d), M_DEVBUF,
359 			      M_NOWAIT | M_ZERO);
360 	if (priv == NULL)
361 		return ENOMEM;
362 
363 	error = devfs_set_cdevpriv(ap->a_fp, priv, netmap_dtor);
364 	if (error)
365 	        return error;
366 
367 	priv->np_refcount = 1;
368 
369 	return 0;
370 }
371 
372 
373 struct dev_ops netmap_cdevsw = {
374 	{ "netmap", 0, 0 },
375 	.d_open = netmap_open,
376 	.d_mmap_single = netmap_mmap_single,
377 	.d_ioctl = netmap_ioctl,
378 	.d_kqfilter = netmap_kqfilter,
379 	.d_close = netmap_close,
380 };
381 
382 
383 /*
384  * Kernel entry point.
385  *
386  * Initialize/finalize the module and return.
387  *
388  * Return 0 on success, errno on failure.
389  */
390 static int
391 netmap_loader(__unused struct module *module, int event, __unused void *arg)
392 {
393 	int error = 0;
394 
395 	switch (event) {
396 	case MOD_LOAD:
397 		error = netmap_init();
398 		break;
399 
400 	case MOD_UNLOAD:
401 		netmap_fini();
402 		break;
403 
404 	default:
405 		error = EOPNOTSUPP;
406 		break;
407 	}
408 
409 	return (error);
410 }
411 
412 
413 DEV_MODULE(netmap, netmap_loader, NULL);
414