1 | /* |
---|
2 | * $Id$ |
---|
3 | */ |
---|
4 | |
---|
5 | #define RTEMS_FAST_MUTEX |
---|
6 | |
---|
7 | #ifdef RTEMS_FAST_MUTEX |
---|
8 | #define __RTEMS_VIOLATE_KERNEL_VISIBILITY__ 1 |
---|
9 | #endif |
---|
10 | |
---|
11 | #include <string.h> |
---|
12 | #include <stdarg.h> |
---|
13 | #include <stdio.h> |
---|
14 | #include <errno.h> |
---|
15 | |
---|
16 | #include <rtems.h> |
---|
17 | #include <rtems/libio.h> |
---|
18 | #include <rtems/error.h> |
---|
19 | #include <rtems/rtems_bsdnet.h> |
---|
20 | #include <sys/types.h> |
---|
21 | #include <sys/param.h> |
---|
22 | #include <sys/domain.h> |
---|
23 | #include <sys/mbuf.h> |
---|
24 | #include <sys/socketvar.h> |
---|
25 | #include <sys/socket.h> |
---|
26 | #include <sys/sockio.h> |
---|
27 | #include <sys/callout.h> |
---|
28 | #include <sys/proc.h> |
---|
29 | #include <sys/ioctl.h> |
---|
30 | #include <net/if.h> |
---|
31 | #include <net/route.h> |
---|
32 | #include <netinet/in.h> |
---|
33 | #include <vm/vm.h> |
---|
34 | #include <arpa/inet.h> |
---|
35 | |
---|
36 | #include <net/netisr.h> |
---|
37 | #include <net/route.h> |
---|
38 | |
---|
39 | /* |
---|
40 | * Memory allocation |
---|
41 | */ |
---|
42 | static int nmbuf = (64 * 1024) / MSIZE; |
---|
43 | int nmbclusters = (128 * 1024) / MCLBYTES; |
---|
44 | |
---|
45 | /* |
---|
46 | * Socket buffering parameters |
---|
47 | */ |
---|
48 | unsigned long sb_efficiency = 8; |
---|
49 | |
---|
50 | /* |
---|
51 | * Network task synchronization |
---|
52 | */ |
---|
53 | static rtems_id networkSemaphore; |
---|
54 | #ifdef RTEMS_FAST_MUTEX |
---|
55 | Semaphore_Control *the_networkSemaphore; |
---|
56 | #endif |
---|
57 | static rtems_id networkDaemonTid; |
---|
58 | static rtems_unsigned32 networkDaemonPriority; |
---|
59 | static void networkDaemon (void *task_argument); |
---|
60 | |
---|
61 | /* |
---|
62 | * Network timing |
---|
63 | */ |
---|
64 | int rtems_bsdnet_ticks_per_second; |
---|
65 | int rtems_bsdnet_microseconds_per_tick; |
---|
66 | |
---|
67 | /* |
---|
68 | * Callout processing |
---|
69 | */ |
---|
70 | static rtems_interval ticksWhenCalloutsLastChecked; |
---|
71 | static struct callout *callfree, calltodo; |
---|
72 | |
---|
73 | /* |
---|
74 | * FreeBSD variables |
---|
75 | */ |
---|
76 | int nfs_diskless_valid; |
---|
77 | |
---|
78 | /* |
---|
79 | * BOOTP values |
---|
80 | */ |
---|
81 | struct in_addr rtems_bsdnet_log_host_address; |
---|
82 | struct in_addr rtems_bsdnet_bootp_server_address; |
---|
83 | char *rtems_bsdnet_bootp_boot_file_name; |
---|
84 | char *rtems_bsdnet_bootp_server_name; |
---|
85 | char *rtems_bsdnet_domain_name; |
---|
86 | struct in_addr rtems_bsdnet_nameserver[sizeof rtems_bsdnet_config.name_server / |
---|
87 | sizeof rtems_bsdnet_config.name_server[0]]; |
---|
88 | int rtems_bsdnet_nameserver_count; |
---|
89 | struct in_addr rtems_bsdnet_ntpserver[sizeof rtems_bsdnet_config.ntp_server / |
---|
90 | sizeof rtems_bsdnet_config.ntp_server[0]]; |
---|
91 | int rtems_bsdnet_ntpserver_count; |
---|
92 | long rtems_bsdnet_timeoffset; |
---|
93 | |
---|
94 | /* |
---|
95 | * Perform FreeBSD memory allocation. |
---|
96 | * FIXME: This should be modified to keep memory allocation statistics. |
---|
97 | */ |
---|
98 | #undef malloc |
---|
99 | #undef free |
---|
100 | extern void *malloc (size_t); |
---|
101 | extern void free (void *); |
---|
102 | void * |
---|
103 | rtems_bsdnet_malloc (unsigned long size, int type, int flags) |
---|
104 | { |
---|
105 | void *p; |
---|
106 | int try = 0; |
---|
107 | |
---|
108 | for (;;) { |
---|
109 | p = malloc (size); |
---|
110 | if (p || (flags & M_NOWAIT)) |
---|
111 | return p; |
---|
112 | rtems_bsdnet_semaphore_release (); |
---|
113 | if (++try >= 30) { |
---|
114 | printf ("rtems_bsdnet_malloc still waiting.\n"); |
---|
115 | try = 0; |
---|
116 | } |
---|
117 | rtems_task_wake_after (rtems_bsdnet_ticks_per_second); |
---|
118 | rtems_bsdnet_semaphore_obtain (); |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | /* |
---|
123 | * Free FreeBSD memory |
---|
124 | * FIXME: This should be modified to keep memory allocation statistics. |
---|
125 | */ |
---|
126 | void |
---|
127 | rtems_bsdnet_free (void *addr, int type) |
---|
128 | { |
---|
129 | free (addr); |
---|
130 | } |
---|
131 | |
---|
132 | /* |
---|
133 | * Do the initializations required by the BSD code |
---|
134 | */ |
---|
135 | static int |
---|
136 | bsd_init (void) |
---|
137 | { |
---|
138 | int i; |
---|
139 | char *p; |
---|
140 | |
---|
141 | /* |
---|
142 | * Set up mbuf cluster data strutures |
---|
143 | */ |
---|
144 | p = malloc ((nmbclusters*MCLBYTES)+MCLBYTES-1); |
---|
145 | if (p == NULL) { |
---|
146 | printf ("Can't get network cluster memory.\n"); |
---|
147 | return -1; |
---|
148 | } |
---|
149 | p = (char *)(((unsigned long)p + (MCLBYTES-1)) & ~(MCLBYTES-1)); |
---|
150 | mbutl = (struct mbuf *)p; |
---|
151 | for (i = 0; i < nmbclusters; i++) { |
---|
152 | ((union mcluster *)p)->mcl_next = mclfree; |
---|
153 | mclfree = (union mcluster *)p; |
---|
154 | p += MCLBYTES; |
---|
155 | mbstat.m_clfree++; |
---|
156 | } |
---|
157 | mbstat.m_clusters = nmbclusters; |
---|
158 | mclrefcnt = malloc (nmbclusters); |
---|
159 | if (mclrefcnt == NULL) { |
---|
160 | printf ("Can't get mbuf cluster reference counts memory.\n"); |
---|
161 | return -1; |
---|
162 | } |
---|
163 | memset (mclrefcnt, '\0', nmbclusters); |
---|
164 | |
---|
165 | /* |
---|
166 | * Set up mbuf data structures |
---|
167 | */ |
---|
168 | |
---|
169 | p = malloc(nmbuf * MSIZE + MSIZE - 1); |
---|
170 | p = (char *)(((unsigned int)p + MSIZE - 1) & ~(MSIZE - 1)); |
---|
171 | if (p == NULL) { |
---|
172 | printf ("Can't get network memory.\n"); |
---|
173 | return -1; |
---|
174 | } |
---|
175 | for (i = 0; i < nmbuf; i++) { |
---|
176 | ((struct mbuf *)p)->m_next = mmbfree; |
---|
177 | mmbfree = (struct mbuf *)p; |
---|
178 | p += MSIZE; |
---|
179 | } |
---|
180 | mbstat.m_mbufs = nmbuf; |
---|
181 | mbstat.m_mtypes[MT_FREE] = nmbuf; |
---|
182 | |
---|
183 | /* |
---|
184 | * Set up domains |
---|
185 | */ |
---|
186 | { |
---|
187 | extern struct domain routedomain; |
---|
188 | extern struct domain inetdomain; |
---|
189 | |
---|
190 | routedomain.dom_next = domains; |
---|
191 | domains = &routedomain; |
---|
192 | inetdomain.dom_next = domains; |
---|
193 | domains = &inetdomain; |
---|
194 | domaininit (NULL); |
---|
195 | } |
---|
196 | |
---|
197 | /* |
---|
198 | * Set up interfaces |
---|
199 | */ |
---|
200 | ifinit (NULL); |
---|
201 | return 0; |
---|
202 | } |
---|
203 | |
---|
204 | /* |
---|
205 | * Initialize and start network operations |
---|
206 | */ |
---|
207 | static int |
---|
208 | rtems_bsdnet_initialize (void) |
---|
209 | { |
---|
210 | rtems_status_code sc; |
---|
211 | |
---|
212 | /* |
---|
213 | * Set the priority of all network tasks |
---|
214 | */ |
---|
215 | if (rtems_bsdnet_config.network_task_priority == 0) |
---|
216 | networkDaemonPriority = 100; |
---|
217 | else |
---|
218 | networkDaemonPriority = rtems_bsdnet_config.network_task_priority; |
---|
219 | |
---|
220 | /* |
---|
221 | * Set the memory allocation limits |
---|
222 | */ |
---|
223 | if (rtems_bsdnet_config.mbuf_bytecount) |
---|
224 | nmbuf = rtems_bsdnet_config.mbuf_bytecount / MSIZE; |
---|
225 | if (rtems_bsdnet_config.mbuf_cluster_bytecount) |
---|
226 | nmbclusters = rtems_bsdnet_config.mbuf_cluster_bytecount / MCLBYTES; |
---|
227 | |
---|
228 | /* |
---|
229 | * Create the task-synchronization semaphore |
---|
230 | */ |
---|
231 | sc = rtems_semaphore_create (rtems_build_name('B', 'S', 'D', 'n'), |
---|
232 | 0, |
---|
233 | RTEMS_PRIORITY | |
---|
234 | RTEMS_BINARY_SEMAPHORE | |
---|
235 | RTEMS_INHERIT_PRIORITY | |
---|
236 | RTEMS_NO_PRIORITY_CEILING | |
---|
237 | RTEMS_LOCAL, |
---|
238 | 0, |
---|
239 | &networkSemaphore); |
---|
240 | if (sc != RTEMS_SUCCESSFUL) { |
---|
241 | printf ("Can't create network seamphore: `%s'\n", rtems_status_text (sc)); |
---|
242 | return -1; |
---|
243 | } |
---|
244 | #ifdef RTEMS_FAST_MUTEX |
---|
245 | { |
---|
246 | Objects_Locations location; |
---|
247 | the_networkSemaphore = _Semaphore_Get( networkSemaphore, &location ); |
---|
248 | _Thread_Enable_dispatch(); |
---|
249 | } |
---|
250 | #endif |
---|
251 | |
---|
252 | /* |
---|
253 | * Compute clock tick conversion factors |
---|
254 | */ |
---|
255 | rtems_clock_get (RTEMS_CLOCK_GET_TICKS_PER_SECOND, &rtems_bsdnet_ticks_per_second); |
---|
256 | if (rtems_bsdnet_ticks_per_second <= 0) |
---|
257 | rtems_bsdnet_ticks_per_second = 1; |
---|
258 | rtems_bsdnet_microseconds_per_tick = 1000000 / rtems_bsdnet_ticks_per_second; |
---|
259 | |
---|
260 | /* |
---|
261 | * Ensure that `seconds' is greater than 0 |
---|
262 | */ |
---|
263 | rtems_task_wake_after (rtems_bsdnet_ticks_per_second); |
---|
264 | |
---|
265 | /* |
---|
266 | * Set up BSD-style sockets |
---|
267 | */ |
---|
268 | if (bsd_init () < 0) |
---|
269 | return -1; |
---|
270 | |
---|
271 | /* |
---|
272 | * Start network daemon |
---|
273 | */ |
---|
274 | networkDaemonTid = rtems_bsdnet_newproc ("ntwk", 4096, networkDaemon, NULL); |
---|
275 | |
---|
276 | /* |
---|
277 | * Let other network tasks begin |
---|
278 | */ |
---|
279 | rtems_bsdnet_semaphore_release (); |
---|
280 | return 0; |
---|
281 | } |
---|
282 | |
---|
283 | /* |
---|
284 | * Obtain network mutex |
---|
285 | */ |
---|
286 | void |
---|
287 | rtems_bsdnet_semaphore_obtain (void) |
---|
288 | { |
---|
289 | #ifdef RTEMS_FAST_MUTEX |
---|
290 | ISR_Level level; |
---|
291 | _ISR_Disable (level); |
---|
292 | _CORE_mutex_Seize ( |
---|
293 | &the_networkSemaphore->Core_control.mutex, |
---|
294 | networkSemaphore, |
---|
295 | 1, /* wait */ |
---|
296 | 0, /* forever */ |
---|
297 | level |
---|
298 | ); |
---|
299 | if (_Thread_Executing->Wait.return_code) |
---|
300 | rtems_panic ("Can't obtain network semaphore\n"); |
---|
301 | #else |
---|
302 | rtems_status_code sc; |
---|
303 | |
---|
304 | sc = rtems_semaphore_obtain (networkSemaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT); |
---|
305 | if (sc != RTEMS_SUCCESSFUL) |
---|
306 | rtems_panic ("Can't obtain network semaphore: `%s'\n", rtems_status_text (sc)); |
---|
307 | #endif |
---|
308 | } |
---|
309 | |
---|
310 | /* |
---|
311 | * Release network mutex |
---|
312 | */ |
---|
313 | void |
---|
314 | rtems_bsdnet_semaphore_release (void) |
---|
315 | { |
---|
316 | #ifdef RTEMS_FAST_MUTEX |
---|
317 | int i; |
---|
318 | |
---|
319 | _Thread_Disable_dispatch(); |
---|
320 | i = _CORE_mutex_Surrender ( |
---|
321 | &the_networkSemaphore->Core_control.mutex, |
---|
322 | networkSemaphore, |
---|
323 | NULL |
---|
324 | ); |
---|
325 | _Thread_Enable_dispatch(); |
---|
326 | if (i) |
---|
327 | rtems_panic ("Can't release network semaphore\n"); |
---|
328 | #else |
---|
329 | rtems_status_code sc; |
---|
330 | |
---|
331 | sc = rtems_semaphore_release (networkSemaphore); |
---|
332 | if (sc != RTEMS_SUCCESSFUL) |
---|
333 | rtems_panic ("Can't release network semaphore: `%s'\n", rtems_status_text (sc)); |
---|
334 | #endif |
---|
335 | } |
---|
336 | |
---|
337 | /* |
---|
338 | * Wait for something to happen to a socket buffer |
---|
339 | */ |
---|
340 | int |
---|
341 | sbwait(sb) |
---|
342 | struct sockbuf *sb; |
---|
343 | { |
---|
344 | rtems_event_set events; |
---|
345 | rtems_id tid; |
---|
346 | rtems_status_code sc; |
---|
347 | |
---|
348 | /* |
---|
349 | * Soak up any pending events. |
---|
350 | * The sleep/wakeup synchronization in the FreeBSD |
---|
351 | * kernel has no memory. |
---|
352 | */ |
---|
353 | rtems_event_receive (SBWAIT_EVENT, RTEMS_EVENT_ANY | RTEMS_NO_WAIT, RTEMS_NO_TIMEOUT, &events); |
---|
354 | |
---|
355 | /* |
---|
356 | * Set this task as the target of the wakeup operation. |
---|
357 | */ |
---|
358 | rtems_task_ident (RTEMS_SELF, 0, &tid); |
---|
359 | sb->sb_sel.si_pid = tid; |
---|
360 | |
---|
361 | /* |
---|
362 | * Show that socket is waiting |
---|
363 | */ |
---|
364 | sb->sb_flags |= SB_WAIT; |
---|
365 | |
---|
366 | /* |
---|
367 | * Release the network semaphore. |
---|
368 | */ |
---|
369 | rtems_bsdnet_semaphore_release (); |
---|
370 | |
---|
371 | /* |
---|
372 | * Wait for the wakeup event. |
---|
373 | */ |
---|
374 | sc = rtems_event_receive (SBWAIT_EVENT, RTEMS_EVENT_ANY | RTEMS_WAIT, sb->sb_timeo, &events); |
---|
375 | |
---|
376 | /* |
---|
377 | * Reobtain the network semaphore. |
---|
378 | */ |
---|
379 | rtems_bsdnet_semaphore_obtain (); |
---|
380 | |
---|
381 | /* |
---|
382 | * Return the status of the wait. |
---|
383 | */ |
---|
384 | switch (sc) { |
---|
385 | case RTEMS_SUCCESSFUL: return 0; |
---|
386 | case RTEMS_TIMEOUT: return EWOULDBLOCK; |
---|
387 | default: return ENXIO; |
---|
388 | } |
---|
389 | } |
---|
390 | |
---|
391 | |
---|
392 | /* |
---|
393 | * Wake up the task waiting on a socket buffer. |
---|
394 | */ |
---|
395 | void |
---|
396 | sowakeup(so, sb) |
---|
397 | register struct socket *so; |
---|
398 | register struct sockbuf *sb; |
---|
399 | { |
---|
400 | if (sb->sb_flags & SB_WAIT) { |
---|
401 | sb->sb_flags &= ~SB_WAIT; |
---|
402 | rtems_event_send (sb->sb_sel.si_pid, SBWAIT_EVENT); |
---|
403 | } |
---|
404 | if (sb->sb_wakeup) { |
---|
405 | (*sb->sb_wakeup) (so, sb->sb_wakeuparg); |
---|
406 | } |
---|
407 | } |
---|
408 | |
---|
409 | /* |
---|
410 | * For now, a socket can be used by only one task at a time. |
---|
411 | */ |
---|
412 | int |
---|
413 | sb_lock(sb) |
---|
414 | register struct sockbuf *sb; |
---|
415 | { |
---|
416 | rtems_panic ("Socket buffer is already in use."); |
---|
417 | return 0; |
---|
418 | } |
---|
419 | void |
---|
420 | wakeup (void *p) |
---|
421 | { |
---|
422 | rtems_panic ("Wakeup called"); |
---|
423 | } |
---|
424 | |
---|
425 | /* |
---|
426 | * Wait for a connection/disconnection event. |
---|
427 | */ |
---|
428 | int |
---|
429 | soconnsleep (struct socket *so) |
---|
430 | { |
---|
431 | rtems_event_set events; |
---|
432 | rtems_id tid; |
---|
433 | rtems_status_code sc; |
---|
434 | |
---|
435 | /* |
---|
436 | * Soak up any pending events. |
---|
437 | * The sleep/wakeup synchronization in the FreeBSD |
---|
438 | * kernel has no memory. |
---|
439 | */ |
---|
440 | rtems_event_receive (SOSLEEP_EVENT, RTEMS_EVENT_ANY | RTEMS_NO_WAIT, RTEMS_NO_TIMEOUT, &events); |
---|
441 | |
---|
442 | /* |
---|
443 | * Set this task as the target of the wakeup operation. |
---|
444 | */ |
---|
445 | if (so->so_pgid) |
---|
446 | rtems_panic ("Another task is already sleeping on that socket"); |
---|
447 | rtems_task_ident (RTEMS_SELF, 0, &tid); |
---|
448 | so->so_pgid = tid; |
---|
449 | |
---|
450 | /* |
---|
451 | * Wait for the wakeup event. |
---|
452 | */ |
---|
453 | sc = rtems_bsdnet_event_receive (SOSLEEP_EVENT, RTEMS_EVENT_ANY | RTEMS_WAIT, so->so_rcv.sb_timeo, &events); |
---|
454 | |
---|
455 | /* |
---|
456 | * Relinquish ownership of the socket. |
---|
457 | */ |
---|
458 | so->so_pgid = 0; |
---|
459 | |
---|
460 | switch (sc) { |
---|
461 | case RTEMS_SUCCESSFUL: return 0; |
---|
462 | case RTEMS_TIMEOUT: return EWOULDBLOCK; |
---|
463 | default: return ENXIO; |
---|
464 | } |
---|
465 | } |
---|
466 | |
---|
467 | /* |
---|
468 | * Wake up a task waiting for a connection/disconnection to complete. |
---|
469 | */ |
---|
470 | void |
---|
471 | soconnwakeup (struct socket *so) |
---|
472 | { |
---|
473 | if (so->so_pgid) |
---|
474 | rtems_event_send (so->so_pgid, SOSLEEP_EVENT); |
---|
475 | } |
---|
476 | |
---|
477 | /* |
---|
478 | * Send an event to the network daemon. |
---|
479 | * This corresponds to sending a software interrupt in the BSD kernel. |
---|
480 | */ |
---|
481 | void |
---|
482 | rtems_bsdnet_schednetisr (int n) |
---|
483 | { |
---|
484 | rtems_event_send (networkDaemonTid, 1 << n); |
---|
485 | } |
---|
486 | |
---|
487 | /* |
---|
488 | * The network daemon |
---|
489 | * This provides a context to run BSD software interrupts |
---|
490 | */ |
---|
491 | static void |
---|
492 | networkDaemon (void *task_argument) |
---|
493 | { |
---|
494 | rtems_event_set events; |
---|
495 | rtems_interval now; |
---|
496 | int ticksPassed; |
---|
497 | unsigned32 timeout; |
---|
498 | struct callout *c; |
---|
499 | |
---|
500 | for (;;) { |
---|
501 | c = calltodo.c_next; |
---|
502 | if (c) |
---|
503 | timeout = c->c_time; |
---|
504 | else |
---|
505 | timeout = RTEMS_NO_TIMEOUT; |
---|
506 | rtems_bsdnet_event_receive (NETISR_EVENTS, |
---|
507 | RTEMS_EVENT_ANY | RTEMS_WAIT, |
---|
508 | timeout, |
---|
509 | &events); |
---|
510 | if (events & NETISR_IP_EVENT) |
---|
511 | ipintr (); |
---|
512 | if (events & NETISR_ARP_EVENT) |
---|
513 | arpintr (); |
---|
514 | rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &now); |
---|
515 | ticksPassed = now - ticksWhenCalloutsLastChecked; |
---|
516 | if (ticksPassed != 0) { |
---|
517 | ticksWhenCalloutsLastChecked = now; |
---|
518 | |
---|
519 | c = calltodo.c_next; |
---|
520 | if (c) { |
---|
521 | c->c_time -= ticksPassed; |
---|
522 | while ((c = calltodo.c_next) != NULL && c->c_time <= 0) { |
---|
523 | void *arg; |
---|
524 | void (*func) (void *); |
---|
525 | |
---|
526 | func = c->c_func; |
---|
527 | arg = c->c_arg; |
---|
528 | calltodo.c_next = c->c_next; |
---|
529 | c->c_next = callfree; |
---|
530 | callfree = c; |
---|
531 | (*func)(arg); |
---|
532 | } |
---|
533 | } |
---|
534 | } |
---|
535 | } |
---|
536 | } |
---|
537 | |
---|
538 | /* |
---|
539 | * Structure passed to task-start stub |
---|
540 | */ |
---|
541 | struct newtask { |
---|
542 | void (*entry)(void *); |
---|
543 | void *arg; |
---|
544 | }; |
---|
545 | |
---|
546 | /* |
---|
547 | * Task-start stub |
---|
548 | */ |
---|
549 | static void |
---|
550 | taskEntry (rtems_task_argument arg) |
---|
551 | { |
---|
552 | struct newtask t; |
---|
553 | |
---|
554 | /* |
---|
555 | * Pick up task information and free |
---|
556 | * the memory allocated to pass the |
---|
557 | * information to this task. |
---|
558 | */ |
---|
559 | t = *(struct newtask *)arg; |
---|
560 | free ((struct newtask *)arg); |
---|
561 | |
---|
562 | /* |
---|
563 | * Enter the competition for the network semaphore |
---|
564 | */ |
---|
565 | rtems_bsdnet_semaphore_obtain (); |
---|
566 | |
---|
567 | /* |
---|
568 | * Enter the task |
---|
569 | */ |
---|
570 | (*t.entry)(t.arg); |
---|
571 | rtems_panic ("Network task returned!\n"); |
---|
572 | } |
---|
573 | |
---|
574 | /* |
---|
575 | * Start a network task |
---|
576 | */ |
---|
577 | rtems_id |
---|
578 | rtems_bsdnet_newproc (char *name, int stacksize, void(*entry)(void *), void *arg) |
---|
579 | { |
---|
580 | struct newtask *t; |
---|
581 | char nm[4]; |
---|
582 | rtems_id tid; |
---|
583 | rtems_status_code sc; |
---|
584 | |
---|
585 | strncpy (nm, name, 4); |
---|
586 | sc = rtems_task_create (rtems_build_name(nm[0], nm[1], nm[2], nm[3]), |
---|
587 | networkDaemonPriority, |
---|
588 | stacksize, |
---|
589 | RTEMS_PREEMPT|RTEMS_NO_TIMESLICE|RTEMS_NO_ASR|RTEMS_INTERRUPT_LEVEL(0), |
---|
590 | RTEMS_NO_FLOATING_POINT|RTEMS_LOCAL, |
---|
591 | &tid); |
---|
592 | if (sc != RTEMS_SUCCESSFUL) |
---|
593 | rtems_panic ("Can't create network daemon `%s': `%s'\n", name, rtems_status_text (sc)); |
---|
594 | |
---|
595 | /* |
---|
596 | * Set up task arguments |
---|
597 | */ |
---|
598 | t = malloc (sizeof *t); |
---|
599 | t->entry = entry; |
---|
600 | t->arg = arg; |
---|
601 | |
---|
602 | /* |
---|
603 | * Start the task |
---|
604 | */ |
---|
605 | sc = rtems_task_start (tid, taskEntry, (rtems_task_argument)t); |
---|
606 | if (sc != RTEMS_SUCCESSFUL) |
---|
607 | rtems_panic ("Can't start network daemon `%s': `%s'\n", name, rtems_status_text (sc)); |
---|
608 | |
---|
609 | /* |
---|
610 | * Let our caller know the i.d. of the new task |
---|
611 | */ |
---|
612 | return tid; |
---|
613 | } |
---|
614 | |
---|
615 | rtems_status_code rtems_bsdnet_event_receive ( |
---|
616 | rtems_event_set event_in, |
---|
617 | rtems_option option_set, |
---|
618 | rtems_interval ticks, |
---|
619 | rtems_event_set *event_out) |
---|
620 | { |
---|
621 | rtems_status_code sc; |
---|
622 | |
---|
623 | rtems_bsdnet_semaphore_release (); |
---|
624 | sc = rtems_event_receive (event_in, option_set, ticks, event_out); |
---|
625 | rtems_bsdnet_semaphore_obtain (); |
---|
626 | return sc; |
---|
627 | } |
---|
628 | |
---|
629 | /* |
---|
630 | * Return time since startup |
---|
631 | */ |
---|
632 | void |
---|
633 | microtime (struct timeval *t) |
---|
634 | { |
---|
635 | rtems_interval now; |
---|
636 | |
---|
637 | rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &now); |
---|
638 | t->tv_sec = now / rtems_bsdnet_ticks_per_second; |
---|
639 | t->tv_usec = (now % rtems_bsdnet_ticks_per_second) * rtems_bsdnet_microseconds_per_tick; |
---|
640 | } |
---|
641 | |
---|
642 | unsigned long |
---|
643 | rtems_bsdnet_seconds_since_boot (void) |
---|
644 | { |
---|
645 | rtems_interval now; |
---|
646 | |
---|
647 | rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &now); |
---|
648 | return now / rtems_bsdnet_ticks_per_second; |
---|
649 | } |
---|
650 | |
---|
651 | /* |
---|
652 | * Fake random number generator |
---|
653 | */ |
---|
654 | unsigned long |
---|
655 | rtems_bsdnet_random (void) |
---|
656 | { |
---|
657 | rtems_interval now; |
---|
658 | |
---|
659 | rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &now); |
---|
660 | return (now * 99991); |
---|
661 | } |
---|
662 | |
---|
663 | /* |
---|
664 | * Callout list processing |
---|
665 | */ |
---|
666 | void |
---|
667 | timeout(void (*ftn)(void *), void *arg, int ticks) |
---|
668 | { |
---|
669 | register struct callout *new, *p, *t; |
---|
670 | |
---|
671 | if (ticks <= 0) |
---|
672 | ticks = 1; |
---|
673 | |
---|
674 | /* Fill in the next free callout structure. */ |
---|
675 | if (callfree == NULL) { |
---|
676 | callfree = malloc (sizeof *callfree); |
---|
677 | if (callfree == NULL) |
---|
678 | rtems_panic ("No memory for timeout table entry"); |
---|
679 | callfree->c_next = NULL; |
---|
680 | } |
---|
681 | |
---|
682 | new = callfree; |
---|
683 | callfree = new->c_next; |
---|
684 | new->c_arg = arg; |
---|
685 | new->c_func = ftn; |
---|
686 | |
---|
687 | /* |
---|
688 | * The time for each event is stored as a difference from the time |
---|
689 | * of the previous event on the queue. Walk the queue, correcting |
---|
690 | * the ticks argument for queue entries passed. Correct the ticks |
---|
691 | * value for the queue entry immediately after the insertion point |
---|
692 | * as well. Watch out for negative c_time values; these represent |
---|
693 | * overdue events. |
---|
694 | */ |
---|
695 | for (p = &calltodo; |
---|
696 | (t = p->c_next) != NULL && ticks > t->c_time; p = t) |
---|
697 | if (t->c_time > 0) |
---|
698 | ticks -= t->c_time; |
---|
699 | new->c_time = ticks; |
---|
700 | if (t != NULL) |
---|
701 | t->c_time -= ticks; |
---|
702 | |
---|
703 | /* Insert the new entry into the queue. */ |
---|
704 | p->c_next = new; |
---|
705 | new->c_next = t; |
---|
706 | } |
---|
707 | |
---|
708 | /* |
---|
709 | * Ticks till specified time |
---|
710 | * XXX: This version worries only about seconds, but that's good |
---|
711 | * enough for the way the network code uses this routine. |
---|
712 | */ |
---|
713 | int |
---|
714 | hzto(struct timeval *tv) |
---|
715 | { |
---|
716 | long diff = tv->tv_sec - rtems_bsdnet_seconds_since_boot(); |
---|
717 | |
---|
718 | if (diff <= 0) |
---|
719 | return 1; |
---|
720 | return diff * rtems_bsdnet_ticks_per_second; |
---|
721 | } |
---|
722 | |
---|
723 | /* |
---|
724 | * Kernel debugging |
---|
725 | */ |
---|
726 | int rtems_bsdnet_log_priority; |
---|
727 | void |
---|
728 | rtems_bsdnet_log (int priority, const char *fmt, ...) |
---|
729 | { |
---|
730 | va_list args; |
---|
731 | |
---|
732 | if (priority & rtems_bsdnet_log_priority) { |
---|
733 | va_start (args, fmt); |
---|
734 | vprintf (fmt, args); |
---|
735 | va_end (args); |
---|
736 | } |
---|
737 | } |
---|
738 | |
---|
739 | /* |
---|
740 | * IP header checksum routine for processors which don't have an inline version |
---|
741 | */ |
---|
742 | u_int |
---|
743 | in_cksum_hdr (const void *ip) |
---|
744 | { |
---|
745 | rtems_unsigned32 sum; |
---|
746 | const rtems_unsigned16 *sp; |
---|
747 | int i; |
---|
748 | |
---|
749 | sum = 0; |
---|
750 | sp = (rtems_unsigned16 *)ip; |
---|
751 | for (i = 0 ; i < 10 ; i++) |
---|
752 | sum += *sp++; |
---|
753 | while (sum > 0xFFFF) |
---|
754 | sum = (sum & 0xffff) + (sum >> 16); |
---|
755 | return ~sum & 0xFFFF; |
---|
756 | } |
---|
757 | |
---|
758 | /* |
---|
759 | * Manipulate routing tables |
---|
760 | */ |
---|
761 | int rtems_bsdnet_rtrequest ( |
---|
762 | int req, |
---|
763 | struct sockaddr *dst, |
---|
764 | struct sockaddr *gateway, |
---|
765 | struct sockaddr *netmask, |
---|
766 | int flags, |
---|
767 | struct rtentry **net_nrt) |
---|
768 | { |
---|
769 | int error; |
---|
770 | |
---|
771 | rtems_bsdnet_semaphore_obtain (); |
---|
772 | error = rtrequest (req, dst, gateway, netmask, flags, net_nrt); |
---|
773 | rtems_bsdnet_semaphore_release (); |
---|
774 | if (error) { |
---|
775 | errno = error; |
---|
776 | return -1; |
---|
777 | } |
---|
778 | return 0; |
---|
779 | } |
---|
780 | |
---|
781 | static int |
---|
782 | rtems_bsdnet_setup (void) |
---|
783 | { |
---|
784 | struct rtems_bsdnet_ifconfig *ifp; |
---|
785 | short flags; |
---|
786 | struct sockaddr_in address; |
---|
787 | struct sockaddr_in netmask; |
---|
788 | struct sockaddr_in broadcast; |
---|
789 | struct sockaddr_in gateway; |
---|
790 | int i; |
---|
791 | extern char *strdup (const char *cp); |
---|
792 | |
---|
793 | /* |
---|
794 | * Set local parameters |
---|
795 | */ |
---|
796 | if (rtems_bsdnet_config.hostname) |
---|
797 | sethostname (rtems_bsdnet_config.hostname, |
---|
798 | strlen (rtems_bsdnet_config.hostname)); |
---|
799 | if (rtems_bsdnet_config.domainname) |
---|
800 | rtems_bsdnet_domain_name = |
---|
801 | strdup (rtems_bsdnet_config.domainname); |
---|
802 | if (rtems_bsdnet_config.log_host) |
---|
803 | rtems_bsdnet_log_host_address.s_addr = |
---|
804 | inet_addr (rtems_bsdnet_config.log_host); |
---|
805 | for (i = 0 ; i < sizeof rtems_bsdnet_config.name_server / |
---|
806 | sizeof rtems_bsdnet_config.name_server[0] ; i++) { |
---|
807 | if (!rtems_bsdnet_config.name_server[i]) |
---|
808 | break; |
---|
809 | rtems_bsdnet_nameserver[rtems_bsdnet_nameserver_count++].s_addr |
---|
810 | = inet_addr (rtems_bsdnet_config.name_server[i]); |
---|
811 | } |
---|
812 | for (i = 0 ; i < sizeof rtems_bsdnet_config.ntp_server / |
---|
813 | sizeof rtems_bsdnet_config.ntp_server[0] ; i++) { |
---|
814 | if (!rtems_bsdnet_config.ntp_server[i]) |
---|
815 | break; |
---|
816 | rtems_bsdnet_ntpserver[rtems_bsdnet_ntpserver_count++].s_addr |
---|
817 | = inet_addr (rtems_bsdnet_config.ntp_server[i]); |
---|
818 | } |
---|
819 | |
---|
820 | /* |
---|
821 | * Configure interfaces |
---|
822 | */ |
---|
823 | for (ifp = rtems_bsdnet_config.ifconfig ; ifp ; ifp = ifp->next) { |
---|
824 | if (ifp->ip_address == NULL) |
---|
825 | continue; |
---|
826 | |
---|
827 | /* |
---|
828 | * Bring interface up |
---|
829 | */ |
---|
830 | flags = IFF_UP; |
---|
831 | if (rtems_bsdnet_ifconfig (ifp->name, SIOCSIFFLAGS, &flags) < 0) { |
---|
832 | printf ("Can't bring %s up: %s\n", ifp->name, strerror (errno)); |
---|
833 | continue; |
---|
834 | } |
---|
835 | |
---|
836 | /* |
---|
837 | * Set interface netmask |
---|
838 | */ |
---|
839 | memset (&netmask, '\0', sizeof netmask); |
---|
840 | netmask.sin_len = sizeof netmask; |
---|
841 | netmask.sin_family = AF_INET; |
---|
842 | netmask.sin_addr.s_addr = inet_addr (ifp->ip_netmask); |
---|
843 | if (rtems_bsdnet_ifconfig (ifp->name, SIOCSIFNETMASK, &netmask) < 0) { |
---|
844 | printf ("Can't set %s netmask: %s\n", ifp->name, strerror (errno)); |
---|
845 | continue; |
---|
846 | } |
---|
847 | |
---|
848 | /* |
---|
849 | * Set interface address |
---|
850 | */ |
---|
851 | memset (&address, '\0', sizeof address); |
---|
852 | address.sin_len = sizeof address; |
---|
853 | address.sin_family = AF_INET; |
---|
854 | address.sin_addr.s_addr = inet_addr (ifp->ip_address); |
---|
855 | if (rtems_bsdnet_ifconfig (ifp->name, SIOCSIFADDR, &address) < 0) { |
---|
856 | printf ("Can't set %s address: %s\n", ifp->name, strerror (errno)); |
---|
857 | continue; |
---|
858 | } |
---|
859 | |
---|
860 | /* |
---|
861 | * Set interface broadcast address if the interface has the |
---|
862 | * broadcast flag set. |
---|
863 | */ |
---|
864 | if (rtems_bsdnet_ifconfig (ifp->name, SIOCGIFFLAGS, &flags) < 0) { |
---|
865 | printf ("Can't read %s flags: %s\n", ifp->name, strerror (errno)); |
---|
866 | continue; |
---|
867 | } |
---|
868 | if (flags & IFF_BROADCAST) { |
---|
869 | memset (&broadcast, '\0', sizeof broadcast); |
---|
870 | broadcast.sin_len = sizeof broadcast; |
---|
871 | broadcast.sin_family = AF_INET; |
---|
872 | broadcast.sin_addr.s_addr = |
---|
873 | address.sin_addr.s_addr | ~netmask.sin_addr.s_addr; |
---|
874 | if (rtems_bsdnet_ifconfig (ifp->name, SIOCSIFBRDADDR, &broadcast) < 0) { |
---|
875 | struct in_addr in_addr; |
---|
876 | in_addr.s_addr = broadcast.sin_addr.s_addr; |
---|
877 | printf ("Can't set %s broadcast address %s: %s\n", |
---|
878 | ifp->name, inet_ntoa (in_addr), strerror (errno)); |
---|
879 | } |
---|
880 | } |
---|
881 | } |
---|
882 | |
---|
883 | /* |
---|
884 | * Set default route |
---|
885 | */ |
---|
886 | if (rtems_bsdnet_config.gateway) { |
---|
887 | address.sin_addr.s_addr = INADDR_ANY; |
---|
888 | netmask.sin_addr.s_addr = INADDR_ANY; |
---|
889 | memset (&gateway, '\0', sizeof gateway); |
---|
890 | gateway.sin_len = sizeof gateway; |
---|
891 | gateway.sin_family = AF_INET; |
---|
892 | gateway.sin_addr.s_addr = inet_addr (rtems_bsdnet_config.gateway); |
---|
893 | if (rtems_bsdnet_rtrequest ( |
---|
894 | RTM_ADD, |
---|
895 | (struct sockaddr *)&address, |
---|
896 | (struct sockaddr *)&gateway, |
---|
897 | (struct sockaddr *)&netmask, |
---|
898 | (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL) < 0) { |
---|
899 | printf ("Can't set default route: %s\n", strerror (errno)); |
---|
900 | return -1; |
---|
901 | } |
---|
902 | } |
---|
903 | return 0; |
---|
904 | } |
---|
905 | |
---|
906 | /* |
---|
907 | * Initialize the network |
---|
908 | */ |
---|
909 | int |
---|
910 | rtems_bsdnet_initialize_network (void) |
---|
911 | { |
---|
912 | struct rtems_bsdnet_ifconfig *ifp; |
---|
913 | |
---|
914 | /* |
---|
915 | * Start network tasks. |
---|
916 | * Initialize BSD network data structures. |
---|
917 | */ |
---|
918 | if (rtems_bsdnet_initialize () < 0) |
---|
919 | return -1; |
---|
920 | |
---|
921 | /* |
---|
922 | * Attach interfaces |
---|
923 | */ |
---|
924 | for (ifp = rtems_bsdnet_config.ifconfig ; ifp ; ifp = ifp->next) { |
---|
925 | rtems_bsdnet_attach (ifp); |
---|
926 | } |
---|
927 | |
---|
928 | /* |
---|
929 | * Bring up the network |
---|
930 | */ |
---|
931 | if (rtems_bsdnet_setup () < 0) |
---|
932 | return -1; |
---|
933 | if (rtems_bsdnet_config.bootp) |
---|
934 | (*rtems_bsdnet_config.bootp)(); |
---|
935 | return 0; |
---|
936 | } |
---|
937 | |
---|
938 | /* |
---|
939 | * Attach a network interface. |
---|
940 | */ |
---|
941 | void rtems_bsdnet_attach (struct rtems_bsdnet_ifconfig *ifp) |
---|
942 | { |
---|
943 | if (ifp) { |
---|
944 | rtems_bsdnet_semaphore_obtain (); |
---|
945 | (ifp->attach)(ifp, 1); |
---|
946 | rtems_bsdnet_semaphore_release (); |
---|
947 | } |
---|
948 | } |
---|
949 | |
---|
950 | /* |
---|
951 | * Detach a network interface. |
---|
952 | */ |
---|
953 | void rtems_bsdnet_detach (struct rtems_bsdnet_ifconfig *ifp) |
---|
954 | { |
---|
955 | if (ifp) { |
---|
956 | rtems_bsdnet_semaphore_obtain (); |
---|
957 | (ifp->attach)(ifp, 0); |
---|
958 | rtems_bsdnet_semaphore_release (); |
---|
959 | } |
---|
960 | } |
---|
961 | |
---|
962 | /* |
---|
963 | * Interface Configuration. |
---|
964 | */ |
---|
965 | int rtems_bsdnet_ifconfig (const char *ifname, unsigned32 cmd, void *param) |
---|
966 | { |
---|
967 | int s, r = 0; |
---|
968 | struct ifreq ifreq; |
---|
969 | |
---|
970 | /* |
---|
971 | * Configure interfaces |
---|
972 | */ |
---|
973 | s = socket (AF_INET, SOCK_DGRAM, 0); |
---|
974 | if (s < 0) |
---|
975 | return -1; |
---|
976 | |
---|
977 | strncpy (ifreq.ifr_name, ifname, IFNAMSIZ); |
---|
978 | |
---|
979 | rtems_bsdnet_semaphore_obtain (); |
---|
980 | |
---|
981 | switch (cmd) { |
---|
982 | case SIOCSIFADDR: |
---|
983 | case SIOCSIFNETMASK: |
---|
984 | memcpy (&ifreq.ifr_addr, param, sizeof (struct sockaddr)); |
---|
985 | r = ioctl (s, cmd, &ifreq); |
---|
986 | break; |
---|
987 | |
---|
988 | case OSIOCGIFADDR: |
---|
989 | case SIOCGIFADDR: |
---|
990 | case OSIOCGIFNETMASK: |
---|
991 | case SIOCGIFNETMASK: |
---|
992 | if ((r = ioctl (s, cmd, &ifreq)) < 0) |
---|
993 | break; |
---|
994 | memcpy (param, &ifreq.ifr_addr, sizeof (struct sockaddr)); |
---|
995 | break; |
---|
996 | |
---|
997 | case SIOCGIFFLAGS: |
---|
998 | case SIOCSIFFLAGS: |
---|
999 | if ((r = ioctl (s, SIOCGIFFLAGS, &ifreq)) < 0) |
---|
1000 | break; |
---|
1001 | if (cmd == SIOCGIFFLAGS) { |
---|
1002 | *((short*) param) = ifreq.ifr_flags; |
---|
1003 | break; |
---|
1004 | } |
---|
1005 | ifreq.ifr_flags |= *((short*) param); |
---|
1006 | r = ioctl (s, SIOCSIFFLAGS, &ifreq); |
---|
1007 | break; |
---|
1008 | |
---|
1009 | case SIOCSIFDSTADDR: |
---|
1010 | memcpy (&ifreq.ifr_dstaddr, param, sizeof (struct sockaddr)); |
---|
1011 | r = ioctl (s, cmd, &ifreq); |
---|
1012 | break; |
---|
1013 | |
---|
1014 | case OSIOCGIFDSTADDR: |
---|
1015 | case SIOCGIFDSTADDR: |
---|
1016 | if ((r = ioctl (s, cmd, &ifreq)) < 0) |
---|
1017 | break; |
---|
1018 | memcpy (param, &ifreq.ifr_dstaddr, sizeof (struct sockaddr)); |
---|
1019 | break; |
---|
1020 | |
---|
1021 | case SIOCSIFBRDADDR: |
---|
1022 | memcpy (&ifreq.ifr_broadaddr, param, sizeof (struct sockaddr)); |
---|
1023 | r = ioctl (s, cmd, &ifreq); |
---|
1024 | break; |
---|
1025 | |
---|
1026 | case OSIOCGIFBRDADDR: |
---|
1027 | case SIOCGIFBRDADDR: |
---|
1028 | if ((r = ioctl (s, cmd, &ifreq)) < 0) |
---|
1029 | break; |
---|
1030 | memcpy (param, &ifreq.ifr_broadaddr, sizeof (struct sockaddr)); |
---|
1031 | break; |
---|
1032 | |
---|
1033 | case SIOCSIFMETRIC: |
---|
1034 | ifreq.ifr_metric = *((int*) param); |
---|
1035 | r = ioctl (s, cmd, &ifreq); |
---|
1036 | break; |
---|
1037 | |
---|
1038 | case SIOCGIFMETRIC: |
---|
1039 | if ((r = ioctl (s, cmd, &ifreq)) < 0) |
---|
1040 | break; |
---|
1041 | *((int*) param) = ifreq.ifr_metric; |
---|
1042 | break; |
---|
1043 | |
---|
1044 | case SIOCSIFMTU: |
---|
1045 | ifreq.ifr_mtu = *((int*) param); |
---|
1046 | r = ioctl (s, cmd, &ifreq); |
---|
1047 | break; |
---|
1048 | |
---|
1049 | case SIOCGIFMTU: |
---|
1050 | if ((r = ioctl (s, cmd, &ifreq)) < 0) |
---|
1051 | break; |
---|
1052 | *((int*) param) = ifreq.ifr_mtu; |
---|
1053 | break; |
---|
1054 | |
---|
1055 | case SIOCSIFPHYS: |
---|
1056 | ifreq.ifr_phys = *((int*) param); |
---|
1057 | r = ioctl (s, cmd, &ifreq); |
---|
1058 | break; |
---|
1059 | |
---|
1060 | case SIOCGIFPHYS: |
---|
1061 | if ((r = ioctl (s, cmd, &ifreq)) < 0) |
---|
1062 | break; |
---|
1063 | *((int*) param) = ifreq.ifr_phys; |
---|
1064 | break; |
---|
1065 | |
---|
1066 | case SIOCSIFMEDIA: |
---|
1067 | ifreq.ifr_media = *((int*) param); |
---|
1068 | r = ioctl (s, cmd, &ifreq); |
---|
1069 | break; |
---|
1070 | |
---|
1071 | case SIOCGIFMEDIA: |
---|
1072 | if ((r = ioctl (s, cmd, &ifreq)) < 0) |
---|
1073 | break; |
---|
1074 | *((int*) param) = ifreq.ifr_media; |
---|
1075 | break; |
---|
1076 | |
---|
1077 | default: |
---|
1078 | errno = EOPNOTSUPP; |
---|
1079 | r = -1; |
---|
1080 | break; |
---|
1081 | } |
---|
1082 | |
---|
1083 | rtems_bsdnet_semaphore_release (); |
---|
1084 | |
---|
1085 | close (s); |
---|
1086 | return r; |
---|
1087 | } |
---|
1088 | |
---|
1089 | /* |
---|
1090 | * Parse a network driver name into a name and a unit number |
---|
1091 | */ |
---|
1092 | int |
---|
1093 | rtems_bsdnet_parse_driver_name (const struct rtems_bsdnet_ifconfig *config, char **namep) |
---|
1094 | { |
---|
1095 | const char *cp = config->name; |
---|
1096 | char c; |
---|
1097 | int unitNumber = 0; |
---|
1098 | |
---|
1099 | if (cp == NULL) { |
---|
1100 | printf ("No network driver name.\n"); |
---|
1101 | return -1; |
---|
1102 | } |
---|
1103 | while ((c = *cp++) != '\0') { |
---|
1104 | if ((c >= '0') && (c <= '9')) { |
---|
1105 | int len = cp - config->name; |
---|
1106 | if ((len < 2) || (len > 50)) |
---|
1107 | break; |
---|
1108 | for (;;) { |
---|
1109 | unitNumber = (unitNumber * 10) + (c - '0'); |
---|
1110 | c = *cp++; |
---|
1111 | if (c == '\0') { |
---|
1112 | char *unitName = malloc (len); |
---|
1113 | if (unitName == NULL) { |
---|
1114 | printf ("No memory.\n"); |
---|
1115 | return -1; |
---|
1116 | } |
---|
1117 | strncpy (unitName, config->name, len - 1); |
---|
1118 | unitName[len-1] = '\0'; |
---|
1119 | *namep = unitName; |
---|
1120 | return unitNumber; |
---|
1121 | } |
---|
1122 | if ((c < '0') || (c > '9')) |
---|
1123 | break; |
---|
1124 | } |
---|
1125 | break; |
---|
1126 | } |
---|
1127 | } |
---|
1128 | printf ("Bad network driver name `%s'.\n", config->name); |
---|
1129 | return -1; |
---|
1130 | } |
---|
1131 | |
---|
1132 | /* |
---|
1133 | * Handle requests for more network memory |
---|
1134 | * XXX: Another possibility would be to use a semaphore here with |
---|
1135 | * a release in the mbuf free macro. I have chosen this `polling' |
---|
1136 | * approach because: |
---|
1137 | * 1) It is simpler. |
---|
1138 | * 2) It adds no complexity to the free macro. |
---|
1139 | * 3) Running out of mbufs should be a rare |
---|
1140 | * condition -- predeployment testing of |
---|
1141 | * an application should indicate the |
---|
1142 | * required mbuf pool size. |
---|
1143 | * XXX: Should there be a panic if a task is stuck in the loop for |
---|
1144 | * more than a minute or so? |
---|
1145 | */ |
---|
1146 | int |
---|
1147 | m_mballoc (int nmb, int nowait) |
---|
1148 | { |
---|
1149 | if (nowait) |
---|
1150 | return 0; |
---|
1151 | m_reclaim (); |
---|
1152 | if (mmbfree == NULL) { |
---|
1153 | int try = 0; |
---|
1154 | int print_limit = 30 * rtems_bsdnet_ticks_per_second; |
---|
1155 | |
---|
1156 | mbstat.m_wait++; |
---|
1157 | for (;;) { |
---|
1158 | rtems_bsdnet_semaphore_release (); |
---|
1159 | rtems_task_wake_after (1); |
---|
1160 | rtems_bsdnet_semaphore_obtain (); |
---|
1161 | if (mmbfree) |
---|
1162 | break; |
---|
1163 | if (++try >= print_limit) { |
---|
1164 | printf ("Still waiting for mbuf.\n"); |
---|
1165 | try = 0; |
---|
1166 | } |
---|
1167 | } |
---|
1168 | } |
---|
1169 | else { |
---|
1170 | mbstat.m_drops++; |
---|
1171 | } |
---|
1172 | return 1; |
---|
1173 | } |
---|
1174 | |
---|
1175 | int |
---|
1176 | m_clalloc(ncl, nowait) |
---|
1177 | { |
---|
1178 | if (nowait) |
---|
1179 | return 0; |
---|
1180 | m_reclaim (); |
---|
1181 | if (mclfree == NULL) { |
---|
1182 | int try = 0; |
---|
1183 | int print_limit = 30 * rtems_bsdnet_ticks_per_second; |
---|
1184 | |
---|
1185 | mbstat.m_wait++; |
---|
1186 | for (;;) { |
---|
1187 | rtems_bsdnet_semaphore_release (); |
---|
1188 | rtems_task_wake_after (1); |
---|
1189 | rtems_bsdnet_semaphore_obtain (); |
---|
1190 | if (mclfree) |
---|
1191 | break; |
---|
1192 | if (++try >= print_limit) { |
---|
1193 | printf ("Still waiting for mbuf cluster.\n"); |
---|
1194 | try = 0; |
---|
1195 | } |
---|
1196 | } |
---|
1197 | } |
---|
1198 | else { |
---|
1199 | mbstat.m_drops++; |
---|
1200 | } |
---|
1201 | return 1; |
---|
1202 | } |
---|