etherlink.hh revision 11071
12847Sksewell@umich.edu/*
27783SGiacomo.Gabrielli@arm.com * Copyright (c) 2015 ARM Limited
37783SGiacomo.Gabrielli@arm.com * All rights reserved
47783SGiacomo.Gabrielli@arm.com *
57783SGiacomo.Gabrielli@arm.com * The license below extends only to copyright in the software and shall
67783SGiacomo.Gabrielli@arm.com * not be construed as granting a license to any other intellectual
77783SGiacomo.Gabrielli@arm.com * property including but not limited to intellectual property relating
87783SGiacomo.Gabrielli@arm.com * to a hardware implementation of the functionality of the software
97783SGiacomo.Gabrielli@arm.com * licensed hereunder.  You may use the software subject to the license
107783SGiacomo.Gabrielli@arm.com * terms below provided that you ensure that this notice is replicated
117783SGiacomo.Gabrielli@arm.com * unmodified and in its entirety in all distributions of the software,
127783SGiacomo.Gabrielli@arm.com * modified or unmodified, in source code or in binary form.
137783SGiacomo.Gabrielli@arm.com *
145596Sgblack@eecs.umich.edu * Copyright (c) 2002-2005 The Regents of The University of Michigan
152847Sksewell@umich.edu * All rights reserved.
162847Sksewell@umich.edu *
172847Sksewell@umich.edu * Redistribution and use in source and binary forms, with or without
182847Sksewell@umich.edu * modification, are permitted provided that the following conditions are
192847Sksewell@umich.edu * met: redistributions of source code must retain the above copyright
202847Sksewell@umich.edu * notice, this list of conditions and the following disclaimer;
212847Sksewell@umich.edu * redistributions in binary form must reproduce the above copyright
222847Sksewell@umich.edu * notice, this list of conditions and the following disclaimer in the
232847Sksewell@umich.edu * documentation and/or other materials provided with the distribution;
242847Sksewell@umich.edu * neither the name of the copyright holders nor the names of its
252847Sksewell@umich.edu * contributors may be used to endorse or promote products derived from
262847Sksewell@umich.edu * this software without specific prior written permission.
272847Sksewell@umich.edu *
282847Sksewell@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
292847Sksewell@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
302847Sksewell@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
312847Sksewell@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
322847Sksewell@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
332847Sksewell@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
342847Sksewell@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
352847Sksewell@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
362847Sksewell@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
372847Sksewell@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
382847Sksewell@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
392847Sksewell@umich.edu *
405596Sgblack@eecs.umich.edu * Authors: Nathan Binkert
412847Sksewell@umich.edu */
422847Sksewell@umich.edu
432847Sksewell@umich.edu/* @file
442847Sksewell@umich.edu * Device module for modelling a fixed bandwidth full duplex ethernet link
452847Sksewell@umich.edu */
465596Sgblack@eecs.umich.edu
476658Snate@binkert.org#ifndef __DEV_ETHERLINK_HH__
488229Snate@binkert.org#define __DEV_ETHERLINK_HH__
498229Snate@binkert.org
505596Sgblack@eecs.umich.edu#include <queue>
515596Sgblack@eecs.umich.edu
522847Sksewell@umich.edu#include "base/types.hh"
535596Sgblack@eecs.umich.edu#include "dev/etherint.hh"
545596Sgblack@eecs.umich.edu#include "dev/etherobject.hh"
555596Sgblack@eecs.umich.edu#include "dev/etherpkt.hh"
565596Sgblack@eecs.umich.edu#include "params/EtherLink.hh"
575596Sgblack@eecs.umich.edu#include "sim/eventq.hh"
585596Sgblack@eecs.umich.edu#include "sim/sim_object.hh"
595596Sgblack@eecs.umich.edu
605596Sgblack@eecs.umich.educlass EtherDump;
615596Sgblack@eecs.umich.educlass Checkpoint;
625596Sgblack@eecs.umich.edu/*
635596Sgblack@eecs.umich.edu * Model for a fixed bandwidth full duplex ethernet link
645596Sgblack@eecs.umich.edu */
655596Sgblack@eecs.umich.educlass EtherLink : public EtherObject
665596Sgblack@eecs.umich.edu{
675596Sgblack@eecs.umich.edu  protected:
685596Sgblack@eecs.umich.edu    class Interface;
695596Sgblack@eecs.umich.edu
705596Sgblack@eecs.umich.edu    /*
715596Sgblack@eecs.umich.edu     * Model for a single uni-directional link
725596Sgblack@eecs.umich.edu     */
735596Sgblack@eecs.umich.edu    class Link
745596Sgblack@eecs.umich.edu    {
755596Sgblack@eecs.umich.edu      protected:
765596Sgblack@eecs.umich.edu        const std::string objName;
775596Sgblack@eecs.umich.edu
785596Sgblack@eecs.umich.edu        EtherLink *const parent;
795596Sgblack@eecs.umich.edu        const int number;
805596Sgblack@eecs.umich.edu
815596Sgblack@eecs.umich.edu        Interface *txint;
825596Sgblack@eecs.umich.edu        Interface *rxint;
835596Sgblack@eecs.umich.edu
848902Sandreas.hansson@arm.com        const double ticksPerByte;
855596Sgblack@eecs.umich.edu        const Tick linkDelay;
865596Sgblack@eecs.umich.edu        const Tick delayVar;
875596Sgblack@eecs.umich.edu        EtherDump *const dump;
885596Sgblack@eecs.umich.edu
898502Sgblack@eecs.umich.edu      protected:
907720Sgblack@eecs.umich.edu        /*
917720Sgblack@eecs.umich.edu         * Transfer is complete
925596Sgblack@eecs.umich.edu         */
935596Sgblack@eecs.umich.edu        EthPacketPtr packet;
948502Sgblack@eecs.umich.edu        void txDone();
955596Sgblack@eecs.umich.edu        typedef EventWrapper<Link, &Link::txDone> DoneEvent;
969252Sdjordje.kovacevic@arm.com        friend void DoneEvent::process();
979252Sdjordje.kovacevic@arm.com        DoneEvent doneEvent;
985596Sgblack@eecs.umich.edu
995596Sgblack@eecs.umich.edu        /**
1005596Sgblack@eecs.umich.edu         * Maintain a queue of in-flight packets. Assume that the
1015596Sgblack@eecs.umich.edu         * delay is non-zero and constant (i.e., at most one packet
1025596Sgblack@eecs.umich.edu         * per tick).
1035596Sgblack@eecs.umich.edu         */
1045596Sgblack@eecs.umich.edu        std::deque<std::pair<Tick, EthPacketPtr>> txQueue;
1055596Sgblack@eecs.umich.edu
1065596Sgblack@eecs.umich.edu        void processTxQueue();
1075596Sgblack@eecs.umich.edu        typedef EventWrapper<Link, &Link::processTxQueue> TxQueueEvent;
1085596Sgblack@eecs.umich.edu        friend void TxQueueEvent::process();
1095596Sgblack@eecs.umich.edu        TxQueueEvent txQueueEvent;
1105596Sgblack@eecs.umich.edu
1117783SGiacomo.Gabrielli@arm.com        void txComplete(EthPacketPtr packet);
1129046SAli.Saidi@ARM.com
1139046SAli.Saidi@ARM.com      public:
1149046SAli.Saidi@ARM.com        Link(const std::string &name, EtherLink *p, int num,
1157783SGiacomo.Gabrielli@arm.com             double rate, Tick delay, Tick delay_var, EtherDump *dump);
1167783SGiacomo.Gabrielli@arm.com        ~Link() {}
1177783SGiacomo.Gabrielli@arm.com
1187783SGiacomo.Gabrielli@arm.com        const std::string name() const { return objName; }
1199046SAli.Saidi@ARM.com
1209046SAli.Saidi@ARM.com        bool busy() const { return (bool)packet; }
1217783SGiacomo.Gabrielli@arm.com        bool transmit(EthPacketPtr packet);
1229046SAli.Saidi@ARM.com
1239046SAli.Saidi@ARM.com        void setTxInt(Interface *i) { assert(!txint); txint = i; }
1247783SGiacomo.Gabrielli@arm.com        void setRxInt(Interface *i) { assert(!rxint); rxint = i; }
1255596Sgblack@eecs.umich.edu
1268471SGiacomo.Gabrielli@arm.com        void serialize(const std::string &base, CheckpointOut &cp) const;
1278471SGiacomo.Gabrielli@arm.com        void unserialize(const std::string &base, CheckpointIn &cp);
1289252Sdjordje.kovacevic@arm.com    };
1299252Sdjordje.kovacevic@arm.com
1309252Sdjordje.kovacevic@arm.com    /*
1319252Sdjordje.kovacevic@arm.com     * Interface at each end of the link
1329252Sdjordje.kovacevic@arm.com     */
1339252Sdjordje.kovacevic@arm.com    class Interface : public EtherInt
1349252Sdjordje.kovacevic@arm.com    {
1358471SGiacomo.Gabrielli@arm.com      private:
1368471SGiacomo.Gabrielli@arm.com        Link *txlink;
1375596Sgblack@eecs.umich.edu
1385596Sgblack@eecs.umich.edu      public:
1395596Sgblack@eecs.umich.edu        Interface(const std::string &name, Link *txlink, Link *rxlink);
1405596Sgblack@eecs.umich.edu        bool recvPacket(EthPacketPtr packet) { return txlink->transmit(packet); }
1415596Sgblack@eecs.umich.edu        void sendDone() { peer->sendDone(); }
1425596Sgblack@eecs.umich.edu        bool isBusy() { return txlink->busy(); }
1435596Sgblack@eecs.umich.edu    };
1445596Sgblack@eecs.umich.edu
1455596Sgblack@eecs.umich.edu    Link *link[2];
1465596Sgblack@eecs.umich.edu    Interface *interface[2];
1475596Sgblack@eecs.umich.edu
1485596Sgblack@eecs.umich.edu  public:
1495596Sgblack@eecs.umich.edu    typedef EtherLinkParams Params;
1507783SGiacomo.Gabrielli@arm.com    EtherLink(const Params *p);
1517783SGiacomo.Gabrielli@arm.com    virtual ~EtherLink();
1527783SGiacomo.Gabrielli@arm.com
1539046SAli.Saidi@ARM.com    const Params *
1547783SGiacomo.Gabrielli@arm.com    params() const
1557783SGiacomo.Gabrielli@arm.com    {
1567783SGiacomo.Gabrielli@arm.com        return dynamic_cast<const Params *>(_params);
1575596Sgblack@eecs.umich.edu    }
1585596Sgblack@eecs.umich.edu
1595596Sgblack@eecs.umich.edu    virtual EtherInt *getEthPort(const std::string &if_name, int idx);
1605596Sgblack@eecs.umich.edu
1615596Sgblack@eecs.umich.edu    void serialize(CheckpointOut &cp) const M5_ATTR_OVERRIDE;
1625596Sgblack@eecs.umich.edu    void unserialize(CheckpointIn &cp) M5_ATTR_OVERRIDE;
1635596Sgblack@eecs.umich.edu
1645596Sgblack@eecs.umich.edu};
1655596Sgblack@eecs.umich.edu
1665596Sgblack@eecs.umich.edu#endif // __ETHERLINK_HH__
1675596Sgblack@eecs.umich.edu