netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [libnftnl PATCH 1/6] examples: merge nft-table-{xml|json}-add.c
@ 2014-08-22  9:39 Arturo Borrero Gonzalez
  2014-08-22  9:39 ` [libnftnl PATCH 2/6] examples: nft-table-parse-add: add batching support Arturo Borrero Gonzalez
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: Arturo Borrero Gonzalez @ 2014-08-22  9:39 UTC (permalink / raw)
  To: netfilter-devel; +Cc: pablo

Merge the two examples in one. An input argument choose the format to use.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
---
 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

diff --git a/examples/Makefile.am b/examples/Makefile.am
index f35924d..69f5c7f 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -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}
diff --git a/examples/nft-table-json-add.c b/examples/nft-table-json-add.c
deleted file mode 100644
index a9bd217..0000000
--- a/examples/nft-table-json-add.c
+++ /dev/null
@@ -1,124 +0,0 @@
-/*
- * (C) 2013 by Álvaro Neira Ayuso <alvaroneay@gmail.com>
- *
- * Based on nft-table-xml-add from:
- *
- * (C) 2013 by Pablo Neira Ayuso <pablo@netfilter.org>
- * (C) 2013 by Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This code has been sponsored by Sophos Astaro <http://www.sophos.com>
- */
-
-#include <stdlib.h>
-#include <time.h>
-#include <string.h>
-#include <netinet/in.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <errno.h>
-
-#include <linux/netfilter/nf_tables.h>
-
-#include <libmnl/libmnl.h>
-#include <libnftnl/table.h>
-
-int main(int argc, char *argv[])
-{
-	struct mnl_socket *nl;
-	char buf[MNL_SOCKET_BUFFER_SIZE];
-	struct nlmsghdr *nlh;
-	uint32_t portid, seq;
-	struct nft_table *t = NULL;
-	int ret, fd;
-	uint16_t family;
-	char json[4096];
-	char reprint[4096];
-	struct nft_parse_err *err;
-
-	if (argc < 2) {
-		printf("Usage: %s <json-file>\n", argv[0]);
-		exit(EXIT_FAILURE);
-	}
-
-	fd = open(argv[1], O_RDONLY);
-	if (fd < 0) {
-		perror("open");
-		exit(EXIT_FAILURE);
-	}
-
-	if (read(fd, json, sizeof(json)) < 0) {
-		perror("read");
-		close(fd);
-		exit(EXIT_FAILURE);
-	}
-	close(fd);
-
-	t = nft_table_alloc();
-	if (t == NULL) {
-		perror("OOM");
-		exit(EXIT_FAILURE);
-	}
-
-	err = nft_parse_err_alloc();
-	if (err == NULL) {
-		perror("error");
-		exit(EXIT_FAILURE);
-	}
-
-	if (nft_table_parse(t, NFT_PARSE_JSON, json, err) < 0) {
-		nft_parse_perror("Unable to parse JSON file", err);
-		exit(EXIT_FAILURE);
-	}
-
-	nft_table_snprintf(reprint, sizeof(reprint), t, NFT_OUTPUT_JSON, 0);
-	printf("Parsed:\n%s\n", reprint);
-
-	family = nft_table_attr_get_u32(t, NFT_TABLE_ATTR_FAMILY);
-
-	seq = time(NULL);
-
-	nlh = nft_table_nlmsg_build_hdr(buf, NFT_MSG_NEWTABLE, family,
-					NLM_F_CREATE|NLM_F_ACK, seq);
-	nft_table_nlmsg_build_payload(nlh, t);
-	nft_table_free(t);
-	nft_parse_err_free(err);
-
-	nl = mnl_socket_open(NETLINK_NETFILTER);
-	if (nl == NULL) {
-		perror("mnl_socket_open");
-		exit(EXIT_FAILURE);
-	}
-
-	if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
-		perror("mnl_socket_bind");
-		exit(EXIT_FAILURE);
-	}
-	portid = mnl_socket_get_portid(nl);
-
-	if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
-		perror("mnl_socket_send");
-		exit(EXIT_FAILURE);
-	}
-
-	ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
-	while (ret > 0) {
-		ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL);
-		if (ret <= 0)
-			break;
-		ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
-	}
-	if (ret == -1) {
-		perror("error");
-		exit(EXIT_FAILURE);
-	}
-
-	mnl_socket_close(nl);
-
-	return EXIT_SUCCESS;
-}
diff --git a/examples/nft-table-parse-add.c b/examples/nft-table-parse-add.c
new file mode 100644
index 0000000..0faedad
--- /dev/null
+++ b/examples/nft-table-parse-add.c
@@ -0,0 +1,145 @@
+/*
+ * (C) 2013 by Pablo Neira Ayuso <pablo@netfilter.org>
+ * (C) 2013 by Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This code has been sponsored by Sophos Astaro <http://www.sophos.com>
+ */
+
+#include <stdlib.h>
+#include <time.h>
+#include <string.h>
+#include <netinet/in.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#include <linux/netfilter/nf_tables.h>
+
+#include <libmnl/libmnl.h>
+#include <libnftnl/table.h>
+#include <libnftnl/common.h>
+
+static struct nft_table *table_parse_file(const char *file, uint16_t format)
+{
+	int fd;
+	struct nft_table *t;
+	struct nft_parse_err *err;
+	char data[4096];
+
+	t = nft_table_alloc();
+	if (t == NULL) {
+		perror("OOM");
+		return NULL;
+	}
+
+	fd = open(file, O_RDONLY);
+	if (fd < 0) {
+		perror("open");
+		return NULL;
+	}
+
+	if (read(fd, data, sizeof(data)) < 0) {
+		perror("read");
+		close(fd);
+		return NULL;
+	}
+	close(fd);
+
+	err = nft_parse_err_alloc();
+	if (err == NULL) {
+		perror("error");
+		return NULL;
+	}
+
+	if (nft_table_parse(t, format, data, err) < 0) {
+		nft_parse_perror("Unable to parse file", err);
+		nft_parse_err_free(err);
+		return NULL;
+	}
+
+	nft_parse_err_free(err);
+	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;
+	struct nft_table *t = NULL;
+	int ret;
+	uint16_t family, format, outformat;
+
+	if (argc < 3) {
+		printf("Usage: %s {xml|json} <file>\n", argv[0]);
+		exit(EXIT_FAILURE);
+	}
+
+	if (strcmp(argv[1], "xml") == 0) {
+		format = NFT_PARSE_XML;
+		outformat = NFT_OUTPUT_XML;
+	} else if (strcmp(argv[1], "json") == 0) {
+		format = NFT_PARSE_JSON;
+		outformat = NFT_OUTPUT_JSON;
+	} else {
+		printf("Unknown format: xml, json\n");
+		exit(EXIT_FAILURE);
+	}
+
+	t = table_parse_file(argv[2], format);
+	if (t == NULL)
+		exit(EXIT_FAILURE);
+
+	nft_table_fprintf(stdout, t, outformat, 0);
+	fprintf(stdout, "\n");
+
+	family = nft_table_attr_get_u32(t, NFT_TABLE_ATTR_FAMILY);
+
+	seq = time(NULL);
+
+	nlh = nft_table_nlmsg_build_hdr(buf, NFT_MSG_NEWTABLE, family,
+					NLM_F_CREATE|NLM_F_ACK, seq);
+	nft_table_nlmsg_build_payload(nlh, t);
+	nft_table_free(t);
+
+	nl = mnl_socket_open(NETLINK_NETFILTER);
+	if (nl == NULL) {
+		perror("mnl_socket_open");
+		exit(EXIT_FAILURE);
+	}
+
+	if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
+		perror("mnl_socket_bind");
+		exit(EXIT_FAILURE);
+	}
+	portid = mnl_socket_get_portid(nl);
+
+	if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
+		perror("mnl_socket_send");
+		exit(EXIT_FAILURE);
+	}
+
+	ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
+	while (ret > 0) {
+		ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL);
+		if (ret <= 0)
+			break;
+		ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
+	}
+	if (ret == -1) {
+		perror("error");
+		exit(EXIT_FAILURE);
+	}
+
+	mnl_socket_close(nl);
+
+	return EXIT_SUCCESS;
+}
diff --git a/examples/nft-table-xml-add.c b/examples/nft-table-xml-add.c
deleted file mode 100644
index f36f0ab..0000000
--- a/examples/nft-table-xml-add.c
+++ /dev/null
@@ -1,121 +0,0 @@
-/*
- * (C) 2013 by Pablo Neira Ayuso <pablo@netfilter.org>
- * (C) 2013 by Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This code has been sponsored by Sophos Astaro <http://www.sophos.com>
- */
-
-#include <stdlib.h>
-#include <time.h>
-#include <string.h>
-#include <netinet/in.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <errno.h>
-
-#include <linux/netfilter/nf_tables.h>
-
-#include <libmnl/libmnl.h>
-#include <libnftnl/table.h>
-#include <libnftnl/common.h>
-
-int main(int argc, char *argv[])
-{
-	struct mnl_socket *nl;
-	char buf[MNL_SOCKET_BUFFER_SIZE];
-	struct nlmsghdr *nlh;
-	uint32_t portid, seq;
-	struct nft_table *t = NULL;
-	int ret, fd;
-	uint16_t family;
-	char xml[4096];
-	char reprint[4096];
-	struct nft_parse_err *err;
-
-	if (argc < 2) {
-		printf("Usage: %s <xml-file>\n", argv[0]);
-		exit(EXIT_FAILURE);
-	}
-
-	fd = open(argv[1], O_RDONLY);
-	if (fd < 0) {
-		perror("open");
-		exit(EXIT_FAILURE);
-	}
-
-	if (read(fd, xml, sizeof(xml)) < 0) {
-		perror("read");
-		close(fd);
-		exit(EXIT_FAILURE);
-	}
-	close(fd);
-
-	t = nft_table_alloc();
-	if (t == NULL) {
-		perror("OOM");
-		exit(EXIT_FAILURE);
-	}
-
-	err = nft_parse_err_alloc();
-	if (err == NULL) {
-		perror("error");
-		exit(EXIT_FAILURE);
-	}
-
-	if (nft_table_parse(t, NFT_PARSE_XML, xml, err) < 0) {
-		nft_parse_perror("Unable to parse XML file", err);
-		exit(EXIT_FAILURE);
-	}
-
-	nft_table_snprintf(reprint, sizeof(reprint), t, NFT_OUTPUT_XML, 0);
-	printf("Parsed:\n%s\n", reprint);
-
-	family = nft_table_attr_get_u32(t, NFT_TABLE_ATTR_FAMILY);
-
-	seq = time(NULL);
-
-	nlh = nft_table_nlmsg_build_hdr(buf, NFT_MSG_NEWTABLE, family,
-					NLM_F_CREATE|NLM_F_ACK, seq);
-	nft_table_nlmsg_build_payload(nlh, t);
-	nft_table_free(t);
-	nft_parse_err_free(err);
-
-	nl = mnl_socket_open(NETLINK_NETFILTER);
-	if (nl == NULL) {
-		perror("mnl_socket_open");
-		exit(EXIT_FAILURE);
-	}
-
-	if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
-		perror("mnl_socket_bind");
-		exit(EXIT_FAILURE);
-	}
-	portid = mnl_socket_get_portid(nl);
-
-	if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
-		perror("mnl_socket_send");
-		exit(EXIT_FAILURE);
-	}
-
-	ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
-	while (ret > 0) {
-		ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL);
-		if (ret <= 0)
-			break;
-		ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
-	}
-	if (ret == -1) {
-		perror("error");
-		exit(EXIT_FAILURE);
-	}
-
-	mnl_socket_close(nl);
-
-	return EXIT_SUCCESS;
-}

--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [libnftnl PATCH 2/6] examples: nft-table-parse-add: add batching support
  2014-08-22  9:39 [libnftnl PATCH 1/6] examples: merge nft-table-{xml|json}-add.c Arturo Borrero Gonzalez
@ 2014-08-22  9:39 ` Arturo Borrero Gonzalez
  2014-08-22  9:39 ` [libnftnl PATCH 3/6] examples: nft-table-add: add table_add_parse() Arturo Borrero Gonzalez
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Arturo Borrero Gonzalez @ 2014-08-22  9:39 UTC (permalink / raw)
  To: netfilter-devel; +Cc: pablo

Add batching support to operate with recent kernels.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
---
 examples/nft-table-parse-add.c |   40 ++++++++++++++++++++++++++++++++--------
 1 file changed, 32 insertions(+), 8 deletions(-)

diff --git a/examples/nft-table-parse-add.c b/examples/nft-table-parse-add.c
index 0faedad..c21b6ba 100644
--- a/examples/nft-table-parse-add.c
+++ b/examples/nft-table-parse-add.c
@@ -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));


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [libnftnl PATCH 3/6] examples: nft-table-add: add table_add_parse()
  2014-08-22  9:39 [libnftnl PATCH 1/6] examples: merge nft-table-{xml|json}-add.c Arturo Borrero Gonzalez
  2014-08-22  9:39 ` [libnftnl PATCH 2/6] examples: nft-table-parse-add: add batching support Arturo Borrero Gonzalez
