Skip to contentSkip to conversation

Eliminate packet copy when constructing struct pkt_buff

RFC PATCH Superseded Duncan Roe 2 participants 9 messages May 4, 2021 – Aug 4, 2021 netfilter

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Duncan Roe

author
#0 lore raw

cover Eliminate packet copy when constructing struct pkt_buff

Hi Pablo,

This is item 2 of 4 after which I think we could do a new release.

Item 3 is to eliminate packet copy when returning a mangled packet in a verdict. I have this working in inline code, not yet factored into function calls.

Item 4 is to document how to use the library (i.e. using non-deprecated calls). I haven't started it yet.
Perhaps I can get rid of the implicit forced-load of libnfnetlink (only used by deprecated functions).

Cheers ... Duncan.

Duncan Roe (1):
Eliminate packet copy when constructing struct pkt_buff

 examples/nf-queue.c                    | 22 ++++++-
 include/libnetfilter_queue/Makefile.am |  1 +
 include/libnetfilter_queue/callback.h  | 11 ++++
 include/libnetfilter_queue/pktbuff.h   |  2 +
 src/Makefile.am                        |  1 +
 src/extra/callback.c                   | 52 +++++++++++++++++
 src/extra/pktbuff.c                    | 80 ++++++++++++++++++--------
 7 files changed, 141 insertions(+), 28 deletions(-)
 create mode 100644 include/libnetfilter_queue/callback.h
 create mode 100644 src/extra/callback.c
Signature · 1 line
2.17.5
author
#1 lore raw

RFC PATCH 1/1 Eliminate packet copy when constructing struct pkt_buff

To avoid a copy, the new code takes advantage of the fact that the netfilter netlink queue never returns multipart messages.
This means that the buffer space following that callback data is available for packet expansion when mangling.

nfq_cb_run() is a new nfq-specific callback runqueue for netlink messages. The principal function of nfq_cb_run() is to pass to the called function what is the length of free space after the packet.
As a side benefit, nfq_cb_run() also gives the called function a pointer to a zeroised struct pkt_buff, avoiding the malloc / free that was previously needed.

nfq_cb_t is a new typedef for the function called by nfq_cb_run() [c.f. mnl_cb_t / mnl_cb_run].

No doxygen documentation update yet -
will do once function prototypes are agreed.
In the meantime, examples/nf-queue.c is updated to demonstrate the new API.

---
 examples/nf-queue.c                    | 22 ++++++-
 include/libnetfilter_queue/Makefile.am |  1 +
 include/libnetfilter_queue/callback.h  | 11 ++++
 include/libnetfilter_queue/pktbuff.h   |  2 +
 src/Makefile.am                        |  1 +
 src/extra/callback.c                   | 52 +++++++++++++++++
 src/extra/pktbuff.c                    | 80 ++++++++++++++++++--------
 7 files changed, 141 insertions(+), 28 deletions(-)
 create mode 100644 include/libnetfilter_queue/callback.h
 create mode 100644 src/extra/callback.c
examples/nf-queue.c +19 -3
index 3da2c24..15c0d77 100644
@@ -13,6 +13,8 @@
 #include <linux/types.h>
 #include <linux/netfilter/nfnetlink_queue.h>
 
+#include <libnetfilter_queue/pktbuff.h>
+#include <libnetfilter_queue/callback.h>
 #include <libnetfilter_queue/libnetfilter_queue.h>
 
 /* only for NFQA_CT, not needed otherwise: */
@@ -46,13 +48,17 @@ nfq_send_verdict(int queue_num, uint32_t id)
 	}
 }
 
