Linux-libre 5.3.12-gnu
[librecmc/linux-libre.git] / include / linux / netfilter / ipset / ip_set_counter.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 #ifndef _IP_SET_COUNTER_H
3 #define _IP_SET_COUNTER_H
4
5 /* Copyright (C) 2015 Jozsef Kadlecsik <kadlec@netfilter.org> */
6
7 #ifdef __KERNEL__
8
9 static inline void
10 ip_set_add_bytes(u64 bytes, struct ip_set_counter *counter)
11 {
12         atomic64_add((long long)bytes, &(counter)->bytes);
13 }
14
15 static inline void
16 ip_set_add_packets(u64 packets, struct ip_set_counter *counter)
17 {
18         atomic64_add((long long)packets, &(counter)->packets);
19 }
20
21 static inline u64
22 ip_set_get_bytes(const struct ip_set_counter *counter)
23 {
24         return (u64)atomic64_read(&(counter)->bytes);
25 }
26
27 static inline u64
28 ip_set_get_packets(const struct ip_set_counter *counter)
29 {
30         return (u64)atomic64_read(&(counter)->packets);
31 }
32
33 static inline bool
34 ip_set_match_counter(u64 counter, u64 match, u8 op)
35 {
36         switch (op) {
37         case IPSET_COUNTER_NONE:
38                 return true;
39         case IPSET_COUNTER_EQ:
40                 return counter == match;
41         case IPSET_COUNTER_NE:
42                 return counter != match;
43         case IPSET_COUNTER_LT:
44                 return counter < match;
45         case IPSET_COUNTER_GT:
46                 return counter > match;
47         }
48         return false;
49 }
50
51 static inline void
52 ip_set_update_counter(struct ip_set_counter *counter,
53                       const struct ip_set_ext *ext, u32 flags)
54 {
55         if (ext->packets != ULLONG_MAX &&
56             !(flags & IPSET_FLAG_SKIP_COUNTER_UPDATE)) {
57                 ip_set_add_bytes(ext->bytes, counter);
58                 ip_set_add_packets(ext->packets, counter);
59         }
60 }
61
62 static inline bool
63 ip_set_put_counter(struct sk_buff *skb, const struct ip_set_counter *counter)
64 {
65         return nla_put_net64(skb, IPSET_ATTR_BYTES,
66                              cpu_to_be64(ip_set_get_bytes(counter)),
67                              IPSET_ATTR_PAD) ||
68                nla_put_net64(skb, IPSET_ATTR_PACKETS,
69                              cpu_to_be64(ip_set_get_packets(counter)),
70                              IPSET_ATTR_PAD);
71 }
72
73 static inline void
74 ip_set_init_counter(struct ip_set_counter *counter,
75                     const struct ip_set_ext *ext)
76 {
77         if (ext->bytes != ULLONG_MAX)
78                 atomic64_set(&(counter)->bytes, (long long)(ext->bytes));
79         if (ext->packets != ULLONG_MAX)
80                 atomic64_set(&(counter)->packets, (long long)(ext->packets));
81 }
82
83 #endif /* __KERNEL__ */
84 #endif /* _IP_SET_COUNTER_H */