From: Pablo Neira Ayuso <pablo@netfilter.org>
To: netfilter-devel@vger.kernel.org
Cc: kaber@trash.net
Subject: [PATCH 2/3 nft] src: add chain netlink messages to the batch
Date: Thu, 27 Mar 2014 22:54:50 +0100 [thread overview]
Message-ID: <1395957291-5018-2-git-send-email-pablo@netfilter.org> (raw)
In-Reply-To: <1395957291-5018-1-git-send-email-pablo@netfilter.org>
This patch moves the chain netlink messages to the big netlink
batch that is sent to kernel-space.
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
---
include/mnl.h | 4 ++
src/mnl.c | 35 ++++++++++++++++
src/netlink.c | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++----
3 files changed, 158 insertions(+), 7 deletions(-)
diff --git a/include/mnl.h b/include/mnl.h
index f328fc9..461f2c2 100644
--- a/include/mnl.h
+++ b/include/mnl.h
@@ -35,8 +35,12 @@ struct nft_rule_list *mnl_nft_rule_dump(struct mnl_socket *nf_sock,
int mnl_nft_chain_add(struct mnl_socket *nf_sock, struct nft_chain *nlc,
unsigned int flags);
+int mnl_nft_chain_batch_add(struct mnl_socket *nf_sock, struct nft_chain *nlc,
+ unsigned int flags);
int mnl_nft_chain_delete(struct mnl_socket *nf_sock, struct nft_chain *nlc,
unsigned int flags);
+int mnl_nft_chain_batch_del(struct mnl_socket *nf_sock, struct nft_chain *nlc,
+ unsigned int flags);
struct nft_chain_list *mnl_nft_chain_dump(struct mnl_socket *nf_sock,
int family);
int mnl_nft_chain_get(struct mnl_socket *nf_sock, struct nft_chain *nlc,
diff --git a/src/mnl.c b/src/mnl.c
index 2565bcd..d1a21f9 100644
--- a/src/mnl.c
+++ b/src/mnl.c
@@ -412,6 +412,24 @@ int mnl_nft_chain_add(struct mnl_socket *nf_sock, struct nft_chain *nlc,
return mnl_talk(nf_sock, nlh, nlh->nlmsg_len, NULL, NULL);
}
+int mnl_nft_chain_batch_add(struct mnl_socket *nf_sock, struct nft_chain *nlc,
+ unsigned int flags)
+
+{
+ struct nlmsghdr *nlh;
+
+ nlh = nft_chain_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
+ 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);
+
+ if (!mnl_nlmsg_batch_next(batch))
+ mnl_batch_page_add();
+
+ return 0;
+}
+
int mnl_nft_chain_delete(struct mnl_socket *nf_sock, struct nft_chain *nlc,
unsigned int flags)
{
@@ -426,6 +444,23 @@ int mnl_nft_chain_delete(struct mnl_socket *nf_sock, struct nft_chain *nlc,
return mnl_talk(nf_sock, nlh, nlh->nlmsg_len, NULL, NULL);
}
+int mnl_nft_chain_batch_del(struct mnl_socket *nf_sock, struct nft_chain *nlc,
+ unsigned int flags)
+{
+ struct nlmsghdr *nlh;
+
+ nlh = nft_chain_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
+ NFT_MSG_DELCHAIN,
+ nft_chain_attr_get_u32(nlc, NFT_CHAIN_ATTR_FAMILY),
+ NLM_F_ACK, seq);
+ nft_chain_nlmsg_build_payload(nlh, nlc);
+
+ if (!mnl_nlmsg_batch_next(batch))
+ mnl_batch_page_add();
+
+ return 0;
+}
+
static int chain_cb(const struct nlmsghdr *nlh, void *data)
{
struct nft_chain_list *nlc_list = data;
diff --git a/src/netlink.c b/src/netlink.c
index 922a596..a78b284 100644
--- a/src/netlink.c
+++ b/src/netlink.c
@@ -441,9 +441,10 @@ void netlink_dump_chain(struct nft_chain *nlc)
#endif
}
-int netlink_add_chain(struct netlink_ctx *ctx, const struct handle *h,
- const struct location *loc, const struct chain *chain,
- bool excl)
+static int netlink_add_chain_compat(struct netlink_ctx *ctx,
+ const struct handle *h,
+ const struct location *loc,
+ const struct chain *chain, bool excl)
{
struct nft_chain *nlc;
int err;
@@ -467,8 +468,52 @@ int netlink_add_chain(struct netlink_ctx *ctx, const struct handle *h,
return err;
}
-int netlink_rename_chain(struct netlink_ctx *ctx, const struct handle *h,
- const struct location *loc, const char *name)
+static int netlink_add_chain_batch(struct netlink_ctx *ctx,
+ const struct handle *h,
+ const struct location *loc,
+ const struct chain *chain, bool excl)
+{
+ struct nft_chain *nlc;
+ int err;
+
+ nlc = alloc_nft_chain(h);
+ if (chain != NULL && chain->flags & CHAIN_F_BASECHAIN) {
+ nft_chain_attr_set_u32(nlc, NFT_CHAIN_ATTR_HOOKNUM,
+ chain->hooknum);
+ nft_chain_attr_set_u32(nlc, NFT_CHAIN_ATTR_PRIO,
+ chain->priority);
+ nft_chain_attr_set_str(nlc, NFT_CHAIN_ATTR_TYPE,
+ chain->type);
+ }
+ netlink_dump_chain(nlc);
+ err = mnl_nft_chain_batch_add(nf_sock, nlc, excl ? NLM_F_EXCL : 0);
+ nft_chain_free(nlc);
+
+ if (err < 0) {
+ netlink_io_error(ctx, loc, "Could not add chain: %s",
+ strerror(errno));
+ }
+ return err;
+}
+
+int netlink_add_chain(struct netlink_ctx *ctx, const struct handle *h,
+ const struct location *loc, const struct chain *chain,
+ bool excl)
+{
+ int ret;
+
+ if (ctx->batch_supported)
+ ret = netlink_add_chain_batch(ctx, h, loc, chain, excl);
+ else
+ ret = netlink_add_chain_compat(ctx, h, loc, chain, excl);
+
+ return ret;
+}
+
+static int netlink_rename_chain_compat(struct netlink_ctx *ctx,
+ const struct handle *h,
+ const struct location *loc,
+ const char *name)
{
struct nft_chain *nlc;
int err;
@@ -485,8 +530,43 @@ int netlink_rename_chain(struct netlink_ctx *ctx, const struct handle *h,
return err;
}
-int netlink_delete_chain(struct netlink_ctx *ctx, const struct handle *h,
- const struct location *loc)
+static int netlink_rename_chain_batch(struct netlink_ctx *ctx,
+ const struct handle *h,
+ const struct location *loc,
+ const char *name)
+{
+ struct nft_chain *nlc;
+ int err;
+
+ nlc = alloc_nft_chain(h);
+ nft_chain_attr_set_str(nlc, NFT_CHAIN_ATTR_NAME, name);
+ netlink_dump_chain(nlc);
+ err = mnl_nft_chain_batch_add(nf_sock, nlc, 0);
+ nft_chain_free(nlc);
+
+ if (err < 0) {
+ netlink_io_error(ctx, loc, "Could not rename chain: %s",
+ strerror(errno));
+ }
+ return err;
+}
+
+int netlink_rename_chain(struct netlink_ctx *ctx, const struct handle *h,
+ const struct location *loc, const char *name)
+{
+ int ret;
+
+ if (ctx->batch_supported)
+ ret = netlink_rename_chain_batch(ctx, h, loc, name);
+ else
+ ret = netlink_rename_chain_compat(ctx, h, loc, name);
+
+ return ret;
+}
+
+static int netlink_del_chain_compat(struct netlink_ctx *ctx,
+ const struct handle *h,
+ const struct location *loc)
{
struct nft_chain *nlc;
int err;
@@ -496,12 +576,44 @@ int netlink_delete_chain(struct netlink_ctx *ctx, const struct handle *h,
err = mnl_nft_chain_delete(nf_sock, nlc, 0);
nft_chain_free(nlc);
+ if (err < 0) {
+ netlink_io_error(ctx, loc, "Could not delete chain: %s",
+ strerror(errno));
+ }
+ return err;
+}
+
+static int netlink_del_chain_batch(struct netlink_ctx *ctx,
+ const struct handle *h,
+ const struct location *loc)
+{
+ struct nft_chain *nlc;
+ int err;
+
+ nlc = alloc_nft_chain(h);
+ netlink_dump_chain(nlc);
+ err = mnl_nft_chain_batch_del(nf_sock, nlc, 0);
+ nft_chain_free(nlc);
+
if (err < 0)
netlink_io_error(ctx, loc, "Could not delete chain: %s",
strerror(errno));
return err;
}
+int netlink_delete_chain(struct netlink_ctx *ctx, const struct handle *h,
+ const struct location *loc)
+{
+ int ret;
+
+ if (ctx->batch_supported)
+ ret = netlink_del_chain_batch(ctx, h, loc);
+ else
+ ret = netlink_del_chain_compat(ctx, h, loc);
+
+ return ret;
+}
+
static int list_chain_cb(struct nft_chain *nlc, void *arg)
{
struct netlink_ctx *ctx = arg;
--
1.7.10.4
next prev parent reply other threads:[~2014-03-27 21:55 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-03-27 21:54 [PATCH 1/3 nft] src: add set netlink message to the batch Pablo Neira Ayuso
2014-03-27 21:54 ` Pablo Neira Ayuso [this message]
2014-03-27 21:54 ` [PATCH 3/3 nft] src: add table netlink messages " Pablo Neira Ayuso
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1395957291-5018-2-git-send-email-pablo@netfilter.org \
--to=pablo@netfilter.org \
--cc=kaber@trash.net \
--cc=netfilter-devel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).