Skip to contentSkip to conversation

slirp: misc fixes

PATCH In review Herve Poussineau 4 participants 11 messages Apr 12, 2011 – Apr 18, 2011

lore
Share & tools
lore
#0 lore raw

cover slirp: misc fixes

These patches contain some fixes for the internal TFTP server. With these patches, MS Windows PE can be booted via PXE.

 slirp/tftp.c |   69 +++++++++++++++++++++++++++++++++++++++++----------------
 slirp/tftp.h |    2 +
 2 files changed, 51 insertions(+), 20 deletions(-)
#1 lore raw

PATCH 1/3 slirp: Implement TFTP Blocksize option

From: Herv� Poussineau <[email protected]>

This option is described in RFC 1783. As this is only an optional field, we may ignore it in some situations and handle it in some others. Here, if client requests a block size bigger than the block size we emit (512 bytes), accept the option with a value of 512

---
 slirp/tftp.c |   40 ++++++++++++++++++++++++++++++++--------
 1 files changed, 32 insertions(+), 8 deletions(-)
slirp/tftp.c +32 -8
index 8055ccc..7ef44c9 100644
@@ -116,13 +116,13 @@ static int tftp_read_data(struct tftp_session *spt, uint16_t block_nr,
 }
 
 static int tftp_send_oack(struct tftp_session *spt,
-                          const char *key, uint32_t value,
+                          const char *key[], uint32_t value[], int nb,
                           struct tftp_t *recv_tp)
 {
     struct sockaddr_in saddr, daddr;
     struct mbuf *m;
     struct tftp_t *tp;
-    int n = 0;
+    int i, n = 0;
 
     m = m_get(spt->slirp);
 
@@ -136,10 +136,12 @@ static int tftp_send_oack(struct tftp_session *spt,
     m->m_data += sizeof(struct udpiphdr);
 
     tp->tp_op = htons(TFTP_OACK);
-    n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%s",
-                  key) + 1;
-    n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%u",
-                  value) + 1;
+    for (i = 0; i < nb; i++) {
+      n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%s",
+                    key[i]) + 1;
+      n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%u",
+                    value[i]) + 1;
+    }
 
     saddr.sin_addr = recv_tp->ip.ip_dst;
     saddr.sin_port = recv_tp->udp.uh_dport;
@@ -260,6 +262,9 @@ static void tftp_handle_rrq(Slirp *slirp, struct tftp_t *tp, int pktlen)
   int s, k;
   size_t prefix_len;
   char *req_fname;
+  const char *option_name[2];
+  uint32_t option_value[2];
+  int nb_options = 0;
 
   /* check if a session already exists and if so terminate it */
   s = tftp_session_find(slirp, tp);
@@ -364,9 +369,28 @@ static void tftp_handle_rrq(Slirp *slirp, struct tftp_t *tp, int pktlen)
 	      }
 	  }
 
-	  tftp_send_oack(spt, "tsize", tsize, tp);
-	  return;
+	  option_name[nb_options] = "tsize";
+	  option_value[nb_options] = tsize;
+	  nb_options++;
       }
+    if (strcasecmp(key, "blksize") == 0) {
+      int blksize = atoi(value);
+
+      /* If blksize option is bigger than what we will
+       * emit, accept the option with our packet size.
+       * Otherwise, simply do as we didn't see the option.
+       */
+      if (blksize >= 512) {
+        option_name[nb_options] = "blksize";
+        option_value[nb_options] = 512;
+        nb_options++;
+      }
+    }
+  }
+
+  if (nb_options > 0) {
+    tftp_send_oack(spt, option_name, option_value, nb_options, tp);
+    return;
   }
 
   tftp_send_data(spt, 1, tp);
Signature · 1 line
1.6.0.2.GIT
#2 lore raw

PATCH 2/3 slirp: Handle more than 65535 blocks in TFTP transfers

From: Herv� Poussineau <[email protected]>

