Skip to contentSkip to conversation

datatype: fix print of raw numerical symbol values

PATCH Superseded Florian Westphal 2 participants 4 messages Jun 16, 2019 – Jun 17, 2019 netfilter

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH 1/1 datatype: fix print of raw numerical symbol values

The two rules:
arp operation 1-2 accept
arp operation 256-512 accept

are both shown as 256-512:

chain in_public {
arp operation 256-512 accept
arp operation 256-512 accept
meta mark "1"
tcp flags 2,4
}

This is because range expression enforces numeric output, yet nft_print doesn't respect byte order.

Behave as if we had no symbol in the first place and call the base type print function instead.

This means we now respect format specifier as well:

	chain in_public {
                arp operation 1-2 accept
                arp operation 256-512 accept
                meta mark "0x00000001"
                tcp flags 0x2,0x4
	}

Without fix, added test case will fail:
'add rule arp test-arp input arp operation 1-2': 'arp operation 1-2' mismatches 'arp operation 256-512'

---
Note there is a discrepancy between output when we have a symbol and when we do not.

Example, add rule:
meta mark "foo"

(with '1 "foo"' in rt_marks), nft will print quotes when symbol printing is inhibited via -n, but elides them in case the symbol is not available.

 src/datatype.c                    | 2 +-
 tests/py/arp/arp.t                | 1 +
 tests/py/arp/arp.t.payload        | 6 ++++++
 tests/py/arp/arp.t.payload.netdev | 8 ++++++++
 4 files changed, 16 insertions(+), 1 deletion(-)
