Reliable data transfer

Lab 2

Build rdt2.0. Break it. Fix rdt2.1.

Where we are

Where are we in the network stack?

THE INTERNET PROTOCOL STACK Application Transport ← today: reliable data transfer Network Link Physical WHO TALKS TO WHOM sending application Sender — transport layer, rdt2.py unreliable channel — may corrupt a packet Receiver — transport layer, rdt2.py receiving application

The transport layer delivers application data reliably over a channel that is not. Today's sender and receiver are that layer in miniature: the rdt protocols of Chapter 3, not TCP.

Today

Today

  • GivenRun rdt1.0, paste its failing line into Q0.
  • BuildImplement rdt2.0.
  • BreakSee what a corrupted reply does to it.
  • FixRepair rdt2.1.

First: accept, clone, python3 check.py. After you push submit/1, you can leave.

Recap

rdt1.0

SENDER Wait for call from above start rdt_send(data) packet = make_pkt(data) udt_send(packet) ← event ← actions RECEIVER Wait for call from below start rdt_rcv(packet) extract(packet, data) deliver_data(data)
SENDER RECEIVER Wait for call from above Wait for call from below d1 rdt_send(d1) deliver_data(d1) d2 rdt_send(d2) deliver_data(d2) no checksum: one byte wrong never finds out d2 delivered wrong, silently
Questiond2 arrives corrupted. What happens?
Recap

rdt2.0

SENDER Wait for call from above Wait for ACK or NAK rdt_send(data) sndpkt = make_pkt(data, checksum) udt_send(sndpkt) rdt_rcv(rcvpkt) && isACK(rcvpkt) Λ rdt_rcv(rcvpkt) && isNAK(rcvpkt) udt_send(sndpkt) RECEIVER Wait for call from below rdt_rcv(rcvpkt) && corrupt(rcvpkt) udt_send(NAK) rdt_rcv(rcvpkt) && notcorrupt(rcvpkt) extract(rcvpkt, data) deliver_data(data) udt_send(ACK)

In colour: the four arrows you write in rdt2.py. Corrupt → NAK, intact → deliver and ACK; NAK → resend, ACK → next segment.

Recap

rdt2.0: corrupted data

SENDER RECEIVER Wait for call from above Wait for call from below d1 ACK d2 NAK d2 ACK rdt_send(d1) → wait for ACK/NAK intact: deliver d1, send ACK ACK → wait for call rdt_send(d2) → wait for ACK/NAK corrupt: send NAK nothing delivered NAK: send d2 again (still waiting) intact: deliver d2, send ACK ACK → wait for call
Recap

rdt2.0: corrupted reply

SENDER RECEIVER Wait for call from above Wait for call from below d1 rdt_send(d1) → wait for ACK/NAK send ACK or NAK reply garbled reply ACK or NAK?
QuestionShould the sender resend, or move on?

rdt2.0 has no arrow for a corrupted reply. Suppose it guesses.

Recap

rdt2.0: corrupted reply, guess ACK

SENDER RECEIVER Wait for call from above Wait for call from below d1 rdt_send(d1) → wait for ACK/NAK corrupt: send NAK nothing delivered NAK garbled reply guess ACK → wait for call d2 rdt_send(d2) intact: deliver d2, send ACK d1 never arrives d1 lost, silently
Recap

rdt2.0: corrupted reply, guess NAK

SENDER RECEIVER Wait for call from above Wait for call from below d1 rdt_send(d1) → wait for ACK/NAK intact: deliver d1, send ACK ACK garbled reply guess NAK → send d1 again d1 (still waiting) intact: deliver d1, send ACK cannot tell it is a copy d1 delivered twice
Neither guess is safe. The receiver cannot tell a copy from new data.
Recap

Sequence numbers

SENDER SENDS d1 seq=0 d2 seq=1 d3 seq=0 d4 seq=1 RECEIVER EXPECTS 0, then 1 1, then 0 0, then 1 1, then 0 one packet in flight at a time, so one bit is enough

the number it expects

New data: deliver it, send ACK, expect the other number.

the other number

A copy: send ACK again, do not deliver it.

corrupted

Send NAK, as in rdt2.0.

Recap

rdt2.1: corrupted reply

