From patchwork Thu Mar 27 21:54:51 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pablo Neira Ayuso X-Patchwork-Id: 334480 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 37B05140092 for ; Fri, 28 Mar 2014 08:55:05 +1100 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757396AbaC0VzB (ORCPT ); Thu, 27 Mar 2014 17:55:01 -0400 Received: from mail.us.es ([193.147.175.20]:49661 "EHLO mail.us.es" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757410AbaC0VzA (ORCPT ); Thu, 27 Mar 2014 17:55:00 -0400 Received: (qmail 26278 invoked from network); 27 Mar 2014 22:54:58 +0100 Received: from unknown (HELO us.es) (192.168.2.12) by us.es with SMTP; 27 Mar 2014 22:54:58 +0100 Received: (qmail 26610 invoked by uid 507); 27 Mar 2014 21:54:58 -0000 X-Qmail-Scanner-Diagnostics: from 127.0.0.1 by antivirus2 (envelope-from , uid 501) with qmail-scanner-2.10 (clamdscan: 0.98.1/18706. spamassassin: 3.3.2. Clear:RC:1(127.0.0.1):SA:0(-102.2/7.5):. Processed in 1.957312 secs); 27 Mar 2014 21:54:58 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on antivirus2 X-Spam-Level: X-Spam-Status: No, score=-102.2 required=7.5 tests=ALL_TRUSTED,BAYES_50, SMTPAUTH_US,USER_IN_WHITELIST autolearn=disabled version=3.3.2 X-Spam-ASN: X-Envelope-From: pablo@netfilter.org Received: from unknown (HELO antivirus2) (127.0.0.1) by us.es with SMTP; 27 Mar 2014 21:54:56 -0000 Received: from 192.168.1.13 (192.168.1.13) by antivirus2 (F-Secure/fsigk_smtp/412/antivirus2); Thu, 27 Mar 2014 22:54:56 +0100 (CET) X-Virus-Status: clean(F-Secure/fsigk_smtp/412/antivirus2) Received: (qmail 3847 invoked from network); 27 Mar 2014 22:54:56 +0100 Received: from unknown (HELO soleta.us.es) (pneira@us.es@10.100.44.199) by mail.us.es with SMTP; 27 Mar 2014 22:54:56 +0100 From: Pablo Neira Ayuso To: netfilter-devel@vger.kernel.org Cc: kaber@trash.net Subject: [PATCH 3/3 nft] src: add table netlink messages to the batch Date: Thu, 27 Mar 2014 22:54:51 +0100 Message-Id: <1395957291-5018-3-git-send-email-pablo@netfilter.org> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1395957291-5018-1-git-send-email-pablo@netfilter.org> References: <1395957291-5018-1-git-send-email-pablo@netfilter.org> Sender: netfilter-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netfilter-devel@vger.kernel.org This patch moves the table messages to the netlink batch that is sent to kernel-space. Signed-off-by: Pablo Neira Ayuso --- include/mnl.h | 6 ++++- src/mnl.c | 34 ++++++++++++++++++++++++++ src/netlink.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 110 insertions(+), 6 deletions(-) diff --git a/include/mnl.h b/include/mnl.h index 461f2c2..dab2b76 100644 --- a/include/mnl.h +++ b/include/mnl.h @@ -48,8 +48,12 @@ int mnl_nft_chain_get(struct mnl_socket *nf_sock, struct nft_chain *nlc, int mnl_nft_table_add(struct mnl_socket *nf_sock, struct nft_table *nlt, unsigned int flags); +int mnl_nft_table_batch_add(struct mnl_socket *nf_sock, struct nft_table *nlt, + unsigned int flags); int mnl_nft_table_delete(struct mnl_socket *nf_sock, struct nft_table *nlt, - unsigned int flags); + unsigned int flags); +int mnl_nft_table_batch_del(struct mnl_socket *nf_sock, struct nft_table *nlt, + unsigned int flags); struct nft_table_list *mnl_nft_table_dump(struct mnl_socket *nf_sock, int family); int mnl_nft_table_get(struct mnl_socket *nf_sock, struct nft_table *nlt, diff --git a/src/mnl.c b/src/mnl.c index d1a21f9..edfae82 100644 --- a/src/mnl.c +++ b/src/mnl.c @@ -542,6 +542,23 @@ int mnl_nft_table_add(struct mnl_socket *nf_sock, struct nft_table *nlt, return mnl_talk(nf_sock, nlh, nlh->nlmsg_len, NULL, NULL); } +int mnl_nft_table_batch_add(struct mnl_socket *nf_sock, struct nft_table *nlt, + unsigned int flags) +{ + struct nlmsghdr *nlh; + + nlh = nft_table_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch), + NFT_MSG_NEWTABLE, + nft_table_attr_get_u32(nlt, NFT_TABLE_ATTR_FAMILY), + NLM_F_ACK | flags, seq); + nft_table_nlmsg_build_payload(nlh, nlt); + + if (!mnl_nlmsg_batch_next(batch)) + mnl_batch_page_add(); + + return 0; +} + int mnl_nft_table_delete(struct mnl_socket *nf_sock, struct nft_table *nlt, unsigned int flags) { @@ -556,6 +573,23 @@ int mnl_nft_table_delete(struct mnl_socket *nf_sock, struct nft_table *nlt, return mnl_talk(nf_sock, nlh, nlh->nlmsg_len, NULL, NULL); } +int mnl_nft_table_batch_del(struct mnl_socket *nf_sock, struct nft_table *nlt, + unsigned int flags) +{ + struct nlmsghdr *nlh; + + nlh = nft_table_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch), + NFT_MSG_DELTABLE, + nft_table_attr_get_u32(nlt, NFT_TABLE_ATTR_FAMILY), + NLM_F_ACK, seq); + nft_table_nlmsg_build_payload(nlh, nlt); + + if (!mnl_nlmsg_batch_next(batch)) + mnl_batch_page_add(); + + return 0; +} + static int table_cb(const struct nlmsghdr *nlh, void *data) { struct nft_table_list *nlt_list = data; diff --git a/src/netlink.c b/src/netlink.c index a78b284..91cc62a 100644 --- a/src/netlink.c +++ b/src/netlink.c @@ -728,9 +728,10 @@ int netlink_flush_chain(struct netlink_ctx *ctx, const struct handle *h, return netlink_del_rule_batch(ctx, h, loc); } -int netlink_add_table(struct netlink_ctx *ctx, const struct handle *h, - const struct location *loc, const struct table *table, - bool excl) +static int netlink_add_table_compat(struct netlink_ctx *ctx, + const struct handle *h, + const struct location *loc, + const struct table *table, bool excl) { struct nft_table *nlt; int err; @@ -745,8 +746,42 @@ int netlink_add_table(struct netlink_ctx *ctx, const struct handle *h, return err; } -int netlink_delete_table(struct netlink_ctx *ctx, const struct handle *h, - const struct location *loc) +static int netlink_add_table_batch(struct netlink_ctx *ctx, + const struct handle *h, + const struct location *loc, + const struct table *table, bool excl) +{ + struct nft_table *nlt; + int err; + + nlt = alloc_nft_table(h); + err = mnl_nft_table_batch_add(nf_sock, nlt, excl ? NLM_F_EXCL : 0); + nft_table_free(nlt); + + if (err < 0) { + netlink_io_error(ctx, loc, "Could not add table: %s", + strerror(errno)); + } + return err; +} + +int netlink_add_table(struct netlink_ctx *ctx, const struct handle *h, + const struct location *loc, + const struct table *table, bool excl) +{ + int ret; + + if (ctx->batch_supported) + ret = netlink_add_table_batch(ctx, h, loc, table, excl); + else + ret = netlink_add_table_compat(ctx, h, loc, table, excl); + + return ret; +} + +static int netlink_del_table_compat(struct netlink_ctx *ctx, + const struct handle *h, + const struct location *loc) { struct nft_table *nlt; int err; @@ -761,6 +796,37 @@ int netlink_delete_table(struct netlink_ctx *ctx, const struct handle *h, return err; } +static int netlink_del_table_batch(struct netlink_ctx *ctx, + const struct handle *h, + const struct location *loc) +{ + struct nft_table *nlt; + int err; + + nlt = alloc_nft_table(h); + err = mnl_nft_table_batch_del(nf_sock, nlt, 0); + nft_table_free(nlt); + + if (err < 0) { + netlink_io_error(ctx, loc, "Could not delete table: %s", + strerror(errno)); + } + return err; +} + +int netlink_delete_table(struct netlink_ctx *ctx, const struct handle *h, + const struct location *loc) +{ + int ret; + + if (ctx->batch_supported) + ret = netlink_del_table_batch(ctx, h, loc); + else + ret = netlink_del_table_compat(ctx, h, loc); + + return ret; +} + void netlink_dump_table(struct nft_table *nlt) { #ifdef DEBUG