ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Unix/Linux/ether_linux.cpp
Revision: 1.6
Committed: 2005-01-30T21:48:20Z (19 years, 10 months ago) by gbeauche
Branch: MAIN
Changes since 1.5: +1 -1 lines
Log Message:
Happy New Year 2005!

File Contents

# Content
1 /*
2 * ether_linux.cpp - SheepShaver Ethernet Device Driver (DLPI), Linux specific stuff
3 *
4 * SheepShaver (C) 1997-2005 Marc Hellwig and 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 <pthread.h>
24 #include <semaphore.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <sys/ioctl.h>
29 #include <sys/poll.h>
30 #include <sys/wait.h>
31 #include <stdio.h>
32 #include <string.h>
33
34 #if defined(HAVE_LINUX_IF_H) && defined(HAVE_LINUX_IF_TUN_H)
35 #include <linux/if.h>
36 #include <linux/if_tun.h>
37 #endif
38
39 #if defined(HAVE_NET_IF_H) && defined(HAVE_NET_IF_TUN_H)
40 #include <net/if.h>
41 #include <net/if_tun.h>
42 #endif
43
44 #include "main.h"
45 #include "macos_util.h"
46 #include "prefs.h"
47 #include "user_strings.h"
48 #include "ether.h"
49 #include "ether_defs.h"
50
51 #define DEBUG 0
52 #include "debug.h"
53
54 #define STATISTICS 0
55 #define MONITOR 0
56
57
58 // Ethernet device types
59 enum {
60 NET_IF_SHEEPNET,
61 NET_IF_ETHERTAP,
62 NET_IF_TUNTAP,
63 };
64
65 // Constants
66 static const char ETHERCONFIG_FILE_NAME[] = DATADIR "/tunconfig";
67
68 // Global variables
69 static int fd = -1; // fd of sheep_net device
70
71 static pthread_t ether_thread; // Packet reception thread
72 static bool thread_active = false; // Flag: Packet reception thread installed
73
74 static sem_t int_ack; // Interrupt acknowledge semaphore
75 static uint8 ether_addr[6]; // Our Ethernet address
76
77 static bool net_open = false; // Flag: initialization succeeded, network device open
78 static int net_if_type = -1; // Ethernet device type
79 static char *net_if_name; // TUN/TAP device name
80 static const char *net_if_script; // Network config script
81
82
83 // Prototypes
84 static void *receive_func(void *arg);
85
86
87 /*
88 * Execute network script up|down
89 */
90
91 static bool execute_network_script(const char *action)
92 {
93 if (net_if_script == NULL || net_if_name == NULL)
94 return false;
95
96 int pid = fork();
97 if (pid >= 0) {
98 if (pid == 0) {
99 char *args[4];
100 args[0] = (char *)net_if_script;
101 args[1] = net_if_name;
102 args[2] = (char *)action;
103 args[3] = NULL;
104 execv(net_if_script, args);
105 exit(1);
106 }
107 int status;
108 while (waitpid(pid, &status, 0) != pid);
109 return WIFEXITED(status) && WEXITSTATUS(status) == 0;
110 }
111
112 return false;
113 }
114
115
116 /*
117 * Initialize ethernet
118 */
119
120 void EtherInit(void)
121 {
122 int nonblock = 1;
123 char str[256];
124
125 // Do nothing if the user disabled the network
126 if (PrefsFindBool("nonet"))
127 return;
128
129 // Do nothing if no Ethernet device specified
130 const char *name = PrefsFindString("ether");
131 if (name == NULL)
132 return;
133
134 // Determine Ether device type
135 net_if_type = -1;
136 if (strncmp(name, "tap", 3) == 0)
137 net_if_type = NET_IF_ETHERTAP;
138 #if ENABLE_TUNTAP
139 else if (strcmp(name, "tun") == 0)
140 net_if_type = NET_IF_TUNTAP;
141 #endif
142 else
143 net_if_type = NET_IF_SHEEPNET;
144
145 // Open sheep_net or ethertap device
146 char dev_name[16];
147 switch (net_if_type) {
148 case NET_IF_ETHERTAP:
149 sprintf(dev_name, "/dev/%s", name);
150 break;
151 case NET_IF_TUNTAP:
152 strcpy(dev_name, "/dev/net/tun");
153 break;
154 case NET_IF_SHEEPNET:
155 strcpy(dev_name, "/dev/sheep_net");
156 break;
157 }
158 fd = open(dev_name, O_RDWR);
159 if (fd < 0) {
160 sprintf(str, GetString(STR_NO_SHEEP_NET_DRIVER_WARN), dev_name, strerror(errno));
161 WarningAlert(str);
162 goto open_error;
163 }
164
165 #if ENABLE_TUNTAP
166 // Open TUN/TAP interface
167 if (net_if_type == NET_IF_TUNTAP) {
168 struct ifreq ifr;
169 memset(&ifr, 0, sizeof(ifr));
170 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
171 strcpy(ifr.ifr_name, "tun%d");
172 if (ioctl(fd, TUNSETIFF, (void *) &ifr) != 0) {
173 sprintf(str, GetString(STR_SHEEP_NET_ATTACH_WARN), strerror(errno));
174 WarningAlert(str);
175 goto open_error;
176 }
177
178 // Get network config script file path
179 net_if_script = PrefsFindString("etherconfig");
180 if (net_if_script == NULL)
181 net_if_script = ETHERCONFIG_FILE_NAME;
182
183 // Start network script up
184 if (net_if_script == NULL) {
185 sprintf(str, GetString(STR_TUN_TAP_CONFIG_WARN), "script not found");
186 WarningAlert(str);
187 goto open_error;
188 }
189 net_if_name = strdup(ifr.ifr_name);
190 if (!execute_network_script("up")) {
191 sprintf(str, GetString(STR_TUN_TAP_CONFIG_WARN), "script execute error");
192 WarningAlert(str);
193 goto open_error;
194 }
195 D(bug("Connected to host network interface: %s\n", net_if_name));
196 }
197 #endif
198
199 // Attach to selected Ethernet card
200 if (net_if_type == NET_IF_SHEEPNET && ioctl(fd, SIOCSIFLINK, name) < 0) {
201 sprintf(str, GetString(STR_SHEEP_NET_ATTACH_WARN), strerror(errno));
202 WarningAlert(str);
203 goto open_error;
204 }
205
206 // Set nonblocking I/O
207 ioctl(fd, FIONBIO, &nonblock);
208
209 // Get Ethernet address
210 if (net_if_type == NET_IF_ETHERTAP) {
211 pid_t p = getpid(); // If configured for multicast, ethertap requires that the lower 32 bit of the Ethernet address are our PID
212 ether_addr[0] = 0xfe;
213 ether_addr[1] = 0xfd;
214 ether_addr[2] = p >> 24;
215 ether_addr[3] = p >> 16;
216 ether_addr[4] = p >> 8;
217 ether_addr[5] = p;
218 } else
219 ioctl(fd, SIOCGIFADDR, ether_addr);
220 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]));
221
222 // Start packet reception thread
223 if (sem_init(&int_ack, 0, 0) < 0) {
224 WarningAlert("WARNING: Cannot init semaphore");
225 goto open_error;
226 }
227 thread_active = (pthread_create(&ether_thread, NULL, receive_func, NULL) == 0);
228 if (!thread_active) {
229 WarningAlert("WARNING: Cannot start Ethernet thread");
230 goto open_error;
231 }
232
233 // Everything OK
234 net_open = true;
235 return;
236
237 open_error:
238 if (thread_active) {
239 pthread_cancel(ether_thread);
240 pthread_join(ether_thread, NULL);
241 sem_destroy(&int_ack);
242 thread_active = false;
243 }
244 if (fd > 0) {
245 close(fd);
246 fd = -1;
247 }
248 }
249
250
251 /*
252 * Exit ethernet
253 */
254
255 void EtherExit(void)
256 {
257 // Stop reception thread
258 if (thread_active) {
259 pthread_cancel(ether_thread);
260 pthread_join(ether_thread, NULL);
261 sem_destroy(&int_ack);
262 thread_active = false;
263 }
264
265 // Shut down TUN/TAP interface
266 if (net_if_type == NET_IF_TUNTAP)
267 execute_network_script("down");
268
269 // Free TUN/TAP device name
270 if (net_if_name)
271 free(net_if_name);
272
273 // Close sheep_net device
274 if (fd > 0)
275 close(fd);
276
277 #if STATISTICS
278 // Show statistics
279 printf("%ld messages put on write queue\n", num_wput);
280 printf("%ld error acks\n", num_error_acks);
281 printf("%ld packets transmitted (%ld raw, %ld normal)\n", num_tx_packets, num_tx_raw_packets, num_tx_normal_packets);
282 printf("%ld tx packets dropped because buffer full\n", num_tx_buffer_full);
283 printf("%ld packets received\n", num_rx_packets);
284 printf("%ld packets passed upstream (%ld Fast Path, %ld normal)\n", num_rx_fastpath + num_unitdata_ind, num_rx_fastpath, num_unitdata_ind);
285 printf("EtherIRQ called %ld times\n", num_ether_irq);
286 printf("%ld rx packets dropped due to low memory\n", num_rx_no_mem);
287 printf("%ld rx packets dropped because no stream found\n", num_rx_dropped);
288 printf("%ld rx packets dropped because stream not ready\n", num_rx_stream_not_ready);
289 printf("%ld rx packets dropped because no memory for unitdata_ind\n", num_rx_no_unitdata_mem);
290 #endif
291 }
292
293
294 /*
295 * Get ethernet hardware address
296 */
297
298 void AO_get_ethernet_address(uint8 *addr)
299 {
300 if (net_open) {
301 OTCopy48BitAddress(ether_addr, addr);
302 } else {
303 addr[0] = 0x12;
304 addr[1] = 0x34;
305 addr[2] = 0x56;
306 addr[3] = 0x78;
307 addr[4] = 0x9a;
308 addr[5] = 0xbc;
309 }
310 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]));
311 }
312
313
314 /*
315 * Enable multicast address
316 */
317
318 void AO_enable_multicast(uint8 *addr)
319 {
320 D(bug("AO_enable_multicast\n"));
321 if (net_open) {
322 if (net_if_type != NET_IF_TUNTAP && ioctl(fd, SIOCADDMULTI, addr) < 0) {
323 D(bug("WARNING: couldn't enable multicast address\n"));
324 }
325 }
326 }
327
328
329 /*
330 * Disable multicast address
331 */
332
333 void AO_disable_multicast(uint8 *addr)
334 {
335 D(bug("AO_disable_multicast\n"));
336 if (net_open) {
337 if (net_if_type != NET_IF_TUNTAP && ioctl(fd, SIOCDELMULTI, addr) < 0) {
338 D(bug("WARNING: couldn't disable multicast address\n"));
339 }
340 }
341 }
342
343
344 /*
345 * Transmit one packet
346 */
347
348 void AO_transmit_packet(mblk_t *mp)
349 {
350 D(bug("AO_transmit_packet\n"));
351 if (net_open) {
352
353 // Copy packet to buffer
354 uint8 packet[1516], *p = packet;
355 int len = 0;
356 if (net_if_type == NET_IF_ETHERTAP) {
357 *p++ = 0; // Ethertap discards the first 2 bytes
358 *p++ = 0;
359 len += 2;
360 }
361 while (mp) {
362 uint32 size = mp->b_wptr - mp->b_rptr;
363 memcpy(p, mp->b_rptr, size);
364 len += size;
365 p += size;
366 mp = mp->b_cont;
367 }
368
369 #if MONITOR
370 bug("Sending Ethernet packet:\n");
371 for (int i=0; i<len; i++) {
372 bug("%02x ", packet[i]);
373 }
374 bug("\n");
375 #endif
376
377 // Transmit packet
378 if (write(fd, packet, len) < 0) {
379 D(bug("WARNING: couldn't transmit packet\n"));
380 num_tx_buffer_full++;
381 } else
382 num_tx_packets++;
383 }
384 }
385
386
387 /*
388 * Packet reception thread
389 */
390
391 static void *receive_func(void *arg)
392 {
393 for (;;) {
394
395 // Wait for packets to arrive
396 struct pollfd pf = {fd, POLLIN, 0};
397 int res = poll(&pf, 1, -1);
398 if (res <= 0)
399 break;
400
401 if (ether_driver_opened) {
402 // Trigger Ethernet interrupt
403 D(bug(" packet received, triggering Ethernet interrupt\n"));
404 SetInterruptFlag(INTFLAG_ETHER);
405 TriggerInterrupt();
406
407 // Wait for interrupt acknowledge by EtherInterrupt()
408 sem_wait(&int_ack);
409 } else
410 usleep(20000);
411 }
412 return NULL;
413 }
414
415
416 /*
417 * Ethernet interrupt
418 */
419
420 void EtherIRQ(void)
421 {
422 D(bug("EtherIRQ\n"));
423 num_ether_irq++;
424 OTEnterInterrupt();
425
426 // Send received packets to OpenTransport
427 uint8 packet[1516];
428 for (;;) {
429
430 if (net_if_type != NET_IF_SHEEPNET) {
431
432 // Read packet from ethertap device
433 ssize_t size = read(fd, packet, net_if_type == NET_IF_ETHERTAP ? 1516 : 1514);
434 if (size < 14)
435 break;
436
437 #if MONITOR
438 bug("Receiving Ethernet packet:\n");
439 for (int i=0; i<size; i++) {
440 bug("%02x ", packet[i]);
441 }
442 bug("\n");
443 #endif
444
445 // Pointer to packet data (Ethernet header)
446 uint8 *p = packet;
447 if (net_if_type == NET_IF_ETHERTAP) {
448 p += 2; // Ethertap has two random bytes before the packet
449 size -= 2;
450 }
451
452 // Wrap packet in message block
453 num_rx_packets++;
454 mblk_t *mp;
455 if ((mp = allocb(size, 0)) != NULL) {
456 D(bug(" packet data at %p\n", (void *)mp->b_rptr));
457 memcpy(mp->b_rptr, p, size);
458 mp->b_wptr += size;
459 ether_packet_received(mp);
460 } else {
461 D(bug("WARNING: Cannot allocate mblk for received packet\n"));
462 num_rx_no_mem++;
463 }
464
465 } else {
466
467 // Get size of first packet
468 int size = 0;
469 if (ioctl(fd, FIONREAD, &size) < 0 || size == 0)
470 break;
471
472 // Discard packets which are too short
473 if (size < 14) {
474 uint8 dummy[14];
475 read(fd, dummy, size);
476 continue;
477 }
478
479 // Truncate packets which are too long
480 if (size > 1514)
481 size = 1514;
482
483 // Read packet and wrap it in message block
484 num_rx_packets++;
485 mblk_t *mp;
486 if ((mp = allocb(size, 0)) != NULL) {
487 D(bug(" packet data at %p\n", (void *)mp->b_rptr));
488 read(fd, mp->b_rptr, size);
489 #if MONITOR
490 bug("Receiving Ethernet packet:\n");
491 for (int i=0; i<size; i++) {
492 bug("%02x ", ((uint8 *)mp->b_rptr)[i]);
493 }
494 bug("\n");
495 #endif
496 mp->b_wptr += size;
497 ether_packet_received(mp);
498 } else {
499 D(bug("WARNING: Cannot allocate mblk for received packet\n"));
500 num_rx_no_mem++;
501 }
502 }
503 }
504 OTLeaveInterrupt();
505
506 // Acknowledge interrupt to reception thread
507 D(bug(" EtherIRQ done\n"));
508 sem_post(&int_ack);
509 }