~ubuntu-branches/ubuntu/lucid/vde2/lucid-proposed

« back to all changes in this revision

Viewing changes to bochs/eth_vde.cc

  • Committer: Bazaar Package Importer
  • Author(s): Filippo Giunchedi
  • Date: 2008-06-17 15:36:32 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20080617153632-5318x7iv0zwmu3zu
Tags: 2.2.1-1
* New upstream release
  - fix vlan commands on amd64 (Closes: #484295)
  - fix mac addresses switch between ports (Closes: #469098)
* Suggest: qemu and kvm as requested in #461514
* Expand and spell-check README.Debian, add manual method example
  (Closes: #466363)
* Do not assume MAKEDEV presence in postinst
* Remove /usr/bin/daemon usage from ifupdown scripts (and Recommends)
* Add manpage for vde_tunctl
* Upgrade to S-V 3.8.0 (add Homepage field) 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/////////////////////////////////////////////////////////////////////////
2
 
// $Id: eth_vde.cc 157 2007-05-15 01:17:25Z shammash $
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.
11
 
//
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.
16
 
//
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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
20
 
 
21
 
// eth_vde.cc  - Virtual Distributed Ethernet interface by Renzo Davoli <renzo@cs.unibo.it>
22
 
//
23
 
 
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
28
 
 
29
 
#include "bochs.h"
30
 
#if BX_NE2K_SUPPORT
31
 
 
32
 
#define LOG_THIS bx_devices.pluginNE2kDevice->
33
 
 
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>
52
 
 
53
 
#include <config.h>
54
 
 
55
 
#define SWITCH_MAGIC 0xfeedface
56
 
 
57
 
//#define VDE_VIRTUAL_HW_ADDR             0xDEADBEEF
58
 
#define BX_ETH_VDE_LOGGING 0
59
 
#define BX_PACKET_BUFSIZ 2048   // Enough for an ether frame
60
 
 
61
 
int vde_alloc(char *dev,int *fdp,struct sockaddr_un *pdataout);
62
 
 
63
 
//
64
 
//  Define the class. This is private to this module
65
 
//
66
 
class bx_vde_pktmover_c : public eth_pktmover_c {
67
 
public:
68
 
  bx_vde_pktmover_c(const char *netif, const char *macaddr,
69
 
                     eth_rx_handler_t rxh,
70
 
                     void *rxarg);
71
 
  void sendpkt(void *buf, unsigned io_len);
72
 
private:
73
 
  int fd;
74
 
  int rx_timer_index;
75
 
  static void rx_timer_handler(void *);
76
 
  void rx_timer ();
77
 
  FILE *txlog, *txlog_txt, *rxlog, *rxlog_txt;
78
 
  int fddata;
79
 
  struct sockaddr_un dataout;
80
 
};
81
 
 
82
 
 
83
 
//
84
 
//  Define the static class that registers the derived pktmover class,
85
 
// and allocates one on request.
86
 
//
87
 
class bx_vde_locator_c : public eth_locator_c {
88
 
public:
89
 
  bx_vde_locator_c(void) : eth_locator_c("vde") {}
90
 
protected:
91
 
  eth_pktmover_c *allocate(const char *netif, const char *macaddr,
92
 
                           eth_rx_handler_t rxh,
93
 
                           void *rxarg) {
94
 
    return (new bx_vde_pktmover_c(netif, macaddr, rxh, rxarg));
95
 
  }
96
 
} bx_vde_match;
97
 
 
98
 
 
99
 
//
100
 
// Define the methods for the bx_vde_pktmover derived class
101
 
//
102
 
 
103
 
// the constructor
104
 
bx_vde_pktmover_c::bx_vde_pktmover_c(const char *netif, 
105
 
                                       const char *macaddr,
106
 
                                       eth_rx_handler_t rxh,
107
 
                                       void *rxarg)
108
 
{
109
 
  int flags;
110
 
  //if (strncmp (netif, "vde", 3) != 0) {
111
 
   // BX_PANIC (("eth_vde: interface name (%s) must be vde", netif));
112
 
  //}
113
 
  char intname[IFNAMSIZ];
114
 
  if (netif == NULL || strcmp(netif,"") == 0) 
115
 
          strcpy(intname,"/tmp/vde.ctl");
116
 
  else
117
 
        strcpy(intname,netif);
118
 
  fd=vde_alloc(intname,&fddata,&dataout);
119
 
  if (fd < 0) {
120
 
    BX_PANIC (("open failed on %s: %s", netif, strerror (errno)));
121
 
    return;
122
 
  }
123
 
 
124
 
  /* set O_ASYNC flag so that we can poll with read() */
125
 
  if ((flags = fcntl( fd, F_GETFL)) < 0) {
126
 
    BX_PANIC (("getflags on vde device: %s", strerror (errno)));
127
 
  }
128
 
  flags |= O_NONBLOCK;
129
 
  if (fcntl( fd, F_SETFL, flags ) < 0) {
130
 
    BX_PANIC (("set vde device flags: %s", strerror (errno)));
131
 
  }
132
 
 
133
 
  BX_INFO (("eth_vde: opened %s device", netif));
134
 
 
135
 
  /* Execute the configuration script */
136
 
  char *scriptname=bx_options.ne2k.Oscript->getptr();
137
 
  if((scriptname != NULL)
138
 
   &&(strcmp(scriptname, "") != 0)
139
 
   &&(strcmp(scriptname, "none") != 0)) {
140
 
    if (execute_script(scriptname, intname) < 0)
141
 
      BX_ERROR (("execute script '%s' on %s failed", scriptname, intname));
142
 
    }
143
 
 
144
 
  // Start the rx poll 
145
 
  this->rx_timer_index = 
146
 
    bx_pc_system.register_timer(this, this->rx_timer_handler, 1000,
147
 
                                1, 1, "eth_vde"); // continuous, active
148
 
  this->rxh   = rxh;
149
 
  this->rxarg = rxarg;
150
 
#if BX_ETH_VDE_LOGGING
151
 
  // eventually Bryce wants txlog to dump in pcap format so that
152
 
  // tcpdump -r FILE can read it and interpret packets.
153
 
  txlog = fopen ("ne2k-tx.log", "wb");
154
 
  if (!txlog) BX_PANIC (("open ne2k-tx.log failed"));
155
 
  txlog_txt = fopen ("ne2k-txdump.txt", "wb");
156
 
  if (!txlog_txt) BX_PANIC (("open ne2k-txdump.txt failed"));
157
 
  fprintf (txlog_txt, "vde packetmover readable log file\n");
158
 
  fprintf (txlog_txt, "net IF = %s\n", netif);
159
 
  fprintf (txlog_txt, "MAC address = ");
160
 
  for (int i=0; i<6; i++) 
161
 
    fprintf (txlog_txt, "%02x%s", 0xff & macaddr[i], i<5?":" : "");
162
 
  fprintf (txlog_txt, "\n--\n");
163
 
  fflush (txlog_txt);
164
 
 
165
 
  rxlog = fopen ("ne2k-rx.log", "wb");
166
 
  if (!rxlog) BX_PANIC (("open ne2k-rx.log failed"));
167
 
  rxlog_txt = fopen ("ne2k-rxdump.txt", "wb");
168
 
  if (!rxlog_txt) BX_PANIC (("open ne2k-rxdump.txt failed"));
169
 
  fprintf (rxlog_txt, "vde packetmover readable log file\n");
170
 
  fprintf (rxlog_txt, "net IF = %s\n", netif);
171
 
  fprintf (rxlog_txt, "MAC address = ");
172
 
  for (int i=0; i<6; i++) 
173
 
    fprintf (rxlog_txt, "%02x%s", 0xff & macaddr[i], i<5?":" : "");
174
 
  fprintf (rxlog_txt, "\n--\n");
175
 
  fflush (rxlog_txt);
176
 
 
177
 
#endif
178
 
}
179
 
 
180
 
void
181
 
bx_vde_pktmover_c::sendpkt(void *buf, unsigned io_len)
182
 
{
183
 
  unsigned int size;
184
 
  //size = write (fd, buf, io_len);
185
 
  //size=send(fd,buf,io_len,0);
186
 
  size=sendto(fddata,buf,io_len,0,(struct sockaddr *) &dataout, sizeof(struct sockaddr_un));
187
 
  if (size != io_len) {
188
 
    BX_PANIC (("write on vde device: %s", strerror (errno)));
189
 
  } else {
190
 
    BX_INFO (("wrote %d bytes on vde", io_len));
191
 
  }
192
 
#if BX_ETH_VDE_LOGGING
193
 
  BX_DEBUG (("sendpkt length %u", io_len));
194
 
  // dump raw bytes to a file, eventually dump in pcap format so that
195
 
  // tcpdump -r FILE can interpret them for us.
196
 
  int n = fwrite (buf, io_len, 1, txlog);
197
 
  if (n != 1) BX_ERROR (("fwrite to txlog failed", io_len));
198
 
  // dump packet in hex into an ascii log file
199
 
  fprintf (txlog_txt, "NE2K transmitting a packet, length %u\n", io_len);
200
 
  Bit8u *charbuf = (Bit8u *)buf;
201
 
  for (n=0; n<io_len; n++) {
202
 
    if (((n % 16) == 0) && n>0)
203
 
      fprintf (txlog_txt, "\n");
204
 
    fprintf (txlog_txt, "%02x ", charbuf[n]);
205
 
  }
206
 
  fprintf (txlog_txt, "\n--\n");
207
 
  // flush log so that we see the packets as they arrive w/o buffering
208
 
  fflush (txlog);
209
 
  fflush (txlog_txt);
210
 
#endif
211
 
}
212
 
 
213
 
void bx_vde_pktmover_c::rx_timer_handler (void *this_ptr)
214
 
{
215
 
  bx_vde_pktmover_c *class_ptr = (bx_vde_pktmover_c *) this_ptr;
216
 
  class_ptr->rx_timer();
217
 
}
218
 
 
219
 
void bx_vde_pktmover_c::rx_timer ()
220
 
{
221
 
  int nbytes;
222
 
  Bit8u buf[BX_PACKET_BUFSIZ];
223
 
  Bit8u *rxbuf;
224
 
  struct sockaddr_un datain;
225
 
  socklen_t datainsize;
226
 
 
227
 
  datainsize=sizeof(datain);
228
 
  if (fd<0) return;
229
 
  //nbytes = read (fd, buf, sizeof(buf));
230
 
  nbytes=recvfrom(fddata,buf,sizeof(buf),MSG_DONTWAIT|MSG_WAITALL,(struct sockaddr *) &datain, &datainsize);
231
 
 
232
 
  rxbuf=buf;
233
 
 
234
 
  // hack: TUN/TAP device likes to create an ethernet header which has
235
 
  // the same source and destination address FE:FD:00:00:00:00.
236
 
  // Change the dest address to FE:FD:00:00:00:01.
237
 
  rxbuf[5] = 1;
238
 
 
239
 
  if (nbytes>0)
240
 
    BX_INFO (("vde read returned %d bytes", nbytes));
241
 
  if (nbytes<0) {
242
 
    if (errno != EAGAIN)
243
 
      BX_ERROR (("vde read error: %s", strerror(errno)));
244
 
    return;
245
 
  }
246
 
#if BX_ETH_VDE_LOGGING
247
 
  if (nbytes > 0) {
248
 
    BX_DEBUG (("receive packet length %u", nbytes));
249
 
    // dump raw bytes to a file, eventually dump in pcap format so that
250
 
    // tcpdump -r FILE can interpret them for us.
251
 
    int n = fwrite (rxbuf, nbytes, 1, rxlog);
252
 
    if (n != 1) BX_ERROR (("fwrite to rxlog failed", nbytes));
253
 
    // dump packet in hex into an ascii log file
254
 
    fprintf (rxlog_txt, "NE2K received a packet, length %u\n", nbytes);
255
 
    for (n=0; n<nbytes; n++) {
256
 
      if (((n % 16) == 0) && n>0)
257
 
        fprintf (rxlog_txt, "\n");
258
 
      fprintf (rxlog_txt, "%02x ", rxbuf[n]);
259
 
    }
260
 
    fprintf (rxlog_txt, "\n--\n");
261
 
    // flush log so that we see the packets as they arrive w/o buffering
262
 
    fflush (rxlog);
263
 
    fflush (rxlog_txt);
264
 
  }
265
 
#endif
266
 
  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]));
267
 
  if (nbytes < 60) {
268
 
    BX_INFO (("packet too short (%d), padding to 60", nbytes));
269
 
    nbytes = 60;
270
 
  }
271
 
  (*rxh)(rxarg, rxbuf, nbytes);
272
 
}
273
 
 
274
 
