gem5  v19.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  * Authors: Anthony Gutierrez
29  * Mohammad Alian
30  */
31 
32 /* @file
33  * Device model for an ethernet switch
34  */
35 
36 #ifndef __DEV_ETHERSWITCH_HH__
37 #define __DEV_ETHERSWITCH_HH__
38 
39 #include <map>
40 #include <set>
41 
42 #include "base/inet.hh"
43 #include "dev/net/etherint.hh"
44 #include "dev/net/etherlink.hh"
45 #include "dev/net/etherpkt.hh"
46 #include "dev/net/pktfifo.hh"
47 #include "params/EtherSwitch.hh"
48 #include "sim/eventq.hh"
49 #include "sim/sim_object.hh"
50 
51 class EtherSwitch : public SimObject
52 {
53  public:
54  typedef EtherSwitchParams Params;
55 
56  EtherSwitch(const Params *p);
57  ~EtherSwitch();
58 
59  const Params * params() const
60  {
61  return dynamic_cast<const Params*>(_params);
62  }
63 
64  Port &getPort(const std::string &if_name,
65  PortID idx=InvalidPortID) override;
66 
67  protected:
71  class Interface : public EtherInt, public Serializable
72  {
73  public:
74  Interface(const std::string &name, EtherSwitch *_etherSwitch,
75  uint64_t outputBufferSize, Tick delay, Tick delay_var,
76  double rate, unsigned id);
81  bool recvPacket(EthPacketPtr packet);
85  void enqueue(EthPacketPtr packet, unsigned senderId);
86  void sendDone() {}
88 
90  void learnSenderAddr(Net::EthAddr srcMacAddr, Interface *sender);
91 
92  void serialize(CheckpointOut &cp) const;
93  void unserialize(CheckpointIn &cp);
94 
95  private:
96  const double ticksPerByte;
98  const Tick delayVar;
99  const unsigned interfaceId;
100 
102  protected:
103  struct PortFifoEntry : public Serializable
104  {
105  PortFifoEntry(EthPacketPtr pkt, Tick recv_tick, unsigned id)
106  : packet(pkt), recvTick(recv_tick), srcId(id) {}
107 
110  // id of the port that the packet has been received from
111  unsigned srcId;
113  {
114  packet = nullptr;
115  recvTick = 0;
116  srcId = 0;
117  }
118  void serialize(CheckpointOut &cp) const;
119  void unserialize(CheckpointIn &cp);
120  };
121 
122  class PortFifo : public Serializable
123  {
124  protected:
125  struct EntryOrder {
126  bool operator() (const PortFifoEntry& lhs,
127  const PortFifoEntry& rhs) const
128  {
129  if (lhs.recvTick == rhs.recvTick)
130  return lhs.srcId < rhs.srcId;
131  else
132  return lhs.recvTick < rhs.recvTick;
133  }
134  };
135  std::set<PortFifoEntry, EntryOrder> fifo;
136 
137  const std::string objName;
138  const unsigned _maxsize;
139  unsigned _size;
140 
141  public:
142  PortFifo(const std::string &name, int max)
143  :objName(name), _maxsize(max), _size(0) {}
145 
146  const std::string name() { return objName; }
147  // Returns the available capacity of the fifo.
148  // It can return a negative value because in "push" function
149  // we first push the received packet into the fifo and then
150  // check if we exceed the available capacity (if avail() < 0)
151  // and remove packets from the end of fifo
152  int avail() const { return _maxsize - _size; }
153 
154  EthPacketPtr front() { return fifo.begin()->packet; }
155  bool empty() const { return _size == 0; }
156  unsigned size() const { return _size; }
157 
162  bool push(EthPacketPtr ptr, unsigned senderId);
163  void pop();
164  void clear();
168  void serialize(CheckpointOut &cp) const;
169  void unserialize(CheckpointIn &cp);
170  };
175  void transmit();
177  };
178 
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 #endif // __DEV_ETHERSWITCH_HH__
const double ttl
Definition: etherswitch.hh:186
PortFifoEntry(EthPacketPtr pkt, Tick recv_tick, unsigned id)
Definition: etherswitch.hh:105
const std::string & name() const
Return port name (for DPRINTF).
Definition: etherint.hh:61
Ports are used to interface objects to each other.
Definition: port.hh:60
Port & getPort(const std::string &if_name, PortID idx=InvalidPortID) override
Get a port with a given name and index.
Definition: etherswitch.cc:66
bool recvPacket(EthPacketPtr packet)
When a packet is received from a device, route it through an (several) output queue(s) ...
Definition: etherswitch.cc:139
const PortID InvalidPortID
Definition: types.hh:238
Interface(const std::string &name, EtherSwitch *_etherSwitch, uint64_t outputBufferSize, Tick delay, Tick delay_var, double rate, unsigned id)
Definition: etherswitch.cc:127
Model for an Ethernet switch port.
Definition: etherswitch.hh:71
Definition: cprintf.cc:42
std::vector< Interface * > interfaces
Definition: etherswitch.hh:188
std::map< uint64_t, SwitchTableEntry > forwardingTable
Definition: etherswitch.hh:190
STL vector class.
Definition: stl.hh:40
EventFunctionWrapper txEvent
Definition: etherswitch.hh:176
PortFifo(const std::string &name, int max)
Definition: etherswitch.hh:142
PortFifo outputFifo
output fifo at each interface
Definition: etherswitch.hh:174
void learnSenderAddr(Net::EthAddr srcMacAddr, Interface *sender)
Definition: etherswitch.cc:238
Interface * lookupDestPort(Net::EthAddr destAddr)
Definition: etherswitch.cc:214
std::set< PortFifoEntry, EntryOrder > fifo
Definition: etherswitch.hh:135
EtherSwitchParams Params
Definition: etherswitch.hh:54
uint64_t Tick
Tick count type.
Definition: types.hh:63
EtherSwitch * parent
Definition: etherswitch.hh:101
void serialize(CheckpointOut &cp) const
Serialize an object.
Definition: etherswitch.cc:277
std::shared_ptr< EthPacketData > EthPacketPtr
Definition: etherpkt.hh:90
Basic support for object serialization.
Definition: serialize.hh:153
const unsigned interfaceId
Definition: etherswitch.hh:99
EtherSwitch(const Params *p)
Definition: etherswitch.cc:45
const double ticksPerByte
Definition: etherswitch.hh:96
const Params * params() const
Definition: etherswitch.hh:59
std::ostream CheckpointOut
Definition: serialize.hh:68
void unserialize(CheckpointIn &cp)
Unserialize an object.
Definition: etherswitch.cc:312
void enqueue(EthPacketPtr packet, unsigned senderId)
enqueue packet to the outputFifo
Definition: etherswitch.cc:166
const SimObjectParams * _params
Cached copy of the object parameters.
Definition: sim_object.hh:110
int16_t PortID
Port index/ID type, and a symbolic name for an invalid port id.
Definition: types.hh:237
Bitfield< 0 > p
void serialize(CheckpointOut &cp) const
Serialize an object.
Definition: etherswitch.cc:304
Abstract superclass for simulation objects.
Definition: sim_object.hh:96
void unserialize(CheckpointIn &cp)
Unserialize an object.
Definition: etherswitch.cc:290

Generated on Fri Feb 28 2020 16:27:01 for gem5 by doxygen 1.8.13