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_ipv6.h"
18 #define RTE_TABLE_LPM_MAX_NEXT_HOPS 256
20 #ifdef RTE_TABLE_STATS_COLLECT
22 #define RTE_TABLE_LPM_IPV6_STATS_PKTS_IN_ADD(table, val) \
23 table->stats.n_pkts_in += val
24 #define RTE_TABLE_LPM_IPV6_STATS_PKTS_LOOKUP_MISS(table, val) \
25 table->stats.n_pkts_lookup_miss += val
29 #define RTE_TABLE_LPM_IPV6_STATS_PKTS_IN_ADD(table, val)
30 #define RTE_TABLE_LPM_IPV6_STATS_PKTS_LOOKUP_MISS(table, val)
34 struct rte_table_lpm_ipv6 {
35 struct rte_table_stats stats;
37 /* Input parameters */
39 uint32_t entry_unique_size;
43 /* Handle to low-level LPM table */
46 /* Next Hop Table (NHT) */
47 uint32_t nht_users[RTE_TABLE_LPM_MAX_NEXT_HOPS];
48 uint8_t nht[0] __rte_cache_aligned;
52 rte_table_lpm_ipv6_create(void *params, int socket_id, uint32_t entry_size)
54 struct rte_table_lpm_ipv6_params *p =
56 struct rte_table_lpm_ipv6 *lpm;
57 struct rte_lpm6_config lpm6_config;
58 uint32_t total_size, nht_size;
60 /* Check input parameters */
62 RTE_LOG(ERR, TABLE, "%s: NULL input parameters\n", __func__);
65 if (p->n_rules == 0) {
66 RTE_LOG(ERR, TABLE, "%s: Invalid n_rules\n", __func__);
69 if (p->number_tbl8s == 0) {
70 RTE_LOG(ERR, TABLE, "%s: Invalid n_rules\n", __func__);
73 if (p->entry_unique_size == 0) {
74 RTE_LOG(ERR, TABLE, "%s: Invalid entry_unique_size\n",
78 if (p->entry_unique_size > entry_size) {
79 RTE_LOG(ERR, TABLE, "%s: Invalid entry_unique_size\n",
83 if (p->name == NULL) {
84 RTE_LOG(ERR, TABLE, "%s: Table name is NULL\n",
88 entry_size = RTE_ALIGN(entry_size, sizeof(uint64_t));
90 /* Memory allocation */
91 nht_size = RTE_TABLE_LPM_MAX_NEXT_HOPS * entry_size;
92 total_size = sizeof(struct rte_table_lpm_ipv6) + nht_size;
93 lpm = rte_zmalloc_socket("TABLE", total_size, RTE_CACHE_LINE_SIZE,
97 "%s: Cannot allocate %u bytes for LPM IPv6 table\n",
98 __func__, total_size);
102 /* LPM low-level table creation */
103 lpm6_config.max_rules = p->n_rules;
104 lpm6_config.number_tbl8s = p->number_tbl8s;
105 lpm6_config.flags = 0;
106 lpm->lpm = rte_lpm6_create(p->name, socket_id, &lpm6_config);
107 if (lpm->lpm == NULL) {
110 "Unable to create low-level LPM IPv6 table\n");
114 /* Memory initialization */
115 lpm->entry_size = entry_size;
116 lpm->entry_unique_size = p->entry_unique_size;
117 lpm->n_rules = p->n_rules;
118 lpm->offset = p->offset;
124 rte_table_lpm_ipv6_free(void *table)
126 struct rte_table_lpm_ipv6 *lpm = table;
128 /* Check input parameters */
130 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
134 /* Free previously allocated resources */
135 rte_lpm6_free(lpm->lpm);
142 nht_find_free(struct rte_table_lpm_ipv6 *lpm, uint32_t *pos)
146 for (i = 0; i < RTE_TABLE_LPM_MAX_NEXT_HOPS; i++) {
147 if (lpm->nht_users[i] == 0) {
157 nht_find_existing(struct rte_table_lpm_ipv6 *lpm, void *entry, uint32_t *pos)
161 for (i = 0; i < RTE_TABLE_LPM_MAX_NEXT_HOPS; i++) {
162 uint8_t *nht_entry = &lpm->nht[i * lpm->entry_size];
164 if ((lpm->nht_users[i] > 0) && (memcmp(nht_entry, entry,
165 lpm->entry_unique_size) == 0)) {
175 rte_table_lpm_ipv6_entry_add(
182 struct rte_table_lpm_ipv6 *lpm = table;
183 struct rte_table_lpm_ipv6_key *ip_prefix =
185 uint32_t nht_pos = 0, nht_pos0 = 0, nht_pos0_valid = 0;
188 /* Check input parameters */
190 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
193 if (ip_prefix == NULL) {
194 RTE_LOG(ERR, TABLE, "%s: ip_prefix parameter is NULL\n",
199 RTE_LOG(ERR, TABLE, "%s: entry parameter is NULL\n", __func__);
203 if ((ip_prefix->depth == 0) || (ip_prefix->depth > 128)) {
204 RTE_LOG(ERR, TABLE, "%s: invalid depth (%d)\n", __func__,
209 /* Check if rule is already present in the table */
210 status = rte_lpm6_is_rule_present(lpm->lpm, ip_prefix->ip,
211 ip_prefix->depth, &nht_pos0);
212 nht_pos0_valid = status > 0;
214 /* Find existing or free NHT entry */
215 if (nht_find_existing(lpm, entry, &nht_pos) == 0) {
218 if (nht_find_free(lpm, &nht_pos) == 0) {
219 RTE_LOG(ERR, TABLE, "%s: NHT full\n", __func__);
223 nht_entry = &lpm->nht[nht_pos * lpm->entry_size];
224 memcpy(nht_entry, entry, lpm->entry_size);
227 /* Add rule to low level LPM table */
228 if (rte_lpm6_add(lpm->lpm, ip_prefix->ip, ip_prefix->depth,
230 RTE_LOG(ERR, TABLE, "%s: LPM IPv6 rule add failed\n", __func__);
234 /* Commit NHT changes */
235 lpm->nht_users[nht_pos]++;
236 lpm->nht_users[nht_pos0] -= nht_pos0_valid;
238 *key_found = nht_pos0_valid;
239 *entry_ptr = (void *) &lpm->nht[nht_pos * lpm->entry_size];
244 rte_table_lpm_ipv6_entry_delete(
250 struct rte_table_lpm_ipv6 *lpm = table;
251 struct rte_table_lpm_ipv6_key *ip_prefix =
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 > 128)) {
267 RTE_LOG(ERR, TABLE, "%s: invalid depth (%d)\n", __func__,
272 /* Return if rule is not present in the table */
273 status = rte_lpm6_is_rule_present(lpm->lpm, ip_prefix->ip,
274 ip_prefix->depth, &nht_pos);
276 RTE_LOG(ERR, TABLE, "%s: LPM IPv6 algorithmic error\n",
285 /* Delete rule from the low-level LPM table */
286 status = rte_lpm6_delete(lpm->lpm, ip_prefix->ip, ip_prefix->depth);
288 RTE_LOG(ERR, TABLE, "%s: LPM IPv6 rule delete failed\n",
293 /* Commit NHT changes */
294 lpm->nht_users[nht_pos]--;
298 memcpy(entry, &lpm->nht[nht_pos * lpm->entry_size],
305 rte_table_lpm_ipv6_lookup(
307 struct rte_mbuf **pkts,
309 uint64_t *lookup_hit_mask,
312 struct rte_table_lpm_ipv6 *lpm = (struct rte_table_lpm_ipv6 *) table;
313 uint64_t pkts_out_mask = 0;
316 __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
317 RTE_TABLE_LPM_IPV6_STATS_PKTS_IN_ADD(lpm, n_pkts_in);
320 for (i = 0; i < (uint32_t)(RTE_PORT_IN_BURST_SIZE_MAX -
321 __builtin_clzll(pkts_mask)); i++) {
322 uint64_t pkt_mask = 1LLU << i;
324 if (pkt_mask & pkts_mask) {
325 struct rte_mbuf *pkt = pkts[i];
326 uint8_t *ip = RTE_MBUF_METADATA_UINT8_PTR(pkt,
331 status = rte_lpm6_lookup(lpm->lpm, ip, &nht_pos);
333 pkts_out_mask |= pkt_mask;
334 entries[i] = (void *) &lpm->nht[nht_pos *
340 *lookup_hit_mask = pkts_out_mask;
341 RTE_TABLE_LPM_IPV6_STATS_PKTS_LOOKUP_MISS(lpm, n_pkts_in - __builtin_popcountll(pkts_out_mask));
346 rte_table_lpm_ipv6_stats_read(void *table, struct rte_table_stats *stats, int clear)
348 struct rte_table_lpm_ipv6 *t = table;
351 memcpy(stats, &t->stats, sizeof(t->stats));
354 memset(&t->stats, 0, sizeof(t->stats));
359 struct rte_table_ops rte_table_lpm_ipv6_ops = {
360 .f_create = rte_table_lpm_ipv6_create,
361 .f_free = rte_table_lpm_ipv6_free,
362 .f_add = rte_table_lpm_ipv6_entry_add,
363 .f_delete = rte_table_lpm_ipv6_entry_delete,
365 .f_delete_bulk = NULL,
366 .f_lookup = rte_table_lpm_ipv6_lookup,
367 .f_stats = rte_table_lpm_ipv6_stats_read,