Skip to contentSkip to conversation

bridge/mdb: fix missing new line when show bridge mdb

PATCH Superseded Hangbin Liu 3 participants 9 messages Sep 5, 2018 – Sep 12, 2018

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bridge/mdb: fix missing new line when show bridge mdb

The bridge mdb show is broken on current iproute2. e.g.
]# bridge mdb show
34: br0 veth0_br 224.1.1.2 temp 34: br0 veth0_br 224.1.1.1 temp

After fix:
]# bridge mdb show

34: br0  veth0_br  224.1.1.2  temp
34: br0  veth0_br  224.1.1.1  temp
Fixes c7c1a1ef51aea ("bridge: colorize output and use JSON print library")
---
 bridge/mdb.c | 8 ++++++++
 1 file changed, 8 insertions(+)
bridge/mdb.c +8 -0
index f38dc67..d89c065 100644
@@ -107,6 +107,10 @@ static void br_print_router_ports(FILE *f, struct rtattr *attr,
 			fprintf(f, "%s ", port_ifname);
 		}
 	}
+
+	if (!is_json_context() && !show_stats)
+		fprintf(f, "\n");
+
 	close_json_array(PRINT_JSON, NULL);
 }
 
@@ -164,6 +168,10 @@ static void print_mdb_entry(FILE *f, int ifindex, const struct br_mdb_entry *e,
 		print_string(PRINT_ANY, "timer", " %s",
 			     format_timer(timer));
 	}
+
+	if (!is_json_context())
+		fprintf(f, "\n");
+
 	close_json_object();
 }
 
Signature · 1 line
2.5.5
#1 lore raw
Quoted 32 lines · On Wed, 5 Sep 2018 11:33:43 +0800 Hangbin Liu <[email protected]> wrote:

The bridge mdb show is broken on current iproute2. e.g.
]# bridge mdb show
34: br0 veth0_br 224.1.1.2 temp 34: br0 veth0_br 224.1.1.1 temp

After fix:
]# bridge mdb show

34: br0  veth0_br  224.1.1.2  temp
34: br0  veth0_br  224.1.1.1  temp
Fixes c7c1a1ef51aea ("bridge: colorize output and use JSON print library")
---
 bridge/mdb.c | 8 ++++++++
 1 file changed, 8 insertions(+)
bridge/mdb.c +4 -0
index f38dc67..d89c065 100644
@@ -107,6 +107,10 @@ static void br_print_router_ports(FILE *f, struct rtattr *attr,
 			fprintf(f, "%s ", port_ifname);
 		}
 	}
+
+	if (!is_json_context() && !show_stats)
+		fprintf(f, "\n");
+
 	close_json_array(PRINT_JSON, NULL);
 }
 

bridge/mdb.c static void print_mdb_entry(FILE *f, int ifindex, const struct br_mdb_entry *e, · line 177

Quoted 10 lines
bridge/mdb.c +4 -0
@@ -164,6 +168,10 @@ static void print_mdb_entry(FILE *f, int ifindex, const struct br_mdb_entry *e,
 		print_string(PRINT_ANY, "timer", " %s",
 			     format_timer(timer));
 	}
+
+	if (!is_json_context())
+		fprintf(f, "\n");
+
 	close_json_object();
 }
 

Thanks for catching this.

Now that there is a json print library, the preferred pattern for this is:

	print_string(PRINT_FP, NULL, "\n", NULL);

I plan to introduce a helper

	print_fp(...)

and it would be easier if all places were consistent.

#2 lore raw

Hi Stephen,

Quoted 14 lines · Stephen Hemminger

bridge/mdb.c static void print_mdb_entry(FILE *f, int ifindex, const struct br_mdb_entry *e, · line 177

Quoted 11 lines
bridge/mdb.c +4 -0
@@ -164,6 +168,10 @@ static void print_mdb_entry(FILE *f, int ifindex, const struct br_mdb_entry *e,
 		print_string(PRINT_ANY, "timer", " %s",
 			     format_timer(timer));
 	}
+
+	if (!is_json_context())
+		fprintf(f, "\n");
+
 	close_json_object();
 }
 

