Skip to contentSkip to conversation

Add JSON output options to 'iw' for scan results

PATCH Johannes Berg 5 participants 10 messages Mar 5, 2024 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw
Quoted 3 lines · Isaev Ruslan

v5 changes:
- add json print to print_ap_channel_report()
- minor refactor open_json_object()

Alright, yay, so mechanical submission issues out of the way, this looks readable :)

Quoted 1 line

branch: v5.19

That's almost two years old, so you'll obviously have to rebase it on the latest main branch. But we can have more fundamental discussions first.

Quoted 4 lines

This commit introduces the ability to output scan results from 'iw' in JSON format, similar to the 'ip' utility from the iproute2 package. The addition aims to enhance the tool's usability and scriptability by providing a structured data format option.

Generally, I'm not opposed to that, but I think we've had this discussion before, and I don't want to see a lot of extra complexity in the code for it.

Quoted 3 lines

Two new command-line options are added:
- '-j': Outputs scan results in compact JSON format.
- '-jj': Outputs scan results in pretty-printed JSON format.

With tools like 'jq' being near ubiquitous, is there much value in having two output formats? I'd almost say only support -j (and I don't like '-jj' anyway, probably would prefer -j/--json and --json=pretty or so), and then 'jq' can do pretty-printing and coloring etc.?

Review

Quoted 4 lines
(quoted diff) +4 -0
+	//close root json object and deinit jsonw
+	if(iw_json){
+		delete_json_obj();
+	}

Please generally try to follow the (existing/Linux) coding style more closely. I don't mind C99 comments but a space would be nice, space after if, space before the opening brace, no braces needed here though, perhaps more (just not on these four lines :-) )

Quoted 1 line

+++ b/json/json_print.c

I'm generally not going to look into these files for now, but including them internally means we have to maintain them. I'd almost prefer a library that can be used.

However, with that said,

Quoted 7 lines

