netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [libnftnl PATCH 1/2] set_elem: Use nftnl_data_reg_snprintf()
@ 2020-12-14 18:02 Phil Sutter
  2020-12-14 18:02 ` [libnftnl PATCH 2/2] set_elem: Include key_end data reg in print output Phil Sutter
  2020-12-14 18:30 ` [libnftnl PATCH 1/2] set_elem: Use nftnl_data_reg_snprintf() Pablo Neira Ayuso
  0 siblings, 2 replies; 4+ messages in thread
From: Phil Sutter @ 2020-12-14 18:02 UTC (permalink / raw)
  To: Pablo Neira Ayuso; +Cc: netfilter-devel

Introduce a flag to allow toggling the '0x' prefix when printing data
values, then use the existing routines to print data registers from
set_elem code.

Signed-off-by: Phil Sutter <phil@nwl.cc>
---
 include/data_reg.h  |  4 ++++
 src/expr/data_reg.c |  6 +++++-
 src/set_elem.c      | 16 ++++++++--------
 3 files changed, 17 insertions(+), 9 deletions(-)

diff --git a/include/data_reg.h b/include/data_reg.h
index d9578aa479901..0d6b77829cf89 100644
--- a/include/data_reg.h
+++ b/include/data_reg.h
@@ -13,6 +13,10 @@ enum {
 	DATA_CHAIN,
 };
 
