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"
59 class TapEvent
: public PollEvent
65 TapEvent(EtherTapBase
*_tap
, int fd
, int e
)
66 : PollEvent(fd
, e
), tap(_tap
) {}
67 virtual void process(int revent
) { tap
->recvReal(revent
); }
70 EtherTapBase::EtherTapBase(const Params
*p
)
71 : EtherObject(p
), buflen(p
->bufsz
), dump(p
->dump
), event(NULL
),
72 interface(NULL
), txEvent(this)
74 buffer
= new uint8_t[buflen
];
75 interface
= new EtherTapInt(name() + ".interface", this);
78 EtherTapBase::~EtherTapBase()
86 EtherTapBase::serialize(CheckpointOut
&cp
) const
88 SERIALIZE_SCALAR(buflen
);
89 uint8_t *buffer
= (uint8_t *)this->buffer
;
90 SERIALIZE_ARRAY(buffer
, buflen
);
92 bool tapevent_present
= false;
94 tapevent_present
= true;
95 SERIALIZE_SCALAR(tapevent_present
);
98 SERIALIZE_SCALAR(tapevent_present
);
103 EtherTapBase::unserialize(CheckpointIn
&cp
)
105 UNSERIALIZE_SCALAR(buflen
);
106 uint8_t *buffer
= (uint8_t *)this->buffer
;
107 UNSERIALIZE_ARRAY(buffer
, buflen
);
109 bool tapevent_present
;
110 UNSERIALIZE_SCALAR(tapevent_present
);
111 if (tapevent_present
) {
112 event
= new TapEvent(this, 0, 0);
113 event
->unserialize(cp
);
115 pollQueue
.schedule(event
);
121 EtherTapBase::pollFd(int fd
)
124 event
= new TapEvent(this, fd
, POLLIN
|POLLERR
);
125 pollQueue
.schedule(event
);
129 EtherTapBase::stopPolling()
138 EtherTapBase::getEthPort(const std::string
&if_name
, int idx
)
140 if (if_name
== "tap") {
141 if (interface
->getPeer())
142 panic("Interface already connected to\n");
149 EtherTapBase::recvSimulated(EthPacketPtr packet
)
154 DPRINTF(Ethernet
, "EtherTap sim->real len=%d\n", packet
->length
);
155 DDUMP(EthernetData
, packet
->data
, packet
->length
);
157 bool success
= sendReal(packet
->data
, packet
->length
);
159 interface
->recvDone();
165 EtherTapBase::sendSimulated(void *data
, size_t len
)
168 packet
= make_shared
<EthPacketData
>(len
);
169 packet
->length
= len
;
170 packet
->simLength
= len
;
171 memcpy(packet
->data
, data
, len
);
173 DPRINTF(Ethernet
, "EtherTap real->sim len=%d\n", packet
->length
);
174 DDUMP(EthernetData
, packet
->data
, packet
->length
);
175 if (!packetBuffer
.empty() || !interface
->sendPacket(packet
)) {
176 DPRINTF(Ethernet
, "bus busy...buffer for retransmission\n");
177 packetBuffer
.push(packet
);
178 if (!txEvent
.scheduled())
179 schedule(txEvent
, curTick() + retryTime
);
186 EtherTapBase::retransmit()
188 if (packetBuffer
.empty())
191 EthPacketPtr packet
= packetBuffer
.front();
192 if (interface
->sendPacket(packet
)) {
195 DPRINTF(Ethernet
, "EtherTap retransmit\n");
196 packetBuffer
.front() = NULL
;
200 if (!packetBuffer
.empty() && !txEvent
.scheduled())
201 schedule(txEvent
, curTick() + retryTime
);
208 class Event
: public PollEvent
211 TapListener
*listener
;
214 Event(TapListener
*l
, int fd
, int e
) : PollEvent(fd
, e
), listener(l
) {}
216 void process(int revent
) override
{ listener
->accept(); }
225 ListenSocket listener
;
230 TapListener(EtherTapStub
*t
, int p
) : event(NULL
), tap(t
), port(p
) {}
231 ~TapListener() { delete event
; }
237 TapListener::listen()
239 while (!listener
.listen(port
, true)) {
240 DPRINTF(Ethernet
, "TapListener(listen): Can't bind port %d\n", port
);
244 ccprintf(cerr
, "Listening for tap connection on port %d\n", port
);
245 event
= new Event(this, listener
.getfd(), POLLIN
|POLLERR
);
246 pollQueue
.schedule(event
);
250 TapListener::accept()
252 // As a consequence of being called from the PollQueue, we might
253 // have been called from a different thread. Migrate to "our"
255 EventQueue::ScopedMigration
migrate(tap
->eventQueue());
257 if (!listener
.islistening())
258 panic("TapListener(accept): cannot accept if we're not listening!");
260 int sfd
= listener
.accept(true);
266 EtherTapStub::EtherTapStub(const Params
*p
) : EtherTapBase(p
), socket(-1)
268 if (ListenSocket::allDisabled())
269 fatal("All listeners are disabled! EtherTapStub can't work!");
271 listener
= new TapListener(this, p
->port
);
275 EtherTapStub::~EtherTapStub()
281 EtherTapStub::serialize(CheckpointOut
&cp
) const
283 EtherTapBase::serialize(cp
);
285 SERIALIZE_SCALAR(socket
);
286 SERIALIZE_SCALAR(buffer_used
);
287 SERIALIZE_SCALAR(frame_len
);
291 EtherTapStub::unserialize(CheckpointIn
&cp
)
293 EtherTapBase::unserialize(cp
);
295 UNSERIALIZE_SCALAR(socket
);
296 UNSERIALIZE_SCALAR(buffer_used
);
297 UNSERIALIZE_SCALAR(frame_len
);
302 EtherTapStub::attach(int fd
)
310 DPRINTF(Ethernet
, "EtherTapStub attached\n");
315 EtherTapStub::detach()
317 DPRINTF(Ethernet
, "EtherTapStub detached\n");
324 EtherTapStub::recvReal(int revent
)
326 if (revent
& POLLERR
) {
331 if (!(revent
& POLLIN
))
334 // Read in as much of the new data as we can.
335 int len
= read(socket
, buffer
+ buffer_used
, buflen
- buffer_used
);
342 // If there's not enough data for the frame length, wait for more.
343 if (buffer_used
< sizeof(uint32_t))
347 frame_len
= ntohl(*(uint32_t *)buffer
);
349 DPRINTF(Ethernet
, "Received data from peer: len=%d buffer_used=%d "
350 "frame_len=%d\n", len
, buffer_used
, frame_len
);
352 uint8_t *frame_start
= &buffer
[sizeof(uint32_t)];
353 while (frame_len
!= 0 && buffer_used
>= frame_len
+ sizeof(uint32_t)) {
354 sendSimulated(frame_start
, frame_len
);
357 buffer_used
-= frame_len
+ sizeof(uint32_t);
358 if (buffer_used
> 0) {
359 // If there's still any data left, move it into position.
360 memmove(buffer
, frame_start
+ frame_len
, buffer_used
);
364 if (buffer_used
>= sizeof(uint32_t))
365 frame_len
= ntohl(*(uint32_t *)buffer
);
370 EtherTapStub::sendReal(const void *data
, size_t len
)
372 uint32_t frame_len
= htonl(len
);
373 ssize_t ret
= write(socket
, &frame_len
, sizeof(frame_len
));
374 if (ret
!= sizeof(frame_len
))
376 return write(socket
, data
, len
) == len
;
381 EtherTapStubParams::create()
383 return new EtherTapStub(this);