src: eliminate packet copy when constructing struct pktbuff
PATCH v4 1/1 src: eliminate packet copy when constructing struct pktbuff
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.
pktb_setup is a new function to initialise a new struct pkt_buff.
pktb_head_size() is a new function to return the amount of memory to reserve for a new struct pkt_buff.
nfq_cb_t is a new typedef for the function called by nfq_cb_run() [c.f. mnl_cb_t / mnl_cb_run].
examples/nf-queue.c is updated to demonstrate the new API.
---
v4:
New functions have man pages / html doc
struct pkt_buff remains opaquepktb_populate renamed pktb_setup
doxygen/Makefile.am | 1 + doxygen/doxygen.cfg.in | 1 + examples/nf-queue.c | 24 ++++++- include/libnetfilter_queue/Makefile.am | 1 + include/libnetfilter_queue/callback.h | 11 +++ include/libnetfilter_queue/pktbuff.h | 3 + src/Makefile.am | 1 + src/extra/callback.c | 93 ++++++++++++++++++++++++++ src/extra/pktbuff.c | 53 +++++++++++++-- 9 files changed, 181 insertions(+), 7 deletions(-) create mode 100644 include/libnetfilter_queue/callback.h create mode 100644 src/extra/callback.c
doxygen/Makefile.am +1 -0
@@ -8,6 +8,7 @@ doc_srcs = $(top_srcdir)/src/libnetfilter_queue.c\ $(top_srcdir)/src/extra/ipv6.c\ $(top_srcdir)/src/extra/tcp.c\ $(top_srcdir)/src/extra/udp.c\ + $(top_srcdir)/src/extra/callback.c\ $(top_srcdir)/src/extra/icmp.c doxyfile.stamp: $(doc_srcs) Makefile
doxygen/doxygen.cfg.in +1 -0
@@ -13,6 +13,7 @@ EXCLUDE_SYMBOLS = EXPORT_SYMBOL \ nfq_handle \ nfq_data \ nfq_q_handle \ + data_carrier \ tcp_flag_word EXAMPLE_PATTERNS = INPUT_FILTER = "sed 's/EXPORT_SYMBOL//g'"
examples/nf-queue.c +21 -3
@@ -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> /* NFQA_CT requires CTA_* attributes defined in nfnetlink_conntrack.h */
@@ -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, +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 j;if (nfq_nlmsg_parse (nlh, attr) <0 ) {perror ("problems parsing" );
@@ -69,7 +75,9 @@ 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_setup (data,AF_INET , payload, plen, supplied_extra); skbinfo = attr[NFQA_SKB_INFO ] ?ntohl (mnl_attr_get_u32 (attr[NFQA_SKB_INFO ])) :0 ;
@@ -115,6 +123,13 @@ static int queue_cb(const struct nlmsghdr *nlh, void *data)printf (", checksum not ready" );puts (")" ); +printf ("payload (len=%d) [" , plen); + +for (j =0 ; j <pktb_len (pktb); j++) +printf ("%02x" , payload[j]); + +printf ("]\n" ); +nfq_send_verdict (ntohs (nfg->res_id), id);return MNL_CB_OK ;
@@ -129,6 +144,9 @@ int main(int argc, char *argv[])int ret;unsigned int portid, queue_num; +/* Reserve space to be [re-]used as a struct pktbuff.*/ +char pkt_b[pktb_head_size ()]; +if (argc !=2 ) {printf ("Usage: %s [queue_num]\n" , argv[0 ]);exit (EXIT_FAILURE );
@@ -186,7 +204,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, pkt_b);if (ret <0 ){perror ("mnl_cb_run" );exit (EXIT_FAILURE );
include/libnetfilter_queue/Makefile.am +1 -0
@@ -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
@@ -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,size_t extra); + +int nfq_cb_run (const void *buf,size_t buflen,size_t bufsiz,unsigned int portid,nfq_cb_t cb_data,void *data); + +#endif
include/libnetfilter_queue/pktbuff.h +3 -0
@@ -6,6 +6,9 @@ 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_setup (void *pkt_b,int family,void *data,size_t len,size_t extra); +size_t pktb_head_size (void ); +uint8_t *pktb_data (struct pkt_buff *pktb);uint32_t pktb_len (struct pkt_buff *pktb);
src/Makefile.am +1 -0
@@ -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 +93 -0
93 lines not on this page · raw message
src/extra/pktbuff.c +49 -4
83 lines not on this page · raw message
Signature · 1 line
2.17.5
Hi Pablo,
Quoted 17 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 ofnfq_cb_run()is to pass to the called function what is the length of free space after the packet.
pktb_setupis a new function to initialise a new structpkt_buff.
pktb_head_size()is a new function to return the amount of memory to reserve for a new structpkt_buff.
nfq_cb_tis a new typedef for the function called bynfq_cb_run()[c.f.mnl_cb_t/mnl_cb_run].
Quoted 8 lines
examples/nf-queue.cis updated to demonstrate the new API.Signed-off-by Duncan Roe <[email protected]>---
v4:
New functions have man pages / html doc
structpkt_buffremains opaquepktb_populaterenamedpktb_setup
[...]
This patch locks in that the netfilter queue will never batch multiple packets:
is that a problem?
If you like I could send a v5 documenting that programs that hold on to multiple packets should [continue to] use mnl_cb_run / pktb_alloc / pktb_free.
Re documentation: of course I would like to rework it to replace "deprecated" with e.g. "old interface". There would be a section comparing the old and new interfaces. I would rather not have to write in that section that the old interface is faster (but can't mangle).
If you apply this patch, the old interface would no longer be faster.
Cheers ... Duncan.