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