cosc 3407: operating systems lecture 21: network protocols

31
Network Protocols Protocol: agreement between two parties as to how information is to be transmitted. Example: system calls are the protocol between the operating system and applications NFS WWW E- mail ssh RPC TCP UDP IP Packet radio ATM Etherne t Physical/Link Layer Network Layer Transport Layer

Upload: laurence-eaton

Post on 03-Jan-2016

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: COSC 3407: Operating Systems Lecture 21: Network Protocols

Network Protocols Protocol: agreement between two parties as to

how information is to be transmitted. Example: system calls are the protocol between

the operating system and applications

NFS WWW E-mail ssh

RPC

TCPUDP

IP

Packet radioATMEthernetPhysical/Link Layer

Network Layer

Transport Layer

Page 2: COSC 3407: Operating Systems Lecture 21: Network Protocols

Network Protocols Networking examples: many levels

– Physical level: mechanical and electrical network (e.g. how are 0 and 1 represented)

– Link level: packet formats/error control (for instance, the CSMA/CD protocol)

– Network level: network routing, addressing – Transport Level: reliable message delivery

Layering – build complex services from simpler services

Page 3: COSC 3407: Operating Systems Lecture 21: Network Protocols

Protocols

Our goal in the following is to show how to construct a secure, ordered, message service routed to anywhere

Physical Reality: packets

Abstraction: messages

Limited size Arbitrary size

Unordered (sometimes) Ordered

Unreliable Reliable

Machine-to-machine Process-to-process

Only on local area net Routed anywhere

Asynchronous Synchronous

Insecure Secure

Page 4: COSC 3407: Operating Systems Lecture 21: Network Protocols

Basic Networking Limitations The physical/link layer is pretty limited

– Packets are of limited size (called the “Maximum Transfer Unit or MTU: often 200-1500 bytes in size)

– Routing is limited to within a physical link (wire) or perhaps through a switch

– Packets can get lost or garbled– Physical routers crash/links get damaged

Datagram: an independent, self-contained network message whose arrival, arrival time, and content are not guaranteed

Need resilient routing algorithms to send messages on wide area– Multi-hop routing mechanisms– Redundant links/Ability to route around failed links

Page 5: COSC 3407: Operating Systems Lecture 21: Network Protocols

Arbitrary size messages Arbitrary size messages on top of limited size

ones

Send N little messages – split up message into fixed size packets– abcdefgh -> 1 of 3/abc 2 of 3/def 3 of 3/gh

Checksum can be computed on each fragment, or on whole message

Page 6: COSC 3407: Operating Systems Lecture 21: Network Protocols

IP – Internet Protocol Deliver messages unreliably (“Best effort”) from

one machine in internet to another.a. Routes packets from one machine through

internet to anotherb. Some intermediate links may have limited size.

– Fragments on demand, re-assembles at destination

c. Unreliable, unordered, machine->machine Includes 256 different “sub-protocols” built on

top of IP– Examples: ICMP(1), TCP(6), UDP (17),

IPSEC(50,51)

Page 7: COSC 3407: Operating Systems Lecture 21: Network Protocols

IP Packet Format

16-bit identificationToS4

13-bit frag offTotal length(16-bits)

protocolTTL 16-bit header checksum32-bit source IP address

32-bit destination IP address

IHLflags

options (if any)

Data

0 15 16 31IP Ver4

IP HeaderLength

Size of datagram(header+data)

Flags &Fragmentationto split large

messages

Time toLive (hops)

Type oftransportprotocol

IP header20 bytes

Page 8: COSC 3407: Operating Systems Lecture 21: Network Protocols

Process-process communication User process communication on top of machine to

machine communication What we really want is routing from processprocess

– Example: ssh, email, ftp, web browsing Mailbox (or “port”) address – include in each

message, the destination mailbox. Allows you to direct each message to correct process A “port” is a 16-bit identifier used in addition to IP

address– A communication channel (connection) defined by 4

items: [source address, source port, dest address, dest port]

Page 9: COSC 3407: Operating Systems Lecture 21: Network Protocols

UDP – Unreliable Datagram Protocol Unreliable, unordered, user-to-user

