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

Generated on Fri Feb 28 2020 16:27:01 for gem5 by doxygen 1.8.13