SENDER RECEIVER Wait for call 0 from above Wait for 0 from below d1 seq=0 rdt_send(d1) → wait for ACK/NAK 0 seq 0: deliver d1, send ACK → wait for 1 ACK 0 garbled reply always resend → d1 again d1 seq=0 (still waiting) seq 0 again: a copy send ACK, do not deliver ACK 0 ACK → wait for call 1 d2 seq=1 seq 1: deliver d2, send ACK
Every byte delivered, once. Resending is safe now: the receiver can tell a copy from new data.
Recap

Which protocol survives which channel

The channel…rdt1.0rdt2.0 rdt2.1
delivers everything intactyesyesyes
corrupts data packetsnoyesyes
corrupts ACKs and NAKs toononoyes

Every cell is one command: python3 test.py <protocol> <channel>

The lab

The simulator

your Sender rdt2.py the link 25 ms each way may corrupt a packet never loses or reorders one your Receiver rdt2.py udt_send(DATA) rdt_rcv(DATA) udt_send(ACK / NAK) rdt_rcv(ACK / NAK) a virtual clock wakes both ends every 5 ms with on_tick()

You write the sender and the receiver. The simulator delays and corrupts; no real network, and the same seed always gives the same run.

The lab

The API

On the worksheetIn the code
event rdt_send(data)on_tick(self, now), when state == WAIT_FOR_CALLgiven
event rdt_rcv(rcvpkt)rdt_rcv(self, rcvpkt, now), in Sender and in Receiveryou write
the state bubblesself.state: WAIT_FOR_CALL or WAIT_FOR_ACK_OR_NAK
the actionsthe same names: make_pkt, udt_send, extract, deliver_data
make_pkt(seq, flags, data)      # build a packet; seq = 0 until rdt2.1
self.udt_send(pkt)              # put it on the channel
corrupt(rcvpkt)                 # failed its checksum?   notcorrupt(rcvpkt): the opposite
extract(rcvpkt)                 # -> (seq, flags, data)
isACK(rcvpkt)  isNAK(rcvpkt)    # an intact ACK / NAK?
self.deliver_data(data)         # receiver only: hand data to the application

self.sndpkt                     # sender: the packet in flight
self.segments, self.index       # sender: the data, chopped; which segment you are on
self.state                      # sender: WAIT_FOR_CALL or WAIT_FOR_ACK_OR_NAK
The lab

Testing and debugging

test.py

python3 test.py rdt2 corrupt-data — the packets. Use it to see what went wrong.

check.py

python3 check.py 2 — pass or fail per case. The same checks the grader runs.

editrdt2.py→ checkpython3 check.py 2→ readthe test.py line under the FAIL→ fixthe first packet that is not what you meant→ repeatuntil every case is ok
$ python3 check.py 2
Part 2 — rdt2.0 — packets get corrupted  [10 points]
  ok    clean channel
  ok    20% of packets corrupted
  FAIL  40% corrupted: never finished — stuck waiting for a reply
        see the packets:  python3 test.py rdt2 corrupt-data --rate 0.4 --seed 16 --bytes 1000
  …
  -> 2/5 cases, 4/10 points
Reference

Reference

The rest is for later, while you work.

  • rdt1.0, rdt2.0, rdt2.1: the complete state machines
  • What a packet capture can and cannot tell you
  • Reading a test.py timeline
  • Submitting
Reference

rdt1.0: the complete machines

SENDER Wait for call from above start rdt_send(data) packet = make_pkt(data) udt_send(packet) ← event ← actions RECEIVER Wait for call from below start rdt_rcv(packet) extract(packet, data) deliver_data(data)
SENDER RECEIVER Wait for call from above Wait for call from below d1 d2 rdt_send(d1) deliver_data(d1) rdt_send(d2) deliver_data(d2)

Assumes the channel delivers every packet intact: no checksum, no reply, nothing to resend. One state each.

Reference

rdt2.0: the complete machines

SENDER Wait for call from above Wait for ACK or NAK rdt_send(data) sndpkt = make_pkt(data, checksum) udt_send(sndpkt) rdt_rcv(rcvpkt) && isACK(rcvpkt) Λ rdt_rcv(rcvpkt) && isNAK(rcvpkt) udt_send(sndpkt) RECEIVER Wait for call from below rdt_rcv(rcvpkt) && corrupt(rcvpkt) udt_send(NAK) rdt_rcv(rcvpkt) && notcorrupt(rcvpkt) extract(rcvpkt, data) deliver_data(data) udt_send(ACK)

