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