@ 2014-08-22  9:39 ` Arturo Borrero Gonzalez
  2014-08-22  9:39 ` [libnftnl PATCH 4/6] examples: nft-table-add: add batching support Arturo Borrero Gonzalez
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Arturo Borrero Gonzalez @ 2014-08-22  9:39 UTC (permalink / raw)
  To: netfilter-devel; +Cc: pablo

This fucntion parses the command line options and creates the nft_table object.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
---
 examples/nft-table-add.c |   55 +++++++++++++++++++++++++++++-----------------
 1 file changed, 35 insertions(+), 20 deletions(-)

diff --git a/examples/nft-table-add.c b/examples/nft-table-add.c
index 3b7572f..9c3ae6f 100644
--- a/examples/nft-table-add.c
+++ b/examples/nft-table-add.c
@@ -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);


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [libnftnl PATCH 4/6] examples: nft-table-add: add batching support
  2014-08-22  9:39 [libnftnl PATCH 1/6] examples: merge nft-table-{xml|json}-add.c Arturo Borrero Gonzalez
  2014-08-22  9:39 ` [libnftnl PATCH 2/6] examples: nft-table-parse-add: add batching support Arturo Borrero Gonzalez
  2014-08-22  9:39 ` [libnftnl PATCH 3/6] examples: nft-table-add: add table_add_parse() Arturo Borrero Gonzalez
