add "windowsize" (RFC 7440) support for tftp
Subsystems (2)
v4 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 speeds, 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 |
Patchset has been tested with
| for i in data-0 data-100B data-1KiB data-1432B data-64KiB data-1MiB data-4MiB; do | tftp "$i" | tftp -p "$i" | done
against tftp servers with and without rfc 2747 support (OACK).
The window size related parts of the patchset (with deactivated selftest) increase the barebox binary size by
| add/remove: 6/0 grow/shrink: 7/2 up/down: 1572/-32 (1540) | Function old new delta | tftp_handler 756 1324 +568 | tftp_allocate_transfer - 196 +196 | tftp_put_data - 184 +184 | tftp_window_cache_remove - 124 +124 | tftp_window_cache_get_pos - 120 +120 | tftp_send 296 412 +116 | tftp_do_open 428 512 +84 | tftp_states - 72 +72 | tftp_do_close 260 312 +52 | tftp_init 16 60 +44 | tftp_open 64 68 +4 | tftp_lookup 136 140 +4 | g_tftp_window_size - 4 +4 | tftp_read 180 164 -16 | tftp_poll 180 164 -16 | Total: Before=629556, After=631096, chg +0.24%
Turning of the datagram cache (CONFIG_FS_TFTP_REORDER_CACHE_SIZE=0) reduces the overhead to
| add/remove: 3/0 grow/shrink: 6/2 up/down: 808/-32 (776) | Function old new delta | tftp_handler 756 1092 +336 | tftp_allocate_transfer - 144 +144 | tftp_send 296 412 +116 | tftp_do_open 428 512 +84 | tftp_states - 72 +72 | tftp_init 16 60 +44 | tftp_open 64 68 +4 | tftp_lookup 136 140 +4 | g_tftp_window_size - 4 +4 | tftp_read 180 164 -16 | tftp_poll 180 164 -16 | Total: Before=629556, After=630332, chg +0.12%
Restoring the old behaviour by CONFIG_FS_TFTP_MAX_WINDOW_SIZE=1 shows an overhead of
| add/remove: 3/0 grow/shrink: 6/2 up/down: 720/-32 (688) | Function old new delta | tftp_handler 756 1088 +332 | tftp_allocate_transfer - 144 +144 | tftp_do_open 428 512 +84 | tftp_states - 72 +72 | tftp_init 16 60 +44 | tftp_send 296 328 +32 | tftp_open 64 68 +4 | tftp_lookup 136 140 +4 | g_tftp_window_size - 4 +4 | tftp_read 180 164 -16 | tftp_poll 180 164 -16 | Total: Before=629556, After=630244, chg +0.11%
Enrico Scholz (21):
tftp: add some 'const' annotations
tftp: allow to change tftp port
cmd:tftp: add '-P' option to set tftp server port number
tftp: do not set 'tsize' in WRQ requests
tftp: assign 'priv->block' later in WRQ
tftp: minor refactoring of RRQ/WRQ packet generation code
tftp: replace hardcoded blksize by global constant
tftp: remove sanity check of first block
tftp: add debug_assert() macro
tftp: allocate buffers and fifo dynamically
tftp: add sanity check for OACK response
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: add selftest
tftp: accept OACK + DATA datagrams only in certain states
tftp: add some documentation about windowsize support
Documentation/filesystems/tftp.rst | 38 ++ commands/tftp.c | 22 +- fs/Kconfig | 36 ++ fs/tftp-selftest.h | 56 +++ fs/tftp.c | 763 +++++++++++++++++++++++++---- test/self/Kconfig | 7 + 6 files changed, 824 insertions(+), 98 deletions(-) create mode 100644 fs/tftp-selftest.h
---
v3 -> v4
- fix operation with non rfc 2347 servers
- do not send 'tsize' in WRQ requests
- add more sanity checks
- add some documentation
v2 -> v3
- use "port=XX" mount options instead of global 'tftp.port' variable
- allocate fifo and send buffer dynamically based on block- and window size of the transfer. Do not use fixed constants anymore
- rewritten cache code; use bitmap based functions with O(1) complexity instead of iterating over (small) arrays
- unittest for cache functions
- add information about binary sizes
v1 -> v2
- fixes for non rfc7440 servers
Signature · 1 line
2.37.2
PATCH v4 1/21 tftp: add some 'const' annotations
--- fs/tftp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/tftp.c +1 -1
@@ -94,7 +94,7 @@ static int tftp_truncate(struct device_d *dev, FILE *f, loff_t size)return 0 ; } -static char *tftp_states[] = { +static char const *const tftp_states[] = { [STATE_INVALID ] ="INVALID" , [STATE_RRQ ] ="RRQ" , [STATE_WRQ ] ="WRQ" ,
Signature · 1 line
2.37.2
PATCH v4 2/21 tftp: allow to change tftp port
In reply to Enrico ScholzThis adds a 'port=<port>' mount option for tftp filesystems.
Useful e.g. when working with a local, non-privileged tftp server
--- fs/tftp.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
fs/tftp.c +5 -2
@@ -30,6 +30,7 @@#include <linux/stat.h> #include <linux/err.h> #include <kfifo.h> +#include <parseopt.h> #include <linux/sizes.h> #define TFTP_PORT 69/* Well known TFTP port number */
@@ -376,6 +377,7 @@ static struct file_priv *tftp_do_open(struct device_d *dev,struct file_priv *priv;struct tftp_priv *tpriv = dev->priv;int ret; +unsigned short port =TFTP_PORT ; priv =xzalloc (sizeof (*priv));
@@ -405,8 +407,9 @@ 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); +parseopt_hu (fsdev->options,"port" , &port); + + priv->tftp_con =net_udp_new (tpriv->server, port, tftp_handler, priv);if (IS_ERR (priv->tftp_con)) { ret =PTR_ERR (priv->tftp_con); goto out1;
Signature · 1 line
2.37.2
PATCH v4 3/21 cmd:tftp: add '-P' option to set tftp server port number
In reply to Enrico Scholz--- commands/tftp.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-)
commands/tftp.c +19 -3
@@ -21,15 +21,24 @@ static int do_tftpb(int argc, char *argv[])char *source, *dest, *freep;int opt;int tftp_push =0 ; +int port =-1 ;int ret;IPaddr_t ip;char ip4_str[sizeof ("255.255.255.255" )]; +char mount_opts[sizeof ("port=12345" )]; -while ((opt =getopt (argc, argv,"p" )) >0 ) { +while ((opt =getopt (argc, argv,"pP:" )) >0 ) {switch (opt) {case 'p' : tftp_push =1 ;break ; +case 'P' : + port =simple_strtoul (optarg,NULL ,0 ); +if (port <=0 || port >0xffff ) { +pr_err ("invalid port '%s'\n" , optarg); +return COMMAND_ERROR_USAGE ; + } +break ;default :return COMMAND_ERROR_USAGE ; }
@@ -59,7 +68,13 @@ static int do_tftpb(int argc, char *argv[]) ip =net_get_serverip ();sprintf (ip4_str,"%pI4" , &ip); - ret =mount (ip4_str,"tftp" ,TFTP_MOUNT_PATH ,NULL ); + +if (port >=0 ) +sprintf (mount_opts,"port=%u" , port); +else + mount_opts[0 ] ='\0' ; + + ret =mount (ip4_str,"tftp" ,TFTP_MOUNT_PATH , mount_opts);if (ret) goto err_rmdir;
@@ -84,12 +99,13 @@ BAREBOX_CMD_HELP_TEXT("server address is taken from the environment (ethX.server BAREBOX_CMD_HELP_TEXT("") BAREBOX_CMD_HELP_TEXT("Options:") BAREBOX_CMD_HELP_OPT ("-p", "push to TFTP server") +BAREBOX_CMD_HELP_OPT ("-P PORT", "tftp server port number") BAREBOX_CMD_HELP_ENDBAREBOX_CMD_START (tftp) .cmd = do_tftpb, BAREBOX_CMD_DESC("load (or save) a file using TFTP") - BAREBOX_CMD_OPTS("[-p] SOURCE [DEST]") + BAREBOX_CMD_OPTS("[-p] [-P <port>] SOURCE [DEST]") BAREBOX_CMD_GROUP(CMD_GRP_NET) BAREBOX_CMD_HELP(cmd_tftp_help) BAREBOX_CMD_END
Signature · 1 line
2.37.2
PATCH v4 4/21 tftp: do not set 'tsize' in WRQ requests
In reply to Enrico ScholzThe filesize is not known for push requests and barebox always sent '0'. Server might reject data because it will always exceed this length.
Send this option only for RRQ requests.
--- fs/tftp.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
fs/tftp.c +9 -4
@@ -132,18 +132,23 @@ static int tftp_send(struct file_priv *priv)"octet%c" "timeout%c" "%d%c" -"tsize%c" -"%lld%c" "blksize%c" "1432" , priv->filename +1 ,0 ,0 ,0 ,TIMEOUT ,0 , -0 , - priv->filesize,0 , 0); pkt++; + +if (!priv->push) +/* we do not know the filesize in WRQ requests and +'priv->filesize' will always be zero */ + pkt +=sprintf ((unsigned char *)pkt, +"tsize%c%lld%c" , +'\0' , priv->filesize, +'\0' ); + len = pkt - xp;break ;
Signature · 1 line
2.37.2
PATCH v4 5/21 tftp: assign 'priv->block' later in WRQ
In reply to Enrico ScholzSome refactoring; makes next patches cleaner.
--- fs/tftp.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
fs/tftp.c +5 -5
@@ -254,6 +254,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 )
@@ -276,14 +277,13 @@ static void tftp_recv(struct file_priv *priv,if (!priv->push)break ; - priv->block =ntohs (*(uint16_t *)pkt); -if (priv->block != priv->last_block) { -pr_vdebug ("ack %d != %d\n" , priv->block, priv->last_block); + block =ntohs (*(uint16_t *)pkt); +if (block != priv->last_block) { +pr_vdebug ("ack %d != %d\n" , block, priv->last_block);break ; } - priv->block++; - + priv->block = block +1 ;tftp_timer_reset (priv);if (priv->state ==STATE_LAST ) {
Signature · 1 line
2.37.2
PATCH v4 6/21 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 | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
fs/tftp.c +5 -5
@@ -134,11 +134,11 @@ static int tftp_send(struct file_priv *priv)"%d%c" "blksize%c" "1432" , - priv->filename +1 ,0 , -0 , -0 , -TIMEOUT ,0 , - 0); + priv->filename +1 ,'\0' , +'\0' ,/* "octet" */ +'\0' ,/* "timeout" */ +TIMEOUT ,'\0' , + '\0'); /* "blksize" */ pkt++;if (!priv->push)
Signature · 1 line
2.37.2
PATCH v4 7/21 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
@@ -65,6 +65,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
@@ -133,12 +134,13 @@ static int tftp_send(struct file_priv *priv)"timeout%c" "%d%c" "blksize%c" -"1432" , +"%u" , priv->filename +1 ,'\0' ,'\0' ,/* "octet" */ '\0' ,/* "timeout" */ TIMEOUT ,'\0' , - '\0'); /* "blksize" */ +'\0' ,/* "blksize" */ + TFTP_MTU_SIZE); pkt++;if (!priv->push)
Signature · 1 line
2.37.2
PATCH v4 8/21 tftp: remove sanity check of first block
In reply to Enrico ScholzWith tftp window size support in the next patches, the first received block might be !=1 (e.g. when it was reordered or dropped). There could be checked whether it is in the first window, but the corresponding sanity check can be dropped completely:
- OACK logic verifies that we speak with a tftp server (which always sends block #1 as the first one). Diagnostic will help only with non rfc 2743 servers (which are probably very rare resp. non existing nowadays)
- the code some lines later handles this case already
Remove the check and simplify things.
--- fs/tftp.c | 8 -------- 1 file changed, 8 deletions(-)
fs/tftp.c +0 -8
@@ -321,14 +321,6 @@ static void tftp_recv(struct file_priv *priv, priv->state =STATE_RDATA ; priv->tftp_con->udp->uh_dport = uh_sport; priv->last_block =0 ; - -if (priv->block !=1 ) {/* Assertion */ -pr_err ("error: First block is not block 1 (%d)\n" , - priv->block); - priv->err = -EINVAL ; - priv->state =STATE_DONE ; -break ; - } }if (priv->block == priv->last_block)
Signature · 1 line
2.37.2
PATCH v4 9/21 tftp: add debug_assert() macro
In reply to Enrico ScholzIs a noop in normal cases (when compiler sees that the condition can be evaluated without sideeffects) but allows optimizations based on the condition.
E.g. in
| void foo(int a)
| {
| debug_assert(a == 23);
|
| if (a == 23)
| return;
|
| bar();
| }the call to 'bar()' will be optimized away.
--- fs/tftp.c | 9 +++++++++ 1 file changed, 9 insertions(+)
fs/tftp.c +9 -0
@@ -70,6 +70,15 @@#define TFTP_ERR_RESEND 1 +#ifdef DEBUG +# define debug_assert (_cond) BUG_ON(!(_cond)) +#else +# define debug_assert (_cond) do { \ + if (!(_cond)) \ + __builtin_unreachable(); \ + } while (0) +#endif +struct file_priv {struct net_connection *tftp_con;int push;
Signature · 1 line
2.37.2
PATCH v4 10/21 tftp: allocate buffers and fifo dynamically
In reply to Enrico ScholzUse the actual blocksize for allocating buffers instead of assuming an hardcoded value.
This requires to add an additional 'START' state which is entered after receiving (RRQ) or sending (WRQ) the OACK. Without it, the next state would be entered and the (not allocated yet) fifo be used.
For non-rfc 2347 servers (which do not understand OACK and start with data transfer immediately after RRQ/WRQ), additional transitions in the state machine were implemented.
Code adds some sanity checks in the new code paths.
--- fs/tftp.c | 194 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 144 insertions(+), 50 deletions(-)
fs/tftp.c +144 -50
@@ -59,14 +59,15 @@#define STATE_WRQ 2#define STATE_RDATA 3#define STATE_WDATA 4 -#define STATE_OACK 5 +/* OACK from server has been received and we can begin to sent either the ACK +(for RRQ) or data (for WRQ) */ +#define STATE_START 5#define STATE_WAITACK 6#define STATE_LAST 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
@@ -111,10 +112,10 @@ static char const * const tftp_states[] = { [STATE_WRQ] = "WRQ", [STATE_RDATA] = "RDATA", [STATE_WDATA] = "WDATA", - [STATE_OACK] = "OACK", [STATE_WAITACK] = "WAITACK", [STATE_LAST] = "LAST", [STATE_DONE] = "DONE", + [STATE_START] = "START", };static int tftp_send (struct file_priv *priv)
@@ -166,7 +167,6 @@ static int tftp_send(struct file_priv *priv)case STATE_RDATA :if (priv->block == priv->block_requested)return 0 ; -case STATE_OACK : xp = pkt; s = (uint16_t *)pkt; *s++ =htons (TFTP_ACK );
@@ -261,11 +261,39 @@ static void tftp_timer_reset(struct file_priv *priv) priv->progress_timeout = priv->resend_timeout =get_time_ns (); } +static int tftp_allocate_transfer (struct file_priv *priv) +{ +debug_assert (!priv->fifo); +debug_assert (!priv->buf); + + priv->fifo =kfifo_alloc (priv->blocksize); +if (!priv->fifo) + goto err; + +if (priv->push) { + priv->buf =xmalloc (priv->blocksize); +if (!priv->buf) { +kfifo_free (priv->fifo); + priv->fifo =NULL ; + goto err; + } + } + +return 0 ; + +err: + priv->err = -ENOMEM ; + priv->state =STATE_DONE ; + +return priv->err; +} +static void tftp_recv (struct file_priv *priv,uint8_t *pkt,unsigned len,uint16_t uh_sport) {uint16_t opcode;uint16_t block; +int rc;/* according to RFC1350 minimal tftp packet length is 4 bytes */ if (len <4 )
@@ -294,48 +322,64 @@ static void tftp_recv(struct file_priv *priv,break ; } - priv->block = block +1 ; -tftp_timer_reset (priv); +switch (priv->state) { +case STATE_WRQ : + priv->tftp_con->udp->uh_dport = uh_sport; + priv->state =STATE_START ; +break ; -if (priv->state ==STATE_LAST ) { +case STATE_WAITACK : + priv->state =STATE_WDATA ; +break ; + +case STATE_LAST : priv->state =STATE_DONE ;break ; + +default : +pr_warn ("ACK packet in %s state\n" , + tftp_states[priv->state]); + goto ack_out; } - priv->tftp_con->udp->uh_dport = uh_sport; - priv->state =STATE_WDATA ; + + priv->block = block +1 ; +tftp_timer_reset (priv); + + ack_out:break ;case TFTP_OACK :tftp_parse_oack (priv, pkt, len); priv->tftp_con->udp->uh_dport = uh_sport; - -if (priv->push) { -/* send first block */ - priv->state =STATE_WDATA ; - priv->block =1 ; - }else { -/* send ACK */ - priv->state =STATE_OACK ; - priv->block =0 ; -tftp_send (priv); - } - + priv->state =STATE_START ;break ; +case TFTP_DATA : len -=2 ; priv->block =ntohs (*(uint16_t *)pkt); -if (priv->state ==STATE_RRQ || priv->state ==STATE_OACK ) { -/* first block received */ - priv->state =STATE_RDATA ; +if (priv->state ==STATE_RRQ ) { +/* first block received; entered only with non rfc +2347 (TFTP Option extension) compliant servers */ priv->tftp_con->udp->uh_dport = uh_sport; + priv->state =STATE_RDATA ; priv->last_block =0 ; + + rc =tftp_allocate_transfer (priv); +if (rc <0 ) +break ; }if (priv->block == priv->last_block)/* Same block again; ignore it. */ break ; +if (len > priv->blocksize) { +pr_warn ("tftp: oversized packet (%u > %d) received\n" , + len, priv->blocksize); +break ; + } + priv->last_block = priv->block;tftp_timer_reset (priv);
@@ -378,6 +422,30 @@ static void tftp_handler(void *ctx, char *packet, unsigned len)tftp_recv (priv, pkt,net_eth_to_udplen (packet), udp->uh_sport); } +static int tftp_start_transfer (struct file_priv *priv) +{ +int rc; + + rc =tftp_allocate_transfer (priv); +if (rc <0 ) +/* function sets 'priv->state = STATE_DONE' and 'priv->err' in +error case */ +return rc; + +if (priv->push) { +/* send first block */ + priv->state =STATE_WDATA ; + priv->block =1 ; + }else { +/* send ACK */ + priv->state =STATE_RDATA ; + priv->block =0 ; +tftp_send (priv); + } + +return 0 ; +} +static struct file_priv *tftp_do_open (struct device_d *dev,int accmode,struct dentry *dentry) {
@@ -409,48 +477,74 @@ static struct file_priv *tftp_do_open(struct device_d *dev, priv->blocksize =TFTP_BLOCK_SIZE ; priv->block_requested =-1 ; - priv->fifo =kfifo_alloc (TFTP_FIFO_SIZE ); -if (!priv->fifo) { - ret = -ENOMEM ; - goto out; - } -parseopt_hu (fsdev->options,"port" , &port); priv->tftp_con =net_udp_new (tpriv->server, port, tftp_handler, priv);if (IS_ERR (priv->tftp_con)) { ret =PTR_ERR (priv->tftp_con); - goto out1; + goto out; } ret =tftp_send (priv);if (ret) - goto out2; + goto out1;tftp_timer_reset (priv); -while (priv->state !=STATE_RDATA && - priv->state !=STATE_DONE && - priv->state !=STATE_WDATA ) { - ret =tftp_poll (priv); -if (ret ==TFTP_ERR_RESEND ) -tftp_send (priv); -if (ret <0 ) - goto out2; - } -if (priv->state ==STATE_DONE && priv->err) { - ret = priv->err; - goto out2; - } +/* - 'ret < 0' ... error +- 'ret == 0' ... further tftp_poll() required +- 'ret == 1' ... startup finished */ +do { +switch (priv->state) { +case STATE_DONE : +/* branch is entered in two situations: +- non rfc 2347 compliant servers finished the +transfer by sending a small file +- some error occurred */ +if (priv->err <0 ) + ret = priv->err; +else + ret =1 ; +break ; - priv->buf =xmalloc (priv->blocksize); +case STATE_START : + ret =tftp_start_transfer (priv); +if (!ret) + ret =1 ; +break ; + +case STATE_RDATA : +/* first data block of non rfc 2347 servers */ + ret =1 ; +break ; + +case STATE_RRQ : +case STATE_WRQ : + ret =tftp_poll (priv); +if (ret ==TFTP_ERR_RESEND ) { +tftp_send (priv); + ret =0 ; + } +break ; + +default : +debug_assert (false); +break ; + } + }while (ret ==0 ); + +if (ret <0 ) + goto out1;return priv; -out2: -net_unregister (priv->tftp_con); out1: -kfifo_free (priv->fifo); +net_unregister (priv->tftp_con); out: +if (priv->fifo) +kfifo_free (priv->fifo); + +free (priv->filename); +free (priv->buf);free (priv);return ERR_PTR (ret);
@@ -567,7 +661,7 @@ static int tftp_read(struct device_d *dev, FILE *f, void *buf, size_t insize)if (priv->state ==STATE_DONE )return outsize; -if (TFTP_FIFO_SIZE -kfifo_len (priv->fifo) >= priv->blocksize) +if (kfifo_len (priv->fifo) ==0 )tftp_send (priv); ret =tftp_poll (priv);
Signature · 1 line
2.37.2
PATCH v4 11/21 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, 16 insertions(+), 3 deletions(-)
fs/tftp.c +16 -3
@@ -229,7 +229,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;
@@ -246,7 +246,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" ))
@@ -254,6 +254,13 @@ 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 ) { +pr_warn ("tftp: invalid oack response\n" ); +return -EINVAL ; + } + +return 0 ; }static void tftp_timer_reset (struct file_priv *priv)
@@ -349,8 +356,14 @@ 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 (tftp_parse_oack (priv, pkt, len) <0 ) { + priv->err = -EINVAL ; + priv->state =STATE_DONE ; +break ; + } + priv->state =STATE_START ;break ;
Signature · 1 line
2.37.2
PATCH v4 12/21 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
@@ -95,6 +95,7 @@ struct file_priv {void *buf;int blocksize;int block_requested; +bool is_getattr; };struct tftp_priv {
@@ -460,7 +461,7 @@ static int tftp_start_transfer(struct file_priv *priv) }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;
@@ -489,6 +490,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;parseopt_hu (fsdev->options,"port" , &port);
@@ -567,7 +569,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);
@@ -783,7 +785,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.37.2
PATCH v4 13/21 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
@@ -151,7 +151,9 @@ static int tftp_send(struct file_priv *priv)'\0' ,/* "timeout" */ TIMEOUT ,'\0' ,'\0' ,/* "blksize" */ - TFTP_MTU_SIZE); +/* use only a minimal blksize for getattr +operations, */ + priv->is_getattr ? TFTP_BLOCK_SIZE : TFTP_MTU_SIZE); pkt++;if (!priv->push)
Signature · 1 line
2.37.2
PATCH v4 14/21 tftp: refactor data processing
In reply to Enrico Scholzmove block handling into dedicated function
--- fs/tftp.c | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-)
fs/tftp.c +21 -16
@@ -298,6 +298,26 @@ err:return priv->err; } +static void tftp_put_data (struct file_priv *priv,uint16_t block, +void const *pkt,size_t len) +{ +if (len > priv->blocksize) { +pr_warn ("tftp: oversized packet (%zu > %d) received\n" , + len, priv->blocksize); +return ; + } + + 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) {
@@ -390,23 +410,8 @@ static void tftp_recv(struct file_priv *priv,/* Same block again; ignore it. */ break ; -if (len > priv->blocksize) { -pr_warn ("tftp: oversized packet (%u > %d) received\n" , - len, priv->blocksize); -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.37.2
PATCH v4 15/21 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
@@ -301,6 +301,8 @@ err:static void tftp_put_data (struct file_priv *priv,uint16_t block,void const *pkt,size_t len) { +unsigned int sz; +if (len > priv->blocksize) {pr_warn ("tftp: oversized packet (%zu > %d) received\n" , len, priv->blocksize);
@@ -309,9 +311,14 @@ static void tftp_put_data(struct file_priv *priv, uint16_t block, 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.37.2
PATCH v4 16/21 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.
This commit increases barebox size by
| add/remove: 1/0 grow/shrink: 4/1 up/down: 148/-16 (132) | Function old new delta | tftp_send 336 384 +48 | tftp_handler 880 928 +48 | tftp_init 16 60 +44 | tftp_allocate_transfer 100 104 +4 | g_tftp_window_size - 4 +4 | tftp_poll 180 164 -16 | Total: Before=629980, After=630112, chg +0.02%
Setting FS_TFTP_MAX_WINDOW_SIZE to zero reduces it to
| add/remove: 1/0 grow/shrink: 3/2 up/down: 96/-52 (44) | Function old new delta | tftp_init 16 60 +44 | tftp_handler 880 924 +44 | tftp_allocate_transfer 100 104 +4 | g_tftp_window_size - 4 +4 | tftp_poll 180 164 -16 | tftp_send 336 300 -36 | Total: Before=629980, After=630024, chg +0.01%
--- fs/Kconfig | 14 ++++++++++++++ fs/tftp.c | 54 +++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 55 insertions(+), 13 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 +41 -13
169 lines not on this page · raw message
Signature · 1 line
2.37.2
PATCH v4 17/21 tftp: do not use 'priv->block' for RRQ
In reply to Enrico Scholzattribute is not used outside tftp_recv() for RRQ.
--- fs/tftp.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
fs/tftp.c +3 -3
@@ -425,7 +425,7 @@ static void tftp_recv(struct file_priv *priv,case TFTP_DATA : len -=2 ; - priv->block =ntohs (*(uint16_t *)pkt); + block =ntohs (*(uint16_t *)pkt);if (priv->state ==STATE_RRQ ) {/* first block received; entered only with non rfc
@@ -440,11 +440,11 @@ static void tftp_recv(struct file_priv *priv,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.37.2
PATCH v4 18/21 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 and barebox binary size, it is an Kconfig option.
bloat-o-meter reports with a non-zero FS_TFTP_REORDER_CACHE_SIZE
| add/remove: 3/0 grow/shrink: 4/0 up/down: 916/0 (916) | Function old new delta | tftp_handler 920 1244 +324 | tftp_put_data - 184 +184 | tftp_window_cache_remove - 124 +124 | tftp_window_cache_get_pos - 120 +120 | tftp_allocate_transfer 104 188 +84 | tftp_do_close 260 312 +52 | tftp_send 384 412 +28 | Total: Before=630104, After=631020, chg +0.15%
After setting FS_TFTP_REORDER_CACHE_SIZE Kconfig option to 0, numbers are going down to
| add/remove: 0/0 grow/shrink: 3/0 up/down: 152/0 (152) | Function old new delta | tftp_handler 920 1012 +92 | tftp_allocate_transfer 104 136 +32 | tftp_send 384 412 +28 | Total: Before=630104, After=630256, chg +0.02%
--- fs/Kconfig | 22 ++++ fs/tftp.c | 307 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 323 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) and barebox binary size (around 700 bytes). + + 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 +301 -6
367 lines not on this page · raw message
Signature · 1 line
2.37.2
PATCH v4 19/21 tftp: add selftest
In reply to Enrico ScholzUnittest for window cache functions.
--- fs/tftp-selftest.h | 56 ++++++++++++++++++++++++ fs/tftp.c | 106 ++++++++++++++++++++++++++++++++++++++++++++- test/self/Kconfig | 7 +++ 3 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 fs/tftp-selftest.h
fs/tftp-selftest.h new file +56 -0
@@ -0,0 +1,56 @@ +// SPDX-License-Identifier: GPL-2.0+ +// SPDX-FileCopyrightText: 2022 Enrico Scholz <[email protected]> + +#ifndef H_BAREBOX_FS_TFTP_SELFTEST_H +#define H_BAREBOX_FS_TFTP_SELFTEST_H + +#include <bselftest.h> + +#define _expect_fmt (_v) _Generic(_v, \ + void const *: "%p", \ + void *: "%p", \ + bool: "%d", \ + long int: "%lld", \ + long unsigned int: "%llu", \ + unsigned short: "%h", \ + signed int: "%d", \ + unsigned int: "%u") + +#define _expect_op (_a, _b, _op) \ + ({ \ + __typeof__(_a) __a = (_a); \ + __typeof__(_b) __b = (_b); \ + \ + total_tests++; \ + \ + if (!(__a _op __b)) { \ + char fmt[sizeof "%s:%d: failed: %XXX %XXX\n" # _op]; \ + strcpy(fmt, "%s:%d: failed: "); \ + strcat(fmt, _expect_fmt(__a)); \ + strcat(fmt, " " # _op " "); \ + strcat(fmt, _expect_fmt(__b)); \ + strcat(fmt, "\n"); \ + \ + failed_tests++; \ + printf(fmt, __func__, __LINE__, __a, __b); \ + } \ + }) + +#define _as_void (_p) ({ \ + void const *__res = (_p); \ + __res; \ + }) + +#define expect_eq (_a, _b) _expect_op(_a, _b, ==); +#define expect_ne (_a, _b) _expect_op(_a, _b, !=); +#define expect_it (_a) _expect_op(_a, true, ==); + +#define expect_err (_a) _expect_op(_a, 0, <); +#define expect_ok (_a) _expect_op(_a, 0, ==); + +/* _Generic() does not match 'void *' for typed pointers; cast them to raw +'void *' first */ +#define expect_NULL (_a) _expect_op(_as_void(_a), NULL, ==); +#define expect_PTR (_a) _expect_op(_as_void(_a), NULL, !=); + +#endif /* H_BAREBOX_FS_TFTP_SELFTEST_H */
fs/tftp.c +105 -1
121 lines not on this page · raw message
test/self/Kconfig +7 -0
@@ -29,6 +29,7 @@ config SELFTEST_ENABLE_ALL bool "Enable all self-tests" select SELFTEST_PRINTF select SELFTEST_PROGRESS_NOTIFIER + imply SELFTEST_TFTP help Selects all self-tests compatible with current configuration
@@ -51,4 +52,10 @@ config SELFTEST_OF_MANIPULATION config SELFTEST_PROGRESS_NOTIFIER bool "progress notifier selftest" +config SELFTEST_TFTP + bool "tftp selftest" + depends on FS_TFTP + help + Tests tftp functionality + endif
Signature · 1 line
2.37.2
PATCH v4 20/21 tftp: accept OACK + DATA datagrams only in certain states
In reply to Enrico ScholzThese packets are valid in certain points of the transfer only and accepting them too early or too late can corrupt internal states.
Reject them when they are unexpected.
--- fs/tftp.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
fs/tftp.c +12 -0
@@ -713,6 +713,12 @@ static void tftp_recv(struct file_priv *priv,break ;case TFTP_OACK : +if (priv->state !=STATE_RRQ && priv->state !=STATE_WRQ ) { +pr_warn ("OACK packet in %s state\n" , + tftp_states[priv->state]); +break ; + } + priv->tftp_con->udp->uh_dport = uh_sport;if (tftp_parse_oack (priv, pkt, len) <0 ) {
@@ -741,6 +747,12 @@ static void tftp_recv(struct file_priv *priv,break ; } +if (priv->state !=STATE_RDATA ) { +pr_warn ("DATA packet in %s state\n" , + tftp_states[priv->state]); +break ; + } +tftp_handle_data (priv, block, pkt +2 , len);break ;
Signature · 1 line
2.37.2
PATCH v4 21/21 tftp: add some documentation about windowsize support
In reply to Enrico Scholz--- Documentation/filesystems/tftp.rst | 38 ++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)
Documentation/filesystems/tftp.rst +38 -0
@@ -20,3 +20,41 @@ Example: In addition to the TFTP filesystem implementation, barebox does also have a :ref:`tftp command <command_tftp>`. + +RFC 7440 "windowsize" support +============================= + +barebox supports the tftp windowsize option for downloading files. It +is not implemented for uploads. + +Generally, this option greatly improves the download speed (factors +4-30 are not uncommon). But choosing a too large windowsize can have +the opposite effect. Performance depends on: + + - the network infrastructure: when the tftp server sends files with + 1Gb/s but there are components in the network (switches or the + target nic) which support only 100 Mb/s, packets will be dropped. + + The lower the internal buffers of the bottleneck components, the + lower the optimal window size. + + In practice (iMX8MP on a Netgear GS108Ev3 with a port configured to + 100 Mb/s) it had to be reduced to + + .. code-block:: console + global tftp.windowsize=26 + + for example. + + - the target network driver: datagrams from server will arive faster + than they can be processed and must be buffered internally. For + example, the `fec-imx` driver reserves place for + + .. code-block:: c + #define FEC_RBD_NUM 64 + + packets before they are dropped + + - partially the workload: copying downloaded files to ram will be + faster than burning them into flash. Latter can consume internal + buffers quicker so that windowsize might be reduced
Signature · 1 line
2.37.2
Sascha Hauer
AppliedQuoted 16 lines · Enrico Scholz
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 speedupIn the test, I downloaded variable sized files which were filled from
/dev/urandom. E.g.
Quoted 6 lines
| :/ global tftp.windowsize=128 | :/ cp -v /mnt/tftp/data-100MiB /tmp/data && sha1sum /tmp/data | [################################################################] 104857600 bytes, 98550375 bytes/sFor slow connection speeds, smaller files (1MiB, 4 MiB + 20 MiB) were used.
Applied, thanks
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 |