2 * Copyright (c) 2014 The Regents of The University of Michigan
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.
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.
28 * Authors: Anthony Gutierrez
33 * Device model for an ethernet switch
36 #ifndef __DEV_ETHERSWITCH_HH__
37 #define __DEV_ETHERSWITCH_HH__
42 #include "base/inet.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/sim_object.hh"
51 class EtherSwitch : public SimObject
54 typedef EtherSwitchParams Params;
56 EtherSwitch(const Params *p);
59 const Params * params() const
61 return dynamic_cast<const Params*>(_params);
64 Port &getPort(const std::string &if_name,
65 PortID idx=InvalidPortID) override;
69 * Model for an Ethernet switch port
71 class Interface : public EtherInt, public Serializable
74 Interface(const std::string &name, EtherSwitch *_etherSwitch,
75 uint64_t outputBufferSize, Tick delay, Tick delay_var,
76 double rate, unsigned id);
78 * When a packet is received from a device, route it
79 * through an (several) output queue(s)
81 bool recvPacket(EthPacketPtr packet);
83 * enqueue packet to the outputFifo
85 void enqueue(EthPacketPtr packet, unsigned senderId);
87 Tick switchingDelay();
89 Interface* lookupDestPort(Net::EthAddr destAddr);
90 void learnSenderAddr(Net::EthAddr srcMacAddr, Interface *sender);
92 void serialize(CheckpointOut &cp) const;
93 void unserialize(CheckpointIn &cp);
96 const double ticksPerByte;
97 const Tick switchDelay;
99 const unsigned interfaceId;
103 struct PortFifoEntry : public Serializable
105 PortFifoEntry(EthPacketPtr pkt, Tick recv_tick, unsigned id)
106 : packet(pkt), recvTick(recv_tick), srcId(id) {}
110 // id of the port that the packet has been received from
118 void serialize(CheckpointOut &cp) const;
119 void unserialize(CheckpointIn &cp);
122 class PortFifo : public Serializable
126 bool operator() (const PortFifoEntry& lhs,
127 const PortFifoEntry& rhs) const
129 if (lhs.recvTick == rhs.recvTick)
130 return lhs.srcId < rhs.srcId;
132 return lhs.recvTick < rhs.recvTick;
135 std::set<PortFifoEntry, EntryOrder> fifo;
137 const std::string objName;
138 const unsigned _maxsize;
142 PortFifo(const std::string &name, int max)
143 :objName(name), _maxsize(max), _size(0) {}
146 const std::string name() { return objName; }
147 // Returns the available capacity of the fifo.
148 // It can return a negative value because in "push" function
149 // we first push the received packet into the fifo and then
150 // check if we exceed the available capacity (if avail() < 0)
151 // and remove packets from the end of fifo
152 int avail() const { return _maxsize - _size; }
154 EthPacketPtr front() { return fifo.begin()->packet; }
155 bool empty() const { return _size == 0; }
156 unsigned size() const { return _size; }
159 * Push a packet into the fifo
160 * and sort the packets with same recv tick by port id
162 bool push(EthPacketPtr ptr, unsigned senderId);
166 * Serialization stuff
168 void serialize(CheckpointOut &cp) const;
169 void unserialize(CheckpointIn &cp);
172 * output fifo at each interface
176 EventFunctionWrapper txEvent;
179 struct SwitchTableEntry {
180 Interface *interface;
185 // time to live for MAC address mappings
187 // all interfaces of the switch
188 std::vector<Interface*> interfaces;
189 // table that maps MAC address to interfaces
190 std::map<uint64_t, SwitchTableEntry> forwardingTable;
192 void serialize(CheckpointOut &cp) const override;
193 void unserialize(CheckpointIn &cp) override;
196 #endif // __DEV_ETHERSWITCH_HH__