807765d91ac289e8703ee8878677827303f81dc5
2 * Copyright (c) 2003-2004 The Regents of The University of Michigan
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.
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.
30 * Interface to connect a simulated ethernet device to the real world
33 #if defined(__OpenBSD__) || defined(__APPLE__)
34 #include <sys/param.h>
36 #include <netinet/in.h>
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"
62 class Event
: public PollEvent
65 TapListener
*listener
;
68 Event(TapListener
*l
, int fd
, int e
)
69 : PollEvent(fd
, e
), listener(l
) {}
71 virtual void process(int revent
) { listener
->accept(); }
78 ListenSocket listener
;
83 TapListener(EtherTap
*t
, int p
)
84 : event(NULL
), tap(t
), port(p
) {}
85 ~TapListener() { if (event
) delete event
; }
94 while (!listener
.listen(port
, true)) {
95 DPRINTF(Ethernet
, "TapListener(listen): Can't bind port %d\n", port
);
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
);
105 TapListener::accept()
107 if (!listener
.islistening())
108 panic("TapListener(accept): cannot accept if we're not listening!");
110 int sfd
= listener
.accept(true);
117 class TapEvent
: public PollEvent
123 TapEvent(EtherTap
*_tap
, int fd
, int e
)
124 : PollEvent(fd
, e
), tap(_tap
) {}
125 virtual void process(int revent
) { tap
->process(revent
); }
128 EtherTap::EtherTap(const string
&name
, EtherDump
*d
, int port
, int bufsz
)
129 : EtherInt(name
), event(NULL
), socket(-1), buflen(bufsz
), dump(d
),
132 buffer
= new char[buflen
];
133 listener
= new TapListener(this, port
);
137 EtherTap::~EtherTap()
148 EtherTap::attach(int fd
)
156 DPRINTF(Ethernet
, "EtherTap attached\n");
157 event
= new TapEvent(this, socket
, POLLIN
|POLLERR
);
158 pollQueue
.schedule(event
);
164 DPRINTF(Ethernet
, "EtherTap detached\n");
172 EtherTap::recvPacket(PacketPtr packet
)
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
);
193 EtherTap::process(int revent
)
195 if (revent
& POLLERR
) {
200 char *data
= buffer
+ sizeof(u_int32_t
);
201 if (!(revent
& POLLIN
))
204 if (buffer_offset
< data_len
+ sizeof(u_int32_t
)) {
205 int len
= read(socket
, buffer
+ buffer_offset
, buflen
- buffer_offset
);
211 buffer_offset
+= len
;
214 data_len
= ntohl(*(u_int32_t
*)buffer
);
216 DPRINTF(Ethernet
, "Received data from peer: len=%d buffer_offset=%d "
217 "data_len=%d\n", len
, buffer_offset
, data_len
);
220 while (data_len
!= 0 && buffer_offset
>= data_len
+ sizeof(u_int32_t
)) {
222 packet
= new PacketData(data_len
);
223 packet
->length
= data_len
;
224 memcpy(packet
->data
, data
, data_len
);
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
);
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
+ 1000);
247 EtherTap::retransmit()
249 if (packetBuffer
.empty())
252 PacketPtr packet
= packetBuffer
.front();
253 if (sendPacket(packet
)) {
256 DPRINTF(Ethernet
, "EtherTap retransmit\n");
257 packetBuffer
.front() = NULL
;
261 if (!packetBuffer
.empty() && !txEvent
.scheduled())
262 txEvent
.schedule(curTick
+ 1000);
265 //=====================================================================
268 EtherTap::serialize(ostream
&os
)
270 SERIALIZE_SCALAR(socket
);
271 SERIALIZE_SCALAR(buflen
);
272 SERIALIZE_ARRAY((uint8_t *)buffer
,buflen
);
273 SERIALIZE_SCALAR(buffer_offset
);
274 SERIALIZE_SCALAR(data_len
);
276 bool tapevent_present
= false;
278 tapevent_present
= true;
279 SERIALIZE_SCALAR(tapevent_present
);
280 event
->serialize(os
);
283 SERIALIZE_SCALAR(tapevent_present
);
288 EtherTap::unserialize(Checkpoint
*cp
, const std::string
§ion
)
290 UNSERIALIZE_SCALAR(socket
);
291 UNSERIALIZE_SCALAR(buflen
);
292 UNSERIALIZE_ARRAY((uint8_t *)buffer
,buflen
);
293 UNSERIALIZE_SCALAR(buffer_offset
);
294 UNSERIALIZE_SCALAR(data_len
);
296 bool tapevent_present
;
297 UNSERIALIZE_SCALAR(tapevent_present
);
298 if (tapevent_present
) {
299 event
= new TapEvent(this, socket
, POLLIN
|POLLERR
);
301 event
->unserialize(cp
,section
);
303 if (event
->queued()) {
304 pollQueue
.schedule(event
);
309 //=====================================================================
311 BEGIN_DECLARE_SIM_OBJECT_PARAMS(EtherTap
)
313 SimObjectParam
<EtherInt
*> peer
;
314 SimObjectParam
<EtherDump
*> dump
;
315 Param
<unsigned> port
;
316 Param
<unsigned> bufsz
;
318 END_DECLARE_SIM_OBJECT_PARAMS(EtherTap
)
320 BEGIN_INIT_SIM_OBJECT_PARAMS(EtherTap
)
322 INIT_PARAM_DFLT(peer
, "peer interface", NULL
),
323 INIT_PARAM_DFLT(dump
, "object to dump network packets to", NULL
),
324 INIT_PARAM_DFLT(port
, "tap port", 3500),
325 INIT_PARAM_DFLT(bufsz
, "tap buffer size", 10000)
327 END_INIT_SIM_OBJECT_PARAMS(EtherTap
)
330 CREATE_SIM_OBJECT(EtherTap
)
332 EtherTap
*tap
= new EtherTap(getInstanceName(), dump
, port
, bufsz
);
342 REGISTER_SIM_OBJECT("EtherTap", EtherTap
)