RFC 1350 does not mention block count-roll over. However, a lot of TFTP servers implement it to be able to transmit big files, so do it also

---
 slirp/tftp.c |    9 +++++----
 slirp/tftp.h |    1 +
 2 files changed, 6 insertions(+), 4 deletions(-)
slirp/tftp.c +5 -4
index 7ef44c9..7e63269 100644
@@ -92,7 +92,7 @@ static int tftp_session_find(Slirp *slirp, struct tftp_t *tp)
   return -1;
 }
 
-static int tftp_read_data(struct tftp_session *spt, uint16_t block_nr,
+static int tftp_read_data(struct tftp_session *spt, uint32_t block_nr,
                           uint8_t *buf, int len)
 {
   int fd;
@@ -196,7 +196,7 @@ out:
 }
 
 static int tftp_send_data(struct tftp_session *spt,
-                          uint16_t block_nr,
+                          uint32_t block_nr,
 			  struct tftp_t *recv_tp)
 {
   struct sockaddr_in saddr, daddr;
@@ -221,7 +221,7 @@ static int tftp_send_data(struct tftp_session *spt,
   m->m_data += sizeof(struct udpiphdr);
 
   tp->tp_op = htons(TFTP_DATA);
-  tp->x.tp_data.tp_block_nr = htons(block_nr);
+  tp->x.tp_data.tp_block_nr = htons(block_nr & 0xffff);
 
   saddr.sin_addr = recv_tp->ip.ip_dst;
   saddr.sin_port = recv_tp->udp.uh_dport;
@@ -253,6 +253,7 @@ static int tftp_send_data(struct tftp_session *spt,
     tftp_session_terminate(spt);
   }
 
+  spt->block_nr = block_nr;
   return 0;
 }
 
@@ -407,7 +408,7 @@ static void tftp_handle_ack(Slirp *slirp, struct tftp_t *tp, int pktlen)
   }
 
   if (tftp_send_data(&slirp->tftp_sessions[s],
-		     ntohs(tp->x.tp_data.tp_block_nr) + 1,
+		     slirp->tftp_sessions[s].block_nr + 1,
 		     tp) < 0) {
     return;
   }
slirp/tftp.h +1 -0
index 72e5e91..471f22e 100644
@@ -36,6 +36,7 @@ struct tftp_session {
 
     struct in_addr client_ip;
     uint16_t client_port;
+    uint32_t block_nr;
 
     int timestamp;
 };
Signature · 1 line
1.6.0.2.GIT
#3 lore raw

PATCH 3/3 slirp: improve TFTP performance

From: Herv� Poussineau <[email protected]>

When transfering a file, keep it open during the whole transfer, instead of opening/closing it for each block.

---
 slirp/tftp.c |   20 ++++++++++++--------
 slirp/tftp.h |    1 +
 2 files changed, 13 insertions(+), 8 deletions(-)
slirp/tftp.c +12 -8
index 7e63269..48748f1 100644
@@ -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;
+    }
     qemu_free(spt->filename);
     spt->slirp = NULL;
 }
@@ -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) {
-        qemu_free(spt->filename);
+        tftp_session_terminate(spt);
         goto found;
     }
   }
@@ -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;
 
