Skip to contentSkip to conversation

net: tftp: Add client support for RFC 7440

PATCH Superseded Ramon Fried 2 participants 3 messages May 16, 2020 – May 18, 2020 Networking

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 net: tftp: Add client support for RFC 7440

Add support for RFC 7440: "TFTP Windowsize Option".

This optional feature allows the client and server to negotiate a window size of consecutive blocks to send as an alternative for replacing the single-block lockstep schema.

windowsize can be defined statically during compilation by setting CONFIG_TFTP_WINDOWSIZE, or defined in runtime by setting an environment variable: "tftpwindowsize" If not defined, the windowsize is set to 1, meaning that it behaves as it was never defined.

Choosing the appropriate windowsize depends on the specific network topology, underlying NIC.
You should test various windowsize scenarios and see which best work for you.

Setting a windowsize too big can actually decreases performance.

---
v2:
* Don't send windowsize option on tftpput, as it's not implemented yet.
* Don't send NACK for every out of order block that arrives, one nack is enough.

 README     |  5 ++++
 net/tftp.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 71 insertions(+), 5 deletions(-)
README +5 -0
index be9e6391d6..b85b44201f 100644
@@ -3522,6 +3522,11 @@ List of environment variables (most likely not complete):
 		  downloads succeed with high packet loss rates, or with
 		  unreliable TFTP servers or client hardware.
 
+  tftpwindowsize	- if this is set, the value is used for TFTP's
+		  window size as described by RFC 7440.
+		  This means that count of blocks we can receive before
+		  sending ack to server.
+
   vlan		- When set to a value < 4095 the traffic over
 		  Ethernet is encapsulated/received over 802.1q
 		  VLAN tagged frames.
net/tftp.c +66 -5

133 lines not on this page · raw message

Signature · 1 line
2.26.2
#1 lore raw

PATCH v2 1/1 net: tftp: Add client support for RFC 7440

README List of environment variables (most likely not complete): · line 3528

Quoted 12 lines · Ramon Fried

[...]
index be9e6391d6..b85b44201f 100644

README +4 -0
@@ -3522,6 +3522,11 @@ List of environment variables (most likely not complete):
 		  downloads succeed with high packet loss rates, or with
 		  unreliable TFTP servers or client hardware.
 
+  tftpwindowsize	- if this is set, the value is used for TFTP's
+		  window size as described by RFC 7440.
+		  This means that count of blocks we can receive before
+		  sending ack to server.

that -> the ?

Quoted 17 lines
README +1 -0
+
   vlan		- When set to a value < 4095 the traffic over
 		  Ethernet is encapsulated/received over 802.1q
 		  VLAN tagged frames.
net/tftp.c +0 -1
index be24e63075..e2c005da6e 100644
@@ -5,7 +5,6 @@
  * Copyright 2011 Comelit Group SpA,
  *                Luca Ceresoli <[email protected]>
  */
-
 #include <common.h>
 #include <command.h>
 #include <efi_loader.h>

net/tftp.c static int tftp_tsize; · line 97

Quoted 4 lines
net/tftp.c +1 -0
@@ -95,6 +94,12 @@ static int	tftp_tsize;
 /* The number of hashes we printed */
 static short	tftp_tsize_num_hash;
 #endif
+/* The window size neogiciated */

neogiciated -> negotiated

Quoted 9 lines
net/tftp.c +5 -0
+static ushort	tftp_windowsize;
+/* Next block to send ack to */
+static ushort	tftp_next_ack;
+/* Last nack block we send */
+static ushort	tftp_last_nack;
 #ifdef CONFIG_CMD_TFTPPUT
 /* 1 if writing, else 0 */
 static int	tftp_put_active;

net/tftp.c static char tftp_filename[MAX_LEN]; · line 146

Quoted 8 lines
net/tftp.c +5 -0
@@ -134,8 +139,19 @@ static char tftp_filename[MAX_LEN];
  * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
  */
 
