gem5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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/etherobject.hh"
45 #include "dev/net/etherpkt.hh"
46 
47 #if USE_TUNTAP
48 #include "params/EtherTap.hh"
49 
50 #endif
51 
52 #include "params/EtherTapStub.hh"
53 #include "sim/eventq.hh"
54 #include "sim/sim_object.hh"
55 
56 class TapEvent;
57 class EtherTapInt;
58 
59 class EtherTapBase : public EtherObject
60 {
61  public:
62  typedef EtherTapBaseParams Params;
63  EtherTapBase(const Params *p);
64  virtual ~EtherTapBase();
65 
66  const Params *
67  params() const
68  {
69  return dynamic_cast<const Params *>(_params);
70  }
71 
72  void serialize(CheckpointOut &cp) const override;
73  void unserialize(CheckpointIn &cp) override;
74 
75  protected:
76  uint8_t *buffer;
77  int buflen;
78 
80 
81 
82  /*
83  * Interface to the real network.
84  */
85  protected:
86  friend class TapEvent;
88  void pollFd(int fd);
89  void stopPolling();
90 
91  // Receive data from the real network.
92  virtual void recvReal(int revent) = 0;
93  // Prepare and send data out to the real network.
94  virtual bool sendReal(const void *data, size_t len) = 0;
95 
96 
97  /*
98  * Interface to the simulated network.
99  */
100  protected:
102 
103  public:
104  EtherInt *getEthPort(const std::string &if_name, int idx) 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();
112 
113  class TxEvent : public Event
114  {
115  protected:
117 
118  public:
119  TxEvent(EtherTapBase *_tap) : tap(_tap) {}
120  void process() { tap->retransmit(); }
121  virtual const char *description() const
122  { return "EtherTapBase retransmit"; }
123  };
124 
125  friend class TxEvent;
127 };
128 
129 class EtherTapInt : public EtherInt
130 {
131  private:
133  public:
134  EtherTapInt(const std::string &name, EtherTapBase *t) :
135  EtherInt(name), tap(t)
136  { }
137 
138  bool recvPacket(EthPacketPtr pkt) override
139  { return tap->recvSimulated(pkt); }
140  void sendDone() override {}
141 };
142 
143 
144 class TapListener;
145 
146 /*
147  * Interface to connect a simulated ethernet device to the real world. An
148  * external helper program bridges between this object's TCP port and a
149  * source/sink for Ethernet frames. Each frame going in either direction is
150  * prepended with the frame's length in a 32 bit integer in network byte order.
151  */
153 {
154  public:
155  typedef EtherTapStubParams Params;
156  EtherTapStub(const Params *p);
157  ~EtherTapStub();
158 
159  const Params *
160  params() const
161  {
162  return dynamic_cast<const Params *>(_params);
163  }
164 
165  void serialize(CheckpointOut &cp) const override;
166  void unserialize(CheckpointIn &cp) override;
167 
168 
169  protected:
170  friend class TapListener;
172 
173  int socket;
174 
175  void attach(int fd);
176  void detach();
177 
178  uint32_t buffer_used;
179  uint32_t frame_len;
180 
181  void recvReal(int revent) override;
182  bool sendReal(const void *data, size_t len) override;
183 };
184 
185 
186 #if USE_TUNTAP
187 class EtherTap : public EtherTapBase
188 {
189  public:
190  typedef EtherTapParams Params;
191  EtherTap(const Params *p);
192  ~EtherTap();
193 
194  const Params *
195  params() const
196  {
197  return dynamic_cast<const Params *>(_params);
198  }
199 
200 
201  protected:
202  int tap;
203 
204  void recvReal(int revent) override;
205  bool sendReal(const void *data, size_t len) override;
206 };
207 #endif
208 
209 
210 #endif // __DEV_NET_ETHERTAP_HH__
EtherTapStub(const Params *p)
Definition: ethertap.cc:281
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition: ethertap.cc:306
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition: ethertap.cc:101
const Params * params() const
Definition: ethertap.hh:160
uint32_t buffer_used
Definition: ethertap.hh:178
TxEvent txEvent
Definition: ethertap.hh:126
void sendSimulated(void *data, size_t len)
Definition: ethertap.cc:180
void retransmit()
Definition: ethertap.cc:201
TapEvent * event
Definition: ethertap.hh:87
void detach()
Definition: ethertap.cc:330
void sendDone() override
Definition: ethertap.hh:140
const char data[]
Definition: circlebuf.cc:43
virtual ~EtherTapBase()
Definition: ethertap.cc:93
uint32_t frame_len
Definition: ethertap.hh:179
EtherTapBase * tap
Definition: ethertap.hh:132
bool recvPacket(EthPacketPtr pkt) override
Definition: ethertap.hh:138
TxEvent(EtherTapBase *_tap)
Definition: ethertap.hh:119
bool recvSimulated(EthPacketPtr packet)
Definition: ethertap.cc:164
EtherDump * dump
Definition: ethertap.hh:79
EtherObjectParams Params
Definition: etherobject.hh:51
EtherTapBase * tap
Definition: ethertap.hh:116
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition: ethertap.cc:296
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition: ethertap.cc:118
virtual bool sendReal(const void *data, size_t len)=0
std::shared_ptr< EthPacketData > EthPacketPtr
Definition: etherpkt.hh:90
bool sendReal(const void *data, size_t len) override
Definition: ethertap.cc:385
EtherTapBase(const Params *p)
Definition: ethertap.cc:85
void stopPolling()
Definition: ethertap.cc:144
The base EtherObject class, allows for an accesor function to a simobj that returns the Port...
Definition: etherobject.hh:48
const Params * params() const
Definition: ethertap.hh:67
virtual void recvReal(int revent)=0
std::ostream CheckpointOut
Definition: serialize.hh:67
Definition: eventq.hh:185
EtherTapBaseParams Params
Definition: ethertap.hh:62
const SimObjectParams * _params
Cached copy of the object parameters.
Definition: sim_object.hh:107
Bitfield< 18, 16 > len
Definition: miscregs.hh:1626
EtherTapInt * interface
Definition: ethertap.hh:101
const std::string & name() const
Return port name (for DPRINTF).
Definition: etherint.hh:60
EtherInt * getEthPort(const std::string &if_name, int idx) override
Additional function to return the Port of a memory object.
Definition: ethertap.cc:153
void attach(int fd)
Definition: ethertap.cc:317
std::queue< EthPacketPtr > packetBuffer
Definition: ethertap.hh:110
EtherTapStubParams Params
Definition: ethertap.hh:155
Bitfield< 5 > t
Definition: miscregs.hh:1382
Bitfield< 14, 12 > fd
Definition: types.hh:155
void recvReal(int revent) override
Definition: ethertap.cc:339
Bitfield< 0 > p
uint8_t * buffer
Definition: ethertap.hh:76
virtual const char * description() const
Return a C string describing the event.
Definition: ethertap.hh:121
EtherTapInt(const std::string &name, EtherTapBase *t)
Definition: ethertap.hh:134
Base Ethernet Object declaration.
TapListener * listener
Definition: ethertap.hh:171
void pollFd(int fd)
Definition: ethertap.cc:136

Generated on Fri Jun 9 2017 13:03:46 for gem5 by doxygen 1.8.6