netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* iproute2 bug in json output for encap
@ 2023-04-14  8:29 Lars Ekman
  2023-04-14 15:21 ` Stephen Hemminger
  0 siblings, 1 reply; 6+ messages in thread
From: Lars Ekman @ 2023-04-14  8:29 UTC (permalink / raw)
  To: netdev

The destination is lost in json printout and replaced by the encap 
destination. The destination can even be ipv6 for an ipv4 route.

Example:

vm-002 ~ # ip route add 10.0.0.0/24 proto 5 dev ip6tnl6 encap ip6 dst 
fd00::192.168.2.221
vm-002 ~ # ip route show proto 5
10.0.0.0/24  encap ip6 id 0 src :: dst fd00::c0a8:2dd hoplimit 0 tc 0 
dev ip6tnl6 scope link
vm-002 ~ # ip -j route show proto 5 | jq
[
   {
     "dst": "fd00::c0a8:2dd",
     "encap": "ip6",
     "id": 0,
     "src": "::",
     "hoplimit": 0,
     "tc": 0,
     "dev": "ip6tnl6",
     "scope": "link",
     "flags": []
   }
]


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

* Re: iproute2 bug in json output for encap
  2023-04-14  8:29 iproute2 bug in json output for encap Lars Ekman
@ 2023-04-14 15:21 ` Stephen Hemminger
  2023-04-14 16:12   ` Lars Ekman
  0 siblings, 1 reply; 6+ messages in thread
From: Stephen Hemminger @ 2023-04-14 15:21 UTC (permalink / raw)
  To: Lars Ekman; +Cc: netdev

On Fri, 14 Apr 2023 10:29:15 +0200
Lars Ekman <uablrek@gmail.com> wrote:

> The destination is lost in json printout and replaced by the encap 
> destination. The destination can even be ipv6 for an ipv4 route.
> 
> Example:
> 
> vm-002 ~ # ip route add 10.0.0.0/24 proto 5 dev ip6tnl6 encap ip6 dst 
> fd00::192.168.2.221
> vm-002 ~ # ip route show proto 5
> 10.0.0.0/24  encap ip6 id 0 src :: dst fd00::c0a8:2dd hoplimit 0 tc 0 
> dev ip6tnl6 scope link
> vm-002 ~ # ip -j route show proto 5 | jq
> [
>    {
>      "dst": "fd00::c0a8:2dd",
>      "encap": "ip6",
>      "id": 0,
>      "src": "::",
>      "hoplimit": 0,
>      "tc": 0,
>      "dev": "ip6tnl6",
>      "scope": "link",
>      "flags": []
>    }
> ]
> 

Both JSON and regular output show the same address which is coming from
the kernel.  I.e not a JSON problem. Also, you don't need to use jq
ip has -p flag to pretty print.

I can not reproduce this with current kernel and iproute2.
# ip route add 192.168.11.0/24 proto 5 dev dummy0 encap ip6 dst fd00::192.168.2.221

# ip route show proto 5
192.168.11.0/24  encap ip6 id 0 src :: dst fd00::c0a8:2dd hoplimit 0 tc 0 dev dummy0 scope link 

# ip -j -p route show proto 5
[ {
        "dst": "192.168.11.0/24",
        "encap": "ip6",
        "id": 0,
        "src": "::",
        "dst": "fd00::c0a8:2dd",
        "hoplimit": 0,
        "tc": 0,
        "dev": "dummy0",
        "scope": "link",
        "flags": [ ]
    } ]


# ip -V
ip utility, iproute2-6.1.0, libbpf 1.1.0
# uname -r
6.1.0-7-amd64



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

* Re: iproute2 bug in json output for encap
  2023-04-14 15:21 ` Stephen Hemminger
@ 2023-04-14 16:12   ` Lars Ekman
  2023-04-14 16:26     ` Lars Ekman
  0 siblings, 1 reply; 6+ messages in thread
From: Lars Ekman @ 2023-04-14 16:12 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: netdev

Hi,

Thanks for the reply. You are right, it is jq that eats one ot the
double "dst" items.

