networking
code
it would be better to have some knowledge about principles of Computer Composition, especially about PCI & NIC. qemu -machine virt maps PCIe config space into memory address space at 0x30000000L, then config e1000 registers.
int
e1000_transmit(struct mbuf *m)
{
//
// Your code here.
//
// the mbuf contains an ethernet frame; program it into
// the TX descriptor ring so that the e1000 sends it. Stash
// a pointer so that it can be freed after sending.
//
acquire(&e1000_lock);
int index = regs[E1000_TDT];
if((tx_ring[index].status & E1000_TXD_STAT_DD) == 0){
release(&e1000_lock);
return -1;
}
if(tx_mbufs[index])
mbuffree(tx_mbufs[index]);
tx_ring[index].addr = (uint64)m->head;
tx_ring[index].length = m->len;
tx_ring[index].cmd = E1000_TXD_CMD_RS | E1000_TXD_CMD_EOP;
tx_mbufs[index] = m;
regs[E1000_TDT] = (regs[E1000_TDT] + 1) % TX_RING_SIZE;
release(&e1000_lock);
return 0;
}
static void
e1000_recv(void)
{
//
// Your code here.
//
// Check for packets that have arrived from the e1000
// Create and deliver an mbuf for each packet (using net_rx()).
//
while(1){
int index = regs[E1000_RDT];
index = (index + 1) % RX_RING_SIZE;
if((rx_ring[index].status & E1000_TXD_STAT_DD) == 0)
return;
rx_mbufs[index]->len = rx_ring[index].length;
net_rx(rx_mbufs[index]);
rx_mbufs[index] = mbufalloc(0);
rx_ring[index].addr = (uint64)rx_mbufs[index]->head;
rx_ring[index].status = 0;
regs[E1000_RDT] = index;
}
}
conclusion
all labs finished now, this class is very good to make you have a strongly understanding about the system kernel. bye!
Please find complete code here github