37#include "debug/EthernetAll.hh"
47 for (
int i = 0;
i <
p.port_interface_connection_count; ++
i) {
48 std::string interfaceName =
csprintf(
"%s.interface%d",
name(),
i);
50 p.output_buffer_size,
p.delay,
51 p.delay_var,
p.fabric_speed,
i);
67 if (if_name ==
"interface") {
85 DPRINTF(Ethernet,
"Fifo is full. Drop packet: len=%d\n",
86 std::prev(
fifo.end())->packet->length);
88 _size -= std::prev(
fifo.end())->packet->length;
93 warn(
"EtherSwitch: Packet length (%d) exceeds the maximum storage "
94 "capacity of port fifo (%d)", ptr->length,
_maxsize);
101 if (!
empty() &&
fifo.begin()->packet == ptr) {
113 assert(_size >= fifo.begin()->packet->length);
115 _size -= fifo.begin()->packet->length;
116 fifo.erase(fifo.begin());
128 uint64_t outputBufferSize,
Tick delay,
129 Tick delay_var,
double rate,
unsigned id)
143 learnSenderAddr(srcMacAddr,
this);
144 Interface *receiver = lookupDestPort(destMacAddr);
147 for (
auto it : parent->interfaces)
149 it->enqueue(packet, interfaceId);
151 DPRINTF(Ethernet,
"sending packet from MAC %x on port "
152 "%s to MAC %x on port %s\n", uint64_t(srcMacAddr),
153 this->
name(), uint64_t(destMacAddr), receiver->
name());
155 receiver->
enqueue(packet, interfaceId);
175 if (outputFifo.push(packet, senderId)) {
176 parent->reschedule(txEvent,
curTick() + switchingDelay(),
true);
184 assert(!outputFifo.empty());
186 if (!sendPacket(outputFifo.front())) {
187 DPRINTF(Ethernet,
"output port busy...retry later\n");
188 if (!txEvent.scheduled())
191 DPRINTF(Ethernet,
"packet sent: len=%d\n", outputFifo.front()->length);
195 if (!outputFifo.empty()) {
196 parent->schedule(txEvent,
curTick() + switchingDelay());
204 Tick delay = (
Tick)ceil(((
double)outputFifo.front()->simLength
205 * ticksPerByte) + 1.0);
208 delay += switchDelay;
215 auto it = parent->forwardingTable.find(uint64_t(destMacAddr));
217 if (it == parent->forwardingTable.end()) {
218 DPRINTF(Ethernet,
"no entry in forwaring table for MAC: "
219 "%x\n", uint64_t(destMacAddr));
224 if ((
curTick() - it->second.lastUseTime) > parent->ttl) {
227 parent->forwardingTable.erase(it);
231 DPRINTF(Ethernet,
"found entry for MAC address %x on port %s\n",
232 uint64_t(destMacAddr), it->second.interface->name());
233 return it->second.interface;
241 auto it = parent->forwardingTable.find(uint64_t(srcMacAddr));
245 if (it == parent->forwardingTable.end()) {
246 DPRINTF(Ethernet,
"adding forwarding table entry for MAC "
247 " address %x on port %s\n", uint64_t(srcMacAddr),
252 parent->forwardingTable.insert(std::make_pair(uint64_t(srcMacAddr),
253 forwardingTableEntry));
255 it->second.lastUseTime =
curTick();
263 it->serializeSection(cp, it->name());
271 it->unserializeSection(cp, it->name());
278 bool event_scheduled = txEvent.scheduled();
281 if (event_scheduled) {
282 Tick event_time = txEvent.when();
285 outputFifo.serializeSection(cp,
"outputFifo");
291 bool event_scheduled;
294 if (event_scheduled) {
297 parent->schedule(txEvent, event_time);
299 outputFifo.unserializeSection(cp,
"outputFifo");
305 packet->serialize(
"packet", cp);
313 packet = std::make_shared<EthPacketData>(16384);
314 packet->unserialize(
"packet", cp);
323 int fifosize = fifo.size();
328 for (
const auto &entry : fifo)
329 entry.serializeSection(cp,
csprintf(
"entry%d",
i++));
341 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.
std::enable_if_t< std::is_integral_v< T >, T > random()
Use the SFINAE idiom to choose an implementation based on whether the type is integral or floating po...
#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 - Pranith Kumar Copyright (c) 2020 Inria 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.