Skip to content

Commit

Permalink
slirp: improve TFTP performance
Browse files Browse the repository at this point in the history
When transferring a file, keep it open during the whole transfer,
instead of opening/closing it for each block.

Signed-off-by: Hervé Poussineau <hpoussin@reactos.org>
Reviewed-by: Aurelien Jarno <aurelien@aurel32.net>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
  • Loading branch information
hpoussin authored and jan-kiszka committed Sep 13, 2012
1 parent e56afbc commit 78be056
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
32 changes: 18 additions & 14 deletions slirp/tftp.c
Expand Up @@ -37,6 +37,10 @@ static inline void tftp_session_update(struct tftp_session *spt)

static void tftp_session_terminate(struct tftp_session *spt)
{
if (spt->fd >= 0) {
close(spt->fd);
spt->fd = -1;
}
g_free(spt->filename);
spt->slirp = NULL;
}
Expand All @@ -54,7 +58,7 @@ static int tftp_session_allocate(Slirp *slirp, struct tftp_t *tp)

/* sessions time out after 5 inactive seconds */
if ((int)(curtime - spt->timestamp) > 5000) {
g_free(spt->filename);
tftp_session_terminate(spt);
goto found;
}
}
Expand All @@ -64,6 +68,7 @@ static int tftp_session_allocate(Slirp *slirp, struct tftp_t *tp)
found:
memset(spt, 0, sizeof(*spt));
memcpy(&spt->client_ip, &tp->ip.ip_src, sizeof(spt->client_ip));
spt->fd = -1;
spt->client_port = tp->udp.uh_sport;
spt->slirp = slirp;

Expand Down Expand Up @@ -95,24 +100,23 @@ static int tftp_session_find(Slirp *slirp, struct tftp_t *tp)
static int tftp_read_data(struct tftp_session *spt, uint16_t block_nr,
uint8_t *buf, int len)
{
int fd;
int bytes_read = 0;

fd = open(spt->filename, O_RDONLY | O_BINARY);
int bytes_read = 0;

if (fd < 0) {
return -1;
}
if (spt->fd < 0) {
spt->fd = open(spt->filename, O_RDONLY | O_BINARY);
}

if (len) {
lseek(fd, block_nr * 512, SEEK_SET);
if (spt->fd < 0) {
return -1;
}

bytes_read = read(fd, buf, len);
}
if (len) {
lseek(spt->fd, block_nr * 512, SEEK_SET);

close(fd);
bytes_read = read(spt->fd, buf, len);
}

return bytes_read;
return bytes_read;
}

static int tftp_send_oack(struct tftp_session *spt,
Expand Down
1 change: 1 addition & 0 deletions slirp/tftp.h
Expand Up @@ -33,6 +33,7 @@ struct tftp_t {
struct tftp_session {
Slirp *slirp;
char *filename;
int fd;

struct in_addr client_ip;
uint16_t client_port;
Expand Down

0 comments on commit 78be056

Please sign in to comment.