]>
Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
72205fc6 JK |
2 | #ifndef _IP_SET_TIMEOUT_H |
3 | #define _IP_SET_TIMEOUT_H | |
4 | ||
075e64c0 | 5 | /* Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu> |
72205fc6 JK |
6 | */ |
7 | ||
8 | #ifdef __KERNEL__ | |
9 | ||
10 | /* How often should the gc be run by default */ | |
11 | #define IPSET_GC_TIME (3 * 60) | |
12 | ||
13 | /* Timeout period depending on the timeout value of the given set */ | |
14 | #define IPSET_GC_PERIOD(timeout) \ | |
15 | ((timeout/3) ? min_t(u32, (timeout)/3, IPSET_GC_TIME) : 1) | |
16 | ||
075e64c0 JK |
17 | /* Entry is set with no timeout value */ |
18 | #define IPSET_ELEM_PERMANENT 0 | |
72205fc6 | 19 | |
075e64c0 JK |
20 | /* Set is defined with timeout support: timeout value may be 0 */ |
21 | #define IPSET_NO_TIMEOUT UINT_MAX | |
72205fc6 | 22 | |
30a2e107 JK |
23 | /* Max timeout value, see msecs_to_jiffies() in jiffies.h */ |
24 | #define IPSET_MAX_TIMEOUT (UINT_MAX >> 1)/MSEC_PER_SEC | |
25 | ||
ca134ce8 JK |
26 | #define ip_set_adt_opt_timeout(opt, set) \ |
27 | ((opt)->ext.timeout != IPSET_NO_TIMEOUT ? (opt)->ext.timeout : (set)->timeout) | |
ac8cc925 | 28 | |
72205fc6 JK |
29 | static inline unsigned int |
30 | ip_set_timeout_uget(struct nlattr *tb) | |
31 | { | |
32 | unsigned int timeout = ip_set_get_h32(tb); | |
33 | ||
127f5591 | 34 | /* Normalize to fit into jiffies */ |
30a2e107 JK |
35 | if (timeout > IPSET_MAX_TIMEOUT) |
36 | timeout = IPSET_MAX_TIMEOUT; | |
127f5591 | 37 | |
30a2e107 | 38 | return timeout; |
72205fc6 JK |
39 | } |
40 | ||
72205fc6 | 41 | static inline bool |
da9fbfa7 | 42 | ip_set_timeout_expired(const unsigned long *t) |
72205fc6 | 43 | { |
b57b2d1f | 44 | return *t != IPSET_ELEM_PERMANENT && time_is_before_jiffies(*t); |
72205fc6 JK |
45 | } |
46 | ||
075e64c0 | 47 | static inline void |
b57b2d1f | 48 | ip_set_timeout_set(unsigned long *timeout, u32 value) |
72205fc6 | 49 | { |
b57b2d1f JK |
50 | unsigned long t; |
51 | ||
52 | if (!value) { | |
075e64c0 JK |
53 | *timeout = IPSET_ELEM_PERMANENT; |
54 | return; | |
55 | } | |
72205fc6 | 56 | |
b57b2d1f JK |
57 | t = msecs_to_jiffies(value * MSEC_PER_SEC) + jiffies; |
58 | if (t == IPSET_ELEM_PERMANENT) | |
72205fc6 | 59 | /* Bingo! :-) */ |
b57b2d1f JK |
60 | t--; |
61 | *timeout = t; | |
72205fc6 JK |
62 | } |
63 | ||
64 | static inline u32 | |
da9fbfa7 | 65 | ip_set_timeout_get(const unsigned long *timeout) |
72205fc6 | 66 | { |
bd975e69 JK |
67 | u32 t; |
68 | ||
69 | if (*timeout == IPSET_ELEM_PERMANENT) | |
70 | return 0; | |
71 | ||
72 | t = jiffies_to_msecs(*timeout - jiffies)/MSEC_PER_SEC; | |
73 | /* Zero value in userspace means no timeout */ | |
74 | return t == 0 ? 1 : t; | |
72205fc6 | 75 | } |
72205fc6 JK |
76 | |
77 | #endif /* __KERNEL__ */ | |
72205fc6 | 78 | #endif /* _IP_SET_TIMEOUT_H */ |