cs144-sp23, Lab Checkpoint 6: putting it all together

记录 cs144 Spring-23 Lab6: putting it all together 的思路与实践难点。

1. 内容简介

Lab6 是一个选择性的实验, 在前 6 个实验中我们已经完成了网络的框架, Checkpoint 0 (a reliable byte stream), Checkpoints 1–3 (the Transmission Control Protocol), Checkpoint 4 (an IP/Ethernet network interface) 以及 Checkpoint 5 (an IP router), 该实验的目的是使用前述的网络栈实现真实的网络通信, 这需要我们有个 partner, 一人分饰两角也是可以的。

2. The Network

在该实验中需要一对可行的网络栈组合成一个真实的网络通信环境, 两方都需要构建 host 和 router, 网络拓扑如下图所示:

The Network
The Network
  1. 对于个人而言, 需要使用同一套代码在不同的终端中启用 server 以及 client。
  2. 为了使用 relay, 我们需要选择任意的一个 1024 到 64000 之间的偶数以区分不同的 group。 我们可以选择 “4088”。
  3. server 端在 build 文件夹下输入 ./apps/endtoend server cs144.keithw.org 4088, 若顺利的话 server 端会打印如下信息:

     cs144@cs144-ubuntu22:~/minnow/build$ ./apps/endtoend server cs144.keithw.org 4088
     DEBUG: Network interface has Ethernet address 02:00:00:91:a1:8f and IP address 172.16.0.1
     DEBUG: Network interface has Ethernet address 02:00:00:bf:ba:0e and IP address 10.0.0.172
     DEBUG: adding route 172.16.0.0/12 => (direct) on interface 0
     DEBUG: adding route 10.0.0.0/8 => (direct) on interface 1
     DEBUG: adding route 192.168.0.0/16 => 10.0.0.192 on interface 1
     DEBUG: Network interface has Ethernet address 9a:c0:ea:b5:88:82 and IP address 172.16.0.100
     DEBUG: Listening for incoming connection...
    
  4. client 端在 build 文件夹下输入 ./apps/endtoend client cs144.keithw.org 4089, 若顺利的话 client 端会打印如下信息:

     cs144@cs144-ubuntu22:~/minnow/build$ ./apps/endtoend client cs144.keithw.org 4089
     DEBUG: Network interface has Ethernet address 02:00:00:e0:84:14 and IP address 192.168.0.1
     DEBUG: Network interface has Ethernet address 02:00:00:e8:1a:da and IP address 10.0.0.192
     DEBUG: adding route 192.168.0.0/16 => (direct) on interface 0
     DEBUG: adding route 10.0.0.0/8 => (direct) on interface 1
     DEBUG: adding route 172.16.0.0/12 => 10.0.0.172 on interface 1
     DEBUG: Network interface has Ethernet address 7e:fa:65:55:c7:4e and IP address 192.168.0.50
     DEBUG: Connecting from 192.168.0.50:7907...
     DEBUG: Connecting to 172.16.0.100:1234...
     Successfully connected to 172.16.0.100:1234.
    

    并且 server 端会额外打印一条信息:

     New connection from 192.168.0.50:7907.
    
  5. 若以上信息都能正确打印, 则意味着 TCP 握手成功, 若有问题则需要在之前的命令后加上 debug 进入 debug 模式进行调试。

    • 我们可以在 server 或者 client 任意一端输入数据, 就能在相应的另一端看到数据。

      经过测试发现需要在 Terminal 输入需要发送的数据后按下 Ctrl-D 才能发送数据并在 remote 端看到。

    • 可以使用 Ctrl-D 关闭链接。 server 或 client 关闭链接后会终止那一端的 ByteStream 的输出, 但仍会持续接收数据直到另一端也关闭了 ByteStream 的输出, 我们需要验证这点。

      首先需要确保 Terminal 中已经没有数据了, 此时我们按下 Ctrl-D 才会断开 TCP 的链接。

      • server

          # press Ctrl-D at server
          DEBUG: Outbound stream to 192.168.0.50:36742 finished (0 seqnos still in flight)
          # press Ctrl-D at client
          DEBUG: Inbound stream from 192.168.0.50:36742 finished cleanly.
          DEBUG: Waiting for clean shutdown...
          DEBUG: Outbound stream to 192.168.0.50:36742 has been fully acknowledged.
          DEBUG: TCP connection finished cleanly.
          done.
          Exiting... done.
        
      • client

          # press Ctrl-D at server
          ######## Nothing ########
          # press Ctrl-D at client
          DEBUG: Outbound stream to 172.16.0.100:1234 finished (0 seqnos still in flight).
          DEBUG: Inbound stream from 172.16.0.100:1234 finished cleanly.
          DEBUG: Waiting for clean shutdown...
          DEBUG: Outbound stream to 172.16.0.100:1234 has been fully acknowledged.
          DEBUG: TCP connection finished cleanly.
          done.
          Exiting... done.
        
    • 当 server 和 client 都关闭 ByteStream 输出后并且一端又持续存在数秒后, 两边的程序应该会正常退出。

3. 文件传输

3.1 操作流程

当基本的通信功能实现之后就可以通过网络进行文件的传输。

  • 将 1M 大小的随机数写入文件 /tmp/big.txt

      dd if=/dev/urandom bs=1M count=1 of=/tmp/big.txt
    
  • 让 server 端在接受连接后立刻发送文件。

      ./apps/endtoend server cs144.keithw.org 4088 < /tmp/big.txt
    
  • 让 client 端关闭输出数据流并下载文件。

      </dev/null ./apps/endtoend client cs144.keithw.org 4089 > /tmp/big-received.txt
    
  • 比较两个文件确保二者一致

      sha256sum /tmp/big.txt
      sha256sum /tmp/big-received.txt
    

如果 SHA-256 哈希值都能匹配上, 那我们就可以确定文件被正确传输了。

3.2 输出打印

  • server

      DEBUG: Inbound stream from 192.168.0.50:27200 finished cleanly.
      DEBUG: Waiting for clean shutdown...
      DEBUG: Outbound stream to 192.168.0.50:27200 finished (63000 seqnos still in flight).
      DEBUG: Outbound stream to 192.168.0.50:27200 has been fully acknowledged.
      DEBUG: TCP connection finished cleanly.
      done.
      Exiting... done.
    
  • client

      DEBUG: Outbound stream to 172.16.0.100:1234 finished (0 seqnos still in flight).
      DEBUG: Outbound stream to 172.16.0.100:1234 has been fully acknowledged.
      DEBUG: Inbound stream from 172.16.0.100:1234 finished cleanly.
      DEBUG: TCP connection finished cleanly.
      DEBUG: Waiting for clean shutdown... done.
      Exiting... done.
    
  • SHA-256

      e0462e19b156c6a02c9f89f4069b624b4eefd1a4837c5d71397db3253d469ce1  /tmp/big.txt
      e0462e19b156c6a02c9f89f4069b624b4eefd1a4837c5d71397db3253d469ce1  /tmp/big-received.txt