communication Built on top of IP (IP Protocol 17) Important aspect: low overhead!

– Often used for high-bandwidth video streams

UDP Data

16-bit UDP length 16-bit UDP checksum16-bit source port 16-bit destination port

IP Header(20 bytes)

Page 10: COSC 3407: Operating Systems Lecture 21: Network Protocols

Ordered messages Ordered messages on top of unordered ones IP can re-order packets – send A, B arrives: B, A How do we fix this? Assign sequence numbers to

successive packets –– 0, 1, 2, 3, ...

If arrive out of order, don’t deliver #3 to user application until get #2.

Sequence numbers specific to a connection – for example, the machine-machine (or mailbox-mailbox) pair.

This means put “source” as well as “destination” in each header.

Page 11: COSC 3407: Operating Systems Lecture 21: Network Protocols

Performance considerations Overhead – CPU time to put packet on wire Latency – how long before first bit of packet

arrives at receiver– Raw transfer time + overhead at each routing hop

Throughput – maximum bytes per second– Depends on “wire speed”, but also limited by

slowest router (routing delay) or by congestion at routers

Contributions to Latency– Wire latency: depends on speed of light on wire

» about 1.5 ns/foot– Router latency: depends on internals of router

» Could be < 1 ms (for a good router)» Question: can router handle full wire throughput?

Page 12: COSC 3407: Operating Systems Lecture 21: Network Protocols

Example How long to send 4KB packet over various networks? Typical

overhead to send a packet: 1 ms. Ethernet (10 – 10,000Mb/s) within Parker:

– Latency: speed of light = 1.5 ns / foot, implies < 1 microsecond (if no routers in path).

– Throughput delay: packet doesn’t arrive until all its bits get there! So 4KB/10 Mb/s = 3 milliseconds (roughly as long as a disk!)

ATM (155 Mb/s) within Parker:– Latency same.– Throughput delay: 4KB/ 155 Mb/s = 200 microseconds.

ATM cross-country?– Latency: 3000 miles * 5000 ft/mile => 15 milliseconds.– Throughput delay: same as above.

How many bits are in transit at the same time?– 15 ms * 155 Mb/s => 280 KB

Key to good performance: in local area, minimize overhead, improve bandwidth. In wide area, keep pipeline full.

Page 13: COSC 3407: Operating Systems Lecture 21: Network Protocols

Reliable message delivery Reliable message delivery on top of unreliable

delivery All of these networks can garble, drop messages.1. Physical media – if transmit close to maximum rate,

get more throughput, even if some messages get lost

2. Congestion – what if no place to put incoming message (no buffer space)?– Point-to-point network: no space at each

switch/router– Broadcast network: two hosts try to use same link?– In any network: no buffer space at destination– Rate mismatch: what if sender sends faster than

receiver can process?

Page 14: COSC 3407: Operating Systems Lecture 21: Network Protocols

Reliable message delivery

1. Detect garbling at receiver via checksum, discard if incorrect

2. Receiver ack’s if received properly3. Timeout at sender. If no ack, retransmit

Page 15: COSC 3407: Operating Systems Lecture 21: Network Protocols

Reliable message delivery Some questions: If the sender doesn’t get an ack, does that mean the

receiver didn’t get the original message?– No.

What if ack gets dropped? Or if message gets delayed.– Sender doesn’t get ack; retransmits. Receiver gets

message twice, ack’s each. Solution: put sequence number in message to identify re-

transmitted packets. Receiver checks for duplicate sequence #’s. If so, discards.

1. Sender must keep copy of every message that has not been ack’ed yet (easy)

2. Receiver must keep track of every message that could be a duplicate (hard! How does receiver know when it’s ok to forget about received messages?)

Page 16: COSC 3407: Operating Systems Lecture 21: Network Protocols

Reliable message delivery Several approaches to maintaining state at

sender/receiver:a. Alternating bit protocol. One bit sequence number.

– Send one message at a time;– don’t send next message until ack received. – Sender only keeps copy of last message; – receiver keeps track of sequence # of last message

received. Pros:

– Simple, Small overhead Cons

– Poor performance» Wire can hold multiple messages; want to