+/* When windowsize is defined to 1,
+ * tftp behaves the same way as it was
+ * never declared
+ */
+#ifdef CONFIG_TFTP_WINDOWSIZE

I'm not sure what's the policy but I think that new CONFIG_ options should be added to a Kconfig file.

Quoted 29 lines
net/tftp.c +14 -0
+#define TFTP_WINDOWSIZE CONFIG_TFTP_WINDOWSIZE
+#else
+#define TFTP_WINDOWSIZE 1
+#endif
+
 static unsigned short tftp_block_size = TFTP_BLOCK_SIZE;
 static unsigned short tftp_block_size_option = CONFIG_TFTP_BLOCKSIZE;
+static unsigned short tftp_window_size_option = TFTP_WINDOWSIZE;
 
 static inline int store_block(int block, uchar *src, unsigned int len)
 {
@@ -348,6 +364,14 @@ static void tftp_send(void)
 		/* try for more effic. blk size */
 		pkt += sprintf((char *)pkt, "blksize%c%d%c",
 				0, tftp_block_size_option, 0);
+
+		/* try for more effic. window size.
+		 * Implemented only for tftp get.
+		 * Don't bother sending if it's 1
+		 */
+		if (tftp_state == STATE_SEND_RRQ && tftp_window_size_option > 1)
+			pkt += sprintf((char *)pkt, "windowsize%c%d%c",
+					0, tftp_window_size_option, 0);
 		len = pkt - xp;
 		break;
 
@@ -500,6 +524,15 @@ static void tftp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
 				      (char *)pkt + i + 6, tftp_tsize);
 			}