@ 2014-08-22  9:39 ` Arturo Borrero Gonzalez
  2014-08-22  9:39 ` [libnftnl PATCH 5/6] examples: nft-table-del: add table_del_parse() Arturo Borrero Gonzalez
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Arturo Borrero Gonzalez @ 2014-08-22  9:39 UTC (permalink / raw)
  To: netfilter-devel; +Cc: pablo

Adds batching support to this code example, so it works with current kernels.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
---
 examples/nft-table-add.c |   33 ++++++++++++++++++++++++++++-----
 1 file changed, 28 insertions(+), 5 deletions(-)

diff --git a/examples/nft-table-add.c b/examples/nft-table-add.c
index 9c3ae6f..828091e 100644
--- a/examples/nft-table-add.c
+++ b/examples/nft-table-add.c
@@ -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));


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [libnftnl PATCH 5/6] examples: nft-table-del: add table_del_parse()
  2014-08-22  9:39 [libnftnl PATCH 1/6] examples: merge nft-table-{xml|json}-add.c Arturo Borrero Gonzalez
                   ` (2 preceding siblings ...)
  2014-08-22  9:39 ` [libnftnl PATCH 4/6] examples: nft-table-add: add batching support Arturo Borrero Gonzalez
@ 2014-08-22  9:39 ` Arturo Borrero Gonzalez
  2014-08-22  9:39 ` [libnftnl PATCH 6/6] examples: nft-table-del: add batching support Arturo Borrero Gonzalez
  2014-08-24 13:01 ` [libnftnl PATCH 1/6] examples: merge nft-table-{xml|json}-add.c Pablo Neira Ayuso
  5 siblings, 0 replies; 7+ messages in thread
From: Arturo Borrero Gonzalez @ 2014-08-22  9:39 UTC (permalink / raw)
  To: netfilter-devel; +Cc: pablo

This new function parses the input arguments and generates the nft_table.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
---
 examples/nft-table-del.c |   55 +++++++++++++++++++++++++++++-----------------
 1 file changed, 35 insertions(+), 20 deletions(-)

diff --git a/examples/nft-table-del.c b/examples/nft-table-del.c
index ed1140d..ae7db27 100644
--- a/examples/nft-table-del.c
+++ b/examples/nft-table-del.c
@@ -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);
 


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [libnftnl PATCH 6/6] examples: nft-table-del: add batching support
  2014-08-22  9:39 [libnftnl PATCH 1/6] examples: merge nft-table-{xml|json}-add.c Arturo Borrero Gonzalez
                   ` (3 preceding siblings ...)
  2014-08-22  9:39 ` [libnftnl PATCH 5/6] examples: nft-table-del: add table_del_parse() Arturo Borrero Gonzalez
@ 2014-08-22  9:39 ` Arturo Borrero Gonzalez
  2014-08-24 13:01 ` [libnftnl PATCH 1/6] examples: merge nft-table-{xml|json}-add.c Pablo Neira Ayuso
  5 siblings, 0 replies; 7+ messages in thread
From: Arturo Borrero Gonzalez @ 2014-08-22  9:39 UTC (permalink / raw)
  To: netfilter-devel; +Cc: pablo

Add batching support so this code example works with current kernels.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
---
 examples/nft-table-del.c |   36 ++++++++++++++++++++++++++++++------
 1 file changed, 30 insertions(+), 6 deletions(-)

diff --git a/examples/nft-table-del.c b/examples/nft-table-del.c
index ae7db27..334d20b 100644
--- a/examples/nft-table-del.c
+++ b/examples/nft-table-del.c
@@ -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));


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [libnftnl PATCH 1/6] examples: merge nft-table-{xml|json}-add.c
  2014-08-22  9:39 [libnftnl PATCH 1/6] examples: merge nft-table-{xml|json}-add.c Arturo Borrero Gonzalez
                   ` (4 preceding siblings ...)
  2014-08-22  9:39 ` [libnftnl PATCH 6/6] examples: nft-table-del: add batching support Arturo Borrero Gonzalez
@ 2014-08-24 13:01 ` Pablo Neira Ayuso
  5 siblings, 0 replies; 7+ messages in thread
