5544c1b
From c1b408d0c9d836e0a95d1e0695c0c6c605ceb368 Mon Sep 17 00:00:00 2001
5544c1b
From: =?UTF-8?q?Herv=C3=A9=20Poussineau?= <hpoussin@reactos.org>
5544c1b
Date: Mon, 10 Sep 2012 20:52:25 +0200
5544c1b
Subject: [PATCH] slirp: improve TFTP performance
5544c1b
MIME-Version: 1.0
5544c1b
Content-Type: text/plain; charset=UTF-8
5544c1b
Content-Transfer-Encoding: 8bit
5544c1b
5544c1b
When transferring a file, keep it open during the whole transfer,
5544c1b
instead of opening/closing it for each block.
5544c1b
5544c1b
Signed-off-by: Hervé Poussineau <hpoussin@reactos.org>
5544c1b
Reviewed-by: Aurelien Jarno <aurelien@aurel32.net>
5544c1b
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
5544c1b
(cherry picked from commit 78be056628c76ff73eedeade86fde44b97343c79)
5544c1b
5544c1b
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
5544c1b
---
5544c1b
 slirp/tftp.c | 32 ++++++++++++++++++--------------
5544c1b
 slirp/tftp.h |  1 +
5544c1b
 2 files changed, 19 insertions(+), 14 deletions(-)
5544c1b
5544c1b
diff --git a/slirp/tftp.c b/slirp/tftp.c
5544c1b
index b78765f..520dbd6 100644
5544c1b
--- a/slirp/tftp.c
5544c1b
+++ b/slirp/tftp.c
5544c1b
@@ -37,6 +37,10 @@ static inline void tftp_session_update(struct tftp_session *spt)
5544c1b
 
5544c1b
 static void tftp_session_terminate(struct tftp_session *spt)
5544c1b
 {
5544c1b
+    if (spt->fd >= 0) {
5544c1b
+        close(spt->fd);
5544c1b
+        spt->fd = -1;
5544c1b
+    }
5544c1b
     g_free(spt->filename);
5544c1b
     spt->slirp = NULL;
5544c1b
 }
5544c1b
@@ -54,7 +58,7 @@ static int tftp_session_allocate(Slirp *slirp, struct tftp_t *tp)
5544c1b
 
5544c1b
     /* sessions time out after 5 inactive seconds */
5544c1b
     if ((int)(curtime - spt->timestamp) > 5000) {
5544c1b
-        g_free(spt->filename);
5544c1b
+        tftp_session_terminate(spt);
5544c1b
         goto found;
5544c1b
     }
5544c1b
   }
5544c1b
@@ -64,6 +68,7 @@ static int tftp_session_allocate(Slirp *slirp, struct tftp_t *tp)
5544c1b
  found:
5544c1b
   memset(spt, 0, sizeof(*spt));
5544c1b
   memcpy(&spt->client_ip, &tp->ip.ip_src, sizeof(spt->client_ip));
5544c1b
+  spt->fd = -1;
5544c1b
   spt->client_port = tp->udp.uh_sport;
5544c1b
   spt->slirp = slirp;
5544c1b
 
5544c1b
@@ -95,24 +100,23 @@ static int tftp_session_find(Slirp *slirp, struct tftp_t *tp)
5544c1b
 static int tftp_read_data(struct tftp_session *spt, uint16_t block_nr,
5544c1b
                           uint8_t *buf, int len)
5544c1b
 {
5544c1b
-  int fd;
5544c1b
-  int bytes_read = 0;
5544c1b
-
5544c1b
-  fd = open(spt->filename, O_RDONLY | O_BINARY);
5544c1b
+    int bytes_read = 0;
5544c1b
 
5544c1b
-  if (fd < 0) {
5544c1b
-    return -1;
5544c1b
-  }
5544c1b
+    if (spt->fd < 0) {
5544c1b
+        spt->fd = open(spt->filename, O_RDONLY | O_BINARY);
5544c1b
+    }
5544c1b
 
5544c1b
-  if (len) {
5544c1b
-    lseek(fd, block_nr * 512, SEEK_SET);
5544c1b
+    if (spt->fd < 0) {
5544c1b
+        return -1;
5544c1b
+    }
5544c1b
 
5544c1b
-    bytes_read = read(fd, buf, len);
5544c1b
-  }
5544c1b
+    if (len) {
5544c1b
+        lseek(spt->fd, block_nr * 512, SEEK_SET);
5544c1b
 
5544c1b
-  close(fd);
5544c1b
+        bytes_read = read(spt->fd, buf, len);
5544c1b
+    }
5544c1b
 
5544c1b
-  return bytes_read;
5544c1b
+    return bytes_read;
5544c1b
 }
5544c1b
 
5544c1b
 static int tftp_send_oack(struct tftp_session *spt,
5544c1b
diff --git a/slirp/tftp.h b/slirp/tftp.h
5544c1b
index 72e5e91..9c364ea 100644
5544c1b
--- a/slirp/tftp.h
5544c1b
+++ b/slirp/tftp.h
5544c1b
@@ -33,6 +33,7 @@ struct tftp_t {
5544c1b
 struct tftp_session {
5544c1b
     Slirp *slirp;
5544c1b
     char *filename;
5544c1b
+    int fd;
5544c1b
 
5544c1b
     struct in_addr client_ip;
5544c1b
     uint16_t client_port;