1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
8 #include <rte_common.h>
10 #include <rte_memory.h>
11 #include <rte_malloc.h>
12 #include <rte_byteorder.h>
16 #include "rte_table_lpm.h"
18 #ifndef RTE_TABLE_LPM_MAX_NEXT_HOPS
19 #define RTE_TABLE_LPM_MAX_NEXT_HOPS 65536
22 #ifdef RTE_TABLE_STATS_COLLECT
24 #define RTE_TABLE_LPM_STATS_PKTS_IN_ADD(table, val) \
25 table->stats.n_pkts_in += val
26 #define RTE_TABLE_LPM_STATS_PKTS_LOOKUP_MISS(table, val) \
27 table->stats.n_pkts_lookup_miss += val
31 #define RTE_TABLE_LPM_STATS_PKTS_IN_ADD(table, val)
32 #define RTE_TABLE_LPM_STATS_PKTS_LOOKUP_MISS(table, val)
36 struct rte_table_lpm {
37 struct rte_table_stats stats;
39 /* Input parameters */
41 uint32_t entry_unique_size;
45 /* Handle to low-level LPM table */
48 /* Next Hop Table (NHT) */
49 uint32_t nht_users[RTE_TABLE_LPM_MAX_NEXT_HOPS];
50 uint8_t nht[0] __rte_cache_aligned;
54 rte_table_lpm_create(void *params, int socket_id, uint32_t entry_size)
56 struct rte_table_lpm_params *p = params;
57 struct rte_table_lpm *lpm;
58 struct rte_lpm_config lpm_config;
60 uint32_t total_size, nht_size;
62 /* Check input parameters */
64 RTE_LOG(ERR, TABLE, "%s: NULL input parameters\n", __func__);
67 if (p->n_rules == 0) {
68 RTE_LOG(ERR, TABLE, "%s: Invalid n_rules\n", __func__);
71 if (p->number_tbl8s == 0) {
72 RTE_LOG(ERR, TABLE, "%s: Invalid number_tbl8s\n", __func__);
75 if (p->entry_unique_size == 0) {
76 RTE_LOG(ERR, TABLE, "%s: Invalid entry_unique_size\n",
80 if (p->entry_unique_size > entry_size) {
81 RTE_LOG(ERR, TABLE, "%s: Invalid entry_unique_size\n",
85 if (p->name == NULL) {
86 RTE_LOG(ERR, TABLE, "%s: Table name is NULL\n",
90 entry_size = RTE_ALIGN(entry_size, sizeof(uint64_t));
92 /* Memory allocation */
93 nht_size = RTE_TABLE_LPM_MAX_NEXT_HOPS * entry_size;
94 total_size = sizeof(struct rte_table_lpm) + nht_size;
95 lpm = rte_zmalloc_socket("TABLE", total_size, RTE_CACHE_LINE_SIZE,
99 "%s: Cannot allocate %u bytes for LPM table\n",
100 __func__, total_size);
104 /* LPM low-level table creation */
105 lpm_config.max_rules = p->n_rules;
106 lpm_config.number_tbl8s = p->number_tbl8s;
107 lpm_config.flags = p->flags;
108 lpm->lpm = rte_lpm_create(p->name, socket_id, &lpm_config);
110 if (lpm->lpm == NULL) {
112 RTE_LOG(ERR, TABLE, "Unable to create low-level LPM table\n");
116 /* Memory initialization */
117 lpm->entry_size = entry_size;
118 lpm->entry_unique_size = p->entry_unique_size;
119 lpm->n_rules = p->n_rules;
120 lpm->offset = p->offset;
126 rte_table_lpm_free(void *table)
128 struct rte_table_lpm *lpm = table;
130 /* Check input parameters */
132 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
136 /* Free previously allocated resources */
137 rte_lpm_free(lpm->lpm);
144 nht_find_free(struct rte_table_lpm *lpm, uint32_t *pos)
148 for (i = 0; i < RTE_TABLE_LPM_MAX_NEXT_HOPS; i++) {
149 if (lpm->nht_users[i] == 0) {
159 nht_find_existing(struct rte_table_lpm *lpm, void *entry, uint32_t *pos)
163 for (i = 0; i < RTE_TABLE_LPM_MAX_NEXT_HOPS; i++) {
164 uint8_t *nht_entry = &lpm->nht[i * lpm->entry_size];
166 if ((lpm->nht_users[i] > 0) && (memcmp(nht_entry, entry,
167 lpm->entry_unique_size) == 0)) {
177 rte_table_lpm_entry_add(
184 struct rte_table_lpm *lpm = table;
185 struct rte_table_lpm_key *ip_prefix = key;
186 uint32_t nht_pos, nht_pos0_valid;
188 uint32_t nht_pos0 = 0;
190 /* Check input parameters */
192 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
195 if (ip_prefix == NULL) {
196 RTE_LOG(ERR, TABLE, "%s: ip_prefix parameter is NULL\n",
201 RTE_LOG(ERR, TABLE, "%s: entry parameter is NULL\n", __func__);
205 if ((ip_prefix->depth == 0) || (ip_prefix->depth > 32)) {
206 RTE_LOG(ERR, TABLE, "%s: invalid depth (%d)\n",
207 __func__, ip_prefix->depth);
211 /* Check if rule is already present in the table */
212 status = rte_lpm_is_rule_present(lpm->lpm, ip_prefix->ip,
213 ip_prefix->depth, &nht_pos0);
214 nht_pos0_valid = status > 0;
216 /* Find existing or free NHT entry */
217 if (nht_find_existing(lpm, entry, &nht_pos) == 0) {
220 if (nht_find_free(lpm, &nht_pos) == 0) {
221 RTE_LOG(ERR, TABLE, "%s: NHT full\n", __func__);
225 nht_entry = &lpm->nht[nht_pos * lpm->entry_size];
226 memcpy(nht_entry, entry, lpm->entry_size);
229 /* Add rule to low level LPM table */
230 if (rte_lpm_add(lpm->lpm, ip_prefix->ip, ip_prefix->depth, nht_pos) < 0) {
231 RTE_LOG(ERR, TABLE, "%s: LPM rule add failed\n", __func__);
235 /* Commit NHT changes */
236 lpm->nht_users[nht_pos]++;
237 lpm->nht_users[nht_pos0] -= nht_pos0_valid;
239 *key_found = nht_pos0_valid;
240 *entry_ptr = (void *) &lpm->nht[nht_pos * lpm->entry_size];
245 rte_table_lpm_entry_delete(
251 struct rte_table_lpm *lpm = table;
252 struct rte_table_lpm_key *ip_prefix = key;
256 /* Check input parameters */
258 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
261 if (ip_prefix == NULL) {
262 RTE_LOG(ERR, TABLE, "%s: ip_prefix parameter is NULL\n",
266 if ((ip_prefix->depth == 0) || (ip_prefix->depth > 32)) {
267 RTE_LOG(ERR, TABLE, "%s: invalid depth (%d)\n", __func__,
272 /* Return if rule is not present in the table */
273 status = rte_lpm_is_rule_present(lpm->lpm, ip_prefix->ip,
274 ip_prefix->depth, &nht_pos);
276 RTE_LOG(ERR, TABLE, "%s: LPM algorithmic error\n", __func__);
284 /* Delete rule from the low-level LPM table */
285 status = rte_lpm_delete(lpm->lpm, ip_prefix->ip, ip_prefix->depth);
287 RTE_LOG(ERR, TABLE, "%s: LPM rule delete failed\n", __func__);
291 /* Commit NHT changes */
292 lpm->nht_users[nht_pos]--;
296 memcpy(entry, &lpm->nht[nht_pos * lpm->entry_size],
303 rte_table_lpm_lookup(
305 struct rte_mbuf **pkts,
307 uint64_t *lookup_hit_mask,
310 struct rte_table_lpm *lpm = (struct rte_table_lpm *) table;
311 uint64_t pkts_out_mask = 0;
314 __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
315 RTE_TABLE_LPM_STATS_PKTS_IN_ADD(lpm, n_pkts_in);
318 for (i = 0; i < (uint32_t)(RTE_PORT_IN_BURST_SIZE_MAX -
319 __builtin_clzll(pkts_mask)); i++) {
320 uint64_t pkt_mask = 1LLU << i;
322 if (pkt_mask & pkts_mask) {
323 struct rte_mbuf *pkt = pkts[i];
324 uint32_t ip = rte_bswap32(
325 RTE_MBUF_METADATA_UINT32(pkt, lpm->offset));
329 status = rte_lpm_lookup(lpm->lpm, ip, &nht_pos);
331 pkts_out_mask |= pkt_mask;
332 entries[i] = (void *) &lpm->nht[nht_pos *
338 *lookup_hit_mask = pkts_out_mask;
339 RTE_TABLE_LPM_STATS_PKTS_LOOKUP_MISS(lpm, n_pkts_in - __builtin_popcountll(pkts_out_mask));
344 rte_table_lpm_stats_read(void *table, struct rte_table_stats *stats, int clear)
346 struct rte_table_lpm *t = table;
349 memcpy(stats, &t->stats, sizeof(t->stats));
352 memset(&t->stats, 0, sizeof(t->stats));
357 struct rte_table_ops rte_table_lpm_ops = {
358 .f_create = rte_table_lpm_create,
359 .f_free = rte_table_lpm_free,
360 .f_add = rte_table_lpm_entry_add,
361 .f_delete = rte_table_lpm_entry_delete,
363 .f_delete_bulk = NULL,
364 .f_lookup = rte_table_lpm_lookup,
365 .f_stats = rte_table_lpm_stats_read,