New pktb_usebuf() function
cover New pktb_usebuf() function
pktb_usebuf() is a copy of pktb_alloc() with the first part modified to use a supplied buffer rather than calloc() one. I thought this would give a measurable performance boost and it does.
All the code after the memset() call is common to pktb_alloc(). If you like, I can submit a v2 with this code in a static function called by both.
Duncan Roe (1):
src: Add alternative function to pktb_alloc to avoid malloc / free
overhead
include/libnetfilter_queue/pktbuff.h | 2 + src/extra/pktbuff.c | 82 ++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+)
--
2.14.5
PATCH 1/1 src: Add alternative function to pktb_alloc to avoid malloc / free overhead
New function pktb_usebuf() works like pktb_alloc() except it has 2 extra arguments, being a user-supplied buffer and its length. Performance testing is ongoing, so the 5% improvement claimed in the documentation is conservative.
Updated:
include/libnetfilter_queue/pktbuff.h: Add pktb_usebuf() prototype
src/extra/pktbuff.c: Implement pktb_usebuf()
--- include/libnetfilter_queue/pktbuff.h | 2 + src/extra/pktbuff.c | 82 ++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+)
include/libnetfilter_queue/pktbuff.h +2 -0
@@ -4,6 +4,8 @@struct pkt_buff ;struct pkt_buff *pktb_alloc (int family,void *data,size_t len,size_t extra); +struct pkt_buff *pktb_usebuf (int family,void *data,size_t len,size_t extra, +void *buf,size_t bufsize);void pktb_free (struct pkt_buff *pktb);uint8_t *pktb_data (struct pkt_buff *pktb);
src/extra/pktbuff.c +82 -0
88 lines not on this page · raw message
Signature · 1 line
2.14.5
Hi Pablo,
Quoted 10 lines · Duncan Roe
pktb_usebuf()is a copy ofpktb_alloc()with the first part modified to use a supplied buffer rather thancalloc()one. I thought this would give a measurable performance boost and it does.
All the code after thememset()call is common topktb_alloc(). If you like, I can submit a v2 with this code in a static function called by both.Duncan Roe (1):
src: Add alternative function topktb_allocto avoid malloc / free
overhead
Quoted 7 lines
include/libnetfilter_queue/pktbuff.h | 2 + src/extra/pktbuff.c | 82 ++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+)--
2.14.5
After a day or so of testing, it appears pktb_usebuf() saves 6% overall CPU over pktb_alloc() in a program similar to libnetfilter_queue/examples/nf-queue.c (but not printing every packet). The variance is quite high, so plus or minus 1%. Can supply more details of testing methodology if you want them.
Overall I would say 6% is a worthwhile saving.
What about it?
Cheers ... Duncan.
Hi Pablo,
Do you think pktb_make() might be a better name for this new function?
I noticed an errant free() call after "errno = EPROTONOSUPPORT" so there will have to be a v2 anyway.
I could merge the (now almost-) identical second halves of pktb_alloc and pktb_make into a static function to avoid the duplicate code maintenance overhead. Would you prefer that?
Cheers ... Duncan.
Quoted 17 lines · Duncan Roe
pktb_usebuf()is a copy ofpktb_alloc()with the first part modified to use a supplied buffer rather thancalloc()one. I thought this would give a measurable performance boost and it does.
All the code after thememset()call is common topktb_alloc(). If you like, I can submit a v2 with this code in a static function called by both.Duncan Roe (1):
src: Add alternative function topktb_allocto avoid malloc / free
overheadinclude/libnetfilter_queue/pktbuff.h | 2 + src/extra/pktbuff.c | 82 ++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+)--
2.14.5
v2 cover New pktb_make() function
In reply to Duncan RoeThis patch offers a faster alternative / replacement function to pktb_alloc().
pktb_make() is a copy of the first part of pktb_alloc() modified to use a supplied buffer rather than calloc() one. It then calls the second part of pktb_alloc() which is modified to be a static function.
Can't think of a use case where one would choose to use pktb_alloc over pktb_make.
In a furure documentation update, might relegate pktb_alloc and pktb_free to "other functions".
Duncan Roe (1):
src: Add alternative function to pktb_alloc to avoid malloc / free
overhead
include/libnetfilter_queue/pktbuff.h | 1 + src/extra/pktbuff.c | 60 ++++++++++++++++++++++++++++++++++-- 2 files changed, 59 insertions(+), 2 deletions(-)
v2: - Function name changed to pktb_make (was pktb_usebuf)
- Cvt 2nd 1/2 of pktb_malloc into a static function common to pktb_make
Signature · 1 line
2.14.5
PATCH v2 1/1 src: Add alternative function to pktb_alloc to avoid malloc / free overhead
New function pktb_make() works like pktb_alloc() except it has 2 extra arguments, being a user-supplied buffer and its length. Performance testing using a program derived from examples/nf_queue.c shows 6% improvement, so the 5% improvement claimed in the documentation is conservative.
Updated:
include/libnetfilter_queue/pktbuff.h: Add pktb_make() prototype
src/extra/pktbuff.c: Implement pktb_make()
--- include/libnetfilter_queue/pktbuff.h | 1 + src/extra/pktbuff.c | 60 ++++++++++++++++++++++++++++++++++-- 2 files changed, 59 insertions(+), 2 deletions(-)
include/libnetfilter_queue/pktbuff.h +1 -0
@@ -4,6 +4,7 @@struct pkt_buff ;struct pkt_buff *pktb_alloc (int family,void *data,size_t len,size_t extra); +struct pkt_buff *pktb_make (int family,void *data,size_t len,size_t extra,void *buf,size_t bufsize);void pktb_free (struct pkt_buff *pktb);uint8_t *pktb_data (struct pkt_buff *pktb);
src/extra/pktbuff.c +58 -2
88 lines not on this page · raw message
Signature · 1 line
2.14.5
Pablo Neira Ayuso
maintainerRe: [PATCH libnetfilter_queue v2 0/1] New pktb_make() function
In reply to Duncan RoeQuoted 10 lines · Duncan Roe
This patch offers a faster alternative / replacement function to
pktb_alloc().
pktb_make()is a copy of the first part ofpktb_alloc()modified to use a supplied buffer rather thancalloc()one. It then calls the second part ofpktb_alloc()which is modified to be a static function.Can't think of a use case where one would choose to use
pktb_allocoverpktb_make.
In a furure documentation update, might relegatepktb_allocandpktb_freeto "other functions".
This is very useful.
Would you explore something looking similar to what I'm attaching?
Warning: Compile tested only :-)
Thanks.
src/extra/pktbuff.c +76 -28
@@ -29,6 +29,58 @@* @{ */ +static struct pkt_buff *__pktb_alloc (int family,void *data,size_t len, +size_t extra) +{ +struct pkt_buff *pktb; + + pktb =calloc (1 ,sizeof (struct pkt_buff ) + len + extra); +if (pktb ==NULL ) +return NULL ; + +return pktb; +} + +static int pktb_setup_family (struct pkt_buff *pktb,int family) +{ +switch (family) { +case AF_INET : +case AF_INET6 : + pktb->network_header = pktb->data; +break ; +case AF_BRIDGE : { +struct ethhdr *ethhdr = (struct ethhdr *)pktb->data; + + pktb->mac_header = pktb->data; + +switch (ethhdr->h_proto) { +case ETH_P_IP : +case ETH_P_IPV6 : + pktb->network_header = pktb->data +ETH_HLEN ; +break ; +default : +/* This protocol is unsupported. */ + errno =EPROTONOSUPPORT ; +return -1 ; + } +break ; + } + } + +return 0 ; +} + +static void pktb_setup_metadata (struct pkt_buff *pktb,void *pkt_data, +size_t len,size_t extra) +{ + pktb->len = len; + pktb->data_len = len + extra; + + pktb->head = pkt_data; + pktb->data = pkt_data; + pktb->tail = pktb->head + len; +} +/** * pktb_alloc - allocate a new packet buffer * \param family Indicate what family. Currently supported families are
@@ -54,45 +106,41 @@ struct pkt_buff *pktb_alloc(int family, void *data, size_t len, size_t extra)struct pkt_buff *pktb;void *pkt_data; - pktb =calloc (1 ,sizeof (struct pkt_buff ) + len + extra); -if (pktb ==NULL ) + pktb =__pktb_alloc (family, data, len, extra); +if (!pktb)return NULL ;/* Better make sure alignment is correct. */ pkt_data = (uint8_t *)pktb +sizeof (struct pkt_buff );memcpy (pkt_data, data, len); - pktb->len = len; - pktb->data_len = len + extra; +pktb_setup_metadata (pktb, pkt_data, len, extra); - pktb->head = pkt_data; - pktb->data = pkt_data; - pktb->tail = pktb->head + len; +if (pktb_setup_family (pktb, family) <0 ) { +free (pktb); +return NULL ; + } -switch (family) { -case AF_INET : -case AF_INET6 : - pktb->network_header = pktb->data; -break ; -case AF_BRIDGE : { -struct ethhdr *ethhdr = (struct ethhdr *)pktb->data; +return pktb; +} - pktb->mac_header = pktb->data; +EXPORT_SYMBOL +struct pkt_buff *pktb_alloc_data(int family, void *data, size_t len) +{ +struct pkt_buff *pktb; -switch (ethhdr->h_proto) { -case ETH_P_IP : -case ETH_P_IPV6 : - pktb->network_header = pktb->data +ETH_HLEN ; -break ; -default : -/* This protocol is unsupported. */ - errno =EPROTONOSUPPORT ; -free (pktb); -return NULL ; - } -break ; - } + pktb =__pktb_alloc (family, data,0 ,0 ); +if (!pktb) +return NULL ; + + pktb->data = data; +pktb_setup_metadata (pktb, data, len,0 ); + +if (pktb_setup_family (pktb, family) <0 ) { +free (pktb); +return NULL ; } +return pktb; }
Re: [PATCH libnetfilter_queue v2 0/1] New pktb_make() function
Quoted 54 lines · Pablo Neira Ayuso
Quoted 10 lines · Duncan Roe
This patch offers a faster alternative / replacement function to
pktb_alloc().
pktb_make()is a copy of the first part ofpktb_alloc()modified to use a supplied buffer rather thancalloc()one. It then calls the second part ofpktb_alloc()which is modified to be a static function.Can't think of a use case where one would choose to use
pktb_allocoverpktb_make.
In a furure documentation update, might relegatepktb_allocandpktb_freeto "other functions".This is very useful.
Would you explore something looking similar to what I'm attaching?
Warning: Compile tested only :-)
Thanks.
src/extra/pktbuff.c +26
-0@@ -29,6 +29,58 @@* @{ */ +static struct pkt_buff *__pktb_alloc (int family,void *data,size_t len, +size_t extra) +{ +struct pkt_buff *pktb; + + pktb =calloc (1 ,sizeof (struct pkt_buff ) + len + extra); +if (pktb ==NULL ) +return NULL ; + +return pktb; +} + +static int pktb_setup_family (struct pkt_buff *pktb,int family) +{ +switch (family) { +case AF_INET : +case AF_INET6 : + pktb->network_header = pktb->data; +break ; +case AF_BRIDGE : { +struct ethhdr *ethhdr = (struct ethhdr *)pktb->data; + + pktb->mac_header = pktb->data; + +switch (ethhdr->h_proto) { +case ETH_P_IP :
src/extra/pktbuff.c line 67
Quoted 10 lines
src/extra/pktbuff.c +10
-0+case ETH_P_IPV6 : + pktb->network_header = pktb->data +ETH_HLEN ; +break ; +default : +/* This protocol is unsupported. */ + errno =EPROTONOSUPPORT ; +return -1 ; + } +break ; + }
GRR! I just wasted 20 minutes looking at these last 3 lines. Fix the indentation
src/extra/pktbuff.c line 76
Quoted 9 lines
src/extra/pktbuff.c +9
-0+ } + +return 0 ; +} + +static void pktb_setup_metadata (struct pkt_buff *pktb,void *pkt_data, +size_t len,size_t extra) +{ + pktb->len = len;
You know, we only need any 2 of len, data and tail. This has caused bugs in the past, e.g. commit 8a4316f31. In the code, len seems to be the least used - will see if I can't get rid of it.
Quoted 70 lines
src/extra/pktbuff.c +25
-28+ pktb->data_len = len + extra; + + pktb->head = pkt_data; + pktb->data = pkt_data; + pktb->tail = pktb->head + len; +} +/** * pktb_alloc - allocate a new packet buffer * \param family Indicate what family. Currently supported families are @@ -54,45 +106,41 @@ struct pkt_buff *pktb_alloc(int family, void *data, size_t len, size_t extra)struct pkt_buff *pktb;void *pkt_data; - pktb =calloc (1 ,sizeof (struct pkt_buff ) + len + extra); -if (pktb ==NULL ) + pktb =__pktb_alloc (family, data, len, extra); +if (!pktb)return NULL ;/* Better make sure alignment is correct. */ pkt_data = (uint8_t *)pktb +sizeof (struct pkt_buff );memcpy (pkt_data, data, len); - pktb->len = len; - pktb->data_len = len + extra; +pktb_setup_metadata (pktb, pkt_data, len, extra); - pktb->head = pkt_data; - pktb->data = pkt_data; - pktb->tail = pktb->head + len; +if (pktb_setup_family (pktb, family) <0 ) { +free (pktb); +return NULL ; + } -switch (family) { -case AF_INET : -case AF_INET6 : - pktb->network_header = pktb->data; -break ; -case AF_BRIDGE : { -struct ethhdr *ethhdr = (struct ethhdr *)pktb->data; +return pktb; +} - pktb->mac_header = pktb->data; +EXPORT_SYMBOL +struct pkt_buff *pktb_alloc_data(int family, void *data, size_t len) +{ +struct pkt_buff *pktb; -switch (ethhdr->h_proto) { -case ETH_P_IP : -case ETH_P_IPV6 : - pktb->network_header = pktb->data +ETH_HLEN ; -break ; -default : -/* This protocol is unsupported. */ - errno =EPROTONOSUPPORT ; -free (pktb); -return NULL ; - } -break ; - } + pktb =__pktb_alloc (family, data,0 ,0 ); +if (!pktb) +return NULL ; + + pktb->data = data;
src/extra/pktbuff.c struct pkt_buff *pktb_alloc(int family, void *data, size_t len, size_t extra) · line 146
Quoted 10 lines
src/extra/pktbuff.c +6
-0+pktb_setup_metadata (pktb, data, len,0 ); + +if (pktb_setup_family (pktb, family) <0 ) { +free (pktb); +return NULL ; } +return pktb; }
Ok, so this is another approach to reducing CPU time: avoid memcpy of data.
That's great if you're not mangling content.
But if you are mangling, beware. pktb now has pointers into the buffer you used for receiving from Netlink so you must use a different buffer when sending.
I'll be updating my performance testing regime to get results more quickly, so will be reporting back at some stage. It remains the case that malloc / free were taking at least 5% of overall CPU with short packets (and btw most of that is free(): as a rule of thumb I have found free cpu = 3 * malloc cpu)
Pablo Neira Ayuso
maintainerRe: [PATCH libnetfilter_queue v2 0/1] New pktb_make() function
Quoted 165 lines · Duncan Roe
Quoted 54 lines · Pablo Neira Ayuso
Quoted 10 lines · Duncan Roe
This patch offers a faster alternative / replacement function to
pktb_alloc().
pktb_make()is a copy of the first part ofpktb_alloc()modified to use a supplied buffer rather thancalloc()one. It then calls the second part ofpktb_alloc()which is modified to be a static function.Can't think of a use case where one would choose to use
pktb_allocoverpktb_make.
In a furure documentation update, might relegatepktb_allocandpktb_freeto "other functions".This is very useful.
Would you explore something looking similar to what I'm attaching?
Warning: Compile tested only :-)
Thanks.
src/extra/pktbuff.c +26
-0@@ -29,6 +29,58 @@* @{ */ +static struct pkt_buff *__pktb_alloc (int family,void *data,size_t len, +size_t extra) +{ +struct pkt_buff *pktb; + + pktb =calloc (1 ,sizeof (struct pkt_buff ) + len + extra); +if (pktb ==NULL ) +return NULL ; + +return pktb; +} + +static int pktb_setup_family (struct pkt_buff *pktb,int family) +{ +switch (family) { +case AF_INET : +case AF_INET6 : + pktb->network_header = pktb->data; +break ; +case AF_BRIDGE : { +struct ethhdr *ethhdr = (struct ethhdr *)pktb->data; + + pktb->mac_header = pktb->data; + +switch (ethhdr->h_proto) { +case ETH_P_IP :src/extra/pktbuff.c line 67
Quoted 10 lines
src/extra/pktbuff.c +10
-0+case ETH_P_IPV6 : + pktb->network_header = pktb->data +ETH_HLEN ; +break ; +default : +/* This protocol is unsupported. */ + errno =EPROTONOSUPPORT ; +return -1 ; + } +break ; + }GRR! I just wasted 20 minutes looking at these last 3 lines. Fix the indentation
src/extra/pktbuff.c line 76
Quoted 9 lines
src/extra/pktbuff.c +9
-0+ } + +return 0 ; +} + +static void pktb_setup_metadata (struct pkt_buff *pktb,void *pkt_data, +size_t len,size_t extra) +{ + pktb->len = len;You know, we only need any 2 of len, data and tail. This has caused bugs in the past, e.g. commit
8a4316f31. In the code, len seems to be the least used - will see if I can't get rid of it.Quoted 70 lines
src/extra/pktbuff.c +25
-28+ pktb->data_len = len + extra; + + pktb->head = pkt_data; + pktb->data = pkt_data; + pktb->tail = pktb->head + len; +} +/** * pktb_alloc - allocate a new packet buffer * \param family Indicate what family. Currently supported families are @@ -54,45 +106,41 @@ struct pkt_buff *pktb_alloc(int family, void *data, size_t len, size_t extra)struct pkt_buff *pktb;void *pkt_data; - pktb =calloc (1 ,sizeof (struct pkt_buff ) + len + extra); -if (pktb ==NULL ) + pktb =__pktb_alloc (family, data, len, extra); +if (!pktb)return NULL ;/* Better make sure alignment is correct. */ pkt_data = (uint8_t *)pktb +sizeof (struct pkt_buff );memcpy (pkt_data, data, len); - pktb->len = len; - pktb->data_len = len + extra; +pktb_setup_metadata (pktb, pkt_data, len, extra); - pktb->head = pkt_data; - pktb->data = pkt_data; - pktb->tail = pktb->head + len; +if (pktb_setup_family (pktb, family) <0 ) { +free (pktb); +return NULL ; + } -switch (family) { -case AF_INET : -case AF_INET6 : - pktb->network_header = pktb->data; -break ; -case AF_BRIDGE : { -struct ethhdr *ethhdr = (struct ethhdr *)pktb->data; +return pktb; +} - pktb->mac_header = pktb->data; +EXPORT_SYMBOL +struct pkt_buff *pktb_alloc_data(int family, void *data, size_t len) +{ +struct pkt_buff *pktb; -switch (ethhdr->h_proto) { -case ETH_P_IP : -case ETH_P_IPV6 : - pktb->network_header = pktb->data +ETH_HLEN ; -break ; -default : -/* This protocol is unsupported. */ - errno =EPROTONOSUPPORT ; -free (pktb); -return NULL ; - } -break ; - } + pktb =__pktb_alloc (family, data,0 ,0 ); +if (!pktb) +return NULL ; + + pktb->data = data;src/extra/pktbuff.c struct pkt_buff *pktb_alloc(int family, void *data, size_t len, size_t extra) · line 146
Quoted 10 lines
src/extra/pktbuff.c +6
-0+pktb_setup_metadata (pktb, data, len,0 ); + +if (pktb_setup_family (pktb, family) <0 ) { +free (pktb); +return NULL ; } +return pktb; }Ok, so this is another approach to reducing CPU time: avoid memcpy of data.
Quoted 4 lines
That's great if you're not mangling content.
But if you are mangling, beware. pktb now has pointers into the buffer you used for receiving from Netlink so you must use a different buffer when sending.
Probably we can store a flag in the pkt_buff structure to say "this buffer is now ours" so mangling can just trigger a clone via malloc()
+ memcpy() only for that path.
Or you can just document this, although handling this case for the library would be make it easier to user for users.
Thanks.
Re: [PATCH libnetfilter_queue v2 0/1] New pktb_make() function
Quoted 22 lines · Pablo Neira Ayuso
Quoted 21 lines · Duncan Roe
Quoted 19 lines · Pablo Neira Ayuso
Quoted 10 lines · Duncan Roe
This patch offers a faster alternative / replacement function to
pktb_alloc().
pktb_make()is a copy of the first part ofpktb_alloc()modified to use a supplied buffer rather thancalloc()one. It then calls the second part ofpktb_alloc()which is modified to be a static function.Can't think of a use case where one would choose to use
pktb_allocoverpktb_make.
In a furure documentation update, might relegatepktb_allocandpktb_freeto "other functions".This is very useful.
Would you explore something looking similar to what I'm attaching?
Warning: Compile tested only :-)
Thanks.
[...]
Quoted 11 lines
Quoted 7 lines
Ok, so this is another approach to reducing CPU time: avoid memcpy of data.
That's great if you're not mangling content.
But if you are mangling, beware. pktb now has pointers into the buffer you used for receiving from Netlink so you must use a different buffer when sending.
Probably we can store a flag in the
pkt_buffstructure to say "this buffer is now ours" so mangling can just trigger a clone viamalloc()
+memcpy()only for that path.
Not for pktb_make: we never call pktb_free.
Quoted 3 lines
Or you can just document this, although handling this case for the library would be make it easier to user for users.
ATM I have to document that you must use separate Rx & Tx netlink buffers if mangling at all, because otherwise you may get overlapping data moves or worse.
So I'm inclined to document about no length increase. But ...
There *is* room at the end of the buffer, as long as the received Netlink message didn't fill it. We just need some way to know about it - maybe extra args to the _data functions and maybe as well more _data helper functions to keep it easy to use.
As well, I've been toying with the idea of not copying data out of the Rx buffer at all, but rather building the verdict message around it. I have in mind a user-space-only header to go at the front of the buffer, to be created before receiving into it.
There's still the copies out from and back into kernel space - guess we're stuck with those.
Cheers ... Duncan.
Quoted 2 lines
Thanks.
Re: [PATCH libnetfilter_queue v2 0/1] New pktb_make() function
In reply to Pablo Neira AyusoHi Pablo,
Quoted 143 lines · Pablo Neira Ayuso
Quoted 10 lines · Duncan Roe
This patch offers a faster alternative / replacement function to
pktb_alloc().
pktb_make()is a copy of the first part ofpktb_alloc()modified to use a supplied buffer rather thancalloc()one. It then calls the second part ofpktb_alloc()which is modified to be a static function.Can't think of a use case where one would choose to use
pktb_allocoverpktb_make.
In a furure documentation update, might relegatepktb_allocandpktb_freeto "other functions".This is very useful.
Would you explore something looking similar to what I'm attaching?
Warning: Compile tested only :-)
Thanks.
src/extra/pktbuff.c +70
-28@@ -29,6 +29,58 @@* @{ */ +static struct pkt_buff *__pktb_alloc (int family,void *data,size_t len, +size_t extra) +{ +struct pkt_buff *pktb; + + pktb =calloc (1 ,sizeof (struct pkt_buff ) + len + extra); +if (pktb ==NULL ) +return NULL ; + +return pktb; +} + +static int pktb_setup_family (struct pkt_buff *pktb,int family) +{ +switch (family) { +case AF_INET : +case AF_INET6 : + pktb->network_header = pktb->data; +break ; +case AF_BRIDGE : { +struct ethhdr *ethhdr = (struct ethhdr *)pktb->data; + + pktb->mac_header = pktb->data; + +switch (ethhdr->h_proto) { +case ETH_P_IP : +case ETH_P_IPV6 : + pktb->network_header = pktb->data +ETH_HLEN ; +break ; +default : +/* This protocol is unsupported. */ + errno =EPROTONOSUPPORT ; +return -1 ; + } +break ; + } + } + +return 0 ; +} + +static void pktb_setup_metadata (struct pkt_buff *pktb,void *pkt_data, +size_t len,size_t extra) +{ + pktb->len = len; + pktb->data_len = len + extra; + + pktb->head = pkt_data; + pktb->data = pkt_data; + pktb->tail = pktb->head + len; +} +/** * pktb_alloc - allocate a new packet buffer * \param family Indicate what family. Currently supported families are @@ -54,45 +106,41 @@ struct pkt_buff *pktb_alloc(int family, void *data, size_t len, size_t extra)struct pkt_buff *pktb;void *pkt_data; - pktb =calloc (1 ,sizeof (struct pkt_buff ) + len + extra); -if (pktb ==NULL ) + pktb =__pktb_alloc (family, data, len, extra); +if (!pktb)return NULL ;/* Better make sure alignment is correct. */ pkt_data = (uint8_t *)pktb +sizeof (struct pkt_buff );memcpy (pkt_data, data, len); - pktb->len = len; - pktb->data_len = len + extra; +pktb_setup_metadata (pktb, pkt_data, len, extra); - pktb->head = pkt_data; - pktb->data = pkt_data; - pktb->tail = pktb->head + len; +if (pktb_setup_family (pktb, family) <0 ) { +free (pktb); +return NULL ; + } -switch (family) { -case AF_INET : -case AF_INET6 : - pktb->network_header = pktb->data; -break ; -case AF_BRIDGE : { -struct ethhdr *ethhdr = (struct ethhdr *)pktb->data; +return pktb; +} - pktb->mac_header = pktb->data; +EXPORT_SYMBOL +struct pkt_buff *pktb_alloc_data(int family, void *data, size_t len) +{ +struct pkt_buff *pktb; -switch (ethhdr->h_proto) { -case ETH_P_IP : -case ETH_P_IPV6 : - pktb->network_header = pktb->data +ETH_HLEN ; -break ; -default : -/* This protocol is unsupported. */ - errno =EPROTONOSUPPORT ; -free (pktb); -return NULL ; - } -break ; - } + pktb =__pktb_alloc (family, data,0 ,0 ); +if (!pktb) +return NULL ; + + pktb->data = data;
src/extra/pktbuff.c struct pkt_buff *pktb_alloc(int family, void *data, size_t len, size_t extra) · line 146
Quoted 10 lines
src/extra/pktbuff.c +6
-0+pktb_setup_metadata (pktb, data, len,0 ); + +if (pktb_setup_family (pktb, family) <0 ) { +free (pktb); +return NULL ; } +return pktb; }
The results are in. The spreadsheet is available at
https://github.com/duncan-roe/nfq/blob/master/nfq6/nfq6_timings.ods, but I'll summarise them here.
Four functions are compared: pktb_alloc(), pktb_alloc_data(), pktb_make() and pktb_make_data(). The timings for pktb_alloc & pktb_alloc_data include a call to pktb_free(). When using pktb_make, one must not call pktb_free.
The test is to time calling the function (pair) 100000000 (1e8) times. In all cases, this takes < 60 seconds. Two series of tests: one with 50-byte packets (udp/IPv6, 2 data chars) and the other with 1500-byte (max MTU) packets.
Part-way through testing, pktb_make was improved to not zeroise the area into which packet data will be copied. This is actually a tiny performance hit at 50-byte, could determine a crossover point at which to zeroise in 1 go but didn't consider the extra complexity to be warrantied.
Here's a table of percentages:
bytes|pktb_alloc|pktb_alloc_data|pktb_make|pktb_make_data -----|----------|---------------|---------|-------------- 50| 100| 56.2| 50.8| 28.9 -----|----------|---------------|---------|-------------- 1500| 100| 22.9| 59.6| 11.5
I have yet to do the doxygen documentation for the _data variants, then can send in the code.
Cheers ... Duncan.
PATCH v2 1/1 src: Add faster alternatives to pktb_alloc()
In reply to Pablo Neira AyusoFunctions pktb_alloc_data, pktb_make and pktb_make_data are defined. The pktb_make pair are syggested as replacements for the pktb_alloc (now) pair because they are always faster.
- Add prototypes to include/libnetfilter_queue/pktbuff.h
- Add pktb_alloc_data much as per Pablo's email of Wed, 8 Jan 2020
speedup: point to packet data in netlink receive buffer rather than copy to
area immediately following pktb struct
- Add pktb_make much like pktb_usebuf proposed on 10 Dec 2019
2 sppedups: 1. Use an existing buffer rather than calloc and (later) free one.
2. Only zero struct and extra parts of pktb - the rest is overwritten by copy (calloc has to zero the lot).
- Add pktb_make_data
3 speedups: All of the above
- Document the new functions
- Move pktb_alloc and pktb_alloc_data into the "other functions" group since they are slower than the "make" equivalent functions
--- include/libnetfilter_queue/pktbuff.h | 3 + src/extra/pktbuff.c | 296 ++++++++++++++++++++++++++++++----- 2 files changed, 261 insertions(+), 38 deletions(-)
include/libnetfilter_queue/pktbuff.h +3 -0
@@ -4,6 +4,9 @@struct pkt_buff ;struct pkt_buff *pktb_alloc (int family,void *data,size_t len,size_t extra); +struct pkt_buff *pktb_alloc_data (int family,void *data,size_t len); +struct pkt_buff *pktb_make (int family,void *data,size_t len,size_t extra,void *buf,size_t bufsize); +struct pkt_buff *pktb_make_data (int family,void *data,size_t len,void *buf,size_t bufsize);void pktb_free (struct pkt_buff *pktb);uint8_t *pktb_data (struct pkt_buff *pktb);
src/extra/pktbuff.c +258 -38
343 lines not on this page · raw message
Signature · 1 line
2.14.5
Re: [PATCH libnetfilter_queue v2] src: Add faster alternatives to pktb_alloc()
Hi Pablo,
Quoted 18 lines · Duncan Roe
Functions
pktb_alloc_data,pktb_makeandpktb_make_dataare defined. Thepktb_makepair are syggested as replacements for thepktb_alloc(now) pair because they are always faster.- Add prototypes to
include/libnetfilter_queue/pktbuff.h
- Addpktb_alloc_datamuch as per Pablo's email of Wed, 8 Jan 2020
speedup: point to packet data in netlink receive buffer rather than copy to
area immediately following pktb struct
- Addpktb_makemuch likepktb_usebufproposed on 10 Dec 2019
2 sppedups: 1. Use an existing buffer rather than calloc and (later) free one.
2. Only zero struct and extra parts of pktb - the rest is overwritten by copy (calloc has to zero the lot).
- Addpktb_make_data
3 speedups: All of the above
- Document the new functions
- Movepktb_allocandpktb_alloc_datainto the "other functions" group since they are slower than the "make" equivalent functions
Quoted 1 line
Signed-off-by Duncan Roe <[email protected]>
[...]
Did you notice this? I guess it should have been v3 really.
I'd really appreciate some feedback soon, if you or whoever could find the time.
Cheers ... Duncan.
Pablo Neira Ayuso
maintainerRe: [PATCH libnetfilter_queue v2] src: Add faster alternatives to pktb_alloc()
In reply to Duncan RoeQuoted 29 lines · Duncan Roe
Functions
pktb_alloc_data,pktb_makeandpktb_make_dataare defined. Thepktb_makepair are syggested as replacements for thepktb_alloc(now) pair because they are always faster.- Add prototypes to
include/libnetfilter_queue/pktbuff.h
- Addpktb_alloc_datamuch as per Pablo's email of Wed, 8 Jan 2020
speedup: point to packet data in netlink receive buffer rather than copy to
area immediately following pktb struct
- Addpktb_makemuch likepktb_usebufproposed on 10 Dec 2019
2 sppedups: 1. Use an existing buffer rather than calloc and (later) free one.
2. Only zero struct and extra parts of pktb - the rest is overwritten by copy (calloc has to zero the lot).
- Addpktb_make_data
3 speedups: All of the above
- Document the new functions
- Movepktb_allocandpktb_alloc_datainto the "other functions" group since they are slower than the "make" equivalent functionsSigned-off-by Duncan Roe <[email protected]>--- include/libnetfilter_queue/pktbuff.h | 3 + src/extra/pktbuff.c | 296 ++++++++++++++++++++++++++++++----- 2 files changed, 261 insertions(+), 38 deletions(-)include/libnetfilter_queue/pktbuff.h +0
-0
include/libnetfilter_queue/pktbuff.h line 9
Quoted 6 lines
include/libnetfilter_queue/pktbuff.h +3
-0@@ -4,6 +4,9 @@struct pkt_buff ;struct pkt_buff *pktb_alloc (int family,void *data,size_t len,size_t extra); +struct pkt_buff *pktb_alloc_data (int family,void *data,size_t len); +struct pkt_buff *pktb_make (int family,void *data,size_t len,size_t extra,void *buf,size_t bufsize); +struct pkt_buff *pktb_make_data (int family,void *data,size_t len,void *buf,size_t bufsize);
Hm, when I delivered the patch to you, I forgot that you main point was that you wanted to skip the memory allocation.
I wonder if all these new functions can be consolidated into one single function, something like:
struct pkt_buff *pktb_alloc2(int family, void *head, size_t head_size, void *data, size_t len, size_t extra);
The idea is that:
* head is the memory area that is large enough for the struct pkt_buff (metadata). You can add a new pktb_head_size() function that returns the size of opaque struct pkt_buff structure (whose layout is not exposed to the user). I think you can this void *buf in your pktb_make function.
* data is the memory area to store the network packet itself.
Then, you can allocate head and data in the stack and skip malloc/calloc.
Would this work for you? I would prefer if there is just one single advanced function to do this.
Thanks for your patience.
Re: [PATCH libnetfilter_queue v2] src: Add faster alternatives to pktb_alloc()
Hi Pablo,
Quoted 1 line · Pablo Neira Ayuso
On Sat, Feb 01, 2020 at 05:21:27PM +1100, Duncan Roe wrote:
[...]
Quoted 23 lines
include/libnetfilter_queue/pktbuff.h line 9
Quoted 4 lines
struct
pkt_buff*pktb_alloc(int family, void *data,size_tlen,size_textra);include/libnetfilter_queue/pktbuff.h +3
-0+struct pkt_buff *pktb_alloc_data (int family,void *data,size_t len); +struct pkt_buff *pktb_make (int family,void *data,size_t len,size_t extra,void *buf,size_t bufsize); +struct pkt_buff *pktb_make_data (int family,void *data,size_t len,void *buf,size_t bufsize);Hm, when I delivered the patch to you, I forgot that you main point was that you wanted to skip the memory allocation.
I wonder if all these new functions can be consolidated into one single function, something like:
struct pkt_buff *pktb_alloc2(int family, void *head, size_t head_size, void *data, size_t len, size_t extra);The idea is that:
* head is the memory area that is large enough for the struct
pkt_buff(metadata). You can add a newpktb_head_size()function that returns the size of opaque structpkt_buffstructure (whose layout is not exposed to the user). I think you can this void *buf in yourpktb_makefunction.* data is the memory area to store the network packet itself.
Quoted 7 lines
Then, you can allocate head and data in the stack and skip malloc/calloc.
Would this work for you? I would prefer if there is just one single advanced function to do this.
Thanks for your patience.
This patch set is not the last word. It would really help my development process if you could just apply the patch set as-is.
Quoted 1 line
I would prefer if there is just one single advanced function ...
There *is* only 1 "advanced" function listed on the "User-space network packet buffer" web page: pktb_make_data.
pktb_alloc must be kept for legacy support but it's documented on the "Other functions" page.
pktb_alloc_data was only written for the benefit of timing tests. I can send a patch to withdraw it as soon as this set is accepted.
Or, I can submit v4 with pktb_alloc_data removed. But only if you agree you will then commit the patch.
Quoted 1 line
* data is the memory area to store the network packet itself.
Eh?? The plan is to leave the data where it is. pktb_make_data does that, it's fine to use unless mangling could increase the packet size.
A more advance set of functions could lift that restriction. I'm still keen to investigate the savings to be had by not having to move packet data for an advanced variant of nfq_nlmsg_verdict_put_pkt. The idea is to use a new structure (essentially metadata plus a struct nlmsghdr) which I have tentatively named struct nlmsg_buffer. A number of advanced funtion variants would use it in place of struct nlmsghdr. Please LMK if you would be interested in this.
Quoted 1 line
Thanks for your patience.
I have been working on something else :/
I had to put libnetfilter_queue development on hold because juggling 3 branches was just getting to be too much.
(others are https://www.spinics.net/lists/netfilter-devel/msg65661.html (man pages) and https://www.spinics.net/lists/netfilter-devel/msg65585.html (add more helper functions to simplify coding)).
I can send a man pages update as soon as you commit something.
Cheers ... Duncan.
Re: [PATCH libnetfilter_queue v2] src: Add faster alternatives to pktb_alloc()
Quoted 3 lines · Duncan Roe
Hi Pablo,
On Wed, Feb 19, 2020 at 07:04:10PM +0100, Pablo Neira Ayuso wrote:
[...]
Quoted 7 lines
I had to put
libnetfilter_queuedevelopment on hold because juggling 3 branches was just getting to be too much.(others are https://www.spinics.net/lists/netfilter-devel/msg65661.html (man pages) and https://www.spinics.net/lists/netfilter-devel/msg65585.html (add more helper functions to simplify coding)).
Please see https://www.spinics.net/lists/netfilter-devel/msg65584.html for a roadmap of the new helper functions. Another plus for the new functions would be that they would all have man pages (as long as you take that patch ;)
Cheers ... Duncan.
Re: [PATCH libnetfilter_queue v2] src: Add faster alternatives to pktb_alloc()
In reply to Pablo Neira AyusoHi Pablo,
Quoted 47 lines · Pablo Neira Ayuso
Quoted 29 lines · Duncan Roe
Functions
pktb_alloc_data,pktb_makeandpktb_make_dataare defined. Thepktb_makepair are syggested as replacements for thepktb_alloc(now) pair because they are always faster.- Add prototypes to
include/libnetfilter_queue/pktbuff.h
- Addpktb_alloc_datamuch as per Pablo's email of Wed, 8 Jan 2020
speedup: point to packet data in netlink receive buffer rather than copy to
area immediately following pktb struct
- Addpktb_makemuch likepktb_usebufproposed on 10 Dec 2019
2 sppedups: 1. Use an existing buffer rather than calloc and (later) free one.
2. Only zero struct and extra parts of pktb - the rest is overwritten by copy (calloc has to zero the lot).
- Addpktb_make_data
3 speedups: All of the above
- Document the new functions
- Movepktb_allocandpktb_alloc_datainto the "other functions" group since they are slower than the "make" equivalent functionsSigned-off-by Duncan Roe <[email protected]>--- include/libnetfilter_queue/pktbuff.h | 3 + src/extra/pktbuff.c | 296 ++++++++++++++++++++++++++++++----- 2 files changed, 261 insertions(+), 38 deletions(-)include/libnetfilter_queue/pktbuff.h +0
-0include/libnetfilter_queue/pktbuff.h line 9
Quoted 6 lines
include/libnetfilter_queue/pktbuff.h +3
-0@@ -4,6 +4,9 @@struct pkt_buff ;struct pkt_buff *pktb_alloc (int family,void *data,size_t len,size_t extra); +struct pkt_buff *pktb_alloc_data (int family,void *data,size_t len); +struct pkt_buff *pktb_make (int family,void *data,size_t len,size_t extra,void *buf,size_t bufsize); +struct pkt_buff *pktb_make_data (int family,void *data,size_t len,void *buf,size_t bufsize);Hm, when I delivered the patch to you, I forgot that you main point was that you wanted to skip the memory allocation.
I wonder if all these new functions can be consolidated into one single function, something like:
struct pkt_buff *pktb_alloc2(int family, void *head, size_t head_size, void *data, size_t len, size_t extra);The idea is that:
Quoted 7 lines
* head is the memory area that is large enough for the struct
pkt_buff(metadata). You can add a newpktb_head_size()function that returns the size of opaque structpkt_buffstructure (whose layout is not exposed to the user). I think you can this void *buf in yourpktb_makefunction.* data is the memory area to store the network packet itself.
Wait, you need data & len to describe where the data is *now*. You need an extra buf, buflen pair for where to put it.
Quoted 8 lines
Then, you can allocate head and data in the stack and skip malloc/calloc.
Would this work for you? I would prefer if there is just one single advanced function to do this.
Thanks for your patience.
I think I can do as you requested.
Just one thing: do you really think pktb_alloc2 is a good name when users *must not* call pktb_free? pktb_put would have been good but it's already taken.
Perhaps pktb_alloc2 could set a flag for pktb_free to become a no-op and maybe issue a warning the first time it is called - would that be worth doing?
In pktb_alloc2, if extra == 0 then buf can be NULL and buflen 0, since the data will be left in place. That way, we get a single entry point doing as much optimising as possible.
Cheers ... Duncan.
cover src & doc: pktb_alloc2
In reply to Pablo Neira AyusoHi Pablo,
Herewith pktb_alloc2() as you suggested.
There is no malloc/free and data is only moved if extra > 0.
Cheers ... Duncan.
Duncan Roe (1):
New faster pktb_alloc2 replaces pktb_alloc & pktb_free
fixmanpages.sh | 6 +- include/libnetfilter_queue/pktbuff.h | 2 + src/extra/pktbuff.c | 204 +++++++++++++++++++++++++++++------ src/nlmsg.c | 14 ++- 4 files changed, 185 insertions(+), 41 deletions(-)
--
2.14.5
PATCH 1/1 New faster pktb_alloc2 replaces pktb_alloc & pktb_free
pktb_alloc2() avoids the malloc()/free() overhead in pktb_alloc(). Additionally, if the "extra" argument is zero, pktb_alloc2() leaves the packet data in place rather than copying it (mangling is still allowed except to increae the packet size).
New function pktb_head_size() gives size of opaque struct pkt_buff for allocating space in the stack.
- Mark old functions DEPRECATED in the documentation.
- Update sample code snippet in nfq_nlmsg_verdict_put_pkt documentation.
--- fixmanpages.sh | 6 +- include/libnetfilter_queue/pktbuff.h | 2 + src/extra/pktbuff.c | 204 +++++++++++++++++++++++++++++------ src/nlmsg.c | 14 ++- 4 files changed, 185 insertions(+), 41 deletions(-)
fixmanpages.sh +3 -3
@@ -31,11 +31,11 @@ function main add2group nfq_nlmsg_verdict_put_mark nfq_nlmsg_verdict_put_pkt setgroup nlmsg nfq_nlmsg_parse add2group nfq_nlmsg_put - setgroup pktbuff pktb_alloc - add2group pktb_data pktb_len pktb_mangle pktb_mangled - add2group pktb_free + setgroup pktbuff pktb_alloc2 + add2group pktb_data pktb_len pktb_mangle pktb_mangled pktb_head_size setgroup otherfns pktb_tailroom add2group pktb_mac_header pktb_network_header pktb_transport_header + add2group pktb_alloc pktb_free setgroup uselessfns pktb_push add2group pktb_pull pktb_put pktb_trim setgroup tcp nfq_tcp_get_hdr
include/libnetfilter_queue/pktbuff.h +2 -0
@@ -4,6 +4,7 @@struct pkt_buff ;struct pkt_buff *pktb_alloc (int family,void *data,size_t len,size_t extra); +struct pkt_buff *pktb_alloc2 (int family,void *head,size_t headsize,void *data,size_t len,size_t extra,void *buf,size_t bufsize);void pktb_free (struct pkt_buff *pktb);uint8_t *pktb_data (struct pkt_buff *pktb);
@@ -22,5 +23,6 @@ uint8_t *pktb_transport_header(struct pkt_buff *pktb);int pktb_mangle (struct pkt_buff *pktb,int dataoff,unsigned int match_offset,unsigned int match_len,const char *rep_buffer,unsigned int rep_len);bool pktb_mangled (const struct pkt_buff *pktb); +size_t pktb_head_size (void );#endif
src/extra/pktbuff.c +171 -33
268 lines not on this page · raw message
src/nlmsg.c +9 -5
@@ -106,26 +106,30 @@ EXPORT_SYMBOL* * This code snippet uses nfq_udp_mangle_ipv4. See nf-queue.c for * context: -* \verbatim -// main calls queue_cb (line 64) to process an enqueued packet: +* \code {.c} +// main calls queue_cb (line 49) to process an enqueued packet: // Extra variables uint8_t *payload, *rep_data; unsigned int match_offset, match_len, rep_len; +char head[pktb_head_size()]; // The next line was commented-out (with payload void*) payload = mnl_attr_get_payload(attr[NFQA_PAYLOAD]); // Copy data to a packet buffer (allow 255 bytes for mangling). -pktb = pktb_alloc(AF_INET, payload, plen, 255); +#define EXTRA 255 +char pktbuf[plen + EXTRA]; +pktb = pktb_alloc2(AF_INET, head, sizeof head, payload, plen, EXTRA, +pktbuf, sizeof pktbuf); // (decide that this packet needs mangling) nfq_udp_mangle_ipv4(pktb, match_offset, match_len, rep_data, rep_len); // nfq_udp_mangle_ipv4 updates packet length, no need to track locally -// Eventually nfq_send_verdict (line 39) gets called +// Eventually nfq_send_verdict (line 24) gets called // The received packet may or may not have been modified. // Add this code before nfq_nlmsg_verdict_put call: if (pktb_mangled(pktb)) nfq_nlmsg_verdict_put_pkt(nlh, pktb_data(pktb), pktb_len(pktb)); -\endverbatim +\endcode */ void nfq_nlmsg_verdict_put_pkt (struct nlmsghdr *nlh,const void *pkt,uint32_t plen)
Signature · 1 line
2.14.5