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