simh-testsetgenerator/sim_ether.c
Bob Supnik 2c2dd5ea33 Notes For V2.10-0
WARNING: V2.10 has reorganized and renamed some of the definition
files for the PDP-10, PDP-11, and VAX.  Be sure to delete all
previous source files before you unpack the Zip archive, or
unpack it into a new directory structure.

WARNING: V2.10 has a new, more comprehensive save file format.
Restoring save files from previous releases will cause 'invalid
register' errors and loss of CPU option flags, device enable/
disable flags, unit online/offline flags, and unit writelock
flags.

WARNING: If you are using Visual Studio .NET through the IDE,
be sure to turn off the /Wp64 flag in the project settings, or
dozens of spurious errors will be generated.

WARNING: Compiling Ethernet support under Windows requires
extra steps; see the Ethernet readme file.  Ethernet support is
currently available only for Windows, Linux, NetBSD, and OpenBSD.

1. New Features

1.1 SCP and Libraries

- The VT emulation package has been replaced by the capability
  to remote the console to a Telnet session.  Telnet clients
  typically have more complete and robust VT100 emulation.
- Simulated devices may now have statically allocated buffers,
  in addition to dynamically allocated buffers or disk-based
  data stores.
- The DO command now takes substitutable arguments (max 9).
  In command files, %n represents substitutable argument n.
- The initial command line is now interpreted as the command
  name and substitutable arguments for a DO command.  This is
  backward compatible to prior versions.
- The initial command line parses switches.  -Q is interpreted
  as quiet mode; informational messages are suppressed.
- The HELP command now takes an optional argument.  HELP <cmd>
  types help on the specified command.
- Hooks have been added for implementing GUI-based consoles,
  as well as simulator-specific command extensions.  A few
  internal data structures and definitions have changed.
- Two new routines (tmxr_open_master, tmxr_close_master) have
  been added to sim_tmxr.c.  The calling sequence for
  sim_accept_conn has been changed in sim_sock.c.
- The calling sequence for the VM boot routine has been modified
  to add an additional parameter.
- SAVE now saves, and GET now restores, controller and unit flags.
- Library sim_ether.c has been added for Ethernet support.

1.2 VAX

- Non-volatile RAM (NVR) can behave either like a memory or like
  a disk-based peripheral.  If unattached, it behaves like memory
  and is saved and restored by SAVE and RESTORE, respectively.
  If attached, its contents are loaded from disk by ATTACH and
  written back to disk at DETACH and EXIT.
- SHOW <device> VECTOR displays the device's interrupt vector.
  A few devices allow the vector to be changed with SET
  <device> VECTOR=nnn.
- SHOW CPU IOSPACE displays the I/O space address map.
- The TK50 (TMSCP tape) has been added.
- The DEQNA/DELQA (Qbus Ethernet controllers) have been added.
- Autoconfiguration support has been added.
- The paper tape reader has been removed from vax_stddev.c and
  now references a common implementation file, dec_pt.h.
- Examine and deposit switches now work on all devices, not just
  the CPU.
- Device address conflicts are not detected until simulation starts.

1.3 PDP-11

- SHOW <device> VECTOR displays the device's interrupt vector.
  Most devices allow the vector to be changed with SET
  <device> VECTOR=nnn.
- SHOW CPU IOSPACE displays the I/O space address map.
- The TK50 (TMSCP tape), RK611/RK06/RK07 (cartridge disk),
  RX211 (double density floppy), and KW11P programmable clock
  have been added.
- The DEQNA/DELQA (Qbus Ethernet controllers) have been added.
- Autoconfiguration support has been added.
- The paper tape reader has been removed from pdp11_stddev.c and
  now references a common implementation file, dec_pt.h.
- Device bootstraps now use the actual CSR specified by the
  SET ADDRESS command, rather than just the default CSR.  Note
  that PDP-11 operating systems may NOT support booting with
  non-standard addresses.
- Specifying more than 256KB of memory, or changing the bus
  configuration, causes all peripherals that are not compatible
  with the current bus configuration to be disabled.
- Device address conflicts are not detected until simulation starts.

1.4 PDP-10

- SHOW <device> VECTOR displays the device's interrupt vector.
  A few devices allow the vector to be changed with SET
  <device> VECTOR=nnn.
- SHOW CPU IOSPACE displays the I/O space address map.
- The RX211 (double density floppy) has been added; it is off
  by default.
