ethertap.hh revision 12055
12SN/A/*
21762SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan
32SN/A * All rights reserved.
42SN/A *
52SN/A * Redistribution and use in source and binary forms, with or without
62SN/A * modification, are permitted provided that the following conditions are
72SN/A * met: redistributions of source code must retain the above copyright
82SN/A * notice, this list of conditions and the following disclaimer;
92SN/A * redistributions in binary form must reproduce the above copyright
102SN/A * notice, this list of conditions and the following disclaimer in the
112SN/A * documentation and/or other materials provided with the distribution;
122SN/A * neither the name of the copyright holders nor the names of its
132SN/A * contributors may be used to endorse or promote products derived from
142SN/A * this software without specific prior written permission.
152SN/A *
162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665SN/A *
282665SN/A * Authors: Nathan Binkert
292SN/A */
302SN/A
312SN/A/* @file
322SN/A * Interface to connect a simulated ethernet device to the real world
332SN/A */
342SN/A
3511263Sandreas.sandberg@arm.com#ifndef __DEV_NET_ETHERTAP_HH__
3611263Sandreas.sandberg@arm.com#define __DEV_NET_ETHERTAP_HH__
372SN/A
382SN/A#include <queue>
392SN/A#include <string>
402SN/A
414981SN/A#include "base/pollevent.hh"
4211263Sandreas.sandberg@arm.com#include "dev/net/etherint.hh"
4311263Sandreas.sandberg@arm.com#include "dev/net/etherobject.hh"
4411263Sandreas.sandberg@arm.com#include "dev/net/etherpkt.hh"
4512054Sgabeblack@google.com#include "params/EtherTapStub.hh"
4656SN/A#include "sim/eventq.hh"
4756SN/A#include "sim/sim_object.hh"
482SN/A
491872SN/Aclass TapEvent;
5012055Sgabeblack@google.comclass EtherTapInt;
5112055Sgabeblack@google.com
5212055Sgabeblack@google.comclass EtherTapBase : public EtherObject
5312055Sgabeblack@google.com{
5412055Sgabeblack@google.com  public:
5512055Sgabeblack@google.com    typedef EtherTapBaseParams Params;
5612055Sgabeblack@google.com    EtherTapBase(const Params *p);
5712055Sgabeblack@google.com    virtual ~EtherTapBase();
5812055Sgabeblack@google.com
5912055Sgabeblack@google.com    const Params *
6012055Sgabeblack@google.com    params() const
6112055Sgabeblack@google.com    {
6212055Sgabeblack@google.com        return dynamic_cast<const Params *>(_params);
6312055Sgabeblack@google.com    }
6412055Sgabeblack@google.com
6512055Sgabeblack@google.com    void serialize(CheckpointOut &cp) const override;
6612055Sgabeblack@google.com    void unserialize(CheckpointIn &cp) override;
6712055Sgabeblack@google.com
6812055Sgabeblack@google.com  protected:
6912055Sgabeblack@google.com    uint8_t *buffer;
7012055Sgabeblack@google.com    int buflen;
7112055Sgabeblack@google.com
7212055Sgabeblack@google.com    EtherDump *dump;
7312055Sgabeblack@google.com
7412055Sgabeblack@google.com
7512055Sgabeblack@google.com    /*
7612055Sgabeblack@google.com     * Interface to the real network.
7712055Sgabeblack@google.com     */
7812055Sgabeblack@google.com  protected:
7912055Sgabeblack@google.com    friend class TapEvent;
8012055Sgabeblack@google.com    TapEvent *event;
8112055Sgabeblack@google.com    void pollFd(int fd);
8212055Sgabeblack@google.com    void stopPolling();
8312055Sgabeblack@google.com
8412055Sgabeblack@google.com    // Receive data from the real network.
8512055Sgabeblack@google.com    virtual void recvReal(int revent) = 0;
8612055Sgabeblack@google.com    // Prepare and send data out to the real network.
8712055Sgabeblack@google.com    virtual bool sendReal(const void *data, size_t len) = 0;
8812055Sgabeblack@google.com
8912055Sgabeblack@google.com
9012055Sgabeblack@google.com    /*
9112055Sgabeblack@google.com     * Interface to the simulated network.
9212055Sgabeblack@google.com     */
9312055Sgabeblack@google.com  protected:
9412055Sgabeblack@google.com    EtherTapInt *interface;
9512055Sgabeblack@google.com
9612055Sgabeblack@google.com  public:
9712055Sgabeblack@google.com    EtherInt *getEthPort(const std::string &if_name, int idx) override;
9812055Sgabeblack@google.com
9912055Sgabeblack@google.com    bool recvSimulated(EthPacketPtr packet);
10012055Sgabeblack@google.com    void sendSimulated(void *data, size_t len);
10112055Sgabeblack@google.com
10212055Sgabeblack@google.com  protected:
10312055Sgabeblack@google.com    std::queue<EthPacketPtr> packetBuffer;
10412055Sgabeblack@google.com    void retransmit();
10512055Sgabeblack@google.com
10612055Sgabeblack@google.com    class TxEvent : public Event
10712055Sgabeblack@google.com    {
10812055Sgabeblack@google.com      protected:
10912055Sgabeblack@google.com        EtherTapBase *tap;
11012055Sgabeblack@google.com
11112055Sgabeblack@google.com      public:
11212055Sgabeblack@google.com        TxEvent(EtherTapBase *_tap) : tap(_tap) {}
11312055Sgabeblack@google.com        void process() { tap->retransmit(); }
11412055Sgabeblack@google.com        virtual const char *description() const
11512055Sgabeblack@google.com            { return "EtherTapBase retransmit"; }
11612055Sgabeblack@google.com    };
11712055Sgabeblack@google.com
11812055Sgabeblack@google.com    friend class TxEvent;
11912055Sgabeblack@google.com    TxEvent txEvent;
12012055Sgabeblack@google.com};
12112055Sgabeblack@google.com
12212055Sgabeblack@google.comclass EtherTapInt : public EtherInt
12312055Sgabeblack@google.com{
12412055Sgabeblack@google.com  private:
12512055Sgabeblack@google.com    EtherTapBase *tap;
12612055Sgabeblack@google.com  public:
12712055Sgabeblack@google.com    EtherTapInt(const std::string &name, EtherTapBase *t) :
12812055Sgabeblack@google.com            EtherInt(name), tap(t)
12912055Sgabeblack@google.com    { }
13012055Sgabeblack@google.com
13112055Sgabeblack@google.com    bool recvPacket(EthPacketPtr pkt) override
13212055Sgabeblack@google.com        { return tap->recvSimulated(pkt); }
13312055Sgabeblack@google.com    void sendDone() override {}
13412055Sgabeblack@google.com};
13512055Sgabeblack@google.com
13612055Sgabeblack@google.com
1371872SN/Aclass TapListener;
1381872SN/A
1392SN/A/*
14012054Sgabeblack@google.com * Interface to connect a simulated ethernet device to the real world. An
14112054Sgabeblack@google.com * external helper program bridges between this object's TCP port and a
14212054Sgabeblack@google.com * source/sink for Ethernet frames. Each frame going in either direction is
14312054Sgabeblack@google.com * prepended with the frame's length in a 32 bit integer in network byte order.
1442SN/A */
14512055Sgabeblack@google.comclass EtherTapStub : public EtherTapBase
1462SN/A{
1472SN/A  public:
14812054Sgabeblack@google.com    typedef EtherTapStubParams Params;
14912054Sgabeblack@google.com    EtherTapStub(const Params *p);
15012055Sgabeblack@google.com    ~EtherTapStub();
1512SN/A
1524981SN/A    const Params *
1534981SN/A    params() const
1544981SN/A    {
1554981SN/A        return dynamic_cast<const Params *>(_params);
1564981SN/A    }
1574981SN/A
15811168SN/A    void serialize(CheckpointOut &cp) const override;
15911168SN/A    void unserialize(CheckpointIn &cp) override;
16012055Sgabeblack@google.com
16112055Sgabeblack@google.com
16212055Sgabeblack@google.com  protected:
16312055Sgabeblack@google.com    friend class TapListener;
16412055Sgabeblack@google.com    TapListener *listener;
16512055Sgabeblack@google.com
16612055Sgabeblack@google.com    int socket;
16712055Sgabeblack@google.com
16812055Sgabeblack@google.com    void attach(int fd);
16912055Sgabeblack@google.com    void detach();
17012055Sgabeblack@google.com
17112055Sgabeblack@google.com    uint32_t buffer_used;
17212055Sgabeblack@google.com    uint32_t frame_len;
17312055Sgabeblack@google.com
17412055Sgabeblack@google.com    void recvReal(int revent) override;
17512055Sgabeblack@google.com    bool sendReal(const void *data, size_t len) override;
1762SN/A};
1772SN/A
1784981SN/A
17911263Sandreas.sandberg@arm.com#endif // __DEV_NET_ETHERTAP_HH__
180