Bugfix: on linux 2.6.x vdeqemu/vde_plug/bochs interface failed (socketsize uninitiali...
[vde.git] / vde / bochs / eth_vde.cc
bloba4237305f7aa72e7f0700f90e16d01beca9d049b
1 /////////////////////////////////////////////////////////////////////////
2 // $Id$
3 /////////////////////////////////////////////////////////////////////////
4 //
5 // Copyright (C) 2003 Renzo Davoli
6 //
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 // eth_vde.cc - Virtual Distributed Ethernet interface by Renzo Davoli <renzo@cs.unibo.it>
24 // Define BX_PLUGGABLE in files that can be compiled into plugins. For
25 // platforms that require a special tag on exported symbols, BX_PLUGGABLE
26 // is used to know when we are exporting symbols and when we are importing.
27 #define BX_PLUGGABLE
29 #include "bochs.h"
30 #if BX_NE2K_SUPPORT
32 #define LOG_THIS bx_devices.pluginNE2kDevice->
34 #include <signal.h>
35 #include <sys/param.h>
36 #include <sys/ioctl.h>
37 #include <sys/poll.h>
38 #include <sys/time.h>
39 #include <sys/resource.h>
40 #include <asm/types.h>
41 #include <sys/socket.h>
42 #include <sys/uio.h>
43 #include <sys/wait.h>
44 #include <sys/un.h>
45 #include <linux/netlink.h>
46 #include <linux/if.h>
47 #include <assert.h>
48 #include <fcntl.h>
49 #include <errno.h>
50 #include <unistd.h>
51 #include <stdint.h>
53 #define SWITCH_MAGIC 0xfeedface
55 //#define VDE_VIRTUAL_HW_ADDR 0xDEADBEEF
56 #define BX_ETH_VDE_LOGGING 0
57 #define BX_PACKET_BUFSIZ 2048 // Enough for an ether frame
59 int vde_alloc(char *dev,int *fdp,struct sockaddr_un *pdataout);
62 // Define the class. This is private to this module
64 class bx_vde_pktmover_c : public eth_pktmover_c {
65 public:
66 bx_vde_pktmover_c(const char *netif, const char *macaddr,
67 eth_rx_handler_t rxh,
68 void *rxarg);
69 void sendpkt(void *buf, unsigned io_len);
70 private:
71 int fd;
72 int rx_timer_index;
73 static void rx_timer_handler(void *);
74 void rx_timer ();
75 FILE *txlog, *txlog_txt, *rxlog, *rxlog_txt;
76 int fddata;
77 struct sockaddr_un dataout;
82 // Define the static class that registers the derived pktmover class,
83 // and allocates one on request.
85 class bx_vde_locator_c : public eth_locator_c {
86 public:
87 bx_vde_locator_c(void) : eth_locator_c("vde") {}
88 protected:
89 eth_pktmover_c *allocate(const char *netif, const char *macaddr,
90 eth_rx_handler_t rxh,
91 void *rxarg) {
92 return (new bx_vde_pktmover_c(netif, macaddr, rxh, rxarg));
94 } bx_vde_match;
98 // Define the methods for the bx_vde_pktmover derived class
101 // the constructor
102 bx_vde_pktmover_c::bx_vde_pktmover_c(const char *netif,
103 const char *macaddr,
104 eth_rx_handler_t rxh,
105 void *rxarg)
107 int flags;
108 //if (strncmp (netif, "vde", 3) != 0) {
109 // BX_PANIC (("eth_vde: interface name (%s) must be vde", netif));
111 char intname[IFNAMSIZ];
112 if (netif == NULL || strcmp(netif,"") == 0)
113 strcpy(intname,"/tmp/vde.ctl");
114 else
115 strcpy(intname,netif);
116 fd=vde_alloc(intname,&fddata,&dataout);
117 if (fd < 0) {
118 BX_PANIC (("open failed on %s: %s", netif, strerror (errno)));
119 return;
122 /* set O_ASYNC flag so that we can poll with read() */
123 if ((flags = fcntl( fd, F_GETFL)) < 0) {
124 BX_PANIC (("getflags on vde device: %s", strerror (errno)));
126 flags |= O_NONBLOCK;
127 if (fcntl( fd, F_SETFL, flags ) < 0) {
128 BX_PANIC (("set vde device flags: %s", strerror (errno)));
131 BX_INFO (("eth_vde: opened %s device", netif));
133 /* Execute the configuration script */
134 char *scriptname=bx_options.ne2k.Oscript->getptr();
135 if((scriptname != NULL)
136 &&(strcmp(scriptname, "") != 0)
137 &&(strcmp(scriptname, "none") != 0)) {
138 if (execute_script(scriptname, intname) < 0)
139 BX_ERROR (("execute script '%s' on %s failed", scriptname, intname));
142 // Start the rx poll
143 this->rx_timer_index =
144 bx_pc_system.register_timer(this, this->rx_timer_handler, 1000,
145 1, 1, "eth_vde"); // continuous, active
146 this->rxh = rxh;
147 this->rxarg = rxarg;
148 #if BX_ETH_VDE_LOGGING
149 // eventually Bryce wants txlog to dump in pcap format so that
150 // tcpdump -r FILE can read it and interpret packets.
151 txlog = fopen ("ne2k-tx.log", "wb");
152 if (!txlog) BX_PANIC (("open ne2k-tx.log failed"));
153 txlog_txt = fopen ("ne2k-txdump.txt", "wb");
154 if (!txlog_txt) BX_PANIC (("open ne2k-txdump.txt failed"));
155 fprintf (txlog_txt, "vde packetmover readable log file\n");
156 fprintf (txlog_txt, "net IF = %s\n", netif);
157 fprintf (txlog_txt, "MAC address = ");
158 for (int i=0; i<6; i++)
159 fprintf (txlog_txt, "%02x%s", 0xff & macaddr[i], i<5?":" : "");
160 fprintf (txlog_txt, "\n--\n");
161 fflush (txlog_txt);
163 rxlog = fopen ("ne2k-rx.log", "wb");
164 if (!rxlog) BX_PANIC (("open ne2k-rx.log failed"));
165 rxlog_txt = fopen ("ne2k-rxdump.txt", "wb");
166 if (!rxlog_txt) BX_PANIC (("open ne2k-rxdump.txt failed"));
167 fprintf (rxlog_txt, "vde packetmover readable log file\n");
168 fprintf (rxlog_txt, "net IF = %s\n", netif);
169 fprintf (rxlog_txt, "MAC address = ");
170 for (int i=0; i<6; i++)
171 fprintf (rxlog_txt, "%02x%s", 0xff & macaddr[i], i<5?":" : "");
172 fprintf (rxlog_txt, "\n--\n");
173 fflush (rxlog_txt);
175 #endif
178 void
179 bx_vde_pktmover_c::sendpkt(void *buf, unsigned io_len)
181 unsigned int size;
182 //size = write (fd, buf, io_len);
183 //size=send(fd,buf,io_len,0);
184 size=sendto(fddata,buf,io_len,0,(struct sockaddr *) &dataout, sizeof(struct sockaddr_un));
185 if (size != io_len) {
186 BX_PANIC (("write on vde device: %s", strerror (errno)));
187 } else {
188 BX_INFO (("wrote %d bytes on vde", io_len));
190 #if BX_ETH_VDE_LOGGING
191 BX_DEBUG (("sendpkt length %u", io_len));
192 // dump raw bytes to a file, eventually dump in pcap format so that
193 // tcpdump -r FILE can interpret them for us.
194 int n = fwrite (buf, io_len, 1, txlog);
195 if (n != 1) BX_ERROR (("fwrite to txlog failed", io_len));
196 // dump packet in hex into an ascii log file
197 fprintf (txlog_txt, "NE2K transmitting a packet, length %u\n", io_len);
198 Bit8u *charbuf = (Bit8u *)buf;
199 for (n=0; n<io_len; n++) {
200 if (((n % 16) == 0) && n>0)
201 fprintf (txlog_txt, "\n");
202 fprintf (txlog_txt, "%02x ", charbuf[n]);
204 fprintf (txlog_txt, "\n--\n");
205 // flush log so that we see the packets as they arrive w/o buffering
206 fflush (txlog);
207 fflush (txlog_txt);
208 #endif
211 void bx_vde_pktmover_c::rx_timer_handler (void *this_ptr)
213 bx_vde_pktmover_c *class_ptr = (bx_vde_pktmover_c *) this_ptr;
214 class_ptr->rx_timer();
217 void bx_vde_pktmover_c::rx_timer ()
219 int nbytes;
220 Bit8u buf[BX_PACKET_BUFSIZ];
221 Bit8u *rxbuf;
222 struct sockaddr_un datain;
223 socklen_t datainsize;
225 datainsize=sizeof(datain);
226 if (fd<0) return;
227 //nbytes = read (fd, buf, sizeof(buf));
228 nbytes=recvfrom(fddata,buf,sizeof(buf),MSG_DONTWAIT|MSG_WAITALL,(struct sockaddr *) &datain, &datainsize);
230 rxbuf=buf;
232 // hack: TUN/TAP device likes to create an ethernet header which has
233 // the same source and destination address FE:FD:00:00:00:00.
234 // Change the dest address to FE:FD:00:00:00:01.
235 rxbuf[5] = 1;
237 if (nbytes>0)
238 BX_INFO (("vde read returned %d bytes", nbytes));
239 if (nbytes<0) {
240 if (errno != EAGAIN)
241 BX_ERROR (("vde read error: %s", strerror(errno)));
242 return;
244 #if BX_ETH_VDE_LOGGING
245 if (nbytes > 0) {
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
260 fflush (rxlog);
261 fflush (rxlog_txt);
263 #endif
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]));
265 if (nbytes < 60) {
266 BX_INFO (("packet too short (%d), padding to 60", nbytes));
267 nbytes = 60;
269 (*rxh)(rxarg, rxbuf, nbytes);
272 //enum request_type { REQ_NEW_CONTROL };
273 #define REQ_NEW_CONTROL 0
275 struct request_v3 {
276 uint32_t magic;
277 uint32_t version;
278 //enum request_type type;
279 int type;
280 struct sockaddr_un sock;
283 static int send_fd(char *name, int fddata, struct sockaddr_un *datasock, int group)
285 int pid = getpid();
286 struct request_v3 req;
287 int fdctl;
288 struct sockaddr_un sock;
289 if((fdctl = socket(AF_UNIX, SOCK_STREAM, 0)) < 0){
290 perror("socket");
291 return(-1);
293 sock.sun_family = AF_UNIX;
294 snprintf(sock.sun_path, sizeof(sock.sun_path), "%s", name);
295 if(connect(fdctl, (struct sockaddr *) &sock, sizeof(sock))){
296 perror("connect");
297 return(-1);
299 req.magic=SWITCH_MAGIC;
300 req.version=3;
301 req.type=((int)REQ_NEW_CONTROL)+((group > 0)?((geteuid()<<8) + group) << 8:0);
302 req.sock.sun_family=AF_UNIX;
303 memset(req.sock.sun_path, 0, sizeof(req.sock.sun_path));
304 sprintf(&req.sock.sun_path[1], "%5d", pid);
305 if(bind(fddata, (struct sockaddr *) &req.sock, sizeof(req.sock)) < 0){
306 perror("bind");
307 return(-1);
309 if (send(fdctl,&req,sizeof(req),0) < 0) {
310 perror("send");
311 return(-1);
313 if (recv(fdctl,datasock,sizeof(struct sockaddr_un),0)<0) {
314 perror("recv");
315 return(-1);
317 return fdctl;
320 int vde_alloc(char *dev, int *fdp, struct sockaddr_un *pdataout)
322 //struct ifreq ifr;
323 int fd, err;
324 int fddata;
326 if((fddata = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0){
327 return -1;
331 if( (fd = send_fd(dev, fddata, pdataout, 0)) < 0 )
332 return -1;
334 //memset(&ifr, 0, sizeof(ifr));
335 *fdp=fddata;
337 return fd;
340 #endif /* if BX_NE2K_SUPPORT */