Skip to contentSkip to conversation

tcp: add window scaling and RTTM support

PATCH In review Josef Bacik 2 participants 4 messages Feb 1, 2016 – Feb 22, 2016

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 tcp: add window scaling and RTTM support

Sometimes we have to provision boxes across regions, such as California to Sweden. The http server has a 10 minute timeout, so if we can't get our 250mb image transferred fast enough our provisioning fails, which is not ideal. So add tcp window scaling on open connections and set the window size to 1mb. With this change we're able to get higher sustained transfers between regions and can transfer our image in well below 10 minutes. Without this patch we'd time out every time halfway through the transfer.

RTTM is needed in order to make window scaling work well under heavy congestion or packet loss. In most cases grub could recover with just window scaling enabled, but on some machines the congestion would be so high that it would never recover and would timeout.

I've made the window size configureable with the grub env variable "tcp_window_size". By default this is set to 1mb but can be configured to whatever a user wants, and we will calculate the appropriate window size and scale settings. Thanks,

---
v2->v3:
-Addressed all of Andrei's comments.
-Fixed the tcp_window_size option to set the global window size and scale so we
don't calculate every time we open a new connection.
-Fixed how we check the options on receive.

 grub-core/net/net.c |   1 +
 grub-core/net/tcp.c | 186 +++++++++++++++++++++++++++++++++++++++++++++++++---
 include/grub/net.h  |   2 +
 3 files changed, 181 insertions(+), 8 deletions(-)
grub-core/net/net.c +1 -0
index 21a4e94..16e985b 100644
@@ -1751,6 +1751,7 @@ GRUB_MOD_INIT(net)
 				       "", N_("list network cards"));
   cmd_lsaddr = grub_register_command ("net_ls_addr", grub_cmd_listaddrs,
 				       "", N_("list network addresses"));
+  grub_net_tcp_init ();
   grub_bootp_init ();
   grub_dns_init ();
 
grub-core/net/tcp.c +178 -8

311 lines not on this page · raw message

include/grub/net.h +2 -0
index 4571b72..fa3d286 100644
@@ -551,6 +551,8 @@ grub_net_add_dns_server (const struct grub_net_network_level_address *s);
 void
 grub_net_remove_dns_server (const struct grub_net_network_level_address *s);
 
+void
+grub_net_tcp_init (void);
 
 extern char *grub_net_default_server;
 
Signature · 1 line
1.8.1
#1 lore raw

01.02.2016 21:30, Josef Bacik пишет:
...