vm-002 ~ # ip -j -p route show proto 5
[ {
        "dst": "192.168.11.0/24",
        "encap": "ip6",
        "id": 0,
        "src": "::",
        "dst": "fd00::c0a8:2dd",
        "hoplimit": 0,
        "tc": 0,
        "dev": "dummy0",
        "scope": "link",
        "flags": [ ]
    } ]
vm-002 ~ # ip -j -p route show proto 5 | jq
[
  {
    "dst": "fd00::c0a8:2dd",
    "encap": "ip6",
    "id": 0,
    "src": "::",
    "hoplimit": 0,
    "tc": 0,
    "dev": "dummy0",
    "scope": "link",
    "flags": []
  }
]

Sorry for the fuss.

Best Regards,

Lars Ekman


And btw I did upgrade *before* posting :-)

vm-002 ~ # ip -V
ip utility, iproute2-6.2.0, libbpf 1.1.0
vm-002 ~ # uname -r
6.2.7



On 2023-04-14 17:21, Stephen Hemminger wrote:
> On Fri, 14 Apr 2023 10:29:15 +0200
> Lars Ekman <uablrek@gmail.com> wrote:
>
>> The destination is lost in json printout and replaced by the encap 
>> destination. The destination can even be ipv6 for an ipv4 route.
>>
>> Example:
>>
>> vm-002 ~ # ip route add 10.0.0.0/24 proto 5 dev ip6tnl6 encap ip6 dst 
>> fd00::192.168.2.221
>> vm-002 ~ # ip route show proto 5
>> 10.0.0.0/24  encap ip6 id 0 src :: dst fd00::c0a8:2dd hoplimit 0 tc 0 
>> dev ip6tnl6 scope link
>> vm-002 ~ # ip -j route show proto 5 | jq
>> [
>>    {
>>      "dst": "fd00::c0a8:2dd",
>>      "encap": "ip6",
>>      "id": 0,
>>      "src": "::",
>>      "hoplimit": 0,
>>      "tc": 0,
>>      "dev": "ip6tnl6",
>>      "scope": "link",
>>      "flags": []
>>    }
>> ]
>>
> Both JSON and regular output show the same address which is coming from
> the kernel.  I.e not a JSON problem. Also, you don't need to use jq
> ip has -p flag to pretty print.
>
> I can not reproduce this with current kernel and iproute2.
> # ip route add 192.168.11.0/24 proto 5 dev dummy0 encap ip6 dst fd00::192.168.2.221
>
> # ip route show proto 5
> 192.168.11.0/24  encap ip6 id 0 src :: dst fd00::c0a8:2dd hoplimit 0 tc 0 dev dummy0 scope link 
>
> # ip -j -p route show proto 5
> [ {
>         "dst": "192.168.11.0/24",
>         "encap": "ip6",
>         "id": 0,
>         "src": "::",
>         "dst": "fd00::c0a8:2dd",
>         "hoplimit": 0,
>         "tc": 0,
>         "dev": "dummy0",
>         "scope": "link",
>         "flags": [ ]
>     } ]
>
>
> # ip -V
> ip utility, iproute2-6.1.0, libbpf 1.1.0
> # uname -r
> 6.1.0-7-amd64
>
>

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

* Re: iproute2 bug in json output for encap
  2023-04-14 16:12   ` Lars Ekman
@ 2023-04-14 16:26     ` Lars Ekman
  2023-04-14 16:44       ` Stephen Hemminger
  2023-04-14 17:01       ` Stephen Hemminger
  0 siblings, 2 replies; 6+ messages in thread
From: Lars Ekman @ 2023-04-14 16:26 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: netdev

Hi again,

Digging a little deeper I see that the double "dst" items will cause
problems with most (all?) json parsers. I intend to use "go" and json
parsing will be parsed to a "map" (hash-table) so duplicate keys will
not work.

https://stackoverflow.com/questions/21832701/does-json-syntax-allow-duplicate-keys-in-an-object

IMHO it would be better to use a structured "encap" item. Something like;

[ {
    "dst": "192.168.11.0/24",
    "encap": {
        "protocol": "ip6",
        "id": 0,
        "src": "::",
        "dst": "fd00::c0a8:2dd",
        "hoplimit": 0,
        "tc": 0
    },
    "scope": "link",
    "flags": [ ]
} ]

