12 #include <linux/netfilter/nf_tables.h>
14 #include <libmnl/libmnl.h>
15 #include <libnftnl/object.h>
21 nftnl_obj_counter_set(
struct nftnl_obj *e, uint16_t type,
22 const void *data, uint32_t data_len)
24 struct nftnl_obj_counter *ctr = nftnl_obj_data(e);
27 case NFTNL_OBJ_CTR_BYTES:
28 memcpy(&ctr->bytes, data, data_len);
30 case NFTNL_OBJ_CTR_PKTS:
31 memcpy(&ctr->pkts, data, data_len);
38 nftnl_obj_counter_get(
const struct nftnl_obj *e, uint16_t type,
41 struct nftnl_obj_counter *ctr = nftnl_obj_data(e);
44 case NFTNL_OBJ_CTR_BYTES:
45 *data_len =
sizeof(ctr->bytes);
47 case NFTNL_OBJ_CTR_PKTS:
48 *data_len =
sizeof(ctr->pkts);
54 static int nftnl_obj_counter_cb(
const struct nlattr *attr,
void *data)
56 const struct nlattr **tb = data;
57 int type = mnl_attr_get_type(attr);
59 if (mnl_attr_type_valid(attr, NFTA_COUNTER_MAX) < 0)
63 case NFTA_COUNTER_BYTES:
64 case NFTA_COUNTER_PACKETS:
65 if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0)
75 nftnl_obj_counter_build(
struct nlmsghdr *nlh,
const struct nftnl_obj *e)
77 struct nftnl_obj_counter *ctr = nftnl_obj_data(e);
79 if (e->flags & (1 << NFTNL_OBJ_CTR_BYTES))
80 mnl_attr_put_u64(nlh, NFTA_COUNTER_BYTES, htobe64(ctr->bytes));
81 if (e->flags & (1 << NFTNL_OBJ_CTR_PKTS))
82 mnl_attr_put_u64(nlh, NFTA_COUNTER_PACKETS, htobe64(ctr->pkts));
86 nftnl_obj_counter_parse(
struct nftnl_obj *e,
struct nlattr *attr)
88 struct nftnl_obj_counter *ctr = nftnl_obj_data(e);
89 struct nlattr *tb[NFTA_COUNTER_MAX+1] = {};
91 if (mnl_attr_parse_nested(attr, nftnl_obj_counter_cb, tb) < 0)
94 if (tb[NFTA_COUNTER_BYTES]) {
95 ctr->bytes = be64toh(mnl_attr_get_u64(tb[NFTA_COUNTER_BYTES]));
96 e->flags |= (1 << NFTNL_OBJ_CTR_BYTES);
98 if (tb[NFTA_COUNTER_PACKETS]) {
99 ctr->pkts = be64toh(mnl_attr_get_u64(tb[NFTA_COUNTER_PACKETS]));
100 e->flags |= (1 << NFTNL_OBJ_CTR_PKTS);
106 static int nftnl_obj_counter_snprintf(
char *buf,
size_t len, uint32_t flags,
107 const struct nftnl_obj *e)
109 struct nftnl_obj_counter *ctr = nftnl_obj_data(e);
111 return snprintf(buf, len,
"pkts %"PRIu64
" bytes %"PRIu64
" ",
112 ctr->pkts, ctr->bytes);
115 static struct attr_policy obj_ctr_attr_policy[__NFTNL_OBJ_CTR_MAX] = {
116 [NFTNL_OBJ_CTR_BYTES] = { .maxlen =
sizeof(uint64_t) },
117 [NFTNL_OBJ_CTR_PKTS] = { .maxlen =
sizeof(uint64_t) },
120 struct obj_ops obj_ops_counter = {
122 .type = NFT_OBJECT_COUNTER,
123 .alloc_len =
sizeof(
struct nftnl_obj_counter),
124 .nftnl_max_attr = __NFTNL_OBJ_CTR_MAX - 1,
125 .attr_policy = obj_ctr_attr_policy,
126 .set = nftnl_obj_counter_set,
127 .get = nftnl_obj_counter_get,
128 .parse = nftnl_obj_counter_parse,
129 .build = nftnl_obj_counter_build,
130 .output = nftnl_obj_counter_snprintf,