ethertap.cc (12054:ab04045965d1) | ethertap.cc (12055:945e851d846b) |
---|---|
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; --- 42 unchanged lines hidden (view full) --- 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 | 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; --- 42 unchanged lines hidden (view full) --- 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 */ | 59class TapEvent : public PollEvent 60{ 61 protected: 62 EtherTapBase *tap; 63 64 public: 65 TapEvent(EtherTapBase *_tap, int fd, int e) 66 : PollEvent(fd, e), tap(_tap) {} 67 virtual void process(int revent) { tap->recvReal(revent); } 68}; 69 70EtherTapBase::EtherTapBase(const Params *p) 71 : EtherObject(p), buflen(p->bufsz), dump(p->dump), event(NULL), 72 interface(NULL), txEvent(this) 73{ 74 buffer = new uint8_t[buflen]; 75 interface = new EtherTapInt(name() + ".interface", this); 76} 77 78EtherTapBase::~EtherTapBase() 79{ 80 delete buffer; 81 delete event; 82 delete interface; 83} 84 85void 86EtherTapBase::serialize(CheckpointOut &cp) const 87{ 88 SERIALIZE_SCALAR(buflen); 89 uint8_t *buffer = (uint8_t *)this->buffer; 90 SERIALIZE_ARRAY(buffer, buflen); 91 92 bool tapevent_present = false; 93 if (event) { 94 tapevent_present = true; 95 SERIALIZE_SCALAR(tapevent_present); 96 event->serialize(cp); 97 } else { 98 SERIALIZE_SCALAR(tapevent_present); 99 } 100} 101 102void 103EtherTapBase::unserialize(CheckpointIn &cp) 104{ 105 UNSERIALIZE_SCALAR(buflen); 106 uint8_t *buffer = (uint8_t *)this->buffer; 107 UNSERIALIZE_ARRAY(buffer, buflen); 108 109 bool tapevent_present; 110 UNSERIALIZE_SCALAR(tapevent_present); 111 if (tapevent_present) { 112 event = new TapEvent(this, 0, 0); 113 event->unserialize(cp); 114 if (event->queued()) 115 pollQueue.schedule(event); 116 } 117} 118 119 120void 121EtherTapBase::pollFd(int fd) 122{ 123 assert(!event); 124 event = new TapEvent(this, fd, POLLIN|POLLERR); 125 pollQueue.schedule(event); 126} 127 128void 129EtherTapBase::stopPolling() 130{ 131 assert(event); 132 delete event; 133 event = NULL; 134} 135 136 137EtherInt* 138EtherTapBase::getEthPort(const std::string &if_name, int idx) 139{ 140 if (if_name == "tap") { 141 if (interface->getPeer()) 142 panic("Interface already connected to\n"); 143 return interface; 144 } 145 return NULL; 146} 147 148bool 149EtherTapBase::recvSimulated(EthPacketPtr packet) 150{ 151 if (dump) 152 dump->dump(packet); 153 154 DPRINTF(Ethernet, "EtherTap sim->real len=%d\n", packet->length); 155 DDUMP(EthernetData, packet->data, packet->length); 156 157 bool success = sendReal(packet->data, packet->length); 158 159 interface->recvDone(); 160 161 return success; 162} 163 164void 165EtherTapBase::sendSimulated(void *data, size_t len) 166{ 167 EthPacketPtr packet; 168 packet = make_shared<EthPacketData>(len); 169 packet->length = len; 170 packet->simLength = len; 171 memcpy(packet->data, data, len); 172 173 DPRINTF(Ethernet, "EtherTap real->sim len=%d\n", packet->length); 174 DDUMP(EthernetData, packet->data, packet->length); 175 if (!packetBuffer.empty() || !interface->sendPacket(packet)) { 176 DPRINTF(Ethernet, "bus busy...buffer for retransmission\n"); 177 packetBuffer.push(packet); 178 if (!txEvent.scheduled()) 179 schedule(txEvent, curTick() + retryTime); 180 } else if (dump) { 181 dump->dump(packet); 182 } 183} 184 185void 186EtherTapBase::retransmit() 187{ 188 if (packetBuffer.empty()) 189 return; 190 191 EthPacketPtr packet = packetBuffer.front(); 192 if (interface->sendPacket(packet)) { 193 if (dump) 194 dump->dump(packet); 195 DPRINTF(Ethernet, "EtherTap retransmit\n"); 196 packetBuffer.front() = NULL; 197 packetBuffer.pop(); 198 } 199 200 if (!packetBuffer.empty() && !txEvent.scheduled()) 201 schedule(txEvent, curTick() + retryTime); 202} 203 204 |
61class TapListener 62{ 63 protected: | 205class TapListener 206{ 207 protected: |
64 /** 65 */ | |
66 class Event : public PollEvent 67 { 68 protected: 69 TapListener *listener; 70 71 public: | 208 class Event : public PollEvent 209 { 210 protected: 211 TapListener *listener; 212 213 public: |
72 Event(TapListener *l, int fd, int e) 73 : PollEvent(fd, e), listener(l) {} | 214 Event(TapListener *l, int fd, int e) : PollEvent(fd, e), listener(l) {} |
74 | 215 |
75 virtual void process(int revent) { listener->accept(); } | 216 void process(int revent) override { listener->accept(); } |
76 }; 77 78 friend class Event; 79 Event *event; 80 | 217 }; 218 219 friend class Event; 220 Event *event; 221 |
222 void accept(); 223 |
|
81 protected: 82 ListenSocket listener; 83 EtherTapStub *tap; 84 int port; 85 86 public: | 224 protected: 225 ListenSocket listener; 226 EtherTapStub *tap; 227 int port; 228 229 public: |
87 TapListener(EtherTapStub *t, int p) 88 : event(NULL), tap(t), port(p) {} 89 ~TapListener() { if (event) delete event; } | 230 TapListener(EtherTapStub *t, int p) : event(NULL), tap(t), port(p) {} 231 ~TapListener() { delete event; } |
90 | 232 |
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); --- 16 unchanged lines hidden (view full) --- 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 | 233 void listen(); 234}; 235 236void 237TapListener::listen() 238{ 239 while (!listener.listen(port, true)) { 240 DPRINTF(Ethernet, "TapListener(listen): Can't bind port %d\n", port); --- 16 unchanged lines hidden (view full) --- 257 if (!listener.islistening()) 258 panic("TapListener(accept): cannot accept if we're not listening!"); 259 260 int sfd = listener.accept(true); 261 if (sfd != -1) 262 tap->attach(sfd); 263} 264 |
124/** 125 */ 126class TapEvent : public PollEvent 127{ 128 protected: 129 EtherTapStub *tap; | |
130 | 265 |
131 public: 132 TapEvent(EtherTapStub *_tap, int fd, int e) 133 : PollEvent(fd, e), tap(_tap) {} 134 virtual void process(int revent) { tap->process(revent); } 135}; 136 137EtherTapStub::EtherTapStub(const Params *p) 138 : EtherObject(p), event(NULL), socket(-1), buflen(p->bufsz), dump(p->dump), 139 interface(NULL), txEvent(this) | 266EtherTapStub::EtherTapStub(const Params *p) : EtherTapBase(p), socket(-1) |
140{ 141 if (ListenSocket::allDisabled()) 142 fatal("All listeners are disabled! EtherTapStub can't work!"); 143 | 267{ 268 if (ListenSocket::allDisabled()) 269 fatal("All listeners are disabled! EtherTapStub can't work!"); 270 |
144 buffer = new char[buflen]; | |
145 listener = new TapListener(this, p->port); 146 listener->listen(); | 271 listener = new TapListener(this, p->port); 272 listener->listen(); |
147 interface = new EtherTapInt(name() + ".interface", this); | |
148} 149 150EtherTapStub::~EtherTapStub() 151{ | 273} 274 275EtherTapStub::~EtherTapStub() 276{ |
152 if (event) 153 delete event; 154 if (buffer) 155 delete [] buffer; 156 157 delete interface; | |
158 delete listener; 159} 160 161void | 277 delete listener; 278} 279 280void |
281EtherTapStub::serialize(CheckpointOut &cp) const 282{ 283 EtherTapBase::serialize(cp); 284 285 SERIALIZE_SCALAR(socket); 286 SERIALIZE_SCALAR(buffer_used); 287 SERIALIZE_SCALAR(frame_len); 288} 289 290void 291EtherTapStub::unserialize(CheckpointIn &cp) 292{ 293 EtherTapBase::unserialize(cp); 294 295 UNSERIALIZE_SCALAR(socket); 296 UNSERIALIZE_SCALAR(buffer_used); 297 UNSERIALIZE_SCALAR(frame_len); 298} 299 300 301void |
|
162EtherTapStub::attach(int fd) 163{ 164 if (socket != -1) 165 close(fd); 166 | 302EtherTapStub::attach(int fd) 303{ 304 if (socket != -1) 305 close(fd); 306 |
167 buffer_offset = 0; 168 data_len = 0; | 307 buffer_used = 0; 308 frame_len = 0; |
169 socket = fd; 170 DPRINTF(Ethernet, "EtherTapStub attached\n"); | 309 socket = fd; 310 DPRINTF(Ethernet, "EtherTapStub attached\n"); |
171 event = new TapEvent(this, socket, POLLIN|POLLERR); 172 pollQueue.schedule(event); | 311 pollFd(socket); |
173} 174 175void 176EtherTapStub::detach() 177{ 178 DPRINTF(Ethernet, "EtherTapStub detached\n"); | 312} 313 314void 315EtherTapStub::detach() 316{ 317 DPRINTF(Ethernet, "EtherTapStub detached\n"); |
179 delete event; 180 event = 0; | 318 stopPolling(); |
181 close(socket); 182 socket = -1; 183} 184 | 319 close(socket); 320 socket = -1; 321} 322 |
185bool 186EtherTapStub::recvPacket(EthPacketPtr packet) 187{ 188 if (dump) 189 dump->dump(packet); 190 191 DPRINTF(Ethernet, "EtherTapStub 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 | 323void |
207EtherTapStub::sendDone() 208{} 209 210void 211EtherTapStub::process(int revent) | 324EtherTapStub::recvReal(int revent) |
212{ 213 if (revent & POLLERR) { 214 detach(); 215 return; 216 } 217 | 325{ 326 if (revent & POLLERR) { 327 detach(); 328 return; 329 } 330 |
218 char *data = buffer + sizeof(uint32_t); | |
219 if (!(revent & POLLIN)) 220 return; 221 | 331 if (!(revent & POLLIN)) 332 return; 333 |
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); | 334 // Read in as much of the new data as we can. 335 int len = read(socket, buffer + buffer_used, buflen - buffer_used); 336 if (len == 0) { 337 detach(); 338 return; |
236 } | 339 } |
340 buffer_used += len; |
|
237 | 341 |
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, "EtherTapStub 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 267EtherTapStub::retransmit() 268{ 269 if (packetBuffer.empty()) | 342 // If there's not enough data for the frame length, wait for more. 343 if (buffer_used < sizeof(uint32_t)) |
270 return; 271 | 344 return; 345 |
272 EthPacketPtr packet = packetBuffer.front(); 273 if (interface->sendPacket(packet)) { 274 if (dump) 275 dump->dump(packet); 276 DPRINTF(Ethernet, "EtherTapStub retransmit\n"); 277 packetBuffer.front() = NULL; 278 packetBuffer.pop(); 279 } | 346 if (frame_len == 0) 347 frame_len = ntohl(*(uint32_t *)buffer); |
280 | 348 |
281 if (!packetBuffer.empty() && !txEvent.scheduled()) 282 schedule(txEvent, curTick() + retryTime); 283} | 349 DPRINTF(Ethernet, "Received data from peer: len=%d buffer_used=%d " 350 "frame_len=%d\n", len, buffer_used, frame_len); |
284 | 351 |
285EtherInt* 286EtherTapStub::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} | 352 uint8_t *frame_start = &buffer[sizeof(uint32_t)]; 353 while (frame_len != 0 && buffer_used >= frame_len + sizeof(uint32_t)) { 354 sendSimulated(frame_start, frame_len); |
295 | 355 |
356 // Bookkeeping. 357 buffer_used -= frame_len + sizeof(uint32_t); 358 if (buffer_used > 0) { 359 // If there's still any data left, move it into position. 360 memmove(buffer, frame_start + frame_len, buffer_used); 361 } 362 frame_len = 0; |
|
296 | 363 |
297//===================================================================== 298 299void 300EtherTapStub::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); | 364 if (buffer_used >= sizeof(uint32_t)) 365 frame_len = ntohl(*(uint32_t *)buffer); |
314 } | 366 } |
315 else { 316 SERIALIZE_SCALAR(tapevent_present); 317 } | |
318} 319 | 367} 368 |
320void 321EtherTapStub::unserialize(CheckpointIn &cp) | 369bool 370EtherTapStub::sendReal(const void *data, size_t len) |
322{ | 371{ |
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 } | 372 uint32_t frame_len = htonl(len); 373 ssize_t ret = write(socket, &frame_len, sizeof(frame_len)); 374 if (ret != sizeof(frame_len)) 375 return false; 376 return write(socket, data, len) == len; |
341} 342 | 377} 378 |
343//===================================================================== | |
344 345EtherTapStub * 346EtherTapStubParams::create() 347{ 348 return new EtherTapStub(this); 349} | 379 380EtherTapStub * 381EtherTapStubParams::create() 382{ 383 return new EtherTapStub(this); 384} |