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()
154 EtherTap::attach(int fd
)
162 DPRINTF(Ethernet
, "EtherTap attached\n");
163 event
= new TapEvent(this, socket
, POLLIN
|POLLERR
);
164 pollQueue
.schedule(event
);
170 DPRINTF(Ethernet
, "EtherTap detached\n");
178 EtherTap::recvPacket(EthPacketPtr packet
)
183 DPRINTF(Ethernet
, "EtherTap output len=%d\n", packet
->length
);
184 DDUMP(EthernetData
, packet
->data
, packet
->length
);
185 uint32_t len
= htonl(packet
->length
);
186 ssize_t ret
= write(socket
, &len
, sizeof(len
));
187 if (ret
!= sizeof(len
))
189 ret
= write(socket
, packet
->data
, packet
->length
);
190 if (ret
!= packet
->length
)
193 interface
->recvDone();
203 EtherTap::process(int revent
)
205 if (revent
& POLLERR
) {
210 char *data
= buffer
+ sizeof(uint32_t);
211 if (!(revent
& POLLIN
))
214 if (buffer_offset
< data_len
+ sizeof(uint32_t)) {
215 int len
= read(socket
, buffer
+ buffer_offset
, buflen
- buffer_offset
);
221 buffer_offset
+= len
;
224 data_len
= ntohl(*(uint32_t *)buffer
);
226 DPRINTF(Ethernet
, "Received data from peer: len=%d buffer_offset=%d "
227 "data_len=%d\n", len
, buffer_offset
, data_len
);
230 while (data_len
!= 0 && buffer_offset
>= data_len
+ sizeof(uint32_t)) {
232 packet
= new EthPacketData(data_len
);
233 packet
->length
= data_len
;
234 memcpy(packet
->data
, data
, data_len
);
236 buffer_offset
-= data_len
+ sizeof(uint32_t);
237 assert(buffer_offset
>= 0);
238 if (buffer_offset
> 0) {
239 memmove(buffer
, data
+ data_len
, buffer_offset
);
240 data_len
= ntohl(*(uint32_t *)buffer
);
244 DPRINTF(Ethernet
, "EtherTap input len=%d\n", packet
->length
);
245 DDUMP(EthernetData
, packet
->data
, packet
->length
);
246 if (!interface
->sendPacket(packet
)) {
247 DPRINTF(Ethernet
, "bus busy...buffer for retransmission\n");
248 packetBuffer
.push(packet
);
249 if (!txEvent
.scheduled())
250 schedule(txEvent
, curTick() + retryTime
);
258 EtherTap::retransmit()
260 if (packetBuffer
.empty())
263 EthPacketPtr packet
= packetBuffer
.front();
264 if (interface
->sendPacket(packet
)) {
267 DPRINTF(Ethernet
, "EtherTap retransmit\n");
268 packetBuffer
.front() = NULL
;
272 if (!packetBuffer
.empty() && !txEvent
.scheduled())
273 schedule(txEvent
, curTick() + retryTime
);
277 EtherTap::getEthPort(const std::string
&if_name
, int idx
)
279 if (if_name
== "tap") {
280 if (interface
->getPeer())
281 panic("Interface already connected to\n");
288 //=====================================================================
291 EtherTap::serialize(ostream
&os
)
293 SERIALIZE_SCALAR(socket
);
294 SERIALIZE_SCALAR(buflen
);
295 uint8_t *buffer
= (uint8_t *)this->buffer
;
296 SERIALIZE_ARRAY(buffer
, buflen
);
297 SERIALIZE_SCALAR(buffer_offset
);
298 SERIALIZE_SCALAR(data_len
);
300 bool tapevent_present
= false;
302 tapevent_present
= true;
303 SERIALIZE_SCALAR(tapevent_present
);
304 event
->serialize(os
);
307 SERIALIZE_SCALAR(tapevent_present
);
312 EtherTap::unserialize(Checkpoint
*cp
, const std::string
§ion
)
314 UNSERIALIZE_SCALAR(socket
);
315 UNSERIALIZE_SCALAR(buflen
);
316 uint8_t *buffer
= (uint8_t *)this->buffer
;
317 UNSERIALIZE_ARRAY(buffer
, buflen
);
318 UNSERIALIZE_SCALAR(buffer_offset
);
319 UNSERIALIZE_SCALAR(data_len
);
321 bool tapevent_present
;
322 UNSERIALIZE_SCALAR(tapevent_present
);
323 if (tapevent_present
) {
324 event
= new TapEvent(this, socket
, POLLIN
|POLLERR
);
326 event
->unserialize(cp
,section
);
328 if (event
->queued()) {
329 pollQueue
.schedule(event
);
334 //=====================================================================
337 EtherTapParams::create()
339 return new EtherTap(this);