29
1 The Transport The Transport Layer Layer The Internet Transport Protocols – TCP and UDP

1 The Transport Layer The Internet Transport Protocols – TCP and UDP

Embed Size (px)

Citation preview

Page 1: 1 The Transport Layer The Internet Transport Protocols – TCP and UDP

11

The Transport LayerThe Transport LayerThe Internet Transport Protocols –

TCP and UDP

Page 2: 1 The Transport Layer The Internet Transport Protocols – TCP and UDP

22

OverviewOverview

IntroIntro The TCP Service ModelThe TCP Service Model The TCP ProtocolThe TCP Protocol The TCP Segment HeaderThe TCP Segment Header TCP Connection ManagementTCP Connection Management TCP Transmission PolicyTCP Transmission Policy TCP Congestion controlTCP Congestion control TCP Timer managementTCP Timer management UDPUDP

Page 3: 1 The Transport Layer The Internet Transport Protocols – TCP and UDP

33

Transport Layer in the InternetTransport Layer in the Internet

The Internet has two main protocols The Internet has two main protocols in the transport layer – a in the transport layer – a connection connection orientedoriented protocol, and a protocol, and a connectionlessconnectionless one. We will study one. We will study both of them.both of them.

The connection oriented protocol is The connection oriented protocol is TCP.TCP.

The connectionless is UDP. The connectionless is UDP. Because UDP is basically just IP with Because UDP is basically just IP with

a short header added, we will focus a short header added, we will focus on TCP.on TCP.

Page 4: 1 The Transport Layer The Internet Transport Protocols – TCP and UDP

44

IntroIntro TCP (Transmission Control Protocol)TCP (Transmission Control Protocol) was was

specifically designed to provide a specifically designed to provide a reliable reliable end-to-end byte streamend-to-end byte stream over an unreliable over an unreliable internetwork.internetwork.

An internetwork differs from a single An internetwork differs from a single network because different parts may have network because different parts may have quite different topologies, bandwidth, quite different topologies, bandwidth, delays, packet sizes, and other delays, packet sizes, and other parameters. TCP was designed to parameters. TCP was designed to dynamically adaptdynamically adapt to properties of the to properties of the internetwork and to be internetwork and to be robustrobust in the face in the face of many kinds of many kinds of failuresof failures..

Page 5: 1 The Transport Layer The Internet Transport Protocols – TCP and UDP

55

IntroIntro Each machine supporting TCP has Each machine supporting TCP has TCP transport entityTCP transport entity, ,

either a user process or part of the kernel, which manages either a user process or part of the kernel, which manages TCP streams and interfaces to the IP layer.TCP streams and interfaces to the IP layer.

A TCP entity accepts user data streams from local A TCP entity accepts user data streams from local processes, breaks them up into pieces not exceeding 64K processes, breaks them up into pieces not exceeding 64K bytes, and sends each piece as a separate IP datagram. bytes, and sends each piece as a separate IP datagram.

When IP datagrams containing TCP data arrive at a When IP datagrams containing TCP data arrive at a machine, they are given to the TCP entity, which machine, they are given to the TCP entity, which reconstructs the original byte streams.reconstructs the original byte streams.

The The IP gives not guaranteeIP gives not guarantee that datagrams will be that datagrams will be delivered properly, so delivered properly, so it is up the TCPit is up the TCP to time out and to time out and retransmit them as need be. Datagrams that do arrive retransmit them as need be. Datagrams that do arrive may well be in the wrong order; it is also up to the TCP to may well be in the wrong order; it is also up to the TCP to reassemble them into messages in proper sequence. reassemble them into messages in proper sequence.

Page 6: 1 The Transport Layer The Internet Transport Protocols – TCP and UDP

66

The TCP Service ModelThe TCP Service Model TCP Service is obtained by having both theTCP Service is obtained by having both the

