src: restore interface to index cache
Pablo Neira Ayuso
maintainerPATCH 1/1 src: restore interface to index cache
From: Pablo Neira <[email protected]>
nftables used to have a cache to speed up interface name <-> index lookup, restore it using libmnl.
This reduces netlink traffic since if_nametoindex() and if_indextoname() open, send a request, receive the list of interface and close a netlink socket for each call. I think this is also good for consistency since nft -f will operate with the same index number when reloading the ruleset.
The cache is populated by when nft_if_nametoindex() and nft_if_indextoname() are used for first time. Then, it it released in the output path. In the interactive mode, it is invalidated after each command.
--- include/Makefile.am | 1 + include/iface.h | 18 +++++++ src/Makefile.am | 1 + src/cli.c | 2 + src/iface.c | 147 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.c | 4 +- src/meta.c | 5 +- 7 files changed, 175 insertions(+), 3 deletions(-) create mode 100644 include/iface.h create mode 100644 src/iface.c
include/Makefile.am +1 -0
@@ -4,6 +4,7 @@ noinst_HEADERS = cli.h \ datatype.h \ expression.h \ gmputil.h \ + iface.h \ mnl.h \ nftables.h \ payload.h \
include/iface.h new file +18 -0
@@ -0,0 +1,18 @@ +#ifndef _NFTABLES_IFACE_H_ +#define _NFTABLES_IFACE_H_ + +#include <net/if.h> + +struct iface { +struct list_head list; +char name[IFNAMSIZ ]; +uint32_t ifindex; +}; + +unsigned int nft_if_nametoindex (const char *name); +char *nft_if_indextoname (unsigned int ifindex,char *name); + +void iface_cache_update (void ); +void iface_cache_release (void ); + +#endif
src/Makefile.am +1 -0
@@ -44,6 +44,7 @@ nft_SOURCES = main.c \ utils.c \ erec.c \ mnl.c \ + iface.c \ scanner.l \ parser_bison.y
src/cli.c +2 -0
@@ -28,6 +28,7 @@#include <parser.h> #include <erec.h> #include <utils.h> +#include <iface.h> #include <cli.h> #define CMDLINE_HISTFILE ".nft.history"
@@ -123,6 +124,7 @@ static void cli_complete(char *line)nft_run (scanner, state, &msgs);erec_print_list (stdout, &msgs);xfree (line); +iface_cache_release (); }static char **cli_completion (const char *text,int start,int end)
src/iface.c new file +147 -0
147 lines not on this page · raw message
src/main.c +3 -1
@@ -25,6 +25,7 @@#include <netlink.h> #include <erec.h> #include <mnl.h> +#include <iface.h> #include <cli.h> unsigned int max_errors =10 ;
@@ -362,7 +363,8 @@ int main(int argc, char * const *argv) out:scanner_destroy (scanner);erec_print_list (stderr, &msgs); -xfree (buf); +iface_cache_release (); +return rc; }
src/meta.c +3 -2
@@ -30,6 +30,7 @@#include <gmputil.h> #include <utils.h> #include <erec.h> +#include <iface.h> static struct symbol_table *realm_tbl;static void __init realm_table_init (void )
@@ -138,7 +139,7 @@ static void ifindex_type_print(const struct expr *expr)int ifindex; ifindex =mpz_get_uint32 (expr->value); -if (if_indextoname (ifindex, name)) +if (nft_if_indextoname (ifindex, name))printf ("%s" , name);else printf ("%d" , ifindex);
@@ -149,7 +150,7 @@ static struct error_record *ifindex_type_parse(const struct expr *sym, {int ifindex; - ifindex =if_nametoindex (sym->identifier); + ifindex =nft_if_nametoindex (sym->identifier);if (ifindex ==0 )return error (&sym->location,"Interface does not exist" );
Signature · 1 line
1.7.10.4
Quoted 5 lines · Pablo Neira Ayuso
From: Pablo Neira <[email protected]>
nftables used to have a cache to speed up interface name <-> index lookup, restore it using libmnl.
Quoted 8 lines
This reduces netlink traffic since
if_nametoindex()andif_indextoname()open, send a request, receive the list of interface and close a netlink socket for each call. I think this is also good for consistency since nft -f will operate with the same index number when reloading the ruleset.The cache is populated by when
nft_if_nametoindex()andnft_if_indextoname()are used for first time. Then, it it released in the output path. In the interactive mode, it is invalidated after each command.
Looks very good, thanks for your patience :)