ethertap.hh revision 11168
110260SAndrew.Bardsley@arm.com/*
210260SAndrew.Bardsley@arm.com * Copyright (c) 2003-2005 The Regents of The University of Michigan
310260SAndrew.Bardsley@arm.com * All rights reserved.
410260SAndrew.Bardsley@arm.com *
510260SAndrew.Bardsley@arm.com * Redistribution and use in source and binary forms, with or without
610260SAndrew.Bardsley@arm.com * modification, are permitted provided that the following conditions are
710260SAndrew.Bardsley@arm.com * met: redistributions of source code must retain the above copyright
810260SAndrew.Bardsley@arm.com * notice, this list of conditions and the following disclaimer;
910260SAndrew.Bardsley@arm.com * redistributions in binary form must reproduce the above copyright
1010260SAndrew.Bardsley@arm.com * notice, this list of conditions and the following disclaimer in the
1110260SAndrew.Bardsley@arm.com * documentation and/or other materials provided with the distribution;
1210260SAndrew.Bardsley@arm.com * neither the name of the copyright holders nor the names of its
1310260SAndrew.Bardsley@arm.com * contributors may be used to endorse or promote products derived from
1410260SAndrew.Bardsley@arm.com * this software without specific prior written permission.
1510260SAndrew.Bardsley@arm.com *
1610260SAndrew.Bardsley@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1710260SAndrew.Bardsley@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1810260SAndrew.Bardsley@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1910260SAndrew.Bardsley@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2010260SAndrew.Bardsley@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2110260SAndrew.Bardsley@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2210260SAndrew.Bardsley@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2310260SAndrew.Bardsley@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2410260SAndrew.Bardsley@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2510260SAndrew.Bardsley@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2610260SAndrew.Bardsley@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2711680SCurtis.Dunham@arm.com *
2810260SAndrew.Bardsley@arm.com * Authors: Nathan Binkert
2910260SAndrew.Bardsley@arm.com */
3010260SAndrew.Bardsley@arm.com
3110260SAndrew.Bardsley@arm.com/* @file
3210260SAndrew.Bardsley@arm.com * Interface to connect a simulated ethernet device to the real world
3310260SAndrew.Bardsley@arm.com */
3410260SAndrew.Bardsley@arm.com
3510260SAndrew.Bardsley@arm.com#ifndef __ETHERTAP_HH__
3610260SAndrew.Bardsley@arm.com#define __ETHERTAP_HH__
3710260SAndrew.Bardsley@arm.com
3810260SAndrew.Bardsley@arm.com#include <queue>
3910260SAndrew.Bardsley@arm.com#include <string>
4010260SAndrew.Bardsley@arm.com
4110260SAndrew.Bardsley@arm.com#include "base/pollevent.hh"
4210260SAndrew.Bardsley@arm.com#include "dev/etherint.hh"
4310260SAndrew.Bardsley@arm.com#include "dev/etherobject.hh"
4410260SAndrew.Bardsley@arm.com#include "dev/etherpkt.hh"
4510260SAndrew.Bardsley@arm.com#include "params/EtherTap.hh"
4610260SAndrew.Bardsley@arm.com#include "sim/eventq.hh"
4710260SAndrew.Bardsley@arm.com#include "sim/sim_object.hh"
4810260SAndrew.Bardsley@arm.com
4910260SAndrew.Bardsley@arm.comclass TapEvent;
5010260SAndrew.Bardsley@arm.comclass TapListener;
5110260SAndrew.Bardsley@arm.comclass EtherTapInt;
5210260SAndrew.Bardsley@arm.com
5310260SAndrew.Bardsley@arm.com/*
5410260SAndrew.Bardsley@arm.com * Interface to connect a simulated ethernet device to the real world
5510260SAndrew.Bardsley@arm.com */
5610260SAndrew.Bardsley@arm.comclass EtherTap : public EtherObject
5710260SAndrew.Bardsley@arm.com{
5810260SAndrew.Bardsley@arm.com  protected:
5910260SAndrew.Bardsley@arm.com    friend class TapEvent;
6010260SAndrew.Bardsley@arm.com    TapEvent *event;
6110260SAndrew.Bardsley@arm.com
6210260SAndrew.Bardsley@arm.com  protected:
6310260SAndrew.Bardsley@arm.com    friend class TapListener;
6410260SAndrew.Bardsley@arm.com    TapListener *listener;
6510260SAndrew.Bardsley@arm.com    int socket;
6610260SAndrew.Bardsley@arm.com    char *buffer;
6710260SAndrew.Bardsley@arm.com    int buflen;
6810260SAndrew.Bardsley@arm.com    uint32_t buffer_offset;
6910260SAndrew.Bardsley@arm.com    uint32_t data_len;
7010260SAndrew.Bardsley@arm.com
7110260SAndrew.Bardsley@arm.com    EtherDump *dump;
7210260SAndrew.Bardsley@arm.com
7310260SAndrew.Bardsley@arm.com    void attach(int fd);
7410260SAndrew.Bardsley@arm.com    void detach();
7510260SAndrew.Bardsley@arm.com
7610260SAndrew.Bardsley@arm.com  protected:
7710260SAndrew.Bardsley@arm.com    std::string device;
7810260SAndrew.Bardsley@arm.com    std::queue<EthPacketPtr> packetBuffer;
7910260SAndrew.Bardsley@arm.com    EtherTapInt *interface;
8010260SAndrew.Bardsley@arm.com
8110260SAndrew.Bardsley@arm.com    void process(int revent);
8210260SAndrew.Bardsley@arm.com    void enqueue(EthPacketData *packet);
8310260SAndrew.Bardsley@arm.com    void retransmit();
8410260SAndrew.Bardsley@arm.com
8510260SAndrew.Bardsley@arm.com    /*
8610260SAndrew.Bardsley@arm.com     */
8710260SAndrew.Bardsley@arm.com    class TxEvent : public Event
8810260SAndrew.Bardsley@arm.com    {
8910260SAndrew.Bardsley@arm.com      protected:
9010260SAndrew.Bardsley@arm.com        EtherTap *tap;
9110260SAndrew.Bardsley@arm.com
9210260SAndrew.Bardsley@arm.com      public:
9310260SAndrew.Bardsley@arm.com        TxEvent(EtherTap *_tap) : tap(_tap) {}
9410260SAndrew.Bardsley@arm.com        void process() { tap->retransmit(); }
9510260SAndrew.Bardsley@arm.com        virtual const char *description() const
9610260SAndrew.Bardsley@arm.com            { return "EtherTap retransmit"; }
9710260SAndrew.Bardsley@arm.com    };
9810260SAndrew.Bardsley@arm.com
9910260SAndrew.Bardsley@arm.com    friend class TxEvent;
10010260SAndrew.Bardsley@arm.com    TxEvent txEvent;
10110260SAndrew.Bardsley@arm.com
10210260SAndrew.Bardsley@arm.com  public:
10310260SAndrew.Bardsley@arm.com    typedef EtherTapParams Params;
10410260SAndrew.Bardsley@arm.com    EtherTap(const Params *p);
10510260SAndrew.Bardsley@arm.com    virtual ~EtherTap();
10610260SAndrew.Bardsley@arm.com
10710260SAndrew.Bardsley@arm.com    const Params *
10810260SAndrew.Bardsley@arm.com    params() const
10911680SCurtis.Dunham@arm.com    {
11011680SCurtis.Dunham@arm.com        return dynamic_cast<const Params *>(_params);
11111680SCurtis.Dunham@arm.com    }
11211680SCurtis.Dunham@arm.com
11311680SCurtis.Dunham@arm.com    virtual EtherInt *getEthPort(const std::string &if_name, int idx);
11411680SCurtis.Dunham@arm.com
11511680SCurtis.Dunham@arm.com    virtual bool recvPacket(EthPacketPtr packet);
11611680SCurtis.Dunham@arm.com    virtual void sendDone();
11711680SCurtis.Dunham@arm.com
11811680SCurtis.Dunham@arm.com    void serialize(CheckpointOut &cp) const override;
11911680SCurtis.Dunham@arm.com    void unserialize(CheckpointIn &cp) override;
12011680SCurtis.Dunham@arm.com};
12111680SCurtis.Dunham@arm.com
12211680SCurtis.Dunham@arm.comclass EtherTapInt : public EtherInt
12311680SCurtis.Dunham@arm.com{
12411680SCurtis.Dunham@arm.com  private:
12511680SCurtis.Dunham@arm.com    EtherTap *tap;
12611680SCurtis.Dunham@arm.com  public:
12711680SCurtis.Dunham@arm.com    EtherTapInt(const std::string &name, EtherTap *t)
12811680SCurtis.Dunham@arm.com            : EtherInt(name), tap(t)
12911680SCurtis.Dunham@arm.com    { }
13011680SCurtis.Dunham@arm.com
13111680SCurtis.Dunham@arm.com    virtual bool recvPacket(EthPacketPtr pkt) { return tap->recvPacket(pkt); }
13211680SCurtis.Dunham@arm.com    virtual void sendDone() { tap->sendDone(); }
13311680SCurtis.Dunham@arm.com};
13411680SCurtis.Dunham@arm.com
13511680SCurtis.Dunham@arm.com
13611680SCurtis.Dunham@arm.com#endif // __ETHERTAP_HH__
13711680SCurtis.Dunham@arm.com