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