examples: merge nft-table-{xml|json}-add.c
PATCH 1/6 examples: merge nft-table-{xml|json}-add.c
Merge the two examples in one. An input argument choose the format to use.
--- examples/Makefile.am | 10 +-- examples/nft-table-json-add.c | 124 ---------------------------------- examples/nft-table-parse-add.c | 145 ++++++++++++++++++++++++++++++++++++++++ examples/nft-table-xml-add.c | 121 --------------------------------- 4 files changed, 148 insertions(+), 252 deletions(-) delete mode 100644 examples/nft-table-json-add.c create mode 100644 examples/nft-table-parse-add.c delete mode 100644 examples/nft-table-xml-add.c
examples/Makefile.am +3 -7
@@ -1,8 +1,7 @@ include $(top_srcdir)/Make_global.am check_PROGRAMS = nft-table-add \ - nft-table-xml-add \ - nft-table-json-add \ + nft-table-parse-add \ nft-table-upd \ nft-table-del \ nft-table-get \
@@ -29,11 +28,8 @@ check_PROGRAMS = nft-table-add \ nft_table_add_SOURCES = nft-table-add.c nft_table_add_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} -nft_table_xml_add_SOURCES = nft-table-xml-add.c -nft_table_xml_add_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} - -nft_table_json_add_SOURCES = nft-table-json-add.c -nft_table_json_add_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} +nft_table_parse_add_SOURCES = nft-table-parse-add.c +nft_table_parse_add_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS} nft_table_upd_SOURCES = nft-table-upd.c nft_table_upd_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS}
examples/nft-table-json-add.c deleted +0 -124
124 lines not on this page · raw message
examples/nft-table-parse-add.c new file +145 -0
145 lines not on this page · raw message
examples/nft-table-xml-add.c deleted +0 -121
121 lines not on this page · raw message
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH 2/6 examples: nft-table-parse-add: add batching support
Add batching support to operate with recent kernels.
--- examples/nft-table-parse-add.c | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-)
examples/nft-table-parse-add.c +32 -8
@@ -73,10 +73,11 @@ int main(int argc, char *argv[])struct mnl_socket *nl;char buf[MNL_SOCKET_BUFFER_SIZE ];struct nlmsghdr *nlh; -uint32_t portid, seq; +uint32_t portid, seq, table_seq;struct nft_table *t =NULL ; -int ret; +int ret, batching;uint16_t family, format, outformat; +struct mnl_nlmsg_batch *batch;if (argc <3 ) {printf ("Usage: %s {xml|json} <file>\n" , argv[0 ]);
@@ -101,14 +102,34 @@ int main(int argc, char *argv[])nft_table_fprintf (stdout, t, outformat,0 );fprintf (stdout,"\n" ); - family =nft_table_attr_get_u32 (t,NFT_TABLE_ATTR_FAMILY ); - seq =time (NULL ); + batching =nft_batch_is_supported (); +if (batching <0 ) { +perror ("cannot talk to nfnetlink" ); +exit (EXIT_FAILURE ); + } + + batch =mnl_nlmsg_batch_start (buf,sizeof (buf)); + +if (batching) { +nft_batch_begin (mnl_nlmsg_batch_current (batch), seq++); +mnl_nlmsg_batch_next (batch); + } + + family =nft_table_attr_get_u32 (t,NFT_TABLE_ATTR_FAMILY ); - nlh =nft_table_nlmsg_build_hdr (buf,NFT_MSG_NEWTABLE , family, -NLM_F_CREATE |NLM_F_ACK , seq); + table_seq = seq; + nlh =nft_table_nlmsg_build_hdr (mnl_nlmsg_batch_current (batch), +NFT_MSG_NEWTABLE , family, +NLM_F_CREATE |NLM_F_ACK , seq++);nft_table_nlmsg_build_payload (nlh, t);nft_table_free (t); +mnl_nlmsg_batch_next (batch); + +if (batching) { +nft_batch_end (mnl_nlmsg_batch_current (batch), seq++); +mnl_nlmsg_batch_next (batch); + } nl =mnl_socket_open (NETLINK_NETFILTER );if (nl ==NULL ) {
@@ -122,14 +143,17 @@ int main(int argc, char *argv[]) } portid =mnl_socket_get_portid (nl); -if (mnl_socket_sendto (nl, nlh, nlh->nlmsg_len) <0 ) { +if (mnl_socket_sendto (nl,mnl_nlmsg_batch_head (batch), +mnl_nlmsg_batch_size (batch)) <0 ) {perror ("mnl_socket_send" );exit (EXIT_FAILURE ); } +mnl_nlmsg_batch_stop (batch); + ret =mnl_socket_recvfrom (nl, buf,sizeof (buf));while (ret >0 ) { - ret =mnl_cb_run (buf, ret, seq, portid,NULL ,NULL ); + ret =mnl_cb_run (buf, ret, table_seq, portid,NULL ,NULL );if (ret <=0 )break ; ret =mnl_socket_recvfrom (nl, buf,sizeof (buf));
PATCH 3/6 examples: nft-table-add: add table_add_parse()
In reply to Arturo Borrero GonzalezThis fucntion parses the command line options and creates the nft_table object.
--- examples/nft-table-add.c | 55 +++++++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 20 deletions(-)
examples/nft-table-add.c +35 -20
@@ -20,27 +20,11 @@#include <libmnl/libmnl.h> #include <libnftnl/table.h> -int main (int argc,char *argv[]) +static struct nft_table *table_add_parse (int argc,char *argv[]) { -struct mnl_socket *nl; -char buf[MNL_SOCKET_BUFFER_SIZE ]; -struct nlmsghdr *nlh; -uint32_t portid, seq, family; -struct nft_table *t =NULL ; -int ret; - -if (argc !=3 ) { -fprintf (stderr,"%s <family> <name>\n" , argv[0 ]); -exit (EXIT_FAILURE ); - } +struct nft_table *t; +uint16_t family; - t =nft_table_alloc (); -if (t ==NULL ) { -perror ("OOM" ); -exit (EXIT_FAILURE ); - } - - seq =time (NULL );if (strcmp (argv[1 ],"ip" ) ==0 ) family =NFPROTO_IPV4 ;else if (strcmp (argv[1 ],"ip6" ) ==0 )
@@ -51,11 +35,42 @@ int main(int argc, char *argv[]) family =NFPROTO_ARP ; else {fprintf (stderr,"Unknown family: ip, ip6, bridge, arp\n" ); +return NULL ; + } + + t =nft_table_alloc (); +if (t ==NULL ) { +perror ("OOM" ); +return NULL ; + } + +nft_table_attr_set_u32 (t,NFT_TABLE_ATTR_FAMILY , family); +nft_table_attr_set_str (t,NFT_TABLE_ATTR_NAME , argv[2 ]); + +return t; +} + +int main (int argc,char *argv[]) +{ +struct mnl_socket *nl; +char buf[MNL_SOCKET_BUFFER_SIZE ]; +struct nlmsghdr *nlh; +uint32_t portid, seq, family; +struct nft_table *t; +int ret; + +if (argc !=3 ) { +fprintf (stderr,"%s <family> <name>\n" , argv[0 ]);exit (EXIT_FAILURE ); } -nft_table_attr_set (t,NFT_TABLE_ATTR_NAME , argv[2 ]); + t =table_add_parse (argc, argv); +if (t ==NULL ) +exit (EXIT_FAILURE ); + + seq =time (NULL ); + family =nft_table_attr_get_u32 (t,NFT_TABLE_ATTR_FAMILY ); nlh =nft_table_nlmsg_build_hdr (buf,NFT_MSG_NEWTABLE , family,NLM_F_ACK , seq);nft_table_nlmsg_build_payload (nlh, t);
PATCH 4/6 examples: nft-table-add: add batching support
In reply to Arturo Borrero GonzalezAdds batching support to this code example, so it works with current kernels.
--- examples/nft-table-add.c | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-)
examples/nft-table-add.c +28 -5
@@ -55,9 +55,10 @@ int main(int argc, char *argv[])struct mnl_socket *nl;char buf[MNL_SOCKET_BUFFER_SIZE ];struct nlmsghdr *nlh; -uint32_t portid, seq, family; +uint32_t portid, seq, table_seq, family;struct nft_table *t; -int ret; +struct mnl_nlmsg_batch *batch; +int ret, batching;if (argc !=3 ) {fprintf (stderr,"%s <family> <name>\n" , argv[0 ]);
@@ -68,13 +69,32 @@ int main(int argc, char *argv[])if (t ==NULL )exit (EXIT_FAILURE ); + batching =nft_batch_is_supported (); +if (batching <0 ) { +perror ("cannot talk to nfnetlink" ); +exit (EXIT_FAILURE ); + } seq =time (NULL ); + batch =mnl_nlmsg_batch_start (buf,sizeof (buf)); + +if (batching) { +nft_batch_begin (mnl_nlmsg_batch_current (batch), seq++); +mnl_nlmsg_batch_next (batch); + } + + table_seq = seq; family =nft_table_attr_get_u32 (t,NFT_TABLE_ATTR_FAMILY ); nlh =nft_table_nlmsg_build_hdr (buf,NFT_MSG_NEWTABLE , family, -NLM_F_ACK , seq); +NLM_F_ACK , seq++);nft_table_nlmsg_build_payload (nlh, t);nft_table_free (t); +mnl_nlmsg_batch_next (batch); + +if (batching) { +nft_batch_end (mnl_nlmsg_batch_current (batch), seq++); +mnl_nlmsg_batch_next (batch); + } nl =mnl_socket_open (NETLINK_NETFILTER );if (nl ==NULL ) {
@@ -88,14 +108,17 @@ int main(int argc, char *argv[]) } portid =mnl_socket_get_portid (nl); -if (mnl_socket_sendto (nl, nlh, nlh->nlmsg_len) <0 ) { +if (mnl_socket_sendto (nl,mnl_nlmsg_batch_head (batch), +mnl_nlmsg_batch_size (batch)) <0 ) {perror ("mnl_socket_send" );exit (EXIT_FAILURE ); } +mnl_nlmsg_batch_stop (batch); + ret =mnl_socket_recvfrom (nl, buf,sizeof (buf));while (ret >0 ) { - ret =mnl_cb_run (buf, ret, seq, portid,NULL ,NULL ); + ret =mnl_cb_run (buf, ret, table_seq, portid,NULL ,NULL );if (ret <=0 )break ; ret =mnl_socket_recvfrom (nl, buf,sizeof (buf));
PATCH 5/6 examples: nft-table-del: add table_del_parse()
In reply to Arturo Borrero GonzalezThis new function parses the input arguments and generates the nft_table.
--- examples/nft-table-del.c | 55 +++++++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 20 deletions(-)
examples/nft-table-del.c +35 -20
@@ -20,13 +20,43 @@#include <libmnl/libmnl.h> #include <libnftnl/table.h> +static struct nft_table *table_del_parse (int argc,char *argv[]) +{ +struct nft_table *t; +uint16_t family; + +if (strcmp (argv[1 ],"ip" ) ==0 ) + family =NFPROTO_IPV4 ; +else if (strcmp (argv[1 ],"ip6" ) ==0 ) + family =NFPROTO_IPV6 ; +else if (strcmp (argv[1 ],"bridge" ) ==0 ) + family =NFPROTO_BRIDGE ; +else if (strcmp (argv[1 ],"arp" ) ==0 ) + family =NFPROTO_ARP ; +else { +fprintf (stderr,"Unknown family: ip, ip6, bridge, arp\n" ); +return NULL ; + } + + t =nft_table_alloc (); +if (t ==NULL ) { +perror ("OOM" ); +return NULL ; + } + +nft_table_attr_set_str (t,NFT_TABLE_ATTR_NAME , argv[2 ]); +nft_table_attr_set_u32 (t,NFT_TABLE_ATTR_FAMILY , family); + +return t; +} +int main (int argc,char *argv[]) {struct mnl_socket *nl;char buf[MNL_SOCKET_BUFFER_SIZE ];struct nlmsghdr *nlh;uint32_t portid, seq, family; -struct nft_table *t =NULL ; +struct nft_table *t;int ret;if (argc !=3 ) {
@@ -34,30 +64,15 @@ int main(int argc, char *argv[])exit (EXIT_FAILURE ); } - t =nft_table_alloc (); -if (t ==NULL ) { -perror ("OOM" ); + t =table_del_parse (argc, argv); +if (t ==NULL )exit (EXIT_FAILURE ); - } - seq =time (NULL ); -if (strcmp (argv[1 ],"ip" ) ==0 ) - family =NFPROTO_IPV4 ; -else if (strcmp (argv[1 ],"ip6" ) ==0 ) - family =NFPROTO_IPV6 ; -else if (strcmp (argv[1 ],"bridge" ) ==0 ) - family =NFPROTO_BRIDGE ; -else if (strcmp (argv[1 ],"arp" ) ==0 ) - family =NFPROTO_ARP ; -else { -fprintf (stderr,"Unknown family: ip, ip6, bridge, arp\n" ); -exit (EXIT_FAILURE ); - } + seq =time (NULL ); + family =nft_table_attr_get_u32 (t,NFT_TABLE_ATTR_FAMILY ); nlh =nft_table_nlmsg_build_hdr (buf,NFT_MSG_DELTABLE , family,NLM_F_ACK , seq); -nft_table_attr_set (t,NFT_TABLE_ATTR_NAME , argv[2 ]); -nft_table_nlmsg_build_payload (nlh, t);nft_table_free (t);
PATCH 6/6 examples: nft-table-del: add batching support
In reply to Arturo Borrero GonzalezAdd batching support so this code example works with current kernels.
--- examples/nft-table-del.c | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-)
examples/nft-table-del.c +30 -6
@@ -55,9 +55,10 @@ int main(int argc, char *argv[])struct mnl_socket *nl;char buf[MNL_SOCKET_BUFFER_SIZE ];struct nlmsghdr *nlh; -uint32_t portid, seq, family; +uint32_t portid, seq, table_seq, family;struct nft_table *t; -int ret; +struct mnl_nlmsg_batch *batch; +int ret, batching;if (argc !=3 ) {fprintf (stderr,"%s <family> <name>\n" , argv[0 ]);
@@ -68,14 +69,34 @@ int main(int argc, char *argv[])if (t ==NULL )exit (EXIT_FAILURE ); + batching =nft_batch_is_supported (); +if (batching <0 ) { +perror ("cannot talk to nfnetlink" ); +exit (EXIT_FAILURE ); + } seq =time (NULL ); + batch =mnl_nlmsg_batch_start (buf,sizeof (buf)); + +if (batching) { +nft_batch_begin (mnl_nlmsg_batch_current (batch), seq++); +mnl_nlmsg_batch_next (batch); + } + + table_seq = seq; family =nft_table_attr_get_u32 (t,NFT_TABLE_ATTR_FAMILY ); - nlh =nft_table_nlmsg_build_hdr (buf,NFT_MSG_DELTABLE , family, -NLM_F_ACK , seq); + nlh =nft_table_nlmsg_build_hdr (mnl_nlmsg_batch_current (batch), +NFT_MSG_DELTABLE , family, +NLM_F_ACK , seq++);nft_table_nlmsg_build_payload (nlh, t); +mnl_nlmsg_batch_next (batch);nft_table_free (t); +if (batching) { +nft_batch_end (mnl_nlmsg_batch_current (batch), seq++); +mnl_nlmsg_batch_next (batch); + } + nl =mnl_socket_open (NETLINK_NETFILTER );if (nl ==NULL ) {perror ("mnl_socket_open" );
@@ -88,14 +109,17 @@ int main(int argc, char *argv[]) } portid =mnl_socket_get_portid (nl); -if (mnl_socket_sendto (nl, nlh, nlh->nlmsg_len) <0 ) { +if (mnl_socket_sendto (nl,mnl_nlmsg_batch_head (batch), +mnl_nlmsg_batch_size (batch)) <0 ) {perror ("mnl_socket_send" );exit (EXIT_FAILURE ); } +mnl_nlmsg_batch_stop (batch); + ret =mnl_socket_recvfrom (nl, buf,sizeof (buf));while (ret >0 ) { - ret =mnl_cb_run (buf, ret, seq, portid,NULL ,NULL ); + ret =mnl_cb_run (buf, ret, table_seq, portid,NULL ,NULL );if (ret <=0 )break ; ret =mnl_socket_recvfrom (nl, buf,sizeof (buf));
I have applied these series (from 1 to 6).
Thanks Arturo.