netfilter: Create revision 2 of xt_hashlimit to support higher pps rates
Subsystems (3)
PATCH v3 2/2 netfilter: Create revision 2 of xt_hashlimit to support higher pps rates
V2:
Removed the call to BUG() in cfg_copy, we return -EINVAL instead and all calls to cfg_copy check for this
V3:
change "revision" in the call to cfg_copy inside htable_create to 2 previously this would pass down revision from the function parameter this is wrong since *cfg here is always hashlimit_cfg2
There is no change in userspace patch so V2 will work
--
netfilter: Create revision 2 of xt_hashlimit to support higher pps rates
Create a new revision for the hashlimit iptables extension module. Rev 2 will support higher pps of upto 1 million, Version 1 supports only 10k.
To support this we have to increase the size of the variables avg and burst in hashlimit_cfg to 64-bit. Create two new structs hashlimit_cfg2 and xt_hashlimit_mtinfo2 and also create newer versions of all the functions for match, checkentry and destroy.
Some of the functions like hashlimit_mt, hashlimit_mt_check etc are very similar in both rev1 and rev2 with only minor changes, so I have split those functions and moved all the common code to a *_common function.
--- include/uapi/linux/netfilter/xt_hashlimit.h | 23 ++ net/netfilter/xt_hashlimit.c | 330 ++++++++++++++++++++++------ 2 files changed, 285 insertions(+), 68 deletions(-)
include/uapi/linux/netfilter/xt_hashlimit.h +23 -0
@@ -6,6 +6,7 @@/* timings are in milliseconds. */ #define XT_HASHLIMIT_SCALE_v1 10000 +#define XT_HASHLIMIT_SCALE 1000000llu/* 1/10,000 sec period => max of 10,000/sec. Min rate is then 429490 * seconds, or one packet every 59 hours. */
@@ -63,6 +64,20 @@ struct hashlimit_cfg1 {__u8 srcmask, dstmask; }; +struct hashlimit_cfg2 { +__u32 mode;/* bitmask of XT_HASHLIMIT_HASH_* */ +__u64 avg;/* Average secs between packets * scale */ +__u64 burst;/* Period multiplier for upper limit. */ + +/* user specified */ +__u32 size;/* how many buckets */ +__u32 max;/* max number of entries */ +__u32 gc_interval;/* gc interval */ +__u32 expire;/* when do entries expire? */ + +__u8 srcmask, dstmask; +}; +struct xt_hashlimit_mtinfo1 {char name[IFNAMSIZ ];struct hashlimit_cfg1 cfg;
@@ -71,4 +86,12 @@ struct xt_hashlimit_mtinfo1 { struct xt_hashlimit_htable *hinfo __attribute__((aligned(8))); }; +struct xt_hashlimit_mtinfo2 { +char name[NAME_MAX ]; +struct hashlimit_cfg2 cfg; + +/* Used internally by the kernel */ +struct xt_hashlimit_htable *hinfo __attribute__((aligned (8 ))); +}; +#endif /* _UAPI_XT_HASHLIMIT_H */
net/netfilter/xt_hashlimit.c +262 -68
576 lines not on this page · raw message
Signature · 1 line
1.9.1
include/uapi/linux/netfilter/xt_hashlimit.h struct hashlimit_cfg1 { · line 70
Quoted 4 lines · Vishwanath Pai
include/uapi/linux/netfilter/xt_hashlimit.h +4
-0+struct hashlimit_cfg2 { +__u32 mode;/* bitmask of XT_HASHLIMIT_HASH_* */ +__u64 avg;/* Average secs between packets * scale */ +__u64 burst;/* Period multiplier for upper limit. */
This would have different sizes between i386 and x86_64, necessiting additional compat functions. It should be padded or reordered instead.
Quoted 17 lines
include/uapi/linux/netfilter/xt_hashlimit.h +17
-0+ +/* user specified */ +__u32 size;/* how many buckets */ +__u32 max;/* max number of entries */ +__u32 gc_interval;/* gc interval */ +__u32 expire;/* when do entries expire? */ + +__u8 srcmask, dstmask; +}; + +struct xt_hashlimit_mtinfo2 { +char name[NAME_MAX ]; +struct hashlimit_cfg2 cfg; + +/* Used internally by the kernel */ +struct xt_hashlimit_htable *hinfo __attribute__((aligned (8 ))); +};
Thanks for pointing this out, I will reorder the fields to:
struct hashlimit_cfg2 {
__u64 avg; /* Average secs between packets * scale */
__u64 burst;
__u32 mode; /* bitmask of XT_HASHLIMIT_HASH_* */This should fix the hole and avoid padding.
-Vishwanath
Quoted 9 lines · Jan Engelhardt
Quoted 4 lines · Vishwanath Pai
include/uapi/linux/netfilter/xt_hashlimit.h struct hashlimit_cfg1 { · line 70
Quoted 4 lines
include/uapi/linux/netfilter/xt_hashlimit.h +4
-0+struct hashlimit_cfg2 { +__u32 mode;/* bitmask of XT_HASHLIMIT_HASH_* */ +__u64 avg;/* Average secs between packets * scale */ +__u64 burst;/* Period multiplier for upper limit. */ This would have different sizes between i386 and
x86_64, necessiting additional compat functions. It should be padded or reordered instead.
Pablo Neira Ayuso
maintainerQuoted 8 lines · Vishwanath Pai
Thanks for pointing this out, I will reorder the fields to:
struct hashlimit_cfg2 { __u64 avg; /* Average secs between packets * scale */ __u64 burst; __u32 mode; /* bitmask of XT_HASHLIMIT_HASH_* */This should fix the hole and avoid padding.
I have manually mangled this here, and applied, to get this change in this in merge window.
I also have to revert the rename of XT_HASHLIMIT_SCALE to XT_HASHLIMIT_SCALE_v1. This is exposed through uapi, this change would break userspace.
Please, follow up in case of any fallout, thanks.
Pablo Neira Ayuso
maintainerQuoted 8 lines · Pablo Neira Ayuso
Quoted 7 lines · Vishwanath Pai
Thanks for pointing this out, I will reorder the fields to:
struct hashlimit_cfg2 { __u64 avg; /* Average secs between packets * scale */ __u64 burst; __u32 mode; /* bitmask of XT_HASHLIMIT_HASH_* */
Quoted 8 lines
Quoted 1 line
This should fix the hole and avoid padding.
I have manually mangled this here, and applied, to get this change in this in merge window.
I also have to revert the rename of
XT_HASHLIMIT_SCALEtoXT_HASHLIMIT_SCALE_v1. This is exposed through uapi, this change would break userspace.
This requires that you rebase and resubmit your userspace patches for iptables though.
Thanks for your patience to get this in, btw.
Hi Vishwanath,
2016-09-23 0:43 GMT+08:00 Vishwanath Pai <[email protected]>:
Quoted 19 lines
/* Precision saver. */
net/netfilter/xt_hashlimit.c +9
-6-static u32 user2credits (u32 user) +static u64 user2credits (u64 user,int revision) { -/* If multiplying would overflow... */ -if (user >0xFFFFFFFF / (HZ *CREDITS_PER_JIFFY_v1)) -/* Divide first. */ -return (user / XT_HASHLIMIT_SCALE_v1) *\ -HZ * CREDITS_PER_JIFFY_v1; +if (revision ==1 ) { +/* If multiplying would overflow... */ +if (user >0xFFFFFFFF / (HZ *CREDITS_PER_JIFFY_v1)) +/* Divide first. */ +return (user / XT_HASHLIMIT_SCALE_v1) *\ +HZ * CREDITS_PER_JIFFY_v1; + +return (user *HZ * CREDITS_PER_JIFFY_v1) \
net/netfilter/xt_hashlimit.c static u32 xt_hashlimit_len_to_chunks(u32 len) · line 482
Quoted 10 lines
net/netfilter/xt_hashlimit.c +7
-1+ / XT_HASHLIMIT_SCALE_v1; + }else { +if (user >0xFFFFFFFFFFFFFFFF / (HZ *CREDITS_PER_JIFFY )) +return (user /XT_HASHLIMIT_SCALE ) *\ +HZ *CREDITS_PER_JIFFY ; -return (user *HZ * CREDITS_PER_JIFFY_v1) / XT_HASHLIMIT_SCALE_v1; +return (user *HZ *CREDITS_PER_JIFFY ) /XT_HASHLIMIT_SCALE ; + } }
In my memory, 64-bit division operation should be replaced by div_u64 or div64_u64, otherwise on some 32-bit architecture systems, link error will happen. Something like this:
... undefined reference to `__udivdi3'.
Quoted 27 lines · Liping Zhang
Hi Vishwanath,
2016-09-23 0:43 GMT+08:00 Vishwanath Pai <[email protected]>:
Quoted 24 lines
/* Precision saver. */
net/netfilter/xt_hashlimit.c +14
-6-static u32 user2credits (u32 user) +static u64 user2credits (u64 user,int revision) { -/* If multiplying would overflow... */ -if (user >0xFFFFFFFF / (HZ *CREDITS_PER_JIFFY_v1)) -/* Divide first. */ -return (user / XT_HASHLIMIT_SCALE_v1) *\ -HZ * CREDITS_PER_JIFFY_v1; +if (revision ==1 ) { +/* If multiplying would overflow... */ +if (user >0xFFFFFFFF / (HZ *CREDITS_PER_JIFFY_v1)) +/* Divide first. */ +return (user / XT_HASHLIMIT_SCALE_v1) *\ +HZ * CREDITS_PER_JIFFY_v1; + +return (user *HZ * CREDITS_PER_JIFFY_v1) \ + / XT_HASHLIMIT_SCALE_v1; + }else { +if (user >0xFFFFFFFFFFFFFFFF / (HZ *CREDITS_PER_JIFFY )) +return (user /XT_HASHLIMIT_SCALE ) *\ +HZ *CREDITS_PER_JIFFY ;
Quoted 10 lines
net/netfilter/xt_hashlimit.c static u32 xt_hashlimit_len_to_chunks(u32 len) · line 482
Quoted 5 lines
net/netfilter/xt_hashlimit.c +2
-1-return (user *HZ * CREDITS_PER_JIFFY_v1) / XT_HASHLIMIT_SCALE_v1; +return (user *HZ *CREDITS_PER_JIFFY ) /XT_HASHLIMIT_SCALE ; + } }In my memory, 64-bit division operation should be replaced by
div_u64ordiv64_u64, otherwise on some 32-bit architecture systems, link error will happen. Something like this:
... undefined reference to `__udivdi3'.
I did not know that, thanks for pointing it out. I will send a patch to fix this.
-Vishwanath