src/datatype.c +1 -1
index 8ae3aa1c3f90..d193ccc0a659 100644
@@ -198,7 +198,7 @@ void symbolic_constant_print(const struct symbol_table *tbl,
 		nft_print(octx, "\"");
 
 	if (nft_output_numeric_symbol(octx))
-		nft_print(octx, "%" PRIu64 "", val);
+		expr_basetype(expr)->print(expr, octx);
 	else
 		nft_print(octx, "%s", s->identifier);
 
tests/py/arp/arp.t +1 -0
index 86bab5232eaf..2540c0a77419 100644
@@ -38,6 +38,7 @@ arp plen != {33-55};ok
 
 arp operation {nak, inreply, inrequest, rreply, rrequest, reply, request};ok
 arp operation != {nak, inreply, inrequest, rreply, rrequest, reply, request};ok
+arp operation 1-2;ok
 arp operation request;ok
 arp operation reply;ok
 arp operation rrequest;ok
tests/py/arp/arp.t.payload +6 -0
index d36bef183396..52c993294810 100644
@@ -188,6 +188,12 @@ arp test-arp input
   [ payload load 2b @ network header + 6 => reg 1 ]
   [ lookup reg 1 set __set%d 0x1 ]
 
+# arp operation 1-2
+arp test-arp input
+  [ payload load 2b @ network header + 6 => reg 1 ]
+  [ cmp gte reg 1 0x00000100 ]
+  [ cmp lte reg 1 0x00000200 ]
+
 # arp operation request
 arp test-arp input
   [ payload load 2b @ network header + 6 => reg 1 ]
tests/py/arp/arp.t.payload.netdev +8 -0
index 0146cf500ee2..667691fff2f6 100644
@@ -246,6 +246,14 @@ netdev test-netdev ingress
   [ payload load 2b @ network header + 6 => reg 1 ]
   [ lookup reg 1 set __set%d 0x1 ]
 
+# arp operation 1-2
+netdev test-netdev ingress
+  [ meta load protocol => reg 1 ]
+  [ cmp eq reg 1 0x00000608 ]
+  [ payload load 2b @ network header + 6 => reg 1 ]
+  [ cmp gte reg 1 0x00000100 ]
+  [ cmp lte reg 1 0x00000200 ]
+
 # arp operation request
 netdev test-netdev ingress 
   [ meta load protocol => reg 1 ]
Signature · 1 line
2.21.0
#1 lore raw
Quoted 16 lines · Florian Westphal

The two rules:
arp operation 1-2 accept
arp operation 256-512 accept

are both shown as 256-512:

chain in_public {
arp operation 256-512 accept
arp operation 256-512 accept
meta mark "1"
tcp flags 2,4
}

This is because range expression enforces numeric output, yet nft_print doesn't respect byte order.

Quoted 8 lines

Behave as if we had no symbol in the first place and call the base type print function instead.

This means we now respect format specifier as well:

	chain in_public {
                arp operation 1-2 accept
                arp operation 256-512 accept
                meta mark "0x00000001"

Hm, why is "1" turned into "0x00000001"?

I would expect quoted mark means: look at rt_marks.

Thanks!

Quoted 10 lines
                tcp flags 0x2,0x4
	}

Without fix, added test case will fail:
'add rule arp test-arp input arp operation 1-2': 'arp operation 1-2' mismatches 'arp operation 256-512'

---
Note there is a discrepancy between output when we have a symbol and when we do not.

Quoted 6 lines

Example, add rule:
meta mark "foo"

(with '1 "foo"' in rt_marks), nft will print quotes when symbol printing is inhibited via -n, but elides them in case the symbol is not available.

Then, we also need a patch to regard NFT_CTX_OUTPUT_NUMERIC_ALL, right?

Quoted 19 lines
 src/datatype.c                    | 2 +-
 tests/py/arp/arp.t                | 1 +
 tests/py/arp/arp.t.payload        | 6 ++++++
 tests/py/arp/arp.t.payload.netdev | 8 ++++++++
 4 files changed, 16 insertions(+), 1 deletion(-)
src/datatype.c +1 -1
index 8ae3aa1c3f90..d193ccc0a659 100644
@@ -198,7 +198,7 @@ void symbolic_constant_print(const struct symbol_table *tbl,
 		nft_print(octx, "\"");
 
 	if (nft_output_numeric_symbol(octx))
-		nft_print(octx, "%" PRIu64 "", val);
+		expr_basetype(expr)->print(expr, octx);
 	else
 		nft_print(octx, "%s", s->identifier);
 
author maintainer
#2 lore raw
Quoted 7 lines · Pablo Neira Ayuso
Quoted 5 lines

This means we now respect format specifier as well:

	chain in_public {
                arp operation 1-2 accept
                arp operation 256-512 accept
                meta mark "0x00000001"

Hm, why is "1" turned into "0x00000001"?

Because it will now respect basefmt, and that is:

const struct datatype mark_type = {
	...
        .basefmt        = "0x%.8Zx",
Quoted 11 lines
Quoted 9 lines

Note there is a discrepancy between output when we have a symbol and when we do not.

Example, add rule:
meta mark "foo"

(with '1 "foo"' in rt_marks), nft will print quotes when symbol printing is inhibited via -n, but elides them in case the symbol is not available.

Then, we also need a patch to regard NFT_CTX_OUTPUT_NUMERIC_ALL, right?

Not sure what you mean.

symbolic_constant_print()

does:

if (no_symbol_found)

	return print_raw();
 if (quotes)
	 nft_print(octx, "\"");
 if (nft_output_numeric_symbol(octx))
	 expr_basetype(expr)->print(expr, octx);
 else
	  nft_print(octx, "%s", s->identifier);

...

maybe either do:

 if (no_symbol_found) {
	 if (quotes)
		 ....
	print_raw();
    ...
    return;

}

(i.e., print quotes if no symbol found), or

if (nft_output_numeric_symbol(octx)) {
expr_basetype(expr)->print(expr, octx);
} else {
if (quotes) ..

	  nft_print(octx, "\"%s\"", s->identifier);
   else
	   nft_print(octx, "%s", s->identifier);
}

i.e., only print the "" if we found a symbol translation.

#3 lore raw
Quoted 7 lines · Florian Westphal
Quoted 6 lines · Pablo Neira Ayuso
Quoted 5 lines

This means we now respect format specifier as well:

	chain in_public {
                arp operation 1-2 accept
                arp operation 256-512 accept
                meta mark "0x00000001"
Quoted 7 lines
Quoted 1 line

Hm, why is "1" turned into "0x00000001"?

Because it will now respect basefmt, and that is:

const struct datatype mark_type = {
	...
        .basefmt        = "0x%.8Zx",

We don't want this, right? I mean, no quotes in that case.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Note there is a discrepancy between output when we have a symbol and when we do not.

Example, add rule:
meta mark "foo"

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

(with '1 "foo"' in rt_marks), nft will print quotes when symbol printing is inhibited via -n, but elides them in case the symbol is not available.

Then, we also need a patch to regard NFT_CTX_OUTPUT_NUMERIC_ALL, right?

Not sure what you mean.

I mean:

# nft list ruleset
table ip x {
        chain y {
                meta mark "test"
        }
}
# nft list ruleset -n
table ip x {
        chain y {
                meta mark "20"
        }
}

This output with -n should not print quotes, ie. no "20".

Quoted 35 lines

symbolic_constant_print()

does:

if (no_symbol_found)

	return print_raw();
 if (quotes)
	 nft_print(octx, "\"");
 if (nft_output_numeric_symbol(octx))
	 expr_basetype(expr)->print(expr, octx);
 else
	  nft_print(octx, "%s", s->identifier);

...

maybe either do:

 if (no_symbol_found) {
	 if (quotes)
		 ....
	print_raw();
    ...
    return;

}

(i.e., print quotes if no symbol found), or

if (nft_output_numeric_symbol(octx)) {
   expr_basetype(expr)->print(expr, octx);
} else {
   if (quotes) ..
	  nft_print(octx, "\"%s\"", s->identifier);
   else
	   nft_print(octx, "%s", s->identifier);
}
Quoted 1 line

i.e., only print the "" if we found a symbol translation.

Agreed :-).

BTW, this probably takes me back to the proposal not to strip off quotes from the scanner step. Hence, quoted strings will trigger a rt_mark lookup, otherwise we assume this is a 32-bit integer mark type. I'm refering to the parser side, so meta mark "0x20" means:
Search for 0x20 key in rt_marks.