-static int queue_cb(const struct nlmsghdr *nlh, void *data)
+static int queue_cb(const struct nlmsghdr *nlh, void *data,
+		    struct pkt_buff *supplied_pktb, size_t supplied_extra)
 {
 	struct nfqnl_msg_packet_hdr *ph = NULL;
 	struct nlattr *attr[NFQA_MAX+1] = {};
+	struct pkt_buff *pktb;
 	uint32_t id = 0, skbinfo;
 	struct nfgenmsg *nfg;
+	uint8_t *payload;
 	uint16_t plen;
+	int i;
 
 	if (nfq_nlmsg_parse(nlh, attr) < 0) {
 		perror("problems parsing");
@@ -69,7 +75,10 @@ static int queue_cb(const struct nlmsghdr *nlh, void *data)
 	ph = mnl_attr_get_payload(attr[NFQA_PACKET_HDR]);
 
 	plen = mnl_attr_get_payload_len(attr[NFQA_PAYLOAD]);
-	/* void *payload = mnl_attr_get_payload(attr[NFQA_PAYLOAD]); */
+	payload = mnl_attr_get_payload(attr[NFQA_PAYLOAD]);
+
+	pktb = pktb_populate(supplied_pktb, AF_INET, payload, plen,
+			     supplied_extra);
 
 	skbinfo = attr[NFQA_SKB_INFO] ? ntohl(mnl_attr_get_u32(attr[NFQA_SKB_INFO])) : 0;
 
@@ -97,6 +106,13 @@ static int queue_cb(const struct nlmsghdr *nlh, void *data)
 		printf(", checksum not ready");
 	puts(")");
 
+	printf("payload (len=%d) [", plen);
+
+	for (i = 0; i < pktb_len(pktb); i++)
+		printf("%02x", payload[i]);
+
+	printf("]\n");
+
 	nfq_send_verdict(ntohs(nfg->res_id), id);
 
 	return MNL_CB_OK;
@@ -168,7 +184,7 @@ int main(int argc, char *argv[])
 			exit(EXIT_FAILURE);
 		}
 
-		ret = mnl_cb_run(buf, ret, 0, portid, queue_cb, NULL);
+		ret = nfq_cb_run(buf, ret, sizeof_buf, portid, queue_cb, NULL);
 		if (ret < 0){
 			perror("mnl_cb_run");
 			exit(EXIT_FAILURE);
include/libnetfilter_queue/Makefile.am +1 -0
index e436bab..ed6524b 100644
@@ -5,4 +5,5 @@ pkginclude_HEADERS = libnetfilter_queue.h	\
 		     libnetfilter_queue_ipv6.h	\
 		     libnetfilter_queue_tcp.h	\
 		     libnetfilter_queue_udp.h	\
+		     callback.h			\
 		     pktbuff.h
include/libnetfilter_queue/callback.h new file +11 -0
new file mode 100644
index 0000000..27bfe31
@@ -0,0 +1,11 @@
+#ifndef _LIBNETFILTER_QUEUE_CALLBACK_H_
+#define _LIBNETFILTER_QUEUE_CALLBACK_H_
+
+struct nlattr;
+struct pkt_buff;
+
+typedef int (*nfq_cb_t)(const struct nlmsghdr *nlh, void *data, struct pkt_buff *pktb, size_t extra);
+
+int nfq_cb_run(const void *buf, size_t buflen, size_t bufcap, unsigned int portid, nfq_cb_t cb_data, void *data);
+
+#endif
include/libnetfilter_queue/pktbuff.h +2 -0
index 42bc153..33829cc 100644
@@ -6,6 +6,8 @@ struct pkt_buff;
 struct pkt_buff *pktb_alloc(int family, void *data, size_t len, size_t extra);
 void pktb_free(struct pkt_buff *pktb);
 
+struct pkt_buff *pktb_populate(struct pkt_buff *pktb, int family, void *data, size_t len, size_t extra);
+
 uint8_t *pktb_data(struct pkt_buff *pktb);
 uint32_t pktb_len(struct pkt_buff *pktb);
 
src/Makefile.am +1 -0
index 079853e..376bc45 100644
@@ -31,6 +31,7 @@ libnetfilter_queue_la_LDFLAGS = -Wc,-nostartfiles \
 libnetfilter_queue_la_SOURCES = libnetfilter_queue.c	\
 				nlmsg.c			\
 				extra/checksum.c	\
+				extra/callback.c	\
 				extra/icmp.c		\
 				extra/ipv6.c		\
 				extra/tcp.c		\
src/extra/callback.c new file +52 -0

52 lines not on this page · raw message

src/extra/pktbuff.c +55 -25

110 lines not on this page · raw message

Signature · 1 line
2.17.5
author
#2 lore raw

v2 cover Speed-up

Hi Pablo,

The RFC didn't elicit any comments in 2 weeks so I guess the new function prototypes are acceptable.

This update replaces use of __thread variables wih passing down the needed info via the data arg. This saves 5 seconds CPU over 100,000,000 packets on my AMD Ryzen 7. The original data arg is also passed down.

Please apply this patch when next you find time to look at user space,

Cheers ... Duncan.

Duncan Roe (1):
Eliminate packet copy when constructing struct pkt_buff

 examples/nf-queue.c                    | 22 ++++++-
 include/libnetfilter_queue/Makefile.am |  1 +
 include/libnetfilter_queue/callback.h  | 11 ++++
 include/libnetfilter_queue/pktbuff.h   |  2 +
 src/Makefile.am                        |  1 +
 src/extra/callback.c                   | 60 +++++++++++++++++++
 src/extra/pktbuff.c                    | 80 ++++++++++++++++++--------
 7 files changed, 149 insertions(+), 28 deletions(-)
 create mode 100644 include/libnetfilter_queue/callback.h
 create mode 100644 src/extra/callback.c

--
2.17.5

author
#3 lore raw

PATCH v2 1/1 Eliminate packet copy when constructing struct pkt_buff

In reply to Duncan Roe

To avoid a copy, the new code takes advantage of the fact that the netfilter netlink queue never returns multipart messages.
This means that the buffer space following that callback data is available for packet expansion when mangling.

nfq_cb_run() is a new nfq-specific callback runqueue for netlink messages. The principal function of nfq_cb_run() is to pass to the called function what is the length of free space after the packet.
As a side benefit, nfq_cb_run() also gives the called functio a pointer to a zeroised struct pkt_buff, avoiding the malloc / free that was previously needed.

nfq_cb_t is a new typedef for the function called by nfq_cb_run() [c.f. mnl_cb_t / mnl_cb_run].

No doxygen documentation update yet -
will do once function prototypes are agreed.
In the meantime, examples/nf-queue.c is updated to demonstrate the new API.

---
 examples/nf-queue.c                    | 22 ++++++-
 include/libnetfilter_queue/Makefile.am |  1 +
 include/libnetfilter_queue/callback.h  | 11 ++++
 include/libnetfilter_queue/pktbuff.h   |  2 +
 src/Makefile.am                        |  1 +
 src/extra/callback.c                   | 60 +++++++++++++++++++
 src/extra/pktbuff.c                    | 80 ++++++++++++++++++--------
 7 files changed, 149 insertions(+), 28 deletions(-)
 create mode 100644 include/libnetfilter_queue/callback.h
 create mode 100644 src/extra/callback.c
examples/nf-queue.c +19 -3
index 3da2c24..15c0d77 100644
@@ -13,6 +13,8 @@
 #include <linux/types.h>
 #include <linux/netfilter/nfnetlink_queue.h>
 
+#include <libnetfilter_queue/pktbuff.h>
+#include <libnetfilter_queue/callback.h>
 #include <libnetfilter_queue/libnetfilter_queue.h>
 
 /* only for NFQA_CT, not needed otherwise: */
@@ -46,13 +48,17 @@ nfq_send_verdict(int queue_num, uint32_t id)
 	}
 }
 
