Skip to content

Commit

Permalink
cli: add nl-nh-list utility
Browse files Browse the repository at this point in the history
  • Loading branch information
zstas committed Jul 27, 2023
1 parent 3f133f9 commit b648fd2
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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 \
Expand Down Expand Up @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions include/netlink/cli/nh.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* SPDX-License-Identifier: LGPL-2.1-only */
/*
* Copyright (c) 2022 Stanislav Zaikin <[email protected]>
*/

#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
7 changes: 7 additions & 0 deletions libnl-cli-3.sym
Original file line number Diff line number Diff line change
Expand Up @@ -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;

61 changes: 61 additions & 0 deletions src/lib/nh.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* SPDX-License-Identifier: LGPL-2.1-only */
/*
* Copyright (c) 2022 Stanislav Zaikin <[email protected]>
*/

/**
* @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);
}

/** @} */
72 changes: 72 additions & 0 deletions src/nl-nh-list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* SPDX-License-Identifier: LGPL-2.1-only */
/*
* Copyright (c) 2003-2010 Thomas Graf <[email protected]>
*/

#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;
}

0 comments on commit b648fd2

Please sign in to comment.