summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStanislav Zaikin <zstaseg@gmail.com>2023-07-27 09:11:07 (GMT)
committerThomas Haller <thaller@redhat.com>2023-07-31 13:11:28 (GMT)
commitbeba5a189cf0032b2edc86fd12a5591809025f4f (patch)
tree19453d95ecc2c3e4a15fe2dd09101d0a67fa1920
parent780d06ae8bee5b2a8b3072a215de71929363e8d4 (diff)
downloadlibnl-beba5a189cf0032b2edc86fd12a5591809025f4f.zip
libnl-beba5a189cf0032b2edc86fd12a5591809025f4f.tar.gz
libnl-beba5a189cf0032b2edc86fd12a5591809025f4f.tar.bz2
cli: add nl-nh-list utility
-rw-r--r--.gitignore1
-rw-r--r--Makefile.am5
-rw-r--r--include/netlink/cli/nh.h30
-rw-r--r--libnl-cli-3.sym6
-rw-r--r--src/lib/nh.c61
-rw-r--r--src/nl-nh-list.c75
6 files changed, 178 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index 1597957..e3761a5 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 b2796b6..b0e47ce 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -213,6 +213,7 @@ libnlinclude_netlink_cli_HEADERS = \
include/netlink/cli/link.h \
include/netlink/cli/mdb.h \
include/netlink/cli/neigh.h \
+ include/netlink/cli/nh.h \
include/netlink/cli/qdisc.h \
include/netlink/cli/route.h \
include/netlink/cli/rule.h \
@@ -634,6 +635,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 +719,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 +819,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 0000000..ee08c8e
--- /dev/null
+++ b/include/netlink/cli/nh.h
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: LGPL-2.1-only */
+/*
+ * Copyright (c) 2022 Stanislav Zaikin <zstaseg@gmail.com>
+ */
+
+#ifndef __NETLINK_CLI_NH_H_
+#define __NETLINK_CLI_NH_H_
+
+#include <netlink/route/nh.h>
+#include <netlink/cli/utils.h>
+
+#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 71523ae..75f03cd 100644
--- a/libnl-cli-3.sym
+++ b/libnl-cli-3.sym
@@ -121,3 +121,9 @@ 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 0000000..7f9722b
--- /dev/null
+++ b/src/lib/nh.c
@@ -0,0 +1,61 @@
+/* SPDX-License-Identifier: LGPL-2.1-only */
+/*
+ * Copyright (c) 2022 Stanislav Zaikin <zstaseg@gmail.com>
+ */
+
+/**
+ * @ingroup cli
+ * @defgroup cli_nh nhs
+ *
+ * @{
+ */
+
+#include <netlink/cli/utils.h>
+#include <netlink/cli/nh.h>
+#include <netlink/route/nh.h>
+#include <linux/if.h>
+
+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 0000000..17568df
--- /dev/null
+++ b/src/nl-nh-list.c
@@ -0,0 +1,75 @@
+/* SPDX-License-Identifier: LGPL-2.1-only */
+/*
+ * Copyright (c) 2022 Stanislav Zaikin <zstaseg@gmail.com>
+ */
+
+#include <netlink/cli/utils.h>
+#include <netlink/cli/nh.h>
+
+#include <netlink/route/nh.h>
+
+#include <linux/netlink.h>
+
+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, &params);
+
+ return 0;
+}