ethertap.cc revision 11701:5e7599457b97
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#include "dev/net/ethertap.hh" 36 37#if defined(__OpenBSD__) || defined(__APPLE__) 38#include <sys/param.h> 39 40#endif 41#include <netinet/in.h> 42#include <unistd.h> 43 44#include <deque> 45#include <string> 46 47#include "base/misc.hh" 48#include "base/pollevent.hh" 49#include "base/socket.hh" 50#include "base/trace.hh" 51#include "debug/Ethernet.hh" 52#include "debug/EthernetData.hh" 53#include "dev/net/etherdump.hh" 54#include "dev/net/etherint.hh" 55#include "dev/net/etherpkt.hh" 56 57using namespace std; 58 59/** 60 */ 61class TapListener 62{ 63 protected: 64 /** 65 */ 66 class Event : public PollEvent 67 { 68 protected: 69 TapListener *listener; 70 71 public: 72 Event(TapListener *l, int fd, int e) 73 : PollEvent(fd, e), listener(l) {} 74 75 virtual void process(int revent) { listener->accept(); } 76 }; 77 78 friend class Event; 79 Event *event; 80 81 protected: 82 ListenSocket listener; 83 EtherTap *tap; 84 int port; 85 86 public: 87 TapListener(EtherTap *t, int p) 88 : event(NULL), tap(t), port(p) {} 89 ~TapListener() { if (event) delete event; } 90 91 void accept(); 92 void listen(); 93}; 94 95void 96TapListener::listen() 97{ 98 while (!listener.listen(port, true)) { 99 DPRINTF(Ethernet, "TapListener(listen): Can't bind port %d\n", port); 100 port++; 101 } 102 103 ccprintf(cerr, "Listening for tap connection on port %d\n", port); 104 event = new Event(this, listener.getfd(), POLLIN|POLLERR); 105 pollQueue.schedule(event); 106} 107 108void 109TapListener::accept() 110{ 111 // As a consequence of being called from the PollQueue, we might 112 // have been called from a different thread. Migrate to "our" 113 // thread. 114 EventQueue::ScopedMigration migrate(tap->eventQueue()); 115 116 if (!listener.islistening()) 117 panic("TapListener(accept): cannot accept if we're not listening!"); 118 119 int sfd = listener.accept(true); 120 if (sfd != -1) 121 tap->attach(sfd); 122} 123 124/** 125 */ 126class TapEvent : public PollEvent 127{ 128 protected: 129 EtherTap *tap; 130 131 public: 132 TapEvent(EtherTap *_tap, int fd, int e) 133 : PollEvent(fd, e), tap(_tap) {} 134 virtual void process(int revent) { tap->process(revent); } 135}; 136 137EtherTap::EtherTap(const Params *p) 138 : EtherObject(p), event(NULL), socket(-1), buflen(p->bufsz), dump(p->dump), 139 interface(NULL), txEvent(this) 140{ 141 if (ListenSocket::allDisabled()) 142 fatal("All listeners are disabled! EtherTap can't work!"); 143 144 buffer = new char[buflen]; 145 listener = new TapListener(this, p->port); 146 listener->listen(); 147 interface = new EtherTapInt(name() + ".interface", this); 148} 149 150EtherTap::~EtherTap() 151{ 152 if (event) 153 delete event; 154 if (buffer) 155 delete [] buffer; 156 157 delete interface; 158 delete listener; 159} 160 161void 162EtherTap::attach(int fd) 163{ 164 if (socket != -1) 165 close(fd); 166 167 buffer_offset = 0; 168 data_len = 0; 169 socket = fd; 170 DPRINTF(Ethernet, "EtherTap attached\n"); 171 event = new TapEvent(this, socket, POLLIN|POLLERR); 172 pollQueue.schedule(event); 173} 174 175void 176EtherTap::detach() 177{ 178 DPRINTF(Ethernet, "EtherTap detached\n"); 179 delete event; 180 event = 0; 181 close(socket); 182 socket = -1; 183} 184 185bool 186EtherTap::recvPacket(EthPacketPtr packet) 187{ 188 if (dump) 189 dump->dump(packet); 190 191 DPRINTF(Ethernet, "EtherTap output len=%d\n", packet->length); 192 DDUMP(EthernetData, packet->data, packet->length); 193 uint32_t len = htonl(packet->length); 194 ssize_t ret = write(socket, &len, sizeof(len)); 195 if (ret != sizeof(len)) 196 return false; 197 ret = write(socket, packet->data, packet->length); 198 if (ret != packet->length) 199 return false; 200 201 interface->recvDone(); 202 203 return true; 204} 205 206void 207EtherTap::sendDone() 208{} 209 210void 211EtherTap::process(int revent) 212{ 213 if (revent & POLLERR) { 214 detach(); 215 return; 216 } 217 218 char *data = buffer + sizeof(uint32_t); 219 if (!(revent & POLLIN)) 220 return; 221 222 if (buffer_offset < data_len + sizeof(uint32_t)) { 223 int len = read(socket, buffer + buffer_offset, buflen - buffer_offset); 224 if (len == 0) { 225 detach(); 226 return; 227 } 228 229 buffer_offset += len; 230 231 if (data_len == 0) 232 data_len = ntohl(*(uint32_t *)buffer); 233 234 DPRINTF(Ethernet, "Received data from peer: len=%d buffer_offset=%d " 235 "data_len=%d\n", len, buffer_offset, data_len); 236 } 237 238 while (data_len != 0 && buffer_offset >= data_len + sizeof(uint32_t)) { 239 EthPacketPtr packet; 240 packet = make_shared<EthPacketData>(data_len); 241 packet->length = data_len; 242 packet->simLength = data_len; 243 memcpy(packet->data, data, data_len); 244 245 assert(buffer_offset >= data_len + sizeof(uint32_t)); 246 buffer_offset -= data_len + sizeof(uint32_t); 247 if (buffer_offset > 0) { 248 memmove(buffer, data + data_len, buffer_offset); 249 data_len = ntohl(*(uint32_t *)buffer); 250 } else 251 data_len = 0; 252 253 DPRINTF(Ethernet, "EtherTap input len=%d\n", packet->length); 254 DDUMP(EthernetData, packet->data, packet->length); 255 if (!interface->sendPacket(packet)) { 256 DPRINTF(Ethernet, "bus busy...buffer for retransmission\n"); 257 packetBuffer.push(packet); 258 if (!txEvent.scheduled()) 259 schedule(txEvent, curTick() + retryTime); 260 } else if (dump) { 261 dump->dump(packet); 262 } 263 } 264} 265 266void 267EtherTap::retransmit() 268{ 269 if (packetBuffer.empty()) 270 return; 271 272 EthPacketPtr packet = packetBuffer.front(); 273 if (interface->sendPacket(packet)) { 274 if (dump) 275 dump->dump(packet); 276 DPRINTF(Ethernet, "EtherTap retransmit\n"); 277 packetBuffer.front() = NULL; 278 packetBuffer.pop(); 279 } 280 281 if (!packetBuffer.empty() && !txEvent.scheduled()) 282 schedule(txEvent, curTick() + retryTime); 283} 284 285EtherInt* 286EtherTap::getEthPort(const std::string &if_name, int idx) 287{ 288 if (if_name == "tap") { 289 if (interface->getPeer()) 290 panic("Interface already connected to\n"); 291 return interface; 292 } 293 return NULL; 294} 295 296 297//===================================================================== 298 299void 300EtherTap::serialize(CheckpointOut &cp) const 301{ 302 SERIALIZE_SCALAR(socket); 303 SERIALIZE_SCALAR(buflen); 304 uint8_t *buffer = (uint8_t *)this->buffer; 305 SERIALIZE_ARRAY(buffer, buflen); 306 SERIALIZE_SCALAR(buffer_offset); 307 SERIALIZE_SCALAR(data_len); 308 309 bool tapevent_present = false; 310 if (event) { 311 tapevent_present = true; 312 SERIALIZE_SCALAR(tapevent_present); 313 event->serialize(cp); 314 } 315 else { 316 SERIALIZE_SCALAR(tapevent_present); 317 } 318} 319 320void 321EtherTap::unserialize(CheckpointIn &cp) 322{ 323 UNSERIALIZE_SCALAR(socket); 324 UNSERIALIZE_SCALAR(buflen); 325 uint8_t *buffer = (uint8_t *)this->buffer; 326 UNSERIALIZE_ARRAY(buffer, buflen); 327 UNSERIALIZE_SCALAR(buffer_offset); 328 UNSERIALIZE_SCALAR(data_len); 329 330 bool tapevent_present; 331 UNSERIALIZE_SCALAR(tapevent_present); 332 if (tapevent_present) { 333 event = new TapEvent(this, socket, POLLIN|POLLERR); 334 335 event->unserialize(cp); 336 337 if (event->queued()) { 338 pollQueue.schedule(event); 339 } 340 } 341} 342 343//===================================================================== 344 345EtherTap * 346EtherTapParams::create() 347{ 348 return new EtherTap(this); 349} 350