ethertap.hh revision 12054
16145Snate@binkert.org/*
26145Snate@binkert.org * Copyright (c) 2003-2005 The Regents of The University of Michigan
36145Snate@binkert.org * All rights reserved.
46145Snate@binkert.org *
56145Snate@binkert.org * Redistribution and use in source and binary forms, with or without
66145Snate@binkert.org * modification, are permitted provided that the following conditions are
76145Snate@binkert.org * met: redistributions of source code must retain the above copyright
86145Snate@binkert.org * notice, this list of conditions and the following disclaimer;
96145Snate@binkert.org * redistributions in binary form must reproduce the above copyright
106145Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
116145Snate@binkert.org * documentation and/or other materials provided with the distribution;
126145Snate@binkert.org * neither the name of the copyright holders nor the names of its
136145Snate@binkert.org * contributors may be used to endorse or promote products derived from
146145Snate@binkert.org * this software without specific prior written permission.
156145Snate@binkert.org *
166145Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
176145Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
186145Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
196145Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
206145Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
216145Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
226145Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236145Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246145Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256145Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
266145Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276145Snate@binkert.org *
286145Snate@binkert.org * Authors: Nathan Binkert
296145Snate@binkert.org */
307054Snate@binkert.org
316145Snate@binkert.org/* @file
326145Snate@binkert.org * Interface to connect a simulated ethernet device to the real world
336145Snate@binkert.org */
346145Snate@binkert.org
356145Snate@binkert.org#ifndef __DEV_NET_ETHERTAP_HH__
366145Snate@binkert.org#define __DEV_NET_ETHERTAP_HH__
376145Snate@binkert.org
386145Snate@binkert.org#include <queue>
396145Snate@binkert.org#include <string>
406145Snate@binkert.org
416145Snate@binkert.org#include "base/pollevent.hh"
426145Snate@binkert.org#include "dev/net/etherint.hh"
436145Snate@binkert.org#include "dev/net/etherobject.hh"
446145Snate@binkert.org#include "dev/net/etherpkt.hh"
456145Snate@binkert.org#include "params/EtherTapStub.hh"
466145Snate@binkert.org#include "sim/eventq.hh"
476145Snate@binkert.org#include "sim/sim_object.hh"
486145Snate@binkert.org
496145Snate@binkert.orgclass TapEvent;
506145Snate@binkert.orgclass TapListener;
516145Snate@binkert.orgclass EtherTapInt;
526145Snate@binkert.org
536145Snate@binkert.org/*
546145Snate@binkert.org * Interface to connect a simulated ethernet device to the real world. An
556284Snate@binkert.org * external helper program bridges between this object's TCP port and a
566145Snate@binkert.org * source/sink for Ethernet frames. Each frame going in either direction is
576145Snate@binkert.org * prepended with the frame's length in a 32 bit integer in network byte order.
586145Snate@binkert.org */
596145Snate@binkert.orgclass EtherTapStub : public EtherObject
606145Snate@binkert.org{
616145Snate@binkert.org  protected:
626145Snate@binkert.org    friend class TapEvent;
637054Snate@binkert.org    TapEvent *event;
647054Snate@binkert.org
656145Snate@binkert.org  protected:
667055Snate@binkert.org    friend class TapListener;
677055Snate@binkert.org    TapListener *listener;
687054Snate@binkert.org    int socket;
696154Snate@binkert.org    char *buffer;
706154Snate@binkert.org    int buflen;
716154Snate@binkert.org    uint32_t buffer_offset;
727054Snate@binkert.org    uint32_t data_len;
736876Ssteve.reinhardt@amd.com
746145Snate@binkert.org    EtherDump *dump;
756145Snate@binkert.org
766145Snate@binkert.org    void attach(int fd);
776145Snate@binkert.org    void detach();
786145Snate@binkert.org
796145Snate@binkert.org  protected:
806145Snate@binkert.org    std::string device;
817054Snate@binkert.org    std::queue<EthPacketPtr> packetBuffer;
827054Snate@binkert.org    EtherTapInt *interface;
837054Snate@binkert.org
846876Ssteve.reinhardt@amd.com    void process(int revent);
856876Ssteve.reinhardt@amd.com    void enqueue(EthPacketData *packet);
867054Snate@binkert.org    void retransmit();
876145Snate@binkert.org
887054Snate@binkert.org    /*
896145Snate@binkert.org     */
907055Snate@binkert.org    class TxEvent : public Event
917054Snate@binkert.org    {
927055Snate@binkert.org      protected:
936285Snate@binkert.org        EtherTapStub *tap;
947054Snate@binkert.org
956145Snate@binkert.org      public:
967054Snate@binkert.org        TxEvent(EtherTapStub *_tap) : tap(_tap) {}
977054Snate@binkert.org        void process() { tap->retransmit(); }
987054Snate@binkert.org        virtual const char *description() const
997054Snate@binkert.org            { return "EtherTapStub retransmit"; }
1006145Snate@binkert.org    };
1017054Snate@binkert.org
1027054Snate@binkert.org    friend class TxEvent;
1036145Snate@binkert.org    TxEvent txEvent;
1047054Snate@binkert.org
1056145Snate@binkert.org  public:
1067054Snate@binkert.org    typedef EtherTapStubParams Params;
1077054Snate@binkert.org    EtherTapStub(const Params *p);
1087054Snate@binkert.org    virtual ~EtherTapStub();
1097054Snate@binkert.org
1107054Snate@binkert.org    const Params *
1117054Snate@binkert.org    params() const
1127054Snate@binkert.org    {
1137054Snate@binkert.org        return dynamic_cast<const Params *>(_params);
1147054Snate@binkert.org    }
1157054Snate@binkert.org
1166145Snate@binkert.org    EtherInt *getEthPort(const std::string &if_name, int idx) override;
1177055Snate@binkert.org
1186145Snate@binkert.org    virtual bool recvPacket(EthPacketPtr packet);
1197054Snate@binkert.org    virtual void sendDone();
1207054Snate@binkert.org
1217054Snate@binkert.org    void serialize(CheckpointOut &cp) const override;
1227054Snate@binkert.org    void unserialize(CheckpointIn &cp) override;
1237054Snate@binkert.org};
1247054Snate@binkert.org
1257054Snate@binkert.orgclass EtherTapInt : public EtherInt
1267054Snate@binkert.org{
1276145Snate@binkert.org  private:
1287054Snate@binkert.org    EtherTapStub *tap;
1297054Snate@binkert.org  public:
1307054Snate@binkert.org    EtherTapInt(const std::string &name, EtherTapStub *t)
1316145Snate@binkert.org            : EtherInt(name), tap(t)
1327054Snate@binkert.org    { }
1337054Snate@binkert.org
1347054Snate@binkert.org    virtual bool recvPacket(EthPacketPtr pkt) { return tap->recvPacket(pkt); }
1356145Snate@binkert.org    virtual void sendDone() { tap->sendDone(); }
1367054Snate@binkert.org};
1377054Snate@binkert.org
1387054Snate@binkert.org
1397054Snate@binkert.org#endif // __DEV_NET_ETHERTAP_HH__
1407054Snate@binkert.org