Named counter objects support in nft
Subsystems (2)
v4 cover Named counter objects support in nft
Hi,
With this patchset, we add named counter objects support. It is similar to nfacct in iptables.
Examples of use in nft of this new feature:
i
* Add a new counter:
nft add counter ip filter http-traffic
nft add counter ip filter https-traffic
* Delete the counter (No rule uses this counter)
nft delete counter ip filter https-traffic
* Set the counter to a rule.
nft add rule ip filter output tcp dport 80 counter name http-traffic nft add rule ip filter output tcp dport 443 counter name https-traffic
* Reset the number of bytes and packets of the counter. nft reset counter ip filter http-traffic
* List all counter:
nft list counters
* List information about the counter.
nft list counter ip test https-traffic
Example of the table with some counter:
# nft list table ip test
table ip filter {
counter http-traffic { pkts 779 bytes 99495}
counter https-traffic { pkts 189 bytes 37824}
chain output {
type filter hook output priority 0;
tcp dport http counter name http-traffic
tcp dport https counter name https-traffic
}
}It is difficult to reuse the existing code of nfacct because:
* nfacct does not have transation support transactions.
* We need something that integrated well to nf_tables.
No quota support yet.
[Changes in v4]
* Add supppot for "nft list counters" command
* Add support for reset counter command in nft:
Example: "nft reset counter ip test counter1"
* Add support for reset counters command in nft.
Example: "nft reset counters"
* Add named counters with default values.
Example: nft add counter ip test c1 { packets 2 bytes 10}
These changes were sugguested by Pablo Neira
[Changes in v3]
* Delete the patch " Rename from nft_counter to nft_counter_priv". Now, we use "nft_counter" and nft_named_ctr names.
* Add support for NFT_NAMED_CTR_INACTIVE
These changes were sugguested by Pablo Neira
* Fix a kernel panic
[Changes in v2]
* This deletes the acct module and uses the counter module.
* This renames from nft_counter to nft_counter_priv struct
* This uses _COUNTER_ names instead of _ACCT_ names in variables and functions
* Rename acct netlink attributes to named counter netlink attributes. The new names are NFTA_NAMED_CTR_XXX
* This limits NFT_CTR_MAXNAMELEN to 16
* This fixes some memory problems
These changes were sugguested by Pablo Neira and Patrick McHardy.
Ana Rey Botello (1):
netfilter: add named counters support
include/net/netfilter/nf_tables.h | 47 +++ include/uapi/linux/netfilter/nf_tables.h | 31 ++ net/netfilter/nf_tables_api.c | 482 +++++++++++++++++++++++++++++- net/netfilter/nft_counter.c | 97 +++++- 4 files changed, 651 insertions(+), 6 deletions(-)
[libnftnf]
Ana Rey (1):
src: Add named counters support
Ana Rey Botello (1):
tests: add unit tests for counters
examples/Makefile.am | 27 +- examples/nft-counter-add.c | 140 ++++++++ examples/nft-counter-del.c | 135 +++++++ examples/nft-counter-get.c | 137 +++++++ examples/nft-counter-reset.c | 123 +++++++ examples/nft-counters-get.c | 136 +++++++ examples/nft-rule-counter-add.c | 222 ++++++++++++ include/buffer.h | 1 + include/libnftnl/Makefile.am | 3 +- include/libnftnl/counter.h | 97 +++++ include/libnftnl/expr.h | 1 + include/linux/netfilter/nf_tables.h | 31 ++ src/Makefile.am | 1 + src/counter.c | 673 +++++++++++++++++++++++++++++++++++ src/expr/counter.c | 48 ++- src/internal.h | 6 + src/libnftnl.map | 30 ++ tests/Makefile.am | 4 + tests/nft-counter-test.c | 86 +++++ tests/nft-expr_counter-test.c | 4 + tests/test-script.sh | 1 + 21 files changed, 1902 insertions(+), 4 deletions(-) create mode 100644 examples/nft-counter-add.c create mode 100644 examples/nft-counter-del.c create mode 100644 examples/nft-counter-get.c create mode 100644 examples/nft-counter-reset.c create mode 100644 examples/nft-counters-get.c create mode 100644 examples/nft-rule-counter-add.c create mode 100644 include/libnftnl/counter.h create mode 100644 src/counter.c create mode 100644 tests/nft-counter-test.c
[nft]
Ana Rey (2):
src: Add named counters support
tests: regression: Add counters support
include/linux/netfilter/nf_tables.h | 30 ++++ include/mnl.h | 11 ++ include/netlink.h | 27 +++ include/rule.h | 47 ++++++ include/statement.h | 1 + src/evaluate.c | 14 +- src/mnl.c | 135 +++++++++++++++ src/netlink.c | 318 +++++++++++++++++++++++++++++++++++ src/netlink_delinearize.c | 3 + src/netlink_linearize.c | 4 + src/parser_bison.y | 101 ++++++++++- src/rule.c | 154 +++++++++++++++++ src/scanner.l | 2 + src/statement.c | 8 +- tests/regression/ip/counter.t | 15 ++ tests/regression/nft-test.py | 110 ++++++++++++ 16 files changed, 972 insertions(+), 8 deletions(-) create mode 100644 tests/regression/ip/counter.t
Signature · 1 line
1.7.10.4
PATCH v4 1/1 netfilter: add named counters support
Add support for the named counter object. It is similar to nfacct in iptables.
Some examples of use in nft of this new feature:
* Add a new counter:
nft add counter ip filter http-traffic
nft add counter ip filter https-traffic
* Add a new counter with a specific values:
nft add counter ip filter https-traffic { packets 100 bytes 2000}
* Delete the counter (No rule uses this counter)
nft delete counter ip filter https-traffic
* Set the counter to a rule.
nft add rule ip filter output tcp dport 80 counter name http-traffic nft add rule ip filter output tcp dport 443 counter name https-traffic
* Reset the number of bytes and packets of the counter. nft reset counter ip filter http-traffic
* Reset the number of bytes and packets of all counter. nft reset counters
* List all counter:
nft list counters
* List information about the counter.
nft list counter ip test https-traffic
Example of the table with some counter:
# nft list table ip test
table ip filter {
counter http-traffic { pkts 779 bytes 99495}
counter https-traffic { pkts 189 bytes 37824}
chain output {
type filter hook output priority 0;
tcp dport http counter name http-traffic
tcp dport https counter name https-traffic
}
}--- include/net/netfilter/nf_tables.h | 47 +++ include/uapi/linux/netfilter/nf_tables.h | 31 ++ net/netfilter/nf_tables_api.c | 482 +++++++++++++++++++++++++++++- net/netfilter/nft_counter.c | 97 +++++- 4 files changed, 651 insertions(+), 6 deletions(-)
include/net/netfilter/nf_tables.h +47 -0
@@ -408,6 +408,16 @@ struct nft_trans {char data[0 ]; }; +struct nft_trans_counter { +struct nft_named_ctr *counter; +u32 counter_id; +}; + +#define nft_trans_counter (trans) \ + (((struct nft_trans_counter *)trans->data)->counter) +#define nft_trans_counter_id (trans) \ + (((struct nft_trans_counter *)trans->data)->counter_id) +struct nft_trans_rule {struct nft_rule *rule; };
@@ -572,6 +582,7 @@ unsigned int nft_do_chain(struct nft_pktinfo *pkt,* @list: used internally * @chains: chains in the table * @sets: sets in the table +* @counters: counters in the table * @hgenerator: handle generator state * @use: number of chain references to this table * @flags: table flag (see enum nft_table_flags)
@@ -581,6 +592,7 @@ struct nft_table {struct list_head list;struct list_head chains;struct list_head sets; +struct list_head counters;u64 hgenerator;u32 use;u16 flags;
@@ -639,6 +651,41 @@ void nft_unregister_chain_type(const struct nf_chain_type *);int nft_register_expr (struct nft_expr_type *);void nft_unregister_expr (struct nft_expr_type *); +/** +* struct nft_counter - nf_tables counter_priv instance +* +* @pkts: number of packets +* @bytes: number of bytes +*/ +struct nft_counter { +seqlock_t lock; +u64 bytes; +u64 packets; +}; + +/** +* struct nft_named_ctr - nf_tables counter instance +* +* @list: table counter list node +* @name: name of the counter +* @counter: nft_priv_counter +* @bytes: number of bytes +* @use: number of rule references to this counter +* @flags: counter flags +*/ +struct nft_named_ctr { +struct list_head list; +char name[NFT_CTR_MAXNAMELEN ]; +struct nft_counter counter; +u32 use:31 , + flags:1 ; +}; + +void nft_counter_put (struct nft_named_ctr *counter); +int nft_counter_get (struct nft_named_ctr *counter); +struct nft_named_ctr *nf_tables_counter_lookup (const struct nft_table *table, +const struct nlattr *nla); +#define nft_dereference (p) \ nfnl_dereference(p, NFNL_SUBSYS_NFTABLES)
include/uapi/linux/netfilter/nf_tables.h +31 -0
@@ -2,6 +2,7 @@#define _LINUX_NF_TABLES_H#define NFT_CHAIN_MAXNAMELEN 32 +#define NFT_CTR_MAXNAMELEN 16#define NFT_USERDATA_MAXLEN 256enum nft_registers {
@@ -53,6 +54,10 @@ enum nft_verdicts {* @NFT_MSG_DELSETELEM: delete a set element (enum nft_set_elem_attributes) * @NFT_MSG_NEWGEN: announce a new generation, only for events (enum nft_gen_attributes) * @NFT_MSG_GETGEN: get the rule-set generation (enum nft_gen_attributes) +* @NFT_MSG_NEWCOUNTER: create a new counter (enum nft_named_ctr_attributes) +* @NFT_MSG_GETCOUNTER: get a counter (enum nft_named_ctr_attributes) +* @NFT_MSG_GETCOUNTER_ZERO: get a reset counter (enum nft_named_ctr_attributes) +* @NFT_MSG_DELCOUNTER: delete a counter (enum nft_named_ctr_attributes) */ enum nf_tables_msg_types {NFT_MSG_NEWTABLE ,
@@ -72,6 +77,10 @@ enum nf_tables_msg_types {NFT_MSG_DELSETELEM ,NFT_MSG_NEWGEN ,NFT_MSG_GETGEN , +NFT_MSG_NEWCOUNTER , +NFT_MSG_GETCOUNTER , +NFT_MSG_GETCOUNTER_ZERO , +NFT_MSG_DELCOUNTER , NFT_MSG_MAX, };
@@ -695,16 +704,38 @@ enum nft_limit_attributes {* * @NFTA_COUNTER_BYTES: number of bytes (NLA_U64) * @NFTA_COUNTER_PACKETS: number of packets (NLA_U64) +* @NFTA_COUNTER_NAME: name of the counter (NLA_STRING) */ enum nft_counter_attributes {NFTA_COUNTER_UNSPEC ,NFTA_COUNTER_BYTES ,NFTA_COUNTER_PACKETS , +NFTA_COUNTER_NAME , __NFTA_COUNTER_MAX };#define NFTA_COUNTER_MAX (__NFTA_COUNTER_MAX - 1)/** +* enum nft_named_ctr_attributes - nf_tables named counter netlink attributes +* +* @NFTA_NAMED_CTR_NAME: named counter name (NLA_STRING) +* @NFTA_NAMED_CTR_TABLE: table name (NLA_STRING) +* @NFTA_NAMED_CTR_USE: number of references to this named counter (NLA_U32) +* @NFTA_NAMED_CTR_BYTES: number of bytes (NLA_U64) +* @NFTA_NAMED_CTR_PACKETS: number of packets (NLA_U64) +*/ +enum nft_named_ctr_attributes { +NFTA_NAMED_CTR_UNSPEC , +NFTA_NAMED_CTR_NAME , +NFTA_NAMED_CTR_TABLE , +NFTA_NAMED_CTR_USE , +NFTA_NAMED_CTR_BYTES , +NFTA_NAMED_CTR_PACKETS , + __NFTA_NAMED_CTR_MAX +}; +#define NFTA_NAMED_CTR_MAX (__NFTA_NAMED_CTR_MAX - 1) + +/** * enum nft_log_attributes - nf_tables log expression netlink attributes * * @NFTA_LOG_GROUP: netlink group to send messages to (NLA_U32)
net/netfilter/nf_tables_api.c +481 -1
542 lines not on this page · raw message
net/netfilter/nft_counter.c +92 -5
134 lines not on this page · raw message
Signature · 1 line
1.7.10.4
Quoted 4 lines · Ana Rey Botello
Hi,
With this patchset, we add named counter objects support. It is similar to nfacct in iptables.
Thanks for doing this work. I have some patches coming up very soon which have kind of an intersection with your work, so I'd ask for a little bit of patience until we can integrate them.
Basically what my patches will do is to allow to statically or dynamically "stateify" and expression type, including counters. Your patches are one of the missing pieces in doing that, however we most likely want to change them in a way that they not only allow access to counter objects, but any kind of expression. Its a rather minor change to your work, but requires some things I'm still working on. Hope to have something ready by the end of next week.
I'll keep you posted.
Quoted 9 lines · Patrick McHardy
Quoted 4 lines · Ana Rey Botello
Hi,
With this patchset, we add named counter objects support. It is similar to nfacct in iptables.
Thanks for doing this work. I have some patches coming up very soon which have kind of an intersection with your work, so I'd ask for a little bit of patience until we can integrate them.
ok, I'll be patient.
Quoted 12 lines
Basically what my patches will do is to allow to statically or dynamically "stateify" and expression type, including counters. Your patches are one of the missing pieces in doing that, however we most likely want to change them in a way that they not only allow access to counter objects, but any kind of expression. Its a rather minor change to your work, but requires some things I'm still working on. Hope to have something ready by the end of next week.
I'll keep you posted.