Lecture 5: Reliability and Data Transfer
Lecture 5 discusses reliable and unreliable data transfer in the
Internet. It explains the best-effort nature of packet delivery,
the end-to-end argument, and the timeliness-vs-reliability trade-off
inherent in the design of the Internet. And it discusses three
transport protocols in use in the Internet, UDP, TCP, and QUIC,
and how the provide different degrees of timeliness and reliability,
and offer different services to applications.
Part 1: Reliability and Data Transfer
The first part of the lecture discusses packet loss in the Internet.
It talks about the causes of packet loss, the end-to-end argument,
and the timeliness-reliability trade-off.
00:00:00.000
In this lecture I want to move
00:00:03.396
on from the discussion of connection establishment,
00:00:05.792
and talk instead about reliability and effective
00:00:08.188
data transfer across the network.
00:00:10.000
There are four parts to this.
00:00:12.000
In this first part, I’ll talk briefly
00:00:14.415
about packet loss in the Internet,
00:00:16.485
and the trade-off between reliability and timeliness.
00:00:19.000
Then, I’ll move on to discuss unreliable
00:00:21.633
data using UDP, and talk about the
00:00:24.267
types of applications that benefit from this.
00:00:27.000
In part three, I’ll talk about reliable
00:00:29.780
data transfer with TCP. I’ll discuss the
00:00:32.560
TCP service model, how TCP ensures data
00:00:35.340
is delivered reliably, and some of the
00:00:38.120
limitations of TCP relating to head-of-line blocking.
00:00:41.000
Then, in the final part, I’ll conclude
00:00:43.579
by discussing how QUIC transfers data and
00:00:46.158
how this differs from TCP.
00:00:50.000
I want to start by discussing packet
00:00:52.390
loss in the Internet. What we mean
00:00:54.780
when we say that the Internet provides
00:00:57.171
a best effort service. The end-to-end argument.
00:00:59.561
And the timeliness vs reliability trade-off inherent
00:01:01.951
in the design of the Internet.
00:01:06.000
As we discussed back in lecture 1,
00:01:08.581
the Internet is a best effort packet
00:01:11.162
delivery network.
00:01:12.000
This means that it’s unreliable by design.
00:01:15.000
IP packets can be lost, delayed,
00:01:17.453
reordered, or corrupted in transit. And this
00:01:20.315
is regarded as a feature, rather than
00:01:23.176
a bug. A network that can’t deliver
00:01:26.038
a packet is supposed to discard it.
00:01:29.000
There are many reasons why a packet
00:01:31.580
can get lost or discarded. It could
00:01:34.160
be due to a transmission error,
00:01:36.371
where electrical noise of wireless interference corrupts
00:01:38.951
the packet in transit, making the packet
00:01:41.531
unreadable.
00:01:42.000
Or it could be because too much
00:01:44.297
traffic is arriving at some intermediate link
00:01:46.594
in the network, so an intermediate router
00:01:48.891
runs out of buffer space. If traffic
00:01:51.188
is arriving at a router from several
00:01:53.484
different incoming links, but all going to
00:01:55.781
the same destination, so it’s arriving faster
00:01:58.078
than it can be delivered, a queue
00:02:00.375
of packets will build up, waiting for
00:02:02.672
transmission.
00:02:03.000
If this situation persists, the queue might
00:02:05.396
grow so much that a router runs
00:02:07.792
out of memory, and has no choice
00:02:10.188
but to discard the packets.
00:02:12.000
Or packets could be lost because of
00:02:14.294
a link failure. Or a router bug.
00:02:16.589
Or for other reasons.
00:02:18.000
How often this happens varies significantly.
00:02:22.000
The packet loss rate depends on the
00:02:25.500
type of link.
00:02:27.000
Wireless links tend to be less reliable
00:02:29.513
than wired links, for example. It’s reasonably
00:02:32.026
likely that packet sent over a wireless
00:02:34.538
link, such as WiFi or 4G,
00:02:36.692
will be corrupted in transit due to
00:02:39.205
noise, interference, or cross traffic.
00:02:41.000
This is very unlikely on an Ethernet
00:02:44.118
or optical fibre link.
00:02:46.000
The packet loss rate also depends on
00:02:49.150
the overall quality and robustness of the
00:02:52.300
infrastructure. Countries with well developed and well
00:02:55.450
maintained infrastructure tend to have reliable Internet
00:02:58.600
links; countries with less robust or lower
00:03:01.750
capacity infrastructure tend to see more problems.
00:03:05.000
And the loss rate depends on the
00:03:07.495
protocol. Some protocols intentionally try to push
00:03:09.990
links to capacity, causing temporary overload as
00:03:12.485
they try to find the limit,
00:03:14.623
as they try to find the maximum
00:03:17.118
transmission rate they can achieve.
00:03:19.000
TCP and QUIC do this in many
00:03:21.967
cases, depending on the congestion control algorithm
00:03:24.933
used, as we’ll see in lecture 6.
00:03:28.000
Other applications, such as telephony or video
00:03:30.195
conferencing, tend to have an upper bound
00:03:32.391
in the amount of data they can
00:03:34.586
send.
00:03:35.000
Whatever the reason, though, some packet loss
00:03:38.811
is inevitable.
00:03:40.000
The transport layer needs to recognise this.
00:03:42.852
It must detect packet loss. And,
00:03:45.296
if the application needs reliability, it must
00:03:48.148
retransmit or otherwise repair any lost data.
00:03:53.000
That the Internet provides best effort packet
00:03:56.220
delivery is a result of the end-to-end
00:03:59.440
argument.
00:04:00.000
The end-to-end argument considers whether it’s better
00:04:02.683
to place functionality inside the network or
00:04:05.367
at the end points.
00:04:07.000
For example, rather than provide best effort
00:04:09.848
delivery, we could try to make the
00:04:12.695
network deliver packets reliably. We could design
00:04:15.543
some way to detect packet loss on
00:04:18.391
a particular link, and request that the
00:04:21.239
lost packets be retransmitted locally, somewhere within
00:04:24.086
the network.
00:04:25.000
And, indeed, some network links do this.
00:04:27.736
In WiFi networks, for example, the base
00:04:30.473
station acknowledges packets it receives from the
00:04:33.209
clients, and requests any corrupted packets are
00:04:35.945
re-sent, to correct the error.
00:04:38.000
The problem is, that unless this mechanism
00:04:40.317
is 100% perfect all the time,
00:04:42.302
then end systems will still need to
00:04:44.619
check if the data has been received
00:04:46.936
correctly, and will still need some way
00:04:49.252
of retransmitting packets in the case of
00:04:51.569
problems.
00:04:52.000
And if they’ve got that, why bother
00:04:55.177
with the in-network retransmission and repair?
00:04:58.000
Often times, if you add features into
00:05:01.317
the network routers, they end up duplicating
00:05:04.635
functionality that the network endpoints need to
00:05:07.952
provide anyway.
00:05:09.000
Maybe the performance benefit of adding features
00:05:11.841
to the network is so big that
00:05:14.682
it’s worth while.
00:05:16.000
But often, the right thing to do
00:05:18.893
is to keep the network simple.
00:05:21.373
Omit anything that can be done by
00:05:24.267
the endpoints. And favour simplicity over the
00:05:27.160
absolute optimal performance.
00:05:28.500
The end-to-end argument is one of the
00:05:30.848
defining principles of the Internet. And I
00:05:33.197
think it’s still a good approach to
00:05:35.545
take, when possible. Keep the network simple,
00:05:37.894
if you can
00:05:39.000
The paper linked from the slide talks
00:05:40.867
about this subject in a lot more
00:05:42.733
detail.
00:05:46.000
Irrespective of whether retransmission of lost packets
00:05:49.461
happen between the endpoints or within the
00:05:52.922
network, it takes time.
00:05:55.000
This leads to a fundamental trade-off in
00:05:57.275
the design of the network.
00:05:59.000
If a connection is to be reliable,
00:06:02.755
it cannot guarantee timeliness.
00:06:05.000
It’s not possible to build absolutely perfect
00:06:07.784
network links, that never discard or corrupt
00:06:10.569
packets. There’s always some risk that the
00:06:13.353
data is lost and needs to be
00:06:16.138
retransmitted. And retransmitting a packet will always
00:06:18.922
take time, and so disrupt the timeliness
00:06:21.707
of the delivery.
00:06:23.000
And similarly, if a connection is to
00:06:25.638
be timely, it cannot guarantee reliability.
00:06:28.000
There’s a trade-off to be made.
00:06:32.000
Protocols like UDP are timely but don’t
00:06:34.991
attempt to be reliable. They send packets,
00:06:37.982
and if they get lost, they get
00:06:40.973
lost.
00:06:41.500
TCP and QUIC, on the other hand,
00:06:44.087
aim to be reliable. They send the
00:06:46.674
packets, and if they get lost,
00:06:48.891
they retransmit them.
00:06:50.000
And if the retransmission gets lost? They
00:06:52.656
try again, until the data eventually arrives.
00:06:55.312
As we’ll see in part 3 of
00:06:57.968
this lecture, this causes head of line
00:07:00.624
blocking, making the protocol less timely.
00:07:03.000
And other protocols, such as the Real-time
00:07:06.088
Transport Protocol, RTP, that I’ll talk about
00:07:09.176
in lecture 7, or the partially reliable
00:07:12.265
version of the Stream Control Transport Protocol,
00:07:15.353
SCTP, aim for a middle ground.
00:07:18.000
They try to correct some, but not
00:07:20.742
all, of the transmission errors. The try
00:07:23.483
to achieve a balance, a middle-ground,
00:07:25.833
between timeliness and reliability.
00:07:30.000
The different protocols exist because different applications
00:07:33.871
make different trade-offs. Some applications prefer timeliness,
00:07:37.741
some prefer reliability.
00:07:39.500
For applications like web browsing, email,
00:07:41.849
or messaging, you want to receive all
00:07:44.589
the data. If I’m loading a web
00:07:47.330
site, I’d like it to load quickly,
00:07:50.070
sure. But I prefer for it to
00:07:52.811
load slowly, and be uncorrupted, rather than
00:07:55.551
load quickly with some parts missing.
00:07:58.000
For a video conferencing tool, like Zoom,
00:08:00.608
though, the trade-off is different. If I’m
00:08:03.215
having a conversation with someone, it’s more
00:08:05.823
important that the latency is low,
00:08:08.058
than the picture quality is perfect.
00:08:10.293
The same may be true for gaming.
00:08:13.000
And this has implications for the way
00:08:15.482
we design the network.
00:08:17.000
It means that the IP layer needs
00:08:19.683
to be unreliable. It needs to be
00:08:22.367
a best effort network.
00:08:24.000
If the IP layer is unreliable,
00:08:26.453
protocols like TCP and QUIC can sit
00:08:29.315
on top and retransmit packets to make
00:08:32.176
it reliable. A transport protocol can make
00:08:35.038
an unreliable network into a reliable one.
00:08:38.000
But if the IP layer is reliable,
00:08:40.520
if the IP layer retransmits packets itself,
00:08:43.040
then the network, the applications, the transport
00:08:45.560
protocols, can’t undo that.
00:08:52.000
So this concludes the discussion of packet
00:08:54.441
loss and why the Internet opts to
00:08:56.881
provide an unreliable, best-effort, service.
00:08:58.624
In the next part, I’ll talk about
00:09:01.165
UDP and how to make use of
00:09:03.605
an unreliable transport protocol.
Part 2: Unreliable Data Using UDP
The second part of the lecture discusses UDP. It outlines the UDP
service model, and reviews how to send and receive data using UDP
sockets, and the implications of unreliable delivery for applications
using UDP. It discusses how UDP is suitable for real-time applications
that prioritise low-latency over reliability. And is discusses the use
of UDP as a substrate on which alternative transport protocols can be
implemented, avoiding some of the challenges of protocol ossification.
00:00:00.000
In this part, I’ll move on to
00:00:03.062
discuss how to send unreliable data using
00:00:05.124
UDP.
00:00:06.169
I’ll talk about the UDP service model,
00:00:08.331
how to send and receive packets,
00:00:10.099
and how to layer protocols on top
00:00:12.161
of UDP.
00:00:14.000
UDP provides an unreliable, connectionless, datagram service.
00:00:18.000
It adds only two features on top
00:00:20.581
of the IP layer: port numbers and
00:00:23.163
a checksum.
00:00:24.000
The checksum is used to detect whether
00:00:26.315
the packet has been corrupted in transit.
00:00:28.631
If so, the packet will be discarded
00:00:30.946
by the UDP code in the operating
00:00:33.262
system of the receiver, and won’t be
00:00:35.577
delivered to the application.
00:00:37.000
The port numbers determine what application receives
00:00:39.850
the UDP datagrams when they arrive at
00:00:42.700
the destination. They’re set by the bind()
00:00:45.550
call, once the socket has been created.
00:00:48.500
The Internet Assigned Numbers Authority, the IANA,
00:00:51.074
maintains a list of well-known UDP port
00:00:53.648
numbers which you should use for particular
00:00:56.223
applications. This is linked from the bottom
00:00:58.797
of the slide.
00:01:00.000
UDP is very minimal. It doesn’t provide
00:01:02.935
reliability, or ordering, or congestion control.
00:01:05.450
It just delivers packets to an application,
00:01:08.385
that’s bound to a particular port.
00:01:11.000
Mostly, UDP is used as a substrate.
00:01:13.457
It’s a base on which higher-layer protocols
00:01:15.915
are built. QUIC is an example of
00:01:18.372
this, as we discussed in the last
00:01:20.830
lecture. Others are the Real-time Transport Protocol,
00:01:23.287
and the DNS protocol, that we’ll talk
00:01:25.745
about later in the course.
00:01:30.000
UDP is connectionless. It’s got no notion
00:01:32.633
of clients or servers, or of establishing
00:01:35.267
a connection before it can be used.
00:01:38.000
To use UDP, you first create a
00:01:40.464
socket.
00:01:41.566
Then you call bind(), to choose the
00:01:44.130
local port on which that socket listens
00:01:46.594
for incoming datagrams.
00:01:47.000
They you call recvfrom() if you want
00:01:49.404
to receive a datagram on that socket,
00:01:51.809
or sendto() if you want to send
00:01:54.213
a datagram.
00:01:55.000
You don’t need to connect. You don’t
00:01:58.013
need to accept connections. You just send
00:02:01.026
and receive data. And maybe that data
00:02:04.039
is delivered.
00:02:05.000
When you’re finished, you close the socket.
00:02:09.000
Protocols that run on top of UDP,
00:02:11.631
such as QUIC, might add support for
00:02:14.262
connections, reliability, ordering, congestion control, and so
00:02:16.893
on, but UDP itself supports none of
00:02:19.524
this.
00:02:23.000
To send a UDP datagram, you use
00:02:25.483
the sendto() function.
00:02:26.547
This work similarly to the send() function
00:02:29.130
you used to send data over a
00:02:31.613
TCP connection in the labs, except that
00:02:34.096
it takes two additional parameters to indicate
00:02:36.579
the address to which the datagram should
00:02:39.062
be sent, and the size of that
00:02:41.545
address.
00:02:42.000
When using TCP, you establish a connection
00:02:44.517
between a socket, bound to a local
00:02:47.034
address and port, and a server listening
00:02:49.551
on a particular port on some remote
00:02:52.068
IP address. And once the connection is
00:02:54.585
established, all the data goes over that
00:02:57.102
connection, to the same destination.
00:02:59.000
UDP is not like that.
00:03:02.000
Every time you call sendto(), you specify
00:03:04.822
the destination address. Every packet you send
00:03:07.644
from a UDP socket can go to
00:03:10.466
a different destination, if you want.
00:03:12.884
There’s no notion of connections.
00:03:15.000
Now, you can call connect() on a
00:03:17.609
UDP socket, but confusingly it doesn’t create
00:03:20.218
a connection. Rather, it just remembers the
00:03:22.827
address you give it, so you can
00:03:25.436
call send(), rather than sendto() in future,
00:03:28.045
to save having to specify the address
00:03:30.655
each time.
00:03:33.000
To receive a UDP datagram, you call
00:03:36.220
the recvfrom() function, as shown on the
00:03:39.440
slide.
00:03:40.000
This is like the recv() call you
00:03:42.426
use with TCP, but again it has
00:03:44.851
two additional parameters. These allow it to
00:03:47.277
record the address that the received datagram
00:03:49.702
came from, so you can use them
00:03:52.128
in the sendto() function to send a
00:03:54.553
reply.
00:03:55.000
You can also call recv(), rather than
00:03:57.826
recvfrom(), like with TCP, and it works,
00:04:00.652
but it doesn’t give you the return
00:04:03.478
address, so it’s not very useful.
00:04:06.000
The important point with UDP is that
00:04:08.887
packets can be lost, delayed, or reordered
00:04:11.775
in transit, and UDP doesn’t attempt to
00:04:14.662
recover from this.
00:04:16.000
Just because you send a datagram,
00:04:18.000
doesn’t mean it will arrive. And if
00:04:20.333
datagrams do arrive, they won’t necessarily arrive
00:04:22.667
in the order sent.
00:04:28.500
Unlike TCP, where data written to a
00:04:31.342
connection in a single send() call might
00:04:34.185
end up being split across multiple read()
00:04:37.027
calls at the receiver, a single UDP
00:04:39.870
send generates exactly one datagram.
00:04:42.000
If it’s delivered at all, the data
00:04:44.826
sent by a single call to sendto()
00:04:47.652
will be delivered by a single call
00:04:50.478
to recvfrom(). UDP doesn’t split messages.
00:04:53.000
But UDP is otherwise unreliable.
00:04:55.000
Datagrams can be lost, delayed, reordered,
00:04:58.240
or duplicated in transit.
00:05:00.500
Data sent with sendto() might never arrive.
00:05:03.650
Or it might arrive more than once.
00:05:07.000
Or data sent in consecutive calls to
00:05:09.911
sendto() might arrive out of order,
00:05:12.405
with data sent later arriving first.
00:05:15.000
UDP doesn’t attempt to correct any of
00:05:18.811
these things.
00:05:20.000
The protocol you build on top of
00:05:22.352
UDP might choose to do so.
00:05:24.368
For example, we saw that QUIC adds
00:05:26.820
packet sequence numbers and acknowledgement frames to
00:05:29.172
the data it sends within UDP packets.
00:05:31.524
This lets it put the data back
00:05:33.876
into the correct order, and retransmit any
00:05:36.228
missing packets.
00:05:37.000
But there’s no requirement that the protocol
00:05:39.567
running over UDP is reliable.
00:05:41.500
RTP, the Real-time Transport Protocol, that’s used
00:05:44.557
for video conferencing apps, puts sequence numbers
00:05:47.614
and timestamps inside the UDP datagrams it
00:05:50.671
sends, so it can know if any
00:05:53.729
data is missing, and it can conceal
00:05:56.786
loss or reconstruct the packet playout time,
00:05:59.843
but it generally doesn’t retransmit missing data.
00:06:03.000
UDP gives the application the choice of
00:06:05.557
building reliability, if it wants it.
00:06:07.748
But it doesn’t require that the applications
00:06:10.304
deliver data reliably.
00:06:14.000
Applications that use UDP need to organise
00:06:16.753
the data they send, so it’s useful
00:06:19.507
if some data is lost.
00:06:21.473
Different applications do this in different ways,
00:06:24.327
depending on their needs.
00:06:26.000
QUIC, for example, organises the data into
00:06:29.453
sub-streams within a connection, and retransmits missing
00:06:32.907
data.
00:06:33.500
Video conferencing applications tend to do something
00:06:36.913
different.
00:06:37.500
The way video compression works, is that
00:06:40.300
the codec sends occasional full frames of
00:06:43.100
video, known as I-frames, index frames,
00:06:45.500
every few seconds. And in between these
00:06:48.300
it sends only the differences from the
00:06:51.100
previous frame, known as P-frames, predicted frames.
00:06:54.000
In a video call, it’s common for
00:06:56.433
the background to stay the same,
00:06:58.517
while the person moves in the foreground,
00:07:00.950
so a lot of the frame is
00:07:03.382
the same each time. By only sending
00:07:05.815
the differences, video compression saves bandwidth.
00:07:08.000
But this affects how the application treats
00:07:10.730
the different datagrams.
00:07:12.000
If a UDP datagram containing a predicted
00:07:14.709
frame is lost, it’s not that important.
00:07:17.417
You’ll get a glitch in one frame
00:07:20.126
of video.
00:07:21.000
But if a UDP datagram containing an
00:07:23.465
index frame, or part of an index
00:07:25.929
frame, is lost, then that matters a
00:07:28.394
lot more because the next few seconds
00:07:30.858
worth of video are predicted based on
00:07:33.323
that index frame. Losing an index frame
00:07:35.788
corrupts several seconds worth of video.
00:07:38.000
For this reason, many video conferencing apps
00:07:40.736
running over UDP try to determine if
00:07:43.473
missing packets contained an index frame or
00:07:46.209
not. And they try to retransmit index
00:07:48.945
frames, but not predicted frames.
00:07:51.000
The details of how they do this
00:07:53.429
aren’t really important, unless you’re building a
00:07:55.859
video conferencing app.
00:07:57.000
What’s important though, is that UDP gives
00:07:59.826
the application flexibility to be unreliable for
00:08:02.652
some of the datagrams it sends,
00:08:05.074
while trying to deliver other datagrams reliably.
00:08:08.000
You don’t have that flexibility with TCP.
00:08:12.500
UDP is harder to use, because it
00:08:14.775
provides very few services to help your
00:08:17.050
application, but it’s more flexible because you
00:08:19.325
can build exactly the services you need
00:08:21.600
on top of UDP.
00:08:26.000
Fundamentally, UDP doesn’t make any attempt to
00:08:30.153
provide sequencing, reliability, timing recovery, or congestion
00:08:34.307
control.
00:08:35.000
It just delivers datagrams on a best
00:08:37.256
effort basis.
00:08:38.000
It lets you build any type of
00:08:40.753
transport protocol you want, running inside UDP
00:08:43.507
packets.
00:08:44.000
Maybe that transport protocols has sequence numbers
00:08:47.072
and acknowledgements, and retransmits some or all
00:08:50.144
of the lost packets.
00:08:52.000
Maybe, instead, it uses error correcting codes,
00:08:55.072
to allow some of the packets to
00:08:58.144
be repaired without retransmission.
00:09:00.000
Maybe it includes timestamps, so the receiver
00:09:02.858
can carefully reconstruct the timing.
00:09:05.000
Maybe it contains other information.
00:09:06.967
The point is that UDP gives you
00:09:09.820
flexibility, but at the cost of having
00:09:12.573
to implement these features yourself. At the
00:09:15.327
cost of adding complexity.
00:09:18.000
There’s a lot to think about when
00:09:20.580
writing a UDP-based protocol or a UDP-
00:09:23.161
based application.
00:09:23.898
If you use a transport protocol,
00:09:26.210
like QUIC or like RTP, that runs
00:09:28.790
over UDP, then the designers of that
00:09:31.371
protocol have made these decisions, and will
00:09:33.951
have given you a library you can
00:09:36.531
use.
00:09:37.000
If not, if you’re designing your own
00:09:39.777
protocol that runs over UDP, then the
00:09:42.553
IETF has written some guidelines, highlighting the
00:09:45.330
issues you need to think about,
00:09:47.710
in RFC 8085.
00:09:49.000
Please read this before you try and
00:09:50.721
write applications that use UDP. There are
00:09:52.442
a lot of non-obvious things that can
00:09:54.162
catch you out.
00:09:58.000
So, that concludes our discussion of UDP.
00:10:00.763
In the next part, I’ll talk about
00:10:03.526
how TCP delivers data reliably.
Part 3: Reliable Data With TCP
The third part of the lecture discusses TCP. It outlines the TCP
service model and shows how to send and receive data using a TCP
connection. It explains how TCP ensures reliable and order data
transfer, using sequence numbers and acknowledgements. And it
explains TCP loss detection using timeouts and triple-duplicate
acknowledgements. The issue of head-of-line blocking in TCP
connections is discussed, as an example of the timeliness vs
reliability trade-off.
00:00:00.000
In this part I want to talk
00:00:02.964
about how reliable data is delivered using
00:00:05.929
TCP connections. I’ll talk about the TCP
00:00:08.893
service model, how TCP uses sequence numbers
00:00:11.858
and acknowledgments, and how packet loss detection
00:00:14.822
and recovery works in TCP.
00:00:17.040
Thinking about the TCP service model,
00:00:19.637
as we've seen in previous lectures,
00:00:22.234
TCP provides a reliable, ordered, byte stream
00:00:25.263
delivery service that runs over IP.
00:00:27.960
The applications write data into the TCP
00:00:30.485
socket, that buffers it up in the
00:00:33.009
sending system, and then delivers it over
00:00:35.534
a sequence of data segments over the
00:00:38.059
IP layer.
00:00:38.880
When these data packets, these data segments,
00:00:41.457
are received, they are accumulated in a
00:00:44.035
receive buffer at the receiver. If anything
00:00:46.612
is lost, or arrives out of order,
00:00:49.190
it's re-transmitted, and eventually the data is
00:00:51.767
delivered to the application.
00:00:53.340
The data delivered to the application is
00:00:55.897
always delivered reliably, and in the order
00:00:58.455
sent.
00:00:58.920
If something is lost, if something needs
00:01:01.910
to be re-transmitted, this stalls the delivery
00:01:04.900
of the later data, to make sure
00:01:07.890
that everything is always delivered in order.
00:01:10.980
TCP delivers, as we say, an order,
00:01:14.690
reliable, byte stream.
00:01:16.380
After the connection has been established,
00:01:18.619
after the SYN, SYN-ACK, ACK handshake,
00:01:20.858
the client and the server can send
00:01:23.470
and receive data.
00:01:24.690
The data can flow in either direction
00:01:27.624
within that TCP connection.
00:01:29.400
It’s usual that the data follows a
00:01:31.908
request response pattern. You open the connection.
00:01:34.417
The client sends a request to the
00:01:36.925
server. The server replies with a response.
00:01:39.433
The client makes another request. The server
00:01:41.942
replies with another response, and so on.
00:01:44.550
But TCP doesn't make any requirements on
00:01:46.912
this. There’s no requirement that the data
00:01:49.274
flows in a request response pattern,
00:01:51.299
and the client and the server can
00:01:53.661
send data in any order they feel
00:01:56.023
like.
00:01:56.460
TCP does ensure that the data is
00:01:59.013
delivered reliably, and in the order it
00:02:01.566
was sent, though.
00:02:02.760
TCP sends acknowledgments for each data segment
00:02:05.387
as it's received. And if any data
00:02:08.013
is lost, it retransmits that lost data.
00:02:10.740
And if segments are delayed and arrive
00:02:13.519
out of order, or if a segment
00:02:16.298
has to be re-transmitted and arrives out
00:02:19.078
of order, then TCP will reconstruct the
00:02:21.857
order before giving the segments back to
00:02:24.636
the application.
00:02:25.530
In order to send data over a
00:02:28.105
TCP connection you use the send() function.
00:02:30.780
This transmits a block of data over
00:02:33.681
the TCP connection. The parameters are the
00:02:36.583
file descriptor representing the socket – the
00:02:39.484
TCP socket, the data, the length of
00:02:42.386
the data, and a flag. And the
00:02:45.287
flag field is usually zero.
00:02:47.460
The send() function blocks until all the
00:02:49.897
data can be written.
00:02:51.390
And it might take a significant amount
00:02:54.190
of time to do this, depending on
00:02:56.990
the available capacity of the network.
00:02:59.490
It also might not be able to
00:03:01.545
send all the data.
00:03:02.820
If the connection is congested, and can't
00:03:05.528
accept any more data, then the send()
00:03:08.236
function will return to indicate that it
00:03:10.944
wasn't able to successfully send all the
00:03:13.652
data that was requested.
00:03:15.300
The return value from the send() function
00:03:17.029
is the amount of data it actually
00:03:18.758
managed to send on the connection.
00:03:20.239
And that can be less than the
00:03:21.968
amount it was asked to send.
00:03:23.550
In which case, you need to figure
00:03:25.852
out what data was not sent,
00:03:27.825
by looking at the return value,
00:03:29.798
and the amount you asked for,
00:03:31.771
and re-send just the missing part in
00:03:34.072
another call.
00:03:34.830
Similarly, if an error occurs, if the
00:03:37.600
connection has failed for some reason,
00:03:39.974
the send() function will return -1,
00:03:42.348
and it will set the global variable
00:03:45.117
errno to indicate that.
00:03:46.800
On the receiving side you call the
00:03:49.552
recv() function to receive data on a
00:03:52.304
TCP connection.
00:03:53.190
The recv() function blocks until data is
00:03:56.455
available, or until the connection is closed.
00:03:59.820
It’s passed a size,
00:04:02.250
It’s passed a buffer, buf, and the
00:04:04.667
size of the buffer, BUFLEN, and it
00:04:07.083
reads up to BUFLEN bytes of data.
00:04:09.600
And what it returns is the number
00:04:12.543
of bytes of data that were read.
00:04:15.487
Or, if the connection was closed,
00:04:18.010
it returns zero. Or, if an error
00:04:20.953
occurs, it returns -1, and again sets
00:04:23.897
global variable errno to indicate what happened.
00:04:26.940
When a recv() call finishes, you have
00:04:29.253
to check these three possibilities. You have
00:04:31.566
to check if the return value is
00:04:33.879
zero, to indicate that the connection is
00:04:36.192
closed and you've successfully received all the
00:04:38.505
data in that connection. At which point,
00:04:40.817
you should also close the connection.
00:04:42.900
You have to check if the return
00:04:45.221
value is minus one, in which case
00:04:47.543
an error has occurred, and that connection
00:04:49.864
has failed, and you need to somehow
00:04:52.185
handle that error.
00:04:53.280
And you need to check if it's
00:04:55.453
some other value, to indicate that you've
00:04:57.626
received some data, and then you need
00:04:59.798
to process that data.
00:05:01.140
What's important is to remember that the
00:05:03.999
recv() call just gives you that data
00:05:06.857
in the buffer. If the return value
00:05:09.716
from receive is 157, this indicates that
00:05:12.575
the buffer has 157 bytes of data
00:05:15.433
in it.
00:05:16.350
What the recv() called doesn't ever do,
00:05:19.271
is add a terminating null to that
00:05:22.193
buffer.
00:05:22.710
Now, if you're careful that doesn't matter,
00:05:25.513
because you know how much data is
00:05:28.315
in the buffer, and you can explicitly
00:05:31.118
process the data up to that length.
00:05:34.020
But, a common problem with TCP-based applications,
00:05:37.432
is that they treat the data as
00:05:40.843
if it was a string.
00:05:43.380
They pass it to the printf() call
00:05:45.892
using %s as if it were a
00:05:48.404
string, or they pass it to function
00:05:50.917
like strstr() to search for a string
00:05:53.429
within it, or strcpy(), or something like
00:05:55.941
that.
00:05:56.400
And the problem is the string functions
00:05:59.159
assume there’s a terminating null, and the
00:06:01.919
recv() call doesn't provide one.
00:06:03.990
If you're going to pass the data
00:06:06.478
that's returned from a recv() call to
00:06:08.965
one of the C string functions,
00:06:11.097
you need to explicitly add that null
00:06:13.585
yourself.
00:06:14.040
You need to look at the buffer,
00:06:16.465
add the null at the end,
00:06:18.544
after the last byte which was successfully
00:06:20.969
received. If you don't do, this the
00:06:23.394
string functions will just run off the
00:06:25.819
end of the buffer and you'll get
00:06:28.244
a buffer overflow attack.
00:06:29.730
And this is a significant security risk.
00:06:32.490
It’s one of the biggest security problems
00:06:35.249
with network code using C. It’s misusing
00:06:38.009
these buffers, accidentally using one of the
00:06:40.768
string functions, and it just reads off
00:06:43.528
the end of buffer, and who knows
00:06:46.287
what it processes.
00:06:48.570
When you send data using TCP,
00:06:51.484
the send() call enqueues the data for
00:06:54.884
transmission.
00:06:55.470
The operating system, the TCP code in
00:06:57.914
the operating system, splits the data you've
00:07:00.358
written using the various send() calls into
00:07:02.801
what’s known as segments, and puts each
00:07:05.245
of these into a TCP packet.
00:07:07.440
The TCP packets are sent in IP
00:07:10.368
packets. And TCP runs a congestion control
00:07:13.296
algorithm to decide when it can send
00:07:16.223
those packets.
00:07:17.160
Each TCP segment, each segment is in
00:07:20.047
a TCP packet. The TCP packets have
00:07:22.933
a header, which has a sequence number.
00:07:25.920
When the connection setup handshake happens,
00:07:28.438
in the SYN and the SYN-ACK packets,
00:07:31.375
the connection agrees the initial sequence numbers;
00:07:34.313
agrees the starting value for the sequence
00:07:37.250
numbers.
00:07:37.770
If you’re the client, for example;
00:07:40.279
the client picks a sequence number at
00:07:43.205
random, and sends this in its SYN
00:07:46.132
packet.
00:07:46.650
And then when it starts sending data,
00:07:48.966
the next data packet has a sequence
00:07:51.281
number that is one higher than that
00:07:53.597
in the SYN packet.
00:07:55.020
And, as it continues to send data,
00:07:57.851
the sequence numbers increase by the number
00:08:00.682
of data bytes sent.
00:08:02.400
So, for example, if the initial sequence
00:08:05.568
number was 1001, just picked randomly,
00:08:08.284
and it sends 30 bytes of data
00:08:11.452
in the packet, then the next sequence
00:08:14.620
number will be 1031.
00:08:16.530
The sequence number spaces are separate for
00:08:18.803
each in each direction. The sequence numbers
00:08:21.075
the client uses increase based on the
00:08:23.348
initial sequence number the client sent the
00:08:25.621
SYN packet.
00:08:26.370
The sequence numbers the server use,
00:08:28.603
start based on the initial sequence number
00:08:31.207
the server sent in the SYN-ACK packet,
00:08:33.812
and increase based on the amount of
00:08:36.417
data the server is sending. The two
00:08:39.022
number spaces are unrelated.
00:08:41.610
What's important is that calls to send()
00:08:45.578
don't map directly onto TCP segments.
00:08:49.080
If the data which is given to
00:08:51.766
a send() call is too big to
00:08:54.452
fit into one TCP segment, then the
00:08:57.138
TCP code will split it across several
00:08:59.824
segments; it'll split it across several packets.
00:09:02.610
Similarly, if the data you send,
00:09:04.890
that data you give the send() call
00:09:07.550
is quite small, TCP might not send
00:09:10.210
it immediately.
00:09:11.070
It might buffer it up, combine it
00:09:13.702
with data sent as part of a
00:09:16.335
later send() call. And combine it,
00:09:18.591
and send it in a single larger
00:09:21.224
segment, a single larger TCP packet.
00:09:23.580
This is an idea known as Nagle’s
00:09:26.593
algorithm. It's there to improve efficiency by
00:09:29.605
only sending big packets, because there's a
00:09:32.618
certain amount of overhead for each packet.
00:09:35.730
Each packet that’s sent by TCP has
00:09:38.030
a TCP header. It’s got an IP
00:09:40.330
header. It's got the Ethernet or the
00:09:42.630
WiFi headers depending on the link layer.
00:09:45.030
And that adds a certain amount of
00:09:47.046
overhead. It’s about, I think, 40 bytes
00:09:49.063
per packet. So if you're only sending
00:09:51.079
a small amount of data, that's a
00:09:53.096
lot of overhead, a lot of wasted
00:09:55.112
data.
00:09:55.500
So TCP, with the Nagle algorithm,
00:09:57.344
tries to combine these packets into larger
00:09:59.495
packets when it can. But, of course,
00:10:01.645
this adds some delay. It’s got to
00:10:03.796
wait for you to send more data;
00:10:05.947
wait to see if it can form
00:10:08.098
a bigger packet.
00:10:09.120
If you really need low latency,
00:10:11.416
you can disable the Nagle algorithm.
00:10:13.712
There’s a socket option called TCP_NODELAY,
00:10:16.007
and we see the code on the
00:10:18.686
slide to show how to use that.
00:10:21.364
So you create the socket, you
00:10:23.760
establish the connection, and then you call
00:10:26.673
the TCP_NODELAY option and that turns this
00:10:29.586
off. And this means that every time
00:10:32.500
you send() on the socket, it immediately
00:10:35.413
gets sent as quickly as possible.
00:10:38.010
One implication of this behaviour, though,
00:10:40.548
where TCP can either split data written
00:10:43.508
in a single send() across multiple segments,
00:10:46.468
or where it can combine several send()
00:10:49.429
calls into a single segment, is that
00:10:52.389
the data returned by the recv() calls
00:10:55.350
doesn't always correspond to a single send().
00:10:58.410
When you call recv(), you might get
00:11:00.612
just part of a message. And you
00:11:02.815
need to call recv() again to get
00:11:05.017
the rest of the message.
00:11:06.690
Or you may get several messages in
00:11:10.057
one recv() call.
00:11:12.600
When you're using TCP, the recv() calls
00:11:14.934
return the data reliably, and they return
00:11:17.269
the data in the order that it
00:11:19.603
was sent.
00:11:20.370
But what they don't do is frame
00:11:23.003
the data. What they don't do is
00:11:25.636
preserve the message boundaries.
00:11:27.240
For example, if we're using HTTP,
00:11:29.577
which we see, we see an example
00:11:32.304
of an HTTP message that might be
00:11:35.030
sent,
00:11:35.520
an HTTP response that might be sent,
00:11:38.525
by a web server back to a
00:11:41.531
browser.
00:11:42.060
If we're using HTTP, what we would
00:11:44.502
like is that the whole response is
00:11:46.943
received in one go. So if we're
00:11:49.385
implementing a web browser we just call
00:11:51.826
recv() on the TCP connection
00:11:53.670
and we get all of the headers,
00:11:55.590
and all of the body, in just
00:11:57.509
in just one call to recv() and
00:11:59.429
we can then parse it, and process
00:12:01.349
it, and deal with it.
00:12:02.820
TCP doesn't guarantee this, though.
00:12:06.120
It can split the messages arbitrarily,
00:12:08.551
depending on how much data was in
00:12:11.387
the packets, what size packets the underlying
00:12:14.223
link layers can send, and on the
00:12:17.059
available capacity of the network depending on
00:12:19.895
the congestion control.
00:12:21.210
And it can split the packets at
00:12:23.652
arbitrary points.
00:12:24.450
For example, if we look at the
00:12:26.416
slide, we see that the headers,
00:12:28.101
some of them are labeled in red,
00:12:30.067
some are in blue,
00:12:31.290
some of the body is in blue,
00:12:33.217
some the rest of the body is
00:12:35.143
in green. And it could be that
00:12:37.070
the TCP connection splits the data up,
00:12:38.996
so that the first recv() call just
00:12:40.923
gets the part of the headers highlighted
00:12:42.850
in red,
00:12:43.500
ending halfway through the “ETag:” line.
00:12:46.470
And then you have to call recv()
00:12:48.343
again. And then you get the part
00:12:50.217
of the message highlighted in blue,
00:12:51.823
which contains the rest of the headers
00:12:53.696
and the first part of the body.
00:12:55.570
Then you have to call recv() again,
00:12:57.443
to get the rest of the message
00:12:59.317
that's highlighted in green on the slide.
00:13:01.290
And this makes it much harder to
00:13:03.675
parse; much harder for the programmer.
00:13:05.820
Because you have to look at the
00:13:07.868
data you've got, parse it, check to
00:13:09.916
see if you've got the whole message,
00:13:11.965
check if you've received the complete headers,
00:13:14.013
check to see if you've received the
00:13:16.061
complete body. And you have to handle
00:13:18.109
the fact that you might have partial
00:13:20.157
messages.
00:13:20.550
And it's something which makes it a
00:13:22.503
little bit hard to debug, because if
00:13:24.455
you only send small messages,
00:13:25.950
if you're sending packets which are only
00:13:28.349
like 1000 bytes, or so, they’re probably
00:13:30.747
small enough to fit in a single
00:13:33.146
packet, and they always get delivered in
00:13:35.545
one go.
00:13:36.330
It’s only when you start sending a
00:13:39.251
larger packets, or sending lots of data
00:13:42.172
over connection so things get split up
00:13:45.092
due to congestion control, that you start
00:13:48.013
to see this behaviour where the messages
00:13:50.934
get split at arbitrary points.
00:13:54.120
So as we've seen, the TCP segments
00:13:56.975
contain sequence numbers, and the sequence numbers
00:13:59.830
count up with the number of bytes
00:14:02.684
being sent.
00:14:03.600
Each TCP segment also has an acknowledgement
00:14:08.474
number.
00:14:09.270
When a TCP segment is sent,
00:14:12.926
it acknowledges any segments that have previously
00:14:17.191
been received.
00:14:18.510
So if,
00:14:20.520
if a TCP endpoint has received some
00:14:24.347
data on a TCP connection,
00:14:27.180
when it sends its next packet,
00:14:29.415
the ACK bit will be set in
00:14:32.023
the TCP header, to indicate that the
00:14:34.630
acknowledgement number is valid, and the acknowledgement
00:14:37.237
number will have a value indicating the
00:14:39.845
next sequence number it is expecting.
00:14:42.180
That is, the next contiguous byte it's
00:14:45.744
expecting on the connection.
00:14:47.880
So, in the example, we have a
00:14:50.499
slightly unrealistic example in that the connection
00:14:53.119
is sending one byte at a time,
00:14:55.738
and the first packet is sent with
00:14:58.357
sequence number five.
00:14:59.580
And then the next packet is sent
00:15:01.708
with sequence number six, and then seven,
00:15:03.836
and eight, and nine, and ten,
00:15:05.660
and so on. And this is what
00:15:07.788
might happen with an ssh connection,
00:15:09.612
where each key you type generates a
00:15:11.740
TCP segment, with just the one key
00:15:13.868
press in it.
00:15:14.880
And when those packets are received at
00:15:18.301
host B, it sends a TCP segment
00:15:21.722
with the acknowledgement bit set, acknowledging what's
00:15:25.143
expected next.
00:15:26.220
So when it receives the TCP packet
00:15:28.960
with sequence number five, and one byte
00:15:31.699
of data in it, it sends an
00:15:34.439
acknowledgement saying it got it, and it's
00:15:37.179
expecting the packet with sequence number six
00:15:39.919
next.
00:15:40.410
When it receives the packet with sequence
00:15:43.137
number six, and one byte of data
00:15:45.865
in it, it sends an acknowledgement saying
00:15:48.592
it's expecting seven. And so on.
00:15:51.030
TCP only ever acknowledges the next contiguous
00:15:56.000
sequence number expected.
00:15:58.230
And if a packet is lost,
00:16:02.037
subsequent packets generate duplicate acknowledgments.
00:16:05.310
So in this case, packet five was
00:16:07.916
sent. It got to the receiver,
00:16:10.149
and that sent the acknowledgement saying it
00:16:12.754
expected six. Six was sent, arrived at
00:16:15.360
the receiver, so the acknowledgement says it
00:16:17.966
expects seven.
00:16:18.810
Seven was sent, arrives at the receiver,
00:16:21.971
sends the acknowledgement saying it expects
00:16:24.780
eight. Eight was sent, and gets lost.
00:16:29.460
Nine was sent, and arrives at the
00:16:32.549
receiver.
00:16:33.090
At this point, the receiver’s received the
00:16:35.856
packets with sequence numbers five, six,
00:16:38.227
and seven; eight is missing; and nine
00:16:40.993
has arrived. So the next contiguous sequence
00:16:43.759
number it's expecting is still eight.
00:16:46.230
So it sends an acknowledgement saying “I’m
00:16:49.567
expecting sequence number eight next”.
00:16:52.050
The packet sent, the next packet sent,
00:16:55.063
has sequence number 10. This arrives,
00:16:57.646
the acknowledgement goes back saying “I still
00:17:00.659
haven't got eight, I’m still expecting eight”,
00:17:03.672
and this carries on. TCP keeps sending
00:17:06.685
duplicate acknowledgments while there’s a gap in
00:17:09.698
the sequence number space.
00:17:11.520
In addition, we don't show it here,
00:17:14.008
but TCP can also send delayed acknowledgments,
00:17:16.497
where it only acknowledges every second packet.
00:17:18.985
In this case the acknowledgments might go,
00:17:21.473
six, eight. The packet with sequence number
00:17:23.962
five is sent, and it acknowledges six.
00:17:26.550
Packet with number six is sent,
00:17:28.784
and arrives, and packet number seven is
00:17:31.391
sent, and then it sends the acknowledgement
00:17:33.998
saying it's expecting eight. So it doesn't
00:17:36.604
have to send every acknowledgement, it can
00:17:39.211
sent every other acknowledgement to reduce the
00:17:41.818
overheads.
00:17:43.290
TCP uses the acknowledgments to detect packet
00:17:47.215
loss; to detect when segments are lost.
00:17:51.240
There’s two ways in which it does
00:17:54.171
this.
00:17:54.690
The first is that if it sends
00:17:57.626
data, but for some reason the acknowledgments
00:18:00.561
stop entirely.
00:18:01.500
This is a sign that either the
00:18:03.866
receiver has failed,
00:18:04.980
And, you know, the packets are being
00:18:07.303
delivered to the receiver, but the application
00:18:09.627
has crashed, and there's nothing there to
00:18:11.950
receive the data, to reply.
00:18:13.710
Or it's an indication that the network
00:18:15.806
connection has failed, and the packets are
00:18:17.903
just not reaching the receiver.
00:18:19.500
So if TCP is sending data,
00:18:22.042
and it's not getting any acknowledgments back,
00:18:25.008
after a while it times out and
00:18:27.973
uses this as an indication that the
00:18:30.939
connection has failed.
00:18:32.310
Alternatively, it can be sending data,
00:18:35.375
and if some data is lost,
00:18:38.440
but the later segments arrive, then TCP
00:18:42.015
will start sending the duplicate acknowledgments.
00:18:45.180
Again, back to the example, we see
00:18:47.908
that packet eight is lost, packet nine
00:18:50.635
arrives, and the sequence number, the acknowledgement
00:18:53.363
number, comes back says “I’m expecting sequence
00:18:56.091
number eight”.
00:18:56.970
And packet ten is sent and it
00:18:59.386
arrives, and it still says “I’m still
00:19:01.803
expecting packet with sequence number eight”,
00:19:03.874
and this just carries on.
00:19:05.700
And, eventually, TCP gets what's known as
00:19:08.703
a triple duplicate acknowledgement. It’s got the
00:19:11.706
original acknowledgement saying it's expecting packet eight,
00:19:14.708
and then three duplicates following that,
00:19:17.282
so four packets in total, all saying
00:19:20.285
“I’m still expecting packet eight”.
00:19:22.530
And what this indicates, is that data
00:19:25.345
is still arriving, but something's got lost.
00:19:28.260
It only generates acknowledgements when a new
00:19:30.815
packet arrives, so if we keep seeing
00:19:33.370
acknowledgments indicating the same thing, this indicates
00:19:35.925
that new packets arriving, because that's what
00:19:38.480
triggers the acknowledgement to be sent,
00:19:40.770
but there's still a packet missing,
00:19:43.414
and it's telling us which one it's
00:19:46.499
expecting.
00:19:47.040
At that point TCP assumes that the
00:19:50.412
packet has got lost, and retransmits that
00:19:53.784
segment. It retransmits the packet with sequence
00:19:57.157
number eight.
00:19:59.220
Why does it wait for a triple
00:20:02.456
duplicate acknowledgement?
00:20:03.480
Why does it not just retransmit it
00:20:06.156
immediately. when it sees a duplicate?
00:20:08.550
Well, the example we see here illustrates
00:20:12.820
that.
00:20:13.530
In this case, a packet with sequence
00:20:15.972
number five is sent, containing one byte
00:20:18.415
of data, and it arrives, and the
00:20:20.857
receiver acknowledges it, saying it's expecting six.
00:20:23.400
And six is sent, and it arrives,
00:20:26.270
and the receiver acknowledges it, indicating it’s
00:20:29.140
expecting seven.
00:20:30.060
And packet seven is sent, and it's
00:20:33.029
delayed. And packet eight is sent,
00:20:35.575
and eventually arrives at the receiver.
00:20:38.220
Now the receiver hasn't received packet seven
00:20:41.105
yet, so it sends an acknowledgement which
00:20:43.989
says “I’m still expecting seven”. So that's
00:20:46.874
a duplicate acknowledgement.
00:20:48.210
At that point packet seven, which was
00:20:51.735
delayed, finally does arrive.
00:20:53.850
Now packet seven has arrived, packet eight
00:20:56.455
had arrived previously, so what is now
00:20:59.059
expecting is nine, so it sends an
00:21:01.664
acknowledgement for nine.
00:21:02.880
And we see that the acknowledgments go
00:21:06.005
six, seven, seven, nine, because that packet
00:21:09.131
seven was delayed a little bit.
00:21:11.910
And if TCP reacts to a single
00:21:14.590
duplicate acknowledgement as an indication that the
00:21:17.269
packet was lost, then you run the
00:21:19.949
risk that you're resending a packet on
00:21:22.628
the assumption when it was lost,
00:21:24.925
when it was just merely delayed a
00:21:27.604
little bit.
00:21:28.470
And there's a trade off you can
00:21:30.936
make here.
00:21:31.740
Do you treat, a single duplicate as
00:21:34.615
an indication of loss? Do you treat
00:21:37.491
two duplicates as an indication of loss?
00:21:40.366
Three? Four? Five? At what point do
00:21:43.241
you say “this as an indication of
00:21:46.116
loss”, rather than just “this is a
00:21:48.992
slightly delayed packet, and it might recover
00:21:51.867
itself in a minute”?
00:21:53.610
The reason that a triple duplicate is
00:21:56.420
used, is because someone did some measurements,
00:21:59.231
and decided that packets being delayed
00:22:01.740
enough to cause one or two duplicates,
00:22:04.344
because they arrived just a little bit
00:22:06.949
out of order, was relatively common.
00:22:09.181
But packets being delayed enough that they
00:22:11.786
cause three or more duplicates is rare.
00:22:14.490
So it's balancing-off speed of loss detection
00:22:17.772
vs. the likelihood that a merely delayed
00:22:21.053
packet is treated as if it were
00:22:24.335
lost, and retransmitted unnecessarily.
00:22:26.310
And, based on the statistics, the belief
00:22:29.485
by the designers of TCP was that
00:22:32.661
waiting for three duplicates was the right
00:22:35.836
threshold.
00:22:36.390
And you could make a TCP version
00:22:38.903
that reduced this to two, or even
00:22:41.416
one duplicate, and it would respond to
00:22:43.928
loss faster, but would have the risk
00:22:46.441
that it's more likely to unnecessarily retransmit
00:22:48.954
something that's just delayed.
00:22:50.490
Or you could make it four,
00:22:52.830
five, six, even more duplicate acknowledgments,
00:22:55.170
which will be less likely to unnecessarily
00:22:57.900
retransmit data. But it’d be slower,
00:23:00.240
because it would be slower in responding
00:23:02.970
to loss, and slower in retransmitting actually
00:23:05.700
lost packets.
00:23:12.780
The other behaviour of TCP. which is
00:23:16.677
worth noting, is head-of-line blocking.
00:23:19.560
Now, in this case we're sending something
00:23:21.867
more realistic. We're sending full size packets,
00:23:24.174
with 1500 bytes of data in each
00:23:26.480
packet.
00:23:26.910
And 1500 is the maximum packet size
00:23:29.330
that you can send in an Ethernet
00:23:31.749
packet, or in a WiFi packet,
00:23:33.823
so this is a typical size that
00:23:36.243
actually gets sent.
00:23:37.380
In this case, the first packet is
00:23:40.794
sent with sequence numbers in the range
00:23:44.209
zero through to 1499.
00:23:46.260
And this arrives at the receiver,
00:23:48.500
and the receiver sends an acknowledgement saying
00:23:51.113
it got it, and the next packet
00:23:53.727
it’s expecting has sequence number 1500.
00:23:55.967
So it sends an acknowledgement for 1500.
00:23:58.680
And if there’s a recv() call outstanding
00:24:01.047
on that socket, that recv() call will
00:24:03.414
return at that point, and return 1500
00:24:05.781
bytes of data. It returns the data
00:24:08.147
as it was received.
00:24:09.600
The next packet arrives at the receiver,
00:24:12.592
containing sequence numbers 1500 through to 2999,
00:24:15.584
and again the recv() call, if there
00:24:18.576
is one, will return, and return that
00:24:21.568
next 1500 bytes.
00:24:22.950
Similarly, when the packet containing the next
00:24:25.823
1500 comes in, the receiver will send
00:24:28.695
the ACK saying “I’m expecting 4500”,
00:24:31.158
and the recv() call will return.
00:24:33.720
The packet containing sequence numbers 4500 though
00:24:38.047
to 5999 is lost.
00:24:40.620
The packet containing 6000 through to 7499
00:24:46.675
arrives.
00:24:47.640
The acknowledgement goes back indicating that it’s
00:24:50.158
still expecting sequence number 4500, because that
00:24:52.676
packet got lost. And at that point,
00:24:55.194
some data has arrived, some new data
00:24:57.712
has arrived at the receiver.
00:24:59.610
But there's a gap. The packets,
00:25:02.343
the packet, containing data with sequence numbers
00:25:05.531
4500 through to 5999 is still missing.
00:25:08.820
So if the receiver application has called
00:25:12.550
recv() on that socket, it won't return.
00:25:16.380
The data has arrived, it's buffered up
00:25:18.845
in the TCP layer in the operating
00:25:21.309
system, but TCP won't give it back
00:25:23.774
to the application.
00:25:24.930
And the packets can keep being sent,
00:25:27.944
and the receiver keeps sending the duplicate
00:25:30.957
acknowledgments, and eventually it’s sent the triple
00:25:33.971
duplicate acknowledgement, and the TCP sender notices
00:25:36.984
and retransmits the packet with sequence numbers
00:25:39.998
4500 through to 5999.
00:25:41.820
And eventually those arrive at the receiver.
00:25:45.900
At that point, the receiver has a
00:25:48.965
contiguous block of data available, with no
00:25:52.030
gaps in it, and it returns all
00:25:55.095
of the data from sequence number 4500
00:25:58.161
up to sequence number 12,000,
00:26:00.450
up to the application in one go.
00:26:03.210
And if the application has given a
00:26:06.332
big enough buffer, at that point the
00:26:09.455
recv() call will returned 7500 bytes of
00:26:12.577
data. It’ll return all of that received
00:26:15.700
data in one big burst.
00:26:18.030
And then, as the data, you know,
00:26:20.685
gets retransmitted, as the data arrives,
00:26:22.961
it will just keep, you know,
00:26:25.237
the recv() call will unblock and data
00:26:27.892
will start flowing.
00:26:29.130
The point is the TCP receiver waits
00:26:31.705
for any missing data to be delivered.
00:26:34.380
If anything's missing, the triple duplicate ACK
00:26:38.077
happens, it eventually gets retransmitted, and the
00:26:41.773
receiver won't return anything to the application
00:26:45.470
until that retransmission has happened.
00:26:48.210
It’s called head of line blocking.
00:26:50.082
The data stops being delivered, until it
00:26:52.267
can be delivered in sequence to the
00:26:54.451
application. It’s all just buffered up in
00:26:56.636
the operating system, in the TCP code.
00:26:58.920
TCP always gives the data to the
00:27:01.111
application in a contiguous ordered sequence,
00:27:02.988
in the order it was sent.
00:27:04.866
And this is another reason why the
00:27:07.056
recv() calls don't always preserve the message
00:27:09.247
boundaries.
00:27:09.660
Because it depends how much data was
00:27:12.040
queued up because of packet losses,
00:27:14.080
and so on, so that it can
00:27:16.460
always be delivered in order.
00:27:19.260
The head of line blocking increases the
00:27:21.886
total download time. We see on the
00:27:24.513
left, the case where one packet was
00:27:27.139
lost, and had to be re-transmitted.
00:27:29.490
And we see on the right,
00:27:31.044
the case where all the packets were
00:27:32.857
received on time. And we see an
00:27:34.670
increase in the download time because of
00:27:36.483
the packet loss.
00:27:41.070
It blocks the receiving, it delays things
00:27:43.690
a little bit, waiting for the retransmission.
00:27:46.410
And it increases the overall download time
00:27:50.498
a little bit.
00:27:52.350
It disrupts the behaviour of when the
00:27:56.150
packets are received, during the download quite
00:27:59.950
significantly. We see 1500, 1500, 1500,
00:28:03.207
big gap, seven thousand five hundred,
00:28:06.464
1500, 1500,
00:28:07.650
in the case where the packets were
00:28:09.496
lost. Or, in the case where they
00:28:11.343
were all received, the data is coming
00:28:13.189
in quite smoothly. It's regularly spaced.
00:28:14.772
So it affects the timing, it effects
00:28:16.618
when the data is delivered to the
00:28:18.465
application, and it has a smaller effect
00:28:20.311
on the overall download times.
00:28:28.650
And if you're building real time applications,
00:28:31.378
this is a significant problem. We see
00:28:34.106
the case on the right, if everything
00:28:36.834
is delivered on time, then the data
00:28:39.563
is released to the application very quickly
00:28:42.291
and very predictably.
00:28:43.560
And you don't need
00:28:47.310
much buffering delay at the receiver.
00:28:49.295
Things can be just delivered, things are
00:28:51.611
just delivered to the application, repeatedly on
00:28:53.927
a regular schedule.
00:28:55.020
But the minute something gets lost,
00:28:56.968
it has to wait for the retransmission.
00:28:59.340
In this case it waits for one
00:29:00.950
round trip time, because the ACK has
00:29:02.560
to get back, and then the data
00:29:04.170
has to be retransmitted.
00:29:05.190
Plus, it has to wait for four
00:29:07.482
times the gap between packets, to allow
00:29:09.774
for the four duplicates, the triple tripling
00:29:12.066
ACK and the original ACK, so you
00:29:14.358
get one round trip time plus four
00:29:16.650
times the packet spacing.
00:29:18.060
So if you're using TCP to send,
00:29:20.545
for example, speech data, where it's sending
00:29:23.030
packets regularly every 20 milliseconds, you need
00:29:25.515
to buffer 80 milliseconds plus the round
00:29:28.000
trip time, to allow for these re-transmissions,
00:29:30.485
if you're using it for a real
00:29:32.970
time application.
00:29:33.780
Because,
00:29:35.880
it waits for the retransmissions, and because
00:29:38.653
of the head of line blocking.
00:29:41.130
And when you're using applications like Netflix
00:29:43.471
or the iPlayer, when you press play
00:29:45.812
on the video there’s a little pause
00:29:48.152
where it says “buffering”.
00:29:49.590
This is what it's doing. It’s buffering
00:29:52.460
up enough data that it can wait
00:29:55.330
for the retransmissions to happen,
00:29:57.480
buffering up enough data in the TCP
00:29:59.862
connection that it can keep playing out
00:30:02.245
the video frames, in order, while still
00:30:04.627
allowing time for a retransmission to happen.
00:30:07.110
So it's buffering up the data waiting,
00:30:09.428
making sure there's enough enough data buffered
00:30:11.747
up, because of this head of line
00:30:14.065
blocking issue in TCP.
00:30:20.310
So that concludes the discussion of TCP.
00:30:23.959
It gives you an ordered, reliable,
00:30:27.087
byte stream.
00:30:28.230
As a service model it's easy to
00:30:30.802
understand. It’s like reading from a file;
00:30:33.374
you read from the connection and the
00:30:35.946
bytes arrive reliably and in the order
00:30:38.518
they were sent.
00:30:39.720
The timing, though, is unpredictable. How much
00:30:42.248
you get from the connection each time
00:30:44.776
you read from it,
00:30:46.320
and whether the data arrives regularly,
00:30:48.428
or whether it's arrives in big bursts
00:30:50.888
with large gaps between them, depends on
00:30:53.348
how much data is lost, and depends
00:30:55.807
on whether the TCP has to retransmit
00:30:58.267
missing data.
00:30:59.070
And if you're just using this to
00:31:01.392
download files that doesn't matter. It means
00:31:03.715
that the progress bar is perhaps inaccurate,
00:31:06.037
but otherwise it doesn't make much difference.
00:31:08.460
But, if you're using it for real
00:31:11.260
time applications, like video streaming, like telephony,
00:31:14.060
this head of line blocking can quite
00:31:16.860
significantly affect the play out.
00:31:18.960
And a lot of that is the
00:31:21.521
reason why applications use, why real time
00:31:24.082
applications use, UDP. And for those that
00:31:26.643
don't use UDP,
00:31:27.840
applications like Netflix that use adaptive streaming
00:31:31.343
over HTTP, which we'll talk about in
00:31:34.845
lecture seven, that's why there’s this buffering
00:31:38.348
delay before they start playing.
00:31:40.950
And, of course, the lack of framing
00:31:42.652
complicates the application design, you have to
00:31:44.355
parse the data to make sure you've
00:31:46.057
got all the data;
00:31:47.130
there's no message boundaries in there,
00:31:49.099
so you have to parse the data.
00:31:51.397
It doesn't tell you, the connection doesn't
00:31:53.694
tell you, when you've received all the
00:31:55.992
data.
00:31:57.420
So that's it for TCP.
00:32:00.420
It delivers data reliably. It uses sequence
00:32:03.526
numbers and acknowledgments to indicate when the
00:32:06.632
data arrived.
00:32:07.620
It uses timeouts to indicate that a
00:32:10.030
connection has failed. And it uses this
00:32:12.440
idea of triple duplicate ACKs to indicate
00:32:14.850
that a packet has been lost,
00:32:16.916
and trigger a retransmission of any lost
00:32:19.326
data.
00:32:19.770
What I’ll talk about in the next
00:32:21.783
part is QUIC and how it differs
00:32:23.795
from the way TCP handles reliability.
Part 4: Reliable Data Transfer With QUIC
The final part of the lecture discusses reliable data transfer using
QUIC. It outlines the QUIC service model, and how it differs from
that of TCP, and shows how QUIC achieves reliable data transfer.
It discusses how QUIC provides multiple streams within a single
connection, and consider how this affects head-of-line blocking
and latency. Approaches to making best use of multiple streams
are discussed..
00:00:00.000
In this final part I’d like to
00:00:02.542
talk about how reliable data transfer works
00:00:05.083
with QUIC, and how it's different to
00:00:07.625
reliable data transfer with TCP.
00:00:09.540
I’ll talk a little bit about the
00:00:11.748
QUIC service model, and how it handles
00:00:13.956
packet numbers and retransmission. I’ll talk about
00:00:16.165
the multi-streaming features of QUIC. And I’ll
00:00:18.373
talk about how it avoids head-of-line blocking.
00:00:23.341
The service model for TCP, as we
00:00:26.166
saw previously, is that it delivers a
00:00:28.991
single reliable, ordered, byte stream of data.
00:00:31.816
Applications write a stream of bytes in,
00:00:34.642
and that stream of bytes is delivered
00:00:37.467
to the receiver, eventually.
00:00:39.181
QUIC, by contrast, delivers several ordered reliable
00:00:41.974
byte streams within a single connection.
00:00:44.369
Applications can separate the data they're sending
00:00:47.162
into different streams, and each stream is
00:00:49.956
delivered reliably and in order.
00:00:52.051
QUIC doesn't preserve the ordering between the
00:00:54.661
streams within a connection, so if you
00:00:57.271
send in one stream, and then send
00:00:59.880
in a second stream, then the data
00:01:02.490
you sent second, in that second stream,
00:01:05.100
may arrive first, but it preserves the
00:01:07.710
ordering with a stream.
00:01:09.301
And you can treat each stream as
00:01:11.836
if it were running multiple TCP connections
00:01:14.370
in parallel, so it gives you the
00:01:16.905
same service model with several streams of
00:01:19.440
data, or you could perhaps treat each
00:01:21.975
stream as a
00:01:23.161
sequence of messages to be sent,
00:01:25.616
with the streams indicating message boundaries.
00:01:30.361
QUIC delivers data in packets.
00:01:33.451
Each QUIC packet has a packet sequence
00:01:36.385
number, a packet number,
00:01:38.161
and the packet numbers
00:01:40.981
are split into two packet number spaces.
00:01:44.401
The packets sent during the initial QUIC
00:01:47.158
handshake start with packet sequence number zero,
00:01:49.914
and that packet sequence number increases by
00:01:52.671
one for each packet sent during the
00:01:55.427
handshake.
00:01:55.921
Then, when the handshake’s complete, and it
00:01:58.801
switches to sending data, it resets the
00:02:01.680
packet sequence number to zero and starts
00:02:04.560
again.
00:02:05.071
Within each of these packet number spaces,
00:02:07.680
the handshake space, and the data space,
00:02:10.288
the packet number sequence starts at zero,
00:02:12.897
and goes up by one for every
00:02:15.506
packet sent.
00:02:16.351
That is, the sequence numbers in QUIC,
00:02:18.608
the packet numbers in QUIC, count the
00:02:20.864
number of packets of data being sent.
00:02:23.221
That's different to TCP. In TCP,
00:02:25.406
the sequence number in the header counts
00:02:27.956
the offset within the byte stream,
00:02:30.142
it counts how many bytes of data
00:02:32.691
have been sent. Whereas in QUIC,
00:02:34.877
the packet numbers count the number of
00:02:37.427
packets.
00:02:37.891
Inside a QUIC packet is a sequence
00:02:40.637
of frames. Some of those frames may
00:02:43.383
be stream frames, and stream frames carry
00:02:46.129
data.
00:02:46.621
Each stream frame has a stream ID,
00:02:48.814
so it knows which of the many
00:02:51.008
sub-streams it’s carrying data for, and it
00:02:53.201
also has the = amount of data
00:02:55.394
being carried, and the offset of that
00:02:57.588
data from the start of the stream.
00:02:59.881
So, essentially the stream contains sequence numbers
00:03:02.269
which play the same role as TCP
00:03:04.658
sequence numbers, in that they count bytes
00:03:07.046
of data being sent in that stream.
00:03:09.434
And the packets have sequence numbers that
00:03:11.823
count the number of packets being sent.
00:03:14.311
And we can see this in the
00:03:16.844
diagram on the right, where we see
00:03:19.377
the packet numbers going up, zero,
00:03:21.548
one, two, three, four. And the stream
00:03:24.081
numbers, packet zero carries data from the
00:03:26.613
first stream, bytes zero through 1000.
00:03:28.784
Packet one carries data from the first
00:03:31.317
stream, bytes 1001 to 2000. And packet
00:03:33.850
two carries bytes 2001 to 2500
00:03:36.121
from the first stream, and zero to
00:03:39.616
500 from the second stream, and so
00:03:43.112
on.
00:03:43.711
And we see that we can send
00:03:45.746
data on multiple streams in a single
00:03:47.780
packet.
00:03:50.401
QUIC doesn't preserve message boundaries within the
00:03:53.204
streams. In the same way that,
00:03:55.606
within a TCP stream, if you write
00:03:58.409
data to the stream and
00:04:00.511
the amount you write is too big
00:04:02.596
to fit into a packet, it may
00:04:04.682
be arbitrarily split between packets.
00:04:06.271
Or if the data you send in
00:04:08.054
a TCP Stream is too small,
00:04:09.583
and doesn't fill a whole packet,
00:04:11.111
it may be delayed waiting for more
00:04:12.894
data, to be able to fill up
00:04:14.677
the packet before it’s sent.
00:04:16.051
The same thing happens with QUIC.
00:04:17.789
If the amount of data you write
00:04:19.817
to a stream is too big to
00:04:21.845
fit into a QUIC packet, then it
00:04:23.873
will be split across multiple packets.
00:04:25.711
Similarly, if the amount of data you
00:04:27.881
write to a stream is very small,
00:04:30.051
QUIC may buffer it up, delay it,
00:04:32.221
wait for more data, so it can
00:04:34.391
send it and fill a complete packet.
00:04:36.661
In addition, QUIC can take data from
00:04:38.942
more than one stream, and send it
00:04:41.223
in a single packet, if there’s space
00:04:43.503
to do so.
00:04:44.581
And if there's more than one stream
00:04:46.841
with data that's available to send,
00:04:48.778
then the QUIC sender can make an
00:04:51.038
arbitrary decision, how it prioritises that data,
00:04:53.298
and how it delivers frames from each
00:04:55.558
stream.
00:04:55.981
And usually it will split those,
00:04:58.625
the data from the streams, so each
00:05:01.709
packet has data from,
00:05:03.571
half the data from, one stream,
00:05:05.214
and half from another stream. But it
00:05:07.131
may alternate them if it wants,
00:05:08.773
sending one packet with data from stream
00:05:10.690
1, one from stream 2, one from
00:05:12.607
stream 1, one from stream 2,
00:05:14.250
and so on.
00:05:17.971
On the receiving side, the receiver sends,
00:05:20.487
the QUIC receiver sends acknowledgments for the
00:05:23.003
packets it receives.
00:05:24.181
So, unlike TCP which acknowledges the next
00:05:26.881
expected sequence number, a QUIC receiver just
00:05:29.580
sends an acknowledgement to say “I got
00:05:32.280
this packet”.
00:05:33.151
So when packet zero arrives, it sends
00:05:35.405
an acknowledgement saying “I got packet zero”.
00:05:37.658
And when packet one arrives, it sends
00:05:39.912
an acknowledgement saying “I got packet one”,
00:05:42.165
and so on.
00:05:43.231
The sender needs to remember what data
00:05:45.433
it puts in each packet, so it
00:05:47.635
knows when it gets an acknowledgement for
00:05:49.837
packet two that,
00:05:50.881
in this case, it contained bytes 2001
00:05:53.498
to 2500 from stream one, and bytes
00:05:56.115
zero through 500 from stream two.
00:05:58.359
That information isn't in the acknowledgments.
00:06:00.602
What's in the acknowledgments it's just the
00:06:03.219
packet numbers, so the sender needs to
00:06:05.837
keep track of how it puts the
00:06:08.454
data from the streams into the packets.
00:06:12.361
The acknowledgments in QUIC are also a
00:06:15.126
bit more sophisticated than they are in
00:06:17.891
TCP, in that it doesn't just have
00:06:20.656
an acknowledgement number field in the header.
00:06:23.521
Rather, it sends the acknowledgments as frames
00:06:26.560
in the packets coming back.
00:06:28.831
And this gives a lot more flexibility,
00:06:31.762
because
00:06:32.281
it can have a fairly sophisticated frame
00:06:35.282
format, and it can change the frame
00:06:38.282
format to include different, to support different
00:06:41.283
ways of sending a header, if it
00:06:44.284
needs to.
00:06:45.241
In the initial version of QUIC,
00:06:47.281
what's in the frame format, in the
00:06:49.661
ACK frames coming back from the receiver
00:06:52.041
to the sender,
00:06:53.161
is a field indicating the largest acknowledgement,
00:06:56.503
which is essentially the same as the
00:06:59.845
TCP acknowledgment – it tells you what's
00:07:03.187
what's the highest sequence number received.
00:07:06.151
There's an ACK delay field, that tells
00:07:08.948
you how long between receiving that packet
00:07:11.744
the receiver waited before sending the acknowledgement.
00:07:14.641
So this is the delay in the
00:07:17.487
receiver. And by measuring the time it
00:07:20.333
takes for the acknowledgment come back,
00:07:22.773
and removing this ACK delay field,
00:07:25.212
you can estimate the network round trip
00:07:28.058
time excluding the processing delays in the
00:07:30.904
receiver.
00:07:31.411
There’s a list of ACK ranges.
00:07:33.623
And the ACK ranges are a way
00:07:36.204
of the receiver saying “I got a
00:07:38.785
range of packets”.
00:07:39.991
So you can send an acknowledgement that
00:07:41.946
says, I got packets from five through
00:07:43.902
seven
00:07:44.281
in a single go. And you can
00:07:46.321
split this up, with multiple ACK ranges.
00:07:48.360
So you could have an acknowledgement that
00:07:50.400
says “I got packet five; I got
00:07:52.440
packets
00:07:52.831
seven through nine; and I got packets
00:07:55.279
11 through 15” and you can send
00:07:57.726
that all within a single acknowledgement block,
00:08:00.174
in an ACK frame, within the reverse
00:08:02.622
path stream.
00:08:03.421
And this gives it more flexibility,
00:08:05.756
so it doesn't just have to acknowledge
00:08:08.480
the most recently received packet, which gives
00:08:11.205
the sender more information to make retransmissions.
00:08:13.929
This is a bit like the TCP
00:08:16.653
selective acknowledgement extension.
00:08:21.751
Like TCP, QUIC will retransmit lost data.
00:08:26.011
The difference is that TCP retransmits packets,
00:08:29.167
exactly as they would be originally sent,
00:08:32.322
so the retransmission looks just the same
00:08:35.478
as the original packet.
00:08:37.381
QUIC never retransmits packets. Each packet in
00:08:40.824
QUIC has a unique packet sequence number,
00:08:44.266
and each packet is only ever transmitted
00:08:47.709
once.
00:08:48.301
What QUIC rather does, is it retransmits
00:08:50.992
the data which was in those packets
00:08:53.683
in a new packet.
00:08:55.321
So in this example, we see that
00:08:57.995
packet, on the slide, we see that
00:09:00.670
packet number two got lost, and it
00:09:03.344
contain the data bytes 2001 to 2500
00:09:06.018
from stream one, and bytes zero through
00:09:08.693
500 from stream two.
00:09:10.321
And, when it gets the the acknowledgments
00:09:12.771
indicating that packet was lost, it resends
00:09:15.221
that data.
00:09:16.021
And in this case it's sending in
00:09:19.449
packet six, it’s resending the first bytes
00:09:22.878
of data from stream, it’s sending the
00:09:26.306
bytes 2001 to 2500 from stream one,
00:09:29.734
and it will eventually, at some point
00:09:33.163
later, retransmit the data from stream two.
00:09:36.691
As we say, each packet has a
00:09:39.194
unique packet sequence number. Since we're not,
00:09:41.696
since each packet is acknowledged as it
00:09:44.199
arrives, and it's not acknowledging the highest,
00:09:46.801
not acknowledging the next sequence number expected
00:09:49.217
in the same way TCP does,
00:09:51.288
you can’t do the triple duplicate ACK
00:09:53.704
in the same way, because you don't
00:09:56.120
get duplicate ACKs. Each ACK acknowledges the
00:09:58.536
next new packet.
00:09:59.671
Rather QUIC declares a packet to be
00:10:02.348
lost when it's got ACKS for three
00:10:05.025
packets with higher packet numbers than the
00:10:07.701
one which it sent.
00:10:09.331
At that point, it can retransmit the
00:10:11.734
data that was in that packet.
00:10:13.794
And that’s QUIC’s equivalent to the triple
00:10:16.198
duplicate ACK; it's three following sequence numbers
00:10:18.601
rather than three duplicate sequence numbers.
00:10:20.761
And also, just like TCP, if there's
00:10:22.651
a timeout, and it stops getting ACKs,
00:10:24.541
then it declares the packets to be
00:10:26.431
lost.
00:10:31.381
QUIC delivers multiple streams within a single
00:10:34.878
connection. And within each stream, the data
00:10:38.375
is delivered reliably, and in the order
00:10:41.872
it was sent.
00:10:43.471
If a packet’s lost, then that clearly
00:10:46.102
causes data for the stream, streams,
00:10:48.357
where the data was included in that
00:10:50.988
packet to be lost.
00:10:52.591
Whether a packet loss effects one,
00:10:54.811
or more, streams really depends on how
00:10:57.401
the sender chooses to put the data
00:10:59.991
from different streams into the packets.
00:11:02.311
It’s possible that a QUIC packet can
00:11:04.913
contain data from several streams. We saw
00:11:07.514
in the examples, how the packets contain
00:11:10.116
data from both stream one and stream
00:11:12.718
two simultaneously.
00:11:13.561
In that case, if a packet is
00:11:15.820
lost, it will affect both of the
00:11:18.078
streams, all of the streams if there’s
00:11:20.337
data from more than two streams in
00:11:22.596
the packet.
00:11:23.341
Equally, a QUIC sender can choose to
00:11:25.914
alternate, and send one packet with data
00:11:28.488
from stream one, and then another packet
00:11:31.061
with data from stream two, and only
00:11:33.635
ever put data from a single stream
00:11:36.208
in each packet.
00:11:37.411
The specification puts no requirements on how
00:11:40.678
the sender does this, and different senders
00:11:43.944
can choose to do it differently depending
00:11:47.311
whether they're trying to make progress on
00:11:50.442
each stream simultaneously, or whether they want
00:11:53.574
to
00:11:54.121
they want to alternate, and make sure
00:11:57.201
that packet loss only ever affects a
00:12:00.281
single stream.
00:12:01.261
Depending on how they do this,
00:12:03.564
the streams can suffer from head of
00:12:06.250
line blocking independently.
00:12:07.501
If data is lost on a particular
00:12:11.309
stream, then that stream can't deliver later
00:12:15.117
data to the application, until that
00:12:18.481
lost data has been transmitted. But the
00:12:20.999
other streams, if they've got all the
00:12:23.517
data, can keep delivering to the application.
00:12:26.036
So streams suffer from head of line
00:12:28.554
blocking individually, but there's no head of
00:12:31.072
line blocking between streams.
00:12:32.611
This means that the data is delivered
00:12:35.957
reliably, and in order, on a stream,
00:12:39.303
but order’s not preserved between streams.
00:12:42.271
It’s quite possible that one stream can
00:12:44.540
be blocked, waiting for a retransmission of
00:12:46.810
some of the data in the packets,
00:12:49.079
while the other streams are continuing to
00:12:51.349
deliver data and haven't seen any loss
00:12:53.618
on that stream.
00:12:54.691
Each stream is sent and received independently.
00:12:57.009
And this means if you're careful with
00:12:59.327
how you split data
00:13:00.751
across streams, and if the implementation is
00:13:02.808
careful with how it puts data from
00:13:04.865
streams into different packets, it can limit
00:13:06.922
the duration of the head of line
00:13:08.979
blocking, and make the streams independent in
00:13:11.036
terms of head of line blocking and
00:13:13.093
data delivery.
00:13:18.571
QUIC delivers, as we've seen, several ordered,
00:13:22.360
reliable, byte streams of data in a
00:13:26.149
single connection.
00:13:27.331
How you treat these different bytes streams,
00:13:30.126
is, I think, still a matter of
00:13:32.922
interpretation.
00:13:33.421
It's possible to treat a QUIC connection
00:13:36.594
as though it was several parallel TCP
00:13:39.768
connections.
00:13:40.321
So, rather than opening multiple TCP connections
00:13:42.709
to a server, you open one QUIC
00:13:45.097
connection, and you send and receive several
00:13:47.485
streams of data within that.
00:13:49.291
And then you treat each stream of
00:13:51.794
data as-if it were a TCP stream,
00:13:54.297
and you parse and process the data
00:13:56.800
as if it were a TCP stream.
00:13:59.303
And you possibly send multiple requests,
00:14:01.448
and get multiple responses, over each stream.
00:14:04.051
Or, you can treat the streams more
00:14:06.355
as a framing device.
00:14:07.771
You can say that each stream,
00:14:09.664
you can choose to interpret each stream,
00:14:11.871
as sending a single object. And then,
00:14:14.079
when you send data from the stream,
00:14:16.287
on that stream, once you finish sending
00:14:18.495
that object, you close the stream and
00:14:20.703
move on to use the next one.
00:14:23.011
And, on the receiving side, you just
00:14:25.571
read all the data until you see
00:14:28.130
the end of stream marker, and then
00:14:30.690
you process it knowing you’ve got a
00:14:33.250
complete object.
00:14:34.081
And I think that the best practices,
00:14:36.449
the way of thinking about a QUIC
00:14:38.818
connection, and the streams within a connection,
00:14:41.186
is still evolving.
00:14:42.301
And it's not clear which of these
00:14:44.119
two approaches is the necessarily the right
00:14:45.937
way to do it. And I think
00:14:47.754
it probably depends on the application what
00:14:49.572
makes the most sense.
00:14:53.971
So, to conclude for this lecture.
00:14:57.361
We spoke a little bit about best
00:15:00.461
effort packet delivery on the Internet,
00:15:03.119
and why the IP layer delivers data
00:15:06.219
unreliably, and why it's appropriate to have
00:15:09.319
a best effort network.
00:15:11.191
Then we spoke a bit about the
00:15:13.493
different transports.
00:15:14.251
The UDP transport that provides an unreliable,
00:15:17.858
but timely, service on which you can
00:15:21.464
build more sophisticated user space application protocols.
00:15:25.171
We spoke about TCP, that provides a
00:15:27.981
reliable ordered stream delivery service. And we
00:15:30.791
spoke about QUIC, that provides a reliable
00:15:33.602
ordered delivery service with multiple streams of
00:15:36.412
data. And it’s clear there’s different services,
00:15:39.222
different transport protocols, for different needs.
00:15:41.731
What I want to move on to
00:15:44.138
next time, is starting to talk about
00:15:46.544
congestion control and how all these different
00:15:49.051
transport protocols manage the rate at which
00:15:51.487
they send data.
Discussion
Lecture 5 discussed reliable data transfer over the Internet. It started
with a discussion of best effort packet delivery, and an explanation of
why it makes sense for the Internet to be designed to be an unreliable
network. Then, it moved on to discuss UDP and how to make applications
and new transport protocols that work on an unreliable network. There's
a trade-off between timeliness and reliability that's important here,
and the lecture gave some examples of this to illustrate why many
real-time applications used UDP.
The bulk of the lecture discussed TCP. It spoke about how TCP sends
acknowledgement for packets, how timeouts and triple-duplicate ACKs
indicate loss, and why a triple-duplicate ACK is chosen as the loss
signal. We also discussed head-of-line blocking, and how the in-order,
single stream, reliable service model of TCP leads to head-of-line
blocking and potential latency.
Finally, we discussed the differences between QUIC and TCP. QUIC
acknowledges packets rather than bytes within a stream, uses ACK
frames rather than an ACK header, and delivers multiple streams
of data, allowing it to avoid head-of-line blocking in many cases.
The focus of the discussion will be on how TCP ensures reliability,
to make sure the mechanism is understood, and on the differences
between the TCP and QUIC service models and how QUIC can improve
latency. We'll also discuss how UDP can form a substrate, to easily
allow new transports, to suit different needs, to be built and
deployed.