1262152Sluigi/*
2262152Sluigi * Copyright (C) 2012-2014 Matteo Landi, Luigi Rizzo, Giuseppe Lettieri. All rights reserved.
3262152Sluigi *
4262152Sluigi * Redistribution and use in source and binary forms, with or without
5262152Sluigi * modification, are permitted provided that the following conditions
6262152Sluigi * are met:
7262152Sluigi *   1. Redistributions of source code must retain the above copyright
8262152Sluigi *      notice, this list of conditions and the following disclaimer.
9262152Sluigi *   2. Redistributions in binary form must reproduce the above copyright
10262152Sluigi *      notice, this list of conditions and the following disclaimer in the
11262152Sluigi *    documentation and/or other materials provided with the distribution.
12262152Sluigi *
13262152Sluigi * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14262152Sluigi * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15262152Sluigi * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16262152Sluigi * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17262152Sluigi * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18262152Sluigi * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19262152Sluigi * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20262152Sluigi * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21262152Sluigi * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22262152Sluigi * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23262152Sluigi * SUCH DAMAGE.
24262152Sluigi */
25262152Sluigi
26262152Sluigi/*
27262152Sluigi * $FreeBSD: releng/10.2/sys/dev/netmap/netmap_mem2.h 270252 2014-08-20 23:34:36Z luigi $
28262152Sluigi *
29262152Sluigi * (New) memory allocator for netmap
30262152Sluigi */
31262152Sluigi
32262152Sluigi/*
33262152Sluigi * This allocator creates three memory pools:
34262152Sluigi *	nm_if_pool	for the struct netmap_if
35262152Sluigi *	nm_ring_pool	for the struct netmap_ring
36262152Sluigi *	nm_buf_pool	for the packet buffers.
37262152Sluigi *
38262152Sluigi * that contain netmap objects. Each pool is made of a number of clusters,
39262152Sluigi * multiple of a page size, each containing an integer number of objects.
40262152Sluigi * The clusters are contiguous in user space but not in the kernel.
41262152Sluigi * Only nm_buf_pool needs to be dma-able,
42262152Sluigi * but for convenience use the same type of allocator for all.
43262152Sluigi *
44262152Sluigi * Once mapped, the three pools are exported to userspace
45262152Sluigi * as a contiguous block, starting from nm_if_pool. Each
46262152Sluigi * cluster (and pool) is an integral number of pages.
47262152Sluigi *   [ . . . ][ . . . . . .][ . . . . . . . . . .]
48262152Sluigi *    nm_if     nm_ring            nm_buf
49262152Sluigi *
50262152Sluigi * The userspace areas contain offsets of the objects in userspace.
51262152Sluigi * When (at init time) we write these offsets, we find out the index
52262152Sluigi * of the object, and from there locate the offset from the beginning
53262152Sluigi * of the region.
54262152Sluigi *
55262152Sluigi * The invididual allocators manage a pool of memory for objects of
56262152Sluigi * the same size.
57262152Sluigi * The pool is split into smaller clusters, whose size is a
58262152Sluigi * multiple of the page size. The cluster size is chosen
59262152Sluigi * to minimize the waste for a given max cluster size
60262152Sluigi * (we do it by brute force, as we have relatively few objects
61262152Sluigi * per cluster).
62262152Sluigi *
63262152Sluigi * Objects are aligned to the cache line (64 bytes) rounding up object
64262152Sluigi * sizes when needed. A bitmap contains the state of each object.
65262152Sluigi * Allocation scans the bitmap; this is done only on attach, so we are not
66262152Sluigi * too worried about performance
67262152Sluigi *
68262152Sluigi * For each allocator we can define (thorugh sysctl) the size and
69262152Sluigi * number of each object. Memory is allocated at the first use of a
70262152Sluigi * netmap file descriptor, and can be freed when all such descriptors
71262152Sluigi * have been released (including unmapping the memory).
72262152Sluigi * If memory is scarce, the system tries to get as much as possible
73262152Sluigi * and the sysctl values reflect the actual allocation.
74262152Sluigi * Together with desired values, the sysctl export also absolute
75262152Sluigi * min and maximum values that cannot be overridden.
76262152Sluigi *
77262152Sluigi * struct netmap_if:
78262152Sluigi *	variable size, max 16 bytes per ring pair plus some fixed amount.
79262152Sluigi *	1024 bytes should be large enough in practice.
80262152Sluigi *
81262152Sluigi *	In the worst case we have one netmap_if per ring in the system.
82262152Sluigi *
83262152Sluigi * struct netmap_ring
84262152Sluigi *	variable size, 8 byte per slot plus some fixed amount.
85262152Sluigi *	Rings can be large (e.g. 4k slots, or >32Kbytes).
86262152Sluigi *	We default to 36 KB (9 pages), and a few hundred rings.
87262152Sluigi *
88262152Sluigi * struct netmap_buffer
89262152Sluigi *	The more the better, both because fast interfaces tend to have
90262152Sluigi *	many slots, and because we may want to use buffers to store
91262152Sluigi *	packets in userspace avoiding copies.
92262152Sluigi *	Must contain a full frame (eg 1518, or more for vlans, jumbo
93262152Sluigi *	frames etc.) plus be nicely aligned, plus some NICs restrict
94262152Sluigi *	the size to multiple of 1K or so. Default to 2K
95262152Sluigi */
96262152Sluigi#ifndef _NET_NETMAP_MEM2_H_
97262152Sluigi#define _NET_NETMAP_MEM2_H_
98262152Sluigi
99262152Sluigi
100262152Sluigi
101262152Sluigi/* We implement two kinds of netmap_mem_d structures:
102262152Sluigi *
103262152Sluigi * - global: used by hardware NICS;
104262152Sluigi *
105262152Sluigi * - private: used by VALE ports.
106262152Sluigi *
107262152Sluigi * In both cases, the netmap_mem_d structure has the same lifetime as the
108262152Sluigi * netmap_adapter of the corresponding NIC or port. It is the responsibility of
109262152Sluigi * the client code to delete the private allocator when the associated
110262152Sluigi * netmap_adapter is freed (this is implemented by the NAF_MEM_OWNER flag in
111262152Sluigi * netmap.c).  The 'refcount' field counts the number of active users of the
112262152Sluigi * structure. The global allocator uses this information to prevent/allow
113262152Sluigi * reconfiguration. The private allocators release all their memory when there
114262152Sluigi * are no active users.  By 'active user' we mean an existing netmap_priv
115262152Sluigi * structure holding a reference to the allocator.
116262152Sluigi */
117262152Sluigi
118262152Sluigiextern struct netmap_mem_d nm_mem;
119262152Sluigi
120270252Sluigistruct lut_entry* netmap_mem_get_lut(struct netmap_mem_d *);
121270252Sluigiu_int      netmap_mem_get_buftotal(struct netmap_mem_d *);
122270252Sluigisize_t     netmap_mem_get_bufsize(struct netmap_mem_d *);
123262152Sluigivm_paddr_t netmap_mem_ofstophys(struct netmap_mem_d *, vm_ooffset_t);
124270252Sluigiint	   netmap_mem_finalize(struct netmap_mem_d *, struct netmap_adapter *);
125262152Sluigiint 	   netmap_mem_init(void);
126262152Sluigivoid 	   netmap_mem_fini(void);
127270252Sluigistruct netmap_if * netmap_mem_if_new(struct netmap_adapter *);
128262152Sluigivoid 	   netmap_mem_if_delete(struct netmap_adapter *, struct netmap_if *);
129262152Sluigiint	   netmap_mem_rings_create(struct netmap_adapter *);
130262152Sluigivoid	   netmap_mem_rings_delete(struct netmap_adapter *);
131270252Sluigivoid 	   netmap_mem_deref(struct netmap_mem_d *, struct netmap_adapter *);
132262152Sluigiint	   netmap_mem_get_info(struct netmap_mem_d *, u_int *size, u_int *memflags, uint16_t *id);
133262152Sluigissize_t    netmap_mem_if_offset(struct netmap_mem_d *, const void *vaddr);
134262152Sluigistruct netmap_mem_d* netmap_mem_private_new(const char *name,
135262152Sluigi	u_int txr, u_int txd, u_int rxr, u_int rxd, u_int extra_bufs, u_int npipes,
136262152Sluigi	int* error);
137262152Sluigivoid	   netmap_mem_private_delete(struct netmap_mem_d *);
138262152Sluigi
139270252Sluigi#define NETMAP_MEM_PRIVATE	0x2	/* allocator uses private address space */
140270252Sluigi#define NETMAP_MEM_IO		0x4	/* the underlying memory is mmapped I/O */
141262152Sluigi
142262152Sluigiuint32_t netmap_extra_alloc(struct netmap_adapter *, uint32_t *, uint32_t n);
143262152Sluigi
144262152Sluigi
145262152Sluigi#endif
146