net/tftp.c static void tftp_handler(uchar *pkt, unsigned dest, struct in_addr sip, · line 535

Quoted 10 lines
net/tftp.c +9 -0
 #endif
+			if (strcmp((char *)pkt + i,  "windowsize") == 0) {
+				tftp_windowsize =
+					simple_strtoul((char *)pkt + i + 11,
+						       NULL, 10);
+			    debug("windowsize = %s, %d\n",
+				  (char *)pkt + i + 11, tftp_windowsize);
+			}
+
+			tftp_next_ack = tftp_windowsize;

This assignment can be moved outside of the for loop.

Quoted 13 lines
net/tftp.c +6 -0
 		}
 #ifdef CONFIG_CMD_TFTPPUT
 		if (tftp_put_active) {
@@ -514,6 +547,26 @@ static void tftp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
 		if (len < 2)
 			return;
 		len -= 2;
+
+		if (ntohs(*(__be16 *)pkt) != (ushort)(tftp_cur_block + 1)) {
+			debug("Received unexpected block: %d, expected: %d\n",
+			      ntohs(*(__be16 *)pkt),
+			      (ushort)(tftp_cur_block + 1));
+			/*

net/tftp.c static void tftp_handler(uchar *pkt, unsigned dest, struct in_addr sip, · line 565

Quoted 10 lines
net/tftp.c +10 -0
+			 * If one packet is dropped most likely
+			 * all other buffers in the window
+			 * that will arrive will cause a sending NACK.
+			 * This just overwellms the server, let's just send one.
+			 */
+			if (tftp_last_nack != tftp_cur_block) {
+				tftp_send();
+				tftp_last_nack = tftp_cur_block;
+				tftp_next_ack = (ushort)(tftp_cur_block +
+							 tftp_windowsize);

Here neither tftp_cur_block nor tftp_windowsize were updated, so it shouldn't be necessary to update tftp_next_ack?

If the first data packet is lost and we receive data block > 1, we call tftp_send() still in state STATE_SEND_RRQ, which sends again the read request. I would have expected instead to send an ack for block zero to force the server to retransmit block 1; but I guess both ways are ok.

net/tftp.c static void tftp_handler(uchar *pkt, unsigned dest, struct in_addr sip, · line 570

Quoted 5 lines
net/tftp.c +4 -0
+			}
+			break;
+		}
+
 		tftp_cur_block = ntohs(*(__be16 *)pkt);

Perhaps, just increase tftp_cur_block by one, since we have already verified that "ntohs(*(__be16 *)pkt) == (ushort)(tftp_cur_block + 1)" before.

In tftp_handler() there is this debug message:

if (tftp_state == STATE_SEND_RRQ)

     debug("Server did not acknowledge timeout option!\n");

printed when the server doesn't reply to the options. Since multiple options are now supported (not only 'timeout'), the message should be updated (this was already inaccurate before your patch, but it would be the right moment to fix it).

Beniamino

#2 lore raw

PATCH v2 1/1 net: tftp: Add client support for RFC 7440

Thanks for the review.
Will send v3 shortly.

Quoted 10 lines · Beniamino Galvani
Quoted 8 lines · Ramon Fried

[...]
index be9e6391d6..b85b44201f 100644

README +0 -0
@@ -3522,6 +3522,11 @@ List of environment variables (most likely not complete):
                downloads succeed with high packet loss rates, or with
                unreliable TFTP servers or client hardware.
 
Quoted 6 lines

README List of environment variables (most likely not complete): · line 3528

Quoted 4 lines
README +4 -0
+  tftpwindowsize     - if this is set, the value is used for TFTP's
+               window size as described by RFC 7440.
+               This means that count of blocks we can receive before
+               sending ack to server.

that -> the ?

Yes, typo. fixed.

Quoted 16 lines
Quoted 16 lines
README +1 -0
+
   vlan               - When set to a value < 4095 the traffic over
                Ethernet is encapsulated/received over 802.1q
                VLAN tagged frames.
net/tftp.c +0 -1
index be24e63075..e2c005da6e 100644
@@ -5,7 +5,6 @@
  * Copyright 2011 Comelit Group SpA,
  *                Luca Ceresoli <[email protected]>
  */
-
 #include <common.h>
 #include <command.h>
Quoted 8 lines

net/tftp.c static int tftp_tsize; · line 97

Quoted 6 lines
net/tftp.c +1 -0
 #include <efi_loader.h>
@@ -95,6 +94,12 @@ static int tftp_tsize;
 /* The number of hashes we printed */
 static short tftp_tsize_num_hash;
 #endif
+/* The window size neogiciated */

neogiciated -> negotiated

Typo. fixed. thanks.

Quoted 13 lines
Quoted 13 lines
net/tftp.c +5 -0
+static ushort        tftp_windowsize;
+/* Next block to send ack to */
+static ushort        tftp_next_ack;
+/* Last nack block we send */
+static ushort        tftp_last_nack;
 #ifdef CONFIG_CMD_TFTPPUT
 /* 1 if writing, else 0 */
 static int   tftp_put_active;
@@ -134,8 +139,19 @@ static char tftp_filename[MAX_LEN];
  * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
  */
 
Quoted 8 lines

net/tftp.c static char tftp_filename[MAX_LEN]; · line 146

Quoted 5 lines
net/tftp.c +5 -0
+/* When windowsize is defined to 1,
+ * tftp behaves the same way as it was
+ * never declared
+ */
+#ifdef CONFIG_TFTP_WINDOWSIZE

I'm not sure what's the policy but I think that new CONFIG_ options should be added to a Kconfig file.

Yes, I added a new option in Kconfig. thanks.

Quoted 27 lines
Quoted 27 lines
net/tftp.c +14 -0
+#define TFTP_WINDOWSIZE CONFIG_TFTP_WINDOWSIZE
+#else
+#define TFTP_WINDOWSIZE 1
+#endif
+
 static unsigned short tftp_block_size = TFTP_BLOCK_SIZE;
 static unsigned short tftp_block_size_option = CONFIG_TFTP_BLOCKSIZE;
+static unsigned short tftp_window_size_option = TFTP_WINDOWSIZE;
 
 static inline int store_block(int block, uchar *src, unsigned int len)
 {
@@ -348,6 +364,14 @@ static void tftp_send(void)
              /* try for more effic. blk size */
              pkt += sprintf((char *)pkt, "blksize%c%d%c",
                              0, tftp_block_size_option, 0);
+
+             /* try for more effic. window size.
+              * Implemented only for tftp get.
+              * Don't bother sending if it's 1
+              */
+             if (tftp_state == STATE_SEND_RRQ && tftp_window_size_option > 1)
+                     pkt += sprintf((char *)pkt, "windowsize%c%d%c",
+                                     0, tftp_window_size_option, 0);
              len = pkt - xp;
              break;
 
Quoted 15 lines

net/tftp.c static void tftp_handler(uchar *pkt, unsigned dest, struct in_addr sip, · line 535

Quoted 13 lines
net/tftp.c +9 -0
@@ -500,6 +524,15 @@ static void tftp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
                                    (char *)pkt + i + 6, tftp_tsize);
                      }
 #endif
+                     if (strcmp((char *)pkt + i,  "windowsize") == 0) {
+                             tftp_windowsize =
+                                     simple_strtoul((char *)pkt + i + 11,
+                                                    NULL, 10);
+                         debug("windowsize = %s, %d\n",
+                               (char *)pkt + i + 11, tftp_windowsize);
+                     }
+
+                     tftp_next_ack = tftp_windowsize;

This assignment can be moved outside of the for loop.

Done.

Quoted 19 lines
Quoted 19 lines
net/tftp.c +11 -0
              }
 #ifdef CONFIG_CMD_TFTPPUT
              if (tftp_put_active) {
@@ -514,6 +547,26 @@ static void tftp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
              if (len < 2)
                      return;
              len -= 2;
+
+             if (ntohs(*(__be16 *)pkt) != (ushort)(tftp_cur_block + 1)) {
+                     debug("Received unexpected block: %d, expected: %d\n",
+                           ntohs(*(__be16 *)pkt),
+                           (ushort)(tftp_cur_block + 1));
+                     /*
+                      * If one packet is dropped most likely
+                      * all other buffers in the window
+                      * that will arrive will cause a sending NACK.
+                      * This just overwellms the server, let's just send one.
+                      */
Quoted 8 lines

net/tftp.c static void tftp_handler(uchar *pkt, unsigned dest, struct in_addr sip, · line 565

Quoted 5 lines
net/tftp.c +5 -0
+                     if (tftp_last_nack != tftp_cur_block) {
+                             tftp_send();
+                             tftp_last_nack = tftp_cur_block;
+                             tftp_next_ack = (ushort)(tftp_cur_block +
+                                                      tftp_windowsize);

Here neither tftp_cur_block nor tftp_windowsize were updated, so it shouldn't be necessary to update tftp_next_ack?

tftp_windowsize can't change during data transfer. If we receive unexpected buffer.
We send the last block we got, and set the next ack to the last block
+ tftp_windowsize.

Quoted 6 lines

If the first data packet is lost and we receive data block > 1, we call tftp_send() still in state STATE_SEND_RRQ, which sends again the read request. I would have expected instead to send an ack for block zero to force the server to retransmit block 1; but I guess both ways are ok.

server implementation actually don't respond to NACK packets, to avoid the sorcerers apprentice bug.
The just wait for timeout and re-transmit the last packet they sent.

Quoted 22 lines

net/tftp.c static void tftp_handler(uchar *pkt, unsigned dest, struct in_addr sip, · line 570

Quoted 6 lines
net/tftp.c +4 -0
+                     }
+                     break;
+             }
+
              tftp_cur_block = ntohs(*(__be16 *)pkt);

Perhaps, just increase tftp_cur_block by one, since we have already verified that "ntohs(*(__be16 *)pkt) == (ushort)(tftp_cur_block + 1)" before.

In tftp_handler() there is this debug message:

if (tftp_state == STATE_SEND_RRQ)

     debug("Server did not acknowledge timeout option!\n");

printed when the server doesn't reply to the options. Since multiple options are now supported (not only 'timeout'), the message should be updated (this was already inaccurate before your patch, but it would be the right moment to fix it).

Beniamino