Thanks for catching this.

Quoted 3 lines

Now that there is a json print library, the preferred pattern for this is:

	print_string(PRINT_FP, NULL, "\n", NULL);

Are we going to replace all printf() by json print library, even not in json context? If yes, I can post a v2 patch. Becuase there are still a lot fprintf() in mdb.c.

Quoted 5 lines

I plan to introduce a helper

	print_fp(...)

and it would be easier if all places were consistent.

cool, that would be more clear.

Thanks
Hangbin

#3 lore raw

PATCH v2 1/1 bridge/mdb: fix missing new line when show bridge mdb

In reply to Hangbin Liu

The bridge mdb show is broken on current iproute2. e.g.
]# bridge mdb show
34: br0 veth0_br 224.1.1.2 temp 34: br0 veth0_br 224.1.1.1 temp

After fix:
]# bridge mdb show

34: br0  veth0_br  224.1.1.2  temp
34: br0  veth0_br  224.1.1.1  temp

v2: use json print lib as Stephen suggested.

Fixes c7c1a1ef51aea ("bridge: colorize output and use JSON print library")
---
 bridge/mdb.c | 8 ++++++++
 1 file changed, 8 insertions(+)
bridge/mdb.c +8 -0
index f38dc67..408117d 100644
@@ -107,6 +107,10 @@ static void br_print_router_ports(FILE *f, struct rtattr *attr,
 			fprintf(f, "%s ", port_ifname);
 		}
 	}
+
+	if (!is_json_context() && !show_stats)
+		print_string(PRINT_FP, NULL, "\n", NULL);
+
 	close_json_array(PRINT_JSON, NULL);
 }
 
@@ -164,6 +168,10 @@ static void print_mdb_entry(FILE *f, int ifindex, const struct br_mdb_entry *e,
 		print_string(PRINT_ANY, "timer", " %s",
 			     format_timer(timer));
 	}
+
+	if (!is_json_context())
+		print_string(PRINT_FP, NULL, "\n", NULL);
+
 	close_json_object();
 }
 
Signature · 1 line
2.5.5
#4 lore raw

Hi Hangbin,

On Tue, Sep 11, 2018 at 09:26:35AM +0800, Hangbin Liu wrote:
[...]

bridge/mdb.c static void br_print_router_ports(FILE *f, struct rtattr *attr, · line 112

Quoted 2 lines
bridge/mdb.c +2 -0
+	if (!is_json_context() && !show_stats)
+		print_string(PRINT_FP, NULL, "\n", NULL);

There is no need to check for !is_json_context() here. You give a type of PRINT_FP which won't lead to output if JSON is active. For reference, check print_color_string() in lib/json_print.c and _IS_JSON_CONTEXT macro.

Cheers, Phil

#5 lore raw
In reply to Hangbin Liu

bridge/mdb.c static void br_print_router_ports(FILE *f, struct rtattr *attr, · line 112

Quoted 2 lines · On Tue, 11 Sep 2018 09:26:35 +0800 Hangbin Liu <[email protected]> wrote:
bridge/mdb.c +2 -0
+	if (!is_json_context() && !show_stats)
+		print_string(PRINT_FP, NULL, "\n", NULL);

I just added print_nl to json_print which does what you want.

#6 lore raw

PATCH v3 1/1 bridge/mdb: fix missing new line when show bridge mdb

In reply to Hangbin Liu

The bridge mdb show is broken on current iproute2. e.g.
]# bridge mdb show
34: br0 veth0_br 224.1.1.2 temp 34: br0 veth0_br 224.1.1.1 temp

After fix:
]# bridge mdb show

34: br0  veth0_br  224.1.1.2  temp
34: br0  veth0_br  224.1.1.1  temp

v2: Use json print lib as Stephen suggested.
v3: No need to use is_json_context() as Phil pointed out.

Fixes c7c1a1ef51aea ("bridge: colorize output and use JSON print library")
---
 bridge/mdb.c | 6 ++++++
 1 file changed, 6 insertions(+)
