etherlink.hh revision 1762
1/*
2 * Copyright (c) 2002-2005 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
29/* @file
30 * Device module for modelling a fixed bandwidth full duplex ethernet link
31 */
32
33#ifndef __DEV_ETHERLINK_HH__
34#define __DEV_ETHERLINK_HH__
35
36#include "dev/etherint.hh"
37#include "dev/etherpkt.hh"
38#include "sim/eventq.hh"
39#include "sim/host.hh"
40#include "sim/sim_object.hh"
41
42class EtherDump;
43class Checkpoint;
44/*
45 * Model for a fixed bandwidth full duplex ethernet link
46 */
47class EtherLink : public SimObject
48{
49  protected:
50    class Interface;
51
52    friend class LinkDelayEvent;
53     /*
54      * Model for a single uni-directional link
55      */
56    class Link
57    {
58      protected:
59        std::string objName;
60
61        EtherLink *parent;
62        int number;
63
64        Interface *txint;
65        Interface *rxint;
66
67        double ticksPerByte;
68        Tick linkDelay;
69        EtherDump *dump;
70
71      protected:
72        /*
73         * Transfer is complete
74         */
75        PacketPtr packet;
76        void txDone();
77        typedef EventWrapper<Link, &Link::txDone> DoneEvent;
78        friend void DoneEvent::process();
79        DoneEvent doneEvent;
80
81        friend class LinkDelayEvent;
82        void txComplete(PacketPtr packet);
83
84      public:
85        Link(const std::string &name, EtherLink *p, int num,
86             double rate, Tick delay, EtherDump *dump);
87        ~Link() {}
88
89        const std::string name() const { return objName; }
90
91        bool busy() const { return (bool)packet; }
92        bool transmit(PacketPtr packet);
93
94        void setTxInt(Interface *i) { assert(!txint); txint = i; }
95        void setRxInt(Interface *i) { assert(!rxint); rxint = i; }
96
97        void serialize(const std::string &base, std::ostream &os);
98        void unserialize(const std::string &base, Checkpoint *cp,
99                                 const std::string &section);
100    };
101
102    /*
103     * Interface at each end of the link
104     */
105    class Interface : public EtherInt
106    {
107      private:
108        Link *txlink;
109
110      public:
111        Interface(const std::string &name, Link *txlink, Link *rxlink);
112        bool recvPacket(PacketPtr packet) { return txlink->transmit(packet); }
113        void sendDone() { peer->sendDone(); }
114    };
115
116    Link *link[2];
117    EtherInt *interface[2];
118
119  public:
120    EtherLink(const std::string &name, EtherInt *peer0, EtherInt *peer1,
121              double rate, Tick delay, EtherDump *dump);
122    virtual ~EtherLink();
123
124    virtual void serialize(std::ostream &os);
125    virtual void unserialize(Checkpoint *cp, const std::string &section);
126
127};
128
129#endif // __ETHERLINK_HH__
130