[PATCH 11/13] tftp: reorder tftp packets

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



With the tftp "windowsize" option, reordering of udp datagrams becomes
an issue.  Depending on the network topology, this reordering occurs
several times with large tftp transfers and will heavily reduce the
transfer speed.

This patch adds a packet cache so that datagrams can be reassembled in
the correct order.

Because it increases memory usage, it is an Kconfig option.

Signed-off-by: Enrico Scholz <enrico.scholz@xxxxxxxxxxxxxxxxx>
---
 fs/Kconfig |  22 +++++++
 fs/tftp.c  | 165 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 181 insertions(+), 6 deletions(-)

diff --git a/fs/Kconfig b/fs/Kconfig
index 0c4934285942..02aa25d6abf7 100644
--- a/fs/Kconfig
+++ b/fs/Kconfig
@@ -57,6 +57,28 @@ config FS_TFTP_MAX_WINDOW_SIZE
 	  Requires tftp "windowsize" (RFC 7440) support on server side
 	  to have an effect.
 
+config FS_TFTP_REORDER_CACHE_SIZE
+	int
+	prompt "number of out-of-order tftp packets to be cached"
+	depends on FS_TFTP
+	default 16 if FS_TFTP_MAX_WINDOW_SIZE > 16
+	default  0 if FS_TFTP_MAX_WINDOW_SIZE = 1
+        ## TODO: it should be 'FS_TFTP_MAX_WINDOW_SIZE - 1' but this
+        ## is not supported by Kconfig
+	default FS_TFTP_MAX_WINDOW_SIZE
+	range 0 FS_TFTP_MAX_WINDOW_SIZE
+	help
+	  UDP allows reordering of datagrams; with this option,
+	  unexpected tftp packets will be cached and later
+	  reassembled.  This increases stability of the tftp download
+	  with the cost of memory (around 1440 bytes per cache
+	  element).
+
+          A value of 0 disables caching.
+
+	  Requires tftp "windowsize" (RFC 7440) support on server side
+	  to have an effect.
+
 config FS_OMAP4_USBBOOT
 	bool
 	prompt "Filesystem over usb boot"
diff --git a/fs/tftp.c b/fs/tftp.c
index f85136f03e22..2c2ff081be51 100644
--- a/fs/tftp.c
+++ b/fs/tftp.c
@@ -68,6 +68,9 @@
 #define TFTP_MTU_SIZE		1432	/* MTU based block size */
 #define TFTP_MAX_WINDOW_SIZE	CONFIG_FS_TFTP_MAX_WINDOW_SIZE
 
+/* size of cache which deals with udp reordering */
+#define TFTP_WINDOW_CACHE_NUM	CONFIG_FS_TFTP_REORDER_CACHE_SIZE
+
 /* calculate fifo so that it can hold the complete window plus the incoming
    packet.  Add some extra space just in case...  */
 #define TFTP_FIFO_SIZE		(TFTP_MTU_SIZE * TFTP_MAX_WINDOW_SIZE + 2048)
@@ -76,6 +79,15 @@
 
 static int g_tftp_window_size = TFTP_MAX_WINDOW_SIZE / 1;
 
+struct tftp_block {
+	uint16_t id;
+	uint8_t data[TFTP_MTU_SIZE];
+
+	/* len will not exceed TFTP_MTU_SIZE; 14 bits should suffice... */
+	uint16_t len:14;
+	bool valid:1;
+};
+
 struct file_priv {
 	struct net_connection *tftp_con;
 	int push;
@@ -93,12 +105,49 @@ struct file_priv {
 	int blocksize;
 	unsigned int windowsize;
 	bool is_getattr;
+#if TFTP_WINDOW_CACHE_NUM > 0
+	struct tftp_block window_cache[TFTP_WINDOW_CACHE_NUM];
+#endif
 };
 
 struct tftp_priv {
 	IPaddr_t server;
 };
 
+inline static size_t tftp_window_cache_size(struct file_priv const *priv)
+{
+#if TFTP_WINDOW_CACHE_NUM > 0
+	return min_t(unsigned int, priv->windowsize - 1,
+		     ARRAY_SIZE(priv->window_cache));
+#else
+	return 0;
+#endif
+}
+
+inline static struct tftp_block *tftp_window_cache_get(struct file_priv *priv,
+						       size_t idx)
+{
+#if TFTP_WINDOW_CACHE_NUM > 0
+	return &priv->window_cache[idx];
+#else
+	return NULL;
+#endif
+}
+
+static void tftp_window_cache_clear(struct file_priv *priv)
+{
+	size_t i;
+
+	for (i = 0; i < tftp_window_cache_size(priv); ++i) {
+		struct tftp_block *block = tftp_window_cache_get(priv, i);
+
+		if (block->valid)
+			pr_debug("cached item #%d still valid\n", block->id);
+
+		block->valid = false;
+	}
+}
+
 static int tftp_truncate(struct device_d *dev, FILE *f, loff_t size)
 {
 	return 0;
@@ -185,6 +234,7 @@ static int tftp_send(struct file_priv *priv)
 		priv->ack_block += priv->windowsize;
 		pkt = (unsigned char *)s;
 		len = pkt - xp;
+		tftp_window_cache_clear(priv);
 		break;
 	}
 
@@ -294,6 +344,113 @@ static void tftp_put_data(struct file_priv *priv, uint16_t block,
 	}
 }
 