- The paper tape now references a common implementation file,
  dec_pt.h.
- Device address conflicts are not detected until simulation starts.

1.5 PDP-1

- DECtape (then known as MicroTape) support has been added.
- The line printer and DECtape can be disabled and enabled.

1.6 PDP-8

- The RX28 (double density floppy) has been added as an option to
  the existing RX8E controller.
- SHOW <device> DEVNO displays the device's device number.  Most
  devices allow the device number to be changed with SET <device>
  DEVNO=nnn.
- Device number conflicts are not detected until simulation starts.

1.7 IBM 1620

- The IBM 1620 simulator has been released.

1.8 AltairZ80

- A hard drive has been added for increased storage.
- Several bugs have been fixed.

1.9 HP 2100

- The 12845A has been added and made the default line printer (LPT).
  The 12653A has been renamed LPS and is off by default.  It also
  supports the diagnostic functions needed to run the DCPC and DMS
  diagnostics.
- The 12557A/13210A disk defaults to the 13210A (7900/7901).
- The 12559A magtape is off by default.
- New CPU options (EAU/NOEAU) enable/disable the extended arithmetic
  instructions for the 2116.  These instructions are standard on
  the 2100 and 21MX.
- New CPU options (MPR/NOMPR) enable/disable memory protect for the
  2100 and 21MX.
- New CPU options (DMS/NODMS) enable/disable the dynamic mapping
  instructions for the 21MX.
- The 12539 timebase generator autocalibrates.

1.10 Simulated Magtapes

- Simulated magtapes recognize end of file and the marker
  0xFFFFFFFF as end of medium.  Only the TMSCP tape simulator
  can generate an end of medium marker.
- The error handling in simulated magtapes was overhauled to be
  consistent through all simulators.

1.11 Simulated DECtapes

- Added support for RT11 image file format (256 x 16b) to DECtapes.

2. Release Notes

2.1 Bugs Fixed

- TS11/TSV05 was not simulating the XS0_MOT bit, causing failures
  under VMS.  In addition, two of the CTL options were coded
  interchanged.
- IBM 1401 tape was not setting a word mark under group mark for
  load mode reads.  This caused the diagnostics to crash.
- SCP bugs in ssh_break and set_logon were fixed (found by Dave
  Hittner).
- Numerous bugs in the HP 2100 extended arithmetic, floating point,
  21MX, DMS, and IOP instructions were fixed.  Bugs were also fixed
  in the memory protect and DMS functions.  The moving head disks
  (DP, DQ) were revised to simulate the hardware more accurately.
  Missing functions in DQ (address skip, read address) were added.

2.2 HP 2100 Debugging

- The HP 2100 CPU nows runs all of the CPU diagnostics.
- The peripherals run most of the peripheral diagnostics.  There
  is still a problem in overlapped seek operation on the disks.
  See the file hp2100_diag.txt for details.

3. In Progress

These simulators are not finished and are available in a separate
Zip archive distribution.

- Interdata 16b/32b: coded, partially tested.  See the file
  id_diag.txt for details.
- SDS 940: coded, partially tested.
2011-04-15 08:33:49 -07:00

493 lines
15 KiB
C

