personal commit

This commit is contained in:
whatever 2024-11-20 23:00:16 +08:00
parent 0b94a6a45f
commit 079dcbaa31
5 changed files with 88 additions and 9 deletions

View File

@ -61,7 +61,7 @@ endif
# riscv64-unknown-elf- or riscv64-linux-gnu-
# perhaps in /opt/riscv/bin
#TOOLPREFIX =
TOOLPREFIX = /opt/riscv/bin/riscv64-unknown-elf-
# Try to infer the correct TOOLPREFIX if not set
ifndef TOOLPREFIX

View File

@ -238,7 +238,7 @@ def make(*target):
post_make()
def show_command(cmd):
from pipes import quote
from shlex import quote
print("\n$", " ".join(map(quote, cmd)))
def maybe_unlink(*paths):

View File

@ -101,9 +101,22 @@ e1000_transmit(char *buf, int len)
// the TX descriptor ring so that the e1000 sends it. Stash
// a pointer so that it can be freed after send completes.
//
acquire(&e1000_lock);
struct tx_desc *cur=&tx_ring[regs[E1000_TDT]];
if(cur->status!=E1000_TXD_STAT_DD)
goto err;
if(cur->addr)
kfree((void*)cur->addr);
cur->length=len;
cur->addr=(uint64)buf;
cur->cmd=E1000_TXD_CMD_EOP|E1000_TXD_CMD_RS;
cur->status=0;
regs[E1000_TDT]=(regs[E1000_TDT]+1)%TX_RING_SIZE;
release(&e1000_lock);
return 0;
err:
release(&e1000_lock);
return -1;
}
static void
@ -115,7 +128,17 @@ e1000_recv(void)
// Check for packets that have arrived from the e1000
// Create and deliver a buf for each packet (using net_rx()).
//
while(1){
const uint32 id=(regs[E1000_RDT]+1)%RX_RING_SIZE;
struct rx_desc *cur=&rx_ring[id];
if((cur->status&E1000_RXD_STAT_DD)==0)
break;
net_rx((char*)cur->addr,cur->length);
cur->status=0;
rx_bufs[id]=kalloc();
cur->addr=(uint64)rx_bufs[id];
regs[E1000_RDT]=id;
}
}
void

View File

@ -24,7 +24,11 @@ netinit(void)
{
initlock(&netlock, "netlock");
}
#define MAXPORT 65536
#define MAXCNT 16
void *udpq[MAXPORT][MAXCNT];
int udpc[MAXPORT];
int pused[MAXPORT];
//
// bind(int port)
@ -37,7 +41,9 @@ sys_bind(void)
//
// Your code here.
//
short port;
argint(0, (int *)&port);
pused[port] = 1;
return -1;
}
@ -77,6 +83,35 @@ sys_recv(void)
//
// Your code here.
//
acquire(&netlock);
struct proc *p = myproc();
short dport;
uint64 src,sport,buf;
int maxlen;
argint(0, (int*)&dport);
argaddr(1, &src);
argaddr(2,&sport);
argaddr(3,&buf);
argint(4, &maxlen);
if(!udpc[dport])
sleep(&udpc[dport], &netlock);
struct eth *eth=(struct eth *)udpq[dport][udpc[dport]];
struct ip *ip=(struct ip *)(eth+1);
struct udp *udp=(struct udp *)(ip+1);
*(int *)src=ntohl(ip->ip_src);
*(int *)sport=ntohs(udp->sport);
char *payload=(char *)(udp+1);
const int len=udp->ulen-sizeof(struct udp);
if(len>maxlen)
goto err;
if(copyout(p->pagetable,buf,payload,maxlen)<0)
goto err;
kfree(eth);
--udpc[dport];
release(&netlock);
return len;
err:
release(&netlock);
return -1;
}
@ -179,6 +214,8 @@ sys_send(void)
return 0;
}
void
ip_rx(char *buf, int len)
{
@ -187,11 +224,22 @@ ip_rx(char *buf, int len)
if(seen_ip == 0)
printf("ip_rx: received an IP packet\n");
seen_ip = 1;
//
// Your code here.
//
if(len<sizeof(struct eth)+sizeof(struct ip)+sizeof(struct udp))
return;
struct eth *eth=(struct eth *)buf;
struct ip *ip=(struct ip *)(eth+1);
struct udp *udp=(struct udp *)(ip+1);
const short dport=ntohs(udp->dport);
if(!pused[dport])
return;
if(udpc[dport]==MAXCNT)
return;
udpq[dport][dport]=buf;
++udpc[dport];
wakeup(&udpc[dport]);
}
//

View File

@ -0,0 +1,8 @@
{
"folders": [
{
"path": "."
}
],
"settings": {}
}