ethertap.hh revision 1872
111507SCurtis.Dunham@arm.com/*
211507SCurtis.Dunham@arm.com * Copyright (c) 2003-2005 The Regents of The University of Michigan
311680SCurtis.Dunham@arm.com * All rights reserved.
411680SCurtis.Dunham@arm.com *
511680SCurtis.Dunham@arm.com * Redistribution and use in source and binary forms, with or without
611507SCurtis.Dunham@arm.com * modification, are permitted provided that the following conditions are
711687Sandreas.hansson@arm.com * met: redistributions of source code must retain the above copyright
811687Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer;
911687Sandreas.hansson@arm.com * redistributions in binary form must reproduce the above copyright
1011687Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer in the
1111687Sandreas.hansson@arm.com * documentation and/or other materials provided with the distribution;
1211507SCurtis.Dunham@arm.com * neither the name of the copyright holders nor the names of its
1311507SCurtis.Dunham@arm.com * contributors may be used to endorse or promote products derived from
1411507SCurtis.Dunham@arm.com * this software without specific prior written permission.
1511507SCurtis.Dunham@arm.com *
1611680SCurtis.Dunham@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1711507SCurtis.Dunham@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1811570SCurtis.Dunham@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1911570SCurtis.Dunham@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2011507SCurtis.Dunham@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2111507SCurtis.Dunham@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2211507SCurtis.Dunham@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2311570SCurtis.Dunham@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2411570SCurtis.Dunham@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2511680SCurtis.Dunham@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2611680SCurtis.Dunham@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2711680SCurtis.Dunham@arm.com */
2811680SCurtis.Dunham@arm.com
2911680SCurtis.Dunham@arm.com/* @file
3011680SCurtis.Dunham@arm.com * Interface to connect a simulated ethernet device to the real world
3111680SCurtis.Dunham@arm.com */
3211680SCurtis.Dunham@arm.com
3311570SCurtis.Dunham@arm.com#ifndef __ETHERTAP_HH__
3411507SCurtis.Dunham@arm.com#define __ETHERTAP_HH__
3511570SCurtis.Dunham@arm.com
3611507SCurtis.Dunham@arm.com#include <queue>
3711570SCurtis.Dunham@arm.com#include <string>
3811507SCurtis.Dunham@arm.com
3911507SCurtis.Dunham@arm.com#include "dev/etherint.hh"
4011570SCurtis.Dunham@arm.com#include "dev/etherpkt.hh"
4111507SCurtis.Dunham@arm.com#include "sim/eventq.hh"
4211507SCurtis.Dunham@arm.com#include "base/pollevent.hh"
4311507SCurtis.Dunham@arm.com#include "sim/sim_object.hh"
4411507SCurtis.Dunham@arm.com
4511507SCurtis.Dunham@arm.comclass TapEvent;
4611570SCurtis.Dunham@arm.comclass TapListener;
4711507SCurtis.Dunham@arm.com
4811507SCurtis.Dunham@arm.com/*
4911507SCurtis.Dunham@arm.com * Interface to connect a simulated ethernet device to the real world
5011507SCurtis.Dunham@arm.com */
5111507SCurtis.Dunham@arm.comclass EtherTap : public EtherInt
5211507SCurtis.Dunham@arm.com{
5311507SCurtis.Dunham@arm.com  protected:
5411507SCurtis.Dunham@arm.com    friend class TapEvent;
5511507SCurtis.Dunham@arm.com    TapEvent *event;
5611507SCurtis.Dunham@arm.com
5711507SCurtis.Dunham@arm.com  protected:
5811507SCurtis.Dunham@arm.com    friend class TapListener;
5911507SCurtis.Dunham@arm.com    TapListener *listener;
6011507SCurtis.Dunham@arm.com    int socket;
6111507SCurtis.Dunham@arm.com    char *buffer;
6211507SCurtis.Dunham@arm.com    int buflen;
6311507SCurtis.Dunham@arm.com    int32_t buffer_offset;
6411507SCurtis.Dunham@arm.com    int32_t data_len;
6511507SCurtis.Dunham@arm.com
6611507SCurtis.Dunham@arm.com    EtherDump *dump;
6711507SCurtis.Dunham@arm.com
6811507SCurtis.Dunham@arm.com    void attach(int fd);
6911507SCurtis.Dunham@arm.com    void detach();
7011507SCurtis.Dunham@arm.com
7111507SCurtis.Dunham@arm.com  protected:
7211507SCurtis.Dunham@arm.com    std::string device;
7311507SCurtis.Dunham@arm.com    std::queue<PacketPtr> packetBuffer;
7411507SCurtis.Dunham@arm.com
7511507SCurtis.Dunham@arm.com    void process(int revent);
7611507SCurtis.Dunham@arm.com    void enqueue(PacketData *packet);
7711507SCurtis.Dunham@arm.com    void retransmit();
7811507SCurtis.Dunham@arm.com
7911680SCurtis.Dunham@arm.com    /*
8011507SCurtis.Dunham@arm.com     */
8111507SCurtis.Dunham@arm.com    class TxEvent : public Event
8211507SCurtis.Dunham@arm.com    {
8311507SCurtis.Dunham@arm.com      protected:
8411507SCurtis.Dunham@arm.com        EtherTap *tap;
8511507SCurtis.Dunham@arm.com
8611570SCurtis.Dunham@arm.com      public:
8711507SCurtis.Dunham@arm.com        TxEvent(EtherTap *_tap)
8811507SCurtis.Dunham@arm.com            : Event(&mainEventQueue), tap(_tap) {}
8911507SCurtis.Dunham@arm.com        void process() { tap->retransmit(); }
9011507SCurtis.Dunham@arm.com        virtual const char *description() { return "retransmit event"; }
9111507SCurtis.Dunham@arm.com    };
9211507SCurtis.Dunham@arm.com
9311507SCurtis.Dunham@arm.com    friend class TxEvent;
9411680SCurtis.Dunham@arm.com    TxEvent txEvent;
9511680SCurtis.Dunham@arm.com
9611570SCurtis.Dunham@arm.com  public:
9711507SCurtis.Dunham@arm.com    EtherTap(const std::string &name, EtherDump *dump, int port, int bufsz);
9811507SCurtis.Dunham@arm.com    virtual ~EtherTap();
9911507SCurtis.Dunham@arm.com
10011507SCurtis.Dunham@arm.com    virtual bool recvPacket(PacketPtr packet);
10111507SCurtis.Dunham@arm.com    virtual void sendDone();
10211507SCurtis.Dunham@arm.com
10311507SCurtis.Dunham@arm.com    virtual void serialize(std::ostream &os);
10411507SCurtis.Dunham@arm.com    virtual void unserialize(Checkpoint *cp, const std::string &section);
10511507SCurtis.Dunham@arm.com};
10611507SCurtis.Dunham@arm.com
10711507SCurtis.Dunham@arm.com#endif // __ETHERTAP_HH__
10811507SCurtis.Dunham@arm.com