gem5  v22.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 #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__
const std::string & name() const
Return port name (for DPRINTF).
Definition: etherint.hh:62
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
std::set< PortFifoEntry, EntryOrder > fifo
Definition: etherswitch.hh:134
void serialize(CheckpointOut &cp) const
Serialization stuff.
Definition: etherswitch.cc:320
PortFifo(const std::string &name, int max)
Definition: etherswitch.hh:141
void unserialize(CheckpointIn &cp)
Unserialize an object.
Definition: etherswitch.cc:333
Model for an Ethernet switch port.
Definition: etherswitch.hh:70
PortFifo outputFifo
output fifo at each interface
Definition: etherswitch.hh:173
void learnSenderAddr(networking::EthAddr srcMacAddr, Interface *sender)
Definition: etherswitch.cc:237
Interface * lookupDestPort(networking::EthAddr destAddr)
Definition: etherswitch.cc:213
void unserialize(CheckpointIn &cp)
Unserialize an object.
Definition: etherswitch.cc:289
void enqueue(EthPacketPtr packet, unsigned senderId)
enqueue packet to the outputFifo
Definition: etherswitch.cc:165
bool recvPacket(EthPacketPtr packet)
When a packet is received from a device, route it through an (several) output queue(s)
Definition: etherswitch.cc:138
EventFunctionWrapper txEvent
Definition: etherswitch.hh:175
Interface(const std::string &name, EtherSwitch *_etherSwitch, uint64_t outputBufferSize, Tick delay, Tick delay_var, double rate, unsigned id)
Definition: etherswitch.cc:126
void serialize(CheckpointOut &cp) const
Serialize an object.
Definition: etherswitch.cc:276
std::vector< Interface * > interfaces
Definition: etherswitch.hh:188
EtherSwitchParams Params
Definition: etherswitch.hh:57
Port & getPort(const std::string &if_name, PortID idx=InvalidPortID) override
Get a port with a given name and index.
Definition: etherswitch.cc:65
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition: etherswitch.cc:268
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition: etherswitch.cc:260
EtherSwitch(const Params &p)
Definition: etherswitch.cc:44
std::map< uint64_t, SwitchTableEntry > forwardingTable
Definition: etherswitch.hh:190
const double ttl
Definition: etherswitch.hh:186
Ports are used to interface objects to each other.
Definition: port.hh:62
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
Basic support for object serialization.
Definition: serialize.hh:170
Abstract superclass for simulation objects.
Definition: sim_object.hh:148
STL vector class.
Definition: stl.hh:37
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
uint64_t Tick
Tick count type.
Definition: types.hh:58
std::shared_ptr< EthPacketData > EthPacketPtr
Definition: etherpkt.hh:90
void unserialize(CheckpointIn &cp)
Unserialize an object.
Definition: etherswitch.cc:311
void serialize(CheckpointOut &cp) const
Serialize an object.
Definition: etherswitch.cc:303
PortFifoEntry(EthPacketPtr pkt, Tick recv_tick, unsigned id)
Definition: etherswitch.hh:103
bool operator()(const PortFifoEntry &lhs, const PortFifoEntry &rhs) const
Definition: etherswitch.hh:125

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