Best Regards,

Lars Ekman



On 2023-04-14 18:12, Lars Ekman wrote:
> Hi,
>
> Thanks for the reply. You are right, it is jq that eats one ot the
> double "dst" items.
>
> vm-002 ~ # ip -j -p route show proto 5
> [ {
>         "dst": "192.168.11.0/24",
>         "encap": "ip6",
>         "id": 0,
>         "src": "::",
>         "dst": "fd00::c0a8:2dd",
>         "hoplimit": 0,
>         "tc": 0,
>         "dev": "dummy0",
>         "scope": "link",
>         "flags": [ ]
>     } ]
> vm-002 ~ # ip -j -p route show proto 5 | jq
> [
>   {
>     "dst": "fd00::c0a8:2dd",
>     "encap": "ip6",
>     "id": 0,
>     "src": "::",
>     "hoplimit": 0,
>     "tc": 0,
>     "dev": "dummy0",
>     "scope": "link",
>     "flags": []
>   }
> ]
>
> Sorry for the fuss.
>
> Best Regards,
>
> Lars Ekman
>
>
> And btw I did upgrade *before* posting :-)
>
> vm-002 ~ # ip -V
> ip utility, iproute2-6.2.0, libbpf 1.1.0
> vm-002 ~ # uname -r
> 6.2.7
>
>
>
> On 2023-04-14 17:21, Stephen Hemminger wrote:
>> On Fri, 14 Apr 2023 10:29:15 +0200
>> Lars Ekman <uablrek@gmail.com> wrote:
>>
>>> The destination is lost in json printout and replaced by the encap 
>>> destination. The destination can even be ipv6 for an ipv4 route.
>>>
>>> Example:
>>>
>>> vm-002 ~ # ip route add 10.0.0.0/24 proto 5 dev ip6tnl6 encap ip6 dst 
>>> fd00::192.168.2.221
>>> vm-002 ~ # ip route show proto 5
>>> 10.0.0.0/24  encap ip6 id 0 src :: dst fd00::c0a8:2dd hoplimit 0 tc 0 
>>> dev ip6tnl6 scope link
>>> vm-002 ~ # ip -j route show proto 5 | jq
>>> [
>>>    {
>>>      "dst": "fd00::c0a8:2dd",
>>>      "encap": "ip6",
>>>      "id": 0,
>>>      "src": "::",
>>>      "hoplimit": 0,
>>>      "tc": 0,
>>>      "dev": "ip6tnl6",
>>>      "scope": "link",
>>>      "flags": []
>>>    }
>>> ]
>>>
>> Both JSON and regular output show the same address which is coming from
>> the kernel.  I.e not a JSON problem. Also, you don't need to use jq
>> ip has -p flag to pretty print.
>>
>> I can not reproduce this with current kernel and iproute2.
>> # ip route add 192.168.11.0/24 proto 5 dev dummy0 encap ip6 dst fd00::192.168.2.221
>>
>> # ip route show proto 5
>> 192.168.11.0/24  encap ip6 id 0 src :: dst fd00::c0a8:2dd hoplimit 0 tc 0 dev dummy0 scope link 
>>
>> # ip -j -p route show proto 5
>> [ {
>>         "dst": "192.168.11.0/24",
>>         "encap": "ip6",
>>         "id": 0,
>>         "src": "::",
>>         "dst": "fd00::c0a8:2dd",
>>         "hoplimit": 0,
>>         "tc": 0,
>>         "dev": "dummy0",
>>         "scope": "link",
>>         "flags": [ ]
>>     } ]
>>
>>
>> # ip -V
>> ip utility, iproute2-6.1.0, libbpf 1.1.0
>> # uname -r
>> 6.1.0-7-amd64
>>
>>

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

