2 * Copyright (c) 2003 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__)
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 EtherPacket
;
223 packet
->data
= new uint8_t[data_len
];
224 packet
->length
= data_len
;
225 memcpy(packet
->data
, data
, data_len
);
227 buffer_offset
-= data_len
+ sizeof(u_int32_t
);
228 assert(buffer_offset
>= 0);
229 if (buffer_offset
> 0) {
230 memmove(buffer
, data
+ data_len
, buffer_offset
);
231 data_len
= ntohl(*(u_int32_t
*)buffer
);
235 DPRINTF(Ethernet
, "EtherTap input len=%d\n", packet
->length
);
236 DDUMP(EthernetData
, packet
->data
, packet
->length
);
237 if (!sendPacket(packet
)) {
238 DPRINTF(Ethernet
, "bus busy...buffer for retransmission\n");
239 packetBuffer
.push(packet
);
240 if (!txEvent
.scheduled())
241 txEvent
.schedule(curTick
+ 1000);
248 EtherTap::retransmit()
250 if (packetBuffer
.empty())
253 PacketPtr packet
= packetBuffer
.front();
254 if (sendPacket(packet
)) {
257 DPRINTF(Ethernet
, "EtherTap retransmit\n");
258 packetBuffer
.front() = NULL
;
262 if (!packetBuffer
.empty() && !txEvent
.scheduled())
263 txEvent
.schedule(curTick
+ 1000);
266 //=====================================================================
269 EtherTap::serialize(ostream
&os
)
271 SERIALIZE_SCALAR(socket
);
272 SERIALIZE_SCALAR(buflen
);
273 SERIALIZE_ARRAY((uint8_t *)buffer
,buflen
);
274 SERIALIZE_SCALAR(buffer_offset
);
275 SERIALIZE_SCALAR(data_len
);
277 bool tapevent_present
= false;
279 tapevent_present
= true;
280 SERIALIZE_SCALAR(tapevent_present
);
281 event
->serialize(os
);
284 SERIALIZE_SCALAR(tapevent_present
);
289 EtherTap::unserialize(Checkpoint
*cp
, const std::string
§ion
)
291 UNSERIALIZE_SCALAR(socket
);
292 UNSERIALIZE_SCALAR(buflen
);
293 UNSERIALIZE_ARRAY((uint8_t *)buffer
,buflen
);
294 UNSERIALIZE_SCALAR(buffer_offset
);
295 UNSERIALIZE_SCALAR(data_len
);
297 bool tapevent_present
;
298 UNSERIALIZE_SCALAR(tapevent_present
);
299 if (tapevent_present
) {
300 event
= new TapEvent(this, socket
, POLLIN
|POLLERR
);
302 event
->unserialize(cp
,section
);
304 if (event
->queued()) {
305 pollQueue
.schedule(event
);
310 //=====================================================================
312 BEGIN_DECLARE_SIM_OBJECT_PARAMS(EtherTap
)
314 SimObjectParam
<EtherInt
*> peer
;
315 SimObjectParam
<EtherDump
*> packet_dump
;
316 Param
<uint16_t> port
;
317 Param
<uint16_t> bufsz
;
319 END_DECLARE_SIM_OBJECT_PARAMS(EtherTap
)
321 BEGIN_INIT_SIM_OBJECT_PARAMS(EtherTap
)
323 INIT_PARAM_DFLT(peer
, "peer interface", NULL
),
324 INIT_PARAM_DFLT(packet_dump
, "object to dump network packets to", NULL
),
325 INIT_PARAM_DFLT(port
, "tap port", 3500),
326 INIT_PARAM_DFLT(bufsz
, "tap buffer size", 10000)
328 END_INIT_SIM_OBJECT_PARAMS(EtherTap
)
331 CREATE_SIM_OBJECT(EtherTap
)
333 EtherTap
*tap
= new EtherTap(getInstanceName(), packet_dump
, port
, bufsz
);
343 REGISTER_SIM_OBJECT("EtherTap", EtherTap
)