Showing posts with label spdylay. Show all posts
Showing posts with label spdylay. Show all posts

Thursday, May 10, 2012

Bug Fix for SPDY/3 Flow Control

‹prev | My Chain | next›

For the past few nights, I have been playing code tag with Tatsuhiro Tsujikawa in an attempt to sort out the finer details of SPDY flow control, which is new in version 3. I have a server implementation in a node-spdy branch that works with Chrome, not Tatsuhiro's spdylay.

Today, Tatsuhiro did me one better than in previous days. Not only does spdylay have additional fixes, but he also fixed my node-spdy code. So first, I update spdylay from the GitHub repository:
git fetch origin
git merge origin/master
make
make install
As for the patch to node-spdy, it is simple enough. As I suspected yesterday, I was doing something wrong and the patch identifies my problem as always sending along data in chunks of 1300—regardless of the current receive window size. If there were 100 bytes left in the receive window, I had simply paused transfer at that point and awaited a WINDOW_UPDATE (Chrome would send one). What the patch does, is send 100bytes, taking the receive window down to exactly zero bytes, and then dumping the remainder of the buffer onto the queue:
Stream.prototype._writeData = function _writeData(fin, buffer) {
  var len;
  if (this.framer.version == 3) {
    if (this._transferWindowSize <= 0) {
      this._transferWindowBuffer.push([fin, buffer]);
      return;
    }
    len = Math.min(this._transferWindowSize, buffer.length);
    if (len < buffer.length) {
      this._transferWindowBuffer.push([fin, buffer.slice(len)]);
      fin = false;
    }
    this._transferWindowSize -= len;
  }

  this.lock(function() {
    var stream = this,
        frame = this.framer.dataFrame(this.id, fin, buffer.slice(0, len));

    // actually write here...
  }
};
That makes complete sense and I have no idea why I did not see that myself. I had a nagging feeling that something was not right, but Chrome's acceptance lulled me into a false sense of security.

Of course, to actually verify that it works, I load the page in Chrome:


So far, so good (no broken images). Checking out the SPDY tab of chrome://net-internals, I see the expected small transfers as the receive window is exhausted followed shortly by a WINDOW_UPDATE from Chrome indicating that more data may be transferred, and finally by more data:
SPDY_SESSION_RECV_DATA
--> flags = 0
--> size = 1300
--> stream_id = 9
SPDY_SESSION_RECV_DATA
--> flags = 0
--> size = 124
--> stream_id = 9
SPDY_SESSION_SENT_WINDOW_UPDATE
--> delta = 65536
--> stream_id = 9
SPDY_SESSION_RECV_DATA
--> flags = 0
--> size = 1176
--> stream_id = 9
SPDY_SESSION_RECV_DATA
--> flags = 0
--> size = 1300
--> stream_id = 9
This stream ends with a normal DATA FIN frame (shows as zero length):
SPDY_SESSION_RECV_DATA
--> flags = 0
--> size = 1300
--> stream_id = 9
SPDY_SESSION_RECV_DATA
--> flags = 0
--> size = 28
--> stream_id = 9
SPDY_SESSION_RECV_DATA
--> flags = 0
--> size = 0
--> stream_id = 9
Ultimately, the conversation ends with a couple of PINGs and all would seem well:
SPDY_SESSION_PING
--> type = "sent"
--> unique_id = 3
SPDY_SESSION_PING
--> type = "received"
--> unique_id = 3
Now for the real moment of truth, does it still work in spdylay? Well, of course it does!
➜  spdylay git:(master) spdycat -v -n https://localhost:3000/images/pipelining.jpg
...
[  0.182] recv DATA frame (stream_id=1, flags=0, length=369)
[  0.182] recv DATA frame (stream_id=1, flags=1, length=0)
It still works when I explicitly supply the initial receive window size (216 = 64kb):
➜  spdylay git:(master) spdycat -v -w16 -n https://localhost:3000/images/pipelining.jpg
...
[  0.178] recv DATA frame (stream_id=1, flags=0, length=369)
[  0.178] recv DATA frame (stream_id=1, flags=1, length=0)
The reason that I started playing with spdylay was to test different initial receive windows, so I specify a window of 16kb:
➜  spdylay git:(master) spdycat -v -w14 -n https://localhost:3000/images/pipelining.jpg
...
[  2.974] recv DATA frame (stream_id=1, flags=0, length=369)
[  2.974] recv DATA frame (stream_id=1, flags=1, length=0)
So yay!

Huge thanks to Tatsuhiro Tsujikawa for not only the tool to help test this, but also for fixing my own code. It is pretty damn cool that the Internet makes this possible, but not nearly as cool that someone on the other side of the planet would help out a stranger like that.

I believe that this leaves the spdy-v3 branch of node-spdy effectively feature complete. There are a few things that I will verify tomorrow, but for now... yay!


Day #381

Wednesday, May 9, 2012

Disagreement on the Spdy Receive Window

‹prev | My Chain | next›

After some initial difficulty, I ended yesterday with spdylay able to make spdy/3 connections to the spdy-v3 branch of node-spdy. It was able to make a connection, but not complete—likely due to a control flow issue in either spdylay or node-spdy. So tonight I set out to isolate the problem and, ideally, fix it.

But first, I update spdylay. As I mentioned, I had some initial difficulty with the connect primarily due to a bug in node-spdy's SETTINGS frame. Chrome seems to silently ignore the problem, proceeding as if the extra data were not present. Spdylay, on the other hand, silently failed in response to the problem.

I had fully intended to report the silent failure as an issue, but was pleasantly surprised to find that Tatsuhiro Tsujikawa had not only read last night's work, but already added the feature. So I:
git fetch origin
git merge origin/master
make
make install
With that, I get started investigating my control flow issue.

First up tonight, I try a simpler case than last night—a default control flow request. That is, I call spdycat on my very large image without an alternate receive window size (no -w flag):
➜  spdylay git:(master) spdycat -v -n https://localhost:3000/images/pipelining.jpg
[  0.003] NPN select next protocol: the remote server offers:
          * spdy/3
          * spdy/2
          * http/1.1
          * http/1.0
          NPN selected the protocol: spdy/3
[  0.009] send SYN_STREAM frame <version=3, flags=1, length=118>
          (stream_id=1, assoc_stream_id=0, pri=3)
          :host: localhost:3000
          :method: GET
          :path: /images/pipelining.jpg
          :scheme: https
          :version: HTTP/1.1
          accept: */*
          user-agent: spdylay/0.1.0
[  0.010] recv SETTINGS frame <version=3, flags=0, length=12>
          (niv=1)
          [4(1):100]
[  0.018] recv SYN_REPLY frame <version=3, flags=0, length=34>
          (stream_id=1) 
          :status: 200  
          :version: HTTP/1.1
[  0.018] recv DATA frame (stream_id=1, flags=0, length=1300)
[  0.018] recv DATA frame (stream_id=1, flags=0, length=1300)
[  0.018] recv DATA frame (stream_id=1, flags=0, length=1300)
...
[  0.422] send WINDOW_UPDATE frame 
          (stream_id=1, delta_window_size=33160)
...
[  0.462] recv DATA frame (stream_id=1, flags=0, length=1300)
[  0.462] recv DATA frame (stream_id=1, flags=0, length=369)
[  0.462] recv DATA frame (stream_id=1, flags=1, length=0)
[  0.462] send GOAWAY frame 
          (last_good_stream_id=0)
As there should be, spdycat issues numerous WINDOW_UPDATEs to the server along the way to indicate that it is ready to process more data. When node-spdy sees this, it send the data along until the receive window is again empty and blocks for another WINDOW_UPDATE.

Everything looks OK there although the last line is somewhat enigmatic. It seems like stream_id=1 was successful, but spdycat reports the last "good" stream_id as "0". As a quick sanity check, I load the page containing this image in Chrome. In the SPDY tab of chrome://net-internals, I see that the last two DATA frames are 1300 and 369 bytes long, just as with spdycat:
SPDY_SESSION_RECV_DATA
--> flags = 0
--> size = 1300
--> stream_id = 7
SPDY_SESSION_RECV_DATA
--> flags = 0
--> size = 369
--> stream_id = 7
SPDY_SESSION_RECV_DATA
--> flags = 0
--> size = 0
--> stream_id = 7
(the last DATA frame is a FIN by virtue of its zero size)

So I take from this that spdycat and Chrome both agree up on the default case. As for the non-default case, when I tell spdycat to use an initial receive window of 16kb (214 = 16kb), I still do not get complete transfer:
➜  spdylay git:(master) spdycat -v -w14 -n https://localhost:3000/images/pipelining.jpg
...
[  0.008] send SETTINGS frame 
          (niv=1)
          [7(0):16384]
[  0.013] send SYN_STREAM frame 
          (stream_id=1, assoc_stream_id=0, pri=3)
          :host: localhost:3000
          :method: GET
          :path: /images/pipelining.jpg
          :scheme: https
          :version: HTTP/1.1
          accept: */*
          user-agent: spdylay/0.1.0
...
[  0.231] send WINDOW_UPDATE frame 
          (stream_id=1, delta_window_size=9100)
[  0.256] recv DATA frame (stream_id=1, flags=0, length=1300)
[  0.295] recv DATA frame (stream_id=1, flags=0, length=1300)
[  0.295] recv DATA frame (stream_id=1, flags=0, length=1300)
[  0.295] recv DATA frame (stream_id=1, flags=0, length=1300)
[  0.296] recv DATA frame (stream_id=1, flags=0, length=1300)
[  0.296] recv DATA frame (stream_id=1, flags=0, length=1300)

                                 

Fatal
There are actually six WINDOW_UPDATEs issued by spdycat and processed correctly by node-spdy. For whatever reason, that last one fails. The most obvious cause is that spdycat and node-spdy disagree on the remaining window size. To answer that, I need to see what node-spdy thinks remains...

I add a console.log() to node-spdy to show me this information:
Stream.prototype._writeData = function _writeData(fin, buffer) {
  if (this.framer.version == 3) {
    if (this._transferWindowSize > 0) {
      this._transferWindowSize -= buffer.length;
      console.log("I think the transferWindowSize is: " + this._transferWindowSize);
    }

    if (this._transferWindowSize < 0) {
      this._transferWindowBuffer.push([fin, buffer]);
      return;
    }
  }

  // actually write to the stream...
};
I re-run spdycat -w14, and again get the unexplained pause. Back in the node-spdy window, I find:
I think the transferWindowSize is: 15084
I think the transferWindowSize is: 13784
I think the transferWindowSize is: 12484
I think the transferWindowSize is: 11184
I think the transferWindowSize is: 9884
I think the transferWindowSize is: 8584
I think the transferWindowSize is: 7284
I think the transferWindowSize is: 5984
I think the transferWindowSize is: 4684
I think the transferWindowSize is: 3384
I think the transferWindowSize is: 2084
I think the transferWindowSize is: 784
I think the transferWindowSize is: -516
I think the transferWindowSize is: 7284
I think the transferWindowSize is: 5984
I think the transferWindowSize is: 4684
I think the transferWindowSize is: 3384
I think the transferWindowSize is: 2084
I think the transferWindowSize is: 784
I think the transferWindowSize is: -516
...
The pattern of 2084, 784 and -516 remaining repeats itself over and over again until...
...
I think the transferWindowSize is: 1424
I think the transferWindowSize is: 124
I think the transferWindowSize is: -1176
...
I do not know if that is important because it is followed by another successful pattern of 2084, 784 and -516, followed by the one that hangs the connection:
...
I think the transferWindowSize is: 2084
I think the transferWindowSize is: 784
I think the transferWindowSize is: -516
I think the transferWindowSize is: 7284
I think the transferWindowSize is: 5984
I think the transferWindowSize is: 4684
I think the transferWindowSize is: 3384
I think the transferWindowSize is: 2084
I think the transferWindowSize is: 784
I think the transferWindowSize is: -516
Unfortunately, I seem to be stuck here. I think node-spdy is calculating this correctly—it definitely works with Chrome and spdylay for the default case.

Update: I am able to spdylay to transfer the large image if I switch the receive window exhaustion around in node-spdy. That is, if I calculate the receive window size after the decision is made as to whether or not the window is exhausted:
Stream.prototype._writeData = function _writeData(fin, buffer) {
  if (this.framer.version == 3) {
    if (this._transferWindowSize <= 0) {
      this._transferWindowBuffer.push([fin, buffer]);
      return;
    }

    if (this._transferWindowSize > 0) {
      this._transferWindowSize -= buffer.length;
    }
  }

  // actually write to the stream...
};
Then, everything works in spdycat for the default case (no -w option):
➜  spdylay git:(master) spdycat -v -n https://localhost:3000/images/pipelining.jpg
...
[  0.162] recv DATA frame (stream_id=1, flags=0, length=1300)
[  0.162] recv DATA frame (stream_id=1, flags=0, length=369)
[  0.162] recv DATA frame (stream_id=1, flags=1, length=0)
[  0.162] send GOAWAY frame 
          (last_good_stream_id=0)
It works with -w16 (which should be the same as the default case):
➜  spdylay git:(master) spdycat -v -w16 -n https://localhost:3000/images/pipelining.jpg
...
[  0.166] recv DATA frame (stream_id=1, flags=0, length=1300)
[  0.166] recv DATA frame (stream_id=1, flags=0, length=369)
[  0.166] recv DATA frame (stream_id=1, flags=1, length=0)
[  0.167] send GOAWAY frame 
          (last_good_stream_id=0)
And it even works with a non-default transfer window like 16kb:
➜  spdylay git:(master) spdycat -v -w14 -n https://localhost:3000/images/pipelining.jpg
...
[  0.514] recv DATA frame (stream_id=1, flags=0, length=1300)
[  0.514] recv DATA frame (stream_id=1, flags=0, length=369)
[  0.514] recv DATA frame (stream_id=1, flags=1, length=0)
Fatal
(I am unsure why there is a "Fatal" at the end there—it also pops up on occasion for the -w16 case)

So problem solved? I just need to keep the updated calculation and I am done, right? Well, no. Unfortunately Chrome does not like this control flow calculation. Both large images in my sample web page now fail to load:


And, checking the SPDY tab, I see that Chrome disagrees with the transfer window size:
...
SPDY_SESSION_RECV_DATA
--> flags = 0
--> size = 1300
--> stream_id = 7
SPDY_SESSION_SEND_RST_STREAM
--> description = "Negative recv window size"
--> status = 1
--> stream_id = 7
...
I know that this updated implementation is incorrect because Chrome refuses it. That said, I don't know that spdylay's implementation is incorrect—my implementation may only work incidentally. Hopefully Tatsuhiro is paying attention again...



Day #381

Tuesday, May 8, 2012

Spdylay and SPDY/3

‹prev | My Chain | next›

I had all but given up trying to test out my flow control implementation in node-spdy. Flow control is new in version 3 of the SPDY protocol. I think my implementation—even the race condition handling—is sound. But, since Chrome currently has no way to adjust its own data transfer window, I cannot use it to test my solution.

While chatting on the spdy-dev mailing list, however, Tatsuhiro Tsujikawa, the maintainer of spdylay said that he had added support for different receive window sizes. That sounds perfect, so I grab a copy of the spdylay repository and run the configure steps:
git clone https://github.com/tatsuhiro-t/spdylay.git
autoreconf -i
automake # there was no visible output from this command
autoconf # there was no visible output from this command
./configure --prefix=$HOME/local
The --prefix=$HOME/local option is my preference. I do not like installing unsigned code with root privileges, so I use the standard --prefix switch to signify that the resultant executable and shared libraries will be installed in $HOME/local (instead of the default /usr/local).

I do not have to worry about the prerequisites. I have been playing with SPDY for so long that I already have all the important development libraries needed.

The result of the configure command confirms that everything is in order:
configure: summary of build options:

    version:        0.1.0 shared 0:0:0
    Host type:      x86_64-unknown-linux-gnu
    Install prefix: /home/chris/local
    C compiler:     gcc
    CFlags:         -g -O2  
    Library types:  Shared=yes, Static=yes
    CUnit:          no
    OpenSSL:        yes
    Examples:       yes
So I build and install spdylay:
make
make install
With that, I have the desired spdycat command installed:
➜  spdylay git:(master) which spdycat
/home/chris/local/bin/spdycat
More importantly, it is not crashy:
➜  spdylay git:(master) spdycat --help
Usage: spdycat [-Onv3] [-t=seconds] [-w=window_bits] [--cert=CERT]
               [--key=KEY] URI...

OPTIONS:
    -v, --verbose      Print debug information such as reception/
                       transmission of frames and name/value pairs.
    -n, --null-out     Discard downloaded data.
    -O, --remote-name  Save download data in the current directory.
                       The filename is dereived from URI. If URI
                       ends with '/', 'index.html' is used as a
                       filename. Not implemented yet.
    -3, --spdy3        Only use SPDY/3.
    -t, --timeout=N    Timeout each request after N seconds.
    -w, --window-bits=N  Sets the initial window size to 2**N.
    --cert=CERT        Use the specified client certificate file.
                       The file must be in PEM format.
    --key=KEY          Use the client private key file. The file
                       must be in PEM format.
So, it is time to fire it up against my spdy/3 express.js site:
➜  spdylay git:(master) spdycat -v3 https://localhost:3000
[  0.004] NPN select next protocol: the remote server offers:
          * spdy/3
          * spdy/2
          * http/1.1
          * http/1.0
          NPN selected the protocol: spdy/3
[  0.013] send SYN_STREAM frame <version=3, flags=1, length=102>
          (stream_id=1, assoc_stream_id=0, pri=3)
          :host: localhost:3000
          :method: GET
          :path: /
          :scheme: https
          :version: HTTP/1.1
          accept: */*
          user-agent: spdylay/0.1.0
[  0.021] send GOAWAY frame <version=3, flags=0, length=8>
          (last_good_stream_id=0)
Hrm... that does not seem right. It looks to have successfully connected and determined that the server was spdy/3 capable. But immediately after sending the usual SYN_STREAM, it told the server to GOAWAY. There is no explanation that I can see for this seemingly rude behavior.

So I downgrade my SPDY server to spdy/2 and try again:
➜  spdylay git:(master) spdycat -v https://localhost:3000         
[  0.003] NPN select next protocol: the remote server offers:
          * spdy/2
          * http/1.1
          * http/1.0
          NPN selected the protocol: spdy/2
[  0.009] send SYN_STREAM frame <version=2, flags=1, length=111>
          (stream_id=1, assoc_stream_id=0, pri=3)
          :host: localhost:3000
          :method: GET
          :path: /
          :scheme: https
          :version: HTTP/1.1
          accept: */*
          user-agent: spdylay/0.1.0
[  0.011] recv SETTINGS frame <version=2, flags=0, length=12>
          (niv=1)
          [4(1):100]
[  0.047] recv SYN_REPLY frame <version=2, flags=0, length=80>
          (stream_id=1)
          x-powered-by: Express
          content-type: text/html
          content-length: 629
          :status: 200 OK
          :version: HTTP/1.1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head><title>Express w/ SPDY 3</title><link rel="stylesheet" href="/stylesheets/style.css"/><script src="/main.js"></script></head><body><h1>Express w/ SPDY 3</h1><p>Welcome to Express w/ SPDY 3</p><p><Here>is a huge image (830kb)</Here><br/><jammed>into a height=200</jammed><br/>&lt;img&gt; tag:
</p><img src="images/pipelining.jpg" height="200"/><p><And>another image (956kb)</And><br/><Also>jammed into a small</Also><br/>&lt;img&gt; tag:
</p><img src="images/tube.jpg" height="200"/></body></html>[  0.047] recv DATA frame (stream_id=1, flags=0, length=629)
[  0.047] recv DATA frame (stream_id=1, flags=1, length=0)
[  0.047] send GOAWAY frame <version=2, flags=0, length=4>
          (last_good_stream_id=0)
Hrm... it would be nice if spdycat followed the Javascript and image links in there in order to get a better feel for real SPDY conversations. Still, it works so my problems is not spdycat but something in the spdy/3 conversation.

Since I spdycat is being unhelpful here, I am forced to inspect the SPDY conversation with Wireshark. What I find is that node-spdy is sending back a SETTINGS frame, which ought to be perfectly OK:


It ought to be OK because both Chrome and Firefox not only accept this frame, but seem to process it. spdycat on the other hand, not so much.

Update: Figured out my problem. I had encoded the wrong number of entries in the SETTINGS frame (I advertised 2 but only sent 1). I could ask for a more helpful error message, but the problem is solved and I have my spdy/3 conversation:
➜  spdylay git:(master) spdycat -vvv https://localhost:3000 
[  0.007] NPN select next protocol: the remote server offers:
          * spdy/3
          * spdy/2
          * http/1.1
          * http/1.0
          NPN selected the protocol: spdy/3
[  0.017] send SYN_STREAM frame <version=3, flags=1, length=102>
          (stream_id=1, assoc_stream_id=0, pri=3)
          :host: localhost:3000
          :method: GET
          :path: /
          :scheme: https
          :version: HTTP/1.1
          accept: */*
          user-agent: spdylay/0.1.0
[  0.019] recv SETTINGS frame <version=3, flags=0, length=12>
          (niv=1)
          [4(1):100]
[  0.093] recv SYN_REPLY frame <version=3, flags=0, length=34>
          (stream_id=1)
          :status: 200
          :version: HTTP/1.1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head><title>Express w/ SPDY 3</title><link rel="stylesheet" href="/stylesheets/style.css"/><script src="/main.js"></script></head><body><h1>Express w/ SPDY 3</h1><p>Welcome to Express w/ SPDY 3</p><p><Here>is a huge image (830kb)</Here><br/><jammed>into a height=200</jammed><br/>&lt;img&gt; tag:
</p><img src="images/pipelining.jpg" height="200"/><p><And>another image (956kb)</And><br/><Also>jammed into a small</Also><br/>&lt;img&gt; tag:
</p><img src="images/tube.jpg" height="200"/></body></html>[  0.093] recv DATA frame (stream_id=1, flags=0, length=629)
[  0.094] recv DATA frame (stream_id=1, flags=1, length=0)
[  0.094] send GOAWAY frame <version=3, flags=0, length=8>
          (last_good_stream_id=0)
With that, I can try out my update receive window code by way of the -w switch. I use that switch to specify a 16kb receive window (214 == 16kb):
➜  spdylay git:(master) spdycat -v -w14 -n https://localhost:3000/images/pipelining.jpg
[  0.003] NPN select next protocol: the remote server offers:
          * spdy/3
          * spdy/2
          * http/1.1
          * http/1.0
          NPN selected the protocol: spdy/3
[  0.008] send SETTINGS frame <version=3, flags=0, length=12>
          (niv=1)
          [7(0):16384]
[  0.008] send SYN_STREAM frame <version=3, flags=1, length=118>
          (stream_id=1, assoc_stream_id=0, pri=3)
          :host: localhost:3000
          :method: GET
          :path: /images/pipelining.jpg
          :scheme: https
          :version: HTTP/1.1
          accept: */*
          user-agent: spdylay/0.1.0
[  0.009] recv SETTINGS frame <version=3, flags=0, length=12>
          (niv=1)
          [4(1):100]
[  0.017] recv SYN_REPLY frame <version=3, flags=0, length=34>
          (stream_id=1)
          :status: 200
          :version: HTTP/1.1
[  0.017] recv DATA frame (stream_id=1, flags=0, length=1300)
[  0.017] recv DATA frame (stream_id=1, flags=0, length=1300)
[  0.017] recv DATA frame (stream_id=1, flags=0, length=1300)
[  0.017] recv DATA frame (stream_id=1, flags=0, length=1300)
[  0.017] recv DATA frame (stream_id=1, flags=0, length=1300)
....
          (stream_id=1, delta_window_size=9100)
[  0.227] recv DATA frame (stream_id=1, flags=0, length=1300)
[  0.265] recv DATA frame (stream_id=1, flags=0, length=1300)
[  0.265] recv DATA frame (stream_id=1, flags=0, length=1300)
[  0.265] recv DATA frame (stream_id=1, flags=0, length=1300)
[  0.265] recv DATA frame (stream_id=1, flags=0, length=1300)
[  0.265] recv DATA frame (stream_id=1, flags=0, length=1300)

It actually seems to work right up until the end when the server seemingly fails to send more data. At first glance, my implementation in node-spdy seems to be at fault, but I will investigate further tomorrow.


Day #380