XINU
net.c
Go to the documentation of this file.
1 /* net.c - net_init, netin, eth_hton */
2 
3 #include <xinu.h>
4 #include <stdio.h>
5 
6 struct network NetData;
9 
10 /*------------------------------------------------------------------------
11  * net_init - Initialize network data structures and processes
12  *------------------------------------------------------------------------
13  */
14 
15 void net_init (void)
16 {
17  int32 nbufs; /* Total no of buffers */
18 
19  /* Initialize the network data structure */
20 
21  memset((char *)&NetData, NULLCH, sizeof(struct network));
22 
23  /* Obtain the Ethernet MAC address */
24 
25  control(ETHER0, ETH_CTRL_GET_MAC, (int32)NetData.ethucast, 0);
26 
27  memset((char *)NetData.ethbcast, 0xFF, ETH_ADDR_LEN);
28 
29  /* Initialize the random port seed */
30 
32 
33  /* Create the network buffer pool */
34 
35  nbufs = UDP_SLOTS * UDP_QSIZ + ICMP_SLOTS * ICMP_QSIZ + 1;
36 
37  netbufpool = mkbufpool(PACKLEN, nbufs);
38 
39  /* Initialize the ARP cache */
40 
41  arp_init();
42 
43  /* Initialize UDP */
44 
45  udp_init();
46 
47  /* Initialize ICMP */
48 
49  icmp_init();
50 
51  /* Initialize the IP output queue */
52 
53  ipoqueue.iqhead = 0;
54  ipoqueue.iqtail = 0;
56  if((int32)ipoqueue.iqsem == SYSERR) {
57  panic("Cannot create ip output queue semaphore");
58  return;
59  }
60 
61  /* Create the IP output process */
62 
63  resume(create(ipout, NETSTK, NETPRIO, "ipout", 0, NULL));
64 
65  /* Create a network input process */
66 
67  resume(create(netin, NETSTK, NETPRIO, "netin", 0, NULL));
68 }
69 
70 
71 /*------------------------------------------------------------------------
72  * netin - Repeatedly read and process the next incoming packet
73  *------------------------------------------------------------------------
74  */
75 
77 {
78  struct netpacket *pkt; /* Ptr to current packet */
79  int32 retval; /* Return value from read */
80 
81  /* Do forever: read a packet from the network and process */
82 
83  while(1) {
84 
85  /* Allocate a buffer */
86 
87  pkt = (struct netpacket *)getbuf(netbufpool);
88 
89  /* Obtain next packet that arrives */
90 
91  retval = read(ETHER0, (char *)pkt, PACKLEN);
92  if(retval == SYSERR) {
93  panic("Cannot read from Ethernet\n");
94  }
95 
96  /* Convert Ethernet Type to host order */
97 
98  eth_ntoh(pkt);
99 
100  /* Demultiplex on Ethernet type */
101 
102  switch (pkt->net_ethtype) {
103 
104  case ETH_ARP: /* Handle ARP */
105  arp_in((struct arppacket *)pkt);
106  continue;
107 
108  case ETH_IP: /* Handle IP */
109  ip_in(pkt);
110  continue;
111 
112  case ETH_IPv6: /* Handle IPv6 */
113  freebuf((char *)pkt);
114  continue;
115 
116  default: /* Ignore all other incoming packets */
117  freebuf((char *)pkt);
118  continue;
119  }
120  }
121 }
122 
123 /*------------------------------------------------------------------------
124  * eth_hton - Convert Ethernet type field to network byte order
125  *------------------------------------------------------------------------
126  */
127 void eth_hton(
128  struct netpacket *pktptr
129  )
130 {
131  pktptr->net_ethtype = htons(pktptr->net_ethtype);
132 }
133 
134 
135 /*------------------------------------------------------------------------
136  * eth_ntoh - Convert Ethernet type field to host byte order
137  *------------------------------------------------------------------------
138  */
139 void eth_ntoh(
140  struct netpacket *pktptr
141  )
142 {
143  pktptr->net_ethtype = ntohs(pktptr->net_ethtype);
144 }
145 
146 /*------------------------------------------------------------------------
147  * getport - Retrieve a random port number
148  *------------------------------------------------------------------------
149  */
151 {
152  netportseed = 1103515245 * netportseed + 12345;
153  return 50000 + ((uint16)((netportseed >> 16)) % 15535);
154 }
#define NULL
連結リスト用のNULLポインタ
Definition: kernel.h:68
void eth_hton(struct netpacket *pktptr)
Definition: net.c:127
void arp_in(struct arppacket *)
Definition: arp.c:158
void ip_in(struct netpacket *)
Definition: ip.c:13
uint16 getport()
Definition: net.c:150
uint16 net_ethtype
Definition: net.h:19
sid32 semcreate(int32)
未使用セマフォを割り当て、そのセマフォへのインデックス(セマフォID)を返す。
Definition: semcreate.c:22
sid32 iqsem
パケット(pkts)をカウントするセマフォ
Definition: ip.h:37
#define NETSTK
Definition: net.h:3
全てのシステムヘッダファイルをインクルードする。
#define SYSERR
処理が失敗した場合
Definition: kernel.h:79
process netin()
Definition: net.c:76
syscall read(did32, char *, uint32)
Definition: read.c:9
#define ICMP_SLOTS
Definition: icmp.h:3
#define ETH_IPv6
Definition: net.h:11
#define UDP_SLOTS
Definition: udp.h:3
void icmp_init(void)
Definition: icmp.c:12
bpid32 mkbufpool(int32, int32)
バッファプール用のメモリを割り当て、バッファ同士をリンクする。
Definition: mkbufpool.c:38
syscall control(did32, int32, int32, int32)
Definition: control.c:9
int32 iqhead
次に送信するパケットのインデックス
Definition: ip.h:33
void udp_init(void)
Definition: udp.c:12
Definition: net.h:54
#define ETH_CTRL_GET_MAC
Definition: ether.h:45
#define ICMP_QSIZ
Definition: icmp.h:4
int32 bpid32
バッファプールID
Definition: kernel.h:34
void eth_ntoh(struct netpacket *pktptr)
Definition: net.c:139
uint32 getticks(void)
CPUリセット以降のclock tick数を取得する。
Definition: getticks.c:14
struct iqentry ipoqueue
ネットワーク送信キュー
Definition: ip.c:6
#define UDP_QSIZ
Definition: udp.h:4
#define PACKLEN
Definition: net.h:50
IP&Ethernet用のARPパケットフォーマット
Definition: arp.h:39
void net_init(void)
Definition: net.c:15
struct network NetData
Definition: net.c:6
#define ETH_ARP
Definition: net.h:9
int int32
符号あり32ビット整数(int)
Definition: kernel.h:11
unsigned short uint16
符号なし16ビット整数(unsigned short)
Definition: kernel.h:17
void * memset(void *, const int, int32)
指定のByteブロックに対して、同じ値をNバイト分書き込む。
Definition: memset.c:13
pri16 resume(pid32)
プロセスを休止状態(サスペンド)からREADY状態に遷移させる。
Definition: resume.c:20
#define NETPRIO
Definition: net.h:4
#define ntohs(x)
Definition: prototypes.h:622
char * getbuf(bpid32)
事前に確保されたバッファプールからバッファを取得する。
Definition: getbuf.c:19
pid32 create(void *, uint32, pri16, char *, uint32,...)
関数の実行を開始するプロセスを作成する。
Definition: create.c:55
Definition: net.h:16
void panic(char *)
Panic状態に陥った旨のメッセージを表示し、全てのプロセスを停止させる。
Definition: panic.c:12
uint32 netportseed
Definition: net.c:8
#define ETH_IP
Definition: net.h:10
syscall freebuf(char *)
バッファプールから取得したバッファを解放する。
Definition: freebuf.c:19
#define ETH_ADDR_LEN
Definition: ether.h:10
int32 iqtail
次の空きスロットのインデックス。
Definition: ip.h:35
void arp_init(void)
Definition: arp.c:11
unsigned int uint32
符号なし32ビット整数(unsigned int)
Definition: kernel.h:15
#define htons(x)
Definition: prototypes.h:619
#define ETHER0
Definition: conf.h:34
#define NULLCH
NULL文字(NULL終端)
Definition: kernel.h:70
bpid32 netbufpool
Definition: net.c:7
int32 process
プロセスの最上位レベル関数 返り値の型
Definition: kernel.h:53
process ipout(void)
Definition: ip.c:356