Skip to contentSkip to conversation

src: restore interface to index cache

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

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

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

For the interactive mode, we fall back on if_nametoindex() and if_indextoname() to make sure that we always get fresh interface name to index mappings.

---
v3: Fall back to if_nametoindex() and if_indextoname() in interactive mode.

 include/Makefile.am |    1 +
 include/iface.h     |   16 ++++++
 include/nftables.h  |    1 +
 src/Makefile.am     |    1 +
 src/iface.c         |  140 +++++++++++++++++++++++++++++++++++++++++++++++++++
 src/main.c          |    7 ++-
 src/meta.c          |    5 +-
 7 files changed, 168 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/iface.c new file +140 -0

140 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 10 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.

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.

Quoted 6 lines

For the interactive mode, we fall back on if_nametoindex() and if_indextoname() to make sure that we always get fresh interface name to index mappings.

---
v3: Fall back to if_nametoindex() and if_indextoname() in interactive mode.

That seems like a good way. One more suggestions - how about only doing a cache fill on the first invocation? That way we can avoid it in many cases, f.i. set listings, some times for rulesets as well, flushing, ...