2 * Copyright (c) 2003-2005 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.
28 * Authors: Nathan Binkert
32 * Interface to connect a simulated ethernet device to the real world
35 #if defined(__OpenBSD__) || defined(__APPLE__)
36 #include <sys/param.h>
38 #include <netinet/in.h>
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 "sim/builder.hh"
64 class Event
: public PollEvent
67 TapListener
*listener
;
70 Event(TapListener
*l
, int fd
, int e
)
71 : PollEvent(fd
, e
), listener(l
) {}
73 virtual void process(int revent
) { listener
->accept(); }
80 ListenSocket listener
;
85 TapListener(EtherTap
*t
, int p
)
86 : event(NULL
), tap(t
), port(p
) {}
87 ~TapListener() { if (event
) delete event
; }
96 while (!listener
.listen(port
, true)) {
97 DPRINTF(Ethernet
, "TapListener(listen): Can't bind port %d\n", port
);
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
);
107 TapListener::accept()
109 if (!listener
.islistening())
110 panic("TapListener(accept): cannot accept if we're not listening!");
112 int sfd
= listener
.accept(true);
119 class TapEvent
: public PollEvent
125 TapEvent(EtherTap
*_tap
, int fd
, int e
)
126 : PollEvent(fd
, e
), tap(_tap
) {}
127 virtual void process(int revent
) { tap
->process(revent
); }
130 EtherTap::EtherTap(const string
&name
, EtherDump
*d
, int port
, int bufsz
)
131 : EtherInt(name
), event(NULL
), socket(-1), buflen(bufsz
), dump(d
),
134 buffer
= new char[buflen
];
135 listener
= new TapListener(this, port
);
139 EtherTap::~EtherTap()
150 EtherTap::attach(int fd
)
158 DPRINTF(Ethernet
, "EtherTap attached\n");
159 event
= new TapEvent(this, socket
, POLLIN
|POLLERR
);
160 pollQueue
.schedule(event
);
166 DPRINTF(Ethernet
, "EtherTap detached\n");
174 EtherTap::recvPacket(EthPacketPtr packet
)
179 DPRINTF(Ethernet
, "EtherTap output len=%d\n", packet
->length
);
180 DDUMP(EthernetData
, packet
->data
, packet
->length
);
181 u_int32_t len
= htonl(packet
->length
);
182 write(socket
, &len
, sizeof(len
));
183 write(socket
, packet
->data
, packet
->length
);
195 EtherTap::process(int revent
)
197 if (revent
& POLLERR
) {
202 char *data
= buffer
+ sizeof(u_int32_t
);
203 if (!(revent
& POLLIN
))
206 if (buffer_offset
< data_len
+ sizeof(u_int32_t
)) {
207 int len
= read(socket
, buffer
+ buffer_offset
, buflen
- buffer_offset
);
213 buffer_offset
+= len
;
216 data_len
= ntohl(*(u_int32_t
*)buffer
);
218 DPRINTF(Ethernet
, "Received data from peer: len=%d buffer_offset=%d "
219 "data_len=%d\n", len
, buffer_offset
, data_len
);
222 while (data_len
!= 0 && buffer_offset
>= data_len
+ sizeof(u_int32_t
)) {
224 packet
= new EthPacketData(data_len
);
225 packet
->length
= data_len
;
226 memcpy(packet
->data
, data
, data_len
);
228 buffer_offset
-= data_len
+ sizeof(u_int32_t
);
229 assert(buffer_offset
>= 0);
230 if (buffer_offset
> 0) {
231 memmove(buffer
, data
+ data_len
, buffer_offset
);
232 data_len
= ntohl(*(u_int32_t
*)buffer
);
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
);
250 EtherTap::retransmit()
252 if (packetBuffer
.empty())
255 EthPacketPtr packet
= packetBuffer
.front();
256 if (sendPacket(packet
)) {
259 DPRINTF(Ethernet
, "EtherTap retransmit\n");
260 packetBuffer
.front() = NULL
;
264 if (!packetBuffer
.empty() && !txEvent
.scheduled())
265 txEvent
.schedule(curTick
+ retryTime
);
268 //=====================================================================
271 EtherTap::serialize(ostream
&os
)
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
);
280 bool tapevent_present
= false;
282 tapevent_present
= true;
283 SERIALIZE_SCALAR(tapevent_present
);
284 event
->serialize(os
);
287 SERIALIZE_SCALAR(tapevent_present
);
292 EtherTap::unserialize(Checkpoint
*cp
, const std::string
§ion
)
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
);
301 bool tapevent_present
;
302 UNSERIALIZE_SCALAR(tapevent_present
);
303 if (tapevent_present
) {
304 event
= new TapEvent(this, socket
, POLLIN
|POLLERR
);
306 event
->unserialize(cp
,section
);
308 if (event
->queued()) {
309 pollQueue
.schedule(event
);
314 //=====================================================================
316 BEGIN_DECLARE_SIM_OBJECT_PARAMS(EtherTap
)
318 SimObjectParam
<EtherInt
*> peer
;
319 SimObjectParam
<EtherDump
*> dump
;
320 Param
<unsigned> port
;
321 Param
<unsigned> bufsz
;
323 END_DECLARE_SIM_OBJECT_PARAMS(EtherTap
)
325 BEGIN_INIT_SIM_OBJECT_PARAMS(EtherTap
)
327 INIT_PARAM_DFLT(peer
, "peer interface", NULL
),
328 INIT_PARAM_DFLT(dump
, "object to dump network packets to", NULL
),
329 INIT_PARAM_DFLT(port
, "tap port", 3500),
330 INIT_PARAM_DFLT(bufsz
, "tap buffer size", 10000)
332 END_INIT_SIM_OBJECT_PARAMS(EtherTap
)
335 CREATE_SIM_OBJECT(EtherTap
)
337 EtherTap
*tap
= new EtherTap(getInstanceName(), dump
, port
, bufsz
);
347 REGISTER_SIM_OBJECT("EtherTap", EtherTap
)