expr: make map lookup expression as an argument in vmap statement
PATCH 1/1 expr: make map lookup expression as an argument in vmap statement
Support nested map lookups combined with vmap lookup as shown in the example below. This syntax enables flexibility to use the values of a map as keys for looking up vmap when users have two distinct maps for different purposes and do not want to alter any packet-related objects (e.g., packet mark, ct mark, ip fields) to store the value returned from the first map lookup for the final vmap lookup.
Command:
add rule ip table-a ip saddr map @map1 vmap @map2
Output:
chain table-a {
ip saddr map @map1 vmap @map2
}
It also supports multiple map lookups prior to vmap if users need to use multiple maps for the same query, such as
chain table-a {
ip saddr map @map1 map @map2 vmap @map3 }
--- src/evaluate.c | 2 +- src/netlink_delinearize.c | 12 +++++++----- src/parser_bison.y | 8 ++++++++ 3 files changed, 16 insertions(+), 6 deletions(-)
evaluate.c +1 -1
@@ -2052,7 +2052,7 @@ static int expr_evaluate_map(struct eval_ctx *ctx, struct expr **expr)expr_set_context (&ctx->ectx,NULL ,0 );if (expr_evaluate (ctx, &map->map) <0 )return -1 ; -if (expr_is_constant (map->map)) +if (map->map->etype !=EXPR_MAP &&expr_is_constant (map->map))return expr_error (ctx->msgs, map->map,"Map expression can not be constant" );
netlink_delinearize.c +7 -5
@@ -428,11 +428,13 @@ static void netlink_parse_lookup(struct netlink_parse_ctx *ctx,return netlink_error (ctx, loc,"Lookup expression has no left hand side" ); -if (left->len < set->key->len) { -expr_free (left); - left =netlink_parse_concat_expr (ctx, loc, sreg, set->key->len); -if (left ==NULL ) -return ; +if (left->etype !=EXPR_MAP ) { +if (left->len < set->key->len) { +expr_free (left); + left =netlink_parse_concat_expr (ctx, loc, sreg, set->key->len); +if (left ==NULL ) +return ; + } } right =set_ref_expr_alloc (loc, set);
parser_bison.y +8 -0
@@ -3254,6 +3254,10 @@ verdict_stmt : verdict_expr ; verdict_map_stmt : concat_expr VMAP verdict_map_expr + { + $$ = map_expr_alloc(&@$, $1, $3); + } + | map_expr VMAP verdict_map_expr { $$ = map_expr_alloc(&@$, $1, $3); }
@@ -4579,6 +4583,10 @@ multiton_rhs_expr : prefix_rhs_expr ; map_expr : concat_expr MAP rhs_expr + { + $$ = map_expr_alloc(&@$, $1, $3); + } + | map_expr MAP rhs_expr { $$ = map_expr_alloc(&@$, $1, $3); }
Signature · 1 line
2.44.0.windows.1
Florian Westphal
maintainerQuoted 7 lines · Son Dinh
Support nested map lookups combined with vmap lookup as shown in the example below. This syntax enables flexibility to use the values of a map as keys for looking up vmap when users have two distinct maps for different purposes and do not want to alter any packet-related objects (e.g., packet mark, ct mark, ip fields) to store the value returned from the first map lookup for the final vmap lookup.
This needs at least one test case.
Quoted 2 lines · Florian Westphal
This needs at least one test case.
Thanks for reviewing the patch. I'll try to write a new test case for it.
PATCH v2 1/1 expr: make map lookup expression as an argument in vmap statement
Support nested map lookups combined with vmap lookup as shown in the example below. This syntax enables flexibility to use the values of a map as keys for looking up vmap when users have two distinct maps for different purposes and do not want to alter any packet-related objects (e.g., packet mark, ct mark, ip fields) to store the value returned from the first map lookup for the final vmap lookup.
Command:
add rule ip table-a ip saddr map @map1 vmap @map2
Output:
chain table-a {
ip saddr map @map1 vmap @map2
}
It also supports multiple map lookups prior to vmap if users need to use multiple maps for the same query, such as
chain table-a {
ip saddr map @map1 map @map2 vmap @map3 }
--- src/evaluate.c | 2 +- src/netlink_delinearize.c | 12 +- src/parser_bison.y | 8 + src/parser_json.c | 10 +- tests/py/ip/sets.t | 5 + tests/py/ip/sets.t.json | 24 +++ tests/py/ip/sets.t.payload.inet | 9 + tests/py/ip/sets.t.payload.ip | 6 + tests/py/ip/sets.t.payload.netdev | 8 + tests/py/ip6/sets.t | 6 + tests/py/ip6/sets.t.json | 24 +++ tests/py/ip6/sets.t.payload.inet | 9 + tests/py/ip6/sets.t.payload.ip6 | 6 + tests/py/ip6/sets.t.payload.netdev | 8 + .../dumps/map_to_vmap_lookups.json-nft | 192 ++++++++++++++++++ .../packetpath/dumps/map_to_vmap_lookups.nft | 25 +++ .../testcases/packetpath/map_to_vmap_lookups | 35 ++++ 17 files changed, 381 insertions(+), 8 deletions(-) create mode 100644 tests/shell/testcases/packetpath/dumps/map_to_vmap_lookups.json-nft create mode 100644 tests/shell/testcases/packetpath/dumps/map_to_vmap_lookups.nft create mode 100755 tests/shell/testcases/packetpath/map_to_vmap_lookups
src/evaluate.c +1 -1
@@ -2052,7 +2052,7 @@ static int expr_evaluate_map(struct eval_ctx *ctx, struct expr **expr)expr_set_context (&ctx->ectx,NULL ,0 );if (expr_evaluate (ctx, &map->map) <0 )return -1 ; -if (expr_is_constant (map->map)) +if (map->map->etype !=EXPR_MAP &&expr_is_constant (map->map))return expr_error (ctx->msgs, map->map,"Map expression can not be constant" );
src/netlink_delinearize.c +7 -5
@@ -428,11 +428,13 @@ static void netlink_parse_lookup(struct netlink_parse_ctx *ctx,return netlink_error (ctx, loc,"Lookup expression has no left hand side" ); -if (left->len < set->key->len) { -expr_free (left); - left =netlink_parse_concat_expr (ctx, loc, sreg, set->key->len); -if (left ==NULL ) -return ; +if (left->etype !=EXPR_MAP ) { +if (left->len < set->key->len) { +expr_free (left); + left =netlink_parse_concat_expr (ctx, loc, sreg, set->key->len); +if (left ==NULL ) +return ; + } } right =set_ref_expr_alloc (loc, set);
src/parser_bison.y +8 -0
@@ -3254,6 +3254,10 @@ verdict_stmt : verdict_expr ; verdict_map_stmt : concat_expr VMAP verdict_map_expr + { + $$ = map_expr_alloc(&@$, $1, $3); + } + | map_expr VMAP verdict_map_expr { $$ = map_expr_alloc(&@$, $1, $3); }
@@ -4579,6 +4583,10 @@ multiton_rhs_expr : prefix_rhs_expr ; map_expr : concat_expr MAP rhs_expr + { + $$ = map_expr_alloc(&@$, $1, $3); + } + | map_expr MAP rhs_expr { $$ = map_expr_alloc(&@$, $1, $3); }
src/parser_json.c +8 -2
@@ -1417,13 +1417,19 @@ static struct expr *json_parse_map_expr(struct json_ctx *ctx, "key", &jkey, "data", &jdata))return NULL ; - key =json_parse_map_lhs_expr (ctx, jkey); +if (json_typeof (jkey) ==JSON_OBJECT ) + key =json_parse_expr (ctx, jkey); +else + key =json_parse_map_lhs_expr (ctx, jkey);if (!key) {json_error (ctx,"Illegal map expression key." );return NULL ; } - data =json_parse_rhs_expr (ctx, jdata); +if (json_typeof (jdata) ==JSON_STRING ) + data =json_parse_expr (ctx, jdata); +else + data =json_parse_rhs_expr (ctx, jdata);if (!data) {json_error (ctx,"Illegal map expression data." );expr_free (key);
tests/py/ip/sets.t +5 -0
@@ -66,3 +66,8 @@ ip saddr @set6 drop;ok ip saddr vmap { 1.1.1.1 : drop, * : accept };ok meta mark set ip saddr map { 1.1.1.1 : 0x00000001, * : 0x00000002 };ok +# test nested map lookups combined with vmap lookup +!map2 type ipv4_addr : ipv4_addr;ok +!map3 type ipv4_addr : inet_service;ok +!map4 type inet_service : verdict;ok +ip saddr map @map2 map @map3 vmap @map4;ok
tests/py/ip/sets.t.json +24 -0
@@ -303,3 +303,27 @@ } ] +# ip saddr map @map2 map @map3 vmap @map4 +[ + { + "vmap": { + "data": "@map4", + "key": { + "map": { + "data": "@map3", + "key": { + "map": { + "data": "@map2", + "key": { + "payload": { + "field": "saddr", + "protocol": "ip" + } + } + } + } + } + } + } + } +]
tests/py/ip/sets.t.payload.inet +9 -0
@@ -104,3 +104,12 @@ inet [ payload load 4b @ network header + 12 => reg 1 ] [ lookup reg 1 set __map%d dreg 1 ] [ meta set mark with reg 1 ] + +# ip saddr map @map2 map @map3 vmap @map4 +inet test-inet input + [ meta load nfproto => reg 1 ] + [ cmp eq reg 1 0x00000002 ] + [ payload load 4b @ network header + 12 => reg 1 ] + [ lookup reg 1 set map2 dreg 1 ] + [ lookup reg 1 set map3 dreg 1 ] + [ lookup reg 1 set map4 dreg 0 ]
tests/py/ip/sets.t.payload.ip +6 -0
@@ -81,3 +81,9 @@ ip test-ip4 input [ meta load mark => reg 10 ] [ dynset add reg_key 1 set map1 sreg_data 10 ] +# ip saddr map @map2 map @map3 vmap @map4 +ip test-ip4 input + [ payload load 4b @ network header + 12 => reg 1 ] + [ lookup reg 1 set map2 dreg 1 ] + [ lookup reg 1 set map3 dreg 1 ] + [ lookup reg 1 set map4 dreg 0 ]
tests/py/ip/sets.t.payload.netdev +8 -0
@@ -105,3 +105,11 @@ netdev test-netdev ingress [ meta load mark => reg 10 ] [ dynset add reg_key 1 set map1 sreg_data 10 ] +# ip saddr map @map2 map @map3 vmap @map4 +netdev test-netdev ingress + [ meta load protocol => reg 1 ] + [ cmp eq reg 1 0x00000008 ] + [ payload load 4b @ network header + 12 => reg 1 ] + [ lookup reg 1 set map2 dreg 1 ] + [ lookup reg 1 set map3 dreg 1 ] + [ lookup reg 1 set map4 dreg 0 ]
tests/py/ip6/sets.t +6 -0
@@ -46,3 +46,9 @@ add @set5 { ip6 saddr . ip6 daddr };ok add @map1 { ip6 saddr . ip6 daddr : meta mark };ok delete @set5 { ip6 saddr . ip6 daddr };ok + +# test nested map lookups combined with vmap lookup +!map2 type ipv6_addr : ipv6_addr;ok +!map3 type ipv6_addr : inet_service;ok +!map4 type inet_service : verdict;ok +ip6 saddr map @map2 map @map3 vmap @map4;ok
tests/py/ip6/sets.t.json +24 -0
@@ -148,3 +148,27 @@ } ] +# ip6 saddr map @map2 map @map3 vmap @map4 +[ + { + "vmap": { + "data": "@map4", + "key": { + "map": { + "data": "@map3", + "key": { + "map": { + "data": "@map2", + "key": { + "payload": { + "field": "saddr", + "protocol": "ip6" + } + } + } + } + } + } + } + } +]
tests/py/ip6/sets.t.payload.inet +9 -0
@@ -47,3 +47,12 @@ inet test-inet input [ payload load 16b @ network header + 8 => reg 1 ] [ payload load 16b @ network header + 24 => reg 2 ] [ dynset delete reg_key 1 set set5 ] + +# ip6 saddr map @map2 map @map3 vmap @map4 +inet test-inet input + [ meta load nfproto => reg 1 ] + [ cmp eq reg 1 0x0000000a ] + [ payload load 16b @ network header + 8 => reg 1 ] + [ lookup reg 1 set map2 dreg 1 ] + [ lookup reg 1 set map3 dreg 1 ] + [ lookup reg 1 set map4 dreg 0 ]
tests/py/ip6/sets.t.payload.ip6 +6 -0
@@ -36,3 +36,9 @@ ip6 test-ip6 input [ meta load mark => reg 3 ] [ dynset add reg_key 1 set map1 sreg_data 3 ] +# ip6 saddr map @map2 map @map3 vmap @map4 +ip6 test-ip6 input + [ payload load 16b @ network header + 8 => reg 1 ] + [ lookup reg 1 set map2 dreg 1 ] + [ lookup reg 1 set map3 dreg 1 ] + [ lookup reg 1 set map4 dreg 0 ]
tests/py/ip6/sets.t.payload.netdev +8 -0
@@ -48,3 +48,11 @@ netdev test-netdev ingress [ meta load mark => reg 3 ] [ dynset add reg_key 1 set map1 sreg_data 3 ] +# ip6 saddr map @map2 map @map3 vmap @map4 +netdev test-netdev ingress + [ meta load protocol => reg 1 ] + [ cmp eq reg 1 0x0000dd86 ] + [ payload load 16b @ network header + 8 => reg 1 ] + [ lookup reg 1 set map2 dreg 1 ] + [ lookup reg 1 set map3 dreg 1 ] + [ lookup reg 1 set map4 dreg 0 ]
tests/shell/testcases/packetpath/dumps/map_to_vmap_lookups.json-nft new file +192 -0
192 lines not on this page · raw message
tests/shell/testcases/packetpath/dumps/map_to_vmap_lookups.nft new file +25 -0
@@ -0,0 +1,25 @@ +table ip t { + map s1 { + type ipv4_addr : ipv4_addr + elements = { 127.0.0.1 : 10.0.0.1, 127.0.0.2 : 10.0.0.2 } + } + + map s2 { + type ipv4_addr : verdict + elements = { 10.0.0.1 : goto c1, 10.0.0.2 : goto c2 } + } + + chain c1 { + counter packets 1 bytes 84 + } + + chain c2 { + counter packets 2 bytes 168 + } + + chain c { + type filter hook input priority filter; policy accept; + icmp type echo-request ip daddr map @s1 vmap @s2 + icmp type echo-request counter packets 0 bytes 0 + } +}
tests/shell/testcases/packetpath/map_to_vmap_lookups new file +35 -0
@@ -0,0 +1,35 @@ +#!/bin/bash + +set -e + +$NFT -f /dev/stdin <<"EOF" +table ip t { + map s1 { + type ipv4_addr : ipv4_addr + elements = { 127.0.0.1 : 10.0.0.1, 127.0.0.2 : 10.0.0.2 } + } + + map s2 { + type ipv4_addr : verdict + elements = { 10.0.0.1 : goto c1, 10.0.0.2 : goto c2 } + } + + chain c1 { + counter + } + + chain c2 { + counter + } + + chain c { + type filter hook input priority filter; + icmp type echo-request ip daddr map @s1 vmap @s2 + icmp type echo-request counter + } +} +EOF + +ip link set lo up +ping -q -c 1 127.0.0.1 > /dev/null +ping -q -c 2 127.0.0.2 > /dev/null
Signature · 1 line
2.44.0.windows.1