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@netfilter.org> */
10 ip_set_add_bytes(u64 bytes, struct ip_set_counter *counter)
12 atomic64_add((long long)bytes, &(counter)->bytes);
16 ip_set_add_packets(u64 packets, struct ip_set_counter *counter)
18 atomic64_add((long long)packets, &(counter)->packets);
22 ip_set_get_bytes(const struct ip_set_counter *counter)
24 return (u64)atomic64_read(&(counter)->bytes);
28 ip_set_get_packets(const struct ip_set_counter *counter)
30 return (u64)atomic64_read(&(counter)->packets);
34 ip_set_match_counter(u64 counter, u64 match, u8 op)
37 case IPSET_COUNTER_NONE:
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;
52 ip_set_update_counter(struct ip_set_counter *counter,
53 const struct ip_set_ext *ext, u32 flags)
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);
63 ip_set_put_counter(struct sk_buff *skb, const struct ip_set_counter *counter)
65 return nla_put_net64(skb, IPSET_ATTR_BYTES,
66 cpu_to_be64(ip_set_get_bytes(counter)),
68 nla_put_net64(skb, IPSET_ATTR_PACKETS,
69 cpu_to_be64(ip_set_get_packets(counter)),
74 ip_set_init_counter(struct ip_set_counter *counter,
75 const struct ip_set_ext *ext)
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));
83 #endif /* __KERNEL__ */
84 #endif /* _IP_SET_COUNTER_H */