ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/ether_unix.cpp
Revision: 1.32
Committed: 2011-12-28T20:22:25Z (12 years, 9 months ago) by asvitkine
Branch: MAIN
Changes since 1.31: +0 -1 lines
Log Message:
fix some unused var/function warnings

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * ether_unix.cpp - Ethernet device driver, Unix specific stuff (Linux and FreeBSD)
3     *
4 gbeauche 1.31 * Basilisk II (C) 1997-2008 Christian Bauer
5 cebix 1.1 *
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 gbeauche 1.25 /*
24     * NOTES concerning MacOS X issues:
25     * - poll() does not exist in 10.2.8, but is available in 10.4.4
26     * - select(), and very likely poll(), are not cancellation points. So
27     * the ethernet thread doesn't stop on exit. An explicit check is
28     * performed to workaround this problem.
29     */
30     #if (defined __APPLE__ && defined __MACH__) || ! defined HAVE_POLL
31     #define USE_POLL 0
32     #else
33     #define USE_POLL 1
34     #endif
35    
36 gbeauche 1.29 // Define to let the slirp library determine the right timeout for select()
37     #define USE_SLIRP_TIMEOUT 1
38    
39 gbeauche 1.17 #ifdef HAVE_SYS_POLL_H
40     #include <sys/poll.h>
41     #endif
42 cebix 1.1 #include <sys/ioctl.h>
43 cebix 1.3 #include <sys/socket.h>
44 gbeauche 1.9 #include <sys/wait.h>
45 cebix 1.3 #include <netinet/in.h>
46 cebix 1.1 #include <pthread.h>
47     #include <semaphore.h>
48     #include <errno.h>
49     #include <stdio.h>
50 gbeauche 1.30 #include <signal.h>
51 cebix 1.3 #include <map>
52 cebix 1.2
53 gbeauche 1.17 #if defined(__FreeBSD__) || defined(sgi) || (defined(__APPLE__) && defined(__MACH__))
54 cebix 1.1 #include <net/if.h>
55     #endif
56    
57 gbeauche 1.9 #if defined(HAVE_LINUX_IF_H) && defined(HAVE_LINUX_IF_TUN_H)
58     #include <linux/if.h>
59     #include <linux/if_tun.h>
60     #endif
61    
62     #if defined(HAVE_NET_IF_H) && defined(HAVE_NET_IF_TUN_H)
63     #include <net/if.h>
64     #include <net/if_tun.h>
65     #endif
66    
67 gbeauche 1.18 #ifdef HAVE_SLIRP
68 gbeauche 1.16 #include "libslirp.h"
69 gbeauche 1.18 #endif
70 gbeauche 1.15
71 cebix 1.1 #include "cpu_emulation.h"
72     #include "main.h"
73     #include "macos_util.h"
74     #include "prefs.h"
75     #include "user_strings.h"
76     #include "ether.h"
77     #include "ether_defs.h"
78    
79 cebix 1.3 #ifndef NO_STD_NAMESPACE
80     using std::map;
81     #endif
82    
83 cebix 1.1 #define DEBUG 0
84     #include "debug.h"
85    
86 gbeauche 1.23 #define STATISTICS 0
87 cebix 1.1 #define MONITOR 0
88    
89    
90 gbeauche 1.9 // Ethernet device types
91     enum {
92     NET_IF_SHEEPNET,
93     NET_IF_ETHERTAP,
94     NET_IF_TUNTAP,
95 gbeauche 1.15 NET_IF_SLIRP
96 gbeauche 1.9 };
97    
98     // Constants
99     static const char ETHERCONFIG_FILE_NAME[] = DATADIR "/tunconfig";
100    
101 cebix 1.1 // Global variables
102     static int fd = -1; // fd of sheep_net device
103     static pthread_t ether_thread; // Packet reception thread
104     static pthread_attr_t ether_thread_attr; // Packet reception thread attributes
105     static bool thread_active = false; // Flag: Packet reception thread installed
106     static sem_t int_ack; // Interrupt acknowledge semaphore
107 cebix 1.2 static bool udp_tunnel; // Flag: UDP tunnelling active, fd is the socket descriptor
108 gbeauche 1.9 static int net_if_type = -1; // Ethernet device type
109 gbeauche 1.10 static char *net_if_name = NULL; // TUN/TAP device name
110 gbeauche 1.9 static const char *net_if_script = NULL; // Network config script
111 gbeauche 1.15 static pthread_t slirp_thread; // Slirp reception thread
112     static bool slirp_thread_active = false; // Flag: Slirp reception threadinstalled
113     static int slirp_output_fd = -1; // fd of slirp output pipe
114 gbeauche 1.21 static int slirp_input_fds[2] = { -1, -1 }; // fds of slirp input pipe
115 gbeauche 1.23 #ifdef SHEEPSHAVER
116     static bool net_open = false; // Flag: initialization succeeded, network device open
117     static uint8 ether_addr[6]; // Our Ethernet address
118     #else
119     const bool ether_driver_opened = true; // Flag: is the MacOS driver opened?
120     #endif
121 cebix 1.1
122 cebix 1.3 // Attached network protocols, maps protocol type to MacOS handler address
123     static map<uint16, uint32> net_protocols;
124    
125 cebix 1.1 // Prototypes
126     static void *receive_func(void *arg);
127 gbeauche 1.15 static void *slirp_receive_func(void *arg);
128 gbeauche 1.23 static int16 ether_do_add_multicast(uint8 *addr);
129     static int16 ether_do_del_multicast(uint8 *addr);
130     static int16 ether_do_write(uint32 arg);
131     static void ether_do_interrupt(void);
132 cebix 1.1
133    
134     /*
135 cebix 1.2 * Start packet reception thread
136     */
137    
138     static bool start_thread(void)
139     {
140     if (sem_init(&int_ack, 0, 0) < 0) {
141     printf("WARNING: Cannot init semaphore");
142     return false;
143     }
144    
145 cebix 1.7 Set_pthread_attr(&ether_thread_attr, 1);
146 cebix 1.2 thread_active = (pthread_create(&ether_thread, &ether_thread_attr, receive_func, NULL) == 0);
147     if (!thread_active) {
148     printf("WARNING: Cannot start Ethernet thread");
149     return false;
150     }
151    
152 gbeauche 1.15 #ifdef HAVE_SLIRP
153     if (net_if_type == NET_IF_SLIRP) {
154     slirp_thread_active = (pthread_create(&slirp_thread, NULL, slirp_receive_func, NULL) == 0);
155     if (!slirp_thread_active) {
156     printf("WARNING: Cannot start slirp reception thread\n");
157     return false;
158     }
159     }
160     #endif
161    
162 cebix 1.2 return true;
163     }
164    
165    
166     /*
167     * Stop packet reception thread
168     */
169    
170     static void stop_thread(void)
171     {
172 gbeauche 1.15 #ifdef HAVE_SLIRP
173     if (slirp_thread_active) {
174 gbeauche 1.20 #ifdef HAVE_PTHREAD_CANCEL
175 gbeauche 1.15 pthread_cancel(slirp_thread);
176 gbeauche 1.20 #endif
177 gbeauche 1.15 pthread_join(slirp_thread, NULL);
178     slirp_thread_active = false;
179     }
180     #endif
181    
182 cebix 1.2 if (thread_active) {
183 gbeauche 1.20 #ifdef HAVE_PTHREAD_CANCEL
184 cebix 1.2 pthread_cancel(ether_thread);
185 gbeauche 1.20 #endif
186 cebix 1.2 pthread_join(ether_thread, NULL);
187     sem_destroy(&int_ack);
188     thread_active = false;
189     }
190     }
191    
192    
193     /*
194 gbeauche 1.9 * Execute network script up|down
195     */
196    
197     static bool execute_network_script(const char *action)
198     {
199     if (net_if_script == NULL || net_if_name == NULL)
200     return false;
201    
202     int pid = fork();
203     if (pid >= 0) {
204     if (pid == 0) {
205     char *args[4];
206     args[0] = (char *)net_if_script;
207 gbeauche 1.10 args[1] = net_if_name;
208 gbeauche 1.9 args[2] = (char *)action;
209     args[3] = NULL;
210     execv(net_if_script, args);
211     exit(1);
212     }
213     int status;
214     while (waitpid(pid, &status, 0) != pid);
215     return WIFEXITED(status) && WEXITSTATUS(status) == 0;
216     }
217    
218     return false;
219     }
220    
221    
222     /*
223 cebix 1.1 * Initialization
224     */
225    
226 cebix 1.2 bool ether_init(void)
227 cebix 1.1 {
228 gbeauche 1.28 int val, nonblock = 1;
229 cebix 1.1 char str[256];
230    
231     // Do nothing if no Ethernet device specified
232     const char *name = PrefsFindString("ether");
233     if (name == NULL)
234 cebix 1.2 return false;
235 cebix 1.1
236 gbeauche 1.9 // Determine Ethernet device type
237     net_if_type = -1;
238     if (strncmp(name, "tap", 3) == 0)
239     net_if_type = NET_IF_ETHERTAP;
240     #if ENABLE_TUNTAP
241     else if (strcmp(name, "tun") == 0)
242     net_if_type = NET_IF_TUNTAP;
243     #endif
244 gbeauche 1.15 #ifdef HAVE_SLIRP
245     else if (strcmp(name, "slirp") == 0)
246     net_if_type = NET_IF_SLIRP;
247     #endif
248 gbeauche 1.9 else
249     net_if_type = NET_IF_SHEEPNET;
250 cebix 1.1
251 gbeauche 1.30 // Don't raise SIGPIPE, let errno be set to EPIPE
252     struct sigaction sigpipe_sa;
253     if (sigaction(SIGPIPE, NULL, &sigpipe_sa) == 0) {
254     assert(sigpipe_sa.sa_handler == SIG_DFL || sigpipe_sa.sa_handler == SIG_IGN);
255     sigfillset(&sigpipe_sa.sa_mask);
256     sigpipe_sa.sa_flags = 0;
257     sigpipe_sa.sa_handler = SIG_IGN;
258     sigaction(SIGPIPE, &sigpipe_sa, NULL);
259     }
260    
261 gbeauche 1.15 #ifdef HAVE_SLIRP
262     // Initialize slirp library
263     if (net_if_type == NET_IF_SLIRP) {
264 gbeauche 1.22 if (slirp_init() < 0) {
265     sprintf(str, GetString(STR_SLIRP_NO_DNS_FOUND_WARN));
266     WarningAlert(str);
267     return false;
268     }
269 gbeauche 1.15
270     // Open slirp output pipe
271     int fds[2];
272     if (pipe(fds) < 0)
273     return false;
274     fd = fds[0];
275     slirp_output_fd = fds[1];
276 gbeauche 1.21
277     // Open slirp input pipe
278     if (pipe(slirp_input_fds) < 0)
279     return false;
280 gbeauche 1.15 }
281     #endif
282    
283 gbeauche 1.11 // Open sheep_net or ethertap or TUN/TAP device
284 cebix 1.1 char dev_name[16];
285 gbeauche 1.9 switch (net_if_type) {
286     case NET_IF_ETHERTAP:
287 cebix 1.1 sprintf(dev_name, "/dev/%s", name);
288 gbeauche 1.9 break;
289     case NET_IF_TUNTAP:
290 gbeauche 1.11 strcpy(dev_name, "/dev/net/tun");
291 gbeauche 1.9 break;
292     case NET_IF_SHEEPNET:
293 cebix 1.1 strcpy(dev_name, "/dev/sheep_net");
294 gbeauche 1.9 break;
295     }
296 gbeauche 1.15 if (net_if_type != NET_IF_SLIRP) {
297     fd = open(dev_name, O_RDWR);
298     if (fd < 0) {
299     sprintf(str, GetString(STR_NO_SHEEP_NET_DRIVER_WARN), dev_name, strerror(errno));
300     WarningAlert(str);
301     goto open_error;
302     }
303 cebix 1.1 }
304    
305 gbeauche 1.9 #if ENABLE_TUNTAP
306     // Open TUN/TAP interface
307     if (net_if_type == NET_IF_TUNTAP) {
308     struct ifreq ifr;
309     memset(&ifr, 0, sizeof(ifr));
310     ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
311     strcpy(ifr.ifr_name, "tun%d");
312     if (ioctl(fd, TUNSETIFF, (void *) &ifr) != 0) {
313     sprintf(str, GetString(STR_SHEEP_NET_ATTACH_WARN), strerror(errno));
314     WarningAlert(str);
315     goto open_error;
316     }
317    
318     // Get network config script file path
319     net_if_script = PrefsFindString("etherconfig");
320     if (net_if_script == NULL)
321     net_if_script = ETHERCONFIG_FILE_NAME;
322    
323     // Start network script up
324     if (net_if_script == NULL) {
325     sprintf(str, GetString(STR_TUN_TAP_CONFIG_WARN), "script not found");
326     WarningAlert(str);
327     goto open_error;
328     }
329 gbeauche 1.10 net_if_name = strdup(ifr.ifr_name);
330 gbeauche 1.9 if (!execute_network_script("up")) {
331     sprintf(str, GetString(STR_TUN_TAP_CONFIG_WARN), "script execute error");
332     WarningAlert(str);
333     goto open_error;
334     }
335     D(bug("Connected to host network interface: %s\n", net_if_name));
336     }
337     #endif
338    
339 cebix 1.1 #if defined(__linux__)
340     // Attach sheep_net to selected Ethernet card
341 gbeauche 1.9 if (net_if_type == NET_IF_SHEEPNET && ioctl(fd, SIOCSIFLINK, name) < 0) {
342 cebix 1.1 sprintf(str, GetString(STR_SHEEP_NET_ATTACH_WARN), strerror(errno));
343     WarningAlert(str);
344     goto open_error;
345     }
346     #endif
347    
348     // Set nonblocking I/O
349 gbeauche 1.27 #ifdef USE_FIONBIO
350     if (ioctl(fd, FIONBIO, &nonblock) < 0) {
351     sprintf(str, GetString(STR_BLOCKING_NET_SOCKET_WARN), strerror(errno));
352     WarningAlert(str);
353     goto open_error;
354     }
355     #else
356 gbeauche 1.28 val = fcntl(fd, F_GETFL, 0);
357 gbeauche 1.27 if (val < 0 || fcntl(fd, F_SETFL, val | O_NONBLOCK) < 0) {
358     sprintf(str, GetString(STR_BLOCKING_NET_SOCKET_WARN), strerror(errno));
359     WarningAlert(str);
360     goto open_error;
361     }
362     #endif
363 cebix 1.1
364     // Get Ethernet address
365 gbeauche 1.9 if (net_if_type == NET_IF_ETHERTAP) {
366 cebix 1.1 pid_t p = getpid(); // If configured for multicast, ethertap requires that the lower 32 bit of the Ethernet address are our PID
367     ether_addr[0] = 0xfe;
368     ether_addr[1] = 0xfd;
369     ether_addr[2] = p >> 24;
370     ether_addr[3] = p >> 16;
371     ether_addr[4] = p >> 8;
372     ether_addr[5] = p;
373 gbeauche 1.15 #ifdef HAVE_SLIRP
374     } else if (net_if_type == NET_IF_SLIRP) {
375     ether_addr[0] = 0x52;
376     ether_addr[1] = 0x54;
377     ether_addr[2] = 0x00;
378     ether_addr[3] = 0x12;
379     ether_addr[4] = 0x34;
380     ether_addr[5] = 0x56;
381     #endif
382 cebix 1.1 } else
383     ioctl(fd, SIOCGIFADDR, ether_addr);
384     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]));
385    
386     // Start packet reception thread
387 cebix 1.2 if (!start_thread())
388 cebix 1.1 goto open_error;
389    
390     // Everything OK
391 cebix 1.2 return true;
392 cebix 1.1
393     open_error:
394 cebix 1.2 stop_thread();
395    
396 cebix 1.1 if (fd > 0) {
397     close(fd);
398     fd = -1;
399     }
400 gbeauche 1.21 if (slirp_input_fds[0] >= 0) {
401     close(slirp_input_fds[0]);
402     slirp_input_fds[0] = -1;
403     }
404     if (slirp_input_fds[1] >= 0) {
405     close(slirp_input_fds[1]);
406     slirp_input_fds[1] = -1;
407     }
408 gbeauche 1.15 if (slirp_output_fd >= 0) {
409     close(slirp_output_fd);
410     slirp_output_fd = -1;
411     }
412 cebix 1.2 return false;
413 cebix 1.1 }
414    
415    
416     /*
417     * Deinitialization
418     */
419    
420 cebix 1.2 void ether_exit(void)
421 cebix 1.1 {
422 gbeauche 1.20 // Stop reception threads
423     stop_thread();
424 cebix 1.1
425 gbeauche 1.10 // Shut down TUN/TAP interface
426     if (net_if_type == NET_IF_TUNTAP)
427     execute_network_script("down");
428    
429     // Free TUN/TAP device name
430     if (net_if_name)
431     free(net_if_name);
432    
433 cebix 1.1 // Close sheep_net device
434     if (fd > 0)
435     close(fd);
436 gbeauche 1.15
437 gbeauche 1.21 // Close slirp input buffer
438     if (slirp_input_fds[0] >= 0)
439     close(slirp_input_fds[0]);
440     if (slirp_input_fds[1] >= 0)
441     close(slirp_input_fds[1]);
442    
443 gbeauche 1.15 // Close slirp output buffer
444     if (slirp_output_fd > 0)
445     close(slirp_output_fd);
446 gbeauche 1.23
447     #if STATISTICS
448     // Show statistics
449     printf("%ld messages put on write queue\n", num_wput);
450     printf("%ld error acks\n", num_error_acks);
451     printf("%ld packets transmitted (%ld raw, %ld normal)\n", num_tx_packets, num_tx_raw_packets, num_tx_normal_packets);
452     printf("%ld tx packets dropped because buffer full\n", num_tx_buffer_full);
453     printf("%ld packets received\n", num_rx_packets);
454     printf("%ld packets passed upstream (%ld Fast Path, %ld normal)\n", num_rx_fastpath + num_unitdata_ind, num_rx_fastpath, num_unitdata_ind);
455     printf("EtherIRQ called %ld times\n", num_ether_irq);
456     printf("%ld rx packets dropped due to low memory\n", num_rx_no_mem);
457     printf("%ld rx packets dropped because no stream found\n", num_rx_dropped);
458     printf("%ld rx packets dropped because stream not ready\n", num_rx_stream_not_ready);
459     printf("%ld rx packets dropped because no memory for unitdata_ind\n", num_rx_no_unitdata_mem);
460     #endif
461 cebix 1.1 }
462    
463    
464     /*
465 gbeauche 1.23 * Glue around low-level implementation
466     */
467    
468     #ifdef SHEEPSHAVER
469     // Error codes
470     enum {
471     eMultiErr = -91,
472     eLenErr = -92,
473     lapProtErr = -94,
474     excessCollsns = -95
475     };
476    
477     // Initialize ethernet
478     void EtherInit(void)
479     {
480     net_open = false;
481    
482     // Do nothing if the user disabled the network
483     if (PrefsFindBool("nonet"))
484     return;
485    
486     net_open = ether_init();
487     }
488    
489     // Exit ethernet
490     void EtherExit(void)
491     {
492     ether_exit();
493     net_open = false;
494     }
495    
496     // Get ethernet hardware address
497     void AO_get_ethernet_address(uint32 arg)
498     {
499     uint8 *addr = Mac2HostAddr(arg);
500     if (net_open)
501     OTCopy48BitAddress(ether_addr, addr);
502     else {
503     addr[0] = 0x12;
504     addr[1] = 0x34;
505     addr[2] = 0x56;
506     addr[3] = 0x78;
507     addr[4] = 0x9a;
508     addr[5] = 0xbc;
509     }
510     D(bug("AO_get_ethernet_address: got address %02x%02x%02x%02x%02x%02x\n", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]));
511     }
512    
513     // Add multicast address
514     void AO_enable_multicast(uint32 addr)
515     {
516     if (net_open)
517     ether_do_add_multicast(Mac2HostAddr(addr));
518     }
519    
520     // Disable multicast address
521     void AO_disable_multicast(uint32 addr)
522     {
523     if (net_open)
524     ether_do_del_multicast(Mac2HostAddr(addr));
525     }
526    
527     // Transmit one packet
528     void AO_transmit_packet(uint32 mp)
529     {
530     if (net_open) {
531     switch (ether_do_write(mp)) {
532     case noErr:
533     num_tx_packets++;
534     break;
535     case excessCollsns:
536     num_tx_buffer_full++;
537     break;
538     }
539     }
540     }
541    
542     // Copy packet data from message block to linear buffer
543     static inline int ether_arg_to_buffer(uint32 mp, uint8 *p)
544     {
545     return ether_msgb_to_buffer(mp, p);
546     }
547    
548     // Ethernet interrupt
549     void EtherIRQ(void)
550     {
551     D(bug("EtherIRQ\n"));
552     num_ether_irq++;
553    
554     OTEnterInterrupt();
555     ether_do_interrupt();
556     OTLeaveInterrupt();
557    
558     // Acknowledge interrupt to reception thread
559     D(bug(" EtherIRQ done\n"));
560     sem_post(&int_ack);
561     }
562     #else
563     // Add multicast address
564     int16 ether_add_multicast(uint32 pb)
565     {
566     return ether_do_add_multicast(Mac2HostAddr(pb + eMultiAddr));
567     }
568    
569     // Disable multicast address
570     int16 ether_del_multicast(uint32 pb)
571     {
572     return ether_do_del_multicast(Mac2HostAddr(pb + eMultiAddr));
573     }
574    
575     // Transmit one packet
576     int16 ether_write(uint32 wds)
577     {
578     return ether_do_write(wds);
579     }
580    
581     // Copy packet data from WDS to linear buffer
582     static inline int ether_arg_to_buffer(uint32 wds, uint8 *p)
583     {
584     return ether_wds_to_buffer(wds, p);
585     }
586    
587     // Dispatch packet to protocol handler
588     static void ether_dispatch_packet(uint32 p, uint32 length)
589     {
590     // Get packet type
591     uint16 type = ReadMacInt16(p + 12);
592    
593     // Look for protocol
594     uint16 search_type = (type <= 1500 ? 0 : type);
595     if (net_protocols.find(search_type) == net_protocols.end())
596     return;
597     uint32 handler = net_protocols[search_type];
598    
599     // No default handler
600     if (handler == 0)
601     return;
602    
603     // Copy header to RHA
604     Mac2Mac_memcpy(ether_data + ed_RHA, p, 14);
605     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)));
606    
607     // Call protocol handler
608     M68kRegisters r;
609     r.d[0] = type; // Packet type
610     r.d[1] = length - 14; // Remaining packet length (without header, for ReadPacket)
611     r.a[0] = p + 14; // Pointer to packet (Mac address, for ReadPacket)
612     r.a[3] = ether_data + ed_RHA + 14; // Pointer behind header in RHA
613     r.a[4] = ether_data + ed_ReadPacket; // Pointer to ReadPacket/ReadRest routines
614     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]));
615     Execute68k(handler, &r);
616     }
617    
618     // Ethernet interrupt
619     void EtherInterrupt(void)
620     {
621     D(bug("EtherIRQ\n"));
622     ether_do_interrupt();
623    
624     // Acknowledge interrupt to reception thread
625     D(bug(" EtherIRQ done\n"));
626     sem_post(&int_ack);
627     }
628     #endif
629    
630    
631     /*
632 cebix 1.1 * Reset
633     */
634    
635 cebix 1.3 void ether_reset(void)
636 cebix 1.1 {
637 cebix 1.3 net_protocols.clear();
638 cebix 1.1 }
639    
640    
641     /*
642     * Add multicast address
643     */
644    
645 gbeauche 1.23 static int16 ether_do_add_multicast(uint8 *addr)
646 cebix 1.1 {
647 gbeauche 1.15 switch (net_if_type) {
648     case NET_IF_ETHERTAP:
649     case NET_IF_SHEEPNET:
650 gbeauche 1.23 if (ioctl(fd, SIOCADDMULTI, addr) < 0) {
651 gbeauche 1.15 D(bug("WARNING: Couldn't enable multicast address\n"));
652     if (net_if_type == NET_IF_ETHERTAP)
653     return noErr;
654     else
655     return eMultiErr;
656     }
657     default:
658 cebix 1.1 return noErr;
659 gbeauche 1.15 }
660 cebix 1.1 }
661    
662    
663     /*
664     * Delete multicast address
665     */
666    
667 gbeauche 1.23 static int16 ether_do_del_multicast(uint8 *addr)
668 cebix 1.1 {
669 gbeauche 1.15 switch (net_if_type) {
670     case NET_IF_ETHERTAP:
671     case NET_IF_SHEEPNET:
672 gbeauche 1.23 if (ioctl(fd, SIOCDELMULTI, addr) < 0) {
673 gbeauche 1.15 D(bug("WARNING: Couldn't disable multicast address\n"));
674     return eMultiErr;
675     }
676     default:
677 cebix 1.1 return noErr;
678 gbeauche 1.15 }
679 cebix 1.1 }
680    
681    
682     /*
683     * Attach protocol handler
684     */
685    
686     int16 ether_attach_ph(uint16 type, uint32 handler)
687     {
688 cebix 1.3 if (net_protocols.find(type) != net_protocols.end())
689 cebix 1.1 return lapProtErr;
690 cebix 1.3 net_protocols[type] = handler;
691     return noErr;
692 cebix 1.1 }
693    
694    
695     /*
696     * Detach protocol handler
697     */
698    
699     int16 ether_detach_ph(uint16 type)
700     {
701 cebix 1.3 if (net_protocols.erase(type) == 0)
702 cebix 1.1 return lapProtErr;
703 cebix 1.3 return noErr;
704 cebix 1.1 }
705    
706    
707     /*
708     * Transmit raw ethernet packet
709     */
710    
711 gbeauche 1.23 static int16 ether_do_write(uint32 arg)
712 cebix 1.1 {
713     // Copy packet to buffer
714     uint8 packet[1516], *p = packet;
715     int len = 0;
716     #if defined(__linux__)
717 gbeauche 1.9 if (net_if_type == NET_IF_ETHERTAP) {
718 cebix 1.1 *p++ = 0; // Linux ethertap discards the first 2 bytes
719     *p++ = 0;
720     len += 2;
721     }
722     #endif
723 gbeauche 1.23 len += ether_arg_to_buffer(arg, p);
724 cebix 1.1
725     #if MONITOR
726     bug("Sending Ethernet packet:\n");
727     for (int i=0; i<len; i++) {
728     bug("%02x ", packet[i]);
729     }
730     bug("\n");
731     #endif
732    
733     // Transmit packet
734 gbeauche 1.15 #ifdef HAVE_SLIRP
735     if (net_if_type == NET_IF_SLIRP) {
736 gbeauche 1.21 const int slirp_input_fd = slirp_input_fds[1];
737     write(slirp_input_fd, &len, sizeof(len));
738     write(slirp_input_fd, packet, len);
739 gbeauche 1.15 return noErr;
740     } else
741     #endif
742 cebix 1.1 if (write(fd, packet, len) < 0) {
743     D(bug("WARNING: Couldn't transmit packet\n"));
744     return excessCollsns;
745     } else
746     return noErr;
747     }
748    
749    
750     /*
751 cebix 1.2 * Start UDP packet reception thread
752     */
753    
754     bool ether_start_udp_thread(int socket_fd)
755     {
756     fd = socket_fd;
757     udp_tunnel = true;
758     return start_thread();
759     }
760    
761    
762     /*
763     * Stop UDP packet reception thread
764     */
765    
766     void ether_stop_udp_thread(void)
767     {
768     stop_thread();
769     fd = -1;
770     }
771    
772    
773     /*
774 gbeauche 1.15 * SLIRP output buffer glue
775     */
776    
777     #ifdef HAVE_SLIRP
778     int slirp_can_output(void)
779     {
780     return 1;
781     }
782    
783     void slirp_output(const uint8 *packet, int len)
784     {
785     write(slirp_output_fd, packet, len);
786     }
787    
788     void *slirp_receive_func(void *arg)
789     {
790 gbeauche 1.21 const int slirp_input_fd = slirp_input_fds[0];
791    
792 gbeauche 1.15 for (;;) {
793     // Wait for packets to arrive
794     fd_set rfds, wfds, xfds;
795     int nfds;
796     struct timeval tv;
797    
798 gbeauche 1.21 // ... in the input queue
799     FD_ZERO(&rfds);
800     FD_SET(slirp_input_fd, &rfds);
801     tv.tv_sec = 0;
802     tv.tv_usec = 0;
803     if (select(slirp_input_fd + 1, &rfds, NULL, NULL, &tv) > 0) {
804     int len;
805     read(slirp_input_fd, &len, sizeof(len));
806     uint8 packet[1516];
807     assert(len <= sizeof(packet));
808     read(slirp_input_fd, packet, len);
809     slirp_input(packet, len);
810     }
811    
812     // ... in the output queue
813 gbeauche 1.15 nfds = -1;
814     FD_ZERO(&rfds);
815     FD_ZERO(&wfds);
816     FD_ZERO(&xfds);
817 gbeauche 1.29 int timeout = slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
818     #if ! USE_SLIRP_TIMEOUT
819     timeout = 10000;
820     #endif
821 gbeauche 1.15 tv.tv_sec = 0;
822 gbeauche 1.29 tv.tv_usec = timeout;
823 gbeauche 1.15 if (select(nfds + 1, &rfds, &wfds, &xfds, &tv) >= 0)
824     slirp_select_poll(&rfds, &wfds, &xfds);
825 gbeauche 1.20
826     #ifdef HAVE_PTHREAD_TESTCANCEL
827     // Explicit cancellation point if select() was not covered
828     // This seems to be the case on MacOS X 10.2
829     pthread_testcancel();
830     #endif
831 gbeauche 1.15 }
832     return NULL;
833     }
834     #else
835     int slirp_can_output(void)
836     {
837     return 0;
838     }
839    
840     void slirp_output(const uint8 *packet, int len)
841     {
842     }
843     #endif
844    
845    
846     /*
847 cebix 1.1 * Packet reception thread
848     */
849    
850     static void *receive_func(void *arg)
851     {
852     for (;;) {
853    
854     // Wait for packets to arrive
855 gbeauche 1.25 #if USE_POLL
856 gbeauche 1.20 struct pollfd pf = {fd, POLLIN, 0};
857     int res = poll(&pf, 1, -1);
858     #else
859     fd_set rfds;
860     FD_ZERO(&rfds);
861     FD_SET(fd, &rfds);
862     // A NULL timeout could cause select() to block indefinitely,
863     // even if it is supposed to be a cancellation point [MacOS X]
864     struct timeval tv = { 0, 20000 };
865     int res = select(fd + 1, &rfds, NULL, NULL, &tv);
866     #ifdef HAVE_PTHREAD_TESTCANCEL
867     pthread_testcancel();
868     #endif
869     if (res == 0 || (res == -1 && errno == EINTR))
870     continue;
871     #endif
872 cebix 1.1 if (res <= 0)
873     break;
874    
875 gbeauche 1.23 if (ether_driver_opened) {
876     // Trigger Ethernet interrupt
877     D(bug(" packet received, triggering Ethernet interrupt\n"));
878     SetInterruptFlag(INTFLAG_ETHER);
879     TriggerInterrupt();
880    
881     // Wait for interrupt acknowledge by EtherInterrupt()
882     sem_wait(&int_ack);
883     } else
884 gbeauche 1.29 Delay_usec(20000);
885 cebix 1.1 }
886     return NULL;
887     }
888    
889    
890     /*
891     * Ethernet interrupt - activate deferred tasks to call IODone or protocol handlers
892     */
893    
894 gbeauche 1.23 void ether_do_interrupt(void)
895 cebix 1.1 {
896     // Call protocol handler for received packets
897 gbeauche 1.14 EthernetPacket ether_packet;
898     uint32 packet = ether_packet.addr();
899 cebix 1.2 ssize_t length;
900 cebix 1.1 for (;;) {
901    
902 gbeauche 1.23 #ifndef SHEEPSHAVER
903 cebix 1.2 if (udp_tunnel) {
904    
905     // Read packet from socket
906     struct sockaddr_in from;
907     socklen_t from_len = sizeof(from);
908 gbeauche 1.14 length = recvfrom(fd, Mac2HostAddr(packet), 1514, 0, (struct sockaddr *)&from, &from_len);
909 cebix 1.2 if (length < 14)
910     break;
911     ether_udp_read(packet, length, &from);
912    
913 gbeauche 1.23 } else
914     #endif
915     {
916 cebix 1.2
917     // Read packet from sheep_net device
918 cebix 1.1 #if defined(__linux__)
919 gbeauche 1.14 length = read(fd, Mac2HostAddr(packet), net_if_type == NET_IF_ETHERTAP ? 1516 : 1514);
920 cebix 1.1 #else
921 gbeauche 1.14 length = read(fd, Mac2HostAddr(packet), 1514);
922 cebix 1.1 #endif
923 cebix 1.2 if (length < 14)
924     break;
925 cebix 1.1
926     #if MONITOR
927 cebix 1.2 bug("Receiving Ethernet packet:\n");
928     for (int i=0; i<length; i++) {
929 gbeauche 1.14 bug("%02x ", ReadMacInt8(packet + i));
930 cebix 1.2 }
931     bug("\n");
932 cebix 1.1 #endif
933    
934 cebix 1.2 // Pointer to packet data (Ethernet header)
935 gbeauche 1.14 uint32 p = packet;
936 cebix 1.1 #if defined(__linux__)
937 gbeauche 1.9 if (net_if_type == NET_IF_ETHERTAP) {
938 cebix 1.2 p += 2; // Linux ethertap has two random bytes before the packet
939     length -= 2;
940     }
941 cebix 1.1 #endif
942    
943 gbeauche 1.23 // Dispatch packet
944     ether_dispatch_packet(p, length);
945 cebix 1.2 }
946 cebix 1.1 }
947     }