Skip to contentSkip to conversation

netfilter: xtables: Add snapshot of hardidletimer target

PATCH Superseded Manoj Basapathi 3 participants 4 messages Feb 4, 2020

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 netfilter: xtables: Add snapshot of hardidletimer target

This is a snapshot of hardidletimer netfilter target.

This patch implements a hardidletimer Xtables target that can be used to identify when interfaces have been idle for a certain period of time.

Timers are identified by labels and are created when a rule is set with a new label. The rules also take a timeout value (in seconds) as an option. If more than one rule uses the same timer label, the timer will be restarted whenever any of the rules get a hit.

One entry for each timer is created in sysfs. This attribute contains the timer remaining for the timer to expire. The attributes are located under the xt_idletimer class:

/sys/class/xt_idletimer/timers/<label>

When the timer expires, the target module sends a sysfs notification to the userspace, which can then decide what to do (eg. disconnect to save power)

Compared to IDLETIMER, HARDIDLETIMER can send notifications when CPU is in suspend too, to notify the timer expiry.

v1->v2: Moved all functionality into IDLETIMER module to avoid code duplication per comment from Florian.

---
 include/uapi/linux/netfilter/xt_IDLETIMER.h |  11 +
 net/netfilter/xt_IDLETIMER.c                | 236 +++++++++++++++++++-
 2 files changed, 239 insertions(+), 8 deletions(-)
include/uapi/linux/netfilter/xt_IDLETIMER.h +11 -0
index 3c586a19baea..0899d140fda4 100644
@@ -33,6 +33,7 @@
 #include <linux/types.h>
 
 #define MAX_IDLETIMER_LABEL_SIZE 28
+#define XT_IDLETIMER_ALARM 0x01
 
 struct idletimer_tg_info {
 	__u32 timeout;
@@ -43,4 +44,14 @@ struct idletimer_tg_info {
 	struct idletimer_tg *timer __attribute__((aligned(8)));
 };
 
+struct idletimer_tg_info_v1 {
+	__u32 timeout;
+
+	char label[MAX_IDLETIMER_LABEL_SIZE];
+
+	__u8 timer_type;
+
+	/* for kernel module internal use only */
+	struct idletimer_tg *timer __attribute__((aligned(8)));
+};
 #endif
net/netfilter/xt_IDLETIMER.c +228 -8

317 lines not on this page · raw message

Signature · 1 line
2.25.0
#1 lore raw
Thank you for the patch! Perhaps something to improve:

Hi Manoj,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on nf-next/master]
[also build test WARNING on nf/master v5.5 next-20200204]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/Manoj-Basapathi/netfilter-xtables-Add-snapshot-of-hardidletimer-target/20200204-195814
base:   https://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next.git master
config: i386-allyesconfig (attached as .config)
compiler: gcc-7 (Debian 7.5.0-3) 7.5.0
reproduce:
        # save the attached .config to linux build tree
        make ARCH=i386 

If you fix the issue, kindly add following tag

All warnings (new ones prefixed by >>):

net/netfilter/xt_IDLETIMER.c: In function 'idletimer_tg_checkentry_v1':