sender and receiver create end points, sender and receiver create end points, called called sockets. sockets. Each socket has a socket number Each socket has a socket number (address)(address) consisting of the IP address of the consisting of the IP address of the host and host and a 16-bit number local to that host, a 16-bit number local to that host, calledcalled a port. a port.

To obtain TCP service,To obtain TCP service, a connection a connection must be must be explicitly established between a socket on explicitly established between a socket on the sending machine and a socket on the the sending machine and a socket on the receiving machine. receiving machine.

A socket may be used for multiple A socket may be used for multiple connections at the same time. In other words, connections at the same time. In other words, two or more connections may terminate at two or more connections may terminate at the same socket. the same socket.

Page 7: 1 The Transport Layer The Internet Transport Protocols – TCP and UDP

77

The TCP Service ModelThe TCP Service Model Port numbers Port numbers below 1024below 1024 are called are called well-known portswell-known ports and and

are reserved for standard services. For example, any are reserved for standard services. For example, any process wishing to establish a connection to a host to process wishing to establish a connection to a host to transfer a file using FTP can connect to the destination transfer a file using FTP can connect to the destination host’s port 21 to contact its FTP daemon/service. Similarly, host’s port 21 to contact its FTP daemon/service. Similarly, to establish a remote login session using TELNET, port 23 is to establish a remote login session using TELNET, port 23 is used. Port 80 is used for HTTP, port 443 is used for SSL, etc.used. Port 80 is used for HTTP, port 443 is used for SSL, etc.

Ports Ports between 1024 and 5000between 1024 and 5000 are called are called ephemeralephemeral and are and are free to use (not reserved). The client’s socket would use free to use (not reserved). The client’s socket would use such port. such port.

All TCP connections are All TCP connections are full-duplexfull-duplex and and point-to-pointpoint-to-point. Full . Full duplex means that traffic can go in both directions at the duplex means that traffic can go in both directions at the same time. Point-to-point means that each connection has same time. Point-to-point means that each connection has exactly two end points. TCP does not support multicasting exactly two end points. TCP does not support multicasting or broadcasting.or broadcasting.

Page 8: 1 The Transport Layer The Internet Transport Protocols – TCP and UDP

88

A TCP connection is A TCP connection is byte streambyte stream, not a , not a message stream. Message boundaries are message stream. Message boundaries are not preserved end to end.not preserved end to end.

For example, if the sending process does For example, if the sending process does four 512-byte writes to a TCP stream, four 512-byte writes to a TCP stream, these data may be delivered to the these data may be delivered to the receiving process as four 512-byte receiving process as four 512-byte chunks, or two 1024-byte chunks, or one chunks, or two 1024-byte chunks, or one 2048-byte chunk, or some other way.2048-byte chunk, or some other way.

When an application passes data to TCP, When an application passes data to TCP, TCP TCP maymay send it immediately or send it immediately or bufferbuffer it it (in order to collect a larger amount to (in order to collect a larger amount to send at once), at its discretion.send at once), at its discretion.

The TCP Service ModelThe TCP Service Model

Page 9: 1 The Transport Layer The Internet Transport Protocols – TCP and UDP

99

The TCP ProtocolThe TCP Protocol Every byte on a TCP connection has its own 32-Every byte on a TCP connection has its own 32-

bit bit sequence numbersequence number. . The sending and receiving TCP entities exchange The sending and receiving TCP entities exchange

data in the form of data in the form of segmentssegments. A segment consists . A segment consists of a fixed 20-byte header (plus an optional part) of a fixed 20-byte header (plus an optional part) followed by 0 or more data bytes. The TCP followed by 0 or more data bytes. The TCP software decides how big segments should be. It software decides how big segments should be. It can accumulate data from several writes into one can accumulate data from several writes into one segment or split data from one write over segment or split data from one write over multiple segments.multiple segments.

Two limits restrict the Two limits restrict the segment sizesegment size::• Each segment, including the TCP header, must fit in the Each segment, including the TCP header, must fit in the

64K byte IP64K byte IP payload payload• Each network has a Each network has a maximum transfer unitmaximum transfer unit or or MTUMTU, and , and