+static bool in_window(uint16_t block, uint16_t start, uint16_t end)
+{
+	return ((start <= block && block <= end) ||
+		(block <= end   && end   <= start));
+}
+
+static int tftp_window_cache_insert(struct file_priv *priv, uint16_t id,
+				    void const *data, size_t len)
+{
+	size_t		i;
+
+	if (len > sizeof tftp_window_cache_get(priv, 0)->data) {
+		pr_err("datagram too large\n");
+		return -ENOMEM;
+	}
+
+	for (i = 0; i < tftp_window_cache_size(priv); ++i) {
+		struct tftp_block *block = tftp_window_cache_get(priv, i);
+
+		if (block->valid && block->id == id) {
+			if (len == block->len &&
+			    memcmp(data, block->data, len) == 0) {
+				return 0;
+			}
+
+			pr_err("cached block #%d differs from actual one\n",
+			       id);
+
+			return -EBADMSG;
+		}
+
+		if (!block->valid) {
+			memcpy(block->data, data, len);
+			block->id = id;
+			block->len = len;
+			block->valid = true;
+
+			return 0;
+		}
+	}
+
+	return -ENOMEM;
+}
+
+static bool tftp_window_cache_apply(struct file_priv *priv)
+{
+	uint16_t exp_id = priv->last_block + 1;
+	bool res = false;
+	size_t i;
+
+	for (i = 0; i < tftp_window_cache_size(priv); ++i) {
+		struct tftp_block *block = tftp_window_cache_get(priv, i);
+
+		/* can be already the case when entering the function, or
+		   caused by tftp_put_data() below */
+		if (priv->state != STATE_RDATA)
+			break;
+
+		if (!block->valid || block->id != exp_id)
+			continue;
+
+		tftp_put_data(priv, block->id, block->data, block->len);
+
+		block->valid = false;
+		exp_id = priv->last_block + 1;
+
+		res = true;
+	}
+
+	return res;
+}
+
+static void tftp_handle_data(struct file_priv *priv, uint16_t block,
+			     void const *data, size_t len)
+{
+	uint16_t exp_block;
+	int rc;
+
+	exp_block = priv->last_block + 1;
+
+	if (exp_block == block) {
+		/* datagram over network is the expected one; put it in the
+		   fifo directly and try to apply cached items then */
+		tftp_timer_reset(priv);
+		tftp_put_data(priv, block, data, len);
+
+		while (tftp_window_cache_apply(priv)) {
+			/* noop */
+		}
+	} else if (!in_window(block, exp_block, priv->ack_block)) {
+		/* completely unexpected and unrelated to actual window;
+		   ignore the packet. */
+		printf("B");
+	} else {
+		/* The 'rc < 0' below happens e.g. when datagrams in the first
+		   part of the transfer window are dropped.
+
+		   TODO: this will usually result in a timeout
+		   (TFTP_RESEND_TIMEOUT).  It should be possible to bypass
+		   this timeout by acknowledging the last packet (e.g. by
+		   doing 'priv->ack_block = priv->last_block' here). */
+		rc = tftp_window_cache_insert(priv, block, data, len);
+		if (rc < 0)
+			printf("M");
+	}
+}
+
 static void tftp_recv(struct file_priv *priv,
 			uint8_t *pkt, unsigned len, uint16_t uh_sport)
 {
@@ -364,6 +521,7 @@ static void tftp_recv(struct file_priv *priv,
 			priv->state = STATE_RDATA;
 			priv->tftp_con->udp->uh_dport = uh_sport;
 			priv->last_block = 0;
+			tftp_window_cache_clear(priv);
 
 			if (block != 1) {	/* Assertion */
 				pr_err("error: First block is not block 1 (%d)\n",
@@ -374,12 +532,7 @@ static void tftp_recv(struct file_priv *priv,
 			}
 		}
 
-		if (block != (uint16_t)(priv->last_block + 1)) {
-			break;
-		}
-
-		tftp_timer_reset(priv);
-		tftp_put_data(priv, block, pkt + 2, len);
+		tftp_handle_data(priv, block, pkt + 2, len);
 
 		break;
 
-- 
2.36.1





[Index of Archives]     [Linux Embedded]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux