ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/Linux/ether_linux.cpp
Revision: 1.1
Committed: 1999-10-03T14:16:25Z (24 years, 11 months ago) by cebix
Branch: MAIN
Branch point for: cebix
Log Message:
Initial revision

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * ether_unix.cpp - Ethernet device driver, Unix specific stuff
3     *
4     * Basilisk II (C) 1997-1999 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     #include <semaphore.h>
26     #include <errno.h>
27     #include <stdio.h>
28    
29     #include "cpu_emulation.h"
30     #include "main.h"
31     #include "macos_util.h"
32     #include "prefs.h"
33     #include "user_strings.h"
34     #include "ether.h"
35     #include "ether_defs.h"
36    
37     #define DEBUG 0
38     #include "debug.h"
39    
40     #define MONITOR 0
41    
42    
43     // List of attached protocols
44     struct NetProtocol {
45     NetProtocol *next;
46     uint16 type;
47     uint32 handler;
48     };
49    
50     static NetProtocol *prot_list = NULL;
51    
52    
53     // Global variables
54     static int fd = -1; // fd of sheep_net device
55     static pthread_t ether_thread; // Packet reception thread
56     static pthread_attr_t ether_thread_attr; // Packet reception thread attributes
57     static bool thread_active = false; // Flag: Packet reception thread installed
58     static sem_t int_ack; // Interrupt acknowledge semaphore
59     static bool is_ethertap; // Flag: Ethernet device is ethertap
60    
61     // Prototypes
62     static void *receive_func(void *arg);
63    
64    
65     /*
66     * Find protocol in list
67     */
68    
69     static NetProtocol *find_protocol(uint16 type)
70     {
71     // All 802.2 types are the same
72     if (type <= 1500)
73     type = 0;
74    
75     // Search list (we could use hashing here but there are usually only three
76     // handlers installed: 0x0000 for AppleTalk and 0x0800/0x0806 for TCP/IP)
77     NetProtocol *p = prot_list;
78     while (p) {
79     if (p->type == type)
80     return p;
81     p = p->next;
82     }
83     return NULL;
84     }
85    
86    
87     /*
88     * Initialization
89     */
90    
91     void EtherInit(void)
92     {
93     int nonblock = 1;
94     char str[256];
95    
96     // Do nothing if no Ethernet device specified
97     const char *name = PrefsFindString("ether");
98     if (name == NULL)
99     return;
100    
101     // Is it Ethertap?
102     is_ethertap = (strncmp(name, "tap", 3) == 0);
103    
104     // Open sheep_net or ethertap device
105     char dev_name[16];
106     if (is_ethertap)
107     sprintf(dev_name, "/dev/%s", name);
108     else
109     strcpy(dev_name, "/dev/sheep_net");
110     fd = open(dev_name, O_RDWR);
111     if (fd < 0) {
112     sprintf(str, GetString(STR_NO_SHEEP_NET_DRIVER_WARN), dev_name, strerror(errno));
113     WarningAlert(str);
114     goto open_error;
115     }
116    
117     // Attach sheep_net to selected Ethernet card
118     if (!is_ethertap && ioctl(fd, SIOCSIFLINK, name) < 0) {
119     sprintf(str, GetString(STR_SHEEP_NET_ATTACH_WARN), strerror(errno));
120     WarningAlert(str);
121     goto open_error;
122     }
123    
124     // Set nonblocking I/O
125     ioctl(fd, FIONBIO, &nonblock);
126    
127     // Get Ethernet address
128     if (is_ethertap) {
129     pid_t p = getpid(); // If configured for multicast, ethertap requires that the lower 32 bit of the Ethernet address are our PID
130     ether_addr[0] = 0xfe;
131     ether_addr[1] = 0xfd;
132     ether_addr[2] = p >> 24;
133     ether_addr[3] = p >> 16;
134     ether_addr[4] = p >> 8;
135     ether_addr[5] = p;
136     } else
137     ioctl(fd, SIOCGIFADDR, ether_addr);
138     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]));
139    
140     // Start packet reception thread
141     if (sem_init(&int_ack, 0, 0) < 0) {
142     printf("WARNING: Cannot init semaphore");
143     goto open_error;
144     }
145     pthread_attr_init(&ether_thread_attr);
146     #if defined(_POSIX_THREAD_PRIORITY_SCHEDULING)
147     if (geteuid() == 0) {
148     pthread_attr_setinheritsched(&ether_thread_attr, PTHREAD_EXPLICIT_SCHED);
149     pthread_attr_setschedpolicy(&ether_thread_attr, SCHED_FIFO);
150     struct sched_param fifo_param;
151     fifo_param.sched_priority = (sched_get_priority_min(SCHED_FIFO) + sched_get_priority_max(SCHED_FIFO)) / 2 + 1;
152     pthread_attr_setschedparam(&ether_thread_attr, &fifo_param);
153     }
154     #endif
155     thread_active = (pthread_create(&ether_thread, &ether_thread_attr, receive_func, NULL) == 0);
156     if (!thread_active) {
157     printf("WARNING: Cannot start Ethernet thread");
158     goto open_error;
159     }
160    
161     // Everything OK
162     net_open = true;
163     return;
164    
165     open_error:
166     if (thread_active) {
167     pthread_cancel(ether_thread);
168     pthread_join(ether_thread, NULL);
169     sem_destroy(&int_ack);
170     thread_active = false;
171     }
172     if (fd > 0) {
173     close(fd);
174     fd = -1;
175     }
176     }
177    
178    
179     /*
180     * Deinitialization
181     */
182    
183     void EtherExit(void)
184     {
185     // Stop reception thread
186     if (thread_active) {
187     pthread_cancel(ether_thread);
188     pthread_join(ether_thread, NULL);
189     sem_destroy(&int_ack);
190     thread_active = false;
191     }
192    
193     // Close sheep_net device
194     if (fd > 0)
195     close(fd);
196    
197     // Remove all protocols
198     NetProtocol *p = prot_list;
199     while (p) {
200     NetProtocol *next = p->next;
201     delete p;
202     p = next;
203     }
204     }
205    
206    
207     /*
208     * Reset
209     */
210    
211     void EtherReset(void)
212     {
213     // Remove all protocols
214     NetProtocol *p = prot_list;
215     while (p) {
216     NetProtocol *next = p->next;
217     delete p;
218     p = next;
219     }
220     }
221    
222    
223     /*
224     * Add multicast address
225     */
226    
227     int16 ether_add_multicast(uint32 pb)
228     {
229     if (ioctl(fd, SIOCADDMULTI, Mac2HostAddr(pb + eMultiAddr)) < 0) {
230     D(bug("WARNING: Couldn't enable multicast address\n"));
231     if (is_ethertap)
232     return noErr;
233     else
234     return eMultiErr;
235     } else
236     return noErr;
237     }
238    
239    
240     /*
241     * Delete multicast address
242     */
243    
244     int16 ether_del_multicast(uint32 pb)
245     {
246     if (ioctl(fd, SIOCDELMULTI, Mac2HostAddr(pb + eMultiAddr)) < 0) {
247     D(bug("WARNING: Couldn't disable multicast address\n"));
248     return eMultiErr;
249     } else
250     return noErr;
251     }
252    
253    
254     /*
255     * Attach protocol handler
256     */
257    
258     int16 ether_attach_ph(uint16 type, uint32 handler)
259     {
260     // Already attached?
261     NetProtocol *p = find_protocol(type);
262     if (p != NULL)
263     return lapProtErr;
264     else {
265     // No, create and attach
266     p = new NetProtocol;
267     p->next = prot_list;
268     p->type = type;
269     p->handler = handler;
270     prot_list = p;
271     return noErr;
272     }
273     }
274    
275    
276     /*
277     * Detach protocol handler
278     */
279    
280     int16 ether_detach_ph(uint16 type)
281     {
282     NetProtocol *p = find_protocol(type);
283     if (p != NULL) {
284     NetProtocol *q = prot_list;
285     if (p == q) {
286     prot_list = NULL;
287     delete p;
288     return noErr;
289     }
290     while (q) {
291     if (q->next == p) {
292     q->next = p->next;
293     delete p;
294     return noErr;
295     }
296     q = q->next;
297     }
298     return lapProtErr;
299     } else
300     return lapProtErr;
301     }
302    
303    
304     /*
305     * Transmit raw ethernet packet
306     */
307    
308     int16 ether_write(uint32 wds)
309     {
310     // Set source address
311     uint32 hdr = ReadMacInt32(wds + 2);
312     memcpy(Mac2HostAddr(hdr + 6), ether_addr, 6);
313    
314     // Copy packet to buffer
315     uint8 packet[1516], *p = packet;
316     int len = 0;
317     if (is_ethertap) {
318     *p++ = 0; // Ethertap discards the first 2 bytes
319     *p++ = 0;
320     len += 2;
321     }
322     for (;;) {
323     int w = ReadMacInt16(wds);
324     if (w == 0)
325     break;
326     memcpy(p, Mac2HostAddr(ReadMacInt32(wds + 2)), w);
327     len += w;
328     p += w;
329     wds += 6;
330     }
331    
332     #if MONITOR
333     bug("Sending Ethernet packet:\n");
334     for (int i=0; i<len; i++) {
335     bug("%02x ", packet[i]);
336     }
337     bug("\n");
338     #endif
339    
340     // Transmit packet
341     if (write(fd, packet, len) < 0) {
342     D(bug("WARNING: Couldn't transmit packet\n"));
343     return excessCollsns;
344     } else
345     return noErr;
346     }
347    
348    
349     /*
350     * Packet reception thread
351     */
352    
353     static void *receive_func(void *arg)
354     {
355     for (;;) {
356    
357     // Wait for packets to arrive
358     struct pollfd pf = {fd, POLLIN, 0};
359     int res = poll(&pf, 1, -1);
360     if (res <= 0)
361     break;
362    
363     // Trigger Ethernet interrupt
364     D(bug(" packet received, triggering Ethernet interrupt\n"));
365     SetInterruptFlag(INTFLAG_ETHER);
366     TriggerInterrupt();
367    
368     // Wait for interrupt acknowledge by EtherInterrupt()
369     sem_wait(&int_ack);
370     }
371     return NULL;
372     }
373    
374    
375     /*
376     * Ethernet interrupt - activate deferred tasks to call IODone or protocol handlers
377     */
378    
379     void EtherInterrupt(void)
380     {
381     D(bug("EtherIRQ\n"));
382    
383     // Call protocol handler for received packets
384     uint8 packet[1516];
385     for (;;) {
386    
387     // Read packet from sheep_net device
388     ssize_t length = read(fd, packet, is_ethertap ? 1516 : 1514);
389     if (length < 14)
390     break;
391    
392     #if MONITOR
393     bug("Receiving Ethernet packet:\n");
394     for (int i=0; i<length; i++) {
395     bug("%02x ", packet[i]);
396     }
397     bug("\n");
398     #endif
399    
400     // Pointer to packet data (Ethernet header)
401     uint8 *p = packet;
402     if (is_ethertap) {
403     p += 2; // Ethertap has two random bytes before the packet
404     length -= 2;
405     }
406    
407     // Get packet type
408     uint16 type = ntohs(*(uint16 *)(p + 12));
409    
410     // Look for protocol
411     NetProtocol *prot = find_protocol(type);
412     if (prot == NULL)
413     continue;
414    
415     // No default handler
416     if (prot->handler == 0)
417     continue;
418    
419     // Copy header to RHA
420     memcpy(Mac2HostAddr(ether_data + ed_RHA), p, 14);
421     D(bug(" header %08lx%04lx %08lx%04lx %04lx\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)));
422    
423     // Call protocol handler
424     M68kRegisters r;
425     r.d[0] = type; // Packet type
426     r.d[1] = length - 14; // Remaining packet length (without header, for ReadPacket)
427     r.a[0] = (uint32)p + 14; // Pointer to packet (host address, for ReadPacket)
428     r.a[3] = ether_data + ed_RHA + 14; // Pointer behind header in RHA
429     r.a[4] = ether_data + ed_ReadPacket; // Pointer to ReadPacket/ReadRest routines
430     D(bug(" calling protocol handler %08lx, type %08lx, length %08lx, data %08lx, rha %08lx, read_packet %08lx\n", prot->handler, r.d[0], r.d[1], r.a[0], r.a[3], r.a[4]));
431     Execute68k(prot->handler, &r);
432     }
433    
434     // Acknowledge interrupt to reception thread
435     D(bug(" EtherIRQ done\n"));
436     sem_post(&int_ack);
437     }