bridge/mdb.c +6 -0
index f38dc67..2f24b01 100644
@@ -107,6 +107,10 @@ static void br_print_router_ports(FILE *f, struct rtattr *attr,
 			fprintf(f, "%s ", port_ifname);
 		}
 	}
+
+	if (!show_stats)
+		print_string(PRINT_FP, NULL, "\n", NULL);
+
 	close_json_array(PRINT_JSON, NULL);
 }
 
@@ -164,6 +168,8 @@ static void print_mdb_entry(FILE *f, int ifindex, const struct br_mdb_entry *e,
 		print_string(PRINT_ANY, "timer", " %s",
 			     format_timer(timer));
 	}
+
+	print_string(PRINT_FP, NULL, "\n", NULL);
 	close_json_object();
 }
 
Signature · 1 line
2.5.5
#7 lore raw
Quoted 35 lines · On Tue, 11 Sep 2018 22:04:50 +0800 Hangbin Liu <[email protected]> wrote:

The bridge mdb show is broken on current iproute2. e.g.
]# bridge mdb show
34: br0 veth0_br 224.1.1.2 temp 34: br0 veth0_br 224.1.1.1 temp

After fix:
]# bridge mdb show

34: br0  veth0_br  224.1.1.2  temp
34: br0  veth0_br  224.1.1.1  temp

v2: Use json print lib as Stephen suggested.
v3: No need to use is_json_context() as Phil pointed out.

Fixes c7c1a1ef51aea ("bridge: colorize output and use JSON print library")
---
 bridge/mdb.c | 6 ++++++
 1 file changed, 6 insertions(+)
bridge/mdb.c +4 -0
index f38dc67..2f24b01 100644
@@ -107,6 +107,10 @@ static void br_print_router_ports(FILE *f, struct rtattr *attr,
 			fprintf(f, "%s ", port_ifname);
 		}
 	}
+
+	if (!show_stats)
+		print_string(PRINT_FP, NULL, "\n", NULL);
+
 	close_json_array(PRINT_JSON, NULL);
 }
 

bridge/mdb.c static void print_mdb_entry(FILE *f, int ifindex, const struct br_mdb_entry *e, · line 172

Quoted 5 lines
bridge/mdb.c +2 -0
@@ -164,6 +168,8 @@ static void print_mdb_entry(FILE *f, int ifindex, const struct br_mdb_entry *e,
 		print_string(PRINT_ANY, "timer", " %s",
 			     format_timer(timer));
 	}
+
+	print_string(PRINT_FP, NULL, "\n", NULL);

This can now just be:

	print_nl();

The problem with print_string(PRINT_FP, NULL, "\n", NULL) is that it doesn't handle oneline mode correctly.

#8 lore raw

PATCH v4 1/1 bridge/mdb: fix missing new line when show bridge mdb

In reply to Hangbin Liu

The bridge mdb show is broken on current iproute2. e.g.
]# bridge mdb show
34: br0 veth0_br 224.1.1.2 temp 34: br0 veth0_br 224.1.1.1 temp

After fix:
]# bridge mdb show

34: br0  veth0_br  224.1.1.2  temp
34: br0  veth0_br  224.1.1.1  temp

v2: Use json print lib as Stephen suggested.
v3: No need to use is_json_context() as print_string() could handle both cases.
v4: use new function print_nl() to print new line in non-json mode.

Fixes c7c1a1ef51aea ("bridge: colorize output and use JSON print library")
---
 bridge/mdb.c | 6 ++++++
 1 file changed, 6 insertions(+)
bridge/mdb.c +6 -0
index cc1b454..841a361 100644
@@ -107,6 +107,10 @@ static void br_print_router_ports(FILE *f, struct rtattr *attr,
 			fprintf(f, "%s ", port_ifname);
 		}
 	}
+
+	if (!show_stats)
+		print_nl();
+
 	close_json_array(PRINT_JSON, NULL);
 }
 
@@ -157,6 +161,8 @@ static void print_mdb_entry(FILE *f, int ifindex, const struct br_mdb_entry *e,
 		print_string(PRINT_ANY, "timer", " %s",
 			     format_timer(timer));
 	}
+
+	print_nl();
 	close_json_object();
 }
 
Signature · 1 line
2.5.5