forked from woai3c/MIT6.828
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserv.c
356 lines (299 loc) · 7.6 KB
/
serv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
* Network server main loop -
* serves IPC requests from other environments.
*/
#include <inc/x86.h>
#include <inc/string.h>
#include <inc/env.h>
#include <inc/ns.h>
#include <inc/lib.h>
#include <arch/perror.h>
#include <arch/thread.h>
#include <lwip/sockets.h>
#include <lwip/netif.h>
#include <lwip/stats.h>
#include <lwip/sys.h>
#include <lwip/tcp.h>
#include <lwip/udp.h>
#include <lwip/dhcp.h>
#include <lwip/tcpip.h>
#include <lwip/stats.h>
#include <lwip/netbuf.h>
#include <netif/etharp.h>
#include <jif/jif.h>
#include "ns.h"
/* errno to make lwIP happy */
int errno;
struct netif nif;
#define debug 0
struct timer_thread {
uint32_t msec;
void (*func)(void);
const char *name;
};
static struct timer_thread t_arp;
static struct timer_thread t_tcpf;
static struct timer_thread t_tcps;
static envid_t timer_envid;
static envid_t input_envid;
static envid_t output_envid;
static bool buse[QUEUE_SIZE];
static int next_i(int i) { return (i+1) % QUEUE_SIZE; }
static int prev_i(int i) { return (i ? i-1 : QUEUE_SIZE-1); }
static void *
get_buffer(void) {
void *va;
int i;
for (i = 0; i < QUEUE_SIZE; i++)
if (!buse[i]) break;
if (i == QUEUE_SIZE) {
panic("NS: buffer overflow");
return 0;
}
va = (void *)(REQVA + i * PGSIZE);
buse[i] = 1;
return va;
}
static void
put_buffer(void *va) {
int i = ((uint32_t)va - REQVA) / PGSIZE;
buse[i] = 0;
}
static void
lwip_init(struct netif *nif, void *if_state,
uint32_t init_addr, uint32_t init_mask, uint32_t init_gw)
{
struct ip_addr ipaddr, netmask, gateway;
ipaddr.addr = init_addr;
netmask.addr = init_mask;
gateway.addr = init_gw;
if (0 == netif_add(nif, &ipaddr, &netmask, &gateway,
if_state,
jif_init,
ip_input))
panic("lwip_init: error in netif_add\n");
netif_set_default(nif);
netif_set_up(nif);
}
static void __attribute__((noreturn))
net_timer(uint32_t arg)
{
struct timer_thread *t = (struct timer_thread *) arg;
for (;;) {
uint32_t cur = sys_time_msec();
lwip_core_lock();
t->func();
lwip_core_unlock();
thread_wait(0, 0, cur + t->msec);
}
}
static void
start_timer(struct timer_thread *t, void (*func)(void), const char *name, int msec)
{
t->msec = msec;
t->func = func;
t->name = name;
int r = thread_create(0, name, &net_timer, (uint32_t)t);
if (r < 0)
panic("cannot create timer thread: %s", e2s(r));
}
static void
tcpip_init_done(void *arg)
{
uint32_t *done = arg;
*done = 1;
thread_wakeup(done);
}
void
serve_init(uint32_t ipaddr, uint32_t netmask, uint32_t gw)
{
int r;
lwip_core_lock();
uint32_t done = 0;
tcpip_init(&tcpip_init_done, &done);
lwip_core_unlock();
thread_wait(&done, 0, (uint32_t)~0);
lwip_core_lock();
lwip_init(&nif, &output_envid, ipaddr, netmask, gw);
start_timer(&t_arp, ðarp_tmr, "arp timer", ARP_TMR_INTERVAL);
start_timer(&t_tcpf, &tcp_fasttmr, "tcp f timer", TCP_FAST_INTERVAL);
start_timer(&t_tcps, &tcp_slowtmr, "tcp s timer", TCP_SLOW_INTERVAL);
struct in_addr ia = {ipaddr};
cprintf("ns: %02x:%02x:%02x:%02x:%02x:%02x"
" bound to static IP %s\n",
nif.hwaddr[0], nif.hwaddr[1], nif.hwaddr[2],
nif.hwaddr[3], nif.hwaddr[4], nif.hwaddr[5],
inet_ntoa(ia));
lwip_core_unlock();
cprintf("NS: TCP/IP initialized.\n");
}
static void
process_timer(envid_t envid) {
uint32_t start, now, to;
if (envid != timer_envid) {
cprintf("NS: received timer interrupt from envid %x not timer env\n", envid);
return;
}
start = sys_time_msec();
thread_yield();
now = sys_time_msec();
to = TIMER_INTERVAL - (now - start);
ipc_send(envid, to, 0, 0);
}
struct st_args {
int32_t reqno;
uint32_t whom;
union Nsipc *req;
};
static void
serve_thread(uint32_t a) {
struct st_args *args = (struct st_args *)a;
union Nsipc *req = args->req;
int r;
switch (args->reqno) {
case NSREQ_ACCEPT:
{
struct Nsret_accept ret;
ret.ret_addrlen = req->accept.req_addrlen;
r = lwip_accept(req->accept.req_s, &ret.ret_addr,
&ret.ret_addrlen);
memmove(req, &ret, sizeof ret);
break;
}
case NSREQ_BIND:
r = lwip_bind(req->bind.req_s, &req->bind.req_name,
req->bind.req_namelen);
break;
case NSREQ_SHUTDOWN:
r = lwip_shutdown(req->shutdown.req_s, req->shutdown.req_how);
break;
case NSREQ_CLOSE:
r = lwip_close(req->close.req_s);
break;
case NSREQ_CONNECT:
r = lwip_connect(req->connect.req_s, &req->connect.req_name,
req->connect.req_namelen);
break;
case NSREQ_LISTEN:
r = lwip_listen(req->listen.req_s, req->listen.req_backlog);
break;
case NSREQ_RECV:
// Note that we read the request fields before we
// overwrite it with the response data.
r = lwip_recv(req->recv.req_s, req->recvRet.ret_buf,
req->recv.req_len, req->recv.req_flags);
break;
case NSREQ_SEND:
r = lwip_send(req->send.req_s, &req->send.req_buf,
req->send.req_size, req->send.req_flags);
break;
case NSREQ_SOCKET:
r = lwip_socket(req->socket.req_domain, req->socket.req_type,
req->socket.req_protocol);
break;
case NSREQ_INPUT:
jif_input(&nif, (void *)&req->pkt);
r = 0;
break;
default:
cprintf("Invalid request code %d from %08x\n", args->whom, args->req);
r = -E_INVAL;
break;
}
if (r == -1) {
char buf[100];
snprintf(buf, sizeof buf, "ns req type %d", args->reqno);
perror(buf);
}
if (args->reqno != NSREQ_INPUT)
ipc_send(args->whom, r, 0, 0);
put_buffer(args->req);
sys_page_unmap(0, (void*) args->req);
free(args);
}
void
serve(void) {
int32_t reqno;
uint32_t whom;
int i, perm;
void *va;
while (1) {
// ipc_recv will block the entire process, so we flush
// all pending work from other threads. We limit the
// number of yields in case there's a rogue thread.
for (i = 0; thread_wakeups_pending() && i < 32; ++i)
thread_yield();
perm = 0;
va = get_buffer();
reqno = ipc_recv((int32_t *) &whom, (void *) va, &perm);
if (debug) {
cprintf("ns req %d from %08x\n", reqno, whom);
}
// first take care of requests that do not contain an argument page
if (reqno == NSREQ_TIMER) {
process_timer(whom);
put_buffer(va);
continue;
}
// All remaining requests must contain an argument page
if (!(perm & PTE_P)) {
cprintf("Invalid request from %08x: no argument page\n", whom);
continue; // just leave it hanging...
}
// Since some lwIP socket calls will block, create a thread and
// process the rest of the request in the thread.
struct st_args *args = malloc(sizeof(struct st_args));
if (!args)
panic("could not allocate thread args structure");
args->reqno = reqno;
args->whom = whom;
args->req = va;
thread_create(0, "serve_thread", serve_thread, (uint32_t)args);
thread_yield(); // let the thread created run
}
}
static void
tmain(uint32_t arg) {
serve_init(inet_addr(IP),
inet_addr(MASK),
inet_addr(DEFAULT));
serve();
}
void
umain(int argc, char **argv)
{
envid_t ns_envid = sys_getenvid();
binaryname = "ns";
// fork off the timer thread which will send us periodic messages
timer_envid = fork();
if (timer_envid < 0)
panic("error forking");
else if (timer_envid == 0) {
timer(ns_envid, TIMER_INTERVAL);
return;
}
// fork off the input thread which will poll the NIC driver for input
// packets
input_envid = fork();
if (input_envid < 0)
panic("error forking");
else if (input_envid == 0) {
input(ns_envid);
return;
}
// fork off the output thread that will send the packets to the NIC
// driver
output_envid = fork();
if (output_envid < 0)
panic("error forking");
else if (output_envid == 0) {
output(ns_envid);
return;
}
// lwIP requires a user threading library; start the library and jump
// into a thread to continue initialization.
thread_init();
thread_create(0, "main", tmain, 0);
thread_yield();
// never coming here!
}