net done
This commit is contained in:
parent
079dcbaa31
commit
0d0a728f73
@ -237,3 +237,4 @@ void netinit(void);
|
|||||||
void net_rx(char *buf, int len);
|
void net_rx(char *buf, int len);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
#define DEBUG_INFO printf("File: %s, Line: %d\n", __FILE__, __LINE__)
|
||||||
|
|||||||
61
kernel/net.c
61
kernel/net.c
@ -24,11 +24,11 @@ netinit(void)
|
|||||||
{
|
{
|
||||||
initlock(&netlock, "netlock");
|
initlock(&netlock, "netlock");
|
||||||
}
|
}
|
||||||
#define MAXPORT 65536
|
#define MAXPORT 10000+1
|
||||||
#define MAXCNT 16
|
#define MAXCNT 16
|
||||||
void *udpq[MAXPORT][MAXCNT];
|
static void *udpq[MAXPORT][MAXCNT];
|
||||||
int udpc[MAXPORT];
|
static char udph[MAXPORT],udpt[MAXPORT],udpc[MAXPORT];
|
||||||
int pused[MAXPORT];
|
static int pused[MAXPORT];
|
||||||
|
|
||||||
//
|
//
|
||||||
// bind(int port)
|
// bind(int port)
|
||||||
@ -85,29 +85,44 @@ sys_recv(void)
|
|||||||
//
|
//
|
||||||
acquire(&netlock);
|
acquire(&netlock);
|
||||||
struct proc *p = myproc();
|
struct proc *p = myproc();
|
||||||
short dport;
|
int dport;
|
||||||
uint64 src,sport,buf;
|
uint64 src,sport,buf;
|
||||||
int maxlen;
|
int maxlen;
|
||||||
argint(0, (int*)&dport);
|
argint(0, (int*)&dport);
|
||||||
argaddr(1, &src);
|
argaddr(1, &src);
|
||||||
argaddr(2,&sport);
|
argaddr(2, &sport);
|
||||||
argaddr(3,&buf);
|
argaddr(3, &buf);
|
||||||
argint(4, &maxlen);
|
argint(4, &maxlen);
|
||||||
if(!udpc[dport])
|
while(!udpc[dport]){
|
||||||
|
if(p->killed)
|
||||||
|
goto err;
|
||||||
sleep(&udpc[dport], &netlock);
|
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 ip *ip=(struct ip *)(eth+1);
|
||||||
struct udp *udp=(struct udp *)(ip+1);
|
struct udp *udp=(struct udp *)(ip+1);
|
||||||
*(int *)src=ntohl(ip->ip_src);
|
const int srcbuf=ntohl(ip->ip_src);
|
||||||
*(int *)sport=ntohs(udp->sport);
|
const short sportbuf=ntohs(udp->sport);
|
||||||
char *payload=(char *)(udp+1);
|
if(copyout(p->pagetable,src,(char *)&srcbuf,sizeof(int))<0){
|
||||||
const int len=udp->ulen-sizeof(struct udp);
|
|
||||||
if(len>maxlen)
|
|
||||||
goto err;
|
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);
|
kfree(eth);
|
||||||
--udpc[dport];
|
--udpc[dport];
|
||||||
|
udph[dport]=(udph[dport]+1)%MAXCNT;
|
||||||
release(&netlock);
|
release(&netlock);
|
||||||
return len;
|
return len;
|
||||||
err:
|
err:
|
||||||
@ -153,6 +168,7 @@ in_cksum(const unsigned char *addr, int len)
|
|||||||
//
|
//
|
||||||
// send(int sport, int dst, int dport, char *buf, int len)
|
// send(int sport, int dst, int dport, char *buf, int len)
|
||||||
//
|
//
|
||||||
|
|
||||||
uint64
|
uint64
|
||||||
sys_send(void)
|
sys_send(void)
|
||||||
{
|
{
|
||||||
@ -168,11 +184,9 @@ sys_send(void)
|
|||||||
argint(2, &dport);
|
argint(2, &dport);
|
||||||
argaddr(3, &bufaddr);
|
argaddr(3, &bufaddr);
|
||||||
argint(4, &len);
|
argint(4, &len);
|
||||||
|
|
||||||
int total = len + sizeof(struct eth) + sizeof(struct ip) + sizeof(struct udp);
|
int total = len + sizeof(struct eth) + sizeof(struct ip) + sizeof(struct udp);
|
||||||
if(total > PGSIZE)
|
if(total > PGSIZE)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
char *buf = kalloc();
|
char *buf = kalloc();
|
||||||
if(buf == 0){
|
if(buf == 0){
|
||||||
printf("sys_send: kalloc failed\n");
|
printf("sys_send: kalloc failed\n");
|
||||||
@ -210,7 +224,6 @@ sys_send(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
e1000_transmit(buf, total);
|
e1000_transmit(buf, total);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -234,12 +247,16 @@ ip_rx(char *buf, int len)
|
|||||||
struct udp *udp=(struct udp *)(ip+1);
|
struct udp *udp=(struct udp *)(ip+1);
|
||||||
const short dport=ntohs(udp->dport);
|
const short dport=ntohs(udp->dport);
|
||||||
if(!pused[dport])
|
if(!pused[dport])
|
||||||
return;
|
goto abandon;
|
||||||
if(udpc[dport]==MAXCNT)
|
if(udpc[dport]==MAXCNT)
|
||||||
return;
|
goto abandon;
|
||||||
udpq[dport][dport]=buf;
|
udpq[dport][(int)udpt[dport]]=buf;//tail
|
||||||
|
udpt[dport]=(udpt[dport]+1)%MAXCNT;
|
||||||
++udpc[dport];
|
++udpc[dport];
|
||||||
wakeup(&udpc[dport]);
|
wakeup(&udpc[dport]);
|
||||||
|
return;
|
||||||
|
abandon:
|
||||||
|
kfree(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|||||||
@ -150,6 +150,7 @@ kerneltrap()
|
|||||||
if((which_dev = devintr()) == 0){
|
if((which_dev = devintr()) == 0){
|
||||||
// interrupt or trap from an unknown source
|
// interrupt or trap from an unknown source
|
||||||
printf("scause=0x%lx sepc=0x%lx stval=0x%lx\n", scause, r_sepc(), r_stval());
|
printf("scause=0x%lx sepc=0x%lx stval=0x%lx\n", scause, r_sepc(), r_stval());
|
||||||
|
printf("pid=%d\n",myproc()->pid);
|
||||||
panic("kerneltrap");
|
panic("kerneltrap");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -7,6 +7,7 @@
|
|||||||
#include "kernel/net.h"
|
#include "kernel/net.h"
|
||||||
#include "kernel/stat.h"
|
#include "kernel/stat.h"
|
||||||
#include "user/user.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).
|
// send a single UDP packet (but don't recv() the reply).
|
||||||
@ -472,7 +473,6 @@ ping3()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
sleep(1);
|
sleep(1);
|
||||||
|
|
||||||
//
|
//
|
||||||
// send so many packets from 2008 and 2010 that some of the
|
// send so many packets from 2008 and 2010 that some of the
|
||||||
// replies must be dropped due to the requirement
|
// replies must be dropped due to the requirement
|
||||||
@ -493,7 +493,6 @@ ping3()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
sleep(1);
|
sleep(1);
|
||||||
|
|
||||||
//
|
//
|
||||||
// send another packet from 2009.
|
// send another packet from 2009.
|
||||||
//
|
//
|
||||||
@ -510,7 +509,6 @@ ping3()
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// did both reply packets for 2009 arrive?
|
// did both reply packets for 2009 arrive?
|
||||||
//
|
//
|
||||||
@ -524,7 +522,6 @@ ping3()
|
|||||||
fprintf(2, "ping3: recv() failed\n");
|
fprintf(2, "ping3: recv() failed\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(src != 0x0A000202){ // 10.0.2.2
|
if(src != 0x0A000202){ // 10.0.2.2
|
||||||
printf("ping3: wrong ip src %x\n", src);
|
printf("ping3: wrong ip src %x\n", src);
|
||||||
return 0;
|
return 0;
|
||||||
@ -539,7 +536,6 @@ ping3()
|
|||||||
printf("ping3: wrong length %d\n", cc);
|
printf("ping3: wrong length %d\n", cc);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// printf("port=%d ii=%d: %c%c%c\n", port, ii, ibuf[0], ibuf[1], ibuf[2]);
|
// printf("port=%d ii=%d: %c%c%c\n", port, ii, ibuf[0], ibuf[1], ibuf[2]);
|
||||||
|
|
||||||
char buf[4];
|
char buf[4];
|
||||||
@ -581,7 +577,6 @@ ping3()
|
|||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
close(fds[1]);
|
close(fds[1]);
|
||||||
|
|
||||||
sleep(5);
|
sleep(5);
|
||||||
static char nbuf[512];
|
static char nbuf[512];
|
||||||
int n = read(fds[0], nbuf, sizeof(nbuf));
|
int n = read(fds[0], nbuf, sizeof(nbuf));
|
||||||
@ -595,7 +590,6 @@ ping3()
|
|||||||
}
|
}
|
||||||
|
|
||||||
printf("ping3: OK\n");
|
printf("ping3: OK\n");
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user