diff --git a/src/cmdline.c b/src/cmdline.c index 9f3f3d4..0781307 100644 --- a/src/cmdline.c +++ b/src/cmdline.c @@ -1,4 +1,5 @@ #include +#include #include #include "cmdline.h" @@ -8,6 +9,7 @@ const struct option opts[] = {"config", required_argument, NULL, 'c'}, {"offload", no_argument, NULL, 'o'}, {"skb", no_argument, NULL, 's'}, + {"time", required_argument, NULL, 't'}, {"list", no_argument, NULL, 'l'}, {"help", no_argument, NULL, 'h'}, {NULL, 0, NULL, 0} @@ -24,7 +26,7 @@ void parsecommandline(struct cmdline *cmd, int argc, char *argv[]) { int c; - while ((c = getopt_long(argc, argv, "c:oslh", opts, NULL)) != -1) + while ((c = getopt_long(argc, argv, "c:ost:lh", opts, NULL)) != -1) { switch (c) { @@ -43,6 +45,11 @@ void parsecommandline(struct cmdline *cmd, int argc, char *argv[]) break; + case 't': + cmd->time = atoi(optarg); + + break; + case 'l': cmd->list = 1; diff --git a/src/cmdline.h b/src/cmdline.h index b7f0c3e..4425c8d 100644 --- a/src/cmdline.h +++ b/src/cmdline.h @@ -5,6 +5,7 @@ struct cmdline char *cfgfile; unsigned int offload : 1; unsigned int skb : 1; + unsigned int time; unsigned int list : 1; unsigned int help : 1; }; diff --git a/src/xdpfw.c b/src/xdpfw.c index 348620e..3574683 100644 --- a/src/xdpfw.c +++ b/src/xdpfw.c @@ -311,6 +311,7 @@ int main(int argc, char *argv[]) "--config -c => Config file location (default is /etc/xdpfw/xdpfw.conf).\n" \ "--offload -o => Tries to load the XDP program in hardware/offload mode." \ "--skb -s => Force the XDP program to load with SKB mode instead of DRV." \ + "--time -t => How long to run the program for in seconds before exiting. 0 or not set = infinite.\n" \ "--list -l => Print config details including filters (this will exit program after done).\n" \ "--help -h => Print help menu.\n"); @@ -471,11 +472,19 @@ int main(int argc, char *argv[]) // Receive CPU count for stats map parsing. int cpus = get_nprocs_conf(); + unsigned int endTime = (cmd.time > 0) ? time(NULL) + cmd.time : 0; + while (cont) { // Get current time. time_t curTime = time(NULL); + // Check if we should end the program. + if (endTime > 0 && curTime >= endTime) + { + break; + } + // Check for auto-update. if (cfg.updatetime > 0 && (curTime - lastupdated) > cfg.updatetime) {