xref: /dpdk/lib/mbuf/rte_mbuf_pool_ops.c (revision ae67895b507bb6af22263c79ba0d5c374b396485)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright 2018 NXP
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson 
599a2dd95SBruce Richardson #include <string.h>
699a2dd95SBruce Richardson #include <rte_eal.h>
799a2dd95SBruce Richardson #include <rte_mbuf.h>
899a2dd95SBruce Richardson #include <rte_errno.h>
999a2dd95SBruce Richardson #include <rte_mbuf_pool_ops.h>
1099a2dd95SBruce Richardson 
11e906c931SStephen Hemminger #include "mbuf_log.h"
12e906c931SStephen Hemminger 
1399a2dd95SBruce Richardson int
rte_mbuf_set_platform_mempool_ops(const char * ops_name)1499a2dd95SBruce Richardson rte_mbuf_set_platform_mempool_ops(const char *ops_name)
1599a2dd95SBruce Richardson {
1699a2dd95SBruce Richardson 	const struct rte_memzone *mz;
1799a2dd95SBruce Richardson 
1899a2dd95SBruce Richardson 	size_t len = strnlen(ops_name, RTE_MEMPOOL_OPS_NAMESIZE);
1999a2dd95SBruce Richardson 	if (len == 0)
2099a2dd95SBruce Richardson 		return -EINVAL;
2199a2dd95SBruce Richardson 	if (len == RTE_MEMPOOL_OPS_NAMESIZE)
2299a2dd95SBruce Richardson 		return -ENAMETOOLONG;
2399a2dd95SBruce Richardson 
2499a2dd95SBruce Richardson 	mz = rte_memzone_lookup("mbuf_platform_pool_ops");
2599a2dd95SBruce Richardson 	if (mz == NULL) {
2699a2dd95SBruce Richardson 		mz = rte_memzone_reserve("mbuf_platform_pool_ops",
2799a2dd95SBruce Richardson 			RTE_MEMPOOL_OPS_NAMESIZE, SOCKET_ID_ANY, 0);
2899a2dd95SBruce Richardson 		if (mz == NULL)
2999a2dd95SBruce Richardson 			return -rte_errno;
3099a2dd95SBruce Richardson 		strcpy(mz->addr, ops_name);
3199a2dd95SBruce Richardson 		return 0;
3299a2dd95SBruce Richardson 	} else if (strcmp(mz->addr, ops_name) == 0) {
3399a2dd95SBruce Richardson 		return 0;
3499a2dd95SBruce Richardson 	}
3599a2dd95SBruce Richardson 
36*ae67895bSDavid Marchand 	MBUF_LOG(ERR,
37*ae67895bSDavid Marchand 		"%s is already registered as platform mbuf pool ops",
3899a2dd95SBruce Richardson 		(char *)mz->addr);
3999a2dd95SBruce Richardson 	return -EEXIST;
4099a2dd95SBruce Richardson }
4199a2dd95SBruce Richardson 
4299a2dd95SBruce Richardson const char *
rte_mbuf_platform_mempool_ops(void)4399a2dd95SBruce Richardson rte_mbuf_platform_mempool_ops(void)
4499a2dd95SBruce Richardson {
4599a2dd95SBruce Richardson 	const struct rte_memzone *mz;
4699a2dd95SBruce Richardson 
4799a2dd95SBruce Richardson 	mz = rte_memzone_lookup("mbuf_platform_pool_ops");
4899a2dd95SBruce Richardson 	if (mz == NULL)
4999a2dd95SBruce Richardson 		return NULL;
5099a2dd95SBruce Richardson 	return mz->addr;
5199a2dd95SBruce Richardson }
5299a2dd95SBruce Richardson 
5399a2dd95SBruce Richardson int
rte_mbuf_set_user_mempool_ops(const char * ops_name)5499a2dd95SBruce Richardson rte_mbuf_set_user_mempool_ops(const char *ops_name)
5599a2dd95SBruce Richardson {
5699a2dd95SBruce Richardson 	const struct rte_memzone *mz;
5799a2dd95SBruce Richardson 
5899a2dd95SBruce Richardson 	size_t len = strnlen(ops_name, RTE_MEMPOOL_OPS_NAMESIZE);
5999a2dd95SBruce Richardson 	if (len == 0)
6099a2dd95SBruce Richardson 		return -EINVAL;
6199a2dd95SBruce Richardson 	if (len == RTE_MEMPOOL_OPS_NAMESIZE)
6299a2dd95SBruce Richardson 		return -ENAMETOOLONG;
6399a2dd95SBruce Richardson 
6499a2dd95SBruce Richardson 	mz = rte_memzone_lookup("mbuf_user_pool_ops");
6599a2dd95SBruce Richardson 	if (mz == NULL) {
6699a2dd95SBruce Richardson 		mz = rte_memzone_reserve("mbuf_user_pool_ops",
6799a2dd95SBruce Richardson 			RTE_MEMPOOL_OPS_NAMESIZE, SOCKET_ID_ANY, 0);
6899a2dd95SBruce Richardson 		if (mz == NULL)
6999a2dd95SBruce Richardson 			return -rte_errno;
7099a2dd95SBruce Richardson 	}
7199a2dd95SBruce Richardson 
7299a2dd95SBruce Richardson 	strcpy(mz->addr, ops_name);
7399a2dd95SBruce Richardson 	return 0;
7499a2dd95SBruce Richardson 
7599a2dd95SBruce Richardson }
7699a2dd95SBruce Richardson 
7799a2dd95SBruce Richardson const char *
rte_mbuf_user_mempool_ops(void)7899a2dd95SBruce Richardson rte_mbuf_user_mempool_ops(void)
7999a2dd95SBruce Richardson {
8099a2dd95SBruce Richardson 	const struct rte_memzone *mz;
8199a2dd95SBruce Richardson 
8299a2dd95SBruce Richardson 	mz = rte_memzone_lookup("mbuf_user_pool_ops");
8399a2dd95SBruce Richardson 	if (mz == NULL)
8499a2dd95SBruce Richardson 		return rte_eal_mbuf_user_pool_ops();
8599a2dd95SBruce Richardson 	return mz->addr;
8699a2dd95SBruce Richardson }
8799a2dd95SBruce Richardson 
8899a2dd95SBruce Richardson /* Return mbuf pool ops name */
8999a2dd95SBruce Richardson const char *
rte_mbuf_best_mempool_ops(void)9099a2dd95SBruce Richardson rte_mbuf_best_mempool_ops(void)
9199a2dd95SBruce Richardson {
9299a2dd95SBruce Richardson 	/* User defined mempool ops takes the priority */
9399a2dd95SBruce Richardson 	const char *best_ops = rte_mbuf_user_mempool_ops();
9499a2dd95SBruce Richardson 	if (best_ops)
9599a2dd95SBruce Richardson 		return best_ops;
9699a2dd95SBruce Richardson 
9799a2dd95SBruce Richardson 	/* Next choice is platform configured mempool ops */
9899a2dd95SBruce Richardson 	best_ops = rte_mbuf_platform_mempool_ops();
9999a2dd95SBruce Richardson 	if (best_ops)
10099a2dd95SBruce Richardson 		return best_ops;
10199a2dd95SBruce Richardson 
10299a2dd95SBruce Richardson 	/* Last choice is to use the compile time config pool */
10399a2dd95SBruce Richardson 	return RTE_MBUF_DEFAULT_MEMPOOL_OPS;
10499a2dd95SBruce Richardson }
105