etherlink.cc revision 2
11689SN/A/* 27944SGiacomo.Gabrielli@arm.com * Copyright (c) 2003 The Regents of The University of Michigan 37944SGiacomo.Gabrielli@arm.com * All rights reserved. 47944SGiacomo.Gabrielli@arm.com * 57944SGiacomo.Gabrielli@arm.com * Redistribution and use in source and binary forms, with or without 67944SGiacomo.Gabrielli@arm.com * modification, are permitted provided that the following conditions are 77944SGiacomo.Gabrielli@arm.com * met: redistributions of source code must retain the above copyright 87944SGiacomo.Gabrielli@arm.com * notice, this list of conditions and the following disclaimer; 97944SGiacomo.Gabrielli@arm.com * redistributions in binary form must reproduce the above copyright 107944SGiacomo.Gabrielli@arm.com * notice, this list of conditions and the following disclaimer in the 117944SGiacomo.Gabrielli@arm.com * documentation and/or other materials provided with the distribution; 127944SGiacomo.Gabrielli@arm.com * neither the name of the copyright holders nor the names of its 137944SGiacomo.Gabrielli@arm.com * contributors may be used to endorse or promote products derived from 142326SN/A * this software without specific prior written permission. 151689SN/A * 161689SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 171689SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 181689SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 191689SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 201689SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 211689SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 221689SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 231689SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 241689SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 251689SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 261689SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 271689SN/A */ 281689SN/A 291689SN/A/* @file 301689SN/A * Device module for modelling a fixed bandwidth full duplex ethernet link 311689SN/A */ 321689SN/A 331689SN/A#include <deque> 341689SN/A#include <string> 351689SN/A#include <vector> 361689SN/A 371689SN/A#include <math.h> 381689SN/A 392665Ssaidi@eecs.umich.edu#include "etherlink.hh" 402665Ssaidi@eecs.umich.edu#include "etherdump.hh" 412831Sksewell@umich.edu#include "etherint.hh" 421689SN/A#include "etherpkt.hh" 431689SN/A#include "trace.hh" 442064SN/A#include "universe.hh" 451060SN/A 461060SN/Ausing namespace std; 472292SN/A 481717SN/AEtherLink::EtherLink(const std::string &name, EtherInt *i1, EtherInt *i2, 498232Snate@binkert.org Tick speed, EtherDump *dump) 504762Snate@binkert.org : SimObject(name) 516221Snate@binkert.org{ 524762Snate@binkert.org double rate = ((double)ticksPerSecond * 8.0) / (double)speed; 531060SN/A 546221Snate@binkert.org link1 = new Link(name + ".link1", rate, dump); 555529Snate@binkert.org link2 = new Link(name + ".link2", rate, dump); 561061SN/A 572292SN/A int1 = new Interface(name + ".int1", link1, link2); 585606Snate@binkert.org int2 = new Interface(name + ".int2", link2, link1); 595606Snate@binkert.org 605606Snate@binkert.org int1->setPeer(i1); 611060SN/A i1->setPeer(int1); 622292SN/A int2->setPeer(i2); 632292SN/A i2->setPeer(int2); 642292SN/A} 652292SN/A 662292SN/AEtherLink::~EtherLink() 672292SN/A{ 682292SN/A delete link1; 692326SN/A delete link2; 702292SN/A 712292SN/A delete int1; 722292SN/A delete int2; 732292SN/A} 742292SN/A 752292SN/AEtherLink::Interface::Interface(const std::string &name, Link *tx, Link *rx) 765336Shines@cs.fsu.edu : EtherInt(name), txlink(tx) 772292SN/A{ 784873Sstever@eecs.umich.edu tx->setTxInt(this); 792292SN/A rx->setRxInt(this); 802292SN/A} 812292SN/A 824329Sktlim@umich.eduEtherLink::Link::Link(const std::string &name, double rate, EtherDump *d) 835529Snate@binkert.org : Serializeable(name), txint(NULL), rxint(NULL), ticks_per_byte(rate), 844329Sktlim@umich.edu dump(d), event(&mainEventQueue, this) 854329Sktlim@umich.edu{} 864329Sktlim@umich.edu 872292SN/Avoid 882292SN/AEtherLink::Link::txDone() 892292SN/A{ 902292SN/A rxint->sendPacket(packet); 912292SN/A 922292SN/A if (dump) 932292SN/A dump->dump(packet); 942292SN/A 952307SN/A DPRINTF(Ethernet, "EtherLink packet received: len=%d\n", packet->length); 962307SN/A DDUMP(EthernetData, packet->data, packet->length); 975529Snate@binkert.org 981060SN/A packet = 0; 991060SN/A assert(!busy()); 1001060SN/A 1011060SN/A txint->sendDone(); 1021060SN/A} 1031060SN/A 1042326SN/Abool 1051060SN/AEtherLink::Link::transmit(PacketPtr pkt) 1061060SN/A{ 1071060SN/A if (busy()) { 1081060SN/A DPRINTF(Ethernet, "EtherLink packet not sent, link busy\n"); 1092292SN/A return false; 1106221Snate@binkert.org } 1116221Snate@binkert.org 1126221Snate@binkert.org DPRINTF(Ethernet, "EtherLink packet sent: len=%d\n", pkt->length); 1131060SN/A DDUMP(EthernetData, pkt->data, pkt->length); 1141060SN/A 1152307SN/A packet = pkt; 1162292SN/A int delay = (int)ceil(((double)pkt->length * ticks_per_byte) + 1.0); 1172980Sgblack@eecs.umich.edu DPRINTF(Ethernet, "EtherLink scheduling packet: delay=%d, (rate=%f)\n", 1182292SN/A delay, ticks_per_byte); 1192292SN/A event.schedule(curTick + delay); 1202292SN/A 1212292SN/A return true; 1222292SN/A} 1232292SN/A 1242292SN/ABEGIN_DECLARE_SIM_OBJECT_PARAMS(EtherLink) 1252292SN/A 1262292SN/A SimObjectParam<EtherInt *> interface1; 1272292SN/A SimObjectParam<EtherInt *> interface2; 1286221Snate@binkert.org Param<int> link_speed; 1296221Snate@binkert.org SimObjectParam<EtherDump *> packet_dump; 1302292SN/A 1312292SN/AEND_DECLARE_SIM_OBJECT_PARAMS(EtherLink) 1322292SN/A 1332292SN/ABEGIN_INIT_SIM_OBJECT_PARAMS(EtherLink) 1342292SN/A 1352292SN/A INIT_PARAM(interface1, "interface 1"), 1362292SN/A INIT_PARAM(interface2, "interface 2"), 1372292SN/A INIT_PARAM_DFLT(link_speed, "link speed in bits per second", 100000000), 1382292SN/A INIT_PARAM_DFLT(packet_dump, "object to dump network packets to", NULL) 1396221Snate@binkert.org 1406221Snate@binkert.orgEND_INIT_SIM_OBJECT_PARAMS(EtherLink) 1412292SN/A 1422292SN/ACREATE_SIM_OBJECT(EtherLink) 1432831Sksewell@umich.edu{ 1442292SN/A return new EtherLink(getInstanceName(), interface1, interface2, link_speed, 1452292SN/A packet_dump); 1462292SN/A} 1472292SN/A 1482292SN/AREGISTER_SIM_OBJECT("EtherLink", EtherLink) 1492292SN/A