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>
44 #include "base/misc.hh"
45 #include "base/pollevent.hh"
46 #include "base/socket.hh"
47 #include "base/trace.hh"
48 #include "debug/Ethernet.hh"
49 #include "debug/EthernetData.hh"
50 #include "dev/etherdump.hh"
51 #include "dev/etherint.hh"
52 #include "dev/etherpkt.hh"
53 #include "dev/ethertap.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 Params
*p
)
131 : EtherObject(p
), event(NULL
), socket(-1), buflen(p
->bufsz
), dump(p
->dump
),
132 interface(NULL
), txEvent(this)
134 if (ListenSocket::allDisabled())
135 fatal("All listeners are disabled! EtherTap can't work!");
137 buffer
= new char[buflen
];
138 listener
= new TapListener(this, p
->port
);
140 interface
= new EtherTapInt(name() + ".interface", this);
143 EtherTap::~EtherTap()
155 EtherTap::attach(int fd
)
163 DPRINTF(Ethernet
, "EtherTap attached\n");
164 event
= new TapEvent(this, socket
, POLLIN
|POLLERR
);
165 pollQueue
.schedule(event
);
171 DPRINTF(Ethernet
, "EtherTap detached\n");
179 EtherTap::recvPacket(EthPacketPtr packet
)
184 DPRINTF(Ethernet
, "EtherTap output len=%d\n", packet
->length
);
185 DDUMP(EthernetData
, packet
->data
, packet
->length
);
186 uint32_t len
= htonl(packet
->length
);
187 ssize_t ret
= write(socket
, &len
, sizeof(len
));
188 if (ret
!= sizeof(len
))
190 ret
= write(socket
, packet
->data
, packet
->length
);
191 if (ret
!= packet
->length
)
194 interface
->recvDone();
204 EtherTap::process(int revent
)
206 if (revent
& POLLERR
) {
211 char *data
= buffer
+ sizeof(uint32_t);
212 if (!(revent
& POLLIN
))
215 if (buffer_offset
< data_len
+ sizeof(uint32_t)) {
216 int len
= read(socket
, buffer
+ buffer_offset
, buflen
- buffer_offset
);
222 buffer_offset
+= len
;
225 data_len
= ntohl(*(uint32_t *)buffer
);
227 DPRINTF(Ethernet
, "Received data from peer: len=%d buffer_offset=%d "
228 "data_len=%d\n", len
, buffer_offset
, data_len
);
231 while (data_len
!= 0 && buffer_offset
>= data_len
+ sizeof(uint32_t)) {
233 packet
= new EthPacketData(data_len
);
234 packet
->length
= data_len
;
235 memcpy(packet
->data
, data
, data_len
);
237 buffer_offset
-= data_len
+ sizeof(uint32_t);
238 assert(buffer_offset
>= 0);
239 if (buffer_offset
> 0) {
240 memmove(buffer
, data
+ data_len
, buffer_offset
);
241 data_len
= ntohl(*(uint32_t *)buffer
);
245 DPRINTF(Ethernet
, "EtherTap input len=%d\n", packet
->length
);
246 DDUMP(EthernetData
, packet
->data
, packet
->length
);
247 if (!interface
->sendPacket(packet
)) {
248 DPRINTF(Ethernet
, "bus busy...buffer for retransmission\n");
249 packetBuffer
.push(packet
);
250 if (!txEvent
.scheduled())
251 schedule(txEvent
, curTick() + retryTime
);
259 EtherTap::retransmit()
261 if (packetBuffer
.empty())
264 EthPacketPtr packet
= packetBuffer
.front();
265 if (interface
->sendPacket(packet
)) {
268 DPRINTF(Ethernet
, "EtherTap retransmit\n");
269 packetBuffer
.front() = NULL
;
273 if (!packetBuffer
.empty() && !txEvent
.scheduled())
274 schedule(txEvent
, curTick() + retryTime
);
278 EtherTap::getEthPort(const std::string
&if_name
, int idx
)
280 if (if_name
== "tap") {
281 if (interface
->getPeer())
282 panic("Interface already connected to\n");
289 //=====================================================================
292 EtherTap::serialize(ostream
&os
)
294 SERIALIZE_SCALAR(socket
);
295 SERIALIZE_SCALAR(buflen
);
296 uint8_t *buffer
= (uint8_t *)this->buffer
;
297 SERIALIZE_ARRAY(buffer
, buflen
);
298 SERIALIZE_SCALAR(buffer_offset
);
299 SERIALIZE_SCALAR(data_len
);
301 bool tapevent_present
= false;
303 tapevent_present
= true;
304 SERIALIZE_SCALAR(tapevent_present
);
305 event
->serialize(os
);
308 SERIALIZE_SCALAR(tapevent_present
);
313 EtherTap::unserialize(Checkpoint
*cp
, const std::string
§ion
)
315 UNSERIALIZE_SCALAR(socket
);
316 UNSERIALIZE_SCALAR(buflen
);
317 uint8_t *buffer
= (uint8_t *)this->buffer
;
318 UNSERIALIZE_ARRAY(buffer
, buflen
);
319 UNSERIALIZE_SCALAR(buffer_offset
);
320 UNSERIALIZE_SCALAR(data_len
);
322 bool tapevent_present
;
323 UNSERIALIZE_SCALAR(tapevent_present
);
324 if (tapevent_present
) {
325 event
= new TapEvent(this, socket
, POLLIN
|POLLERR
);
327 event
->unserialize(cp
,section
);
329 if (event
->queued()) {
330 pollQueue
.schedule(event
);
335 //=====================================================================
338 EtherTapParams::create()
340 return new EtherTap(this);