@@ -95,23 +100,22 @@ static int tftp_session_find(Slirp *slirp, struct tftp_t *tp)
 static int tftp_read_data(struct tftp_session *spt, uint32_t block_nr,
                           uint8_t *buf, int len)
 {
-  int fd;
   int bytes_read = 0;
 
-  fd = open(spt->filename, O_RDONLY | O_BINARY);
+  if (spt->fd < 0) {
+    spt->fd = open(spt->filename, O_RDONLY | O_BINARY);
+  }
 
-  if (fd < 0) {
+  if (spt->fd < 0) {
     return -1;
   }
 
   if (len) {
-    lseek(fd, block_nr * 512, SEEK_SET);
+    lseek(spt->fd, block_nr * 512, SEEK_SET);
 
-    bytes_read = read(fd, buf, len);
+    bytes_read = read(spt->fd, buf, len);
   }
 
-  close(fd);
-
   return bytes_read;
 }
 
slirp/tftp.h +1 -0
index 471f22e..51704e4 100644
@@ -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;
Signature · 1 line
1.6.0.2.GIT
Reviewed-by on v1 3/3
#4 lore raw

Re: [Qemu-devel] [PATCH 3/3] slirp: improve TFTP performance

Quoted 62 lines · Herve Poussineau

From: Herv? Poussineau <[email protected]>

When transfering a file, keep it open during the whole transfer, instead of opening/closing it for each block.

---
 slirp/tftp.c |   20 ++++++++++++--------
 slirp/tftp.h |    1 +
 2 files changed, 13 insertions(+), 8 deletions(-)
slirp/tftp.c +10 -5
index 7e63269..48748f1 100644
@@ -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;
+    }
     qemu_free(spt->filename);
     spt->slirp = NULL;
 }
@@ -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) {
-        qemu_free(spt->filename);
+        tftp_session_terminate(spt);
         goto found;
     }
   }
@@ -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;
 
@@ -95,23 +100,22 @@ static int tftp_session_find(Slirp *slirp, struct tftp_t *tp)
 static int tftp_read_data(struct tftp_session *spt, uint32_t block_nr,
                           uint8_t *buf, int len)
 {
-  int fd;
   int bytes_read = 0;
 
-  fd = open(spt->filename, O_RDONLY | O_BINARY);
+  if (spt->fd < 0) {
+    spt->fd = open(spt->filename, O_RDONLY | O_BINARY);
+  }
 
-  if (fd < 0) {
+  if (spt->fd < 0) {
     return -1;
   }
 
   if (len) {
-    lseek(fd, block_nr * 512, SEEK_SET);

slirp/tftp.c static int tftp_session_find(Slirp *slirp, struct tftp_t *tp) · line 120

Quoted 10 lines
slirp/tftp.c +2 -3
+    lseek(spt->fd, block_nr * 512, SEEK_SET);
 
-    bytes_read = read(fd, buf, len);
+    bytes_read = read(spt->fd, buf, len);
   }
 
-  close(fd);
-
   return bytes_read;
 }

Given you are rewriting this most of this function, it's probably the good moment to also fix the indentation to 4 spaces.

Quoted 15 lines
slirp/tftp.h +1 -0
index 471f22e..51704e4 100644
@@ -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;
Signature · 1 line
1.6.0.2.GIT

Except the minor nit above:

Signature · 2 lines
Aurelien Jarno                          GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net
Reviewed-by on v1 2/3
#5 lore raw

Re: [Qemu-devel] [PATCH 2/3] slirp: Handle more than 65535 blocks in TFTP transfers

In reply to Herve Poussineau
Quoted 10 lines · Herve Poussineau

From: Herv? Poussineau <[email protected]>

RFC 1350 does not mention block count-roll over. However, a lot of TFTP servers implement it to be able to transmit big files, so do it also

---
 slirp/tftp.c |    9 +++++----
 slirp/tftp.h |    1 +
 2 files changed, 6 insertions(+), 4 deletions(-)
Quoted 65 lines
slirp/tftp.c +5 -4
index 7ef44c9..7e63269 100644
@@ -92,7 +92,7 @@ static int tftp_session_find(Slirp *slirp, struct tftp_t *tp)
   return -1;
 }
 
