gem5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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/etherobject.hh"
46 #include "dev/net/etherpkt.hh"
47 #include "dev/net/pktfifo.hh"
48 #include "params/EtherSwitch.hh"
49 #include "sim/eventq.hh"
50 
51 class EtherSwitch : public EtherObject
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  EtherInt *getEthPort(const std::string &if_name, int idx) override;
65 
66  protected:
70  class Interface : public EtherInt, public Serializable
71  {
72  public:
73  Interface(const std::string &name, EtherSwitch *_etherSwitch,
74  uint64_t outputBufferSize, Tick delay, Tick delay_var,
75  double rate, unsigned id);
80  bool recvPacket(EthPacketPtr packet);
84  void enqueue(EthPacketPtr packet, unsigned senderId);
85  void sendDone() {}
87 
89  void learnSenderAddr(Net::EthAddr srcMacAddr, Interface *sender);
90 
91  void serialize(CheckpointOut &cp) const;
92  void unserialize(CheckpointIn &cp);
93 
94  private:
95  const double ticksPerByte;
97  const Tick delayVar;
98  const unsigned interfaceId;
99 
101  protected:
102  struct PortFifoEntry : public Serializable
103  {
104  PortFifoEntry(EthPacketPtr pkt, Tick recv_tick, unsigned id)
105  : packet(pkt), recvTick(recv_tick), srcId(id) {}
106 
109  // id of the port that the packet has been received from
110  unsigned srcId;
112  {
113  packet = nullptr;
114  recvTick = 0;
115  srcId = 0;
116  }
117  void serialize(CheckpointOut &cp) const;
118  void unserialize(CheckpointIn &cp);
119  };
120 
121  class PortFifo : public Serializable
122  {
123  protected:
124  struct EntryOrder {
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 
181  };
182 
183  private:
184  // time to live for MAC address mappings
185  const double ttl;
186  // all interfaces of the switch
188  // table that maps MAC address to interfaces
189  std::map<uint64_t, SwitchTableEntry> forwardingTable;
190 
191  void serialize(CheckpointOut &cp) const override;
192  void unserialize(CheckpointIn &cp) override;
193 };
194 
195 #endif // __DEV_ETHERSWITCH_HH__
void serialize(CheckpointOut &cp) const
Serialize an object.
Definition: etherswitch.cc:304
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition: etherswitch.cc:261
void serialize(CheckpointOut &cp) const
Serialize an object.
Definition: etherswitch.cc:277
const double ttl
Definition: etherswitch.hh:185
PortFifoEntry(EthPacketPtr pkt, Tick recv_tick, unsigned id)
Definition: etherswitch.hh:104
bool recvPacket(EthPacketPtr packet)
When a packet is received from a device, route it through an (several) output queue(s) ...
Definition: etherswitch.cc:139
Interface(const std::string &name, EtherSwitch *_etherSwitch, uint64_t outputBufferSize, Tick delay, Tick delay_var, double rate, unsigned id)
Definition: etherswitch.cc:128
Model for an Ethernet switch port.
Definition: etherswitch.hh:70
std::vector< Interface * > interfaces
Definition: etherswitch.hh:187
std::map< uint64_t, SwitchTableEntry > forwardingTable
Definition: etherswitch.hh:189
STL vector class.
Definition: stl.hh:40
PortFifo(const std::string &name, int max)
Definition: etherswitch.hh:141
PortFifo outputFifo
output fifo at each interface
Definition: etherswitch.hh:173
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:134
EtherSwitchParams Params
Definition: etherswitch.hh:54
EtherObjectParams Params
Definition: etherobject.hh:51
uint64_t Tick
Tick count type.
Definition: types.hh:63
void unserialize(CheckpointIn &cp)
Unserialize an object.
Definition: etherswitch.cc:334
EtherSwitch * parent
Definition: etherswitch.hh:100
std::shared_ptr< EthPacketData > EthPacketPtr
Definition: etherpkt.hh:90
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition: etherswitch.cc:269
void serialize(CheckpointOut &cp) const
Serialization stuff.
Definition: etherswitch.cc:321
EtherInt * getEthPort(const std::string &if_name, int idx) override
Additional function to return the Port of a memory object.
Definition: etherswitch.cc:66
bool operator()(const PortFifoEntry &lhs, const PortFifoEntry &rhs) const
Definition: etherswitch.hh:125
Basic support for object serialization.
Definition: serialize.hh:220
The base EtherObject class, allows for an accesor function to a simobj that returns the Port...
Definition: etherobject.hh:48
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:78
const unsigned interfaceId
Definition: etherswitch.hh:98
EtherSwitch(const Params *p)
Definition: etherswitch.cc:45
const double ticksPerByte
Definition: etherswitch.hh:95
const Params * params() const
Definition: etherswitch.hh:59
std::ostream CheckpointOut
Definition: serialize.hh:67
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:107
const std::string & name() const
Return port name (for DPRINTF).
Definition: etherint.hh:60
EventWrapper< Interface,&Interface::transmit > txEvent
Definition: etherswitch.hh:175
Bitfield< 0 > p
Base Ethernet Object declaration.
void unserialize(CheckpointIn &cp)
Unserialize an object.
Definition: etherswitch.cc:290

Generated on Fri Jun 9 2017 13:03:46 for gem5 by doxygen 1.8.6