/* sim_ether.c: OS-dependent network routines
------------------------------------------------------------------------------
Copyright (c) 2002, 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"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of the author shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from the author.
------------------------------------------------------------------------------
Modification history:
31-Oct-02 DTH Added USE_NETWORK conditional
Reworked not attached test
Added OpenBSD support (from Federico Schwindt)
Added ethX detection simplification (from Megan Gentry)
Removed sections of temporary code
Added parameter validation
23-Oct-02 DTH Beta 5 released
22-Oct-02 DTH Added all_multicast and promiscuous support
Fixed not attached behavior
21-Oct-02 DTH Added NetBSD support (from Jason Thorpe)
Patched buffer size to make sure entire packet is read in
Made 'ethX' check characters passed as well as length
Corrected copyright again
16-Oct-02 DTH Beta 4 released
Corrected copyright
09-Oct-02 DTH Beta 3 released
Added pdp11 write acceleration (from Patrick Caulfield)
08-Oct-02 DTH Beta 2 released
Integrated with 2.10-0p4
Added variable vector and copyrights
04-Oct-02 DTH Added linux support (from Patrick Caulfield)
03-Oct-02 DTH Beta version of xq/sim_ether released for SIMH 2.09-11
24-Sep-02 DTH Finished eth_devices, eth_getname
18-Sep-02 DTH Callbacks implemented
13-Sep-02 DTH Basic packet read/write written
20-Aug-02 DTH Created Sim_Ether for O/S independant ethernet implementation
------------------------------------------------------------------------------
Work left to do:
1) Add Asynchronous Packet Writing to WIN32 platform
2) Addition of other host Operating Systems
3) Addition of BPF packet filtering for efficiency
------------------------------------------------------------------------------
*/
#include "sim_ether.h"
/*============================================================================*/
/* OS-independant ethernet routines */
/*============================================================================*/
void eth_mac_fmt(ETH_MAC* mac, char* buff)
{
uint8* m = (uint8*) mac;
sprintf(buff, "%02X-%02X-%02X-%02X-%02X-%02X", m[0], m[1], m[2], m[3], m[4], m[5]);
return;
}
void eth_packet_trace(ETH_PACK* packet, char* msg)
{
unsigned char src[20];
unsigned char dst[20];
unsigned short* proto = (unsigned short*) &packet->msg[12];
eth_mac_fmt((ETH_MAC*)&packet->msg[0], dst);
eth_mac_fmt((ETH_MAC*)&packet->msg[6], src);
printf("%s: dst: %s src: %s protocol: %d len: %d\n",
msg, dst, src, *proto, packet->len);
}
char* eth_getname(int number, char* name)
{
#define ETH_SUPPORTED_DEVICES 10
ETH_LIST list[ETH_SUPPORTED_DEVICES];
int count = eth_devices(ETH_SUPPORTED_DEVICES, list);
if (count < number) return 0;
strcpy(name, list[number].name);
return name;
}
void eth_zero(ETH_DEV* dev)
{
/* set all members to NULL OR 0 */
memset(dev, 0, sizeof(ETH_DEV));
}
/*============================================================================*/
/* Non-implemented versions */
/*============================================================================*/
#if !defined (WIN32) && !defined(linux) && !defined(__NetBSD__) && \
!defined (__OpenBSD__) || !defined (USE_NETWORK)
t_stat eth_open (ETH_DEV* dev, char* name)
{return SCPE_NOFNC;}
t_stat eth_close (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)
{return SCPE_NOFNC;}
t_stat eth_filter (ETH_DEV* dev, int addr_count, ETH_MAC* addresses,
ETH_BOOL all_multicast, ETH_BOOL promiscuous)
{return SCPE_NOFNC;}
int eth_devices (int max, ETH_LIST* dev)
{return 0;}
#else /* endif unimplemented */
/*============================================================================*/
/* WIN32, Linux, NetBSD, and OpenBSD routines */
/* Uses WinPCAP and libpcap packages */
/*============================================================================*/
#include <ctype.h>
#include <pcap.h>
#ifdef WIN32
#include <packet32.h>
#endif /* WIN32 */
#if defined (__NetBSD__) || defined (__OpenBSD__)
#include <sys/ioctl.h>
#include <net/bpf.h>
#endif /* __NetBSD__ || __OpenBSD__*/
#if defined (linux) || defined(__NetBSD__) || defined (__OpenBSD__)
#include <fcntl.h>
#endif /* linux || __NetBSD__ || __OpenBSD__ */
t_stat eth_open(ETH_DEV* dev, char* name)
{
const int bufsz = (BUFSIZ < ETH_MAX_PACKET) ? ETH_MAX_PACKET : BUFSIZ;
char errbuf[PCAP_ERRBUF_SIZE];
char temp[1024];
char* savname = name;
int i, num;
/* initialize device */
eth_zero(dev);
/* translate name of type "ethX" to real device name */
if ((strlen(name) == 4)
&& (tolower(name[0]) == 'e')
&& (tolower(name[1]) == 't')
&& (tolower(name[2]) == 'h')
&& isdigit(name[3])
) {
num = atoi(&name[3]);
savname = eth_getname(num, temp);
}
/* attempt to connect device */
dev->handle = (void*) pcap_open_live(savname, bufsz, ETH_PROMISC, -1, errbuf);
if (!dev->handle) { /* can't open device */
#ifdef _DEBUG
printf("pcap_open_live: %s\n", errbuf);
#endif
return SCPE_OPENERR;
}
/* save name of device */
dev->name = malloc(strlen(savname)+1);
strcpy(dev->name, savname);
#if defined (__NetBSD__) || defined(__OpenBSD__)
/* tell the kernel that the header is fully-formed when it gets it.
this is required in order to fake the src address. */
i = 1;
ioctl(pcap_fileno(dev->handle), BIOCSHDRCMPLT, &i);
#endif /* __NetBSD__ || __OpenBSD__ */
#if defined(linux) || defined(__NetBSD__) || defined (__OpenBSD__)
/* set file non-blocking */
fcntl(pcap_fileno(dev->handle), F_SETFL, fcntl(pcap_fileno(dev->handle), F_GETFL, 0) | O_NONBLOCK);
#endif /* linux || __NetBSD__ || __OpenBSD__ */
return SCPE_OK;
}
t_stat eth_close(ETH_DEV* dev)
{
/* make sure device exists */
if (!dev) return SCPE_UNATT;
/* close the device */
pcap_close(dev->handle);
/* clean up the mess */
free(dev->name);
eth_zero(dev);
return SCPE_OK;
}
t_stat eth_write(ETH_DEV* dev, ETH_PACK* packet, ETH_PCALLBACK routine)
{
int status = 1; /* default to failure */
/* make sure device exists */
if (!dev) return SCPE_UNATT;
/* make sure packet exists */
if (!packet) return SCPE_ARG;
/* make sure packet is acceptable length */
if ((packet->len >= ETH_MIN_PACKET) && (packet->len <= ETH_MAX_PACKET)) {
/* dispatch write request (synchronous; no need to save write info to dev) */
#ifdef _DEBUG
eth_packet_trace (packet, "writing");
#endif
status = pcap_sendpacket((pcap_t*)dev->handle, (u_char*)packet->msg, packet->len);
} /* if packet->len */
/* call optional write callback function */
if (routine)
(routine)(status);
return SCPE_OK;
}
void eth_callback(u_char* info, const struct pcap_pkthdr* header, const u_char* data)
{
ETH_DEV* dev = (ETH_DEV*) info;
/*+ temporary packet filter - should be done by BPF filter */
int to_me = 0;
int from_me = 0;
int i;
for (i = 0; i < ETH_FILTER_MAX; 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;
} /* for */
/* all multicast mode? */
if (dev->all_multicast && (data[0] & 0x01)) to_me = 1;
/* promiscuous mode? */
if (dev->promiscuous) to_me = 1;
if (to_me && !from_me) {
/*- temporary packet filter - should be done by driver filter */
/* set data in passed read packet */
dev->read_packet->len = header->len;
memcpy(dev->read_packet->msg, data, header->len);
#ifdef _DEBUG
eth_packet_trace (dev->read_packet, "reading");
#endif
/* call optional read callback function */
if (dev->read_callback)
(dev->read_callback)(0);
/*+ temporary packet filter - should be done by driver filter */
} /* if use */
/*- temporary packet filter - should be done by driver filter */
}
t_stat eth_read(ETH_DEV* dev, ETH_PACK* packet, ETH_PCALLBACK routine)
{
int status;
/* make sure device exists */
if (!dev) return SCPE_UNATT;
/* make sure packet exists */
if (!packet) return SCPE_ARG;
/* set read packet */
dev->read_packet = packet;
/* set optional callback routine */
dev->read_callback = routine;
/* dispatch read request */
status = pcap_dispatch((pcap_t*)dev->handle, 1, &eth_callback, (u_char*)dev);
return SCPE_OK;
}
t_stat eth_filter(ETH_DEV* dev, int addr_count, ETH_MAC* addresses,
ETH_BOOL all_multicast, ETH_BOOL promiscuous)
{
int i;
/* make sure device exists */
if (!dev) return SCPE_UNATT;
/* should be implemented as a BPF filter, but for now.. */
/* filter count OK? */
if ((addr_count < 0) || (addr_count > ETH_FILTER_MAX))
return SCPE_ARG;
else
if (!addresses) return SCPE_ARG;
/* clear filter array */
memset(dev->filter_address, 0, sizeof(ETH_MAC) * ETH_FILTER_MAX);
/* set new filter addresses */
for (i = 0; i < addr_count; i++)
memcpy(dev->filter_address[i], addresses[i], sizeof(ETH_MAC));
/* set all_multicast and promiscuous flags */
dev->all_multicast = all_multicast;
dev->promiscuous = promiscuous;
return SCPE_OK;
}
int eth_devices(int max, ETH_LIST* list)
{
int i, index, len;
uint8 buffer[2048];
uint8 buffer2[2048];
uint8* cptr = buffer2;
int size = sizeof(buffer);
unsigned long ret;
/* get names of devices from packet driver */
ret = PacketGetAdapterNames(buffer, &size);
/* device names in ascii or unicode format? */
if ((buffer[1] == 0) && (buffer[3] == 0)) { /* unicode.. <sigh> */
int i = 0;
int cptr_inc = 2;
/* want to use buffer for scanning, so copy to buffer2 */
memcpy (buffer2, buffer, sizeof(buffer));
/* convert unicode to ascii (assuming every other byte is zero) */
while (cptr < (buffer2 + sizeof(buffer2))) {
buffer[i] = *cptr;
if ((buffer[i] == 0) && (buffer[i-1] == 0)) { /* end of unicode devices */
/* descriptions are in ascii, so change increment */
cptr_inc = 1;
}
cptr += cptr_inc;
i++;
}
}
/* scan ascii string and load list*/
index = 0;
cptr = buffer;
/* extract device names and numbers */
while (len = strlen(cptr)) {
list[index].num = index;
strcpy(list[index].name, cptr);
cptr += len + 1;
index++;
}
cptr += 2;
/* extract device descriptions */
for (i=0; i < index; i++) {
len = strlen(cptr);
strcpy(list[i].desc, cptr);
cptr += len + 1;
}
return index; /* count of devices */
}
#endif /* (WIN32 || linux || __NetBSD__ || __OpenBSD__) && USE_NETWORK */
/*============================================================================*/
/* linux-specific code */
/*============================================================================*/
#if defined (linux) && defined (USE_NETWORK)
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <features.h> /* for the glibc version number */
#if (__GLIBC__ >= 2 && __GLIBC_MINOR >= 1) || __GLIBC__ >= 3
#include <netpacket/packet.h>
#include <net/ethernet.h> /* the L2 protocols */
#else /*__GLIBC__*/
#include <asm/types.h>
#include <linux/if.h>
#include <linux/if_arp.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h> /* The L2 protocols */
#endif /*__GLIBC__*/
int pcap_sendpacket(pcap_t* handle, u_char* msg, int len)
{
return (send(pcap_fileno(handle), msg, len, 0) == len)?0:-1;
}
int PacketGetAdapterNames(char* buffer, int* size)
{
struct ifreq ifr;
int iindex = 1;
int sock = socket(PF_PACKET, SOCK_RAW, 0);
int ptr = 0;
ifr.ifr_ifindex = iindex;
while (ioctl(sock, SIOCGIFNAME, &ifr) == 0) {
/* Only use ethernet interfaces */
ioctl(sock, SIOCGIFHWADDR, &ifr);
if (ifr.ifr_hwaddr.sa_family == ARPHRD_ETHER) {
strcpy(buffer+ptr, ifr.ifr_name);
ptr += strlen(buffer)+1;
}
ifr.ifr_ifindex = ++iindex;
}
close(sock);
buffer[ptr++] = '\0';
buffer[ptr++] = '\0';
*size = ptr;
}
#endif /* linux && USE_NETWORK */
/*============================================================================*/
/* NetBSD/OpenBSD-specific code */
/*============================================================================*/
#if (defined (__NetBSD__) || defined(__OpenBSD__)) && defined (USE_NETWORK)
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include <ifaddrs.h>
#include <string.h>
int pcap_sendpacket(pcap_t* handle, u_char* msg, int len)
{
return (write(pcap_fileno(handle), msg, len) == len)?0:-1;
}
int PacketGetAdapterNames(char* buffer, int* size)
{
const struct sockaddr_dl *sdl;
struct ifaddrs *ifap, *ifa;
char *p;
int ptr = 0;
if (getifaddrs(&ifap) != 0) {
*size = 0;
return (0);
}
p = NULL;
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
if (ifa->ifa_addr->sa_family != AF_LINK)
continue;
if (p && strcmp(p, ifa->ifa_name) == 0)
continue;
sdl = (const struct sockaddr_dl *) ifa->ifa_addr;
if (sdl->sdl_type != IFT_ETHER)
continue;
strcpy(buffer+ptr, ifa->ifa_name);
ptr += strlen(ifa->ifa_name)+1;
}
freeifaddrs(ifap);
buffer[ptr++] = '\0';
buffer[ptr++] = '\0';
*size = ptr;
return (ptr);
}
#endif /* (__NetBSD__ || __OpenBSD__) && USE_NETWORK */