>> <command-line>:0:16: warning: format '%ld' expects argument of type 'long int', but argument 3 has type 'time64_t {aka long long int}' [-Wformat=]
>> net/netfilter/xt_IDLETIMER.c:17:21: note: in expansion of macro 'KBUILD_MODNAME'
    #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
                        ^~~~~~~~~~~~~~
   include/linux/dynamic_debug.h:125:15: note: in expansion of macro 'pr_fmt'
      func(&id, ##__VA_ARGS__);  \
                  ^~~~~~~~~~~
   include/linux/dynamic_debug.h:143:2: note: in expansion of macro '__dynamic_func_call'
     __dynamic_func_call(__UNIQUE_ID(ddebug), fmt, func, ##__VA_ARGS__)
     ^~~~~~~~~~~~~~~~~~~
   include/linux/dynamic_debug.h:153:2: note: in expansion of macro '_dynamic_func_call'
     _dynamic_func_call(fmt, __dynamic_pr_debug,  \
     ^~~~~~~~~~~~~~~~~~
   include/linux/printk.h:335:2: note: in expansion of macro 'dynamic_pr_debug'
     dynamic_pr_debug(fmt, ##__VA_ARGS__)
     ^~~~~~~~~~~~~~~~
>> net/netfilter/xt_IDLETIMER.c:376:5: note: in expansion of macro 'pr_debug'
        pr_debug("time_expiry_remaining %ld\n",
        ^~~~~~~~
--
   net//netfilter/xt_IDLETIMER.c: In function 'idletimer_tg_checkentry_v1':
>> <command-line>:0:16: warning: format '%ld' expects argument of type 'long int', but argument 3 has type 'time64_t {aka long long int}' [-Wformat=]
   net//netfilter/xt_IDLETIMER.c:17:21: note: in expansion of macro 'KBUILD_MODNAME'
    #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
                        ^~~~~~~~~~~~~~
   include/linux/dynamic_debug.h:125:15: note: in expansion of macro 'pr_fmt'
      func(&id, ##__VA_ARGS__);  \
                  ^~~~~~~~~~~
   include/linux/dynamic_debug.h:143:2: note: in expansion of macro '__dynamic_func_call'
     __dynamic_func_call(__UNIQUE_ID(ddebug), fmt, func, ##__VA_ARGS__)
     ^~~~~~~~~~~~~~~~~~~
   include/linux/dynamic_debug.h:153:2: note: in expansion of macro '_dynamic_func_call'
     _dynamic_func_call(fmt, __dynamic_pr_debug,  \
     ^~~~~~~~~~~~~~~~~~
   include/linux/printk.h:335:2: note: in expansion of macro 'dynamic_pr_debug'
     dynamic_pr_debug(fmt, ##__VA_ARGS__)
     ^~~~~~~~~~~~~~~~
   net//netfilter/xt_IDLETIMER.c:376:5: note: in expansion of macro 'pr_debug'
        pr_debug("time_expiry_remaining %ld\n",
        ^~~~~~~~

vim +/KBUILD_MODNAME +17 net/netfilter/xt_IDLETIMER.c

0902b469bd2506 Luciano Coelho 2010-06-15 @17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 0902b469bd2506 Luciano Coelho 2010-06-15 18

:::::: The code at line 17 was first introduced by commit
:::::: 0902b469bd25065aa0688c3cee6f11744c817e7c netfilter: xtables: idletimer target implementation

:::::: TO: Luciano Coelho <[email protected]> :::::: CC: Patrick McHardy <[email protected]>

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org Intel Corporation

maintainer
#2 lore raw
In reply to Manoj Basapathi

net/netfilter/xt_IDLETIMER.c static int idletimer_tg_create(struct idletimer_tg_info *info) · line 235

Quoted 4 lines · Manoj Basapathi
net/netfilter/xt_IDLETIMER.c +4 -0
+	} else {
+		timer_setup(&info->timer->timer, idletimer_tg_expired, 0);
+	mod_timer(&info->timer->timer,
+		  msecs_to_jiffies(info->timeout * 1000) + jiffies);

Looks like indendation is off here.

Quoted 4 lines
net/netfilter/xt_IDLETIMER.c +4 -0
+static unsigned int idletimer_tg_target_v1(struct sk_buff *skb,
+					 const struct xt_action_param *par)
+{
+	const struct idletimer_tg_info_v1 *info = par->targinfo;

net/netfilter/xt_IDLETIMER.c static unsigned int idletimer_tg_target(struct sk_buff *skb, · line 281

Quoted 10 lines
net/netfilter/xt_IDLETIMER.c +10 -0
+
+	pr_debug("resetting timer %s, timeout period %u\n",
+		 info->label, info->timeout);
+
+	if (info->timer->timer_type & XT_IDLETIMER_ALARM) {
+		ktime_t tout = ktime_set(info->timeout, 0);
+		alarm_start_relative(&info->timer->alarm, tout);
+	} else {
+	mod_timer(&info->timer->timer,
+		  msecs_to_jiffies(info->timeout * 1000) + jiffies);

and here, then again later on.

Quoted 17 lines
net/netfilter/xt_IDLETIMER.c +17 -0
+static int idletimer_tg_checkentry_v1(const struct xt_tgchk_param *par)
+{
+	struct idletimer_tg_info_v1 *info = par->targinfo;
+	int ret;
+
+	pr_debug("checkentry targinfo%s\n", info->label);
+
+	if (info->timeout == 0) {
+		pr_debug("timeout value is zero\n");
+		return -EINVAL;
+	}
+	if (info->timeout >= INT_MAX / 1000) {
+		pr_debug("timeout value is too big\n");
+		return -EINVAL;
+	}
+	if (info->label[0] == '\0' ||
+		strnlen(info->label,

net/netfilter/xt_IDLETIMER.c static int idletimer_tg_checkentry(const struct xt_tgchk_param *par) · line 358

Quoted 10 lines
net/netfilter/xt_IDLETIMER.c +10 -0
+			MAX_IDLETIMER_LABEL_SIZE) == MAX_IDLETIMER_LABEL_SIZE) {
+		pr_debug("label is empty or not nul-terminated\n");
+		return -EINVAL;
+	}
+
+	if (info->timer_type > XT_IDLETIMER_ALARM) {
+		pr_debug("invalid value for timer type\n");
+		return -EINVAL;
+	}
+

This looks like a lot of code duplication with v0 version of the target. Any chance for code re-use?

The v1 struct you made is cast-able to v0 for timeout and label checks, so you could try and split that to a helper that you can then call from existing checkentry and the new one.

You can do this in a preparation patch.

Rest looks fine to me.

#3 lore raw
In reply to Manoj Basapathi
Thank you for the patch! Perhaps something to improve:

Hi Manoj,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on nf-next/master]
[also build test WARNING on nf/master v5.5 next-20200204]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/Manoj-Basapathi/netfilter-xtables-Add-snapshot-of-hardidletimer-target/20200204-195814
base:   https://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next.git master
config: m68k-multi_defconfig (attached as .config)
compiler: m68k-linux-gcc (GCC) 7.5.0

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=7.5.0 make.cross ARCH=m68k

If you fix the issue, kindly add following tag

All warnings (new ones prefixed by >>):

In file included from include/linux/kernel.h:15:0,
from include/linux/list.h:9,
from include/linux/module.h:12,
from net/netfilter/xt_IDLETIMER.c:19:
net/netfilter/xt_IDLETIMER.c: In function 'idletimer_tg_checkentry_v1':

>> include/linux/kern_levels.h:5:18: warning: format '%ld' expects argument of type 'long int', but argument 2 has type 'time64_t {aka long long int}' [-Wformat=]
    #define KERN_SOH "\001"  /* ASCII Start Of Header */
                     ^
   include/linux/printk.h:137:10: note: in definition of macro 'no_printk'
      printk(fmt, ##__VA_ARGS__);  \
             ^~~
   include/linux/kern_levels.h:15:20: note: in expansion of macro 'KERN_SOH'
    #define KERN_DEBUG KERN_SOH "7" /* debug-level messages */
                       ^~~~~~~~
   include/linux/printk.h:341:12: note: in expansion of macro 'KERN_DEBUG'
     no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
               ^~~~~~~~~~
   net/netfilter/xt_IDLETIMER.c:376:5: note: in expansion of macro 'pr_debug'
        pr_debug("time_expiry_remaining %ld\n",
        ^~~~~~~~

--
In file included from include/linux/kernel.h:15:0,
from include/linux/list.h:9,
from include/linux/module.h:12,
from net//netfilter/xt_IDLETIMER.c:19:
net//netfilter/xt_IDLETIMER.c: In function 'idletimer_tg_checkentry_v1':

>> include/linux/kern_levels.h:5:18: warning: format '%ld' expects argument of type 'long int', but argument 2 has type 'time64_t {aka long long int}' [-Wformat=]
    #define KERN_SOH "\001"  /* ASCII Start Of Header */
                     ^
   include/linux/printk.h:137:10: note: in definition of macro 'no_printk'
      printk(fmt, ##__VA_ARGS__);  \
             ^~~
   include/linux/kern_levels.h:15:20: note: in expansion of macro 'KERN_SOH'
    #define KERN_DEBUG KERN_SOH "7" /* debug-level messages */
                       ^~~~~~~~
   include/linux/printk.h:341:12: note: in expansion of macro 'KERN_DEBUG'
     no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
               ^~~~~~~~~~
   net//netfilter/xt_IDLETIMER.c:376:5: note: in expansion of macro 'pr_debug'
        pr_debug("time_expiry_remaining %ld\n",
        ^~~~~~~~

vim +5 include/linux/kern_levels.h

314ba3520e513a Joe Perches 2012-07-30 4
04d2c8c83d0e3a Joe Perches 2012-07-30 @5 #define KERN_SOH "\001" /* ASCII Start Of Header */
04d2c8c83d0e3a Joe Perches 2012-07-30 6 #define KERN_SOH_ASCII '\001'
04d2c8c83d0e3a Joe Perches 2012-07-30 7

:::::: The code at line 5 was first introduced by commit
:::::: 04d2c8c83d0e3ac5f78aeede51babb3236200112 printk: convert the format for KERN_<LEVEL> to a 2 byte pattern

:::::: TO: Joe Perches <[email protected]>
:::::: CC: Linus Torvalds <[email protected]>

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org Intel Corporation