Skip to contentSkip to conversation

src: restore interface to index cache

PATCH In review Pablo Neira Ayuso 2 participants 2 messages Apr 11, 2015 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH v4 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.

In the interactive mode, the first call to nft_if_nametoindex() and nft_if_indextoname() updates the cache, then follow up calls will use what they find in the cache. By when the command is complete, the cache is invalidated.

---
v4: Another spin on the interactive mode, see changelog above for details.

 include/Makefile.am |    1 +
 include/iface.h     |   16 ++++++
 include/nftables.h  |    1 +
 src/Makefile.am     |    1 +
 src/cli.c           |    3 ++
 src/iface.c         |  144 +++++++++++++++++++++++++++++++++++++++++++++++++++
 src/main.c          |    7 ++-
 src/meta.c          |    5 +-
 8 files changed, 175 insertions(+), 3 deletions(-)
 create mode 100644 include/iface.h
 create mode 100644 src/iface.c
include/Makefile.am +1 -0
index f22561b..465d804 100644
@@ -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
new file mode 100644
index 0000000..ecfcc09
@@ -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);
+char *nft_if_indextoname(unsigned int ifindex, char *name);
+
+void iface_cache_update(void);
+void iface_cache_release(void);
+
+#endif
include/nftables.h +1 -0
index cf19de8..aa8d219 100644
@@ -29,6 +29,7 @@ extern unsigned int numeric_output;
 extern unsigned int ip2name_output;
 extern unsigned int handle_output;
 extern unsigned int debug_level;
+extern bool interactive;
 extern const char *include_paths[INCLUDE_PATHS_MAX];
 
 enum nftables_exit_codes {
src/Makefile.am +1 -0
index 2410fd3..fd63219 100644
@@ -44,6 +44,7 @@ nft_SOURCES =	main.c				\
 		utils.c				\
 		erec.c				\
 		mnl.c				\
+		iface.c				\
 		scanner.l			\
 		parser_bison.y
 
src/cli.c +3 -0
index f3827b8..d54d85f 100644
@@ -21,6 +21,7 @@
 #include <string.h>
 #include <ctype.h>
 #include <limits.h>
+#include <net/if.h>
 #include <readline/readline.h>
 #include <readline/history.h>
 
@@ -28,6 +29,7 @@
 #include <parser.h>
 #include <erec.h>
 #include <utils.h>
+#include <iface.h>
 #include <cli.h>
 
 #define CMDLINE_HISTFILE	".nft.history"
@@ -123,6 +125,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 +144 -0

144 lines not on this page · raw message

src/main.c +6 -1
index 4590c30..8f51b4a 100644
@@ -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;
@@ -34,6 +36,7 @@ unsigned int handle_output;
 #ifdef DEBUG
 unsigned int debug_level;
 #endif
+bool interactive;
 
 const char *include_paths[INCLUDE_PATHS_MAX] = { DEFAULT_INCLUDE_PATH };
 static unsigned int num_include_paths = 1;
@@ -253,7 +256,6 @@ int main(int argc, char * const *argv)
 	LIST_HEAD(msgs);
 	char *buf = NULL, *filename = NULL;
 	unsigned int len;
-	bool interactive = false;
 	int i, val, rc = NFT_EXIT_SUCCESS;
 
 	while (1) {
@@ -357,8 +359,11 @@ int main(int argc, char * const *argv)
 		exit(NFT_EXIT_FAILURE);
 	}
 
+	iface_cache_update();
 	if (nft_run(scanner, &state, &msgs) != 0)
 		rc = NFT_EXIT_FAILURE;
+
+	iface_cache_release();
 out:
 	scanner_destroy(scanner);
 	erec_print_list(stderr, &msgs);
src/meta.c +3 -2
index ad57228..bfc1258 100644
@@ -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
#1 lore raw
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() 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.

In the interactive mode, the first call to nft_if_nametoindex() and nft_if_indextoname() updates the cache, then follow up calls will use what they find in the cache. By when the command is complete, the cache is invalidated.

Not sure if this is based on my suggestion. It makes sense of course, but what I meant was:

src/iface.c line 121

Quoted 6 lines
src/iface.c new file +6 -0
+unsigned int nft_if_nametoindex(const char *name)
+{
+	struct iface *iface;
+
+	if (interactive && !iface_cache_init)
+		iface_cache_update();

To only update the cache if it is actually needed, so update here always if !iface_cache_init (independant of interactive) and remove the unconditional update from main(). Basically the only difference would be that non-interactive mode does no invalidation.

There are many cases where interfaces are not used, like listing rules without ifindex use, anything related to sets, chains, ..., so we can avoid filling the cache if it is not needed.

Besides that looks very good.