[PATCH RFC net-next 1/2] netconsole: extract message fragmentation into write_msg_target()

Breno Leitao posted 2 patches 1 week, 3 days ago
[PATCH RFC net-next 1/2] netconsole: extract message fragmentation into write_msg_target()
Posted by Breno Leitao 1 week, 3 days ago
Refactor the message fragmentation logic in write_msg() by extracting it
into a separate write_msg_target() helper function. This makes the code
more maintainable and prepares for future reuse in nbcon support for
non-extended consoles.

The helper function takes a target, message, and length, then handles
splitting the message into MAX_PRINT_CHUNK-sized fragments for sending
via send_udp().

No functional change intended.

Signed-off-by: Breno Leitao <leitao@debian.org>
---
 drivers/net/netconsole.c | 26 ++++++++++++++++----------
 1 file changed, 16 insertions(+), 10 deletions(-)

diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c
index bb6e03a92956..f4b1706fb081 100644
--- a/drivers/net/netconsole.c
+++ b/drivers/net/netconsole.c
@@ -1559,6 +1559,20 @@ static void append_release(char *buf)
 	scnprintf(buf, MAX_PRINT_CHUNK, "%s,", release);
 }
 
+static void write_msg_target(struct netconsole_target *nt, const char *msg,
+			     unsigned int len)
+{
+	const char *tmp = msg;
+	int frag, left = len;
+
+	while (left > 0) {
+		frag = min(left, MAX_PRINT_CHUNK);
+		send_udp(nt, tmp, frag);
+		tmp += frag;
+		left -= frag;
+	}
+}
+
 static void send_fragmented_body(struct netconsole_target *nt,
 				 const char *msgbody, int header_len,
 				 int msgbody_len, int extradata_len)
@@ -1728,10 +1742,8 @@ static void write_ext_msg(struct console *con, const char *msg,
 
 static void write_msg(struct console *con, const char *msg, unsigned int len)
 {
-	int frag, left;
-	unsigned long flags;
 	struct netconsole_target *nt;
-	const char *tmp;
+	unsigned long flags;
 
 	if (oops_only && !oops_in_progress)
 		return;
@@ -1748,13 +1760,7 @@ static void write_msg(struct console *con, const char *msg, unsigned int len)
 			 * at least one target if we die inside here, instead
 			 * of unnecessarily keeping all targets in lock-step.
 			 */
-			tmp = msg;
-			for (left = len; left;) {
-				frag = min(left, MAX_PRINT_CHUNK);
-				send_udp(nt, tmp, frag);
-				tmp += frag;
-				left -= frag;
-			}
+			write_msg_target(nt, msg, len);
 		}
 	}
 	spin_unlock_irqrestore(&target_list_lock, flags);

-- 
2.47.3
Re: [PATCH RFC net-next 1/2] netconsole: extract message fragmentation into write_msg_target()
Posted by Petr Mladek 1 week ago
On Fri 2025-11-21 03:26:07, Breno Leitao wrote:
> Refactor the message fragmentation logic in write_msg() by extracting it
> into a separate write_msg_target() helper function. This makes the code
> more maintainable and prepares for future reuse in nbcon support for
> non-extended consoles.
> 
> The helper function takes a target, message, and length, then handles
> splitting the message into MAX_PRINT_CHUNK-sized fragments for sending
> via send_udp().
> 
> No functional change intended.

> --- a/drivers/net/netconsole.c
> +++ b/drivers/net/netconsole.c
> @@ -1559,6 +1559,20 @@ static void append_release(char *buf)
>  	scnprintf(buf, MAX_PRINT_CHUNK, "%s,", release);
>  }
>  
> +static void write_msg_target(struct netconsole_target *nt, const char *msg,
> +			     unsigned int len)
> +{
> +	const char *tmp = msg;
> +	int frag, left = len;
> +
> +	while (left > 0) {
> +		frag = min(left, MAX_PRINT_CHUNK);
> +		send_udp(nt, tmp, frag);
> +		tmp += frag;
> +		left -= frag;
> +	}
> +}
> +
>  static void send_fragmented_body(struct netconsole_target *nt,
>  				 const char *msgbody, int header_len,
>  				 int msgbody_len, int extradata_len)
> @@ -1748,13 +1760,7 @@ static void write_msg(struct console *con, const char *msg, unsigned int len)
>  			 * at least one target if we die inside here, instead
>  			 * of unnecessarily keeping all targets in lock-step.
>  			 */
> -			tmp = msg;
> -			for (left = len; left;) {
> -				frag = min(left, MAX_PRINT_CHUNK);
> -				send_udp(nt, tmp, frag);
> -				tmp += frag;
> -				left -= frag;
> -			}
> +			write_msg_target(nt, msg, len);

I would call the new function send_msg_udp() to make it symetric with:

static void write_ext_msg(struct console *con, const char *msg,
			  unsigned int len)
{
[...]
			send_ext_msg_udp(nt, msg, len);
[...]
}

By other words, use "write_*()" when struct console * is passed
and send_*_udp() when struct netconsole_target * is passed.

The inconsistence confused me... ;-)

Note that write_msg()/write_ext_msg() are cut&pasted.
The only difference would be send_msg_udp()/send_ext_msg_udp().

>  		}
>  	}
>  	spin_unlock_irqrestore(&target_list_lock, flags);

Otherwise, I confirm that it is just a refactoring with no
functional change.

Feel free to use, ideally after the renaming function:

Reviewed-by: Petr Mladek <pmladek@suse.com>

Best Regards,
Petr