-static int tftp_read_data(struct tftp_session *spt, uint16_t block_nr,
+static int tftp_read_data(struct tftp_session *spt, uint32_t block_nr,
                           uint8_t *buf, int len)
 {
   int fd;
@@ -196,7 +196,7 @@ out:
 }
 
 static int tftp_send_data(struct tftp_session *spt,
-                          uint16_t block_nr,
+                          uint32_t block_nr,
 			  struct tftp_t *recv_tp)
 {
   struct sockaddr_in saddr, daddr;
@@ -221,7 +221,7 @@ static int tftp_send_data(struct tftp_session *spt,
   m->m_data += sizeof(struct udpiphdr);
 
   tp->tp_op = htons(TFTP_DATA);
-  tp->x.tp_data.tp_block_nr = htons(block_nr);
+  tp->x.tp_data.tp_block_nr = htons(block_nr & 0xffff);
 
   saddr.sin_addr = recv_tp->ip.ip_dst;
   saddr.sin_port = recv_tp->udp.uh_dport;
@@ -253,6 +253,7 @@ static int tftp_send_data(struct tftp_session *spt,
     tftp_session_terminate(spt);
   }
 
+  spt->block_nr = block_nr;
   return 0;
 }
 
@@ -407,7 +408,7 @@ static void tftp_handle_ack(Slirp *slirp, struct tftp_t *tp, int pktlen)
   }
 
   if (tftp_send_data(&slirp->tftp_sessions[s],
-		     ntohs(tp->x.tp_data.tp_block_nr) + 1,
+		     slirp->tftp_sessions[s].block_nr + 1,
 		     tp) < 0) {
     return;
   }
slirp/tftp.h +1 -0
index 72e5e91..471f22e 100644
@@ -36,6 +36,7 @@ struct tftp_session {
 
     struct in_addr client_ip;
     uint16_t client_port;
+    uint32_t block_nr;
 
     int timestamp;
 };
Signature · 1 line
1.6.0.2.GIT
Signature · 2 lines
Aurelien Jarno                          GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net
#6 lore raw

Re: [Qemu-devel] [PATCH 1/3] slirp: Implement TFTP Blocksize option

In reply to Herve Poussineau
Quoted 11 lines · Herve Poussineau

From: Herv? Poussineau <[email protected]>

This option is described in RFC 1783. As this is only an optional field, we may ignore it in some situations and handle it in some others. Here, if client requests a block size bigger than the block size we emit (512 bytes), accept the option with a value of 512

---
 slirp/tftp.c |   40 ++++++++++++++++++++++++++++++++--------
 1 files changed, 32 insertions(+), 8 deletions(-)

Have you tested PXELINUX and gPXE?

Stefan

#7 lore raw

Re: [Qemu-devel] [PATCH 1/3] slirp: Implement TFTP Blocksize option

Hi,

Yes, I've tried booting with PXELINUX and gPXE, and I didn't see any problem.

Hervé

Quoted 21 lines · Stefan Hajnoczi
Quoted 7 lines · Herve Poussineau

From: Herv? Poussineau <[email protected]>

This option is described in RFC 1783. As this is only an optional field, we may ignore it in some situations and handle it in some others. Here, if client requests a block size bigger than the block size we emit (512 bytes), accept the option with a value of 512

Quoted 5 lines
---
 slirp/tftp.c |   40 ++++++++++++++++++++++++++++++++--------
 1 files changed, 32 insertions(+), 8 deletions(-)

Have you tested PXELINUX and gPXE?

Stefan

#8 lore raw

Re: [Qemu-devel] [PATCH 1/3] slirp: Implement TFTP Blocksize option

Quoted 4 lines · Hervé Poussineau

Hi,

Yes, I've tried booting with PXELINUX and gPXE, and I didn't see any problem.

Great. Thanks for confirming.

Stefan

#9 lore raw

Re: [Qemu-devel] [PATCH 1/3] slirp: Implement TFTP Blocksize option

In reply to Herve Poussineau
Quoted 1 line · Herve Poussineau

From: Herv� Poussineau<[email protected]>

Note: your charset is UTF-8 but this is Latin-1 (I think). Please use consistent charsets.

Signature · 1 line
error compiling committee.c: too many arguments to function
#10 lore raw

Re: [Qemu-devel] [PATCH 1/3] slirp: Implement TFTP Blocksize option

In reply to Herve Poussineau
Quoted 11 lines · Herve Poussineau

From: Herv? Poussineau <[email protected]>

This option is described in RFC 1783. As this is only an optional field, we may ignore it in some situations and handle it in some others. Here, if client requests a block size bigger than the block size we emit (512 bytes), accept the option with a value of 512

---
 slirp/tftp.c |   40 ++++++++++++++++++++++++++++++++--------
 1 files changed, 32 insertions(+), 8 deletions(-)

The code looks fine except a small indentation issue below. However I don't really see the point of such a patch. This patch basically look for this option, but beside acking it, the behavior doesn't change. On the other hand servers are free to not support all the possible option, in which case they should simply ignore them (i.e. the current behavior).

Quoted 62 lines
slirp/tftp.c +19 -8
index 8055ccc..7ef44c9 100644
@@ -116,13 +116,13 @@ static int tftp_read_data(struct tftp_session *spt, uint16_t block_nr,
 }
 
 static int tftp_send_oack(struct tftp_session *spt,
-                          const char *key, uint32_t value,
+                          const char *key[], uint32_t value[], int nb,
                           struct tftp_t *recv_tp)
 {
     struct sockaddr_in saddr, daddr;
     struct mbuf *m;
     struct tftp_t *tp;
-    int n = 0;
+    int i, n = 0;
 
     m = m_get(spt->slirp);
 
@@ -136,10 +136,12 @@ static int tftp_send_oack(struct tftp_session *spt,
     m->m_data += sizeof(struct udpiphdr);
 
     tp->tp_op = htons(TFTP_OACK);
-    n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%s",
-                  key) + 1;
-    n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%u",
-                  value) + 1;
+    for (i = 0; i < nb; i++) {
+      n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%s",
+                    key[i]) + 1;
+      n += snprintf(tp->x.tp_buf + n, sizeof(tp->x.tp_buf) - n, "%u",
+                    value[i]) + 1;
+    }
 
     saddr.sin_addr = recv_tp->ip.ip_dst;
     saddr.sin_port = recv_tp->udp.uh_dport;
