gem5  v21.1.0.2
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 #include <string>
39 #include <vector>
40 
41 #include "base/inet.hh"
42 #include "dev/net/etherint.hh"
43 #include "dev/net/etherlink.hh"
44 #include "dev/net/etherpkt.hh"
45 #include "dev/net/pktfifo.hh"
46 #include "params/EtherSwitch.hh"
47 #include "sim/eventq.hh"
48 #include "sim/serialize.hh"
49 #include "sim/sim_object.hh"
50 
51 namespace gem5
52 {
53 
54 class EtherSwitch : public SimObject
55 {
56  public:
57  using Params = EtherSwitchParams;
58 
59  EtherSwitch(const Params &p);
60  ~EtherSwitch();
61 
62  Port &getPort(const std::string &if_name,
63  PortID idx=InvalidPortID) override;
64 
65  protected:
69  class Interface : public EtherInt, public Serializable
70  {
71  public:
72  Interface(const std::string &name, EtherSwitch *_etherSwitch,
73  uint64_t outputBufferSize, Tick delay, Tick delay_var,
74  double rate, unsigned id);
79  bool recvPacket(EthPacketPtr packet);
83  void enqueue(EthPacketPtr packet, unsigned senderId);
84  void sendDone() {}
86 
88  void learnSenderAddr(networking::EthAddr srcMacAddr, Interface *sender);
89 
90  void serialize(CheckpointOut &cp) const;
91  void unserialize(CheckpointIn &cp);
92 
93  private:
94  const double ticksPerByte;
96  const Tick delayVar;
97  const unsigned interfaceId;
98 
100  protected:
101  struct PortFifoEntry : public Serializable
102  {
103  PortFifoEntry(EthPacketPtr pkt, Tick recv_tick, unsigned id)
104  : packet(pkt), recvTick(recv_tick), srcId(id) {}
105 
108  // id of the port that the packet has been received from
109  unsigned srcId;
111  {
112  packet = nullptr;
113  recvTick = 0;
114  srcId = 0;
115  }
116  void serialize(CheckpointOut &cp) const;
117  void unserialize(CheckpointIn &cp);
118  };
119 
120  class PortFifo : public Serializable
121  {
122  protected:
123  struct EntryOrder
124  {
125  bool operator() (const PortFifoEntry& lhs,
126  const PortFifoEntry& rhs) const
127  {
128  if (lhs.recvTick == rhs.recvTick)
129  return lhs.srcId < rhs.srcId;
130  else
131  return lhs.recvTick < rhs.recvTick;
132  }
133  };
134  std::set<PortFifoEntry, EntryOrder> fifo;
135 
136  const std::string objName;
137  const unsigned _maxsize;
138  unsigned _size;
139 
140  public:
141  PortFifo(const std::string &name, int max)
142  :objName(name), _maxsize(max), _size(0) {}
144 
145  const std::string name() { return objName; }
146  // Returns the available capacity of the fifo.
147  // It can return a negative value because in "push" function
148  // we first push the received packet into the fifo and then
149  // check if we exceed the available capacity (if avail() < 0)
150  // and remove packets from the end of fifo
151  int avail() const { return _maxsize - _size; }
152 
153  EthPacketPtr front() { return fifo.begin()->packet; }
154  bool empty() const { return _size == 0; }
155  unsigned size() const { return _size; }
156 
161  bool push(EthPacketPtr ptr, unsigned senderId);
162  void pop();
163  void clear();
167  void serialize(CheckpointOut &cp) const;
168  void unserialize(CheckpointIn &cp);
169  };
174  void transmit();
176  };
177 
179  {
182  };
183 
184  private:
185  // time to live for MAC address mappings
186  const double ttl;
187  // all interfaces of the switch
189  // table that maps MAC address to interfaces
190  std::map<uint64_t, SwitchTableEntry> forwardingTable;
191 
192  void serialize(CheckpointOut &cp) const override;
193  void unserialize(CheckpointIn &cp) override;
194 };
195 
196 } // namespace gem5
197 
198 #endif // __DEV_ETHERSWITCH_HH__
gem5::EtherSwitch::interfaces
std::vector< Interface * > interfaces
Definition: etherswitch.hh:188
gem5::PortID
int16_t PortID
Port index/ID type, and a symbolic name for an invalid port id.
Definition: types.hh:252
gem5::EtherSwitch::Interface::PortFifo::name
const std::string name()
Definition: etherswitch.hh:145
gem5::EtherSwitch::Interface::PortFifo::objName
const std::string objName
Definition: etherswitch.hh:136
gem5::EtherSwitch::EtherSwitch
EtherSwitch(const Params &p)
Definition: etherswitch.cc:44
serialize.hh
gem5::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:126
gem5::EtherSwitch::Interface::PortFifoEntry::PortFifoEntry
PortFifoEntry(EthPacketPtr pkt, Tick recv_tick, unsigned id)
Definition: etherswitch.hh:103
gem5::EtherSwitch::Interface::interfaceId
const unsigned interfaceId
Definition: etherswitch.hh:97
gem5::CheckpointIn
Definition: serialize.hh:68
gem5::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:79
gem5::EtherSwitch::Interface::outputFifo
PortFifo outputFifo
output fifo at each interface
Definition: etherswitch.hh:173
gem5::EtherSwitch::unserialize
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition: etherswitch.cc:268
etherint.hh
gem5::EtherSwitch::Interface::serialize
void serialize(CheckpointOut &cp) const
Serialize an object.
Definition: etherswitch.cc:276
gem5::EtherSwitch::forwardingTable
std::map< uint64_t, SwitchTableEntry > forwardingTable
Definition: etherswitch.hh:190
gem5::EtherSwitch::Interface::PortFifoEntry::recvTick
Tick recvTick
Definition: etherswitch.hh:107
gem5::EtherInt::name
const std::string & name() const
Return port name (for DPRINTF).
Definition: etherint.hh:62
gem5::EtherSwitch::ttl
const double ttl
Definition: etherswitch.hh:186
gem5::EtherSwitch::Interface::PortFifo::EntryOrder
Definition: etherswitch.hh:123
std::vector
STL vector class.
Definition: stl.hh:37
gem5::EtherInt
Definition: etherint.hh:50
gem5::InvalidPortID
const PortID InvalidPortID
Definition: types.hh:253
gem5::EtherSwitch::Interface::PortFifoEntry::~PortFifoEntry
~PortFifoEntry()
Definition: etherswitch.hh:110
gem5::EtherSwitch::Interface::ticksPerByte
const double ticksPerByte
Definition: etherswitch.hh:94
gem5::EtherSwitch::Interface::transmit
void transmit()
Definition: etherswitch.cc:181
gem5::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:65
gem5::EtherSwitch::Interface::txEvent
EventFunctionWrapper txEvent
Definition: etherswitch.hh:175
gem5::Serializable
Basic support for object serialization.
Definition: serialize.hh:169
gem5::EtherSwitch::Params
EtherSwitchParams Params
Definition: etherswitch.hh:57
gem5::EthPacketPtr
std::shared_ptr< EthPacketData > EthPacketPtr
Definition: etherpkt.hh:90
sim_object.hh
gem5::EtherSwitch::Interface::delayVar
const Tick delayVar
Definition: etherswitch.hh:96
gem5::EtherSwitch::Interface::PortFifoEntry
Definition: etherswitch.hh:101
gem5::EtherSwitch::SwitchTableEntry::interface
Interface * interface
Definition: etherswitch.hh:180
gem5::EtherSwitch::Interface::PortFifo::pop
void pop()
Definition: etherswitch.cc:108
gem5::EtherSwitch::Interface::PortFifoEntry::serialize
void serialize(CheckpointOut &cp) const
Serialize an object.
Definition: etherswitch.cc:303
gem5::networking::EthAddr
Definition: inet.hh:78
gem5::MipsISA::p
Bitfield< 0 > p
Definition: pra_constants.hh:326
gem5::Tick
uint64_t Tick
Tick count type.
Definition: types.hh:58
gem5::EtherSwitch::Interface::PortFifo::serialize
void serialize(CheckpointOut &cp) const
Serialization stuff.
Definition: etherswitch.cc:320
gem5::EtherSwitch::serialize
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition: etherswitch.cc:260
gem5::EtherSwitch::Interface::PortFifo::clear
void clear()
Definition: etherswitch.cc:120
gem5::EtherSwitch::Interface::PortFifo::empty
bool empty() const
Definition: etherswitch.hh:154
gem5::EtherSwitch::Interface::PortFifo::avail
int avail() const
Definition: etherswitch.hh:151
gem5::SimObject
Abstract superclass for simulation objects.
Definition: sim_object.hh:146
gem5::EtherSwitch
Definition: etherswitch.hh:54
gem5::EtherSwitch::Interface::switchDelay
const Tick switchDelay
Definition: etherswitch.hh:95
gem5::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:76
gem5::EtherSwitch::Interface::enqueue
void enqueue(EthPacketPtr packet, unsigned senderId)
enqueue packet to the outputFifo
Definition: etherswitch.cc:165
gem5::EtherSwitch::Interface::unserialize
void unserialize(CheckpointIn &cp)
Unserialize an object.
Definition: etherswitch.cc:289
gem5::EtherSwitch::Interface::PortFifoEntry::packet
EthPacketPtr packet
Definition: etherswitch.hh:106
gem5::EtherSwitch::Interface::PortFifoEntry::unserialize
void unserialize(CheckpointIn &cp)
Unserialize an object.
Definition: etherswitch.cc:311
gem5::EventFunctionWrapper
Definition: eventq.hh:1115
gem5::EtherSwitch::Interface::switchingDelay
Tick switchingDelay()
Definition: etherswitch.cc:202
gem5::EtherSwitch::Interface::PortFifo::_size
unsigned _size
Definition: etherswitch.hh:138
gem5::EtherSwitch::Interface::lookupDestPort
Interface * lookupDestPort(networking::EthAddr destAddr)
Definition: etherswitch.cc:213
gem5::EtherSwitch::Interface::PortFifo::unserialize
void unserialize(CheckpointIn &cp)
Unserialize an object.
Definition: etherswitch.cc:333
gem5::Port
Ports are used to interface objects to each other.
Definition: port.hh:61
gem5::EtherSwitch::Interface::PortFifoEntry::srcId
unsigned srcId
Definition: etherswitch.hh:109
gem5::EtherSwitch::Interface::PortFifo::_maxsize
const unsigned _maxsize
Definition: etherswitch.hh:137
gem5::EtherSwitch::Interface::PortFifo::EntryOrder::operator()
bool operator()(const PortFifoEntry &lhs, const PortFifoEntry &rhs) const
Definition: etherswitch.hh:125
gem5::EtherSwitch::Interface::PortFifo::size
unsigned size() const
Definition: etherswitch.hh:155
pktfifo.hh
gem5::EtherSwitch::Interface
Model for an Ethernet switch port.
Definition: etherswitch.hh:69
etherpkt.hh
gem5::EtherSwitch::SwitchTableEntry::lastUseTime
Tick lastUseTime
Definition: etherswitch.hh:181
gem5::CheckpointOut
std::ostream CheckpointOut
Definition: serialize.hh:66
gem5::EtherSwitch::~EtherSwitch
~EtherSwitch()
Definition: etherswitch.cc:56
gem5::EtherSwitch::Interface::PortFifo::PortFifo
PortFifo(const std::string &name, int max)
Definition: etherswitch.hh:141
gem5::EtherSwitch::Interface::learnSenderAddr
void learnSenderAddr(networking::EthAddr srcMacAddr, Interface *sender)
Definition: etherswitch.cc:237
inet.hh
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40
gem5::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:138
gem5::EtherSwitch::Interface::PortFifo::~PortFifo
~PortFifo()
Definition: etherswitch.hh:143
gem5::EtherSwitch::Interface::parent
EtherSwitch * parent
Definition: etherswitch.hh:99
gem5::EtherSwitch::Interface::PortFifo::fifo
std::set< PortFifoEntry, EntryOrder > fifo
Definition: etherswitch.hh:134
gem5::EtherSwitch::Interface::PortFifo
Definition: etherswitch.hh:120
gem5::EtherSwitch::Interface::PortFifo::front
EthPacketPtr front()
Definition: etherswitch.hh:153
gem5::EtherSwitch::SwitchTableEntry
Definition: etherswitch.hh:178
gem5::EtherSwitch::Interface::sendDone
void sendDone()
Definition: etherswitch.hh:84
eventq.hh

Generated on Tue Sep 21 2021 12:25:17 for gem5 by doxygen 1.8.17