mbuf: add pool ops selection functions
[dpdk.git] / lib / librte_mbuf / rte_mbuf_pool_ops.c
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 int
12 rte_mbuf_set_platform_mempool_ops(const char *ops_name)
13 {
14         const struct rte_memzone *mz;
15
16         if (strlen(ops_name) >= RTE_MEMPOOL_OPS_NAMESIZE)
17                 return -ENAMETOOLONG;
18
19         mz = rte_memzone_lookup("mbuf_platform_pool_ops");
20         if (mz == NULL) {
21                 mz = rte_memzone_reserve("mbuf_platform_pool_ops",
22                         RTE_MEMPOOL_OPS_NAMESIZE, SOCKET_ID_ANY, 0);
23                 if (mz == NULL)
24                         return -rte_errno;
25                 strncpy(mz->addr, ops_name, strlen(ops_name));
26                 return 0;
27         } else if (strcmp(mz->addr, ops_name) == 0) {
28                 return 0;
29         }
30
31         RTE_LOG(ERR, MBUF,
32                 "%s is already registered as platform mbuf pool ops\n",
33                 (char *)mz->addr);
34         return -EEXIST;
35 }
36
37 const char *
38 rte_mbuf_platform_mempool_ops(void)
39 {
40         const struct rte_memzone *mz;
41
42         mz = rte_memzone_lookup("mbuf_platform_pool_ops");
43         if (mz == NULL)
44                 return NULL;
45         return mz->addr;
46 }
47
48 int
49 rte_mbuf_set_user_mempool_ops(const char *ops_name)
50 {
51         const struct rte_memzone *mz;
52
53         if (strlen(ops_name) >= RTE_MEMPOOL_OPS_NAMESIZE)
54                 return -ENAMETOOLONG;
55
56         mz = rte_memzone_lookup("mbuf_user_pool_ops");
57         if (mz == NULL) {
58                 mz = rte_memzone_reserve("mbuf_user_pool_ops",
59                         RTE_MEMPOOL_OPS_NAMESIZE, SOCKET_ID_ANY, 0);
60                 if (mz == NULL)
61                         return -rte_errno;
62         }
63
64         strncpy(mz->addr, ops_name, strlen(ops_name));
65         return 0;
66
67 }
68
69 const char *
70 rte_mbuf_user_mempool_ops(void)
71 {
72         const struct rte_memzone *mz;
73
74         mz = rte_memzone_lookup("mbuf_user_pool_ops");
75         if (mz == NULL)
76                 return rte_eal_mbuf_default_mempool_ops();
77         return mz->addr;
78 }
79
80 /* Return mbuf pool ops name */
81 const char *
82 rte_mbuf_best_mempool_ops(void)
83 {
84         /* User defined mempool ops takes the priority */
85         const char *best_ops = rte_mbuf_user_mempool_ops();
86         if (best_ops)
87                 return best_ops;
88
89         /* Next choice is platform configured mempool ops */
90         best_ops = rte_mbuf_platform_mempool_ops();
91         if (best_ops)
92                 return best_ops;
93
94         /* Last choice is to use the compile time config pool */
95         return RTE_MBUF_DEFAULT_MEMPOOL_OPS;
96 }