xref: /freebsd-13.1/sys/dev/netmap/netmap_mem2.h (revision b6e66be2)
1718cf2ccSPedro F. Giffuni /*-
2718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3718cf2ccSPedro F. Giffuni  *
437e3a6d3SLuigi Rizzo  * Copyright (C) 2012-2014 Matteo Landi
537e3a6d3SLuigi Rizzo  * Copyright (C) 2012-2016 Luigi Rizzo
637e3a6d3SLuigi Rizzo  * Copyright (C) 2012-2016 Giuseppe Lettieri
737e3a6d3SLuigi Rizzo  * All rights reserved.
8ab1e2860SLuigi Rizzo  *
9ab1e2860SLuigi Rizzo  * Redistribution and use in source and binary forms, with or without
10ab1e2860SLuigi Rizzo  * modification, are permitted provided that the following conditions
11ab1e2860SLuigi Rizzo  * are met:
12ab1e2860SLuigi Rizzo  *   1. Redistributions of source code must retain the above copyright
13ab1e2860SLuigi Rizzo  *      notice, this list of conditions and the following disclaimer.
14ab1e2860SLuigi Rizzo  *   2. Redistributions in binary form must reproduce the above copyright
15ab1e2860SLuigi Rizzo  *      notice, this list of conditions and the following disclaimer in the
16ab1e2860SLuigi Rizzo  *    documentation and/or other materials provided with the distribution.
17ab1e2860SLuigi Rizzo  *
18ab1e2860SLuigi Rizzo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19ab1e2860SLuigi Rizzo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20ab1e2860SLuigi Rizzo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21ab1e2860SLuigi Rizzo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22ab1e2860SLuigi Rizzo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23ab1e2860SLuigi Rizzo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24ab1e2860SLuigi Rizzo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25ab1e2860SLuigi Rizzo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26ab1e2860SLuigi Rizzo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27ab1e2860SLuigi Rizzo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28ab1e2860SLuigi Rizzo  * SUCH DAMAGE.
29ab1e2860SLuigi Rizzo  */
30ab1e2860SLuigi Rizzo 
31ab1e2860SLuigi Rizzo /*
32ab1e2860SLuigi Rizzo  * $FreeBSD$
33ab1e2860SLuigi Rizzo  *
34ab1e2860SLuigi Rizzo  * (New) memory allocator for netmap
35ab1e2860SLuigi Rizzo  */
36ab1e2860SLuigi Rizzo 
37ab1e2860SLuigi Rizzo /*
38ab1e2860SLuigi Rizzo  * This allocator creates three memory pools:
39ab1e2860SLuigi Rizzo  *	nm_if_pool	for the struct netmap_if
40ab1e2860SLuigi Rizzo  *	nm_ring_pool	for the struct netmap_ring
41ab1e2860SLuigi Rizzo  *	nm_buf_pool	for the packet buffers.
42ab1e2860SLuigi Rizzo  *
43ab1e2860SLuigi Rizzo  * that contain netmap objects. Each pool is made of a number of clusters,
44ab1e2860SLuigi Rizzo  * multiple of a page size, each containing an integer number of objects.
45ab1e2860SLuigi Rizzo  * The clusters are contiguous in user space but not in the kernel.
46ab1e2860SLuigi Rizzo  * Only nm_buf_pool needs to be dma-able,
47ab1e2860SLuigi Rizzo  * but for convenience use the same type of allocator for all.
48ab1e2860SLuigi Rizzo  *
49ab1e2860SLuigi Rizzo  * Once mapped, the three pools are exported to userspace
50ab1e2860SLuigi Rizzo  * as a contiguous block, starting from nm_if_pool. Each
51ab1e2860SLuigi Rizzo  * cluster (and pool) is an integral number of pages.
52ab1e2860SLuigi Rizzo  *   [ . . . ][ . . . . . .][ . . . . . . . . . .]
53ab1e2860SLuigi Rizzo  *    nm_if     nm_ring            nm_buf
54ab1e2860SLuigi Rizzo  *
55ab1e2860SLuigi Rizzo  * The userspace areas contain offsets of the objects in userspace.
56ab1e2860SLuigi Rizzo  * When (at init time) we write these offsets, we find out the index
57ab1e2860SLuigi Rizzo  * of the object, and from there locate the offset from the beginning
58ab1e2860SLuigi Rizzo  * of the region.
59ab1e2860SLuigi Rizzo  *
60ab1e2860SLuigi Rizzo  * The invididual allocators manage a pool of memory for objects of
61ab1e2860SLuigi Rizzo  * the same size.
62ab1e2860SLuigi Rizzo  * The pool is split into smaller clusters, whose size is a
63ab1e2860SLuigi Rizzo  * multiple of the page size. The cluster size is chosen
64ab1e2860SLuigi Rizzo  * to minimize the waste for a given max cluster size
65ab1e2860SLuigi Rizzo  * (we do it by brute force, as we have relatively few objects
66ab1e2860SLuigi Rizzo  * per cluster).
67ab1e2860SLuigi Rizzo  *
68ab1e2860SLuigi Rizzo  * Objects are aligned to the cache line (64 bytes) rounding up object
69ab1e2860SLuigi Rizzo  * sizes when needed. A bitmap contains the state of each object.
70ab1e2860SLuigi Rizzo  * Allocation scans the bitmap; this is done only on attach, so we are not
71ab1e2860SLuigi Rizzo  * too worried about performance
72ab1e2860SLuigi Rizzo  *
73ab1e2860SLuigi Rizzo  * For each allocator we can define (thorugh sysctl) the size and
74ab1e2860SLuigi Rizzo  * number of each object. Memory is allocated at the first use of a
75ab1e2860SLuigi Rizzo  * netmap file descriptor, and can be freed when all such descriptors
76ab1e2860SLuigi Rizzo  * have been released (including unmapping the memory).
77ab1e2860SLuigi Rizzo  * If memory is scarce, the system tries to get as much as possible
78ab1e2860SLuigi Rizzo  * and the sysctl values reflect the actual allocation.
79ab1e2860SLuigi Rizzo  * Together with desired values, the sysctl export also absolute
80ab1e2860SLuigi Rizzo  * min and maximum values that cannot be overridden.
81ab1e2860SLuigi Rizzo  *
82ab1e2860SLuigi Rizzo  * struct netmap_if:
83ab1e2860SLuigi Rizzo  *	variable size, max 16 bytes per ring pair plus some fixed amount.
84ab1e2860SLuigi Rizzo  *	1024 bytes should be large enough in practice.
85ab1e2860SLuigi Rizzo  *
86ab1e2860SLuigi Rizzo  *	In the worst case we have one netmap_if per ring in the system.
87ab1e2860SLuigi Rizzo  *
88ab1e2860SLuigi Rizzo  * struct netmap_ring
89ab1e2860SLuigi Rizzo  *	variable size, 8 byte per slot plus some fixed amount.
90ab1e2860SLuigi Rizzo  *	Rings can be large (e.g. 4k slots, or >32Kbytes).
91ab1e2860SLuigi Rizzo  *	We default to 36 KB (9 pages), and a few hundred rings.
92ab1e2860SLuigi Rizzo  *
93ab1e2860SLuigi Rizzo  * struct netmap_buffer
94ab1e2860SLuigi Rizzo  *	The more the better, both because fast interfaces tend to have
95ab1e2860SLuigi Rizzo  *	many slots, and because we may want to use buffers to store
96ab1e2860SLuigi Rizzo  *	packets in userspace avoiding copies.
97ab1e2860SLuigi Rizzo  *	Must contain a full frame (eg 1518, or more for vlans, jumbo
98ab1e2860SLuigi Rizzo  *	frames etc.) plus be nicely aligned, plus some NICs restrict
99ab1e2860SLuigi Rizzo  *	the size to multiple of 1K or so. Default to 2K
100ab1e2860SLuigi Rizzo  */
101ab1e2860SLuigi Rizzo #ifndef _NET_NETMAP_MEM2_H_
102ab1e2860SLuigi Rizzo #define _NET_NETMAP_MEM2_H_
103ab1e2860SLuigi Rizzo 
104ab1e2860SLuigi Rizzo 
105ab1e2860SLuigi Rizzo 
106ab1e2860SLuigi Rizzo /* We implement two kinds of netmap_mem_d structures:
107ab1e2860SLuigi Rizzo  *
108ab1e2860SLuigi Rizzo  * - global: used by hardware NICS;
109ab1e2860SLuigi Rizzo  *
110ab1e2860SLuigi Rizzo  * - private: used by VALE ports.
111ab1e2860SLuigi Rizzo  *
112ab1e2860SLuigi Rizzo  * In both cases, the netmap_mem_d structure has the same lifetime as the
113ab1e2860SLuigi Rizzo  * netmap_adapter of the corresponding NIC or port. It is the responsibility of
114ab1e2860SLuigi Rizzo  * the client code to delete the private allocator when the associated
115ab1e2860SLuigi Rizzo  * netmap_adapter is freed (this is implemented by the NAF_MEM_OWNER flag in
116ab1e2860SLuigi Rizzo  * netmap.c).  The 'refcount' field counts the number of active users of the
117ab1e2860SLuigi Rizzo  * structure. The global allocator uses this information to prevent/allow
118ab1e2860SLuigi Rizzo  * reconfiguration. The private allocators release all their memory when there
119ab1e2860SLuigi Rizzo  * are no active users.  By 'active user' we mean an existing netmap_priv
120ab1e2860SLuigi Rizzo  * structure holding a reference to the allocator.
121ab1e2860SLuigi Rizzo  */
122ab1e2860SLuigi Rizzo 
123ab1e2860SLuigi Rizzo extern struct netmap_mem_d nm_mem;
124c3e9b4dbSLuiz Otavio O Souza typedef uint16_t nm_memid_t;
125ab1e2860SLuigi Rizzo 
12637e3a6d3SLuigi Rizzo int	   netmap_mem_get_lut(struct netmap_mem_d *, struct netmap_lut *);
127c3e9b4dbSLuiz Otavio O Souza nm_memid_t netmap_mem_get_id(struct netmap_mem_d *);
128ab1e2860SLuigi Rizzo vm_paddr_t netmap_mem_ofstophys(struct netmap_mem_d *, vm_ooffset_t);
12937e3a6d3SLuigi Rizzo #ifdef _WIN32
13037e3a6d3SLuigi Rizzo PMDL win32_build_user_vm_map(struct netmap_mem_d* nmd);
13137e3a6d3SLuigi Rizzo #endif
1324bf50f18SLuigi Rizzo int	   netmap_mem_finalize(struct netmap_mem_d *, struct netmap_adapter *);
133ab1e2860SLuigi Rizzo int 	   netmap_mem_init(void);
134ab1e2860SLuigi Rizzo void 	   netmap_mem_fini(void);
135c3e9b4dbSLuiz Otavio O Souza struct netmap_if * netmap_mem_if_new(struct netmap_adapter *, struct netmap_priv_d *);
136f9790aebSLuigi Rizzo void 	   netmap_mem_if_delete(struct netmap_adapter *, struct netmap_if *);
137f9790aebSLuigi Rizzo int	   netmap_mem_rings_create(struct netmap_adapter *);
138f9790aebSLuigi Rizzo void	   netmap_mem_rings_delete(struct netmap_adapter *);
1394f80b14cSVincenzo Maffione int 	   netmap_mem_deref(struct netmap_mem_d *, struct netmap_adapter *);
14037e3a6d3SLuigi Rizzo int	   netmap_mem2_get_pool_info(struct netmap_mem_d *, u_int, u_int *, u_int *);
1412ff91c17SVincenzo Maffione int	   netmap_mem_get_info(struct netmap_mem_d *, uint64_t *size,
1422ff91c17SVincenzo Maffione 				u_int *memflags, nm_memid_t *id);
143f9790aebSLuigi Rizzo ssize_t    netmap_mem_if_offset(struct netmap_mem_d *, const void *vaddr);
144c3e9b4dbSLuiz Otavio O Souza struct netmap_mem_d* netmap_mem_private_new( u_int txr, u_int txd, u_int rxr, u_int rxd,
145c3e9b4dbSLuiz Otavio O Souza 		u_int extra_bufs, u_int npipes, int* error);
146847bf383SLuigi Rizzo 
147c3e9b4dbSLuiz Otavio O Souza #define netmap_mem_get(d) __netmap_mem_get(d, __FUNCTION__, __LINE__)
148c3e9b4dbSLuiz Otavio O Souza #define netmap_mem_put(d) __netmap_mem_put(d, __FUNCTION__, __LINE__)
149c3e9b4dbSLuiz Otavio O Souza struct netmap_mem_d* __netmap_mem_get(struct netmap_mem_d *, const char *, int);
150847bf383SLuigi Rizzo void __netmap_mem_put(struct netmap_mem_d *, const char *, int);
151c3e9b4dbSLuiz Otavio O Souza struct netmap_mem_d* netmap_mem_find(nm_memid_t);
1524f80b14cSVincenzo Maffione unsigned netmap_mem_bufsize(struct netmap_mem_d *nmd);
1534f80b14cSVincenzo Maffione 
1544f80b14cSVincenzo Maffione #ifdef WITH_EXTMEM
1552ff91c17SVincenzo Maffione struct netmap_mem_d* netmap_mem_ext_create(uint64_t, struct nmreq_pools_info *, int *);
1564f80b14cSVincenzo Maffione #else /* !WITH_EXTMEM */
1574f80b14cSVincenzo Maffione #define netmap_mem_ext_create(nmr, _perr) \
1584f80b14cSVincenzo Maffione 	({ int *perr = _perr; if (perr) *(perr) = EOPNOTSUPP; NULL; })
1594f80b14cSVincenzo Maffione #endif /* WITH_EXTMEM */
160ab1e2860SLuigi Rizzo 
161*b6e66be2SVincenzo Maffione #ifdef WITH_PTNETMAP
16237e3a6d3SLuigi Rizzo struct netmap_mem_d* netmap_mem_pt_guest_new(struct ifnet *,
16337e3a6d3SLuigi Rizzo 					     unsigned int nifp_offset,
164844a6f0cSLuigi Rizzo 					     unsigned int memid);
16537e3a6d3SLuigi Rizzo struct ptnetmap_memdev;
16637e3a6d3SLuigi Rizzo struct netmap_mem_d* netmap_mem_pt_guest_attach(struct ptnetmap_memdev *, uint16_t);
16737e3a6d3SLuigi Rizzo int netmap_mem_pt_guest_ifp_del(struct netmap_mem_d *, struct ifnet *);
168*b6e66be2SVincenzo Maffione #endif /* WITH_PTNETMAP */
16937e3a6d3SLuigi Rizzo 
1702ff91c17SVincenzo Maffione int netmap_mem_pools_info_get(struct nmreq_pools_info *,
1712ff91c17SVincenzo Maffione 				struct netmap_mem_d *);
172844a6f0cSLuigi Rizzo 
1734bf50f18SLuigi Rizzo #define NETMAP_MEM_PRIVATE	0x2	/* allocator uses private address space */
1744bf50f18SLuigi Rizzo #define NETMAP_MEM_IO		0x4	/* the underlying memory is mmapped I/O */
1754f80b14cSVincenzo Maffione #define NETMAP_MEM_EXT		0x10	/* external memory (not remappable) */
176ab1e2860SLuigi Rizzo 
177f0ea3689SLuigi Rizzo uint32_t netmap_extra_alloc(struct netmap_adapter *, uint32_t *, uint32_t n);
178ab1e2860SLuigi Rizzo 
1792ff91c17SVincenzo Maffione #ifdef WITH_EXTMEM
1802ff91c17SVincenzo Maffione #include <net/netmap_virt.h>
1812ff91c17SVincenzo Maffione struct nm_os_extmem; /* opaque */
1822ff91c17SVincenzo Maffione struct nm_os_extmem *nm_os_extmem_create(unsigned long, struct nmreq_pools_info *, int *perror);
1832ff91c17SVincenzo Maffione char *nm_os_extmem_nextpage(struct nm_os_extmem *);
1842ff91c17SVincenzo Maffione int nm_os_extmem_nr_pages(struct nm_os_extmem *);
1852ff91c17SVincenzo Maffione int nm_os_extmem_isequal(struct nm_os_extmem *, struct nm_os_extmem *);
1862ff91c17SVincenzo Maffione void nm_os_extmem_delete(struct nm_os_extmem *);
1872ff91c17SVincenzo Maffione #endif /* WITH_EXTMEM */
1882ff91c17SVincenzo Maffione 
189ab1e2860SLuigi Rizzo #endif
190