add "windowsize" (RFC 7440) support for tftp
Subsystems (2)
cover add "windowsize" (RFC 7440) support for tftp
The tftp "windowsize" greatly improves the performance of tftp transfers. This patchset adds support for it.
The first two patches are a little bit unrelated and enhance the 'cp -v' output by giving information about the transfer speed. They can be dropped if they are unwanted.
I tested the function with an iMX8MP platform in three environments:
- at home over OpenVPN on an ADSL 50 line --> 27x speedup
- 1 Gb/s connection --> 9x speedup
- connection over 100 Mb/s switch --> 4x speedup
In the test, I downloaded variable sized files which were filled from /dev/urandom. E.g.
| :/ global tftp.windowsize=128 | :/ cp -v /mnt/tftp/data-100MiB /tmp/data && sha1sum /tmp/data | [################################################################] 104857600 bytes, 98550375 bytes/s
For slow connection speed, smaller files (1MiB, 4 MiB + 20 MiB) were used.
The numbers (bytes/s) are
| windowsize | VPN | 1 Gb/s | 100 Mb/s | |------------|-----------|------------|------------| | 128 | 3.869.284 | 98.643.085 | 11.434.852 | | 64 | 3.863.581 | 98.550.375 | 11.434.852 | | 48 | 3.431.580 | 94.211.680 | 11.275.010 | | 32 | 2.835.129 | 85.250.081 | 10.985.605 | | 24 | 2.344.858 | 77.787.537 | 10.765.667 | | 16 | 1.734.186 | 67.519.381 | 10.210.087 | | 12 | 1.403.340 | 61.972.576 | 9.915.612 | | 8 | 1.002.462 | 50.852.376 | 9.016.130 | | 6 | 775.573 | 42.781.558 | 8.422.297 | | 4 | 547.845 | 32.066.544 | 6.835.567 | | 3 | 412.987 | 26.526.081 | 6.322.435 | | 2 | 280.987 | 19.120.641 | 5.494.241 | | 1 | 141.699 | 10.431.516 | 2.967.224 | |------------|-----------|------------|------------| | unpatched | 140.587 | 10.553.301 | 2.978.063 |
Enrico Scholz (13):
progress: add close_progress() to display some statistics libfile:copy_file: show statistics in verbose mode
tftp: minor refactoring of RRQ/WRQ packet generation code
tftp: replace hardcoded blksize by global constant
tftp: record whether tftp file is opened for lookup operation only
tftp: reduce block size on lookup requests
tftp: refactor data processing
tftp: detect out-of-memory situations
tftp: implement 'windowsize' (RFC 7440) support
tftp: do not use 'priv->block' for RRQ
tftp: reorder tftp packets
tftp: allow to change tftp port
tftp: add sanity check for OACK response
fs/Kconfig | 36 ++++++ fs/tftp.c | 298 +++++++++++++++++++++++++++++++++++++------- include/progress.h | 1 + lib/libfile.c | 3 + lib/show_progress.c | 25 ++++ 5 files changed, 319 insertions(+), 44 deletions(-)
Signature · 1 line
2.36.1
PATCH 1/13 progress: add close_progress() to display some statistics
With a later patch, this will produce output like
| :/ cp -v /mnt/tftp/tmp /tmp/
| [########...#########] 29138411 bytes, 127413 bytes/s
--- include/progress.h | 1 + lib/show_progress.c | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+)
include/progress.h +1 -0
@@ -16,6 +16,7 @@ void init_progression_bar(loff_t max);* spinner is printed. */ void show_progress (loff_t now); +void close_progress (loff_t total);extern struct notifier_head progress_notifier;
lib/show_progress.c +25 -0
@@ -24,6 +24,7 @@static loff_t printed;static loff_t progress_max;static unsigned spin; +static uint64_t start_tm;void show_progress (loff_t now) {
@@ -47,11 +48,35 @@ void show_progress(loff_t now) } } +void close_progress (loff_t total) +{ +uint64_t now =get_time_ns (); +unsigned long speed; +unsigned long delta; + +if (now <= start_tm) { +pr_crit ("tm mismatch" ); +return ; + } + +if (total <0 ) +return ; + +/* convert to ms and add '1' to avoid div-by-zero */ + delta =div_u64 (now - start_tm,1000000 ) +1 ; + +/* speed is bytes/s */ + speed =div_u64 (total *1000 , delta); + +printf ("] %llu bytes, %lu bytes/s" , (unsigned long long )total, speed); +} +void init_progression_bar (loff_t max) { printed =0 ; progress_max = max; spin =0 ; + start_tm =get_time_ns ();if (progress_max && progress_max !=FILESIZE_MAX )printf ("\t[%*s]\r\t[" ,HASHES_PER_LINE ,"" );else
Signature · 1 line
2.36.1
PATCH 2/13 libfile:copy_file: show statistics in verbose mode
In reply to Enrico Scholz--- lib/libfile.c | 3 +++ 1 file changed, 3 insertions(+)
lib/libfile.c +3 -0
@@ -454,6 +454,9 @@ int copy_file(const char *src, const char *dst, int verbose) } } +if (verbose) +close_progress (total); + ret =0 ; out:if (verbose)
Signature · 1 line
2.36.1
PATCH 3/13 tftp: minor refactoring of RRQ/WRQ packet generation code
In reply to Enrico ScholzHaving 11 printf arguments with lot of them being 0, makes it difficulty to read and extend.
Add some comments and use '\0' for %c.
--- fs/tftp.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
fs/tftp.c +7 -7
@@ -135,13 +135,13 @@ static int tftp_send(struct file_priv *priv)"%lld%c" "blksize%c" "1432" , - priv->filename +1 ,0 , -0 , -0 , -TIMEOUT ,0 , -0 , - priv->filesize,0 , - 0); + priv->filename +1 ,'\0' , +'\0' ,/* "octet" */ +'\0' ,/* "timeout" */ +TIMEOUT ,'\0' , +'\0' ,/* "tsize" */ + priv->filesize,'\0' , + '\0'); /* "blksize" */ pkt++; len = pkt - xp;break ;
Signature · 1 line
2.36.1
PATCH 4/13 tftp: replace hardcoded blksize by global constant
In reply to Enrico Scholz--- fs/tftp.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
fs/tftp.c +4 -2
@@ -64,6 +64,7 @@#define STATE_DONE 8#define TFTP_BLOCK_SIZE 512/* default TFTP block size */ +#define TFTP_MTU_SIZE 1432/* MTU based block size */ #define TFTP_FIFO_SIZE 4096#define TFTP_ERR_RESEND 1
@@ -134,14 +135,15 @@ static int tftp_send(struct file_priv *priv)"tsize%c" "%lld%c" "blksize%c" -"1432" , +"%u" , priv->filename +1 ,'\0' ,'\0' ,/* "octet" */ '\0' ,/* "timeout" */ TIMEOUT ,'\0' ,'\0' ,/* "tsize" */ priv->filesize,'\0' , - '\0'); /* "blksize" */ +'\0' ,/* "blksize" */ + TFTP_MTU_SIZE); pkt++; len = pkt - xp;break ;
Signature · 1 line
2.36.1
PATCH 5/13 tftp: record whether tftp file is opened for lookup operation only
In reply to Enrico ScholzOpening a tftp is done in two steps: at first tftp_lookup() is called to get the filesize and then it is opened again and data are read.
The tftp_lookup() call sends only a RRQ/WRQ, reads then the "tsize" from the response and closes the transfer by sending an error datagram. The tftp server will send a full data window.
To prevent unneeded traffic, later patches set parameters to reduce the size of the server response.
We need knowledge about type of operation which is recorded in an "is_getattr" attribute.
--- fs/tftp.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
fs/tftp.c +5 -3
@@ -84,6 +84,7 @@ struct file_priv {void *buf;int blocksize;int block_requested; +bool is_getattr; };struct tftp_priv {
@@ -372,7 +373,7 @@ static void tftp_handler(void *ctx, char *packet, unsigned len) }static struct file_priv *tftp_do_open (struct device_d *dev, -int accmode,struct dentry *dentry) +int accmode,struct dentry *dentry,bool is_getattr) {struct fs_device_d *fsdev =dev_to_fs_device (dev);struct file_priv *priv;
@@ -400,6 +401,7 @@ static struct file_priv *tftp_do_open(struct device_d *dev, priv->filename =dpath (dentry, fsdev->vfsmount.mnt_root); priv->blocksize =TFTP_BLOCK_SIZE ; priv->block_requested =-1 ; + priv->is_getattr = is_getattr; priv->fifo =kfifo_alloc (TFTP_FIFO_SIZE );if (!priv->fifo) {
@@ -451,7 +453,7 @@ static int tftp_open(struct device_d *dev, FILE *file, const char *filename) {struct file_priv *priv; - priv =tftp_do_open (dev, file->flags, file->dentry); + priv =tftp_do_open (dev, file->flags, file->dentry, false);if (IS_ERR (priv))return PTR_ERR (priv);
@@ -667,7 +669,7 @@ static struct dentry *tftp_lookup(struct inode *dir, struct dentry *dentry,struct file_priv *priv;loff_t filesize; - priv =tftp_do_open (&fsdev->dev,O_RDONLY , dentry); + priv =tftp_do_open (&fsdev->dev,O_RDONLY , dentry, true);if (IS_ERR (priv))return NULL ;
Signature · 1 line
2.36.1
PATCH 6/13 tftp: reduce block size on lookup requests
In reply to Enrico ScholzSave some bytes on network traffic by reducing the server response for lookup requests.
--- fs/tftp.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
fs/tftp.c +3 -1
@@ -144,7 +144,9 @@ static int tftp_send(struct file_priv *priv)'\0' ,/* "tsize" */ priv->filesize,'\0' ,'\0' ,/* "blksize" */ - TFTP_MTU_SIZE); +/* use only a minimal blksize for getattr +operations, */ + priv->is_getattr ? TFTP_BLOCK_SIZE : TFTP_MTU_SIZE); pkt++; len = pkt - xp;break ;
Signature · 1 line
2.36.1
PATCH 7/13 tftp: refactor data processing
In reply to Enrico Scholzmove block handling into dedicated function
--- fs/tftp.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-)
fs/tftp.c +15 -10
@@ -249,6 +249,20 @@ static void tftp_timer_reset(struct file_priv *priv) priv->progress_timeout = priv->resend_timeout =get_time_ns (); } +static void tftp_put_data (struct file_priv *priv,uint16_t block, +void const *pkt,size_t len) +{ + priv->last_block = block; + +kfifo_put (priv->fifo, pkt, len); + +if (len < priv->blocksize) { +tftp_send (priv); + priv->err =0 ; + priv->state =STATE_DONE ; + } +} +static void tftp_recv (struct file_priv *priv,uint8_t *pkt,unsigned len,uint16_t uh_sport) {
@@ -332,17 +346,8 @@ static void tftp_recv(struct file_priv *priv,/* Same block again; ignore it. */ break ; - priv->last_block = priv->block; -tftp_timer_reset (priv); - -kfifo_put (priv->fifo, pkt +2 , len); - -if (len < priv->blocksize) { -tftp_send (priv); - priv->err =0 ; - priv->state =STATE_DONE ; - } +tftp_put_data (priv, priv->block, pkt +2 , len);break ;
Signature · 1 line
2.36.1
PATCH 8/13 tftp: detect out-of-memory situations
In reply to Enrico Scholzit should never happen due to the program logic; but detect a failed kfifo_put() just in case...
--- fs/tftp.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
fs/tftp.c +9 -2
@@ -252,11 +252,18 @@ static void tftp_timer_reset(struct file_priv *priv)static void tftp_put_data (struct file_priv *priv,uint16_t block,void const *pkt,size_t len) { +unsigned int sz; + priv->last_block = block; -kfifo_put (priv->fifo, pkt, len); + sz =kfifo_put (priv->fifo, pkt, len); -if (len < priv->blocksize) { +if (sz != len) { +pr_err ("tftp: not enough room in kfifo (only %u out of %zu written\n" , + sz, len); + priv->err = -ENOMEM ; + priv->state =STATE_DONE ; + }else if (len < priv->blocksize) {tftp_send (priv); priv->err =0 ; priv->state =STATE_DONE ;
Signature · 1 line
2.36.1
PATCH 9/13 tftp: implement 'windowsize' (RFC 7440) support
In reply to Enrico ScholzResults (with the reorder patch; numbers in bytes/s) on an iMX8MP are:
| windowsize | VPN | 1 Gb/s | 100 Mb/s | |------------|-----------|------------|------------| | 128 | 3.869.284 | 98.643.085 | 11.434.852 | | 64 | 3.863.581 | 98.550.375 | 11.434.852 | | 48 | 3.431.580 | 94.211.680 | 11.275.010 | | 32 | 2.835.129 | 85.250.081 | 10.985.605 | | 24 | 2.344.858 | 77.787.537 | 10.765.667 | | 16 | 1.734.186 | 67.519.381 | 10.210.087 | | 12 | 1.403.340 | 61.972.576 | 9.915.612 | | 8 | 1.002.462 | 50.852.376 | 9.016.130 | | 6 | 775.573 | 42.781.558 | 8.422.297 | | 4 | 547.845 | 32.066.544 | 6.835.567 | | 3 | 412.987 | 26.526.081 | 6.322.435 | | 2 | 280.987 | 19.120.641 | 5.494.241 | | 1 | 141.699 | 10.431.516 | 2.967.224 |
(VPN = OpenVPN on ADSL 50 Mb/s).
The window size can be configured at runtime.
--- fs/Kconfig | 14 ++++++++++++++ fs/tftp.c | 54 ++++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 54 insertions(+), 14 deletions(-)
fs/Kconfig +14 -0
@@ -43,6 +43,20 @@ config FS_TFTP prompt "tftp support" depends on NET +config FS_TFTP_MAX_WINDOW_SIZE + int + prompt "maximum tftp window size (RFC 7440)" + depends on FS_TFTP + default 128 + range 1 128 + help + The maximum allowed tftp "windowsize" (RFC 7440). Higher + value increase speed of the tftp download with the cost of + memory (1432 bytes per slot). + + Requires tftp "windowsize" (RFC 7440) support on server side + to have an effect. + config FS_OMAP4_USBBOOT bool prompt "Filesystem over usb boot"
fs/tftp.c +40 -14
158 lines not on this page · raw message
Signature · 1 line
2.36.1
PATCH v2 9/13 tftp: implement 'windowsize' (RFC 7440) support
Results (with the reorder patch; numbers in bytes/s) on an iMX8MP are:
| windowsize | VPN | 1 Gb/s | 100 Mb/s | |------------|-----------|------------|------------| | 128 | 3.869.284 | 98.643.085 | 11.434.852 | | 64 | 3.863.581 | 98.550.375 | 11.434.852 | | 48 | 3.431.580 | 94.211.680 | 11.275.010 | | 32 | 2.835.129 | 85.250.081 | 10.985.605 | | 24 | 2.344.858 | 77.787.537 | 10.765.667 | | 16 | 1.734.186 | 67.519.381 | 10.210.087 | | 12 | 1.403.340 | 61.972.576 | 9.915.612 | | 8 | 1.002.462 | 50.852.376 | 9.016.130 | | 6 | 775.573 | 42.781.558 | 8.422.297 | | 4 | 547.845 | 32.066.544 | 6.835.567 | | 3 | 412.987 | 26.526.081 | 6.322.435 | | 2 | 280.987 | 19.120.641 | 5.494.241 | | 1 | 141.699 | 10.431.516 | 2.967.224 |
(VPN = OpenVPN on ADSL 50 Mb/s).
The window size can be configured at runtime.
--- fs/Kconfig | 14 ++++++++++++++ fs/tftp.c | 51 ++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 54 insertions(+), 11 deletions(-)
fs/Kconfig +14 -0
@@ -43,6 +43,20 @@ config FS_TFTP prompt "tftp support" depends on NET +config FS_TFTP_MAX_WINDOW_SIZE + int + prompt "maximum tftp window size (RFC 7440)" + depends on FS_TFTP + default 128 + range 1 128 + help + The maximum allowed tftp "windowsize" (RFC 7440). Higher + value increase speed of the tftp download with the cost of + memory (1432 bytes per slot). + + Requires tftp "windowsize" (RFC 7440) support on server side + to have an effect. + config FS_OMAP4_USBBOOT bool prompt "Filesystem over usb boot"
fs/tftp.c +40 -11
147 lines not on this page · raw message
Signature · 1 line
2.37.1
Re: [PATCH v2 09/13] tftp: implement 'windowsize' (RFC 7440) support
Quoted 39 lines · Enrico Scholz
Results (with the reorder patch; numbers in bytes/s) on an iMX8MP are:
| windowsize | VPN | 1 Gb/s | 100 Mb/s | |------------|-----------|------------|------------| | 128 | 3.869.284 | 98.643.085 | 11.434.852 | | 64 | 3.863.581 | 98.550.375 | 11.434.852 | | 48 | 3.431.580 | 94.211.680 | 11.275.010 | | 32 | 2.835.129 | 85.250.081 | 10.985.605 | | 24 | 2.344.858 | 77.787.537 | 10.765.667 | | 16 | 1.734.186 | 67.519.381 | 10.210.087 | | 12 | 1.403.340 | 61.972.576 | 9.915.612 | | 8 | 1.002.462 | 50.852.376 | 9.016.130 | | 6 | 775.573 | 42.781.558 | 8.422.297 | | 4 | 547.845 | 32.066.544 | 6.835.567 | | 3 | 412.987 | 26.526.081 | 6.322.435 | | 2 | 280.987 | 19.120.641 | 5.494.241 | | 1 | 141.699 | 10.431.516 | 2.967.224 |(VPN = OpenVPN on ADSL 50 Mb/s).
The window size can be configured at runtime.
Signed-off-by Enrico Scholz <[email protected]>--- fs/Kconfig | 14 ++++++++++++++ fs/tftp.c | 51 ++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 54 insertions(+), 11 deletions(-)fs/Kconfig +3
-0@@ -43,6 +43,20 @@ config FS_TFTP prompt "tftp support" depends on NET +config FS_TFTP_MAX_WINDOW_SIZE + int + prompt "maximum tftp window size (RFC 7440)"
fs/Kconfig config FS_TFTP · line 58
Quoted 10 lines
fs/Kconfig +10
-0+ depends on FS_TFTP + default 128 + range 1 128 + help + The maximum allowed tftp "windowsize" (RFC 7440). Higher + value increase speed of the tftp download with the cost of + memory (1432 bytes per slot). + + Requires tftp "windowsize" (RFC 7440) support on server side + to have an effect.
Can we agree on some sane default here and drop this from Kconfig? Same for FS_TFTP_REORDER_CACHE_SIZE.
Quoted 19 lines
fs/Kconfig +1
-0+ config FS_OMAP4_USBBOOT bool prompt "Filesystem over usb boot"fs/tftp.c +2
-0@@ -29,7 +29,9 @@#include <init.h> #include <linux/stat.h> #include <linux/err.h> +#include <linux/kernel.h> #include <kfifo.h> +#include <globalvar.h> #include <linux/sizes.h> #define TFTP_PORT 69/* Well known TFTP port number */
fs/tftp.c line 74
Quoted 9 lines
fs/tftp.c +5
-1@@ -65,15 +67,22 @@#define TFTP_BLOCK_SIZE 512/* default TFTP block size */ #define TFTP_MTU_SIZE 1432/* MTU based block size */ -#define TFTP_FIFO_SIZE 4096 +#define TFTP_MAX_WINDOW_SIZE CONFIG_FS_TFTP_MAX_WINDOW_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)
Memory should be allocated according to the actual window size, not to the maximum window size. Otherwise I don't see a reason to decrease the window size using global.tftp.windowsize when the memory allocated is always the same.
Sascha
Signature · 4 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
Re: [PATCH v2 09/13] tftp: implement 'windowsize' (RFC 7440) support
Quoted 7 lines · Sascha Hauer
Quoted 7 lines
fs/Kconfig +7
-0+config FS_TFTP_MAX_WINDOW_SIZE + int + prompt "maximum tftp window size (RFC 7440)" + depends on FS_TFTP + default 128 + range 1 128 + help
Quoted 8 lines
fs/Kconfig config FS_TFTP · line 58
Quoted 6 lines
fs/Kconfig +6
-0+ The maximum allowed tftp "windowsize" (RFC 7440). Higher + value increase speed of the tftp download with the cost of + memory (1432 bytes per slot). + + Requires tftp "windowsize" (RFC 7440) support on server side + to have an effect.Can we agree on some sane default here and drop this from
Kconfig?
I think, it is difficult to find a sane default. Processors like iMX8 can deal with large window sizes, but low end ones like iMX6ULL can not handle data fast enough and require smaller sizes (around max 30).
Workloads like writing to slow memory (cp /mnt/tftp/... /dev/nand) might require smaller sizes too. Or networks with high drop rates.
That's why, I think it should be possible to configure it in some _defconfig.
Quoted 6 lines
fs/tftp.c line 74
Quoted 3 lines
fs/tftp.c +3
-0+/* 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)Memory should be allocated according to the actual window size, not to the maximum window size.
ok; I will try it. Moving 'kfifo_alloc()' should be possible in
| static struct file_priv *tftp_do_open(struct device_d *dev, | int accmode, struct dentry *dentry, bool is_getattr) | | priv->fifo = kfifo_alloc(TFTP_FIFO_SIZE); | | ret = tftp_poll(priv);
Quoted 2 lines
Otherwise I don't see a reason to decrease the window size using global.tftp.windowsize when the memory allocated is always the same.
As I said above: some workloads or networks with a high packet drop rate work better with lower window sizes.
Enrco
Re: [PATCH v2 09/13] tftp: implement 'windowsize' (RFC 7440) support
Quoted 22 lines · Enrico Scholz
Quoted 7 lines · Sascha Hauer
Quoted 7 lines
fs/Kconfig +7
-0+config FS_TFTP_MAX_WINDOW_SIZE + int + prompt "maximum tftp window size (RFC 7440)" + depends on FS_TFTP + default 128 + range 1 128 + helpQuoted 8 lines
fs/Kconfig config FS_TFTP · line 58
Quoted 6 lines
fs/Kconfig +6
-0+ The maximum allowed tftp "windowsize" (RFC 7440). Higher + value increase speed of the tftp download with the cost of + memory (1432 bytes per slot). + + Requires tftp "windowsize" (RFC 7440) support on server side + to have an effect.Can we agree on some sane default here and drop this from
Kconfig?I think, it is difficult to find a sane default. Processors like iMX8 can deal with large window sizes, but low end ones like iMX6ULL can not handle data fast enough and require smaller sizes (around max 30).
Quoted 5 lines
Workloads like writing to slow memory (cp
/mnt/tftp/.../dev/nand) might require smaller sizes too. Or networks with high drop rates.That's why, I think it should be possible to configure it in some
_defconfig.
It's exposed to global.tftp.windowsize and with this you can store a good value on the device and you can provide different defaults for different boards in their default environments during compile time. It's much more flexible than Kconfig.
Sascha
Signature · 4 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
PATCH 10/13 tftp: do not use 'priv->block' for RRQ
In reply to Enrico Scholzattribute is not used outside tftp_recv() for RRQ.
--- fs/tftp.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
fs/tftp.c +6 -6
@@ -298,6 +298,7 @@ static void tftp_recv(struct file_priv *priv, uint8_t *pkt, unsigned len, uint16_t uh_sport) { uint16_t opcode; +uint16_t block;/* according to RFC1350 minimal tftp packet length is 4 bytes */ if (len <4 )
@@ -349,7 +350,6 @@ static void tftp_recv(struct file_priv *priv, }else {/* send ACK */ priv->state =STATE_OACK ; - priv->block =0 ; priv->last_block =0 ;tftp_send (priv); }
@@ -357,7 +357,7 @@ static void tftp_recv(struct file_priv *priv,break ;case TFTP_DATA : len -=2 ; - priv->block =ntohs (*(uint16_t *)pkt); + block =ntohs (*(uint16_t *)pkt);if (priv->state ==STATE_RRQ || priv->state ==STATE_OACK ) {/* first block received */
@@ -365,21 +365,21 @@ static void tftp_recv(struct file_priv *priv, priv->tftp_con->udp->uh_dport = uh_sport; priv->last_block =0 ; -if (priv->block !=1 ) {/* Assertion */ +if (block !=1 ) {/* Assertion */ pr_err ("error: First block is not block 1 (%d)\n" , - priv->block); + block); priv->err = -EINVAL ; priv->state =STATE_DONE ;break ; } } -if (priv->block != (uint16_t )(priv->last_block +1 )) { +if (block != (uint16_t )(priv->last_block +1 )) {break ; }tftp_timer_reset (priv); -tftp_put_data (priv, priv->block, pkt +2 , len); +tftp_put_data (priv, block, pkt +2 , len);break ;
Signature · 1 line
2.36.1
PATCH 11/13 tftp: reorder tftp packets
In reply to Enrico ScholzWith 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.
--- fs/Kconfig | 22 +++++++ fs/tftp.c | 165 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 181 insertions(+), 6 deletions(-)
fs/Kconfig +22 -0
@@ -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"
fs/tftp.c +159 -6
213 lines not on this page · raw message
Signature · 1 line
2.36.1
Re: [PATCH 11/13] tftp: reorder tftp packets
Quoted 80 lines · Enrico Scholz
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
Kconfigoption.Signed-off-by Enrico Scholz <[email protected]>--- fs/Kconfig | 22 +++++++ fs/tftp.c | 165 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 181 insertions(+), 6 deletions(-)fs/Kconfig +22
-0@@ -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"fs/tftp.c +12
-0@@ -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;
fs/tftp.c struct file_priv { · line 111
Quoted 7 lines
fs/tftp.c +3
-0@@ -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 };
Can you use a struct list_head here rather than an array? With that you can use list_add_sort() to sort the cached packets by id and it becomes easy to see if the next packet is already there or not without iterating over the cache multiple times. Also by allocating a tftp_block dynamically when you need it you only occupy memory for packets that are received in the wrong order, not for a fixed number of blocks.
Sascha
Signature · 4 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
PATCH 12/13 tftp: allow to change tftp port
In reply to Enrico Scholzuseful e.g. when working with a local, non-privileged tftp server
--- fs/tftp.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
fs/tftp.c +3 -1
@@ -78,6 +78,7 @@#define TFTP_ERR_RESEND 1static int g_tftp_window_size =TFTP_MAX_WINDOW_SIZE /1 ; +static int g_tftp_port =TFTP_PORT ;struct tftp_block {uint16_t id;
@@ -600,7 +601,7 @@ static struct file_priv *tftp_do_open(struct device_d *dev, goto out; } - priv->tftp_con =net_udp_new (tpriv->server,TFTP_PORT , tftp_handler, + priv->tftp_con =net_udp_new (tpriv->server, g_tftp_port, tftp_handler, priv);if (IS_ERR (priv->tftp_con)) { ret =PTR_ERR (priv->tftp_con);
@@ -944,6 +945,7 @@ static struct fs_driver_d tftp_driver = {static int tftp_init (void ) {globalvar_add_simple_int ("tftp.windowsize" , &g_tftp_window_size,"%u" ); +globalvar_add_simple_int ("tftp.port" , &g_tftp_port,"%u" );return register_fs_driver (&tftp_driver); }
Signature · 1 line
2.36.1
Re: [PATCH 12/13] tftp: allow to change tftp port
Quoted 29 lines · Enrico Scholz
useful e.g. when working with a local, non-privileged tftp server
Signed-off-by Enrico Scholz <[email protected]>--- fs/tftp.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)fs/tftp.c +2
-1@@ -78,6 +78,7 @@#define TFTP_ERR_RESEND 1static int g_tftp_window_size =TFTP_MAX_WINDOW_SIZE /1 ; +static int g_tftp_port =TFTP_PORT ;struct tftp_block {uint16_t id;@@ -600,7 +601,7 @@ static struct file_priv *tftp_do_open(struct device_d *dev, goto out; } - priv->tftp_con =net_udp_new (tpriv->server,TFTP_PORT , tftp_handler, + priv->tftp_con =net_udp_new (tpriv->server, g_tftp_port, tftp_handler, priv);if (IS_ERR (priv->tftp_con)) { ret =PTR_ERR (priv->tftp_con);
fs/tftp.c static struct fs_driver_d tftp_driver = { · line 948
Quoted 4 lines
fs/tftp.c +1
-0@@ -944,6 +945,7 @@ static struct fs_driver_d tftp_driver = {static int tftp_init (void ) {globalvar_add_simple_int ("tftp.windowsize" , &g_tftp_window_size,"%u" ); +globalvar_add_simple_int ("tftp.port" , &g_tftp_port,"%u" );
The port is connection specific. I think we should rather pass the port through mount using -o, something like mount -o port=4711 <server> <path>. Grep for "parseopt_hu" to see how this can be done.
Sascha
Signature · 4 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
PATCH 13/13 tftp: add sanity check for OACK response
In reply to Enrico ScholzCatch bad 'blocksize' or 'windowsize' responses from the server.
--- fs/tftp.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-)
fs/tftp.c +15 -4
@@ -290,7 +290,7 @@ static int tftp_poll(struct file_priv *priv)return 0 ; } -static void tftp_parse_oack (struct file_priv *priv,unsigned char *pkt,int len) +static int tftp_parse_oack (struct file_priv *priv,unsigned char *pkt,int len) {unsigned char *opt, *val, *s;
@@ -307,7 +307,7 @@ static void tftp_parse_oack(struct file_priv *priv, unsigned char *pkt, int len) opt = s; val = s +strlen (s) +1 ;if (val > s + len) -return ; +break ;if (!strcmp (opt,"tsize" )) priv->filesize =simple_strtoull (val,NULL ,10 );if (!strcmp (opt,"blksize" ))
@@ -317,6 +317,15 @@ static void tftp_parse_oack(struct file_priv *priv, unsigned char *pkt, int len)pr_debug ("OACK opt: %s val: %s\n" , opt, val); s = val +strlen (val) +1 ; } + +if (priv->blocksize >TFTP_MTU_SIZE || + priv->windowsize >TFTP_MAX_WINDOW_SIZE || + priv->windowsize ==0 ) { +pr_warn ("tftp: invalid oack response" ); +return -EINVAL ; + } + +return 0 ; }static void tftp_timer_reset (struct file_priv *priv)
@@ -498,10 +507,12 @@ static void tftp_recv(struct file_priv *priv,break ;case TFTP_OACK : -tftp_parse_oack (priv, pkt, len); priv->tftp_con->udp->uh_dport = uh_sport; -if (priv->push) { +if (tftp_parse_oack (priv, pkt, len) <0 ) { + priv->err = -EINVAL ; + priv->state =STATE_DONE ; + }else if (priv->push) {/* send first block */ priv->state =STATE_WDATA ; priv->block =1 ;
Signature · 1 line
2.36.1
PATCH v2 13/13 tftp: add sanity check for OACK response
Catch bad 'blocksize' or 'windowsize' responses from the server.
--- fs/tftp.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-)
fs/tftp.c +15 -4
@@ -292,7 +292,7 @@ static int tftp_poll(struct file_priv *priv)return 0 ; } -static void tftp_parse_oack (struct file_priv *priv,unsigned char *pkt,int len) +static int tftp_parse_oack (struct file_priv *priv,unsigned char *pkt,int len) {unsigned char *opt, *val, *s;
@@ -309,7 +309,7 @@ static void tftp_parse_oack(struct file_priv *priv, unsigned char *pkt, int len) opt = s; val = s +strlen (s) +1 ;if (val > s + len) -return ; +break ;if (!strcmp (opt,"tsize" )) priv->filesize =simple_strtoull (val,NULL ,10 );if (!strcmp (opt,"blksize" ))
@@ -319,6 +319,15 @@ static void tftp_parse_oack(struct file_priv *priv, unsigned char *pkt, int len)pr_debug ("OACK opt: %s val: %s\n" , opt, val); s = val +strlen (val) +1 ; } + +if (priv->blocksize >TFTP_MTU_SIZE || + priv->windowsize >TFTP_MAX_WINDOW_SIZE || + priv->windowsize ==0 ) { +pr_warn ("tftp: invalid oack response\n" ); +return -EINVAL ; + } + +return 0 ; }static void tftp_timer_reset (struct file_priv *priv)
@@ -500,10 +509,12 @@ static void tftp_recv(struct file_priv *priv,break ;case TFTP_OACK : -tftp_parse_oack (priv, pkt, len); priv->tftp_con->udp->uh_dport = uh_sport; -if (priv->push) { +if (tftp_parse_oack (priv, pkt, len) <0 ) { + priv->err = -EINVAL ; + priv->state =STATE_DONE ; + }else if (priv->push) {/* send first block */ priv->state =STATE_WDATA ; priv->block =1 ;
Signature · 1 line
2.37.1
Hi Enrico,
Quoted 2 lines · Enrico Scholz
The tftp "windowsize" greatly improves the performance of tftp transfers. This patchset adds support for it.
TCP for the poor, nice ;)
Very nice stuff indeed. I gave it a quick test and TFTP works as before, that's good to start with, but to gain speed improvements I need a RFC7440 capable server on the other end. Which server did you test it with? Have any RFC7440 capable servers made it to the distributions already?
Sascha
Signature · 4 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
Quoted 10 lines · Sascha Hauer
Quoted 2 lines
The tftp "windowsize" greatly improves the performance of tftp transfers. This patchset adds support for it.
TCP for the poor, nice ;)
Very nice stuff indeed. I gave it a quick test and TFTP works as before, that's good to start with, but to gain speed improvements I need a RFC7440 capable server on the other end. Which server did you test it with? Have any RFC7440 capable servers made it to the distributions already?
https://github.com/ensc/r-tftpd ;)
yes... it is problem to implement both server and client side. But the window size feature also works with PXE clients and u-boot, so I think it is correct.
I started to implement it with transparent proxying support in mind (e.g. translate tftp requests to http ones, or fetch cached images with 'rsync').
But when I saw the effects of windows size support, I focused on that.
Enrico