First Commit of my working state
[simh.git] / sim_ether.h
1 /* sim_ether.h: OS-dependent network information
2 ------------------------------------------------------------------------------
3
4 Copyright (c) 2002-2005, David T. Hittner
5
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom the
11 Software is furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23 Except as contained in this notice, the name of the author shall not be
24 used in advertising or otherwise to promote the sale, use or other dealings
25 in this Software without prior written authorization from the author.
26
27 ------------------------------------------------------------------------------
28
29 Modification history:
30
31 30-Nov-05 DTH Added CRC length to packet and more field comments
32 04-Feb-04 DTH Added debugging information
33 14-Jan-04 MP Generalized BSD support issues
34 05-Jan-04 DTH Added eth_mac_scan
35 26-Dec-03 DTH Added ethernet show and queue functions from pdp11_xq
36 23-Dec-03 DTH Added status to packet
37 01-Dec-03 DTH Added reflections, tweaked decnet fix items
38 25-Nov-03 DTH Verified DECNET_FIX, reversed ifdef to mainstream code
39 14-Nov-03 DTH Added #ifdef DECNET_FIX for problematic duplicate detection code
40 07-Jun-03 MP Added WIN32 support for DECNET duplicate address detection.
41 05-Jun-03 DTH Added used to struct eth_packet
42 01-Feb-03 MP Changed some uint8 strings to char* to reflect usage
43 22-Oct-02 DTH Added all_multicast and promiscuous support
44 21-Oct-02 DTH Corrected copyright again
45 16-Oct-02 DTH Fixed copyright
46 08-Oct-02 DTH Integrated with 2.10-0p4, added variable vector and copyrights
47 03-Oct-02 DTH Beta version of xq/sim_ether released for SIMH 2.09-11
48 15-Aug-02 DTH Started XQ simulation
49
50 ------------------------------------------------------------------------------
51 */
52
53 #ifndef _SIM_ETHER_H
54 #define _SIM_ETHER_H
55
56 #include "sim_defs.h"
57
58 /* make common BSD code a bit easier to read in this file */
59 /* OS/X seems to define and compile using one of these BSD types */
60 #if defined(__NetBSD__) || defined (__OpenBSD__) || defined (__FreeBSD__)
61 #define xBSD 1
62 #endif
63 #if !defined(__FreeBSD__) && !defined(_WIN32) && !defined(VMS)
64 #define USE_SETNONBLOCK 1
65 #endif
66
67 #if defined(__sun__) && defined(__i386__)
68 #define USE_READER_THREAD 1
69 #endif
70
71 /* make common winpcap code a bit easier to read in this file */
72 #if defined(_WIN32) || defined(VMS)
73 #define PCAP_READ_TIMEOUT -1
74 #else
75 #define PCAP_READ_TIMEOUT 1
76 #endif
77
78 /* set related values to have correct relationships */
79 #if defined (USE_READER_THREAD)
80 #if defined (USE_SETNONBLOCK)
81 #undef USE_SETNONBLOCK
82 #endif
83 #undef PCAP_READ_TIMEOUT
84 #define PCAP_READ_TIMEOUT 15
85 #if !defined (xBSD) && !defined(_WIN32) && !defined(VMS)
86 #define MUST_DO_SELECT
87 #endif
88 #endif
89
90 /*
91 USE_BPF is defined to let this code leverage the libpcap/OS kernel provided
92 BPF packet filtering. This generally will enhance performance. It may not
93 be available in some environments and/or it may not work correctly, so
94 undefining this will still provide working code here.
95 */
96 #define USE_BPF 1
97
98 #if defined (USE_READER_THREAD)
99 #include <pthread.h>
100 #endif
101
102 /* structure declarations */
103
104 #define ETH_PROMISC 1 /* promiscuous mode = true */
105 #define ETH_TIMEOUT -1 /* read timeout in milliseconds (immediate) */
106 #define ETH_FILTER_MAX 20 /* maximum address filters */
107 #define ETH_DEV_NAME_MAX 256 /* maximum device name size */
108 #define ETH_DEV_DESC_MAX 256 /* maximum device description size */
109 #define ETH_MIN_PACKET 60 /* minimum ethernet packet size */
110 #define ETH_MAX_PACKET 1514 /* maximum ethernet packet size */
111 #define ETH_MAX_DEVICE 10 /* maximum ethernet devices */
112 #define ETH_CRC_SIZE 4 /* ethernet CRC size */
113 #define ETH_FRAME_SIZE 1518 /* ethernet maximum frame size */
114
115 #define DECNET_SELF_FRAME(dnet_mac, msg) \
116 ((memcmp(dnet_mac, msg , 6) == 0) && \
117 (memcmp(dnet_mac, msg+6, 6) == 0))
118
119 struct eth_packet {
120 uint8 msg[ETH_FRAME_SIZE]; /* ethernet frame (message) */
121 int len; /* packet length without CRC */
122 int used; /* bytes processed (used in packet chaining) */
123 int status; /* transmit/receive status */
124 int crc_len; /* packet length with CRC */
125 };
126
127 struct eth_item {
128 int type; /* receive (0=setup, 1=loopback, 2=normal) */
129 struct eth_packet packet;
130 };
131
132 struct eth_queue {
133 int max;
134 int count;
135 int head;
136 int tail;
137 int loss;
138 int high;
139 struct eth_item* item;
140 };
141
142 struct eth_list {
143 int num;
144 char name[ETH_DEV_NAME_MAX];
145 char desc[ETH_DEV_DESC_MAX];
146 };
147
148 typedef int ETH_BOOL;
149 typedef unsigned char ETH_MAC[6];
150 typedef struct eth_packet ETH_PACK;
151 typedef void (*ETH_PCALLBACK)(int status);
152 typedef struct eth_list ETH_LIST;
153 typedef struct eth_queue ETH_QUE;
154 typedef struct eth_item ETH_ITEM;
155
156 struct eth_device {
157 char* name; /* name of ethernet device */
158 void* handle; /* handle of implementation-specific device */
159 ETH_PCALLBACK read_callback; /* read callback function */
160 ETH_PCALLBACK write_callback; /* write callback function */
161 ETH_PACK* read_packet; /* read packet */
162 ETH_PACK* write_packet; /* write packet */
163 ETH_MAC filter_address[ETH_FILTER_MAX]; /* filtering addresses */
164 int addr_count; /* count of filtering addresses */
165 ETH_BOOL promiscuous; /* promiscuous mode flag */
166 ETH_BOOL all_multicast; /* receive all multicast messages */
167 int32 decnet_self_sent; /* loopback packets sent but not seen */
168 ETH_MAC decnet_addr; /* decnet address of interface */
169 DEVICE* dptr; /* device ethernet is attached to */
170 uint32 dbit; /* debugging bit */
171 int reflections; /* packet reflections on interface */
172 int need_crc; /* device needs CRC (Cyclic Redundancy Check) */
173 #if defined (USE_READER_THREAD)
174 ETH_QUE read_queue;
175 pthread_mutex_t lock;
176 pthread_t reader_thread; /* Reader Thread Id */
177 #endif
178 };
179
180 typedef struct eth_device ETH_DEV;
181
182 /* prototype declarations*/
183
184 t_stat eth_open (ETH_DEV* dev, char* name, /* open ethernet interface */
185 DEVICE* dptr, uint32 dbit);
186 t_stat eth_close (ETH_DEV* dev); /* close ethernet interface */
187 t_stat eth_write (ETH_DEV* dev, ETH_PACK* packet, /* write sychronous packet; */
188 ETH_PCALLBACK routine); /* callback when done */
189 t_stat eth_read (ETH_DEV* dev, ETH_PACK* packet, /* read single packet; */
190 ETH_PCALLBACK routine); /* callback when done*/
191 t_stat eth_filter (ETH_DEV* dev, int addr_count, /* set filter on incoming packets */
192 ETH_MAC* addresses,
193 ETH_BOOL all_multicast,
194 ETH_BOOL promiscuous);
195 int eth_devices (int max, ETH_LIST* dev); /* get ethernet devices on host */
196 void eth_setcrc (ETH_DEV* dev, int need_crc); /* enable/disable CRC mode */
197
198 void eth_packet_trace (ETH_DEV* dev, const uint8 *msg, int len, char* txt); /* trace ethernet packet */
199 t_stat eth_show (FILE* st, UNIT* uptr, /* show ethernet devices */
200 int32 val, void* desc);
201
202 void eth_mac_fmt (ETH_MAC* add, char* buffer); /* format ethernet mac address */
203 t_stat eth_mac_scan (ETH_MAC* mac, char* strmac); /* scan string for mac, put in mac */
204
205 t_stat ethq_init (ETH_QUE* que, int max); /* initialize FIFO queue */
206 void ethq_clear (ETH_QUE* que); /* clear FIFO queue */
207 void ethq_remove (ETH_QUE* que); /* remove item from FIFO queue */
208 void ethq_insert (ETH_QUE* que, int32 type, /* insert item into FIFO queue */
209 ETH_PACK* packet, int32 status);
210
211
212 #endif /* _SIM_ETHER_H */