xref: /dpdk/lib/table/rte_table_array.c (revision 30a1de10)
1*99a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
2*99a2dd95SBruce Richardson  * Copyright(c) 2010-2014 Intel Corporation
3*99a2dd95SBruce Richardson  */
4*99a2dd95SBruce Richardson 
5*99a2dd95SBruce Richardson #include <string.h>
6*99a2dd95SBruce Richardson #include <stdio.h>
7*99a2dd95SBruce Richardson 
8*99a2dd95SBruce Richardson #include <rte_common.h>
9*99a2dd95SBruce Richardson #include <rte_malloc.h>
10*99a2dd95SBruce Richardson #include <rte_log.h>
11*99a2dd95SBruce Richardson 
12*99a2dd95SBruce Richardson #include "rte_table_array.h"
13*99a2dd95SBruce Richardson 
14*99a2dd95SBruce Richardson #ifdef RTE_TABLE_STATS_COLLECT
15*99a2dd95SBruce Richardson 
16*99a2dd95SBruce Richardson #define RTE_TABLE_ARRAY_STATS_PKTS_IN_ADD(table, val) \
17*99a2dd95SBruce Richardson 	table->stats.n_pkts_in += val
18*99a2dd95SBruce Richardson #define RTE_TABLE_ARRAY_STATS_PKTS_LOOKUP_MISS(table, val) \
19*99a2dd95SBruce Richardson 	table->stats.n_pkts_lookup_miss += val
20*99a2dd95SBruce Richardson 
21*99a2dd95SBruce Richardson #else
22*99a2dd95SBruce Richardson 
23*99a2dd95SBruce Richardson #define RTE_TABLE_ARRAY_STATS_PKTS_IN_ADD(table, val)
24*99a2dd95SBruce Richardson #define RTE_TABLE_ARRAY_STATS_PKTS_LOOKUP_MISS(table, val)
25*99a2dd95SBruce Richardson 
26*99a2dd95SBruce Richardson #endif
27*99a2dd95SBruce Richardson 
28*99a2dd95SBruce Richardson struct rte_table_array {
29*99a2dd95SBruce Richardson 	struct rte_table_stats stats;
30*99a2dd95SBruce Richardson 
31*99a2dd95SBruce Richardson 	/* Input parameters */
32*99a2dd95SBruce Richardson 	uint32_t entry_size;
33*99a2dd95SBruce Richardson 	uint32_t n_entries;
34*99a2dd95SBruce Richardson 	uint32_t offset;
35*99a2dd95SBruce Richardson 
36*99a2dd95SBruce Richardson 	/* Internal fields */
37*99a2dd95SBruce Richardson 	uint32_t entry_pos_mask;
38*99a2dd95SBruce Richardson 
39*99a2dd95SBruce Richardson 	/* Internal table */
40*99a2dd95SBruce Richardson 	uint8_t array[0] __rte_cache_aligned;
41*99a2dd95SBruce Richardson } __rte_cache_aligned;
42*99a2dd95SBruce Richardson 
43*99a2dd95SBruce Richardson static void *
rte_table_array_create(void * params,int socket_id,uint32_t entry_size)44*99a2dd95SBruce Richardson rte_table_array_create(void *params, int socket_id, uint32_t entry_size)
45*99a2dd95SBruce Richardson {
46*99a2dd95SBruce Richardson 	struct rte_table_array_params *p = params;
47*99a2dd95SBruce Richardson 	struct rte_table_array *t;
48*99a2dd95SBruce Richardson 	uint32_t total_cl_size, total_size;
49*99a2dd95SBruce Richardson 
50*99a2dd95SBruce Richardson 	/* Check input parameters */
51*99a2dd95SBruce Richardson 	if ((p == NULL) ||
52*99a2dd95SBruce Richardson 	    (p->n_entries == 0) ||
53*99a2dd95SBruce Richardson 		(!rte_is_power_of_2(p->n_entries)))
54*99a2dd95SBruce Richardson 		return NULL;
55*99a2dd95SBruce Richardson 
56*99a2dd95SBruce Richardson 	/* Memory allocation */
57*99a2dd95SBruce Richardson 	total_cl_size = (sizeof(struct rte_table_array) +
58*99a2dd95SBruce Richardson 			RTE_CACHE_LINE_SIZE) / RTE_CACHE_LINE_SIZE;
59*99a2dd95SBruce Richardson 	total_cl_size += (p->n_entries * entry_size +
60*99a2dd95SBruce Richardson 			RTE_CACHE_LINE_SIZE) / RTE_CACHE_LINE_SIZE;
61*99a2dd95SBruce Richardson 	total_size = total_cl_size * RTE_CACHE_LINE_SIZE;
62*99a2dd95SBruce Richardson 	t = rte_zmalloc_socket("TABLE", total_size, RTE_CACHE_LINE_SIZE, socket_id);
63*99a2dd95SBruce Richardson 	if (t == NULL) {
64*99a2dd95SBruce Richardson 		RTE_LOG(ERR, TABLE,
65*99a2dd95SBruce Richardson 			"%s: Cannot allocate %u bytes for array table\n",
66*99a2dd95SBruce Richardson 			__func__, total_size);
67*99a2dd95SBruce Richardson 		return NULL;
68*99a2dd95SBruce Richardson 	}
69*99a2dd95SBruce Richardson 
70*99a2dd95SBruce Richardson 	/* Memory initialization */
71*99a2dd95SBruce Richardson 	t->entry_size = entry_size;
72*99a2dd95SBruce Richardson 	t->n_entries = p->n_entries;
73*99a2dd95SBruce Richardson 	t->offset = p->offset;
74*99a2dd95SBruce Richardson 	t->entry_pos_mask = t->n_entries - 1;
75*99a2dd95SBruce Richardson 
76*99a2dd95SBruce Richardson 	return t;
77*99a2dd95SBruce Richardson }
78*99a2dd95SBruce Richardson 
79*99a2dd95SBruce Richardson static int
rte_table_array_free(void * table)80*99a2dd95SBruce Richardson rte_table_array_free(void *table)
81*99a2dd95SBruce Richardson {
82*99a2dd95SBruce Richardson 	struct rte_table_array *t = table;
83*99a2dd95SBruce Richardson 
84*99a2dd95SBruce Richardson 	/* Check input parameters */
85*99a2dd95SBruce Richardson 	if (t == NULL) {
86*99a2dd95SBruce Richardson 		RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
87*99a2dd95SBruce Richardson 		return -EINVAL;
88*99a2dd95SBruce Richardson 	}
89*99a2dd95SBruce Richardson 
90*99a2dd95SBruce Richardson 	/* Free previously allocated resources */
91*99a2dd95SBruce Richardson 	rte_free(t);
92*99a2dd95SBruce Richardson 
93*99a2dd95SBruce Richardson 	return 0;
94*99a2dd95SBruce Richardson }
95*99a2dd95SBruce Richardson 
96*99a2dd95SBruce Richardson static int
rte_table_array_entry_add(void * table,void * key,void * entry,int * key_found,void ** entry_ptr)97*99a2dd95SBruce Richardson rte_table_array_entry_add(
98*99a2dd95SBruce Richardson 	void *table,
99*99a2dd95SBruce Richardson 	void *key,
100*99a2dd95SBruce Richardson 	void *entry,
101*99a2dd95SBruce Richardson 	int *key_found,
102*99a2dd95SBruce Richardson 	void **entry_ptr)
103*99a2dd95SBruce Richardson {
104*99a2dd95SBruce Richardson 	struct rte_table_array *t = table;
105*99a2dd95SBruce Richardson 	struct rte_table_array_key *k = key;
106*99a2dd95SBruce Richardson 	uint8_t *table_entry;
107*99a2dd95SBruce Richardson 
108*99a2dd95SBruce Richardson 	/* Check input parameters */
109*99a2dd95SBruce Richardson 	if (table == NULL) {
110*99a2dd95SBruce Richardson 		RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
111*99a2dd95SBruce Richardson 		return -EINVAL;
112*99a2dd95SBruce Richardson 	}
113*99a2dd95SBruce Richardson 	if (key == NULL) {
114*99a2dd95SBruce Richardson 		RTE_LOG(ERR, TABLE, "%s: key parameter is NULL\n", __func__);
115*99a2dd95SBruce Richardson 		return -EINVAL;
116*99a2dd95SBruce Richardson 	}
117*99a2dd95SBruce Richardson 	if (entry == NULL) {
118*99a2dd95SBruce Richardson 		RTE_LOG(ERR, TABLE, "%s: entry parameter is NULL\n", __func__);
119*99a2dd95SBruce Richardson 		return -EINVAL;
120*99a2dd95SBruce Richardson 	}
121*99a2dd95SBruce Richardson 	if (key_found == NULL) {
122*99a2dd95SBruce Richardson 		RTE_LOG(ERR, TABLE, "%s: key_found parameter is NULL\n",
123*99a2dd95SBruce Richardson 			__func__);
124*99a2dd95SBruce Richardson 		return -EINVAL;
125*99a2dd95SBruce Richardson 	}
126*99a2dd95SBruce Richardson 	if (entry_ptr == NULL) {
127*99a2dd95SBruce Richardson 		RTE_LOG(ERR, TABLE, "%s: entry_ptr parameter is NULL\n",
128*99a2dd95SBruce Richardson 			__func__);
129*99a2dd95SBruce Richardson 		return -EINVAL;
130*99a2dd95SBruce Richardson 	}
131*99a2dd95SBruce Richardson 
132*99a2dd95SBruce Richardson 	table_entry = &t->array[k->pos * t->entry_size];
133*99a2dd95SBruce Richardson 	memcpy(table_entry, entry, t->entry_size);
134*99a2dd95SBruce Richardson 	*key_found = 1;
135*99a2dd95SBruce Richardson 	*entry_ptr = (void *) table_entry;
136*99a2dd95SBruce Richardson 
137*99a2dd95SBruce Richardson 	return 0;
138*99a2dd95SBruce Richardson }
139*99a2dd95SBruce Richardson 
140*99a2dd95SBruce Richardson static int
rte_table_array_lookup(void * table,struct rte_mbuf ** pkts,uint64_t pkts_mask,uint64_t * lookup_hit_mask,void ** entries)141*99a2dd95SBruce Richardson rte_table_array_lookup(
142*99a2dd95SBruce Richardson 	void *table,
143*99a2dd95SBruce Richardson 	struct rte_mbuf **pkts,
144*99a2dd95SBruce Richardson 	uint64_t pkts_mask,
145*99a2dd95SBruce Richardson 	uint64_t *lookup_hit_mask,
146*99a2dd95SBruce Richardson 	void **entries)
147*99a2dd95SBruce Richardson {
148*99a2dd95SBruce Richardson 	struct rte_table_array *t = (struct rte_table_array *) table;
149*99a2dd95SBruce Richardson 	__rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
150*99a2dd95SBruce Richardson 	RTE_TABLE_ARRAY_STATS_PKTS_IN_ADD(t, n_pkts_in);
151*99a2dd95SBruce Richardson 	*lookup_hit_mask = pkts_mask;
152*99a2dd95SBruce Richardson 
153*99a2dd95SBruce Richardson 	if ((pkts_mask & (pkts_mask + 1)) == 0) {
154*99a2dd95SBruce Richardson 		uint64_t n_pkts = __builtin_popcountll(pkts_mask);
155*99a2dd95SBruce Richardson 		uint32_t i;
156*99a2dd95SBruce Richardson 
157*99a2dd95SBruce Richardson 		for (i = 0; i < n_pkts; i++) {
158*99a2dd95SBruce Richardson 			struct rte_mbuf *pkt = pkts[i];
159*99a2dd95SBruce Richardson 			uint32_t entry_pos = RTE_MBUF_METADATA_UINT32(pkt,
160*99a2dd95SBruce Richardson 				t->offset) & t->entry_pos_mask;
161*99a2dd95SBruce Richardson 
162*99a2dd95SBruce Richardson 			entries[i] = (void *) &t->array[entry_pos *
163*99a2dd95SBruce Richardson 				t->entry_size];
164*99a2dd95SBruce Richardson 		}
165*99a2dd95SBruce Richardson 	} else {
166*99a2dd95SBruce Richardson 		for ( ; pkts_mask; ) {
167*99a2dd95SBruce Richardson 			uint32_t pkt_index = __builtin_ctzll(pkts_mask);
168*99a2dd95SBruce Richardson 			uint64_t pkt_mask = 1LLU << pkt_index;
169*99a2dd95SBruce Richardson 			struct rte_mbuf *pkt = pkts[pkt_index];
170*99a2dd95SBruce Richardson 			uint32_t entry_pos = RTE_MBUF_METADATA_UINT32(pkt,
171*99a2dd95SBruce Richardson 				t->offset) & t->entry_pos_mask;
172*99a2dd95SBruce Richardson 
173*99a2dd95SBruce Richardson 			entries[pkt_index] = (void *) &t->array[entry_pos *
174*99a2dd95SBruce Richardson 				t->entry_size];
175*99a2dd95SBruce Richardson 			pkts_mask &= ~pkt_mask;
176*99a2dd95SBruce Richardson 		}
177*99a2dd95SBruce Richardson 	}
178*99a2dd95SBruce Richardson 
179*99a2dd95SBruce Richardson 	return 0;
180*99a2dd95SBruce Richardson }
181*99a2dd95SBruce Richardson 
182*99a2dd95SBruce Richardson static int
rte_table_array_stats_read(void * table,struct rte_table_stats * stats,int clear)183*99a2dd95SBruce Richardson rte_table_array_stats_read(void *table, struct rte_table_stats *stats, int clear)
184*99a2dd95SBruce Richardson {
185*99a2dd95SBruce Richardson 	struct rte_table_array *array = table;
186*99a2dd95SBruce Richardson 
187*99a2dd95SBruce Richardson 	if (stats != NULL)
188*99a2dd95SBruce Richardson 		memcpy(stats, &array->stats, sizeof(array->stats));
189*99a2dd95SBruce Richardson 
190*99a2dd95SBruce Richardson 	if (clear)
191*99a2dd95SBruce Richardson 		memset(&array->stats, 0, sizeof(array->stats));
192*99a2dd95SBruce Richardson 
193*99a2dd95SBruce Richardson 	return 0;
194*99a2dd95SBruce Richardson }
195*99a2dd95SBruce Richardson 
196*99a2dd95SBruce Richardson struct rte_table_ops rte_table_array_ops = {
197*99a2dd95SBruce Richardson 	.f_create = rte_table_array_create,
198*99a2dd95SBruce Richardson 	.f_free = rte_table_array_free,
199*99a2dd95SBruce Richardson 	.f_add = rte_table_array_entry_add,
200*99a2dd95SBruce Richardson 	.f_delete = NULL,
201*99a2dd95SBruce Richardson 	.f_add_bulk = NULL,
202*99a2dd95SBruce Richardson 	.f_delete_bulk = NULL,
203*99a2dd95SBruce Richardson 	.f_lookup = rte_table_array_lookup,
204*99a2dd95SBruce Richardson 	.f_stats = rte_table_array_stats_read,
205*99a2dd95SBruce Richardson };
206