ethertap.cc revision 2665
1/* 2 * Copyright (c) 2003-2005 The Regents of The University of Michigan 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; 9 * redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution; 12 * neither the name of the copyright holders nor the names of its 13 * contributors may be used to endorse or promote products derived from 14 * this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * Authors: Nathan Binkert 29 */ 30 31/* @file 32 * Interface to connect a simulated ethernet device to the real world 33 */ 34 35#if defined(__OpenBSD__) || defined(__APPLE__) 36#include <sys/param.h> 37#endif 38#include <netinet/in.h> 39 40#include <unistd.h> 41 42#include <deque> 43#include <string> 44 45#include "base/misc.hh" 46#include "base/pollevent.hh" 47#include "base/socket.hh" 48#include "base/trace.hh" 49#include "dev/etherdump.hh" 50#include "dev/etherint.hh" 51#include "dev/etherpkt.hh" 52#include "dev/ethertap.hh" 53#include "sim/builder.hh" 54 55using namespace std; 56 57/** 58 */ 59class TapListener 60{ 61 protected: 62 /** 63 */ 64 class Event : public PollEvent 65 { 66 protected: 67 TapListener *listener; 68 69 public: 70 Event(TapListener *l, int fd, int e) 71 : PollEvent(fd, e), listener(l) {} 72 73 virtual void process(int revent) { listener->accept(); } 74 }; 75 76 friend class Event; 77 Event *event; 78 79 protected: 80 ListenSocket listener; 81 EtherTap *tap; 82 int port; 83 84 public: 85 TapListener(EtherTap *t, int p) 86 : event(NULL), tap(t), port(p) {} 87 ~TapListener() { if (event) delete event; } 88 89 void accept(); 90 void listen(); 91}; 92 93void 94TapListener::listen() 95{ 96 while (!listener.listen(port, true)) { 97 DPRINTF(Ethernet, "TapListener(listen): Can't bind port %d\n", port); 98 port++; 99 } 100 101 ccprintf(cerr, "Listening for tap connection on port %d\n", port); 102 event = new Event(this, listener.getfd(), POLLIN|POLLERR); 103 pollQueue.schedule(event); 104} 105 106void 107TapListener::accept() 108{ 109 if (!listener.islistening()) 110 panic("TapListener(accept): cannot accept if we're not listening!"); 111 112 int sfd = listener.accept(true); 113 if (sfd != -1) 114 tap->attach(sfd); 115} 116 117/** 118 */ 119class TapEvent : public PollEvent 120{ 121 protected: 122 EtherTap *tap; 123 124 public: 125 TapEvent(EtherTap *_tap, int fd, int e) 126 : PollEvent(fd, e), tap(_tap) {} 127 virtual void process(int revent) { tap->process(revent); } 128}; 129 130EtherTap::EtherTap(const string &name, EtherDump *d, int port, int bufsz) 131 : EtherInt(name), event(NULL), socket(-1), buflen(bufsz), dump(d), 132 txEvent(this) 133{ 134 buffer = new char[buflen]; 135 listener = new TapListener(this, port); 136 listener->listen(); 137} 138 139EtherTap::~EtherTap() 140{ 141 if (event) 142 delete event; 143 if (buffer) 144 delete [] buffer; 145 146 delete listener; 147} 148 149void 150EtherTap::attach(int fd) 151{ 152 if (socket != -1) 153 close(fd); 154 155 buffer_offset = 0; 156 data_len = 0; 157 socket = fd; 158 DPRINTF(Ethernet, "EtherTap attached\n"); 159 event = new TapEvent(this, socket, POLLIN|POLLERR); 160 pollQueue.schedule(event); 161} 162 163void 164EtherTap::detach() 165{ 166 DPRINTF(Ethernet, "EtherTap detached\n"); 167 delete event; 168 event = 0; 169 close(socket); 170 socket = -1; 171} 172 173bool 174EtherTap::recvPacket(EthPacketPtr packet) 175{ 176 if (dump) 177 dump->dump(packet); 178 179 DPRINTF(Ethernet, "EtherTap output len=%d\n", packet->length); 180 DDUMP(EthernetData, packet->data, packet->length); 181 u_int32_t len = htonl(packet->length); 182 write(socket, &len, sizeof(len)); 183 write(socket, packet->data, packet->length); 184 185 recvDone(); 186 187 return true; 188} 189 190void 191EtherTap::sendDone() 192{} 193 194void 195EtherTap::process(int revent) 196{ 197 if (revent & POLLERR) { 198 detach(); 199 return; 200 } 201 202 char *data = buffer + sizeof(u_int32_t); 203 if (!(revent & POLLIN)) 204 return; 205 206 if (buffer_offset < data_len + sizeof(u_int32_t)) { 207 int len = read(socket, buffer + buffer_offset, buflen - buffer_offset); 208 if (len == 0) { 209 detach(); 210 return; 211 } 212 213 buffer_offset += len; 214 215 if (data_len == 0) 216 data_len = ntohl(*(u_int32_t *)buffer); 217 218 DPRINTF(Ethernet, "Received data from peer: len=%d buffer_offset=%d " 219 "data_len=%d\n", len, buffer_offset, data_len); 220 } 221 222 while (data_len != 0 && buffer_offset >= data_len + sizeof(u_int32_t)) { 223 EthPacketPtr packet; 224 packet = new EthPacketData(data_len); 225 packet->length = data_len; 226 memcpy(packet->data, data, data_len); 227 228 buffer_offset -= data_len + sizeof(u_int32_t); 229 assert(buffer_offset >= 0); 230 if (buffer_offset > 0) { 231 memmove(buffer, data + data_len, buffer_offset); 232 data_len = ntohl(*(u_int32_t *)buffer); 233 } else 234 data_len = 0; 235 236 DPRINTF(Ethernet, "EtherTap input len=%d\n", packet->length); 237 DDUMP(EthernetData, packet->data, packet->length); 238 if (!sendPacket(packet)) { 239 DPRINTF(Ethernet, "bus busy...buffer for retransmission\n"); 240 packetBuffer.push(packet); 241 if (!txEvent.scheduled()) 242 txEvent.schedule(curTick + retryTime); 243 } else if (dump) { 244 dump->dump(packet); 245 } 246 } 247} 248 249void 250EtherTap::retransmit() 251{ 252 if (packetBuffer.empty()) 253 return; 254 255 EthPacketPtr packet = packetBuffer.front(); 256 if (sendPacket(packet)) { 257 if (dump) 258 dump->dump(packet); 259 DPRINTF(Ethernet, "EtherTap retransmit\n"); 260 packetBuffer.front() = NULL; 261 packetBuffer.pop(); 262 } 263 264 if (!packetBuffer.empty() && !txEvent.scheduled()) 265 txEvent.schedule(curTick + retryTime); 266} 267 268//===================================================================== 269 270void 271EtherTap::serialize(ostream &os) 272{ 273 SERIALIZE_SCALAR(socket); 274 SERIALIZE_SCALAR(buflen); 275 uint8_t *buffer = (uint8_t *)this->buffer; 276 SERIALIZE_ARRAY(buffer, buflen); 277 SERIALIZE_SCALAR(buffer_offset); 278 SERIALIZE_SCALAR(data_len); 279 280 bool tapevent_present = false; 281 if (event) { 282 tapevent_present = true; 283 SERIALIZE_SCALAR(tapevent_present); 284 event->serialize(os); 285 } 286 else { 287 SERIALIZE_SCALAR(tapevent_present); 288 } 289} 290 291void 292EtherTap::unserialize(Checkpoint *cp, const std::string §ion) 293{ 294 UNSERIALIZE_SCALAR(socket); 295 UNSERIALIZE_SCALAR(buflen); 296 uint8_t *buffer = (uint8_t *)this->buffer; 297 UNSERIALIZE_ARRAY(buffer, buflen); 298 UNSERIALIZE_SCALAR(buffer_offset); 299 UNSERIALIZE_SCALAR(data_len); 300 301 bool tapevent_present; 302 UNSERIALIZE_SCALAR(tapevent_present); 303 if (tapevent_present) { 304 event = new TapEvent(this, socket, POLLIN|POLLERR); 305 306 event->unserialize(cp,section); 307 308 if (event->queued()) { 309 pollQueue.schedule(event); 310 } 311 } 312} 313 314//===================================================================== 315 316BEGIN_DECLARE_SIM_OBJECT_PARAMS(EtherTap) 317 318 SimObjectParam<EtherInt *> peer; 319 SimObjectParam<EtherDump *> dump; 320 Param<unsigned> port; 321 Param<unsigned> bufsz; 322 323END_DECLARE_SIM_OBJECT_PARAMS(EtherTap) 324 325BEGIN_INIT_SIM_OBJECT_PARAMS(EtherTap) 326 327 INIT_PARAM_DFLT(peer, "peer interface", NULL), 328 INIT_PARAM_DFLT(dump, "object to dump network packets to", NULL), 329 INIT_PARAM_DFLT(port, "tap port", 3500), 330 INIT_PARAM_DFLT(bufsz, "tap buffer size", 10000) 331 332END_INIT_SIM_OBJECT_PARAMS(EtherTap) 333 334 335CREATE_SIM_OBJECT(EtherTap) 336{ 337 EtherTap *tap = new EtherTap(getInstanceName(), dump, port, bufsz); 338 339 if (peer) { 340 tap->setPeer(peer); 341 peer->setPeer(tap); 342 } 343 344 return tap; 345} 346 347REGISTER_SIM_OBJECT("EtherTap", EtherTap) 348