fill up at (wire latency throughput)

– doesn’t work if network can delayor duplicate messages arbitrarily

A Bmsg,#0

ack,#0

msg,#0

msg,#1

ack,#1

ack,#0

Page 17: COSC 3407: Operating Systems Lecture 21: Network Protocols

Reliable message deliveryb. Window-based protocol (TCP).

– Send up to N messages at a time, without waiting for ack.

– “Window” reflects storage at receiver – sender shouldn’t overrun receiver’s buffer space.

– Each message has sequence number. – Receiver can say, “I’ve ack’ed up to message #X ->

any message below X is a duplicate.A Bmsg,#0

ack,#0

Page 18: COSC 3407: Operating Systems Lecture 21: Network Protocols

Reliable message delivery Acks serve dual purpose:

– Reliability: Confirming packet received– Flow Control: Receiver ready for packet

» Remaining space in queue at receiver can be returned with ACK

What if message gets garbled/dropped? Receiver will get messages out of order!– Discard any messages that arrive out of order?

» Simple, worse performance

– Keep copy until sender fills in the missing piece?» Reduces # of retransmits, more complex

What if ack gets dropped? – Timeout and resend just the un-acknowledged

message.

Page 19: COSC 3407: Operating Systems Lecture 21: Network Protocols

TCP: transmission control protocol Reliable byte stream between two processes on

different machines over Internet (read, write, flush).

Fragments byte stream into packets, hands packets to IP.

Uses window-based protocol (to minimize state at sender and receiver): send up to N messages at a time, without waiting for ack.

“Window” reflects storage at receiver – sender shouldn’t overrun receiver’s buffer space.

Sender has three regions: sent and ack’ed, sent and not ack’ed, not yet sent

Page 20: COSC 3407: Operating Systems Lecture 21: Network Protocols

TCP: transmission control protocol Receiver has three regions: received and ack’ed (given to

application), received and buffered, not yet received (or received and discarded because out of order)

Each ack says: “got all messages up to #”. What happens if ack is delayed, arrives out of order? OK in this scheme. Just discard.

Sequence numbers

Sender messages

Sent, ack’ed

Sent, not ack’ed

Not yet sent

receiver messages

Received, given to app

Received, buffered

Not yet received

Page 21: COSC 3407: Operating Systems Lecture 21: Network Protocols

Arbitrary Size Messages (revisited) Face similar issues as in TCP when building big

messages on small ones, when messages can get dropped.

1. Ack each fragment? Lots of acks2. One ack for entire big message? Re-transmit all

fragments, even if only one gets dropped3. “Blast protocol” – send one ack, tells sender

which pieces were missing. Selective retransmit.

Page 22: COSC 3407: Operating Systems Lecture 21: Network Protocols

Initialization How do you know which sequence # to start with?

– When machine boots, ok to start with #0?» No. Could send two separate messages with the same serial

#!» Receiver might end up discarding valid packets, or duplicate

ack from original transmission might hide lost packet– Also, if it is possible to predict sequence numbers, might

be possible for attacker to hijack TCP connection1. Time to live: each TCP packet has a deadline. If not delivered

in X seconds, then dropped. – Thus, can re-use sequence numbers if wait for all packets

in flight to be delivered or to expire.2. Epoch # – uniquely identifies which set of sequence numbers

are being used. – Put in every message, epoch # incremented on crash

and/or when run out of sequence #’s, and stored on disk.3. Pseudo-random increment to previous sequence number

– Used by a number of implementations now

Page 23: COSC 3407: Operating Systems Lecture 21: Network Protocols

Congestion How long should timeout be for re-sending messages?

– Too long? Wastes time if message is dropped.– Too short? Retransmit even though ack will arrive

shortly. Stability problem: more congestion -> ack is delayed ->

unnecessary timeout -> more traffic -> more congestion TCP solution: “slow start”. Originally, window size = buffer

space on remote end. Now, window size = control on how much to add to

congestion. Start sending slowly.

– If no timeout, slowly increase window size (throughput).

– If a timeout occurs, it means there’s congestion, so cut window size (throughput) in half.

