Skip to contentSkip to conversation

route_bench: Filter out all responses.

PATCH In review David Miller 2 participants 3 messages Feb 20, 2011

Subsystems (2)
lore
Share & tools
lore

David Miller

author maintainer
#0 lore raw

PATCH 1/1 route_bench: Filter out all responses.

Install a socket filter to reduce the pure netlink overhead.

Unfortunately the libmnl library does not provide a way to set socket options that are of level other than SOL_NETLINK.

So we hack it by knowing some things about libmnl internals.

---

The knowledge of libmnl internals is unfortunate, but there is currently no other way to do this.

 route_bench.c |   33 +++++++++++++++++++++++++--------
 1 files changed, 25 insertions(+), 8 deletions(-)
route_bench.c +25 -8
index c75ea45..6c6b525 100644
@@ -22,6 +22,17 @@
 #include <linux/if.h>
 #include <linux/if_link.h>
 #include <linux/rtnetlink.h>
+#include <linux/filter.h>
+
+/* XXX Ugly knowledge of internals, but there is currently no way
+ * XXX provided by the libmnl library to set socket options that are
+ * XXX of level other than SOL_NETLINK.   And we need to set one of
+ * XXX level SOL_SOCKET to install the socket filter.
+ */
+struct mnl_socket {
+	int 			fd;
+	struct sockaddr_nl	addr;
+};
 
 static int usage(void)
 {
@@ -139,8 +150,10 @@ static int do_bench(int count, in_addr_t src_addr, in_addr_t dst_addr,
 		    unsigned int mark, unsigned int iif)
 {
 	char send_buf[MNL_SOCKET_BUFFER_SIZE];
-	char recv_buf[MNL_SOCKET_BUFFER_SIZE];
 	unsigned int min, sec, frac, tmp;
+	struct sock_filter insns = { .code = BPF_RET | BPF_K,
+				     .k = 0 };
+	struct sock_fprog filter = { .len = 1, .filter = &insns, };
 	struct bench_state *s = &state;
 	struct timeval start_time;
 	struct timeval end_time;
@@ -148,7 +161,7 @@ static int do_bench(int count, in_addr_t src_addr, in_addr_t dst_addr,
 	struct nlmsghdr *nlh;
 	unsigned int portid;
 	struct rtmsg *rtm;
-	int i;
+	int i, err;
 
 	init_bench_state(s);
 
@@ -165,10 +178,15 @@ static int do_bench(int count, in_addr_t src_addr, in_addr_t dst_addr,
 
 	portid = mnl_socket_get_portid(nl);
 
+	err = setsockopt(nl->fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter));
+	if (err) {
+		perror("setsockopt");
+		return -1;
+	}
+
 	gettimeofday(&start_time, NULL);
 	for (i = 0; i < count; i++) {
 		unsigned int seq;
-		int ret;
 
 		nlh = mnl_nlmsg_put_header(send_buf);
 		nlh->nlmsg_type = RTM_GETROUTE;
@@ -197,11 +215,10 @@ static int do_bench(int count, in_addr_t src_addr, in_addr_t dst_addr,
 			perror("mnl_socket_sendto");
 			return -1;
 		}
-		ret = mnl_socket_recvfrom(nl, recv_buf, sizeof(recv_buf));
-		if (ret < 0) {
-			perror("mnl_sock_recvfrom");
-			return -1;
-		}
+
+		/* No need to do a receive, as the socket filter rejects
+		 * all packets.
+		 */
 
 		advance_bench_state(s);
 	}
Signature · 1 line
1.7.4.1
#1 lore raw
Quoted 61 lines · David Miller

Install a socket filter to reduce the pure netlink overhead.

Unfortunately the libmnl library does not provide a way to set socket options that are of level other than SOL_NETLINK.

So we hack it by knowing some things about libmnl internals.

---

The knowledge of libmnl internals is unfortunate, but there is currently no other way to do this.

 route_bench.c |   33 +++++++++++++++++++++++++--------
 1 files changed, 25 insertions(+), 8 deletions(-)
route_bench.c +15 -2
index c75ea45..6c6b525 100644
@@ -22,6 +22,17 @@
 #include <linux/if.h>
 #include <linux/if_link.h>
 #include <linux/rtnetlink.h>
+#include <linux/filter.h>
+
+/* XXX Ugly knowledge of internals, but there is currently no way
+ * XXX provided by the libmnl library to set socket options that are
+ * XXX of level other than SOL_NETLINK.   And we need to set one of
+ * XXX level SOL_SOCKET to install the socket filter.
+ */
+struct mnl_socket {
+	int 			fd;
+	struct sockaddr_nl	addr;
+};
 
 static int usage(void)
 {
@@ -139,8 +150,10 @@ static int do_bench(int count, in_addr_t src_addr, in_addr_t dst_addr,
 		    unsigned int mark, unsigned int iif)
 {
 	char send_buf[MNL_SOCKET_BUFFER_SIZE];
-	char recv_buf[MNL_SOCKET_BUFFER_SIZE];
 	unsigned int min, sec, frac, tmp;
+	struct sock_filter insns = { .code = BPF_RET | BPF_K,
+				     .k = 0 };
+	struct sock_fprog filter = { .len = 1, .filter = &insns, };
 	struct bench_state *s = &state;
 	struct timeval start_time;
 	struct timeval end_time;
@@ -148,7 +161,7 @@ static int do_bench(int count, in_addr_t src_addr, in_addr_t dst_addr,
 	struct nlmsghdr *nlh;
 	unsigned int portid;
 	struct rtmsg *rtm;
-	int i;
+	int i, err;
 
 	init_bench_state(s);
 

route_bench.c static int do_bench(int count, in_addr_t src_addr, in_addr_t dst_addr, · line 186

Quoted 9 lines
route_bench.c +6 -0
@@ -165,10 +178,15 @@ static int do_bench(int count, in_addr_t src_addr, in_addr_t dst_addr,
 
 	portid = mnl_socket_get_portid(nl);
 
+	err = setsockopt(nl->fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter));
+	if (err) {
+		perror("setsockopt");
+		return -1;
+	}
+

You can use:

extern int mnl_socket_get_fd(const struct mnl_socket *nl);

Thus, you can invoke setsockopt directly ;-).

author maintainer
#2 lore raw

From: Pablo Neira Ayuso <[email protected]>
Date: Sun, 20 Feb 2011 23:28:15 +0100

Quoted 5 lines

You can use:

extern int mnl_socket_get_fd(const struct mnl_socket *nl);

Thus, you can invoke setsockopt directly ;-).

Thank you, I didn't notice that interface.

I'll check in the following.

--------------------
route_bench: Remove knowledge of libmnl internals, not needed.

Since mnl_socket_get_fd() exists, we can use that.

Thanks to Pablo.

---
 route_bench.c |   13 ++-----------
 1 files changed, 2 insertions(+), 11 deletions(-)
route_bench.c +2 -11
index 6c6b525..063b15e 100644
@@ -24,16 +24,6 @@
 #include <linux/rtnetlink.h>
 #include <linux/filter.h>
 
-/* XXX Ugly knowledge of internals, but there is currently no way
- * XXX provided by the libmnl library to set socket options that are
- * XXX of level other than SOL_NETLINK.   And we need to set one of
- * XXX level SOL_SOCKET to install the socket filter.
- */
-struct mnl_socket {
-	int 			fd;
-	struct sockaddr_nl	addr;
-};
-
 static int usage(void)
 {
 	printf("usage: route_bench [ -o ] [ -l count ]\n");
@@ -178,7 +168,8 @@ static int do_bench(int count, in_addr_t src_addr, in_addr_t dst_addr,
 
 	portid = mnl_socket_get_portid(nl);
 
-	err = setsockopt(nl->fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter));
+	err = setsockopt(mnl_socket_get_fd(nl), SOL_SOCKET,
+			 SO_ATTACH_FILTER, &filter, sizeof(filter));
 	if (err) {
 		perror("setsockopt");
 		return -1;
Signature · 1 line
1.7.4.1