aboutsummaryrefslogtreecommitdiffstats
path: root/dpdk/dpdk-v18.11_patches/0007-mbuf-add-dynamic-mbuf-mempool-support.patch
blob: 8618928f22429b2bbdc89b73af6baaadbcc949ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
From c2a2b8eec349156b31f2faab61cc6063ef3f0c61 Mon Sep 17 00:00:00 2001
From: Jianfeng Tan <henry.tjf@antfin.com>
Date: Wed, 26 Dec 2018 14:40:07 +0000
Subject: [PATCH 2/2] mbuf: add dynamic mbuf mempool support

Signed-off-by: Jianfeng Tan <henry.tjf@antfin.com>
---
 examples/Makefile                      |  1 +
 examples/dynamic_mbuf_pool/Makefile    | 56 ++++++++++++++++
 examples/dynamic_mbuf_pool/main.c      | 92 ++++++++++++++++++++++++++
 examples/dynamic_mbuf_pool/meson.build | 11 +++
 lib/librte_mbuf/rte_mbuf.c             | 51 ++++++++++++++
 lib/librte_mbuf/rte_mbuf.h             |  5 ++
 lib/librte_mbuf/rte_mbuf_version.map   |  8 ++-
 7 files changed, 223 insertions(+), 1 deletion(-)
 create mode 100644 examples/dynamic_mbuf_pool/Makefile
 create mode 100644 examples/dynamic_mbuf_pool/main.c
 create mode 100644 examples/dynamic_mbuf_pool/meson.build

diff --git a/examples/Makefile b/examples/Makefile
index 33fe0e586..3df9cb7ad 100644
--- a/examples/Makefile
+++ b/examples/Makefile
@@ -21,6 +21,7 @@ DIRS-$(CONFIG_RTE_LIBRTE_CRYPTODEV) += fips_validation
 DIRS-$(CONFIG_RTE_LIBRTE_FLOW_CLASSIFY) += flow_classify
 DIRS-y += flow_filtering
 DIRS-y += helloworld
+DIRS-y += dynamic_mbuf_pool
 DIRS-$(CONFIG_RTE_LIBRTE_PIPELINE) += ip_pipeline
 ifeq ($(CONFIG_RTE_LIBRTE_LPM),y)
 DIRS-$(CONFIG_RTE_IP_FRAG) += ip_reassembly
