From b648fd23e07e9b347b160d90e27611d9a485e917 Mon Sep 17 00:00:00 2001 From: Stanislav Zaikin Date: Thu, 27 Jul 2023 11:11:07 +0200 Subject: [PATCH] cli: add nl-nh-list utility --- .gitignore | 1 + Makefile.am | 4 +++ include/netlink/cli/nh.h | 30 +++++++++++++++++ libnl-cli-3.sym | 7 ++++ src/lib/nh.c | 61 ++++++++++++++++++++++++++++++++++ src/nl-nh-list.c | 72 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 175 insertions(+) create mode 100644 include/netlink/cli/nh.h create mode 100644 src/lib/nh.c create mode 100644 src/nl-nh-list.c diff --git a/.gitignore b/.gitignore index 15979570b..e3761a517 100644 --- a/.gitignore +++ b/.gitignore @@ -97,6 +97,7 @@ test-suite.log /src/nl-neigh-delete /src/nl-neigh-list /src/nl-neightbl-list +/src/nl-nh-list /src/nl-pktloc-lookup /src/nl-qdisc-add /src/nl-qdisc-delete diff --git a/Makefile.am b/Makefile.am index b2796b695..bdf83635a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -634,6 +634,7 @@ src_lib_libnl_cli_3_la_SOURCES = \ src/lib/exp.c \ src/lib/link.c \ src/lib/neigh.c \ + src/lib/nh.c \ src/lib/qdisc.c \ src/lib/route.c \ src/lib/rule.c \ @@ -717,6 +718,7 @@ cli_programs = \ src/nl-neigh-delete \ src/nl-neigh-list \ src/nl-neightbl-list \ + src/nl-nh-list \ src/nl-pktloc-lookup \ src/nl-qdisc-add \ src/nl-qdisc-delete \ @@ -816,6 +818,8 @@ src_nl_neigh_list_CPPFLAGS = $(src_cppflags) src_nl_neigh_list_LDADD = $(src_ldadd) src_nl_neightbl_list_CPPFLAGS = $(src_cppflags) src_nl_neightbl_list_LDADD = $(src_ldadd) +src_nl_nh_list_CPPFLAGS = $(src_cppflags) +src_nl_nh_list_LDADD = $(src_ldadd) src_nl_pktloc_lookup_CPPFLAGS = $(src_cppflags) src_nl_pktloc_lookup_LDADD = $(src_ldadd) src_nl_qdisc_add_CPPFLAGS = $(src_cppflags) diff --git a/include/netlink/cli/nh.h b/include/netlink/cli/nh.h new file mode 100644 index 000000000..ba5f11940 --- /dev/null +++ b/include/netlink/cli/nh.h @@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: LGPL-2.1-only */ +/* + * Copyright (c) 2022 Stanislav Zaikin + */ + +#ifndef __NETLINK_CLI_NH_H_ +#define __NETLINK_CLI_NH_H_ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +extern struct rtnl_nh *nl_cli_nh_alloc(void); +extern struct nl_cache *nl_cli_nh_alloc_cache_family(struct nl_sock *, int); +extern struct nl_cache *nl_cli_nh_alloc_cache_family_flags(struct nl_sock *, int, + unsigned int); +extern struct nl_cache *nl_cli_nh_alloc_cache(struct nl_sock *); +extern struct nl_cache *nl_cli_nh_alloc_cache_flags(struct nl_sock *, + unsigned int); + +extern void nl_cli_nh_parse_family(struct rtnl_nh *, char *); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/libnl-cli-3.sym b/libnl-cli-3.sym index 71523aeb0..329a2a398 100644 --- a/libnl-cli-3.sym +++ b/libnl-cli-3.sym @@ -121,3 +121,10 @@ global: nl_cli_link_alloc_cache_flags; nl_cli_link_alloc_cache_family_flags; } libnl_3; + +libnl_3_8 { +global: + nl_cli_nh_alloc; + nl_cli_nh_alloc_cache; +} libnl_3_2_28; + diff --git a/src/lib/nh.c b/src/lib/nh.c new file mode 100644 index 000000000..a543a7936 --- /dev/null +++ b/src/lib/nh.c @@ -0,0 +1,61 @@ +/* SPDX-License-Identifier: LGPL-2.1-only */ +/* + * Copyright (c) 2022 Stanislav Zaikin + */ + +/** + * @ingroup cli + * @defgroup cli_nh nhs + * + * @{ + */ + +#include +#include +#include +#include + +struct rtnl_nh *nl_cli_nh_alloc(void) +{ + struct rtnl_nh *nh; + + nh = rtnl_nh_alloc(); + if (!nh) + nl_cli_fatal(ENOMEM, "Unable to allocate nh object"); + + return nh; +} + +struct nl_cache *nl_cli_nh_alloc_cache_family_flags(struct nl_sock *sock, + int family, + unsigned int flags) +{ + struct nl_cache *cache; + int err; + + if ((err = rtnl_nh_alloc_cache(sock, family, &cache)) < 0) + nl_cli_fatal(err, "Unable to allocate nh cache: %s", + nl_geterror(err)); + + nl_cache_mngt_provide(cache); + + return cache; +} + +struct nl_cache *nl_cli_nh_alloc_cache_family(struct nl_sock *sock, int family) +{ + return nl_cli_nh_alloc_cache_family_flags(sock, family, 0); +} + +struct nl_cache *nl_cli_nh_alloc_cache(struct nl_sock *sock) +{ + return nl_cli_nh_alloc_cache_family(sock, AF_UNSPEC); +} + +struct nl_cache *nl_cli_nh_alloc_cache_flags(struct nl_sock *sock, + unsigned int flags) +{ + return nl_cli_nh_alloc_cache_family_flags(sock, AF_UNSPEC, flags); +} + +/** @} */ diff --git a/src/nl-nh-list.c b/src/nl-nh-list.c new file mode 100644 index 000000000..5fcebed54 --- /dev/null +++ b/src/nl-nh-list.c @@ -0,0 +1,72 @@ +/* SPDX-License-Identifier: LGPL-2.1-only */ +/* + * Copyright (c) 2003-2010 Thomas Graf + */ + +#include +#include + +#include + +#include + +static void print_usage(void) +{ + printf( +"Usage: nl-nh-list [OPTIONS]... \n" +"\n" +"OPTIONS\n" +" --details Show detailed information of each link\n" +" -h, --help Show this help text.\n" +" -v, --version Show versioning information.\n" +"\n" +" -n, --name=NAME Name of link\n" +" -i, --index Interface index (unique identifier)\n" +" --family=NAME Link address family\n" + ); + exit(0); +} + +int main(int argc, char *argv[]) +{ + struct nl_sock *sock; + struct nl_cache *link_cache; + struct nl_dump_params params = { + .dp_type = NL_DUMP_LINE, + .dp_fd = stdout, + }; + + sock = nl_cli_alloc_socket(); + nl_cli_connect(sock, NETLINK_ROUTE); + + for (;;) { + int c, optidx = 0; + enum { + ARG_FAMILY = 257, + ARG_DETAILS, + }; + static struct option long_opts[] = { + { "details", 0, 0, ARG_DETAILS }, + { "help", 0, 0, 'h' }, + { "version", 0, 0, 'v' }, + { "name", 1, 0, 'n' }, + { 0, 0, 0, 0 } + }; + + c = getopt_long(argc, argv, "hvn:i:", long_opts, &optidx); + if (c == -1) + break; + + switch (c) { + case ARG_DETAILS: params.dp_type = NL_DUMP_DETAILS; break; + case 'h': print_usage(); break; + case 'v': nl_cli_print_version(); break; + } + } + + link_cache = nl_cli_nh_alloc_cache(sock); + + nl_cache_dump(link_cache, ¶ms); + + return 0; +}