ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

7. Lab: networking

2022-04-05 10:02:04  阅读:225  来源: 互联网

标签:networking tx rx Lab tail EOP E1000 e1000


https://pdos.csail.mit.edu/6.S081/2021/labs/net.html

1. 要求

lab 要求简单来说就是实现网卡驱动的 transmitrecv 功能。其实只要跟着 lab 的 hints 做就可以了,难度较低。

2. 实现

首先是 transmit 功能,这里比较麻烦的是确定 tx_desc.cmd 的值,查阅下文档即可。文档中标注:

VLE, IFCS, and IC are qualified by EOP. That is, hardware interprets these bits ONLY when
EOP is set.
Hardware only sets the DD bit for descriptors with RS set.

因此设置 DD bit 和 EOP bit 即可。其实头文件中关于 cmd 也只定义了这 2 个 bit。

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);
  uint32 tail = regs[E1000_TDT];
  struct tx_desc* txdesc = &tx_ring[tail];
  if ((txdesc->status & E1000_TXD_STAT_DD) == 0){
    release(&e1000_lock);
    return -1;    // the E1000 hasn't finished the corresponding previous transmission request, so return an error.
  }
  
  if (tx_mbufs[tail])
    mbuffree(tx_mbufs[tail]);

  tx_mbufs[tail] = m;
  txdesc->addr = (uint64)m->head;
  txdesc->length = m->len;
  // VLE, IFCS, and IC are qualified by EOP. That is, hardware interprets these bits ONLY when
  // EOP is set.
  // Hardware only sets the DD bit for descriptors with RS set.
  txdesc->cmd = (E1000_TXD_CMD_EOP | E1000_TXD_CMD_RS);
  regs[E1000_TDT] = (tail + 1) % TX_RING_SIZE;
  release(&e1000_lock);
  return 0;
}

接着实现 recv 功能,这里需要注意的是,recv 在一次中断可能收到多个 packet。因此我们需要从 tail 处开始,依次遍历,检查 DD bit 是否为 1,为 1 表示该描述符可以进行 net_rx 操作。
其次需要注意要在 net_rx 前释放锁,否则会引起 panic。

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()).
  //
  uint32 tail = (regs[E1000_RDT] + 1) % RX_RING_SIZE;
  struct rx_desc* rxdesc = &rx_ring[tail];

  while(rxdesc->status & E1000_RXD_STAT_DD){
    acquire(&e1000_lock);

    struct mbuf* buf = rx_mbufs[tail];
    mbufput(buf, rxdesc->length);

    struct mbuf* m = mbufalloc(0);
    rxdesc->addr = (uint64)m->head; 
    rxdesc->status = 0;
    rx_mbufs[tail] = m;
    regs[E1000_RDT] = tail;
    release(&e1000_lock);

    net_rx(buf);
    tail = (tail + 1) % RX_RING_SIZE;
    rxdesc = &rx_ring[tail];
  }
}

3. 小结

该 lab 实现的思路都在 hint 中,关于 recv 和 transmit 的 ring 数组,其结构图大致如下:
image.png
image.png

标签:networking,tx,rx,Lab,tail,EOP,E1000,e1000
来源: https://www.cnblogs.com/lawliet12/p/16101522.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有