gem5  v20.1.0.0
etherswitch.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 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  * Device model for an ethernet switch
31  */
32 
33 #ifndef __DEV_ETHERSWITCH_HH__
34 #define __DEV_ETHERSWITCH_HH__
35 
36 #include <map>
37 #include <set>
38 
39 #include "base/inet.hh"
40 #include "dev/net/etherint.hh"
41 #include "dev/net/etherlink.hh"
42 #include "dev/net/etherpkt.hh"
43 #include "dev/net/pktfifo.hh"
44 #include "params/EtherSwitch.hh"
45 #include "sim/eventq.hh"
46 #include "sim/sim_object.hh"
47 
48 class EtherSwitch : public SimObject
49 {
50  public:
51  typedef EtherSwitchParams Params;
52 
53  EtherSwitch(const Params *p);
54  ~EtherSwitch();
55 
56  const Params * params() const
57  {
58  return dynamic_cast<const Params*>(_params);
59  }
60 
61  Port &getPort(const std::string &if_name,
62  PortID idx=InvalidPortID) override;
63 
64  protected:
68  class Interface : public EtherInt, public Serializable
69  {
70  public:
71  Interface(const std::string &name, EtherSwitch *_etherSwitch,
72  uint64_t outputBufferSize, Tick delay, Tick delay_var,
73  double rate, unsigned id);
78  bool recvPacket(EthPacketPtr packet);
82  void enqueue(EthPacketPtr packet, unsigned senderId);
83  void sendDone() {}
85 
87  void learnSenderAddr(Net::EthAddr srcMacAddr, Interface *sender);
88 
89  void serialize(CheckpointOut &cp) const;
91 
92  private:
93  const double ticksPerByte;
95  const Tick delayVar;
96  const unsigned interfaceId;
97 
99  protected:
100  struct PortFifoEntry : public Serializable
101  {
102  PortFifoEntry(EthPacketPtr pkt, Tick recv_tick, unsigned id)
103  : packet(pkt), recvTick(recv_tick), srcId(id) {}
104 
107  // id of the port that the packet has been received from
108  unsigned srcId;
110  {
111  packet = nullptr;
112  recvTick = 0;
113  srcId = 0;
114  }
115  void serialize(CheckpointOut &cp) const;
116  void unserialize(CheckpointIn &cp);
117  };
118 
119  class PortFifo : public Serializable
120  {
121  protected:
122  struct EntryOrder {
123  bool operator() (const PortFifoEntry& lhs,
124  const PortFifoEntry& rhs) const
125  {
126  if (lhs.recvTick == rhs.recvTick)
127  return lhs.srcId < rhs.srcId;
128  else
129  return lhs.recvTick < rhs.recvTick;
130  }
131  };
132  std::set<PortFifoEntry, EntryOrder> fifo;
133 
134  const std::string objName;
135  const unsigned _maxsize;
136  unsigned _size;
137 
138  public:
139  PortFifo(const std::string &name, int max)
140  :objName(name), _maxsize(max), _size(0) {}
142 
143  const std::string name() { return objName; }
144  // Returns the available capacity of the fifo.
145  // It can return a negative value because in "push" function
146  // we first push the received packet into the fifo and then
147  // check if we exceed the available capacity (if avail() < 0)
148  // and remove packets from the end of fifo
149  int avail() const { return _maxsize - _size; }
150 
151  EthPacketPtr front() { return fifo.begin()->packet; }
152  bool empty() const { return _size == 0; }
153  unsigned size() const { return _size; }
154 
159  bool push(EthPacketPtr ptr, unsigned senderId);
160  void pop();
161  void clear();
165  void serialize(CheckpointOut &cp) const;
166  void unserialize(CheckpointIn &cp);
167  };
172  void transmit();
174  };
175 
179  };
180 
181  private:
182  // time to live for MAC address mappings
183  const double ttl;
184  // all interfaces of the switch
186  // table that maps MAC address to interfaces
187  std::map<uint64_t, SwitchTableEntry> forwardingTable;
188 
189  void serialize(CheckpointOut &cp) const override;
190  void unserialize(CheckpointIn &cp) override;
191 };
192 
193 #endif // __DEV_ETHERSWITCH_HH__
EtherSwitch::Interface::PortFifo
Definition: etherswitch.hh:119
EtherSwitch::Interface::PortFifo::_size
unsigned _size
Definition: etherswitch.hh:136
EtherSwitch::Interface::sendDone
void sendDone()
Definition: etherswitch.hh:83
EtherSwitch::Interface::PortFifoEntry::serialize
void serialize(CheckpointOut &cp) const
Serialize an object.
Definition: etherswitch.cc:301
EtherSwitch::Interface::PortFifo::fifo
std::set< PortFifoEntry, EntryOrder > fifo
Definition: etherswitch.hh:132
EtherSwitch::getPort
Port & getPort(const std::string &if_name, PortID idx=InvalidPortID) override
Get a port with a given name and index.
Definition: etherswitch.cc:63
EtherSwitch::interfaces
std::vector< Interface * > interfaces
Definition: etherswitch.hh:185
EtherSwitch::Interface::serialize
void serialize(CheckpointOut &cp) const
Serialize an object.
Definition: etherswitch.cc:274
InvalidPortID
const PortID InvalidPortID
Definition: types.hh:238
EtherSwitch::Interface::parent
EtherSwitch * parent
Definition: etherswitch.hh:98
EtherSwitch::SwitchTableEntry
Definition: etherswitch.hh:176
EtherSwitch::Interface::ticksPerByte
const double ticksPerByte
Definition: etherswitch.hh:93
EtherInt
Definition: etherint.hh:47
Serializable
Basic support for object serialization.
Definition: serialize.hh:172
EtherSwitch
Definition: etherswitch.hh:48
EtherSwitch::Interface::PortFifo::serialize
void serialize(CheckpointOut &cp) const
Serialization stuff.
Definition: etherswitch.cc:318
Net::EthAddr
Definition: inet.hh:72
EtherSwitch::Interface::PortFifo::_maxsize
const unsigned _maxsize
Definition: etherswitch.hh:135
EtherSwitch::Interface::PortFifo::size
unsigned size() const
Definition: etherswitch.hh:153
etherint.hh
EtherSwitch::Interface::PortFifoEntry::srcId
unsigned srcId
Definition: etherswitch.hh:108
Tick
uint64_t Tick
Tick count type.
Definition: types.hh:63
EtherSwitch::Interface::PortFifo::name
const std::string name()
Definition: etherswitch.hh:143
EtherSwitch::Interface
Model for an Ethernet switch port.
Definition: etherswitch.hh:68
PortID
int16_t PortID
Port index/ID type, and a symbolic name for an invalid port id.
Definition: types.hh:237
EtherSwitch::Interface::PortFifoEntry::PortFifoEntry
PortFifoEntry(EthPacketPtr pkt, Tick recv_tick, unsigned id)
Definition: etherswitch.hh:102
std::vector
STL vector class.
Definition: stl.hh:37
EtherSwitch::forwardingTable
std::map< uint64_t, SwitchTableEntry > forwardingTable
Definition: etherswitch.hh:187
EtherSwitch::Interface::PortFifo::push
bool push(EthPacketPtr ptr, unsigned senderId)
Push a packet into the fifo and sort the packets with same recv tick by port id.
Definition: etherswitch.cc:74
EtherSwitch::Interface::transmit
void transmit()
Definition: etherswitch.cc:179
EventFunctionWrapper
Definition: eventq.hh:1101
EtherSwitch::SwitchTableEntry::lastUseTime
Tick lastUseTime
Definition: etherswitch.hh:178
EtherSwitch::Interface::PortFifoEntry::unserialize
void unserialize(CheckpointIn &cp)
Unserialize an object.
Definition: etherswitch.cc:309
EtherSwitch::Interface::PortFifoEntry::~PortFifoEntry
~PortFifoEntry()
Definition: etherswitch.hh:109
EtherSwitch::unserialize
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition: etherswitch.cc:266
EtherSwitch::Interface::PortFifo::~PortFifo
~PortFifo()
Definition: etherswitch.hh:141
cp
Definition: cprintf.cc:40
EtherSwitch::Params
EtherSwitchParams Params
Definition: etherswitch.hh:51
EtherSwitch::Interface::PortFifo::EntryOrder
Definition: etherswitch.hh:122
EtherSwitch::SwitchTableEntry::interface
Interface * interface
Definition: etherswitch.hh:177
EtherSwitch::Interface::learnSenderAddr
void learnSenderAddr(Net::EthAddr srcMacAddr, Interface *sender)
Definition: etherswitch.cc:235
sim_object.hh
EtherSwitch::Interface::PortFifo::pop
void pop()
Definition: etherswitch.cc:106
EtherSwitch::Interface::PortFifo::EntryOrder::operator()
bool operator()(const PortFifoEntry &lhs, const PortFifoEntry &rhs) const
Definition: etherswitch.hh:123
EtherSwitch::EtherSwitch
EtherSwitch(const Params *p)
Definition: etherswitch.cc:42
EtherSwitch::Interface::PortFifo::empty
bool empty() const
Definition: etherswitch.hh:152
Port
Ports are used to interface objects to each other.
Definition: port.hh:56
EtherSwitch::Interface::enqueue
void enqueue(EthPacketPtr packet, unsigned senderId)
enqueue packet to the outputFifo
Definition: etherswitch.cc:163
EtherSwitch::Interface::PortFifo::clear
void clear()
Definition: etherswitch.cc:118
Port::id
const PortID id
A numeric identifier to distinguish ports in a vector, and set to InvalidPortID in case this port is ...
Definition: port.hh:74
EtherSwitch::Interface::PortFifoEntry::packet
EthPacketPtr packet
Definition: etherswitch.hh:105
EtherSwitch::Interface::PortFifo::objName
const std::string objName
Definition: etherswitch.hh:134
EtherSwitch::Interface::outputFifo
PortFifo outputFifo
output fifo at each interface
Definition: etherswitch.hh:171
EthPacketPtr
std::shared_ptr< EthPacketData > EthPacketPtr
Definition: etherpkt.hh:87
EtherSwitch::Interface::Interface
Interface(const std::string &name, EtherSwitch *_etherSwitch, uint64_t outputBufferSize, Tick delay, Tick delay_var, double rate, unsigned id)
Definition: etherswitch.cc:124
EtherInt::name
const std::string & name() const
Return port name (for DPRINTF).
Definition: etherint.hh:59
EtherSwitch::Interface::PortFifo::PortFifo
PortFifo(const std::string &name, int max)
Definition: etherswitch.hh:139
EtherSwitch::~EtherSwitch
~EtherSwitch()
Definition: etherswitch.cc:54
EtherSwitch::Interface::PortFifo::avail
int avail() const
Definition: etherswitch.hh:149
SimObject::_params
const SimObjectParams * _params
Cached copy of the object parameters.
Definition: sim_object.hh:110
EtherSwitch::Interface::PortFifo::front
EthPacketPtr front()
Definition: etherswitch.hh:151
EtherSwitch::Interface::PortFifoEntry
Definition: etherswitch.hh:100
EtherSwitch::Interface::switchingDelay
Tick switchingDelay()
Definition: etherswitch.cc:200
EtherSwitch::Interface::lookupDestPort
Interface * lookupDestPort(Net::EthAddr destAddr)
Definition: etherswitch.cc:211
pktfifo.hh
etherpkt.hh
EtherSwitch::Interface::recvPacket
bool recvPacket(EthPacketPtr packet)
When a packet is received from a device, route it through an (several) output queue(s)
Definition: etherswitch.cc:136
EtherSwitch::Interface::delayVar
const Tick delayVar
Definition: etherswitch.hh:95
CheckpointOut
std::ostream CheckpointOut
Definition: serialize.hh:63
EtherSwitch::Interface::txEvent
EventFunctionWrapper txEvent
Definition: etherswitch.hh:173
EtherSwitch::serialize
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition: etherswitch.cc:258
EtherSwitch::Interface::PortFifo::unserialize
void unserialize(CheckpointIn &cp)
Unserialize an object.
Definition: etherswitch.cc:331
EtherSwitch::params
const Params * params() const
Definition: etherswitch.hh:56
EtherSwitch::Interface::interfaceId
const unsigned interfaceId
Definition: etherswitch.hh:96
MipsISA::p
Bitfield< 0 > p
Definition: pra_constants.hh:323
inet.hh
CheckpointIn
Definition: serialize.hh:67
EtherSwitch::Interface::unserialize
void unserialize(CheckpointIn &cp)
Unserialize an object.
Definition: etherswitch.cc:287
EtherSwitch::Interface::PortFifoEntry::recvTick
Tick recvTick
Definition: etherswitch.hh:106
EtherSwitch::ttl
const double ttl
Definition: etherswitch.hh:183
EtherSwitch::Interface::switchDelay
const Tick switchDelay
Definition: etherswitch.hh:94
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