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"
4212056Sgabeblack@google.com#include "config/use_tuntap.hh"
4311263Sandreas.sandberg@arm.com#include "dev/net/etherint.hh"
4411263Sandreas.sandberg@arm.com#include "dev/net/etherpkt.hh"
4512056Sgabeblack@google.com
4612056Sgabeblack@google.com#if USE_TUNTAP
4712056Sgabeblack@google.com#include "params/EtherTap.hh"
4812056Sgabeblack@google.com
4912056Sgabeblack@google.com#endif
5012056Sgabeblack@google.com
5112054Sgabeblack@google.com#include "params/EtherTapStub.hh"
5256SN/A#include "sim/eventq.hh"
5356SN/A#include "sim/sim_object.hh"
542SN/A
551872SN/Aclass TapEvent;
5612055Sgabeblack@google.comclass EtherTapInt;
5712055Sgabeblack@google.com
5813784Sgabeblack@google.comclass EtherTapBase : public SimObject
5912055Sgabeblack@google.com{
6012055Sgabeblack@google.com  public:
6112055Sgabeblack@google.com    typedef EtherTapBaseParams Params;
6212055Sgabeblack@google.com    EtherTapBase(const Params *p);
6312055Sgabeblack@google.com    virtual ~EtherTapBase();
6412055Sgabeblack@google.com
6512055Sgabeblack@google.com    const Params *
6612055Sgabeblack@google.com    params() const
6712055Sgabeblack@google.com    {
6812055Sgabeblack@google.com        return dynamic_cast<const Params *>(_params);
6912055Sgabeblack@google.com    }
7012055Sgabeblack@google.com
7112055Sgabeblack@google.com    void serialize(CheckpointOut &cp) const override;
7212055Sgabeblack@google.com    void unserialize(CheckpointIn &cp) override;
7312055Sgabeblack@google.com
7412055Sgabeblack@google.com  protected:
7512055Sgabeblack@google.com    uint8_t *buffer;
7612055Sgabeblack@google.com    int buflen;
7712055Sgabeblack@google.com
7812055Sgabeblack@google.com    EtherDump *dump;
7912055Sgabeblack@google.com
8012055Sgabeblack@google.com
8112055Sgabeblack@google.com    /*
8212055Sgabeblack@google.com     * Interface to the real network.
8312055Sgabeblack@google.com     */
8412055Sgabeblack@google.com  protected:
8512055Sgabeblack@google.com    friend class TapEvent;
8612055Sgabeblack@google.com    TapEvent *event;
8712055Sgabeblack@google.com    void pollFd(int fd);
8812055Sgabeblack@google.com    void stopPolling();
8912055Sgabeblack@google.com
9012055Sgabeblack@google.com    // Receive data from the real network.
9112055Sgabeblack@google.com    virtual void recvReal(int revent) = 0;
9212055Sgabeblack@google.com    // Prepare and send data out to the real network.
9312055Sgabeblack@google.com    virtual bool sendReal(const void *data, size_t len) = 0;
9412055Sgabeblack@google.com
9512055Sgabeblack@google.com
9612055Sgabeblack@google.com    /*
9712055Sgabeblack@google.com     * Interface to the simulated network.
9812055Sgabeblack@google.com     */
9912055Sgabeblack@google.com  protected:
10012055Sgabeblack@google.com    EtherTapInt *interface;
10112055Sgabeblack@google.com
10212055Sgabeblack@google.com  public:
10313784Sgabeblack@google.com    Port &getPort(const std::string &if_name,
10413784Sgabeblack@google.com                  PortID idx=InvalidPortID) override;
10512055Sgabeblack@google.com
10612055Sgabeblack@google.com    bool recvSimulated(EthPacketPtr packet);
10712055Sgabeblack@google.com    void sendSimulated(void *data, size_t len);
10812055Sgabeblack@google.com
10912055Sgabeblack@google.com  protected:
11012055Sgabeblack@google.com    std::queue<EthPacketPtr> packetBuffer;
11112055Sgabeblack@google.com    void retransmit();
11212130Sspwilson2@wisc.edu    EventFunctionWrapper txEvent;
11312055Sgabeblack@google.com};
11412055Sgabeblack@google.com
11512055Sgabeblack@google.comclass EtherTapInt : public EtherInt
11612055Sgabeblack@google.com{
11712055Sgabeblack@google.com  private:
11812055Sgabeblack@google.com    EtherTapBase *tap;
11912055Sgabeblack@google.com  public:
12012055Sgabeblack@google.com    EtherTapInt(const std::string &name, EtherTapBase *t) :
12112055Sgabeblack@google.com            EtherInt(name), tap(t)
12212055Sgabeblack@google.com    { }
12312055Sgabeblack@google.com
12412055Sgabeblack@google.com    bool recvPacket(EthPacketPtr pkt) override
12512055Sgabeblack@google.com        { return tap->recvSimulated(pkt); }
12612055Sgabeblack@google.com    void sendDone() override {}
12712055Sgabeblack@google.com};
12812055Sgabeblack@google.com
12912055Sgabeblack@google.com
1301872SN/Aclass TapListener;
1311872SN/A
1322SN/A/*
13312054Sgabeblack@google.com * Interface to connect a simulated ethernet device to the real world. An
13412054Sgabeblack@google.com * external helper program bridges between this object's TCP port and a
13512054Sgabeblack@google.com * source/sink for Ethernet frames. Each frame going in either direction is
13612054Sgabeblack@google.com * prepended with the frame's length in a 32 bit integer in network byte order.
1372SN/A */
13812055Sgabeblack@google.comclass EtherTapStub : public EtherTapBase
1392SN/A{
1402SN/A  public:
14112054Sgabeblack@google.com    typedef EtherTapStubParams Params;
14212054Sgabeblack@google.com    EtherTapStub(const Params *p);
14312055Sgabeblack@google.com    ~EtherTapStub();
1442SN/A
1454981SN/A    const Params *
1464981SN/A    params() const
1474981SN/A    {
1484981SN/A        return dynamic_cast<const Params *>(_params);
1494981SN/A    }
1504981SN/A
15111168SN/A    void serialize(CheckpointOut &cp) const override;
15211168SN/A    void unserialize(CheckpointIn &cp) override;
15312055Sgabeblack@google.com
15412055Sgabeblack@google.com
15512055Sgabeblack@google.com  protected:
15612055Sgabeblack@google.com    friend class TapListener;
15712055Sgabeblack@google.com    TapListener *listener;
15812055Sgabeblack@google.com
15912055Sgabeblack@google.com    int socket;
16012055Sgabeblack@google.com
16112055Sgabeblack@google.com    void attach(int fd);
16212055Sgabeblack@google.com    void detach();
16312055Sgabeblack@google.com
16412055Sgabeblack@google.com    uint32_t buffer_used;
16512055Sgabeblack@google.com    uint32_t frame_len;
16612055Sgabeblack@google.com
16712055Sgabeblack@google.com    void recvReal(int revent) override;
16812055Sgabeblack@google.com    bool sendReal(const void *data, size_t len) override;
1692SN/A};
1702SN/A
1714981SN/A
17212056Sgabeblack@google.com#if USE_TUNTAP
17312056Sgabeblack@google.comclass EtherTap : public EtherTapBase
17412056Sgabeblack@google.com{
17512056Sgabeblack@google.com  public:
17612056Sgabeblack@google.com    typedef EtherTapParams Params;
17712056Sgabeblack@google.com    EtherTap(const Params *p);
17812056Sgabeblack@google.com    ~EtherTap();
17912056Sgabeblack@google.com
18012056Sgabeblack@google.com    const Params *
18112056Sgabeblack@google.com    params() const
18212056Sgabeblack@google.com    {
18312056Sgabeblack@google.com        return dynamic_cast<const Params *>(_params);
18412056Sgabeblack@google.com    }
18512056Sgabeblack@google.com
18612056Sgabeblack@google.com
18712056Sgabeblack@google.com  protected:
18812056Sgabeblack@google.com    int tap;
18912056Sgabeblack@google.com
19012056Sgabeblack@google.com    void recvReal(int revent) override;
19112056Sgabeblack@google.com    bool sendReal(const void *data, size_t len) override;
19212056Sgabeblack@google.com};
19312056Sgabeblack@google.com#endif
19412056Sgabeblack@google.com
19512056Sgabeblack@google.com
19611263Sandreas.sandberg@arm.com#endif // __DEV_NET_ETHERTAP_HH__
197