//enum request_type { REQ_NEW_CONTROL };
275
 
#define REQ_NEW_CONTROL 0
276
 
 
277
 
struct request_v3 {
278
 
        uint32_t magic;
279
 
        uint32_t version;
280
 
        //enum request_type type;
281
 
        int type;
282
 
        struct sockaddr_un sock;
283
 
};
284
 
 
285
 
static int send_fd(char *name, int fddata, struct sockaddr_un *datasock, int group)
286
 
{
287
 
        int pid = getpid();
288
 
        struct request_v3 req;
289
 
        int fdctl;
290
 
        struct sockaddr_un sock;
291
 
        if((fdctl = socket(AF_UNIX, SOCK_STREAM, 0)) < 0){
292
 
                perror("socket");
293
 
                return(-1);
294
 
        }
295
 
        sock.sun_family = AF_UNIX;
296
 
        snprintf(sock.sun_path, sizeof(sock.sun_path), "%s", name);
297
 
        if(connect(fdctl, (struct sockaddr *) &sock, sizeof(sock))){
298
 
                perror("connect");
299
 
                return(-1);
300
 
        }
301
 
        req.magic=SWITCH_MAGIC;
302
 
        req.version=3;
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);
307
 
        if(bind(fddata, (struct sockaddr *) &req.sock, sizeof(req.sock)) < 0){
308
 
                perror("bind");
309
 
                return(-1);
310
 
        }
311
 
        if (send(fdctl,&req,sizeof(req),0) < 0) {
312
 
                perror("send");
313
 
                return(-1);
314
 
        }
315
 
        if (recv(fdctl,datasock,sizeof(struct sockaddr_un),0)<0) {
316
 
                perror("recv");
317
 
                return(-1);
318
 
        }
319
 
        return fdctl;
320
 
}
321
 
 
322
 
  int vde_alloc(char *dev, int *fdp, struct sockaddr_un *pdataout)
323
 
  {
324
 
      //struct ifreq ifr;
325
 
      int fd, err;
326
 
      int fddata;
327
 
 
328
 
      if((fddata = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0){ 
329
 
              return -1;
330
 
      }
331
 
 
332
 
 
333
 
      if( (fd = send_fd(dev, fddata, pdataout, 0)) < 0 )
334
 
         return -1;
335
 
 
336
 
      //memset(&ifr, 0, sizeof(ifr));
337
 
      *fdp=fddata;
338
 
 
339
 
      return fd;
340
 
  }              
341
 
 
342
 
#endif /* if BX_NE2K_SUPPORT */