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