etherlink.cc revision 195
16167SN/A/*
26167SN/A * Copyright (c) 2003 The Regents of The University of Michigan
36167SN/A * All rights reserved.
410036SAli.Saidi@ARM.com *
58835SAli.Saidi@ARM.com * Redistribution and use in source and binary forms, with or without
610036SAli.Saidi@ARM.com * modification, are permitted provided that the following conditions are
77935SN/A * met: redistributions of source code must retain the above copyright
87935SN/A * notice, this list of conditions and the following disclaimer;
97935SN/A * redistributions in binary form must reproduce the above copyright
106167SN/A * notice, this list of conditions and the following disclaimer in the
116167SN/A * documentation and/or other materials provided with the distribution;
126167SN/A * neither the name of the copyright holders nor the names of its
1310315Snilay@cs.wisc.edu * contributors may be used to endorse or promote products derived from
148835SAli.Saidi@ARM.com * this software without specific prior written permission.
159864Snilay@cs.wisc.edu *
169864Snilay@cs.wisc.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1710036SAli.Saidi@ARM.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
188835SAli.Saidi@ARM.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
198835SAli.Saidi@ARM.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2010315Snilay@cs.wisc.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
218835SAli.Saidi@ARM.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2210063Snilay@cs.wisc.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
237935SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
249864Snilay@cs.wisc.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
258721SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
268721SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
278835SAli.Saidi@ARM.com */
288835SAli.Saidi@ARM.com
297935SN/A/* @file
307935SN/A * Device module for modelling a fixed bandwidth full duplex ethernet link
317935SN/A */
327935SN/A
337935SN/A#include <cmath>
347935SN/A#include <deque>
357935SN/A#include <string>
368983Snate@binkert.org#include <vector>
376167SN/A
389864Snilay@cs.wisc.edu#include "base/trace.hh"
399864Snilay@cs.wisc.edu#include "dev/etherdump.hh"
409864Snilay@cs.wisc.edu#include "dev/etherint.hh"
4110315Snilay@cs.wisc.edu#include "dev/etherlink.hh"
4210036SAli.Saidi@ARM.com#include "dev/etherpkt.hh"
4310315Snilay@cs.wisc.edu#include "sim/builder.hh"
449864Snilay@cs.wisc.edu#include "sim/universe.hh"
459864Snilay@cs.wisc.edu
466167SN/Ausing namespace std;
476167SN/A
489864Snilay@cs.wisc.eduEtherLink::EtherLink(const std::string &name, EtherInt *i1, EtherInt *i2,
4910063Snilay@cs.wisc.edu                     Tick speed, EtherDump *dump)
506167SN/A    : SimObject(name)
519864Snilay@cs.wisc.edu{
526167SN/A    double rate = ((double)ticksPerSecond * 8.0) / (double)speed;
536167SN/A
548835SAli.Saidi@ARM.com    link1 = new Link(name + ".link1", rate, dump);
556167SN/A    link2 = new Link(name + ".link2", rate, dump);
566167SN/A
5710036SAli.Saidi@ARM.com    int1 = new Interface(name + ".int1", link1, link2);
586167SN/A    int2 = new Interface(name + ".int2", link2, link1);
596167SN/A
608835SAli.Saidi@ARM.com    int1->setPeer(i1);
619469Snilay@cs.wisc.edu    i1->setPeer(int1);
626167SN/A    int2->setPeer(i2);
636167SN/A    i2->setPeer(int2);
646167SN/A}
656167SN/A
666167SN/AEtherLink::~EtherLink()
676167SN/A{
688835SAli.Saidi@ARM.com    delete link1;
696167SN/A    delete link2;
709864Snilay@cs.wisc.edu
7110229Snilay@cs.wisc.edu    delete int1;
729469Snilay@cs.wisc.edu    delete int2;
736167SN/A}
746167SN/A
756167SN/AEtherLink::Interface::Interface(const std::string &name, Link *tx, Link *rx)
769469Snilay@cs.wisc.edu    : EtherInt(name), txlink(tx)
779469Snilay@cs.wisc.edu{
786167SN/A    tx->setTxInt(this);
799864Snilay@cs.wisc.edu    rx->setRxInt(this);
809864Snilay@cs.wisc.edu}
819864Snilay@cs.wisc.edu
8210315Snilay@cs.wisc.eduEtherLink::Link::Link(const std::string &name, double rate, EtherDump *d)
8310036SAli.Saidi@ARM.com    : Serializeable(name), txint(NULL), rxint(NULL), ticks_per_byte(rate),
8410315Snilay@cs.wisc.edu      dump(d), event(&mainEventQueue, this)
859864Snilay@cs.wisc.edu{}
869864Snilay@cs.wisc.edu
876167SN/Avoid
886167SN/AEtherLink::Link::txDone()
8910036SAli.Saidi@ARM.com{
906167SN/A    if (dump)
916167SN/A        dump->dump(packet);
928835SAli.Saidi@ARM.com
938835SAli.Saidi@ARM.com    DPRINTF(Ethernet, "EtherLink packet received: len=%d\n", packet->length);
9410036SAli.Saidi@ARM.com    DDUMP(EthernetData, packet->data, packet->length);
958835SAli.Saidi@ARM.com
969469Snilay@cs.wisc.edu    rxint->sendPacket(packet);
979469Snilay@cs.wisc.edu
9810036SAli.Saidi@ARM.com    packet = 0;
999469Snilay@cs.wisc.edu    assert(!busy());
1006167SN/A
1016167SN/A    txint->sendDone();
10210036SAli.Saidi@ARM.com}
1036167SN/A
1046167SN/Abool
1056167SN/AEtherLink::Link::transmit(PacketPtr pkt)
1066167SN/A{
10710036SAli.Saidi@ARM.com    if (busy()) {
1086167SN/A        DPRINTF(Ethernet, "EtherLink packet not sent, link busy\n");
1096167SN/A        return false;
1106167SN/A    }
1116167SN/A
1126167SN/A    DPRINTF(Ethernet, "EtherLink packet sent: len=%d\n", pkt->length);
1136167SN/A    DDUMP(EthernetData, pkt->data, pkt->length);
1146167SN/A
1156167SN/A    packet = pkt;
1166167SN/A    int delay = (int)ceil(((double)pkt->length * ticks_per_byte) + 1.0);
11710036SAli.Saidi@ARM.com    DPRINTF(Ethernet, "EtherLink scheduling packet: delay=%d, (rate=%f)\n",
11810229Snilay@cs.wisc.edu            delay, ticks_per_byte);
1196167SN/A    event.schedule(curTick + delay);
1206167SN/A
1216167SN/A    return true;
1226167SN/A}
1236167SN/A
1246167SN/ABEGIN_DECLARE_SIM_OBJECT_PARAMS(EtherLink)
1256167SN/A
1266167SN/A    SimObjectParam<EtherInt *> interface1;
1276167SN/A    SimObjectParam<EtherInt *> interface2;
12810451Snilay@cs.wisc.edu    Param<int> link_speed;
1296167SN/A    SimObjectParam<EtherDump *> packet_dump;
13010315Snilay@cs.wisc.edu
13110315Snilay@cs.wisc.eduEND_DECLARE_SIM_OBJECT_PARAMS(EtherLink)
13210315Snilay@cs.wisc.edu
13310315Snilay@cs.wisc.eduBEGIN_INIT_SIM_OBJECT_PARAMS(EtherLink)
13410315Snilay@cs.wisc.edu
13510315Snilay@cs.wisc.edu    INIT_PARAM(interface1, "interface 1"),
13610315Snilay@cs.wisc.edu    INIT_PARAM(interface2, "interface 2"),
13710315Snilay@cs.wisc.edu    INIT_PARAM_DFLT(link_speed, "link speed in bits per second", 100000000),
1389469Snilay@cs.wisc.edu    INIT_PARAM_DFLT(packet_dump, "object to dump network packets to", NULL)
1399469Snilay@cs.wisc.edu
1409469Snilay@cs.wisc.eduEND_INIT_SIM_OBJECT_PARAMS(EtherLink)
1419864Snilay@cs.wisc.edu
1429864Snilay@cs.wisc.eduCREATE_SIM_OBJECT(EtherLink)
14310036SAli.Saidi@ARM.com{
1449469Snilay@cs.wisc.edu    return new EtherLink(getInstanceName(), interface1, interface2, link_speed,
1459469Snilay@cs.wisc.edu                         packet_dump);
1469469Snilay@cs.wisc.edu}
1479578Snilay@cs.wisc.edu
1489469Snilay@cs.wisc.eduREGISTER_SIM_OBJECT("EtherLink", EtherLink)
1499469Snilay@cs.wisc.edu