1 /* SPDX-License-Identifier: GPL-2.0-only */
2 #ifndef _IP_SET_COUNTER_H
3 #define _IP_SET_COUNTER_H
5 /* Copyright (C) 2015 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
11 ip_set_add_bytes(u64 bytes, struct ip_set_counter *counter)
13 atomic64_add((long long)bytes, &(counter)->bytes);
17 ip_set_add_packets(u64 packets, struct ip_set_counter *counter)
19 atomic64_add((long long)packets, &(counter)->packets);
23 ip_set_get_bytes(const struct ip_set_counter *counter)
25 return (u64)atomic64_read(&(counter)->bytes);
29 ip_set_get_packets(const struct ip_set_counter *counter)
31 return (u64)atomic64_read(&(counter)->packets);
35 ip_set_match_counter(u64 counter, u64 match, u8 op)
38 case IPSET_COUNTER_NONE:
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;
53 ip_set_update_counter(struct ip_set_counter *counter,
54 const struct ip_set_ext *ext, u32 flags)
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);
64 ip_set_put_counter(struct sk_buff *skb, const struct ip_set_counter *counter)
66 return nla_put_net64(skb, IPSET_ATTR_BYTES,
67 cpu_to_be64(ip_set_get_bytes(counter)),
69 nla_put_net64(skb, IPSET_ATTR_PACKETS,
70 cpu_to_be64(ip_set_get_packets(counter)),
75 ip_set_init_counter(struct ip_set_counter *counter,
76 const struct ip_set_ext *ext)
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));
84 #endif /* __KERNEL__ */
85 #endif /* _IP_SET_COUNTER_H */