4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <rte_common.h>
39 #include <rte_memory.h>
40 #include <rte_malloc.h>
41 #include <rte_byteorder.h>
45 #include "rte_table_lpm_ipv6.h"
47 #define RTE_TABLE_LPM_MAX_NEXT_HOPS 256
49 #ifdef RTE_TABLE_STATS_COLLECT
51 #define RTE_TABLE_LPM_IPV6_STATS_PKTS_IN_ADD(table, val) \
52 table->stats.n_pkts_in += val
53 #define RTE_TABLE_LPM_IPV6_STATS_PKTS_LOOKUP_MISS(table, val) \
54 table->stats.n_pkts_lookup_miss += val
58 #define RTE_TABLE_LPM_IPV6_STATS_PKTS_IN_ADD(table, val)
59 #define RTE_TABLE_LPM_IPV6_STATS_PKTS_LOOKUP_MISS(table, val)
63 struct rte_table_lpm_ipv6 {
64 struct rte_table_stats stats;
66 /* Input parameters */
68 uint32_t entry_unique_size;
72 /* Handle to low-level LPM table */
75 /* Next Hop Table (NHT) */
76 uint32_t nht_users[RTE_TABLE_LPM_MAX_NEXT_HOPS];
77 uint8_t nht[0] __rte_cache_aligned;
81 rte_table_lpm_ipv6_create(void *params, int socket_id, uint32_t entry_size)
83 struct rte_table_lpm_ipv6_params *p =
84 (struct rte_table_lpm_ipv6_params *) params;
85 struct rte_table_lpm_ipv6 *lpm;
86 struct rte_lpm6_config lpm6_config;
87 uint32_t total_size, nht_size;
89 /* Check input parameters */
91 RTE_LOG(ERR, TABLE, "%s: NULL input parameters\n", __func__);
94 if (p->n_rules == 0) {
95 RTE_LOG(ERR, TABLE, "%s: Invalid n_rules\n", __func__);
98 if (p->number_tbl8s == 0) {
99 RTE_LOG(ERR, TABLE, "%s: Invalid n_rules\n", __func__);
102 if (p->entry_unique_size == 0) {
103 RTE_LOG(ERR, TABLE, "%s: Invalid entry_unique_size\n",
107 if (p->entry_unique_size > entry_size) {
108 RTE_LOG(ERR, TABLE, "%s: Invalid entry_unique_size\n",
113 entry_size = RTE_ALIGN(entry_size, sizeof(uint64_t));
115 /* Memory allocation */
116 nht_size = RTE_TABLE_LPM_MAX_NEXT_HOPS * entry_size;
117 total_size = sizeof(struct rte_table_lpm_ipv6) + nht_size;
118 lpm = rte_zmalloc_socket("TABLE", total_size, RTE_CACHE_LINE_SIZE,
122 "%s: Cannot allocate %u bytes for LPM IPv6 table\n",
123 __func__, total_size);
127 /* LPM low-level table creation */
128 lpm6_config.max_rules = p->n_rules;
129 lpm6_config.number_tbl8s = p->number_tbl8s;
130 lpm6_config.flags = 0;
131 lpm->lpm = rte_lpm6_create("LPM IPv6", socket_id, &lpm6_config);
132 if (lpm->lpm == NULL) {
135 "Unable to create low-level LPM IPv6 table\n");
139 /* Memory initialization */
140 lpm->entry_size = entry_size;
141 lpm->entry_unique_size = p->entry_unique_size;
142 lpm->n_rules = p->n_rules;
143 lpm->offset = p->offset;
149 rte_table_lpm_ipv6_free(void *table)
151 struct rte_table_lpm_ipv6 *lpm = (struct rte_table_lpm_ipv6 *) table;
153 /* Check input parameters */
155 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
159 /* Free previously allocated resources */
160 rte_lpm6_free(lpm->lpm);
167 nht_find_free(struct rte_table_lpm_ipv6 *lpm, uint32_t *pos)
171 for (i = 0; i < RTE_TABLE_LPM_MAX_NEXT_HOPS; i++) {
172 if (lpm->nht_users[i] == 0) {
182 nht_find_existing(struct rte_table_lpm_ipv6 *lpm, void *entry, uint32_t *pos)
186 for (i = 0; i < RTE_TABLE_LPM_MAX_NEXT_HOPS; i++) {
187 uint8_t *nht_entry = &lpm->nht[i * lpm->entry_size];
189 if ((lpm->nht_users[i] > 0) && (memcmp(nht_entry, entry,
190 lpm->entry_unique_size) == 0)) {
200 rte_table_lpm_ipv6_entry_add(
207 struct rte_table_lpm_ipv6 *lpm = (struct rte_table_lpm_ipv6 *) table;
208 struct rte_table_lpm_ipv6_key *ip_prefix =
209 (struct rte_table_lpm_ipv6_key *) key;
210 uint32_t nht_pos, nht_pos0_valid;
214 /* Check input parameters */
216 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
219 if (ip_prefix == NULL) {
220 RTE_LOG(ERR, TABLE, "%s: ip_prefix parameter is NULL\n",
225 RTE_LOG(ERR, TABLE, "%s: entry parameter is NULL\n", __func__);
229 if ((ip_prefix->depth == 0) || (ip_prefix->depth > 128)) {
230 RTE_LOG(ERR, TABLE, "%s: invalid depth (%d)\n", __func__,
235 /* Check if rule is already present in the table */
236 status = rte_lpm6_is_rule_present(lpm->lpm, ip_prefix->ip,
237 ip_prefix->depth, &nht_pos0);
238 nht_pos0_valid = status > 0;
240 /* Find existing or free NHT entry */
241 if (nht_find_existing(lpm, entry, &nht_pos) == 0) {
244 if (nht_find_free(lpm, &nht_pos) == 0) {
245 RTE_LOG(ERR, TABLE, "%s: NHT full\n", __func__);
249 nht_entry = &lpm->nht[nht_pos * lpm->entry_size];
250 memcpy(nht_entry, entry, lpm->entry_size);
253 /* Add rule to low level LPM table */
254 if (rte_lpm6_add(lpm->lpm, ip_prefix->ip, ip_prefix->depth,
255 (uint8_t) nht_pos) < 0) {
256 RTE_LOG(ERR, TABLE, "%s: LPM IPv6 rule add failed\n", __func__);
260 /* Commit NHT changes */
261 lpm->nht_users[nht_pos]++;
262 lpm->nht_users[nht_pos0] -= nht_pos0_valid;
264 *key_found = nht_pos0_valid;
265 *entry_ptr = (void *) &lpm->nht[nht_pos * lpm->entry_size];
270 rte_table_lpm_ipv6_entry_delete(
276 struct rte_table_lpm_ipv6 *lpm = (struct rte_table_lpm_ipv6 *) table;
277 struct rte_table_lpm_ipv6_key *ip_prefix =
278 (struct rte_table_lpm_ipv6_key *) key;
282 /* Check input parameters */
284 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
287 if (ip_prefix == NULL) {
288 RTE_LOG(ERR, TABLE, "%s: ip_prefix parameter is NULL\n",
292 if ((ip_prefix->depth == 0) || (ip_prefix->depth > 128)) {
293 RTE_LOG(ERR, TABLE, "%s: invalid depth (%d)\n", __func__,
298 /* Return if rule is not present in the table */
299 status = rte_lpm6_is_rule_present(lpm->lpm, ip_prefix->ip,
300 ip_prefix->depth, &nht_pos);
302 RTE_LOG(ERR, TABLE, "%s: LPM IPv6 algorithmic error\n",
311 /* Delete rule from the low-level LPM table */
312 status = rte_lpm6_delete(lpm->lpm, ip_prefix->ip, ip_prefix->depth);
314 RTE_LOG(ERR, TABLE, "%s: LPM IPv6 rule delete failed\n",
319 /* Commit NHT changes */
320 lpm->nht_users[nht_pos]--;
324 memcpy(entry, &lpm->nht[nht_pos * lpm->entry_size],
331 rte_table_lpm_ipv6_lookup(
333 struct rte_mbuf **pkts,
335 uint64_t *lookup_hit_mask,
338 struct rte_table_lpm_ipv6 *lpm = (struct rte_table_lpm_ipv6 *) table;
339 uint64_t pkts_out_mask = 0;
342 __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
343 RTE_TABLE_LPM_IPV6_STATS_PKTS_IN_ADD(lpm, n_pkts_in);
346 for (i = 0; i < (uint32_t)(RTE_PORT_IN_BURST_SIZE_MAX -
347 __builtin_clzll(pkts_mask)); i++) {
348 uint64_t pkt_mask = 1LLU << i;
350 if (pkt_mask & pkts_mask) {
351 struct rte_mbuf *pkt = pkts[i];
352 uint8_t *ip = RTE_MBUF_METADATA_UINT8_PTR(pkt,
357 status = rte_lpm6_lookup(lpm->lpm, ip, &nht_pos);
359 pkts_out_mask |= pkt_mask;
360 entries[i] = (void *) &lpm->nht[nht_pos *
366 *lookup_hit_mask = pkts_out_mask;
367 RTE_TABLE_LPM_IPV6_STATS_PKTS_LOOKUP_MISS(lpm, n_pkts_in - __builtin_popcountll(pkts_out_mask));
372 rte_table_lpm_ipv6_stats_read(void *table, struct rte_table_stats *stats, int clear)
374 struct rte_table_lpm_ipv6 *t = (struct rte_table_lpm_ipv6 *) table;
377 memcpy(stats, &t->stats, sizeof(t->stats));
380 memset(&t->stats, 0, sizeof(t->stats));
385 struct rte_table_ops rte_table_lpm_ipv6_ops = {
386 .f_create = rte_table_lpm_ipv6_create,
387 .f_free = rte_table_lpm_ipv6_free,
388 .f_add = rte_table_lpm_ipv6_entry_add,
389 .f_delete = rte_table_lpm_ipv6_entry_delete,
390 .f_lookup = rte_table_lpm_ipv6_lookup,
391 .f_stats = rte_table_lpm_ipv6_stats_read,