each segment must fit in the MTU.each segment must fit in the MTU.

Page 10: 1 The Transport Layer The Internet Transport Protocols – TCP and UDP

1010

The TCP ProtocolThe TCP Protocol A segment that is too large for a network that it A segment that is too large for a network that it

must transit can be broken up into multiple must transit can be broken up into multiple segments by a router. Each new segment gets its segments by a router. Each new segment gets its on IP header (20 bytes), so fragmentation by on IP header (20 bytes), so fragmentation by routers increases the total overhead.routers increases the total overhead.

The basic protocol used by TCP entities is the The basic protocol used by TCP entities is the sliding window protocolsliding window protocol:: • when a sender transmits a segment, it also when a sender transmits a segment, it also starts a starts a

timertimer• when the segment arrives at the destination, the when the segment arrives at the destination, the

receiving TCP entity sends back a segment bearing receiving TCP entity sends back a segment bearing an an acknowledgmentacknowledgment number equal to the next number equal to the next sequence number it expects to receivesequence number it expects to receive

• if the sender’s timer goes off before the if the sender’s timer goes off before the acknowledgment is received, the sender transmits acknowledgment is received, the sender transmits the segment againthe segment again

Page 11: 1 The Transport Layer The Internet Transport Protocols – TCP and UDP

1111

The TCP ProtocolThe TCP Protocol Although this protocol sounds simple, there are Although this protocol sounds simple, there are some some

details/issuesdetails/issues to be addressed: to be addressed:• Since segments can be fragmented, it is possible that part of a Since segments can be fragmented, it is possible that part of a

transmitted segment arrives but the rest is lost and never transmitted segment arrives but the rest is lost and never arrives. arrives.

• Segments can also arrive out of order, for example, bytes Segments can also arrive out of order, for example, bytes 3072-4095 can arrive but cannot be acknowledged because 3072-4095 can arrive but cannot be acknowledged because bytes 2048-3071 have not come yet. bytes 2048-3071 have not come yet.

• Segments can also be delayed so long in transit that the Segments can also be delayed so long in transit that the sender times out and retransmits them.sender times out and retransmits them.

• If a retransmitted segment takes a different route than the If a retransmitted segment takes a different route than the original, and is fragmented differently, bits and pieces of both original, and is fragmented differently, bits and pieces of both the original and the duplicate can arrive sporadically, requiring the original and the duplicate can arrive sporadically, requiring careful administration to achieve a reliable byte stream.careful administration to achieve a reliable byte stream.

Page 12: 1 The Transport Layer The Internet Transport Protocols – TCP and UDP

1212

The TCP ProtocolThe TCP Protocol• Finally, with so many networks making up the Finally, with so many networks making up the

Internet, it is possible that a segment may Internet, it is possible that a segment may occasionally hit a congested (or broken) network occasionally hit a congested (or broken) network along its path.along its path.

TCP must be prepared to deal with these TCP must be prepared to deal with these problems and solve them in an efficient problems and solve them in an efficient way. way.

A considerable effort has gone into A considerable effort has gone into optimizing the performance of TCP streams, optimizing the performance of TCP streams, even in the face of network problems. We even in the face of network problems. We will discuss some algorithms next. will discuss some algorithms next.

Page 13: 1 The Transport Layer The Internet Transport Protocols – TCP and UDP

1313

The TCP Segment HeaderThe TCP Segment Header Every Every segment begins with a fixed-format segment begins with a fixed-format

20-byte header20-byte header. . The fixed header may be followed by header The fixed header may be followed by header

options. options. After the options, if any, up to 65,535 – 20 – After the options, if any, up to 65,535 – 20 –

20 = 65, 495 data bytes may follow, where 20 = 65, 495 data bytes may follow, where the first 20 refers to the IP header and the the first 20 refers to the IP header and the second to the TCP header. second to the TCP header.