-static int queue_cb(const struct nlmsghdr *nlh, void *data)
+static int queue_cb(const struct nlmsghdr *nlh, void *data,
+		    struct pkt_buff *supplied_pktb, size_t supplied_extra)
 {
 	struct nfqnl_msg_packet_hdr *ph = NULL;
 	struct nlattr *attr[NFQA_MAX+1] = {};
+	struct pkt_buff *pktb;
 	uint32_t id = 0, skbinfo;
 	struct nfgenmsg *nfg;
+	uint8_t *payload;
 	uint16_t plen;
+	int i;
 
 	if (nfq_nlmsg_parse(nlh, attr) < 0) {
 		perror("problems parsing");
@@ -69,7 +75,10 @@ static int queue_cb(const struct nlmsghdr *nlh, void *data)
 	ph = mnl_attr_get_payload(attr[NFQA_PACKET_HDR]);
 
 	plen = mnl_attr_get_payload_len(attr[NFQA_PAYLOAD]);
-	/* void *payload = mnl_attr_get_payload(attr[NFQA_PAYLOAD]); */
+	payload = mnl_attr_get_payload(attr[NFQA_PAYLOAD]);
+
+	pktb = pktb_populate(supplied_pktb, AF_INET, payload, plen,
+			     supplied_extra);
 
 	skbinfo = attr[NFQA_SKB_INFO] ? ntohl(mnl_attr_get_u32(attr[NFQA_SKB_INFO])) : 0;
 
@@ -97,6 +106,13 @@ static int queue_cb(const struct nlmsghdr *nlh, void *data)
 		printf(", checksum not ready");
 	puts(")");
 
+	printf("payload (len=%d) [", plen);
+
+	for (i = 0; i < pktb_len(pktb); i++)
+		printf("%02x", payload[i]);
+
+	printf("]\n");
+
 	nfq_send_verdict(ntohs(nfg->res_id), id);
 
 	return MNL_CB_OK;
@@ -168,7 +184,7 @@ int main(int argc, char *argv[])
 			exit(EXIT_FAILURE);
 		}
 
-		ret = mnl_cb_run(buf, ret, 0, portid, queue_cb, NULL);
+		ret = nfq_cb_run(buf, ret, sizeof_buf, portid, queue_cb, NULL);
 		if (ret < 0){
 			perror("mnl_cb_run");
 			exit(EXIT_FAILURE);
include/libnetfilter_queue/Makefile.am +1 -0
index e436bab..ed6524b 100644
@@ -5,4 +5,5 @@ pkginclude_HEADERS = libnetfilter_queue.h	\
 		     libnetfilter_queue_ipv6.h	\
 		     libnetfilter_queue_tcp.h	\
 		     libnetfilter_queue_udp.h	\
+		     callback.h			\
 		     pktbuff.h
include/libnetfilter_queue/callback.h new file +11 -0
new file mode 100644
index 0000000..27bfe31
@@ -0,0 +1,11 @@
+#ifndef _LIBNETFILTER_QUEUE_CALLBACK_H_
+#define _LIBNETFILTER_QUEUE_CALLBACK_H_
+
+struct nlattr;
+struct pkt_buff;
+
+typedef int (*nfq_cb_t)(const struct nlmsghdr *nlh, void *data, struct pkt_buff *pktb, size_t extra);
+
+int nfq_cb_run(const void *buf, size_t buflen, size_t bufcap, unsigned int portid, nfq_cb_t cb_data, void *data);
+
+#endif
include/libnetfilter_queue/pktbuff.h +2 -0
index 42bc153..33829cc 100644
@@ -6,6 +6,8 @@ struct pkt_buff;
 struct pkt_buff *pktb_alloc(int family, void *data, size_t len, size_t extra);
 void pktb_free(struct pkt_buff *pktb);
 
+struct pkt_buff *pktb_populate(struct pkt_buff *pktb, int family, void *data, size_t len, size_t extra);
+
 uint8_t *pktb_data(struct pkt_buff *pktb);
 uint32_t pktb_len(struct pkt_buff *pktb);
 
src/Makefile.am +1 -0
index 079853e..376bc45 100644
@@ -31,6 +31,7 @@ libnetfilter_queue_la_LDFLAGS = -Wc,-nostartfiles \
 libnetfilter_queue_la_SOURCES = libnetfilter_queue.c	\
 				nlmsg.c			\
 				extra/checksum.c	\
+				extra/callback.c	\
 				extra/icmp.c		\
 				extra/ipv6.c		\
 				extra/tcp.c		\
src/extra/callback.c new file +60 -0

60 lines not on this page · raw message

src/extra/pktbuff.c +55 -25

110 lines not on this page · raw message

Signature · 1 line
2.17.5
#4 lore raw
Quoted 5 lines · Duncan Roe

To avoid a copy, the new code takes advantage of the fact that the netfilter netlink queue never returns multipart messages.
This means that the buffer space following that callback data is available for packet expansion when mangling.

Quoted 8 lines

nfq_cb_run() is a new nfq-specific callback runqueue for netlink messages. The principal function of nfq_cb_run() is to pass to the called function what is the length of free space after the packet.
As a side benefit, nfq_cb_run() also gives the called functio a pointer to a zeroised struct pkt_buff, avoiding the malloc / free that was previously needed.

nfq_cb_t is a new typedef for the function called by nfq_cb_run() [c.f. mnl_cb_t / mnl_cb_run].

Interesting idea: let me get back to you with a proposal based on this patch.

Meanwhile, I have pushed out the __pktb_setup() function which is going to be needed:

http://git.netfilter.org/libnetfilter_queue/commit/?id=710f891c8a6116f520948f5cf448489947fb7d78

Thanks.

author
#5 lore raw
Quoted 12 lines · Pablo Neira Ayuso
Quoted 11 lines · Duncan Roe

To avoid a copy, the new code takes advantage of the fact that the netfilter netlink queue never returns multipart messages.
This means that the buffer space following that callback data is available for packet expansion when mangling.

nfq_cb_run() is a new nfq-specific callback runqueue for netlink messages. The principal function of nfq_cb_run() is to pass to the called function what is the length of free space after the packet.
As a side benefit, nfq_cb_run() also gives the called functio a pointer to a zeroised struct pkt_buff, avoiding the malloc / free that was previously needed.

Quoted 6 lines
Quoted 2 lines

nfq_cb_t is a new typedef for the function called by nfq_cb_run() [c.f. mnl_cb_t / mnl_cb_run].

Interesting idea: let me get back to you with a proposal based on this patch.

[...]

It occurred to me there is no real need to use a callback any more.

However, mnl_cb_run() does some checks before and after invoking the cb. Some of these checks may still be valid, so leave it as_is?

This patch has been on the table for a while, any idea when you might find time to respond?

Cheers ... Duncan.

author
#6 lore raw
In reply to Pablo Neira Ayuso

Hi Pablo,

Quoted 3 lines · Pablo Neira Ayuso
Quoted 2 lines · Duncan Roe

To avoid a copy, the new code takes advantage of the fact that the netfilter netlink queue never returns multipart messages.

[...]

Quoted 10 lines

Interesting idea: let me get back to you with a proposal based on this patch.

Meanwhile, I have pushed out the __pktb_setup() function which is going to be needed:

http://git.netfilter.org/libnetfilter_queue/commit/?id=710f891c8a6116f520948f5cf448489947fb7d78

Thanks.

It also occurred to me to wonder what is the benefit of having struct pkt_buff be opaque? It's never going to have a buffer tacked on the end of it any more, so can simply be declared to be sizeof(struct pkt_buff).

Users could read the values of struct members directly rather than having to learn and use the current procedural interface. That would have to use less instructions to achieve, but I have yet to benchmark to see if the improvement is measureable.

We could document when (if ever) the structure may be written to directly but even if developers break the rules, what damage can they do? This is a userspace
program: they're not going to crash the kernel.

I sidestepped this question in the code by passing down pktb_instance from local_cb().

Regardless of the above, do you think you might have a proposal for me some time soon?

Cheers ... Duncan.

author
#7 lore raw
In reply to Duncan Roe
Quoted 4 lines · Duncan Roe

Hi Pablo,

This is item 2 of 4 after which I think we could do a new release.

[...]

Item 1 was "make distcheck passes with doxygen enabled", applied.

Cheers ... Duncan.

author
#8 lore raw
In reply to Duncan Roe

Hi Pablo,

Quoted 8 lines · Duncan Roe

Hi Pablo,

This is item 2 of 4 after which I think we could do a new release.

Item 3 is to eliminate packet copy when returning a mangled packet in a verdict.
I have this working in inline code, not yet factored into function calls.

[SNIP]

I have abandoned item 3. Timing tests showed sendmsg() of 3 or 4 buffers to be slower than memcpy() them into 1 buf and send that (i.e. use nfq_nlmsg_verdict_put_pkt() and mnl_socket_sendto()).

Instead, I'll take a look at stopping the automatic load of libnfnetlink, as we discussed a while back.

Cheers ... Duncan.