Major changes to how SimObjects are created and initialized. Almost all
[gem5.git] / src / dev / ethertap.cc
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 "params/EtherTap.hh"
54
55 using namespace std;
56
57 /**
58 */
59 class 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
93 void
94 TapListener::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
106 void
107 TapListener::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 */
119 class 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
130 EtherTap::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
139 EtherTap::~EtherTap()
140 {
141 if (event)
142 delete event;
143 if (buffer)
144 delete [] buffer;
145
146 delete listener;
147 }
148
149 void
150 EtherTap::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
163 void
164 EtherTap::detach()
165 {
166 DPRINTF(Ethernet, "EtherTap detached\n");
167 delete event;
168 event = 0;
169 close(socket);
170 socket = -1;
171 }
172
173 bool
174 EtherTap::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 uint32_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
190 void
191 EtherTap::sendDone()
192 {}
193
194 void
195 EtherTap::process(int revent)
196 {
197 if (revent & POLLERR) {
198 detach();
199 return;
200 }
201
202 char *data = buffer + sizeof(uint32_t);
203 if (!(revent & POLLIN))
204 return;
205
206 if (buffer_offset < data_len + sizeof(uint32_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(*(uint32_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(uint32_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(uint32_t);
229 assert(buffer_offset >= 0);
230 if (buffer_offset > 0) {
231 memmove(buffer, data + data_len, buffer_offset);
232 data_len = ntohl(*(uint32_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
249 void
250 EtherTap::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
270 void
271 EtherTap::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
291 void
292 EtherTap::unserialize(Checkpoint *cp, const std::string &section)
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
316 EtherTap *
317 EtherTapParams::create()
318 {
319 EtherTap *tap = new EtherTap(name, dump, port, bufsz);
320
321 if (peer) {
322 tap->setPeer(peer);
323 peer->setPeer(tap);
324 }
325
326 return tap;
327 }