diff --git a/0readme_ethernet.txt b/0readme_ethernet.txt index 4cc74fb3..9c8c7401 100644 --- a/0readme_ethernet.txt +++ b/0readme_ethernet.txt @@ -2,45 +2,125 @@ This file contains information about the SIMH Ethernet package. ------------------------------------------------------------------------------- -The XQ emulator is a host-independant software emulation of Digital's -DELQA (M7516) and DEQNA (M7504) Q-bus ethernet cards for the SIMH emulator. +The XQ emulator is a host-independent software emulation of Digital's +DELQA-T (M7516-YM), DELQA (M7516) and DEQNA (M7504) Q-bus Ethernet cards +for the SIMH emulator. -The XU emulator is a host-independant software emulation of Digital's DEUNA -(M7792/M7793) and DELUA (M7521) Unibus ethernet cards for the SIMH emulator. +The XU emulator is a host-independent software emulation of Digital's DEUNA +(M7792/M7793) and DELUA (M7521) Unibus Ethernet cards for the SIMH emulator. The XQ and XU simulators use the Sim_Ether module to execute host-specific -packet reads and writes, since all operating systems talk to real ethernet +packet reads and writes, since all operating systems talk to real Ethernet cards/controllers differently. See the comments at the top of sim_ether.c for the list of currently supported host platforms. -The Sim_Ether module sets the selected ethernet card into +The Sim_Ether module sets the selected Ethernet card into promiscuous mode to gather all packets, then filters out the packets that it doesn't want. In Windows, packets having the same source MAC address as the controller are ignored for WinPCAP compatibility (see Windows notes below). -If your ethernet card is plugged into a switch, the promiscuous mode setting +If your Ethernet card is plugged into a switch, the promiscuous mode setting should not cause much of a problem, since the switch will still filter out most of the undesirable traffic. You will only see "excessive" traffic if you are on a direct or hub(repeater) segment. -Using the libpcap/WinPcap interface, the simulated computer cannot "talk" to -the host computer via the selected interface, since the packets are not -reflected back to the host. The workaround for this is to use a second NIC in -the host and connect them both into the same network; then the host and the -simulator can communicate over the physical LAN. +On Windows using the WinPcap interface, the simulated computer can "talk" to +the host computer on the same interface. On other platforms with libpcap +(*nix), the simulated computer can not "talk" to the host computer via the +selected interface, since simulator transmitted packets are not received +by the host's network stack. The workaround for this is to use a second NIC +in the host and connect them both into the same network; then the host and +the simulator can communicate over the physical LAN. -Universal TUN/TAP support provides another solution for the above dual-NIC -problem for systems that support Universal TUN/TAP. Since the TUN/TAP interface -is at a different network level, the host can create a TAP device for the -simulator and then bridge or route packets between the TAP device and the real -network interface. Note that the TAP device and any bridging or routing must be -established before running the simulator; SIMH does not create, bridge, or -route TAP devices for you. +Integrated Universal TUN/TAP support provides another solution for the above +dual-NIC problem for systems that support Universal TUN/TAP. Since the TUN/TAP +interface is a pseudo network interface, the host can create a TAP device for +the simulator and then bridge or route packets between the TAP device and the +real network interface. Note that the TAP device and any bridging or routing +must be established before running the simulator; SIMH does not create, +bridge, or route TAP devices for you. + +Integrated Universal TUN/TAP support can be used for host<->simulator network +traffic (on the platforms where it is available) by using the SIMH command: +"attach xq tap:tapN" (i.e. attach xq tap:tap0). Platforms that this has been +tested on include: Linux, FreeBSD, OpenBSD, NetBSD. Each of these platforms +has some way to create a tap pseudo device and then bridge it with a physical +network interface. + +The following steps were performed to get a working SIMH vax simulator +sharing a physical NIC and allowing Host<->SIMH vax communications: + +Linux (Ubuntu 10.04): + apt-get install bridge-utils + apt-get install uml-utilities + + + #!/bin/sh + HOSTIP=`/sbin/ifconfig eth0 | grep "inet addr" | gawk -- '{ print $2 }' | gawk -F : -- '{ print $2 }'` + HOSTNETMASK=`/sbin/ifconfig eth0 | grep "inet addr" | gawk -- '{ print $4 }' | gawk -F : -- '{ print $2 }'` + HOSTBCASTADDR=`/sbin/ifconfig eth0 | grep "inet addr" | gawk -- '{ print $3 }' | gawk -F : -- '{ print $2 }'` + HOSTDEFAULTGATEWAY=`/sbin/route -n | grep ^0.0.0.0 | gawk -- '{ print $2 }'` + # + /usr/sbin/tunctl -t tap0 [-u someuser] + /sbin/ifconfig tap0 up + # + # Now convert eth0 to a bridge and bridge it with the TAP interface + /usr/sbin/brctl addbr br0 + /usr/sbin/brctl addif br0 eth0 + /usr/sbin/brctl setfd br0 0 + /sbin/ifconfig eth0 0.0.0.0 + /sbin/ifconfig br0 $HOSTIP netmask $HOSTNETMASK broadcast $HOSTBCASTADDR up + # set the default route to the br0 interface + /sbin/route add -net 0.0.0.0/0 gw $HOSTDEFAULTGATEWAY + # bridge in the tap device + /usr/sbin/brctl addif br0 tap0 + /sbin/ifconfig tap0 0.0.0.0 + + # Run simulator and "attach xq tap:tap0" + +OpenBSD (OpenBSD 4.6) + + /sbin/ifconfig tun0 create + /sbin/ifconfig tun0 link0 + /sbin/ifconfig tun0 up + + /sbin/ifconfig bridge0 create + /sbin/brconfig bridge0 fwddelay 4 + /sbin/brconfig bridge0 add em0 add tun0 # Change em0 to reflect your physical NIC name + /sbin/brconfig bridge0 up + + # Run simulator and "attach xq tap:tun0" + +FreeBSD (FreeBSD 8.0) + + /sbin/ifconfig tap0 create + /sbin/ifconfig tap0 up + + /sbin/ifconfig bridge0 create + /sbin/ifconfig bridge0 addm em0 addm tap0 # Change em0 to reflect your physical NIC name + /sbin/ifconfig bridge0 up + + # Run simulator and "attach xq tap:tap0" + # Note: it seems that on FreeBSD you may have to + # "/sbin/ifconfig tap0 up" and "/sbin/ifconfig bridge0 up" prior to each + # time simh "attach"es the tap:tap0 device + +NetBSD (NetBSD 5.0.2) + + /sbin/ifconfig tap0 create + /sbin/ifconfig tap0 up + + /sbin/ifconfig bridge0 create + /sbin/brconfig bridge0 fwddelay 1 + /sbin/brconfig bridge0 add wm0 add tap0 # Change wm0 to reflect your physical NIC name + /sbin/brconfig bridge0 up + + # Run simulator and "attach xq tap:tap0" ------------------------------------------------------------------------------- Windows notes: - 1. The Windows-specific code uses the WinPCAP 3.0 package from + 1. The Windows-specific code uses the WinPCAP 4.x package from http://www.winpcap.org. This package for windows simulates the libpcap package that is freely available for un*x systems. @@ -48,29 +128,27 @@ Windows notes: 3. The first time the WinPCAP driver is used, it will be dynamically loaded, and the user must be an Administrator on the machine to do so. If you need - to run as an unprivileged user, you must set the service to autostart. See - the WinPCAP documentation for details on the static loading workaround. - - 4. If you want to use TAP devices, they must be created before running SIMH. - (TAP component from the OpenVPN project; http://openvpn.sourceforge.net) - - 5. Compaq PATHWORKS 32 v7.2 also enabled bridging for the ethernet adapters - when the DECNET and LAT drivers were installed; TAP was not needed. + to run as an unprivileged user, you must set the "npf" driver to autostart. + Current WinPcap installers provide an option to configure this at + installation time. Building on Windows: - 1. Install WinPCAP 3.0 runtime and the WinPCAP Developer's kit. + 1. Install WinPCAP 4.x runtime and the WinPCAP Developer's kit. - 2. Put the required .h files (bittypes,devioctl,ip6_misc,packet32,pcap, - pcap-stdinc).h from the WinPCAP 3.0 developer's kit in the compiler's path + 2. Put the required .h files (bittypes,devioctl,ip6_misc,pcap,pcap-stdinc + packet32,ntddndis).h from the WinPCAP 4.x developer's kit in the + compiler's include file path - 3. Put the required .lib files (packet,wpcap).lib from the WinPCAP 3.0 - developer's kit in the linker's path + 3. Put the required .lib files (packet,wpcap).lib from the WinPCAP 4.x + developer's kit in the linker's library path 4. If you're using Borland C++, use COFF2OMF to convert the .lib files into a format that can be used by the compiler. - 5. Define USE_NETWORK. + 5. Define USE_NETWORK. The current windows network built binaries will + run on any system. regardless of whether or not WinPcap is installed, + and will provide Network functionality when WinPcap is available. 6. Build it! @@ -83,30 +161,33 @@ Linux, {Free|Net|Open}BSD, OS/X, and Un*x notes: Sim_Ether has been reworked to be more universal; because of this, you will need to get a version of libpcap that is 0.9 or greater. This can be downloaded from www.tcpdump.org - see the comments at the top of Sim_ether.c -for details. - -At the time of this release, the "Current Version" available at: -http://www.tcpdump.org/daily/libpcap-current.tar.gz is the -latest checked-in source code that is actually higher than the released -0.8.3 version number. Specifically, for all platforms, it contains code that -opens the ethernet device in Read/Write mode instead of the Read-Only mode -that previous libpcap versions for platforms which use one of pcap-bpf.c, -pcap-pf.c, or pcap-snit.c. This capabiligy now exists to support a newly -provided generic packet sending capability. +for details. ----- WARNING ----- WARNING ----- WARNING ----- WARNING ----- WARNING ----- 1. For all platforms, you must run SIMH(scp) with sufficient privilege to - allow the ethernet card can be set into promiscuous mode and to write - packets through the driver. For most Unix/Unix-like platforms this will - mean running as root. For systems which use bpf devices (NetBSD, - OpenBSD, FreeBSD and OS/X) it is possible to set permissions on the bpf - devices to allow read and write access to users other than root (For - example: chmod 666 /dev/bpf*). Doing this, has its own security issues. + allow the Ethernet card can be set into promiscuous mode and to write + packets through the driver. + a) For Windows systems this means having administrator privileges to + start the "npf" driver. The current WinPcap installer offers an + option to autostart the "npf" driver when the system boots. + b) For more recent Linux systems, The concepts leveraging "Filesystem + Capabilities" can be used to specifically grant the simh binary + the needed privileges to access the network. The article at: + http://packetlife.net/blog/2010/mar/19/sniffing-wireshark-non-root-user/ + describes how to do this for wireshark. The exact same capabilities + are needed by SIMH for network support. Use that article as a guide. + c) For Unix/Unix-like systems which use bpf devices (NetBSD, + OpenBSD, FreeBSD and OS/X) it is possible to set permissions on + the bpf devices to allow read and write access to users other + than root (For example: chmod 666 /dev/bpf*). Doing this, has + its own security issues. + d) For other platforms this will likely mean running as root. Additional alternative methods for avoiding the 'run as root' requirement will be welcomed. - 2. If you want to use TAP devices, they must be created before running SIMH. + 2. If you want to use TAP devices, and any surrounding system network/bridge + setup must be done before running SIMH. Building on Linux, {Free|Net|Open}BSD, OS/X, Un*x: @@ -116,12 +197,18 @@ Building on Linux, {Free|Net|Open}BSD, OS/X, Un*x: Linux : search for your variant on http://rpmfind.net OS/X : Apple Developer's site? - NOTE: These repositories will not likely contain a version - of libpcap greater than 0.8.1 for several years since - other packages in these repositories don't depend on a - later version than they currently have. + NOTE: These repositories for older versions of these platforms + don't contain a version of libpcap greater than 0.8.1. + However, most(all) recent releases of *nix environments ship + with sufficiently recent versions of libpcap either automatically + installed or available for installation as part of the + distribution. - 2. Use 'make USE_NETWORK=1' + 2. If you install the vendor supplied libpcap-dev package and it provides + a /usr/lib/libpcap.a file, then the existing makefile will automatically + use the vendor supplied library without any additional arguments. + If you have downloaded and built the libpcap from tcpdump.org, then + you can use it during a build by typing 'make USE_NETWORK=1' 3. Build it! @@ -132,27 +219,27 @@ OpenVMS Alpha notes: when required VCI promiscuous mode support was added. Hobbyists can get the required version of VMS from the OpenVMS Alpha Hobbyist Kit 3.0. - Running a simulator built with ethernet support on a version of VMS prior - to 7.3-1 will behave as if there is no ethernet support built in due to + Running a simulator built with Ethernet support on a version of VMS prior + to 7.3-1 will behave as if there is no Ethernet support built in due to the inability of the software to set the PCAPVCM into promiscuous mode. - An example display of fully functional ethernet support: + An example display of fully functional Ethernet support: sim> SHOW XQ ETH ETH devices: 0 we0 (VMS Device: _EWA0:) 1 we1 (VMS Device: _EWB0:) - An example display when the simulator was built without ethernet support + An example display when the simulator was built without Ethernet support or is not running the required version of VMS: sim> SHOW XQ ETH ETH devices: no network devices are available 2. You must place the PCAPVCM.EXE execlet in SYS$LOADABLE_IMAGES before - running a simulator with ethernet support. Note: This is done by the + running a simulator with Ethernet support. Note: This is done by the build commands in descrip.mms. - 3. You must have CMKRNL privilege to SHOW or ATTACH an ethernet device; + 3. You must have CMKRNL privilege to SHOW or ATTACH an Ethernet device; alternatively, you can INSTALL the simulator with CMKRNL privilege. 4. If you use a second adapter to communicate to the host, SOME protocol @@ -162,13 +249,13 @@ OpenVMS Alpha notes: Building on OpenVMS Alpha: The current descrip.mms file will build simulators capable of using - ethernet support with them automatically. These currently are: VAX, + Ethernet support with them automatically. These currently are: VAX, PDP11, and PDP10. The descrip.mms driven builds will also build the pcap library and build and install the VCI execlet. 1. Fetch the VMS-PCAP zip file from: http://simh.trailing-edge.com/sources/vms-pcap.zip - 2. Unzip it into the base of the simh distribution directory. + 2. Unzip it into the base of the SIMH distribution directory. 3. Build the simulator(s) with MMS or MMK: $ MMx {VAX,PDP11,PDP10, etc...} @@ -195,15 +282,15 @@ RSX11M+ system image that also contains DECNET, LAT, and/or TCP/IP software. ------------------------------------------------------------------------------- -How to debug problems with the ethernet subsystems: +How to debug problems with the Ethernet subsystems: PLEASE read the host-specific notes in sim_ether.c! While running SCP, the following commands can be used to enable debug messages: - scp> SET DEBUG STDERR - scp> SET XQ DEBUG={ETH|TRC|REG|WRN|CSR|VAR|SAN|SET|PCK} - scp> SET XU DEBUG={ETH|TRC|REG|WRN} + sim> SET DEBUG STDERR + sim> SET XQ DEBUG=TRACE;CSR;VAR;WARN;SETUP;SANITY;REG;PACKET;DATA;ETH + sim> SET XU DEBUG=ETH;TRACE;REG;WARN;PACKET;DATA Documentation of the functionality of these debug modifiers can be found in pdp11_xq.h and pdp11_xu.h. Inline debugging has replaced the previous #ifdef @@ -212,16 +299,13 @@ style of debugging, which required recompilation before debugging. ------------------------------------------------------------------------------- Things planned for future releases: - 1. PDP-11 bootstrap/bootrom - 2. Full MOP implementation - 3. DESQA support (if someone can get me the user manuals) - 4. DETQA support [DELQA-Turbo] (I have the manual) + 1. Full MOP implementation ------------------------------------------------------------------------------- Things which I need help with: 1. Information about Remote MOP processing - 2. VAX/PDP-11 hardware diagnotics image files and docs, to test XQ thoroughly. + 2. VAX/PDP-11 hardware diagnostics image files and docs, to test XQ thoroughly. 3. Feedback on operation with other VAX/PDP-11 OS's. ------------------------------------------------------------------------------- @@ -237,11 +321,160 @@ Dave Change Log =============================================================================== + 12-Jan-11 DTH Added SHOW XU FILTERS modifier + 11-Jan-11 DTH Corrected DEUNA/DELUA SELFTEST command, enabling use by + VMS 3.7, VMS 4.7, and Ultrix 1.1 + 09-Jan-11 MP Fixed missing crc data when USE_READER_THREAD is defined and + crc's are needed (only the pdp11_xu) + 16-Dec-10 MP added priority boost for read and write threads when + USE_READER_THREAD does I/O in separate threads. This helps + throughput since it allows these I/O bound threads to preempt + the main thread (which is executing simulated instructions). + 09-Dec-10 MP allowed more flexible parsing of MAC address strings + 09-Dec-10 MP Added support to determine if network address conflicts exist + 07-Dec-10 MP Reworked DECnet self detection to the more general approach + of loopback self when any Physical Address is being set. + 06-Dec-10 MP Added loopback processing support to pdp11_xu.c + 06-Dec-10 MP Fixed loopback processing to correctly handle forward packets. + 04-Dec-10 MP Changed eth_write to do nonblocking writes when + USE_READER_THREAD is defined. + 30-Nov-10 MP Fixed the fact that no broadcast packets were received by the DEUNA + 29-Nov-10 MP Fixed interrupt dispatch issue which caused delivered packets + (in and out) to sometimes not interrupt the CPU after processing. + 17-Jun-10 MP Fixed bug in the AUTODIN II hash filtering. + 14-Jun-10 MP Added support for integrated Tap networking interfaces on BSD + platforms. + 13-Jun-10 MP Added support for integrated Tap networking interfaces on Linux + platforms. + 31-May-10 MP Added support for more TOE (TCP Offload Engine) features for IPv4 + network traffic from the host and/or from hosts on the LAN. These + new TOE features are: LSO (Large Send Offload) and Jumbo packet + fragmentation support. These features allow a simulated network + device to support traffic when a host leverages a NIC's Large + Send Offload capabilities to fragment and/or segment outgoing + network traffic. Additionally a simulated network device can + reasonably exist on a LAN which is configured to use Jumbo frames. + 21-May-10 MP Added functionality to fix up IP header checksums to accommodate + packets from a host with a NIC which has TOE (TCP Offload Engine) + enabled which is expected to implement the checksum computations + in hardware. Since we catch packets before they arrive at the + NIC the expected checksum insertions haven't been performed yet. + This processing is only done for packets sent from the host to + the guest we're supporting. In general this will be a relatively + small number of packets so it is done for all IP frame packets + coming from the host to the guest. In order to make the + determination of packets specifically arriving from the host we + need to know the hardware MAC address of the host NIC. Currently + determining a NIC's MAC address is relatively easy on Windows. + The non-windows code works on linux and may work on other *nix + platforms either as is or with slight modifications. The code, + as implemented, only messes with this activity if the host + interface MAC address can be determined. + 20-May-10 MP Added general support to deal with receiving packets smaller + than ETH_MIN_PACKET in length. These come from packets + looped back by some bridging mechanism and need to be padded + to the minimum frame size. A real NIC won't pass us any + packets like that. This fix belongs here since this layer + is responsible for interfacing to the physical layer + devices, AND it belongs here to get CRC processing right. + 15-Aug-08 MP Fixed transmitted packets to have the correct source MAC address. + Fixed incorrect address filter setting calling eth_filter(). + 07-Mar-08 MP Fixed the SCP visible SA registers to always display the + ROM MAC address, even after it is changed by SET XQ MAC=. + 07-Mar-08 MP Added changes so that the Console DELQA diagnostic (>>>TEST 82) + will succeed. + 03-Mar-08 MP Added DELQA-T (aka DELQA Plus) device emulation support. + 06-Feb-08 MP Added dropped frame statistics to record when the receiver discards + received packets due to the receiver being disabled, or due to the + XQ device's packet receive queue being full. + Fixed bug in receive processing when we're not polling. This could + cause receive processing to never be activated again if we don't + read all available packets via eth_read each time we get the + opportunity. + 31-Jan-08 MP Added the ability to Coalesce received packet interrupts. This + is enabled by SET XQ POLL=DELAY=nnn where nnn is a number of + microseconds to delay the triggering of an interrupt when a packet + is received. + 29-Jan-08 MP Added SET XQ POLL=DISABLE (aka SET XQ POLL=0) to operate without + polling for packet read completion. + 29-Jan-08 MP Changed the sanity and id timer mechanisms to use a separate timer + unit so that transmit and receive activities can be dealt with + by the normal xq_svc routine. + Dynamically determine the timer polling rate based on the + calibrated tmr_poll and clk_tps values of the simulator. + 25-Jan-08 MP Enabled the SET XQ POLL to be meaningful if the simulator currently + doesn't support idling. + 25-Jan-08 MP Changed xq_debug_setup to use sim_debug instead of printf so that + all debug output goes to the same place. + 25-Jan-08 MP Restored the call to xq_svc after all successful calls to eth_write + to allow receive processing to happen before the next event + service time. This must have been inadvertently commented out + while other things were being tested. + 23-Jan-08 MP Added debugging support to display packet headers and packet data + 18-Jun-07 RMS Added UNIT_IDLE flag + 29-Oct-06 RMS Synced poll and clock + 27-Jan-06 RMS Fixed unaligned accesses in XQB (found by Doug Carman) + 07-Jan-06 RMS Fixed unaligned access bugs (found by Doug Carman) + 07-Sep-05 DTH Removed unused variable + 16-Aug-05 RMS Fixed C++ declaration and cast problems + + 05-Mar-08 MP Added optional multicast filtering support for doing + LANCE style AUTODIN II based hashed filtering. + 07-Feb-08 MP Added eth_show_dev to display Ethernet state + Changed the return value from eth_read to return whether + or not a packet was read. No existing callers used or + checked constant return value that previously was being + supplied. + 29-Jan-08 MP Added eth_set_async to provide a mechanism (when + USE_READER_THREAD is enabled) to allow packet reception + to dynamically update the simulator event queue and + potentially avoid polling for I/O. This provides a minimal + overhead (no polling) maximal responsiveness for network + activities. + 29-Jan-08 MP Properly sequenced activities in eth_close to avoid a race + condition when USE_READER_THREAD is enabled. + 25-Jan-08 MP Changed the following when USE_READER_THREAD is enabled: + - Fixed bug when the simulated device doesn't need crc + in packet data which is read. + - Added call to pcap_setmintocopy to minimize packet + delivery latencies. + - Added ethq_destroy and used it to avoid a memory leak in + eth_close. + - Properly cleaned up pthread mutexes in eth_close. + Migrated to using sim_os_ms_sleep for a delay instead of + a call to select(). + Fixed the bpf filter used when no traffic is to be matched. + Reworked eth_add_packet_crc32 implementation to avoid an + extra buffer copy while reading packets. + Fixed up #ifdef's relating to USE_SHARED so that setting + USE_SHARED or USE_NETWORK will build a working network + environment. + 23-Jan-08 MP Reworked eth_packet_trace and eth_packet_trace_ex to allow + only output Ethernet header+crc and provide a mechanism for + the simulated device to display full packet data debugging. + 17-May-07 DTH Fixed non-Ethernet device removal loop (from Naoki Hamada) + 15-May-07 DTH Added dynamic loading of wpcap.dll; + Corrected exceed max index bug in ethX lookup + 04-May-07 DTH Corrected failure to look up Ethernet device names in + the registry on Windows XP x64 + 10-Jul-06 RMS Fixed linux conditionalization (from Chaskiel Grundman) + 02-Jun-06 JDB Fixed compiler warning for incompatible sscanf parameter + 15-Dec-05 DTH Patched eth_host_devices [remove non-Ethernet devices] + (from Mark Pizzolato and Galen Tackett, 08-Jun-05) + Patched eth_open [tun fix](from Antal Ritter, 06-Oct-05) + 30-Nov-05 DTH Added option to regenerate CRC on received packets; some + Ethernet devices need to pass it on to the simulation, and by + the time libpcap/winpcap gets the packet, the host OS network + layer has already stripped CRC out of the packet + 01-Dec-04 DTH Added Windows user-defined adapter names (from Timothe Litt) + + + 19-Mar-04 Release: 1. Genericized Sim_Ether code, reduced #ifdefs (David Hittner) 2. Further refinement of sim_ether, qualified more platforms (Mark Pizzolato) 3. Added XU module (David Hittner) - 4. Corrected XQ interrupt signalling for PDP11s (David Hittner) + 4. Corrected XQ interrupt signaling for PDP11s (David Hittner) 5. Added inline debugging support (David Hittner) ------------------------------------------------------------------------------- @@ -251,7 +484,7 @@ Dave 2. Added DECNET duplicate detection for Windows (Mark Pizzolato) 3. Added BPF filtering to increase efficiency (Mark Pizzolato) 4. Corrected XQ Runt processing (Mark Pizzolato) - 5. Corrected XQ Sofware Reset (Mark Pizzolato) + 5. Corrected XQ Software Reset (Mark Pizzolato) 6. Corrected XQ Multicast/Promiscuous mode setting/resetting (Mark Pizzolato) 7. Added Universal TUN/TAP support (Mark Pizzolato) 8. Added FreeBSD support (Edward Brocklesby) @@ -271,7 +504,7 @@ Dave 1. Corrected bug in xq_setmac introduced in v3.0 (multiple people) 2. Made XQ rcv buffer allocation dynamic to reduce scp size (David Hittner) 3. Optimized some structs, removed legacy variables (Mark Pizzolato) - 4. Changed #ifdef WIN32 to _WIN32 for consistancy (Mark Pizzolato) + 4. Changed #ifdef WIN32 to _WIN32 for consistency (Mark Pizzolato) ------------------------------------------------------------------------------- @@ -308,7 +541,7 @@ Dave 08-Nov-02 Release: 1. Added USE_NETWORK conditional to Sim_Ether - 2. Fixed behaviour of SHOW XQ ETH if no devices exist + 2. Fixed behavior of SHOW XQ ETH if no devices exist 3. Added OpenBSD support to Sim_Ether (courtesy of Federico Schwindt) 4. Added ethX detection simplification (from Megan Gentry) diff --git a/PDP11/pdp11_xq.c b/PDP11/pdp11_xq.c index 06ef780f..89591bad 100644 --- a/PDP11/pdp11_xq.c +++ b/PDP11/pdp11_xq.c @@ -26,11 +26,12 @@ ------------------------------------------------------------------------------ - This DEQNA/DELQA simulation is based on: + This DEQNA/DELQA/DELQA-T simulation is based on: Digital DELQA Users Guide, Part# EK-DELQA-UG-002 Digital DEQNA Users Guide, Part# EK-DEQNA-UG-001 + Digital DELQA-Plus Addendum to DELQA Users Guide, Part# EK-DELQP-UG-001_Sep89.pdf These manuals can be found online at: - http://www.spies.com/~aek/pdf/dec/qbus + http://www.bitsavers.org/pdf/dec/qbus Certain adaptations have been made because this is an emulation: Ethernet transceiver power flag CSR<12> is ON when attached. @@ -58,6 +59,7 @@ 5. VMS LAT - SET HOST/LAT tests 6. VMS Cluster - SHOW CLUSTER, SHOW DEVICE, and cluster COPY tests 7. Console boot into VMSCluster (>>>B XQAO) + 8. Console DELQA Diagnostic (>>>TEST 82) PDP11: 1. RT-11 v5.3 - FTPSB copy test 2. RSTS/E v10.1 - detects/enables device @@ -66,6 +68,42 @@ Modification history: + 09-Dec-10 MP Added address conflict check during attach. + 06-Dec-10 MP Fixed loopback processing to correctly handle forward packets. + 29-Nov-10 MP Fixed interrupt dispatch issue which caused delivered packets + (in and out) to sometimes not interrupt the CPU after processing. + 07-Mar-08 MP Fixed the SCP visibile SA registers to always display the + ROM mac address, even after it is changed by SET XQ MAC=. + 07-Mar-08 MP Added changes so that the Console DELQA diagnostic (>>>TEST 82) + will succeed. + 03-Mar-08 MP Added DELQA-T (aka DELQA Plus) device emulation support. + 06-Feb-08 MP Added dropped frame statistics to record when the receiver discards + received packets due to the receiver being disabled, or due to the + XQ device's packet receive queue being full. + Fixed bug in receive processing when we're not polling. This could + cause receive processing to never be activated again if we don't + read all available packets via eth_read each time we get the + opportunity. + 31-Jan-08 MP Added the ability to Coalesce received packet interrupts. This + is enabled by SET XQ POLL=DELAY=nnn where nnn is a number of + microseconds to delay the triggering of an interrupt when a packet + is received. + 29-Jan-08 MP Added SET XQ POLL=DISABLE (aka SET XQ POLL=0) to operate without + polling for packet read completion. + 29-Jan-08 MP Changed the sanity and id timer mechanisms to use a separate timer + unit so that transmit and recieve activities can be dealt with + by the normal xq_svc routine. + Dynamically determine the timer polling rate based on the + calibrated tmr_poll and clk_tps values of the simulator. + 25-Jan-08 MP Enabled the SET XQ POLL to be meaningful if the simulator currently + doesn't support idling. + 25-Jan-08 MP Changed xq_debug_setup to use sim_debug instead of printf so that + all debug output goes to the same place. + 25-Jan-08 MP Restored the call to xq_svc after all successful calls to eth_write + to allow receive processing to happen before the next event + service time. This must have been inadvertently commented out + while other things were being tested. + 23-Jan-08 MP Added debugging support to display packet headers and packet data 18-Jun-07 RMS Added UNIT_IDLE flag 29-Oct-06 RMS Synced poll and clock 27-Jan-06 RMS Fixed unaligned accesses in XQB (found by Doug Carman) @@ -90,7 +128,7 @@ device in promiscuous or all multicast mode once it ever had been there. - Fixed output format in show_xq_sanity to end in "\n" - - Added display of All Multicase and promiscuous to + - Added display of All Multicast and promiscuous to xq_show_filters - The stuck in All Multicast or Promiscuous issue is worse than previously thought. See comments in @@ -99,7 +137,7 @@ separator character, since sim_ether's eth_mac_fmt formats them with this separator character. - Changed xq_sw_reset to behave more like the set of - actions described in Table 3-6 of the DELQA manua. + actions described in Table 3-6 of the DELQA manual. The manual mentions "N/A" which I'm interpreting to mean "Not Affected". 05-Jun-03 DTH Added receive packet splitting @@ -212,13 +250,17 @@ #include "pdp11_xq_bootrom.h" extern int32 tmxr_poll; +extern int32 tmr_poll, clk_tps; +extern t_bool sim_idle_enab; extern FILE* sim_deb; +extern FILE *sim_log; extern char* read_line (char *ptr, int32 size, FILE *stream); /* forward declarations */ t_stat xq_rd(int32* data, int32 PA, int32 access); t_stat xq_wr(int32 data, int32 PA, int32 access); t_stat xq_svc(UNIT * uptr); +t_stat xq_tmrsvc(UNIT * uptr); t_stat xq_reset (DEVICE * dptr); t_stat xq_attach (UNIT * uptr, char * cptr); t_stat xq_detach (UNIT * uptr); @@ -235,7 +277,10 @@ t_stat xq_show_poll (FILE* st, UNIT* uptr, int32 val, void* desc); t_stat xq_set_poll (UNIT* uptr, int32 val, char* cptr, void* desc); t_stat xq_process_xbdl(CTLR* xq); t_stat xq_dispatch_xbdl(CTLR* xq); -void xq_start_receiver(void); +t_stat xq_process_turbo_rbdl(CTLR* xq); +t_stat xq_process_turbo_xbdl(CTLR* xq); +void xq_start_receiver(CTLR* xq); +void xq_stop_receiver(CTLR* xq); void xq_sw_reset(CTLR* xq); t_stat xq_ex (t_value *vptr, t_addr addr, UNIT *uptr, int32 sw); t_stat xq_dep (t_value val, t_addr addr, UNIT *uptr, int32 sw); @@ -255,8 +300,10 @@ struct xq_device xqa = { xqa_read_callback, /* read callback routine */ xqa_write_callback, /* write callback routine */ {0x08, 0x00, 0x2B, 0xAA, 0xBB, 0xCC}, /* mac */ - XQ_T_DELQA, /* type */ + XQ_T_DELQA_PLUS, /* type */ + XQ_T_DELQA, /* mode */ XQ_SERVICE_INTERVAL, /* poll */ + 0, 0, /* coalesce */ {0} /* sanity */ }; @@ -264,8 +311,10 @@ struct xq_device xqb = { xqb_read_callback, /* read callback routine */ xqb_write_callback, /* write callback routine */ {0x08, 0x00, 0x2B, 0xBB, 0xCC, 0xDD}, /* mac */ - XQ_T_DELQA, /* type */ + XQ_T_DELQA_PLUS, /* type */ + XQ_T_DELQA, /* mode */ XQ_SERVICE_INTERVAL, /* poll */ + 0, 0, /* coalesce */ {0} /* sanity */ }; @@ -275,15 +324,18 @@ DIB xqa_dib = { IOBA_XQ, IOLN_XQ, &xq_rd, &xq_wr, UNIT xqa_unit[] = { { UDATA (&xq_svc, UNIT_IDLE|UNIT_ATTABLE|UNIT_DISABLE, 2047) }, /* receive timer */ + { UDATA (&xq_tmrsvc, UNIT_IDLE|UNIT_DIS, 0) }, }; REG xqa_reg[] = { - { GRDATA ( SA0, xqa.addr[0], XQ_RDX, 8, 0), REG_RO|REG_FIT}, - { GRDATA ( SA1, xqa.addr[1], XQ_RDX, 8, 0), REG_RO|REG_FIT}, - { GRDATA ( SA2, xqa.addr[2], XQ_RDX, 8, 0), REG_RO|REG_FIT}, - { GRDATA ( SA3, xqa.addr[3], XQ_RDX, 8, 0), REG_RO|REG_FIT}, - { GRDATA ( SA4, xqa.addr[4], XQ_RDX, 8, 0), REG_RO|REG_FIT}, - { GRDATA ( SA5, xqa.addr[5], XQ_RDX, 8, 0), REG_RO|REG_FIT}, + { GRDATA ( SA0, xqa.mac[0], XQ_RDX, 8, 0), REG_RO|REG_FIT}, + { GRDATA ( SA1, xqa.mac[1], XQ_RDX, 8, 0), REG_RO|REG_FIT}, + { GRDATA ( SA2, xqa.mac[2], XQ_RDX, 8, 0), REG_RO|REG_FIT}, + { GRDATA ( SA3, xqa.mac[3], XQ_RDX, 8, 0), REG_RO|REG_FIT}, + { GRDATA ( SA4, xqa.mac[4], XQ_RDX, 8, 0), REG_RO|REG_FIT}, + { GRDATA ( SA5, xqa.mac[5], XQ_RDX, 8, 0), REG_RO|REG_FIT}, + { GRDATA ( MX0, xqa.mac_checksum[0], XQ_RDX, 8, 0), REG_RO|REG_FIT}, + { GRDATA ( MX1, xqa.mac_checksum[1], XQ_RDX, 8, 0), REG_RO|REG_FIT}, { GRDATA ( RBDL, xqa.rbdl[0], XQ_RDX, 16, 0), REG_FIT }, { GRDATA ( RBDH, xqa.rbdl[1], XQ_RDX, 16, 0), REG_FIT }, { GRDATA ( XBDL, xqa.xbdl[0], XQ_RDX, 16, 0), REG_FIT }, @@ -291,6 +343,13 @@ REG xqa_reg[] = { { GRDATA ( VAR, xqa.var, XQ_RDX, 16, 0), REG_FIT }, { GRDATA ( CSR, xqa.csr, XQ_RDX, 16, 0), REG_FIT }, { FLDATA ( INT, xqa.irq, 0) }, + { GRDATA ( TYPE, xqa.type, XQ_RDX, 32, 0), REG_FIT }, + { GRDATA ( MODE, xqa.mode, XQ_RDX, 32, 0), REG_FIT }, + { GRDATA ( POLL, xqa.poll, XQ_RDX, 16, 0), REG_HRO}, + { GRDATA ( CLAT, xqa.coalesce_latency, XQ_RDX, 16, 0), REG_HRO}, + { GRDATA ( CLATT, xqa.coalesce_latency_ticks, XQ_RDX, 16, 0), REG_HRO}, + { GRDATA ( RBDL_BA, xqa.rbdl_ba, XQ_RDX, 32, 0), REG_HRO}, + { GRDATA ( XBDL_BA, xqa.xbdl_ba, XQ_RDX, 32, 0), REG_HRO}, { GRDATA ( SETUP_PRM, xqa.setup.promiscuous, XQ_RDX, 32, 0), REG_HRO}, { GRDATA ( SETUP_MLT, xqa.setup.multicast, XQ_RDX, 32, 0), REG_HRO}, { GRDATA ( SETUP_L1, xqa.setup.l1, XQ_RDX, 32, 0), REG_HRO}, @@ -298,6 +357,17 @@ REG xqa_reg[] = { { GRDATA ( SETUP_L3, xqa.setup.l3, XQ_RDX, 32, 0), REG_HRO}, { GRDATA ( SETUP_SAN, xqa.setup.sanity_timer, XQ_RDX, 32, 0), REG_HRO}, { BRDATA ( SETUP_MACS, &xqa.setup.macs, XQ_RDX, 8, sizeof(xqa.setup.macs)), REG_HRO}, + { BRDATA ( STATS, &xqa.stats, XQ_RDX, 8, sizeof(xqa.setup.macs)), REG_HRO}, + { BRDATA ( TURBO_INIT, &xqa.init, XQ_RDX, 8, sizeof(xqa.init)), REG_HRO}, + { GRDATA ( SRR, xqa.srr, XQ_RDX, 16, 0), REG_FIT }, + { GRDATA ( SRQR, xqa.srqr, XQ_RDX, 16, 0), REG_FIT }, + { GRDATA ( IBA, xqa.iba, XQ_RDX, 32, 0), REG_FIT }, + { GRDATA ( ICR, xqa.icr, XQ_RDX, 16, 0), REG_FIT }, + { GRDATA ( IPEND, xqa.pending_interrupt, XQ_RDX, 16, 0), REG_FIT }, + { GRDATA ( TBINDX, xqa.tbindx, XQ_RDX, 32, 0), REG_HRO}, + { GRDATA ( RBINDX, xqa.rbindx, XQ_RDX, 32, 0), REG_HRO}, + { GRDATA ( IDTMR, xqa.idtmr, XQ_RDX, 32, 0), REG_HRO}, + { GRDATA ( MUST_POLL, xqa.must_poll, XQ_RDX, 32, 0), REG_HRO}, { NULL }, }; @@ -306,15 +376,18 @@ DIB xqb_dib = { IOBA_XQB, IOLN_XQB, &xq_rd, &xq_wr, UNIT xqb_unit[] = { { UDATA (&xq_svc, UNIT_IDLE|UNIT_ATTABLE|UNIT_DISABLE, 2047) }, /* receive timer */ + { UDATA (&xq_tmrsvc, UNIT_IDLE|UNIT_DIS, 0) }, }; REG xqb_reg[] = { - { GRDATA ( SA0, xqb.addr[0], XQ_RDX, 8, 0), REG_RO|REG_FIT}, - { GRDATA ( SA1, xqb.addr[1], XQ_RDX, 8, 0), REG_RO|REG_FIT}, - { GRDATA ( SA2, xqb.addr[2], XQ_RDX, 8, 0), REG_RO|REG_FIT}, - { GRDATA ( SA3, xqb.addr[3], XQ_RDX, 8, 0), REG_RO|REG_FIT}, - { GRDATA ( SA4, xqb.addr[4], XQ_RDX, 8, 0), REG_RO|REG_FIT}, - { GRDATA ( SA5, xqb.addr[5], XQ_RDX, 8, 0), REG_RO|REG_FIT}, + { GRDATA ( SA0, xqb.mac[0], XQ_RDX, 8, 0), REG_RO|REG_FIT}, + { GRDATA ( SA1, xqb.mac[1], XQ_RDX, 8, 0), REG_RO|REG_FIT}, + { GRDATA ( SA2, xqb.mac[2], XQ_RDX, 8, 0), REG_RO|REG_FIT}, + { GRDATA ( SA3, xqb.mac[3], XQ_RDX, 8, 0), REG_RO|REG_FIT}, + { GRDATA ( SA4, xqb.mac[4], XQ_RDX, 8, 0), REG_RO|REG_FIT}, + { GRDATA ( SA5, xqb.mac[5], XQ_RDX, 8, 0), REG_RO|REG_FIT}, + { GRDATA ( MX0, xqb.mac_checksum[0], XQ_RDX, 8, 0), REG_RO|REG_FIT}, + { GRDATA ( MX1, xqb.mac_checksum[1], XQ_RDX, 8, 0), REG_RO|REG_FIT}, { GRDATA ( RBDL, xqb.rbdl[0], XQ_RDX, 16, 0), REG_FIT }, { GRDATA ( RBDH, xqb.rbdl[1], XQ_RDX, 16, 0), REG_FIT }, { GRDATA ( XBDL, xqb.xbdl[0], XQ_RDX, 16, 0), REG_FIT }, @@ -322,6 +395,13 @@ REG xqb_reg[] = { { GRDATA ( VAR, xqb.var, XQ_RDX, 16, 0), REG_FIT }, { GRDATA ( CSR, xqb.csr, XQ_RDX, 16, 0), REG_FIT }, { FLDATA ( INT, xqb.irq, 0) }, + { GRDATA ( TYPE, xqb.type, XQ_RDX, 32, 0), REG_FIT }, + { GRDATA ( MODE, xqb.mode, XQ_RDX, 32, 0), REG_FIT }, + { GRDATA ( POLL, xqb.poll, XQ_RDX, 16, 0), REG_HRO}, + { GRDATA ( CLAT, xqb.coalesce_latency, XQ_RDX, 16, 0), REG_HRO}, + { GRDATA ( CLATT, xqb.coalesce_latency_ticks, XQ_RDX, 16, 0), REG_HRO}, + { GRDATA ( RBDL_BA, xqb.rbdl_ba, XQ_RDX, 32, 0), REG_HRO}, + { GRDATA ( XBDL_BA, xqb.xbdl_ba, XQ_RDX, 32, 0), REG_HRO}, { GRDATA ( SETUP_PRM, xqb.setup.promiscuous, XQ_RDX, 32, 0), REG_HRO}, { GRDATA ( SETUP_MLT, xqb.setup.multicast, XQ_RDX, 32, 0), REG_HRO}, { GRDATA ( SETUP_L1, xqb.setup.l1, XQ_RDX, 32, 0), REG_HRO}, @@ -329,6 +409,17 @@ REG xqb_reg[] = { { GRDATA ( SETUP_L3, xqb.setup.l3, XQ_RDX, 32, 0), REG_HRO}, { GRDATA ( SETUP_SAN, xqb.setup.sanity_timer, XQ_RDX, 32, 0), REG_HRO}, { BRDATA ( SETUP_MACS, &xqb.setup.macs, XQ_RDX, 8, sizeof(xqb.setup.macs)), REG_HRO}, + { BRDATA ( STATS, &xqb.stats, XQ_RDX, 8, sizeof(xqa.setup.macs)), REG_HRO}, + { BRDATA ( TURBO_INIT, &xqb.init, XQ_RDX, 8, sizeof(xqb.init)), REG_HRO}, + { GRDATA ( SRR, xqb.srr, XQ_RDX, 16, 0), REG_FIT }, + { GRDATA ( SRQR, xqb.srqr, XQ_RDX, 16, 0), REG_FIT }, + { GRDATA ( IBA, xqb.iba, XQ_RDX, 32, 0), REG_FIT }, + { GRDATA ( ICR, xqb.icr, XQ_RDX, 16, 0), REG_FIT }, + { GRDATA ( IPEND, xqb.pending_interrupt, XQ_RDX, 16, 0), REG_FIT }, + { GRDATA ( TBINDX, xqb.tbindx, XQ_RDX, 32, 0), REG_HRO}, + { GRDATA ( RBINDX, xqb.rbindx, XQ_RDX, 32, 0), REG_HRO}, + { GRDATA ( IDTMR, xqb.idtmr, XQ_RDX, 32, 0), REG_HRO}, + { GRDATA ( MUST_POLL, xqb.must_poll, XQ_RDX, 32, 0), REG_HRO}, { NULL }, }; @@ -345,10 +436,15 @@ MTAB xq_mod[] = { NULL, &xq_show_filters, NULL }, { MTAB_XTD | MTAB_VDV | MTAB_NMO, 0, "STATS", "STATS", &xq_set_stats, &xq_show_stats, NULL }, - { MTAB_XTD | MTAB_VDV, 0, "TYPE", "TYPE={DEQNA|DELQA}", + { MTAB_XTD | MTAB_VDV, 0, "TYPE", "TYPE={DEQNA|DELQA|DELQA-T}", &xq_set_type, &xq_show_type, NULL }, - { MTAB_XTD | MTAB_VDV, 0, "POLL", "POLL={DEFAULT|4..2500]", +#ifdef USE_READER_THREAD + { MTAB_XTD | MTAB_VDV, 0, "POLL", "POLL={DEFAULT|DISABLED|4..2500|DELAY=nnn}", &xq_set_poll, &xq_show_poll, NULL }, +#else + { MTAB_XTD | MTAB_VDV, 0, "POLL", "POLL={DEFAULT|DISABLED|4..2500}", + &xq_set_poll, &xq_show_poll, NULL }, +#endif { MTAB_XTD | MTAB_VDV | MTAB_NMO, 0, "SANITY", "SANITY={ON|OFF}", &xq_set_sanity, &xq_show_sanity, NULL }, { 0 }, @@ -363,13 +459,14 @@ DEBTAB xq_debug[] = { {"SANITY", DBG_SAN}, {"REG", DBG_REG}, {"PACKET", DBG_PCK}, + {"DATA", DBG_DAT}, {"ETH", DBG_ETH}, {0} }; DEVICE xq_dev = { "XQ", xqa_unit, xqa_reg, xq_mod, - 1, XQ_RDX, 11, 1, XQ_RDX, 16, + 2, XQ_RDX, 11, 1, XQ_RDX, 16, &xq_ex, &xq_dep, &xq_reset, NULL, &xq_attach, &xq_detach, &xqa_dib, DEV_DISABLE | DEV_QBUS | DEV_DEBUG, @@ -378,7 +475,7 @@ DEVICE xq_dev = { DEVICE xqb_dev = { "XQB", xqb_unit, xqb_reg, xq_mod, - 1, XQ_RDX, 11, 1, XQ_RDX, 16, + 2, XQ_RDX, 11, 1, XQ_RDX, 16, &xq_ex, &xq_dep, &xq_reset, NULL, &xq_attach, &xq_detach, &xqb_dib, DEV_DISABLE | DEV_DIS | DEV_QBUS | DEV_DEBUG, @@ -394,8 +491,16 @@ const char* const xq_recv_regnames[] = { "MAC0", "MAC1", "MAC2", "MAC3", "MAC4", "MAC5", "VAR", "CSR" }; +const char* const xqt_recv_regnames[] = { + "MAC0", "MAC1", "MAC2", "MAC3", "MAC4", "MAC5", "SRR", "" +}; + const char* const xq_xmit_regnames[] = { - "", "", "RBDL-Lo", "RBDL-Hi", "XBDL-Lo", "XBDL-Hi", "VAR", "CSR" + "XCR0", "XCR1", "RBDL-Lo", "RBDL-Hi", "XBDL-Lo", "XBDL-Hi", "VAR", "CSR" +}; + +const char* const xqt_xmit_regnames[] = { + "IBAL", "IBAH", "ICR", "", "SRQR", "", "", "ARQR" }; const char* const xq_csr_bits[] = { @@ -408,8 +513,14 @@ const char* const xq_var_bits[] = { "V6", "V7", "S1", "S2", "S3", "RS", "OS", "MS" }; +const char* const xq_srr_bits[] = { + "RS0", "RS1", "", "", "", "", "", "", + "", "TBL", "IME", "PAR", "NXM", "", "CHN", "FES" +}; + /* internal debugging routines */ void xq_debug_setup(CTLR* xq); +void xq_debug_turbo_setup(CTLR* xq); /*============================================================================*/ @@ -543,16 +654,19 @@ t_stat xq_show_stats (FILE* st, UNIT* uptr, int32 val, void* desc) char* fmt = " %-15s%d\n"; CTLR* xq = xq_unit2ctlr(uptr); - fprintf(st, "Ethernet statistics:\n"); - fprintf(st, fmt, "Recv:", xq->var->stats.recv); - fprintf(st, fmt, "Filtered:", xq->var->stats.filter); - fprintf(st, fmt, "Xmit:", xq->var->stats.xmit); - fprintf(st, fmt, "Xmit Fail:", xq->var->stats.fail); - fprintf(st, fmt, "Runts:", xq->var->stats.runt); - fprintf(st, fmt, "Oversize:", xq->var->stats.giant); - fprintf(st, fmt, "Setup:", xq->var->stats.setup); - fprintf(st, fmt, "Loopback:", xq->var->stats.loop); - fprintf(st, fmt, "ReadQ high:", xq->var->ReadQ.high); + fprintf(st, "XQ Ethernet statistics:\n"); + fprintf(st, fmt, "Recv:", xq->var->stats.recv); + fprintf(st, fmt, "Dropped:", xq->var->stats.dropped + xq->var->ReadQ.loss); + fprintf(st, fmt, "Xmit:", xq->var->stats.xmit); + fprintf(st, fmt, "Xmit Fail:", xq->var->stats.fail); + fprintf(st, fmt, "Runts:", xq->var->stats.runt); + fprintf(st, fmt, "Oversize:", xq->var->stats.giant); + fprintf(st, fmt, "SW Reset:", xq->var->stats.reset); + fprintf(st, fmt, "Setup:", xq->var->stats.setup); + fprintf(st, fmt, "Loopback:", xq->var->stats.loop); + fprintf(st, fmt, "ReadQ count:", xq->var->ReadQ.count); + fprintf(st, fmt, "ReadQ high:", xq->var->ReadQ.high); + eth_show_dev(st, xq->var->etherface); return SCPE_OK; } @@ -562,15 +676,28 @@ t_stat xq_show_filters (FILE* st, UNIT* uptr, int32 val, void* desc) char buffer[20]; int i; - fprintf(st, "Filters:\n"); - for (i=0; ivar->setup.macs[i], buffer); - fprintf(st, " [%2d]: %s\n", i, buffer); - }; - if (xq->var->setup.multicast) - fprintf(st, "All Multicast Receive Mode\n"); - if (xq->var->setup.promiscuous) - fprintf(st, "Promiscuous Receive Mode\n"); + if (xq->var->mode == XQ_T_DELQA_PLUS) { + eth_mac_fmt(&xq->var->init.phys, buffer); + fprintf(st, "Physical Address=%s\n", buffer); + if (xq->var->etherface->hash_filter) { + fprintf(st, "Multicast Hash: "); + for (i=0; ivar->etherface->hash); ++i) + fprintf(st, "%02X ", xq->var->etherface->hash[i]); + fprintf(st, "\n"); + } + if (xq->var->init.mode & XQ_IN_MO_PRO) + fprintf(st, "Promiscuous Receive Mode\n"); + } else { + fprintf(st, "Filters:\n"); + for (i=0; ivar->setup.macs[i], buffer); + fprintf(st, " [%2d]: %s\n", i, buffer); + } + if (xq->var->setup.multicast) + fprintf(st, "All Multicast Receive Mode\n"); + if (xq->var->setup.promiscuous) + fprintf(st, "Promiscuous Receive Mode\n"); + } return SCPE_OK; } @@ -581,6 +708,15 @@ t_stat xq_show_type (FILE* st, UNIT* uptr, int32 val, void* desc) switch (xq->var->type) { case XQ_T_DEQNA: fprintf(st, "DEQNA"); break; case XQ_T_DELQA: fprintf(st, "DELQA"); break; + case XQ_T_DELQA_PLUS: fprintf(st, "DELQA-T"); break; + } + if (xq->var->type != xq->var->mode) { + fprintf(st, ",mode="); + switch (xq->var->mode) { + case XQ_T_DEQNA: fprintf(st, "DEQNA"); break; + case XQ_T_DELQA: fprintf(st, "DELQA"); break; + case XQ_T_DELQA_PLUS: fprintf(st, "DELQA-T"); break; + } } return SCPE_OK; } @@ -594,7 +730,11 @@ t_stat xq_set_type (UNIT* uptr, int32 val, char* cptr, void* desc) /* this assumes that the parameter has already been upcased */ if (!strcmp(cptr, "DEQNA")) xq->var->type = XQ_T_DEQNA; else if (!strcmp(cptr, "DELQA")) xq->var->type = XQ_T_DELQA; + else if (!strcmp(cptr, "DELQA-T")) xq->var->type = XQ_T_DELQA_PLUS; else return SCPE_ARG; + xq->var->mode = XQ_T_DELQA; + if (xq->var->type == XQ_T_DEQNA) + xq->var->mode = XQ_T_DEQNA; return SCPE_OK; } @@ -602,7 +742,13 @@ t_stat xq_set_type (UNIT* uptr, int32 val, char* cptr, void* desc) t_stat xq_show_poll (FILE* st, UNIT* uptr, int32 val, void* desc) { CTLR* xq = xq_unit2ctlr(uptr); - fprintf(st, "poll=%d", xq->var->poll); + if (xq->var->poll) + fprintf(st, "poll=%d", xq->var->poll); + else { + fprintf(st, "polling=disabled"); + if (xq->var->coalesce_latency) + fprintf(st, ",latency=%d", xq->var->coalesce_latency); + } return SCPE_OK; } @@ -615,10 +761,22 @@ t_stat xq_set_poll (UNIT* uptr, int32 val, char* cptr, void* desc) /* this assumes that the parameter has already been upcased */ if (!strcmp(cptr, "DEFAULT")) xq->var->poll = XQ_SERVICE_INTERVAL; + else if ((!strcmp(cptr, "DISABLED")) || (!strncmp(cptr, "DELAY=", 6))) { + xq->var->poll = 0; + if (!strncmp(cptr, "DELAY=", 6)) { + int delay = 0; + if (1 != sscanf(cptr+6, "%d", &delay)) + return SCPE_ARG; + xq->var->coalesce_latency = delay; + xq->var->coalesce_latency_ticks = (tmr_poll * clk_tps * xq->var->coalesce_latency) / 1000000; + } + } else { int newpoll = 0; - sscanf(cptr, "%d", &newpoll); - if ((newpoll >= 4) && (newpoll <= 2500)) + if (1 != sscanf(cptr, "%d", &newpoll)) + return SCPE_ARG; + if ((newpoll == 0) || + ((!sim_idle_enab) && (newpoll >= 4) && (newpoll <= 2500))) xq->var->poll = newpoll; else return SCPE_ARG; @@ -660,8 +818,13 @@ t_stat xq_nxm_error(CTLR* xq) const uint16 set_bits = XQ_CSR_NI | XQ_CSR_XI | XQ_CSR_XL | XQ_CSR_RL; sim_debug(DBG_WRN, xq->dev, "Non Existent Memory Error!\n"); - /* set NXM and associated bits in CSR */ - xq_csr_set_clr(xq, set_bits , 0); + if (xq->var->mode == XQ_T_DELQA_PLUS) { + /* set NXM and associated bits in SRR */ + xq->var->srr |= (XQ_SRR_FES | XQ_SRR_NXM); + xq_setint(xq); + } else + /* set NXM and associated bits in CSR */ + xq_csr_set_clr(xq, set_bits , 0); return SCPE_OK; } @@ -670,7 +833,6 @@ t_stat xq_nxm_error(CTLR* xq) */ void xq_write_callback (CTLR* xq, int status) { - t_stat rstatus; int32 wstatus; const uint16 TDR = 100 + xq->var->write_buffer.len * 8; /* arbitrary value */ uint16 write_success[2] = {0}; @@ -681,6 +843,8 @@ void xq_write_callback (CTLR* xq, int status) xq->var->stats.xmit += 1; /* update write status words */ if (status == 0) { /* success */ + if (DBG_PCK & xq->dev->dctrl) + eth_packet_trace_ex(xq->var->etherface, xq->var->write_buffer.msg, xq->var->write_buffer.len, "xq-write", DBG_DAT & xq->dev->dctrl, DBG_PCK); wstatus = Map_WriteW(xq->var->xbdl_ba + 8, 4, write_success); } else { /* failure */ sim_debug(DBG_WRN, xq->dev, "Packet Write Error!\n"); @@ -701,11 +865,6 @@ void xq_write_callback (CTLR* xq, int status) /* clear write buffer */ xq->var->write_buffer.len = 0; - /* next descriptor (implicit) */ - xq->var->xbdl_ba += 12; - - /* finish processing xbdl */ - rstatus = xq_process_xbdl(xq); } void xqa_write_callback (int status) @@ -724,7 +883,7 @@ t_stat xq_rd(int32* data, int32 PA, int32 access) CTLR* xq = xq_pa2ctlr(PA); int index = (PA >> 1) & 07; /* word index */ - sim_debug(DBG_REG, xq->dev, "xq_rd(PA=0x%08X [%s], access=%d)\n", PA, xq_recv_regnames[index], access); + sim_debug(DBG_REG, xq->dev, "xq_rd(PA=0x%08X [%s], access=%d)\n", PA, ((xq->var->mode == XQ_T_DELQA_PLUS) ? xqt_recv_regnames[index] : xq_recv_regnames[index]), access); switch (index) { case 0: case 1: @@ -741,9 +900,14 @@ t_stat xq_rd(int32* data, int32 PA, int32 access) *data = 0xFF00 | xq->var->mac[index]; break; case 6: - sim_debug_u16(DBG_VAR, xq->dev, xq_var_bits, xq->var->var, xq->var->var, 0); - sim_debug (DBG_VAR, xq->dev, ", vec = 0%o\n", (xq->var->var & XQ_VEC_IV)); - *data = xq->var->var; + if (xq->var->mode != XQ_T_DELQA_PLUS) { + sim_debug_u16(DBG_VAR, xq->dev, xq_var_bits, xq->var->var, xq->var->var, 0); + sim_debug (DBG_VAR, xq->dev, ", vec = 0%o\n", (xq->var->var & XQ_VEC_IV)); + *data = xq->var->var; + } else { + sim_debug_u16(DBG_VAR, xq->dev, xq_srr_bits, xq->var->srr, xq->var->srr, 0); + *data = xq->var->srr; + } break; case 7: sim_debug_u16(DBG_CSR, xq->dev, xq_csr_bits, xq->var->csr, xq->var->csr, 1); @@ -765,6 +929,9 @@ t_stat xq_process_rbdl(CTLR* xq) ETH_ITEM* item; uint8* rbuf; + if (xq->var->mode == XQ_T_DELQA_PLUS) + return xq_process_turbo_rbdl(xq); + sim_debug(DBG_TRC, xq->dev, "xq_process_rdbl\n"); /* process buffer descriptors */ @@ -868,6 +1035,7 @@ t_stat xq_process_rbdl(CTLR* xq) if (xq->var->ReadQ.loss) { sim_debug(DBG_WRN, xq->dev, "ReadQ overflow!\n"); xq->var->rbdl_buf[4] |= 0x0001; /* set overflow bit */ + xq->var->stats.dropped += xq->var->ReadQ.loss; xq->var->ReadQ.loss = 0; /* reset loss counter */ } @@ -932,6 +1100,13 @@ t_stat xq_process_mop(CTLR* xq) break; case 9: /* Mop Read/Clear Counters */ break; + case 10: /* DELQA-PLUS Board ROM Version */ + if (xq->var->type == XQ_T_DELQA_PLUS) { + uint16 Delqa_Plus_ROM_Version[3] = {2, 0, 0}; /* 2.0.0 */ + wstatus = Map_WriteB(address, sizeof(Delqa_Plus_ROM_Version), (uint8*) Delqa_Plus_ROM_Version); + if (wstatus) return xq_nxm_error(xq); + } + break; } /* switch */ /* process next meb */ @@ -947,11 +1122,16 @@ t_stat xq_process_setup(CTLR* xq) int count = 0; float secs; t_stat status; + uint32 saved_debug = xq->dev->dctrl; ETH_MAC zeros = {0, 0, 0, 0, 0, 0}; ETH_MAC filters[XQ_FILTER_MAX + 1]; sim_debug(DBG_TRC, xq->dev, "xq_process_setup()\n"); + /* temporarily turn on Ethernet debugging if setup debugging is enabled */ + if (xq->dev->dctrl & DBG_SET) + xq->dev->dctrl |= DBG_ETH; + /* extract filter addresses from setup packet */ memset(xq->var->setup.macs, '\0', sizeof(xq->var->setup.macs)); for (i = 0; i < 7; i++) @@ -1018,17 +1198,16 @@ t_stat xq_process_setup(CTLR* xq) case 7: secs = 64 * 60; break; /* 64 minutes */ } xq->var->sanity.quarter_secs = (int) (secs * 4); - xq->var->sanity.max = (int) (secs * xq->var->poll); } /* finalize sanity timer state */ - xq->var->sanity.timer = xq->var->sanity.max; if (xq->var->sanity.enabled != 2) { if (xq->var->csr & XQ_CSR_SE) xq->var->sanity.enabled = 1; else xq->var->sanity.enabled = 0; } + xq_reset_santmr(xq); /* set ethernet filter */ /* memcpy (filters[count++], xq->mac, sizeof(ETH_MAC)); */ @@ -1044,8 +1223,10 @@ t_stat xq_process_setup(CTLR* xq) /* mark setup block valid */ xq->var->setup.valid = 1; - if (sim_deb && (xq->dev->dctrl & DBG_SET)) - xq_debug_setup(xq); + xq_debug_setup(xq); + + xq->dev->dctrl = saved_debug; /* restore original debugging */ + return SCPE_OK; } @@ -1054,6 +1235,9 @@ t_stat xq_process_setup(CTLR* xq) The DELQA manual does not explicitly state whether or not multiple packets can be written in one transmit operation, so a maximum of 1 packet is assumed. + + MP: Hmmm... Figure 3-1 on page 3-3 step 6 says that descriptors will be processed + until the end of the list is found. */ t_stat xq_process_xbdl(CTLR* xq) @@ -1104,7 +1288,7 @@ t_stat xq_process_xbdl(CTLR* xq) /* add to transmit buffer, making sure it's not too big */ if ((xq->var->write_buffer.len + b_length) > sizeof(xq->var->write_buffer.msg)) - b_length = sizeof(xq->var->write_buffer.msg) - xq->var->write_buffer.len; + b_length = (uint16)(sizeof(xq->var->write_buffer.msg) - xq->var->write_buffer.len); rstatus = Map_ReadB(address, b_length, &xq->var->write_buffer.msg[xq->var->write_buffer.len]); if (rstatus) return xq_nxm_error(xq); xq->var->write_buffer.len += b_length; @@ -1145,14 +1329,14 @@ t_stat xq_process_xbdl(CTLR* xq) status = eth_write(xq->var->etherface, &xq->var->write_buffer, xq->var->wcallback); if (status != SCPE_OK) /* not implemented or unattached */ xq_write_callback(xq, 1); /* fake failure */ -#if 0 - else - xq_svc(&xq->unit[0]); /* service any received data */ -#endif + else { + if (xq->var->coalesce_latency == 0) + xq_svc(&xq->unit[0]); /* service any received data */ + } sim_debug(DBG_WRN, xq->dev, "XBDL completed processing write\n"); - return SCPE_OK; } /* loopback/non-loopback */ + } else { /* not at end-of-message */ sim_debug(DBG_WRN, xq->dev, "XBDL processing implicit chain buffer segment\n"); @@ -1230,12 +1414,254 @@ t_stat xq_dispatch_xbdl(CTLR* xq) return status; } +t_stat xq_process_turbo_rbdl(CTLR* xq) +{ + int i; + t_stat status; + int descriptors_consumed = 0; + uint32 rdra = (xq->var->init.rdra_h << 16) | xq->var->init.rdra_l; + + sim_debug(DBG_TRC, xq->dev, "xq_process_turbo_rbdl()\n"); + + if ((xq->var->srr & XQ_SRR_RESP) != XQ_SRR_STRT) + return SCPE_OK; + + /* Process descriptors in the receive ring while the're available and we have packets */ + do { + uint32 address; + uint16 b_length, rbl; + ETH_ITEM* item; + uint8* rbuf; + + /* stop processing when nothing in read queue */ + if (!xq->var->ReadQ.count) + break; + + i = xq->var->rbindx; + + /* Get receive descriptor from memory */ + status = Map_ReadW (rdra+i*sizeof(xq->var->rring[i]), sizeof(xq->var->rring[i]), (uint16 *)&xq->var->rring[i]); + if (status != SCPE_OK) + return xq_nxm_error(xq); + + /* Done if Buffer not Owned */ + if (xq->var->rring[i].rmd3 & XQ_TMD3_OWN) + break; + + ++descriptors_consumed; + + /* Update ring index */ + xq->var->rbindx = (xq->var->rbindx + 1) % XQ_TURBO_RC_BCNT; + + address = ((xq->var->rring[i].hadr & 0x3F ) << 16) | xq->var->rring[i].ladr; + b_length = ETH_FRAME_SIZE; + + item = &xq->var->ReadQ.item[xq->var->ReadQ.head]; + rbl = item->packet.len + ETH_CRC_SIZE; + rbuf = item->packet.msg; + + /* see if packet must be size-adjusted or is splitting */ + if (item->packet.used) { + int used = item->packet.used; + rbl -= used; + rbuf = &item->packet.msg[used]; + } else { + /* adjust runt packets */ + if (rbl < ETH_MIN_PACKET) { + xq->var->stats.runt += 1; + sim_debug(DBG_WRN, xq->dev, "Runt detected, size = %d\n", rbl); + /* pad runts with zeros up to minimum size - this allows "legal" (size - 60) + processing of those weird short ARP packets that seem to occur occasionally */ + memset(&item->packet.msg[rbl], 0, ETH_MIN_PACKET-rbl); + rbl = ETH_MIN_PACKET; + }; + + /* adjust oversized packets */ + if (rbl > ETH_FRAME_SIZE) { + xq->var->stats.giant += 1; + sim_debug(DBG_WRN, xq->dev, "Giant detected, size=%d\n", rbl); + /* trim giants down to maximum size - no documentation on how to handle the data loss */ + item->packet.len = ETH_MAX_PACKET; + rbl = ETH_FRAME_SIZE; + }; + }; + + /* make sure entire packet fits in buffer - if not, will need to split into multiple buffers */ + if (rbl > b_length) + rbl = b_length; + item->packet.used += rbl; + + /* send data to host */ + status = Map_WriteB(address, rbl, rbuf); + if (status != SCPE_OK) + return xq_nxm_error(xq); + + /* set receive size into RBL - RBL<10:8> maps into Status1<10:8>, + RBL<7:0> maps into Status2<7:0>, and Status2<15:8> (copy) */ + xq->var->rring[i].rmd0 = 0; + xq->var->rring[i].rmd1 = rbl; + xq->var->rring[i].rmd2 = XQ_RMD2_RON | XQ_RMD2_TON; + if (0 == (item->packet.used - rbl)) + xq->var->rring[i].rmd0 |= XQ_RMD0_STP; /* Start of Packet */ + if (item->packet.used == (item->packet.len + ETH_CRC_SIZE)) + xq->var->rring[i].rmd0 |= XQ_RMD0_ENP; /* End of Packet */ + + if (xq->var->ReadQ.loss) { + xq->var->rring[i].rmd2 |= XQ_RMD2_MIS; + sim_debug(DBG_WRN, xq->dev, "ReadQ overflow!\n"); + xq->var->stats.dropped += xq->var->ReadQ.loss; + xq->var->ReadQ.loss = 0; /* reset loss counter */ + } + + Map_ReadW (rdra+(uint32)(((char *)(&xq->var->rring[xq->var->rbindx].rmd3))-((char *)&xq->var->rring)), sizeof(xq->var->rring[xq->var->rbindx].rmd3), (uint16 *)&xq->var->rring[xq->var->rbindx].rmd3); + if (xq->var->rring[xq->var->rbindx].rmd3 & XQ_RMD3_OWN) + xq->var->rring[i].rmd2 |= XQ_RMD2_EOR; + + /* Update receive descriptor in memory (only done after we've processed the contents) */ + /* Note: We're updating all but the end of the descriptor (which we never change) */ + /* AND the driver will be allowed to change once the changed tmd3 (ownership) */ + /* is noted so we avoid walking on its changes */ + xq->var->rring[i].rmd3 |= XQ_TMD3_OWN; /* Return Descriptor to Driver */ + status = Map_WriteW (rdra+i*sizeof(xq->var->rring[i]), sizeof(xq->var->rring[i])-8, (uint16 *)&xq->var->rring[i]); + if (status != SCPE_OK) + return xq_nxm_error(xq); + + /* remove packet from queue */ + if (item->packet.used >= item->packet.len) + ethq_remove(&xq->var->ReadQ); + } while (0 == (xq->var->rring[xq->var->rbindx].rmd3 & XQ_RMD3_OWN)); + + if (xq->var->rring[xq->var->rbindx].rmd3 & XQ_RMD3_OWN) + sim_debug(DBG_WRN, xq->dev, "xq_process_turbo_rbdl() - receive ring full\n"); + + if (descriptors_consumed) + /* Interrupt for Packet Reception Completion */ + xq_setint(xq); + + return SCPE_OK; +} + +t_stat xq_process_turbo_xbdl(CTLR* xq) +{ + int i; + t_stat status; + int descriptors_consumed = 0; + uint32 tdra = (xq->var->init.tdra_h << 16) | xq->var->init.tdra_l; + + sim_debug(DBG_TRC, xq->dev, "xq_process_turbo_xbdl()\n"); + + if ((xq->var->srr & XQ_SRR_RESP) != XQ_SRR_STRT) + return SCPE_OK; + + /* clear transmit buffer */ + xq->var->write_buffer.len = 0; + + /* Process each descriptor in the transmit ring */ + do { + uint32 address; + uint16 b_length; + + i = xq->var->tbindx; + + /* Get transmit descriptor from memory */ + status = Map_ReadW (tdra+i*sizeof(xq->var->xring[i]), sizeof(xq->var->xring[i]), (uint16 *)&xq->var->xring[i]); + if (status != SCPE_OK) + return xq_nxm_error(xq); + + if (xq->var->xring[i].tmd3 & XQ_TMD3_OWN) + break; + + /* Update ring index */ + xq->var->tbindx = (xq->var->tbindx + 1) % XQ_TURBO_XM_BCNT; + + ++descriptors_consumed; + address = ((xq->var->xring[i].hadr & 0x3F ) << 16) | xq->var->xring[i].ladr; + b_length = (xq->var->xring[i].tmd3 & XQ_TMD3_BCT); + + /* add to transmit buffer, making sure it's not too big */ + if ((xq->var->write_buffer.len + b_length) > sizeof(xq->var->write_buffer.msg)) + b_length = (uint16)(sizeof(xq->var->write_buffer.msg) - xq->var->write_buffer.len); + status = Map_ReadB(address, b_length, &xq->var->write_buffer.msg[xq->var->write_buffer.len]); + if (status != SCPE_OK) + return xq_nxm_error(xq); + + xq->var->write_buffer.len += b_length; + if (!(xq->var->xring[i].tmd3 & XQ_TMD3_FOT)) { + /* Process Loopback if in Loopback mode */ + if (xq->var->init.mode & XQ_IN_MO_LOP) { + if ((xq->var->init.mode & XQ_IN_MO_INT) || (xq->var->etherface)) { + /* put packet in read buffer */ + ethq_insert (&xq->var->ReadQ, 1, &xq->var->write_buffer, 0); + status = SCPE_OK; + } else { + /* External loopback fails when not connected */ + status = SCPE_NOFNC; + } + } else + status = eth_write(xq->var->etherface, &xq->var->write_buffer, NULL); + + xq->var->stats.xmit += 1; + if (status != SCPE_OK) { /* not implemented or unattached */ + sim_debug(DBG_WRN, xq->dev, "Packet Write Error!\n"); + xq->var->stats.fail += 1; + xq->var->xring[i].tmd0 = XQ_TMD0_ERR1; + xq->var->xring[i].tmd1 = 100 + xq->var->write_buffer.len * 8; /* arbitrary value */ + xq->var->xring[i].tmd1 |= XQ_TMD1_LCA; + } else { + if (DBG_PCK & xq->dev->dctrl) + eth_packet_trace_ex(xq->var->etherface, xq->var->write_buffer.msg, xq->var->write_buffer.len, "xq-write", DBG_DAT & xq->dev->dctrl, DBG_PCK); + xq->var->xring[i].tmd0 = 0; + xq->var->xring[i].tmd1 = 100 + xq->var->write_buffer.len * 8; /* arbitrary value */ + } + sim_debug(DBG_WRN, xq->dev, "XBDL completed processing write\n"); + /* clear transmit buffer */ + xq->var->write_buffer.len = 0; + xq->var->xring[i].tmd2 = XQ_TMD2_RON | XQ_TMD2_TON; + } + + Map_ReadW (tdra+(uint32)(((char *)(&xq->var->xring[xq->var->tbindx].tmd3))-((char *)&xq->var->xring)), sizeof(xq->var->xring[xq->var->tbindx].tmd3), (uint16 *)&xq->var->xring[xq->var->tbindx].tmd3); + if (xq->var->xring[xq->var->tbindx].tmd3 & XQ_TMD3_OWN) + xq->var->xring[i].tmd2 |= XQ_TMD2_EOR; + + /* Update transmit descriptor in memory (only done after we've processed the contents) */ + /* Note: We're updating all but the end of the descriptor (which we never change) */ + /* AND the driver will be allowed to change once the changed tmd3 (ownership) */ + /* is noted so we avoid walking on its changes */ + xq->var->xring[i].tmd3 |= XQ_TMD3_OWN; /* Return Descriptor to Driver */ + status = Map_WriteW (tdra+i*sizeof(xq->var->xring[i]), sizeof(xq->var->xring[i])-8, (uint16 *)&xq->var->xring[i]); + if (status != SCPE_OK) + return xq_nxm_error(xq); + + } while (0 == (xq->var->xring[xq->var->tbindx].tmd3 & XQ_TMD3_OWN)); + + if (descriptors_consumed) { + + /* Interrupt for Packet Transmission Completion */ + xq_setint(xq); + + if (xq->var->coalesce_latency == 0) + xq_svc(&xq->unit[0]); /* service any received data */ + } else { + /* There appears to be a bug in the VMS SCS/XQ driver when it uses chained + buffers to transmit a packet. It updates the transmit buffer ring in the + correct order (i.e. clearing the ownership on the last packet segment + first), but it writes a transmit request to the ARQR register after adjusting + the ownership of EACH buffer piece. This results in us being awakened once + and finding nothing to do. We ignore this and the next write the ARQR will + properly cause the packet transmission. + */ + sim_debug(DBG_WRN, xq->dev, "xq_process_turbo_xbdl() - Nothing to Transmit\n"); + } + + return status; +} + t_stat xq_process_loopback(CTLR* xq, ETH_PACK* pack) { - ETH_PACK reply; - ETH_MAC physical_address; + ETH_PACK response; + ETH_MAC *physical_address; t_stat status; - int offset = pack->msg[14] | (pack->msg[15] << 8); + int offset = 16 + (pack->msg[14] | (pack->msg[15] << 8)); int function = pack->msg[offset] | (pack->msg[offset+1] << 8); sim_debug(DBG_TRC, xq->dev, "xq_process_loopback()\n"); @@ -1243,19 +1669,27 @@ t_stat xq_process_loopback(CTLR* xq, ETH_PACK* pack) if (function != 2 /*forward*/) return SCPE_NOFNC; - /* create reply packet */ - memcpy (&reply, pack, sizeof(ETH_PACK)); - memcpy (physical_address, xq->var->setup.valid ? xq->var->setup.macs[0] : xq->var->mac, sizeof(ETH_MAC)); - memcpy (&reply.msg[0], &reply.msg[offset+2], sizeof(ETH_MAC)); - memcpy (&reply.msg[6], physical_address, sizeof(ETH_MAC)); - memcpy (&reply.msg[offset+2], physical_address, sizeof(ETH_MAC)); - reply.msg[offset] = 0x01; - offset += 8; - reply.msg[14] = offset & 0xFF; - reply.msg[15] = (offset >> 8) & 0xFF; + /* create forward response packet */ + memcpy (&response, pack, sizeof(ETH_PACK)); + if (xq->var->mode == XQ_T_DELQA_PLUS) + physical_address = &xq->var->init.phys; + else + if (xq->var->setup.valid) + physical_address = &xq->var->setup.macs[0]; + else + physical_address = &xq->var->mac; + memcpy (&response.msg[0], &response.msg[offset+2], sizeof(ETH_MAC)); + memcpy (&response.msg[6], physical_address, sizeof(ETH_MAC)); + offset += 8 - 16; /* Account for the Ethernet Header and Offset value in this number */ + response.msg[14] = offset & 0xFF; + response.msg[15] = (offset >> 8) & 0xFF; - /* send reply packet */ - status = eth_write(xq->var->etherface, &reply, NULL); + /* send response packet */ + status = eth_write(xq->var->etherface, &response, NULL); + ++xq->var->stats.loop; + + if (DBG_PCK & xq->dev->dctrl) + eth_packet_trace_ex(xq->var->etherface, response.msg, response.len, ((function == 1) ? "xq-loopbackreply" : "xq-loopbackforward"), DBG_DAT & xq->dev->dctrl, DBG_PCK); return status; } @@ -1327,7 +1761,11 @@ t_stat xq_process_local (CTLR* xq, ETH_PACK* pack) void xq_read_callback(CTLR* xq, int status) { xq->var->stats.recv += 1; - if (xq->var->csr & XQ_CSR_RE) { /* receiver enabled */ + + if (DBG_PCK & xq->dev->dctrl) + eth_packet_trace_ex(xq->var->etherface, xq->var->read_buffer.msg, xq->var->read_buffer.len, "xq-recvd", DBG_DAT & xq->dev->dctrl, DBG_PCK); + + if ((xq->var->csr & XQ_CSR_RE) || (xq->var->mode == XQ_T_DELQA_PLUS)) { /* receiver enabled */ /* process any packets locally that can be */ t_stat status = xq_process_local (xq, &xq->var->read_buffer); @@ -1336,6 +1774,7 @@ void xq_read_callback(CTLR* xq, int status) if (status != SCPE_OK) ethq_insert(&xq->var->ReadQ, 2, &xq->var->read_buffer, status); } else { + xq->var->stats.dropped += 1; sim_debug(DBG_WRN, xq->dev, "packet received with receiver disabled\n"); } } @@ -1356,6 +1795,13 @@ void xq_sw_reset(CTLR* xq) int i; sim_debug(DBG_TRC, xq->dev, "xq_sw_reset()\n"); + ++xq->var->stats.reset; + + /* Return DELQA-T to DELQA Normal mode */ + if (xq->var->type == XQ_T_DELQA_PLUS) { + xq->var->mode = XQ_T_DELQA; + xq->var->iba = xq->var->srr = 0; + } /* reset csr bits */ xq_csr_set_clr(xq, set_bits, (uint16) ~set_bits); @@ -1384,6 +1830,10 @@ void xq_sw_reset(CTLR* xq) memcpy (filters[count++], xq->var->setup.macs[i], sizeof(ETH_MAC)); eth_filter (xq->var->etherface, count, filters, xq->var->setup.multicast, xq->var->setup.promiscuous); } + + /* Stop receive polling until the receiver is reenabled */ + xq_stop_receiver(xq); + } /* write registers: */ @@ -1398,11 +1848,25 @@ t_stat xq_wr_var(CTLR* xq, int32 data) xq->var->var = (data & XQ_VEC_IV); break; case XQ_T_DELQA: + case XQ_T_DELQA_PLUS: xq->var->var = (xq->var->var & XQ_VEC_RO) | (data & XQ_VEC_RW); /* if switching to DEQNA-LOCK mode clear VAR<14:10> */ - if (~xq->var->var & XQ_VEC_MS) + if (~xq->var->var & XQ_VEC_MS) { + xq->var->mode = XQ_T_DEQNA; xq->var->var &= ~(XQ_VEC_OS | XQ_VEC_RS | XQ_VEC_ST); + } else { + xq->var->mode = XQ_T_DELQA; + } + + /* if Self Test is on, turn it off to signal completion */ + if (xq->var->var & XQ_VEC_RS) { + xq->var->var &= ~XQ_VEC_RS; + if (!xq->var->etherface) + xq->var->var |= XQ_VEC_S1; /* Indicate No Network Connection */ + else + xq->var->var &= ~XQ_VEC_ST; /* Set success Status */ + } break; } @@ -1619,15 +2083,21 @@ t_stat xq_wr_csr(CTLR* xq, int32 data) return SCPE_OK; } -#if 0 /* controller should ALWAYS have an active timer if enabled (for HW sanity) */ - /* start/stop receive timer when RE transitions */ - if ((xq->var->csr ^ data) & XQ_CSR_RE) { - if (data & XQ_CSR_RE) - sim_activate(&xq->unit[0], clock_cosched (tmxr_poll)); - else - sim_cancel(&xq->unit[0]); + /* start receiver when RE transitions to set */ + if (~xq->var->csr & XQ_CSR_RE & data) { + sim_debug(DBG_REG, xq->dev, "xq_wr_csr(data=0x%08X) - receiver started\n", data); + + /* start the read service timer or enable asynch reading as appropriate */ + xq_start_receiver(xq); + } + + /* stop receiver when RE transitions to clear */ + if (xq->var->csr & XQ_CSR_RE & ~data) { + sim_debug(DBG_REG, xq->dev, "xq_wr_csr(data=0x%08X) - receiver stopped\n", data); + + /* stop the read service timer or disable asynch reading as appropriate */ + xq_stop_receiver(xq); } -#endif /* update CSR bits */ xq_csr_set_clr (xq, set_bits, clr_bits); @@ -1641,37 +2111,194 @@ t_stat xq_wr_csr(CTLR* xq, int32 data) return SCPE_OK; } +void xq_start_receiver(CTLR* xq) +{ + if (!xq->var->etherface) + return; + + /* start the read service timer or enable asynch reading as appropriate */ + if (xq->var->must_poll) + sim_activate(xq->unit, (sim_idle_enab ? tmxr_poll : (tmr_poll*clk_tps)/xq->var->poll)); + else + if ((xq->var->poll == 0) || (xq->var->mode == XQ_T_DELQA_PLUS)) + eth_set_async(xq->var->etherface, xq->var->coalesce_latency_ticks); + else + sim_activate(xq->unit, (sim_idle_enab ? tmxr_poll : (tmr_poll*clk_tps)/xq->var->poll)); +} + +void xq_stop_receiver(CTLR* xq) +{ + sim_cancel(&xq->unit[0]); /* Stop Receiving */ + if (xq->var->etherface) + eth_clr_async(xq->var->etherface); +} + +t_stat xq_wr_srqr(CTLR* xq, int32 data) +{ + uint16 set_bits = data & XQ_SRQR_RW; /* set RW set bits */ + + sim_debug(DBG_REG, xq->dev, "xq_wr_srqr(data=0x%08X)\n", data); + + xq->var->srr = set_bits; + + switch (set_bits) { + case XQ_SRQR_STRT: { + t_stat status; + + xq->var->stats.setup += 1; + /* Get init block from memory */ + status = Map_ReadW (xq->var->iba, sizeof(xq->var->init), (uint16 *)&xq->var->init); + if (SCPE_OK != status) { + xq_nxm_error (xq); + } else { + uint32 saved_debug = xq->dev->dctrl; + + /* temporarily turn on Ethernet debugging if setup debugging is enabled */ + if (xq->dev->dctrl & DBG_SET) + xq->dev->dctrl |= DBG_ETH; + + xq_debug_turbo_setup(xq); + + xq->dib->vec = xq->var->init.vector + VEC_Q; + xq->var->tbindx = xq->var->rbindx = 0; + if ((xq->var->sanity.enabled) && (xq->var->init.options & XQ_IN_OP_HIT)) { + xq->var->sanity.quarter_secs = 4*xq->var->init.hit_timeout; + } + xq->var->icr = xq->var->init.options & XQ_IN_OP_INT; + status = eth_filter_hash (xq->var->etherface, 1, &xq->var->init.phys, 0, xq->var->init.mode & XQ_IN_MO_PRO, &xq->var->init.hash_filter); + + xq->dev->dctrl = saved_debug; /* restore original debugging */ + } + /* start the read service timer or enable asynch reading as appropriate */ + xq_start_receiver(xq); + break; + } + case XQ_SRQR_STOP: + xq_stop_receiver(xq); + break; + default: + break; + } + + /* All Writes to SRQR reset the Host Inactivity Timer */ + xq_reset_santmr(xq); + + /* Interrupt after this synchronous request completion */ + xq_setint(xq); + + return SCPE_OK; +} + +t_stat xq_wr_arqr(CTLR* xq, int32 data) +{ + sim_debug(DBG_REG, xq->dev, "xq_wr_arqr(data=0x%08X)\n", data); + + /* initiate transmit activity when requested */ + if (XQ_ARQR_TRQ & data) { + xq_process_turbo_xbdl (xq); + } + /* initiate transmit activity when requested */ + if (XQ_ARQR_RRQ & data) { + xq_process_turbo_rbdl (xq); + } + + /* reset controller when requested */ + if (XQ_ARQR_SR & data) { + xq_sw_reset(xq); + } + + /* All Writes to ARQR reset the Host Inactivity Timer */ + xq_reset_santmr(xq); + + return SCPE_OK; +} + +t_stat xq_wr_icr(CTLR* xq, int32 data) +{ + uint16 old_icr = xq->var->icr; + + sim_debug(DBG_REG, xq->dev, "xq_wr_icr(data=0x%08X)\n", data); + + xq->var->icr = data & XQ_ICR_ENA; + + if (xq->var->icr && !old_icr && xq->var->pending_interrupt) + xq_setint(xq); + + return SCPE_OK; +} + t_stat xq_wr(int32 data, int32 PA, int32 access) { t_stat status; CTLR* xq = xq_pa2ctlr(PA); int index = (PA >> 1) & 07; /* word index */ - sim_debug(DBG_REG, xq->dev, "xq_wr(data=0x%08X, PA=0x%08X[%s], access=%d)\n", data, PA, xq_xmit_regnames[index], access); + sim_debug(DBG_REG, xq->dev, "xq_wr(data=0x%08X, PA=0x%08X[%s], access=%d)\n", data, PA, ((xq->var->mode == XQ_T_DELQA_PLUS) ? xqt_xmit_regnames[index] : xq_xmit_regnames[index]), access); - switch (index) { - case 0: /* these should not be written */ - case 1: + switch (xq->var->mode) { + case XQ_T_DELQA_PLUS: + switch (index) { + case 0: /* IBAL */ + xq->var->iba = (xq->var->iba & 0xFFFF0000) | (data & 0xFFFF); + break; + case 1: /* IBAH */ + xq->var->iba = (xq->var->iba & 0xFFFF) | ((data & 0xFFFF) << 16); + break; + case 2: /* ICR */ + status = xq_wr_icr(xq, data); + break; + case 3: + break; + case 4: /* SRQR */ + status = xq_wr_srqr(xq, data); + break; + case 5: + break; + case 6: + break; + case 7: /* ARQR */ + status = xq_wr_arqr(xq, data); + break; + } break; - case 2: /* receive bdl low bits */ - xq->var->rbdl[0] = data; - break; - case 3: /* receive bdl high bits */ - xq->var->rbdl[1] = data; - status = xq_dispatch_rbdl(xq); /* start receive operation */ - break; - case 4: /* transmit bdl low bits */ - xq->var->xbdl[0] = data; - break; - case 5: /* transmit bdl high bits */ - xq->var->xbdl[1] = data; - status = xq_dispatch_xbdl(xq); /* start transmit operation */ - break; - case 6: /* vector address register */ - status = xq_wr_var(xq, data); - break; - case 7: /* control and status register */ - status = xq_wr_csr(xq, data); + default: /* DEQNA, DELQA Normal */ + switch (index) { + case 0: /* IBAL/XCR0 */ /* these should only be written on a DELQA-T */ + if (xq->var->type == XQ_T_DELQA_PLUS) + xq->var->iba = (xq->var->iba & 0xFFFF0000) | (data & 0xFFFF); + break; + case 1: /* IBAH/XCR1 */ + if (xq->var->type == XQ_T_DELQA_PLUS) { + if (((xq->var->iba & 0xFFFF) == 0x0BAF) && (data == 0xFF00)) { + xq->var->mode = XQ_T_DELQA_PLUS; + xq->var->srr = XQ_SRR_TRBO; + sim_cancel(xq->unit); /* Turn off receive processing until explicitly enabled */ + eth_clr_async(xq->var->etherface); + } + xq->var->iba = (xq->var->iba & 0xFFFF) | ((data & 0xFFFF) << 16); + } + break; + case 2: /* receive bdl low bits */ + xq->var->rbdl[0] = data; + break; + case 3: /* receive bdl high bits */ + xq->var->rbdl[1] = data; + status = xq_dispatch_rbdl(xq); /* start receive operation */ + break; + case 4: /* transmit bdl low bits */ + xq->var->xbdl[0] = data; + break; + case 5: /* transmit bdl high bits */ + xq->var->xbdl[1] = data; + status = xq_dispatch_xbdl(xq); /* start transmit operation */ + break; + case 6: /* vector address register */ + status = xq_wr_var(xq, data); + break; + case 7: /* control and status register */ + status = xq_wr_csr(xq, data); + break; + } break; } return SCPE_OK; @@ -1694,9 +2321,12 @@ t_stat xq_reset(DEVICE* dptr) switch (xq->var->type) { case XQ_T_DEQNA: xq->var->var = 0; + xq->var->mode = XQ_T_DEQNA; break; case XQ_T_DELQA: + case XQ_T_DELQA_PLUS: xq->var->var = XQ_VEC_MS | XQ_VEC_OS; + xq->var->mode = XQ_T_DELQA; break; } xq->dib->vec = 0; @@ -1717,18 +2347,25 @@ t_stat xq_reset(DEVICE* dptr) /* reset ethernet interface */ if (xq->var->etherface) { + /* restore filter on ROM mac address */ status = eth_filter (xq->var->etherface, 1, &xq->var->mac, 0, 0); xq_csr_set_clr(xq, XQ_CSR_OK, 0); /* start service timer */ - sim_activate_abs(&xq->unit[0], tmxr_poll); + sim_activate_abs(&xq->unit[1], (tmr_poll * clk_tps) / 4); + + /* stop the receiver */ + eth_clr_async(xq->var->etherface); } + /* stop the receiver */ + sim_cancel(xq->unit); + /* set hardware sanity controls */ if (xq->var->sanity.enabled) { xq->var->sanity.quarter_secs = XQ_HW_SANITY_SECS * 4/*qsec*/; - xq->var->sanity.max = XQ_HW_SANITY_SECS * xq->var->poll; } + return SCPE_OK; } @@ -1739,7 +2376,7 @@ void xq_reset_santmr(CTLR* xq) sim_debug(DBG_SAN, xq->dev, "SANITY TIMER RESETTING, qsecs: %d\n", xq->var->sanity.quarter_secs); /* reset sanity countdown timer to max count */ - xq->var->sanity.timer = xq->var->sanity.max; + xq->var->sanity.timer = xq->var->sanity.quarter_secs; } } @@ -1750,7 +2387,7 @@ t_stat xq_boot_host(CTLR* xq) The manual says the hardware should force the Qbus BDCOK low for 3.6 microseconds, which will cause the host to reboot. - Since the SIMH Qbus emulator does not have this functionality, we call + Since the SIMH Qbus emulator does not have this functionality, we return a special STOP_ code, and let the CPU stop dispatch routine decide what the appropriate cpu-specific behavior should be. */ @@ -1765,7 +2402,16 @@ t_stat xq_system_id (CTLR* xq, const ETH_MAC dest, uint16 receipt_id) t_stat status; sim_debug(DBG_TRC, xq->dev, "xq_system_id()\n"); - if (xq->var->type != XQ_T_DELQA) /* DELQA-only function */ + + /* reset system ID counter for next event */ + xq->var->idtmr = XQ_SYSTEM_ID_SECS * 4; + + if (xq->var->coalesce_latency) { + /* Adjust latency ticks based on calibrated timer values */ + xq->var->coalesce_latency_ticks = (tmr_poll * clk_tps * xq->var->coalesce_latency) / 1000000; + } + + if (xq->var->type == XQ_T_DEQNA) /* DELQA-only function */ return SCPE_NOFNC; memset (&system_id, 0, sizeof(system_id)); @@ -1811,11 +2457,16 @@ t_stat xq_system_id (CTLR* xq, const ETH_MAC dest, uint16 receipt_id) msg[41] = 0x00; /* type */ msg[42] = 0x01; /* length */ msg[43] = 0x11; /* value (0x11=DELQA) */ + if (xq->var->type == XQ_T_DELQA_PLUS) /* DELQA-T has different Device ID */ + msg[43] = 0x4B; /* value (0x4B(75)=DELQA-T) */ /* write system id */ system_id.len = 60; status = eth_write(xq->var->etherface, &system_id, NULL); + if (DBG_PCK & xq->dev->dctrl) + eth_packet_trace_ex(xq->var->etherface, system_id.msg, system_id.len, "xq-systemid", DBG_DAT & xq->dev->dctrl, DBG_PCK); + return status; } @@ -1827,44 +2478,59 @@ t_stat xq_svc(UNIT* uptr) CTLR* xq = xq_unit2ctlr(uptr); /* if the receiver is enabled */ - if (xq->var->csr & XQ_CSR_RE) { + if ((xq->var->mode == XQ_T_DELQA_PLUS) || (xq->var->csr & XQ_CSR_RE)) { t_stat status; - int queue_size; /* First pump any queued packets into the system */ - if ((xq->var->ReadQ.count > 0) && (~xq->var->csr & XQ_CSR_RL)) - status = xq_process_rbdl(xq); + if ((xq->var->ReadQ.count > 0) && ((xq->var->mode == XQ_T_DELQA_PLUS) || (~xq->var->csr & XQ_CSR_RL))) + xq_process_rbdl(xq); /* Now read and queue packets that have arrived */ - /* This is repeated as long as they are available and we have room */ - do - { - queue_size = xq->var->ReadQ.count; + /* This is repeated as long as they are available */ + do { /* read a packet from the ethernet - processing is via the callback */ status = eth_read (xq->var->etherface, &xq->var->read_buffer, xq->var->rcallback); - } while (queue_size != xq->var->ReadQ.count); + } while (status); /* Now pump any still queued packets into the system */ - if ((xq->var->ReadQ.count > 0) && (~xq->var->csr & XQ_CSR_RL)) - status = xq_process_rbdl(xq); + if ((xq->var->ReadQ.count > 0) && ((xq->var->mode == XQ_T_DELQA_PLUS) || (~xq->var->csr & XQ_CSR_RL))) + xq_process_rbdl(xq); } + /* resubmit service timer */ + if ((xq->var->must_poll) || (xq->var->poll && (xq->var->mode != XQ_T_DELQA_PLUS))) + sim_activate(uptr, (sim_idle_enab ? tmxr_poll : (tmr_poll*clk_tps)/xq->var->poll)); + + return SCPE_OK; +} + +/* +** service routine - used for timer based activities +*/ +t_stat xq_tmrsvc(UNIT* uptr) +{ + CTLR* xq = xq_unit2ctlr(uptr); + /* has sanity timer expired? if so, reboot */ if (xq->var->sanity.enabled) if (--xq->var->sanity.timer <= 0) - xq_boot_host(xq); + if (xq->var->mode != XQ_T_DELQA_PLUS) + return xq_boot_host(xq); + else { /* DELQA-T Host Inactivity Timer expiration means switch out of DELQA-T mode */ + sim_debug(DBG_TRC, xq->dev, "xq_tmrsvc(DELQA-PLUS Host Inactivity Expired\n"); + xq->var->mode = XQ_T_DELQA; + xq->var->iba = xq->var->srr = 0; + xq->var->var = XQ_VEC_MS | XQ_VEC_OS; + } /* has system id timer expired? if so, do system id */ if (--xq->var->idtmr <= 0) { const ETH_MAC mop_multicast = {0xAB, 0x00, 0x00, 0x02, 0x00, 0x00}; xq_system_id(xq, mop_multicast, 0); - - /* reset system ID counter for next event */ - xq->var->idtmr = XQ_SYSTEM_ID_SECS * xq->var->poll; } /* resubmit service timer */ - sim_activate(&xq->unit[0], tmxr_poll); + sim_activate(uptr, (tmr_poll * clk_tps) / 4); return SCPE_OK; } @@ -1904,6 +2570,27 @@ t_stat xq_attach(UNIT* uptr, char* cptr) xq->var->etherface = 0; return status; } + if (xq->var->poll == 0) { + status = eth_set_async(xq->var->etherface, xq->var->coalesce_latency_ticks); + if (status != SCPE_OK) { + free(tptr); + free(xq->var->etherface); + xq->var->etherface = 0; + return status; + } + xq->var->must_poll = 0; + } else { + xq->var->must_poll = (SCPE_OK != eth_clr_async(xq->var->etherface)); + } + if (SCPE_OK != eth_check_address_conflict (xq->var->etherface, &xq->var->mac)) { + char buf[32]; + + eth_mac_fmt(&xq->var->mac, buf); /* format ethernet mac address */ + printf("%s: MAC Address Conflict on LAN for address %s, change the MAC address to a unique value\n", xq->dev->name, buf); + if (sim_log) fprintf (sim_log, "%s: MAC Address Conflict on LAN for address %s, change the MAC address to a unique value\n", xq->dev->name, buf); + eth_close(xq->var->etherface); + return SCPE_NOATT; + } uptr->filename = tptr; uptr->flags |= UNIT_ATT; @@ -1930,8 +2617,9 @@ t_stat xq_detach(UNIT* uptr) free(uptr->filename); uptr->filename = NULL; uptr->flags &= ~UNIT_ATT; - /* cancel service timer */ + /* cancel service timers */ sim_cancel(&xq->unit[0]); + sim_cancel(&xq->unit[1]); } /* turn off transceiver power indicator */ @@ -1942,6 +2630,16 @@ t_stat xq_detach(UNIT* uptr) void xq_setint(CTLR* xq) { + if (xq->var->mode == XQ_T_DELQA_PLUS) { + if (!xq->var->icr) { + xq->var->pending_interrupt = 1; + return; + } + xq->var->pending_interrupt = 0; + } + + sim_debug(DBG_TRC, xq->dev, "xq_setint() - Generate Interrupt\n"); + xq->var->irq = 1; SET_INT(XQ); return; @@ -2030,12 +2728,16 @@ void xq_debug_setup(CTLR* xq) { int i; char buffer[20]; + + if (!(sim_deb && (xq->dev->dctrl & DBG_SET))) + return; + if (xq->var->write_buffer.msg[0]) - printf ("%s: setup> MOP info present!\n", xq->dev->name); + sim_debug(DBG_SET, xq->dev, "%s: setup> MOP info present!\n", xq->dev->name); for (i = 0; i < XQ_FILTER_MAX; i++) { eth_mac_fmt(&xq->var->setup.macs[i], buffer); - printf ("%s: setup> set addr[%d]: %s\n", xq->dev->name, i, buffer); + sim_debug(DBG_SET, xq->dev, "%s: setup> set addr[%d]: %s\n", xq->dev->name, i, buffer); } if (xq->var->write_buffer.len > 128) { @@ -2045,7 +2747,55 @@ void xq_debug_setup(CTLR* xq) if (len & XQ_SETUP_PM) strcat(buffer, "PM "); if (len & XQ_SETUP_LD) strcat(buffer, "LD "); if (len & XQ_SETUP_ST) strcat(buffer, "ST "); - printf ("%s: setup> Length [%d =0x%X, LD:%d, ST:%d] info: %s\n", + sim_debug(DBG_SET, xq->dev, "%s: setup> Length [%d =0x%X, LD:%d, ST:%d] info: %s\n", xq->dev->name, len, len, (len & XQ_SETUP_LD) >> 2, (len & XQ_SETUP_ST) >> 4, buffer); } } + +void xq_debug_turbo_setup(CTLR* xq) +{ + int i; + char buffer[64] = ""; + + if (!(sim_deb && (xq->dev->dctrl & DBG_SET))) + return; + + sim_debug(DBG_SET, xq->dev, "%s: setup> Turbo Initialization Block!\n", xq->dev->name); + + if (xq->var->init.mode & XQ_IN_MO_PRO) strcat(buffer, "PRO "); + if (xq->var->init.mode & XQ_IN_MO_INT) strcat(buffer, "INT "); + if (xq->var->init.mode & XQ_IN_MO_DRT) strcat(buffer, "DRC "); + if (xq->var->init.mode & XQ_IN_MO_DTC) strcat(buffer, "DTC "); + if (xq->var->init.mode & XQ_IN_MO_LOP) strcat(buffer, "LOP "); + sim_debug(DBG_SET, xq->dev, "%s: setup> set Mode: %s\n", xq->dev->name, buffer); + + eth_mac_fmt(&xq->var->init.phys, buffer); + sim_debug(DBG_SET, xq->dev, "%s: setup> set Physical MAC Address: %s\n", xq->dev->name, buffer); + + buffer[0] = '\0'; + for (i = 0; i < sizeof(xq->var->init.hash_filter); i++) + sprintf(&buffer[strlen(buffer)], "%02X ", xq->var->init.hash_filter[i]); + sim_debug(DBG_SET, xq->dev, "%s: setup> set Multicast Hash: %s\n", xq->dev->name, buffer); + + buffer[0] = '\0'; + if (xq->var->init.options & XQ_IN_OP_HIT) strcat(buffer, "HIT "); + if (xq->var->init.options & XQ_IN_OP_INT) strcat(buffer, "INT "); + sim_debug(DBG_SET, xq->dev, "%s: setup> set Options: %s\n", xq->dev->name, buffer); + + sim_debug(DBG_SET, xq->dev, "%s: setup> set Vector: %d =0x%X\n", + xq->dev->name, xq->var->init.vector, xq->var->init.vector); + + sim_debug(DBG_SET, xq->dev, "%s: setup> set Host Inactivity Timeout: %d seconds\n", + xq->dev->name, xq->var->init.hit_timeout); + + buffer[0] = '\0'; + for (i = 0; i < sizeof(xq->var->init.bootpassword); i++) + sprintf(&buffer[strlen(buffer)], "%02X ", xq->var->init.bootpassword[i]); + + sim_debug(DBG_SET, xq->dev, "%s: setup> set Boot Password: %s\n", xq->dev->name, buffer); + + sim_debug(DBG_SET, xq->dev, "%s: setup> set Receive Ring Buffer Address: %02X%04X\n", + xq->dev->name, xq->var->init.rdra_h, xq->var->init.rdra_l); + sim_debug(DBG_SET, xq->dev, "%s: setup> set Transmit Ring Buffer Address: %02X%04X\n", + xq->dev->name, xq->var->init.tdra_h, xq->var->init.tdra_l); +} diff --git a/PDP11/pdp11_xq.h b/PDP11/pdp11_xq.h index 737552c7..16327504 100644 --- a/PDP11/pdp11_xq.h +++ b/PDP11/pdp11_xq.h @@ -28,6 +28,14 @@ Modification history: + 03-Mar-08 MP Added DELQA-T (aka DELQA Plus) device emulation support. + 06-Feb-08 MP Added dropped frame statistics to record when the receiver discards + received packets due to the receiver being disabled, or due to the + XQ device's packet receive queue being full. Also removed the + filter statistic counter since there was no code which ever set it. + 29-Jan-08 MP Dynamically determine the timer polling rate based on the + calibrated tmr_poll and clk_tps values of the simulator. + 23-Jan-08 MP Added debugging support to display packet headers and packet data 07-Jul-05 RMS Removed extraneous externs 20-Jan-04 DTH Added new sanity timer and system id timer 19-Jan-04 DTH Added XQ_SERVICE_INTERVAL, poll @@ -84,12 +92,11 @@ extern int32 int_req[IPL_HLVL]; #define XQ_HW_SANITY_SECS 240 /* seconds before HW sanity timer expires */ #define XQ_MAX_CONTROLLERS 2 /* maximum controllers allowed */ -enum xq_type {XQ_T_DEQNA, XQ_T_DELQA}; +enum xq_type {XQ_T_DEQNA, XQ_T_DELQA, XQ_T_DELQA_PLUS}; struct xq_sanity { int enabled; /* sanity timer enabled? 2=HW, 1=SW, 0=off */ int quarter_secs; /* sanity timer value in 1/4 seconds */ - int max; /* maximum timeout (based on poll) */ int timer; /* countdown timer */ }; @@ -104,17 +111,132 @@ struct xq_setup { ETH_MAC macs[XQ_FILTER_MAX]; /* MAC addresses to respond to */ }; +struct xq_turbo_init_block { /* DELQA-T Initialization Block */ + uint16 mode; +#define XQ_IN_MO_PRO 0x8000 /* Promiscuous Mode */ +#define XQ_IN_MO_INT 0x0040 /* Internal Loopback Mode */ +#define XQ_IN_MO_DRT 0x0020 /* Disable Retry */ +#define XQ_IN_MO_DTC 0x0008 /* Disable Transmit CRC */ +#define XQ_IN_MO_LOP 0x0004 /* Loopback */ + ETH_MAC phys; /* Physical MAC Address */ + ETH_MULTIHASH hash_filter; /* 64bit LANCE Hash Filter for Multicast Address selection */ + uint16 rdra_l; + uint16 rdra_h; + uint16 tdra_l; + uint16 tdra_h; + uint16 options; +#define XQ_IN_OP_HIT 0x0002 /* Host Inactivity Timer Enable Flag */ +#define XQ_IN_OP_INT 0x0001 /* Interrupt Enable Flag*/ + uint16 vector; /* Interrupt Vector */ + uint16 hit_timeout; /* Host Inactivity Timer Timeout Value */ + uint8 bootpassword[6]; /* MOP Console Boot Password */ +}; + +/* DELQA-T Mode - Transmit Buffer Descriptor */ +struct transmit_buffer_descriptor { + uint16 tmd0; +#define XQ_TMD0_ERR1 0x4000 /* Error Summary. The OR of TMD1 (LC0, LCA, and RTR) */ +#define XQ_TMD0_MOR 0x1000 /* More than one retry on transmit */ +#define XQ_TMD0_ONE 0x0800 /* One retry on transmit */ +#define XQ_TMD0_DEF 0x0400 /* Deferral during transmit */ + uint16 tmd1; +#define XQ_TMD1_LCO 0x1000 /* Late collision on transmit - packet not transmitted */ +#define XQ_TMD1_LCA 0x0800 /* Loss of carrier on transmit - packet not transmitted */ +#define XQ_TMD1_RTR 0x0400 /* Retry error on transmit - packet not transmitted */ +#define XQ_TMD1_TDR 0x03FF /* Time Domain Reflectometry value */ + uint16 tmd2; +#define XQ_TMD2_ERR2 0x8000 /* Error Summary. The OR of TMD2 (BBL, CER, and MIS) */ +#define XQ_TMD2_BBL 0x4000 /* Babble error on transmit */ +#define XQ_TMD2_CER 0x2000 /* Collision error on transmit */ +#define XQ_TMD2_MIS 0x1000 /* Packet lost on receive */ +#define XQ_TMD2_EOR 0x0800 /* End Of Receive Ring Reached */ +#define XQ_TMD2_RON 0x0020 /* Receiver On */ +#define XQ_TMD2_TON 0x0010 /* Transmitter On */ + uint16 tmd3; +#define XQ_TMD3_OWN 0x8000 /* Ownership field. 0 = DELQA-T, 1 = Host Driver */ +#define XQ_TMD3_FOT 0x4000 /* First Of Two flag. 1 = first in chained, 0 = no chain or last in chain */ +#define XQ_TMD3_BCT 0x0FFF /* Byte Count */ + uint16 ladr; /* Low 16bits of Buffer Address */ + uint16 hadr; /* Most significant bits of the Buffer Address */ + uint16 hostuse1; + uint16 hostuse2; +}; +#define XQ_TURBO_XM_BCNT 12 /* Transmit Buffer Descriptor Count */ + +struct receive_buffer_descriptor { + uint16 rmd0; +#define XQ_RMD0_ERR3 0x4000 /* Error Summary. The OR of FRA, CRC, OFL and BUF */ +#define XQ_RMD0_FRA 0x2000 /* Framing error on receive */ +#define XQ_RMD0_OFL 0x1000 /* Overflow error on receive (Giant packet) */ +#define XQ_RMD0_CRC 0x0800 /* CRC error on receive */ +#define XQ_RMD0_BUF 0x0400 /* Internal device buffer error. Part of Giant packet lost */ +#define XQ_RMD0_STP 0x0200 /* Start of Packet Flag */ +#define XQ_RMD0_ENP 0x0100 /* End of Packet Flag */ + uint16 rmd1; +#define XQ_RMD1_MCNT 0x0FFF /* Message byte count (including CRC) */ + uint16 rmd2; +#define XQ_RMD2_ERR4 0x8000 /* Error Summary. The OR of RMD2 (RBL, CER, and MIS) */ +#define XQ_RMD2_BBL 0x4000 /* Babble error on transmit */ +#define XQ_RMD2_CER 0x2000 /* Collision error on transmit */ +#define XQ_RMD2_MIS 0x1000 /* Packet lost on receive */ +#define XQ_RMD2_EOR 0x0800 /* End Of Receive Ring Reached */ +#define XQ_RMD2_RON 0x0020 /* Receiver On */ +#define XQ_RMD2_TON 0x0010 /* Transmitter On */ + uint16 rmd3; +#define XQ_RMD3_OWN 0x8000 /* Ownership field. 0 = DELQA-T, 1 = Host Driver */ + uint16 ladr; /* Low 16bits of Buffer Address */ + uint16 hadr; /* Most significant bits of the Buffer Address */ + uint16 hostuse1; + uint16 hostuse2; +}; +#define XQ_TURBO_RC_BCNT 32 /* Receive Buffer Descriptor Count */ + struct xq_stats { int recv; /* received packets */ - int filter; /* filtered packets */ + int dropped; /* received packets dropped */ int xmit; /* transmitted packets */ int fail; /* transmit failed */ int runt; /* runts */ + int reset; /* reset count */ int giant; /* oversize packets */ int setup; /* setup packets */ int loop; /* loopback packets */ }; +#pragma pack(2) +struct xq_mop_counters { + uint16 seconds; /* Seconds since last zeroed */ + uint32 b_rcvd; /* Bytes Received */ + uint32 b_xmit; /* Bytes Transmitted */ + uint32 p_rcvd; /* Packets Received */ + uint32 p_xmit; /* Packets Transmitted */ + uint32 mb_rcvd; /* Multicast Bytes Received */ + uint32 mp_rcvd; /* Multicast Packets Received */ + uint32 p_x_col1; /* Packets Transmitted Initially Deferred */ + uint32 p_x_col2; /* Packets Transmitted after 2 attempts */ + uint32 p_x_col3; /* Packets Transmitted after 3+ attempts */ + uint16 p_x_fail; /* Transmit Packets Aborted (Send Failure) */ + uint16 p_x_f_bitmap; /* Transmit Packets Aborted (Send Failure) Bitmap */ +#define XQ_XF_RTRY 0x0001 /* Excessive Collisions */ +#define XQ_XF_LCAR 0x0002 /* Loss of Carrier */ +#define XQ_XF_MLEN 0x0010 /* Data Block Too Long */ +#define XQ_XF_LCOL 0x0020 /* Late Collision */ + uint16 p_r_fail; /* Packets received with Error (Receive Failure) */ + uint16 p_r_f_bitmap; /* Packets received with Error (Receive Failure) Bitmap */ +#define XQ_RF_CRC 0x0001 /* Block Check Error */ +#define XQ_RF_FRAM 0x0002 /* Framing Error */ +#define XQ_RF_MLEN 0x0004 /* Message Length Error */ + uint16 h_dest_err; /* Host Counter - Unrecognized Frame Destination Error */ + uint16 r_p_lost_i; /* Receive Packet Lost: Internal Buffer Error */ + uint16 r_p_lost_s; /* Receive Packet Lost: System Buffer Error (Unavailable or Truncated) */ + uint16 h_no_buf; /* Host Counter - User Buffer Unavailable */ + uint32 mb_xmit; /* Multicast Bytes Tramsmitted */ + uint16 reserved1; /* */ + uint16 reserved2; /* */ + uint16 babble; /* Babble Counter */ +}; +#pragma pack() + struct xq_meb { /* MEB block */ uint8 type; uint8 add_lo; @@ -128,18 +250,36 @@ struct xq_device { /*+ initialized values - DO NOT MOVE */ ETH_PCALLBACK rcallback; /* read callback routine */ ETH_PCALLBACK wcallback; /* write callback routine */ - ETH_MAC mac; /* MAC address */ + ETH_MAC mac; /* Hardware MAC address */ enum xq_type type; /* controller type */ - int poll; /* poll ethernet times/sec */ + enum xq_type mode; /* controller operating mode */ + uint16 poll; /* configured poll ethernet times/sec for receive */ + uint16 coalesce_latency; /* microseconds to hold-off interrupts when not polling */ + uint16 coalesce_latency_ticks; /* instructions in coalesce_latency microseconds */ struct xq_sanity sanity; /* sanity timer information */ /*- initialized values - DO NOT MOVE */ /* I/O register storage */ - uint16 addr[6]; + uint16 rbdl[2]; uint16 xbdl[2]; uint16 var; uint16 csr; + + uint16 srr; /* Status and Response Register - DELQA-T only */ + uint16 srqr; /* Synchronous Request Register - DELQA-T only */ + uint32 iba; /* Init Block Address Register - DELQA-T only */ + uint16 icr; /* Interrupt Request Register - DELQA-T only */ + uint16 pending_interrupt; /* Pending Interrupt - DELQA-T only */ + struct xq_turbo_init_block + init; + struct transmit_buffer_descriptor + xring[XQ_TURBO_XM_BCNT]; /* Transmit Buffer Ring */ + uint32 tbindx; /* Transmit Buffer Ring Index */ + struct receive_buffer_descriptor + rring[XQ_TURBO_RC_BCNT]; /* Receive Buffer Ring */ + uint32 rbindx; /* Receive Buffer Ring Index */ + uint32 irq; /* interrupt request flag */ /* buffers, etc. */ @@ -151,11 +291,11 @@ struct xq_device { uint32 rbdl_ba; uint32 xbdl_ba; ETH_DEV* etherface; - int receiving; ETH_PACK read_buffer; ETH_PACK write_buffer; ETH_QUE ReadQ; - int idtmr; /* countdown for ID Timer */ + int32 idtmr; /* countdown for ID Timer */ + uint32 must_poll; /* receiver must poll instead of counting on asynch polls */ }; struct xq_controller { @@ -207,6 +347,7 @@ typedef struct xq_controller CTLR; #define XQ_VEC_RO 0x5C02 /* Read-Only bits */ #define XQ_VEC_RW 0xA3FD /* Read/Write bits */ +/* DEQNA - DELQA Normal Mode Buffer Descriptors */ #define XQ_DSC_V 0x8000 /* Valid bit */ #define XQ_DSC_C 0x4000 /* Chain bit */ #define XQ_DSC_E 0x2000 /* End of Message bit [Transmit only] */ @@ -214,11 +355,38 @@ typedef struct xq_controller CTLR; #define XQ_DSC_L 0x0080 /* Low Byte Termination bit [Transmit only] */ #define XQ_DSC_H 0x0040 /* High Byte Start bit [Transmit only] */ +/* DEQNA - DELQA Normal Mode Setup Packet Flags */ #define XQ_SETUP_MC 0x0001 /* multicast bit */ #define XQ_SETUP_PM 0x0002 /* promiscuous bit */ #define XQ_SETUP_LD 0x000C /* led bits */ #define XQ_SETUP_ST 0x0070 /* sanity timer bits */ +/* DELQA-T Mode - Status and Response Register (SRR) */ +#define XQ_SRR_FES 0x8000 /* Fatal Error Summary [RO] */ +#define XQ_SRR_CHN 0x4000 /* Chaining Error [RO] */ +#define XQ_SRR_NXM 0x1000 /* Non-Existant Memory Error [RO] */ +#define XQ_SRR_PAR 0x0800 /* Parity Error (Qbus) [RO] */ +#define XQ_SRR_IME 0x0400 /* Internal Memory Error [RO] */ +#define XQ_SRR_TBL 0x0200 /* Transmit Buffer Too Long Error [RO] */ +#define XQ_SRR_RESP 0x0003 /* Synchronous Response Field [RO] */ +#define XQ_SRR_TRBO 0x0001 /* Select Turbo Response [RO] */ +#define XQ_SRR_STRT 0x0002 /* Start Device Response [RO] */ +#define XQ_SRR_STOP 0x0003 /* Stop Device Response [RO] */ + +/* DELQA-T Mode - Synchronous Request Register (SRQR) */ +#define XQ_SRQR_STRT 0x0002 /* Start Device Request [WO] */ +#define XQ_SRQR_STOP 0x0003 /* Stop Device Request [WO] */ +#define XQ_SRQR_RW 0x0003 /* Writable Bits in SRQR [WO] */ + +/* DELQA-T Mode - Asynchronous Request Register (ARQR) */ +#define XQ_ARQR_TRQ 0x8000 /* Transmit Request [WO] */ +#define XQ_ARQR_RRQ 0x0080 /* Receieve Request [WO] */ +#define XQ_ARQR_SR 0x0002 /* Software Reset Request [WO] */ + +/* DELQA-T Mode - Interrupt Control Register (ICR) */ +#define XQ_ICR_ENA 0x0001 /* Interrupt Enabled [WO] */ + + /* debugging bitmaps */ #define DBG_TRC 0x0001 /* trace routine calls */ #define DBG_REG 0x0002 /* trace read/write registers */ @@ -227,7 +395,8 @@ typedef struct xq_controller CTLR; #define DBG_WRN 0x0010 /* display warnings */ #define DBG_SAN 0x0020 /* display sanity timer info */ #define DBG_SET 0x0040 /* display setup info */ -#define DBG_PCK 0x0080 /* display packets */ +#define DBG_PCK 0x0080 /* display packet headers */ +#define DBG_DAT 0x0100 /* display packet data */ #define DBG_ETH 0x8000 /* debug ethernet device */ #endif /* _PDP11_XQ_H */ diff --git a/PDP11/pdp11_xu.c b/PDP11/pdp11_xu.c index 502a6768..c8ff52f4 100644 --- a/PDP11/pdp11_xu.c +++ b/PDP11/pdp11_xu.c @@ -1,7 +1,7 @@ /* pdp11_xu.c: DEUNA/DELUA ethernet controller simulator ------------------------------------------------------------------------------ - Copyright (c) 2003-2007, David T. Hittner + Copyright (c) 2003-2011, David T. Hittner Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), @@ -30,7 +30,7 @@ Digital DELUA Users Guide, Part# EK-DELUA-UG-002 Digital DEUNA Users Guide, Part# EK-DEUNA-UG-001 These manuals can be found online at: - http://www.spies.com/~aek/pdf/dec/unibus + http://www.bitsavers.org/pdf/dec/unibus Testing performed: 1) Receives/Transmits single packet under custom RSX driver @@ -41,7 +41,13 @@ DECNET - SET HOST in/out, COPY in/out TCP/IP - PING in/out; SET HOST/TELNET in/out, COPY/FTP in/out Clustering - Successfully clustered with AlphaVMS 8.2 - 4) Runs VAX EVDWA diagnostic tests 1-10; tests 11-19 (M68000/ROM/RAM) fail + 4) VMS 4.7 on VAX780 summary: + (Jan/2011: Win7 x64 host; MS VC++ 2008; SIMH v3.8-2 rc1 base; WinPcap 4.1) + LAT - SET HOST/LAT in (no outbound exists) + DECNET - SET HOST in/out, DIR in/out, COPY in/out + TCP/IP - no kit available to test + Clustering - not tested + 5) Runs VAX EVDWA diagnostic tests 1-10; tests 11-19 (M68000/ROM/RAM) fail Known issues: 1) Most auxiliary commands are not implemented yet. @@ -56,6 +62,14 @@ Modification history: + 12-Jan-11 DTH Added SHOW XU FILTERS modifier + 11-Jan-11 DTH Corrected SELFTEST command, enabling use by VMS 3.7, VMS 4.7, and Ultrix 1.1 + 09-Dec-10 MP Added address conflict check during attach. + 06-Dec-10 MP Added loopback processing support + 30-Nov-10 MP Fixed the fact that no broadcast packets were received by the DEUNA + 15-Aug-08 MP Fixed transmitted packets to have the correct source MAC address. + Fixed incorrect address filter setting calling eth_filter(). + 23-Jan-08 MP Added debugging support to display packet headers and packet data 18-Jun-07 RMS Added UNIT_IDLE flag 03-May-07 DTH Added missing FC_RMAL command; cleared multicast on write 29-Oct-06 RMS Synced poll and clock @@ -112,6 +126,7 @@ void xu_clrint (CTLR* xu); void xu_process_receive(CTLR* xu); void xu_dump_rxring(CTLR* xu); void xu_dump_txring(CTLR* xu); +t_stat xu_show_filters (FILE* st, UNIT* uptr, int32 val, void* desc); DIB xua_dib = { IOBA_XU, IOLN_XU, &xu_rd, &xu_wr, 1, IVCL (XU), VEC_XU, {&xu_int} }; @@ -143,6 +158,8 @@ MTAB xu_mod[] = { NULL, ð_show, NULL }, { MTAB_XTD | MTAB_VDV | MTAB_NMO, 0, "STATS", "STATS", &xu_set_stats, &xu_show_stats, NULL }, + { MTAB_XTD | MTAB_VDV | MTAB_NMO, 0, "FILTERS", "FILTERS", + NULL, &xu_show_filters, NULL }, { MTAB_XTD | MTAB_VDV, 0, "TYPE", "TYPE={DEUNA|DELUA}", &xu_set_type, &xu_show_type, NULL }, { 0 }, @@ -156,6 +173,7 @@ DEBTAB xu_debug[] = { {"WARN", DBG_WRN}, {"REG", DBG_REG}, {"PACKET", DBG_PCK}, + {"DATA", DBG_DAT}, {"ETH", DBG_ETH}, {0} }; @@ -294,20 +312,39 @@ t_stat xu_set_stats (UNIT* uptr, int32 val, char* cptr, void* desc) t_stat xu_show_stats (FILE* st, UNIT* uptr, int32 val, void* desc) { - char* fmt = " %-24s%d\n"; + char* fmt = " %-26s%d\n"; CTLR* xu = xu_unit2ctlr(uptr); struct xu_stats* stats = &xu->var->stats; fprintf(st, "Ethernet statistics:\n"); - fprintf(st, fmt, "Seconds since cleared:", stats->secs); - fprintf(st, fmt, "Recv frames:", stats->frecv); - fprintf(st, fmt, "Recv dbytes:", stats->rbytes); - fprintf(st, fmt, "Xmit frames:", stats->ftrans); - fprintf(st, fmt, "Xmit dbytes:", stats->tbytes); - fprintf(st, fmt, "Recv frames(multicast):", stats->mfrecv); - fprintf(st, fmt, "Recv dbytes(multicast):", stats->mrbytes); - fprintf(st, fmt, "Xmit frames(multicast):", stats->mftrans); - fprintf(st, fmt, "Xmit dbytes(multicast):", stats->mtbytes); + fprintf(st, fmt, "Seconds since cleared:", stats->secs); + fprintf(st, fmt, "Recv frames:", stats->frecv); + fprintf(st, fmt, "Recv dbytes:", stats->rbytes); + fprintf(st, fmt, "Xmit frames:", stats->ftrans); + fprintf(st, fmt, "Xmit dbytes:", stats->tbytes); + fprintf(st, fmt, "Recv frames(multicast):", stats->mfrecv); + fprintf(st, fmt, "Recv dbytes(multicast):", stats->mrbytes); + fprintf(st, fmt, "Xmit frames(multicast):", stats->mftrans); + fprintf(st, fmt, "Xmit dbytes(multicast):", stats->mtbytes); + fprintf(st, fmt, "Loopback forward Frames:", stats->loopf); + return SCPE_OK; +} + +t_stat xu_show_filters (FILE* st, UNIT* uptr, int32 val, void* desc) +{ + CTLR* xu = xu_unit2ctlr(uptr); + char buffer[20]; + int i; + + fprintf(st, "Filters:\n"); + for (i=0; ivar->setup.macs[i], buffer); + fprintf(st, " [%2d]: %s\n", i, buffer); + } + if (xu->var->setup.multicast) + fprintf(st, "All Multicast Receive Mode\n"); + if (xu->var->setup.promiscuous) + fprintf(st, "Promiscuous Receive Mode\n"); return SCPE_OK; } @@ -359,13 +396,63 @@ void bit_stat16(uint16* stat, uint16 bits) *stat |= bits; } +t_stat xu_process_loopback(CTLR* xu, ETH_PACK* pack) +{ + ETH_PACK response; + ETH_MAC physical_address; + t_stat status; + int offset = 16 + (pack->msg[14] | (pack->msg[15] << 8)); + int function = pack->msg[offset] | (pack->msg[offset+1] << 8); + + sim_debug(DBG_TRC, xu->dev, "xu_process_loopback()\n"); + + if (function != 2 /*forward*/) + return SCPE_NOFNC; + + /* create forward response packet */ + memcpy (&response, pack, sizeof(ETH_PACK)); + memcpy (physical_address, xu->var->setup.macs[0], sizeof(ETH_MAC)); + memcpy (&response.msg[0], &response.msg[offset+2], sizeof(ETH_MAC)); + memcpy (&response.msg[6], physical_address, sizeof(ETH_MAC)); + offset += 8; + response.msg[14] = offset & 0xFF; + response.msg[15] = (offset >> 8) & 0xFF; + + /* send response packet */ + status = eth_write(xu->var->etherface, &response, NULL); + ++xu->var->stats.loopf; + + if (DBG_PCK & xu->dev->dctrl) + eth_packet_trace_ex(xu->var->etherface, response.msg, response.len, ((function == 1) ? "xu-loopbackreply" : "xu-loopbackforward"), DBG_DAT & xu->dev->dctrl, DBG_PCK); + + return status; +} + t_stat xu_process_local (CTLR* xu, ETH_PACK* pack) { - return SCPE_NOFNC; /* not implemented yet */ + /* returns SCPE_OK if local processing occurred, + otherwise returns SCPE_NOFNC or some other code */ + int protocol; + + sim_debug(DBG_TRC, xu->dev, "xu_process_local()\n"); + + protocol = pack->msg[12] | (pack->msg[13] << 8); + switch (protocol) { + case 0x0090: /* ethernet loopback */ + return xu_process_loopback(xu, pack); + break; + case 0x0260: /* MOP remote console */ + return SCPE_NOFNC; /* not implemented yet */ + break; + } + return SCPE_NOFNC; } void xu_read_callback(CTLR* xu, int status) { + if (DBG_PCK & xu->dev->dctrl) + eth_packet_trace_ex(xu->var->etherface, xu->var->read_buffer.msg, xu->var->read_buffer.len, "xu-recvd", DBG_DAT & xu->dev->dctrl, DBG_PCK); + /* process any packets locally that can be */ status = xu_process_local (xu, &xu->var->read_buffer); @@ -442,6 +529,8 @@ t_stat xu_system_id (CTLR* xu, const ETH_MAC dest, uint16 receipt_id) /* write system id */ system_id.len = 60; status = eth_write(xu->var->etherface, &system_id, NULL); + if (DBG_PCK & xu->dev->dctrl) + eth_packet_trace_ex(xu->var->etherface, system_id.msg, system_id.len, "xu-systemid", DBG_DAT & xu->dev->dctrl, DBG_PCK); return status; } @@ -524,6 +613,7 @@ void xu_setclrint(CTLR* xu, int32 bits) t_stat xu_sw_reset (CTLR* xu) { t_stat status; + int i; sim_debug(DBG_TRC, xu->dev, "xu_sw_reset()\n"); @@ -559,10 +649,12 @@ t_stat xu_sw_reset (CTLR* xu) /* reset ethernet interface */ memcpy (xu->var->setup.macs[0], xu->var->mac, sizeof(ETH_MAC)); - xu->var->setup.mac_count = 1; + for (i=0; i<6; i++) + xu->var->setup.macs[1][i] = 0xff; /* Broadcast Address */ + xu->var->setup.mac_count = 2; if (xu->var->etherface) status = eth_filter (xu->var->etherface, xu->var->setup.mac_count, - &xu->var->mac, xu->var->setup.multicast, + xu->var->setup.macs, xu->var->setup.multicast, xu->var->setup.promiscuous); /* activate device if not disabled */ @@ -670,24 +762,24 @@ int32 xu_command(CTLR* xu) case FC_RMAL: /* read multicast address list */ mtlen = (xu->var->pcb[2] & 0xFF00) >> 8; udbb = xu->var->pcb[1] | ((xu->var->pcb[2] & 03) << 16); - wstatus = Map_WriteB(udbb, mtlen * 3, (uint8*) &xu->var->setup.macs[1]); - break; + wstatus = Map_WriteB(udbb, mtlen * 3, (uint8*) &xu->var->setup.macs[2]); + break; case FC_WMAL: /* write multicast address list */ mtlen = (xu->var->pcb[2] & 0xFF00) >> 8; -sim_debug(DBG_TRC, xu->dev, "FC_WAL: mtlen=%d\n", mtlen); + sim_debug(DBG_TRC, xu->dev, "FC_WAL: mtlen=%d\n", mtlen); if (mtlen > 10) return PCSR0_PCEI; udbb = xu->var->pcb[1] | ((xu->var->pcb[2] & 03) << 16); - /* clear existing multicast list */ - for (i=1; ivar->setup.macs[i][j] = 0; - } - /* get multicast list from host */ - rstatus = Map_ReadB(udbb, mtlen * 6, (uint8*) &xu->var->setup.macs[1]); + /* clear existing multicast list */ + for (i=2; ivar->setup.macs[i][j] = 0; + } + /* get multicast list from host */ + rstatus = Map_ReadB(udbb, mtlen * 6, (uint8*) &xu->var->setup.macs[2]); if (rstatus == 0) { - xu->var->setup.mac_count = mtlen + 1; + xu->var->setup.mac_count = mtlen + 2; status = eth_filter (xu->var->etherface, xu->var->setup.mac_count, xu->var->setup.macs, xu->var->setup.multicast, xu->var->setup.promiscuous); @@ -807,7 +899,7 @@ sim_debug(DBG_TRC, xu->dev, "FC_WAL: mtlen=%d\n", mtlen); case FC_WMODE: /* write mode register */ value = xu->var->mode; xu->var->mode = xu->var->pcb[1]; -sim_debug(DBG_TRC, xu->dev, "FC_WMODE: mode=%04x\n", xu->var->mode); + sim_debug(DBG_TRC, xu->dev, "FC_WMODE: mode=%04x\n", xu->var->mode); /* set promiscuous and multicast flags */ xu->var->setup.promiscuous = (xu->var->mode & MODE_PROM) ? 1 : 0; @@ -816,7 +908,7 @@ sim_debug(DBG_TRC, xu->dev, "FC_WMODE: mode=%04x\n", xu->var->mode); /* if promiscuous or multicast flags changed, change filter */ if ((value ^ xu->var->mode) & (MODE_PROM | MODE_ENAL)) status = eth_filter (xu->var->etherface, xu->var->setup.mac_count, - &xu->var->mac, xu->var->setup.multicast, + xu->var->setup.macs, xu->var->setup.multicast, xu->var->setup.promiscuous); break; @@ -1151,6 +1243,11 @@ void xu_process_transmit(CTLR* xu) runt = 1; } + /* As described in the DEUNA User Guide (Section 4.7), the DEUNA is responsible + for inserting the appropriate source MAC address in the outgoing packet header, + so we do that now. */ + memcpy(xu->var->write_buffer.msg+6, (uint8*)&xu->var->setup.macs[0], sizeof(ETH_MAC)); + /* are we in internal loopback mode ? */ if ((xu->var->mode & MODE_LOOP) && (xu->var->mode & MODE_INTL)) { /* just put packet in receive buffer */ @@ -1160,6 +1257,9 @@ void xu_process_transmit(CTLR* xu) wstatus = eth_write(xu->var->etherface, &xu->var->write_buffer, xu->var->wcallback); if (wstatus) xu->var->pcsr0 |= PCSR0_PCEI; + else + if (DBG_PCK & xu->dev->dctrl) + eth_packet_trace_ex(xu->var->etherface, xu->var->write_buffer.msg, xu->var->write_buffer.len, "xu-write", DBG_DAT & xu->dev->dctrl, DBG_PCK); } /* update transmit status in transmit buffer */ @@ -1263,7 +1363,14 @@ void xu_port_command (CTLR* xu) break; case CMD_SELFTEST: /* SELFTEST */ - xu_sw_reset(xu); + /* + SELFTEST is a <=15-second self diagnostic test, setting various + error flags and the DONE (DNI) flag when complete. For simulation + purposes, signal completion immediately with no errors. This + inexact behavior could be incompatible with any guest machine + diagnostics that are expecting to be able to monitor the + controller's progress through the diagnostic testing. + */ xu->var->pcsr0 |= PCSR0_DNI; break; @@ -1348,7 +1455,7 @@ t_stat xu_rd(int32 *data, int32 PA, int32 access) *data = xu->var->pcsr3; break; } - sim_debug(DBG_TRC, xu->dev, "xu_rd(), PCSR%d, data=%04x\n", reg, *data); + sim_debug(DBG_REG, xu->dev, "xu_rd(), PCSR%d, data=%04x\n", reg, *data); if (PA & 1) sim_debug(DBG_WRN, xu->dev, "xu_rd(), Unexpected Odd address access of PCSR%d\n", reg); return SCPE_OK; @@ -1375,7 +1482,7 @@ t_stat xu_wr(int32 data, int32 PA, int32 access) strcpy(desc, "Unknown"); break; } - sim_debug(DBG_TRC, xu->dev, "xu_wr(), PCSR%d, data=%08x, PA=%08x, access=%d[%s]\n", reg, data, PA, access, desc); + sim_debug(DBG_REG, xu->dev, "xu_wr(), PCSR%d, data=%08x, PA=%08x, access=%d[%s]\n", reg, data, PA, access, desc); switch (reg) { case 00: /* Clear write-one-to-clear interrupt bits */ @@ -1458,6 +1565,15 @@ t_stat xu_attach(UNIT* uptr, char* cptr) xu->var->etherface = 0; return status; } + if (SCPE_OK != eth_check_address_conflict (xu->var->etherface, &xu->var->mac)) { + char buf[32]; + + eth_mac_fmt(&xu->var->mac, buf); /* format ethernet mac address */ + printf("%s: MAC Address Conflict on LAN for address %s\n", xu->dev->name, buf); + if (sim_log) fprintf (sim_log, "%s: MAC Address Conflict on LAN for address %s\n", xu->dev->name, buf); + eth_close(xu->var->etherface); + return SCPE_NOATT; + } uptr->filename = tptr; uptr->flags |= UNIT_ATT; eth_setcrc(xu->var->etherface, 1); /* enable CRC */ diff --git a/PDP11/pdp11_xu.h b/PDP11/pdp11_xu.h index e07b5a53..83a49394 100644 --- a/PDP11/pdp11_xu.h +++ b/PDP11/pdp11_xu.h @@ -1,7 +1,7 @@ /* pdp11_xu.h: DEUNA/DELUA ethernet controller information ------------------------------------------------------------------------------ - Copyright (c) 2003-2008, David T. Hittner + Copyright (c) 2003-2005, David T. Hittner Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), @@ -28,6 +28,7 @@ Modification history: + 23-Jan-08 MP Added debugging support to display packet headers and packet data 08-Dec-05 DTH Added load_server, increased UDBSIZE for system ID parameters 07-Jul-05 RMS Removed extraneous externs 05-Jan-04 DTH Added network statistics @@ -66,7 +67,7 @@ extern int32 int_req[IPL_HLVL]; #include "sim_ether.h" #define XU_QUE_MAX 500 /* message queue array */ -#define XU_FILTER_MAX 11 /* mac + 10 multicast addrs */ +#define XU_FILTER_MAX 12 /* mac + broadcast + 10 multicast addrs */ #define XU_SERVICE_INTERVAL 100 /* times per second */ #define XU_ID_TIMER_VAL 540 /* 9 min * 60 sec */ #define UDBSIZE 200 /* max size of UDB (in words) */ @@ -107,6 +108,7 @@ struct xu_stats { uint16 txccf; /* transmit collision test failure */ uint16 porterr; /* port driver errors */ uint16 bablcnt; /* babble counter */ + uint32 loopf; /* loopback frames processed */ }; struct xu_device { @@ -300,7 +302,8 @@ typedef struct xu_controller CTLR; #define DBG_TRC 0x0001 /* trace routine calls */ #define DBG_REG 0x0002 /* trace read/write registers */ #define DBG_WRN 0x0004 /* display warnings */ -#define DBG_PCK 0x0080 /* display packets */ +#define DBG_PCK 0x0080 /* display packet headers */ +#define DBG_DAT 0x0100 /* display packet data */ #define DBG_ETH 0x8000 /* debug ethernet device */ #endif /* _PDP11_XU_H */ diff --git a/sim_ether.c b/sim_ether.c index 09bd134b..feb04995 100644 --- a/sim_ether.c +++ b/sim_ether.c @@ -49,8 +49,8 @@ networking is needed - there may be known workarounds. Define one of the two macros below to enable networking: - USE_NETWORK - Create statically linked network code - USE_SHARED - Create dynamically linked network code (_WIN32 only) + USE_NETWORK - Create statically linked network code + USE_SHARED - Create dynamically linked network code (_WIN32 only) ------------------------------------------------------------------------------ @@ -118,13 +118,26 @@ Using the threaded approach may require special compile and/or link time switches (i.e. -lpthread or -pthread, etc.) Consult the documentation for your platform as - needed. - MUST_DO_SELECT - Specifies that when USE_READER_THREAD is active, that + needed. Although this may be 'less efficient' than the + non-threaded approach, the efficiency is an overall system + efficiency not necessarily a simulator efficiency. This + means that work is removed from the thread executing + simulated instructions so the simulated system will most + likely run faster (given that modern host CPUs are + multi-core and have someplace to do this work in parallel). + MUST_DO_SELECT - Specifies that, when USE_READER_THREAD is active, select() should be used to determin when available packets are ready for reading. Otherwise, we depend on the libpcap/kernel packet timeout specified on pcap_open_live. If USE_READER_THREAD is not set, then MUST_DO_SELECT is irrelevant + USE_TAP_NETWORK - Specifies that support for tap networking should be + included. This can be leveraged, along with OS bridging + capabilities to share a single LAN interface. This + allows device names of the form tap:tap0 to be specified + at open time. This functionality is only useful/needed + on *nix platforms since native sharing of Windows NIC + devices works with no external magic. NEED_PCAP_SENDPACKET - Specifies that you are using an older version of libpcap @@ -138,6 +151,89 @@ Modification history: + 09-Jan-11 MP Fixed missing crc data when USE_READER_THREAD is defined and + crc's are needed (only the pdp11_xu) + 16-Dec-10 MP added priority boost for read and write threads when + USE_READER_THREAD does I/O in separate threads. This helps + throughput since it allows these I/O bound threads to preempt + the main thread (which is executing simulated instructions). + 09-Dec-10 MP allowed more flexible parsing of MAC address strings + 09-Dec-10 MP Added support to determine if network address conflicts exist + 07-Dec-10 MP Reworked DECnet self detection to the more general approach + of loopback self when a Physical Address is being set. + 04-Dec-10 MP Changed eth_write to do nonblocking writes when + USE_READER_THREAD is defined. + 20-Aug-10 TVO Fix for Mac OSX 10.6 + 17-Jun-10 MP Fixed bug in the AUTODIN II hash filtering. + 14-Jun-10 MP Added support for integrated Tap networking interfaces on BSD + platforms. + 13-Jun-10 MP Added support for integrated Tap networking interfaces on Linux + platforms. + 31-May-10 MP Added support for more TOE (TCP Offload Engine) features for IPv4 + network traffic from the host and/or from hosts on the LAN. These + new TOE features are: LSO (Large Send Offload) and Jumbo packet + fragmentation support. These features allow a simulated network + device to suuport traffic when a host leverages a NIC's Large + Send Offload capabilities to fregment and/or segment outgoing + network traffic. Additionally a simulated network device can + reasonably exist on a LAN which is configured to use Jumbo frames. + 21-May-10 MP Added functionslity to fixup IP header checksums to accomodate + packets from a host with a NIC which has TOE (TCP Offload Engine) + enabled which is expected to implement the checksum computations + in hardware. Since we catch packets before they arrive at the + NIC the expected checksum insertions haven't been performed yet. + This processing is only done for packets sent from the hoat to + the guest we're supporting. In general this will be a relatively + small number of packets so it is done for all IP frame packets + coming from the hoat to the guest. In order to make the + determination of packets specifically arriving from the host we + need to know the hardware MAC address of the host NIC. Currently + determining a NIC's MAC address is relatively easy on Windows. + The non-windows code works on linux and may work on other *nix + platforms either as is or with slight modifications. The code, + as implemented, only messes with this activity if the host + interface MAC address can be determined. + 20-May-10 MP Added general support to deal with receiving packets smaller + than ETH_MIN_PACKET in length. These come from packets + looped back by some bridging mechanism and need to be padded + to the minimum frame size. A real NIC won't pass us any + packets like that. This fix belongs here since this layer + is responsible for interfacing to they physical layer + devices, AND it belongs here to get CRC processing right. + 05-Mar-08 MP Added optional multicast filtering support for doing + LANCE style AUTODIN II based hashed filtering. + 07-Feb-08 MP Added eth_show_dev to display ethernet state + Changed the return value from eth_read to return whether + or not a packet was read. No existing callers used or + checked constant return value that previously was being + supplied. + 29-Jan-08 MP Added eth_set_async to provide a mechanism (when + USE_READER_THREAD is enabled) to allow packet reception + to dynamically update the simulator event queue and + potentially avoid polling for I/O. This provides a minimal + overhead (no polling) maximal responsiveness for network + activities. + 29-Jan-08 MP Properly sequenced activities in eth_close to avoid a race + condition when USE_READER_THREAD is enabled. + 25-Jan-08 MP Changed the following when USE_READER_THREAD is enabled: + - Fixed bug when the simulated device doesn't need crc + in packet data which is read. + - Added call to pcap_setmintocopy to minimize packet + delivery latencies. + - Added ethq_destroy and used it to avoid a memory leak in + eth_close. + - Properly cleaned up pthread mutexes in eth_close. + Migrated to using sim_os_ms_sleep for a delay instead of + a call to select(). + Fixed the bpf filter used when no traffic is to be matched. + Reworked eth_add_packet_crc32 implementation to avoid an + extra buffer copy while reading packets. + Fixedup #ifdef's relating to USE_SHARED so that setting + USE_SHARED or USE_NETWORK will build a working network + environment. + 23-Jan-08 MP Reworked eth_packet_trace and eth_packet_trace_ex to allow + only output ethernet header+crc and provide a mechanism for + the simulated device to display full packet data debugging. 17-May-07 DTH Fixed non-ethernet device removal loop (from Naoki Hamada) 15-May-07 DTH Added dynamic loading of wpcap.dll; Corrected exceed max index bug in ethX lookup @@ -252,36 +348,23 @@ extern FILE *sim_log; t_stat eth_mac_scan (ETH_MAC* mac, char* strmac) { - int i, j; - short unsigned int num; - char cptr[18]; - int len = strlen(strmac); + int a0, a1, a2, a3, a4, a5; const ETH_MAC zeros = {0,0,0,0,0,0}; const ETH_MAC ones = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; ETH_MAC newmac; - /* format of string must be 6 double-digit hex bytes with valid separators - ideally, this mac scanner could allow more flexible formatting later */ - if (len != 17) return SCPE_ARG; - - /* copy string to local storage for mangling */ - strcpy(cptr, strmac); - - /* make sure byte separators are OK */ - for (i=2; i 0xFF) || (a1 > 0xFF) || (a2 > 0xFF) || (a3 > 0xFF) || (a4 > 0xFF) || (a5 > 0xFF)) + return SCPE_ARG; + newmac[0] = a0; + newmac[1] = a1; + newmac[2] = a2; + newmac[3] = a3; + newmac[4] = a4; + newmac[5] = a5; /* final check - mac cannot be broadcast or multicast address */ if (!memcmp(newmac, zeros, sizeof(ETH_MAC)) || /* broadcast */ @@ -354,22 +437,48 @@ uint32 eth_crc32(uint32 crc, const void* vbuf, size_t len) const unsigned char* buf = (const unsigned char*)vbuf; crc ^= mask; + while (len > 8) { + crc = (crc >> 8) ^ crcTable[ (crc ^ (*buf++)) & 0xFF ]; + crc = (crc >> 8) ^ crcTable[ (crc ^ (*buf++)) & 0xFF ]; + crc = (crc >> 8) ^ crcTable[ (crc ^ (*buf++)) & 0xFF ]; + crc = (crc >> 8) ^ crcTable[ (crc ^ (*buf++)) & 0xFF ]; + crc = (crc >> 8) ^ crcTable[ (crc ^ (*buf++)) & 0xFF ]; + crc = (crc >> 8) ^ crcTable[ (crc ^ (*buf++)) & 0xFF ]; + crc = (crc >> 8) ^ crcTable[ (crc ^ (*buf++)) & 0xFF ]; + crc = (crc >> 8) ^ crcTable[ (crc ^ (*buf++)) & 0xFF ]; + len -= 8; + } while (0 != len--) crc = (crc >> 8) ^ crcTable[ (crc ^ (*buf++)) & 0xFF ]; return(crc ^ mask); } -void eth_add_crc32(ETH_PACK* packet) +int eth_get_packet_crc32_data(const uint8 *msg, int len, uint8 *crcdata) { - if (packet->len <= ETH_MAX_PACKET) { - uint32 crc = eth_crc32(0, packet->msg, packet->len); /* calculate CRC */ + int crc_len; + + if (len <= ETH_MAX_PACKET) { + uint32 crc = eth_crc32(0, msg, len); /* calculate CRC */ uint32 ncrc = htonl(crc); /* CRC in network order */ int size = sizeof(ncrc); /* size of crc field */ - memcpy(&packet->msg[packet->len], &ncrc, size); /* append crc to packet */ - packet->crc_len = packet->len + size; /* set packet crc length */ + memcpy(crcdata, &ncrc, size); /* append crc to packet */ + crc_len = len + size; /* set packet crc length */ } else { - packet->crc_len = 0; /* appending crc would destroy packet */ + crc_len = 0; /* appending crc would destroy packet */ } + return crc_len; +} + +int eth_add_packet_crc32(uint8 *msg, int len) +{ + int crc_len; + + if (len <= ETH_MAX_PACKET) { + crc_len = eth_get_packet_crc32_data(msg, len, &msg[len]);/* append crc to packet */ + } else { + crc_len = 0; /* appending crc would destroy packet */ + } + return crc_len; } void eth_setcrc(ETH_DEV* dev, int need_crc) @@ -377,18 +486,18 @@ void eth_setcrc(ETH_DEV* dev, int need_crc) dev->need_crc = need_crc; } -void eth_packet_trace_ex(ETH_DEV* dev, const uint8 *msg, int len, char* txt, int dmp) +void eth_packet_trace_ex(ETH_DEV* dev, const uint8 *msg, int len, char* txt, int detail, uint32 reason) { - if (dev->dptr->dctrl & dev->dbit) { + if (dev->dptr->dctrl & reason) { char src[20]; char dst[20]; unsigned short* proto = (unsigned short*) &msg[12]; uint32 crc = eth_crc32(0, msg, len); eth_mac_fmt((ETH_MAC*)&msg[0], dst); eth_mac_fmt((ETH_MAC*)&msg[6], src); - sim_debug(dev->dbit, dev->dptr, "%s dst: %s src: %s proto: 0x%04X len: %d crc: %X\n", + sim_debug(reason, dev->dptr, "%s dst: %s src: %s proto: 0x%04X len: %d crc: %X\n", txt, dst, src, ntohs(*proto), len, crc); - if (dmp) { + if (detail) { int i, same, group, sidx, oidx; char outbuf[80], strbuf[18]; static char hex[] = "0123456789ABCDEF"; @@ -398,7 +507,7 @@ void eth_packet_trace_ex(ETH_DEV* dev, const uint8 *msg, int len, char* txt, int continue; } if (same > 0) { - sim_debug(dev->dbit, dev->dptr, "%04X thru %04X same as above\r\n", i-(16*same), i-1); + sim_debug(reason, dev->dptr, "%04X thru %04X same as above\n", i-(16*same), i-1); same = 0; } group = (((len - i) > 16) ? 16 : (len - i)); @@ -413,17 +522,17 @@ void eth_packet_trace_ex(ETH_DEV* dev, const uint8 *msg, int len, char* txt, int } outbuf[oidx] = '\0'; strbuf[sidx] = '\0'; - sim_debug(dev->dbit, dev->dptr, "%04X%-48s %s\r\n", i, outbuf, strbuf); + sim_debug(reason, dev->dptr, "%04X%-48s %s\n", i, outbuf, strbuf); } if (same > 0) - sim_debug(dev->dbit, dev->dptr, "%04X thru %04X same as above\r\n", i-(16*same), len-1); + sim_debug(reason, dev->dptr, "%04X thru %04X same as above\n", i-(16*same), len-1); } } } void eth_packet_trace(ETH_DEV* dev, const uint8 *msg, int len, char* txt) { - eth_packet_trace_ex(dev, msg, len, txt, 1/*len > ETH_MAX_PACKET*/); + eth_packet_trace_ex(dev, msg, len, txt, 0, dev->dbit); } char* eth_getname(int number, char* name) @@ -441,11 +550,11 @@ char* eth_getname_bydesc(char* desc, char* name) ETH_LIST list[ETH_MAX_DEVICE]; int count = eth_devices(ETH_MAX_DEVICE, list); int i; - int j=strlen(desc); + size_t j=strlen(desc); for (i=0; i min) min = len; for (i=0; iitem) { - size_t size = sizeof(struct eth_item) * max; - que->max = max; - que->item = (struct eth_item *) malloc(size); - if (que->item) { - /* init dynamic memory */ - memset(que->item, 0, size); - } else { + que->item = (struct eth_item *) calloc(max, sizeof(struct eth_item)); + if (!que->item) { /* failed to allocate memory */ char* msg = "EthQ: failed to allocate dynamic queue[%d]\r\n"; printf(msg, max); if (sim_log) fprintf(sim_log, msg, max); return SCPE_MEM; }; + que->max = max; + }; + ethq_clear(que); + return SCPE_OK; +} + +t_stat ethq_destroy(ETH_QUE* que) +{ + /* release dynamic queue if it exists */ + ethq_clear(que); + que->max = 0; + if (que->item) { + free(que->item); + que->item = NULL; }; return SCPE_OK; } @@ -557,7 +677,7 @@ void ethq_clear(ETH_QUE* que) /* clear packet array */ memset(que->item, 0, sizeof(struct eth_item) * que->max); /* clear rest of structure */ - que->count = que->head = que->tail = que->loss = que->high = 0; + que->count = que->head = que->tail = 0; } void ethq_remove(ETH_QUE* que) @@ -572,7 +692,7 @@ void ethq_remove(ETH_QUE* que) } } -void ethq_insert(ETH_QUE* que, int32 type, ETH_PACK* pack, int32 status) +void ethq_insert_data(ETH_QUE* que, int32 type, const uint8 *data, int used, int len, int crc_len, const uint8 *crc_data, int32 status) { struct eth_item* item; @@ -598,39 +718,58 @@ void ethq_insert(ETH_QUE* que, int32 type, ETH_PACK* pack, int32 status) /* set information in (new) tail item */ item = &que->item[que->tail]; item->type = type; - item->packet.len = pack->len; - item->packet.used = 0; - item->packet.crc_len = pack->crc_len; - memcpy(item->packet.msg, pack->msg, ((pack->len > pack->crc_len) ? pack->len : pack->crc_len)); + item->packet.len = len; + item->packet.used = used; + item->packet.crc_len = crc_len; + memcpy(item->packet.msg, data, ((len > crc_len) ? len : crc_len)); + if (crc_data && (crc_len > len)) + memcpy(&item->packet.msg[len], crc_data, ETH_CRC_SIZE); item->packet.status = status; } +void ethq_insert(ETH_QUE* que, int32 type, ETH_PACK* pack, int32 status) +{ + ethq_insert_data(que, type, pack->msg, pack->used, pack->len, pack->crc_len, NULL, status); +} + /*============================================================================*/ /* Non-implemented versions */ /*============================================================================*/ -#if !defined (USE_NETWORK) && !defined(USE_SHARED) +#if !defined (USE_NETWORK) && (!defined (_WIN32) || !defined (USE_SHARED)) t_stat eth_open(ETH_DEV* dev, char* name, DEVICE* dptr, uint32 dbit) {return SCPE_NOFNC;} t_stat eth_close (ETH_DEV* dev) {return SCPE_NOFNC;} +t_stat eth_check_address_conflict (ETH_DEV* dev, + ETH_MAC* const mac) + {return SCPE_NOFNC;} +t_stat eth_set_async (ETH_DEV *dev, int latency) + {return SCPE_NOFNC;} +t_stat eth_clr_async (ETH_DEV *dev) + {return SCPE_NOFNC;} t_stat eth_write (ETH_DEV* dev, ETH_PACK* packet, ETH_PCALLBACK routine) {return SCPE_NOFNC;} -t_stat eth_read (ETH_DEV* dev, ETH_PACK* packet, ETH_PCALLBACK routine) +int eth_read (ETH_DEV* dev, ETH_PACK* packet, ETH_PCALLBACK routine) {return SCPE_NOFNC;} -t_stat eth_filter (ETH_DEV* dev, int addr_count, ETH_MAC* addresses, +t_stat eth_filter (ETH_DEV* dev, int addr_count, ETH_MAC* const addresses, ETH_BOOL all_multicast, ETH_BOOL promiscuous) {return SCPE_NOFNC;} +t_stat eth_filter_hash (ETH_DEV* dev, int addr_count, ETH_MAC* const addresses, + ETH_BOOL all_multicast, ETH_BOOL promiscuous, ETH_MULTIHASH* const hash) + {return SCPE_NOFNC;} int eth_devices (int max, ETH_LIST* dev) {return -1;} -#else /* endif unimplemented */ +void eth_show_dev (FILE* st, ETH_DEV* dev) + {} +#else /* endif unimplemented */ /*============================================================================*/ /* WIN32, Linux, and xBSD routines use WinPcap and libpcap packages */ /* OpenVMS Alpha uses a WinPcap port and an associated execlet */ /*============================================================================*/ -#if defined (xBSD) && !defined(__APPLE__) +#if defined (xBSD) || defined(__APPLE__) #include #include #endif /* xBSD */ @@ -638,6 +777,20 @@ int eth_devices (int max, ETH_LIST* dev) #include #include +#ifdef USE_TAP_NETWORK +#if defined(__linux) +#include +#include +#include +#elif defined(USE_BSDTUNTAP) +#include +#include +#include +#else /* We don't know how to do this on the current platform */ +#undef USE_TAP_NETWORK +#endif +#endif /* USE_TAP_NETWORK */ + /* Allows windows to look up user-defined adapter names */ #if defined(_WIN32) #include @@ -648,8 +801,8 @@ int eth_devices (int max, ETH_LIST* dev) Etherial/WireShark capture_pcap.c */ /* Dynamic DLL load variables */ -static HINSTANCE hDll = 0; /* handle to DLL */ -static int dll_loaded = 0; /* 0=not loaded, 1=loaded, 2=DLL load failed, 3=Func load failed */ +static HINSTANCE hDll = 0; /* handle to DLL */ +static int dll_loaded = 0; /* 0=not loaded, 1=loaded, 2=DLL load failed, 3=Func load failed */ static char* no_wpcap = "wpcap load failure"; /* define pointers to pcap functions needed */ @@ -663,169 +816,191 @@ static void (*p_pcap_freecode) (struct bpf_program *); static char* (*p_pcap_geterr) (pcap_t *); static int (*p_pcap_lookupnet) (const char *, bpf_u_int32 *, bpf_u_int32 *, char *); static pcap_t* (*p_pcap_open_live) (const char *, int, int, int, char *); +static int (*p_pcap_setmintocopy) (pcap_t* handle, int); +static HANDLE (*p_pcap_getevent) (pcap_t *); static int (*p_pcap_sendpacket) (pcap_t* handle, const u_char* msg, int len); static int (*p_pcap_setfilter) (pcap_t *, struct bpf_program *); static char* (*p_pcap_lib_version) (void); /* load function pointer from DLL */ void load_function(char* function, void** func_ptr) { - *func_ptr = GetProcAddress(hDll, function); - if (*func_ptr == 0) { - char* msg = "Eth: Failed to find function '%s' in wpcap.dll\r\n"; - printf (msg, function); - if (sim_log) fprintf (sim_log, msg, function); - dll_loaded = 3; - } + *func_ptr = GetProcAddress(hDll, function); + if (*func_ptr == 0) { + char* msg = "Eth: Failed to find function '%s' in wpcap.dll\r\n"; + + printf (msg, function); + if (sim_log) fprintf (sim_log, msg, function); + dll_loaded = 3; + } } /* load wpcap.dll as required */ int load_wpcap(void) { - switch(dll_loaded) { - case 0: /* not loaded */ + switch(dll_loaded) { + case 0: /* not loaded */ /* attempt to load DLL */ - hDll = LoadLibrary(TEXT("wpcap.dll")); - if (hDll == 0) { - /* failed to load DLL */ - char* msg = "Eth: Failed to load wpcap.dll\r\n"; - char* msg2 = "Eth: You must install WinPcap 4.x to use networking\r\n"; - printf (msg); - printf (msg2); - if (sim_log) { - fprintf (sim_log, msg); - fprintf (sim_log, msg2); - } - dll_loaded = 2; - break; - } else { - /* DLL loaded OK */ - dll_loaded = 1; - } + hDll = LoadLibrary(TEXT("wpcap.dll")); + if (hDll == 0) { + /* failed to load DLL */ + char* msg = "Eth: Failed to load wpcap.dll\r\n"; + char* msg2 = "Eth: You must install WinPcap 4.x to use networking\r\n"; - /* load required functions; sets dll_load=3 on error */ - load_function("pcap_close", (void**) &p_pcap_close); - load_function("pcap_compile", (void**) &p_pcap_compile); - load_function("pcap_datalink", (void**) &p_pcap_datalink); - load_function("pcap_dispatch", (void**) &p_pcap_dispatch); - load_function("pcap_findalldevs", (void**) &p_pcap_findalldevs); - load_function("pcap_freealldevs", (void**) &p_pcap_freealldevs); - load_function("pcap_freecode", (void**) &p_pcap_freecode); - load_function("pcap_geterr", (void**) &p_pcap_geterr); - load_function("pcap_lookupnet", (void**) &p_pcap_lookupnet); - load_function("pcap_open_live", (void**) &p_pcap_open_live); - load_function("pcap_sendpacket", (void**) &p_pcap_sendpacket); - load_function("pcap_setfilter", (void**) &p_pcap_setfilter); - load_function("pcap_lib_version", (void**) &p_pcap_lib_version); + printf (msg); + printf (msg2); + if (sim_log) { + fprintf (sim_log, msg); + fprintf (sim_log, msg2); + } + dll_loaded = 2; + break; + } else { + /* DLL loaded OK */ + dll_loaded = 1; + } - if (dll_loaded == 1) { - /* log successful load */ - char* version = p_pcap_lib_version(); - printf("%s\n", version); - if (sim_log) - fprintf(sim_log, "%s\n", version); - } - break; - default: /* loaded or failed */ - break; - } - return (dll_loaded == 1) ? 1 : 0; + /* load required functions; sets dll_load=3 on error */ + load_function("pcap_close", (void**) &p_pcap_close); + load_function("pcap_compile", (void**) &p_pcap_compile); + load_function("pcap_datalink", (void**) &p_pcap_datalink); + load_function("pcap_dispatch", (void**) &p_pcap_dispatch); + load_function("pcap_findalldevs", (void**) &p_pcap_findalldevs); + load_function("pcap_freealldevs", (void**) &p_pcap_freealldevs); + load_function("pcap_freecode", (void**) &p_pcap_freecode); + load_function("pcap_geterr", (void**) &p_pcap_geterr); + load_function("pcap_lookupnet", (void**) &p_pcap_lookupnet); + load_function("pcap_open_live", (void**) &p_pcap_open_live); + load_function("pcap_setmintocopy", (void**) &p_pcap_setmintocopy); + load_function("pcap_getevent", (void**) &p_pcap_getevent); + load_function("pcap_sendpacket", (void**) &p_pcap_sendpacket); + load_function("pcap_setfilter", (void**) &p_pcap_setfilter); + load_function("pcap_lib_version", (void**) &p_pcap_lib_version); + + if (dll_loaded == 1) { + /* log successful load */ + char* version = p_pcap_lib_version(); + printf("%s\n", version); + if (sim_log) + fprintf(sim_log, "%s\n", version); + } + break; + default: /* loaded or failed */ + break; + } + return (dll_loaded == 1) ? 1 : 0; } /* define functions with dynamic revectoring */ void pcap_close(pcap_t* a) { - if (load_wpcap() != 0) { - p_pcap_close(a); - } + if (load_wpcap() != 0) { + p_pcap_close(a); + } } int pcap_compile(pcap_t* a, struct bpf_program* b, char* c, int d, bpf_u_int32 e) { - if (load_wpcap() != 0) { - return p_pcap_compile(a, b, c, d, e); - } else { - return 0; - } + if (load_wpcap() != 0) { + return p_pcap_compile(a, b, c, d, e); + } else { + return 0; + } } int pcap_datalink(pcap_t* a) { - if (load_wpcap() != 0) { - return p_pcap_datalink(a); - } else { - return 0; - } + if (load_wpcap() != 0) { + return p_pcap_datalink(a); + } else { + return 0; + } } int pcap_dispatch(pcap_t* a, int b, pcap_handler c, u_char* d) { - if (load_wpcap() != 0) { - return p_pcap_dispatch(a, b, c, d); - } else { - return 0; - } + if (load_wpcap() != 0) { + return p_pcap_dispatch(a, b, c, d); + } else { + return 0; + } } int pcap_findalldevs(pcap_if_t** a, char* b) { - if (load_wpcap() != 0) { - return p_pcap_findalldevs(a, b); - } else { - *a = 0; - strcpy(b, no_wpcap); - return -1; - } + if (load_wpcap() != 0) { + return p_pcap_findalldevs(a, b); + } else { + *a = 0; + strcpy(b, no_wpcap); + return -1; + } } void pcap_freealldevs(pcap_if_t* a) { - if (load_wpcap() != 0) { - p_pcap_freealldevs(a); - } + if (load_wpcap() != 0) { + p_pcap_freealldevs(a); + } } void pcap_freecode(struct bpf_program* a) { - if (load_wpcap() != 0) { - p_pcap_freecode(a); - } + if (load_wpcap() != 0) { + p_pcap_freecode(a); + } } char* pcap_geterr(pcap_t* a) { - if (load_wpcap() != 0) { - return p_pcap_geterr(a); - } else { - return (char*) 0; - } + if (load_wpcap() != 0) { + return p_pcap_geterr(a); + } else { + return (char*) 0; + } } int pcap_lookupnet(const char* a, bpf_u_int32* b, bpf_u_int32* c, char* d) { - if (load_wpcap() != 0) { - return p_pcap_lookupnet(a, b, c, d); - } else { - return 0; - } + if (load_wpcap() != 0) { + return p_pcap_lookupnet(a, b, c, d); + } else { + return 0; + } } pcap_t* pcap_open_live(const char* a, int b, int c, int d, char* e) { - if (load_wpcap() != 0) { - return p_pcap_open_live(a, b, c, d, e); - } else { - return (pcap_t*) 0; - } + if (load_wpcap() != 0) { + return p_pcap_open_live(a, b, c, d, e); + } else { + return (pcap_t*) 0; + } +} + +int pcap_setmintocopy(pcap_t* a, int b) { + if (load_wpcap() != 0) { + return p_pcap_setmintocopy(a, b); + } else { + return 0; + } +} + +HANDLE pcap_getevent(pcap_t* a) { + if (load_wpcap() != 0) { + return p_pcap_getevent(a); + } else { + return (HANDLE) 0; + } } int pcap_sendpacket(pcap_t* a, const u_char* b, int c) { - if (load_wpcap() != 0) { - return p_pcap_sendpacket(a, b, c); - } else { - return 0; - } + if (load_wpcap() != 0) { + return p_pcap_sendpacket(a, b, c); + } else { + return 0; + } } int pcap_setfilter(pcap_t* a, struct bpf_program* b) { - if (load_wpcap() != 0) { - return p_pcap_setfilter(a, b); - } else { - return 0; - } + if (load_wpcap() != 0) { + return p_pcap_setfilter(a, b); + } else { + return 0; + } } #endif /* Some platforms have always had pcap_sendpacket */ -#if defined(_WIN32) || defined(VMS) +#if defined(_WIN32) || defined(__VMS) #define HAS_PCAP_SENDPACKET 1 #else /* The latest libpcap and WinPcap all have pcap_sendpacket */ @@ -848,56 +1023,297 @@ int pcap_sendpacket(pcap_t* handle, const u_char* msg, int len) } #endif /* !HAS_PCAP_SENDPACKET */ +#ifdef _WIN32 +#include +#include + +static int pcap_mac_if_win32(char *AdapterName, UCHAR MACAddress[6]) +{ + LPADAPTER lpAdapter; + PPACKET_OID_DATA OidData; + BOOLEAN Status; + int ReturnValue; + HINSTANCE hDll; /* handle to DLL */ + LPADAPTER (*p_PacketOpenAdapter)(PCHAR AdapterName); + VOID (*p_PacketCloseAdapter)(LPADAPTER lpAdapter); + BOOLEAN (*p_PacketRequest)(LPADAPTER AdapterObject,BOOLEAN Set,PPACKET_OID_DATA OidData); + + hDll = LoadLibrary(TEXT("packet.dll")); + p_PacketOpenAdapter = (void *)GetProcAddress(hDll, "PacketOpenAdapter"); + p_PacketCloseAdapter = (void *)GetProcAddress(hDll, "PacketCloseAdapter"); + p_PacketRequest = (void *)GetProcAddress(hDll, "PacketRequest"); + + /* Open the selected adapter */ + + lpAdapter = p_PacketOpenAdapter(AdapterName); + + if (!lpAdapter || (lpAdapter->hFile == INVALID_HANDLE_VALUE)) { + FreeLibrary(hDll); + return -1; + } + + /* Allocate a buffer to get the MAC adress */ + + OidData = malloc(6 + sizeof(PACKET_OID_DATA)); + if (OidData == NULL) { + p_PacketCloseAdapter(lpAdapter); + FreeLibrary(hDll); + return -1; + } + + /* Retrieve the adapter MAC querying the NIC driver */ + + OidData->Oid = OID_802_3_CURRENT_ADDRESS; + + OidData->Length = 6; + memset(OidData->Data, 0, 6); + + Status = p_PacketRequest(lpAdapter, FALSE, OidData); + if(Status) { + memcpy(MACAddress, OidData->Data, 6); + ReturnValue = 0; + } else + ReturnValue = -1; + + free(OidData); + p_PacketCloseAdapter(lpAdapter); + FreeLibrary(hDll); + return ReturnValue; +} +#endif + +static void eth_get_nic_hw_addr(ETH_DEV* dev, char *devname) +{ + memset(&dev->host_nic_phy_hw_addr, 0, sizeof(dev->host_nic_phy_hw_addr)); + dev->have_host_nic_phy_addr = 0; +#ifdef _WIN32 + if (!pcap_mac_if_win32(devname, (UCHAR *)&dev->host_nic_phy_hw_addr)) + dev->have_host_nic_phy_addr = 1; +#elif !defined (__VMS) +{ + char command[1024]; + FILE *f; + + memset(command, 0, sizeof(command)); + snprintf(command, sizeof(command)-1, "ifconfig %s | grep [0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F] >NIC.hwaddr", devname); + system(command); + if (f = fopen("NIC.hwaddr", "r")) { + if (fgets(command, sizeof(command)-1, f)) { + char *p1, *p2; + + p1 = strchr(command, ':'); + while (p1) { + p2 = strchr(p1+1, ':'); + if (p2 == p1+3) { + int mac_bytes[6]; + if (6 == sscanf(p1-2, "%02x:%02x:%02x:%02x:%02x:%02x", &mac_bytes[0], &mac_bytes[1], &mac_bytes[2], &mac_bytes[3], &mac_bytes[4], &mac_bytes[5])) { + dev->host_nic_phy_hw_addr[0] = mac_bytes[0]; + dev->host_nic_phy_hw_addr[1] = mac_bytes[1]; + dev->host_nic_phy_hw_addr[2] = mac_bytes[2]; + dev->host_nic_phy_hw_addr[3] = mac_bytes[3]; + dev->host_nic_phy_hw_addr[4] = mac_bytes[4]; + dev->host_nic_phy_hw_addr[5] = mac_bytes[5]; + dev->have_host_nic_phy_addr = 1; + } + break; + } + p1 = p2; + } + } + fclose(f); + remove("NIC.hwaddr"); + } +} +#endif +} + +/* Forward declarations */ +static void +_eth_callback(u_char* info, const struct pcap_pkthdr* header, const u_char* data); + +static t_stat +_eth_write(ETH_DEV* dev, ETH_PACK* packet, ETH_PCALLBACK routine); + #if defined (USE_READER_THREAD) #include -void eth_callback(u_char* info, const struct pcap_pkthdr* header, const u_char* data); - static void * _eth_reader(void *arg) { ETH_DEV* volatile dev = (ETH_DEV*)arg; int status; +int sched_policy; +struct sched_param sched_priority; +#if defined (_WIN32) +HANDLE hWait = pcap_getevent ((pcap_t*)dev->handle); +#endif +#if defined (MUST_DO_SELECT) struct timeval timeout; +int sel_ret; +int pcap_fd; - timeout.tv_sec = 0; - timeout.tv_usec = 200*1000; +if (dev->pcap_mode) + pcap_fd = pcap_get_selectable_fd((pcap_t *)dev->handle); +else + pcap_fd = dev->fd_handle; +#endif sim_debug(dev->dbit, dev->dptr, "Reader Thread Starting\n"); - while (dev->handle) { -#if defined (MUST_DO_SELECT) - int sel_ret; + /* Boost Priority for this I/O thread vs the CPU instruction execution + thread which in general won't be readily yielding the processor when + this thread needs to run */ + pthread_getschedparam (pthread_self(), &sched_policy, &sched_priority); + ++sched_priority.sched_priority; + pthread_setschedparam (pthread_self(), sched_policy, &sched_priority); + while ((dev->pcap_mode && dev->handle) || dev->fd_handle) { +#if defined (MUST_DO_SELECT) fd_set setl; FD_ZERO(&setl); - FD_SET(pcap_get_selectable_fd((pcap_t *)dev->handle), &setl); - sel_ret = select(1+pcap_get_selectable_fd((pcap_t *)dev->handle), &setl, NULL, NULL, &timeout); + FD_SET(pcap_fd, &setl); + timeout.tv_sec = 0; + timeout.tv_usec = 250*1000; + sel_ret = select(1+pcap_fd, &setl, NULL, NULL, &timeout); if (sel_ret < 0 && errno != EINTR) break; if (sel_ret > 0) { - /* dispatch read request queue available packets */ - status = pcap_dispatch((pcap_t*)dev->handle, -1, ð_callback, (u_char*)dev); - } +#else /* !MUST_DO_SELECT */ +#if defined (_WIN32) + if (WAIT_OBJECT_0 == WaitForSingleObject (hWait, 250)) { #else - /* dispatch read request queue available packets */ - status = pcap_dispatch((pcap_t*)dev->handle, 1, ð_callback, (u_char*)dev); + if (1) { #endif +#endif /* MUST_DO_SELECT */ + if ((dev->pcap_mode) && (!dev->handle)) + break; + if ((!dev->pcap_mode) && (!dev->fd_handle)) + break; + /* dispatch read request queue available packets */ + if (dev->pcap_mode) + status = pcap_dispatch ((pcap_t*)dev->handle, -1, &_eth_callback, (u_char*)dev); +#ifdef USE_TAP_NETWORK + else { + struct pcap_pkthdr header; + int len; + u_char buf[ETH_MAX_JUMBO_FRAME]; + + memset(&header, 0, sizeof(header)); + len = read(dev->fd_handle, buf, sizeof(buf)); + if (len > 0) { + status = 1; + header.len = len; + _eth_callback((u_char *)dev, &header, buf); + } else { + status = 0; + } + } +#endif /* USE_TAP_NETWORK */ + + if ((status > 0) && (dev->asynch_io)) { + int wakeup_needed; + pthread_mutex_lock (&dev->lock); + wakeup_needed = (dev->read_queue.count != 0); + pthread_mutex_unlock (&dev->lock); + if (wakeup_needed) { + sim_debug(dev->dbit, dev->dptr, "Queueing automatic poll\n"); + sim_activate_abs (dev->dptr->units, dev->asynch_io_latency); + } + } + } } sim_debug(dev->dbit, dev->dptr, "Reader Thread Exiting\n"); return NULL; } + +static void * +_eth_writer(void *arg) +{ +ETH_DEV* volatile dev = (ETH_DEV*)arg; +struct write_request *request; +int sched_policy; +struct sched_param sched_priority; + + /* Boost Priority for this I/O thread vs the CPU instruction execution + thread which in general won't be readily yielding the processor when + this thread needs to run */ + pthread_getschedparam (pthread_self(), &sched_policy, &sched_priority); + ++sched_priority.sched_priority; + pthread_setschedparam (pthread_self(), sched_policy, &sched_priority); + + sim_debug(dev->dbit, dev->dptr, "Writer Thread Starting\n"); + + pthread_mutex_lock (&dev->writer_lock); + while ((dev->pcap_mode && dev->handle) || dev->fd_handle) { + pthread_cond_wait (&dev->writer_cond, &dev->writer_lock); + while (request = dev->write_requests) { + /* Pull buffer off request list */ + dev->write_requests = request->next; + pthread_mutex_unlock (&dev->writer_lock); + + dev->write_status = _eth_write(dev, &request->packet, NULL); + + pthread_mutex_lock (&dev->writer_lock); + /* Put buffer on free buffer list */ + request->next = dev->write_buffers; + dev->write_buffers = request; + } + } + pthread_mutex_unlock (&dev->writer_lock); + + sim_debug(dev->dbit, dev->dptr, "Writer Thread Exiting\n"); + return NULL; +} #endif +t_stat eth_set_async (ETH_DEV *dev, int latency) +{ +#if !defined(USE_READER_THREAD) || !defined(SIM_ASYNCH_IO) + char *msg = "Eth: can't operate asynchronously, must poll\r\n"; + printf ("%s", msg); + if (sim_log) fprintf (sim_log, "%s", msg); + return SCPE_NOFNC; +#else + int wakeup_needed; + + dev->asynch_io = 1; + dev->asynch_io_latency = latency; + pthread_mutex_lock (&dev->lock); + wakeup_needed = (dev->read_queue.count != 0); + pthread_mutex_unlock (&dev->lock); + if (wakeup_needed) { + sim_debug(dev->dbit, dev->dptr, "Queueing automatic poll\n"); + sim_activate_abs (dev->dptr->units, dev->asynch_io_latency); + } +#endif + return SCPE_OK; +} + +t_stat eth_clr_async (ETH_DEV *dev) +{ +#if !defined(USE_READER_THREAD) || !defined(SIM_ASYNCH_IO) + return SCPE_NOFNC; +#else + /* make sure device exists */ + if (!dev) return SCPE_UNATT; + + dev->asynch_io = 0; + return SCPE_OK; +#endif +} + t_stat eth_open(ETH_DEV* dev, char* name, DEVICE* dptr, uint32 dbit) { - const int bufsz = (BUFSIZ < ETH_MAX_PACKET) ? ETH_MAX_PACKET : BUFSIZ; + int bufsz = (BUFSIZ < ETH_MAX_PACKET) ? ETH_MAX_PACKET : BUFSIZ; char errbuf[PCAP_ERRBUF_SIZE]; char temp[1024]; char* savname = name; int num; char* msg; + if (bufsz < ETH_MAX_JUMBO_FRAME) + bufsz = ETH_MAX_JUMBO_FRAME; /* Enable handling of jumbo frames */ + /* initialize device */ eth_zero(dev); @@ -919,23 +1335,87 @@ t_stat eth_open(ETH_DEV* dev, char* name, DEVICE* dptr, uint32 dbit) /* probably is not ethX and has no description */ savname = eth_getname_byname(name, temp); if (savname == 0) /* didn't translate */ - return SCPE_OPENERR; + savname = name; } } /* attempt to connect device */ memset(errbuf, 0, sizeof(errbuf)); - dev->handle = (void*) pcap_open_live(savname, bufsz, ETH_PROMISC, PCAP_READ_TIMEOUT, errbuf); - if (!dev->handle) { /* can't open device */ - msg = "Eth: pcap_open_live error - %s\r\n"; + if (strncmp("tap:", savname, 4)) { + dev->handle = (void*) pcap_open_live(savname, bufsz, ETH_PROMISC, PCAP_READ_TIMEOUT, errbuf); + if (!dev->handle) { /* can't open device */ + msg = "Eth: pcap_open_live error - %s\r\n"; + printf (msg, errbuf); + if (sim_log) fprintf (sim_log, msg, errbuf); + return SCPE_OPENERR; + } + dev->pcap_mode = 1; + } else { + int tun = -1; /* TUN/TAP Socket */ + int on = 1; + char dev_name[64] = ""; + +#if defined(USE_TAP_NETWORK) + if (!strcmp(savname, "tap:tapN")) { + msg = "Eth: Must specify actual tap device name (i.e. tap:tap0)\r\n"; + printf (msg, errbuf); + if (sim_log) fprintf (sim_log, msg, errbuf); + return SCPE_OPENERR; + } +#endif +#if defined(__linux) && defined(USE_TAP_NETWORK) + if ((tun = open("/dev/net/tun", O_RDWR)) >= 0) { + struct ifreq ifr; /* Interface Requests */ + + memset(&ifr, 0, sizeof(ifr)); + /* Set up interface flags */ + strcpy(ifr.ifr_name, savname+4); + ifr.ifr_flags = IFF_TAP|IFF_NO_PI; + + // Send interface requests to TUN/TAP driver. + if (ioctl(tun, TUNSETIFF, &ifr) >= 0) { + if (ioctl(tun, FIONBIO, &on)) { + strncpy(errbuf, strerror(errno), sizeof(errbuf)-1); + close(tun); + } else { + dev->fd_handle = tun; + strcpy(savname, ifr.ifr_name); + } + } else + strncpy(errbuf, strerror(errno), sizeof(errbuf)-1); + } else + strncpy(errbuf, strerror(errno), sizeof(errbuf)-1); +#elif defined(USE_BSDTUNTAP) && defined(USE_TAP_NETWORK) + snprintf(dev_name, sizeof(dev_name)-1, "/dev/%s", savname+4); + dev_name[sizeof(dev_name)-1] = '\0'; + + if ((tun = open(dev_name, O_RDWR)) >= 0) { + if (ioctl(tun, FIONBIO, &on)) { + strncpy(errbuf, strerror(errno), sizeof(errbuf)-1); + close(tun); + } else { + dev->fd_handle = tun; + strcpy(savname, savname+4); + } + } else { + strncpy(errbuf, strerror(errno), sizeof(errbuf)-1); + } +#else + strncpy(errbuf, "No support for tap: devices", sizeof(errbuf)-1); +#endif /* !defined(__linux) && !defined(USE_BSDTUNTAP) */ + } + if (errbuf[0]) { + msg = "Eth: open error - %s\r\n"; printf (msg, errbuf); if (sim_log) fprintf (sim_log, msg, errbuf); return SCPE_OPENERR; - } else { - msg = "Eth: opened %s\r\n"; - printf (msg, savname); - if (sim_log) fprintf (sim_log, msg, savname); } + msg = "Eth: opened OS device %s\r\n"; + printf (msg, savname); + if (sim_log) fprintf (sim_log, msg, savname); + + /* get the NIC's hardware MAC address */ + eth_get_nic_hw_addr(dev, savname); /* save name of device */ dev->name = malloc(strlen(savname)+1); @@ -948,7 +1428,7 @@ t_stat eth_open(ETH_DEV* dev, char* name, DEVICE* dptr, uint32 dbit) #if !defined(HAS_PCAP_SENDPACKET) && defined (xBSD) && !defined (__APPLE__) /* Tell the kernel that the header is fully-formed when it gets it. This is required in order to fake the src address. */ - { + if (dev->pcap_mode) { int one = 1; ioctl(pcap_fileno(dev->handle), BIOCSHDRCMPLT, &one); } @@ -958,23 +1438,40 @@ t_stat eth_open(ETH_DEV* dev, char* name, DEVICE* dptr, uint32 dbit) { pthread_attr_t attr; +#if defined(_WIN32) + pcap_setmintocopy (dev->handle, 0); +#endif ethq_init (&dev->read_queue, 200); /* initialize FIFO queue */ pthread_mutex_init (&dev->lock, NULL); + pthread_mutex_init (&dev->writer_lock, NULL); + pthread_cond_init (&dev->writer_cond, NULL); pthread_attr_init(&attr); pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); pthread_create (&dev->reader_thread, &attr, _eth_reader, (void *)dev); + pthread_create (&dev->writer_thread, &attr, _eth_writer, (void *)dev); pthread_attr_destroy(&attr); } #else /* !defined (USE_READER_THREAD */ #ifdef USE_SETNONBLOCK /* set ethernet device non-blocking so pcap_dispatch() doesn't hang */ - if (pcap_setnonblock (dev->handle, 1, errbuf) == -1) { + if (dev->pcap_mode && (pcap_setnonblock (dev->handle, 1, errbuf) == -1)) { msg = "Eth: Failed to set non-blocking: %s\r\n"; printf (msg, errbuf); if (sim_log) fprintf (sim_log, msg, errbuf); } #endif #endif /* !defined (USE_READER_THREAD */ +#if defined (__APPLE__) + if (1) { + /* Deliver packets immediately, needed for OS X 10.6.2 and later + * (Snow-Leopard). + * See this thread on libpcap and Mac Os X 10.6 Snow Leopard on + * the tcpdump mailinglist: http://seclists.org/tcpdump/2010/q1/110 + */ + int v = 1; + ioctl(pcap_fileno(dev->handle), BIOCIMMEDIATE, &v); + } +#endif return SCPE_OK; } @@ -982,6 +1479,7 @@ t_stat eth_close(ETH_DEV* dev) { char* msg = "Eth: closed %s\r\n"; pcap_t *pcap; + int pcap_fd = dev->fd_handle; /* make sure device exists */ if (!dev) return SCPE_UNATT; @@ -989,14 +1487,40 @@ t_stat eth_close(ETH_DEV* dev) /* close the device */ pcap = (pcap_t *)dev->handle; dev->handle = NULL; - pcap_close(pcap); - printf (msg, dev->name); - if (sim_log) fprintf (sim_log, msg, dev->name); + dev->fd_handle = 0; + dev->have_host_nic_phy_addr = 0; #if defined (USE_READER_THREAD) pthread_join (dev->reader_thread, NULL); + pthread_mutex_destroy (&dev->lock); + pthread_cond_signal (&dev->writer_cond); + pthread_join (dev->writer_thread, NULL); + pthread_mutex_destroy (&dev->writer_lock); + pthread_cond_destroy (&dev->writer_cond); + if (1) { + struct write_request *buffer; + + while (buffer = dev->write_buffers) { + dev->write_buffers = buffer->next; + free(buffer); + } + while (buffer = dev->write_requests) { + dev->write_requests = buffer->next; + free(buffer); + } + } + ethq_destroy (&dev->read_queue); /* release FIFO queue */ #endif + if (dev->pcap_mode) + pcap_close(pcap); +#ifdef USE_TAP_NETWORK + else + close(pcap_fd); +#endif + printf (msg, dev->name); + if (sim_log) fprintf (sim_log, msg, dev->name); + /* clean up the mess */ free(dev->name); eth_zero(dev); @@ -1004,27 +1528,30 @@ t_stat eth_close(ETH_DEV* dev) return SCPE_OK; } -t_stat eth_reflect(ETH_DEV* dev, ETH_MAC mac) +t_stat eth_check_address_conflict (ETH_DEV* dev, + ETH_MAC* const mac) { ETH_PACK send, recv; t_stat status; - int i; - struct timeval delay; + int responses = 0; - /* build a packet */ + sim_debug(dev->dbit, dev->dptr, "Determining Address Conflict...\n"); + + /* build a loopback forward request packet */ memset (&send, 0, sizeof(ETH_PACK)); send.len = ETH_MIN_PACKET; /* minimum packet size */ memcpy(&send.msg[0], mac, sizeof(ETH_MAC)); /* target address */ memcpy(&send.msg[6], mac, sizeof(ETH_MAC)); /* source address */ send.msg[12] = 0x90; /* loopback packet type */ - for (i=14; ireflections = 0; eth_filter(dev, 1, (ETH_MAC *)mac, 0, 0); /* send the packet */ - status = eth_write (dev, &send, NULL); + status = _eth_write (dev, &send, NULL); if (status != SCPE_OK) { char *msg; msg = "Eth: Error Transmitting packet: %s\r\n" @@ -1035,24 +1562,38 @@ t_stat eth_reflect(ETH_DEV* dev, ETH_MAC mac) return status; } - /* if/when we have a sim_os_msleep() we'll use it here instead of this select() */ - delay.tv_sec = 0; - delay.tv_usec = 50*1000; - select(0, NULL, NULL, NULL, &delay); /* make sure things settle into the read path */ + sim_os_ms_sleep (300); /* time for a conflicting host to respond */ - /* empty the read queue and count the reflections */ + /* empty the read queue and count the responses */ do { memset (&recv, 0, sizeof(ETH_PACK)); status = eth_read (dev, &recv, NULL); - if (memcmp(send.msg, recv.msg, ETH_MIN_PACKET)== 0) - dev->reflections++; + if (memcmp(send.msg, recv.msg, 14)== 0) + responses++; } while (recv.len > 0); + sim_debug(dev->dbit, dev->dptr, "Address Conflict = %d\n", responses); + return responses; +} + +t_stat eth_reflect(ETH_DEV* dev) +{ + /* Test with an address no NIC should have. */ + /* We do this to avoid reflections from the wire, */ + /* in the event that a simulated NIC has a MAC address conflict. */ + ETH_MAC mac = {0xfe,0xff,0xff,0xff,0xff,0xfe}; + + sim_debug(dev->dbit, dev->dptr, "Determining Reflections...\n"); + + dev->reflections = 0; + dev->reflections = eth_check_address_conflict (dev, &mac); + sim_debug(dev->dbit, dev->dptr, "Reflections = %d\n", dev->reflections); return dev->reflections; } -t_stat eth_write(ETH_DEV* dev, ETH_PACK* packet, ETH_PCALLBACK routine) +static +t_stat _eth_write(ETH_DEV* dev, ETH_PACK* packet, ETH_PCALLBACK routine) { int status = 1; /* default to failure */ @@ -1067,11 +1608,18 @@ t_stat eth_write(ETH_DEV* dev, ETH_PACK* packet, ETH_PCALLBACK routine) eth_packet_trace (dev, packet->msg, packet->len, "writing"); /* dispatch write request (synchronous; no need to save write info to dev) */ - status = pcap_sendpacket((pcap_t*)dev->handle, (u_char*)packet->msg, packet->len); + if (dev->pcap_mode) + status = pcap_sendpacket((pcap_t*)dev->handle, (u_char*)packet->msg, packet->len); +#ifdef USE_TAP_NETWORK + else + status = ((packet->len == write(dev->fd_handle, (void *)packet->msg, packet->len)) ? 0 : -1); +#endif - /* detect sending of decnet loopback packet */ - if ((status == 0) && DECNET_SELF_FRAME(dev->decnet_addr, packet->msg)) - dev->decnet_self_sent += dev->reflections; + /* detect sending of loopback packet */ + if ((status == 0) && (LOOPBACK_SELF_FRAME(dev->physical_addr, packet->msg))) { + dev->loopback_self_sent += dev->reflections; + dev->loopback_self_sent_total++; + } } /* if packet->len */ @@ -1082,19 +1630,480 @@ t_stat eth_write(ETH_DEV* dev, ETH_PACK* packet, ETH_PCALLBACK routine) return ((status == 0) ? SCPE_OK : SCPE_IOERR); } -void eth_callback(u_char* info, const struct pcap_pkthdr* header, const u_char* data) +t_stat eth_write(ETH_DEV* dev, ETH_PACK* packet, ETH_PCALLBACK routine) +{ +#ifdef USE_READER_THREAD + struct write_request *request; + int write_queue_size = 1; + + /* make sure device exists */ + if (!dev) return SCPE_UNATT; + + /* Get a buffer */ + pthread_mutex_lock (&dev->writer_lock); + if (request = dev->write_buffers) + dev->write_buffers = request->next; + pthread_mutex_unlock (&dev->writer_lock); + if (!request) + request = malloc(sizeof(*request)); + + /* Copy buffer contents */ + request->packet.len = packet->len; + request->packet.used = packet->used; + request->packet.status = packet->status; + request->packet.crc_len = packet->crc_len; + memcpy(request->packet.msg, packet->msg, packet->len); + + /* Insert buffer at the end of the write list (to make sure that */ + /* packets make it to the wire in the order they were presented here) */ + pthread_mutex_lock (&dev->writer_lock); + request->next = NULL; + if (dev->write_requests) { + struct write_request *last_request = dev->write_requests; + + ++write_queue_size; + while (last_request->next) { + last_request = last_request->next; + ++write_queue_size; + } + last_request->next = request; + } else + dev->write_requests = request; + if (write_queue_size > dev->write_queue_peak) + dev->write_queue_peak = write_queue_size; + pthread_mutex_unlock (&dev->writer_lock); + + /* Awaken writer thread to perform actual write */ + pthread_cond_signal (&dev->writer_cond); + + /* Return with a status from some prior write */ + if (routine) + (routine)(dev->write_status); + return dev->write_status; +#else + return _eth_write(dev, packet, routine); +#endif +} + +static int +_eth_hash_lookup(ETH_MULTIHASH hash, const u_char* data) +{ + int key = 0x3f & (eth_crc32(0, data, 6) >> 26); + + key ^= 0x3f; + return (hash[key>>3] & (1 << (key&0x7))); +} + +static int +_eth_hash_validate(ETH_MAC *MultiCastList, int count, ETH_MULTIHASH hash) +{ + ETH_MULTIHASH lhash; + int i; + + memset(lhash, 0, sizeof(lhash)); + for (i=0; i> 26); + + key ^= 0x3F; + printf("MAC: %02X:%02X:%02X:%02X:%02X:%02X Key: %X, Byte: %X, Val: %X\n", + MultiCastList[i][0], MultiCastList[i][1], MultiCastList[i][2], MultiCastList[i][3], MultiCastList[i][4], MultiCastList[i][5], + key, key>>3, (1 << (key&0x7))); + lhash[key>>3] |= (1 << (key&0x7)); + } + if (memcmp(hash, lhash, sizeof(lhash))) { + printf("Inconsistent Computed Hash:\n"); + printf("Should be: %02X %02X %02X %02X %02X %02X %02X %02X\n", + hash[0], hash[1], hash[2], hash[3], + hash[4], hash[5], hash[6], hash[7]); + printf("Was: %02X %02X %02X %02X %02X %02X %02X %02X\n", + lhash[0], lhash[1], lhash[2], lhash[3], + lhash[4], lhash[5], lhash[6], lhash[7]); + } + printf("Should be: %02X %02X %02X %02X %02X %02X %02X %02X\n", + hash[0], hash[1], hash[2], hash[3], + hash[4], hash[5], hash[6], hash[7]); + printf("Was: %02X %02X %02X %02X %02X %02X %02X %02X\n", + lhash[0], lhash[1], lhash[2], lhash[3], + lhash[4], lhash[5], lhash[6], lhash[7]); + return 0; +} + +static void +_eth_test_multicast_hash() +{ + ETH_MAC tMacs[] = { + {0xAB, 0x00, 0x04, 0x01, 0xAC, 0x10}, + {0xAB, 0x00, 0x00, 0x04, 0x00, 0x00}, + {0x09, 0x00, 0x2B, 0x00, 0x00, 0x0F}, + {0x09, 0x00, 0x2B, 0x02, 0x01, 0x04}, + {0x09, 0x00, 0x2B, 0x02, 0x01, 0x07}, + {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, + {0x01, 0x00, 0x5E, 0x00, 0x00, 0x01}}; + ETH_MULTIHASH thash = {0x01, 0x40, 0x00, 0x00, 0x48, 0x88, 0x40, 0x00}; + + _eth_hash_validate(tMacs, sizeof(tMacs)/sizeof(tMacs[0]), thash); +} + +/* The IP header */ +struct IPHeader { + uint8 verhlen; /* Version & Header Length in dwords */ +#define IP_HLEN(IP) (((IP)->verhlen&0xF)<<2) /* Header Length in Bytes */ +#define IP_VERSION(IP) ((((IP)->verhlen)>>4)&0xF) /* IP Version */ + uint8 tos; /* Type of service */ + uint16 total_len; /* Length of the packet in dwords */ + uint16 ident; /* unique identifier */ + uint16 flags; /* Fragmentation Flags */ +#define IP_DF_FLAG (0x4000) +#define IP_MF_FLAG (0x2000) +#define IP_OFFSET_MASK (0x1FFF) +#define IP_FRAG_DF(IP) (ntohs(((IP)->flags))&IP_DF_FLAG) +#define IP_FRAG_MF(IP) (ntohs(((IP)->flags))&IP_MF_FLAG) +#define IP_FRAG_OFFSET(IP) (ntohs(((IP)->flags))&IP_OFFSET_MASK) + uint8 ttl; /* Time to live */ + uint8 proto; /* Protocol number (TCP, UDP etc) */ + uint16 checksum; /* IP checksum */ + uint32 source_ip; /* Source Address */ + uint32 dest_ip; /* Destination Address */ +}; + +/* ICMP header */ +struct ICMPHeader { + uint8 type; /* ICMP packet type */ + uint8 code; /* Type sub code */ + uint16 checksum; /* ICMP Checksum */ + uint32 otherstuff[1];/* optional data */ +}; + +struct UDPHeader { + uint16 source_port; + uint16 dest_port; + uint16 length; /* The length of the entire UDP datagram, including both header and Data fields. */ + uint16 checksum; +}; + +struct TCPHeader { + uint16 source_port; + uint16 dest_port; + uint32 sequence_number; + uint32 acknowledgement_number; + uint16 data_offset_and_flags; +#define TCP_DATA_OFFSET(TCP) ((ntohs((TCP)->data_offset_and_flags)>>12)<<2) +#define TCP_CWR_FLAG (0x80) +#define TCP_ECR_FLAG (0x40) +#define TCP_URG_FLAG (0x20) +#define TCP_ACK_FLAG (0x10) +#define TCP_PSH_FLAG (0x08) +#define TCP_RST_FLAG (0x04) +#define TCP_SYN_FLAG (0x02) +#define TCP_FIN_FLAG (0x01) +#define TCP_FLAGS_MASK (0xFF) + uint16 window; + uint16 checksum; + uint16 urgent; + uint16 otherstuff[1]; /* The rest of the packet */ +}; + +#ifndef IPPROTO_TCP +#define IPPROTO_TCP 6 /* tcp */ +#endif +#ifndef IPPROTO_UDP +#define IPPROTO_UDP 17 /* user datagram protocol */ +#endif +#ifndef IPPROTO_ICMP +#define IPPROTO_ICMP 1 /* control message protocol */ +#endif + +static uint16 +ip_checksum(uint16 *buffer, int size) +{ + unsigned long cksum = 0; + + /* Sum all the words together, adding the final byte if size is odd */ + while (size > 1) { + cksum += *buffer++; + size -= sizeof(*buffer); + } + if (size) { + uint8 endbytes[2]; + + endbytes[0] = *((uint8 *)buffer); + endbytes[1] = 0; + cksum += *((uint16 *)endbytes); + } + + /* Do a little shuffling */ + cksum = (cksum >> 16) + (cksum & 0xffff); + cksum += (cksum >> 16); + + /* Return the bitwise complement of the resulting mishmash */ + return (uint16)(~cksum); +} + +static uint16 +pseudo_checksum(uint16 len, uint16 proto, uint16 *src_addr, uint16 *dest_addr, uint8 *buff) +{ + uint32 sum; + + /* Sum the data first */ + sum = 0xffff&(~ip_checksum((uint16 *)buff, len)); + + /* add the pseudo header which contains the IP source and destinationn addresses */ + sum += src_addr[0]; + sum += src_addr[1]; + sum += dest_addr[0]; + sum += dest_addr[1]; + /* and the protocol number and the length of the UDP packet */ + sum = sum + htons(proto) + htons(len); + + /* Do a little shuffling */ + sum = (sum >> 16) + (sum & 0xffff); + sum += (sum >> 16); + + /* Return the bitwise complement of the resulting mishmash */ + return (uint16)(~sum); +} + +static void +_eth_fix_ip_jumbo_offload(ETH_DEV* dev, const u_char* msg, int len) +{ + unsigned short* proto = (unsigned short*) &msg[12]; + struct IPHeader *IP; + struct TCPHeader *TCP = NULL; + struct UDPHeader *UDP; + struct ICMPHeader *ICMP; + uint16 orig_checksum; + uint16 payload_len; + uint16 mtu_payload; + uint16 ip_flags; + uint16 frag_offset; + struct pcap_pkthdr header; + uint16 tcp_flags; + + /* Only interested in IP frames */ + if (ntohs(*proto) != 0x0800) { + ++dev->jumbo_dropped; /* Non IP Frames are dropped */ + return; + } + IP = (struct IPHeader *)&msg[14]; + if (IP_VERSION(IP) != 4) { + ++dev->jumbo_dropped; /* Non IPv4 jumbo frames are dropped */ + return; + } + if ((IP_HLEN(IP) > len) || (ntohs(IP->total_len) > len)) { + ++dev->jumbo_dropped; /* Bogus header length frames are dropped */ + return; + } + if (IP_FRAG_OFFSET(IP) || IP_FRAG_MF(IP)) { + ++dev->jumbo_dropped; /* Previously fragmented, but currently jumbo sized frames are dropped */ + return; + } + switch (IP->proto) { + case IPPROTO_UDP: + UDP = (struct UDPHeader *)(((char *)IP)+IP_HLEN(IP)); + if (ntohs(UDP->length) > (len-IP_HLEN(IP))) { + ++dev->jumbo_dropped; /* Bogus UDP packet length (packet contained length exceeds packet size) frames are dropped */ + return; + } + if (UDP->checksum == 0) + break; /* UDP Cghecksums are disabled */ + orig_checksum = UDP->checksum; + UDP->checksum = 0; + UDP->checksum = pseudo_checksum(ntohs(UDP->length), IPPROTO_UDP, (uint16 *)(&IP->source_ip), (uint16 *)(&IP->dest_ip), (uint8 *)UDP); + if (orig_checksum != UDP->checksum) + eth_packet_trace (dev, msg, len, "reading jumbo UDP header Checksum Fixed"); + break; + case IPPROTO_ICMP: + ICMP = (struct ICMPHeader *)(((char *)IP)+IP_HLEN(IP)); + orig_checksum = ICMP->checksum; + ICMP->checksum = 0; + ICMP->checksum = ip_checksum((uint16 *)ICMP, ntohs(IP->total_len)-IP_HLEN(IP)); + if (orig_checksum != ICMP->checksum) + eth_packet_trace (dev, msg, len, "reading jumbo ICMP header Checksum Fixed"); + break; + case IPPROTO_TCP: + TCP = (struct TCPHeader *)(((char *)IP)+IP_HLEN(IP)); + if ((TCP_DATA_OFFSET(TCP) > (len-IP_HLEN(IP))) || (TCP_DATA_OFFSET(TCP) < 20)) { + ++dev->jumbo_dropped; /* Bogus TCP packet header length (packet contained length exceeds packet size) frames are dropped */ + return; + } + /* We don't do anything with the TCP checksum since we're going to resegment the TCP data below */ + break; + default: + ++dev->jumbo_dropped; /* We onlt handle UDP, ICMP and TCP jumbo frames others are dropped */ + return; + } + /* Reasonable Checksums are now in the jumbo packet, but we've got to actually */ + /* deliver ONLY standard sized ethernet frames. Our job here is to now act as */ + /* a router might have to and fragment these IPv4 frames as they are delivered */ + /* into the virtual NIC. We do this by walking down the packet and dispatching */ + /* a chunk at a time recomputing an appropriate header for each chunk. For */ + /* datagram oriented protocols (UDP and ICMP) this is done by simple packet */ + /* fragmentation. For TCP this is done by breaking large packets into separate */ + /* TCP packets. */ + memset(&header, 0, sizeof(header)); + switch (IP->proto) { + case IPPROTO_UDP: + case IPPROTO_ICMP: + ++dev->jumbo_fragmented; + payload_len = ntohs(IP->total_len) - IP_HLEN(IP); + mtu_payload = ETH_MIN_JUMBO_FRAME - 14 - IP_HLEN(IP); + frag_offset = 0; + while (payload_len > 0) { + ip_flags = frag_offset; + if (payload_len > mtu_payload) { + ip_flags |= IP_MF_FLAG; + IP->total_len = htons(((mtu_payload>>3)<<3) + IP_HLEN(IP)); + } else { + IP->total_len = htons(payload_len + IP_HLEN(IP)); + } + IP->flags = htons(ip_flags); + IP->checksum = 0; + IP->checksum = ip_checksum((uint16 *)IP, IP_HLEN(IP)); + header.len = 14 + ntohs(IP->total_len); + eth_packet_trace (dev, ((u_char *)IP)-14, header.len, "reading Datagram fragment"); +#if ETH_MIN_JUMBO_FRAME < ETH_MAX_PACKET + { /* Debugging is easier it we read packets directly with pcap */ + ETH_PACK pkt; + + memset(&pkt, 0, sizeof(pkt)); + memcpy(pkt.msg, ((u_char *)IP)-14, header.len); + pkt.len = header.len; + _eth_write(dev, &pkt, NULL); + } +#else + _eth_callback((u_char *)dev, &header, ((u_char *)IP)-14); +#endif + payload_len -= (ntohs(IP->total_len) - IP_HLEN(IP)); + frag_offset += (ntohs(IP->total_len) - IP_HLEN(IP))>>3; + if (payload_len > 0) { + /* Move the MAC and IP headers down to just prior to the next payload segment */ + memcpy(((u_char *)IP) + ntohs(IP->total_len) - (14 + IP_HLEN(IP)), ((u_char *)IP) - 14, 14 + IP_HLEN(IP)); + IP = (struct IPHeader *)(((u_char *)IP) + ntohs(IP->total_len) - IP_HLEN(IP)); + } + } + break; + case IPPROTO_TCP: + ++dev->jumbo_fragmented; + TCP = (struct TCPHeader *)(((char *)IP)+IP_HLEN(IP)); + tcp_flags = ntohs(TCP->data_offset_and_flags)&TCP_FLAGS_MASK; + TCP->data_offset_and_flags = htons(((TCP_DATA_OFFSET(TCP)>>2)<<12)|TCP_ACK_FLAG); + payload_len = ntohs(IP->total_len) - IP_HLEN(IP) - TCP_DATA_OFFSET(TCP); + mtu_payload = ETH_MIN_JUMBO_FRAME - 14 - IP_HLEN(IP) - TCP_DATA_OFFSET(TCP); + while (payload_len > 0) { + if (payload_len > mtu_payload) { + IP->total_len = htons(mtu_payload + IP_HLEN(IP) + TCP_DATA_OFFSET(TCP)); + } else { + TCP->data_offset_and_flags = htons(ntohs(TCP->data_offset_and_flags)|(tcp_flags&(TCP_PSH_FLAG|TCP_ACK_FLAG|TCP_FIN_FLAG|TCP_RST_FLAG))); + IP->total_len = htons(payload_len + IP_HLEN(IP) + TCP_DATA_OFFSET(TCP)); + } + IP->checksum = 0; + IP->checksum = ip_checksum((uint16 *)IP, IP_HLEN(IP)); + TCP->checksum = 0; + TCP->checksum = pseudo_checksum(ntohs(IP->total_len)-IP_HLEN(IP), IPPROTO_TCP, (uint16 *)(&IP->source_ip), (uint16 *)(&IP->dest_ip), (uint8 *)TCP); + header.len = 14 + ntohs(IP->total_len); + eth_packet_trace_ex (dev, ((u_char *)IP)-14, header.len, "reading TCP segment", 1, dev->dbit); +#if ETH_MIN_JUMBO_FRAME < ETH_MAX_PACKET + { /* Debugging is easier it we read packets directly with pcap */ + ETH_PACK pkt; + + memset(&pkt, 0, sizeof(pkt)); + memcpy(pkt.msg, ((u_char *)IP)-14, header.len); + pkt.len = header.len; + _eth_write(dev, &pkt, NULL); + } +#else + _eth_callback((u_char *)dev, &header, ((u_char *)IP)-14); +#endif + payload_len -= (ntohs(IP->total_len) - (IP_HLEN(IP) + TCP_DATA_OFFSET(TCP))); + if (payload_len > 0) { + /* Move the MAC, IP and TCP headers down to just prior to the next payload segment */ + memcpy(((u_char *)IP) + ntohs(IP->total_len) - (14 + IP_HLEN(IP) + TCP_DATA_OFFSET(TCP)), ((u_char *)IP) - 14, 14 + IP_HLEN(IP) + TCP_DATA_OFFSET(TCP)); + IP = (struct IPHeader *)(((u_char *)IP) + ntohs(IP->total_len) - (IP_HLEN(IP) + TCP_DATA_OFFSET(TCP))); + TCP = (struct TCPHeader *)(((char *)IP)+IP_HLEN(IP)); + TCP->sequence_number = htonl(mtu_payload + ntohl(TCP->sequence_number)); + } + } + break; + } +} + +static void +_eth_fix_ip_xsum_offload(ETH_DEV* dev, u_char* msg, int len) +{ + unsigned short* proto = (unsigned short*) &msg[12]; + struct IPHeader *IP; + struct TCPHeader *TCP; + struct UDPHeader *UDP; + struct ICMPHeader *ICMP; + uint16 orig_checksum; + + /* Only need to process locally originated packets */ + if ((!dev->have_host_nic_phy_addr) || (memcmp(msg+6, dev->host_nic_phy_hw_addr, 6))) + return; + /* Only interested in IP frames */ + if (ntohs(*proto) != 0x0800) + return; + IP = (struct IPHeader *)&msg[14]; + if (IP_VERSION(IP) != 4) + return; /* Only interested in IPv4 frames */ + if ((IP_HLEN(IP) > len) || (ntohs(IP->total_len) > len)) + return; /* Bogus header length */ + orig_checksum = IP->checksum; + IP->checksum = 0; + IP->checksum = ip_checksum((uint16 *)IP, IP_HLEN(IP)); + if (orig_checksum != IP->checksum) + eth_packet_trace (dev, msg, len, "reading IP header Checksum Fixed"); + if (IP_FRAG_OFFSET(IP) || IP_FRAG_MF(IP)) + return; /* Insufficient data to compute payload checksum */ + switch (IP->proto) { + case IPPROTO_UDP: + UDP = (struct UDPHeader *)(((char *)IP)+IP_HLEN(IP)); + if (ntohs(UDP->length) > (len-IP_HLEN(IP))) + return; /* packet contained length exceeds packet size */ + if (UDP->checksum == 0) + return; /* UDP Cghecksums are disabled */ + orig_checksum = UDP->checksum; + UDP->checksum = 0; + UDP->checksum = pseudo_checksum(ntohs(UDP->length), IPPROTO_UDP, (uint16 *)(&IP->source_ip), (uint16 *)(&IP->dest_ip), (uint8 *)UDP); + if (orig_checksum != UDP->checksum) + eth_packet_trace (dev, msg, len, "reading UDP header Checksum Fixed"); + break; + case IPPROTO_TCP: + TCP = (struct TCPHeader *)(((char *)IP)+IP_HLEN(IP)); + orig_checksum = TCP->checksum; + TCP->checksum = 0; + TCP->checksum = pseudo_checksum(ntohs(IP->total_len)-IP_HLEN(IP), IPPROTO_TCP, (uint16 *)(&IP->source_ip), (uint16 *)(&IP->dest_ip), (uint8 *)TCP); + if (orig_checksum != TCP->checksum) + eth_packet_trace (dev, msg, len, "reading TCP header Checksum Fixed"); + break; + case IPPROTO_ICMP: + ICMP = (struct ICMPHeader *)(((char *)IP)+IP_HLEN(IP)); + orig_checksum = ICMP->checksum; + ICMP->checksum = 0; + ICMP->checksum = ip_checksum((uint16 *)ICMP, ntohs(IP->total_len)-IP_HLEN(IP)); + if (orig_checksum != ICMP->checksum) + eth_packet_trace (dev, msg, len, "reading ICMP header Checksum Fixed"); + break; + } +} + +static void +_eth_callback(u_char* info, const struct pcap_pkthdr* header, const u_char* data) { ETH_DEV* dev = (ETH_DEV*) info; #ifdef USE_BPF int to_me = 1; + + /* AUTODIN II hash mode? */ + if ((dev->hash_filter) && (data[0] & 0x01) && (!dev->promiscuous) && (!dev->all_multicast)) + to_me = _eth_hash_lookup(dev->hash, data); #else /* !USE_BPF */ int to_me = 0; int from_me = 0; int i; -#ifdef ETH_DEBUG -// eth_packet_trace (dev, data, header->len, "received"); -#endif + eth_packet_trace (dev, data, header->len, "received"); + for (i = 0; i < dev->addr_count; i++) { if (memcmp(data, dev->filter_address[i], 6) == 0) to_me = 1; if (memcmp(&data[6], dev->filter_address[i], 6) == 0) from_me = 1; @@ -1105,13 +2114,19 @@ void eth_callback(u_char* info, const struct pcap_pkthdr* header, const u_char* /* promiscuous mode? */ if (dev->promiscuous) to_me = 1; + + /* AUTODIN II hash mode? */ + if ((dev->hash_filter) && (!to_me) && (data[0] & 0x01)) + to_me = _eth_hash_lookup(dev->hash, data); #endif /* USE_BPF */ - /* detect sending of decnet loopback packet */ - if (DECNET_SELF_FRAME(dev->decnet_addr, data)) { - /* lower reflection count - if already zero, pass it on */ - if (dev->decnet_self_sent > 0) { - dev->decnet_self_sent--; + /* detect reception of loopback packet to our physical address */ + if (LOOPBACK_SELF_FRAME(dev->physical_addr, data)) { + dev->loopback_self_rcvd_total++; + /* lower reflection count - if already zero, pass it on */ + if (dev->loopback_self_sent > 0) { + eth_packet_trace (dev, data, header->len, "ignored"); + dev->loopback_self_sent--; to_me = 0; } #ifndef USE_BPF @@ -1125,26 +2140,60 @@ void eth_callback(u_char* info, const struct pcap_pkthdr* header, const u_char* #else /* !USE_BPF */ if (to_me && !from_me) { #endif + if (header->len > ETH_MIN_JUMBO_FRAME) { + _eth_fix_ip_jumbo_offload(dev, data, header->len); + return; + } #if defined (USE_READER_THREAD) - ETH_PACK tmp_packet; + if (1) { + int crc_len = 0; + uint8 crc_data[4]; + uint32 len = header->len; + u_char* moved_data = NULL; - /* set data in passed read packet */ - tmp_packet.len = header->len; - memcpy(tmp_packet.msg, data, header->len); - if (dev->need_crc) - eth_add_crc32(&tmp_packet); + if (header->len < ETH_MIN_PACKET) { /* Pad runt packets before CRC append */ + moved_data = malloc(ETH_MIN_PACKET); + memcpy(moved_data, data, len); + memset(moved_data + len, 0, ETH_MIN_PACKET-len); + len = ETH_MIN_PACKET; + data = moved_data; + } - eth_packet_trace (dev, tmp_packet.msg, tmp_packet.len, "rcvqd"); + /* If necessary, fix IP header checksums for packets originated locally */ + /* but were presumed to be traversing a NIC which was going to handle that task */ + /* This must be done before any needed CRC calculation */ + _eth_fix_ip_xsum_offload(dev, (u_char*)data, len); + + if (dev->need_crc) + crc_len = eth_get_packet_crc32_data(data, len, crc_data); - pthread_mutex_lock (&dev->lock); - ethq_insert(&dev->read_queue, 2, &tmp_packet, 0); - pthread_mutex_unlock (&dev->lock); + eth_packet_trace (dev, data, len, "rcvqd"); + + pthread_mutex_lock (&dev->lock); + ethq_insert_data(&dev->read_queue, 2, data, 0, len, crc_len, crc_data, 0); + pthread_mutex_unlock (&dev->lock); + free(moved_data); + } #else /* set data in passed read packet */ dev->read_packet->len = header->len; memcpy(dev->read_packet->msg, data, header->len); + /* Handle runt case and pad with zeros. */ + /* The real NIC won't hand us runts from the wire, BUT we may be getting */ + /* some packets looped back before they actually traverse the wire */ + /* (by an internal bridge device for instance) */ + if (header->len < ETH_MIN_PACKET) { + memset(&dev->read_packet->msg[header->len], 0, ETH_MIN_PACKET-header->len); + dev->read_packet->len = ETH_MIN_PACKET; + } + /* If necessary, fix IP header checksums for packets originated by the local host */ + /* but were presumed to be traversing a NIC which was going to handle that task */ + /* This must be done before any needed CRC calculation */ + _eth_fix_ip_xsum_offload(dev, dev->read_packet->msg, dev->read_packet->len); if (dev->need_crc) - eth_add_crc32(dev->read_packet); + dev->read_packet->crc_len = eth_add_packet_crc32(dev->read_packet->msg, dev->read_packet->len); + else + dev->read_packet->crc_len = 0; eth_packet_trace (dev, dev->read_packet->msg, dev->read_packet->len, "reading"); @@ -1155,28 +2204,46 @@ void eth_callback(u_char* info, const struct pcap_pkthdr* header, const u_char* } } -t_stat eth_read(ETH_DEV* dev, ETH_PACK* packet, ETH_PCALLBACK routine) +int eth_read(ETH_DEV* dev, ETH_PACK* packet, ETH_PCALLBACK routine) { int status; /* make sure device exists */ - if (!dev) return SCPE_UNATT; + if (!dev) return 0; /* make sure packet exists */ - if (!packet) return SCPE_ARG; + if (!packet) return 0; + packet->len = 0; #if !defined (USE_READER_THREAD) /* set read packet */ dev->read_packet = packet; - packet->len = 0; /* set optional callback routine */ dev->read_callback = routine; /* dispatch read request to either receive a filtered packet or timeout */ do { - status = pcap_dispatch((pcap_t*)dev->handle, 1, ð_callback, (u_char*)dev); + if (dev->pcap_mode) + status = pcap_dispatch((pcap_t*)dev->handle, 1, &_eth_callback, (u_char*)dev); +#ifdef USE_TAP_NETWORK + else { + struct pcap_pkthdr header; + int len; + u_char buf[ETH_MAX_JUMBO_FRAME]; + + memset(&header, 0, sizeof(header)); + len = read(dev->fd_handle, buf, sizeof(buf)); + if (len > 0) { + status = 1; + header.len = len; + _eth_callback((u_char *)dev, &header, buf); + } else { + status = 0; + } + } +#endif /* USE_TAP_NETWORK */ } while ((status) && (0 == packet->len)); #else /* USE_READER_THREAD */ @@ -1186,23 +2253,34 @@ t_stat eth_read(ETH_DEV* dev, ETH_PACK* packet, ETH_PCALLBACK routine) if (dev->read_queue.count > 0) { ETH_ITEM* item = &dev->read_queue.item[dev->read_queue.head]; packet->len = item->packet.len; - memcpy(packet->msg, item->packet.msg, packet->len); - if (routine) - routine(status); + packet->crc_len = item->packet.crc_len; + memcpy(packet->msg, item->packet.msg, ((packet->len > packet->crc_len) ? packet->len : packet->crc_len)); + status = 1; ethq_remove(&dev->read_queue); } pthread_mutex_unlock (&dev->lock); + if ((status) && (routine)) + routine(0); #endif - return SCPE_OK; + return status; } -t_stat eth_filter(ETH_DEV* dev, int addr_count, ETH_MAC* addresses, +t_stat eth_filter(ETH_DEV* dev, int addr_count, ETH_MAC* const addresses, ETH_BOOL all_multicast, ETH_BOOL promiscuous) +{ +return eth_filter_hash(dev, addr_count, addresses, + all_multicast, promiscuous, + NULL); +} + +t_stat eth_filter_hash(ETH_DEV* dev, int addr_count, ETH_MAC* const addresses, + ETH_BOOL all_multicast, ETH_BOOL promiscuous, + ETH_MULTIHASH* const hash) { int i; bpf_u_int32 bpf_subnet, bpf_netmask; - char buf[110+66*ETH_FILTER_MAX]; + char buf[114+66*ETH_FILTER_MAX]; char errbuf[PCAP_ERRBUF_SIZE]; char mac[20]; char* buf2; @@ -1221,6 +2299,11 @@ t_stat eth_filter(ETH_DEV* dev, int addr_count, ETH_MAC* addresses, else if (!addresses) return SCPE_ARG; + /* test reflections. This is done early in this routine since eth_reflect */ + /* calls eth_filter recursively and thus changes the state of the device. */ + if (dev->reflections == -1) + status = eth_reflect(dev); + /* set new filter addresses */ for (i = 0; i < addr_count; i++) memcpy(dev->filter_address[i], addresses[i], sizeof(ETH_MAC)); @@ -1230,6 +2313,15 @@ t_stat eth_filter(ETH_DEV* dev, int addr_count, ETH_MAC* addresses, dev->all_multicast = all_multicast; dev->promiscuous = promiscuous; + /* store multicast hash data */ + dev->hash_filter = (hash != NULL); + if (hash) { + memcpy(dev->hash, hash, sizeof(*hash)); + sim_debug(dev->dbit, dev->dptr, "Multicast Hash: %02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X\n", + dev->hash[0], dev->hash[1], dev->hash[2], dev->hash[3], + dev->hash[4], dev->hash[5], dev->hash[6], dev->hash[7]); + } + /* print out filter information if debugging */ if (dev->dptr->dctrl & dev->dbit) { sim_debug(dev->dbit, dev->dptr, "Filter Set\n"); @@ -1244,10 +2336,6 @@ t_stat eth_filter(ETH_DEV* dev, int addr_count, ETH_MAC* addresses, sim_debug(dev->dbit, dev->dptr, "Promiscuous\n"); } - /* test reflections */ - if (dev->reflections == -1) - status = eth_reflect(dev, dev->filter_address[0]); - /* setup BPF filters and other fields to minimize packet delivery */ strcpy(buf, ""); @@ -1258,10 +2346,12 @@ t_stat eth_filter(ETH_DEV* dev, int addr_count, ETH_MAC* addresses, for (i = 0; i < addr_count; i++) { eth_mac_fmt(&dev->filter_address[i], mac); if (!strstr(buf, mac)) /* eliminate duplicates */ - sprintf(&buf[strlen(buf)], "%s(ether dst %s)", (*buf) ? " or " : "", mac); + sprintf(&buf[strlen(buf)], "%s(ether dst %s)", (*buf) ? " or " : "((", mac); } - if (dev->all_multicast) - sprintf(&buf[strlen(buf)], "%s(ether multicast)", (*buf) ? " or " : ""); + if (dev->all_multicast || dev->hash_filter) + sprintf(&buf[strlen(buf)], "%s(ether multicast)", (*buf) ? " or " : "(("); + if (strlen(buf) > 0) + sprintf(&buf[strlen(buf)], ")"); } /* construct source filters - this prevents packets from being reflected back @@ -1281,66 +2371,84 @@ t_stat eth_filter(ETH_DEV* dev, int addr_count, ETH_MAC* addresses, } sprintf (&buf[strlen(buf)], ")"); } - /* When starting, DECnet sends out a packet with the source and destination - addresses set to the same value as the DECnet MAC address. This packet is - designed to find and help diagnose DECnet address conflicts. Normally, this - packet would not be seen by the sender, only by the other machine that has - the same DECnet address. If the ethernet subsystem is reflecting packets, - DECnet will fail to start if it sees the reflected packet, since it thinks - another system is using this DECnet address. We have to let these packets - through, so that if another machine has the same DECnet address that we - can detect it. Both eth_write() and eth_callback() help by checking the - reflection count - eth_write() adds the reflection count to - dev->decnet_self_sent, and eth_callback() check the value - if the - dev->decnet_self_sent count is zero, then the packet has come from another - machine with the same address, and needs to be passed on to the simulated - machine. */ - memset(dev->decnet_addr, 0, sizeof(ETH_MAC)); - /* check for decnet address in filters */ + if (strlen(buf) > 0) + sprintf(&buf[strlen(buf)], ")"); + /* When changing the Physical Address on a LAN interface, VMS sends out a + loopback packet with the source and destination addresses set to the same + value as the Physical Address which is being setup. This packet is + designed to find and help diagnose MAC address conflicts (which also + include DECnet address conflicts). Normally, this packet would not be + seen by the sender, only by the other machine that has the same Physical + Address (or possibly DECnet address). If the ethernet subsystem is + reflecting packets, the network startup will fail to start if it sees the + reflected packet, since it thinks another system is using this Physical + Address (or DECnet address). We have to let these packets through, so + that if another machine has the same Physical Address (or DECnet address) + that we can detect it. Both eth_write() and _eth_callback() help by + checking the reflection count - eth_write() adds the reflection count to + dev->loopback_self_sent, and _eth_callback() check the value - if the + dev->loopback_self_sent count is zero, then the packet has come from + another machine with the same address, and needs to be passed on to the + simulated machine. */ + memset(dev->physical_addr, 0, sizeof(ETH_MAC)); + dev->loopback_self_sent = 0; + /* check for physical address in filters */ if ((addr_count) && (dev->reflections > 0)) { for (i = 0; i < addr_count; i++) { + if (dev->filter_address[i][0]&1) + continue; /* skip all multicast addresses */ eth_mac_fmt(&dev->filter_address[i], mac); - if (memcmp(mac, "AA:00:04", 8) == 0) { - memcpy(dev->decnet_addr, &dev->filter_address[i], sizeof(ETH_MAC)); - /* let packets through where dst and src are the same as our decnet address */ + if (strcmp(mac, "00:00:00:00:00:00") != 0) { + memcpy(dev->physical_addr, &dev->filter_address[i], sizeof(ETH_MAC)); + /* let packets through where dst and src are the same as our physical address */ sprintf (&buf[strlen(buf)], " or ((ether dst %s) and (ether src %s))", mac, mac); break; } } } + if ((0 == strlen(buf)) && (!dev->promiscuous)) /* Empty filter means match nothing */ + strcpy(buf, "ether host fe:ff:ff:ff:ff:ff"); /* this should be a good match nothing filter */ sim_debug(dev->dbit, dev->dptr, "BPF string is: |%s|\n", buf); - /* get netmask, which is required for compiling */ - if (pcap_lookupnet(dev->handle, &bpf_subnet, &bpf_netmask, errbuf)<0) { + if (dev->pcap_mode && (pcap_lookupnet(dev->handle, &bpf_subnet, &bpf_netmask, errbuf)<0)) { bpf_netmask = 0; } #ifdef USE_BPF - /* compile filter string */ - if ((status = pcap_compile(dev->handle, &bpf, buf, 1, bpf_netmask)) < 0) { - sprintf(errbuf, "%s", pcap_geterr(dev->handle)); - msg = "Eth: pcap_compile error: %s\r\n"; - printf(msg, errbuf); - if (sim_log) fprintf (sim_log, msg, errbuf); - /* show erroneous BPF string */ - msg = "Eth: BPF string is: |%s|\r\n"; - printf (msg, buf); - if (sim_log) fprintf (sim_log, msg, buf); - } else { - /* apply compiled filter string */ - if ((status = pcap_setfilter(dev->handle, &bpf)) < 0) { + if (dev->pcap_mode) { +#ifdef USE_READER_THREAD + pthread_mutex_lock (&dev->lock); + ethq_clear (&dev->read_queue); /* Empty FIFO Queue when filter list changes */ + pthread_mutex_unlock (&dev->lock); +#endif + /* compile filter string */ + if ((status = pcap_compile(dev->handle, &bpf, buf, 1, bpf_netmask)) < 0) { sprintf(errbuf, "%s", pcap_geterr(dev->handle)); - msg = "Eth: pcap_setfilter error: %s\r\n"; + msg = "Eth: pcap_compile error: %s\r\n"; printf(msg, errbuf); if (sim_log) fprintf (sim_log, msg, errbuf); + sim_debug(dev->dbit, dev->dptr, "Eth: pcap_compile error: %s\n", errbuf); + /* show erroneous BPF string */ + msg = "Eth: BPF string is: |%s|\r\n"; + printf (msg, buf); + if (sim_log) fprintf (sim_log, msg, buf); } else { + /* apply compiled filter string */ + if ((status = pcap_setfilter(dev->handle, &bpf)) < 0) { + sprintf(errbuf, "%s", pcap_geterr(dev->handle)); + msg = "Eth: pcap_setfilter error: %s\r\n"; + printf(msg, errbuf); + if (sim_log) fprintf (sim_log, msg, errbuf); + sim_debug(dev->dbit, dev->dptr, "Eth: pcap_setfilter error: %s\n", errbuf); + } else { #ifdef USE_SETNONBLOCK - /* set file non-blocking */ - status = pcap_setnonblock (dev->handle, 1, errbuf); + /* set file non-blocking */ + status = pcap_setnonblock (dev->handle, 1, errbuf); #endif /* USE_SETNONBLOCK */ + } + pcap_freecode(&bpf); } - pcap_freecode(&bpf); } #endif /* USE_BPF */ @@ -1387,46 +2495,59 @@ int eth_host_devices(int used, int max, ETH_LIST* list) #if defined(_WIN32) /* replace device description with user-defined adapter name (if defined) */ for (i=0; i sizeof(regval))) { - RegCloseKey (reghnd); - continue; - } + if((regtype != REG_SZ) || (reglen > sizeof(regval))) { + RegCloseKey (reghnd); + continue; + } /* registry value seems OK, finish up and replace description */ - RegCloseKey (reghnd ); + RegCloseKey (reghnd ); sprintf (list[i].desc, "%s", regval); } } /* for */ #endif - return used; +#ifdef USE_TAP_NETWORK + if (used < max) { + list[used].num = used; +#if defined(__OpenBSD__) + sprintf(list[used].name, "%s", "tap:tunN"); +#else + sprintf(list[used].name, "%s", "tap:tapN"); +#endif + sprintf(list[used].desc, "%s", "Integrated Tun/Tap support"); + ++used; + } +#endif + + return used; } int eth_devices(int max, ETH_LIST* list) @@ -1467,4 +2588,35 @@ int eth_devices(int max, ETH_LIST* list) return i; } +void eth_show_dev (FILE *st, ETH_DEV* dev) +{ + fprintf(st, "Ethernet Device:\n"); + if (!dev) { + fprintf(st, "-- Not Attached\n"); + return; + } + fprintf(st, " Name: %s\n", dev->name); + fprintf(st, " Reflections: %d\n", dev->reflections); + fprintf(st, " Self Loopbacks Sent: %d\n", dev->loopback_self_sent_total); + fprintf(st, " Self Loopbacks Rcvd: %d\n", dev->loopback_self_rcvd_total); + if (dev->have_host_nic_phy_addr) { + char hw_mac[20]; + + eth_mac_fmt(&dev->host_nic_phy_hw_addr, hw_mac); + fprintf(st, " Host NIC Address: %s\n", hw_mac); + } + if (dev->jumbo_dropped) + fprintf(st, " Jumbo Dropped: %d\n", dev->jumbo_dropped); + if (dev->jumbo_fragmented) + fprintf(st, " Jumbo Fragmented: %d\n", dev->jumbo_fragmented); +#if defined(USE_READER_THREAD) + fprintf(st, " Asynch Interrupts: %s\n", dev->asynch_io?"Enabled":"Disabled"); + if (dev->asynch_io) + fprintf(st, " Interrupt Latency: %d uSec\n", dev->asynch_io_latency); + fprintf(st, " Read Queue: Count: %d\n", dev->read_queue.count); + fprintf(st, " Read Queue: High: %d\n", dev->read_queue.high); + fprintf(st, " Read Queue: Loss: %d\n", dev->read_queue.loss); + fprintf(st, " Peak Write Queue Size: %d\n", dev->write_queue_peak); +#endif +} #endif /* USE_NETWORK */ diff --git a/sim_ether.h b/sim_ether.h index 371ee3ba..4374f31e 100644 --- a/sim_ether.h +++ b/sim_ether.h @@ -28,6 +28,14 @@ Modification history: + 09-Dec-10 MP Added support to determine if network address conflicts exist + 07-Dec-10 MP Reworked DECnet self detection to the more general approach + of loopback self when any Physical Address is being set. + 04-Dec-10 MP Changed eth_write to do nonblocking writes when + USE_READER_THREAD is defined. + 07-Feb-08 MP Added eth_show_dev to display ethernet state + 28-Jan-08 MP Added eth_set_async + 23-Jan-08 MP Added eth_packet_trace_ex and ethq_destroy 30-Nov-05 DTH Added CRC length to packet and more field comments 04-Feb-04 DTH Added debugging information 14-Jan-04 MP Generalized BSD support issues @@ -64,7 +72,7 @@ #define USE_SETNONBLOCK 1 #endif -#if defined(__sun__) && defined(__i386__) +#if (((defined(__sun__) && defined(__i386__)) || defined(__linux)) && !defined(DONT_USE_READER_THREAD)) #define USE_READER_THREAD 1 #endif @@ -108,13 +116,20 @@ #define ETH_DEV_DESC_MAX 256 /* maximum device description size */ #define ETH_MIN_PACKET 60 /* minimum ethernet packet size */ #define ETH_MAX_PACKET 1514 /* maximum ethernet packet size */ +#define ETH_MAX_JUMBO_FRAME 16384 /* maximum ethernet jumbo frame size */ #define ETH_MAX_DEVICE 10 /* maximum ethernet devices */ #define ETH_CRC_SIZE 4 /* ethernet CRC size */ -#define ETH_FRAME_SIZE 1518 /* ethernet maximum frame size */ +#define ETH_FRAME_SIZE (ETH_MAX_PACKET+ETH_CRC_SIZE) /* ethernet maximum frame size */ +#define ETH_MIN_JUMBO_FRAME ETH_MAX_PACKET /* Threshold size for Jumbo Frame Processing */ -#define DECNET_SELF_FRAME(dnet_mac, msg) \ - ((memcmp(dnet_mac, msg , 6) == 0) && \ - (memcmp(dnet_mac, msg+6, 6) == 0)) +#define LOOPBACK_SELF_FRAME(phy_mac, msg) \ + ((memcmp(phy_mac, msg , 6) == 0) && \ + (memcmp(phy_mac, msg+6, 6) == 0) && \ + ((msg)[12] == 0x90) && ((msg)[13] == 0x00) && \ + ((msg)[14] == 0x00) && ((msg)[15] == 0x00) && \ + ((msg)[16] == 0x02) && ((msg)[17] == 0x00) && \ + (memcmp(phy_mac, msg+18, 6) == 0) && \ + ((msg)[24] == 0x01) && ((msg)[25] == 0x00)) struct eth_packet { uint8 msg[ETH_FRAME_SIZE]; /* ethernet frame (message) */ @@ -147,6 +162,7 @@ struct eth_list { typedef int ETH_BOOL; typedef unsigned char ETH_MAC[6]; +typedef unsigned char ETH_MULTIHASH[8]; typedef struct eth_packet ETH_PACK; typedef void (*ETH_PCALLBACK)(int status); typedef struct eth_list ETH_LIST; @@ -156,24 +172,45 @@ typedef struct eth_item ETH_ITEM; struct eth_device { char* name; /* name of ethernet device */ void* handle; /* handle of implementation-specific device */ + int fd_handle; /* fd to kernel device (where needed) */ + int pcap_mode; /* Flag indicating if pcap API are being used to move packets */ ETH_PCALLBACK read_callback; /* read callback function */ ETH_PCALLBACK write_callback; /* write callback function */ ETH_PACK* read_packet; /* read packet */ - ETH_PACK* write_packet; /* write packet */ ETH_MAC filter_address[ETH_FILTER_MAX]; /* filtering addresses */ int addr_count; /* count of filtering addresses */ ETH_BOOL promiscuous; /* promiscuous mode flag */ ETH_BOOL all_multicast; /* receive all multicast messages */ - int32 decnet_self_sent; /* loopback packets sent but not seen */ - ETH_MAC decnet_addr; /* decnet address of interface */ + ETH_BOOL hash_filter; /* filter using AUTODIN II multicast hash */ + ETH_MULTIHASH hash; /* AUTODIN II multicast hash */ + int32 loopback_self_sent; /* loopback packets sent but not seen */ + int32 loopback_self_sent_total; /* total loopback packets sent */ + int32 loopback_self_rcvd_total; /* total loopback packets seen */ + ETH_MAC physical_addr; /* physical address of interface */ + int32 have_host_nic_phy_addr; /* flag indicating that the host_nic_phy_hw_addr is valid */ + ETH_MAC host_nic_phy_hw_addr; /* MAC address of the attached NIC */ + uint32 jumbo_fragmented; /* Giant IPv4 Frames Fragmented */ + uint32 jumbo_dropped; /* Giant Frames Dropped */ DEVICE* dptr; /* device ethernet is attached to */ uint32 dbit; /* debugging bit */ int reflections; /* packet reflections on interface */ - int need_crc; /* device needs CRC (Cyclic Redundancy Check) */ + int need_crc; /* device needs CRC (Cyclic Redundancy Check) */ #if defined (USE_READER_THREAD) + int asynch_io; /* Asynchronous Interrupt scheduling enabled */ + int asynch_io_latency; /* instructions to delay pending interrupt */ ETH_QUE read_queue; pthread_mutex_t lock; pthread_t reader_thread; /* Reader Thread Id */ + pthread_t writer_thread; /* Writer Thread Id */ + pthread_mutex_t writer_lock; + pthread_cond_t writer_cond; + struct write_request { + struct write_request *next; + ETH_PACK packet; + } *write_requests; + int write_queue_peak; + struct write_request *write_buffers; + t_stat write_status; #endif }; @@ -186,18 +223,30 @@ t_stat eth_open (ETH_DEV* dev, char* name, /* open ethernet interfa t_stat eth_close (ETH_DEV* dev); /* close ethernet interface */ t_stat eth_write (ETH_DEV* dev, ETH_PACK* packet, /* write sychronous packet; */ ETH_PCALLBACK routine); /* callback when done */ -t_stat eth_read (ETH_DEV* dev, ETH_PACK* packet, /* read single packet; */ +int eth_read (ETH_DEV* dev, ETH_PACK* packet, /* read single packet; */ ETH_PCALLBACK routine); /* callback when done*/ t_stat eth_filter (ETH_DEV* dev, int addr_count, /* set filter on incoming packets */ - ETH_MAC* addresses, + ETH_MAC* const addresses, ETH_BOOL all_multicast, ETH_BOOL promiscuous); +t_stat eth_filter_hash (ETH_DEV* dev, int addr_count, /* set filter on incoming packets with AUTODIN II based hash */ + ETH_MAC* const addresses, + ETH_BOOL all_multicast, + ETH_BOOL promiscuous, + ETH_MULTIHASH* const hash); +t_stat eth_check_address_conflict (ETH_DEV* dev, + ETH_MAC* const address); int eth_devices (int max, ETH_LIST* dev); /* get ethernet devices on host */ void eth_setcrc (ETH_DEV* dev, int need_crc); /* enable/disable CRC mode */ +t_stat eth_set_async (ETH_DEV* dev, int latency); /* set read behavior to be async */ +t_stat eth_clr_async (ETH_DEV* dev); /* set read behavior to be not async */ +uint32 eth_crc32(uint32 crc, const void* vbuf, size_t len); /* Compute Ethernet Autodin II CRC for buffer */ -void eth_packet_trace (ETH_DEV* dev, const uint8 *msg, int len, char* txt); /* trace ethernet packet */ +void eth_packet_trace (ETH_DEV* dev, const uint8 *msg, int len, char* txt); /* trace ethernet packet header+crc */ +void eth_packet_trace_ex (ETH_DEV* dev, const uint8 *msg, int len, char* txt, int detail, uint32 reason); /* trace ethernet packet */ t_stat eth_show (FILE* st, UNIT* uptr, /* show ethernet devices */ int32 val, void* desc); +void eth_show_dev (FILE*st, ETH_DEV* dev); /* show ethernet device state */ void eth_mac_fmt (ETH_MAC* add, char* buffer); /* format ethernet mac address */ t_stat eth_mac_scan (ETH_MAC* mac, char* strmac); /* scan string for mac, put in mac */ @@ -207,6 +256,10 @@ void ethq_clear (ETH_QUE* que); /* clear FIFO queue */ void ethq_remove (ETH_QUE* que); /* remove item from FIFO queue */ void ethq_insert (ETH_QUE* que, int32 type, /* insert item into FIFO queue */ ETH_PACK* packet, int32 status); +void ethq_insert_data(ETH_QUE* que, int32 type, /* insert item into FIFO queue */ + const uint8 *data, int used, int len, + int crc_len, const uint8 *crc_data, int32 status); +t_stat ethq_destroy(ETH_QUE* que); /* release FIFO queue */ #endif /* _SIM_ETHER_H */