ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/ether_unix.cpp
Revision: 1.29
Committed: 2006-04-02T21:06:50Z (18 years, 6 months ago) by gbeauche
Branch: MAIN
CVS Tags: nigel-build-19
Changes since 1.28: +9 -3 lines
Log Message:
Try to improve slirp performance again (though passive mode is still slower)

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