Page 24: COSC 3407: Operating Systems Lecture 21: Network Protocols

General’s Paradox Can I use messages and retries over an unreliable

network to synchronize two machines so that they are guaranteed to do some operation at the same time?

Remarkably, no, even if all messages get through. General’s paradox: two generals, on separate

mountains. – Can only communicate via messengers; the

messengers can be captured. Need to coordinate the attack

– if they attack at different times, then they all die. – If they attack at the same time, they win.

Page 25: COSC 3407: Operating Systems Lecture 21: Network Protocols

General’s Paradox

Even if all messages are delivered, can’t coordinate! – Can’t simultaneously get two generals or two

machines to agree to do something at the same time.

No solution to this – one of the few things in CS that’s just impossible.

A B11 am ok!

Ok 11’s good for me

So 11 it is?

Yeah, but what if you don’t get this ack?

...........

Page 26: COSC 3407: Operating Systems Lecture 21: Network Protocols

Two phase commit Since we can’t solve the General’s Paradox (i.e.,

simultaneous action), let’s solve a related problem.

Abstraction: distributed transaction – two machines agree to do something, or not do it, atomically (but not necessarily at exactly the same time).

Page 27: COSC 3407: Operating Systems Lecture 21: Network Protocols

Two phase commit Two phase commit protocol does this:

– Use a persistent, stable log on each machine to keep track of whether commit has happened. » If a machine crashes, when it wakes up it first checks its

log to see what state the world was in at the time of the crash.

– Prepare Phase:» The global coordinator requests that all participants will

promise to commit or rollback the transaction» Participants record promise in log, then acknowledge» If anyone votes to abort, coordinator writes “abort” in its

log and tells everyone to abort; each records “abort” in log

– Commit Phase:» After all participants respond that they are prepared,

then the coordinator writes “commit” to its log» Then asks all nodes to commit; they respond with ack» After receive acks, coordinator writes “got commit” to log

– Log can be used to complete this process such that all machines either commit or don’t commit

Page 28: COSC 3407: Operating Systems Lecture 21: Network Protocols

Two phase commit First phase, ask if each can commit – for instance,

transfer of funds from one bank to another. 1. A writes, “Begin transaction” to log 2. A -> B: OK to transfer funds to me? Not enough cash: B-> A: transaction aborted A writes “Abort” to log Enough cash: B: Write new X account balance to log B->A: OK, I can commit

Page 29: COSC 3407: Operating Systems Lecture 21: Network Protocols

Two phase commit Second phase, A can decide for both, whether they will

commit. 3. A: Write new Y account balance to log 4. Write commit to log 5. Send message to B that commit occurred 6. Write “Got commit” to log

What if:– B crashes at 1? Wakes up, does nothing. A will timeout,

abort transaction, retry.– A crashes at 3? Wakes up, sees transaction in progress.

What transaction, sends message to B, abort.– B crashes at 3? B will come back up, look at log, so that

when A sends it “Commit” message, it will say, oh, ok, commit.

Page 30: COSC 3407: Operating Systems Lecture 21: Network Protocols

Two phase commit One problem with 2PC is “Blocking”: That is, a site gets

stuck in a situation where it cannot continue until some other site (usually the coordinator) recovers.

How could this happen?– Participant site B writes a “prepared to commit”

record to its log, sends a “yes” vote to the coordinator (site A) and crashes.

– Site A crashes– Site B wakes up, checks its log and realizes that it had

voted “yes” on the update. » It sends a message to site A, asking what happened. » At this point, B cannot change its mind and decide to

abort, because the update may have committed while it was crashed, thus it is blocked.

» Note, B may be able to learn the fate of the update by asking some of the other participants.

Page 31: COSC 3407: Operating Systems Lecture 21: Network Protocols

Two phase commit Blocking is problematic because a blocked site

must hold resources (for example, locks on updated items, pages pinned in memory, etc.) until it learns the fate of the update.

Question: Can blocking be avoided? Answer: yes! If you are willing to impose some

constraints on the way that voting is done, an algorithm called “Three Phase Commit” can solve the problem. – 3PC is not generally used in practice, however,

due to performance reasons and the low instance of blocking in practice.