diff --git a/examples/dynamic_mbuf_pool/Makefile b/examples/dynamic_mbuf_pool/Makefile
new file mode 100644
index 000000000..f2761f661
--- /dev/null
+++ b/examples/dynamic_mbuf_pool/Makefile
@@ -0,0 +1,56 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2010-2014 Intel Corporation
+
+# binary name
+APP = dynamic_mbuf_pool
+
+# all source are stored in SRCS-y
+SRCS-y := main.c
+
+# Build using pkg-config variables if possible
+$(shell pkg-config --exists libdpdk)
+ifeq ($(.SHELLSTATUS),0)
+
+all: shared
+.PHONY: shared static
+shared: build/$(APP)-shared
+	ln -sf $(APP)-shared build/$(APP)
+static: build/$(APP)-static
+	ln -sf $(APP)-static build/$(APP)
+
+PC_FILE := $(shell pkg-config --path libdpdk)
+CFLAGS += -O3 $(shell pkg-config --cflags libdpdk)
+LDFLAGS_SHARED = $(shell pkg-config --libs libdpdk)
+LDFLAGS_STATIC = -Wl,-Bstatic $(shell pkg-config --static --libs libdpdk)
+
+build/$(APP)-shared: $(SRCS-y) Makefile $(PC_FILE) | build
+	$(CC) $(CFLAGS) $(SRCS-y) -o $@ $(LDFLAGS) $(LDFLAGS_SHARED)
+
+build/$(APP)-static: $(SRCS-y) Makefile $(PC_FILE) | build
+	$(CC) $(CFLAGS) $(SRCS-y) -o $@ $(LDFLAGS) $(LDFLAGS_STATIC)
+
+build:
+	@mkdir -p $@
+
+.PHONY: clean
+clean:
+	rm -f build/$(APP) build/$(APP)-static build/$(APP)-shared
+	rmdir --ignore-fail-on-non-empty build
+
+else
+
+ifeq ($(RTE_SDK),)
+$(error "Please define RTE_SDK environment variable")
+endif
+
+# Default target, can be overridden by command line or environment
+RTE_TARGET ?= x86_64-native-linuxapp-gcc
+
+include $(RTE_SDK)/mk/rte.vars.mk
+
+CFLAGS += -O3
+CFLAGS += $(WERROR_FLAGS)
+
+include $(RTE_SDK)/mk/rte.extapp.mk
+
+endif
diff --git a/examples/dynamic_mbuf_pool/main.c b/examples/dynamic_mbuf_pool/main.c
new file mode 100644
index 000000000..a568d7cec
--- /dev/null
+++ b/examples/dynamic_mbuf_pool/main.c
@@ -0,0 +1,92 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2010-2014 Intel Corporation
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdint.h>
+#include <errno.h>
+#include <sys/queue.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <rte_memory.h>
+#include <rte_launch.h>
+#include <rte_eal.h>
+#include <rte_per_lcore.h>
+#include <rte_lcore.h>
+#include <rte_debug.h>
+#include <rte_memory.h>
+#include <rte_mbuf.h>
+#include <rte_memzone.h>
+
+#define HUGE_2M "/sys/kernel/mm/hugepages/hugepages-2048kB/free_hugepages"
+#define HUGE_1G "/sys/kernel/mm/hugepages/hugepages-1048576kB/free_hugepages"
+
+static long int
+get_value(const char *path)
+{
+	int fd, len;
+	long int value;
+	char buf[1024];
+
+	fd = open(path, O_RDONLY);
+	if (fd < 0)
+		return ULONG_MAX;
+
+	len = read(fd, buf, sizeof(buf));
+
+	close(fd);
+
+	if (len <= 0) {
+		return ULONG_MAX;
+	}
+
+	value = strtol(buf, NULL, 10);
+	return value;
+}
+
+static void
+print_free_hugepages(void)
+{
+	printf("2M: %ld\t\t1G: %ld\n", get_value(HUGE_2M), get_value(HUGE_1G));
+}
+
+int
+main(int argc, char **argv)
+{
+	int i;
+	int ret;
+	int n = 512 * 1024;
+	int dynamic_size = 8 * 1024;
+	struct rte_mbuf *m;
+	struct rte_mempool *mp;
+
+	ret = rte_eal_init(argc, argv);
+	if (ret < 0)
+		rte_panic("Cannot init EAL\n");
+
+	mp = rte_pktmbuf_dynamic_pool_create("mbuf_pool", n,
+				64, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
+				0, dynamic_size);
+	if (mp == NULL)
+		rte_panic("Failed to create mbuf mempool");
+
+	for (i = 0; i < n; i++) {
+		m = rte_pktmbuf_alloc(mp);
+		if (m == NULL)
+			break;
+
+		if ((i % dynamic_size) == 1) {
+			print_free_hugepages();
+			usleep(100 * 1000);
+		}
+	}
+
+	printf("have allocated %d mbufs", i);
+	rte_memzone_dump(stdout);
+
+	return 0;
+}
diff --git a/examples/dynamic_mbuf_pool/meson.build b/examples/dynamic_mbuf_pool/meson.build
new file mode 100644
index 000000000..c34e11e36
--- /dev/null
+++ b/examples/dynamic_mbuf_pool/meson.build
@@ -0,0 +1,11 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2017 Intel Corporation
+
+# meson file, for building this example as part of a main DPDK build.
+#
+# To build this example as a standalone application with an already-installed
+# DPDK instance, use 'make'
+
+sources = files(
+	'main.c'
+)
diff --git a/lib/librte_mbuf/rte_mbuf.c b/lib/librte_mbuf/rte_mbuf.c
index 9790b4fb1..b70abd88c 100644
--- a/lib/librte_mbuf/rte_mbuf.c
+++ b/lib/librte_mbuf/rte_mbuf.c
@@ -167,6 +167,57 @@ rte_pktmbuf_pool_create(const char *name, unsigned int n,
 			data_room_size, socket_id, NULL);
 }
 
