1 /////////////////////////////////////////////////////////////////////////
2 // $Id: eth_vde.cc,v 1.15 2008/02/15 22:05:42 sshwarts Exp $
3 /////////////////////////////////////////////////////////////////////////
5 // Copyright (C) 2003 Renzo Davoli
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Lesser General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // Lesser General Public License for more details.
17 // You should have received a copy of the GNU Lesser General Public
18 // License along with this library; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 /////////////////////////////////////////////////////////////////////////
23 // eth_vde.cc - Virtual Distributed Ethernet interface by Renzo Davoli <renzo@cs.unibo.it>
26 // Define BX_PLUGGABLE in files that can be compiled into plugins. For
27 // platforms that require a special tag on exported symbols, BX_PLUGGABLE
28 // is used to know when we are exporting symbols and when we are importing.
31 #define NO_DEVICE_INCLUDES
34 #if BX_NETWORKING && defined(HAVE_VDE)
38 #define LOG_THIS bx_devices.pluginNE2kDevice->
41 #include <sys/param.h>
42 #include <sys/ioctl.h>
45 #include <sys/resource.h>
46 #include <sys/socket.h>
47 #if defined(__linux__)
48 #include <asm/types.h>
49 #include <linux/netlink.h>
51 #elif BX_HAVE_NET_IF_H
63 #define SWITCH_MAGIC 0xfeedface
65 #define BX_ETH_VDE_LOGGING 0
67 int vde_alloc(char *dev
,int *fdp
,struct sockaddr_un
*pdataout
);
70 // Define the class. This is private to this module
72 class bx_vde_pktmover_c
: public eth_pktmover_c
{
74 bx_vde_pktmover_c(const char *netif
, const char *macaddr
,
76 void *rxarg
, char *script
);
77 void sendpkt(void *buf
, unsigned io_len
);
81 static void rx_timer_handler(void *);
83 FILE *txlog
, *txlog_txt
, *rxlog
, *rxlog_txt
;
85 struct sockaddr_un dataout
;
90 // Define the static class that registers the derived pktmover class,
91 // and allocates one on request.
93 class bx_vde_locator_c
: public eth_locator_c
{
95 bx_vde_locator_c(void) : eth_locator_c("vde") {}
97 eth_pktmover_c
*allocate(const char *netif
, const char *macaddr
,
99 void *rxarg
, char *script
) {
100 return (new bx_vde_pktmover_c(netif
, macaddr
, rxh
, rxarg
, script
));
106 // Define the methods for the bx_vde_pktmover derived class
110 bx_vde_pktmover_c::bx_vde_pktmover_c(const char *netif
,
112 eth_rx_handler_t rxh
,
117 //if (strncmp (netif, "vde", 3) != 0) {
118 // BX_PANIC (("eth_vde: interface name (%s) must be vde", netif));
120 char intname
[IFNAMSIZ
];
121 if (netif
== NULL
|| strcmp(netif
,"") == 0)
122 strcpy(intname
,"/tmp/vde.ctl");
124 strcpy(intname
,netif
);
125 fd
=vde_alloc(intname
,&fddata
,&dataout
);
127 BX_PANIC (("open failed on %s: %s", netif
, strerror (errno
)));
131 /* set O_ASYNC flag so that we can poll with read() */
132 if ((flags
= fcntl(fd
, F_GETFL
)) < 0) {
133 BX_PANIC (("getflags on vde device: %s", strerror (errno
)));
136 if (fcntl(fd
, F_SETFL
, flags
) < 0) {
137 BX_PANIC (("set vde device flags: %s", strerror (errno
)));
140 BX_INFO(("eth_vde: opened %s device", netif
));
142 /* Execute the configuration script */
143 if((script
!= NULL
) && (strcmp(script
, "") != 0) && (strcmp(script
, "none") != 0))
145 if (execute_script(script
, intname
) < 0)
146 BX_ERROR (("execute script '%s' on %s failed", script
, intname
));
150 this->rx_timer_index
=
151 bx_pc_system
.register_timer(this, this->rx_timer_handler
, 1000,
152 1, 1, "eth_vde"); // continuous, active
155 #if BX_ETH_VDE_LOGGING
156 // eventually Bryce wants txlog to dump in pcap format so that
157 // tcpdump -r FILE can read it and interpret packets.
158 txlog
= fopen ("ne2k-tx.log", "wb");
159 if (!txlog
) BX_PANIC (("open ne2k-tx.log failed"));
160 txlog_txt
= fopen ("ne2k-txdump.txt", "wb");
161 if (!txlog_txt
) BX_PANIC (("open ne2k-txdump.txt failed"));
162 fprintf (txlog_txt
, "vde packetmover readable log file\n");
163 fprintf (txlog_txt
, "net IF = %s\n", netif
);
164 fprintf (txlog_txt
, "MAC address = ");
165 for (int i
=0; i
<6; i
++)
166 fprintf (txlog_txt
, "%02x%s", 0xff & macaddr
[i
], i
<5?":" : "");
167 fprintf (txlog_txt
, "\n--\n");
170 rxlog
= fopen ("ne2k-rx.log", "wb");
171 if (!rxlog
) BX_PANIC (("open ne2k-rx.log failed"));
172 rxlog_txt
= fopen ("ne2k-rxdump.txt", "wb");
173 if (!rxlog_txt
) BX_PANIC (("open ne2k-rxdump.txt failed"));
174 fprintf (rxlog_txt
, "vde packetmover readable log file\n");
175 fprintf (rxlog_txt
, "net IF = %s\n", netif
);
176 fprintf (rxlog_txt
, "MAC address = ");
177 for (int i
=0; i
<6; i
++)
178 fprintf (rxlog_txt
, "%02x%s", 0xff & macaddr
[i
], i
<5?":" : "");
179 fprintf (rxlog_txt
, "\n--\n");
185 void bx_vde_pktmover_c::sendpkt(void *buf
, unsigned io_len
)
188 //size = write (fd, buf, io_len);
189 //size=send(fd,buf,io_len,0);
190 size
=sendto(fddata
,buf
,io_len
,0,(struct sockaddr
*) &dataout
, sizeof(struct sockaddr_un
));
191 if (size
!= io_len
) {
192 BX_PANIC (("write on vde device: %s", strerror (errno
)));
194 BX_INFO (("wrote %d bytes on vde", io_len
));
196 #if BX_ETH_VDE_LOGGING
197 BX_DEBUG (("sendpkt length %u", io_len
));
198 // dump raw bytes to a file, eventually dump in pcap format so that
199 // tcpdump -r FILE can interpret them for us.
200 int n
= fwrite (buf
, io_len
, 1, txlog
);
201 if (n
!= 1) BX_ERROR (("fwrite to txlog failed", io_len
));
202 // dump packet in hex into an ascii log file
203 fprintf (txlog_txt
, "NE2K transmitting a packet, length %u\n", io_len
);
204 Bit8u
*charbuf
= (Bit8u
*)buf
;
205 for (n
=0; n
<io_len
; n
++) {
206 if (((n
% 16) == 0) && n
>0)
207 fprintf (txlog_txt
, "\n");
208 fprintf (txlog_txt
, "%02x ", charbuf
[n
]);
210 fprintf (txlog_txt
, "\n--\n");
211 // flush log so that we see the packets as they arrive w/o buffering
217 void bx_vde_pktmover_c::rx_timer_handler (void *this_ptr
)
219 bx_vde_pktmover_c
*class_ptr
= (bx_vde_pktmover_c
*) this_ptr
;
220 class_ptr
->rx_timer();
223 void bx_vde_pktmover_c::rx_timer ()
226 Bit8u buf
[BX_PACKET_BUFSIZE
];
228 struct sockaddr_un datain
;
229 socklen_t datainsize
;
232 //nbytes = read (fd, buf, sizeof(buf));
233 nbytes
=recvfrom(fddata
,buf
,sizeof(buf
),MSG_DONTWAIT
|MSG_WAITALL
,(struct sockaddr
*) &datain
, &datainsize
);
238 BX_INFO (("vde read returned %d bytes", nbytes
));
241 BX_ERROR (("vde read error: %s", strerror(errno
)));
244 #if BX_ETH_VDE_LOGGING
246 BX_DEBUG (("receive packet length %u", nbytes
));
247 // dump raw bytes to a file, eventually dump in pcap format so that
248 // tcpdump -r FILE can interpret them for us.
249 int n
= fwrite (rxbuf
, nbytes
, 1, rxlog
);
250 if (n
!= 1) BX_ERROR (("fwrite to rxlog failed", nbytes
));
251 // dump packet in hex into an ascii log file
252 fprintf (rxlog_txt
, "NE2K received a packet, length %u\n", nbytes
);
253 for (n
=0; n
<nbytes
; n
++) {
254 if (((n
% 16) == 0) && n
>0)
255 fprintf (rxlog_txt
, "\n");
256 fprintf (rxlog_txt
, "%02x ", rxbuf
[n
]);
258 fprintf (rxlog_txt
, "\n--\n");
259 // flush log so that we see the packets as they arrive w/o buffering
264 BX_DEBUG(("eth_vde: got packet: %d bytes, dst=%x:%x:%x:%x:%x:%x, src=%x:%x:%x:%x:%x:%x\n", nbytes
, rxbuf
[0], rxbuf
[1], rxbuf
[2], rxbuf
[3], rxbuf
[4], rxbuf
[5], rxbuf
[6], rxbuf
[7], rxbuf
[8], rxbuf
[9], rxbuf
[10], rxbuf
[11]));
266 BX_INFO (("packet too short (%d), padding to 60", nbytes
));
269 (*rxh
)(rxarg
, rxbuf
, nbytes
);
272 //enum request_type { REQ_NEW_CONTROL };
273 #define REQ_NEW_CONTROL 0
278 //enum request_type type;
280 struct sockaddr_un sock
;
283 static int send_fd(char *name
, int fddata
, struct sockaddr_un
*datasock
, int group
)
286 struct request_v3 req
;
288 struct sockaddr_un sock
;
290 if((fdctl
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0) {
294 sock
.sun_family
= AF_UNIX
;
295 snprintf(sock
.sun_path
, sizeof(sock
.sun_path
), "%s", name
);
296 if(connect(fdctl
, (struct sockaddr
*) &sock
, sizeof(sock
))) {
301 req
.magic
=SWITCH_MAGIC
;
303 req
.type
=((int)REQ_NEW_CONTROL
)+((group
> 0)?((geteuid()<<8) + group
) << 8:0);
304 req
.sock
.sun_family
=AF_UNIX
;
305 memset(req
.sock
.sun_path
, 0, sizeof(req
.sock
.sun_path
));
306 sprintf(&req
.sock
.sun_path
[1], "%5d", pid
);
308 if(bind(fddata
, (struct sockaddr
*) &req
.sock
, sizeof(req
.sock
)) < 0) {
312 if (send(fdctl
,&req
,sizeof(req
),0) < 0) {
316 if (recv(fdctl
,datasock
,sizeof(struct sockaddr_un
),0) < 0) {
323 int vde_alloc(char *dev
, int *fdp
, struct sockaddr_un
*pdataout
)
329 if ((fddata
= socket(AF_UNIX
, SOCK_DGRAM
, 0)) < 0)
332 if ((fd
= send_fd(dev
, fddata
, pdataout
, 0)) < 0)
335 //memset(&ifr, 0, sizeof(ifr));
341 #endif /* if BX_NETWORKING && defined HAVE_VDE */