ethertap.cc revision 11263:8dcc6b40f164
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 memcpy(packet->data, data, data_len); 243 244 buffer_offset -= data_len + sizeof(uint32_t); 245 assert(buffer_offset >= 0); 246 if (buffer_offset > 0) { 247 memmove(buffer, data + data_len, buffer_offset); 248 data_len = ntohl(*(uint32_t *)buffer); 249 } else 250 data_len = 0; 251 252 DPRINTF(Ethernet, "EtherTap input len=%d\n", packet->length); 253 DDUMP(EthernetData, packet->data, packet->length); 254 if (!interface->sendPacket(packet)) { 255 DPRINTF(Ethernet, "bus busy...buffer for retransmission\n"); 256 packetBuffer.push(packet); 257 if (!txEvent.scheduled()) 258 schedule(txEvent, curTick() + retryTime); 259 } else if (dump) { 260 dump->dump(packet); 261 } 262 } 263} 264 265void 266EtherTap::retransmit() 267{ 268 if (packetBuffer.empty()) 269 return; 270 271 EthPacketPtr packet = packetBuffer.front(); 272 if (interface->sendPacket(packet)) { 273 if (dump) 274 dump->dump(packet); 275 DPRINTF(Ethernet, "EtherTap retransmit\n"); 276 packetBuffer.front() = NULL; 277 packetBuffer.pop(); 278 } 279 280 if (!packetBuffer.empty() && !txEvent.scheduled()) 281 schedule(txEvent, curTick() + retryTime); 282} 283 284EtherInt* 285EtherTap::getEthPort(const std::string &if_name, int idx) 286{ 287 if (if_name == "tap") { 288 if (interface->getPeer()) 289 panic("Interface already connected to\n"); 290 return interface; 291 } 292 return NULL; 293} 294 295 296//===================================================================== 297 298void 299EtherTap::serialize(CheckpointOut &cp) const 300{ 301 SERIALIZE_SCALAR(socket); 302 SERIALIZE_SCALAR(buflen); 303 uint8_t *buffer = (uint8_t *)this->buffer; 304 SERIALIZE_ARRAY(buffer, buflen); 305 SERIALIZE_SCALAR(buffer_offset); 306 SERIALIZE_SCALAR(data_len); 307 308 bool tapevent_present = false; 309 if (event) { 310 tapevent_present = true; 311 SERIALIZE_SCALAR(tapevent_present); 312 event->serialize(cp); 313 } 314 else { 315 SERIALIZE_SCALAR(tapevent_present); 316 } 317} 318 319void 320EtherTap::unserialize(CheckpointIn &cp) 321{ 322 UNSERIALIZE_SCALAR(socket); 323 UNSERIALIZE_SCALAR(buflen); 324 uint8_t *buffer = (uint8_t *)this->buffer; 325 UNSERIALIZE_ARRAY(buffer, buflen); 326 UNSERIALIZE_SCALAR(buffer_offset); 327 UNSERIALIZE_SCALAR(data_len); 328 329 bool tapevent_present; 330 UNSERIALIZE_SCALAR(tapevent_present); 331 if (tapevent_present) { 332 event = new TapEvent(this, socket, POLLIN|POLLERR); 333 334 event->unserialize(cp); 335 336 if (event->queued()) { 337 pollQueue.schedule(event); 338 } 339 } 340} 341 342//===================================================================== 343 344EtherTap * 345EtherTapParams::create() 346{ 347 return new EtherTap(this); 348} 349