add/re-enable at_wini debug output
[minix3.git] / servers / inet / generic / eth.c
blob59a17dc33791cfe415d9ce70de94255e8faa693c
1 /*
2 eth.c
4 Copyright 1995 Philip Homburg
5 */
7 #include "inet.h"
8 #include "buf.h"
9 #include "clock.h"
10 #include "event.h"
11 #include "osdep_eth.h"
12 #include "type.h"
14 #include "assert.h"
15 #include "buf.h"
16 #include "eth.h"
17 #include "eth_int.h"
18 #include "io.h"
19 #include "sr.h"
21 THIS_FILE
23 #define ETH_FD_NR (4*IP_PORT_MAX)
24 #define EXPIRE_TIME 60*HZ /* seconds */
26 typedef struct eth_fd
28 int ef_flags;
29 nwio_ethopt_t ef_ethopt;
30 eth_port_t *ef_port;
31 struct eth_fd *ef_type_next;
32 struct eth_fd *ef_send_next;
33 int ef_srfd;
34 acc_t *ef_rdbuf_head;
35 acc_t *ef_rdbuf_tail;
36 get_userdata_t ef_get_userdata;
37 put_userdata_t ef_put_userdata;
38 put_pkt_t ef_put_pkt;
39 time_t ef_exp_time;
40 size_t ef_write_count;
41 ioreq_t ef_ioctl_req;
42 } eth_fd_t;
44 #define EFF_FLAGS 0xf
45 # define EFF_EMPTY 0x0
46 # define EFF_INUSE 0x1
47 # define EFF_BUSY 0xE
48 # define EFF_READ_IP 0x2
49 # define EFF_WRITE_IP 0x4
50 # define EFF_IOCTL_IP 0x8
51 # define EFF_OPTSET 0x10
53 /* Note that the vh_type field is normally considered part of the ethernet
54 * header.
56 typedef struct
58 u16_t vh_type;
59 u16_t vh_vlan;
60 } vlan_hdr_t;
62 FORWARD int eth_checkopt ARGS(( eth_fd_t *eth_fd ));
63 FORWARD void hash_fd ARGS(( eth_fd_t *eth_fd ));
64 FORWARD void unhash_fd ARGS(( eth_fd_t *eth_fd ));
65 FORWARD void eth_buffree ARGS(( int priority ));
66 #ifdef BUF_CONSISTENCY_CHECK
67 FORWARD void eth_bufcheck ARGS(( void ));
68 #endif
69 FORWARD void packet2user ARGS(( eth_fd_t *fd, acc_t *pack, time_t exp_time ));
70 FORWARD void reply_thr_get ARGS(( eth_fd_t *eth_fd,
71 size_t result, int for_ioctl ));
72 FORWARD void reply_thr_put ARGS(( eth_fd_t *eth_fd,
73 size_t result, int for_ioctl ));
74 FORWARD void do_rec_conf ARGS(( eth_port_t *eth_port ));
75 FORWARD u32_t compute_rec_conf ARGS(( eth_port_t *eth_port ));
76 FORWARD acc_t *insert_vlan_hdr ARGS(( eth_port_t *eth_port, acc_t *pack ));
78 PUBLIC eth_port_t *eth_port_table;
79 PUBLIC int no_ethWritePort= 0;
81 PRIVATE eth_fd_t eth_fd_table[ETH_FD_NR];
82 PRIVATE ether_addr_t broadcast= { { 255, 255, 255, 255, 255, 255 } };
84 PUBLIC void eth_prep()
86 eth_port_table= alloc(eth_conf_nr * sizeof(eth_port_table[0]));
89 PUBLIC void eth_init()
91 int i, j;
93 assert (BUF_S >= sizeof(nwio_ethopt_t));
94 assert (BUF_S >= ETH_HDR_SIZE); /* these are in fact static assertions,
95 thus a good compiler doesn't
96 generate any code for this */
99 for (i=0; i<ETH_FD_NR; i++)
100 eth_fd_table[i].ef_flags= EFF_EMPTY;
101 for (i=0; i<eth_conf_nr; i++)
103 eth_port_table[i].etp_flags= EFF_EMPTY;
104 eth_port_table[i].etp_sendq_head= NULL;
105 eth_port_table[i].etp_sendq_tail= NULL;
106 eth_port_table[i].etp_type_any= NULL;
107 ev_init(&eth_port_table[i].etp_sendev);
108 for (j= 0; j<ETH_TYPE_HASH_NR; j++)
109 eth_port_table[i].etp_type[j]= NULL;
110 for (j= 0; j<ETH_VLAN_HASH_NR; j++)
111 eth_port_table[i].etp_vlan_tab[j]= NULL;
114 #ifndef BUF_CONSISTENCY_CHECK
115 bf_logon(eth_buffree);
116 #else
117 bf_logon(eth_buffree, eth_bufcheck);
118 #endif
120 osdep_eth_init();
123 PUBLIC int eth_open(port, srfd, get_userdata, put_userdata, put_pkt,
124 select_res)
125 int port, srfd;
126 get_userdata_t get_userdata;
127 put_userdata_t put_userdata;
128 put_pkt_t put_pkt;
129 select_res_t select_res;
131 int i;
132 eth_port_t *eth_port;
133 eth_fd_t *eth_fd;
135 DBLOCK(0x20, printf("eth_open (%d, %d, %lx, %lx)\n", port, srfd,
136 (unsigned long)get_userdata, (unsigned long)put_userdata));
137 eth_port= &eth_port_table[port];
138 if (!(eth_port->etp_flags & EPF_ENABLED))
139 return EGENERIC;
141 for (i=0; i<ETH_FD_NR && (eth_fd_table[i].ef_flags & EFF_INUSE);
142 i++);
144 if (i>=ETH_FD_NR)
146 DBLOCK(1, printf("out of fds\n"));
147 return EAGAIN;
150 eth_fd= &eth_fd_table[i];
152 eth_fd->ef_flags= EFF_INUSE;
153 eth_fd->ef_ethopt.nweo_flags=NWEO_DEFAULT;
154 eth_fd->ef_port= eth_port;
155 eth_fd->ef_srfd= srfd;
156 assert(eth_fd->ef_rdbuf_head == NULL);
157 eth_fd->ef_get_userdata= get_userdata;
158 eth_fd->ef_put_userdata= put_userdata;
159 eth_fd->ef_put_pkt= put_pkt;
161 return i;
164 PUBLIC int eth_ioctl(fd, req)
165 int fd;
166 ioreq_t req;
168 acc_t *data;
169 eth_fd_t *eth_fd;
170 eth_port_t *eth_port;
172 DBLOCK(0x20, printf("eth_ioctl (%d, 0x%lx)\n", fd, (unsigned long)req));
173 eth_fd= &eth_fd_table[fd];
174 eth_port= eth_fd->ef_port;
176 assert (eth_fd->ef_flags & EFF_INUSE);
178 switch (req)
180 case NWIOSETHOPT:
182 nwio_ethopt_t *ethopt;
183 nwio_ethopt_t oldopt, newopt;
184 int result;
185 u32_t new_en_flags, new_di_flags,
186 old_en_flags, old_di_flags;
188 data= (*eth_fd->ef_get_userdata)(eth_fd->
189 ef_srfd, 0, sizeof(nwio_ethopt_t), TRUE);
191 ethopt= (nwio_ethopt_t *)ptr2acc_data(data);
192 oldopt= eth_fd->ef_ethopt;
193 newopt= *ethopt;
195 old_en_flags= oldopt.nweo_flags & 0xffff;
196 old_di_flags= (oldopt.nweo_flags >> 16) & 0xffff;
197 new_en_flags= newopt.nweo_flags & 0xffff;
198 new_di_flags= (newopt.nweo_flags >> 16) & 0xffff;
199 if (new_en_flags & new_di_flags)
201 bf_afree(data);
202 reply_thr_get (eth_fd, EBADMODE, TRUE);
203 return NW_OK;
206 /* NWEO_ACC_MASK */
207 if (new_di_flags & NWEO_ACC_MASK)
209 bf_afree(data);
210 reply_thr_get (eth_fd, EBADMODE, TRUE);
211 return NW_OK;
213 /* you can't disable access modes */
215 if (!(new_en_flags & NWEO_ACC_MASK))
216 new_en_flags |= (old_en_flags & NWEO_ACC_MASK);
219 /* NWEO_LOC_MASK */
220 if (!((new_en_flags | new_di_flags) & NWEO_LOC_MASK))
222 new_en_flags |= (old_en_flags & NWEO_LOC_MASK);
223 new_di_flags |= (old_di_flags & NWEO_LOC_MASK);
226 /* NWEO_BROAD_MASK */
227 if (!((new_en_flags | new_di_flags) & NWEO_BROAD_MASK))
229 new_en_flags |= (old_en_flags & NWEO_BROAD_MASK);
230 new_di_flags |= (old_di_flags & NWEO_BROAD_MASK);
233 /* NWEO_MULTI_MASK */
234 if (!((new_en_flags | new_di_flags) & NWEO_MULTI_MASK))
236 new_en_flags |= (old_en_flags & NWEO_MULTI_MASK);
237 new_di_flags |= (old_di_flags & NWEO_MULTI_MASK);
238 newopt.nweo_multi= oldopt.nweo_multi;
241 /* NWEO_PROMISC_MASK */
242 if (!((new_en_flags | new_di_flags) & NWEO_PROMISC_MASK))
244 new_en_flags |= (old_en_flags & NWEO_PROMISC_MASK);
245 new_di_flags |= (old_di_flags & NWEO_PROMISC_MASK);
248 /* NWEO_REM_MASK */
249 if (!((new_en_flags | new_di_flags) & NWEO_REM_MASK))
251 new_en_flags |= (old_en_flags & NWEO_REM_MASK);
252 new_di_flags |= (old_di_flags & NWEO_REM_MASK);
253 newopt.nweo_rem= oldopt.nweo_rem;
256 /* NWEO_TYPE_MASK */
257 if (!((new_en_flags | new_di_flags) & NWEO_TYPE_MASK))
259 new_en_flags |= (old_en_flags & NWEO_TYPE_MASK);
260 new_di_flags |= (old_di_flags & NWEO_TYPE_MASK);
261 newopt.nweo_type= oldopt.nweo_type;
264 /* NWEO_RW_MASK */
265 if (!((new_en_flags | new_di_flags) & NWEO_RW_MASK))
267 new_en_flags |= (old_en_flags & NWEO_RW_MASK);
268 new_di_flags |= (old_di_flags & NWEO_RW_MASK);
271 if (eth_fd->ef_flags & EFF_OPTSET)
272 unhash_fd(eth_fd);
274 newopt.nweo_flags= ((unsigned long)new_di_flags << 16) |
275 new_en_flags;
276 eth_fd->ef_ethopt= newopt;
278 result= eth_checkopt(eth_fd);
280 if (result<0)
281 eth_fd->ef_ethopt= oldopt;
282 else
284 unsigned long opt_flags;
285 unsigned changes;
286 opt_flags= oldopt.nweo_flags ^
287 eth_fd->ef_ethopt.nweo_flags;
288 changes= ((opt_flags >> 16) | opt_flags) &
289 0xffff;
290 if (changes & (NWEO_BROAD_MASK |
291 NWEO_MULTI_MASK | NWEO_PROMISC_MASK))
293 do_rec_conf(eth_port);
297 if (eth_fd->ef_flags & EFF_OPTSET)
298 hash_fd(eth_fd);
300 bf_afree(data);
301 reply_thr_get (eth_fd, result, TRUE);
302 return NW_OK;
305 case NWIOGETHOPT:
307 nwio_ethopt_t *ethopt;
308 acc_t *acc;
309 int result;
311 acc= bf_memreq(sizeof(nwio_ethopt_t));
313 ethopt= (nwio_ethopt_t *)ptr2acc_data(acc);
315 *ethopt= eth_fd->ef_ethopt;
317 result= (*eth_fd->ef_put_userdata)(eth_fd->
318 ef_srfd, 0, acc, TRUE);
319 if (result >= 0)
320 reply_thr_put(eth_fd, NW_OK, TRUE);
321 return result;
323 case NWIOGETHSTAT:
325 nwio_ethstat_t *ethstat;
326 acc_t *acc;
327 int result;
329 assert (sizeof(nwio_ethstat_t) <= BUF_S);
331 eth_port= eth_fd->ef_port;
332 if (!(eth_port->etp_flags & EPF_ENABLED))
334 reply_thr_put(eth_fd, EGENERIC, TRUE);
335 return NW_OK;
338 if (!(eth_port->etp_flags & EPF_GOT_ADDR))
340 printf(
341 "eth_ioctl: suspending NWIOGETHSTAT ioctl\n");
343 eth_fd->ef_ioctl_req= req;
344 assert(!(eth_fd->ef_flags & EFF_IOCTL_IP));
345 eth_fd->ef_flags |= EFF_IOCTL_IP;
346 return NW_SUSPEND;
349 acc= bf_memreq(sizeof(nwio_ethstat_t));
350 compare (bf_bufsize(acc), ==, sizeof(*ethstat));
352 ethstat= (nwio_ethstat_t *)ptr2acc_data(acc);
353 ethstat->nwes_addr= eth_port->etp_ethaddr;
355 if (!eth_port->etp_vlan)
357 result= eth_get_stat(eth_port,
358 &ethstat->nwes_stat);
359 if (result != NW_OK)
361 bf_afree(acc);
362 reply_thr_put(eth_fd, result, TRUE);
363 return NW_OK;
366 else
368 /* No statistics */
369 memset(&ethstat->nwes_stat, '\0',
370 sizeof(ethstat->nwes_stat));
373 result= (*eth_fd->ef_put_userdata)(eth_fd->
374 ef_srfd, 0, acc, TRUE);
375 if (result >= 0)
376 reply_thr_put(eth_fd, NW_OK, TRUE);
377 return result;
379 default:
380 break;
382 reply_thr_put(eth_fd, EBADIOCTL, TRUE);
383 return NW_OK;
386 PUBLIC int eth_write(fd, count)
387 int fd;
388 size_t count;
390 eth_fd_t *eth_fd;
391 eth_port_t *eth_port, *rep;
392 acc_t *user_data;
393 int r;
395 eth_fd= &eth_fd_table[fd];
396 eth_port= eth_fd->ef_port;
398 if (!(eth_fd->ef_flags & EFF_OPTSET))
400 reply_thr_get (eth_fd, EBADMODE, FALSE);
401 return NW_OK;
404 assert (!(eth_fd->ef_flags & EFF_WRITE_IP));
406 eth_fd->ef_write_count= count;
407 if (eth_fd->ef_ethopt.nweo_flags & NWEO_RWDATONLY)
408 count += ETH_HDR_SIZE;
410 if (count<ETH_MIN_PACK_SIZE || count>ETH_MAX_PACK_SIZE)
412 DBLOCK(1, printf("illegal packetsize (%d)\n",count));
413 reply_thr_get (eth_fd, EPACKSIZE, FALSE);
414 return NW_OK;
416 eth_fd->ef_flags |= EFF_WRITE_IP;
418 /* Enqueue at the real ethernet port */
419 rep= eth_port->etp_vlan_port;
420 if (!rep)
421 rep= eth_port;
422 if (rep->etp_wr_pack)
424 eth_fd->ef_send_next= NULL;
425 if (rep->etp_sendq_head)
426 rep->etp_sendq_tail->ef_send_next= eth_fd;
427 else
428 rep->etp_sendq_head= eth_fd;
429 rep->etp_sendq_tail= eth_fd;
430 return NW_SUSPEND;
433 user_data= (*eth_fd->ef_get_userdata)(eth_fd->ef_srfd, 0,
434 eth_fd->ef_write_count, FALSE);
435 if (!user_data)
437 eth_fd->ef_flags &= ~EFF_WRITE_IP;
438 reply_thr_get (eth_fd, EFAULT, FALSE);
439 return NW_OK;
441 r= eth_send(fd, user_data, eth_fd->ef_write_count);
442 assert(r == NW_OK);
444 eth_fd->ef_flags &= ~EFF_WRITE_IP;
445 reply_thr_get(eth_fd, eth_fd->ef_write_count, FALSE);
446 return NW_OK;
449 PUBLIC int eth_send(fd, data, data_len)
450 int fd;
451 acc_t *data;
452 size_t data_len;
454 eth_fd_t *eth_fd;
455 eth_port_t *eth_port, *rep;
456 eth_hdr_t *eth_hdr;
457 acc_t *eth_pack;
458 unsigned long nweo_flags;
459 size_t count;
460 ev_arg_t ev_arg;
462 eth_fd= &eth_fd_table[fd];
463 eth_port= eth_fd->ef_port;
465 if (!(eth_fd->ef_flags & EFF_OPTSET))
466 return EBADMODE;
468 count= data_len;
469 if (eth_fd->ef_ethopt.nweo_flags & NWEO_RWDATONLY)
470 count += ETH_HDR_SIZE;
472 if (count<ETH_MIN_PACK_SIZE || count>ETH_MAX_PACK_SIZE)
474 DBLOCK(1, printf("illegal packetsize (%d)\n",count));
475 return EPACKSIZE;
477 rep= eth_port->etp_vlan_port;
478 if (!rep)
479 rep= eth_port;
481 if (rep->etp_wr_pack)
482 return NW_WOULDBLOCK;
484 nweo_flags= eth_fd->ef_ethopt.nweo_flags;
485 if (nweo_flags & NWEO_RWDATONLY)
487 eth_pack= bf_memreq(ETH_HDR_SIZE);
488 eth_pack->acc_next= data;
490 else
491 eth_pack= bf_packIffLess(data, ETH_HDR_SIZE);
493 eth_hdr= (eth_hdr_t *)ptr2acc_data(eth_pack);
495 if (nweo_flags & NWEO_REMSPEC)
496 eth_hdr->eh_dst= eth_fd->ef_ethopt.nweo_rem;
498 if (!(eth_port->etp_flags & EPF_GOT_ADDR))
500 /* No device, discard packet */
501 bf_afree(eth_pack);
502 return NW_OK;
505 if (!(nweo_flags & NWEO_EN_PROMISC))
506 eth_hdr->eh_src= eth_port->etp_ethaddr;
508 if (nweo_flags & NWEO_TYPESPEC)
509 eth_hdr->eh_proto= eth_fd->ef_ethopt.nweo_type;
511 if (eth_addrcmp(eth_hdr->eh_dst, eth_port->etp_ethaddr) == 0)
513 /* Local loopback. */
514 eth_port->etp_wr_pack= eth_pack;
515 ev_arg.ev_ptr= eth_port;
516 ev_enqueue(&eth_port->etp_sendev, eth_loop_ev, ev_arg);
517 return NW_OK;
520 if (rep != eth_port)
522 eth_pack= insert_vlan_hdr(eth_port, eth_pack);
523 if (!eth_pack)
525 /* Packet is silently discarded */
526 return NW_OK;
530 eth_write_port(rep, eth_pack);
531 return NW_OK;
534 PUBLIC int eth_read (fd, count)
535 int fd;
536 size_t count;
538 eth_fd_t *eth_fd;
539 acc_t *pack;
541 eth_fd= &eth_fd_table[fd];
542 if (!(eth_fd->ef_flags & EFF_OPTSET))
544 reply_thr_put(eth_fd, EBADMODE, FALSE);
545 return NW_OK;
547 if (count < ETH_MAX_PACK_SIZE)
549 reply_thr_put(eth_fd, EPACKSIZE, FALSE);
550 return NW_OK;
553 assert(!(eth_fd->ef_flags & EFF_READ_IP));
554 eth_fd->ef_flags |= EFF_READ_IP;
556 while (eth_fd->ef_rdbuf_head)
558 pack= eth_fd->ef_rdbuf_head;
559 eth_fd->ef_rdbuf_head= pack->acc_ext_link;
560 if (get_time() <= eth_fd->ef_exp_time)
562 packet2user(eth_fd, pack, eth_fd->ef_exp_time);
563 if (!(eth_fd->ef_flags & EFF_READ_IP))
564 return NW_OK;
566 else
567 bf_afree(pack);
569 return NW_SUSPEND;
572 PUBLIC int eth_cancel(fd, which_operation)
573 int fd;
574 int which_operation;
576 eth_fd_t *eth_fd, *prev, *loc_fd;
577 eth_port_t *eth_port;
579 DBLOCK(2, printf("eth_cancel (%d)\n", fd));
580 eth_fd= &eth_fd_table[fd];
582 switch (which_operation)
584 case SR_CANCEL_READ:
585 assert (eth_fd->ef_flags & EFF_READ_IP);
586 eth_fd->ef_flags &= ~EFF_READ_IP;
587 reply_thr_put(eth_fd, EINTR, FALSE);
588 break;
589 case SR_CANCEL_WRITE:
590 assert (eth_fd->ef_flags & EFF_WRITE_IP);
591 eth_fd->ef_flags &= ~EFF_WRITE_IP;
593 /* Remove fd from send queue */
594 eth_port= eth_fd->ef_port;
595 if (eth_port->etp_vlan_port)
596 eth_port= eth_port->etp_vlan_port;
597 for (prev= 0, loc_fd= eth_port->etp_sendq_head; loc_fd != NULL;
598 prev= loc_fd, loc_fd= loc_fd->ef_send_next)
600 if (loc_fd == eth_fd)
601 break;
603 assert(loc_fd == eth_fd);
604 if (prev == NULL)
605 eth_port->etp_sendq_head= loc_fd->ef_send_next;
606 else
607 prev->ef_send_next= loc_fd->ef_send_next;
608 if (loc_fd->ef_send_next == NULL)
609 eth_port->etp_sendq_tail= prev;
611 reply_thr_get(eth_fd, EINTR, FALSE);
612 break;
613 case SR_CANCEL_IOCTL:
614 assert (eth_fd->ef_flags & EFF_IOCTL_IP);
615 eth_fd->ef_flags &= ~EFF_IOCTL_IP;
616 reply_thr_get(eth_fd, EINTR, TRUE);
617 break;
618 default:
619 ip_panic(( "got unknown cancel request" ));
621 return NW_OK;
624 PUBLIC int eth_select(fd, operations)
625 int fd;
626 unsigned operations;
628 printf("eth_select: not implemented\n");
629 return 0;
632 PUBLIC void eth_close(fd)
633 int fd;
635 eth_fd_t *eth_fd;
636 eth_port_t *eth_port;
637 acc_t *pack;
639 eth_fd= &eth_fd_table[fd];
641 assert ((eth_fd->ef_flags & EFF_INUSE) &&
642 !(eth_fd->ef_flags & EFF_BUSY));
644 if (eth_fd->ef_flags & EFF_OPTSET)
645 unhash_fd(eth_fd);
646 while (eth_fd->ef_rdbuf_head != NULL)
648 pack= eth_fd->ef_rdbuf_head;
649 eth_fd->ef_rdbuf_head= pack->acc_ext_link;
650 bf_afree(pack);
652 eth_fd->ef_flags= EFF_EMPTY;
654 eth_port= eth_fd->ef_port;
655 do_rec_conf(eth_port);
658 PUBLIC void eth_loop_ev(ev, ev_arg)
659 event_t *ev;
660 ev_arg_t ev_arg;
662 acc_t *pack;
663 eth_port_t *eth_port;
665 eth_port= ev_arg.ev_ptr;
666 assert(ev == &eth_port->etp_sendev);
668 pack= eth_port->etp_wr_pack;
670 assert(!no_ethWritePort);
671 no_ethWritePort= 1;
672 eth_arrive(eth_port, pack, bf_bufsize(pack));
673 assert(no_ethWritePort);
674 no_ethWritePort= 0;
676 eth_port->etp_wr_pack= NULL;
677 eth_restart_write(eth_port);
680 PRIVATE int eth_checkopt (eth_fd)
681 eth_fd_t *eth_fd;
683 /* bug: we don't check access modes yet */
685 unsigned long flags;
686 unsigned int en_di_flags;
687 eth_port_t *eth_port;
688 acc_t *pack;
690 eth_port= eth_fd->ef_port;
691 flags= eth_fd->ef_ethopt.nweo_flags;
692 en_di_flags= (flags >>16) | (flags & 0xffff);
694 if ((en_di_flags & NWEO_ACC_MASK) &&
695 (en_di_flags & NWEO_LOC_MASK) &&
696 (en_di_flags & NWEO_BROAD_MASK) &&
697 (en_di_flags & NWEO_MULTI_MASK) &&
698 (en_di_flags & NWEO_PROMISC_MASK) &&
699 (en_di_flags & NWEO_REM_MASK) &&
700 (en_di_flags & NWEO_TYPE_MASK) &&
701 (en_di_flags & NWEO_RW_MASK))
703 eth_fd->ef_flags |= EFF_OPTSET;
705 else
706 eth_fd->ef_flags &= ~EFF_OPTSET;
708 while (eth_fd->ef_rdbuf_head != NULL)
710 pack= eth_fd->ef_rdbuf_head;
711 eth_fd->ef_rdbuf_head= pack->acc_ext_link;
712 bf_afree(pack);
715 return NW_OK;
718 PRIVATE void hash_fd(eth_fd)
719 eth_fd_t *eth_fd;
721 eth_port_t *eth_port;
722 int hash;
724 eth_port= eth_fd->ef_port;
725 if (eth_fd->ef_ethopt.nweo_flags & NWEO_TYPEANY)
727 eth_fd->ef_type_next= eth_port->etp_type_any;
728 eth_port->etp_type_any= eth_fd;
730 else
732 hash= eth_fd->ef_ethopt.nweo_type;
733 hash ^= (hash >> 8);
734 hash &= (ETH_TYPE_HASH_NR-1);
736 eth_fd->ef_type_next= eth_port->etp_type[hash];
737 eth_port->etp_type[hash]= eth_fd;
741 PRIVATE void unhash_fd(eth_fd)
742 eth_fd_t *eth_fd;
744 eth_port_t *eth_port;
745 eth_fd_t *prev, *curr, **eth_fd_p;
746 int hash;
748 eth_port= eth_fd->ef_port;
749 if (eth_fd->ef_ethopt.nweo_flags & NWEO_TYPEANY)
751 eth_fd_p= &eth_port->etp_type_any;
753 else
755 hash= eth_fd->ef_ethopt.nweo_type;
756 hash ^= (hash >> 8);
757 hash &= (ETH_TYPE_HASH_NR-1);
759 eth_fd_p= &eth_port->etp_type[hash];
761 for (prev= NULL, curr= *eth_fd_p; curr;
762 prev= curr, curr= curr->ef_type_next)
764 if (curr == eth_fd)
765 break;
767 assert(curr);
768 if (prev)
769 prev->ef_type_next= curr->ef_type_next;
770 else
771 *eth_fd_p= curr->ef_type_next;
774 PUBLIC void eth_restart_write(eth_port)
775 eth_port_t *eth_port;
777 eth_fd_t *eth_fd;
778 int r;
780 assert(eth_port->etp_wr_pack == NULL);
781 while (eth_fd= eth_port->etp_sendq_head, eth_fd != NULL)
783 if (eth_port->etp_wr_pack)
784 return;
785 eth_port->etp_sendq_head= eth_fd->ef_send_next;
787 assert(eth_fd->ef_flags & EFF_WRITE_IP);
788 eth_fd->ef_flags &= ~EFF_WRITE_IP;
789 r= eth_write(eth_fd-eth_fd_table, eth_fd->ef_write_count);
790 assert(r == NW_OK);
794 PUBLIC void eth_arrive (eth_port, pack, pack_size)
795 eth_port_t *eth_port;
796 acc_t *pack;
797 size_t pack_size;
800 eth_hdr_t *eth_hdr;
801 ether_addr_t *dst_addr;
802 int pack_stat;
803 ether_type_t type;
804 eth_fd_t *eth_fd, *first_fd, *share_fd;
805 int hash, i;
806 u16_t vlan, temp;
807 time_t exp_time;
808 acc_t *vlan_pack, *hdr_acc, *tmp_acc;
809 eth_port_t *vp;
810 vlan_hdr_t vh;
811 u32_t *p;
813 exp_time= get_time() + EXPIRE_TIME;
815 pack= bf_packIffLess(pack, ETH_HDR_SIZE);
817 eth_hdr= (eth_hdr_t*)ptr2acc_data(pack);
818 dst_addr= &eth_hdr->eh_dst;
820 DIFBLOCK(0x20, dst_addr->ea_addr[0] != 0xFF &&
821 (dst_addr->ea_addr[0] & 0x1),
822 printf("got multicast packet\n"));
824 if (dst_addr->ea_addr[0] & 0x1)
826 /* multi cast or broadcast */
827 if (eth_addrcmp(*dst_addr, broadcast) == 0)
828 pack_stat= NWEO_EN_BROAD;
829 else
830 pack_stat= NWEO_EN_MULTI;
832 else
834 assert(eth_port->etp_flags & EPF_GOT_ADDR);
835 if (eth_addrcmp (*dst_addr, eth_port->etp_ethaddr) == 0)
836 pack_stat= NWEO_EN_LOC;
837 else
838 pack_stat= NWEO_EN_PROMISC;
840 type= eth_hdr->eh_proto;
841 hash= type;
842 hash ^= (hash >> 8);
843 hash &= (ETH_TYPE_HASH_NR-1);
845 if (type == HTONS(ETH_VLAN_PROTO))
847 /* VLAN packet. Extract original ethernet packet */
849 vlan_pack= pack;
850 vlan_pack->acc_linkC++;
851 hdr_acc= bf_cut(vlan_pack, 0, 2*sizeof(ether_addr_t));
852 vlan_pack= bf_delhead(vlan_pack, 2*sizeof(ether_addr_t));
853 vlan_pack= bf_packIffLess(vlan_pack, sizeof(vh));
854 vh= *(vlan_hdr_t *)ptr2acc_data(vlan_pack);
855 vlan_pack= bf_delhead(vlan_pack, sizeof(vh));
856 hdr_acc= bf_append(hdr_acc, vlan_pack);
857 vlan_pack= hdr_acc; hdr_acc= NULL;
858 if (bf_bufsize(vlan_pack) < ETH_MIN_PACK_SIZE)
860 tmp_acc= bf_memreq(sizeof(vh));
862 /* Clear padding */
863 assert(sizeof(vh) <= sizeof(*p));
864 p= (u32_t *)ptr2acc_data(tmp_acc);
865 *p= 0xdeadbeef;
867 vlan_pack= bf_append(vlan_pack, tmp_acc);
868 tmp_acc= NULL;
870 vlan= ntohs(vh.vh_vlan);
871 if (vlan & ETH_TCI_CFI)
873 /* No support for extended address formats */
874 bf_afree(vlan_pack); vlan_pack= NULL;
876 vlan &= ETH_TCI_VLAN_MASK;
878 else
880 /* No VLAN processing */
881 vlan_pack= NULL;
882 vlan= 0; /* lint */
885 first_fd= NULL;
886 for (i= 0; i<2; i++)
888 share_fd= NULL;
890 eth_fd= (i == 0) ? eth_port->etp_type_any :
891 eth_port->etp_type[hash];
892 for (; eth_fd; eth_fd= eth_fd->ef_type_next)
894 if (i && eth_fd->ef_ethopt.nweo_type != type)
895 continue;
896 if (!(eth_fd->ef_ethopt.nweo_flags & pack_stat))
897 continue;
898 if (eth_fd->ef_ethopt.nweo_flags & NWEO_REMSPEC &&
899 eth_addrcmp(eth_hdr->eh_src,
900 eth_fd->ef_ethopt.nweo_rem) != 0)
902 continue;
904 if ((eth_fd->ef_ethopt.nweo_flags & NWEO_ACC_MASK) ==
905 NWEO_SHARED)
907 if (!share_fd)
909 share_fd= eth_fd;
910 continue;
912 if (!eth_fd->ef_rdbuf_head)
913 share_fd= eth_fd;
914 continue;
916 if (!first_fd)
918 first_fd= eth_fd;
919 continue;
921 pack->acc_linkC++;
922 packet2user(eth_fd, pack, exp_time);
924 if (share_fd)
926 pack->acc_linkC++;
927 packet2user(share_fd, pack, exp_time);
930 if (first_fd)
932 if (first_fd->ef_put_pkt &&
933 (first_fd->ef_flags & EFF_READ_IP) &&
934 !(first_fd->ef_ethopt.nweo_flags & NWEO_RWDATONLY))
936 (*first_fd->ef_put_pkt)(first_fd->ef_srfd, pack,
937 pack_size);
939 else
940 packet2user(first_fd, pack, exp_time);
942 else
944 if (pack_stat == NWEO_EN_LOC)
946 DBLOCK(0x01,
947 printf("eth_arrive: dropping packet for proto 0x%x\n",
948 ntohs(type)));
950 else
952 DBLOCK(0x20, printf("dropping packet for proto 0x%x\n",
953 ntohs(type)));
955 bf_afree(pack);
957 if (vlan_pack)
959 hash= ETH_HASH_VLAN(vlan, temp);
960 for (vp= eth_port->etp_vlan_tab[hash]; vp;
961 vp= vp->etp_vlan_next)
963 if (vp->etp_vlan == vlan)
964 break;
966 if (vp)
968 eth_arrive(vp, vlan_pack, pack_size-sizeof(vh));
969 vlan_pack= NULL;
971 else
973 /* No device for VLAN */
974 bf_afree(vlan_pack);
975 vlan_pack= NULL;
980 PUBLIC void eth_reg_vlan(eth_port, vlan_port)
981 eth_port_t *eth_port;
982 eth_port_t *vlan_port;
984 u16_t t, vlan;
985 int h;
987 vlan= vlan_port->etp_vlan;
988 h= ETH_HASH_VLAN(vlan, t);
989 vlan_port->etp_vlan_next= eth_port->etp_vlan_tab[h];
990 eth_port->etp_vlan_tab[h]= vlan_port;
993 PUBLIC void eth_restart_ioctl(eth_port)
994 eth_port_t *eth_port;
996 int i;
997 eth_fd_t *eth_fd;
999 for (i= 0, eth_fd= eth_fd_table; i<ETH_FD_NR; i++, eth_fd++)
1001 if (!(eth_fd->ef_flags & EFF_INUSE))
1002 continue;
1003 if (eth_fd->ef_port != eth_port)
1004 continue;
1005 if (!(eth_fd->ef_flags & EFF_IOCTL_IP))
1006 continue;
1007 if (eth_fd->ef_ioctl_req != NWIOGETHSTAT)
1008 continue;
1010 eth_fd->ef_flags &= ~EFF_IOCTL_IP;
1011 eth_ioctl(i, eth_fd->ef_ioctl_req);
1015 PRIVATE void packet2user (eth_fd, pack, exp_time)
1016 eth_fd_t *eth_fd;
1017 acc_t *pack;
1018 time_t exp_time;
1020 int result;
1021 acc_t *tmp_pack;
1022 size_t size;
1024 assert (eth_fd->ef_flags & EFF_INUSE);
1025 if (!(eth_fd->ef_flags & EFF_READ_IP))
1027 if (pack->acc_linkC != 1)
1029 tmp_pack= bf_dupacc(pack);
1030 bf_afree(pack);
1031 pack= tmp_pack;
1032 tmp_pack= NULL;
1034 pack->acc_ext_link= NULL;
1035 if (eth_fd->ef_rdbuf_head == NULL)
1037 eth_fd->ef_rdbuf_head= pack;
1038 eth_fd->ef_exp_time= exp_time;
1040 else
1041 eth_fd->ef_rdbuf_tail->acc_ext_link= pack;
1042 eth_fd->ef_rdbuf_tail= pack;
1043 return;
1046 if (eth_fd->ef_ethopt.nweo_flags & NWEO_RWDATONLY)
1047 pack= bf_delhead(pack, ETH_HDR_SIZE);
1049 size= bf_bufsize(pack);
1051 if (eth_fd->ef_put_pkt)
1053 (*eth_fd->ef_put_pkt)(eth_fd->ef_srfd, pack, size);
1054 return;
1057 eth_fd->ef_flags &= ~EFF_READ_IP;
1058 result= (*eth_fd->ef_put_userdata)(eth_fd->ef_srfd, (size_t)0, pack,
1059 FALSE);
1060 if (result >=0)
1061 reply_thr_put(eth_fd, size, FALSE);
1062 else
1063 reply_thr_put(eth_fd, result, FALSE);
1066 PRIVATE void eth_buffree (priority)
1067 int priority;
1069 int i;
1070 eth_fd_t *eth_fd;
1071 acc_t *pack;
1073 if (priority == ETH_PRI_FDBUFS_EXTRA)
1075 for (i= 0, eth_fd= eth_fd_table; i<ETH_FD_NR; i++, eth_fd++)
1077 while (eth_fd->ef_rdbuf_head &&
1078 eth_fd->ef_rdbuf_head->acc_ext_link)
1080 pack= eth_fd->ef_rdbuf_head;
1081 eth_fd->ef_rdbuf_head= pack->acc_ext_link;
1082 bf_afree(pack);
1086 if (priority == ETH_PRI_FDBUFS)
1088 for (i= 0, eth_fd= eth_fd_table; i<ETH_FD_NR; i++, eth_fd++)
1090 while (eth_fd->ef_rdbuf_head)
1092 pack= eth_fd->ef_rdbuf_head;
1093 eth_fd->ef_rdbuf_head= pack->acc_ext_link;
1094 bf_afree(pack);
1100 #ifdef BUF_CONSISTENCY_CHECK
1101 PRIVATE void eth_bufcheck()
1103 int i;
1104 eth_fd_t *eth_fd;
1105 acc_t *pack;
1107 for (i= 0; i<eth_conf_nr; i++)
1109 bf_check_acc(eth_port_table[i].etp_rd_pack);
1110 bf_check_acc(eth_port_table[i].etp_wr_pack);
1112 for (i= 0, eth_fd= eth_fd_table; i<ETH_FD_NR; i++, eth_fd++)
1114 for (pack= eth_fd->ef_rdbuf_head; pack;
1115 pack= pack->acc_ext_link)
1117 bf_check_acc(pack);
1121 #endif
1123 PRIVATE void do_rec_conf(eth_port)
1124 eth_port_t *eth_port;
1126 int i;
1127 u32_t flags;
1128 eth_port_t *vp;
1130 assert(eth_port);
1132 if (eth_port->etp_vlan)
1134 /* Configure underlying device */
1135 eth_port= eth_port->etp_vlan_port;
1137 flags= compute_rec_conf(eth_port);
1138 for (i= 0; i<ETH_VLAN_HASH_NR; i++)
1140 for (vp= eth_port->etp_vlan_tab[i]; vp; vp= vp->etp_vlan_next)
1141 flags |= compute_rec_conf(vp);
1143 eth_set_rec_conf(eth_port, flags);
1146 PRIVATE u32_t compute_rec_conf(eth_port)
1147 eth_port_t *eth_port;
1149 eth_fd_t *eth_fd;
1150 u32_t flags;
1151 int i;
1153 flags= NWEO_NOFLAGS;
1154 for (i=0, eth_fd= eth_fd_table; i<ETH_FD_NR; i++, eth_fd++)
1156 if ((eth_fd->ef_flags & (EFF_INUSE|EFF_OPTSET)) !=
1157 (EFF_INUSE|EFF_OPTSET))
1159 continue;
1161 if (eth_fd->ef_port != eth_port)
1162 continue;
1163 flags |= eth_fd->ef_ethopt.nweo_flags;
1165 return flags;
1168 PRIVATE void reply_thr_get (eth_fd, result, for_ioctl)
1169 eth_fd_t *eth_fd;
1170 size_t result;
1171 int for_ioctl;
1173 acc_t *data;
1175 data= (*eth_fd->ef_get_userdata)(eth_fd->ef_srfd, result, 0, for_ioctl);
1176 assert (!data);
1179 PRIVATE void reply_thr_put (eth_fd, result, for_ioctl)
1180 eth_fd_t *eth_fd;
1181 size_t result;
1182 int for_ioctl;
1184 int error;
1186 error= (*eth_fd->ef_put_userdata)(eth_fd->ef_srfd, result, (acc_t *)0,
1187 for_ioctl);
1188 assert(error == NW_OK);
1191 PRIVATE acc_t *insert_vlan_hdr(eth_port, pack)
1192 eth_port_t *eth_port;
1193 acc_t *pack;
1195 acc_t *head_acc, *vh_acc;
1196 u16_t type, vlan;
1197 vlan_hdr_t *vp;
1199 head_acc= bf_cut(pack, 0, 2*sizeof(ether_addr_t));
1200 pack= bf_delhead(pack, 2*sizeof(ether_addr_t));
1201 pack= bf_packIffLess(pack, sizeof(type));
1202 type= *(u16_t *)ptr2acc_data(pack);
1203 if (type == HTONS(ETH_VLAN_PROTO))
1205 /* Packeted is already tagged. Should update vlan number.
1206 * For now, just discard packet.
1208 printf("insert_vlan_hdr: discarding vlan packet\n");
1209 bf_afree(head_acc); head_acc= NULL;
1210 bf_afree(pack); pack= NULL;
1211 return NULL;
1213 vlan= eth_port->etp_vlan; /* priority and CFI are zero */
1215 vh_acc= bf_memreq(sizeof(vlan_hdr_t));
1216 vp= (vlan_hdr_t *)ptr2acc_data(vh_acc);
1217 vp->vh_type= HTONS(ETH_VLAN_PROTO);
1218 vp->vh_vlan= htons(vlan);
1220 head_acc= bf_append(head_acc, vh_acc); vh_acc= NULL;
1221 head_acc= bf_append(head_acc, pack); pack= NULL;
1222 pack= head_acc; head_acc= NULL;
1223 return pack;
1227 * $PchId: eth.c,v 1.23 2005/06/28 14:15:58 philip Exp $