+struct rte_mempool *
+rte_pktmbuf_dynamic_pool_create(const char *name, unsigned int n,
+	unsigned int cache_size, uint16_t priv_size,
+	uint16_t data_room_size, int socket_id, int dynamic_size)
+{
+	struct rte_mempool *mp;
+	struct rte_pktmbuf_pool_private mbp_priv;
+	const char *mp_ops_name;
+	unsigned elt_size;
+	int ret;
+
+	if (RTE_ALIGN(priv_size, RTE_MBUF_PRIV_ALIGN) != priv_size) {
+		RTE_LOG(ERR, MBUF, "mbuf priv_size=%u is not aligned\n",
+			priv_size);
+		rte_errno = EINVAL;
+		return NULL;
+	}
+	elt_size = sizeof(struct rte_mbuf) + (unsigned)priv_size +
+		(unsigned)data_room_size;
+	mbp_priv.mbuf_data_room_size = data_room_size;
+	mbp_priv.mbuf_priv_size = priv_size;
+
+	mp = rte_mempool_create_empty(name, n, elt_size, cache_size,
+		 sizeof(struct rte_pktmbuf_pool_private),
+		 socket_id, MEMPOOL_F_DYNAMIC);
+	if (mp == NULL)
+		return NULL;
+
+	mp_ops_name = rte_mbuf_best_mempool_ops();
+	ret = rte_mempool_set_ops_byname(mp, mp_ops_name, NULL);
+	if (ret != 0) {
+		RTE_LOG(ERR, MBUF, "error setting mempool handler\n");
+		rte_mempool_free(mp);
+		rte_errno = -ret;
+		return NULL;
+	}
+	rte_pktmbuf_pool_init(mp, &mbp_priv);
+
+	rte_mempool_set_dynamic_size(mp, dynamic_size);
+	rte_mempool_set_dynamic_cb(mp, rte_pktmbuf_init);
+
+	ret = rte_mempool_populate_default(mp);
+	if (ret < 0) {
+		rte_mempool_free(mp);
+		rte_errno = -ret;
+		return NULL;
+	}
+
+	return mp;
+}
+
 /* do some sanity checks on a mbuf: panic if it fails */
 void
 rte_mbuf_sanity_check(const struct rte_mbuf *m, int is_header)
diff --git a/lib/librte_mbuf/rte_mbuf.h b/lib/librte_mbuf/rte_mbuf.h
index 3dbc6695e..5a2d81605 100644
--- a/lib/librte_mbuf/rte_mbuf.h
+++ b/lib/librte_mbuf/rte_mbuf.h
@@ -1183,6 +1183,11 @@ rte_pktmbuf_pool_create(const char *name, unsigned n,
 	unsigned cache_size, uint16_t priv_size, uint16_t data_room_size,
 	int socket_id);
 
+struct rte_mempool *
+rte_pktmbuf_dynamic_pool_create(const char *name, unsigned int n,
+	unsigned int cache_size, uint16_t priv_size,
+	uint16_t data_room_size, int socket_id, int dynamic_size);
+
 /**
  * Create a mbuf pool with a given mempool ops name
  *
diff --git a/lib/librte_mbuf/rte_mbuf_version.map b/lib/librte_mbuf/rte_mbuf_version.map
index cae68db8d..d6d25af95 100644
--- a/lib/librte_mbuf/rte_mbuf_version.map
+++ b/lib/librte_mbuf/rte_mbuf_version.map
@@ -44,4 +44,10 @@ DPDK_18.08 {
 	rte_mbuf_set_user_mempool_ops;
 	rte_mbuf_user_mempool_ops;
 	rte_pktmbuf_pool_create_by_ops;
-} DPDK_16.11;
+} DPDK_18.11;
+
+DPDK_18.11 {
+	global:
+
+	rte_pktmbuf_dynamic_pool_create;
+} DPDK_18.12;
-- 
2.17.1