gem5  v20.1.0.0
ethertap.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2003-2005 The Regents of The University of Michigan
3  * All rights reserved.
4  *
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.
15  *
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.
27  */
28 
29 /* @file
30  * Interface to connect a simulated ethernet device to the real world
31  */
32 
33 #ifndef __DEV_NET_ETHERTAP_HH__
34 #define __DEV_NET_ETHERTAP_HH__
35 
36 #include <queue>
37 #include <string>
38 
39 #include "base/pollevent.hh"
40 #include "config/use_tuntap.hh"
41 #include "dev/net/etherint.hh"
42 #include "dev/net/etherpkt.hh"
43 
44 #if USE_TUNTAP
45 #include "params/EtherTap.hh"
46 
47 #endif
48 
49 #include "params/EtherTapStub.hh"
50 #include "sim/eventq.hh"
51 #include "sim/sim_object.hh"
52 
53 class TapEvent;
54 class EtherTapInt;
55 
56 class EtherTapBase : public SimObject
57 {
58  public:
59  typedef EtherTapBaseParams Params;
60  EtherTapBase(const Params *p);
61  virtual ~EtherTapBase();
62 
63  const Params *
64  params() const
65  {
66  return dynamic_cast<const Params *>(_params);
67  }
68 
69  void serialize(CheckpointOut &cp) const override;
70  void unserialize(CheckpointIn &cp) override;
71 
72  protected:
73  uint8_t *buffer;
74  int buflen;
75 
77 
78 
79  /*
80  * Interface to the real network.
81  */
82  protected:
83  friend class TapEvent;
85  void pollFd(int fd);
86  void stopPolling();
87 
88  // Receive data from the real network.
89  virtual void recvReal(int revent) = 0;
90  // Prepare and send data out to the real network.
91  virtual bool sendReal(const void *data, size_t len) = 0;
92 
93 
94  /*
95  * Interface to the simulated network.
96  */
97  protected:
99 
100  public:
101  Port &getPort(const std::string &if_name,
102  PortID idx=InvalidPortID) override;
103 
104  bool recvSimulated(EthPacketPtr packet);
105  void sendSimulated(void *data, size_t len);
106 
107  protected:
108  std::queue<EthPacketPtr> packetBuffer;
109  void retransmit();
111 };
112 
113 class EtherTapInt : public EtherInt
114 {
115  private:
117  public:
118  EtherTapInt(const std::string &name, EtherTapBase *t) :
119  EtherInt(name), tap(t)
120  { }
121 
122  bool recvPacket(EthPacketPtr pkt) override
123  { return tap->recvSimulated(pkt); }
124  void sendDone() override {}
125 };
126 
127 
128 class TapListener;
129 
130 /*
131  * Interface to connect a simulated ethernet device to the real world. An
132  * external helper program bridges between this object's TCP port and a
133  * source/sink for Ethernet frames. Each frame going in either direction is
134  * prepended with the frame's length in a 32 bit integer in network byte order.
135  */
137 {
138  public:
139  typedef EtherTapStubParams Params;
140  EtherTapStub(const Params *p);
141  ~EtherTapStub();
142 
143  const Params *
144  params() const
145  {
146  return dynamic_cast<const Params *>(_params);
147  }
148 
149  void serialize(CheckpointOut &cp) const override;
150  void unserialize(CheckpointIn &cp) override;
151 
152 
153  protected:
154  friend class TapListener;
156 
157  int socket;
158 
159  void attach(int fd);
160  void detach();
161 
162  uint32_t buffer_used;
163  uint32_t frame_len;
164 
165  void recvReal(int revent) override;
166  bool sendReal(const void *data, size_t len) override;
167 };
168 
169 
170 #if USE_TUNTAP
171 class EtherTap : public EtherTapBase
172 {
173  public:
174  typedef EtherTapParams Params;
175  EtherTap(const Params *p);
176  ~EtherTap();
177 
178  const Params *
179  params() const
180  {
181  return dynamic_cast<const Params *>(_params);
182  }
183 
184 
185  protected:
186  int tap;
187 
188  void recvReal(int revent) override;
189  bool sendReal(const void *data, size_t len) override;
190 };
191 #endif
192 
193 
194 #endif // __DEV_NET_ETHERTAP_HH__
EtherTapStub::recvReal
void recvReal(int revent) override
Definition: ethertap.cc:344
data
const char data[]
Definition: circlebuf.test.cc:42
EtherTapBase::sendReal
virtual bool sendReal(const void *data, size_t len)=0
EtherTapStub::params
const Params * params() const
Definition: ethertap.hh:144
EtherTapBase::stopPolling
void stopPolling()
Definition: ethertap.cc:152
InvalidPortID
const PortID InvalidPortID
Definition: types.hh:238
EtherTapBase::retransmit
void retransmit()
Definition: ethertap.cc:206
EtherInt
Definition: etherint.hh:47
ArmISA::fd
Bitfield< 14, 12 > fd
Definition: types.hh:159
EtherTapStub::EtherTapStub
EtherTapStub(const Params *p)
Definition: ethertap.cc:286
etherint.hh
EtherTapBase::sendSimulated
void sendSimulated(void *data, size_t len)
Definition: ethertap.cc:185
PortID
int16_t PortID
Port index/ID type, and a symbolic name for an invalid port id.
Definition: types.hh:237
EtherTapStub
Definition: ethertap.hh:136
EtherTapInt::tap
EtherTapBase * tap
Definition: ethertap.hh:116
EtherTapBase::recvReal
virtual void recvReal(int revent)=0
EtherTapBase::buflen
int buflen
Definition: ethertap.hh:74
EtherTapInt::sendDone
void sendDone() override
Definition: ethertap.hh:124
TapEvent
Definition: ethertap.cc:72
EtherTapStub::unserialize
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition: ethertap.cc:311
EtherTapBase::interface
EtherTapInt * interface
Definition: ethertap.hh:98
EtherTapStub::buffer_used
uint32_t buffer_used
Definition: ethertap.hh:162
EventFunctionWrapper
Definition: eventq.hh:1101
EtherTapInt::recvPacket
bool recvPacket(EthPacketPtr pkt) override
Definition: ethertap.hh:122
EtherTapStub::socket
int socket
Definition: ethertap.hh:157
cp
Definition: cprintf.cc:40
EtherTapStub::listener
TapListener * listener
Definition: ethertap.hh:155
EtherTapStub::Params
EtherTapStubParams Params
Definition: ethertap.hh:139
sim_object.hh
EtherTapInt
Definition: ethertap.hh:113
pollevent.hh
Port
Ports are used to interface objects to each other.
Definition: port.hh:56
EtherTapBase::unserialize
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition: ethertap.cc:126
EtherTapBase::serialize
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition: ethertap.cc:109
EtherTapBase::event
TapEvent * event
Definition: ethertap.hh:84
EtherTapBase::getPort
Port & getPort(const std::string &if_name, PortID idx=InvalidPortID) override
Get a port with a given name and index.
Definition: ethertap.cc:161
TapListener
Definition: ethertap.cc:225
EtherTapStub::attach
void attach(int fd)
Definition: ethertap.cc:322
EtherTapBase::EtherTapBase
EtherTapBase(const Params *p)
Definition: ethertap.cc:92
EthPacketPtr
std::shared_ptr< EthPacketData > EthPacketPtr
Definition: etherpkt.hh:87
EtherInt::name
const std::string & name() const
Return port name (for DPRINTF).
Definition: etherint.hh:59
ArmISA::t
Bitfield< 5 > t
Definition: miscregs_types.hh:67
SimObject::_params
const SimObjectParams * _params
Cached copy of the object parameters.
Definition: sim_object.hh:110
EtherTapBase::params
const Params * params() const
Definition: ethertap.hh:64
ArmISA::len
Bitfield< 18, 16 > len
Definition: miscregs_types.hh:439
EtherTapStub::frame_len
uint32_t frame_len
Definition: ethertap.hh:163
etherpkt.hh
EtherTapBase::Params
EtherTapBaseParams Params
Definition: ethertap.hh:59
CheckpointOut
std::ostream CheckpointOut
Definition: serialize.hh:63
EtherDump
Definition: etherdump.hh:45
EtherTapBase::~EtherTapBase
virtual ~EtherTapBase()
Definition: ethertap.cc:101
EtherTapBase::pollFd
void pollFd(int fd)
Definition: ethertap.cc:144
EtherTapBase::buffer
uint8_t * buffer
Definition: ethertap.hh:73
EtherTapStub::detach
void detach()
Definition: ethertap.cc:335
MipsISA::p
Bitfield< 0 > p
Definition: pra_constants.hh:323
CheckpointIn
Definition: serialize.hh:67
EtherTapBase::txEvent
EventFunctionWrapper txEvent
Definition: ethertap.hh:110
EtherTapStub::~EtherTapStub
~EtherTapStub()
Definition: ethertap.cc:295
EtherTapBase::packetBuffer
std::queue< EthPacketPtr > packetBuffer
Definition: ethertap.hh:108
EtherTapBase::recvSimulated
bool recvSimulated(EthPacketPtr packet)
Definition: ethertap.cc:169
EtherTapStub::serialize
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition: ethertap.cc:301
EtherTapBase
Definition: ethertap.hh:56
EtherTapBase::dump
EtherDump * dump
Definition: ethertap.hh:76
EtherTapInt::EtherTapInt
EtherTapInt(const std::string &name, EtherTapBase *t)
Definition: ethertap.hh:118
EtherTapStub::sendReal
bool sendReal(const void *data, size_t len) override
Definition: ethertap.cc:390
eventq.hh
SimObject
Abstract superclass for simulation objects.
Definition: sim_object.hh:92

Generated on Wed Sep 30 2020 14:02:11 for gem5 by doxygen 1.8.17