gem5 v24.1.0.1
Loading...
Searching...
No Matches
etherswitch.cc
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
34
35#include "base/trace.hh"
36#include "debug/EthernetAll.hh"
37#include "sim/core.hh"
38#include "sim/cur_tick.hh"
39
40namespace gem5
41{
42
44 : SimObject(p), ttl(p.time_to_live)
45{
46 for (int i = 0; i < p.port_interface_connection_count; ++i) {
47 std::string interfaceName = csprintf("%s.interface%d", name(), i);
48 Interface *interface = new Interface(interfaceName, this,
49 p.output_buffer_size, p.delay,
50 p.delay_var, p.fabric_speed, i);
51 interfaces.push_back(interface);
52 }
53}
54
56{
57 for (auto it : interfaces)
58 delete it;
59
60 interfaces.clear();
61}
62
63Port &
64EtherSwitch::getPort(const std::string &if_name, PortID idx)
65{
66 if (if_name == "interface") {
67 panic_if(idx < 0 || idx >= interfaces.size(), "index out of bounds");
68 return *interfaces.at(idx);
69 }
70
71 return SimObject::getPort(if_name, idx);
72}
73
74bool
76{
77 assert(ptr->length);
78
79 _size += ptr->length;
80 fifo.emplace_hint(fifo.end(), ptr, curTick(), senderId);
81
82 // Drop the extra pushed packets from end of the fifo
83 while (avail() < 0) {
84 DPRINTF(Ethernet, "Fifo is full. Drop packet: len=%d\n",
85 std::prev(fifo.end())->packet->length);
86
87 _size -= std::prev(fifo.end())->packet->length;
88 fifo.erase(std::prev(fifo.end()));
89 }
90
91 if (empty()) {
92 warn("EtherSwitch: Packet length (%d) exceeds the maximum storage "
93 "capacity of port fifo (%d)", ptr->length, _maxsize);
94 }
95
96 // Return true if the newly pushed packet gets inserted
97 // at the head of the queue, otherwise return false
98 // We need this information to deschedule the event that has been
99 // scheduled for the old head of queue packet and schedule a new one
100 if (!empty() && fifo.begin()->packet == ptr) {
101 return true;
102 }
103 return false;
104}
105
106void
108{
109 if (empty())
110 return;
111
112 assert(_size >= fifo.begin()->packet->length);
113 // Erase the packet at the head of the queue
114 _size -= fifo.begin()->packet->length;
115 fifo.erase(fifo.begin());
116}
117
118void
120{
121 fifo.clear();
122 _size = 0;
123}
124
126 EtherSwitch *etherSwitch,
127 uint64_t outputBufferSize, Tick delay,
128 Tick delay_var, double rate, unsigned id)
129 : EtherInt(name), ticksPerByte(rate), switchDelay(delay),
130 delayVar(delay_var), interfaceId(id), parent(etherSwitch),
131 outputFifo(name + ".outputFifo", outputBufferSize),
132 txEvent([this]{ transmit(); }, name)
133{
134}
135
136bool
138{
139 networking::EthAddr destMacAddr(packet->data);
140 networking::EthAddr srcMacAddr(&packet->data[6]);
141
142 learnSenderAddr(srcMacAddr, this);
143 Interface *receiver = lookupDestPort(destMacAddr);
144
145 if (!receiver || destMacAddr.multicast() || destMacAddr.broadcast()) {
146 for (auto it : parent->interfaces)
147 if (it != this)
148 it->enqueue(packet, interfaceId);
149 } else {
150 DPRINTF(Ethernet, "sending packet from MAC %x on port "
151 "%s to MAC %x on port %s\n", uint64_t(srcMacAddr),
152 this->name(), uint64_t(destMacAddr), receiver->name());
153
154 receiver->enqueue(packet, interfaceId);
155 }
156 // At the output port, we either have buffer space (no drop) or
157 // don't (drop packet); in both cases packet is received on
158 // the interface successfully and there is no notion of busy
159 // interface here (as we don't have inputFifo)
160 return true;
161}
162
163void
165{
166 // assuming per-interface transmission events,
167 // if the newly push packet gets inserted at the head of the queue
168 // (either there was nothing in the queue or the priority of the new
169 // packet was higher than the packets already in the fifo)
170 // then we need to schedule an event at
171 // "curTick" + "switchingDelay of the packet at the head of the fifo"
172 // to send this packet out the external link
173 // otherwise, there is already a txEvent scheduled
174 if (outputFifo.push(packet, senderId)) {
175 parent->reschedule(txEvent, curTick() + switchingDelay(), true);
176 }
177}
178
179void
181{
182 // there should be something in the output queue
183 assert(!outputFifo.empty());
184
185 if (!sendPacket(outputFifo.front())) {
186 DPRINTF(Ethernet, "output port busy...retry later\n");
187 if (!txEvent.scheduled())
188 parent->schedule(txEvent, curTick() + sim_clock::as_int::ns);
189 } else {
190 DPRINTF(Ethernet, "packet sent: len=%d\n", outputFifo.front()->length);
191 outputFifo.pop();
192 // schedule an event to send the pkt at
193 // the head of queue, if there is any
194 if (!outputFifo.empty()) {
195 parent->schedule(txEvent, curTick() + switchingDelay());
196 }
197 }
198}
199
200Tick
202{
203 Tick delay = (Tick)ceil(((double)outputFifo.front()->simLength
204 * ticksPerByte) + 1.0);
205 if (delayVar != 0)
206 delay += rng->random<Tick>(0, delayVar);
207 delay += switchDelay;
208 return delay;
209}
210
213{
214 auto it = parent->forwardingTable.find(uint64_t(destMacAddr));
215
216 if (it == parent->forwardingTable.end()) {
217 DPRINTF(Ethernet, "no entry in forwaring table for MAC: "
218 "%x\n", uint64_t(destMacAddr));
219 return nullptr;
220 }
221
222 // check if this entry is valid based on TTL and lastUseTime
223 if ((curTick() - it->second.lastUseTime) > parent->ttl) {
224 // TTL for this mapping has been expired, so this item is not
225 // valide anymore, let's remove it from the map
226 parent->forwardingTable.erase(it);
227 return nullptr;
228 }
229
230 DPRINTF(Ethernet, "found entry for MAC address %x on port %s\n",
231 uint64_t(destMacAddr), it->second.interface->name());
232 return it->second.interface;
233}
234
235void
237 Interface *sender)
238{
239 // learn the port for the sending MAC address
240 auto it = parent->forwardingTable.find(uint64_t(srcMacAddr));
241
242 // if the port for sender's MAC address is not cached,
243 // cache it now, otherwise just update lastUseTime time
244 if (it == parent->forwardingTable.end()) {
245 DPRINTF(Ethernet, "adding forwarding table entry for MAC "
246 " address %x on port %s\n", uint64_t(srcMacAddr),
247 sender->name());
248 EtherSwitch::SwitchTableEntry forwardingTableEntry;
249 forwardingTableEntry.interface = sender;
250 forwardingTableEntry.lastUseTime = curTick();
251 parent->forwardingTable.insert(std::make_pair(uint64_t(srcMacAddr),
252 forwardingTableEntry));
253 } else {
254 it->second.lastUseTime = curTick();
255 }
256}
257
258void
260{
261 for (auto it : interfaces)
262 it->serializeSection(cp, it->name());
263
264}
265
266void
268{
269 for (auto it : interfaces)
270 it->unserializeSection(cp, it->name());
271
272}
273
274void
276{
277 bool event_scheduled = txEvent.scheduled();
278 SERIALIZE_SCALAR(event_scheduled);
279
280 if (event_scheduled) {
281 Tick event_time = txEvent.when();
282 SERIALIZE_SCALAR(event_time);
283 }
284 outputFifo.serializeSection(cp, "outputFifo");
285}
286
287void
289{
290 bool event_scheduled;
291 UNSERIALIZE_SCALAR(event_scheduled);
292
293 if (event_scheduled) {
294 Tick event_time;
295 UNSERIALIZE_SCALAR(event_time);
296 parent->schedule(txEvent, event_time);
297 }
298 outputFifo.unserializeSection(cp, "outputFifo");
299}
300
301void
303{
304 packet->serialize("packet", cp);
305 SERIALIZE_SCALAR(recvTick);
306 SERIALIZE_SCALAR(srcId);
307}
308
309void
311{
312 packet = std::make_shared<EthPacketData>(16384);
313 packet->unserialize("packet", cp);
314 UNSERIALIZE_SCALAR(recvTick);
315 UNSERIALIZE_SCALAR(srcId);
316}
317
318void
320{
321 SERIALIZE_SCALAR(_size);
322 int fifosize = fifo.size();
323
324 SERIALIZE_SCALAR(fifosize);
325
326 int i = 0;
327 for (const auto &entry : fifo)
328 entry.serializeSection(cp, csprintf("entry%d", i++));
329}
330
331void
333{
334 UNSERIALIZE_SCALAR(_size);
335 int fifosize;
336
337 UNSERIALIZE_SCALAR(fifosize);
338 fifo.clear();
339
340 for (int i = 0; i < fifosize; ++i) {
341 PortFifoEntry entry(nullptr, 0, 0);
342
343 entry.unserializeSection(cp, csprintf("entry%d", i));
344
345 fifo.insert(entry);
346
347 }
348}
349
350} // namespace gem5
#define DPRINTF(x,...)
Definition trace.hh:209
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.
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
Interface(const std::string &name, EtherSwitch *_etherSwitch, uint64_t outputBufferSize, Tick delay, Tick delay_var, double rate, unsigned id)
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.
EtherSwitch(const Params &p)
virtual std::string name() const
Definition named.hh:47
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
Abstract superclass for simulation objects.
bool broadcast() const
Definition inet.hh:116
bool multicast() const
Definition inet.hh:115
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition logging.hh:214
void unserializeSection(CheckpointIn &cp, const char *name)
Unserialize an a child object.
Definition serialize.cc:81
virtual Port & getPort(const std::string &if_name, PortID idx=InvalidPortID)
Get a port with a given name and index.
#define warn(...)
Definition logging.hh:256
Bitfield< 51, 48 > ttl
Bitfield< 7 > i
Definition misc_types.hh:67
Bitfield< 0 > p
Tick ns
nanosecond
Definition core.cc:68
Copyright (c) 2024 Arm Limited All rights reserved.
Definition binary32.hh:36
Tick curTick()
The universal simulation clock.
Definition cur_tick.hh:46
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::string csprintf(const char *format, const Args &...args)
Definition cprintf.hh:161
std::shared_ptr< EthPacketData > EthPacketPtr
Definition etherpkt.hh:90
#define UNSERIALIZE_SCALAR(scalar)
Definition serialize.hh:575
#define SERIALIZE_SCALAR(scalar)
Definition serialize.hh:568
void unserialize(CheckpointIn &cp)
Unserialize an object.
void serialize(CheckpointOut &cp) const
Serialize an object.

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