Segments without any data are legal and Segments without any data are legal and are commonly used for acknowledgments are commonly used for acknowledgments and control messages.and control messages.

Page 14: 1 The Transport Layer The Internet Transport Protocols – TCP and UDP

1414

Fig. 1. The TCP header

The TCP Segment HeaderThe TCP Segment Header

Page 15: 1 The Transport Layer The Internet Transport Protocols – TCP and UDP

1515

Source portSource port and and Destination portDestination port – identify the – identify the local end points of the connection.local end points of the connection.

Sequence numberSequence number and and acknowledgement numberacknowledgement number (specifies the next sequence number expected)(specifies the next sequence number expected)

TCP header lengthTCP header length – tells now many 32-bit words – tells now many 32-bit words are contained in the TCP header (because Options are contained in the TCP header (because Options field is of variable length)field is of variable length)

Next comes a 6-bit field that is not used.Next comes a 6-bit field that is not used. Next come 6 1-bit flags:Next come 6 1-bit flags:

• URGURG is set to 1 if the Urgent pointer is in use. The Urgent is set to 1 if the Urgent pointer is in use. The Urgent Pointer is used to indicate a byte offset (from the current Pointer is used to indicate a byte offset (from the current sequence number) at which urgent data is locatedsequence number) at which urgent data is located

• ACKACK is set to 1 to indicate that the acknowledgement is set to 1 to indicate that the acknowledgement number field is valid. Otherwise, if set to 0, then this number field is valid. Otherwise, if set to 0, then this segment does not contain an acknowledgmentsegment does not contain an acknowledgment

The TCP Segment HeaderThe TCP Segment Header

Page 16: 1 The Transport Layer The Internet Transport Protocols – TCP and UDP

1616

• PSHPSH bit indicates PUSHed data. The receiver hereby bit indicates PUSHed data. The receiver hereby kindly requested to deliver the data to the application kindly requested to deliver the data to the application upon arrival and not buffer it (done for efficiency)upon arrival and not buffer it (done for efficiency)

• RSTRST bit is used to reset a connection that has become bit is used to reset a connection that has become confused due to a host crash or some other reason. It is confused due to a host crash or some other reason. It is also used to reject an invalid segment or refuse an also used to reject an invalid segment or refuse an attempt to open a connection.attempt to open a connection.

• SYNSYN bit is used to establish connections. SYN=1 and bit is used to establish connections. SYN=1 and ACK=0 – connection request, SYN=1 and ACK=1 – ACK=0 – connection request, SYN=1 and ACK=1 – connection accepted.connection accepted.

• FINFIN but is used to release a connection. It specifies that but is used to release a connection. It specifies that the sender has no more data to transmit. the sender has no more data to transmit.

Window sizeWindow size field tells how many bytes may be field tells how many bytes may be sent starting at the byte acknowledged. sent starting at the byte acknowledged.

The TCP Segment HeaderThe TCP Segment Header

Page 17: 1 The Transport Layer The Internet Transport Protocols – TCP and UDP

1717

A A ChecksumChecksum is also provided for extreme is also provided for extreme reliability – it checksums the header and reliability – it checksums the header and the data.the data.

OptionsOptions field was designed to provide a field was designed to provide a way to add extra facilities not covered by way to add extra facilities not covered by the regular header. For example, allow the regular header. For example, allow each host to specify the maximum TCP each host to specify the maximum TCP payload it is willing to accept. (using large payload it is willing to accept. (using large segments is more efficient than using segments is more efficient than using small ones)small ones)

The TCP Segment HeaderThe TCP Segment Header

Page 18: 1 The Transport Layer The Internet Transport Protocols – TCP and UDP

1818

TCP Connection ManagementTCP Connection Management

Connections are established in TCP Connections are established in TCP using a using a three-way handshakethree-way handshake::• Host 1 chooses a sequence number, Host 1 chooses a sequence number, xx, and , and

sends a CONNECTION REQUEST containing it to sends a CONNECTION REQUEST containing it to host 2.host 2.

