gem5  v20.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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;
90  void unserialize(CheckpointIn &cp);
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__
const double ttl
Definition: etherswitch.hh:183
PortFifoEntry(EthPacketPtr pkt, Tick recv_tick, unsigned id)
Definition: etherswitch.hh:102
const std::string & name() const
Return port name (for DPRINTF).
Definition: etherint.hh:59
Ports are used to interface objects to each other.
Definition: port.hh:56
Port & getPort(const std::string &if_name, PortID idx=InvalidPortID) override
Get a port with a given name and index.
Definition: etherswitch.cc:63
bool recvPacket(EthPacketPtr packet)
When a packet is received from a device, route it through an (several) output queue(s) ...
Definition: etherswitch.cc:136
const PortID InvalidPortID
Definition: types.hh:236
Interface(const std::string &name, EtherSwitch *_etherSwitch, uint64_t outputBufferSize, Tick delay, Tick delay_var, double rate, unsigned id)
Definition: etherswitch.cc:124
Model for an Ethernet switch port.
Definition: etherswitch.hh:68
Definition: cprintf.cc:40
std::vector< Interface * > interfaces
Definition: etherswitch.hh:185
std::map< uint64_t, SwitchTableEntry > forwardingTable
Definition: etherswitch.hh:187
STL vector class.
Definition: stl.hh:37
EventFunctionWrapper txEvent
Definition: etherswitch.hh:173
PortFifo(const std::string &name, int max)
Definition: etherswitch.hh:139
PortFifo outputFifo
output fifo at each interface
Definition: etherswitch.hh:171
void learnSenderAddr(Net::EthAddr srcMacAddr, Interface *sender)
Definition: etherswitch.cc:235
Interface * lookupDestPort(Net::EthAddr destAddr)
Definition: etherswitch.cc:211
std::set< PortFifoEntry, EntryOrder > fifo
Definition: etherswitch.hh:132
EtherSwitchParams Params
Definition: etherswitch.hh:51
uint64_t Tick
Tick count type.
Definition: types.hh:61
EtherSwitch * parent
Definition: etherswitch.hh:98
void serialize(CheckpointOut &cp) const
Serialize an object.
Definition: etherswitch.cc:274
std::shared_ptr< EthPacketData > EthPacketPtr
Definition: etherpkt.hh:87
Basic support for object serialization.
Definition: serialize.hh:166
const unsigned interfaceId
Definition: etherswitch.hh:96
EtherSwitch(const Params *p)
Definition: etherswitch.cc:42
const double ticksPerByte
Definition: etherswitch.hh:93
const Params * params() const
Definition: etherswitch.hh:56
std::ostream CheckpointOut
Definition: serialize.hh:63
void unserialize(CheckpointIn &cp)
Unserialize an object.
Definition: etherswitch.cc:309
void enqueue(EthPacketPtr packet, unsigned senderId)
enqueue packet to the outputFifo
Definition: etherswitch.cc:163
const SimObjectParams * _params
Cached copy of the object parameters.
Definition: sim_object.hh:111
int16_t PortID
Port index/ID type, and a symbolic name for an invalid port id.
Definition: types.hh:235
Bitfield< 0 > p
void serialize(CheckpointOut &cp) const
Serialize an object.
Definition: etherswitch.cc:301
Abstract superclass for simulation objects.
Definition: sim_object.hh:93
void unserialize(CheckpointIn &cp)
Unserialize an object.
Definition: etherswitch.cc:287

Generated on Thu May 28 2020 16:21:32 for gem5 by doxygen 1.8.13