1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
8 #include <rte_common.h>
9 #include <rte_malloc.h>
13 #include "rte_table_lpm_ipv6.h"
15 #define RTE_TABLE_LPM_MAX_NEXT_HOPS 256
17 #ifdef RTE_TABLE_STATS_COLLECT
19 #define RTE_TABLE_LPM_IPV6_STATS_PKTS_IN_ADD(table, val) \
20 table->stats.n_pkts_in += val
21 #define RTE_TABLE_LPM_IPV6_STATS_PKTS_LOOKUP_MISS(table, val) \
22 table->stats.n_pkts_lookup_miss += val
26 #define RTE_TABLE_LPM_IPV6_STATS_PKTS_IN_ADD(table, val)
27 #define RTE_TABLE_LPM_IPV6_STATS_PKTS_LOOKUP_MISS(table, val)
31 struct rte_table_lpm_ipv6 {
32 struct rte_table_stats stats;
34 /* Input parameters */
36 uint32_t entry_unique_size;
40 /* Handle to low-level LPM table */
43 /* Next Hop Table (NHT) */
44 uint32_t nht_users[RTE_TABLE_LPM_MAX_NEXT_HOPS];
45 uint8_t nht[0] __rte_cache_aligned;
49 rte_table_lpm_ipv6_create(void *params, int socket_id, uint32_t entry_size)
51 struct rte_table_lpm_ipv6_params *p =
53 struct rte_table_lpm_ipv6 *lpm;
54 struct rte_lpm6_config lpm6_config;
55 uint32_t total_size, nht_size;
57 /* Check input parameters */
59 RTE_LOG(ERR, TABLE, "%s: NULL input parameters\n", __func__);
62 if (p->n_rules == 0) {
63 RTE_LOG(ERR, TABLE, "%s: Invalid n_rules\n", __func__);
66 if (p->number_tbl8s == 0) {
67 RTE_LOG(ERR, TABLE, "%s: Invalid n_rules\n", __func__);
70 if (p->entry_unique_size == 0) {
71 RTE_LOG(ERR, TABLE, "%s: Invalid entry_unique_size\n",
75 if (p->entry_unique_size > entry_size) {
76 RTE_LOG(ERR, TABLE, "%s: Invalid entry_unique_size\n",
80 if (p->name == NULL) {
81 RTE_LOG(ERR, TABLE, "%s: Table name is NULL\n",
85 entry_size = RTE_ALIGN(entry_size, sizeof(uint64_t));
87 /* Memory allocation */
88 nht_size = RTE_TABLE_LPM_MAX_NEXT_HOPS * entry_size;
89 total_size = sizeof(struct rte_table_lpm_ipv6) + nht_size;
90 lpm = rte_zmalloc_socket("TABLE", total_size, RTE_CACHE_LINE_SIZE,
94 "%s: Cannot allocate %u bytes for LPM IPv6 table\n",
95 __func__, total_size);
99 /* LPM low-level table creation */
100 lpm6_config.max_rules = p->n_rules;
101 lpm6_config.number_tbl8s = p->number_tbl8s;
102 lpm6_config.flags = 0;
103 lpm->lpm = rte_lpm6_create(p->name, socket_id, &lpm6_config);
104 if (lpm->lpm == NULL) {
107 "Unable to create low-level LPM IPv6 table\n");
111 /* Memory initialization */
112 lpm->entry_size = entry_size;
113 lpm->entry_unique_size = p->entry_unique_size;
114 lpm->n_rules = p->n_rules;
115 lpm->offset = p->offset;
121 rte_table_lpm_ipv6_free(void *table)
123 struct rte_table_lpm_ipv6 *lpm = table;
125 /* Check input parameters */
127 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
131 /* Free previously allocated resources */
132 rte_lpm6_free(lpm->lpm);
139 nht_find_free(struct rte_table_lpm_ipv6 *lpm, uint32_t *pos)
143 for (i = 0; i < RTE_TABLE_LPM_MAX_NEXT_HOPS; i++) {
144 if (lpm->nht_users[i] == 0) {
154 nht_find_existing(struct rte_table_lpm_ipv6 *lpm, void *entry, uint32_t *pos)
158 for (i = 0; i < RTE_TABLE_LPM_MAX_NEXT_HOPS; i++) {
159 uint8_t *nht_entry = &lpm->nht[i * lpm->entry_size];
161 if ((lpm->nht_users[i] > 0) && (memcmp(nht_entry, entry,
162 lpm->entry_unique_size) == 0)) {
172 rte_table_lpm_ipv6_entry_add(
179 struct rte_table_lpm_ipv6 *lpm = table;
180 struct rte_table_lpm_ipv6_key *ip_prefix =
182 uint32_t nht_pos = 0, nht_pos0 = 0, nht_pos0_valid = 0;
185 /* Check input parameters */
187 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
190 if (ip_prefix == NULL) {
191 RTE_LOG(ERR, TABLE, "%s: ip_prefix parameter is NULL\n",
196 RTE_LOG(ERR, TABLE, "%s: entry parameter is NULL\n", __func__);
200 if ((ip_prefix->depth == 0) || (ip_prefix->depth > 128)) {
201 RTE_LOG(ERR, TABLE, "%s: invalid depth (%d)\n", __func__,
206 /* Check if rule is already present in the table */
207 status = rte_lpm6_is_rule_present(lpm->lpm, ip_prefix->ip,
208 ip_prefix->depth, &nht_pos0);
209 nht_pos0_valid = status > 0;
211 /* Find existing or free NHT entry */
212 if (nht_find_existing(lpm, entry, &nht_pos) == 0) {
215 if (nht_find_free(lpm, &nht_pos) == 0) {
216 RTE_LOG(ERR, TABLE, "%s: NHT full\n", __func__);
220 nht_entry = &lpm->nht[nht_pos * lpm->entry_size];
221 memcpy(nht_entry, entry, lpm->entry_size);
224 /* Add rule to low level LPM table */
225 if (rte_lpm6_add(lpm->lpm, ip_prefix->ip, ip_prefix->depth,
227 RTE_LOG(ERR, TABLE, "%s: LPM IPv6 rule add failed\n", __func__);
231 /* Commit NHT changes */
232 lpm->nht_users[nht_pos]++;
233 lpm->nht_users[nht_pos0] -= nht_pos0_valid;
235 *key_found = nht_pos0_valid;
236 *entry_ptr = (void *) &lpm->nht[nht_pos * lpm->entry_size];
241 rte_table_lpm_ipv6_entry_delete(
247 struct rte_table_lpm_ipv6 *lpm = table;
248 struct rte_table_lpm_ipv6_key *ip_prefix =
253 /* Check input parameters */
255 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
258 if (ip_prefix == NULL) {
259 RTE_LOG(ERR, TABLE, "%s: ip_prefix parameter is NULL\n",
263 if ((ip_prefix->depth == 0) || (ip_prefix->depth > 128)) {
264 RTE_LOG(ERR, TABLE, "%s: invalid depth (%d)\n", __func__,
269 /* Return if rule is not present in the table */
270 status = rte_lpm6_is_rule_present(lpm->lpm, ip_prefix->ip,
271 ip_prefix->depth, &nht_pos);
273 RTE_LOG(ERR, TABLE, "%s: LPM IPv6 algorithmic error\n",
282 /* Delete rule from the low-level LPM table */
283 status = rte_lpm6_delete(lpm->lpm, ip_prefix->ip, ip_prefix->depth);
285 RTE_LOG(ERR, TABLE, "%s: LPM IPv6 rule delete failed\n",
290 /* Commit NHT changes */
291 lpm->nht_users[nht_pos]--;
295 memcpy(entry, &lpm->nht[nht_pos * lpm->entry_size],
302 rte_table_lpm_ipv6_lookup(
304 struct rte_mbuf **pkts,
306 uint64_t *lookup_hit_mask,
309 struct rte_table_lpm_ipv6 *lpm = (struct rte_table_lpm_ipv6 *) table;
310 uint64_t pkts_out_mask = 0;
313 __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
314 RTE_TABLE_LPM_IPV6_STATS_PKTS_IN_ADD(lpm, n_pkts_in);
317 for (i = 0; i < (uint32_t)(RTE_PORT_IN_BURST_SIZE_MAX -
318 __builtin_clzll(pkts_mask)); i++) {
319 uint64_t pkt_mask = 1LLU << i;
321 if (pkt_mask & pkts_mask) {
322 struct rte_mbuf *pkt = pkts[i];
323 uint8_t *ip = RTE_MBUF_METADATA_UINT8_PTR(pkt,
328 status = rte_lpm6_lookup(lpm->lpm, ip, &nht_pos);
330 pkts_out_mask |= pkt_mask;
331 entries[i] = (void *) &lpm->nht[nht_pos *
337 *lookup_hit_mask = pkts_out_mask;
338 RTE_TABLE_LPM_IPV6_STATS_PKTS_LOOKUP_MISS(lpm, n_pkts_in - __builtin_popcountll(pkts_out_mask));
343 rte_table_lpm_ipv6_stats_read(void *table, struct rte_table_stats *stats, int clear)
345 struct rte_table_lpm_ipv6 *t = table;
348 memcpy(stats, &t->stats, sizeof(t->stats));
351 memset(&t->stats, 0, sizeof(t->stats));
356 struct rte_table_ops rte_table_lpm_ipv6_ops = {
357 .f_create = rte_table_lpm_ipv6_create,
358 .f_free = rte_table_lpm_ipv6_free,
359 .f_add = rte_table_lpm_ipv6_entry_add,
360 .f_delete = rte_table_lpm_ipv6_entry_delete,
362 .f_delete_bulk = NULL,
363 .f_lookup = rte_table_lpm_ipv6_lookup,
364 .f_stats = rte_table_lpm_ipv6_stats_read,