gem5 v24.1.0.1
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#include <string>
39#include <vector>
40
41#include "base/inet.hh"
42#include "base/random.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/serialize.hh"
50#include "sim/sim_object.hh"
51
52namespace gem5
53{
54
55class EtherSwitch : public SimObject
56{
57 public:
58 using Params = EtherSwitchParams;
59
60 EtherSwitch(const Params &p);
62
63 Port &getPort(const std::string &if_name,
64 PortID idx=InvalidPortID) 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(networking::EthAddr srcMacAddr, Interface *sender);
90
91 void serialize(CheckpointOut &cp) const;
92 void unserialize(CheckpointIn &cp);
93
94 private:
95 const double ticksPerByte;
98 const unsigned interfaceId;
100
102 protected:
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:
126 {
127 bool operator() (const PortFifoEntry& lhs,
128 const PortFifoEntry& rhs) const
129 {
130 if (lhs.recvTick == rhs.recvTick)
131 return lhs.srcId < rhs.srcId;
132 else
133 return lhs.recvTick < rhs.recvTick;
134 }
135 };
136 std::set<PortFifoEntry, EntryOrder> fifo;
137
138 const std::string objName;
139 const unsigned _maxsize;
140 unsigned _size;
141
142 public:
143 PortFifo(const std::string &name, int max)
144 :objName(name), _maxsize(max), _size(0) {}
146
147 const std::string name() { return objName; }
148 // Returns the available capacity of the fifo.
149 // It can return a negative value because in "push" function
150 // we first push the received packet into the fifo and then
151 // check if we exceed the available capacity (if avail() < 0)
152 // and remove packets from the end of fifo
153 int avail() const { return _maxsize - _size; }
154
155 EthPacketPtr front() { return fifo.begin()->packet; }
156 bool empty() const { return _size == 0; }
157 unsigned size() const { return _size; }
158
163 bool push(EthPacketPtr ptr, unsigned senderId);
164 void pop();
165 void clear();
169 void serialize(CheckpointOut &cp) const;
170 void unserialize(CheckpointIn &cp);
171 };
176 void transmit();
178 };
179
185
186 private:
187 // time to live for MAC address mappings
188 const double ttl;
189 // all interfaces of the switch
191 // table that maps MAC address to interfaces
192 std::map<uint64_t, SwitchTableEntry> forwardingTable;
193
194 void serialize(CheckpointOut &cp) const override;
195 void unserialize(CheckpointIn &cp) override;
196};
197
198} // namespace gem5
199
200#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.
std::set< PortFifoEntry, EntryOrder > fifo
void serialize(CheckpointOut &cp) const
Serialization stuff.
PortFifo(const std::string &name, int max)
void unserialize(CheckpointIn &cp)
Unserialize an object.
Model for an Ethernet switch port.
PortFifo outputFifo
output fifo at each interface
void learnSenderAddr(networking::EthAddr srcMacAddr, Interface *sender)
Interface * lookupDestPort(networking::EthAddr destAddr)
void unserialize(CheckpointIn &cp)
Unserialize an object.
void enqueue(EthPacketPtr packet, unsigned senderId)
enqueue packet to the outputFifo
bool recvPacket(EthPacketPtr packet)
When a packet is received from a device, route it through an (several) output queue(s)
EventFunctionWrapper txEvent
void serialize(CheckpointOut &cp) const
Serialize an object.
std::vector< Interface * > interfaces
EtherSwitchParams Params
Port & getPort(const std::string &if_name, PortID idx=InvalidPortID) override
Get a port with a given name and index.
void unserialize(CheckpointIn &cp) override
Unserialize an object.
void serialize(CheckpointOut &cp) const override
Serialize an object.
std::map< uint64_t, SwitchTableEntry > forwardingTable
const double ttl
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
std::shared_ptr< Random > RandomPtr
Definition random.hh:65
static RandomPtr genRandom()
Definition random.hh:68
Basic support for object serialization.
Definition serialize.hh:170
Abstract superclass for simulation objects.
STL vector class.
Definition stl.hh:37
Bitfield< 0 > p
Copyright (c) 2024 Arm Limited All rights reserved.
Definition binary32.hh:36
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.
void serialize(CheckpointOut &cp) const
Serialize an object.
PortFifoEntry(EthPacketPtr pkt, Tick recv_tick, unsigned id)
bool operator()(const PortFifoEntry &lhs, const PortFifoEntry &rhs) const

Generated on Mon Jan 13 2025 04:28:34 for gem5 by doxygen 1.9.8