New mnlio functions
cover New mnlio functions
These functions are likely to be reused by almost all programs that want to talk with the kernel subsystem.
We can add here to libnftnl or maybe put in a higher level library.
Let this patchset be a RFC about this subject.
An example of what means using the new mnlio approach is the examples/nft-rule-add.c file. We save lots of LOCs.
Comments welcome.
NOTE: this patchset would require several iterations. For example, I left behind batch_pages.
---
Arturo Borrero Gonzalez (2):
src: add mnlio API functions
examples: use new mnlio API in nft-rule-add.c
examples/nft-rule-add.c | 87 ---- include/libnftnl/Makefile.am | 3 include/libnftnl/mnlio.h | 92 ++++ src/Makefile.am | 1 src/libnftnl.map | 37 ++ src/mnlio.c | 957 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 1096 insertions(+), 81 deletions(-) create mode 100644 include/libnftnl/mnlio.h create mode 100644 src/mnlio.c
Signature · 1 line
Arturo Borrero Gonzalez
RFC PATCH 1/2 src: add mnlio API functions
These functions are likely to be used by all userspace programs to interact with the nftables kernel subsystem.
Lets put in the library, so: its easy to maintain, we can save lots of LOCs, programmers can easily learn how to work with nftables, etc..
This patch will require several iterations: batch_pages are left behind.
--- include/libnftnl/Makefile.am | 3 include/libnftnl/mnlio.h | 92 ++++ src/Makefile.am | 1 src/libnftnl.map | 37 ++ src/mnlio.c | 957 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 1089 insertions(+), 1 deletion(-) create mode 100644 include/libnftnl/mnlio.h create mode 100644 src/mnlio.c
include/libnftnl/Makefile.am +2 -1
@@ -4,4 +4,5 @@ pkginclude_HEADERS = table.h \ expr.h \ set.h \ ruleset.h \ - common.h + common.h \ + mnlio.h
include/libnftnl/mnlio.h new file +92 -0
92 lines not on this page · raw message
src/Makefile.am +1 -0
@@ -35,6 +35,7 @@ libnftnl_la_SOURCES = utils.c \ expr/reject.c \ expr/target.c \ expr/data_reg.h \ + mnlio.c \ libnftnl.map \ expr_ops.h \ internal.h
src/libnftnl.map +37 -0
@@ -194,5 +194,42 @@ global: nft_parse_err_free; nft_parse_perror; + nft_mnlio_talk; + nft_mnlio_batch_put; + nft_mnlio_batch_talk; + nft_mnlio_batch_begin; + nft_mnlio_batch_end; + nft_mnlio_batch_alloc; + nft_mnlio_batch_ready; + nft_mnlio_batch_reset; + nft_mnlio_rule_add; + nft_mnlio_rule_list_add; + nft_mnlio_rule_del; + nft_mnlio_rule_dump; + nft_mnlio_chain_add; + nft_mnlio_chain_list_add; + nft_mnlio_chain_delete; + nft_mnlio_chain_list_delete; + nft_mnlio_chain_dump; + nft_mnlio_chain_get; + nft_mnlio_table_add; + nft_mnlio_table_list_add; + nft_mnlio_table_delete; + nft_mnlio_table_list_delete; + nft_mnlio_table_dump; + nft_mnlio_table_get; + nft_mnlio_set_add; + nft_mnlio_set_list_add; + nft_mnlio_set_delete; + nft_mnlio_set_list_delete; + nft_mnlio_set_dump; + nft_mnlio_set_get; + nft_mnlio_setelem_add; + nft_mnlio_setelem_list_add; + nft_mnlio_setelem_delete; + nft_mnlio_setelem_get; + nft_mnlio_ruleset_dump; + nft_mnlio_ruleset_add; + local: *; };
src/mnlio.c new file +957 -0
957 lines not on this page · raw message
Pablo Neira Ayuso
maintainerRe: [RFC libnftnl PATCH 1/2] src: add mnlio API functions
Hi Arturo,
Quoted 5 lines · Arturo Borrero Gonzalez
These functions are likely to be used by all userspace programs to interact with the nftables kernel subsystem.
Lets put in the library, so: its easy to maintain, we can save lots of LOCs, programmers can easily learn how to work with nftables, etc..
See comments below.
[...]
Quoted 9 lines
src/mnlio.c new file +9
-0+void nft_mnlio_batch_put (struct mnl_nlmsg_batch *batch,int type, +uint32_t seqnum) +{ +struct nlmsghdr *nlh; +struct nfgenmsg *nfg; + + nlh =mnl_nlmsg_put_header (mnl_nlmsg_batch_current (batch)); + nlh->nlmsg_type = type; + nlh->nlmsg_flags =NLM_F_REQUEST ;
src/mnlio.c line 119
Quoted 10 lines
src/mnlio.c new file +10
-0+ nlh->nlmsg_seq = seqnum; + + nfg =mnl_nlmsg_put_extra_header (nlh,sizeof (*nfg)); + nfg->nfgen_family =AF_INET ; + nfg->version =NFNETLINK_V0 ; + nfg->res_id =NFNL_SUBSYS_NFTABLES ; + +mnl_nlmsg_batch_next (batch); +} +EXPORT_SYMBOL (nft_mnlio_batch_put);
You can rename and export this as: nft_batch_nlmsg_build_hdr, it should take a 'void *buf' instead of struct mnl_nlmsg_batch *batch, so we don't force people to use libmnl batching infrastructure. The type should be uint16_t. Add this function to src/batch.c.
src/mnlio.c line 183
Quoted 4 lines
src/mnlio.c new file +4
-0+/* +* Rule +*/ +int nft_mnlio_rule_add(struct nft_rule *nlr, unsigned int flags,
use fixed types, ie. uint32_t instead of unsigned int.
Review
Quoted 1 line
(quoted diff) +1
-0+ struct mnl_nlmsg_batch *batch)
use void *buf instead of the struct mnl_nlmsg_batch *batch, so we can pass a simple char buf[...] or mnl_nlmsg_batch_current(batch).
src/mnlio.c line 191
Quoted 7 lines
src/mnlio.c new file +7
-0+{ +struct nlmsghdr *nlh; + + nlh =nft_rule_nlmsg_build_hdr (mnl_nlmsg_batch_current (batch), +NFT_MSG_NEWRULE , +nft_rule_attr_get_u32 (nlr,NFT_RULE_ATTR_FAMILY ), +NLM_F_CREATE | flags,nft_mnlio_seqnum_alloc ());
^----------^
no assumptions on any flag, just pass flags.
src/mnlio.c line 197
Quoted 6 lines
src/mnlio.c new file +6
-0+ +nft_rule_nlmsg_build_payload (nlh, nlr); + +return 0 ; +} +EXPORT_SYMBOL (nft_mnlio_rule_add);
Rename this function to nft_rule_build_msg, add it to src/rule.c
[...]
src/mnlio.c line 225
Quoted 2 lines
src/mnlio.c new file +2
-0+int nft_mnlio_rule_del (struct nft_rule *nlr,unsigned int flags, +struct mnl_nlmsg_batch *batch)
similar comments like in nft_mnlio_rule_add.
Quoted 14 lines
src/mnlio.c new file +13
-0+{ +struct nlmsghdr *nlh; + + nlh =nft_rule_nlmsg_build_hdr (mnl_nlmsg_batch_current (batch), +NFT_MSG_DELRULE , +nft_rule_attr_get_u32 (nlr,NFT_RULE_ATTR_FAMILY ), +0 ,nft_mnlio_seqnum_alloc ()); + +nft_rule_nlmsg_build_payload (nlh, nlr); + +return 0 ; +} +EXPORT_SYMBOL (nft_mnlio_rule_del);
[...]
src/mnlio.c line 299
Quoted 13 lines
src/mnlio.c new file +11
-0+int nft_mnlio_chain_add (struct mnl_socket *nf_sock,struct nft_chain *nlc, +unsigned int flags) + +{ +char buf[MNL_SOCKET_BUFFER_SIZE ]; +struct nlmsghdr *nlh; + + nlh =nft_chain_nlmsg_build_hdr (buf,NFT_MSG_NEWCHAIN , +nft_chain_attr_get_u32 (nlc,NFT_CHAIN_ATTR_FAMILY ), +NLM_F_CREATE |NLM_F_ACK | flags, seq); +nft_chain_nlmsg_build_payload (nlh, nlc);
Better add nft_chain_build_msg, similar to nft_rule_build_msg.
Same with tables, rules and so on.
Review
Quoted 2 lines
(quoted diff) +2
-0+ + return nft_mnlio_talk(nf_sock, nlh, nlh->nlmsg_len, NULL, NULL);
So remove this _talk call. Thus, the netlink interaction is decoupled, which is more flexible when doing complex stuff.
Let's start with those functions first, you can send incremental patches for other functions that you're including in this patch that you believe that can be useful to others so we can discuss them.
I detected problems in the rulelist to batch handling in your patch, you may run out of the batch boundary that may lead to a memory corruption.
RFC PATCH 2/2 examples: use new mnlio API in nft-rule-add.c
In reply to Arturo Borrero GonzalezUpdate this example to use the new mnlio API.
--- examples/nft-rule-add.c | 87 ++++------------------------------------------- 1 file changed, 7 insertions(+), 80 deletions(-)
examples/nft-rule-add.c +7 -80
@@ -28,6 +28,7 @@#include <libmnl/libmnl.h> #include <libnftnl/rule.h> #include <libnftnl/expr.h> +#include <libnftnl/mnlio.h> static void add_payload (struct nft_rule *r,uint32_t base,uint32_t dreg,uint32_t offset,uint32_t len)
@@ -111,78 +112,12 @@ static struct nft_rule *setup_rule(uint8_t family, const char *table,return r; } -static int seq; - -static void nft_mnl_batch_put (struct mnl_nlmsg_batch *batch,int type) -{ -struct nlmsghdr *nlh; -struct nfgenmsg *nfg; - - nlh =mnl_nlmsg_put_header (mnl_nlmsg_batch_current (batch)); - nlh->nlmsg_type = type; - nlh->nlmsg_flags =NLM_F_REQUEST ; - nlh->nlmsg_seq = seq++; - - nfg =mnl_nlmsg_put_extra_header (nlh,sizeof (*nfg)); - nfg->nfgen_family =AF_INET ; - nfg->version =NFNETLINK_V0 ; - nfg->res_id =NFNL_SUBSYS_NFTABLES ; - -mnl_nlmsg_batch_next (batch); -} - -static int nft_mnl_batch_talk (struct mnl_socket *nl,struct mnl_nlmsg_batch *b) -{ -int ret, fd =mnl_socket_get_fd (nl); -char rcv_buf[MNL_SOCKET_BUFFER_SIZE ]; -fd_set readfds; -struct timeval tv = { - .tv_sec =0 , - .tv_usec =0 - }; - - ret =mnl_socket_sendto (nl,mnl_nlmsg_batch_head (b), -mnl_nlmsg_batch_size (b)); -if (ret ==-1 ) - goto err; - -FD_ZERO (&readfds); -FD_SET (fd, &readfds); - -/* receive and digest all the acknowledgments from the kernel. */ - ret =select (fd+1 , &readfds,NULL ,NULL , &tv); -if (ret ==-1 ) - goto err; - -while (ret >0 &&FD_ISSET (fd, &readfds)) { - ret =mnl_socket_recvfrom (nl, rcv_buf,sizeof (rcv_buf)); -if (ret ==-1 ) - goto err; - - ret =mnl_cb_run (rcv_buf, ret,0 ,mnl_socket_get_portid (nl), -NULL ,NULL ); -if (ret <0 ) - goto err; - - ret =select (fd+1 , &readfds,NULL ,NULL , &tv); -if (ret ==-1 ) - goto err; - -FD_ZERO (&readfds); -FD_SET (fd, &readfds); - } -err: -return ret; -} -int main (int argc,char *argv[]) {struct mnl_socket *nl;struct nft_rule *r; -struct nlmsghdr *nlh; -struct mnl_nlmsg_batch *batch; +struct mnl_nlmsg_batch *batch =nft_mnlio_batch_alloc ();uint8_t family; -char buf[4096 ];if (argc !=4 ) {fprintf (stderr,"Usage: %s <family> <table> <chain>\n" , argv[0 ]);
@@ -211,28 +146,20 @@ int main(int argc, char *argv[])exit (EXIT_FAILURE ); } - batch =mnl_nlmsg_batch_start (buf,sizeof (buf)); - -nft_mnl_batch_put (batch,NFNL_MSG_BATCH_BEGIN ); +nft_mnlio_batch_begin (batch); - nlh =nft_rule_nlmsg_build_hdr (mnl_nlmsg_batch_current (batch), -NFT_MSG_NEWRULE , -nft_rule_attr_get_u32 (r,NFT_RULE_ATTR_FAMILY ), -NLM_F_APPEND |NLM_F_CREATE , seq); - -nft_rule_nlmsg_build_payload (nlh, r); +nft_mnlio_rule_add (r,NLM_F_APPEND , batch);nft_rule_free (r); -mnl_nlmsg_batch_next (batch); -nft_mnl_batch_put (batch,NFNL_MSG_BATCH_END ); +mnl_nlmsg_batch_next (batch); +nft_mnlio_batch_end (batch); -if (nft_mnl_batch_talk (nl, batch) <0 ) { +if (nft_mnlio_batch_talk (nl, batch) <0 ) {perror ("Netlink problem" );exit (EXIT_FAILURE ); }mnl_nlmsg_batch_stop (batch); -mnl_socket_close (nl);return EXIT_SUCCESS ;
Quoted 11 lines · Arturo Borrero Gonzalez
These functions are likely to be reused by almost all programs that want to talk with the kernel subsystem.
We can add here to libnftnl or maybe put in a higher level library.
Let this patchset be a RFC about this subject.
An example of what means using the new mnlio approach is the
examples/nft-rule-add.cfile. We save lots of LOCs.Comments welcome.
I agree, they belong in libnftnl. Thanks for doing this work.
Quoted 19 lines
NOTE: this patchset would require several iterations. For example, I left behind
batch_pages.
---Arturo Borrero Gonzalez (2):
src: add mnlio API functions
examples: use new mnlio API innft-rule-add.cexamples/nft-rule-add.c | 87 ---- include/libnftnl/Makefile.am | 3 include/libnftnl/mnlio.h | 92 ++++ src/Makefile.am | 1 src/libnftnl.map | 37 ++ src/mnlio.c | 957 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 1096 insertions(+), 81 deletions(-) create mode 100644 include/libnftnl/mnlio.h create mode 100644 src/mnlio.c