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",
112 if (p
->name
== NULL
) {
113 RTE_LOG(ERR
, TABLE
, "%s: Table name is NULL\n",
117 entry_size
= RTE_ALIGN(entry_size
, sizeof(uint64_t));
119 /* Memory allocation */
120 nht_size
= RTE_TABLE_LPM_MAX_NEXT_HOPS
* entry_size
;
121 total_size
= sizeof(struct rte_table_lpm_ipv6
) + nht_size
;
122 lpm
= rte_zmalloc_socket("TABLE", total_size
, RTE_CACHE_LINE_SIZE
,
126 "%s: Cannot allocate %u bytes for LPM IPv6 table\n",
127 __func__
, total_size
);
131 /* LPM low-level table creation */
132 lpm6_config
.max_rules
= p
->n_rules
;
133 lpm6_config
.number_tbl8s
= p
->number_tbl8s
;
134 lpm6_config
.flags
= 0;
135 lpm
->lpm
= rte_lpm6_create(p
->name
, socket_id
, &lpm6_config
);
136 if (lpm
->lpm
== NULL
) {
139 "Unable to create low-level LPM IPv6 table\n");
143 /* Memory initialization */
144 lpm
->entry_size
= entry_size
;
145 lpm
->entry_unique_size
= p
->entry_unique_size
;
146 lpm
->n_rules
= p
->n_rules
;
147 lpm
->offset
= p
->offset
;
153 rte_table_lpm_ipv6_free(void *table
)
155 struct rte_table_lpm_ipv6
*lpm
= (struct rte_table_lpm_ipv6
*) table
;
157 /* Check input parameters */
159 RTE_LOG(ERR
, TABLE
, "%s: table parameter is NULL\n", __func__
);
163 /* Free previously allocated resources */
164 rte_lpm6_free(lpm
->lpm
);
171 nht_find_free(struct rte_table_lpm_ipv6
*lpm
, uint32_t *pos
)
175 for (i
= 0; i
< RTE_TABLE_LPM_MAX_NEXT_HOPS
; i
++) {
176 if (lpm
->nht_users
[i
] == 0) {
186 nht_find_existing(struct rte_table_lpm_ipv6
*lpm
, void *entry
, uint32_t *pos
)
190 for (i
= 0; i
< RTE_TABLE_LPM_MAX_NEXT_HOPS
; i
++) {
191 uint8_t *nht_entry
= &lpm
->nht
[i
* lpm
->entry_size
];
193 if ((lpm
->nht_users
[i
] > 0) && (memcmp(nht_entry
, entry
,
194 lpm
->entry_unique_size
) == 0)) {
204 rte_table_lpm_ipv6_entry_add(
211 struct rte_table_lpm_ipv6
*lpm
= (struct rte_table_lpm_ipv6
*) table
;
212 struct rte_table_lpm_ipv6_key
*ip_prefix
=
213 (struct rte_table_lpm_ipv6_key
*) key
;
214 uint32_t nht_pos
, nht_pos0_valid
;
218 /* Check input parameters */
220 RTE_LOG(ERR
, TABLE
, "%s: table parameter is NULL\n", __func__
);
223 if (ip_prefix
== NULL
) {
224 RTE_LOG(ERR
, TABLE
, "%s: ip_prefix parameter is NULL\n",
229 RTE_LOG(ERR
, TABLE
, "%s: entry parameter is NULL\n", __func__
);
233 if ((ip_prefix
->depth
== 0) || (ip_prefix
->depth
> 128)) {
234 RTE_LOG(ERR
, TABLE
, "%s: invalid depth (%d)\n", __func__
,
239 /* Check if rule is already present in the table */
240 status
= rte_lpm6_is_rule_present(lpm
->lpm
, ip_prefix
->ip
,
241 ip_prefix
->depth
, &nht_pos0
);
242 nht_pos0_valid
= status
> 0;
244 /* Find existing or free NHT entry */
245 if (nht_find_existing(lpm
, entry
, &nht_pos
) == 0) {
248 if (nht_find_free(lpm
, &nht_pos
) == 0) {
249 RTE_LOG(ERR
, TABLE
, "%s: NHT full\n", __func__
);
253 nht_entry
= &lpm
->nht
[nht_pos
* lpm
->entry_size
];
254 memcpy(nht_entry
, entry
, lpm
->entry_size
);
257 /* Add rule to low level LPM table */
258 if (rte_lpm6_add(lpm
->lpm
, ip_prefix
->ip
, ip_prefix
->depth
,
259 (uint8_t) nht_pos
) < 0) {
260 RTE_LOG(ERR
, TABLE
, "%s: LPM IPv6 rule add failed\n", __func__
);
264 /* Commit NHT changes */
265 lpm
->nht_users
[nht_pos
]++;
266 lpm
->nht_users
[nht_pos0
] -= nht_pos0_valid
;
268 *key_found
= nht_pos0_valid
;
269 *entry_ptr
= (void *) &lpm
->nht
[nht_pos
* lpm
->entry_size
];
274 rte_table_lpm_ipv6_entry_delete(
280 struct rte_table_lpm_ipv6
*lpm
= (struct rte_table_lpm_ipv6
*) table
;
281 struct rte_table_lpm_ipv6_key
*ip_prefix
=
282 (struct rte_table_lpm_ipv6_key
*) key
;
286 /* Check input parameters */
288 RTE_LOG(ERR
, TABLE
, "%s: table parameter is NULL\n", __func__
);
291 if (ip_prefix
== NULL
) {
292 RTE_LOG(ERR
, TABLE
, "%s: ip_prefix parameter is NULL\n",
296 if ((ip_prefix
->depth
== 0) || (ip_prefix
->depth
> 128)) {
297 RTE_LOG(ERR
, TABLE
, "%s: invalid depth (%d)\n", __func__
,
302 /* Return if rule is not present in the table */
303 status
= rte_lpm6_is_rule_present(lpm
->lpm
, ip_prefix
->ip
,
304 ip_prefix
->depth
, &nht_pos
);
306 RTE_LOG(ERR
, TABLE
, "%s: LPM IPv6 algorithmic error\n",
315 /* Delete rule from the low-level LPM table */
316 status
= rte_lpm6_delete(lpm
->lpm
, ip_prefix
->ip
, ip_prefix
->depth
);
318 RTE_LOG(ERR
, TABLE
, "%s: LPM IPv6 rule delete failed\n",
323 /* Commit NHT changes */
324 lpm
->nht_users
[nht_pos
]--;
328 memcpy(entry
, &lpm
->nht
[nht_pos
* lpm
->entry_size
],
335 rte_table_lpm_ipv6_lookup(
337 struct rte_mbuf
**pkts
,
339 uint64_t *lookup_hit_mask
,
342 struct rte_table_lpm_ipv6
*lpm
= (struct rte_table_lpm_ipv6
*) table
;
343 uint64_t pkts_out_mask
= 0;
346 __rte_unused
uint32_t n_pkts_in
= __builtin_popcountll(pkts_mask
);
347 RTE_TABLE_LPM_IPV6_STATS_PKTS_IN_ADD(lpm
, n_pkts_in
);
350 for (i
= 0; i
< (uint32_t)(RTE_PORT_IN_BURST_SIZE_MAX
-
351 __builtin_clzll(pkts_mask
)); i
++) {
352 uint64_t pkt_mask
= 1LLU << i
;
354 if (pkt_mask
& pkts_mask
) {
355 struct rte_mbuf
*pkt
= pkts
[i
];
356 uint8_t *ip
= RTE_MBUF_METADATA_UINT8_PTR(pkt
,
361 status
= rte_lpm6_lookup(lpm
->lpm
, ip
, &nht_pos
);
363 pkts_out_mask
|= pkt_mask
;
364 entries
[i
] = (void *) &lpm
->nht
[nht_pos
*
370 *lookup_hit_mask
= pkts_out_mask
;
371 RTE_TABLE_LPM_IPV6_STATS_PKTS_LOOKUP_MISS(lpm
, n_pkts_in
- __builtin_popcountll(pkts_out_mask
));
376 rte_table_lpm_ipv6_stats_read(void *table
, struct rte_table_stats
*stats
, int clear
)
378 struct rte_table_lpm_ipv6
*t
= (struct rte_table_lpm_ipv6
*) table
;
381 memcpy(stats
, &t
->stats
, sizeof(t
->stats
));
384 memset(&t
->stats
, 0, sizeof(t
->stats
));
389 struct rte_table_ops rte_table_lpm_ipv6_ops
= {
390 .f_create
= rte_table_lpm_ipv6_create
,
391 .f_free
= rte_table_lpm_ipv6_free
,
392 .f_add
= rte_table_lpm_ipv6_entry_add
,
393 .f_delete
= rte_table_lpm_ipv6_entry_delete
,
395 .f_delete_bulk
= NULL
,
396 .f_lookup
= rte_table_lpm_ipv6_lookup
,
397 .f_stats
= rte_table_lpm_ipv6_stats_read
,