etherswitch.hh revision 11317
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 * Authors: Anthony Gutierrez 29 * Mohammad Alian 30 */ 31 32/* @file 33 * Device model for an ethernet switch 34 */ 35 36#ifndef __DEV_ETHERSWITCH_HH__ 37#define __DEV_ETHERSWITCH_HH__ 38 39#include <unordered_map> 40 41#include "base/inet.hh" 42#include "dev/net/etherint.hh" 43#include "dev/net/etherlink.hh" 44#include "dev/net/etherobject.hh" 45#include "dev/net/etherpkt.hh" 46#include "dev/net/pktfifo.hh" 47#include "params/EtherSwitch.hh" 48#include "sim/eventq.hh" 49 50class EtherSwitch : public EtherObject 51{ 52 public: 53 typedef EtherSwitchParams Params; 54 55 EtherSwitch(const Params *p); 56 ~EtherSwitch(); 57 58 const Params * params() const 59 { 60 return dynamic_cast<const Params*>(_params); 61 } 62 63 EtherInt *getEthPort(const std::string &if_name, int idx); 64 65 protected: 66 /** 67 * Model for an Ethernet switch port 68 */ 69 class Interface : public EtherInt 70 { 71 public: 72 Interface(const std::string &name, EtherSwitch *_etherSwitch, 73 uint64_t outputBufferSize, Tick delay, Tick delay_var, 74 double rate); 75 /** 76 * When a packet is received from a device, route it 77 * through an (several) output queue(s) 78 */ 79 bool recvPacket(EthPacketPtr packet); 80 /** 81 * enqueue packet to the outputFifo 82 */ 83 void enqueue(EthPacketPtr packet); 84 void sendDone() {} 85 Tick switchingDelay(); 86 87 Interface* lookupDestPort(Net::EthAddr destAddr); 88 void learnSenderAddr(Net::EthAddr srcMacAddr, Interface *sender); 89 90 void serialize(const std::string &base, CheckpointOut &cp) const; 91 void unserialize(const std::string &base, CheckpointIn &cp); 92 93 private: 94 const double ticksPerByte; 95 const Tick switchDelay; 96 const Tick delayVar; 97 EtherSwitch *parent; 98 /** 99 * output fifo at each interface 100 */ 101 PacketFifo outputFifo; 102 void transmit(); 103 EventWrapper<Interface, &Interface::transmit> txEvent; 104 }; 105 106 struct SwitchTableEntry { 107 Interface *interface; 108 Tick lastUseTime; 109 }; 110 111 private: 112 // time to live for MAC address mappings 113 const double ttl; 114 // all interfaces of the switch 115 std::vector<Interface*> interfaces; 116 // table that maps MAC address to interfaces 117 std::map<uint64_t, SwitchTableEntry> forwardingTable; 118 119 void serialize(CheckpointOut &cp) const override; 120 void unserialize(CheckpointIn &cp) override; 121}; 122 123#endif // __DEV_ETHERSWITCH_HH__ 124