• Host 2 replies with CONNECTION ACCEPTED Host 2 replies with CONNECTION ACCEPTED acknowledgment acknowledgment xx, and announcing its own , and announcing its own initial sequence number, initial sequence number, yy..

• Finally Host 1 acknowledges host 2’s choice of Finally Host 1 acknowledges host 2’s choice of an initial sequence number in the first data an initial sequence number in the first data that it sends.that it sends.

Page 19: 1 The Transport Layer The Internet Transport Protocols – TCP and UDP

1919

Fig. 2. TCP connection

establishment

TCP Connection ManagementTCP Connection Management

To establish a connection, one side, say a server, passively waits for an incoming connection by executing LISTEN and ACCEPT primitives

The other side, say a client, executes a CONNECT primitive, specifying the IP address and port to which it wants to connect, and the max TCP segment size it is willing to accept

The CONNECT primitive sends a TCP segment with the SYN bit = 1 and ACK = 0 and waits for a response

Page 20: 1 The Transport Layer The Internet Transport Protocols – TCP and UDP

2020

When this segment arrives When this segment arrives at the destination, the TCP at the destination, the TCP entity there checks to see if entity there checks to see if there is a process that has there is a process that has done a LISTEN on the port done a LISTEN on the port given in the Destination port given in the Destination port field. If not, it sends a reply field. If not, it sends a reply with the RST bit on to reject with the RST bit on to reject the connection.the connection.

If some process is listening If some process is listening on the port, that process is on the port, that process is given the incoming TCP given the incoming TCP segment. It can either segment. It can either accept or reject the accept or reject the connection. If it accepts, an connection. If it accepts, an acknowledgment segment is acknowledgment segment is sent back.sent back.

TCP Connection ManagementTCP Connection Management

Page 21: 1 The Transport Layer The Internet Transport Protocols – TCP and UDP

2121

TCP Transmission PolicyTCP Transmission Policy Window management in TCP is not tied to Window management in TCP is not tied to

acknowledgments as it is in most data link acknowledgments as it is in most data link protocols.protocols.

For example: For example: • suppose that the receiver has a 4095-byte buffer. suppose that the receiver has a 4095-byte buffer. • If the sender transmits a 2048-byte segment that is If the sender transmits a 2048-byte segment that is

correctly received, the receiver will acknowledge the correctly received, the receiver will acknowledge the segment. segment.

• However, since it now has only 2048 of buffer space (until However, since it now has only 2048 of buffer space (until the application removes some data from the buffer), it will the application removes some data from the buffer), it will advertise a window of 2048 starting a the next byte advertise a window of 2048 starting a the next byte expected. expected.

• Now the sender transmits another 2048 bytes, which are Now the sender transmits another 2048 bytes, which are acknowledged, but the advertised window is 0. acknowledged, but the advertised window is 0.

• The sender must stop until the application process on the The sender must stop until the application process on the receiving side has removed some data from the buffer. receiving side has removed some data from the buffer. Then, TCP can advertise a larger window.Then, TCP can advertise a larger window.

• There are two exceptions: urgent data may be sent (ex. to There are two exceptions: urgent data may be sent (ex. to allow user to kill the process), or send 1 byte to make the allow user to kill the process), or send 1 byte to make the receiver re-announce the window (in case it was lost)receiver re-announce the window (in case it was lost)

Page 22: 1 The Transport Layer The Internet Transport Protocols – TCP and UDP

2222

When the load offered to any networks is When the load offered to any networks is more than it can handle, congestion builds more than it can handle, congestion builds up. The Internet is no exception. up. The Internet is no exception.

Algorithms have been developed over the Algorithms have been developed over the past decade to deal with congestion. past decade to deal with congestion.

Although the network layer also tries to Although the network layer also tries to manage congestion, most of the heavy manage congestion, most of the heavy lifting is done by TCP because the real lifting is done by TCP because the real solution to congestion is to slow down the solution to congestion is to slow down the data rate. data rate.

