ethertap.hh revision 12054
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; 501872SN/Aclass TapListener; 514981SN/Aclass EtherTapInt; 521872SN/A 532SN/A/* 5412054Sgabeblack@google.com * Interface to connect a simulated ethernet device to the real world. An 5512054Sgabeblack@google.com * external helper program bridges between this object's TCP port and a 5612054Sgabeblack@google.com * source/sink for Ethernet frames. Each frame going in either direction is 5712054Sgabeblack@google.com * prepended with the frame's length in a 32 bit integer in network byte order. 582SN/A */ 5912054Sgabeblack@google.comclass EtherTapStub : public EtherObject 602SN/A{ 612SN/A protected: 622SN/A friend class TapEvent; 632SN/A TapEvent *event; 642SN/A 652SN/A protected: 662SN/A friend class TapListener; 672SN/A TapListener *listener; 682SN/A int socket; 692SN/A char *buffer; 702SN/A int buflen; 716227SN/A uint32_t buffer_offset; 726227SN/A uint32_t data_len; 732SN/A 742SN/A EtherDump *dump; 752SN/A 762SN/A void attach(int fd); 772SN/A void detach(); 782SN/A 792SN/A protected: 802SN/A std::string device; 812566SN/A std::queue<EthPacketPtr> packetBuffer; 824981SN/A EtherTapInt *interface; 832SN/A 842SN/A void process(int revent); 852566SN/A void enqueue(EthPacketData *packet); 862SN/A void retransmit(); 872SN/A 882SN/A /* 892SN/A */ 902SN/A class TxEvent : public Event 912SN/A { 922SN/A protected: 9312054Sgabeblack@google.com EtherTapStub *tap; 942SN/A 952SN/A public: 9612054Sgabeblack@google.com TxEvent(EtherTapStub *_tap) : tap(_tap) {} 972SN/A void process() { tap->retransmit(); } 985336SN/A virtual const char *description() const 9912054Sgabeblack@google.com { return "EtherTapStub retransmit"; } 1002SN/A }; 1012SN/A 1022SN/A friend class TxEvent; 1032SN/A TxEvent txEvent; 1042SN/A 1052SN/A public: 10612054Sgabeblack@google.com typedef EtherTapStubParams Params; 10712054Sgabeblack@google.com EtherTapStub(const Params *p); 10812054Sgabeblack@google.com virtual ~EtherTapStub(); 1092SN/A 1104981SN/A const Params * 1114981SN/A params() const 1124981SN/A { 1134981SN/A return dynamic_cast<const Params *>(_params); 1144981SN/A } 1154981SN/A 11611169SN/A EtherInt *getEthPort(const std::string &if_name, int idx) override; 1174981SN/A 1182566SN/A virtual bool recvPacket(EthPacketPtr packet); 1192SN/A virtual void sendDone(); 120253SN/A 12111168SN/A void serialize(CheckpointOut &cp) const override; 12211168SN/A void unserialize(CheckpointIn &cp) override; 1232SN/A}; 1242SN/A 1254981SN/Aclass EtherTapInt : public EtherInt 1264981SN/A{ 1274981SN/A private: 12812054Sgabeblack@google.com EtherTapStub *tap; 1294981SN/A public: 13012054Sgabeblack@google.com EtherTapInt(const std::string &name, EtherTapStub *t) 1314981SN/A : EtherInt(name), tap(t) 1324981SN/A { } 1334981SN/A 1344981SN/A virtual bool recvPacket(EthPacketPtr pkt) { return tap->recvPacket(pkt); } 1354981SN/A virtual void sendDone() { tap->sendDone(); } 1364981SN/A}; 1374981SN/A 1384981SN/A 13911263Sandreas.sandberg@arm.com#endif // __DEV_NET_ETHERTAP_HH__ 140