Skip to content

Commit 60be416

Browse files
leitaoPaolo Abeni
authored andcommitted
net: netconsole: split send_msg_fragmented
Refactor the send_msg_fragmented() function by extracting the logic for sending the message body into a new function called send_fragmented_body(). Now, send_msg_fragmented() handles appending the release and header, and then delegates the task of breaking up the body and sending the fragments to send_fragmented_body(). This is the final flow now: When send_ext_msg_udp() is called to send a message, it will: - call send_msg_no_fragmentation() if no fragmentation is needed or - call send_msg_fragmented() if fragmentation is needed * send_msg_fragmented() appends the header to the buffer, which is be persisted until the function returns * call send_fragmented_body() to iterate and populate the body of the message. It will not touch the header, and it will only replace the body, writing the msgbody and/or userdata. Also add some comment to make the code easier to review. Signed-off-by: Breno Leitao <[email protected]> Reviewed-by: Simon Horman <[email protected]> Signed-off-by: Paolo Abeni <[email protected]>
1 parent 144d573 commit 60be416

File tree

1 file changed

+50
-31
lines changed

1 file changed

+50
-31
lines changed

drivers/net/netconsole.c

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,46 +1096,30 @@ static void append_release(char *buf)
10961096
scnprintf(buf, MAX_PRINT_CHUNK, "%s,", release);
10971097
}
10981098

1099-
static void send_msg_fragmented(struct netconsole_target *nt,
1100-
const char *msg,
1101-
int msg_len,
1102-
int release_len)
1099+
static void send_fragmented_body(struct netconsole_target *nt, char *buf,
1100+
const char *msgbody, int header_len,
1101+
int msgbody_len)
11031102
{
1104-
int header_len, msgbody_len, body_len;
1105-
static char buf[MAX_PRINT_CHUNK]; /* protected by target_list_lock */
1106-
int offset = 0, userdata_len = 0;
1107-
const char *header, *msgbody;
11081103
const char *userdata = NULL;
1104+
int body_len, offset = 0;
1105+
int userdata_len = 0;
11091106

11101107
#ifdef CONFIG_NETCONSOLE_DYNAMIC
11111108
userdata = nt->userdata_complete;
11121109
userdata_len = nt->userdata_length;
11131110
#endif
11141111

1115-
/* need to insert extra header fields, detect header and msgbody */
1116-
header = msg;
1117-
msgbody = memchr(msg, ';', msg_len);
1118-
if (WARN_ON_ONCE(!msgbody))
1119-
return;
1120-
1121-
header_len = msgbody - header;
1122-
msgbody_len = msg_len - header_len - 1;
1123-
msgbody++;
1124-
1125-
/*
1126-
* Transfer multiple chunks with the following extra header.
1127-
* "ncfrag=<byte-offset>/<total-bytes>"
1112+
/* body_len represents the number of bytes that will be sent. This is
1113+
* bigger than MAX_PRINT_CHUNK, thus, it will be split in multiple
1114+
* packets
11281115
*/
1129-
if (release_len)
1130-
append_release(buf);
1131-
1132-
/* Copy the header into the buffer */
1133-
memcpy(buf + release_len, header, header_len);
1134-
header_len += release_len;
1135-
11361116
body_len = msgbody_len + userdata_len;
1137-
/* for now on, the header will be persisted, and the msgbody
1138-
* will be replaced
1117+
1118+
/* In each iteration of the while loop below, we send a packet
1119+
* containing the header and a portion of the body. The body is
1120+
* composed of two parts: msgbody and userdata. We keep track of how
1121+
* many bytes have been sent so far using the offset variable, which
1122+
* ranges from 0 to the total length of the body.
11391123
*/
11401124
while (offset < body_len) {
11411125
int this_header = header_len;
@@ -1144,7 +1128,7 @@ static void send_msg_fragmented(struct netconsole_target *nt,
11441128
int this_chunk = 0;
11451129

11461130
this_header += scnprintf(buf + this_header,
1147-
sizeof(buf) - this_header,
1131+
MAX_PRINT_CHUNK - this_header,
11481132
",ncfrag=%d/%d;", offset,
11491133
body_len);
11501134

@@ -1199,6 +1183,41 @@ static void send_msg_fragmented(struct netconsole_target *nt,
11991183
}
12001184
}
12011185

1186+
static void send_msg_fragmented(struct netconsole_target *nt,
1187+
const char *msg,
1188+
int msg_len,
1189+
int release_len)
1190+
{
1191+
static char buf[MAX_PRINT_CHUNK]; /* protected by target_list_lock */
1192+
int header_len, msgbody_len;
1193+
const char *msgbody;
1194+
1195+
/* need to insert extra header fields, detect header and msgbody */
1196+
msgbody = memchr(msg, ';', msg_len);
1197+
if (WARN_ON_ONCE(!msgbody))
1198+
return;
1199+
1200+
header_len = msgbody - msg;
1201+
msgbody_len = msg_len - header_len - 1;
1202+
msgbody++;
1203+
1204+
/*
1205+
* Transfer multiple chunks with the following extra header.
1206+
* "ncfrag=<byte-offset>/<total-bytes>"
1207+
*/
1208+
if (release_len)
1209+
append_release(buf);
1210+
1211+
/* Copy the header into the buffer */
1212+
memcpy(buf + release_len, msg, header_len);
1213+
header_len += release_len;
1214+
1215+
/* for now on, the header will be persisted, and the msgbody
1216+
* will be replaced
1217+
*/
1218+
send_fragmented_body(nt, buf, msgbody, header_len, msgbody_len);
1219+
}
1220+
12021221
/**
12031222
* send_ext_msg_udp - send extended log message to target
12041223
* @nt: target to send message to

0 commit comments

Comments
 (0)