* Re: iproute2 bug in json output for encap
  2023-04-14 16:26     ` Lars Ekman
@ 2023-04-14 16:44       ` Stephen Hemminger
  2023-04-14 17:01       ` Stephen Hemminger
  1 sibling, 0 replies; 6+ messages in thread
From: Stephen Hemminger @ 2023-04-14 16:44 UTC (permalink / raw)
  To: Lars Ekman; +Cc: netdev

On Fri, 14 Apr 2023 18:26:03 +0200
Lars Ekman <uablrek@gmail.com> wrote:

> Hi again,
> 
> Digging a little deeper I see that the double "dst" items will cause
> problems with most (all?) json parsers. I intend to use "go" and json
> parsing will be parsed to a "map" (hash-table) so duplicate keys will
> not work.
> 
> https://stackoverflow.com/questions/21832701/does-json-syntax-allow-duplicate-keys-in-an-object
> 
> IMHO it would be better to use a structured "encap" item. Something like;
> 
> [ {
>     "dst": "192.168.11.0/24",
>     "encap": {
>         "protocol": "ip6",
>         "id": 0,
>         "src": "::",
>         "dst": "fd00::c0a8:2dd",
>         "hoplimit": 0,
>         "tc": 0
>     },
>     "scope": "link",
>     "flags": [ ]
> } ]
> 
> Best Regards,
> 
> Lars Ekman

That makes sense, not sure if it would break any existing scripts.
Probably true for ip link as well.


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

* Re: iproute2 bug in json output for encap
  2023-04-14 16:26     ` Lars Ekman
  2023-04-14 16:44       ` Stephen Hemminger
@ 2023-04-14 17:01       ` Stephen Hemminger
  1 sibling, 0 replies; 6+ messages in thread
From: Stephen Hemminger @ 2023-04-14 17:01 UTC (permalink / raw)
  To: Lars Ekman; +Cc: netdev

On Fri, 14 Apr 2023 18:26:03 +0200
Lars Ekman <uablrek@gmail.com> wrote:

> Hi again,
> 
> Digging a little deeper I see that the double "dst" items will cause
> problems with most (all?) json parsers. I intend to use "go" and json
> parsing will be parsed to a "map" (hash-table) so duplicate keys will
> not work.
> 
> https://stackoverflow.com/questions/21832701/does-json-syntax-allow-duplicate-keys-in-an-object
> 
> IMHO it would be better to use a structured "encap" item. Something like;
> 
> [ {
>     "dst": "192.168.11.0/24",
>     "encap": {
>         "protocol": "ip6",
>         "id": 0,
>         "src": "::",
>         "dst": "fd00::c0a8:2dd",
>         "hoplimit": 0,
>         "tc": 0
>     },
>     "scope": "link",
>     "flags": [ ]
> } ]

Something like this?

diff --git a/ip/iproute_lwtunnel.c b/ip/iproute_lwtunnel.c
index 52221c6976b3..37730024caaf 100644
--- a/ip/iproute_lwtunnel.c
+++ b/ip/iproute_lwtunnel.c
@@ -834,14 +834,15 @@ static void print_encap_xfrm(FILE *fp, struct rtattr *encap)
 void lwt_print_encap(FILE *fp, struct rtattr *encap_type,
 			  struct rtattr *encap)
 {
-	int et;
+	uint16_t et;
 
 	if (!encap_type)
 		return;
 
 	et = rta_getattr_u16(encap_type);
-
-	print_string(PRINT_ANY, "encap", " encap %s ", format_encap_type(et));
+	open_json_object("encap");
+	print_string(PRINT_ANY, "encap_type", " encap %s ",
+		     format_encap_type(et));
 
 	switch (et) {
 	case LWTUNNEL_ENCAP_MPLS:
@@ -875,6 +876,7 @@ void lwt_print_encap(FILE *fp, struct rtattr *encap_type,
 		print_encap_xfrm(fp, encap);
 		break;
 	}
+	close_json_object();
 }
 
 static struct ipv6_sr_hdr *parse_srh(char *segbuf, int hmac, bool encap)

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

end of thread, other threads:[~2023-04-14 17:01 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-04-14  8:29 iproute2 bug in json output for encap Lars Ekman
2023-04-14 15:21 ` Stephen Hemminger
2023-04-14 16:12   ` Lars Ekman
2023-04-14 16:26     ` Lars Ekman
2023-04-14 16:44       ` Stephen Hemminger
2023-04-14 17:01       ` Stephen Hemminger

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