110923SN/A/*
210923SN/A * Copyright (c) 2015 ARM Limited
310923SN/A * All rights reserved
410923SN/A *
510923SN/A * The license below extends only to copyright in the software and shall
610923SN/A * not be construed as granting a license to any other intellectual
710923SN/A * property including but not limited to intellectual property relating
810923SN/A * to a hardware implementation of the functionality of the software
910923SN/A * licensed hereunder.  You may use the software subject to the license
1010923SN/A * terms below provided that you ensure that this notice is replicated
1110923SN/A * unmodified and in its entirety in all distributions of the software,
1210923SN/A * modified or unmodified, in source code or in binary form.
1310923SN/A *
1410923SN/A * Redistribution and use in source and binary forms, with or without
1510923SN/A * modification, are permitted provided that the following conditions are
1610923SN/A * met: redistributions of source code must retain the above copyright
1710923SN/A * notice, this list of conditions and the following disclaimer;
1810923SN/A * redistributions in binary form must reproduce the above copyright
1910923SN/A * notice, this list of conditions and the following disclaimer in the
2010923SN/A * documentation and/or other materials provided with the distribution;
2110923SN/A * neither the name of the copyright holders nor the names of its
2210923SN/A * contributors may be used to endorse or promote products derived from
2310923SN/A * this software without specific prior written permission.
2410923SN/A *
2510923SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2610923SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2710923SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2810923SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2910923SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3010923SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3110923SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3210923SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3310923SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3410923SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3510923SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3610923SN/A *
3710923SN/A * Authors: Gabor Dozsa
3810923SN/A */
3910923SN/A
4010923SN/A/* @file
4111290Sgabor.dozsa@arm.com * Device module for a full duplex ethernet link for dist gem5 simulations.
4210923SN/A *
4311290Sgabor.dozsa@arm.com * See comments in dev/net/dist_iface.hh for a generic description of dist
4410923SN/A * gem5 simulations.
4510923SN/A *
4610923SN/A * This class is meant to be a drop in replacement for the EtherLink class for
4711290Sgabor.dozsa@arm.com * dist gem5 runs.
4810923SN/A *
4910923SN/A */
5011290Sgabor.dozsa@arm.com#ifndef __DEV_DIST_ETHERLINK_HH__
5111290Sgabor.dozsa@arm.com#define __DEV_DIST_ETHERLINK_HH__
5210923SN/A
5310923SN/A#include <iostream>
5410923SN/A
5511263SN/A#include "dev/net/etherlink.hh"
5611290Sgabor.dozsa@arm.com#include "params/DistEtherLink.hh"
5710923SN/A
5811290Sgabor.dozsa@arm.comclass DistIface;
5910923SN/Aclass EthPacketData;
6010923SN/A
6110923SN/A/**
6210923SN/A * Model for a fixed bandwidth full duplex ethernet link.
6310923SN/A */
6413784Sgabeblack@google.comclass DistEtherLink : public SimObject
6510923SN/A{
6610923SN/A  protected:
6710923SN/A    class LocalIface;
6810923SN/A
6910923SN/A    /**
7010923SN/A     * Model base class for a single uni-directional link.
7110923SN/A     *
7210923SN/A     * The link will encapsulate and transfer Ethernet packets to/from
7310923SN/A     * the message server.
7410923SN/A     */
7511290Sgabor.dozsa@arm.com    class Link : public Serializable
7610923SN/A    {
7710923SN/A      protected:
7810923SN/A        std::string objName;
7911290Sgabor.dozsa@arm.com        DistEtherLink *parent;
8010923SN/A        LocalIface *localIface;
8110923SN/A        EtherDump *dump;
8211290Sgabor.dozsa@arm.com        DistIface *distIface;
8310923SN/A        Event *event;
8410923SN/A        EthPacketPtr packet;
8510923SN/A
8610923SN/A      public:
8711290Sgabor.dozsa@arm.com        Link(const std::string &name, DistEtherLink *p,
8810923SN/A             EtherDump *d, Event *e) :
8910923SN/A            objName(name), parent(p), localIface(nullptr), dump(d),
9011290Sgabor.dozsa@arm.com            distIface(nullptr), event(e) {}
9110923SN/A
9210923SN/A        ~Link() {}
9310923SN/A
9410923SN/A        const std::string name() const { return objName; }
9510923SN/A        bool busy() const { return (bool)packet; }
9610923SN/A        void setLocalInt(LocalIface *i) { assert(!localIface); localIface=i; }
9710923SN/A
9811290Sgabor.dozsa@arm.com        void serialize(CheckpointOut &cp) const override;
9911290Sgabor.dozsa@arm.com        void unserialize(CheckpointIn &cp) override;
10010923SN/A    };
10110923SN/A
10210923SN/A    /**
10310923SN/A     * Model for a send link.
10410923SN/A     */
10510923SN/A    class TxLink : public Link
10610923SN/A    {
10710923SN/A      protected:
10810923SN/A        /**
10910923SN/A         * Per byte send delay
11010923SN/A         */
11110923SN/A        double ticksPerByte;
11210923SN/A        /**
11310923SN/A         * Random component of the send delay
11410923SN/A         */
11510923SN/A        Tick delayVar;
11610923SN/A
11710923SN/A        /**
11810923SN/A         * Send done callback. Called from doneEvent.
11910923SN/A         */
12010923SN/A        void txDone();
12112087Sspwilson2@wisc.edu        EventFunctionWrapper doneEvent;
12210923SN/A
12310923SN/A      public:
12411290Sgabor.dozsa@arm.com        TxLink(const std::string &name, DistEtherLink *p,
12510923SN/A               double invBW, Tick delay_var, EtherDump *d) :
12610923SN/A            Link(name, p, d, &doneEvent), ticksPerByte(invBW),
12712087Sspwilson2@wisc.edu            delayVar(delay_var), doneEvent([this]{ txDone(); }, name) {}
12810923SN/A        ~TxLink() {}
12910923SN/A
13010923SN/A        /**
13111290Sgabor.dozsa@arm.com         * Register the dist interface to be used to talk to the
13210923SN/A         * peer gem5 processes.
13310923SN/A         */
13411290Sgabor.dozsa@arm.com        void setDistInt(DistIface *m) { assert(!distIface); distIface=m; }
13510923SN/A
13610923SN/A        /**
13710923SN/A         * Initiate sending of a packet via this link.
13810923SN/A         *
13910923SN/A         * @param packet Ethernet packet to send
14010923SN/A         */
14110923SN/A        bool transmit(EthPacketPtr packet);
14210923SN/A    };
14310923SN/A
14410923SN/A    /**
14510923SN/A     * Model for a receive link.
14610923SN/A     */
14710923SN/A    class RxLink : public Link
14810923SN/A    {
14910923SN/A      protected:
15010923SN/A
15110923SN/A        /**
15210923SN/A         * Transmission delay for the simulated Ethernet link.
15310923SN/A         */
15410923SN/A        Tick linkDelay;
15510923SN/A
15610923SN/A        /**
15710923SN/A         * Receive done callback method. Called from doneEvent.
15810923SN/A         */
15911290Sgabor.dozsa@arm.com        void rxDone();
16012087Sspwilson2@wisc.edu        EventFunctionWrapper _doneEvent;
16110923SN/A
16210923SN/A      public:
16310923SN/A
16411290Sgabor.dozsa@arm.com        RxLink(const std::string &name, DistEtherLink *p,
16510923SN/A               Tick delay, EtherDump *d) :
16612087Sspwilson2@wisc.edu            Link(name, p, d, &_doneEvent), linkDelay(delay),
16712087Sspwilson2@wisc.edu            _doneEvent([this]{ rxDone(); }, name) {}
16810923SN/A        ~RxLink() {}
16910923SN/A
17010923SN/A        /**
17111290Sgabor.dozsa@arm.com         * Register our dist interface to talk to the peer gem5 processes.
17210923SN/A         */
17311290Sgabor.dozsa@arm.com        void setDistInt(DistIface *m);
17411290Sgabor.dozsa@arm.com        /**
17511290Sgabor.dozsa@arm.com         * Done events will be scheduled by DistIface (so we need the accessor)
17611290Sgabor.dozsa@arm.com         */
17712087Sspwilson2@wisc.edu        const EventFunctionWrapper *doneEvent() const { return &_doneEvent; }
17810923SN/A    };
17910923SN/A
18010923SN/A    /**
18110923SN/A     * Interface to the local simulated system
18210923SN/A     */
18310923SN/A    class LocalIface : public EtherInt
18410923SN/A    {
18510923SN/A      private:
18610923SN/A        TxLink *txLink;
18710923SN/A
18810923SN/A      public:
18910923SN/A        LocalIface(const std::string &name, TxLink *tx, RxLink *rx,
19011290Sgabor.dozsa@arm.com                   DistIface *m);
19110923SN/A
19210923SN/A        bool recvPacket(EthPacketPtr pkt) { return txLink->transmit(pkt); }
19310923SN/A        void sendDone() { peer->sendDone(); }
19410923SN/A        bool isBusy() { return txLink->busy(); }
19510923SN/A    };
19610923SN/A
19710923SN/A
19810923SN/A  protected:
19910923SN/A    /**
20010923SN/A     * Interface to talk to the peer gem5 processes.
20110923SN/A     */
20211290Sgabor.dozsa@arm.com    DistIface *distIface;
20310923SN/A    /**
20410923SN/A     * Send link
20510923SN/A     */
20610923SN/A    TxLink *txLink;
20710923SN/A    /**
20810923SN/A     * Receive link
20910923SN/A     */
21010923SN/A    RxLink *rxLink;
21110923SN/A    LocalIface *localIface;
21210923SN/A
21311290Sgabor.dozsa@arm.com    Tick linkDelay;
21411290Sgabor.dozsa@arm.com
21510923SN/A  public:
21611290Sgabor.dozsa@arm.com    typedef DistEtherLinkParams Params;
21711290Sgabor.dozsa@arm.com    DistEtherLink(const Params *p);
21811290Sgabor.dozsa@arm.com    ~DistEtherLink();
21910923SN/A
22010923SN/A    const Params *
22110923SN/A    params() const
22210923SN/A    {
22310923SN/A        return dynamic_cast<const Params *>(_params);
22410923SN/A    }
22510923SN/A
22613784Sgabeblack@google.com    Port &getPort(const std::string &if_name,
22713784Sgabeblack@google.com                  PortID idx=InvalidPortID) override;
22810923SN/A
22911290Sgabor.dozsa@arm.com    virtual void init() override;
23011290Sgabor.dozsa@arm.com    virtual void startup() override;
23110923SN/A
23211168SN/A    void serialize(CheckpointOut &cp) const override;
23311168SN/A    void unserialize(CheckpointIn &cp) override;
23410923SN/A};
23510923SN/A
23611290Sgabor.dozsa@arm.com#endif // __DEV_DIST_ETHERLINK_HH__
237