ethertap.hh revision 12054:ab04045965d1
1/*
2 * Copyright (c) 2003-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 * Authors: Nathan Binkert
29 */
30
31/* @file
32 * Interface to connect a simulated ethernet device to the real world
33 */
34
35#ifndef __DEV_NET_ETHERTAP_HH__
36#define __DEV_NET_ETHERTAP_HH__
37
38#include <queue>
39#include <string>
40
41#include "base/pollevent.hh"
42#include "dev/net/etherint.hh"
43#include "dev/net/etherobject.hh"
44#include "dev/net/etherpkt.hh"
45#include "params/EtherTapStub.hh"
46#include "sim/eventq.hh"
47#include "sim/sim_object.hh"
48
49class TapEvent;
50class TapListener;
51class EtherTapInt;
52
53/*
54 * Interface to connect a simulated ethernet device to the real world. An
55 * external helper program bridges between this object's TCP port and a
56 * source/sink for Ethernet frames. Each frame going in either direction is
57 * prepended with the frame's length in a 32 bit integer in network byte order.
58 */
59class EtherTapStub : public EtherObject
60{
61  protected:
62    friend class TapEvent;
63    TapEvent *event;
64
65  protected:
66    friend class TapListener;
67    TapListener *listener;
68    int socket;
69    char *buffer;
70    int buflen;
71    uint32_t buffer_offset;
72    uint32_t data_len;
73
74    EtherDump *dump;
75
76    void attach(int fd);
77    void detach();
78
79  protected:
80    std::string device;
81    std::queue<EthPacketPtr> packetBuffer;
82    EtherTapInt *interface;
83
84    void process(int revent);
85    void enqueue(EthPacketData *packet);
86    void retransmit();
87
88    /*
89     */
90    class TxEvent : public Event
91    {
92      protected:
93        EtherTapStub *tap;
94
95      public:
96        TxEvent(EtherTapStub *_tap) : tap(_tap) {}
97        void process() { tap->retransmit(); }
98        virtual const char *description() const
99            { return "EtherTapStub retransmit"; }
100    };
101
102    friend class TxEvent;
103    TxEvent txEvent;
104
105  public:
106    typedef EtherTapStubParams Params;
107    EtherTapStub(const Params *p);
108    virtual ~EtherTapStub();
109
110    const Params *
111    params() const
112    {
113        return dynamic_cast<const Params *>(_params);
114    }
115
116    EtherInt *getEthPort(const std::string &if_name, int idx) override;
117
118    virtual bool recvPacket(EthPacketPtr packet);
119    virtual void sendDone();
120
121    void serialize(CheckpointOut &cp) const override;
122    void unserialize(CheckpointIn &cp) override;
123};
124
125class EtherTapInt : public EtherInt
126{
127  private:
128    EtherTapStub *tap;
129  public:
130    EtherTapInt(const std::string &name, EtherTapStub *t)
131            : EtherInt(name), tap(t)
132    { }
133
134    virtual bool recvPacket(EthPacketPtr pkt) { return tap->recvPacket(pkt); }
135    virtual void sendDone() { tap->sendDone(); }
136};
137
138
139#endif // __DEV_NET_ETHERTAP_HH__
140