@@ -260,6 +262,9 @@ static void tftp_handle_rrq(Slirp *slirp, struct tftp_t *tp, int pktlen)
   int s, k;
   size_t prefix_len;
   char *req_fname;
+  const char *option_name[2];
+  uint32_t option_value[2];
+  int nb_options = 0;
 
   /* check if a session already exists and if so terminate it */
   s = tftp_session_find(slirp, tp);
@@ -364,9 +369,28 @@ static void tftp_handle_rrq(Slirp *slirp, struct tftp_t *tp, int pktlen)
 	      }
 	  }
 
-	  tftp_send_oack(spt, "tsize", tsize, tp);
-	  return;
+	  option_name[nb_options] = "tsize";
+	  option_value[nb_options] = tsize;
+	  nb_options++;
       }
+    if (strcasecmp(key, "blksize") == 0) {
+      int blksize = atoi(value);
+
+      /* If blksize option is bigger than what we will
+       * emit, accept the option with our packet size.

slirp/tftp.c static void tftp_handle_rrq(Slirp *slirp, struct tftp_t *tp, int pktlen) · line 390

Quoted 10 lines
slirp/tftp.c +10 -0
+       * Otherwise, simply do as we didn't see the option.
+       */
+      if (blksize >= 512) {
+        option_name[nb_options] = "blksize";
+        option_value[nb_options] = 512;
+        nb_options++;
+      }
+    }
+  }
+

I known slirp/tftp.c has weird indentation, but you should probably try to match the indentation from "tsize" for this black, that is 4 space like in QEMU, except for the initial indentation.

Quoted 11 lines
slirp/tftp.c +3 -0
+  if (nb_options > 0) {
+    tftp_send_oack(spt, option_name, option_value, nb_options, tp);
+    return;
   }
 
   tftp_send_data(spt, 1, tp);
Signature · 1 line
1.6.0.2.GIT
Signature · 2 lines
Aurelien Jarno                          GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net