gem5  v22.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/have_tuntap.hh"
41 #include "dev/net/etherint.hh"
42 #include "dev/net/etherpkt.hh"
43 
44 #if HAVE_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 namespace gem5
54 {
55 
56 class TapEvent;
57 class EtherTapInt;
58 
59 class EtherTapBase : public SimObject
60 {
61  public:
62  using Params = EtherTapBaseParams;
63  EtherTapBase(const Params &p);
64  virtual ~EtherTapBase();
65 
66  void serialize(CheckpointOut &cp) const override;
67  void unserialize(CheckpointIn &cp) override;
68 
69  protected:
70  uint8_t *buffer;
71  int buflen;
72 
74 
75 
76  /*
77  * Interface to the real network.
78  */
79  protected:
80  friend class TapEvent;
82  void pollFd(int fd);
83  void stopPolling();
84 
85  // Receive data from the real network.
86  virtual void recvReal(int revent) = 0;
87  // Prepare and send data out to the real network.
88  virtual bool sendReal(const void *data, size_t len) = 0;
89 
90 
91  /*
92  * Interface to the simulated network.
93  */
94  protected:
96 
97  public:
98  Port &getPort(const std::string &if_name,
99  PortID idx=InvalidPortID) override;
100 
101  bool recvSimulated(EthPacketPtr packet);
102  void sendSimulated(void *data, size_t len);
103 
104  protected:
105  std::queue<EthPacketPtr> packetBuffer;
106  void retransmit();
108 };
109 
110 class EtherTapInt : public EtherInt
111 {
112  private:
114  public:
115  EtherTapInt(const std::string &name, EtherTapBase *t) :
116  EtherInt(name), tap(t)
117  { }
118 
119  bool recvPacket(EthPacketPtr pkt) override
120  { return tap->recvSimulated(pkt); }
121  void sendDone() override {}
122 };
123 
124 
125 class TapListener;
126 
127 /*
128  * Interface to connect a simulated ethernet device to the real world. An
129  * external helper program bridges between this object's TCP port and a
130  * source/sink for Ethernet frames. Each frame going in either direction is
131  * prepended with the frame's length in a 32 bit integer in network byte order.
132  */
134 {
135  public:
136  using Params = EtherTapStubParams;
137  EtherTapStub(const Params &p);
138  ~EtherTapStub();
139 
140  void serialize(CheckpointOut &cp) const override;
141  void unserialize(CheckpointIn &cp) override;
142 
143 
144  protected:
145  friend class TapListener;
147 
148  int socket;
149 
150  void attach(int fd);
151  void detach();
152 
153  uint32_t buffer_used;
154  uint32_t frame_len;
155 
156  void recvReal(int revent) override;
157  bool sendReal(const void *data, size_t len) override;
158 };
159 
160 
161 #if HAVE_TUNTAP
162 class EtherTap : public EtherTapBase
163 {
164  public:
165  using Params = EtherTapParams;
166  EtherTap(const Params &p);
167  ~EtherTap();
168 
169 
170  protected:
171  int tap;
172 
173  void recvReal(int revent) override;
174  bool sendReal(const void *data, size_t len) override;
175 };
176 #endif
177 
178 } // namespace gem5
179 
180 #endif // __DEV_NET_ETHERTAP_HH__
const char data[]
const std::string & name() const
Return port name (for DPRINTF).
Definition: etherint.hh:62
Port & getPort(const std::string &if_name, PortID idx=InvalidPortID) override
Get a port with a given name and index.
Definition: ethertap.cc:164
void sendSimulated(void *data, size_t len)
Definition: ethertap.cc:188
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition: ethertap.cc:129
TapEvent * event
Definition: ethertap.hh:81
virtual ~EtherTapBase()
Definition: ethertap.cc:104
EtherTapBaseParams Params
Definition: ethertap.hh:62
virtual void recvReal(int revent)=0
void pollFd(int fd)
Definition: ethertap.cc:147
EtherTapInt * interface
Definition: ethertap.hh:95
uint8_t * buffer
Definition: ethertap.hh:70
bool recvSimulated(EthPacketPtr packet)
Definition: ethertap.cc:172
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition: ethertap.cc:112
std::queue< EthPacketPtr > packetBuffer
Definition: ethertap.hh:105
virtual bool sendReal(const void *data, size_t len)=0
EventFunctionWrapper txEvent
Definition: ethertap.hh:107
EtherTapBase(const Params &p)
Definition: ethertap.cc:95
EtherDump * dump
Definition: ethertap.hh:73
bool recvPacket(EthPacketPtr pkt) override
Definition: ethertap.hh:119
void sendDone() override
Definition: ethertap.hh:121
EtherTapInt(const std::string &name, EtherTapBase *t)
Definition: ethertap.hh:115
EtherTapBase * tap
Definition: ethertap.hh:113
EtherTapStub(const Params &p)
Definition: ethertap.cc:289
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition: ethertap.cc:304
void attach(int fd)
Definition: ethertap.cc:325
void recvReal(int revent) override
Definition: ethertap.cc:347
uint32_t buffer_used
Definition: ethertap.hh:153
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition: ethertap.cc:314
uint32_t frame_len
Definition: ethertap.hh:154
bool sendReal(const void *data, size_t len) override
Definition: ethertap.cc:393
TapListener * listener
Definition: ethertap.hh:146
Ports are used to interface objects to each other.
Definition: port.hh:62
Abstract superclass for simulation objects.
Definition: sim_object.hh:148
uint16_t len
Definition: helpers.cc:62
Bitfield< 14, 12 > fd
Definition: types.hh:150
Bitfield< 51 > t
Definition: pagetable.hh:56
Bitfield< 54 > p
Definition: pagetable.hh:70
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
const PortID InvalidPortID
Definition: types.hh:246
std::ostream CheckpointOut
Definition: serialize.hh:66
int16_t PortID
Port index/ID type, and a symbolic name for an invalid port id.
Definition: types.hh:245
std::shared_ptr< EthPacketData > EthPacketPtr
Definition: etherpkt.hh:90

Generated on Wed Dec 21 2022 10:22:34 for gem5 by doxygen 1.9.1