1 #ifndef _IP_SET_TIMEOUT_H
2 #define _IP_SET_TIMEOUT_H
3 
4 /* Copyright (C) 2003-2011 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 
11 #ifdef __KERNEL__
12 
13 /* How often should the gc be run by default */
14 #define IPSET_GC_TIME			(3 * 60)
15 
16 /* Timeout period depending on the timeout value of the given set */
17 #define IPSET_GC_PERIOD(timeout) \
18 	((timeout/3) ? min_t(u32, (timeout)/3, IPSET_GC_TIME) : 1)
19 
20 /* Set is defined without timeout support: timeout value may be 0 */
21 #define IPSET_NO_TIMEOUT	UINT_MAX
22 
23 #define with_timeout(timeout)	((timeout) != IPSET_NO_TIMEOUT)
24 
25 #define opt_timeout(opt, map)	\
26 	(with_timeout((opt)->timeout) ? (opt)->timeout : (map)->timeout)
27 
28 static inline unsigned int
ip_set_timeout_uget(struct nlattr * tb)29 ip_set_timeout_uget(struct nlattr *tb)
30 {
31 	unsigned int timeout = ip_set_get_h32(tb);
32 
33 	/* Userspace supplied TIMEOUT parameter: adjust crazy size */
34 	return timeout == IPSET_NO_TIMEOUT ? IPSET_NO_TIMEOUT - 1 : timeout;
35 }
36 
37 #ifdef IP_SET_BITMAP_TIMEOUT
38 
39 /* Bitmap specific timeout constants and macros for the entries */
40 
41 /* Bitmap entry is unset */
42 #define IPSET_ELEM_UNSET	0
43 /* Bitmap entry is set with no timeout value */
44 #define IPSET_ELEM_PERMANENT	(UINT_MAX/2)
45 
46 static inline bool
ip_set_timeout_test(unsigned long timeout)47 ip_set_timeout_test(unsigned long timeout)
48 {
49 	return timeout != IPSET_ELEM_UNSET &&
50 	       (timeout == IPSET_ELEM_PERMANENT ||
51 		time_is_after_jiffies(timeout));
52 }
53 
54 static inline bool
ip_set_timeout_expired(unsigned long timeout)55 ip_set_timeout_expired(unsigned long timeout)
56 {
57 	return timeout != IPSET_ELEM_UNSET &&
58 	       timeout != IPSET_ELEM_PERMANENT &&
59 	       time_is_before_jiffies(timeout);
60 }
61 
62 static inline unsigned long
ip_set_timeout_set(u32 timeout)63 ip_set_timeout_set(u32 timeout)
64 {
65 	unsigned long t;
66 
67 	if (!timeout)
68 		return IPSET_ELEM_PERMANENT;
69 
70 	t = msecs_to_jiffies(timeout * 1000) + jiffies;
71 	if (t == IPSET_ELEM_UNSET || t == IPSET_ELEM_PERMANENT)
72 		/* Bingo! */
73 		t++;
74 
75 	return t;
76 }
77 
78 static inline u32
ip_set_timeout_get(unsigned long timeout)79 ip_set_timeout_get(unsigned long timeout)
80 {
81 	return timeout == IPSET_ELEM_PERMANENT ? 0 :
82 		jiffies_to_msecs(timeout - jiffies)/1000;
83 }
84 
85 #else
86 
87 /* Hash specific timeout constants and macros for the entries */
88 
89 /* Hash entry is set with no timeout value */
90 #define IPSET_ELEM_PERMANENT	0
91 
92 static inline bool
ip_set_timeout_test(unsigned long timeout)93 ip_set_timeout_test(unsigned long timeout)
94 {
95 	return timeout == IPSET_ELEM_PERMANENT ||
96 	       time_is_after_jiffies(timeout);
97 }
98 
99 static inline bool
ip_set_timeout_expired(unsigned long timeout)100 ip_set_timeout_expired(unsigned long timeout)
101 {
102 	return timeout != IPSET_ELEM_PERMANENT &&
103 	       time_is_before_jiffies(timeout);
104 }
105 
106 static inline unsigned long
ip_set_timeout_set(u32 timeout)107 ip_set_timeout_set(u32 timeout)
108 {
109 	unsigned long t;
110 
111 	if (!timeout)
112 		return IPSET_ELEM_PERMANENT;
113 
114 	t = msecs_to_jiffies(timeout * 1000) + jiffies;
115 	if (t == IPSET_ELEM_PERMANENT)
116 		/* Bingo! :-) */
117 		t++;
118 
119 	return t;
120 }
121 
122 static inline u32
ip_set_timeout_get(unsigned long timeout)123 ip_set_timeout_get(unsigned long timeout)
124 {
125 	return timeout == IPSET_ELEM_PERMANENT ? 0 :
126 		jiffies_to_msecs(timeout - jiffies)/1000;
127 }
128 #endif /* ! IP_SET_BITMAP_TIMEOUT */
129 
130 #endif	/* __KERNEL__ */
131 
132 #endif /* _IP_SET_TIMEOUT_H */
133