xtables: Add a smaller delay option when waiting for xtables lock
PATCH v2 1/1 xtables: Add a smaller delay option when waiting for xtables lock
ip[6]tables currently waits for 1 second for the xtables lock to be freed if the -w option is used. We have seen that the lock is held much less than that resulting in unnecessary delay when trying to acquire the lock. This problem is even severe in case of latency sensitive applications.
Introduce an delay option with 10ms granularity (experimentally determined) by specifying the delay in [seconds.milliseconds] as an argument to -w. If milliseconds are not specified, the existing behavior of 1 second sleep is preserved.
v1->v2: Change behavior to take millisecond sleep as an argument to -w as suggested by Pablo. Also maintain current behavior for -w to sleep for 1 second as mentioned by Liping.
--- iptables/ip6tables.c | 12 +++++++----- iptables/iptables.c | 12 +++++++----- iptables/xshared.c | 27 ++++++++++++++++++++------- iptables/xshared.h | 2 +- iptables/xtables.c | 6 +++--- 5 files changed, 38 insertions(+), 21 deletions(-)
iptables/ip6tables.c +7 -5
@@ -259,7 +259,9 @@ exit_printhelp(const struct xtables_rule_match *matches)" network interface name ([+] for wildcard)\n" " --table -t table table to manipulate (default: `filter')\n" " --verbose -v verbose mode\n" -" --wait -w [seconds] wait for the xtables lock\n" +" --wait -w [seconds.milliseconds]\n" +" wait for the xtables lock\n" +" 10ms is the minimum millisecond resolution\n" " --line-numbers print line numbers when listing\n" " --exact -x expand numbers (display exact values)\n" /*"[!] --fragment -f match second or further fragments only\n"*/
@@ -1324,7 +1326,7 @@ int do_command6(int argc, char *argv[], char **table,struct in6_addr *smasks =NULL , *dmasks =NULL ;int verbose =0 ; -int wait =0 ; +double wait =0 ;const char *chain =NULL ;const char *shostnetworkmask =NULL , *dhostnetworkmask =NULL ;const char *policy =NULL , *newname =NULL ;
@@ -1606,12 +1608,12 @@ int do_command6(int argc, char *argv[], char **table, } wait =-1 ;if (optarg) { -if (sscanf (optarg,"%i" , &wait) !=1 ) +if (sscanf (optarg,"%lf" , &wait) !=1 )xtables_error (PARAMETER_PROBLEM ,"wait seconds not numeric" ); }else if (optind < argc && argv[optind][0 ] !='-' && argv[optind][0 ] !='!' ) -if (sscanf (argv[optind++],"%i" , &wait) !=1 ) +if (sscanf (argv[optind++],"%lf" , &wait) !=1 )xtables_error (PARAMETER_PROBLEM ,"wait seconds not numeric" );break ;
@@ -1768,7 +1770,7 @@ int do_command6(int argc, char *argv[], char **table,if (wait ==0 )fprintf (stderr,"Perhaps you want to use the -w option?\n" );else -fprintf (stderr,"Stopped waiting after %ds.\n" , wait); +fprintf (stderr,"Stopped waiting after %fs.\n" , wait);xtables_free_opts (1 );exit (RESOURCE_PROBLEM ); }
iptables/iptables.c +7 -5
@@ -253,7 +253,9 @@ exit_printhelp(const struct xtables_rule_match *matches)" network interface name ([+] for wildcard)\n" " --table -t table table to manipulate (default: `filter')\n" " --verbose -v verbose mode\n" -" --wait -w [seconds] wait for the xtables lock\n" +" --wait -w [seconds.milliseconds]\n" +" wait for the xtables lock\n" +" 10ms is the minimum millisecond resolution\n" " --line-numbers print line numbers when listing\n" " --exact -x expand numbers (display exact values)\n" "[!] --fragment -f match second or further fragments only\n"
@@ -1320,7 +1322,7 @@ int do_command4(int argc, char *argv[], char **table,struct in_addr *daddrs =NULL , *dmasks =NULL ;int verbose =0 ; -int wait =0 ; +double wait =0 ;const char *chain =NULL ;const char *shostnetworkmask =NULL , *dhostnetworkmask =NULL ;const char *policy =NULL , *newname =NULL ;
@@ -1599,12 +1601,12 @@ int do_command4(int argc, char *argv[], char **table, } wait =-1 ;if (optarg) { -if (sscanf (optarg,"%i" , &wait) !=1 ) +if (sscanf (optarg,"%lf" , &wait) !=1 )xtables_error (PARAMETER_PROBLEM ,"wait seconds not numeric" ); }else if (optind < argc && argv[optind][0 ] !='-' && argv[optind][0 ] !='!' ) -if (sscanf (argv[optind++],"%i" , &wait) !=1 ) +if (sscanf (argv[optind++],"%lf" , &wait) !=1 )xtables_error (PARAMETER_PROBLEM ,"wait seconds not numeric" );break ;
@@ -1764,7 +1766,7 @@ int do_command4(int argc, char *argv[], char **table,if (wait ==0 )fprintf (stderr,"Perhaps you want to use the -w option?\n" );else -fprintf (stderr,"Stopped waiting after %ds.\n" , wait); +fprintf (stderr,"Stopped waiting after %fs.\n" , wait);xtables_free_opts (1 );exit (RESOURCE_PROBLEM ); }
iptables/xshared.c +20 -7
@@ -15,6 +15,8 @@#include "xshared.h" #define XT_LOCK_NAME "/run/xtables.lock" +#define BASE_MICROSECOND_WAIT 10000 +#define WAIT_PRECISION 100/* * Print out any special helps. A user might like to be able to add a --help
@@ -244,9 +246,11 @@ void xs_init_match(struct xtables_match *match) match->init (match->m); } -bool xtables_lock (int wait) +bool xtables_lock (double wait) { -int fd, waited =0 , i =0 ; +int fd, waited =0 , i =0 , us_wait =0 , us_waited =0 ; + + us_wait = (((int )(wait*WAIT_PRECISION ))%WAIT_PRECISION )*BASE_MICROSECOND_WAIT ; fd =open (XT_LOCK_NAME ,O_CREAT ,0600 );if (fd <0 )
@@ -255,12 +259,21 @@ bool xtables_lock(int wait)while (1 ) {if (flock (fd,LOCK_EX |LOCK_NB ) ==0 )return true; -else if (wait >=0 && waited >= wait) +else if (wait >=0 && waited >= (int )wait && us_waited >= us_wait)return false; -if (++i %2 ==0 ) +if ((++i %2 ==0 && !us_wait) || (++i %10 ==0 ))fprintf (stderr,"Another app is currently holding the xtables lock; " -"waiting (%ds) for it to exit...\n" , waited); - waited++; -sleep (1 ); +"waiting (%ds %dms) for it to exit...\n" , waited, us_waited/1000 ); +if (us_wait) { + us_waited +=BASE_MICROSECOND_WAIT ; +usleep (BASE_MICROSECOND_WAIT ); +if (us_waited ==1000000 ) { + waited++; + us_waited =0 ; + } + }else { + waited++; +sleep (1 ); + } } }
iptables/xshared.h +1 -1
@@ -85,7 +85,7 @@ extern struct xtables_match *load_proto(struct iptables_command_state *);extern int subcmd_main (int ,char **,const struct subcommand *);extern void xs_init_target (struct xtables_target *);extern void xs_init_match (struct xtables_match *); -extern bool xtables_lock (int wait); +extern bool xtables_lock (double wait);extern const struct xtables_afinfo *afinfo;
iptables/xtables.c +3 -3
@@ -686,7 +686,7 @@ void do_parse(struct nft_handle *h, int argc, char *argv[],struct xtables_match *m;struct xtables_rule_match *matchp;struct xtables_target *t; -int wait =0 ; +double wait =0 ;memset (cs,0 ,sizeof (*cs)); cs->jumpto ="" ;
@@ -1009,12 +1009,12 @@ void do_parse(struct nft_handle *h, int argc, char *argv[], "iptables-restore"); }if (optarg) { -if (sscanf (optarg,"%i" , &wait) !=1 ) +if (sscanf (optarg,"%lf" , &wait) !=1 )xtables_error (PARAMETER_PROBLEM ,"wait seconds not numeric" ); }else if (optind < argc && argv[optind][0 ] !='-' && argv[optind][0 ] !='!' ) -if (sscanf (argv[optind++],"%i" , &wait) !=1 ) +if (sscanf (argv[optind++],"%lf" , &wait) !=1 )xtables_error (PARAMETER_PROBLEM ,"wait seconds not numeric" );break ;
Signature · 1 line
1.8.2.1
Pablo Neira Ayuso
maintainerHi Subash,
Quoted 6 lines · Subash Abhinov Kasiviswanathan
ip[6]tables currently waits for 1 second for the xtables lock to be freed if the -w option is used. We have seen that the lock is held much less than that resulting in unnecessary delay when trying to acquire the lock. This problem is even severe in case of latency sensitive applications.
Quoted 8 lines
Introduce an delay option with 10ms granularity (experimentally determined) by specifying the delay in [seconds.milliseconds] as an argument to -w. If milliseconds are not specified, the existing behavior of 1 second sleep is preserved.
v1->v2: Change behavior to take millisecond sleep as an argument to -w as suggested by Pablo. Also maintain current behavior for -w to sleep for 1 second as mentioned by Liping.
My impression after looking at this patch is that you can simplify it using 'struct timeval' and use select() to wait for the time you need.
Quoted 205 lines
Cc Liping Zhang <[email protected]>Cc Pablo Neira Ayuso <[email protected]>Signed-off-by Subash Abhinov Kasiviswanathan <[email protected]>--- iptables/ip6tables.c | 12 +++++++----- iptables/iptables.c | 12 +++++++----- iptables/xshared.c | 27 ++++++++++++++++++++------- iptables/xshared.h | 2 +- iptables/xtables.c | 6 +++--- 5 files changed, 38 insertions(+), 21 deletions(-)iptables/ip6tables.c +7
-5@@ -259,7 +259,9 @@ exit_printhelp(const struct xtables_rule_match *matches)" network interface name ([+] for wildcard)\n" " --table -t table table to manipulate (default: `filter')\n" " --verbose -v verbose mode\n" -" --wait -w [seconds] wait for the xtables lock\n" +" --wait -w [seconds.milliseconds]\n" +" wait for the xtables lock\n" +" 10ms is the minimum millisecond resolution\n" " --line-numbers print line numbers when listing\n" " --exact -x expand numbers (display exact values)\n" /*"[!] --fragment -f match second or further fragments only\n"*/ @@ -1324,7 +1326,7 @@ int do_command6(int argc, char *argv[], char **table,struct in6_addr *smasks =NULL , *dmasks =NULL ;int verbose =0 ; -int wait =0 ; +double wait =0 ;const char *chain =NULL ;const char *shostnetworkmask =NULL , *dhostnetworkmask =NULL ;const char *policy =NULL , *newname =NULL ;@@ -1606,12 +1608,12 @@ int do_command6(int argc, char *argv[], char **table, } wait =-1 ;if (optarg) { -if (sscanf (optarg,"%i" , &wait) !=1 ) +if (sscanf (optarg,"%lf" , &wait) !=1 )xtables_error (PARAMETER_PROBLEM ,"wait seconds not numeric" ); }else if (optind < argc && argv[optind][0 ] !='-' && argv[optind][0 ] !='!' ) -if (sscanf (argv[optind++],"%i" , &wait) !=1 ) +if (sscanf (argv[optind++],"%lf" , &wait) !=1 )xtables_error (PARAMETER_PROBLEM ,"wait seconds not numeric" );break ;@@ -1768,7 +1770,7 @@ int do_command6(int argc, char *argv[], char **table,if (wait ==0 )fprintf (stderr,"Perhaps you want to use the -w option?\n" );else -fprintf (stderr,"Stopped waiting after %ds.\n" , wait); +fprintf (stderr,"Stopped waiting after %fs.\n" , wait);xtables_free_opts (1 );exit (RESOURCE_PROBLEM ); }iptables/iptables.c +7
-5@@ -253,7 +253,9 @@ exit_printhelp(const struct xtables_rule_match *matches)" network interface name ([+] for wildcard)\n" " --table -t table table to manipulate (default: `filter')\n" " --verbose -v verbose mode\n" -" --wait -w [seconds] wait for the xtables lock\n" +" --wait -w [seconds.milliseconds]\n" +" wait for the xtables lock\n" +" 10ms is the minimum millisecond resolution\n" " --line-numbers print line numbers when listing\n" " --exact -x expand numbers (display exact values)\n" "[!] --fragment -f match second or further fragments only\n" @@ -1320,7 +1322,7 @@ int do_command4(int argc, char *argv[], char **table,struct in_addr *daddrs =NULL , *dmasks =NULL ;int verbose =0 ; -int wait =0 ; +double wait =0 ;const char *chain =NULL ;const char *shostnetworkmask =NULL , *dhostnetworkmask =NULL ;const char *policy =NULL , *newname =NULL ;@@ -1599,12 +1601,12 @@ int do_command4(int argc, char *argv[], char **table, } wait =-1 ;if (optarg) { -if (sscanf (optarg,"%i" , &wait) !=1 ) +if (sscanf (optarg,"%lf" , &wait) !=1 )xtables_error (PARAMETER_PROBLEM ,"wait seconds not numeric" ); }else if (optind < argc && argv[optind][0 ] !='-' && argv[optind][0 ] !='!' ) -if (sscanf (argv[optind++],"%i" , &wait) !=1 ) +if (sscanf (argv[optind++],"%lf" , &wait) !=1 )xtables_error (PARAMETER_PROBLEM ,"wait seconds not numeric" );break ;@@ -1764,7 +1766,7 @@ int do_command4(int argc, char *argv[], char **table,if (wait ==0 )fprintf (stderr,"Perhaps you want to use the -w option?\n" );else -fprintf (stderr,"Stopped waiting after %ds.\n" , wait); +fprintf (stderr,"Stopped waiting after %fs.\n" , wait);xtables_free_opts (1 );exit (RESOURCE_PROBLEM ); }iptables/xshared.c +20
-7@@ -15,6 +15,8 @@#include "xshared.h" #define XT_LOCK_NAME "/run/xtables.lock" +#define BASE_MICROSECOND_WAIT 10000 +#define WAIT_PRECISION 100/* * Print out any special helps. A user might like to be able to add a --help @@ -244,9 +246,11 @@ void xs_init_match(struct xtables_match *match) match->init (match->m); } -bool xtables_lock (int wait) +bool xtables_lock (double wait) { -int fd, waited =0 , i =0 ; +int fd, waited =0 , i =0 , us_wait =0 , us_waited =0 ; + + us_wait = (((int )(wait*WAIT_PRECISION ))%WAIT_PRECISION )*BASE_MICROSECOND_WAIT ; fd =open (XT_LOCK_NAME ,O_CREAT ,0600 );if (fd <0 )@@ -255,12 +259,21 @@ bool xtables_lock(int wait)while (1 ) {if (flock (fd,LOCK_EX |LOCK_NB ) ==0 )return true; -else if (wait >=0 && waited >= wait) +else if (wait >=0 && waited >= (int )wait && us_waited >= us_wait)return false; -if (++i %2 ==0 ) +if ((++i %2 ==0 && !us_wait) || (++i %10 ==0 ))fprintf (stderr,"Another app is currently holding the xtables lock; " -"waiting (%ds) for it to exit...\n" , waited); - waited++; -sleep (1 ); +"waiting (%ds %dms) for it to exit...\n" , waited, us_waited/1000 ); +if (us_wait) { + us_waited +=BASE_MICROSECOND_WAIT ; +usleep (BASE_MICROSECOND_WAIT ); +if (us_waited ==1000000 ) { + waited++; + us_waited =0 ; + } + }else { + waited++; +sleep (1 ); + } } }iptables/xshared.h +1
-1@@ -85,7 +85,7 @@ extern struct xtables_match *load_proto(struct iptables_command_state *);extern int subcmd_main (int ,char **,const struct subcommand *);extern void xs_init_target (struct xtables_target *);extern void xs_init_match (struct xtables_match *); -extern bool xtables_lock (int wait); +extern bool xtables_lock (double wait);extern const struct xtables_afinfo *afinfo;iptables/xtables.c +3
-3@@ -686,7 +686,7 @@ void do_parse(struct nft_handle *h, int argc, char *argv[],struct xtables_match *m;struct xtables_rule_match *matchp;struct xtables_target *t; -int wait =0 ; +double wait =0 ;memset (cs,0 ,sizeof (*cs)); cs->jumpto ="" ;@@ -1009,12 +1009,12 @@ void do_parse(struct nft_handle *h, int argc, char *argv[], "iptables-restore"); }if (optarg) { -if (sscanf (optarg,"%i" , &wait) !=1 ) +if (sscanf (optarg,"%lf" , &wait) !=1 )xtables_error (PARAMETER_PROBLEM ,"wait seconds not numeric" ); }else if (optind < argc && argv[optind][0 ] !='-' && argv[optind][0 ] !='!' ) -if (sscanf (argv[optind++],"%i" , &wait) !=1 ) +if (sscanf (argv[optind++],"%lf" , &wait) !=1 )xtables_error (PARAMETER_PROBLEM ,"wait seconds not numeric" );break ;Signature · 1 line
1.8.2.1
Quoted 4 lines
My impression after looking at this patch is that you can simplify it using 'struct timeval' and use
select()to wait for the time you need.
Hi Pablo
Sure, I can send a v3 with the select. Just to clarify, here is what the patch does -
iptables -w 2.1
0.01s sleep, retry
0.02s sleep, retry
0.03s sleep, retry
...
2.1s sleep, exit
Note that it sleeps for 10ms and retries rather than sleeping for a single iteration for 2.1s seconds and then retrying.
Let me know if there are any concerns with this.
--
Qualcomm Innovation Center, Inc.
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project.
Pablo Neira Ayuso
maintainerQuoted 18 lines · On Mon, May 16, 2016 at 09:30:15PM -0600, [email protected] wrote:
Quoted 4 lines
My impression after looking at this patch is that you can simplify it using 'struct timeval' and use
select()to wait for the time you need.Hi Pablo
Sure, I can send a v3 with the select. Just to clarify, here is what the patch does -
iptables -w 2.1
0.01s sleep, retry
0.02s sleep, retry
0.03s sleep, retry
...
2.1s sleep, exit
Quoted 4 lines
Note that it sleeps for 10ms and retries rather than sleeping for a single iteration for 2.1s seconds and then retrying.
Let me know if there are any concerns with this.
But this is changing the existing behaviour, right? My understanding is that -w indicates the net wait time for each try.
Quoted 8 lines
Quoted 8 lines
iptables -w 2.1
0.01s sleep, retry
0.02s sleep, retry
0.03s sleep, retry
...
2.1s sleep, exit
Quoted 7 lines
Quoted 4 lines
Note that it sleeps for 10ms and retries rather than sleeping for a single iteration for 2.1s seconds and then retrying.
Let me know if there are any concerns with this.
But this is changing the existing behaviour, right? My understanding is that -w indicates the net wait time for each try.
Hi Pablo
Currently, each wait time is 1 second. -w is the overall time upto which it has to wait.
bool xtables_lock(int wait)
{
int fd, waited = 0, i = 0;
fd = open(XT_LOCK_NAME, O_CREAT, 0600);
if (fd < 0)
return true;
while (1) {
if (flock(fd, LOCK_EX | LOCK_NB) == 0)
return true;
else if (wait >= 0 && waited >= wait) //total time upto which we need
to wait.
return false;
if (++i % 2 == 0)
fprintf(stderr, "Another app is currently holding the xtables lock; "
"waiting (%ds) for it to exit...\n", waited);
waited++;
sleep(1); //sleep for one second only
}
}My patch does not change the behavior of -w itself. It only changes the sleep interval to 10ms when a decimal is specified. Existing behavior of 1 second sleep for integral interval is preserved.
--
Qualcomm Innovation Center, Inc.
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project.
Quoted 44 lines · On 2016-05-17 12:25, [email protected] wrote:
Quoted 8 lines
Quoted 8 lines
iptables -w 2.1
0.01s sleep, retry
0.02s sleep, retry
0.03s sleep, retry
...
2.1s sleep, exitQuoted 7 lines
Quoted 4 lines
Note that it sleeps for 10ms and retries rather than sleeping for a single iteration for 2.1s seconds and then retrying.
Let me know if there are any concerns with this.
But this is changing the existing behaviour, right? My understanding is that -w indicates the net wait time for each try.
Hi Pablo
Currently, each wait time is 1 second. -w is the overall time upto which it has to wait.
bool
xtables_lock(int wait){ int fd, waited = 0, i = 0; fd = open(XT_LOCK_NAME, O_CREAT, 0600); if (fd < 0) return true; while (1) { if (flock(fd, LOCK_EX | LOCK_NB) == 0) return true; else if (wait >= 0 && waited >= wait) //total time upto which we need to wait. return false; if (++i % 2 == 0) fprintf(stderr, "Another app is currently holding the xtables lock; " "waiting (%ds) for it to exit...\n", waited); waited++; sleep(1); //sleep for one second only } }
Quoted 4 lines
My patch does not change the behavior of -w itself. It only changes the sleep interval to 10ms when a decimal is specified. Existing behavior of 1 second sleep for integral interval is preserved.
Hi Pablo
Please let me know if you have any other concerns. Also, can you tell me if I still need to change the sleep to select. I feel sleep / usleep should suffice here.
--
Qualcomm Innovation Center, Inc.
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project.
Pablo Neira Ayuso
maintainerQuoted 44 lines · On Tue, May 17, 2016 at 12:25:53PM -0600, [email protected] wrote:
Quoted 8 lines
Quoted 8 lines
iptables -w 2.1
0.01s sleep, retry
0.02s sleep, retry
0.03s sleep, retry
...
2.1s sleep, exitQuoted 8 lines
Quoted 5 lines
Note that it sleeps for 10ms and retries rather than sleeping for a single
iteration for 2.1s seconds and then retrying.Let me know if there are any concerns with this.
But this is changing the existing behaviour, right? My understanding is that -w indicates the net wait time for each try.
Hi Pablo
Currently, each wait time is 1 second. -w is the overall time upto which it has to wait.
bool
xtables_lock(int wait){ int fd, waited = 0, i = 0; fd = open(XT_LOCK_NAME, O_CREAT, 0600); if (fd < 0) return true; while (1) { if (flock(fd, LOCK_EX | LOCK_NB) == 0) return true; else if (wait >= 0 && waited >= wait) //total time upto which we need to wait. return false; if (++i % 2 == 0) fprintf(stderr, "Another app is currently holding the xtables lock; " "waiting (%ds) for it to exit...\n", waited); waited++; sleep(1); //sleep for one second only } }
Quoted 2 lines
My patch does not change the behavior of -w itself. It only changes the sleep interval to 10ms when a decimal is specified.
Sorry, then I misunderstood the semantics of -w, I remembered this specified the sleep interval but I was wrong.
Quoted 1 line
Existing behavior of 1 second sleep for integral interval is preserved.
Then, I'd suggest you add a new specific option to specify the interval, allowing this sec.msecs notation, preserving the 1 second interval as default.
Thanks.