40 #include "debug/EthernetAll.hh"
48 for (
int i = 0;
i < p->port_interface_connection_count; ++
i) {
49 std::string interfaceName =
csprintf(
"%s.interface%d",
name(),
i);
50 Interface *
interface = new Interface(interfaceName, this,
51 p->output_buffer_size, p->delay,
52 p->delay_var, p->fabric_speed, i);
72 panic_if(interface->getPeer(),
"interface already connected\n");
87 DPRINTF(Ethernet,
"Fifo is full. Drop packet: len=%d\n",
88 std::prev(
fifo.end())->packet->length);
90 _size -= std::prev(
fifo.end())->packet->length;
95 warn(
"EtherSwitch: Packet length (%d) exceeds the maximum storage "
96 "capacity of port fifo (%d)", ptr->length,
_maxsize);
103 if (!
empty() &&
fifo.begin()->packet == ptr) {
115 assert(_size >= fifo.begin()->packet->length);
117 _size -= fifo.begin()->packet->length;
118 fifo.erase(fifo.begin());
130 uint64_t outputBufferSize,
Tick delay,
131 Tick delay_var,
double rate,
unsigned id)
144 learnSenderAddr(srcMacAddr,
this);
145 Interface *receiver = lookupDestPort(destMacAddr);
148 for (
auto it : parent->interfaces)
150 it->enqueue(packet, interfaceId);
152 DPRINTF(Ethernet,
"sending packet from MAC %x on port "
153 "%s to MAC %x on port %s\n", uint64_t(srcMacAddr),
154 this->
name(), uint64_t(destMacAddr), receiver->
name());
156 receiver->
enqueue(packet, interfaceId);
176 if (outputFifo.push(packet, senderId)) {
177 parent->reschedule(txEvent,
curTick() + switchingDelay(),
true);
185 assert(!outputFifo.empty());
187 if (!sendPacket(outputFifo.front())) {
188 DPRINTF(Ethernet,
"output port busy...retry later\n");
189 if (!txEvent.scheduled())
192 DPRINTF(Ethernet,
"packet sent: len=%d\n", outputFifo.front()->length);
196 if (!outputFifo.empty()) {
197 parent->schedule(txEvent,
curTick() + switchingDelay());
205 Tick delay = (
Tick)ceil(((
double)outputFifo.front()->simLength
206 * ticksPerByte) + 1.0);
209 delay += switchDelay;
216 auto it = parent->forwardingTable.find(uint64_t(destMacAddr));
218 if (it == parent->forwardingTable.end()) {
219 DPRINTF(Ethernet,
"no entry in forwaring table for MAC: "
220 "%x\n", uint64_t(destMacAddr));
225 if ((
curTick() - it->second.lastUseTime) > parent->ttl) {
228 parent->forwardingTable.erase(it);
232 DPRINTF(Ethernet,
"found entry for MAC address %x on port %s\n",
233 uint64_t(destMacAddr), it->second.interface->name());
234 return it->second.interface;
242 auto it = parent->forwardingTable.find(uint64_t(srcMacAddr));
246 if (it == parent->forwardingTable.end()) {
247 DPRINTF(Ethernet,
"adding forwarding table entry for MAC "
248 " address %x on port %s\n", uint64_t(srcMacAddr),
252 forwardingTableEntry.lastUseTime =
curTick();
253 parent->forwardingTable.insert(std::make_pair(uint64_t(srcMacAddr),
254 forwardingTableEntry));
256 it->second.lastUseTime =
curTick();
264 it->serializeSection(cp, it->name());
272 it->unserializeSection(cp, it->name());
279 bool event_scheduled = txEvent.scheduled();
282 if (event_scheduled) {
283 Tick event_time = txEvent.when();
286 outputFifo.serializeSection(cp,
"outputFifo");
292 bool event_scheduled;
295 if (event_scheduled) {
298 parent->schedule(txEvent, event_time);
300 outputFifo.unserializeSection(cp,
"outputFifo");
306 packet->serialize(
"packet", cp);
314 packet = make_shared<EthPacketData>(16384);
315 packet->unserialize(
"packet", cp);
324 int fifosize = fifo.size();
329 for (
const auto &entry : fifo)
330 entry.serializeSection(cp,
csprintf(
"entry%d", i++));
342 for (
int i = 0;
i < fifosize; ++
i) {
353 EtherSwitchParams::create()
void serialize(CheckpointOut &cp) const
Serialize an object.
void serialize(CheckpointOut &cp) const override
Serialize an object.
void serialize(CheckpointOut &cp) const
Serialize an object.
bool recvPacket(EthPacketPtr packet)
When a packet is received from a device, route it through an (several) output queue(s) ...
Interface(const std::string &name, EtherSwitch *_etherSwitch, uint64_t outputBufferSize, Tick delay, Tick delay_var, double rate, unsigned id)
void unserializeSection(CheckpointIn &cp, const char *name)
Unserialize an a child object.
Model for an Ethernet switch port.
panic_if(!root,"Invalid expression\n")
std::vector< Interface * > interfaces
std::enable_if< std::is_integral< T >::value, T >::type random()
Use the SFINAE idiom to choose an implementation based on whether the type is integral or floating po...
PortFifo outputFifo
output fifo at each interface
#define UNSERIALIZE_SCALAR(scalar)
void learnSenderAddr(Net::EthAddr srcMacAddr, Interface *sender)
Tick curTick()
The current simulated tick.
Interface * lookupDestPort(Net::EthAddr destAddr)
std::string csprintf(const char *format, const Args &...args)
std::set< PortFifoEntry, EntryOrder > fifo
uint64_t Tick
Tick count type.
void unserialize(CheckpointIn &cp)
Unserialize an object.
std::shared_ptr< EthPacketData > EthPacketPtr
void unserialize(CheckpointIn &cp) override
Unserialize an object.
void serialize(CheckpointOut &cp) const
Serialization stuff.
EtherInt * getEthPort(const std::string &if_name, int idx) override
Additional function to return the Port of a memory object.
The base EtherObject class, allows for an accesor function to a simobj that returns the Port...
bool push(EthPacketPtr ptr, unsigned senderId)
Push a packet into the fifo and sort the packets with same recv tick by port id.
const unsigned interfaceId
EtherSwitch(const Params *p)
const double ticksPerByte
#define SERIALIZE_SCALAR(scalar)
virtual const std::string name() const
std::ostream CheckpointOut
void unserialize(CheckpointIn &cp)
Unserialize an object.
void enqueue(EthPacketPtr packet, unsigned senderId)
enqueue packet to the outputFifo
const std::string & name() const
Return port name (for DPRINTF).
EventWrapper< Interface,&Interface::transmit > txEvent
void unserialize(CheckpointIn &cp)
Unserialize an object.