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"
63 class Event
: public PollEvent
66 TapListener
*listener
;
69 Event(TapListener
*l
, int fd
, int e
)
70 : PollEvent(fd
, e
), listener(l
) {}
72 virtual void process(int revent
) { listener
->accept(); }
79 ListenSocket listener
;
84 TapListener(EtherTap
*t
, int p
)
85 : event(NULL
), tap(t
), port(p
) {}
86 ~TapListener() { if (event
) delete event
; }
95 while (!listener
.listen(port
, true)) {
96 DPRINTF(Ethernet
, "TapListener(listen): Can't bind port %d\n", port
);
100 ccprintf(cerr
, "Listening for tap connection on port %d\n", port
);
101 event
= new Event(this, listener
.getfd(), POLLIN
|POLLERR
);
102 pollQueue
.schedule(event
);
106 TapListener::accept()
108 if (!listener
.islistening())
109 panic("TapListener(accept): cannot accept if we're not listening!");
111 int sfd
= listener
.accept(true);
118 class TapEvent
: public PollEvent
124 TapEvent(EtherTap
*_tap
, int fd
, int e
)
125 : PollEvent(fd
, e
), tap(_tap
) {}
126 virtual void process(int revent
) { tap
->process(revent
); }
129 EtherTap::EtherTap(const Params
*p
)
130 : EtherObject(p
), event(NULL
), socket(-1), buflen(p
->bufsz
), dump(p
->dump
),
131 interface(NULL
), txEvent(this)
133 if (ListenSocket::allDisabled())
134 fatal("All listeners are disabled! EtherTap can't work!");
136 buffer
= new char[buflen
];
137 listener
= new TapListener(this, p
->port
);
139 interface
= new EtherTapInt(name() + ".interface", this);
142 EtherTap::~EtherTap()
153 EtherTap::attach(int fd
)
161 DPRINTF(Ethernet
, "EtherTap attached\n");
162 event
= new TapEvent(this, socket
, POLLIN
|POLLERR
);
163 pollQueue
.schedule(event
);
169 DPRINTF(Ethernet
, "EtherTap detached\n");
177 EtherTap::recvPacket(EthPacketPtr packet
)
182 DPRINTF(Ethernet
, "EtherTap output len=%d\n", packet
->length
);
183 DDUMP(EthernetData
, packet
->data
, packet
->length
);
184 uint32_t len
= htonl(packet
->length
);
185 ssize_t ret
= write(socket
, &len
, sizeof(len
));
186 if (ret
!= sizeof(len
))
188 ret
= write(socket
, packet
->data
, packet
->length
);
189 if (ret
!= packet
->length
)
192 interface
->recvDone();
202 EtherTap::process(int revent
)
204 if (revent
& POLLERR
) {
209 char *data
= buffer
+ sizeof(uint32_t);
210 if (!(revent
& POLLIN
))
213 if (buffer_offset
< data_len
+ sizeof(uint32_t)) {
214 int len
= read(socket
, buffer
+ buffer_offset
, buflen
- buffer_offset
);
220 buffer_offset
+= len
;
223 data_len
= ntohl(*(uint32_t *)buffer
);
225 DPRINTF(Ethernet
, "Received data from peer: len=%d buffer_offset=%d "
226 "data_len=%d\n", len
, buffer_offset
, data_len
);
229 while (data_len
!= 0 && buffer_offset
>= data_len
+ sizeof(uint32_t)) {
231 packet
= new EthPacketData(data_len
);
232 packet
->length
= data_len
;
233 memcpy(packet
->data
, data
, data_len
);
235 buffer_offset
-= data_len
+ sizeof(uint32_t);
236 assert(buffer_offset
>= 0);
237 if (buffer_offset
> 0) {
238 memmove(buffer
, data
+ data_len
, buffer_offset
);
239 data_len
= ntohl(*(uint32_t *)buffer
);
243 DPRINTF(Ethernet
, "EtherTap input len=%d\n", packet
->length
);
244 DDUMP(EthernetData
, packet
->data
, packet
->length
);
245 if (!interface
->sendPacket(packet
)) {
246 DPRINTF(Ethernet
, "bus busy...buffer for retransmission\n");
247 packetBuffer
.push(packet
);
248 if (!txEvent
.scheduled())
249 schedule(txEvent
, curTick
+ retryTime
);
257 EtherTap::retransmit()
259 if (packetBuffer
.empty())
262 EthPacketPtr packet
= packetBuffer
.front();
263 if (interface
->sendPacket(packet
)) {
266 DPRINTF(Ethernet
, "EtherTap retransmit\n");
267 packetBuffer
.front() = NULL
;
271 if (!packetBuffer
.empty() && !txEvent
.scheduled())
272 schedule(txEvent
, curTick
+ retryTime
);
276 EtherTap::getEthPort(const std::string
&if_name
, int idx
)
278 if (if_name
== "tap") {
279 if (interface
->getPeer())
280 panic("Interface already connected to\n");
287 //=====================================================================
290 EtherTap::serialize(ostream
&os
)
292 SERIALIZE_SCALAR(socket
);
293 SERIALIZE_SCALAR(buflen
);
294 uint8_t *buffer
= (uint8_t *)this->buffer
;
295 SERIALIZE_ARRAY(buffer
, buflen
);
296 SERIALIZE_SCALAR(buffer_offset
);
297 SERIALIZE_SCALAR(data_len
);
299 bool tapevent_present
= false;
301 tapevent_present
= true;
302 SERIALIZE_SCALAR(tapevent_present
);
303 event
->serialize(os
);
306 SERIALIZE_SCALAR(tapevent_present
);
311 EtherTap::unserialize(Checkpoint
*cp
, const std::string
§ion
)
313 UNSERIALIZE_SCALAR(socket
);
314 UNSERIALIZE_SCALAR(buflen
);
315 uint8_t *buffer
= (uint8_t *)this->buffer
;
316 UNSERIALIZE_ARRAY(buffer
, buflen
);
317 UNSERIALIZE_SCALAR(buffer_offset
);
318 UNSERIALIZE_SCALAR(data_len
);
320 bool tapevent_present
;
321 UNSERIALIZE_SCALAR(tapevent_present
);
322 if (tapevent_present
) {
323 event
= new TapEvent(this, socket
, POLLIN
|POLLERR
);
325 event
->unserialize(cp
,section
);
327 if (event
->queued()) {
328 pollQueue
.schedule(event
);
333 //=====================================================================
336 EtherTapParams::create()
338 return new EtherTap(this);