xref: /dpdk/lib/mbuf/rte_mbuf_pool_ops.c (revision daa02b5cddbb8e11b31d41e2bf7bb1ae64dcae2f)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2018 NXP
3  */
4 
5 #include <string.h>
6 #include <rte_compat.h>
7 #include <rte_eal.h>
8 #include <rte_mbuf.h>
9 #include <rte_errno.h>
10 #include <rte_mbuf_pool_ops.h>
11 
12 int
13 rte_mbuf_set_platform_mempool_ops(const char *ops_name)
14 {
15 	const struct rte_memzone *mz;
16 
17 	size_t len = strnlen(ops_name, RTE_MEMPOOL_OPS_NAMESIZE);
18 	if (len == 0)
19 		return -EINVAL;
20 	if (len == RTE_MEMPOOL_OPS_NAMESIZE)
21 		return -ENAMETOOLONG;
22 
23 	mz = rte_memzone_lookup("mbuf_platform_pool_ops");
24 	if (mz == NULL) {
25 		mz = rte_memzone_reserve("mbuf_platform_pool_ops",
26 			RTE_MEMPOOL_OPS_NAMESIZE, SOCKET_ID_ANY, 0);
27 		if (mz == NULL)
28 			return -rte_errno;
29 		strcpy(mz->addr, ops_name);
30 		return 0;
31 	} else if (strcmp(mz->addr, ops_name) == 0) {
32 		return 0;
33 	}
34 
35 	RTE_LOG(ERR, MBUF,
36 		"%s is already registered as platform mbuf pool ops\n",
37 		(char *)mz->addr);
38 	return -EEXIST;
39 }
40 
41 const char *
42 rte_mbuf_platform_mempool_ops(void)
43 {
44 	const struct rte_memzone *mz;
45 
46 	mz = rte_memzone_lookup("mbuf_platform_pool_ops");
47 	if (mz == NULL)
48 		return NULL;
49 	return mz->addr;
50 }
51 
52 int
53 rte_mbuf_set_user_mempool_ops(const char *ops_name)
54 {
55 	const struct rte_memzone *mz;
56 
57 	size_t len = strnlen(ops_name, RTE_MEMPOOL_OPS_NAMESIZE);
58 	if (len == 0)
59 		return -EINVAL;
60 	if (len == RTE_MEMPOOL_OPS_NAMESIZE)
61 		return -ENAMETOOLONG;
62 
63 	mz = rte_memzone_lookup("mbuf_user_pool_ops");
64 	if (mz == NULL) {
65 		mz = rte_memzone_reserve("mbuf_user_pool_ops",
66 			RTE_MEMPOOL_OPS_NAMESIZE, SOCKET_ID_ANY, 0);
67 		if (mz == NULL)
68 			return -rte_errno;
69 	}
70 
71 	strcpy(mz->addr, ops_name);
72 	return 0;
73 
74 }
75 
76 const char *
77 rte_mbuf_user_mempool_ops(void)
78 {
79 	const struct rte_memzone *mz;
80 
81 	mz = rte_memzone_lookup("mbuf_user_pool_ops");
82 	if (mz == NULL)
83 		return rte_eal_mbuf_user_pool_ops();
84 	return mz->addr;
85 }
86 
87 /* Return mbuf pool ops name */
88 const char *
89 rte_mbuf_best_mempool_ops(void)
90 {
91 	/* User defined mempool ops takes the priority */
92 	const char *best_ops = rte_mbuf_user_mempool_ops();
93 	if (best_ops)
94 		return best_ops;
95 
96 	/* Next choice is platform configured mempool ops */
97 	best_ops = rte_mbuf_platform_mempool_ops();
98 	if (best_ops)
99 		return best_ops;
100 
101 	/* Last choice is to use the compile time config pool */
102 	return RTE_MBUF_DEFAULT_MEMPOOL_OPS;
103 }
104