+enum {
+	DATA_F_NOPFX = 1 << 0,
+};
+
 union nftnl_data_reg {
 	struct {
 		uint32_t	val[NFT_DATA_VALUE_MAXLEN / sizeof(uint32_t)];
diff --git a/src/expr/data_reg.c b/src/expr/data_reg.c
index 4e35a799e9591..d3ccc612ce812 100644
--- a/src/expr/data_reg.c
+++ b/src/expr/data_reg.c
@@ -29,10 +29,14 @@ nftnl_data_reg_value_snprintf_default(char *buf, size_t size,
 				      const union nftnl_data_reg *reg,
 				      uint32_t flags)
 {
+	const char *pfx = flags & DATA_F_NOPFX ? "" : "0x";
 	int remain = size, offset = 0, ret, i;
 
+
+
 	for (i = 0; i < div_round_up(reg->len, sizeof(uint32_t)); i++) {
-		ret = snprintf(buf + offset, remain, "0x%.8x ", reg->val[i]);
+		ret = snprintf(buf + offset, remain,
+			       "%s%.8x ", pfx, reg->val[i]);
 		SNPRINTF_BUFFER_SIZE(ret, remain, offset);
 	}
 
diff --git a/src/set_elem.c b/src/set_elem.c
index e82684bc1c53e..51bf2c7b853bb 100644
--- a/src/set_elem.c
+++ b/src/set_elem.c
@@ -629,18 +629,18 @@ static int nftnl_set_elem_snprintf_default(char *buf, size_t size,
 	ret = snprintf(buf, remain, "element ");
 	SNPRINTF_BUFFER_SIZE(ret, remain, offset);
 
-	for (i = 0; i < div_round_up(e->key.len, sizeof(uint32_t)); i++) {
-		ret = snprintf(buf + offset, remain, "%.8x ", e->key.val[i]);
-		SNPRINTF_BUFFER_SIZE(ret, remain, offset);
-	}
+	ret = nftnl_data_reg_snprintf(buf + offset, remain, &e->key,
+				      NFTNL_OUTPUT_DEFAULT,
+				      DATA_F_NOPFX, DATA_VALUE);
+	SNPRINTF_BUFFER_SIZE(ret, remain, offset);
 
 	ret = snprintf(buf + offset, remain, " : ");
 	SNPRINTF_BUFFER_SIZE(ret, remain, offset);
 
-	for (i = 0; i < div_round_up(e->data.len, sizeof(uint32_t)); i++) {
-		ret = snprintf(buf + offset, remain, "%.8x ", e->data.val[i]);
-		SNPRINTF_BUFFER_SIZE(ret, remain, offset);
-	}
+	ret = nftnl_data_reg_snprintf(buf + offset, remain, &e->data,
+				      NFTNL_OUTPUT_DEFAULT,
+				      DATA_F_NOPFX, DATA_VALUE);
+	SNPRINTF_BUFFER_SIZE(ret, remain, offset);
 
 	ret = snprintf(buf + offset, remain, "%u [end]", e->set_elem_flags);
 	SNPRINTF_BUFFER_SIZE(ret, remain, offset);
-- 
2.28.0


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

* [libnftnl PATCH 2/2] set_elem: Include key_end data reg in print output
  2020-12-14 18:02 [libnftnl PATCH 1/2] set_elem: Use nftnl_data_reg_snprintf() Phil Sutter
@ 2020-12-14 18:02 ` Phil Sutter
  2020-12-14 18:30 ` [libnftnl PATCH 1/2] set_elem: Use nftnl_data_reg_snprintf() Pablo Neira Ayuso
  1 sibling, 0 replies; 4+ messages in thread
From: Phil Sutter @ 2020-12-14 18:02 UTC (permalink / raw)
  To: Pablo Neira Ayuso; +Cc: netfilter-devel

Elements of concatenated range sets hold the upper boundary in an extra
data_reg, print it using dash as a somewhat intuitive separator.

Fixes: 04cc28d8d6923 ("set_elem: Introduce support for NFTNL_SET_ELEM_KEY_END")
Signed-off-by: Phil Sutter <phil@nwl.cc>
---
 src/set_elem.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/src/set_elem.c b/src/set_elem.c
index 51bf2c7b853bb..46bb0623a3bb3 100644
--- a/src/set_elem.c
+++ b/src/set_elem.c
@@ -634,6 +634,16 @@ static int nftnl_set_elem_snprintf_default(char *buf, size_t size,
 				      DATA_F_NOPFX, DATA_VALUE);
 	SNPRINTF_BUFFER_SIZE(ret, remain, offset);
 
+	if (e->flags & (1 << NFTNL_SET_ELEM_KEY_END)) {
+		ret = snprintf(buf + offset, remain, " - ");
+		SNPRINTF_BUFFER_SIZE(ret, remain, offset);
+
+		ret = nftnl_data_reg_snprintf(buf + offset, remain, &e->key_end,
+					      NFTNL_OUTPUT_DEFAULT,
+					      DATA_F_NOPFX, DATA_VALUE);
+		SNPRINTF_BUFFER_SIZE(ret, remain, offset);
+	}
+
 	ret = snprintf(buf + offset, remain, " : ");
 	SNPRINTF_BUFFER_SIZE(ret, remain, offset);
 
-- 
2.28.0


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

* Re: [libnftnl PATCH 1/2] set_elem: Use nftnl_data_reg_snprintf()
  2020-12-14 18:02 [libnftnl PATCH 1/2] set_elem: Use nftnl_data_reg_snprintf() Phil Sutter
  2020-12-14 18:02 ` [libnftnl PATCH 2/2] set_elem: Include key_end data reg in print output Phil Sutter
@ 2020-12-14 18:30 ` Pablo Neira Ayuso
  2020-12-15 10:13   ` Phil Sutter
  1 sibling, 1 reply; 4+ messages in thread
From: Pablo Neira Ayuso @ 2020-12-14 18:30 UTC (permalink / raw)
  To: Phil Sutter; +Cc: netfilter-devel

On Mon, Dec 14, 2020 at 07:02:50PM +0100, Phil Sutter wrote:
> Introduce a flag to allow toggling the '0x' prefix when printing data
> values, then use the existing routines to print data registers from
> set_elem code.

Patches LGTM.

You will have to update tests/py too, right?

Thanks.

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

* Re: [libnftnl PATCH 1/2] set_elem: Use nftnl_data_reg_snprintf()
  2020-12-14 18:30 ` [libnftnl PATCH 1/2] set_elem: Use nftnl_data_reg_snprintf() Pablo Neira Ayuso
@ 2020-12-15 10:13   ` Phil Sutter
  0 siblings, 0 replies; 4+ messages in thread
From: Phil Sutter @ 2020-12-15 10:13 UTC (permalink / raw)
  To: Pablo Neira Ayuso; +Cc: netfilter-devel

Hi Pablo,

On Mon, Dec 14, 2020 at 07:30:23PM +0100, Pablo Neira Ayuso wrote:
> On Mon, Dec 14, 2020 at 07:02:50PM +0100, Phil Sutter wrote:
> > Introduce a flag to allow toggling the '0x' prefix when printing data
> > values, then use the existing routines to print data registers from
> > set_elem code.
> 
> Patches LGTM.

Thanks, I'll push it along with the change in nftables' tests/py.

> You will have to update tests/py too, right?

To my surprise, it's merely a single test case that needed adjustment.
Either I missed something (testing stops before payload comparison if
e.g. input and output differ) or we really have quite low concat-range
coverage.

Cheers, Phil

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

end of thread, other threads:[~2020-12-15 10:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-12-14 18:02 [libnftnl PATCH 1/2] set_elem: Use nftnl_data_reg_snprintf() Phil Sutter
2020-12-14 18:02 ` [libnftnl PATCH 2/2] set_elem: Include key_end data reg in print output Phil Sutter
2020-12-14 18:30 ` [libnftnl PATCH 1/2] set_elem: Use nftnl_data_reg_snprintf() Pablo Neira Ayuso
2020-12-15 10:13   ` Phil Sutter

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).