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 struct rte_table_lpm_ipv6 {
50 /* Input parameters */
52 uint32_t entry_unique_size;
56 /* Handle to low-level LPM table */
59 /* Next Hop Table (NHT) */
60 uint32_t nht_users[RTE_TABLE_LPM_MAX_NEXT_HOPS];
61 uint8_t nht[0] __rte_cache_aligned;
65 rte_table_lpm_ipv6_create(void *params, int socket_id, uint32_t entry_size)
67 struct rte_table_lpm_ipv6_params *p =
68 (struct rte_table_lpm_ipv6_params *) params;
69 struct rte_table_lpm_ipv6 *lpm;
70 struct rte_lpm6_config lpm6_config;
71 uint32_t total_size, nht_size;
73 /* Check input parameters */
75 RTE_LOG(ERR, TABLE, "%s: NULL input parameters\n", __func__);
78 if (p->n_rules == 0) {
79 RTE_LOG(ERR, TABLE, "%s: Invalid n_rules\n", __func__);
82 if (p->number_tbl8s == 0) {
83 RTE_LOG(ERR, TABLE, "%s: Invalid n_rules\n", __func__);
86 if (p->entry_unique_size == 0) {
87 RTE_LOG(ERR, TABLE, "%s: Invalid entry_unique_size\n",
91 if (p->entry_unique_size > entry_size) {
92 RTE_LOG(ERR, TABLE, "%s: Invalid entry_unique_size\n",
97 entry_size = RTE_ALIGN(entry_size, sizeof(uint64_t));
99 /* Memory allocation */
100 nht_size = RTE_TABLE_LPM_MAX_NEXT_HOPS * entry_size;
101 total_size = sizeof(struct rte_table_lpm_ipv6) + nht_size;
102 lpm = rte_zmalloc_socket("TABLE", total_size, RTE_CACHE_LINE_SIZE,
106 "%s: Cannot allocate %u bytes for LPM IPv6 table\n",
107 __func__, total_size);
111 /* LPM low-level table creation */
112 lpm6_config.max_rules = p->n_rules;
113 lpm6_config.number_tbl8s = p->number_tbl8s;
114 lpm6_config.flags = 0;
115 lpm->lpm = rte_lpm6_create("LPM IPv6", socket_id, &lpm6_config);
116 if (lpm->lpm == NULL) {
119 "Unable to create low-level LPM IPv6 table\n");
123 /* Memory initialization */
124 lpm->entry_size = entry_size;
125 lpm->entry_unique_size = p->entry_unique_size;
126 lpm->n_rules = p->n_rules;
127 lpm->offset = p->offset;
133 rte_table_lpm_ipv6_free(void *table)
135 struct rte_table_lpm_ipv6 *lpm = (struct rte_table_lpm_ipv6 *) table;
137 /* Check input parameters */
139 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
143 /* Free previously allocated resources */
144 rte_lpm6_free(lpm->lpm);
151 nht_find_free(struct rte_table_lpm_ipv6 *lpm, uint32_t *pos)
155 for (i = 0; i < RTE_TABLE_LPM_MAX_NEXT_HOPS; i++) {
156 if (lpm->nht_users[i] == 0) {
166 nht_find_existing(struct rte_table_lpm_ipv6 *lpm, void *entry, uint32_t *pos)
170 for (i = 0; i < RTE_TABLE_LPM_MAX_NEXT_HOPS; i++) {
171 uint8_t *nht_entry = &lpm->nht[i * lpm->entry_size];
173 if ((lpm->nht_users[i] > 0) && (memcmp(nht_entry, entry,
174 lpm->entry_unique_size) == 0)) {
184 rte_table_lpm_ipv6_entry_add(
191 struct rte_table_lpm_ipv6 *lpm = (struct rte_table_lpm_ipv6 *) table;
192 struct rte_table_lpm_ipv6_key *ip_prefix =
193 (struct rte_table_lpm_ipv6_key *) key;
194 uint32_t nht_pos, nht_pos0_valid;
198 /* Check input parameters */
200 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
203 if (ip_prefix == NULL) {
204 RTE_LOG(ERR, TABLE, "%s: ip_prefix parameter is NULL\n",
209 RTE_LOG(ERR, TABLE, "%s: entry parameter is NULL\n", __func__);
213 if ((ip_prefix->depth == 0) || (ip_prefix->depth > 128)) {
214 RTE_LOG(ERR, TABLE, "%s: invalid depth (%d)\n", __func__,
219 /* Check if rule is already present in the table */
220 status = rte_lpm6_is_rule_present(lpm->lpm, ip_prefix->ip,
221 ip_prefix->depth, &nht_pos0);
222 nht_pos0_valid = status > 0;
224 /* Find existing or free NHT entry */
225 if (nht_find_existing(lpm, entry, &nht_pos) == 0) {
228 if (nht_find_free(lpm, &nht_pos) == 0) {
229 RTE_LOG(ERR, TABLE, "%s: NHT full\n", __func__);
233 nht_entry = &lpm->nht[nht_pos * lpm->entry_size];
234 memcpy(nht_entry, entry, lpm->entry_size);
237 /* Add rule to low level LPM table */
238 if (rte_lpm6_add(lpm->lpm, ip_prefix->ip, ip_prefix->depth,
239 (uint8_t) nht_pos) < 0) {
240 RTE_LOG(ERR, TABLE, "%s: LPM IPv6 rule add failed\n", __func__);
244 /* Commit NHT changes */
245 lpm->nht_users[nht_pos]++;
246 lpm->nht_users[nht_pos0] -= nht_pos0_valid;
248 *key_found = nht_pos0_valid;
249 *entry_ptr = (void *) &lpm->nht[nht_pos * lpm->entry_size];
254 rte_table_lpm_ipv6_entry_delete(
260 struct rte_table_lpm_ipv6 *lpm = (struct rte_table_lpm_ipv6 *) table;
261 struct rte_table_lpm_ipv6_key *ip_prefix =
262 (struct rte_table_lpm_ipv6_key *) key;
266 /* Check input parameters */
268 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
271 if (ip_prefix == NULL) {
272 RTE_LOG(ERR, TABLE, "%s: ip_prefix parameter is NULL\n",
276 if ((ip_prefix->depth == 0) || (ip_prefix->depth > 128)) {
277 RTE_LOG(ERR, TABLE, "%s: invalid depth (%d)\n", __func__,
282 /* Return if rule is not present in the table */
283 status = rte_lpm6_is_rule_present(lpm->lpm, ip_prefix->ip,
284 ip_prefix->depth, &nht_pos);
286 RTE_LOG(ERR, TABLE, "%s: LPM IPv6 algorithmic error\n",
295 /* Delete rule from the low-level LPM table */
296 status = rte_lpm6_delete(lpm->lpm, ip_prefix->ip, ip_prefix->depth);
298 RTE_LOG(ERR, TABLE, "%s: LPM IPv6 rule delete failed\n",
303 /* Commit NHT changes */
304 lpm->nht_users[nht_pos]--;
308 memcpy(entry, &lpm->nht[nht_pos * lpm->entry_size],
315 rte_table_lpm_ipv6_lookup(
317 struct rte_mbuf **pkts,
319 uint64_t *lookup_hit_mask,
322 struct rte_table_lpm_ipv6 *lpm = (struct rte_table_lpm_ipv6 *) table;
323 uint64_t pkts_out_mask = 0;
327 for (i = 0; i < (uint32_t)(RTE_PORT_IN_BURST_SIZE_MAX -
328 __builtin_clzll(pkts_mask)); i++) {
329 uint64_t pkt_mask = 1LLU << i;
331 if (pkt_mask & pkts_mask) {
332 struct rte_mbuf *pkt = pkts[i];
333 uint8_t *ip = RTE_MBUF_METADATA_UINT8_PTR(pkt,
338 status = rte_lpm6_lookup(lpm->lpm, ip, &nht_pos);
340 pkts_out_mask |= pkt_mask;
341 entries[i] = (void *) &lpm->nht[nht_pos *
347 *lookup_hit_mask = pkts_out_mask;
352 struct rte_table_ops rte_table_lpm_ipv6_ops = {
353 .f_create = rte_table_lpm_ipv6_create,
354 .f_free = rte_table_lpm_ipv6_free,
355 .f_add = rte_table_lpm_ipv6_entry_add,
356 .f_delete = rte_table_lpm_ipv6_entry_delete,
357 .f_lookup = rte_table_lpm_ipv6_lookup,