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