xref: /freebsd-14.2/sys/dev/netmap/netmap_mem2.h (revision f0ea3689)
1ab1e2860SLuigi Rizzo /*
217885a7bSLuigi Rizzo  * Copyright (C) 2012-2014 Matteo Landi, Luigi Rizzo, Giuseppe Lettieri. All rights reserved.
3ab1e2860SLuigi Rizzo  *
4ab1e2860SLuigi Rizzo  * Redistribution and use in source and binary forms, with or without
5ab1e2860SLuigi Rizzo  * modification, are permitted provided that the following conditions
6ab1e2860SLuigi Rizzo  * are met:
7ab1e2860SLuigi Rizzo  *   1. Redistributions of source code must retain the above copyright
8ab1e2860SLuigi Rizzo  *      notice, this list of conditions and the following disclaimer.
9ab1e2860SLuigi Rizzo  *   2. Redistributions in binary form must reproduce the above copyright
10ab1e2860SLuigi Rizzo  *      notice, this list of conditions and the following disclaimer in the
11ab1e2860SLuigi Rizzo  *    documentation and/or other materials provided with the distribution.
12ab1e2860SLuigi Rizzo  *
13ab1e2860SLuigi Rizzo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14ab1e2860SLuigi Rizzo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15ab1e2860SLuigi Rizzo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16ab1e2860SLuigi Rizzo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17ab1e2860SLuigi Rizzo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18ab1e2860SLuigi Rizzo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19ab1e2860SLuigi Rizzo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20ab1e2860SLuigi Rizzo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21ab1e2860SLuigi Rizzo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22ab1e2860SLuigi Rizzo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23ab1e2860SLuigi Rizzo  * SUCH DAMAGE.
24ab1e2860SLuigi Rizzo  */
25ab1e2860SLuigi Rizzo 
26ab1e2860SLuigi Rizzo /*
27ab1e2860SLuigi Rizzo  * $FreeBSD$
28ab1e2860SLuigi Rizzo  *
29ab1e2860SLuigi Rizzo  * (New) memory allocator for netmap
30ab1e2860SLuigi Rizzo  */
31ab1e2860SLuigi Rizzo 
32ab1e2860SLuigi Rizzo /*
33ab1e2860SLuigi Rizzo  * This allocator creates three memory pools:
34ab1e2860SLuigi Rizzo  *	nm_if_pool	for the struct netmap_if
35ab1e2860SLuigi Rizzo  *	nm_ring_pool	for the struct netmap_ring
36ab1e2860SLuigi Rizzo  *	nm_buf_pool	for the packet buffers.
37ab1e2860SLuigi Rizzo  *
38ab1e2860SLuigi Rizzo  * that contain netmap objects. Each pool is made of a number of clusters,
39ab1e2860SLuigi Rizzo  * multiple of a page size, each containing an integer number of objects.
40ab1e2860SLuigi Rizzo  * The clusters are contiguous in user space but not in the kernel.
41ab1e2860SLuigi Rizzo  * Only nm_buf_pool needs to be dma-able,
42ab1e2860SLuigi Rizzo  * but for convenience use the same type of allocator for all.
43ab1e2860SLuigi Rizzo  *
44ab1e2860SLuigi Rizzo  * Once mapped, the three pools are exported to userspace
45ab1e2860SLuigi Rizzo  * as a contiguous block, starting from nm_if_pool. Each
46ab1e2860SLuigi Rizzo  * cluster (and pool) is an integral number of pages.
47ab1e2860SLuigi Rizzo  *   [ . . . ][ . . . . . .][ . . . . . . . . . .]
48ab1e2860SLuigi Rizzo  *    nm_if     nm_ring            nm_buf
49ab1e2860SLuigi Rizzo  *
50ab1e2860SLuigi Rizzo  * The userspace areas contain offsets of the objects in userspace.
51ab1e2860SLuigi Rizzo  * When (at init time) we write these offsets, we find out the index
52ab1e2860SLuigi Rizzo  * of the object, and from there locate the offset from the beginning
53ab1e2860SLuigi Rizzo  * of the region.
54ab1e2860SLuigi Rizzo  *
55ab1e2860SLuigi Rizzo  * The invididual allocators manage a pool of memory for objects of
56ab1e2860SLuigi Rizzo  * the same size.
57ab1e2860SLuigi Rizzo  * The pool is split into smaller clusters, whose size is a
58ab1e2860SLuigi Rizzo  * multiple of the page size. The cluster size is chosen
59ab1e2860SLuigi Rizzo  * to minimize the waste for a given max cluster size
60ab1e2860SLuigi Rizzo  * (we do it by brute force, as we have relatively few objects
61ab1e2860SLuigi Rizzo  * per cluster).
62ab1e2860SLuigi Rizzo  *
63ab1e2860SLuigi Rizzo  * Objects are aligned to the cache line (64 bytes) rounding up object
64ab1e2860SLuigi Rizzo  * sizes when needed. A bitmap contains the state of each object.
65ab1e2860SLuigi Rizzo  * Allocation scans the bitmap; this is done only on attach, so we are not
66ab1e2860SLuigi Rizzo  * too worried about performance
67ab1e2860SLuigi Rizzo  *
68ab1e2860SLuigi Rizzo  * For each allocator we can define (thorugh sysctl) the size and
69ab1e2860SLuigi Rizzo  * number of each object. Memory is allocated at the first use of a
70ab1e2860SLuigi Rizzo  * netmap file descriptor, and can be freed when all such descriptors
71ab1e2860SLuigi Rizzo  * have been released (including unmapping the memory).
72ab1e2860SLuigi Rizzo  * If memory is scarce, the system tries to get as much as possible
73ab1e2860SLuigi Rizzo  * and the sysctl values reflect the actual allocation.
74ab1e2860SLuigi Rizzo  * Together with desired values, the sysctl export also absolute
75ab1e2860SLuigi Rizzo  * min and maximum values that cannot be overridden.
76ab1e2860SLuigi Rizzo  *
77ab1e2860SLuigi Rizzo  * struct netmap_if:
78ab1e2860SLuigi Rizzo  *	variable size, max 16 bytes per ring pair plus some fixed amount.
79ab1e2860SLuigi Rizzo  *	1024 bytes should be large enough in practice.
80ab1e2860SLuigi Rizzo  *
81ab1e2860SLuigi Rizzo  *	In the worst case we have one netmap_if per ring in the system.
82ab1e2860SLuigi Rizzo  *
83ab1e2860SLuigi Rizzo  * struct netmap_ring
84ab1e2860SLuigi Rizzo  *	variable size, 8 byte per slot plus some fixed amount.
85ab1e2860SLuigi Rizzo  *	Rings can be large (e.g. 4k slots, or >32Kbytes).
86ab1e2860SLuigi Rizzo  *	We default to 36 KB (9 pages), and a few hundred rings.
87ab1e2860SLuigi Rizzo  *
88ab1e2860SLuigi Rizzo  * struct netmap_buffer
89ab1e2860SLuigi Rizzo  *	The more the better, both because fast interfaces tend to have
90ab1e2860SLuigi Rizzo  *	many slots, and because we may want to use buffers to store
91ab1e2860SLuigi Rizzo  *	packets in userspace avoiding copies.
92ab1e2860SLuigi Rizzo  *	Must contain a full frame (eg 1518, or more for vlans, jumbo
93ab1e2860SLuigi Rizzo  *	frames etc.) plus be nicely aligned, plus some NICs restrict
94ab1e2860SLuigi Rizzo  *	the size to multiple of 1K or so. Default to 2K
95ab1e2860SLuigi Rizzo  */
96ab1e2860SLuigi Rizzo #ifndef _NET_NETMAP_MEM2_H_
97ab1e2860SLuigi Rizzo #define _NET_NETMAP_MEM2_H_
98ab1e2860SLuigi Rizzo 
99ab1e2860SLuigi Rizzo 
100ab1e2860SLuigi Rizzo #define NETMAP_BUF_MAX_NUM	20*4096*2	/* large machine */
101ab1e2860SLuigi Rizzo 
102ab1e2860SLuigi Rizzo #define NETMAP_POOL_MAX_NAMSZ	32
103ab1e2860SLuigi Rizzo 
104ab1e2860SLuigi Rizzo 
105ab1e2860SLuigi Rizzo enum {
106ab1e2860SLuigi Rizzo 	NETMAP_IF_POOL   = 0,
107ab1e2860SLuigi Rizzo 	NETMAP_RING_POOL,
108ab1e2860SLuigi Rizzo 	NETMAP_BUF_POOL,
109ab1e2860SLuigi Rizzo 	NETMAP_POOLS_NR
110ab1e2860SLuigi Rizzo };
111ab1e2860SLuigi Rizzo 
112ab1e2860SLuigi Rizzo 
113ab1e2860SLuigi Rizzo struct netmap_obj_params {
114ab1e2860SLuigi Rizzo 	u_int size;
115ab1e2860SLuigi Rizzo 	u_int num;
116ab1e2860SLuigi Rizzo };
117ab1e2860SLuigi Rizzo struct netmap_obj_pool {
118ab1e2860SLuigi Rizzo 	char name[NETMAP_POOL_MAX_NAMSZ];	/* name of the allocator */
119ab1e2860SLuigi Rizzo 
120ab1e2860SLuigi Rizzo 	/* ---------------------------------------------------*/
121ab1e2860SLuigi Rizzo 	/* these are only meaningful if the pool is finalized */
122ab1e2860SLuigi Rizzo 	/* (see 'finalized' field in netmap_mem_d)            */
123ab1e2860SLuigi Rizzo 	u_int objtotal;         /* actual total number of objects. */
124ab1e2860SLuigi Rizzo 	u_int memtotal;		/* actual total memory space */
125ab1e2860SLuigi Rizzo 	u_int numclusters;	/* actual number of clusters */
126ab1e2860SLuigi Rizzo 
127ab1e2860SLuigi Rizzo 	u_int objfree;          /* number of free objects. */
128ab1e2860SLuigi Rizzo 
129ab1e2860SLuigi Rizzo 	struct lut_entry *lut;  /* virt,phys addresses, objtotal entries */
130ab1e2860SLuigi Rizzo 	uint32_t *bitmap;       /* one bit per buffer, 1 means free */
131ab1e2860SLuigi Rizzo 	uint32_t bitmap_slots;	/* number of uint32 entries in bitmap */
132ab1e2860SLuigi Rizzo 	/* ---------------------------------------------------*/
133ab1e2860SLuigi Rizzo 
134ab1e2860SLuigi Rizzo 	/* limits */
135ab1e2860SLuigi Rizzo 	u_int objminsize;	/* minimum object size */
136ab1e2860SLuigi Rizzo 	u_int objmaxsize;	/* maximum object size */
137ab1e2860SLuigi Rizzo 	u_int nummin;		/* minimum number of objects */
138ab1e2860SLuigi Rizzo 	u_int nummax;		/* maximum number of objects */
139ab1e2860SLuigi Rizzo 
140ab1e2860SLuigi Rizzo 	/* these are changed only by config */
141ab1e2860SLuigi Rizzo 	u_int _objtotal;	/* total number of objects */
142ab1e2860SLuigi Rizzo 	u_int _objsize;		/* object size */
143ab1e2860SLuigi Rizzo 	u_int _clustsize;       /* cluster size */
144ab1e2860SLuigi Rizzo 	u_int _clustentries;    /* objects per cluster */
145ab1e2860SLuigi Rizzo 	u_int _numclusters;	/* number of clusters */
146ab1e2860SLuigi Rizzo 
147ab1e2860SLuigi Rizzo 	/* requested values */
148ab1e2860SLuigi Rizzo 	u_int r_objtotal;
149ab1e2860SLuigi Rizzo 	u_int r_objsize;
150ab1e2860SLuigi Rizzo };
151ab1e2860SLuigi Rizzo 
152ab1e2860SLuigi Rizzo #ifdef linux
153ab1e2860SLuigi Rizzo // XXX a mtx would suffice here 20130415 lr
154ab1e2860SLuigi Rizzo #define NMA_LOCK_T		struct semaphore
155ab1e2860SLuigi Rizzo #else /* !linux */
156ab1e2860SLuigi Rizzo #define NMA_LOCK_T		struct mtx
157ab1e2860SLuigi Rizzo #endif /* linux */
158ab1e2860SLuigi Rizzo 
159ab1e2860SLuigi Rizzo typedef int (*netmap_mem_config_t)(struct netmap_mem_d*);
160ab1e2860SLuigi Rizzo typedef int (*netmap_mem_finalize_t)(struct netmap_mem_d*);
161ab1e2860SLuigi Rizzo typedef void (*netmap_mem_deref_t)(struct netmap_mem_d*);
162ab1e2860SLuigi Rizzo 
163*f0ea3689SLuigi Rizzo typedef uint16_t nm_memid_t;
164ab1e2860SLuigi Rizzo 
165ab1e2860SLuigi Rizzo /* We implement two kinds of netmap_mem_d structures:
166ab1e2860SLuigi Rizzo  *
167ab1e2860SLuigi Rizzo  * - global: used by hardware NICS;
168ab1e2860SLuigi Rizzo  *
169ab1e2860SLuigi Rizzo  * - private: used by VALE ports.
170ab1e2860SLuigi Rizzo  *
171ab1e2860SLuigi Rizzo  * In both cases, the netmap_mem_d structure has the same lifetime as the
172ab1e2860SLuigi Rizzo  * netmap_adapter of the corresponding NIC or port. It is the responsibility of
173ab1e2860SLuigi Rizzo  * the client code to delete the private allocator when the associated
174ab1e2860SLuigi Rizzo  * netmap_adapter is freed (this is implemented by the NAF_MEM_OWNER flag in
175ab1e2860SLuigi Rizzo  * netmap.c).  The 'refcount' field counts the number of active users of the
176ab1e2860SLuigi Rizzo  * structure. The global allocator uses this information to prevent/allow
177ab1e2860SLuigi Rizzo  * reconfiguration. The private allocators release all their memory when there
178ab1e2860SLuigi Rizzo  * are no active users.  By 'active user' we mean an existing netmap_priv
179ab1e2860SLuigi Rizzo  * structure holding a reference to the allocator.
180ab1e2860SLuigi Rizzo  */
181ab1e2860SLuigi Rizzo struct netmap_mem_d {
182ab1e2860SLuigi Rizzo 	NMA_LOCK_T nm_mtx;  /* protect the allocator */
183ab1e2860SLuigi Rizzo 	u_int nm_totalsize; /* shorthand */
184ab1e2860SLuigi Rizzo 
185ab1e2860SLuigi Rizzo 	u_int flags;
186ab1e2860SLuigi Rizzo #define NETMAP_MEM_FINALIZED	0x1	/* preallocation done */
187ab1e2860SLuigi Rizzo #define NETMAP_MEM_PRIVATE	0x2	/* uses private address space */
188ab1e2860SLuigi Rizzo 	int lasterr;		/* last error for curr config */
189ab1e2860SLuigi Rizzo 	int refcount;		/* existing priv structures */
190ab1e2860SLuigi Rizzo 	/* the three allocators */
191ab1e2860SLuigi Rizzo 	struct netmap_obj_pool pools[NETMAP_POOLS_NR];
192ab1e2860SLuigi Rizzo 
193ab1e2860SLuigi Rizzo 	netmap_mem_config_t   config;
194ab1e2860SLuigi Rizzo 	netmap_mem_finalize_t finalize;
195ab1e2860SLuigi Rizzo 	netmap_mem_deref_t    deref;
196*f0ea3689SLuigi Rizzo 
197*f0ea3689SLuigi Rizzo 	nm_memid_t nm_id;	/* allocator identifier */
198*f0ea3689SLuigi Rizzo 
199*f0ea3689SLuigi Rizzo 	/* list of all existing allocators, sorted by nm_id */
200*f0ea3689SLuigi Rizzo 	struct netmap_mem_d *prev, *next;
201ab1e2860SLuigi Rizzo };
202ab1e2860SLuigi Rizzo 
203ab1e2860SLuigi Rizzo extern struct netmap_mem_d nm_mem;
204ab1e2860SLuigi Rizzo 
205ab1e2860SLuigi Rizzo vm_paddr_t netmap_mem_ofstophys(struct netmap_mem_d *, vm_ooffset_t);
206ab1e2860SLuigi Rizzo int	   netmap_mem_finalize(struct netmap_mem_d *);
207ab1e2860SLuigi Rizzo int 	   netmap_mem_init(void);
208ab1e2860SLuigi Rizzo void 	   netmap_mem_fini(void);
209f9790aebSLuigi Rizzo struct netmap_if *
210f9790aebSLuigi Rizzo 	   netmap_mem_if_new(const char *, struct netmap_adapter *);
211f9790aebSLuigi Rizzo void 	   netmap_mem_if_delete(struct netmap_adapter *, struct netmap_if *);
212f9790aebSLuigi Rizzo int	   netmap_mem_rings_create(struct netmap_adapter *);
213f9790aebSLuigi Rizzo void	   netmap_mem_rings_delete(struct netmap_adapter *);
214ab1e2860SLuigi Rizzo void 	   netmap_mem_deref(struct netmap_mem_d *);
215*f0ea3689SLuigi Rizzo int	   netmap_mem_get_info(struct netmap_mem_d *, u_int *size, u_int *memflags, uint16_t *id);
216f9790aebSLuigi Rizzo ssize_t    netmap_mem_if_offset(struct netmap_mem_d *, const void *vaddr);
217*f0ea3689SLuigi Rizzo struct netmap_mem_d* netmap_mem_private_new(const char *name,
218*f0ea3689SLuigi Rizzo 	u_int txr, u_int txd, u_int rxr, u_int rxd, u_int extra_bufs, u_int npipes,
219*f0ea3689SLuigi Rizzo 	int* error);
220f9790aebSLuigi Rizzo void	   netmap_mem_private_delete(struct netmap_mem_d *);
221ab1e2860SLuigi Rizzo 
222ab1e2860SLuigi Rizzo #define NETMAP_BDG_BUF_SIZE(n)	((n)->pools[NETMAP_BUF_POOL]._objsize)
223ab1e2860SLuigi Rizzo 
224*f0ea3689SLuigi Rizzo uint32_t netmap_extra_alloc(struct netmap_adapter *, uint32_t *, uint32_t n);
225ab1e2860SLuigi Rizzo 
226ab1e2860SLuigi Rizzo 
227ab1e2860SLuigi Rizzo #endif
228