netmap: lock(9), k*() foo, no ifdefs
[dragonfly.git] / sys / net / netmap / netmap_freebsd.c
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 #include <sys/bus.h>    /* bus_dmamap_* */
33 #include <sys/malloc.h>
34 #include <sys/socket.h> /* sockaddrs */
35
36 #include <vm/vm.h>      /* vtophys */
37 #include <vm/pmap.h>    /* vtophys */
38 #include <vm/vm_param.h>
39 #include <vm/vm_object.h>
40 #include <vm/vm_page.h>
41 #include <vm/vm_pager.h>
42 #include <vm/uma.h>
43
44 #include <net/if.h>
45 #include <net/if_var.h>
46 #include <net/netmap.h>
47
48 #include "netmap_kern.h"
49 #include "netmap_mem2.h"
50
51
52 /* ======================== FREEBSD-SPECIFIC ROUTINES ================== */
53
54 /*
55  * Intercept the rx routine in the standard device driver.
56  * Second argument is non-zero to intercept, 0 to restore
57  */
58 int
59 netmap_catch_rx(struct netmap_adapter *na, int intercept)
60 {
61         struct netmap_generic_adapter *gna = (struct netmap_generic_adapter *)na;
62         struct ifnet *ifp = na->ifp;
63
64         if (intercept) {
65                 if (gna->save_if_input) {
66                         D("cannot intercept again");
67                         return EINVAL; /* already set */
68                 }
69                 gna->save_if_input = ifp->if_input;
70                 ifp->if_input = generic_rx_handler;
71         } else {
72                 if (!gna->save_if_input){
73                         D("cannot restore");
74                         return EINVAL;  /* not saved */
75                 }
76                 ifp->if_input = gna->save_if_input;
77                 gna->save_if_input = NULL;
78         }
79
80         return 0;
81 }
82
83 /*
84  * Intercept the packet steering routine in the tx path,
85  * so that we can decide which queue is used for an mbuf.
86  * Second argument is non-zero to intercept, 0 to restore.
87  *
88  * XXX see if FreeBSD has such a mechanism
89  */
90 void
91 netmap_catch_packet_steering(struct netmap_generic_adapter *na, int enable)
92 {
93         if (enable) {
94         } else {
95         }
96 }
97
98 /* Transmit routine used by generic_netmap_txsync(). Returns 0 on success
99  * and non-zero on error (which may be packet drops or other errors).
100  * addr and len identify the netmap buffer, m is the (preallocated)
101  * mbuf to use for transmissions.
102  *
103  * We should add a reference to the mbuf so the m_freem() at the end
104  * of the transmission does not consume resources.
105  *
106  * On FreeBSD, and on multiqueue cards, we can force the queue using
107  *      if ((m->m_flags & M_FLOWID) != 0)
108  *              i = m->m_pkthdr.flowid % adapter->num_queues;
109  *      else
110  *              i = curcpu % adapter->num_queues;
111  *
112  */
113 int
114 generic_xmit_frame(struct ifnet *ifp, struct mbuf *m,
115         void *addr, u_int len, u_int ring_nr)
116 {
117         int ret;
118
119         m->m_len = m->m_pkthdr.len = 0;
120
121         // copy data to the mbuf
122         m_copyback(m, 0, len, addr);
123
124         // inc refcount. We are alone, so we can skip the atomic
125         atomic_fetchadd_int(m->m_ext.ref_cnt, 1);
126         m->m_flags |= M_FLOWID;
127         m->m_pkthdr.flowid = ring_nr;
128         m->m_pkthdr.rcvif = ifp; /* used for tx notification */
129         ret = ifp->if_transmit(ifp, m);
130         return ret;
131 }
132
133 /*
134  * The following two functions are empty until we have a generic
135  * way to extract the info from the ifp
136  */
137 int
138 generic_find_num_desc(struct ifnet *ifp, unsigned int *tx, unsigned int *rx)
139 {
140         D("called");
141         return 0;
142 }
143
144 void
145 generic_find_num_queues(struct ifnet *ifp, u_int *txq, u_int *rxq)
146 {
147         D("called");
148         *txq = 1;
149         *rxq = 1;
150 }
151
152 void netmap_mitigation_init(struct netmap_generic_adapter *na)
153 {
154         ND("called");
155         na->mit_pending = 0;
156 }
157
158
159 void netmap_mitigation_start(struct netmap_generic_adapter *na)
160 {
161         ND("called");
162 }
163
164 void netmap_mitigation_restart(struct netmap_generic_adapter *na)
165 {
166         ND("called");
167 }
168
169 int netmap_mitigation_active(struct netmap_generic_adapter *na)
170 {
171         ND("called");
172         return 0;
173 }
174
175 void netmap_mitigation_cleanup(struct netmap_generic_adapter *na)
176 {
177         ND("called");
178 }
179
180
181 /*
182  * In order to track whether pages are still mapped, we hook into
183  * the standard cdev_pager and intercept the constructor and
184  * destructor.
185  */
186
187 struct netmap_vm_handle_t {
188         struct cdev             *dev;
189         struct netmap_priv_d    *priv;
190 };
191
192 static int
193 netmap_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
194     vm_ooffset_t foff, struct ucred *cred, u_short *color)
195 {
196         struct netmap_vm_handle_t *vmh = handle;
197         D("handle %p size %jd prot %d foff %jd",
198                 handle, (intmax_t)size, prot, (intmax_t)foff);
199         dev_ref(vmh->dev);
200         return 0;
201 }
202
203
204 static void
205 netmap_dev_pager_dtor(void *handle)
206 {
207         struct netmap_vm_handle_t *vmh = handle;
208         struct cdev *dev = vmh->dev;
209         struct netmap_priv_d *priv = vmh->priv;
210         D("handle %p", handle);
211         netmap_dtor(priv);
212         kfree(vmh, M_DEVBUF);
213         dev_rel(dev);
214 }
215
216 static int
217 netmap_dev_pager_fault(vm_object_t object, vm_ooffset_t offset,
218         int prot, vm_page_t *mres)
219 {
220         struct netmap_vm_handle_t *vmh = object->handle;
221         struct netmap_priv_d *priv = vmh->priv;
222         vm_paddr_t paddr;
223         vm_page_t page;
224         vm_memattr_t memattr;
225         vm_pindex_t pidx;
226
227         ND("object %p offset %jd prot %d mres %p",
228                         object, (intmax_t)offset, prot, mres);
229         memattr = object->memattr;
230         pidx = OFF_TO_IDX(offset);
231         paddr = netmap_mem_ofstophys(priv->np_mref, offset);
232         if (paddr == 0)
233                 return VM_PAGER_FAIL;
234
235         if (((*mres)->flags & PG_FICTITIOUS) != 0) {
236                 /*
237                  * If the passed in result page is a fake page, update it with
238                  * the new physical address.
239                  */
240                 page = *mres;
241                 vm_page_updatefake(page, paddr, memattr);
242         } else {
243                 /*
244                  * Replace the passed in reqpage page with our own fake page and
245                  * free up the all of the original pages.
246                  */
247 #ifndef VM_OBJECT_WUNLOCK       /* FreeBSD < 10.x */
248 #define VM_OBJECT_WUNLOCK VM_OBJECT_UNLOCK
249 #define VM_OBJECT_WLOCK VM_OBJECT_LOCK
250 #endif /* VM_OBJECT_WUNLOCK */
251
252                 VM_OBJECT_WUNLOCK(object);
253                 page = vm_page_getfake(paddr, memattr);
254                 VM_OBJECT_WLOCK(object);
255                 vm_page_lock(*mres);
256                 vm_page_free(*mres);
257                 vm_page_unlock(*mres);
258                 *mres = page;
259                 vm_page_insert(page, object, pidx);
260         }
261         page->valid = VM_PAGE_BITS_ALL;
262         return (VM_PAGER_OK);
263 }
264
265
266 static struct cdev_pager_ops netmap_cdev_pager_ops = {
267         .cdev_pg_ctor = netmap_dev_pager_ctor,
268         .cdev_pg_dtor = netmap_dev_pager_dtor,
269         .cdev_pg_fault = netmap_dev_pager_fault,
270 };
271
272
273 static int
274 netmap_mmap_single(struct cdev *cdev, vm_ooffset_t *foff,
275         vm_size_t objsize,  vm_object_t *objp, int prot)
276 {
277         int error;
278         struct netmap_vm_handle_t *vmh;
279         struct netmap_priv_d *priv;
280         vm_object_t obj;
281
282         D("cdev %p foff %jd size %jd objp %p prot %d", cdev,
283             (intmax_t )*foff, (intmax_t )objsize, objp, prot);
284
285         vmh = kmalloc(sizeof(struct netmap_vm_handle_t), M_DEVBUF,
286                               M_NOWAIT | M_ZERO);
287         if (vmh == NULL)
288                 return ENOMEM;
289         vmh->dev = cdev;
290
291         NMG_LOCK();
292         error = devfs_get_cdevpriv((void**)&priv);
293         if (error)
294                 goto err_unlock;
295         vmh->priv = priv;
296         priv->np_refcount++;
297         NMG_UNLOCK();
298
299         error = netmap_get_memory(priv);
300         if (error)
301                 goto err_deref;
302
303         obj = cdev_pager_allocate(vmh, OBJT_DEVICE,
304                 &netmap_cdev_pager_ops, objsize, prot,
305                 *foff, NULL);
306         if (obj == NULL) {
307                 D("cdev_pager_allocate failed");
308                 error = EINVAL;
309                 goto err_deref;
310         }
311
312         *objp = obj;
313         return 0;
314
315 err_deref:
316         NMG_LOCK();
317         priv->np_refcount--;
318 err_unlock:
319         NMG_UNLOCK();
320 // err:
321         kfree(vmh, M_DEVBUF);
322         return error;
323 }
324
325
326 // XXX can we remove this ?
327 static int
328 netmap_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
329 {
330         if (netmap_verbose)
331                 D("dev %p fflag 0x%x devtype %d td %p",
332                         dev, fflag, devtype, td);
333         return 0;
334 }
335
336
337 static int
338 netmap_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
339 {
340         struct netmap_priv_d *priv;
341         int error;
342
343         (void)dev;
344         (void)oflags;
345         (void)devtype;
346         (void)td;
347
348         // XXX wait or nowait ?
349         priv = kmalloc(sizeof(struct netmap_priv_d), M_DEVBUF,
350                               M_NOWAIT | M_ZERO);
351         if (priv == NULL)
352                 return ENOMEM;
353
354         error = devfs_set_cdevpriv(priv, netmap_dtor);
355         if (error)
356                 return error;
357
358         priv->np_refcount = 1;
359
360         return 0;
361 }
362
363
364 struct cdevsw netmap_cdevsw = {
365         .d_version = D_VERSION,
366         .d_name = "netmap",
367         .d_open = netmap_open,
368         .d_mmap_single = netmap_mmap_single,
369         .d_ioctl = netmap_ioctl,
370         .d_poll = netmap_poll,
371         .d_close = netmap_close,
372 };
373
374
375 /*
376  * Kernel entry point.
377  *
378  * Initialize/finalize the module and return.
379  *
380  * Return 0 on success, errno on failure.
381  */
382 static int
383 netmap_loader(__unused struct module *module, int event, __unused void *arg)
384 {
385         int error = 0;
386
387         switch (event) {
388         case MOD_LOAD:
389                 error = netmap_init();
390                 break;
391
392         case MOD_UNLOAD:
393                 netmap_fini();
394                 break;
395
396         default:
397                 error = EOPNOTSUPP;
398                 break;
399         }
400
401         return (error);
402 }
403
404
405 DEV_MODULE(netmap, netmap_loader, NULL);