etherlink.hh revision 11263
12SN/A/*
211071SN/A * Copyright (c) 2015 ARM Limited
311071SN/A * All rights reserved
411071SN/A *
511071SN/A * The license below extends only to copyright in the software and shall
611071SN/A * not be construed as granting a license to any other intellectual
711071SN/A * property including but not limited to intellectual property relating
811071SN/A * to a hardware implementation of the functionality of the software
911071SN/A * licensed hereunder.  You may use the software subject to the license
1011071SN/A * terms below provided that you ensure that this notice is replicated
1111071SN/A * unmodified and in its entirety in all distributions of the software,
1211071SN/A * modified or unmodified, in source code or in binary form.
1311071SN/A *
141762SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan
152SN/A * All rights reserved.
162SN/A *
172SN/A * Redistribution and use in source and binary forms, with or without
182SN/A * modification, are permitted provided that the following conditions are
192SN/A * met: redistributions of source code must retain the above copyright
202SN/A * notice, this list of conditions and the following disclaimer;
212SN/A * redistributions in binary form must reproduce the above copyright
222SN/A * notice, this list of conditions and the following disclaimer in the
232SN/A * documentation and/or other materials provided with the distribution;
242SN/A * neither the name of the copyright holders nor the names of its
252SN/A * contributors may be used to endorse or promote products derived from
262SN/A * this software without specific prior written permission.
272SN/A *
282SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
292SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
302SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
312SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
322SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
332SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
342SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
352SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
362SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
372SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
382SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
392665SN/A *
402665SN/A * Authors: Nathan Binkert
412SN/A */
422SN/A
432SN/A/* @file
442SN/A * Device module for modelling a fixed bandwidth full duplex ethernet link
452SN/A */
462SN/A
4711263Sandreas.sandberg@arm.com#ifndef __DEV_NET_ETHERLINK_HH__
4811263Sandreas.sandberg@arm.com#define __DEV_NET_ETHERLINK_HH__
492SN/A
5011071SN/A#include <queue>
5111071SN/A
526216SN/A#include "base/types.hh"
5311263Sandreas.sandberg@arm.com#include "dev/net/etherint.hh"
5411263Sandreas.sandberg@arm.com#include "dev/net/etherobject.hh"
5511263Sandreas.sandberg@arm.com#include "dev/net/etherpkt.hh"
564981SN/A#include "params/EtherLink.hh"
571354SN/A#include "sim/eventq.hh"
5856SN/A#include "sim/sim_object.hh"
592SN/A
602SN/Aclass EtherDump;
611435SN/Aclass Checkpoint;
622SN/A/*
632SN/A * Model for a fixed bandwidth full duplex ethernet link
642SN/A */
654981SN/Aclass EtherLink : public EtherObject
662SN/A{
672SN/A  protected:
682SN/A    class Interface;
692SN/A
7011071SN/A    /*
7111071SN/A     * Model for a single uni-directional link
7211071SN/A     */
731435SN/A    class Link
741435SN/A    {
752SN/A      protected:
7611071SN/A        const std::string objName;
77265SN/A
7811071SN/A        EtherLink *const parent;
7911071SN/A        const int number;
801435SN/A
812SN/A        Interface *txint;
822SN/A        Interface *rxint;
832SN/A
8411071SN/A        const double ticksPerByte;
8511071SN/A        const Tick linkDelay;
8611071SN/A        const Tick delayVar;
8711071SN/A        EtherDump *const dump;
882SN/A
892SN/A      protected:
902SN/A        /*
912SN/A         * Transfer is complete
922SN/A         */
932566SN/A        EthPacketPtr packet;
94633SN/A        void txDone();
95633SN/A        typedef EventWrapper<Link, &Link::txDone> DoneEvent;
961354SN/A        friend void DoneEvent::process();
97633SN/A        DoneEvent doneEvent;
982SN/A
9911071SN/A        /**
10011071SN/A         * Maintain a queue of in-flight packets. Assume that the
10111071SN/A         * delay is non-zero and constant (i.e., at most one packet
10211071SN/A         * per tick).
10311071SN/A         */
10411071SN/A        std::deque<std::pair<Tick, EthPacketPtr>> txQueue;
10511071SN/A
10611071SN/A        void processTxQueue();
10711071SN/A        typedef EventWrapper<Link, &Link::processTxQueue> TxQueueEvent;
10811071SN/A        friend void TxQueueEvent::process();
10911071SN/A        TxQueueEvent txQueueEvent;
11011071SN/A
1112566SN/A        void txComplete(EthPacketPtr packet);
1122SN/A
1132SN/A      public:
1141435SN/A        Link(const std::string &name, EtherLink *p, int num,
1151954SN/A             double rate, Tick delay, Tick delay_var, EtherDump *dump);
1162SN/A        ~Link() {}
1172SN/A
1181435SN/A        const std::string name() const { return objName; }
119265SN/A
1202SN/A        bool busy() const { return (bool)packet; }
1212566SN/A        bool transmit(EthPacketPtr packet);
1222SN/A
1232SN/A        void setTxInt(Interface *i) { assert(!txint); txint = i; }
1242SN/A        void setRxInt(Interface *i) { assert(!rxint); rxint = i; }
125558SN/A
12610905SN/A        void serialize(const std::string &base, CheckpointOut &cp) const;
12710905SN/A        void unserialize(const std::string &base, CheckpointIn &cp);
1282SN/A    };
1292SN/A
1302SN/A    /*
1312SN/A     * Interface at each end of the link
1322SN/A     */
1332SN/A    class Interface : public EtherInt
1342SN/A    {
1352SN/A      private:
1362SN/A        Link *txlink;
1372SN/A
1382SN/A      public:
1392SN/A        Interface(const std::string &name, Link *txlink, Link *rxlink);
1402566SN/A        bool recvPacket(EthPacketPtr packet) { return txlink->transmit(packet); }
1411017SN/A        void sendDone() { peer->sendDone(); }
1424419SN/A        bool isBusy() { return txlink->busy(); }
1432SN/A    };
1442SN/A
1451435SN/A    Link *link[2];
1464981SN/A    Interface *interface[2];
1472SN/A
1482SN/A  public:
1494981SN/A    typedef EtherLinkParams Params;
1504981SN/A    EtherLink(const Params *p);
1512SN/A    virtual ~EtherLink();
152558SN/A
1534981SN/A    const Params *
1544981SN/A    params() const
1554981SN/A    {
1564981SN/A        return dynamic_cast<const Params *>(_params);
1574981SN/A    }
1584981SN/A
15911169SN/A    EtherInt *getEthPort(const std::string &if_name, int idx) override;
1604981SN/A
16111168SN/A    void serialize(CheckpointOut &cp) const override;
16211168SN/A    void unserialize(CheckpointIn &cp) override;
163558SN/A
1642SN/A};
1652SN/A
16611263Sandreas.sandberg@arm.com#endif // __DEV_NET_ETHERLINK_HH__
167