This commit is contained in:
whatever 2024-11-23 13:35:55 +08:00
parent 079dcbaa31
commit 0d0a728f73
5 changed files with 43 additions and 29 deletions

View File

@ -237,3 +237,4 @@ void netinit(void);
void net_rx(char *buf, int len);
#endif
#define DEBUG_INFO printf("File: %s, Line: %d\n", __FILE__, __LINE__)

View File

@ -24,11 +24,11 @@ netinit(void)
{
initlock(&netlock, "netlock");
}
#define MAXPORT 65536
#define MAXPORT 10000+1
#define MAXCNT 16
void *udpq[MAXPORT][MAXCNT];
int udpc[MAXPORT];
int pused[MAXPORT];
static void *udpq[MAXPORT][MAXCNT];
static char udph[MAXPORT],udpt[MAXPORT],udpc[MAXPORT];
static int pused[MAXPORT];
//
// bind(int port)
@ -85,29 +85,44 @@ sys_recv(void)
//
acquire(&netlock);
struct proc *p = myproc();
short dport;
int dport;
uint64 src,sport,buf;
int maxlen;
argint(0, (int*)&dport);
argaddr(1, &src);
argaddr(2,&sport);
argaddr(3,&buf);
argaddr(2, &sport);
argaddr(3, &buf);
argint(4, &maxlen);
if(!udpc[dport])
while(!udpc[dport]){
if(p->killed)
goto err;
sleep(&udpc[dport], &netlock);
struct eth *eth=(struct eth *)udpq[dport][udpc[dport]];
if(p->killed)
goto err;
}
struct eth *eth=(struct eth *)udpq[dport][(int)udph[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)
const int srcbuf=ntohl(ip->ip_src);
const short sportbuf=ntohs(udp->sport);
if(copyout(p->pagetable,src,(char *)&srcbuf,sizeof(int))<0){
goto err;
if(copyout(p->pagetable,buf,payload,maxlen)<0)
goto err;
}
if(copyout(p->pagetable,sport,(char *)&sportbuf,sizeof(short))<0){
goto err;
}
char *payload=(char *)(udp+1);
const int len=ntohs(udp->ulen)-sizeof(struct udp);
if(len>maxlen){
goto err;
}
if(copyout(p->pagetable,buf,payload,len)<0){
goto err;
}
kfree(eth);
--udpc[dport];
udph[dport]=(udph[dport]+1)%MAXCNT;
release(&netlock);
return len;
err:
@ -153,6 +168,7 @@ in_cksum(const unsigned char *addr, int len)
//
// send(int sport, int dst, int dport, char *buf, int len)
//
uint64
sys_send(void)
{
@ -168,11 +184,9 @@ sys_send(void)
argint(2, &dport);
argaddr(3, &bufaddr);
argint(4, &len);
int total = len + sizeof(struct eth) + sizeof(struct ip) + sizeof(struct udp);
if(total > PGSIZE)
return -1;
char *buf = kalloc();
if(buf == 0){
printf("sys_send: kalloc failed\n");
@ -210,7 +224,6 @@ sys_send(void)
}
e1000_transmit(buf, total);
return 0;
}
@ -234,12 +247,16 @@ ip_rx(char *buf, int len)
struct udp *udp=(struct udp *)(ip+1);
const short dport=ntohs(udp->dport);
if(!pused[dport])
return;
goto abandon;
if(udpc[dport]==MAXCNT)
return;
udpq[dport][dport]=buf;
goto abandon;
udpq[dport][(int)udpt[dport]]=buf;//tail
udpt[dport]=(udpt[dport]+1)%MAXCNT;
++udpc[dport];
wakeup(&udpc[dport]);
return;
abandon:
kfree(buf);
}
//

View File

@ -150,6 +150,7 @@ kerneltrap()
if((which_dev = devintr()) == 0){
// interrupt or trap from an unknown source
printf("scause=0x%lx sepc=0x%lx stval=0x%lx\n", scause, r_sepc(), r_stval());
printf("pid=%d\n",myproc()->pid);
panic("kerneltrap");
}

1
time.txt Normal file
View File

@ -0,0 +1 @@
5

View File

@ -7,6 +7,7 @@
#include "kernel/net.h"
#include "kernel/stat.h"
#include "user/user.h"
#define DEBUG_INFO printf("File: %s, Line: %d\n", __FILE__, __LINE__)
//
// send a single UDP packet (but don't recv() the reply).
@ -472,7 +473,6 @@ ping3()
}
}
sleep(1);
//
// send so many packets from 2008 and 2010 that some of the
// replies must be dropped due to the requirement
@ -493,7 +493,6 @@ ping3()
}
}
sleep(1);
//
// send another packet from 2009.
//
@ -510,7 +509,6 @@ ping3()
return 0;
}
}
//
// did both reply packets for 2009 arrive?
//
@ -524,7 +522,6 @@ ping3()
fprintf(2, "ping3: recv() failed\n");
return 0;
}
if(src != 0x0A000202){ // 10.0.2.2
printf("ping3: wrong ip src %x\n", src);
return 0;
@ -539,7 +536,6 @@ ping3()
printf("ping3: wrong length %d\n", cc);
return 0;
}
// printf("port=%d ii=%d: %c%c%c\n", port, ii, ibuf[0], ibuf[1], ibuf[2]);
char buf[4];
@ -581,7 +577,6 @@ ping3()
exit(0);
}
close(fds[1]);
sleep(5);
static char nbuf[512];
int n = read(fds[0], nbuf, sizeof(nbuf));
@ -595,7 +590,6 @@ ping3()
}
printf("ping3: OK\n");
return 1;
}