mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Enrico Scholz <enrico.scholz@sigma-chemnitz.de>
To: barebox@lists.infradead.org
Cc: Enrico Scholz <enrico.scholz@sigma-chemnitz.de>
Subject: [PATCH 11/13] tftp: reorder tftp packets
Date: Mon, 18 Jul 2022 14:22:26 +0200	[thread overview]
Message-ID: <14bf205ffe1ba093e93531b8a43758e18c18a94d.1658144543.git.enrico.scholz@sigma-chemnitz.de> (raw)
In-Reply-To: <cover.1658144543.git.enrico.scholz@sigma-chemnitz.de>

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@sigma-chemnitz.de>
---
 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




  parent reply	other threads:[~2022-07-18 12:25 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-18 12:22 [PATCH 00/13] add "windowsize" (RFC 7440) support for tftp Enrico Scholz
2022-07-18 12:22 ` [PATCH 01/13] progress: add close_progress() to display some statistics Enrico Scholz
2022-07-18 12:22 ` [PATCH 02/13] libfile:copy_file: show statistics in verbose mode Enrico Scholz
2022-07-18 12:22 ` [PATCH 03/13] tftp: minor refactoring of RRQ/WRQ packet generation code Enrico Scholz
2022-07-18 12:22 ` [PATCH 04/13] tftp: replace hardcoded blksize by global constant Enrico Scholz
2022-07-18 12:22 ` [PATCH 05/13] tftp: record whether tftp file is opened for lookup operation only Enrico Scholz
2022-07-18 12:22 ` [PATCH 06/13] tftp: reduce block size on lookup requests Enrico Scholz
2022-07-18 12:22 ` [PATCH 07/13] tftp: refactor data processing Enrico Scholz
2022-07-18 12:22 ` [PATCH 08/13] tftp: detect out-of-memory situations Enrico Scholz
2022-07-18 12:22 ` [PATCH 09/13] tftp: implement 'windowsize' (RFC 7440) support Enrico Scholz
2022-07-31 11:36   ` [PATCH v2 " Enrico Scholz
2022-08-09  8:49     ` Sascha Hauer
2022-08-09  9:28       ` Enrico Scholz
2022-08-09  9:52         ` Sascha Hauer
2022-07-18 12:22 ` [PATCH 10/13] tftp: do not use 'priv->block' for RRQ Enrico Scholz
2022-07-18 12:22 ` Enrico Scholz [this message]
2022-08-09  8:58   ` [PATCH 11/13] tftp: reorder tftp packets Sascha Hauer
2022-07-18 12:22 ` [PATCH 12/13] tftp: allow to change tftp port Enrico Scholz
2022-08-09  8:12   ` Sascha Hauer
2022-07-18 12:22 ` [PATCH 13/13] tftp: add sanity check for OACK response Enrico Scholz
2022-07-31 11:36   ` [PATCH v2 " Enrico Scholz
2022-08-09  9:02 ` [PATCH 00/13] add "windowsize" (RFC 7440) support for tftp Sascha Hauer
2022-08-09  9:35   ` Enrico Scholz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=14bf205ffe1ba093e93531b8a43758e18c18a94d.1658144543.git.enrico.scholz@sigma-chemnitz.de \
    --to=enrico.scholz@sigma-chemnitz.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox