1718cf2ccSPedro F. Giffuni /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni *
417885a7bSLuigi Rizzo * Copyright (C) 2013-2014 Universita` di Pisa. All rights reserved.
5f9790aebSLuigi Rizzo *
6f9790aebSLuigi Rizzo * Redistribution and use in source and binary forms, with or without
7f9790aebSLuigi Rizzo * modification, are permitted provided that the following conditions
8f9790aebSLuigi Rizzo * are met:
9f9790aebSLuigi Rizzo * 1. Redistributions of source code must retain the above copyright
10f9790aebSLuigi Rizzo * notice, this list of conditions and the following disclaimer.
11f9790aebSLuigi Rizzo * 2. Redistributions in binary form must reproduce the above copyright
12f9790aebSLuigi Rizzo * notice, this list of conditions and the following disclaimer in the
13f9790aebSLuigi Rizzo * documentation and/or other materials provided with the distribution.
14f9790aebSLuigi Rizzo *
15f9790aebSLuigi Rizzo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16f9790aebSLuigi Rizzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17f9790aebSLuigi Rizzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18f9790aebSLuigi Rizzo * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19f9790aebSLuigi Rizzo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20f9790aebSLuigi Rizzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21f9790aebSLuigi Rizzo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22f9790aebSLuigi Rizzo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23f9790aebSLuigi Rizzo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24f9790aebSLuigi Rizzo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25f9790aebSLuigi Rizzo * SUCH DAMAGE.
26f9790aebSLuigi Rizzo */
27f9790aebSLuigi Rizzo
28847bf383SLuigi Rizzo #include "opt_inet.h"
29847bf383SLuigi Rizzo #include "opt_inet6.h"
30f9790aebSLuigi Rizzo
31ffaa5debSSepherosa Ziehau #include <sys/param.h>
32f9790aebSLuigi Rizzo #include <sys/module.h>
33f9790aebSLuigi Rizzo #include <sys/errno.h>
3404e0c883SConrad Meyer #include <sys/eventhandler.h>
35ffaa5debSSepherosa Ziehau #include <sys/jail.h>
36f0ea3689SLuigi Rizzo #include <sys/poll.h> /* POLLIN, POLLOUT */
37f9790aebSLuigi Rizzo #include <sys/kernel.h> /* types used in module initialization */
3837e3a6d3SLuigi Rizzo #include <sys/conf.h> /* DEV_MODULE_ORDERED */
39f0ea3689SLuigi Rizzo #include <sys/endian.h>
4037e3a6d3SLuigi Rizzo #include <sys/syscallsubr.h> /* kern_ioctl() */
41f9790aebSLuigi Rizzo
42f9790aebSLuigi Rizzo #include <sys/rwlock.h>
43f9790aebSLuigi Rizzo
44f9790aebSLuigi Rizzo #include <vm/vm.h> /* vtophys */
45f9790aebSLuigi Rizzo #include <vm/pmap.h> /* vtophys */
46f9790aebSLuigi Rizzo #include <vm/vm_param.h>
47f9790aebSLuigi Rizzo #include <vm/vm_object.h>
48f9790aebSLuigi Rizzo #include <vm/vm_page.h>
49f9790aebSLuigi Rizzo #include <vm/vm_pager.h>
50f9790aebSLuigi Rizzo #include <vm/uma.h>
51f9790aebSLuigi Rizzo
52f9790aebSLuigi Rizzo
53f9790aebSLuigi Rizzo #include <sys/malloc.h>
54f9790aebSLuigi Rizzo #include <sys/socket.h> /* sockaddrs */
55f9790aebSLuigi Rizzo #include <sys/selinfo.h>
5637e3a6d3SLuigi Rizzo #include <sys/kthread.h> /* kthread_add() */
5737e3a6d3SLuigi Rizzo #include <sys/proc.h> /* PROC_LOCK() */
5837e3a6d3SLuigi Rizzo #include <sys/unistd.h> /* RFNOWAIT */
5937e3a6d3SLuigi Rizzo #include <sys/sched.h> /* sched_bind() */
6037e3a6d3SLuigi Rizzo #include <sys/smp.h> /* mp_maxid */
6119c4ec08SVincenzo Maffione #include <sys/taskqueue.h> /* taskqueue_enqueue(), taskqueue_create(), ... */
62f9790aebSLuigi Rizzo #include <net/if.h>
63f9790aebSLuigi Rizzo #include <net/if_var.h>
644bf50f18SLuigi Rizzo #include <net/if_types.h> /* IFT_ETHER */
654bf50f18SLuigi Rizzo #include <net/ethernet.h> /* ether_ifdetach */
664bf50f18SLuigi Rizzo #include <net/if_dl.h> /* LLADDR */
67f9790aebSLuigi Rizzo #include <machine/bus.h> /* bus_dmamap_* */
68f0ea3689SLuigi Rizzo #include <netinet/in.h> /* in6_cksum_pseudo() */
69f0ea3689SLuigi Rizzo #include <machine/in_cksum.h> /* in_pseudo(), in_cksum_hdr() */
70f9790aebSLuigi Rizzo
71f9790aebSLuigi Rizzo #include <net/netmap.h>
72f9790aebSLuigi Rizzo #include <dev/netmap/netmap_kern.h>
7337e3a6d3SLuigi Rizzo #include <net/netmap_virt.h>
74f9790aebSLuigi Rizzo #include <dev/netmap/netmap_mem2.h>
75f9790aebSLuigi Rizzo
76f9790aebSLuigi Rizzo
77f9790aebSLuigi Rizzo /* ======================== FREEBSD-SPECIFIC ROUTINES ================== */
78f9790aebSLuigi Rizzo
7919c4ec08SVincenzo Maffione static void
nm_kqueue_notify(void * opaque,int pending)8019c4ec08SVincenzo Maffione nm_kqueue_notify(void *opaque, int pending)
8119c4ec08SVincenzo Maffione {
8219c4ec08SVincenzo Maffione struct nm_selinfo *si = opaque;
8319c4ec08SVincenzo Maffione
8419c4ec08SVincenzo Maffione /* We use a non-zero hint to distinguish this notification call
8519c4ec08SVincenzo Maffione * from the call done in kqueue_scan(), which uses hint=0.
8619c4ec08SVincenzo Maffione */
8719c4ec08SVincenzo Maffione KNOTE_UNLOCKED(&si->si.si_note, /*hint=*/0x100);
8819c4ec08SVincenzo Maffione }
8919c4ec08SVincenzo Maffione
nm_os_selinfo_init(NM_SELINFO_T * si,const char * name)9019c4ec08SVincenzo Maffione int nm_os_selinfo_init(NM_SELINFO_T *si, const char *name) {
9119c4ec08SVincenzo Maffione int err;
9219c4ec08SVincenzo Maffione
9319c4ec08SVincenzo Maffione TASK_INIT(&si->ntfytask, 0, nm_kqueue_notify, si);
9419c4ec08SVincenzo Maffione si->ntfytq = taskqueue_create(name, M_NOWAIT,
9519c4ec08SVincenzo Maffione taskqueue_thread_enqueue, &si->ntfytq);
9619c4ec08SVincenzo Maffione if (si->ntfytq == NULL)
9719c4ec08SVincenzo Maffione return -ENOMEM;
9819c4ec08SVincenzo Maffione err = taskqueue_start_threads(&si->ntfytq, 1, PI_NET, "tq %s", name);
9919c4ec08SVincenzo Maffione if (err) {
10019c4ec08SVincenzo Maffione taskqueue_free(si->ntfytq);
10119c4ec08SVincenzo Maffione si->ntfytq = NULL;
10219c4ec08SVincenzo Maffione return err;
10319c4ec08SVincenzo Maffione }
10419c4ec08SVincenzo Maffione
10519c4ec08SVincenzo Maffione snprintf(si->mtxname, sizeof(si->mtxname), "nmkl%s", name);
10619c4ec08SVincenzo Maffione mtx_init(&si->m, si->mtxname, NULL, MTX_DEF);
10719c4ec08SVincenzo Maffione knlist_init_mtx(&si->si.si_note, &si->m);
10845100257SVincenzo Maffione si->kqueue_users = 0;
10919c4ec08SVincenzo Maffione
11019c4ec08SVincenzo Maffione return (0);
11137e3a6d3SLuigi Rizzo }
11237e3a6d3SLuigi Rizzo
11337e3a6d3SLuigi Rizzo void
nm_os_selinfo_uninit(NM_SELINFO_T * si)11437e3a6d3SLuigi Rizzo nm_os_selinfo_uninit(NM_SELINFO_T *si)
11537e3a6d3SLuigi Rizzo {
11619c4ec08SVincenzo Maffione if (si->ntfytq == NULL) {
11719c4ec08SVincenzo Maffione return; /* si was not initialized */
11819c4ec08SVincenzo Maffione }
11919c4ec08SVincenzo Maffione taskqueue_drain(si->ntfytq, &si->ntfytask);
12019c4ec08SVincenzo Maffione taskqueue_free(si->ntfytq);
12119c4ec08SVincenzo Maffione si->ntfytq = NULL;
1228c9874f5SVincenzo Maffione knlist_delete(&si->si.si_note, curthread, /*islocked=*/0);
12337e3a6d3SLuigi Rizzo knlist_destroy(&si->si.si_note);
12437e3a6d3SLuigi Rizzo /* now we don't need the mutex anymore */
12537e3a6d3SLuigi Rizzo mtx_destroy(&si->m);
12637e3a6d3SLuigi Rizzo }
12737e3a6d3SLuigi Rizzo
128c3e9b4dbSLuiz Otavio O Souza void *
nm_os_malloc(size_t size)129c3e9b4dbSLuiz Otavio O Souza nm_os_malloc(size_t size)
130c3e9b4dbSLuiz Otavio O Souza {
131c3e9b4dbSLuiz Otavio O Souza return malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
132c3e9b4dbSLuiz Otavio O Souza }
133c3e9b4dbSLuiz Otavio O Souza
134c3e9b4dbSLuiz Otavio O Souza void *
nm_os_realloc(void * addr,size_t new_size,size_t old_size __unused)135c3e9b4dbSLuiz Otavio O Souza nm_os_realloc(void *addr, size_t new_size, size_t old_size __unused)
136c3e9b4dbSLuiz Otavio O Souza {
137c3e9b4dbSLuiz Otavio O Souza return realloc(addr, new_size, M_DEVBUF, M_NOWAIT | M_ZERO);
138c3e9b4dbSLuiz Otavio O Souza }
139c3e9b4dbSLuiz Otavio O Souza
140c3e9b4dbSLuiz Otavio O Souza void
nm_os_free(void * addr)141c3e9b4dbSLuiz Otavio O Souza nm_os_free(void *addr)
142c3e9b4dbSLuiz Otavio O Souza {
143c3e9b4dbSLuiz Otavio O Souza free(addr, M_DEVBUF);
144c3e9b4dbSLuiz Otavio O Souza }
145c3e9b4dbSLuiz Otavio O Souza
14637e3a6d3SLuigi Rizzo void
nm_os_ifnet_lock(void)14737e3a6d3SLuigi Rizzo nm_os_ifnet_lock(void)
14837e3a6d3SLuigi Rizzo {
149869d8878SAdrian Chadd IFNET_RLOCK();
15037e3a6d3SLuigi Rizzo }
15137e3a6d3SLuigi Rizzo
15237e3a6d3SLuigi Rizzo void
nm_os_ifnet_unlock(void)15337e3a6d3SLuigi Rizzo nm_os_ifnet_unlock(void)
15437e3a6d3SLuigi Rizzo {
15567ca1051SAdrian Chadd IFNET_RUNLOCK();
15637e3a6d3SLuigi Rizzo }
15737e3a6d3SLuigi Rizzo
15837e3a6d3SLuigi Rizzo static int netmap_use_count = 0;
15937e3a6d3SLuigi Rizzo
16037e3a6d3SLuigi Rizzo void
nm_os_get_module(void)16137e3a6d3SLuigi Rizzo nm_os_get_module(void)
16237e3a6d3SLuigi Rizzo {
16337e3a6d3SLuigi Rizzo netmap_use_count++;
16437e3a6d3SLuigi Rizzo }
16537e3a6d3SLuigi Rizzo
16637e3a6d3SLuigi Rizzo void
nm_os_put_module(void)16737e3a6d3SLuigi Rizzo nm_os_put_module(void)
16837e3a6d3SLuigi Rizzo {
16937e3a6d3SLuigi Rizzo netmap_use_count--;
17037e3a6d3SLuigi Rizzo }
17137e3a6d3SLuigi Rizzo
17237e3a6d3SLuigi Rizzo static void
netmap_ifnet_arrival_handler(void * arg __unused,if_t ifp)173e330262fSJustin Hibbits netmap_ifnet_arrival_handler(void *arg __unused, if_t ifp)
17437e3a6d3SLuigi Rizzo {
17537e3a6d3SLuigi Rizzo netmap_undo_zombie(ifp);
17637e3a6d3SLuigi Rizzo }
17737e3a6d3SLuigi Rizzo
17837e3a6d3SLuigi Rizzo static void
netmap_ifnet_departure_handler(void * arg __unused,if_t ifp)179e330262fSJustin Hibbits netmap_ifnet_departure_handler(void *arg __unused, if_t ifp)
18037e3a6d3SLuigi Rizzo {
18137e3a6d3SLuigi Rizzo netmap_make_zombie(ifp);
18237e3a6d3SLuigi Rizzo }
18337e3a6d3SLuigi Rizzo
18437e3a6d3SLuigi Rizzo static eventhandler_tag nm_ifnet_ah_tag;
18537e3a6d3SLuigi Rizzo static eventhandler_tag nm_ifnet_dh_tag;
18637e3a6d3SLuigi Rizzo
18737e3a6d3SLuigi Rizzo int
nm_os_ifnet_init(void)18837e3a6d3SLuigi Rizzo nm_os_ifnet_init(void)
18937e3a6d3SLuigi Rizzo {
19037e3a6d3SLuigi Rizzo nm_ifnet_ah_tag =
19137e3a6d3SLuigi Rizzo EVENTHANDLER_REGISTER(ifnet_arrival_event,
19237e3a6d3SLuigi Rizzo netmap_ifnet_arrival_handler,
19337e3a6d3SLuigi Rizzo NULL, EVENTHANDLER_PRI_ANY);
19437e3a6d3SLuigi Rizzo nm_ifnet_dh_tag =
19537e3a6d3SLuigi Rizzo EVENTHANDLER_REGISTER(ifnet_departure_event,
19637e3a6d3SLuigi Rizzo netmap_ifnet_departure_handler,
19737e3a6d3SLuigi Rizzo NULL, EVENTHANDLER_PRI_ANY);
19837e3a6d3SLuigi Rizzo return 0;
19937e3a6d3SLuigi Rizzo }
20037e3a6d3SLuigi Rizzo
20137e3a6d3SLuigi Rizzo void
nm_os_ifnet_fini(void)20237e3a6d3SLuigi Rizzo nm_os_ifnet_fini(void)
20337e3a6d3SLuigi Rizzo {
20437e3a6d3SLuigi Rizzo EVENTHANDLER_DEREGISTER(ifnet_arrival_event,
20537e3a6d3SLuigi Rizzo nm_ifnet_ah_tag);
20637e3a6d3SLuigi Rizzo EVENTHANDLER_DEREGISTER(ifnet_departure_event,
20737e3a6d3SLuigi Rizzo nm_ifnet_dh_tag);
20837e3a6d3SLuigi Rizzo }
20937e3a6d3SLuigi Rizzo
2104f80b14cSVincenzo Maffione unsigned
nm_os_ifnet_mtu(if_t ifp)211e330262fSJustin Hibbits nm_os_ifnet_mtu(if_t ifp)
2124f80b14cSVincenzo Maffione {
213e330262fSJustin Hibbits return if_getmtu(ifp);
2144f80b14cSVincenzo Maffione }
2154f80b14cSVincenzo Maffione
216e4166283SLuigi Rizzo rawsum_t
nm_os_csum_raw(uint8_t * data,size_t len,rawsum_t cur_sum)21737e3a6d3SLuigi Rizzo nm_os_csum_raw(uint8_t *data, size_t len, rawsum_t cur_sum)
218f0ea3689SLuigi Rizzo {
219f0ea3689SLuigi Rizzo /* TODO XXX please use the FreeBSD implementation for this. */
220f0ea3689SLuigi Rizzo uint16_t *words = (uint16_t *)data;
221f0ea3689SLuigi Rizzo int nw = len / 2;
222f0ea3689SLuigi Rizzo int i;
223f0ea3689SLuigi Rizzo
224f0ea3689SLuigi Rizzo for (i = 0; i < nw; i++)
225f0ea3689SLuigi Rizzo cur_sum += be16toh(words[i]);
226f0ea3689SLuigi Rizzo
227f0ea3689SLuigi Rizzo if (len & 1)
228f0ea3689SLuigi Rizzo cur_sum += (data[len-1] << 8);
229f0ea3689SLuigi Rizzo
230f0ea3689SLuigi Rizzo return cur_sum;
231f0ea3689SLuigi Rizzo }
232f0ea3689SLuigi Rizzo
233f0ea3689SLuigi Rizzo /* Fold a raw checksum: 'cur_sum' is in host byte order, while the
234f0ea3689SLuigi Rizzo * return value is in network byte order.
235f0ea3689SLuigi Rizzo */
236e4166283SLuigi Rizzo uint16_t
nm_os_csum_fold(rawsum_t cur_sum)23737e3a6d3SLuigi Rizzo nm_os_csum_fold(rawsum_t cur_sum)
238f0ea3689SLuigi Rizzo {
239f0ea3689SLuigi Rizzo /* TODO XXX please use the FreeBSD implementation for this. */
240f0ea3689SLuigi Rizzo while (cur_sum >> 16)
241f0ea3689SLuigi Rizzo cur_sum = (cur_sum & 0xFFFF) + (cur_sum >> 16);
242f0ea3689SLuigi Rizzo
243f0ea3689SLuigi Rizzo return htobe16((~cur_sum) & 0xFFFF);
244f0ea3689SLuigi Rizzo }
245f0ea3689SLuigi Rizzo
nm_os_csum_ipv4(struct nm_iphdr * iph)24637e3a6d3SLuigi Rizzo uint16_t nm_os_csum_ipv4(struct nm_iphdr *iph)
247f0ea3689SLuigi Rizzo {
248f0ea3689SLuigi Rizzo #if 0
249f0ea3689SLuigi Rizzo return in_cksum_hdr((void *)iph);
250f0ea3689SLuigi Rizzo #else
25137e3a6d3SLuigi Rizzo return nm_os_csum_fold(nm_os_csum_raw((uint8_t*)iph, sizeof(struct nm_iphdr), 0));
252f0ea3689SLuigi Rizzo #endif
253f0ea3689SLuigi Rizzo }
254f0ea3689SLuigi Rizzo
255e4166283SLuigi Rizzo void
nm_os_csum_tcpudp_ipv4(struct nm_iphdr * iph,void * data,size_t datalen,uint16_t * check)25637e3a6d3SLuigi Rizzo nm_os_csum_tcpudp_ipv4(struct nm_iphdr *iph, void *data,
257f0ea3689SLuigi Rizzo size_t datalen, uint16_t *check)
258f0ea3689SLuigi Rizzo {
2595a067ae1SLuigi Rizzo #ifdef INET
260f0ea3689SLuigi Rizzo uint16_t pseudolen = datalen + iph->protocol;
261f0ea3689SLuigi Rizzo
26245c67e8fSVincenzo Maffione /* Compute and insert the pseudo-header checksum. */
263f0ea3689SLuigi Rizzo *check = in_pseudo(iph->saddr, iph->daddr,
264f0ea3689SLuigi Rizzo htobe16(pseudolen));
265f0ea3689SLuigi Rizzo /* Compute the checksum on TCP/UDP header + payload
266f0ea3689SLuigi Rizzo * (includes the pseudo-header).
267f0ea3689SLuigi Rizzo */
26837e3a6d3SLuigi Rizzo *check = nm_os_csum_fold(nm_os_csum_raw(data, datalen, 0));
2695a067ae1SLuigi Rizzo #else
2705a067ae1SLuigi Rizzo static int notsupported = 0;
2715a067ae1SLuigi Rizzo if (!notsupported) {
2725a067ae1SLuigi Rizzo notsupported = 1;
273a56136a1SVincenzo Maffione nm_prerr("inet4 segmentation not supported");
2745a067ae1SLuigi Rizzo }
2755a067ae1SLuigi Rizzo #endif
276f0ea3689SLuigi Rizzo }
277f0ea3689SLuigi Rizzo
278e4166283SLuigi Rizzo void
nm_os_csum_tcpudp_ipv6(struct nm_ipv6hdr * ip6h,void * data,size_t datalen,uint16_t * check)27937e3a6d3SLuigi Rizzo nm_os_csum_tcpudp_ipv6(struct nm_ipv6hdr *ip6h, void *data,
280f0ea3689SLuigi Rizzo size_t datalen, uint16_t *check)
281f0ea3689SLuigi Rizzo {
282f0ea3689SLuigi Rizzo #ifdef INET6
283f0ea3689SLuigi Rizzo *check = in6_cksum_pseudo((void*)ip6h, datalen, ip6h->nexthdr, 0);
28437e3a6d3SLuigi Rizzo *check = nm_os_csum_fold(nm_os_csum_raw(data, datalen, 0));
285f0ea3689SLuigi Rizzo #else
286f0ea3689SLuigi Rizzo static int notsupported = 0;
287f0ea3689SLuigi Rizzo if (!notsupported) {
288f0ea3689SLuigi Rizzo notsupported = 1;
289a56136a1SVincenzo Maffione nm_prerr("inet6 segmentation not supported");
290f0ea3689SLuigi Rizzo }
291f0ea3689SLuigi Rizzo #endif
292f0ea3689SLuigi Rizzo }
293f0ea3689SLuigi Rizzo
29437e3a6d3SLuigi Rizzo /* on FreeBSD we send up one packet at a time */
29537e3a6d3SLuigi Rizzo void *
nm_os_send_up(if_t ifp,struct mbuf * m,struct mbuf * prev)296e330262fSJustin Hibbits nm_os_send_up(if_t ifp, struct mbuf *m, struct mbuf *prev)
29737e3a6d3SLuigi Rizzo {
29837e3a6d3SLuigi Rizzo NA(ifp)->if_input(ifp, m);
29937e3a6d3SLuigi Rizzo return NULL;
30037e3a6d3SLuigi Rizzo }
30137e3a6d3SLuigi Rizzo
30237e3a6d3SLuigi Rizzo int
nm_os_mbuf_has_csum_offld(struct mbuf * m)3032a7db7a6SVincenzo Maffione nm_os_mbuf_has_csum_offld(struct mbuf *m)
30437e3a6d3SLuigi Rizzo {
30537e3a6d3SLuigi Rizzo return m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP | CSUM_SCTP |
30637e3a6d3SLuigi Rizzo CSUM_TCP_IPV6 | CSUM_UDP_IPV6 |
3072a7db7a6SVincenzo Maffione CSUM_SCTP_IPV6);
3082a7db7a6SVincenzo Maffione }
3092a7db7a6SVincenzo Maffione
3102a7db7a6SVincenzo Maffione int
nm_os_mbuf_has_seg_offld(struct mbuf * m)3112a7db7a6SVincenzo Maffione nm_os_mbuf_has_seg_offld(struct mbuf *m)
3122a7db7a6SVincenzo Maffione {
3132a7db7a6SVincenzo Maffione return m->m_pkthdr.csum_flags & CSUM_TSO;
31437e3a6d3SLuigi Rizzo }
31537e3a6d3SLuigi Rizzo
31637e3a6d3SLuigi Rizzo static void
freebsd_generic_rx_handler(if_t ifp,struct mbuf * m)317e330262fSJustin Hibbits freebsd_generic_rx_handler(if_t ifp, struct mbuf *m)
31837e3a6d3SLuigi Rizzo {
319c3e9b4dbSLuiz Otavio O Souza int stolen;
320c3e9b4dbSLuiz Otavio O Souza
321a56136a1SVincenzo Maffione if (unlikely(!NM_NA_VALID(ifp))) {
322a56136a1SVincenzo Maffione nm_prlim(1, "Warning: RX packet intercepted, but no"
323a56136a1SVincenzo Maffione " emulated adapter");
324c3e9b4dbSLuiz Otavio O Souza return;
325c3e9b4dbSLuiz Otavio O Souza }
326c3e9b4dbSLuiz Otavio O Souza
3275f6d3778SMark Johnston do {
3285f6d3778SMark Johnston struct mbuf *n;
3295f6d3778SMark Johnston
3305f6d3778SMark Johnston n = m->m_nextpkt;
3315f6d3778SMark Johnston m->m_nextpkt = NULL;
332c3e9b4dbSLuiz Otavio O Souza stolen = generic_rx_handler(ifp, m);
333c3e9b4dbSLuiz Otavio O Souza if (!stolen) {
3346c9fe357SVincenzo Maffione NA(ifp)->if_input(ifp, m);
33537e3a6d3SLuigi Rizzo }
3365f6d3778SMark Johnston m = n;
3375f6d3778SMark Johnston } while (m != NULL);
33837e3a6d3SLuigi Rizzo }
339f0ea3689SLuigi Rizzo
340f9790aebSLuigi Rizzo /*
341f9790aebSLuigi Rizzo * Intercept the rx routine in the standard device driver.
342f9790aebSLuigi Rizzo * Second argument is non-zero to intercept, 0 to restore
343f9790aebSLuigi Rizzo */
344f9790aebSLuigi Rizzo int
nm_os_catch_rx(struct netmap_generic_adapter * gna,int intercept)34537e3a6d3SLuigi Rizzo nm_os_catch_rx(struct netmap_generic_adapter *gna, int intercept)
346f9790aebSLuigi Rizzo {
347847bf383SLuigi Rizzo struct netmap_adapter *na = &gna->up.up;
348e330262fSJustin Hibbits if_t ifp = na->ifp;
3494f80b14cSVincenzo Maffione int ret = 0;
350f9790aebSLuigi Rizzo
3514f80b14cSVincenzo Maffione nm_os_ifnet_lock();
352f9790aebSLuigi Rizzo if (intercept) {
353e330262fSJustin Hibbits if_setcapenablebit(ifp, IFCAP_NETMAP, 0);
354e330262fSJustin Hibbits if_setinputfn(ifp, freebsd_generic_rx_handler);
355f9790aebSLuigi Rizzo } else {
356e330262fSJustin Hibbits if_setcapenablebit(ifp, 0, IFCAP_NETMAP);
3576c9fe357SVincenzo Maffione if_setinputfn(ifp, na->if_input);
358f9790aebSLuigi Rizzo }
3594f80b14cSVincenzo Maffione nm_os_ifnet_unlock();
360f9790aebSLuigi Rizzo
3614f80b14cSVincenzo Maffione return ret;
362f9790aebSLuigi Rizzo }
363f9790aebSLuigi Rizzo
36417885a7bSLuigi Rizzo
365f9790aebSLuigi Rizzo /*
366f9790aebSLuigi Rizzo * Intercept the packet steering routine in the tx path,
367f9790aebSLuigi Rizzo * so that we can decide which queue is used for an mbuf.
368f9790aebSLuigi Rizzo * Second argument is non-zero to intercept, 0 to restore.
369f0ea3689SLuigi Rizzo * On freebsd we just intercept if_transmit.
370f9790aebSLuigi Rizzo */
37137e3a6d3SLuigi Rizzo int
nm_os_catch_tx(struct netmap_generic_adapter * gna,int intercept)37237e3a6d3SLuigi Rizzo nm_os_catch_tx(struct netmap_generic_adapter *gna, int intercept)
373f9790aebSLuigi Rizzo {
37417885a7bSLuigi Rizzo struct netmap_adapter *na = &gna->up.up;
375e330262fSJustin Hibbits if_t ifp = netmap_generic_getifp(gna);
37617885a7bSLuigi Rizzo
3774f80b14cSVincenzo Maffione nm_os_ifnet_lock();
37837e3a6d3SLuigi Rizzo if (intercept) {
379e330262fSJustin Hibbits na->if_transmit = if_gettransmitfn(ifp);
380e330262fSJustin Hibbits if_settransmitfn(ifp, netmap_transmit);
381f9790aebSLuigi Rizzo } else {
382e330262fSJustin Hibbits if_settransmitfn(ifp, na->if_transmit);
383f9790aebSLuigi Rizzo }
3844f80b14cSVincenzo Maffione nm_os_ifnet_unlock();
38537e3a6d3SLuigi Rizzo
38637e3a6d3SLuigi Rizzo return 0;
387f9790aebSLuigi Rizzo }
388f9790aebSLuigi Rizzo
38917885a7bSLuigi Rizzo
390f0ea3689SLuigi Rizzo /*
391f0ea3689SLuigi Rizzo * Transmit routine used by generic_netmap_txsync(). Returns 0 on success
392f9790aebSLuigi Rizzo * and non-zero on error (which may be packet drops or other errors).
393f9790aebSLuigi Rizzo * addr and len identify the netmap buffer, m is the (preallocated)
394f9790aebSLuigi Rizzo * mbuf to use for transmissions.
395f9790aebSLuigi Rizzo *
396ce12afaaSMark Johnston * Zero-copy transmission is possible if netmap is attached directly to a
397ce12afaaSMark Johnston * hardware interface: when cleaning we simply wait for the mbuf cluster
398ce12afaaSMark Johnston * refcount to decrement to 1, indicating that the driver has completed
399ce12afaaSMark Johnston * transmission and is done with the buffer. However, this approach can
400ce12afaaSMark Johnston * lead to queue deadlocks when attaching to software interfaces (e.g.,
401ce12afaaSMark Johnston * if_bridge) since we cannot rely on member ports to promptly reclaim
402ce12afaaSMark Johnston * transmitted mbufs. Since there is no easy way to distinguish these
403ce12afaaSMark Johnston * cases, we currently always copy the buffer.
404f9790aebSLuigi Rizzo *
405ce12afaaSMark Johnston * On multiqueue cards, we can force the queue using
406c2529042SHans Petter Selasky * if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
407f9790aebSLuigi Rizzo * i = m->m_pkthdr.flowid % adapter->num_queues;
408f9790aebSLuigi Rizzo * else
409f9790aebSLuigi Rizzo * i = curcpu % adapter->num_queues;
410f9790aebSLuigi Rizzo */
411f9790aebSLuigi Rizzo int
nm_os_generic_xmit_frame(struct nm_os_gen_arg * a)41237e3a6d3SLuigi Rizzo nm_os_generic_xmit_frame(struct nm_os_gen_arg *a)
413f9790aebSLuigi Rizzo {
414f9790aebSLuigi Rizzo int ret;
41537e3a6d3SLuigi Rizzo u_int len = a->len;
416e330262fSJustin Hibbits if_t ifp = a->ifp;
41737e3a6d3SLuigi Rizzo struct mbuf *m = a->m;
418f9790aebSLuigi Rizzo
419ce12afaaSMark Johnston M_ASSERTPKTHDR(m);
420ce12afaaSMark Johnston KASSERT((m->m_flags & M_EXT) != 0,
421ce12afaaSMark Johnston ("%s: mbuf %p has no cluster", __func__, m));
42237e3a6d3SLuigi Rizzo
423ce12afaaSMark Johnston if (MBUF_REFCNT(m) != 1) {
424ce12afaaSMark Johnston nm_prerr("invalid refcnt %d for %p", MBUF_REFCNT(m), m);
425ce12afaaSMark Johnston panic("in generic_xmit_frame");
426ce12afaaSMark Johnston }
427ce12afaaSMark Johnston if (unlikely(m->m_ext.ext_size < len)) {
428ce12afaaSMark Johnston nm_prlim(2, "size %d < len %d", m->m_ext.ext_size, len);
429ce12afaaSMark Johnston len = m->m_ext.ext_size;
430ce12afaaSMark Johnston }
431ce12afaaSMark Johnston
432ce12afaaSMark Johnston m_copyback(m, 0, len, a->addr);
433e4166283SLuigi Rizzo m->m_len = m->m_pkthdr.len = len;
43437e3a6d3SLuigi Rizzo SET_MBUF_REFCNT(m, 2);
435c2529042SHans Petter Selasky M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE);
43637e3a6d3SLuigi Rizzo m->m_pkthdr.flowid = a->ring_nr;
437f9790aebSLuigi Rizzo m->m_pkthdr.rcvif = ifp; /* used for tx notification */
438e330262fSJustin Hibbits CURVNET_SET(if_getvnet(ifp));
43917885a7bSLuigi Rizzo ret = NA(ifp)->if_transmit(ifp, m);
44023ced944SVincenzo Maffione CURVNET_RESTORE();
44137e3a6d3SLuigi Rizzo return ret ? -1 : 0;
442f9790aebSLuigi Rizzo }
443f9790aebSLuigi Rizzo
4440dc809c0SLuigi Rizzo struct netmap_adapter *
netmap_getna(if_t ifp)4450dc809c0SLuigi Rizzo netmap_getna(if_t ifp)
4460dc809c0SLuigi Rizzo {
447e330262fSJustin Hibbits return (NA(ifp));
4480dc809c0SLuigi Rizzo }
4490dc809c0SLuigi Rizzo
450f9790aebSLuigi Rizzo /*
451f9790aebSLuigi Rizzo * The following two functions are empty until we have a generic
452f9790aebSLuigi Rizzo * way to extract the info from the ifp
453f9790aebSLuigi Rizzo */
454f9790aebSLuigi Rizzo int
nm_os_generic_find_num_desc(if_t ifp,unsigned int * tx,unsigned int * rx)455e330262fSJustin Hibbits nm_os_generic_find_num_desc(if_t ifp, unsigned int *tx, unsigned int *rx)
456f9790aebSLuigi Rizzo {
457f9790aebSLuigi Rizzo return 0;
458f9790aebSLuigi Rizzo }
459f9790aebSLuigi Rizzo
46017885a7bSLuigi Rizzo
461f9790aebSLuigi Rizzo void
nm_os_generic_find_num_queues(if_t ifp,u_int * txq,u_int * rxq)462e330262fSJustin Hibbits nm_os_generic_find_num_queues(if_t ifp, u_int *txq, u_int *rxq)
463f9790aebSLuigi Rizzo {
464c3e9b4dbSLuiz Otavio O Souza unsigned num_rings = netmap_generic_rings ? netmap_generic_rings : 1;
465c3e9b4dbSLuiz Otavio O Souza
466c3e9b4dbSLuiz Otavio O Souza *txq = num_rings;
467c3e9b4dbSLuiz Otavio O Souza *rxq = num_rings;
468f9790aebSLuigi Rizzo }
469f9790aebSLuigi Rizzo
47037e3a6d3SLuigi Rizzo void
nm_os_generic_set_features(struct netmap_generic_adapter * gna)47137e3a6d3SLuigi Rizzo nm_os_generic_set_features(struct netmap_generic_adapter *gna)
47237e3a6d3SLuigi Rizzo {
47337e3a6d3SLuigi Rizzo
47437e3a6d3SLuigi Rizzo gna->rxsg = 1; /* Supported through m_copydata. */
47537e3a6d3SLuigi Rizzo gna->txqdisc = 0; /* Not supported. */
47637e3a6d3SLuigi Rizzo }
47717885a7bSLuigi Rizzo
478e4166283SLuigi Rizzo void
nm_os_mitigation_init(struct nm_generic_mit * mit,int idx,struct netmap_adapter * na)47937e3a6d3SLuigi Rizzo nm_os_mitigation_init(struct nm_generic_mit *mit, int idx, struct netmap_adapter *na)
480f9790aebSLuigi Rizzo {
481f0ea3689SLuigi Rizzo mit->mit_pending = 0;
4824bf50f18SLuigi Rizzo mit->mit_ring_idx = idx;
483f0ea3689SLuigi Rizzo mit->mit_na = na;
484f9790aebSLuigi Rizzo }
485f9790aebSLuigi Rizzo
486f9790aebSLuigi Rizzo
487e4166283SLuigi Rizzo void
nm_os_mitigation_start(struct nm_generic_mit * mit)48837e3a6d3SLuigi Rizzo nm_os_mitigation_start(struct nm_generic_mit *mit)
489f9790aebSLuigi Rizzo {
490f9790aebSLuigi Rizzo }
491f9790aebSLuigi Rizzo
49217885a7bSLuigi Rizzo
493e4166283SLuigi Rizzo void
nm_os_mitigation_restart(struct nm_generic_mit * mit)49437e3a6d3SLuigi Rizzo nm_os_mitigation_restart(struct nm_generic_mit *mit)
495f9790aebSLuigi Rizzo {
496f9790aebSLuigi Rizzo }
497f9790aebSLuigi Rizzo
49817885a7bSLuigi Rizzo
499e4166283SLuigi Rizzo int
nm_os_mitigation_active(struct nm_generic_mit * mit)50037e3a6d3SLuigi Rizzo nm_os_mitigation_active(struct nm_generic_mit *mit)
501f9790aebSLuigi Rizzo {
502a56136a1SVincenzo Maffione
503f9790aebSLuigi Rizzo return 0;
504f9790aebSLuigi Rizzo }
505f9790aebSLuigi Rizzo
50617885a7bSLuigi Rizzo
507e4166283SLuigi Rizzo void
nm_os_mitigation_cleanup(struct nm_generic_mit * mit)50837e3a6d3SLuigi Rizzo nm_os_mitigation_cleanup(struct nm_generic_mit *mit)
509f9790aebSLuigi Rizzo {
510f9790aebSLuigi Rizzo }
511f9790aebSLuigi Rizzo
5124bf50f18SLuigi Rizzo static int
nm_vi_dummy(if_t ifp,u_long cmd,caddr_t addr)513e330262fSJustin Hibbits nm_vi_dummy(if_t ifp, u_long cmd, caddr_t addr)
5144bf50f18SLuigi Rizzo {
515a56136a1SVincenzo Maffione
5164bf50f18SLuigi Rizzo return EINVAL;
5174bf50f18SLuigi Rizzo }
5184bf50f18SLuigi Rizzo
5194bf50f18SLuigi Rizzo static void
nm_vi_start(if_t ifp)520e330262fSJustin Hibbits nm_vi_start(if_t ifp)
5214bf50f18SLuigi Rizzo {
5224bf50f18SLuigi Rizzo panic("nm_vi_start() must not be called");
5234bf50f18SLuigi Rizzo }
5244bf50f18SLuigi Rizzo
5254bf50f18SLuigi Rizzo /*
5264bf50f18SLuigi Rizzo * Index manager of persistent virtual interfaces.
5274bf50f18SLuigi Rizzo * It is used to decide the lowest byte of the MAC address.
5284bf50f18SLuigi Rizzo * We use the same algorithm with management of bridge port index.
5294bf50f18SLuigi Rizzo */
5304bf50f18SLuigi Rizzo #define NM_VI_MAX 255
5314bf50f18SLuigi Rizzo static struct {
5324bf50f18SLuigi Rizzo uint8_t index[NM_VI_MAX]; /* XXX just for a reasonable number */
5334bf50f18SLuigi Rizzo uint8_t active;
5344bf50f18SLuigi Rizzo struct mtx lock;
5354bf50f18SLuigi Rizzo } nm_vi_indices;
5364bf50f18SLuigi Rizzo
5374bf50f18SLuigi Rizzo void
nm_os_vi_init_index(void)53837e3a6d3SLuigi Rizzo nm_os_vi_init_index(void)
5394bf50f18SLuigi Rizzo {
5404bf50f18SLuigi Rizzo int i;
5414bf50f18SLuigi Rizzo for (i = 0; i < NM_VI_MAX; i++)
5424bf50f18SLuigi Rizzo nm_vi_indices.index[i] = i;
5434bf50f18SLuigi Rizzo nm_vi_indices.active = 0;
5444bf50f18SLuigi Rizzo mtx_init(&nm_vi_indices.lock, "nm_vi_indices_lock", NULL, MTX_DEF);
5454bf50f18SLuigi Rizzo }
5464bf50f18SLuigi Rizzo
5474bf50f18SLuigi Rizzo /* return -1 if no index available */
5484bf50f18SLuigi Rizzo static int
nm_vi_get_index(void)5494bf50f18SLuigi Rizzo nm_vi_get_index(void)
5504bf50f18SLuigi Rizzo {
5514bf50f18SLuigi Rizzo int ret;
5524bf50f18SLuigi Rizzo
5534bf50f18SLuigi Rizzo mtx_lock(&nm_vi_indices.lock);
5544bf50f18SLuigi Rizzo ret = nm_vi_indices.active == NM_VI_MAX ? -1 :
5554bf50f18SLuigi Rizzo nm_vi_indices.index[nm_vi_indices.active++];
5564bf50f18SLuigi Rizzo mtx_unlock(&nm_vi_indices.lock);
5574bf50f18SLuigi Rizzo return ret;
5584bf50f18SLuigi Rizzo }
5594bf50f18SLuigi Rizzo
5604bf50f18SLuigi Rizzo static void
nm_vi_free_index(uint8_t val)5614bf50f18SLuigi Rizzo nm_vi_free_index(uint8_t val)
5624bf50f18SLuigi Rizzo {
5634bf50f18SLuigi Rizzo int i, lim;
5644bf50f18SLuigi Rizzo
5654bf50f18SLuigi Rizzo mtx_lock(&nm_vi_indices.lock);
5664bf50f18SLuigi Rizzo lim = nm_vi_indices.active;
5674bf50f18SLuigi Rizzo for (i = 0; i < lim; i++) {
5684bf50f18SLuigi Rizzo if (nm_vi_indices.index[i] == val) {
5694bf50f18SLuigi Rizzo /* swap index[lim-1] and j */
5704bf50f18SLuigi Rizzo int tmp = nm_vi_indices.index[lim-1];
5714bf50f18SLuigi Rizzo nm_vi_indices.index[lim-1] = val;
5724bf50f18SLuigi Rizzo nm_vi_indices.index[i] = tmp;
5734bf50f18SLuigi Rizzo nm_vi_indices.active--;
5744bf50f18SLuigi Rizzo break;
5754bf50f18SLuigi Rizzo }
5764bf50f18SLuigi Rizzo }
5774bf50f18SLuigi Rizzo if (lim == nm_vi_indices.active)
578a56136a1SVincenzo Maffione nm_prerr("Index %u not found", val);
5794bf50f18SLuigi Rizzo mtx_unlock(&nm_vi_indices.lock);
5804bf50f18SLuigi Rizzo }
5814bf50f18SLuigi Rizzo #undef NM_VI_MAX
5824bf50f18SLuigi Rizzo
5834bf50f18SLuigi Rizzo /*
5844bf50f18SLuigi Rizzo * Implementation of a netmap-capable virtual interface that
5854bf50f18SLuigi Rizzo * registered to the system.
5864bf50f18SLuigi Rizzo * It is based on if_tap.c and ip_fw_log.c in FreeBSD 9.
5874bf50f18SLuigi Rizzo *
5884bf50f18SLuigi Rizzo * Note: Linux sets refcount to 0 on allocation of net_device,
5894bf50f18SLuigi Rizzo * then increments it on registration to the system.
5904bf50f18SLuigi Rizzo * FreeBSD sets refcount to 1 on if_alloc(), and does not
5914bf50f18SLuigi Rizzo * increment this refcount on if_attach().
5924bf50f18SLuigi Rizzo */
5934bf50f18SLuigi Rizzo int
nm_os_vi_persist(const char * name,if_t * ret)594e330262fSJustin Hibbits nm_os_vi_persist(const char *name, if_t *ret)
5954bf50f18SLuigi Rizzo {
596e330262fSJustin Hibbits if_t ifp;
5974bf50f18SLuigi Rizzo u_short macaddr_hi;
5984bf50f18SLuigi Rizzo uint32_t macaddr_mid;
5994bf50f18SLuigi Rizzo u_char eaddr[6];
6004bf50f18SLuigi Rizzo int unit = nm_vi_get_index(); /* just to decide MAC address */
6014bf50f18SLuigi Rizzo
6024bf50f18SLuigi Rizzo if (unit < 0)
6034bf50f18SLuigi Rizzo return EBUSY;
6044bf50f18SLuigi Rizzo /*
6054bf50f18SLuigi Rizzo * We use the same MAC address generation method with tap
6064bf50f18SLuigi Rizzo * except for the highest octet is 00:be instead of 00:bd
6074bf50f18SLuigi Rizzo */
6084bf50f18SLuigi Rizzo macaddr_hi = htons(0x00be); /* XXX tap + 1 */
6094bf50f18SLuigi Rizzo macaddr_mid = (uint32_t) ticks;
6104bf50f18SLuigi Rizzo bcopy(&macaddr_hi, eaddr, sizeof(short));
6114bf50f18SLuigi Rizzo bcopy(&macaddr_mid, &eaddr[2], sizeof(uint32_t));
6124bf50f18SLuigi Rizzo eaddr[5] = (uint8_t)unit;
6134bf50f18SLuigi Rizzo
6144bf50f18SLuigi Rizzo ifp = if_alloc(IFT_ETHER);
6154bf50f18SLuigi Rizzo if_initname(ifp, name, IF_DUNIT_NONE);
616e330262fSJustin Hibbits if_setflags(ifp, IFF_UP | IFF_SIMPLEX | IFF_MULTICAST);
617e330262fSJustin Hibbits if_setinitfn(ifp, (void *)nm_vi_dummy);
618e330262fSJustin Hibbits if_setioctlfn(ifp, nm_vi_dummy);
619e330262fSJustin Hibbits if_setstartfn(ifp, nm_vi_start);
620e330262fSJustin Hibbits if_setmtu(ifp, ETHERMTU);
621e330262fSJustin Hibbits if_setsendqlen(ifp, ifqmaxlen);
622e330262fSJustin Hibbits if_setcapabilitiesbit(ifp, IFCAP_LINKSTATE, 0);
623e330262fSJustin Hibbits if_setcapenablebit(ifp, IFCAP_LINKSTATE, 0);
6244bf50f18SLuigi Rizzo
6254bf50f18SLuigi Rizzo ether_ifattach(ifp, eaddr);
6264bf50f18SLuigi Rizzo *ret = ifp;
6274bf50f18SLuigi Rizzo return 0;
6284bf50f18SLuigi Rizzo }
62937e3a6d3SLuigi Rizzo
6304bf50f18SLuigi Rizzo /* unregister from the system and drop the final refcount */
6314bf50f18SLuigi Rizzo void
nm_os_vi_detach(if_t ifp)632e330262fSJustin Hibbits nm_os_vi_detach(if_t ifp)
6334bf50f18SLuigi Rizzo {
634e330262fSJustin Hibbits nm_vi_free_index(((char *)if_getlladdr(ifp))[5]);
6354bf50f18SLuigi Rizzo ether_ifdetach(ifp);
6364bf50f18SLuigi Rizzo if_free(ifp);
6374bf50f18SLuigi Rizzo }
63817885a7bSLuigi Rizzo
6392ff91c17SVincenzo Maffione #ifdef WITH_EXTMEM
6402ff91c17SVincenzo Maffione #include <vm/vm_map.h>
641ee7ffaa2SVincenzo Maffione #include <vm/vm_extern.h>
6422ff91c17SVincenzo Maffione #include <vm/vm_kern.h>
6432ff91c17SVincenzo Maffione struct nm_os_extmem {
6442ff91c17SVincenzo Maffione vm_object_t obj;
6452ff91c17SVincenzo Maffione vm_offset_t kva;
6462ff91c17SVincenzo Maffione vm_offset_t size;
647cfa866f6SMatt Macy uintptr_t scan;
6482ff91c17SVincenzo Maffione };
6492ff91c17SVincenzo Maffione
6502ff91c17SVincenzo Maffione void
nm_os_extmem_delete(struct nm_os_extmem * e)6512ff91c17SVincenzo Maffione nm_os_extmem_delete(struct nm_os_extmem *e)
6522ff91c17SVincenzo Maffione {
653a56136a1SVincenzo Maffione nm_prinf("freeing %zx bytes", (size_t)e->size);
6542ff91c17SVincenzo Maffione vm_map_remove(kernel_map, e->kva, e->kva + e->size);
6552ff91c17SVincenzo Maffione nm_os_free(e);
6562ff91c17SVincenzo Maffione }
6572ff91c17SVincenzo Maffione
6582ff91c17SVincenzo Maffione char *
nm_os_extmem_nextpage(struct nm_os_extmem * e)6592ff91c17SVincenzo Maffione nm_os_extmem_nextpage(struct nm_os_extmem *e)
6602ff91c17SVincenzo Maffione {
6612ff91c17SVincenzo Maffione char *rv = NULL;
6622ff91c17SVincenzo Maffione if (e->scan < e->kva + e->size) {
6632ff91c17SVincenzo Maffione rv = (char *)e->scan;
6642ff91c17SVincenzo Maffione e->scan += PAGE_SIZE;
6652ff91c17SVincenzo Maffione }
6662ff91c17SVincenzo Maffione return rv;
6672ff91c17SVincenzo Maffione }
6682ff91c17SVincenzo Maffione
6692ff91c17SVincenzo Maffione int
nm_os_extmem_isequal(struct nm_os_extmem * e1,struct nm_os_extmem * e2)6702ff91c17SVincenzo Maffione nm_os_extmem_isequal(struct nm_os_extmem *e1, struct nm_os_extmem *e2)
6712ff91c17SVincenzo Maffione {
6723535fae8SMatt Macy return (e1->obj == e2->obj);
6732ff91c17SVincenzo Maffione }
6742ff91c17SVincenzo Maffione
6752ff91c17SVincenzo Maffione int
nm_os_extmem_nr_pages(struct nm_os_extmem * e)6762ff91c17SVincenzo Maffione nm_os_extmem_nr_pages(struct nm_os_extmem *e)
6772ff91c17SVincenzo Maffione {
6782ff91c17SVincenzo Maffione return e->size >> PAGE_SHIFT;
6792ff91c17SVincenzo Maffione }
6802ff91c17SVincenzo Maffione
6812ff91c17SVincenzo Maffione struct nm_os_extmem *
nm_os_extmem_create(unsigned long p,struct nmreq_pools_info * pi,int * perror)6822ff91c17SVincenzo Maffione nm_os_extmem_create(unsigned long p, struct nmreq_pools_info *pi, int *perror)
6832ff91c17SVincenzo Maffione {
6842ff91c17SVincenzo Maffione vm_map_t map;
6852ff91c17SVincenzo Maffione vm_map_entry_t entry;
6862ff91c17SVincenzo Maffione vm_object_t obj;
6872ff91c17SVincenzo Maffione vm_prot_t prot;
6882ff91c17SVincenzo Maffione vm_pindex_t index;
6892ff91c17SVincenzo Maffione boolean_t wired;
6902ff91c17SVincenzo Maffione struct nm_os_extmem *e = NULL;
6912ff91c17SVincenzo Maffione int rv, error = 0;
6922ff91c17SVincenzo Maffione
6932ff91c17SVincenzo Maffione e = nm_os_malloc(sizeof(*e));
6942ff91c17SVincenzo Maffione if (e == NULL) {
6952ff91c17SVincenzo Maffione error = ENOMEM;
6962ff91c17SVincenzo Maffione goto out;
6972ff91c17SVincenzo Maffione }
6982ff91c17SVincenzo Maffione
6992ff91c17SVincenzo Maffione map = &curthread->td_proc->p_vmspace->vm_map;
7002ff91c17SVincenzo Maffione rv = vm_map_lookup(&map, p, VM_PROT_RW, &entry,
7012ff91c17SVincenzo Maffione &obj, &index, &prot, &wired);
7022ff91c17SVincenzo Maffione if (rv != KERN_SUCCESS) {
703a56136a1SVincenzo Maffione nm_prerr("address %lx not found", p);
704ee7ffaa2SVincenzo Maffione error = vm_mmap_to_errno(rv);
7052ff91c17SVincenzo Maffione goto out_free;
7062ff91c17SVincenzo Maffione }
707ee7ffaa2SVincenzo Maffione vm_object_reference(obj);
708ee7ffaa2SVincenzo Maffione
7092ff91c17SVincenzo Maffione /* check that we are given the whole vm_object ? */
7102ff91c17SVincenzo Maffione vm_map_lookup_done(map, entry);
7112ff91c17SVincenzo Maffione
7122ff91c17SVincenzo Maffione e->obj = obj;
713ee7ffaa2SVincenzo Maffione /* Wire the memory and add the vm_object to the kernel map,
714ee7ffaa2SVincenzo Maffione * to make sure that it is not freed even if all the processes
715ee7ffaa2SVincenzo Maffione * that are mmap()ing should munmap() it.
7162ff91c17SVincenzo Maffione */
7172ff91c17SVincenzo Maffione e->kva = vm_map_min(kernel_map);
7182ff91c17SVincenzo Maffione e->size = obj->size << PAGE_SHIFT;
7192ff91c17SVincenzo Maffione rv = vm_map_find(kernel_map, obj, 0, &e->kva, e->size, 0,
7202ff91c17SVincenzo Maffione VMFS_OPTIMAL_SPACE, VM_PROT_READ | VM_PROT_WRITE,
7212ff91c17SVincenzo Maffione VM_PROT_READ | VM_PROT_WRITE, 0);
7222ff91c17SVincenzo Maffione if (rv != KERN_SUCCESS) {
723a56136a1SVincenzo Maffione nm_prerr("vm_map_find(%zx) failed", (size_t)e->size);
724ee7ffaa2SVincenzo Maffione error = vm_mmap_to_errno(rv);
7252ff91c17SVincenzo Maffione goto out_rel;
7262ff91c17SVincenzo Maffione }
7272ff91c17SVincenzo Maffione rv = vm_map_wire(kernel_map, e->kva, e->kva + e->size,
7282ff91c17SVincenzo Maffione VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
7292ff91c17SVincenzo Maffione if (rv != KERN_SUCCESS) {
730a56136a1SVincenzo Maffione nm_prerr("vm_map_wire failed");
731ee7ffaa2SVincenzo Maffione error = vm_mmap_to_errno(rv);
7322ff91c17SVincenzo Maffione goto out_rem;
7332ff91c17SVincenzo Maffione }
7342ff91c17SVincenzo Maffione
7352ff91c17SVincenzo Maffione e->scan = e->kva;
7362ff91c17SVincenzo Maffione
7372ff91c17SVincenzo Maffione return e;
7382ff91c17SVincenzo Maffione
7392ff91c17SVincenzo Maffione out_rem:
7402ff91c17SVincenzo Maffione vm_map_remove(kernel_map, e->kva, e->kva + e->size);
7412ff91c17SVincenzo Maffione out_rel:
7422ff91c17SVincenzo Maffione vm_object_deallocate(e->obj);
743ee7ffaa2SVincenzo Maffione e->obj = NULL;
7442ff91c17SVincenzo Maffione out_free:
7452ff91c17SVincenzo Maffione nm_os_free(e);
7462ff91c17SVincenzo Maffione out:
7472ff91c17SVincenzo Maffione if (perror)
7482ff91c17SVincenzo Maffione *perror = error;
7492ff91c17SVincenzo Maffione return NULL;
7502ff91c17SVincenzo Maffione }
7512ff91c17SVincenzo Maffione #endif /* WITH_EXTMEM */
7522ff91c17SVincenzo Maffione
753b6e66be2SVincenzo Maffione /* ================== PTNETMAP GUEST SUPPORT ==================== */
75437e3a6d3SLuigi Rizzo
755b6e66be2SVincenzo Maffione #ifdef WITH_PTNETMAP
75637e3a6d3SLuigi Rizzo #include <sys/bus.h>
75737e3a6d3SLuigi Rizzo #include <sys/rman.h>
75837e3a6d3SLuigi Rizzo #include <machine/bus.h> /* bus_dmamap_* */
75937e3a6d3SLuigi Rizzo #include <machine/resource.h>
76037e3a6d3SLuigi Rizzo #include <dev/pci/pcivar.h>
76137e3a6d3SLuigi Rizzo #include <dev/pci/pcireg.h>
76237e3a6d3SLuigi Rizzo /*
76337e3a6d3SLuigi Rizzo * ptnetmap memory device (memdev) for freebsd guest,
76437e3a6d3SLuigi Rizzo * ssed to expose host netmap memory to the guest through a PCI BAR.
76537e3a6d3SLuigi Rizzo */
76637e3a6d3SLuigi Rizzo
76737e3a6d3SLuigi Rizzo /*
76837e3a6d3SLuigi Rizzo * ptnetmap memdev private data structure
76937e3a6d3SLuigi Rizzo */
77037e3a6d3SLuigi Rizzo struct ptnetmap_memdev {
77137e3a6d3SLuigi Rizzo device_t dev;
77237e3a6d3SLuigi Rizzo struct resource *pci_io;
77337e3a6d3SLuigi Rizzo struct resource *pci_mem;
77437e3a6d3SLuigi Rizzo struct netmap_mem_d *nm_mem;
77537e3a6d3SLuigi Rizzo };
77637e3a6d3SLuigi Rizzo
77737e3a6d3SLuigi Rizzo static int ptn_memdev_probe(device_t);
77837e3a6d3SLuigi Rizzo static int ptn_memdev_attach(device_t);
77937e3a6d3SLuigi Rizzo static int ptn_memdev_detach(device_t);
78037e3a6d3SLuigi Rizzo static int ptn_memdev_shutdown(device_t);
78137e3a6d3SLuigi Rizzo
78237e3a6d3SLuigi Rizzo static device_method_t ptn_memdev_methods[] = {
78337e3a6d3SLuigi Rizzo DEVMETHOD(device_probe, ptn_memdev_probe),
78437e3a6d3SLuigi Rizzo DEVMETHOD(device_attach, ptn_memdev_attach),
78537e3a6d3SLuigi Rizzo DEVMETHOD(device_detach, ptn_memdev_detach),
78637e3a6d3SLuigi Rizzo DEVMETHOD(device_shutdown, ptn_memdev_shutdown),
78737e3a6d3SLuigi Rizzo DEVMETHOD_END
78837e3a6d3SLuigi Rizzo };
78937e3a6d3SLuigi Rizzo
79037e3a6d3SLuigi Rizzo static driver_t ptn_memdev_driver = {
79137e3a6d3SLuigi Rizzo PTNETMAP_MEMDEV_NAME,
79237e3a6d3SLuigi Rizzo ptn_memdev_methods,
79337e3a6d3SLuigi Rizzo sizeof(struct ptnetmap_memdev),
79437e3a6d3SLuigi Rizzo };
79537e3a6d3SLuigi Rizzo
79637e3a6d3SLuigi Rizzo /* We use (SI_ORDER_MIDDLE+1) here, see DEV_MODULE_ORDERED() invocation
79737e3a6d3SLuigi Rizzo * below. */
798a9424e0fSJohn Baldwin DRIVER_MODULE_ORDERED(ptn_memdev, pci, ptn_memdev_driver, NULL, NULL,
799a9424e0fSJohn Baldwin SI_ORDER_MIDDLE + 1);
80037e3a6d3SLuigi Rizzo
80137e3a6d3SLuigi Rizzo /*
80237e3a6d3SLuigi Rizzo * Map host netmap memory through PCI-BAR in the guest OS,
80337e3a6d3SLuigi Rizzo * returning physical (nm_paddr) and virtual (nm_addr) addresses
80437e3a6d3SLuigi Rizzo * of the netmap memory mapped in the guest.
80537e3a6d3SLuigi Rizzo */
80637e3a6d3SLuigi Rizzo int
nm_os_pt_memdev_iomap(struct ptnetmap_memdev * ptn_dev,vm_paddr_t * nm_paddr,void ** nm_addr,uint64_t * mem_size)807a2a74091SLuigi Rizzo nm_os_pt_memdev_iomap(struct ptnetmap_memdev *ptn_dev, vm_paddr_t *nm_paddr,
808844a6f0cSLuigi Rizzo void **nm_addr, uint64_t *mem_size)
80937e3a6d3SLuigi Rizzo {
81037e3a6d3SLuigi Rizzo int rid;
81137e3a6d3SLuigi Rizzo
812a56136a1SVincenzo Maffione nm_prinf("ptn_memdev_driver iomap");
81337e3a6d3SLuigi Rizzo
81437e3a6d3SLuigi Rizzo rid = PCIR_BAR(PTNETMAP_MEM_PCI_BAR);
815844a6f0cSLuigi Rizzo *mem_size = bus_read_4(ptn_dev->pci_io, PTNET_MDEV_IO_MEMSIZE_HI);
816844a6f0cSLuigi Rizzo *mem_size = bus_read_4(ptn_dev->pci_io, PTNET_MDEV_IO_MEMSIZE_LO) |
817844a6f0cSLuigi Rizzo (*mem_size << 32);
81837e3a6d3SLuigi Rizzo
81937e3a6d3SLuigi Rizzo /* map memory allocator */
82037e3a6d3SLuigi Rizzo ptn_dev->pci_mem = bus_alloc_resource(ptn_dev->dev, SYS_RES_MEMORY,
821844a6f0cSLuigi Rizzo &rid, 0, ~0, *mem_size, RF_ACTIVE);
82237e3a6d3SLuigi Rizzo if (ptn_dev->pci_mem == NULL) {
82337e3a6d3SLuigi Rizzo *nm_paddr = 0;
8244dd44461SLuiz Otavio O Souza *nm_addr = NULL;
82537e3a6d3SLuigi Rizzo return ENOMEM;
82637e3a6d3SLuigi Rizzo }
82737e3a6d3SLuigi Rizzo
82837e3a6d3SLuigi Rizzo *nm_paddr = rman_get_start(ptn_dev->pci_mem);
82937e3a6d3SLuigi Rizzo *nm_addr = rman_get_virtual(ptn_dev->pci_mem);
83037e3a6d3SLuigi Rizzo
831a56136a1SVincenzo Maffione nm_prinf("=== BAR %d start %lx len %lx mem_size %lx ===",
83237e3a6d3SLuigi Rizzo PTNETMAP_MEM_PCI_BAR,
833a2a74091SLuigi Rizzo (unsigned long)(*nm_paddr),
834a2a74091SLuigi Rizzo (unsigned long)rman_get_size(ptn_dev->pci_mem),
835844a6f0cSLuigi Rizzo (unsigned long)*mem_size);
83637e3a6d3SLuigi Rizzo return (0);
83737e3a6d3SLuigi Rizzo }
83837e3a6d3SLuigi Rizzo
839844a6f0cSLuigi Rizzo uint32_t
nm_os_pt_memdev_ioread(struct ptnetmap_memdev * ptn_dev,unsigned int reg)840844a6f0cSLuigi Rizzo nm_os_pt_memdev_ioread(struct ptnetmap_memdev *ptn_dev, unsigned int reg)
841844a6f0cSLuigi Rizzo {
842844a6f0cSLuigi Rizzo return bus_read_4(ptn_dev->pci_io, reg);
843844a6f0cSLuigi Rizzo }
844844a6f0cSLuigi Rizzo
84537e3a6d3SLuigi Rizzo /* Unmap host netmap memory. */
84637e3a6d3SLuigi Rizzo void
nm_os_pt_memdev_iounmap(struct ptnetmap_memdev * ptn_dev)84737e3a6d3SLuigi Rizzo nm_os_pt_memdev_iounmap(struct ptnetmap_memdev *ptn_dev)
84837e3a6d3SLuigi Rizzo {
849a56136a1SVincenzo Maffione nm_prinf("ptn_memdev_driver iounmap");
85037e3a6d3SLuigi Rizzo
85137e3a6d3SLuigi Rizzo if (ptn_dev->pci_mem) {
85237e3a6d3SLuigi Rizzo bus_release_resource(ptn_dev->dev, SYS_RES_MEMORY,
85337e3a6d3SLuigi Rizzo PCIR_BAR(PTNETMAP_MEM_PCI_BAR), ptn_dev->pci_mem);
85437e3a6d3SLuigi Rizzo ptn_dev->pci_mem = NULL;
85537e3a6d3SLuigi Rizzo }
85637e3a6d3SLuigi Rizzo }
85737e3a6d3SLuigi Rizzo
85837e3a6d3SLuigi Rizzo /* Device identification routine, return BUS_PROBE_DEFAULT on success,
85937e3a6d3SLuigi Rizzo * positive on failure */
86037e3a6d3SLuigi Rizzo static int
ptn_memdev_probe(device_t dev)86137e3a6d3SLuigi Rizzo ptn_memdev_probe(device_t dev)
86237e3a6d3SLuigi Rizzo {
86337e3a6d3SLuigi Rizzo if (pci_get_vendor(dev) != PTNETMAP_PCI_VENDOR_ID)
86437e3a6d3SLuigi Rizzo return (ENXIO);
86537e3a6d3SLuigi Rizzo if (pci_get_device(dev) != PTNETMAP_PCI_DEVICE_ID)
86637e3a6d3SLuigi Rizzo return (ENXIO);
86737e3a6d3SLuigi Rizzo
86844d36c97SMark Johnston device_set_descf(dev, "%s PCI adapter", PTNETMAP_MEMDEV_NAME);
86937e3a6d3SLuigi Rizzo
87037e3a6d3SLuigi Rizzo return (BUS_PROBE_DEFAULT);
87137e3a6d3SLuigi Rizzo }
87237e3a6d3SLuigi Rizzo
87337e3a6d3SLuigi Rizzo /* Device initialization routine. */
87437e3a6d3SLuigi Rizzo static int
ptn_memdev_attach(device_t dev)87537e3a6d3SLuigi Rizzo ptn_memdev_attach(device_t dev)
87637e3a6d3SLuigi Rizzo {
87737e3a6d3SLuigi Rizzo struct ptnetmap_memdev *ptn_dev;
87837e3a6d3SLuigi Rizzo int rid;
87937e3a6d3SLuigi Rizzo uint16_t mem_id;
88037e3a6d3SLuigi Rizzo
88137e3a6d3SLuigi Rizzo ptn_dev = device_get_softc(dev);
88237e3a6d3SLuigi Rizzo ptn_dev->dev = dev;
88337e3a6d3SLuigi Rizzo
88437e3a6d3SLuigi Rizzo pci_enable_busmaster(dev);
88537e3a6d3SLuigi Rizzo
88637e3a6d3SLuigi Rizzo rid = PCIR_BAR(PTNETMAP_IO_PCI_BAR);
88737e3a6d3SLuigi Rizzo ptn_dev->pci_io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
88837e3a6d3SLuigi Rizzo RF_ACTIVE);
88937e3a6d3SLuigi Rizzo if (ptn_dev->pci_io == NULL) {
89037e3a6d3SLuigi Rizzo device_printf(dev, "cannot map I/O space\n");
89137e3a6d3SLuigi Rizzo return (ENXIO);
89237e3a6d3SLuigi Rizzo }
89337e3a6d3SLuigi Rizzo
894844a6f0cSLuigi Rizzo mem_id = bus_read_4(ptn_dev->pci_io, PTNET_MDEV_IO_MEMID);
89537e3a6d3SLuigi Rizzo
89637e3a6d3SLuigi Rizzo /* create guest allocator */
89737e3a6d3SLuigi Rizzo ptn_dev->nm_mem = netmap_mem_pt_guest_attach(ptn_dev, mem_id);
89837e3a6d3SLuigi Rizzo if (ptn_dev->nm_mem == NULL) {
89937e3a6d3SLuigi Rizzo ptn_memdev_detach(dev);
90037e3a6d3SLuigi Rizzo return (ENOMEM);
90137e3a6d3SLuigi Rizzo }
90237e3a6d3SLuigi Rizzo netmap_mem_get(ptn_dev->nm_mem);
90337e3a6d3SLuigi Rizzo
904a56136a1SVincenzo Maffione nm_prinf("ptnetmap memdev attached, host memid: %u", mem_id);
90537e3a6d3SLuigi Rizzo
90637e3a6d3SLuigi Rizzo return (0);
90737e3a6d3SLuigi Rizzo }
90837e3a6d3SLuigi Rizzo
90937e3a6d3SLuigi Rizzo /* Device removal routine. */
91037e3a6d3SLuigi Rizzo static int
ptn_memdev_detach(device_t dev)91137e3a6d3SLuigi Rizzo ptn_memdev_detach(device_t dev)
91237e3a6d3SLuigi Rizzo {
91337e3a6d3SLuigi Rizzo struct ptnetmap_memdev *ptn_dev;
91437e3a6d3SLuigi Rizzo
91537e3a6d3SLuigi Rizzo ptn_dev = device_get_softc(dev);
91637e3a6d3SLuigi Rizzo
91737e3a6d3SLuigi Rizzo if (ptn_dev->nm_mem) {
918a56136a1SVincenzo Maffione nm_prinf("ptnetmap memdev detached, host memid %u",
919a56136a1SVincenzo Maffione netmap_mem_get_id(ptn_dev->nm_mem));
92037e3a6d3SLuigi Rizzo netmap_mem_put(ptn_dev->nm_mem);
92137e3a6d3SLuigi Rizzo ptn_dev->nm_mem = NULL;
92237e3a6d3SLuigi Rizzo }
92337e3a6d3SLuigi Rizzo if (ptn_dev->pci_mem) {
92437e3a6d3SLuigi Rizzo bus_release_resource(dev, SYS_RES_MEMORY,
92537e3a6d3SLuigi Rizzo PCIR_BAR(PTNETMAP_MEM_PCI_BAR), ptn_dev->pci_mem);
92637e3a6d3SLuigi Rizzo ptn_dev->pci_mem = NULL;
92737e3a6d3SLuigi Rizzo }
92837e3a6d3SLuigi Rizzo if (ptn_dev->pci_io) {
92937e3a6d3SLuigi Rizzo bus_release_resource(dev, SYS_RES_IOPORT,
93037e3a6d3SLuigi Rizzo PCIR_BAR(PTNETMAP_IO_PCI_BAR), ptn_dev->pci_io);
93137e3a6d3SLuigi Rizzo ptn_dev->pci_io = NULL;
93237e3a6d3SLuigi Rizzo }
93337e3a6d3SLuigi Rizzo
93437e3a6d3SLuigi Rizzo return (0);
93537e3a6d3SLuigi Rizzo }
93637e3a6d3SLuigi Rizzo
93737e3a6d3SLuigi Rizzo static int
ptn_memdev_shutdown(device_t dev)93837e3a6d3SLuigi Rizzo ptn_memdev_shutdown(device_t dev)
93937e3a6d3SLuigi Rizzo {
94037e3a6d3SLuigi Rizzo return bus_generic_shutdown(dev);
94137e3a6d3SLuigi Rizzo }
94237e3a6d3SLuigi Rizzo
943b6e66be2SVincenzo Maffione #endif /* WITH_PTNETMAP */
94437e3a6d3SLuigi Rizzo
945f9790aebSLuigi Rizzo /*
946f9790aebSLuigi Rizzo * In order to track whether pages are still mapped, we hook into
947f9790aebSLuigi Rizzo * the standard cdev_pager and intercept the constructor and
948f9790aebSLuigi Rizzo * destructor.
949f9790aebSLuigi Rizzo */
950f9790aebSLuigi Rizzo
951f9790aebSLuigi Rizzo struct netmap_vm_handle_t {
952f9790aebSLuigi Rizzo struct cdev *dev;
953f9790aebSLuigi Rizzo struct netmap_priv_d *priv;
954f9790aebSLuigi Rizzo };
955f9790aebSLuigi Rizzo
95617885a7bSLuigi Rizzo
957f9790aebSLuigi Rizzo static int
netmap_dev_pager_ctor(void * handle,vm_ooffset_t size,vm_prot_t prot,vm_ooffset_t foff,struct ucred * cred,u_short * color)958f9790aebSLuigi Rizzo netmap_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
959f9790aebSLuigi Rizzo vm_ooffset_t foff, struct ucred *cred, u_short *color)
960f9790aebSLuigi Rizzo {
961f9790aebSLuigi Rizzo struct netmap_vm_handle_t *vmh = handle;
962f0ea3689SLuigi Rizzo
963f0ea3689SLuigi Rizzo if (netmap_verbose)
964a56136a1SVincenzo Maffione nm_prinf("handle %p size %jd prot %d foff %jd",
965f9790aebSLuigi Rizzo handle, (intmax_t)size, prot, (intmax_t)foff);
9664e93beffSLuigi Rizzo if (color)
9674e93beffSLuigi Rizzo *color = 0;
968f9790aebSLuigi Rizzo dev_ref(vmh->dev);
969f9790aebSLuigi Rizzo return 0;
970f9790aebSLuigi Rizzo }
971f9790aebSLuigi Rizzo
972f9790aebSLuigi Rizzo
973f9790aebSLuigi Rizzo static void
netmap_dev_pager_dtor(void * handle)974f9790aebSLuigi Rizzo netmap_dev_pager_dtor(void *handle)
975f9790aebSLuigi Rizzo {
976f9790aebSLuigi Rizzo struct netmap_vm_handle_t *vmh = handle;
977f9790aebSLuigi Rizzo struct cdev *dev = vmh->dev;
978f9790aebSLuigi Rizzo struct netmap_priv_d *priv = vmh->priv;
979f0ea3689SLuigi Rizzo
980f0ea3689SLuigi Rizzo if (netmap_verbose)
981a56136a1SVincenzo Maffione nm_prinf("handle %p", handle);
982f9790aebSLuigi Rizzo netmap_dtor(priv);
983f9790aebSLuigi Rizzo free(vmh, M_DEVBUF);
984f9790aebSLuigi Rizzo dev_rel(dev);
985f9790aebSLuigi Rizzo }
986f9790aebSLuigi Rizzo
98717885a7bSLuigi Rizzo
988f9790aebSLuigi Rizzo static int
netmap_dev_pager_fault(vm_object_t object,vm_ooffset_t offset,int prot,vm_page_t * mres)989f9790aebSLuigi Rizzo netmap_dev_pager_fault(vm_object_t object, vm_ooffset_t offset,
990f9790aebSLuigi Rizzo int prot, vm_page_t *mres)
991f9790aebSLuigi Rizzo {
992f9790aebSLuigi Rizzo struct netmap_vm_handle_t *vmh = object->handle;
993f9790aebSLuigi Rizzo struct netmap_priv_d *priv = vmh->priv;
994847bf383SLuigi Rizzo struct netmap_adapter *na = priv->np_na;
995f9790aebSLuigi Rizzo vm_paddr_t paddr;
996f9790aebSLuigi Rizzo vm_page_t page;
997f9790aebSLuigi Rizzo vm_memattr_t memattr;
998f9790aebSLuigi Rizzo
999a56136a1SVincenzo Maffione nm_prdis("object %p offset %jd prot %d mres %p",
1000f9790aebSLuigi Rizzo object, (intmax_t)offset, prot, mres);
1001f9790aebSLuigi Rizzo memattr = object->memattr;
1002847bf383SLuigi Rizzo paddr = netmap_mem_ofstophys(na->nm_mem, offset);
1003f9790aebSLuigi Rizzo if (paddr == 0)
1004f9790aebSLuigi Rizzo return VM_PAGER_FAIL;
1005f9790aebSLuigi Rizzo
1006f9790aebSLuigi Rizzo if (((*mres)->flags & PG_FICTITIOUS) != 0) {
1007f9790aebSLuigi Rizzo /*
1008f9790aebSLuigi Rizzo * If the passed in result page is a fake page, update it with
1009f9790aebSLuigi Rizzo * the new physical address.
1010f9790aebSLuigi Rizzo */
1011f9790aebSLuigi Rizzo page = *mres;
1012f9790aebSLuigi Rizzo vm_page_updatefake(page, paddr, memattr);
1013f9790aebSLuigi Rizzo } else {
1014f9790aebSLuigi Rizzo /*
1015f9790aebSLuigi Rizzo * Replace the passed in reqpage page with our own fake page and
1016f9790aebSLuigi Rizzo * free up the all of the original pages.
1017f9790aebSLuigi Rizzo */
1018f9790aebSLuigi Rizzo VM_OBJECT_WUNLOCK(object);
1019f9790aebSLuigi Rizzo page = vm_page_getfake(paddr, memattr);
1020f9790aebSLuigi Rizzo VM_OBJECT_WLOCK(object);
10213cf3b4e6SJeff Roberson vm_page_replace(page, object, (*mres)->pindex, *mres);
1022f9790aebSLuigi Rizzo *mres = page;
1023f9790aebSLuigi Rizzo }
1024a6d768d8SVincenzo Maffione page->valid = VM_PAGE_BITS_ALL;
1025f9790aebSLuigi Rizzo return (VM_PAGER_OK);
1026f9790aebSLuigi Rizzo }
1027f9790aebSLuigi Rizzo
1028*7a5b9c4aSJohn Baldwin static void
netmap_dev_pager_path(void * handle,char * path,size_t len)1029*7a5b9c4aSJohn Baldwin netmap_dev_pager_path(void *handle, char *path, size_t len)
1030*7a5b9c4aSJohn Baldwin {
1031*7a5b9c4aSJohn Baldwin struct netmap_vm_handle_t *vmh = handle;
1032*7a5b9c4aSJohn Baldwin struct cdev *dev = vmh->dev;
1033*7a5b9c4aSJohn Baldwin
1034*7a5b9c4aSJohn Baldwin dev_copyname(dev, path, len);
1035*7a5b9c4aSJohn Baldwin }
1036f9790aebSLuigi Rizzo
1037f9790aebSLuigi Rizzo static struct cdev_pager_ops netmap_cdev_pager_ops = {
1038f9790aebSLuigi Rizzo .cdev_pg_ctor = netmap_dev_pager_ctor,
1039f9790aebSLuigi Rizzo .cdev_pg_dtor = netmap_dev_pager_dtor,
1040f9790aebSLuigi Rizzo .cdev_pg_fault = netmap_dev_pager_fault,
1041*7a5b9c4aSJohn Baldwin .cdev_pg_path = netmap_dev_pager_path,
1042f9790aebSLuigi Rizzo };
1043f9790aebSLuigi Rizzo
1044f9790aebSLuigi Rizzo
1045f9790aebSLuigi Rizzo static int
netmap_mmap_single(struct cdev * cdev,vm_ooffset_t * foff,vm_size_t objsize,vm_object_t * objp,int prot)1046f9790aebSLuigi Rizzo netmap_mmap_single(struct cdev *cdev, vm_ooffset_t *foff,
1047f9790aebSLuigi Rizzo vm_size_t objsize, vm_object_t *objp, int prot)
1048f9790aebSLuigi Rizzo {
1049f9790aebSLuigi Rizzo int error;
1050f9790aebSLuigi Rizzo struct netmap_vm_handle_t *vmh;
1051f9790aebSLuigi Rizzo struct netmap_priv_d *priv;
1052f9790aebSLuigi Rizzo vm_object_t obj;
1053f9790aebSLuigi Rizzo
1054f0ea3689SLuigi Rizzo if (netmap_verbose)
1055a56136a1SVincenzo Maffione nm_prinf("cdev %p foff %jd size %jd objp %p prot %d", cdev,
1056f9790aebSLuigi Rizzo (intmax_t )*foff, (intmax_t )objsize, objp, prot);
1057f9790aebSLuigi Rizzo
1058f9790aebSLuigi Rizzo vmh = malloc(sizeof(struct netmap_vm_handle_t), M_DEVBUF,
1059f9790aebSLuigi Rizzo M_NOWAIT | M_ZERO);
1060f9790aebSLuigi Rizzo if (vmh == NULL)
1061f9790aebSLuigi Rizzo return ENOMEM;
1062f9790aebSLuigi Rizzo vmh->dev = cdev;
1063f9790aebSLuigi Rizzo
1064f9790aebSLuigi Rizzo NMG_LOCK();
1065f9790aebSLuigi Rizzo error = devfs_get_cdevpriv((void**)&priv);
1066f9790aebSLuigi Rizzo if (error)
1067f9790aebSLuigi Rizzo goto err_unlock;
1068847bf383SLuigi Rizzo if (priv->np_nifp == NULL) {
1069847bf383SLuigi Rizzo error = EINVAL;
1070847bf383SLuigi Rizzo goto err_unlock;
1071847bf383SLuigi Rizzo }
1072f9790aebSLuigi Rizzo vmh->priv = priv;
10738fd44c93SLuigi Rizzo priv->np_refs++;
1074f9790aebSLuigi Rizzo NMG_UNLOCK();
1075f9790aebSLuigi Rizzo
1076f9790aebSLuigi Rizzo obj = cdev_pager_allocate(vmh, OBJT_DEVICE,
1077f9790aebSLuigi Rizzo &netmap_cdev_pager_ops, objsize, prot,
1078f9790aebSLuigi Rizzo *foff, NULL);
1079f9790aebSLuigi Rizzo if (obj == NULL) {
1080a56136a1SVincenzo Maffione nm_prerr("cdev_pager_allocate failed");
1081f9790aebSLuigi Rizzo error = EINVAL;
1082f9790aebSLuigi Rizzo goto err_deref;
1083f9790aebSLuigi Rizzo }
1084f9790aebSLuigi Rizzo
1085f9790aebSLuigi Rizzo *objp = obj;
1086f9790aebSLuigi Rizzo return 0;
1087f9790aebSLuigi Rizzo
1088f9790aebSLuigi Rizzo err_deref:
1089f9790aebSLuigi Rizzo NMG_LOCK();
10908fd44c93SLuigi Rizzo priv->np_refs--;
1091f9790aebSLuigi Rizzo err_unlock:
1092f9790aebSLuigi Rizzo NMG_UNLOCK();
1093f9790aebSLuigi Rizzo // err:
1094f9790aebSLuigi Rizzo free(vmh, M_DEVBUF);
1095f9790aebSLuigi Rizzo return error;
1096f9790aebSLuigi Rizzo }
1097f9790aebSLuigi Rizzo
1098847bf383SLuigi Rizzo /*
10998fd44c93SLuigi Rizzo * On FreeBSD the close routine is only called on the last close on
11008fd44c93SLuigi Rizzo * the device (/dev/netmap) so we cannot do anything useful.
11018fd44c93SLuigi Rizzo * To track close() on individual file descriptors we pass netmap_dtor() to
1102847bf383SLuigi Rizzo * devfs_set_cdevpriv() on open(). The FreeBSD kernel will call the destructor
1103847bf383SLuigi Rizzo * when the last fd pointing to the device is closed.
1104847bf383SLuigi Rizzo *
11058fd44c93SLuigi Rizzo * Note that FreeBSD does not even munmap() on close() so we also have
11068fd44c93SLuigi Rizzo * to track mmap() ourselves, and postpone the call to
1107847bf383SLuigi Rizzo * netmap_dtor() is called when the process has no open fds and no active
1108847bf383SLuigi Rizzo * memory maps on /dev/netmap, as in linux.
1109847bf383SLuigi Rizzo */
1110f9790aebSLuigi Rizzo static int
netmap_close(struct cdev * dev,int fflag,int devtype,struct thread * td)1111f9790aebSLuigi Rizzo netmap_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
1112f9790aebSLuigi Rizzo {
1113f9790aebSLuigi Rizzo if (netmap_verbose)
1114a56136a1SVincenzo Maffione nm_prinf("dev %p fflag 0x%x devtype %d td %p",
1115f9790aebSLuigi Rizzo dev, fflag, devtype, td);
1116f9790aebSLuigi Rizzo return 0;
1117f9790aebSLuigi Rizzo }
1118f9790aebSLuigi Rizzo
1119f9790aebSLuigi Rizzo
1120f9790aebSLuigi Rizzo static int
netmap_open(struct cdev * dev,int oflags,int devtype,struct thread * td)1121f9790aebSLuigi Rizzo netmap_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
1122f9790aebSLuigi Rizzo {
1123f9790aebSLuigi Rizzo struct netmap_priv_d *priv;
1124f9790aebSLuigi Rizzo int error;
1125f9790aebSLuigi Rizzo
1126f9790aebSLuigi Rizzo (void)dev;
1127f9790aebSLuigi Rizzo (void)oflags;
1128f9790aebSLuigi Rizzo (void)devtype;
1129f9790aebSLuigi Rizzo (void)td;
1130f9790aebSLuigi Rizzo
113137e3a6d3SLuigi Rizzo NMG_LOCK();
113237e3a6d3SLuigi Rizzo priv = netmap_priv_new();
113337e3a6d3SLuigi Rizzo if (priv == NULL) {
113437e3a6d3SLuigi Rizzo error = ENOMEM;
113537e3a6d3SLuigi Rizzo goto out;
113637e3a6d3SLuigi Rizzo }
1137f9790aebSLuigi Rizzo error = devfs_set_cdevpriv(priv, netmap_dtor);
11388fd44c93SLuigi Rizzo if (error) {
113937e3a6d3SLuigi Rizzo netmap_priv_delete(priv);
11408fd44c93SLuigi Rizzo }
114137e3a6d3SLuigi Rizzo out:
114237e3a6d3SLuigi Rizzo NMG_UNLOCK();
1143f9790aebSLuigi Rizzo return error;
1144f9790aebSLuigi Rizzo }
1145f9790aebSLuigi Rizzo
114637e3a6d3SLuigi Rizzo /******************** kthread wrapper ****************/
114737e3a6d3SLuigi Rizzo #include <sys/sysproto.h>
114837e3a6d3SLuigi Rizzo u_int
nm_os_ncpus(void)114937e3a6d3SLuigi Rizzo nm_os_ncpus(void)
115037e3a6d3SLuigi Rizzo {
115137e3a6d3SLuigi Rizzo return mp_maxid + 1;
115237e3a6d3SLuigi Rizzo }
115337e3a6d3SLuigi Rizzo
1154c3e9b4dbSLuiz Otavio O Souza struct nm_kctx_ctx {
1155b6e66be2SVincenzo Maffione /* Userspace thread (kthread creator). */
1156b6e66be2SVincenzo Maffione struct thread *user_td;
115737e3a6d3SLuigi Rizzo
115837e3a6d3SLuigi Rizzo /* worker function and parameter */
1159c3e9b4dbSLuiz Otavio O Souza nm_kctx_worker_fn_t worker_fn;
116037e3a6d3SLuigi Rizzo void *worker_private;
116137e3a6d3SLuigi Rizzo
1162c3e9b4dbSLuiz Otavio O Souza struct nm_kctx *nmk;
116337e3a6d3SLuigi Rizzo
116437e3a6d3SLuigi Rizzo /* integer to manage multiple worker contexts (e.g., RX or TX on ptnetmap) */
116537e3a6d3SLuigi Rizzo long type;
116637e3a6d3SLuigi Rizzo };
116737e3a6d3SLuigi Rizzo
1168c3e9b4dbSLuiz Otavio O Souza struct nm_kctx {
116937e3a6d3SLuigi Rizzo struct thread *worker;
117037e3a6d3SLuigi Rizzo struct mtx worker_lock;
1171c3e9b4dbSLuiz Otavio O Souza struct nm_kctx_ctx worker_ctx;
117237e3a6d3SLuigi Rizzo int run; /* used to stop kthread */
117337e3a6d3SLuigi Rizzo int attach_user; /* kthread attached to user_process */
117437e3a6d3SLuigi Rizzo int affinity;
117537e3a6d3SLuigi Rizzo };
117637e3a6d3SLuigi Rizzo
117737e3a6d3SLuigi Rizzo static void
nm_kctx_worker(void * data)1178c3e9b4dbSLuiz Otavio O Souza nm_kctx_worker(void *data)
117937e3a6d3SLuigi Rizzo {
1180c3e9b4dbSLuiz Otavio O Souza struct nm_kctx *nmk = data;
1181c3e9b4dbSLuiz Otavio O Souza struct nm_kctx_ctx *ctx = &nmk->worker_ctx;
118237e3a6d3SLuigi Rizzo
118337e3a6d3SLuigi Rizzo if (nmk->affinity >= 0) {
118437e3a6d3SLuigi Rizzo thread_lock(curthread);
118537e3a6d3SLuigi Rizzo sched_bind(curthread, nmk->affinity);
118637e3a6d3SLuigi Rizzo thread_unlock(curthread);
118737e3a6d3SLuigi Rizzo }
118837e3a6d3SLuigi Rizzo
118937e3a6d3SLuigi Rizzo while (nmk->run) {
119037e3a6d3SLuigi Rizzo /*
119137e3a6d3SLuigi Rizzo * check if the parent process dies
119237e3a6d3SLuigi Rizzo * (when kthread is attached to user process)
119337e3a6d3SLuigi Rizzo */
119437e3a6d3SLuigi Rizzo if (ctx->user_td) {
119537e3a6d3SLuigi Rizzo PROC_LOCK(curproc);
119637e3a6d3SLuigi Rizzo thread_suspend_check(0);
119737e3a6d3SLuigi Rizzo PROC_UNLOCK(curproc);
119837e3a6d3SLuigi Rizzo } else {
119937e3a6d3SLuigi Rizzo kthread_suspend_check();
120037e3a6d3SLuigi Rizzo }
120137e3a6d3SLuigi Rizzo
1202b6e66be2SVincenzo Maffione /* Continuously execute worker process. */
1203b6e66be2SVincenzo Maffione ctx->worker_fn(ctx->worker_private); /* worker body */
120437e3a6d3SLuigi Rizzo }
120537e3a6d3SLuigi Rizzo
120637e3a6d3SLuigi Rizzo kthread_exit();
120737e3a6d3SLuigi Rizzo }
120837e3a6d3SLuigi Rizzo
120937e3a6d3SLuigi Rizzo void
nm_os_kctx_worker_setaff(struct nm_kctx * nmk,int affinity)1210c3e9b4dbSLuiz Otavio O Souza nm_os_kctx_worker_setaff(struct nm_kctx *nmk, int affinity)
121137e3a6d3SLuigi Rizzo {
121237e3a6d3SLuigi Rizzo nmk->affinity = affinity;
121337e3a6d3SLuigi Rizzo }
121437e3a6d3SLuigi Rizzo
1215c3e9b4dbSLuiz Otavio O Souza struct nm_kctx *
nm_os_kctx_create(struct nm_kctx_cfg * cfg,void * opaque)12162ff91c17SVincenzo Maffione nm_os_kctx_create(struct nm_kctx_cfg *cfg, void *opaque)
121737e3a6d3SLuigi Rizzo {
1218c3e9b4dbSLuiz Otavio O Souza struct nm_kctx *nmk = NULL;
1219844a6f0cSLuigi Rizzo
122037e3a6d3SLuigi Rizzo nmk = malloc(sizeof(*nmk), M_DEVBUF, M_NOWAIT | M_ZERO);
122137e3a6d3SLuigi Rizzo if (!nmk)
122237e3a6d3SLuigi Rizzo return NULL;
122337e3a6d3SLuigi Rizzo
1224869d8878SAdrian Chadd mtx_init(&nmk->worker_lock, "nm_kthread lock", NULL, MTX_DEF);
122537e3a6d3SLuigi Rizzo nmk->worker_ctx.worker_fn = cfg->worker_fn;
122637e3a6d3SLuigi Rizzo nmk->worker_ctx.worker_private = cfg->worker_private;
122737e3a6d3SLuigi Rizzo nmk->worker_ctx.type = cfg->type;
122837e3a6d3SLuigi Rizzo nmk->affinity = -1;
122937e3a6d3SLuigi Rizzo
123037e3a6d3SLuigi Rizzo /* attach kthread to user process (ptnetmap) */
123137e3a6d3SLuigi Rizzo nmk->attach_user = cfg->attach_user;
123237e3a6d3SLuigi Rizzo
123337e3a6d3SLuigi Rizzo return nmk;
123437e3a6d3SLuigi Rizzo }
123537e3a6d3SLuigi Rizzo
123637e3a6d3SLuigi Rizzo int
nm_os_kctx_worker_start(struct nm_kctx * nmk)1237c3e9b4dbSLuiz Otavio O Souza nm_os_kctx_worker_start(struct nm_kctx *nmk)
123837e3a6d3SLuigi Rizzo {
123937e3a6d3SLuigi Rizzo struct proc *p = NULL;
124037e3a6d3SLuigi Rizzo int error = 0;
124137e3a6d3SLuigi Rizzo
1242b6e66be2SVincenzo Maffione /* Temporarily disable this function as it is currently broken
1243b6e66be2SVincenzo Maffione * and causes kernel crashes. The failure can be triggered by
1244b6e66be2SVincenzo Maffione * the "vale_polling_enable_disable" test in ctrl-api-test.c. */
1245b6e66be2SVincenzo Maffione return EOPNOTSUPP;
1246b6e66be2SVincenzo Maffione
1247b6e66be2SVincenzo Maffione if (nmk->worker)
124837e3a6d3SLuigi Rizzo return EBUSY;
124937e3a6d3SLuigi Rizzo
125037e3a6d3SLuigi Rizzo /* check if we want to attach kthread to user process */
125137e3a6d3SLuigi Rizzo if (nmk->attach_user) {
125237e3a6d3SLuigi Rizzo nmk->worker_ctx.user_td = curthread;
125337e3a6d3SLuigi Rizzo p = curthread->td_proc;
125437e3a6d3SLuigi Rizzo }
125537e3a6d3SLuigi Rizzo
125637e3a6d3SLuigi Rizzo /* enable kthread main loop */
125737e3a6d3SLuigi Rizzo nmk->run = 1;
125837e3a6d3SLuigi Rizzo /* create kthread */
1259c3e9b4dbSLuiz Otavio O Souza if((error = kthread_add(nm_kctx_worker, nmk, p,
126037e3a6d3SLuigi Rizzo &nmk->worker, RFNOWAIT /* to be checked */, 0, "nm-kthread-%ld",
126137e3a6d3SLuigi Rizzo nmk->worker_ctx.type))) {
126237e3a6d3SLuigi Rizzo goto err;
126337e3a6d3SLuigi Rizzo }
126437e3a6d3SLuigi Rizzo
1265a56136a1SVincenzo Maffione nm_prinf("nm_kthread started td %p", nmk->worker);
126637e3a6d3SLuigi Rizzo
126737e3a6d3SLuigi Rizzo return 0;
126837e3a6d3SLuigi Rizzo err:
1269a56136a1SVincenzo Maffione nm_prerr("nm_kthread start failed err %d", error);
127037e3a6d3SLuigi Rizzo nmk->worker = NULL;
127137e3a6d3SLuigi Rizzo return error;
127237e3a6d3SLuigi Rizzo }
127337e3a6d3SLuigi Rizzo
127437e3a6d3SLuigi Rizzo void
nm_os_kctx_worker_stop(struct nm_kctx * nmk)1275c3e9b4dbSLuiz Otavio O Souza nm_os_kctx_worker_stop(struct nm_kctx *nmk)
127637e3a6d3SLuigi Rizzo {
1277b6e66be2SVincenzo Maffione if (!nmk->worker)
127837e3a6d3SLuigi Rizzo return;
1279b6e66be2SVincenzo Maffione
128037e3a6d3SLuigi Rizzo /* tell to kthread to exit from main loop */
128137e3a6d3SLuigi Rizzo nmk->run = 0;
128237e3a6d3SLuigi Rizzo
128337e3a6d3SLuigi Rizzo /* wake up kthread if it sleeps */
128437e3a6d3SLuigi Rizzo kthread_resume(nmk->worker);
128537e3a6d3SLuigi Rizzo
128637e3a6d3SLuigi Rizzo nmk->worker = NULL;
128737e3a6d3SLuigi Rizzo }
128837e3a6d3SLuigi Rizzo
128937e3a6d3SLuigi Rizzo void
nm_os_kctx_destroy(struct nm_kctx * nmk)1290c3e9b4dbSLuiz Otavio O Souza nm_os_kctx_destroy(struct nm_kctx *nmk)
129137e3a6d3SLuigi Rizzo {
129237e3a6d3SLuigi Rizzo if (!nmk)
129337e3a6d3SLuigi Rizzo return;
129437e3a6d3SLuigi Rizzo
1295b6e66be2SVincenzo Maffione if (nmk->worker)
1296b6e66be2SVincenzo Maffione nm_os_kctx_worker_stop(nmk);
129737e3a6d3SLuigi Rizzo
129837e3a6d3SLuigi Rizzo free(nmk, M_DEVBUF);
129937e3a6d3SLuigi Rizzo }
130037e3a6d3SLuigi Rizzo
1301f0ea3689SLuigi Rizzo /******************** kqueue support ****************/
1302f0ea3689SLuigi Rizzo
1303f0ea3689SLuigi Rizzo /*
13048c9874f5SVincenzo Maffione * In addition to calling selwakeuppri(), nm_os_selwakeup() also
130519c4ec08SVincenzo Maffione * needs to call knote() to wake up kqueue listeners.
130619c4ec08SVincenzo Maffione * This operation is deferred to a taskqueue in order to avoid possible
130719c4ec08SVincenzo Maffione * lock order reversals; these may happen because knote() grabs a
130819c4ec08SVincenzo Maffione * private lock associated to the 'si' (see struct selinfo,
130919c4ec08SVincenzo Maffione * struct nm_selinfo, and nm_os_selinfo_init), and nm_os_selwakeup()
131019c4ec08SVincenzo Maffione * can be called while holding the lock associated to a different
131119c4ec08SVincenzo Maffione * 'si'.
131219c4ec08SVincenzo Maffione * When calling knote() we use a non-zero 'hint' argument to inform
131319c4ec08SVincenzo Maffione * the netmap_knrw() function that it is being called from
131419c4ec08SVincenzo Maffione * 'nm_os_selwakeup'; this is necessary because when netmap_knrw() is
131519c4ec08SVincenzo Maffione * called by the kevent subsystem (i.e. kevent_scan()) we also need to
131619c4ec08SVincenzo Maffione * call netmap_poll().
1317f0ea3689SLuigi Rizzo *
13188c9874f5SVincenzo Maffione * The netmap_kqfilter() function registers one or another f_event
13198c9874f5SVincenzo Maffione * depending on read or write mode. A pointer to the struct
13208c9874f5SVincenzo Maffione * 'netmap_priv_d' is stored into kn->kn_hook, so that it can later
13218c9874f5SVincenzo Maffione * be passed to netmap_poll(). We pass NULL as a third argument to
13228c9874f5SVincenzo Maffione * netmap_poll(), so that the latter only runs the txsync/rxsync
13238c9874f5SVincenzo Maffione * (if necessary), and skips the nm_os_selrecord() calls.
1324f0ea3689SLuigi Rizzo */
1325f0ea3689SLuigi Rizzo
1326f0ea3689SLuigi Rizzo
1327f0ea3689SLuigi Rizzo void
nm_os_selwakeup(struct nm_selinfo * si)132837e3a6d3SLuigi Rizzo nm_os_selwakeup(struct nm_selinfo *si)
1329f0ea3689SLuigi Rizzo {
133037e3a6d3SLuigi Rizzo selwakeuppri(&si->si, PI_NET);
133145100257SVincenzo Maffione if (si->kqueue_users > 0) {
133219c4ec08SVincenzo Maffione taskqueue_enqueue(si->ntfytq, &si->ntfytask);
1333f0ea3689SLuigi Rizzo }
133445100257SVincenzo Maffione }
1335f0ea3689SLuigi Rizzo
133637e3a6d3SLuigi Rizzo void
nm_os_selrecord(struct thread * td,struct nm_selinfo * si)133737e3a6d3SLuigi Rizzo nm_os_selrecord(struct thread *td, struct nm_selinfo *si)
133837e3a6d3SLuigi Rizzo {
133937e3a6d3SLuigi Rizzo selrecord(td, &si->si);
134037e3a6d3SLuigi Rizzo }
134137e3a6d3SLuigi Rizzo
1342f0ea3689SLuigi Rizzo static void
netmap_knrdetach(struct knote * kn)1343f0ea3689SLuigi Rizzo netmap_knrdetach(struct knote *kn)
1344f0ea3689SLuigi Rizzo {
1345f0ea3689SLuigi Rizzo struct netmap_priv_d *priv = (struct netmap_priv_d *)kn->kn_hook;
134645100257SVincenzo Maffione struct nm_selinfo *si = priv->np_si[NR_RX];
1347f0ea3689SLuigi Rizzo
134845100257SVincenzo Maffione knlist_remove(&si->si.si_note, kn, /*islocked=*/0);
134945100257SVincenzo Maffione NMG_LOCK();
135045100257SVincenzo Maffione KASSERT(si->kqueue_users > 0, ("kqueue_user underflow on %s",
135145100257SVincenzo Maffione si->mtxname));
135245100257SVincenzo Maffione si->kqueue_users--;
135345100257SVincenzo Maffione nm_prinf("kqueue users for %s: %d", si->mtxname, si->kqueue_users);
135445100257SVincenzo Maffione NMG_UNLOCK();
1355f0ea3689SLuigi Rizzo }
1356f0ea3689SLuigi Rizzo
1357f0ea3689SLuigi Rizzo static void
netmap_knwdetach(struct knote * kn)1358f0ea3689SLuigi Rizzo netmap_knwdetach(struct knote *kn)
1359f0ea3689SLuigi Rizzo {
1360f0ea3689SLuigi Rizzo struct netmap_priv_d *priv = (struct netmap_priv_d *)kn->kn_hook;
136145100257SVincenzo Maffione struct nm_selinfo *si = priv->np_si[NR_TX];
1362f0ea3689SLuigi Rizzo
136345100257SVincenzo Maffione knlist_remove(&si->si.si_note, kn, /*islocked=*/0);
136445100257SVincenzo Maffione NMG_LOCK();
136545100257SVincenzo Maffione si->kqueue_users--;
136645100257SVincenzo Maffione nm_prinf("kqueue users for %s: %d", si->mtxname, si->kqueue_users);
136745100257SVincenzo Maffione NMG_UNLOCK();
1368f0ea3689SLuigi Rizzo }
1369f0ea3689SLuigi Rizzo
1370f0ea3689SLuigi Rizzo /*
13718c9874f5SVincenzo Maffione * Callback triggered by netmap notifications (see netmap_notify()),
13728c9874f5SVincenzo Maffione * and by the application calling kevent(). In the former case we
13738c9874f5SVincenzo Maffione * just return 1 (events ready), since we are not able to do better.
13748c9874f5SVincenzo Maffione * In the latter case we use netmap_poll() to see which events are
13758c9874f5SVincenzo Maffione * ready.
1376f0ea3689SLuigi Rizzo */
1377f0ea3689SLuigi Rizzo static int
netmap_knrw(struct knote * kn,long hint,int events)1378f0ea3689SLuigi Rizzo netmap_knrw(struct knote *kn, long hint, int events)
1379f0ea3689SLuigi Rizzo {
1380f0ea3689SLuigi Rizzo struct netmap_priv_d *priv;
1381f0ea3689SLuigi Rizzo int revents;
1382f0ea3689SLuigi Rizzo
1383f0ea3689SLuigi Rizzo if (hint != 0) {
13848c9874f5SVincenzo Maffione /* Called from netmap_notify(), typically from a
13858c9874f5SVincenzo Maffione * thread different from the one issuing kevent().
13868c9874f5SVincenzo Maffione * Assume we are ready. */
1387f0ea3689SLuigi Rizzo return 1;
1388f0ea3689SLuigi Rizzo }
13898c9874f5SVincenzo Maffione
13908c9874f5SVincenzo Maffione /* Called from kevent(). */
13918c9874f5SVincenzo Maffione priv = kn->kn_hook;
13928c9874f5SVincenzo Maffione revents = netmap_poll(priv, events, /*thread=*/NULL);
13938c9874f5SVincenzo Maffione
13948c9874f5SVincenzo Maffione return (events & revents) ? 1 : 0;
1395f0ea3689SLuigi Rizzo }
1396f0ea3689SLuigi Rizzo
1397f0ea3689SLuigi Rizzo static int
netmap_knread(struct knote * kn,long hint)1398f0ea3689SLuigi Rizzo netmap_knread(struct knote *kn, long hint)
1399f0ea3689SLuigi Rizzo {
1400f0ea3689SLuigi Rizzo return netmap_knrw(kn, hint, POLLIN);
1401f0ea3689SLuigi Rizzo }
1402f0ea3689SLuigi Rizzo
1403f0ea3689SLuigi Rizzo static int
netmap_knwrite(struct knote * kn,long hint)1404f0ea3689SLuigi Rizzo netmap_knwrite(struct knote *kn, long hint)
1405f0ea3689SLuigi Rizzo {
1406f0ea3689SLuigi Rizzo return netmap_knrw(kn, hint, POLLOUT);
1407f0ea3689SLuigi Rizzo }
1408f0ea3689SLuigi Rizzo
1409ef9ffb85SMark Johnston static const struct filterops netmap_rfiltops = {
1410f0ea3689SLuigi Rizzo .f_isfd = 1,
1411f0ea3689SLuigi Rizzo .f_detach = netmap_knrdetach,
1412f0ea3689SLuigi Rizzo .f_event = netmap_knread,
1413f0ea3689SLuigi Rizzo };
1414f0ea3689SLuigi Rizzo
1415ef9ffb85SMark Johnston static const struct filterops netmap_wfiltops = {
1416f0ea3689SLuigi Rizzo .f_isfd = 1,
1417f0ea3689SLuigi Rizzo .f_detach = netmap_knwdetach,
1418f0ea3689SLuigi Rizzo .f_event = netmap_knwrite,
1419f0ea3689SLuigi Rizzo };
1420f0ea3689SLuigi Rizzo
1421f0ea3689SLuigi Rizzo
1422f0ea3689SLuigi Rizzo /*
1423f0ea3689SLuigi Rizzo * This is called when a thread invokes kevent() to record
1424f0ea3689SLuigi Rizzo * a change in the configuration of the kqueue().
14258c9874f5SVincenzo Maffione * The 'priv' is the one associated to the open netmap device.
1426f0ea3689SLuigi Rizzo */
1427f0ea3689SLuigi Rizzo static int
netmap_kqfilter(struct cdev * dev,struct knote * kn)1428f0ea3689SLuigi Rizzo netmap_kqfilter(struct cdev *dev, struct knote *kn)
1429f0ea3689SLuigi Rizzo {
1430f0ea3689SLuigi Rizzo struct netmap_priv_d *priv;
1431f0ea3689SLuigi Rizzo int error;
1432f0ea3689SLuigi Rizzo struct netmap_adapter *na;
14330e73f29aSLuigi Rizzo struct nm_selinfo *si;
1434f0ea3689SLuigi Rizzo int ev = kn->kn_filter;
1435f0ea3689SLuigi Rizzo
1436f0ea3689SLuigi Rizzo if (ev != EVFILT_READ && ev != EVFILT_WRITE) {
1437a56136a1SVincenzo Maffione nm_prerr("bad filter request %d", ev);
1438f0ea3689SLuigi Rizzo return 1;
1439f0ea3689SLuigi Rizzo }
1440f0ea3689SLuigi Rizzo error = devfs_get_cdevpriv((void**)&priv);
1441f0ea3689SLuigi Rizzo if (error) {
1442a56136a1SVincenzo Maffione nm_prerr("device not yet setup");
1443f0ea3689SLuigi Rizzo return 1;
1444f0ea3689SLuigi Rizzo }
1445f0ea3689SLuigi Rizzo na = priv->np_na;
1446f0ea3689SLuigi Rizzo if (na == NULL) {
1447a56136a1SVincenzo Maffione nm_prerr("no netmap adapter for this file descriptor");
1448f0ea3689SLuigi Rizzo return 1;
1449f0ea3689SLuigi Rizzo }
1450f0ea3689SLuigi Rizzo /* the si is indicated in the priv */
1451847bf383SLuigi Rizzo si = priv->np_si[(ev == EVFILT_WRITE) ? NR_TX : NR_RX];
1452f0ea3689SLuigi Rizzo kn->kn_fop = (ev == EVFILT_WRITE) ?
1453f0ea3689SLuigi Rizzo &netmap_wfiltops : &netmap_rfiltops;
1454f0ea3689SLuigi Rizzo kn->kn_hook = priv;
145545100257SVincenzo Maffione NMG_LOCK();
145645100257SVincenzo Maffione si->kqueue_users++;
145745100257SVincenzo Maffione nm_prinf("kqueue users for %s: %d", si->mtxname, si->kqueue_users);
145845100257SVincenzo Maffione NMG_UNLOCK();
14598c9874f5SVincenzo Maffione knlist_add(&si->si.si_note, kn, /*islocked=*/0);
14608c9874f5SVincenzo Maffione
1461f0ea3689SLuigi Rizzo return 0;
1462f0ea3689SLuigi Rizzo }
1463f9790aebSLuigi Rizzo
146437e3a6d3SLuigi Rizzo static int
freebsd_netmap_poll(struct cdev * cdevi __unused,int events,struct thread * td)146537e3a6d3SLuigi Rizzo freebsd_netmap_poll(struct cdev *cdevi __unused, int events, struct thread *td)
146637e3a6d3SLuigi Rizzo {
146737e3a6d3SLuigi Rizzo struct netmap_priv_d *priv;
146837e3a6d3SLuigi Rizzo if (devfs_get_cdevpriv((void **)&priv)) {
146937e3a6d3SLuigi Rizzo return POLLERR;
147037e3a6d3SLuigi Rizzo }
147137e3a6d3SLuigi Rizzo return netmap_poll(priv, events, td);
147237e3a6d3SLuigi Rizzo }
147337e3a6d3SLuigi Rizzo
147437e3a6d3SLuigi Rizzo static int
freebsd_netmap_ioctl(struct cdev * dev __unused,u_long cmd,caddr_t data,int ffla __unused,struct thread * td)147537e3a6d3SLuigi Rizzo freebsd_netmap_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t data,
147637e3a6d3SLuigi Rizzo int ffla __unused, struct thread *td)
147737e3a6d3SLuigi Rizzo {
147837e3a6d3SLuigi Rizzo int error;
147937e3a6d3SLuigi Rizzo struct netmap_priv_d *priv;
148037e3a6d3SLuigi Rizzo
1481ffaa5debSSepherosa Ziehau CURVNET_SET(TD_TO_VNET(td));
148237e3a6d3SLuigi Rizzo error = devfs_get_cdevpriv((void **)&priv);
148337e3a6d3SLuigi Rizzo if (error) {
148437e3a6d3SLuigi Rizzo /* XXX ENOENT should be impossible, since the priv
148537e3a6d3SLuigi Rizzo * is now created in the open */
148637e3a6d3SLuigi Rizzo if (error == ENOENT)
148737e3a6d3SLuigi Rizzo error = ENXIO;
148837e3a6d3SLuigi Rizzo goto out;
148937e3a6d3SLuigi Rizzo }
14902ff91c17SVincenzo Maffione error = netmap_ioctl(priv, cmd, data, td, /*nr_body_is_user=*/1);
149137e3a6d3SLuigi Rizzo out:
149237e3a6d3SLuigi Rizzo CURVNET_RESTORE();
149337e3a6d3SLuigi Rizzo
149437e3a6d3SLuigi Rizzo return error;
149537e3a6d3SLuigi Rizzo }
149637e3a6d3SLuigi Rizzo
14972a7db7a6SVincenzo Maffione void
nm_os_onattach(if_t ifp)1498e330262fSJustin Hibbits nm_os_onattach(if_t ifp)
14992a7db7a6SVincenzo Maffione {
1500e330262fSJustin Hibbits if_setcapabilitiesbit(ifp, IFCAP_NETMAP, 0);
15012a7db7a6SVincenzo Maffione }
15022a7db7a6SVincenzo Maffione
15032a7db7a6SVincenzo Maffione void
nm_os_onenter(if_t ifp)1504e330262fSJustin Hibbits nm_os_onenter(if_t ifp)
15052a7db7a6SVincenzo Maffione {
15062a7db7a6SVincenzo Maffione struct netmap_adapter *na = NA(ifp);
15072a7db7a6SVincenzo Maffione
1508e330262fSJustin Hibbits na->if_transmit = if_gettransmitfn(ifp);
1509e330262fSJustin Hibbits if_settransmitfn(ifp, netmap_transmit);
1510e330262fSJustin Hibbits if_setcapenablebit(ifp, IFCAP_NETMAP, 0);
15112a7db7a6SVincenzo Maffione }
15122a7db7a6SVincenzo Maffione
15132a7db7a6SVincenzo Maffione void
nm_os_onexit(if_t ifp)1514e330262fSJustin Hibbits nm_os_onexit(if_t ifp)
15152a7db7a6SVincenzo Maffione {
15162a7db7a6SVincenzo Maffione struct netmap_adapter *na = NA(ifp);
15172a7db7a6SVincenzo Maffione
1518e330262fSJustin Hibbits if_settransmitfn(ifp, na->if_transmit);
1519e330262fSJustin Hibbits if_setcapenablebit(ifp, 0, IFCAP_NETMAP);
15202a7db7a6SVincenzo Maffione }
15212a7db7a6SVincenzo Maffione
152237e3a6d3SLuigi Rizzo extern struct cdevsw netmap_cdevsw; /* XXX used in netmap.c, should go elsewhere */
1523f9790aebSLuigi Rizzo struct cdevsw netmap_cdevsw = {
1524f9790aebSLuigi Rizzo .d_version = D_VERSION,
1525f9790aebSLuigi Rizzo .d_name = "netmap",
1526f9790aebSLuigi Rizzo .d_open = netmap_open,
1527f9790aebSLuigi Rizzo .d_mmap_single = netmap_mmap_single,
152837e3a6d3SLuigi Rizzo .d_ioctl = freebsd_netmap_ioctl,
152937e3a6d3SLuigi Rizzo .d_poll = freebsd_netmap_poll,
1530f0ea3689SLuigi Rizzo .d_kqfilter = netmap_kqfilter,
1531f9790aebSLuigi Rizzo .d_close = netmap_close,
1532f9790aebSLuigi Rizzo };
1533f0ea3689SLuigi Rizzo /*--- end of kqueue support ----*/
1534f9790aebSLuigi Rizzo
1535f9790aebSLuigi Rizzo /*
1536f9790aebSLuigi Rizzo * Kernel entry point.
1537f9790aebSLuigi Rizzo *
1538f9790aebSLuigi Rizzo * Initialize/finalize the module and return.
1539f9790aebSLuigi Rizzo *
1540f9790aebSLuigi Rizzo * Return 0 on success, errno on failure.
1541f9790aebSLuigi Rizzo */
1542f9790aebSLuigi Rizzo static int
netmap_loader(__unused struct module * module,int event,__unused void * arg)1543f9790aebSLuigi Rizzo netmap_loader(__unused struct module *module, int event, __unused void *arg)
1544f9790aebSLuigi Rizzo {
1545f9790aebSLuigi Rizzo int error = 0;
1546f9790aebSLuigi Rizzo
1547f9790aebSLuigi Rizzo switch (event) {
1548f9790aebSLuigi Rizzo case MOD_LOAD:
1549f9790aebSLuigi Rizzo error = netmap_init();
1550f9790aebSLuigi Rizzo break;
1551f9790aebSLuigi Rizzo
1552f9790aebSLuigi Rizzo case MOD_UNLOAD:
1553847adfb7SLuigi Rizzo /*
1554847adfb7SLuigi Rizzo * if some one is still using netmap,
1555847adfb7SLuigi Rizzo * then the module can not be unloaded.
1556847adfb7SLuigi Rizzo */
1557847adfb7SLuigi Rizzo if (netmap_use_count) {
1558a56136a1SVincenzo Maffione nm_prerr("netmap module can not be unloaded - netmap_use_count: %d",
1559847adfb7SLuigi Rizzo netmap_use_count);
1560847adfb7SLuigi Rizzo error = EBUSY;
1561847adfb7SLuigi Rizzo break;
1562847adfb7SLuigi Rizzo }
1563f9790aebSLuigi Rizzo netmap_fini();
1564f9790aebSLuigi Rizzo break;
1565f9790aebSLuigi Rizzo
1566f9790aebSLuigi Rizzo default:
1567f9790aebSLuigi Rizzo error = EOPNOTSUPP;
1568f9790aebSLuigi Rizzo break;
1569f9790aebSLuigi Rizzo }
1570f9790aebSLuigi Rizzo
1571f9790aebSLuigi Rizzo return (error);
1572f9790aebSLuigi Rizzo }
1573f9790aebSLuigi Rizzo
157437e3a6d3SLuigi Rizzo #ifdef DEV_MODULE_ORDERED
157537e3a6d3SLuigi Rizzo /*
157637e3a6d3SLuigi Rizzo * The netmap module contains three drivers: (i) the netmap character device
157737e3a6d3SLuigi Rizzo * driver; (ii) the ptnetmap memdev PCI device driver, (iii) the ptnet PCI
157837e3a6d3SLuigi Rizzo * device driver. The attach() routines of both (ii) and (iii) need the
157937e3a6d3SLuigi Rizzo * lock of the global allocator, and such lock is initialized in netmap_init(),
158037e3a6d3SLuigi Rizzo * which is part of (i).
158137e3a6d3SLuigi Rizzo * Therefore, we make sure that (i) is loaded before (ii) and (iii), using
158237e3a6d3SLuigi Rizzo * the 'order' parameter of driver declaration macros. For (i), we specify
158337e3a6d3SLuigi Rizzo * SI_ORDER_MIDDLE, while higher orders are used with the DRIVER_MODULE_ORDERED
158437e3a6d3SLuigi Rizzo * macros for (ii) and (iii).
158537e3a6d3SLuigi Rizzo */
158637e3a6d3SLuigi Rizzo DEV_MODULE_ORDERED(netmap, netmap_loader, NULL, SI_ORDER_MIDDLE);
158737e3a6d3SLuigi Rizzo #else /* !DEV_MODULE_ORDERED */
1588f9790aebSLuigi Rizzo DEV_MODULE(netmap, netmap_loader, NULL);
158937e3a6d3SLuigi Rizzo #endif /* DEV_MODULE_ORDERED */
159037e3a6d3SLuigi Rizzo MODULE_DEPEND(netmap, pci, 1, 1, 1);
1591735c8d95SLuigi Rizzo MODULE_VERSION(netmap, 1);
159237e3a6d3SLuigi Rizzo /* reduce conditional code */
159337e3a6d3SLuigi Rizzo // linux API, use for the knlist in FreeBSD
159437e3a6d3SLuigi Rizzo /* use a private mutex for the knlist */
1595