]> git.karo-electronics.de Git - karo-tx-linux.git/blob - include/linux/netfilter/ipset/ip_set_timeout.h
Merge tag 'fbdev-fixes-for-3.5-2' of git://github.com/schandinat/linux-2.6
[karo-tx-linux.git] / include / linux / netfilter / ipset / ip_set_timeout.h
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
29 ip_set_timeout_uget(struct nlattr *tb)
30 {
31         unsigned int timeout = ip_set_get_h32(tb);
32
33         /* Normalize to fit into jiffies */
34         if (timeout > UINT_MAX/MSEC_PER_SEC)
35                 timeout = UINT_MAX/MSEC_PER_SEC;
36
37         /* Userspace supplied TIMEOUT parameter: adjust crazy size */
38         return timeout == IPSET_NO_TIMEOUT ? IPSET_NO_TIMEOUT - 1 : timeout;
39 }
40
41 #ifdef IP_SET_BITMAP_TIMEOUT
42
43 /* Bitmap specific timeout constants and macros for the entries */
44
45 /* Bitmap entry is unset */
46 #define IPSET_ELEM_UNSET        0
47 /* Bitmap entry is set with no timeout value */
48 #define IPSET_ELEM_PERMANENT    (UINT_MAX/2)
49
50 static inline bool
51 ip_set_timeout_test(unsigned long timeout)
52 {
53         return timeout != IPSET_ELEM_UNSET &&
54                (timeout == IPSET_ELEM_PERMANENT ||
55                 time_is_after_jiffies(timeout));
56 }
57
58 static inline bool
59 ip_set_timeout_expired(unsigned long timeout)
60 {
61         return timeout != IPSET_ELEM_UNSET &&
62                timeout != IPSET_ELEM_PERMANENT &&
63                time_is_before_jiffies(timeout);
64 }
65
66 static inline unsigned long
67 ip_set_timeout_set(u32 timeout)
68 {
69         unsigned long t;
70
71         if (!timeout)
72                 return IPSET_ELEM_PERMANENT;
73
74         t = msecs_to_jiffies(timeout * 1000) + jiffies;
75         if (t == IPSET_ELEM_UNSET || t == IPSET_ELEM_PERMANENT)
76                 /* Bingo! */
77                 t++;
78
79         return t;
80 }
81
82 static inline u32
83 ip_set_timeout_get(unsigned long timeout)
84 {
85         return timeout == IPSET_ELEM_PERMANENT ? 0 :
86                 jiffies_to_msecs(timeout - jiffies)/1000;
87 }
88
89 #else
90
91 /* Hash specific timeout constants and macros for the entries */
92
93 /* Hash entry is set with no timeout value */
94 #define IPSET_ELEM_PERMANENT    0
95
96 static inline bool
97 ip_set_timeout_test(unsigned long timeout)
98 {
99         return timeout == IPSET_ELEM_PERMANENT ||
100                time_is_after_jiffies(timeout);
101 }
102
103 static inline bool
104 ip_set_timeout_expired(unsigned long timeout)
105 {
106         return timeout != IPSET_ELEM_PERMANENT &&
107                time_is_before_jiffies(timeout);
108 }
109
110 static inline unsigned long
111 ip_set_timeout_set(u32 timeout)
112 {
113         unsigned long t;
114
115         if (!timeout)
116                 return IPSET_ELEM_PERMANENT;
117
118         t = msecs_to_jiffies(timeout * 1000) + jiffies;
119         if (t == IPSET_ELEM_PERMANENT)
120                 /* Bingo! :-) */
121                 t++;
122
123         return t;
124 }
125
126 static inline u32
127 ip_set_timeout_get(unsigned long timeout)
128 {
129         return timeout == IPSET_ELEM_PERMANENT ? 0 :
130                 jiffies_to_msecs(timeout - jiffies)/1000;
131 }
132 #endif /* ! IP_SET_BITMAP_TIMEOUT */
133
134 #endif  /* __KERNEL__ */
135
136 #endif /* _IP_SET_TIMEOUT_H */