+/*
+ * json_print.c "print regular or json output, based on json_writer".
+ *

+ *             This program is free software; you can redistribute it and/or
+ *             modify it under the terms of the GNU General Public License
+ *             as published by the Free Software Foundation; either version
+ *             2 of the License, or (at your option) any later version.

This doesn't work well with the ISC license of iw. Which is another reason to prefer an existing library, I suppose.

Review line 1

Quoted 3 lines

+++ b/json/json_writer.c

(quoted diff) +1 -0
@@ -0,0 +1,296 @@
+// SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) //

We don't use SPDX license tags here yet, so not sure this is meaningful, we also don't have a dir with the actual license texts like the kernel has for instance.

If going this way it'd probably be useful to add SPDX tags to all existing files too.

Here, off the top of my head, I suspect the BSD-2-Clause should actually be compatible to releasing the whole thing under ISC, but need to check that too.

Quoted 15 lines
scan.c +2 -0
@@ -12,6 +12,8 @@
 #include "nl80211.h"
 #include "iw.h"
 
+#include "json/json_print.h"
+
 #define WLAN_CAPABILITY_ESS		(1<<0)
 #define WLAN_CAPABILITY_IBSS		(1<<1)
 #define WLAN_CAPABILITY_CF_POLLABLE	(1<<2)
@@ -546,10 +548,13 @@ static int handle_scan(struct nl80211_state *state,
 
 static void tab_on_first(bool *first)
 {

scan.c static int handle_scan(struct nl80211_state *state, · line 557

Quoted 10 lines
scan.c +6 -3
-	if (!*first)
-		printf("\t");
-	else
+	if (!*first){
+		if(!iw_json){
+			printf("\t");
+		}
+	}else{
 		*first = false;
+	}

Coding style. Not sure why you don't just skip the whole thing if json is turned on?

Quoted 4 lines
scan.c +0 -1
 static void print_ssid(const uint8_t type, uint8_t len, const uint8_t *data,
 		       const struct print_ies_data *ie_buffer)
 {
-	printf(" ");

scan.c static int handle_scan(struct nl80211_state *state,

Quoted 10 lines
scan.c +7 -2
-	print_ssid_escaped(len, data);
-	printf("\n");
+	if(!iw_json){
+		printf(" ");
+		print_ssid_escaped(len, data);
+		printf("\n");
+	} else {
+		print_ssid_escaped(len, data);
+	}
 }

Might be more useful to provide a JSON-output for SSIDs? Surely you'd also want to escape them in a JSON-compatible way, since you'd want to use '\uXXXX' encoding to actually get proper bytes out on the other side without a special decoder?

Quoted 15 lines
scan.c +7 -4
@@ -574,21 +583,35 @@ static void print_supprates(const uint8_t type, uint8_t len,
 {
 	int i;
 
-	printf(" ");
-
-	for (i = 0; i < len; i++) {
-		int r = data[i] & 0x7f;
+	if(iw_json){
+		open_json_array("array", NULL);
+		for (i = 0; i < len; i++) {
+			int r = data[i] & 0x7f;
+			if (r == BSS_MEMBERSHIP_SELECTOR_VHT_PHY && data[i] & 0x80){
+				print_string(PRINT_JSON, NULL, "VHT%s", data[i] & 0x80 ? "*" : "");
+			} else if (r == BSS_MEMBERSHIP_SELECTOR_HT_PHY && data[i] & 0x80){

scan.c static void print_supprates(const uint8_t type, uint8_t len, · line 602

Quoted 10 lines
scan.c +10 -0
+				print_string(PRINT_JSON, NULL, "HT%s", data[i] & 0x80 ? "*" : "");
+			} else {
+				print_string(PRINT_JSON, NULL, "%d.%d%s", r/2, 5*(r&1), data[i] & 0x80 ? "*" : "");
+			}
+		}
+		close_json_array(NULL);
+	}else{
+		printf(" ");
+		for (i = 0; i < len; i++) {
+			int r = data[i] & 0x7f;

So generally, here, no - I don't like this style of working at all. We'd really want to have as little "if (iw_json)" as possible in the code. In code like this, we really shouldn't have such an if at all, but only e.g.

  open_json_array();
  for (...) {
    if (...)
      print_string(...);
  }
  close_json_array();

Surely we can come up with an abstraction where something like print_string() does the right thing either way? And open/close JSON array does the '\n' needed after it, or something?
Maybe we'd pass an argument to open_json_array() that says how we want it printed in the non-JSON case (and rename it to open_array?), e.g. should it all be on one line like here, or should it be multiple lines with " - " prefix on each line, etc. JSON also tracks nesting, I guess, so we could even do that for nested stuff in the normal output where we use tabs etc.?

Generally, I'd also say that we never promised stable output, so if the output changes slightly due to such, I'm perfectly fine with that, whoever relied on it (and that includes me in some internal code) gets to keep the pieces when it breaks ...

Also, looking at the function calls, all those NULL and PRINT_JSON are pointless, as far as I can tell? So I think it'd make more sense to come up with a JSON-supporting but otherwise agnostic API abstraction:

open_array(SINGLE_LINE); // could also be MULTI_LINE for ...
print_string("VHT%s", data[i] & 0x80 ? "*" : "");
close_array();

Maybe open_array returns some kind of token that you pass to the inner calls down to close_array(), or maybe it just keeps a stack of these internally, I could see either way working (though an internal stack would probably be simpler to use, and with a lot of code using it, that might make sense for to trade off against the additional internal complexity)

Somewhat unrelated to that discussion, you might also want to not actually use print_string() for rates here though, and possibly use a different JSON encoding for the rates (and membership selectors), e.g. as an object with a 'basic' property? But I don't care much about that part, though generally I'd argue strings shouldn't be used where there's a better representation with native JSON types.

scan.c static void print_supprates(const uint8_t type, uint8_t len, · line 604

Quoted 2 lines
scan.c +2 -0
+	if(iw_json){
+		print_string(PRINT_JSON, "capabilities_raw", "0x%02x 0x%02x 0x%02x 0x%02x 0x%02x", data[0], data[1], data[2], data[3], data[4]);

That's a really bad JSON representation, a string with bytes, rather than an array of bytes?

scan.c static void print_supprates(const uint8_t type, uint8_t len, · line 609

Quoted 6 lines
scan.c +1 -1
 static void print_erp(const uint8_t type, uint8_t len, const uint8_t *data,
 		      const struct print_ies_data *ie_buffer)
 {
 	if (data[0] == 0x00)
-		printf(" <no flags>");
+		iw_json ? print_bool(PRINT_JSON, "no_flags", "%s", true) : printf(" <no flags>");

It seems like you'd generally want to have a container for each of these, so you have

// probably these two lines outside of the print_erp() call even open_json_object("erp");
print_u8("tag", type);

print_flags(...);

or something, rather than including "no_flags" in the object?

scan.c static void print_supprates(const uint8_t type, uint8_t len, · line 610

Quoted 1 line
scan.c +1 -0
+			iw_json ? print_string(PRINT_JSON, NULL, "WEP-40") : printf("WEP-40");

This goes just along with what I wrote above, but that iw_json thing here seems really pointless, and just makes things difficult.

(I also don't like the use of the ternary operator in this way but that's maybe not quite the point.)

[snip lots of similar stuff]

scan.c static void print_supprates(const uint8_t type, uint8_t len, · line 612

Quoted 3 lines
scan.c +1 -1
 	unsigned int bw160[] = { 5180, 5500, 5955, 6115, 6275, 6435,
-				  6595, 6755, 6915 };
+				6595, 6755, 6915 };

nit: unrelated whitespace change

So ... Like I wrote above, generally I'm not against doing this. But like I also tried to explain above, I think it needs to be less "duplicative". I'm happy to change the code in major ways to make JSON output easier, I'm also happy to let that change the output in some ways (maybe the default should be YAML-compatible ;-) ).

But I'd really want to see this done in a way that doesn't end up with having to duplicate everything all the time.

We'll also need to figure out the licensing situation, and perhaps ideally find a way to not add ~1.5k LOC to support it, but link against something that exists already.

johannes

#2 lore raw

On Tue Mar 5, 2024 at 10:41 AM CET, Johannes Berg wrote:
[...]

Quoted 4 lines

I'm generally not going to look into these files for now, but including them internally means we have to maintain them. I'd almost prefer a library that can be used.

For what it's worth jansson has been very good to me. It has printf() like object creation which usually integrates well with c programs.

Quoted 12 lines

However, with that said,

Quoted 7 lines

+/*
+ * json_print.c "print regular or json output, based on json_writer".
+ *

+ *             This program is free software; you can redistribute it and/or
+ *             modify it under the terms of the GNU General Public License
+ *             as published by the Free Software Foundation; either version
+ *             2 of the License, or (at your option) any later version.
Quoted 2 lines

This doesn't work well with the ISC license of iw. Which is another reason to prefer an existing library, I suppose.

With MIT license https://github.com/akheron/jansson/blob/master/LICENSE

[...]

Quoted 11 lines

So ... Like I wrote above, generally I'm not against doing this. But like I also tried to explain above, I think it needs to be less "duplicative". I'm happy to change the code in major ways to make JSON output easier, I'm also happy to let that change the output in some ways (maybe the default should be YAML-compatible ;-) ).

But I'd really want to see this done in a way that doesn't end up with having to duplicate everything all the time.

For me, having a "good" json representation means, as you pointed out, using the right json underlying type and most of the time I'm afraid that means a complete different code path because of the underlying type/container. It's always a blurry line obviously but duplicating in a complete seperate function that only does the right thing for json output may end up being cleaner codewise.

Quoted 3 lines

We'll also need to figure out the licensing situation, and perhaps ideally find a way to not add ~1.5k LOC to support it, but link against something that exists already.

+1 for not reimplementing the wheel.

Quoted 2 lines

johannes

#3 lore raw
Quoted 2 lines · Nicolas Escande

For what it's worth jansson has been very good to me. It has printf() like object creation which usually integrates well with c programs.

Nice :)

Quoted 1 line

With MIT license https://github.com/akheron/jansson/blob/master/LICENSE

If we link it as a library rather than copying it, that wouldn't even matter (well unless it was GPL too, but even then you could still make a build without it if you don't want the result to become GPL.)

Quoted 5 lines

For me, having a "good" json representation means, as you pointed out, using the right json underlying type and most of the time I'm afraid that means a complete different code path because of the underlying type/container. It's always a blurry line obviously but duplicating in a complete seperate function that only does the right thing for json output may end up being cleaner codewise.

Yeah, depends, I guess. I'd rather see less duplicating, and in the cases like in this patch it seems like it wouldn't even matter so much. But agree it depends on the objects.

Just things like

	iw_json ? print_string(PRINT_JSON, "manufacturer", "%.*s", sublen, data + 4) : printf("\t * Manufacturer: %.*s\n", sublen, data + 4);

seem like the worst of both worlds... More obviously perhaps where it's not even different:

	iw_json ? print_string(PRINT_JSON, NULL, "FT/IEEE 802.1X") : printf("FT/IEEE 802.1X");

or

	iw_json ? print_string(PRINT_JSON, NULL, "%.02x-%.02x-%.02x:%d", data[0], data[1] ,data[2], data[3]) : printf("%.02x-%.02x-%.02x:%d", data[0], data[1] ,data[2], data[3]);

Whereas with something like

	iw_json ? print_string(PRINT_JSON, "response_type", "%d%s", val, val == 3 ? " (AP)" : "") : printf("\t * Response Type: %d%s\n", val, val == 3 ? " (AP)" : "");

(ugh, I don't like those long lines)

you probably don't even want to have the " (AP)" thing in the JSON, but make it a real integer type rather than a string with print_string()!

But we could make that

	output_int("response_type", "Response Type", val);
	if (val == 3)
		hint("(AP)");

and the hint() just doesn't do anything in JSON?

And maybe even "response_type" is translated to "Response Type" for the human output so you don't need to add the other "Response Type" there. Dunno. I really wouldn't mind if in some places that meant we'd now output "Response Type" instead of "response type" or "Response type" or something.

Or perhaps do it the other way around, so "Response Type" is lower-cased and illegal chars like " " are replaced by "_", although that makes the JSON representation less obvious in the code.

Picking one arbitrarily, we'd have something

	output_int("Response Type", val);
	if (val == 3)
		hint("(AP)");

which seems far nicer than

	iw_json ? print_string(PRINT_JSON, "response_type", "%d%s", val, val == 3 ? " (AP)" : "") : printf("\t * Response Type: %d%s\n", val, val == 3 ? " (AP)" : "");

in both code and JSON representation.

Yes we'll have to do something about the \n and indentation and I know I'm handwaving about that, but it seems like with open_object() and close_object() etc. that need to keep a state stack anyway, that should be solvable?

I'd argue that we should try to get somewhere around 80-90% of the cases unified in this matter, and then have special things for the rest that are special kinds of objects, etc.

Right now it's pretty much 100% special cases.

johannes

#4 lore raw
Quoted 20 lines · Johannes Berg
Quoted 2 lines · Nicolas Escande

For what it's worth jansson has been very good to me. It has printf() like object creation which usually integrates well with c programs.

Nice :)

Quoted 1 line

With MIT license https://github.com/akheron/jansson/blob/master/LICENSE

If we link it as a library rather than copying it, that wouldn't even matter (well unless it was GPL too, but even then you could still make a build without it if you don't want the result to become GPL.)

Quoted 5 lines

For me, having a "good" json representation means, as you pointed out, using the right json underlying type and most of the time I'm afraid that means a complete different code path because of the underlying type/container. It's always a blurry line obviously but duplicating in a complete seperate function that only does the right thing for json output may end up being cleaner codewise.

Quoted 3 lines

Yeah, depends, I guess. I'd rather see less duplicating, and in the cases like in this patch it seems like it wouldn't even matter so much. But agree it depends on the objects.

I think i could make a universal printf() like function with additional args for json representation.

Something like this:
before:
printf("\t * Manufacturer: %.*s\n", sublen, data + 4);

after:
printf_json("\t * Manufacturer", "%.*s", sublen, data + 4);

void printf_json(const char* key, const char* fmt, ...) {
    char buf[1024];
    va_list args;
    va_start(args, format);

    if (iw_json) {
        vsnprintf(buf, sizeof(buf), format, args);
        print_string(PRINT_JSON, key, "%s", buf);
    } else {
        printf("\t * %s: ", key);
        vprintf(fmt, args);
        printf("\n");
    }

    va_end(args);
}
Quoted 66 lines

Just things like

        iw_json ? print_string(PRINT_JSON, "manufacturer", "%.*s", sublen, data + 4) : printf("\t * Manufacturer: %.*s\n", sublen, data + 4);

seem like the worst of both worlds... More obviously perhaps where it's not even different:

        iw_json ? print_string(PRINT_JSON, NULL, "FT/IEEE 802.1X") : printf("FT/IEEE 802.1X");

or

        iw_json ? print_string(PRINT_JSON, NULL, "%.02x-%.02x-%.02x:%d", data[0], data[1] ,data[2], data[3]) : printf("%.02x-%.02x-%.02x:%d", data[0], data[1] ,data[2], data[3]);

Whereas with something like

        iw_json ? print_string(PRINT_JSON, "response_type", "%d%s", val, val == 3 ? " (AP)" : "") : printf("\t * Response Type: %d%s\n", val, val == 3 ? " (AP)" : "");

(ugh, I don't like those long lines)

you probably don't even want to have the " (AP)" thing in the JSON, but make it a real integer type rather than a string with print_string()!

But we could make that

        output_int("response_type", "Response Type", val);
        if (val == 3)
                hint("(AP)");

and the hint() just doesn't do anything in JSON?

And maybe even "response_type" is translated to "Response Type" for the human output so you don't need to add the other "Response Type" there. Dunno. I really wouldn't mind if in some places that meant we'd now output "Response Type" instead of "response type" or "Response type" or something.

Or perhaps do it the other way around, so "Response Type" is lower-cased and illegal chars like " " are replaced by "_", although that makes the JSON representation less obvious in the code.

Picking one arbitrarily, we'd have something

        output_int("Response Type", val);
        if (val == 3)
                hint("(AP)");

which seems far nicer than

        iw_json ? print_string(PRINT_JSON, "response_type", "%d%s", val, val == 3 ? " (AP)" : "") : printf("\t * Response Type: %d%s\n", val, val == 3 ? " (AP)" : "");

in both code and JSON representation.

Yes we'll have to do something about the \n and indentation and I know I'm handwaving about that, but it seems like with open_object() and close_object() etc. that need to keep a state stack anyway, that should be solvable?

I'd argue that we should try to get somewhere around 80-90% of the cases unified in this matter, and then have special things for the rest that are special kinds of objects, etc.

Right now it's pretty much 100% special cases.

johannes

#5 lore raw
Quoted 10 lines · Isaev Ruslan

I think i could make a universal printf() like function with additional args for json representation.

Something like this:
before:
printf("\t * Manufacturer: %.*s\n", sublen, data + 4);

after:
printf_json("\t * Manufacturer", "%.*s", sublen, data + 4);

I think you meant

printf_json("Manufacturer", "%.*s", sublen, data + 4);

but yeah, that seems better? Not sure I'd call it print_json() over just print() or output() or so at that point.

Quoted 10 lines
void printf_json(const char* key, const char* fmt, ...) {
    char buf[1024];
    va_list args;
    va_start(args, format);

    if (iw_json) {
        vsnprintf(buf, sizeof(buf), format, args);
        print_string(PRINT_JSON, key, "%s", buf);
    } else {
        printf("\t * %s: ", key);

Guess we'd have to be careful about the indentation. Track the (JSON) object nesting level and apply it that way?

johannes

#6 lore raw
In reply to Nicolas Escande
Quoted 4 lines · Nicolas Escande

On Tue Mar 5, 2024 at 10:41 AM CET, Johannes Berg wrote:
[...]

Quoted 1 line
Quoted 22 lines
Quoted 3 lines

I'm generally not going to look into these files for now, but including them internally means we have to maintain them. I'd almost prefer a library that can be used.

For what it's worth jansson has been very good to me. It has printf() like object creation which usually integrates well with c programs.

Quoted 14 lines

However, with that said,

Quoted 7 lines

+/*
+ * json_print.c "print regular or json output, based on json_writer".
+ *

+ *             This program is free software; you can redistribute it and/or
+ *             modify it under the terms of the GNU General Public License
+ *             as published by the Free Software Foundation; either version
+ *             2 of the License, or (at your option) any later version.

This doesn't work well with the ISC license of iw. Which is another reason to prefer an existing library, I suppose.

IMHO jansson library is more complicated than stream jsonw which is simple as a hatchet.
I can rewrite json_print.c with any LiCENCE we need.

#7 lore raw
In reply to Johannes Berg
Quoted 7 lines · Johannes Berg
Quoted 3 lines · Isaev Ruslan

v5 changes:
- add json print to print_ap_channel_report()
- minor refactor open_json_object()

Alright, yay, so mechanical submission issues out of the way, this looks readable :)

Did the v5 version hit the list? I don't see it, and neither does lore:
<https://lore.kernel.org/all/[email protected]/>

I received v4 directly, not via the list, and that was addressed to "Undisclosed Recipients". lore also doesn't have v4:
<https://lore.kernel.org/all/[email protected]/>

So I don't think the mechanical submission issues are out of the way unless the list rejected the patch due to size, which seems unlikely.

/jeff

#8 lore raw
Quoted 13 lines · Jeff Johnson
Quoted 7 lines · Johannes Berg
Quoted 3 lines · Isaev Ruslan

v5 changes:
- add json print to print_ap_channel_report()
- minor refactor open_json_object()

Alright, yay, so mechanical submission issues out of the way, this looks readable :)

Did the v5 version hit the list? I don't see it, and neither does lore:
<https://lore.kernel.org/all/[email protected]/>

Quoted 7 lines

I received v4 directly, not via the list, and that was addressed to "Undisclosed Recipients". lore also doesn't have v4:
<https://lore.kernel.org/all/[email protected]/>

So I don't think the mechanical submission issues are out of the way unless the list rejected the patch due to size, which seems unlikely.

Yes it did -
https://patchwork.kernel.org/project/linux-wireless/patch/[email protected]/

Looks like the first reply started a separate thread. Probably --in-reply-to was missed.

#10 lore raw
In reply to Aditya Kumar Singh
Quoted 22 lines · Aditya Kumar Singh
Quoted 13 lines · Jeff Johnson
Quoted 7 lines · Johannes Berg
Quoted 3 lines · Isaev Ruslan

v5 changes:
- add json print to print_ap_channel_report()
- minor refactor open_json_object()

Alright, yay, so mechanical submission issues out of the way, this looks readable :)

Did the v5 version hit the list? I don't see it, and neither does lore:
<https://lore.kernel.org/all/[email protected]/>

Quoted 7 lines

I received v4 directly, not via the list, and that was addressed to "Undisclosed Recipients". lore also doesn't have v4:
<https://lore.kernel.org/all/[email protected]/>

So I don't think the mechanical submission issues are out of the way unless the list rejected the patch due to size, which seems unlikely.

Quoted 6 lines

Yes it did -
https://patchwork.kernel.org/project/linux-wireless/patch/[email protected]/

Looks like the first reply started a separate thread. Probably --in-reply-to was missed.

Very strange. There is obviously still some Message-Id: strangeness since the patchwork message id (and lore message id) "[email protected]" doesn't match the In-Reply-To message id in Johannes' reply "[email protected]"

Perhaps the patch was sent to Johannes as a separate unicast message that was separate from the message sent to the list?

/jeff