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