TCP Congestion ControlTCP Congestion Control

Page 23: 1 The Transport Layer The Internet Transport Protocols – TCP and UDP

2323

TCP Congestion ControlTCP Congestion Control

Fig. 3. (a) A fast network feeding a low capacity receiver

(b) A slow network feeding a high capacity receiver

Page 24: 1 The Transport Layer The Internet Transport Protocols – TCP and UDP

2424

In theory congestion can be dealt with by In theory congestion can be dealt with by employing a principle borrowed from employing a principle borrowed from physics: the law of conservation of physics: the law of conservation of packets. The idea is not to inject a new packets. The idea is not to inject a new packet into the network until an old one packet into the network until an old one leaves (i.e. is delivered). TCP attempts to leaves (i.e. is delivered). TCP attempts to achieve this goal by achieve this goal by dynamically dynamically manipulating the Window sizemanipulating the Window size. .

TCP Congestion ControlTCP Congestion Control

Page 25: 1 The Transport Layer The Internet Transport Protocols – TCP and UDP

2525

TCP Timer ManagementTCP Timer Management TCP uses multiple timers (at least conceptually) TCP uses multiple timers (at least conceptually)

to do its work. to do its work.

The most important of these is the The most important of these is the retransmission retransmission timertimer. When a segment is sent, a retransmission . When a segment is sent, a retransmission timer is started. If the segment is acknowledged timer is started. If the segment is acknowledged before the timer expires, the timer is stopped. If, before the timer expires, the timer is stopped. If, on the other hand, the timer goes off before the on the other hand, the timer goes off before the acknowledgment comes in the segment is acknowledgment comes in the segment is retransmitted (and the timer started again).retransmitted (and the timer started again).

The question that arises is: How long should the The question that arises is: How long should the timeout interval be?timeout interval be?

Page 26: 1 The Transport Layer The Internet Transport Protocols – TCP and UDP

2626

TCP Timer ManagementTCP Timer Management This problem is much more difficult in the This problem is much more difficult in the

Internet transport layer than in the generic Internet transport layer than in the generic data link protocols, where the delay is very data link protocols, where the delay is very predictable. predictable.

The solution is to use a The solution is to use a highly dynamic highly dynamic statistical algorithmstatistical algorithm that constantly that constantly adjusts the timeout interval based on adjusts the timeout interval based on continuous measurements of network continuous measurements of network performance. This algorithm was proposed performance. This algorithm was proposed by Jacobsonby Jacobson in 1988. in 1988.

Page 27: 1 The Transport Layer The Internet Transport Protocols – TCP and UDP

2727

UDPUDP The Internet protocol suite also supports a The Internet protocol suite also supports a

connectionlessconnectionless transport protocol, transport protocol, UDP UDP (User Data Protocol)(User Data Protocol)

UDP provides a way for applications to UDP provides a way for applications to send encapsulated raw IP datagrams and send encapsulated raw IP datagrams and send them without having to establish a send them without having to establish a connection.connection.

Many client-server applications that have Many client-server applications that have 1 request and 1 response use UDP rather 1 request and 1 response use UDP rather than go to the trouble of establishing and than go to the trouble of establishing and later releasing a connection.later releasing a connection.

Page 28: 1 The Transport Layer The Internet Transport Protocols – TCP and UDP

2828

UDPUDP

A UDP segment consists of an 8-byte header A UDP segment consists of an 8-byte header followed by the data. followed by the data.

Fig. 4. The UDP header

Page 29: 1 The Transport Layer The Internet Transport Protocols – TCP and UDP

2929

UDPUDP The The two portstwo ports serve the same function as serve the same function as

they do in TCP: to identify the end points they do in TCP: to identify the end points within the source and destination within the source and destination machines. machines.

The The UDP lengthUDP length field includes the 8-byte field includes the 8-byte header and the data.header and the data.

The The UDP checksumUDP checksum is used to verify the is used to verify the size of header and data.size of header and data.