From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail1.windriver.com (mail1.windriver.com [147.11.146.13]) by dpdk.org (Postfix) with ESMTP id 4E795379B for ; Fri, 25 Nov 2016 07:31:35 +0100 (CET) Received: from ALA-HCB.corp.ad.wrs.com (ala-hcb.corp.ad.wrs.com [147.11.189.41]) by mail1.windriver.com (8.15.2/8.15.1) with ESMTPS id uAP6VXh3014550 (version=TLSv1 cipher=AES128-SHA bits=128 verify=FAIL) for ; Thu, 24 Nov 2016 22:31:34 -0800 (PST) Received: from pek-lpggp3.wrs.com (128.224.153.76) by ALA-HCB.corp.ad.wrs.com (147.11.189.41) with Microsoft SMTP Server id 14.3.294.0; Thu, 24 Nov 2016 22:31:33 -0800 From: chunguang yang To: CC: , Date: Fri, 25 Nov 2016 01:31:31 -0500 Message-ID: <1480055491-137021-1-git-send-email-chunguang.yang@windriver.com> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit Subject: [dpdk-dev] [PATCH] lpm: rte_lpm_iterate() - iterate through the routes X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches and discussions about DPDK List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 25 Nov 2016 06:31:36 -0000 From: Jörgen Grahn In practice, there's a need to iterate through the entries of a rte_lpm, apart from the usual insert/delete/lookup operations. This is useful for debugging and perhaps for things like removing all entries referencing a certain nexthop. This patch implements this through rte_lpm_iterate(), which uses a cursor (or iterator) to keep track of the current position. Client code doesn't need to be aware of rte_lpm implementation details. Change-Id: I28ea3d7d92f318988444553ee2bb30b709bcb3b6 Signed-off-by: Jorgen Grahn Signed-off-by: alloc --- lib/librte_lpm/Makefile | 4 +- lib/librte_lpm/rte_lpm_iterate.c | 81 ++++++++++++++++++++++++++++++++++++++++ lib/librte_lpm/rte_lpm_iterate.h | 56 +++++++++++++++++++++++++++ 3 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 lib/librte_lpm/rte_lpm_iterate.c create mode 100644 lib/librte_lpm/rte_lpm_iterate.h diff --git a/lib/librte_lpm/Makefile b/lib/librte_lpm/Makefile index 3dc549d..c45da19 100644 --- a/lib/librte_lpm/Makefile +++ b/lib/librte_lpm/Makefile @@ -42,10 +42,10 @@ EXPORT_MAP := rte_lpm_version.map LIBABIVER := 2 # all source are stored in SRCS-y -SRCS-$(CONFIG_RTE_LIBRTE_LPM) := rte_lpm.c rte_lpm6.c +SRCS-$(CONFIG_RTE_LIBRTE_LPM) := rte_lpm.c rte_lpm6.c rte_lpm_iterate.c # install this header file -SYMLINK-$(CONFIG_RTE_LIBRTE_LPM)-include := rte_lpm.h rte_lpm6.h +SYMLINK-$(CONFIG_RTE_LIBRTE_LPM)-include := rte_lpm.h rte_lpm6.h rte_lpm_iterate.h ifneq ($(filter y,$(CONFIG_RTE_ARCH_ARM) $(CONFIG_RTE_ARCH_ARM64)),) SYMLINK-$(CONFIG_RTE_LIBRTE_LPM)-include += rte_lpm_neon.h diff --git a/lib/librte_lpm/rte_lpm_iterate.c b/lib/librte_lpm/rte_lpm_iterate.c new file mode 100644 index 0000000..f643764 --- /dev/null +++ b/lib/librte_lpm/rte_lpm_iterate.c @@ -0,0 +1,81 @@ +/*- + * BSD LICENSE + * + * Copyright(c) 2014 Jörgen Grahn. All rights reserved. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include "rte_lpm_iterate.h" +#include "rte_lpm.h" + +#include + + +/** + * Iterate through the lpm, pulling out at most 'buflen' valid routes + * (less means we've hit the end). The cursor should be initialized + * to { 0, 0 } before the first call. + * + * The routes are partially sorted, by prefix length. Undefined + * results if the lpm is modified in parallel with or inbetween calls, + * although the iteration will still terminate properly. + */ +unsigned +rte_lpm_iterate(struct rte_lpm_route* const buf, unsigned buflen, + const struct rte_lpm* lpm, + struct rte_lpm_cursor* const cursor) +{ + struct rte_lpm_route* p = buf; + struct rte_lpm_route* const end = p + buflen; + + const struct rte_lpm_rule_info* const rinfo = lpm->rule_info; + const struct rte_lpm_rule* const rtbl = lpm->rules_tbl; + + unsigned d = cursor->d; + unsigned n = cursor->n; + + while(p!=end) { + if(d==32) break; + if(n>=rinfo[d].used_rules) { + d++; + n = 0; + continue; + } + const struct rte_lpm_rule rule = rtbl[rinfo[d].first_rule + n]; + p->addr.s_addr = htonl(rule.ip); + p->plen = d+1; + p->nh = rule.next_hop; + p++; + n++; + } + + cursor->d = d; + cursor->n = n; + + return p - buf; +} diff --git a/lib/librte_lpm/rte_lpm_iterate.h b/lib/librte_lpm/rte_lpm_iterate.h new file mode 100644 index 0000000..25c7841 --- /dev/null +++ b/lib/librte_lpm/rte_lpm_iterate.h @@ -0,0 +1,56 @@ +/*- + * BSD LICENSE + * + * Copyright(c) 2014 Jörgen Grahn. All rights reserved. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef _RTE_LPM_ITERATE_H_ +#define _RTE_LPM_ITERATE_H_ + +#include +#include + +struct rte_lpm; + +struct rte_lpm_cursor { + unsigned d; + unsigned n; +}; + +struct rte_lpm_route { + struct in_addr addr; + uint8_t plen; + uint8_t nh; +}; + +unsigned rte_lpm_iterate(struct rte_lpm_route* buf, unsigned buflen, + const struct rte_lpm* lpm, + struct rte_lpm_cursor* cursor); + +#endif -- 2.7.4