Merge tag 'printk-for-5.3' of git://git.kernel.org/pub/scm/linux/kernel/git/pmladek...
[linux-2.6-microblaze.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@blackhole.kfki.hu>
6  */
7
8 #ifdef __KERNEL__
9
10 static inline void
11 ip_set_add_bytes(u64 bytes, struct ip_set_counter *counter)
12 {
13         atomic64_add((long long)bytes, &(counter)->bytes);
14 }
15
16 static inline void
17 ip_set_add_packets(u64 packets, struct ip_set_counter *counter)
18 {
19         atomic64_add((long long)packets, &(counter)->packets);
20 }
21
22 static inline u64
23 ip_set_get_bytes(const struct ip_set_counter *counter)
24 {
25         return (u64)atomic64_read(&(counter)->bytes);
26 }
27
28 static inline u64
29 ip_set_get_packets(const struct ip_set_counter *counter)
30 {
31         return (u64)atomic64_read(&(counter)->packets);
32 }
33
34 static inline bool
35 ip_set_match_counter(u64 counter, u64 match, u8 op)
36 {
37         switch (op) {
38         case IPSET_COUNTER_NONE:
39                 return true;
40         case IPSET_COUNTER_EQ:
41                 return counter == match;
42         case IPSET_COUNTER_NE:
43                 return counter != match;
44         case IPSET_COUNTER_LT:
45                 return counter < match;
46         case IPSET_COUNTER_GT:
47                 return counter > match;
48         }
49         return false;
50 }
51
52 static inline void
53 ip_set_update_counter(struct ip_set_counter *counter,
54                       const struct ip_set_ext *ext, u32 flags)
55 {
56         if (ext->packets != ULLONG_MAX &&
57             !(flags & IPSET_FLAG_SKIP_COUNTER_UPDATE)) {
58                 ip_set_add_bytes(ext->bytes, counter);
59                 ip_set_add_packets(ext->packets, counter);
60         }
61 }
62
63 static inline bool
64 ip_set_put_counter(struct sk_buff *skb, const struct ip_set_counter *counter)
65 {
66         return nla_put_net64(skb, IPSET_ATTR_BYTES,
67                              cpu_to_be64(ip_set_get_bytes(counter)),
68                              IPSET_ATTR_PAD) ||
69                nla_put_net64(skb, IPSET_ATTR_PACKETS,
70                              cpu_to_be64(ip_set_get_packets(counter)),
71                              IPSET_ATTR_PAD);
72 }
73
74 static inline void
75 ip_set_init_counter(struct ip_set_counter *counter,
76                     const struct ip_set_ext *ext)
77 {
78         if (ext->bytes != ULLONG_MAX)
79                 atomic64_set(&(counter)->bytes, (long long)(ext->bytes));
80         if (ext->packets != ULLONG_MAX)
81                 atomic64_set(&(counter)->packets, (long long)(ext->packets));
82 }
83
84 #endif /* __KERNEL__ */
85 #endif /* _IP_SET_COUNTER_H */