etherlink.cc revision 195
16157Snate@binkert.org/*
26157Snate@binkert.org * Copyright (c) 2003 The Regents of The University of Michigan
36157Snate@binkert.org * All rights reserved.
46157Snate@binkert.org *
56157Snate@binkert.org * Redistribution and use in source and binary forms, with or without
66157Snate@binkert.org * modification, are permitted provided that the following conditions are
76157Snate@binkert.org * met: redistributions of source code must retain the above copyright
86157Snate@binkert.org * notice, this list of conditions and the following disclaimer;
96157Snate@binkert.org * redistributions in binary form must reproduce the above copyright
106157Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
116157Snate@binkert.org * documentation and/or other materials provided with the distribution;
126157Snate@binkert.org * neither the name of the copyright holders nor the names of its
136157Snate@binkert.org * contributors may be used to endorse or promote products derived from
146157Snate@binkert.org * this software without specific prior written permission.
156157Snate@binkert.org *
166157Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
176157Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
186157Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
196157Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
206157Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
216157Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
226157Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236157Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246157Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256157Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
266157Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276157Snate@binkert.org */
286157Snate@binkert.org
296157Snate@binkert.org/* @file
306157Snate@binkert.org * Device module for modelling a fixed bandwidth full duplex ethernet link
3112563Sgabeblack@google.com */
3212563Sgabeblack@google.com
336157Snate@binkert.org#include <cmath>
346157Snate@binkert.org#include <deque>
356157Snate@binkert.org#include <string>
366157Snate@binkert.org#include <vector>
376157Snate@binkert.org
386157Snate@binkert.org#include "base/trace.hh"
396157Snate@binkert.org#include "dev/etherdump.hh"
4012246Sgabeblack@google.com#include "dev/etherint.hh"
4112246Sgabeblack@google.com#include "dev/etherlink.hh"
426157Snate@binkert.org#include "dev/etherpkt.hh"
436157Snate@binkert.org#include "sim/builder.hh"
4410133Sandreas.hansson@arm.com#include "sim/universe.hh"
4510133Sandreas.hansson@arm.com
4610133Sandreas.hansson@arm.comusing namespace std;
4710133Sandreas.hansson@arm.com
4810133Sandreas.hansson@arm.comEtherLink::EtherLink(const std::string &name, EtherInt *i1, EtherInt *i2,
4910133Sandreas.hansson@arm.com                     Tick speed, EtherDump *dump)
5010133Sandreas.hansson@arm.com    : SimObject(name)
5110133Sandreas.hansson@arm.com{
5210133Sandreas.hansson@arm.com    double rate = ((double)ticksPerSecond * 8.0) / (double)speed;
5310133Sandreas.hansson@arm.com
5410133Sandreas.hansson@arm.com    link1 = new Link(name + ".link1", rate, dump);
5510133Sandreas.hansson@arm.com    link2 = new Link(name + ".link2", rate, dump);
5610133Sandreas.hansson@arm.com
5710133Sandreas.hansson@arm.com    int1 = new Interface(name + ".int1", link1, link2);
5810133Sandreas.hansson@arm.com    int2 = new Interface(name + ".int2", link2, link1);
5910133Sandreas.hansson@arm.com
6010133Sandreas.hansson@arm.com    int1->setPeer(i1);
6110133Sandreas.hansson@arm.com    i1->setPeer(int1);
6211755Sandreas.hansson@arm.com    int2->setPeer(i2);
6310133Sandreas.hansson@arm.com    i2->setPeer(int2);
6410133Sandreas.hansson@arm.com}
658492Snilay@cs.wisc.edu
666168Snate@binkert.orgEtherLink::~EtherLink()
676168Snate@binkert.org{
686157Snate@binkert.org    delete link1;
696157Snate@binkert.org    delete link2;
706157Snate@binkert.org
716157Snate@binkert.org    delete int1;
726157Snate@binkert.org    delete int2;
736157Snate@binkert.org}
746157Snate@binkert.org
756157Snate@binkert.orgEtherLink::Interface::Interface(const std::string &name, Link *tx, Link *rx)
766157Snate@binkert.org    : EtherInt(name), txlink(tx)
776157Snate@binkert.org{
786157Snate@binkert.org    tx->setTxInt(this);
796157Snate@binkert.org    rx->setRxInt(this);
806157Snate@binkert.org}
816157Snate@binkert.org
826157Snate@binkert.orgEtherLink::Link::Link(const std::string &name, double rate, EtherDump *d)
836157Snate@binkert.org    : Serializeable(name), txint(NULL), rxint(NULL), ticks_per_byte(rate),
846157Snate@binkert.org      dump(d), event(&mainEventQueue, this)
856157Snate@binkert.org{}
866157Snate@binkert.org
876157Snate@binkert.orgvoid
886157Snate@binkert.orgEtherLink::Link::txDone()
896157Snate@binkert.org{
906157Snate@binkert.org    if (dump)
916157Snate@binkert.org        dump->dump(packet);
926157Snate@binkert.org
936157Snate@binkert.org    DPRINTF(Ethernet, "EtherLink packet received: len=%d\n", packet->length);
946157Snate@binkert.org    DDUMP(EthernetData, packet->data, packet->length);
956157Snate@binkert.org
966157Snate@binkert.org    rxint->sendPacket(packet);
976157Snate@binkert.org
986157Snate@binkert.org    packet = 0;
996157Snate@binkert.org    assert(!busy());
1006157Snate@binkert.org
1016157Snate@binkert.org    txint->sendDone();
1026157Snate@binkert.org}
1036157Snate@binkert.org
1046157Snate@binkert.orgbool
1056157Snate@binkert.orgEtherLink::Link::transmit(PacketPtr pkt)
10612563Sgabeblack@google.com{
1076157Snate@binkert.org    if (busy()) {
1086157Snate@binkert.org        DPRINTF(Ethernet, "EtherLink packet not sent, link busy\n");
1096157Snate@binkert.org        return false;
1106157Snate@binkert.org    }
1118483Sgblack@eecs.umich.edu
1128483Sgblack@eecs.umich.edu    DPRINTF(Ethernet, "EtherLink packet sent: len=%d\n", pkt->length);
1136157Snate@binkert.org    DDUMP(EthernetData, pkt->data, pkt->length);
1146882SBrad.Beckmann@amd.com
1156286Snate@binkert.org    packet = pkt;
1166286Snate@binkert.org    int delay = (int)ceil(((double)pkt->length * ticks_per_byte) + 1.0);
1178092Snilay@cs.wisc.edu    DPRINTF(Ethernet, "EtherLink scheduling packet: delay=%d, (rate=%f)\n",
1186286Snate@binkert.org            delay, ticks_per_byte);
1196286Snate@binkert.org    event.schedule(curTick + delay);
1206157Snate@binkert.org
12111208Sjoseph.gross@amd.com    return true;
1226157Snate@binkert.org}
12311210SBrad.Beckmann@amd.com
12410301Snilay@cs.wisc.eduBEGIN_DECLARE_SIM_OBJECT_PARAMS(EtherLink)
1256157Snate@binkert.org
1266157Snate@binkert.org    SimObjectParam<EtherInt *> interface1;
12711307Santhony.gutierrez@amd.com    SimObjectParam<EtherInt *> interface2;
12811122Snilay@cs.wisc.edu    Param<int> link_speed;
12910301Snilay@cs.wisc.edu    SimObjectParam<EtherDump *> packet_dump;
13010301Snilay@cs.wisc.edu
13110301Snilay@cs.wisc.eduEND_DECLARE_SIM_OBJECT_PARAMS(EtherLink)
13210301Snilay@cs.wisc.edu
13310301Snilay@cs.wisc.eduBEGIN_INIT_SIM_OBJECT_PARAMS(EtherLink)
13411308Santhony.gutierrez@amd.com
13510301Snilay@cs.wisc.edu    INIT_PARAM(interface1, "interface 1"),
13610301Snilay@cs.wisc.edu    INIT_PARAM(interface2, "interface 2"),
13711308Santhony.gutierrez@amd.com    INIT_PARAM_DFLT(link_speed, "link speed in bits per second", 100000000),
13811308Santhony.gutierrez@amd.com    INIT_PARAM_DFLT(packet_dump, "object to dump network packets to", NULL)
13911308Santhony.gutierrez@amd.com
14011308Santhony.gutierrez@amd.comEND_INIT_SIM_OBJECT_PARAMS(EtherLink)
14111308Santhony.gutierrez@amd.com
14211308Santhony.gutierrez@amd.comCREATE_SIM_OBJECT(EtherLink)
14311308Santhony.gutierrez@amd.com{
14411308Santhony.gutierrez@amd.com    return new EtherLink(getInstanceName(), interface1, interface2, link_speed,
14511308Santhony.gutierrez@amd.com                         packet_dump);
14611308Santhony.gutierrez@amd.com}
147
148REGISTER_SIM_OBJECT("EtherLink", EtherLink)
149