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 #include "dev/net/ethertap.hh"
37 #if defined(__OpenBSD__) || defined(__APPLE__)
38 #include <sys/param.h>
41 #include <netinet/in.h>
47 #include "base/misc.hh"
48 #include "base/pollevent.hh"
49 #include "base/socket.hh"
50 #include "base/trace.hh"
51 #include "debug/Ethernet.hh"
52 #include "debug/EthernetData.hh"
53 #include "dev/net/etherdump.hh"
54 #include "dev/net/etherint.hh"
55 #include "dev/net/etherpkt.hh"
66 class Event
: public PollEvent
69 TapListener
*listener
;
72 Event(TapListener
*l
, int fd
, int e
)
73 : PollEvent(fd
, e
), listener(l
) {}
75 virtual void process(int revent
) { listener
->accept(); }
82 ListenSocket listener
;
87 TapListener(EtherTap
*t
, int p
)
88 : event(NULL
), tap(t
), port(p
) {}
89 ~TapListener() { if (event
) delete event
; }
98 while (!listener
.listen(port
, true)) {
99 DPRINTF(Ethernet
, "TapListener(listen): Can't bind port %d\n", port
);
103 ccprintf(cerr
, "Listening for tap connection on port %d\n", port
);
104 event
= new Event(this, listener
.getfd(), POLLIN
|POLLERR
);
105 pollQueue
.schedule(event
);
109 TapListener::accept()
111 // As a consequence of being called from the PollQueue, we might
112 // have been called from a different thread. Migrate to "our"
114 EventQueue::ScopedMigration
migrate(tap
->eventQueue());
116 if (!listener
.islistening())
117 panic("TapListener(accept): cannot accept if we're not listening!");
119 int sfd
= listener
.accept(true);
126 class TapEvent
: public PollEvent
132 TapEvent(EtherTap
*_tap
, int fd
, int e
)
133 : PollEvent(fd
, e
), tap(_tap
) {}
134 virtual void process(int revent
) { tap
->process(revent
); }
137 EtherTap::EtherTap(const Params
*p
)
138 : EtherObject(p
), event(NULL
), socket(-1), buflen(p
->bufsz
), dump(p
->dump
),
139 interface(NULL
), txEvent(this)
141 if (ListenSocket::allDisabled())
142 fatal("All listeners are disabled! EtherTap can't work!");
144 buffer
= new char[buflen
];
145 listener
= new TapListener(this, p
->port
);
147 interface
= new EtherTapInt(name() + ".interface", this);
150 EtherTap::~EtherTap()
162 EtherTap::attach(int fd
)
170 DPRINTF(Ethernet
, "EtherTap attached\n");
171 event
= new TapEvent(this, socket
, POLLIN
|POLLERR
);
172 pollQueue
.schedule(event
);
178 DPRINTF(Ethernet
, "EtherTap detached\n");
186 EtherTap::recvPacket(EthPacketPtr packet
)
191 DPRINTF(Ethernet
, "EtherTap output len=%d\n", packet
->length
);
192 DDUMP(EthernetData
, packet
->data
, packet
->length
);
193 uint32_t len
= htonl(packet
->length
);
194 ssize_t ret
= write(socket
, &len
, sizeof(len
));
195 if (ret
!= sizeof(len
))
197 ret
= write(socket
, packet
->data
, packet
->length
);
198 if (ret
!= packet
->length
)
201 interface
->recvDone();
211 EtherTap::process(int revent
)
213 if (revent
& POLLERR
) {
218 char *data
= buffer
+ sizeof(uint32_t);
219 if (!(revent
& POLLIN
))
222 if (buffer_offset
< data_len
+ sizeof(uint32_t)) {
223 int len
= read(socket
, buffer
+ buffer_offset
, buflen
- buffer_offset
);
229 buffer_offset
+= len
;
232 data_len
= ntohl(*(uint32_t *)buffer
);
234 DPRINTF(Ethernet
, "Received data from peer: len=%d buffer_offset=%d "
235 "data_len=%d\n", len
, buffer_offset
, data_len
);
238 while (data_len
!= 0 && buffer_offset
>= data_len
+ sizeof(uint32_t)) {
240 packet
= make_shared
<EthPacketData
>(data_len
);
241 packet
->length
= data_len
;
242 packet
->simLength
= data_len
;
243 memcpy(packet
->data
, data
, data_len
);
245 assert(buffer_offset
>= data_len
+ sizeof(uint32_t));
246 buffer_offset
-= data_len
+ sizeof(uint32_t);
247 if (buffer_offset
> 0) {
248 memmove(buffer
, data
+ data_len
, buffer_offset
);
249 data_len
= ntohl(*(uint32_t *)buffer
);
253 DPRINTF(Ethernet
, "EtherTap input len=%d\n", packet
->length
);
254 DDUMP(EthernetData
, packet
->data
, packet
->length
);
255 if (!interface
->sendPacket(packet
)) {
256 DPRINTF(Ethernet
, "bus busy...buffer for retransmission\n");
257 packetBuffer
.push(packet
);
258 if (!txEvent
.scheduled())
259 schedule(txEvent
, curTick() + retryTime
);
267 EtherTap::retransmit()
269 if (packetBuffer
.empty())
272 EthPacketPtr packet
= packetBuffer
.front();
273 if (interface
->sendPacket(packet
)) {
276 DPRINTF(Ethernet
, "EtherTap retransmit\n");
277 packetBuffer
.front() = NULL
;
281 if (!packetBuffer
.empty() && !txEvent
.scheduled())
282 schedule(txEvent
, curTick() + retryTime
);
286 EtherTap::getEthPort(const std::string
&if_name
, int idx
)
288 if (if_name
== "tap") {
289 if (interface
->getPeer())
290 panic("Interface already connected to\n");
297 //=====================================================================
300 EtherTap::serialize(CheckpointOut
&cp
) const
302 SERIALIZE_SCALAR(socket
);
303 SERIALIZE_SCALAR(buflen
);
304 uint8_t *buffer
= (uint8_t *)this->buffer
;
305 SERIALIZE_ARRAY(buffer
, buflen
);
306 SERIALIZE_SCALAR(buffer_offset
);
307 SERIALIZE_SCALAR(data_len
);
309 bool tapevent_present
= false;
311 tapevent_present
= true;
312 SERIALIZE_SCALAR(tapevent_present
);
313 event
->serialize(cp
);
316 SERIALIZE_SCALAR(tapevent_present
);
321 EtherTap::unserialize(CheckpointIn
&cp
)
323 UNSERIALIZE_SCALAR(socket
);
324 UNSERIALIZE_SCALAR(buflen
);
325 uint8_t *buffer
= (uint8_t *)this->buffer
;
326 UNSERIALIZE_ARRAY(buffer
, buflen
);
327 UNSERIALIZE_SCALAR(buffer_offset
);
328 UNSERIALIZE_SCALAR(data_len
);
330 bool tapevent_present
;
331 UNSERIALIZE_SCALAR(tapevent_present
);
332 if (tapevent_present
) {
333 event
= new TapEvent(this, socket
, POLLIN
|POLLERR
);
335 event
->unserialize(cp
);
337 if (event
->queued()) {
338 pollQueue
.schedule(event
);
343 //=====================================================================
346 EtherTapParams::create()
348 return new EtherTap(this);