Blame SOURCES/0072-src-support-for-restoring-element-counters.patch

252916
From 0db42cc2d2647ec61441e29445c9f6e0f8946613 Mon Sep 17 00:00:00 2001
252916
From: Phil Sutter <psutter@redhat.com>
252916
Date: Thu, 13 Jan 2022 20:37:56 +0100
252916
Subject: [PATCH] src: support for restoring element counters
252916
252916
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2039594
252916
Upstream Status: nftables commit 1fe6089ddd87e
252916
252916
commit 1fe6089ddd87ee7869d24c0f8849951220cc9b85
252916
Author: Pablo Neira Ayuso <pablo@netfilter.org>
252916
Date:   Wed Mar 11 13:00:01 2020 +0100
252916
252916
    src: support for restoring element counters
252916
252916
    This patch allows you to restore counters in dynamic sets:
252916
252916
     table ip test {
252916
            set test {
252916
                    type ipv4_addr
252916
                    size 65535
252916
                    flags dynamic,timeout
252916
                    timeout 30d
252916
                    gc-interval 1d
252916
                    elements = { 192.168.10.13 expires 19d23h52m27s576ms counter packets 51 bytes 17265 }
252916
            }
252916
            chain output {
252916
                    type filter hook output priority 0;
252916
                    update @test { ip saddr }
252916
            }
252916
     }
252916
252916
    You can also add counters to elements from the control place, ie.
252916
252916
     table ip test {
252916
            set test {
252916
                    type ipv4_addr
252916
                    size 65535
252916
                    elements = { 192.168.2.1 counter packets 75 bytes 19043 }
252916
            }
252916
252916
            chain output {
252916
                    type filter hook output priority filter; policy accept;
252916
                    ip daddr @test
252916
            }
252916
     }
252916
252916
    Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
252916
---
252916
 include/netlink.h       |  1 +
252916
 src/netlink.c           |  3 +++
252916
 src/netlink_linearize.c |  2 +-
252916
 src/parser_bison.y      | 36 +++++++++++++++++++++++++++++++++++-
252916
 4 files changed, 40 insertions(+), 2 deletions(-)
252916
252916
diff --git a/include/netlink.h b/include/netlink.h
252916
index 88d12ba..059092e 100644
252916
--- a/include/netlink.h
252916
+++ b/include/netlink.h
252916
@@ -97,6 +97,7 @@ extern void netlink_gen_data(const struct expr *expr,
252916
 extern void netlink_gen_raw_data(const mpz_t value, enum byteorder byteorder,
252916
 				 unsigned int len,
252916
 				 struct nft_data_linearize *data);
252916
+extern struct nftnl_expr *netlink_gen_stmt_stateful(const struct stmt *stmt);
252916
 
252916
 extern struct expr *netlink_alloc_value(const struct location *loc,
252916
 				        const struct nft_data_delinearize *nld);
252916
diff --git a/src/netlink.c b/src/netlink.c
252916
index 64e51e5..825c2cc 100644
252916
--- a/src/netlink.c
252916
+++ b/src/netlink.c
252916
@@ -136,6 +136,9 @@ static struct nftnl_set_elem *alloc_nftnl_setelem(const struct expr *set,
252916
 	if (elem->expiration)
252916
 		nftnl_set_elem_set_u64(nlse, NFTNL_SET_ELEM_EXPIRATION,
252916
 				       elem->expiration);
252916
+	if (elem->stmt)
252916
+		nftnl_set_elem_set(nlse, NFTNL_SET_ELEM_EXPR,
252916
+				   netlink_gen_stmt_stateful(elem->stmt), 0);
252916
 	if (elem->comment || expr->elem_flags) {
252916
 		udbuf = nftnl_udata_buf_alloc(NFT_USERDATA_MAXLEN);
252916
 		if (!udbuf)
252916
diff --git a/src/netlink_linearize.c b/src/netlink_linearize.c
252916
index f5c6116..3fa1339 100644
252916
--- a/src/netlink_linearize.c
252916
+++ b/src/netlink_linearize.c
252916
@@ -838,7 +838,7 @@ static struct nftnl_expr *netlink_gen_quota_stmt(const struct stmt *stmt)
252916
 	return nle;
252916
 }
252916
 
252916
-static struct nftnl_expr *netlink_gen_stmt_stateful(const struct stmt *stmt)
252916
+struct nftnl_expr *netlink_gen_stmt_stateful(const struct stmt *stmt)
252916
 {
252916
 	switch (stmt->ops->type) {
252916
 	case STMT_CONNLIMIT:
252916
diff --git a/src/parser_bison.y b/src/parser_bison.y
252916
index d38ec30..2cdf8ec 100644
252916
--- a/src/parser_bison.y
252916
+++ b/src/parser_bison.y
252916
@@ -3654,7 +3654,7 @@ meter_key_expr_alloc	:	concat_expr
252916
 			;
252916
 
252916
 set_elem_expr		:	set_elem_expr_alloc
252916
-			|	set_elem_expr_alloc		set_elem_options
252916
+			|	set_elem_expr_alloc		set_elem_expr_options
252916
 			;
252916
 
252916
 set_elem_expr_alloc	:	set_lhs_expr
252916
@@ -3684,6 +3684,40 @@ set_elem_option		:	TIMEOUT			time_spec
252916
 			}
252916
 			;
252916
 
252916
+set_elem_expr_options	:	set_elem_expr_option
252916
+			{
252916
+				$<expr>$	= $<expr>0;
252916
+			}
252916
+			|	set_elem_expr_options	set_elem_expr_option
252916
+			;
252916
+
252916
+set_elem_expr_option	:	TIMEOUT			time_spec
252916
+			{
252916
+				$<expr>0->timeout = $2;
252916
+			}
252916
+			|	EXPIRES		time_spec
252916
+			{
252916
+				$<expr>0->expiration = $2;
252916
+			}
252916
+			|	COUNTER
252916
+			{
252916
+				$<expr>0->stmt = counter_stmt_alloc(&@$);
252916
+			}
252916
+			|	COUNTER	PACKETS	NUM	BYTES	NUM
252916
+			{
252916
+				struct stmt *stmt;
252916
+
252916
+				stmt = counter_stmt_alloc(&@$);
252916
+				stmt->counter.packets = $3;
252916
+				stmt->counter.bytes = $5;
252916
+				$<expr>0->stmt = stmt;
252916
+			}
252916
+			|	comment_spec
252916
+			{
252916
+				$<expr>0->comment = $1;
252916
+			}
252916
+			;
252916
+
252916
 set_lhs_expr		:	concat_rhs_expr
252916
 			|	wildcard_expr
252916
 			;
252916
-- 
252916
2.31.1
252916