From: Pablo Neira Ayuso @ 2014-08-24 13:01 UTC (permalink / raw)
  To: Arturo Borrero Gonzalez; +Cc: netfilter-devel

I have applied these series (from 1 to 6).

Thanks Arturo.


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2014-08-24 13:00 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-08-22  9:39 [libnftnl PATCH 1/6] examples: merge nft-table-{xml|json}-add.c Arturo Borrero Gonzalez
2014-08-22  9:39 ` [libnftnl PATCH 2/6] examples: nft-table-parse-add: add batching support Arturo Borrero Gonzalez
2014-08-22  9:39 ` [libnftnl PATCH 3/6] examples: nft-table-add: add table_add_parse() Arturo Borrero Gonzalez
2014-08-22  9:39 ` [libnftnl PATCH 4/6] examples: nft-table-add: add batching support Arturo Borrero Gonzalez
2014-08-22  9:39 ` [libnftnl PATCH 5/6] examples: nft-table-del: add table_del_parse() Arturo Borrero Gonzalez
2014-08-22  9:39 ` [libnftnl PATCH 6/6] examples: nft-table-del: add batching support Arturo Borrero Gonzalez
2014-08-24 13:01 ` [libnftnl PATCH 1/6] examples: merge nft-table-{xml|json}-add.c Pablo Neira Ayuso

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).