xv6 Lab6 network driver - MIT 6.1810 Fall 2025 Operating System

Last updated on August 17, 2026 pm

This article is currently an experimental machine translation and may contain errors. If anything is unclear, please refer to original Chinese version. I am continuously working to improve the translation.

Reading

Interrupt: hardware needs to get the OS's attention, so it raises an interrupt, a process similar to a system call. But there are differences:

  • Asynchronous, unrelated to the process currently running on the CPU
  • Parallel, the device runs in parallel with the CPU
  • Requires a driver

Many device drivers execute code in two different contexts:

  • Top half: runs in the kernel thread of some process.
  • Bottom half: executes when an interrupt occurs, i.e., the interrupt handler.

Part One: NIC

The network stack has already prepared a memory buffer holding a complete packet (which stores packets); what we need to do is complete two functions.

Explanations of some unfamiliar terms:

  • NIC Network Interface Card, a network card; the E1000 is one type of NIC

  • DMA Direct Memory Access:

    • Generally, hardware cannot directly manipulate kernel memory
    • But once DMA is configured, the E1000 can directly read and write specified memory, i.e., the buffer
    • This way, the CPU doesn't need to move the data itself
  • Descriptor, mainly tells the network card the packet's location, length, and other information; in the code it is tx_desc

    descriptor
    ├── addr: the memory address of the buffer
    ├── length: the packet length
    ├── status: whether the NIC has finished processing
    └── cmd: what operation the NIC is asked to perform
    • TX descriptor: describes "the packet to be sent".
    • RX descriptor: describes "an empty buffer used to receive packets".
  • Descriptor Ring: since the number of descriptors is limited, it is a ring; in the assignment it is 0~15

  • Control register: the driver notifies the E1000 of relevant operations through control registers; specifically, this is regs[]

  • TDT Transmit Descriptor Tail, which can be understood as "which descriptor in the TX ring the driver should fill in next"

  • RDT Receive Descriptor Tail: "the last RX descriptor that the driver has finished processing and handed back to the E1000"

  • DD Descriptor Done, a flag bit in the descriptor's status field

    • For TX, DD means the E1000 has finished sending the packet corresponding to this descriptor and will no longer read its buffer
    • For RX, DD means the E1000 has received a packet and written it completely into the buffer pointed to by this descriptor
  • EOP End Of Packet, the end of a packet

  • RS Report Status

  • net_rx() the entry point for the xv6 network stack to receive packets

First, complete e1000_transmit():

代码C · 26 行
int
e1000_transmit(char *buf, int len)
{
  acquire(&e1000_lock);

  uint32 index = regs[E1000_TDT];
  struct tx_desc *desc = &tx_ring[index];

  if((desc->status & E1000_TXD_STAT_DD) == 0){
    release(&e1000_lock);
    return -1;
  }

  if(desc->addr != 0)
    kfree((void *)desc->addr);

  desc->addr = (uint64)buf;
  desc->length = len;
  desc->cmd = E1000_TXD_CMD_EOP | E1000_TXD_CMD_RS;
  desc->status = 0;

  regs[E1000_TDT] = (index + 1) % TX_RING_SIZE;

  release(&e1000_lock);
  return 0;
}

Following the concepts and hints above, it can be completed.

Then e1000_recv(), which can likewise be completed the same way.

Part Two: UDP Receive

In this part, the spec is given in more detail. Just read it as is; you don't even need to read other manuals.

Follow the hints step by step:

1. Create the data structures

Create a struct to keep track of bound ports and the packets in their queues.

Define two:

  • udp_packet represents a packet in a queue
  • udp_port represents a bound port, and also records the next port, forming a linked list, which can be used to check whether a port is allocated

Use a port object pool to allocate bound ports:

#define UDP_PORTS_PER_PAGE 12

struct udp_port_page {
  struct udp_port ports[UDP_PORTS_PER_PAGE];
  int used;
  struct udp_port_page *next;
};

Implementation of sys_bind(): check the incoming port number, then check whether it is already bound; if not, allocate it, then add it to the ports linked list.

Implementation of ip_rx(): check the received packet, then find the corresponding port.

Implementation of sys_recv(): just follow the hint.

Note: there is a test that tries to obtain the DNS of the MIT pdos website; with some special VPN settings enabled it seems to fail, so I tried turning off the VPN / system proxy / TUN one after another to test... The final result was: turn on the system proxy and turn off TUN.

Remember that after changing the proxy, you also need to restart WSL for it to take effect.

All tests passed

xv6 Lab6 network driver - MIT 6.1810 Fall 2025 Operating System
https://blog.wendain.ing/en/posts/xv6-lab6-network-driver/
Author
wendaining
Posted on
August 10, 2026
Updated on
August 17, 2026
Licensed under