GNU Linux-libre 4.14.290-gnu1
[releases.git] / include / linux / netfilter / ipset / ip_set_counter.h
1 #ifndef _IP_SET_COUNTER_H
2 #define _IP_SET_COUNTER_H
3
4 /* Copyright (C) 2015 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 static inline void
14 ip_set_add_bytes(u64 bytes, struct ip_set_counter *counter)
15 {
16         atomic64_add((long long)bytes, &(counter)->bytes);
17 }
18
19 static inline void
20 ip_set_add_packets(u64 packets, struct ip_set_counter *counter)
21 {
22         atomic64_add((long long)packets, &(counter)->packets);
23 }
24
25 static inline u64
26 ip_set_get_bytes(const struct ip_set_counter *counter)
27 {
28         return (u64)atomic64_read(&(counter)->bytes);
29 }
30
31 static inline u64
32 ip_set_get_packets(const struct ip_set_counter *counter)
33 {
34         return (u64)atomic64_read(&(counter)->packets);
35 }
36
37 static inline void
38 ip_set_update_counter(struct ip_set_counter *counter,
39                       const struct ip_set_ext *ext,
40                       struct ip_set_ext *mext, u32 flags)
41 {
42         if (ext->packets != ULLONG_MAX &&
43             !(flags & IPSET_FLAG_SKIP_COUNTER_UPDATE)) {
44                 ip_set_add_bytes(ext->bytes, counter);
45                 ip_set_add_packets(ext->packets, counter);
46         }
47         if (flags & IPSET_FLAG_MATCH_COUNTERS) {
48                 mext->packets = ip_set_get_packets(counter);
49                 mext->bytes = ip_set_get_bytes(counter);
50         }
51 }
52
53 static inline bool
54 ip_set_put_counter(struct sk_buff *skb, const struct ip_set_counter *counter)
55 {
56         return nla_put_net64(skb, IPSET_ATTR_BYTES,
57                              cpu_to_be64(ip_set_get_bytes(counter)),
58                              IPSET_ATTR_PAD) ||
59                nla_put_net64(skb, IPSET_ATTR_PACKETS,
60                              cpu_to_be64(ip_set_get_packets(counter)),
61                              IPSET_ATTR_PAD);
62 }
63
64 static inline void
65 ip_set_init_counter(struct ip_set_counter *counter,
66                     const struct ip_set_ext *ext)
67 {
68         if (ext->bytes != ULLONG_MAX)
69                 atomic64_set(&(counter)->bytes, (long long)(ext->bytes));
70         if (ext->packets != ULLONG_MAX)
71                 atomic64_set(&(counter)->packets, (long long)(ext->packets));
72 }
73
74 #endif /* __KERNEL__ */
75 #endif /* _IP_SET_COUNTER_H */