Deleted Added
sdiff udiff text old ( 12054:ab04045965d1 ) new ( 12055:945e851d846b )
full compact
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 */
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 EtherTapStub *tap;
84 int port;
85
86 public:
87 TapListener(EtherTapStub *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);

--- 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
124/**
125 */
126class TapEvent : public PollEvent
127{
128 protected:
129 EtherTapStub *tap;
130
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)
140{
141 if (ListenSocket::allDisabled())
142 fatal("All listeners are disabled! EtherTapStub 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
150EtherTapStub::~EtherTapStub()
151{
152 if (event)
153 delete event;
154 if (buffer)
155 delete [] buffer;
156
157 delete interface;
158 delete listener;
159}
160
161void
162EtherTapStub::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, "EtherTapStub attached\n");
171 event = new TapEvent(this, socket, POLLIN|POLLERR);
172 pollQueue.schedule(event);
173}
174
175void
176EtherTapStub::detach()
177{
178 DPRINTF(Ethernet, "EtherTapStub detached\n");
179 delete event;
180 event = 0;
181 close(socket);
182 socket = -1;
183}
184
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
207EtherTapStub::sendDone()
208{}
209
210void
211EtherTapStub::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, "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())
270 return;
271
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 }
280
281 if (!packetBuffer.empty() && !txEvent.scheduled())
282 schedule(txEvent, curTick() + retryTime);
283}
284
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}
295
296
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);
314 }
315 else {
316 SERIALIZE_SCALAR(tapevent_present);
317 }
318}
319
320void
321EtherTapStub::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
345EtherTapStub *
346EtherTapStubParams::create()
347{
348 return new EtherTapStub(this);
349}