36#include "debug/EthernetAll.hh"
46 for (
int i = 0;
i <
p.port_interface_connection_count; ++
i) {
47 std::string interfaceName =
csprintf(
"%s.interface%d",
name(),
i);
49 p.output_buffer_size,
p.delay,
50 p.delay_var,
p.fabric_speed,
i);
66 if (if_name ==
"interface") {
84 DPRINTF(Ethernet,
"Fifo is full. Drop packet: len=%d\n",
85 std::prev(
fifo.end())->packet->length);
87 _size -= std::prev(
fifo.end())->packet->length;
92 warn(
"EtherSwitch: Packet length (%d) exceeds the maximum storage "
93 "capacity of port fifo (%d)", ptr->length,
_maxsize);
100 if (!
empty() &&
fifo.begin()->packet == ptr) {
112 assert(_size >= fifo.begin()->packet->length);
114 _size -= fifo.begin()->packet->length;
115 fifo.erase(fifo.begin());
127 uint64_t outputBufferSize,
Tick delay,
128 Tick delay_var,
double rate,
unsigned id)
142 learnSenderAddr(srcMacAddr,
this);
143 Interface *receiver = lookupDestPort(destMacAddr);
146 for (
auto it : parent->interfaces)
148 it->enqueue(packet, interfaceId);
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());
154 receiver->
enqueue(packet, interfaceId);
174 if (outputFifo.push(packet, senderId)) {
175 parent->reschedule(txEvent,
curTick() + switchingDelay(),
true);
183 assert(!outputFifo.empty());
185 if (!sendPacket(outputFifo.front())) {
186 DPRINTF(Ethernet,
"output port busy...retry later\n");
187 if (!txEvent.scheduled())
190 DPRINTF(Ethernet,
"packet sent: len=%d\n", outputFifo.front()->length);
194 if (!outputFifo.empty()) {
195 parent->schedule(txEvent,
curTick() + switchingDelay());
203 Tick delay = (
Tick)ceil(((
double)outputFifo.front()->simLength
204 * ticksPerByte) + 1.0);
206 delay += rng->random<
Tick>(0, delayVar);
207 delay += switchDelay;
214 auto it = parent->forwardingTable.find(uint64_t(destMacAddr));
216 if (it == parent->forwardingTable.end()) {
217 DPRINTF(Ethernet,
"no entry in forwaring table for MAC: "
218 "%x\n", uint64_t(destMacAddr));
223 if ((
curTick() - it->second.lastUseTime) > parent->ttl) {
226 parent->forwardingTable.erase(it);
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;
240 auto it = parent->forwardingTable.find(uint64_t(srcMacAddr));
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),
251 parent->forwardingTable.insert(std::make_pair(uint64_t(srcMacAddr),
252 forwardingTableEntry));
254 it->second.lastUseTime =
curTick();
262 it->serializeSection(cp, it->name());
270 it->unserializeSection(cp, it->name());
277 bool event_scheduled = txEvent.scheduled();
280 if (event_scheduled) {
281 Tick event_time = txEvent.when();
284 outputFifo.serializeSection(cp,
"outputFifo");
290 bool event_scheduled;
293 if (event_scheduled) {
296 parent->schedule(txEvent, event_time);
298 outputFifo.unserializeSection(cp,
"outputFifo");
304 packet->serialize(
"packet", cp);
312 packet = std::make_shared<EthPacketData>(16384);
313 packet->unserialize(
"packet", cp);
322 int fifosize = fifo.size();
327 for (
const auto &entry : fifo)
328 entry.serializeSection(cp,
csprintf(
"entry%d",
i++));
340 for (
int i = 0;
i < fifosize; ++
i) {
const std::string & name() const
Return port name (for DPRINTF).
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)
const double ticksPerByte
const unsigned interfaceId
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
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
Ports are used to interface objects to each other.
const PortID id
A numeric identifier to distinguish ports in a vector, and set to InvalidPortID in case this port is ...
Abstract superclass for simulation objects.
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
void unserializeSection(CheckpointIn &cp, const char *name)
Unserialize an a child object.
virtual Port & getPort(const std::string &if_name, PortID idx=InvalidPortID)
Get a port with a given name and index.
Copyright (c) 2024 Arm Limited All rights reserved.
Tick curTick()
The universal simulation clock.
std::ostream CheckpointOut
int16_t PortID
Port index/ID type, and a symbolic name for an invalid port id.
uint64_t Tick
Tick count type.
std::string csprintf(const char *format, const Args &...args)
std::shared_ptr< EthPacketData > EthPacketPtr
#define UNSERIALIZE_SCALAR(scalar)
#define SERIALIZE_SCALAR(scalar)
void unserialize(CheckpointIn &cp)
Unserialize an object.
void serialize(CheckpointOut &cp) const
Serialize an object.