ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/ether_unix.cpp
(Generate patch)

Comparing BasiliskII/src/Unix/ether_unix.cpp (file contents):
Revision 1.18 by gbeauche, 2005-05-14T08:10:49Z vs.
Revision 1.23 by gbeauche, 2005-07-03T08:21:32Z

# Line 66 | Line 66 | using std::map;
66   #define DEBUG 0
67   #include "debug.h"
68  
69 + #define STATISTICS 0
70   #define MONITOR 0
71  
72  
# Line 93 | Line 94 | static const char *net_if_script = NULL;
94   static pthread_t slirp_thread;                          // Slirp reception thread
95   static bool slirp_thread_active = false;        // Flag: Slirp reception threadinstalled
96   static int slirp_output_fd = -1;                        // fd of slirp output pipe
97 + static int slirp_input_fds[2] = { -1, -1 };     // fds of slirp input pipe
98 + #ifdef SHEEPSHAVER
99 + static bool net_open = false;                           // Flag: initialization succeeded, network device open
100 + static uint8 ether_addr[6];                                     // Our Ethernet address
101 + #else
102 + const bool ether_driver_opened = true;          // Flag: is the MacOS driver opened?
103 + #endif
104  
105   // Attached network protocols, maps protocol type to MacOS handler address
106   static map<uint16, uint32> net_protocols;
# Line 101 | Line 109 | static map<uint16, uint32> net_protocols
109   static void *receive_func(void *arg);
110   static void *slirp_receive_func(void *arg);
111   static int poll_fd(int fd);
112 + static int16 ether_do_add_multicast(uint8 *addr);
113 + static int16 ether_do_del_multicast(uint8 *addr);
114 + static int16 ether_do_write(uint32 arg);
115 + static void ether_do_interrupt(void);
116  
117  
118   /*
# Line 143 | Line 155 | static void stop_thread(void)
155   {
156   #ifdef HAVE_SLIRP
157          if (slirp_thread_active) {
158 + #ifdef HAVE_PTHREAD_CANCEL
159                  pthread_cancel(slirp_thread);
160 + #endif
161                  pthread_join(slirp_thread, NULL);
162                  slirp_thread_active = false;
163          }
164   #endif
165  
166          if (thread_active) {
167 + #ifdef HAVE_PTHREAD_CANCEL
168                  pthread_cancel(ether_thread);
169 + #endif
170                  pthread_join(ether_thread, NULL);
171                  sem_destroy(&int_ack);
172                  thread_active = false;
# Line 219 | Line 235 | bool ether_init(void)
235   #ifdef HAVE_SLIRP
236          // Initialize slirp library
237          if (net_if_type == NET_IF_SLIRP) {
238 <                slirp_init();
238 >                if (slirp_init() < 0) {
239 >                        sprintf(str, GetString(STR_SLIRP_NO_DNS_FOUND_WARN));
240 >                        WarningAlert(str);
241 >                        return false;
242 >                }
243  
244                  // Open slirp output pipe
245                  int fds[2];
# Line 227 | Line 247 | bool ether_init(void)
247                          return false;
248                  fd = fds[0];
249                  slirp_output_fd = fds[1];
250 +
251 +                // Open slirp input pipe
252 +                if (pipe(slirp_input_fds) < 0)
253 +                        return false;
254          }
255   #endif
256  
# Line 334 | Line 358 | open_error:
358                  close(fd);
359                  fd = -1;
360          }
361 +        if (slirp_input_fds[0] >= 0) {
362 +                close(slirp_input_fds[0]);
363 +                slirp_input_fds[0] = -1;
364 +        }
365 +        if (slirp_input_fds[1] >= 0) {
366 +                close(slirp_input_fds[1]);
367 +                slirp_input_fds[1] = -1;
368 +        }
369          if (slirp_output_fd >= 0) {
370                  close(slirp_output_fd);
371                  slirp_output_fd = -1;
# Line 348 | Line 380 | open_error:
380  
381   void ether_exit(void)
382   {
383 <        // Stop reception thread
384 <        if (thread_active) {
353 <                pthread_cancel(ether_thread);
354 <                pthread_join(ether_thread, NULL);
355 <                sem_destroy(&int_ack);
356 <                thread_active = false;
357 <        }
383 >        // Stop reception threads
384 >        stop_thread();
385  
386          // Shut down TUN/TAP interface
387          if (net_if_type == NET_IF_TUNTAP)
# Line 368 | Line 395 | void ether_exit(void)
395          if (fd > 0)
396                  close(fd);
397  
398 +        // Close slirp input buffer
399 +        if (slirp_input_fds[0] >= 0)
400 +                close(slirp_input_fds[0]);
401 +        if (slirp_input_fds[1] >= 0)
402 +                close(slirp_input_fds[1]);
403 +
404          // Close slirp output buffer
405          if (slirp_output_fd > 0)
406                  close(slirp_output_fd);
407 +
408 + #if STATISTICS
409 +        // Show statistics
410 +        printf("%ld messages put on write queue\n", num_wput);
411 +        printf("%ld error acks\n", num_error_acks);
412 +        printf("%ld packets transmitted (%ld raw, %ld normal)\n", num_tx_packets, num_tx_raw_packets, num_tx_normal_packets);
413 +        printf("%ld tx packets dropped because buffer full\n", num_tx_buffer_full);
414 +        printf("%ld packets received\n", num_rx_packets);
415 +        printf("%ld packets passed upstream (%ld Fast Path, %ld normal)\n", num_rx_fastpath + num_unitdata_ind, num_rx_fastpath, num_unitdata_ind);
416 +        printf("EtherIRQ called %ld times\n", num_ether_irq);
417 +        printf("%ld rx packets dropped due to low memory\n", num_rx_no_mem);
418 +        printf("%ld rx packets dropped because no stream found\n", num_rx_dropped);
419 +        printf("%ld rx packets dropped because stream not ready\n", num_rx_stream_not_ready);
420 +        printf("%ld rx packets dropped because no memory for unitdata_ind\n", num_rx_no_unitdata_mem);
421 + #endif
422   }
423  
424  
425   /*
426 + *  Glue around low-level implementation
427 + */
428 +
429 + #ifdef SHEEPSHAVER
430 + // Error codes
431 + enum {
432 +        eMultiErr               = -91,
433 +        eLenErr                 = -92,
434 +        lapProtErr              = -94,
435 +        excessCollsns   = -95
436 + };
437 +
438 + // Initialize ethernet
439 + void EtherInit(void)
440 + {
441 +        net_open = false;
442 +
443 +        // Do nothing if the user disabled the network
444 +        if (PrefsFindBool("nonet"))
445 +                return;
446 +
447 +        net_open = ether_init();
448 + }
449 +
450 + // Exit ethernet
451 + void EtherExit(void)
452 + {
453 +        ether_exit();
454 +        net_open = false;
455 + }
456 +
457 + // Get ethernet hardware address
458 + void AO_get_ethernet_address(uint32 arg)
459 + {
460 +        uint8 *addr = Mac2HostAddr(arg);
461 +        if (net_open)
462 +                OTCopy48BitAddress(ether_addr, addr);
463 +        else {
464 +                addr[0] = 0x12;
465 +                addr[1] = 0x34;
466 +                addr[2] = 0x56;
467 +                addr[3] = 0x78;
468 +                addr[4] = 0x9a;
469 +                addr[5] = 0xbc;
470 +        }
471 +        D(bug("AO_get_ethernet_address: got address %02x%02x%02x%02x%02x%02x\n", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]));
472 + }
473 +
474 + // Add multicast address
475 + void AO_enable_multicast(uint32 addr)
476 + {
477 +        if (net_open)
478 +                ether_do_add_multicast(Mac2HostAddr(addr));
479 + }
480 +
481 + // Disable multicast address
482 + void AO_disable_multicast(uint32 addr)
483 + {
484 +        if (net_open)
485 +                ether_do_del_multicast(Mac2HostAddr(addr));
486 + }
487 +
488 + // Transmit one packet
489 + void AO_transmit_packet(uint32 mp)
490 + {
491 +        if (net_open) {
492 +                switch (ether_do_write(mp)) {
493 +                case noErr:
494 +                        num_tx_packets++;
495 +                        break;
496 +                case excessCollsns:
497 +                        num_tx_buffer_full++;
498 +                        break;
499 +                }
500 +        }
501 + }
502 +
503 + // Copy packet data from message block to linear buffer
504 + static inline int ether_arg_to_buffer(uint32 mp, uint8 *p)
505 + {
506 +        return ether_msgb_to_buffer(mp, p);
507 + }
508 +
509 + // Ethernet interrupt
510 + void EtherIRQ(void)
511 + {
512 +        D(bug("EtherIRQ\n"));
513 +        num_ether_irq++;
514 +
515 +        OTEnterInterrupt();
516 +        ether_do_interrupt();
517 +        OTLeaveInterrupt();
518 +
519 +        // Acknowledge interrupt to reception thread
520 +        D(bug(" EtherIRQ done\n"));
521 +        sem_post(&int_ack);
522 + }
523 + #else
524 + // Add multicast address
525 + int16 ether_add_multicast(uint32 pb)
526 + {
527 +        return ether_do_add_multicast(Mac2HostAddr(pb + eMultiAddr));
528 + }
529 +
530 + // Disable multicast address
531 + int16 ether_del_multicast(uint32 pb)
532 + {
533 +        return ether_do_del_multicast(Mac2HostAddr(pb + eMultiAddr));
534 + }
535 +
536 + // Transmit one packet
537 + int16 ether_write(uint32 wds)
538 + {
539 +        return ether_do_write(wds);
540 + }
541 +
542 + // Copy packet data from WDS to linear buffer
543 + static inline int ether_arg_to_buffer(uint32 wds, uint8 *p)
544 + {
545 +        return ether_wds_to_buffer(wds, p);
546 + }
547 +
548 + // Dispatch packet to protocol handler
549 + static void ether_dispatch_packet(uint32 p, uint32 length)
550 + {
551 +        // Get packet type
552 +        uint16 type = ReadMacInt16(p + 12);
553 +
554 +        // Look for protocol
555 +        uint16 search_type = (type <= 1500 ? 0 : type);
556 +        if (net_protocols.find(search_type) == net_protocols.end())
557 +                return;
558 +        uint32 handler = net_protocols[search_type];
559 +
560 +        // No default handler
561 +        if (handler == 0)
562 +                return;
563 +
564 +        // Copy header to RHA
565 +        Mac2Mac_memcpy(ether_data + ed_RHA, p, 14);
566 +        D(bug(" header %08x%04x %08x%04x %04x\n", ReadMacInt32(ether_data + ed_RHA), ReadMacInt16(ether_data + ed_RHA + 4), ReadMacInt32(ether_data + ed_RHA + 6), ReadMacInt16(ether_data + ed_RHA + 10), ReadMacInt16(ether_data + ed_RHA + 12)));
567 +
568 +        // Call protocol handler
569 +        M68kRegisters r;
570 +        r.d[0] = type;                                                                  // Packet type
571 +        r.d[1] = length - 14;                                                   // Remaining packet length (without header, for ReadPacket)
572 +        r.a[0] = p + 14;                                                                // Pointer to packet (Mac address, for ReadPacket)
573 +        r.a[3] = ether_data + ed_RHA + 14;                              // Pointer behind header in RHA
574 +        r.a[4] = ether_data + ed_ReadPacket;                    // Pointer to ReadPacket/ReadRest routines
575 +        D(bug(" calling protocol handler %08x, type %08x, length %08x, data %08x, rha %08x, read_packet %08x\n", handler, r.d[0], r.d[1], r.a[0], r.a[3], r.a[4]));
576 +        Execute68k(handler, &r);
577 + }
578 +
579 + // Ethernet interrupt
580 + void EtherInterrupt(void)
581 + {
582 +        D(bug("EtherIRQ\n"));
583 +        ether_do_interrupt();
584 +
585 +        // Acknowledge interrupt to reception thread
586 +        D(bug(" EtherIRQ done\n"));
587 +        sem_post(&int_ack);
588 + }
589 + #endif
590 +
591 +
592 + /*
593   *  Reset
594   */
595  
# Line 388 | Line 603 | void ether_reset(void)
603   *  Add multicast address
604   */
605  
606 < int16 ether_add_multicast(uint32 pb)
606 > static int16 ether_do_add_multicast(uint8 *addr)
607   {
608          switch (net_if_type) {
609          case NET_IF_ETHERTAP:
610          case NET_IF_SHEEPNET:
611 <                if (ioctl(fd, SIOCADDMULTI, Mac2HostAddr(pb + eMultiAddr)) < 0) {
611 >                if (ioctl(fd, SIOCADDMULTI, addr) < 0) {
612                          D(bug("WARNING: Couldn't enable multicast address\n"));
613                          if (net_if_type == NET_IF_ETHERTAP)
614                                  return noErr;
# Line 410 | Line 625 | int16 ether_add_multicast(uint32 pb)
625   *  Delete multicast address
626   */
627  
628 < int16 ether_del_multicast(uint32 pb)
628 > static int16 ether_do_del_multicast(uint8 *addr)
629   {
630          switch (net_if_type) {
631          case NET_IF_ETHERTAP:
632          case NET_IF_SHEEPNET:
633 <                if (ioctl(fd, SIOCDELMULTI, Mac2HostAddr(pb + eMultiAddr)) < 0) {
633 >                if (ioctl(fd, SIOCDELMULTI, addr) < 0) {
634                          D(bug("WARNING: Couldn't disable multicast address\n"));
635                          return eMultiErr;
636                  }
# Line 454 | Line 669 | int16 ether_detach_ph(uint16 type)
669   *  Transmit raw ethernet packet
670   */
671  
672 < int16 ether_write(uint32 wds)
672 > static int16 ether_do_write(uint32 arg)
673   {
674          // Copy packet to buffer
675          uint8 packet[1516], *p = packet;
# Line 466 | Line 681 | int16 ether_write(uint32 wds)
681                  len += 2;
682          }
683   #endif
684 <        len += ether_wds_to_buffer(wds, p);
684 >        len += ether_arg_to_buffer(arg, p);
685  
686   #if MONITOR
687          bug("Sending Ethernet packet:\n");
# Line 479 | Line 694 | int16 ether_write(uint32 wds)
694          // Transmit packet
695   #ifdef HAVE_SLIRP
696          if (net_if_type == NET_IF_SLIRP) {
697 <                slirp_input(packet, len);
697 >                const int slirp_input_fd = slirp_input_fds[1];
698 >                write(slirp_input_fd, &len, sizeof(len));
699 >                write(slirp_input_fd, packet, len);
700                  return noErr;
701          } else
702   #endif
# Line 531 | Line 748 | void slirp_output(const uint8 *packet, i
748  
749   void *slirp_receive_func(void *arg)
750   {
751 +        const int slirp_input_fd = slirp_input_fds[0];
752 +
753          for (;;) {
754                  // Wait for packets to arrive
755                  fd_set rfds, wfds, xfds;
756                  int nfds;
757                  struct timeval tv;
758  
759 +                // ... in the input queue
760 +                FD_ZERO(&rfds);
761 +                FD_SET(slirp_input_fd, &rfds);
762 +                tv.tv_sec = 0;
763 +                tv.tv_usec = 0;
764 +                if (select(slirp_input_fd + 1, &rfds, NULL, NULL, &tv) > 0) {
765 +                        int len;
766 +                        read(slirp_input_fd, &len, sizeof(len));
767 +                        uint8 packet[1516];
768 +                        assert(len <= sizeof(packet));
769 +                        read(slirp_input_fd, packet, len);
770 +                        slirp_input(packet, len);
771 +                }
772 +
773 +                // ... in the output queue
774                  nfds = -1;
775                  FD_ZERO(&rfds);
776                  FD_ZERO(&wfds);
777                  FD_ZERO(&xfds);
778                  slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
779                  tv.tv_sec = 0;
780 <                tv.tv_usec = 16667;
780 >                tv.tv_usec = 10000;
781                  if (select(nfds + 1, &rfds, &wfds, &xfds, &tv) >= 0)
782                          slirp_select_poll(&rfds, &wfds, &xfds);
783 +
784 + #ifdef HAVE_PTHREAD_TESTCANCEL
785 +                // Explicit cancellation point if select() was not covered
786 +                // This seems to be the case on MacOS X 10.2
787 +                pthread_testcancel();
788 + #endif
789          }
790          return NULL;
791   }
# Line 562 | Line 802 | void slirp_output(const uint8 *packet, i
802  
803  
804   /*
565 *  Wait for data to arrive
566 */
567
568 static inline int poll_fd(int fd)
569 {
570 #ifdef HAVE_POLL
571        struct pollfd pf = {fd, POLLIN, 0};
572        return poll(&pf, 1, -1);
573 #else
574        fd_set rfds;
575        FD_ZERO(&rfds);
576        FD_SET(fd, &rfds);
577        return select(1, &rfds, NULL, NULL, NULL);
578 #endif
579 }
580
581
582 /*
805   *  Packet reception thread
806   */
807  
# Line 588 | Line 810 | static void *receive_func(void *arg)
810          for (;;) {
811  
812                  // Wait for packets to arrive
813 <                int res = poll_fd(fd);
813 > #if HAVE_POLL
814 >                struct pollfd pf = {fd, POLLIN, 0};
815 >                int res = poll(&pf, 1, -1);
816 > #else
817 >                fd_set rfds;
818 >                FD_ZERO(&rfds);
819 >                FD_SET(fd, &rfds);
820 >                // A NULL timeout could cause select() to block indefinitely,
821 >                // even if it is supposed to be a cancellation point [MacOS X]
822 >                struct timeval tv = { 0, 20000 };
823 >                int res = select(fd + 1, &rfds, NULL, NULL, &tv);
824 > #ifdef HAVE_PTHREAD_TESTCANCEL
825 >                pthread_testcancel();
826 > #endif
827 >                if (res == 0 || (res == -1 && errno == EINTR))
828 >                        continue;
829 > #endif
830                  if (res <= 0)
831                          break;
832  
833 <                // Trigger Ethernet interrupt
834 <                D(bug(" packet received, triggering Ethernet interrupt\n"));
835 <                SetInterruptFlag(INTFLAG_ETHER);
836 <                TriggerInterrupt();
837 <
838 <                // Wait for interrupt acknowledge by EtherInterrupt()
839 <                sem_wait(&int_ack);
833 >                if (ether_driver_opened) {
834 >                        // Trigger Ethernet interrupt
835 >                        D(bug(" packet received, triggering Ethernet interrupt\n"));
836 >                        SetInterruptFlag(INTFLAG_ETHER);
837 >                        TriggerInterrupt();
838 >
839 >                        // Wait for interrupt acknowledge by EtherInterrupt()
840 >                        sem_wait(&int_ack);
841 >                } else
842 >                        usleep(20000);
843          }
844          return NULL;
845   }
# Line 608 | Line 849 | static void *receive_func(void *arg)
849   *  Ethernet interrupt - activate deferred tasks to call IODone or protocol handlers
850   */
851  
852 < void EtherInterrupt(void)
852 > void ether_do_interrupt(void)
853   {
613        D(bug("EtherIRQ\n"));
614
854          // Call protocol handler for received packets
855          EthernetPacket ether_packet;
856          uint32 packet = ether_packet.addr();
857          ssize_t length;
858          for (;;) {
859  
860 + #ifndef SHEEPSHAVER
861                  if (udp_tunnel) {
862  
863                          // Read packet from socket
# Line 628 | Line 868 | void EtherInterrupt(void)
868                                  break;
869                          ether_udp_read(packet, length, &from);
870  
871 <                } else {
871 >                } else
872 > #endif
873 >                {
874  
875                          // Read packet from sheep_net device
876   #if defined(__linux__)
# Line 656 | Line 898 | void EtherInterrupt(void)
898                          }
899   #endif
900  
901 <                        // Get packet type
902 <                        uint16 type = ReadMacInt16(p + 12);
661 <
662 <                        // Look for protocol
663 <                        uint16 search_type = (type <= 1500 ? 0 : type);
664 <                        if (net_protocols.find(search_type) == net_protocols.end())
665 <                                continue;
666 <                        uint32 handler = net_protocols[search_type];
667 <
668 <                        // No default handler
669 <                        if (handler == 0)
670 <                                continue;
671 <
672 <                        // Copy header to RHA
673 <                        Mac2Mac_memcpy(ether_data + ed_RHA, p, 14);
674 <                        D(bug(" header %08x%04x %08x%04x %04x\n", ReadMacInt32(ether_data + ed_RHA), ReadMacInt16(ether_data + ed_RHA + 4), ReadMacInt32(ether_data + ed_RHA + 6), ReadMacInt16(ether_data + ed_RHA + 10), ReadMacInt16(ether_data + ed_RHA + 12)));
675 <
676 <                        // Call protocol handler
677 <                        M68kRegisters r;
678 <                        r.d[0] = type;                                                                  // Packet type
679 <                        r.d[1] = length - 14;                                                   // Remaining packet length (without header, for ReadPacket)
680 <                        r.a[0] = p + 14;                                                                // Pointer to packet (Mac address, for ReadPacket)
681 <                        r.a[3] = ether_data + ed_RHA + 14;                              // Pointer behind header in RHA
682 <                        r.a[4] = ether_data + ed_ReadPacket;                    // Pointer to ReadPacket/ReadRest routines
683 <                        D(bug(" calling protocol handler %08x, type %08x, length %08x, data %08x, rha %08x, read_packet %08x\n", handler, r.d[0], r.d[1], r.a[0], r.a[3], r.a[4]));
684 <                        Execute68k(handler, &r);
901 >                        // Dispatch packet
902 >                        ether_dispatch_packet(p, length);
903                  }
904          }
687
688        // Acknowledge interrupt to reception thread
689        D(bug(" EtherIRQ done\n"));
690        sem_post(&int_ack);
905   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines