xref: /dpdk/lib/table/rte_table_stub.c (revision 9ad3a41ab2a10db0059e1decdbf3ec038f348e08)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 
5 #include <string.h>
6 
7 #include <rte_malloc.h>
8 
9 #include "rte_table_stub.h"
10 
11 #ifdef RTE_TABLE_STATS_COLLECT
12 
13 #define RTE_TABLE_LPM_STATS_PKTS_IN_ADD(table, val) \
14 	table->stats.n_pkts_in += val
15 #define RTE_TABLE_LPM_STATS_PKTS_LOOKUP_MISS(table, val) \
16 	table->stats.n_pkts_lookup_miss += val
17 
18 #else
19 
20 #define RTE_TABLE_LPM_STATS_PKTS_IN_ADD(table, val)
21 #define RTE_TABLE_LPM_STATS_PKTS_LOOKUP_MISS(table, val)
22 
23 #endif
24 
25 struct rte_table_stub {
26 	struct rte_table_stats stats;
27 };
28 
29 static void *
30 rte_table_stub_create(__rte_unused void *params,
31 		__rte_unused int socket_id,
32 		__rte_unused uint32_t entry_size)
33 {
34 	struct rte_table_stub *stub;
35 	uint32_t size;
36 
37 	size = sizeof(struct rte_table_stub);
38 	stub = rte_zmalloc_socket("TABLE", size, RTE_CACHE_LINE_SIZE,
39 		socket_id);
40 	if (stub == NULL) {
41 		RTE_LOG(ERR, TABLE,
42 			"%s: Cannot allocate %u bytes for stub table\n",
43 			__func__, size);
44 		return NULL;
45 	}
46 
47 	return stub;
48 }
49 
50 static int
51 rte_table_stub_lookup(
52 	__rte_unused void *table,
53 	__rte_unused struct rte_mbuf **pkts,
54 	__rte_unused uint64_t pkts_mask,
55 	uint64_t *lookup_hit_mask,
56 	__rte_unused void **entries)
57 {
58 	__rte_unused struct rte_table_stub *stub = (struct rte_table_stub *) table;
59 	__rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
60 
61 	RTE_TABLE_LPM_STATS_PKTS_IN_ADD(stub, n_pkts_in);
62 	*lookup_hit_mask = 0;
63 	RTE_TABLE_LPM_STATS_PKTS_LOOKUP_MISS(stub, n_pkts_in);
64 
65 	return 0;
66 }
67 
68 static int
69 rte_table_stub_stats_read(void *table, struct rte_table_stats *stats, int clear)
70 {
71 	struct rte_table_stub *t = table;
72 
73 	if (stats != NULL)
74 		memcpy(stats, &t->stats, sizeof(t->stats));
75 
76 	if (clear)
77 		memset(&t->stats, 0, sizeof(t->stats));
78 
79 	return 0;
80 }
81 
82 struct rte_table_ops rte_table_stub_ops = {
83 	.f_create = rte_table_stub_create,
84 	.f_free = NULL,
85 	.f_add = NULL,
86 	.f_delete = NULL,
87 	.f_add_bulk = NULL,
88 	.f_delete_bulk = NULL,
89 	.f_lookup = rte_table_stub_lookup,
90 	.f_stats = rte_table_stub_stats_read,
91 };
92