grub-core/net/tcp.c grub_net_recv_tcp_packet (struct grub_net_buff *nb, · line 846

Quoted 12 lines
grub-core/net/tcp.c +1 -0
@@ -745,6 +817,7 @@ grub_net_recv_tcp_packet (struct grub_net_buff *nb,
 {
   struct tcphdr *tcph;
   grub_net_tcp_socket_t sock;
+  grub_uint32_t tsecr = 0;
   grub_err_t err;
 
   /* Ignore broadcast.  */
@@ -771,6 +844,38 @@ grub_net_recv_tcp_packet (struct grub_net_buff *nb,
       return GRUB_ERR_NONE;
     }
 

Still no proper boundary check.

Quoted 5 lines
grub-core/net/tcp.c +5 -0
+  /* If the packet is large enough to have the timestamp opt then lets look for
+     the tsecr value. */
+  if ((grub_be_to_cpu16 (tcph->flags >> 12) * sizeof (grub_uint32_t)) >=
+      ALIGN_UP (sizeof (struct tcphdr) + sizeof (struct tcp_timestamp_opt), 4))
+    {

grub-core/net/tcp.c grub_net_recv_tcp_packet (struct grub_net_buff *nb, · line 861

Quoted 10 lines
grub-core/net/tcp.c +10 -0
+      struct tcp_opt_hdr *opt;
+      grub_size_t remaining = nb->tail - nb->data;
+
+      opt = (struct tcp_opt_hdr *)(tcph + 1);
+      while (remaining > 0)
+	{
+	  grub_uint8_t len = 1;
+	  if (opt->kind == 8 || opt->kind == 0)
+	    break;
+	  if (opt->kind > 1)

Here (we only can ensure opt->kind here)

Quoted 4 lines
grub-core/net/tcp.c +4 -0
+	    len = opt->length;
+	  if (len > remaining)
+	    len = remaining;
+	  remaining -= len;

grub-core/net/tcp.c grub_net_recv_tcp_packet (struct grub_net_buff *nb, · line 875

Quoted 10 lines
grub-core/net/tcp.c +10 -0
+	  opt = (struct tcp_opt_hdr *)((grub_uint8_t *)opt + len);
+	}
+
+      /* Ok we definitely have the timestamp option. */
+      if (opt->kind == 8)
+	{
+	  struct tcp_timestamp_opt *timestamp;
+
+	  timestamp = (struct tcp_timestamp_opt *)opt;
+	  tsecr = grub_be_to_cpu32 (timestamp->tsval);

And of course here (there is no length check at all when we break out of loop).

Quoted 17 lines
grub-core/net/tcp.c +6 -0
+	}
+    }
+
   FOR_TCP_SOCKETS (sock)
   {
     if (!(grub_be_to_cpu16 (tcph->dst) == sock->in_port
@@ -805,6 +910,9 @@ grub_net_recv_tcp_packet (struct grub_net_buff *nb,
 	sock->their_start_seq = grub_be_to_cpu32 (tcph->seqnr);
 	sock->their_cur_seq = sock->their_start_seq + 1;
 	sock->established = 1;
+	sock->timestamp_supported = 0;
+	if (tsecr)
+	  sock->timestamp_supported = 1;
       }
 
     if (grub_be_to_cpu16 (tcph->flags) & TCP_RST)

grub-core/net/tcp.c grub_net_recv_tcp_packet (struct grub_net_buff *nb, · line 1018

Quoted 5 lines
grub-core/net/tcp.c +2 -0
@@ -906,6 +1014,8 @@ grub_net_recv_tcp_packet (struct grub_net_buff *nb,
 	      return err;
 	    }
 
+	  /* We only update the tsecr when we advance the window. */
+	  sock->cur_tsecr = tsecr;

You intended to check that tsecr >= cur_tsecr.

Quoted 5 lines
grub-core/net/tcp.c +0 -0
 	  sock->their_cur_seq += (nb_top->tail - nb_top->data);
 	  if (grub_be_to_cpu16 (tcph->flags) & TCP_FIN)
 	    {
@@ -999,3 +1109,63 @@ grub_net_tcp_unstall (grub_net_tcp_socket_t sock)
   sock->i_stall = 0;

grub-core/net/tcp.c grub_net_tcp_unstall (grub_net_tcp_socket_t sock) · line 1119

Quoted 10 lines
grub-core/net/tcp.c +8 -0
   ack (sock);
 }
+
+static const char *
+window_get_env (struct grub_env_var *var __attribute__ ((unused)),
+		const char *val __attribute__ ((unused)))
+{
+  return grub_net_tcp_window_size;
+}
+

Oh, that's really redundant as is, string is stored as variable value anyway, no need to duplicate it, just return from window_set_env.

Quoted 6 lines
grub-core/net/tcp.c +6 -0
+static char *
+window_set_env (struct grub_env_var *var __attribute__ ((unused)),
+		const char *val)
+{
+  grub_uint32_t ret;
+

grub-core/net/tcp.c grub_net_tcp_unstall (grub_net_tcp_socket_t sock) · line 1135

Quoted 10 lines
grub-core/net/tcp.c +10 -0
+  if (val == NULL)
+    return NULL;
+
+  grub_error_push ();
+  ret = (grub_uint32_t) grub_strtoul (val, 0, 0);
+  if (grub_errno != GRUB_ERR_NONE)
+    {
+      grub_printf ("Invalid number for window size '%s'.\n", val);
+      grub_errno = GRUB_ERR_NONE;
+      grub_error_pop ();

You just lost proper error return from grub_strtoul. Just return NULL here without any push/pop and error from strtoul will be correctly propagated and printed when set command completes.

grub-core/net/tcp.c grub_net_tcp_unstall (grub_net_tcp_socket_t sock) · line 1143

Quoted 8 lines
grub-core/net/tcp.c +8 -0
+      return NULL;
+    }
+  grub_error_pop ();
+
+  /* A window size greater than 1gib is invalid. */
+  if (ret > 1024 * 1024 * 1024)
+    {
+      grub_printf ("TCP window size must be <= 1gib.\n");

Please use grub_error to set grub_errno and error string; GRUB_ERR_BAD_ARGUMENT looks suitable here.

Quoted 9 lines
grub-core/net/tcp.c +9 -0
+      return NULL;
+    }
+  grub_net_tcp_window_size = grub_strdup (val);
+  tcp_window_size = ret;
+  tcp_window_scale = 0;
+
+  /* The window size is only 16 bits long, so we have to scale it down to fit in
+     the header and calculate the scale along the way. */
+  while (tcp_window_size > 65535)

grub-core/net/tcp.c grub_net_tcp_unstall (grub_net_tcp_socket_t sock) · line 1162

Quoted 10 lines
grub-core/net/tcp.c +10 -0
+    {
+      tcp_window_size >>= 1;
+      tcp_window_scale += 1;
+    }
+
+  return grub_net_tcp_window_size;
+}
+
+/* We set the default window size to 1mib. */
+#define DEFAULT_TCP_WINDOW_SIZE "1048576"

Well ... I'm still unsure we should do it by default.

Quoted 23 lines
grub-core/net/tcp.c +9 -0
+
+void
+grub_net_tcp_init (void)
+{
+  grub_register_variable_hook ("net_tcp_window_size", window_get_env,
+			       window_set_env);
+  grub_env_export ("net_tcp_window_size");
+  grub_env_set ("net_tcp_window_size", DEFAULT_TCP_WINDOW_SIZE);
+}
include/grub/net.h +2 -0
index 4571b72..fa3d286 100644
@@ -551,6 +551,8 @@ grub_net_add_dns_server (const struct grub_net_network_level_address *s);
 void
 grub_net_remove_dns_server (const struct grub_net_network_level_address *s);
 
+void
+grub_net_tcp_init (void);
 
 extern char *grub_net_default_server;
 
#2 lore raw
Quoted 144 lines · Andrei Borzenkov

01.02.2016 21:30, Josef Bacik пишет:
...

grub-core/net/tcp.c grub_net_recv_tcp_packet (struct grub_net_buff *nb, · line 846

Quoted 12 lines
grub-core/net/tcp.c +1 -0
@@ -745,6 +817,7 @@ grub_net_recv_tcp_packet (struct grub_net_buff *nb,
  {
    struct tcphdr *tcph;
    grub_net_tcp_socket_t sock;
+  grub_uint32_t tsecr = 0;
    grub_err_t err;
 
    /* Ignore broadcast.  */
@@ -771,6 +844,38 @@ grub_net_recv_tcp_packet (struct grub_net_buff *nb,
        return GRUB_ERR_NONE;
      }
 

Still no proper boundary check.

Quoted 5 lines
grub-core/net/tcp.c +5 -0
+  /* If the packet is large enough to have the timestamp opt then lets look for
+     the tsecr value. */
+  if ((grub_be_to_cpu16 (tcph->flags >> 12) * sizeof (grub_uint32_t)) >=
+      ALIGN_UP (sizeof (struct tcphdr) + sizeof (struct tcp_timestamp_opt), 4))
+    {

grub-core/net/tcp.c grub_net_recv_tcp_packet (struct grub_net_buff *nb, · line 861

Quoted 10 lines
grub-core/net/tcp.c +10 -0
+      struct tcp_opt_hdr *opt;
+      grub_size_t remaining = nb->tail - nb->data;
+
+      opt = (struct tcp_opt_hdr *)(tcph + 1);
+      while (remaining > 0)
+	{
+	  grub_uint8_t len = 1;
+	  if (opt->kind == 8 || opt->kind == 0)
+	    break;
+	  if (opt->kind > 1)

Here (we only can ensure opt->kind here)

Quoted 4 lines
grub-core/net/tcp.c +4 -0
+	    len = opt->length;
+	  if (len > remaining)
+	    len = remaining;
+	  remaining -= len;

grub-core/net/tcp.c grub_net_recv_tcp_packet (struct grub_net_buff *nb, · line 875

Quoted 10 lines
grub-core/net/tcp.c +10 -0
+	  opt = (struct tcp_opt_hdr *)((grub_uint8_t *)opt + len);
+	}
+
+      /* Ok we definitely have the timestamp option. */
+      if (opt->kind == 8)
+	{
+	  struct tcp_timestamp_opt *timestamp;
+
+	  timestamp = (struct tcp_timestamp_opt *)opt;
+	  tsecr = grub_be_to_cpu32 (timestamp->tsval);

And of course here (there is no length check at all when we break out of loop).

Quoted 17 lines
grub-core/net/tcp.c +6 -0
+	}
+    }
+
    FOR_TCP_SOCKETS (sock)
    {
      if (!(grub_be_to_cpu16 (tcph->dst) == sock->in_port
@@ -805,6 +910,9 @@ grub_net_recv_tcp_packet (struct grub_net_buff *nb,
  	sock->their_start_seq = grub_be_to_cpu32 (tcph->seqnr);
  	sock->their_cur_seq = sock->their_start_seq + 1;
  	sock->established = 1;
+	sock->timestamp_supported = 0;
+	if (tsecr)
+	  sock->timestamp_supported = 1;
        }
 
      if (grub_be_to_cpu16 (tcph->flags) & TCP_RST)

grub-core/net/tcp.c grub_net_recv_tcp_packet (struct grub_net_buff *nb, · line 1018

Quoted 5 lines
grub-core/net/tcp.c +2 -0
@@ -906,6 +1014,8 @@ grub_net_recv_tcp_packet (struct grub_net_buff *nb,
  	      return err;
  	    }
 
+	  /* We only update the tsecr when we advance the window. */
+	  sock->cur_tsecr = tsecr;

You intended to check that tsecr >= cur_tsecr.

Quoted 5 lines
grub-core/net/tcp.c +0 -0
  	  sock->their_cur_seq += (nb_top->tail - nb_top->data);
  	  if (grub_be_to_cpu16 (tcph->flags) & TCP_FIN)
  	    {
@@ -999,3 +1109,63 @@ grub_net_tcp_unstall (grub_net_tcp_socket_t sock)
    sock->i_stall = 0;

grub-core/net/tcp.c grub_net_tcp_unstall (grub_net_tcp_socket_t sock) · line 1119

Quoted 10 lines
grub-core/net/tcp.c +8 -0
    ack (sock);
  }
+
+static const char *
+window_get_env (struct grub_env_var *var __attribute__ ((unused)),
+		const char *val __attribute__ ((unused)))
+{
+  return grub_net_tcp_window_size;
+}
+

Oh, that's really redundant as is, string is stored as variable value anyway, no need to duplicate it, just return from window_set_env.

Quoted 6 lines
grub-core/net/tcp.c +6 -0
+static char *
+window_set_env (struct grub_env_var *var __attribute__ ((unused)),
+		const char *val)
+{
+  grub_uint32_t ret;
+

grub-core/net/tcp.c grub_net_tcp_unstall (grub_net_tcp_socket_t sock) · line 1135

Quoted 10 lines
grub-core/net/tcp.c +10 -0
+  if (val == NULL)
+    return NULL;
+
+  grub_error_push ();
+  ret = (grub_uint32_t) grub_strtoul (val, 0, 0);
+  if (grub_errno != GRUB_ERR_NONE)
+    {
+      grub_printf ("Invalid number for window size '%s'.\n", val);
+      grub_errno = GRUB_ERR_NONE;
+      grub_error_pop ();

You just lost proper error return from grub_strtoul. Just return NULL here without any push/pop and error from strtoul will be correctly propagated and printed when set command completes.

grub-core/net/tcp.c grub_net_tcp_unstall (grub_net_tcp_socket_t sock) · line 1143

Quoted 8 lines
grub-core/net/tcp.c +8 -0
+      return NULL;
+    }
+  grub_error_pop ();
+
+  /* A window size greater than 1gib is invalid. */
+  if (ret > 1024 * 1024 * 1024)
+    {
+      grub_printf ("TCP window size must be <= 1gib.\n");

Please use grub_error to set grub_errno and error string; GRUB_ERR_BAD_ARGUMENT looks suitable here.

Quoted 13 lines
grub-core/net/tcp.c +13 -0
+      return NULL;
+    }
+  grub_net_tcp_window_size = grub_strdup (val);
+  tcp_window_size = ret;
+  tcp_window_scale = 0;
+
+  /* The window size is only 16 bits long, so we have to scale it down to fit in
+     the header and calculate the scale along the way. */
+  while (tcp_window_size > 65535)
+    {
+      tcp_window_size >>= 1;
+      tcp_window_scale += 1;
+    }
Quoted 8 lines

grub-core/net/tcp.c grub_net_tcp_unstall (grub_net_tcp_socket_t sock) · line 1162

Quoted 6 lines
grub-core/net/tcp.c +6 -0
+
+  return grub_net_tcp_window_size;
+}
+
+/* We set the default window size to 1mib. */
+#define DEFAULT_TCP_WINDOW_SIZE "1048576"

Well ... I'm still unsure we should do it by default.

Sorry I just now noticed you replied. I'll fix up the rest of the stuff. And apparently we're the only ones doing netbooting so I feel like we get to pick the default ;). Thanks,

Josef

#3 lore raw

18.02.2016 22:20, Josef Bacik пишет:

Quoted 8 lines
Quoted 4 lines

grub-core/net/tcp.c grub_net_tcp_unstall (grub_net_tcp_socket_t sock) · line 1162

Quoted 2 lines
grub-core/net/tcp.c +2 -0
+/* We set the default window size to 1mib. */
+#define DEFAULT_TCP_WINDOW_SIZE "1048576"

Well ... I'm still unsure we should do it by default.

Sorry I just now noticed you replied. I'll fix up the rest of the stuff. And apparently we're the only ones doing netbooting so I feel like we get to pick the default ;).

Not really. netbooting is quite by many; but not everyone is doing it across continents :)