src: restore interface to index cache
Pablo Neira Ayuso
maintainerPATCH v2 1/1 src: restore interface to index cache
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.
For the interactive mode, the cache is refreshed for each command to keep it current in a poor man fashion. We can revisit this later to use events instead.
Signed-off-by Pablo Neira Ayuso <[email protected]>
--- include/Makefile.am | 1 + include/iface.h | 16 +++++++ src/Makefile.am | 1 + src/iface.c | 133 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.c | 8 +++- src/meta.c | 5 +- 6 files changed, 161 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 +16 -0
@@ -0,0 +1,16 @@ +#ifndef _NFTABLES_IFACE_H_ +#define _NFTABLES_IFACE_H_ + +struct iface { +struct list_head list; +char name[IFNAMSIZ ]; +uint32_t ifindex; +}; + +unsigned int nft_if_nametoindex (const char *name); +int 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/iface.c new file +133 -0
133 lines not on this page · raw message
src/main.c +7 -1
@@ -17,6 +17,7 @@#include <getopt.h> #include <fcntl.h> #include <sys/types.h> +#include <net/if.h> #include <nftables.h> #include <utils.h>
@@ -25,6 +26,7 @@#include <netlink.h> #include <erec.h> #include <mnl.h> +#include <iface.h> #include <cli.h> unsigned int max_errors =10 ;
@@ -228,9 +230,12 @@ int nft_run(void *scanner, struct parser_state *state, struct list_head *msgs)struct cmd *cmd, *next;int ret; +iface_cache_update (); ret =nft_parse (scanner, state); -if (ret !=0 || state->nerrs >0 ) +if (ret !=0 || state->nerrs >0 ) { +iface_cache_release ();return -1 ; + } retry: ret =nft_netlink (state, msgs);if (ret <0 && errno ==EINTR ) {
@@ -242,6 +247,7 @@ retry:list_del (&cmd->list);cmd_free (cmd); } +iface_cache_release ();return ret; }
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