Receiver: corrupt → NAK; intact → deliver, ACK. Sender: NAK → resend; ACK → next segment. Its limit: a corrupted reply has no arrow, so the sender can only guess.

Reference

rdt2.1 sender

SENDER Wait for call 0 from above Wait for ACK or NAK 0 Wait for call 1 from above Wait for ACK or NAK 1 rdt_send(data) sndpkt = make_pkt(0, data, checksum) udt_send(sndpkt) corrupt || isNAK udt_send(sndpkt) notcorrupt && isACK Λ rdt_send(data) sndpkt = make_pkt(1, data, checksum) udt_send(sndpkt) corrupt || isNAK udt_send(sndpkt) notcorrupt && isACK Λ

Four states: which number it is sending, and whether it is waiting. Corrupted reply or NAK: always resend. Intact ACK: the other row.

Reference

rdt2.1 receiver

RECEIVER Wait for 0 from below Wait for 1 from below notcorrupt && has_seq0 extract(rcvpkt, data) deliver_data(data) udt_send(ACK) notcorrupt && has_seq1 extract(rcvpkt, data) deliver_data(data) udt_send(ACK) corrupt udt_send(NAK) notcorrupt && has_seq1 udt_send(ACK) corrupt udt_send(NAK) notcorrupt && has_seq0 udt_send(ACK)

Two states: which number it expects. The expected number is new data: deliver, ACK, switch. The other number is a copy: ACK it again, do not deliver it. Corrupted: NAK.

Reference

Reading the timeline

$ python3 test.py rdt21 corrupt-replies --rate 0.2 --seed 19 --bytes 1000
rdt2.1 on corrupt-replies: 20% of all packets are corrupted — ACKs and NAKs too
  seed 19: LOST DATA — the application received 800 of 1000 bytes; the rest was lost, silently

   time  direction          bytes  what happened
      5  sender -> receiver   217  delivered at 30 ms   DATA seq=0 len=200
     30  receiver -> sender    17  delivered at 55 ms   ACK seq=0
     55  sender -> receiver   217  delivered at 80 ms   DATA seq=1 len=200
     80  receiver -> sender    17  delivered at 105 ms  ACK seq=1
    105  sender -> receiver   217  corrupted, delivered at 130 ms  DATA seq=0 len=200
    130  receiver -> sender    17  corrupted, delivered at 155 ms  NAK seq=0
    155  sender -> receiver   217  delivered at 180 ms  DATA seq=1 len=200   ← moved on
  • Line 130: a garbled NAK, and the sender moves on. DATA 0 is lost.
  • Verdicts: ok · CORRUPTED DATA · LOST DATA · DUPLICATED · STALLED · CRASHED
Reference

What can a packet capture tell us?

your Sender simulated channel corrupts, never loses or reorders your Receiver run-sent.pcap before the channel: every packet as it was sent run-received.pcap after the channel: as it arrived, flipped bits included

one capture shows

which packets, in what order, when

DATA, ACK, NAK, and their sequence numbers

a resend, if the numbers show it

both captures show

whether a packet arrived, and how long it took

whether its bytes changed on the way

whether your ACK reached the sender intact

no capture shows

whether the receiver called deliver_data

whether the application got every byte, or a byte twice

what the sender or receiver decided inside

QuestionThe same DATA packet appears twice in a capture. Did the application receive it twice?
No. A receiver that recognises the resend discards it without delivering; one that does not delivers it again. The capture is identical either way. Captures show what happened on the wire, not what happened inside the application.
Submitting

Submitting

$ python3 check.py
  …
What you hand in from test.py  [10 points]
  ok    answers.md Q0: the rdt1.0 LOST DATA line is there   2/2
  ok    rdt2-flaw.txt: the output of test.py rdt2 corrupt-replies   2/2
  ok    answers.md Q1: written (36 words)   3/3
  FAIL  answers.md Q2: 4 words — write at least 15   0/3
  --    answers.md Q3: written   (read by hand)

Score: 22 / 25 — the same checks run when you push submit/N

Hand in

rdt2.py · rdt21.py · rdt2-flaw.txt · answers.md

Given 2 · Build 10 · Break 8 · Fix 5 = 25. Q3 is read by hand.

Due

Friday 16 October, 11:59 pm. Office hours Thursday.

Three submissions

submit/1, submit/2, submit/3. The last one counts.

git add . && git commit -m "Lab 2" && git push
git tag submit/1 && git push origin submit/1      # the tag is what submits it
← → move · F full screen