ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/ether_unix.cpp
Revision: 1.3
Committed: 2001-07-13T15:39:23Z (23 years, 2 months ago) by cebix
Branch: MAIN
Changes since 1.2: +23 -91 lines
Log Message:
- updated the TECH document
- EtherReset() clears the UDP protocol list
- audio_oss_esd.cpp: AudioExit() calls close_audio()
- ether_unix.cpp: uses map<> for protocol handlers
- updated audio_dummy.cpp and ether_dummy.cpp

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * ether_unix.cpp - Ethernet device driver, Unix specific stuff (Linux and FreeBSD)
3     *
4     * Basilisk II (C) 1997-2001 Christian Bauer
5     *
6     * This program is free software; you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19     */
20    
21     #include "sysdeps.h"
22    
23     #include <sys/ioctl.h>
24     #include <sys/poll.h>
25 cebix 1.3 #include <sys/socket.h>
26     #include <netinet/in.h>
27 cebix 1.1 #include <pthread.h>
28     #include <semaphore.h>
29     #include <errno.h>
30     #include <stdio.h>
31 cebix 1.3 #include <map>
32 cebix 1.2
33 cebix 1.1 #if defined(__FreeBSD__)
34     #include <net/if.h>
35     #endif
36    
37     #include "cpu_emulation.h"
38     #include "main.h"
39     #include "macos_util.h"
40     #include "prefs.h"
41     #include "user_strings.h"
42     #include "ether.h"
43     #include "ether_defs.h"
44    
45 cebix 1.3 #ifndef NO_STD_NAMESPACE
46     using std::map;
47     #endif
48    
49 cebix 1.1 #define DEBUG 0
50     #include "debug.h"
51    
52     #define MONITOR 0
53    
54    
55     // Global variables
56     static int fd = -1; // fd of sheep_net device
57     static pthread_t ether_thread; // Packet reception thread
58     static pthread_attr_t ether_thread_attr; // Packet reception thread attributes
59     static bool thread_active = false; // Flag: Packet reception thread installed
60     static sem_t int_ack; // Interrupt acknowledge semaphore
61     static bool is_ethertap; // Flag: Ethernet device is ethertap
62 cebix 1.2 static bool udp_tunnel; // Flag: UDP tunnelling active, fd is the socket descriptor
63 cebix 1.1
64 cebix 1.3 // Attached network protocols, maps protocol type to MacOS handler address
65     static map<uint16, uint32> net_protocols;
66    
67 cebix 1.1 // Prototypes
68     static void *receive_func(void *arg);
69    
70    
71     /*
72 cebix 1.2 * Start packet reception thread
73     */
74    
75     static bool start_thread(void)
76     {
77     if (sem_init(&int_ack, 0, 0) < 0) {
78     printf("WARNING: Cannot init semaphore");
79     return false;
80     }
81    
82     pthread_attr_init(&ether_thread_attr);
83     #if defined(_POSIX_THREAD_PRIORITY_SCHEDULING)
84     if (geteuid() == 0) {
85     pthread_attr_setinheritsched(&ether_thread_attr, PTHREAD_EXPLICIT_SCHED);
86     pthread_attr_setschedpolicy(&ether_thread_attr, SCHED_FIFO);
87     struct sched_param fifo_param;
88     fifo_param.sched_priority = (sched_get_priority_min(SCHED_FIFO) + sched_get_priority_max(SCHED_FIFO)) / 2 + 1;
89     pthread_attr_setschedparam(&ether_thread_attr, &fifo_param);
90     }
91     #endif
92    
93     thread_active = (pthread_create(&ether_thread, &ether_thread_attr, receive_func, NULL) == 0);
94     if (!thread_active) {
95     printf("WARNING: Cannot start Ethernet thread");
96     return false;
97     }
98    
99     return true;
100     }
101    
102    
103     /*
104     * Stop packet reception thread
105     */
106    
107     static void stop_thread(void)
108     {
109     if (thread_active) {
110     pthread_cancel(ether_thread);
111     pthread_join(ether_thread, NULL);
112     sem_destroy(&int_ack);
113     thread_active = false;
114     }
115     }
116    
117    
118     /*
119 cebix 1.1 * Initialization
120     */
121    
122 cebix 1.2 bool ether_init(void)
123 cebix 1.1 {
124     int nonblock = 1;
125     char str[256];
126    
127     // Do nothing if no Ethernet device specified
128     const char *name = PrefsFindString("ether");
129     if (name == NULL)
130 cebix 1.2 return false;
131 cebix 1.1
132     // Is it Ethertap?
133     is_ethertap = (strncmp(name, "tap", 3) == 0);
134    
135     // Open sheep_net or ethertap device
136     char dev_name[16];
137     if (is_ethertap)
138     sprintf(dev_name, "/dev/%s", name);
139     else
140     strcpy(dev_name, "/dev/sheep_net");
141     fd = open(dev_name, O_RDWR);
142     if (fd < 0) {
143     sprintf(str, GetString(STR_NO_SHEEP_NET_DRIVER_WARN), dev_name, strerror(errno));
144     WarningAlert(str);
145     goto open_error;
146     }
147    
148     #if defined(__linux__)
149     // Attach sheep_net to selected Ethernet card
150     if (!is_ethertap && ioctl(fd, SIOCSIFLINK, name) < 0) {
151     sprintf(str, GetString(STR_SHEEP_NET_ATTACH_WARN), strerror(errno));
152     WarningAlert(str);
153     goto open_error;
154     }
155     #endif
156    
157     // Set nonblocking I/O
158     ioctl(fd, FIONBIO, &nonblock);
159    
160     // Get Ethernet address
161     if (is_ethertap) {
162     pid_t p = getpid(); // If configured for multicast, ethertap requires that the lower 32 bit of the Ethernet address are our PID
163     ether_addr[0] = 0xfe;
164     ether_addr[1] = 0xfd;
165     ether_addr[2] = p >> 24;
166     ether_addr[3] = p >> 16;
167     ether_addr[4] = p >> 8;
168     ether_addr[5] = p;
169     } else
170     ioctl(fd, SIOCGIFADDR, ether_addr);
171     D(bug("Ethernet address %02x %02x %02x %02x %02x %02x\n", ether_addr[0], ether_addr[1], ether_addr[2], ether_addr[3], ether_addr[4], ether_addr[5]));
172    
173     // Start packet reception thread
174 cebix 1.2 if (!start_thread())
175 cebix 1.1 goto open_error;
176    
177     // Everything OK
178 cebix 1.2 return true;
179 cebix 1.1
180     open_error:
181 cebix 1.2 stop_thread();
182    
183 cebix 1.1 if (fd > 0) {
184     close(fd);
185     fd = -1;
186     }
187 cebix 1.2 return false;
188 cebix 1.1 }
189    
190    
191     /*
192     * Deinitialization
193     */
194    
195 cebix 1.2 void ether_exit(void)
196 cebix 1.1 {
197     // Stop reception thread
198     if (thread_active) {
199     pthread_cancel(ether_thread);
200     pthread_join(ether_thread, NULL);
201     sem_destroy(&int_ack);
202     thread_active = false;
203     }
204    
205     // Close sheep_net device
206     if (fd > 0)
207     close(fd);
208     }
209    
210    
211     /*
212     * Reset
213     */
214    
215 cebix 1.3 void ether_reset(void)
216 cebix 1.1 {
217 cebix 1.3 net_protocols.clear();
218 cebix 1.1 }
219    
220    
221     /*
222     * Add multicast address
223     */
224    
225     int16 ether_add_multicast(uint32 pb)
226     {
227     if (ioctl(fd, SIOCADDMULTI, Mac2HostAddr(pb + eMultiAddr)) < 0) {
228     D(bug("WARNING: Couldn't enable multicast address\n"));
229     if (is_ethertap)
230     return noErr;
231     else
232     return eMultiErr;
233     } else
234     return noErr;
235     }
236    
237    
238     /*
239     * Delete multicast address
240     */
241    
242     int16 ether_del_multicast(uint32 pb)
243     {
244     if (ioctl(fd, SIOCDELMULTI, Mac2HostAddr(pb + eMultiAddr)) < 0) {
245     D(bug("WARNING: Couldn't disable multicast address\n"));
246     return eMultiErr;
247     } else
248     return noErr;
249     }
250    
251    
252     /*
253     * Attach protocol handler
254     */
255    
256     int16 ether_attach_ph(uint16 type, uint32 handler)
257     {
258 cebix 1.3 if (net_protocols.find(type) != net_protocols.end())
259 cebix 1.1 return lapProtErr;
260 cebix 1.3 net_protocols[type] = handler;
261     return noErr;
262 cebix 1.1 }
263    
264    
265     /*
266     * Detach protocol handler
267     */
268    
269     int16 ether_detach_ph(uint16 type)
270     {
271 cebix 1.3 if (net_protocols.erase(type) == 0)
272 cebix 1.1 return lapProtErr;
273 cebix 1.3 return noErr;
274 cebix 1.1 }
275    
276    
277     /*
278     * Transmit raw ethernet packet
279     */
280    
281     int16 ether_write(uint32 wds)
282     {
283     // Set source address
284     uint32 hdr = ReadMacInt32(wds + 2);
285     Host2Mac_memcpy(hdr + 6, ether_addr, 6);
286    
287     // Copy packet to buffer
288     uint8 packet[1516], *p = packet;
289     int len = 0;
290     #if defined(__linux__)
291     if (is_ethertap) {
292     *p++ = 0; // Linux ethertap discards the first 2 bytes
293     *p++ = 0;
294     len += 2;
295     }
296     #endif
297 cebix 1.2 len += ether_wds_to_buffer(wds, p);
298 cebix 1.1
299     #if MONITOR
300     bug("Sending Ethernet packet:\n");
301     for (int i=0; i<len; i++) {
302     bug("%02x ", packet[i]);
303     }
304     bug("\n");
305     #endif
306    
307     // Transmit packet
308     if (write(fd, packet, len) < 0) {
309     D(bug("WARNING: Couldn't transmit packet\n"));
310     return excessCollsns;
311     } else
312     return noErr;
313     }
314    
315    
316     /*
317 cebix 1.2 * Start UDP packet reception thread
318     */
319    
320     bool ether_start_udp_thread(int socket_fd)
321     {
322     fd = socket_fd;
323     udp_tunnel = true;
324     return start_thread();
325     }
326    
327    
328     /*
329     * Stop UDP packet reception thread
330     */
331    
332     void ether_stop_udp_thread(void)
333     {
334     stop_thread();
335     fd = -1;
336     }
337    
338    
339     /*
340 cebix 1.1 * Packet reception thread
341     */
342    
343     static void *receive_func(void *arg)
344     {
345     for (;;) {
346    
347     // Wait for packets to arrive
348     struct pollfd pf = {fd, POLLIN, 0};
349     int res = poll(&pf, 1, -1);
350     if (res <= 0)
351     break;
352    
353     // Trigger Ethernet interrupt
354     D(bug(" packet received, triggering Ethernet interrupt\n"));
355     SetInterruptFlag(INTFLAG_ETHER);
356     TriggerInterrupt();
357    
358     // Wait for interrupt acknowledge by EtherInterrupt()
359     sem_wait(&int_ack);
360     }
361     return NULL;
362     }
363    
364    
365     /*
366     * Ethernet interrupt - activate deferred tasks to call IODone or protocol handlers
367     */
368    
369     void EtherInterrupt(void)
370     {
371     D(bug("EtherIRQ\n"));
372    
373     // Call protocol handler for received packets
374     uint8 packet[1516];
375 cebix 1.2 ssize_t length;
376 cebix 1.1 for (;;) {
377    
378 cebix 1.2 if (udp_tunnel) {
379    
380     // Read packet from socket
381     struct sockaddr_in from;
382     socklen_t from_len = sizeof(from);
383     length = recvfrom(fd, packet, 1514, 0, (struct sockaddr *)&from, &from_len);
384     if (length < 14)
385     break;
386     ether_udp_read(packet, length, &from);
387    
388     } else {
389    
390     // Read packet from sheep_net device
391 cebix 1.1 #if defined(__linux__)
392 cebix 1.2 length = read(fd, packet, is_ethertap ? 1516 : 1514);
393 cebix 1.1 #else
394 cebix 1.2 length = read(fd, packet, 1514);
395 cebix 1.1 #endif
396 cebix 1.2 if (length < 14)
397     break;
398 cebix 1.1
399     #if MONITOR
400 cebix 1.2 bug("Receiving Ethernet packet:\n");
401     for (int i=0; i<length; i++) {
402     bug("%02x ", packet[i]);
403     }
404     bug("\n");
405 cebix 1.1 #endif
406    
407 cebix 1.2 // Pointer to packet data (Ethernet header)
408     uint8 *p = packet;
409 cebix 1.1 #if defined(__linux__)
410 cebix 1.2 if (is_ethertap) {
411     p += 2; // Linux ethertap has two random bytes before the packet
412     length -= 2;
413     }
414 cebix 1.1 #endif
415    
416 cebix 1.2 // Get packet type
417     uint16 type = (p[12] << 8) | p[13];
418 cebix 1.1
419 cebix 1.2 // Look for protocol
420 cebix 1.3 uint16 search_type = (type <= 1500 ? 0 : type);
421     if (net_protocols.find(search_type) == net_protocols.end())
422 cebix 1.2 continue;
423 cebix 1.3 uint32 handler = net_protocols[search_type];
424 cebix 1.2
425     // No default handler
426 cebix 1.3 if (handler == 0)
427 cebix 1.2 continue;
428    
429     // Copy header to RHA
430     Host2Mac_memcpy(ether_data + ed_RHA, p, 14);
431     D(bug(" header %08x%04x %08x%04x %04x\n", ReadMacInt32(ether_data + ed_RHA), ReadMacInt16(ether_data + ed_RHA + 4), ReadMacInt32(ether_data + ed_RHA + 6), ReadMacInt16(ether_data + ed_RHA + 10), ReadMacInt16(ether_data + ed_RHA + 12)));
432    
433     // Call protocol handler
434     M68kRegisters r;
435     r.d[0] = type; // Packet type
436     r.d[1] = length - 14; // Remaining packet length (without header, for ReadPacket)
437     r.a[0] = (uint32)p + 14; // Pointer to packet (host address, for ReadPacket)
438     r.a[3] = ether_data + ed_RHA + 14; // Pointer behind header in RHA
439     r.a[4] = ether_data + ed_ReadPacket; // Pointer to ReadPacket/ReadRest routines
440 cebix 1.3 D(bug(" calling protocol handler %08x, type %08x, length %08x, data %08x, rha %08x, read_packet %08x\n", handler, r.d[0], r.d[1], r.a[0], r.a[3], r.a[4]));
441     Execute68k(handler, &r);
442 cebix 1.2 }
443 cebix 1.1 }
444    
445     // Acknowledge interrupt to reception thread
446     D(bug(" EtherIRQ done\n"));
447     sem_post(&int_ack);
448     }