etherlink.cc revision 146
14202Sbinkertn@umich.edu/* 24202Sbinkertn@umich.edu * Copyright (c) 2003 The Regents of The University of Michigan 34202Sbinkertn@umich.edu * All rights reserved. 44202Sbinkertn@umich.edu * 54202Sbinkertn@umich.edu * Redistribution and use in source and binary forms, with or without 64202Sbinkertn@umich.edu * modification, are permitted provided that the following conditions are 74202Sbinkertn@umich.edu * met: redistributions of source code must retain the above copyright 84202Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer; 94202Sbinkertn@umich.edu * redistributions in binary form must reproduce the above copyright 104202Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer in the 114202Sbinkertn@umich.edu * documentation and/or other materials provided with the distribution; 124202Sbinkertn@umich.edu * neither the name of the copyright holders nor the names of its 134202Sbinkertn@umich.edu * contributors may be used to endorse or promote products derived from 144202Sbinkertn@umich.edu * this software without specific prior written permission. 154202Sbinkertn@umich.edu * 164202Sbinkertn@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 174202Sbinkertn@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 184202Sbinkertn@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 194202Sbinkertn@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 204202Sbinkertn@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 214202Sbinkertn@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 224202Sbinkertn@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 234202Sbinkertn@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 244202Sbinkertn@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 254202Sbinkertn@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 264202Sbinkertn@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 274202Sbinkertn@umich.edu */ 284202Sbinkertn@umich.edu 294202Sbinkertn@umich.edu/* @file 304202Sbinkertn@umich.edu * Device module for modelling a fixed bandwidth full duplex ethernet link 314202Sbinkertn@umich.edu */ 324202Sbinkertn@umich.edu 334486Sbinkertn@umich.edu#include <cmath> 344486Sbinkertn@umich.edu#include <deque> 354776Sgblack@eecs.umich.edu#include <string> 364486Sbinkertn@umich.edu#include <vector> 374202Sbinkertn@umich.edu 384202Sbinkertn@umich.edu#include "base/trace.hh" 394202Sbinkertn@umich.edu#include "dev/etherdump.hh" 404202Sbinkertn@umich.edu#include "dev/etherint.hh" 414202Sbinkertn@umich.edu#include "dev/etherlink.hh" 424202Sbinkertn@umich.edu#include "dev/etherpkt.hh" 434202Sbinkertn@umich.edu#include "sim/builder.hh" 444202Sbinkertn@umich.edu#include "sim/universe.hh" 454202Sbinkertn@umich.edu 464202Sbinkertn@umich.eduusing namespace std; 474202Sbinkertn@umich.edu 484202Sbinkertn@umich.eduEtherLink::EtherLink(const std::string &name, EtherInt *i1, EtherInt *i2, 494202Sbinkertn@umich.edu Tick speed, EtherDump *dump) 504202Sbinkertn@umich.edu : SimObject(name) 514202Sbinkertn@umich.edu{ 524202Sbinkertn@umich.edu double rate = ((double)ticksPerSecond * 8.0) / (double)speed; 534826Ssaidi@eecs.umich.edu 544202Sbinkertn@umich.edu link1 = new Link(name + ".link1", rate, dump); 554202Sbinkertn@umich.edu link2 = new Link(name + ".link2", rate, dump); 565016Sgblack@eecs.umich.edu 574486Sbinkertn@umich.edu int1 = new Interface(name + ".int1", link1, link2); 584486Sbinkertn@umich.edu int2 = new Interface(name + ".int2", link2, link1); 594202Sbinkertn@umich.edu 604202Sbinkertn@umich.edu int1->setPeer(i1); 61 i1->setPeer(int1); 62 int2->setPeer(i2); 63 i2->setPeer(int2); 64} 65 66EtherLink::~EtherLink() 67{ 68 delete link1; 69 delete link2; 70 71 delete int1; 72 delete int2; 73} 74 75EtherLink::Interface::Interface(const std::string &name, Link *tx, Link *rx) 76 : EtherInt(name), txlink(tx) 77{ 78 tx->setTxInt(this); 79 rx->setRxInt(this); 80} 81 82EtherLink::Link::Link(const std::string &name, double rate, EtherDump *d) 83 : Serializeable(name), txint(NULL), rxint(NULL), ticks_per_byte(rate), 84 dump(d), event(&mainEventQueue, this) 85{} 86 87void 88EtherLink::Link::txDone() 89{ 90 rxint->sendPacket(packet); 91 92 if (dump) 93 dump->dump(packet); 94 95 DPRINTF(Ethernet, "EtherLink packet received: len=%d\n", packet->length); 96 DDUMP(EthernetData, packet->data, packet->length); 97 98 packet = 0; 99 assert(!busy()); 100 101 txint->sendDone(); 102} 103 104bool 105EtherLink::Link::transmit(PacketPtr pkt) 106{ 107 if (busy()) { 108 DPRINTF(Ethernet, "EtherLink packet not sent, link busy\n"); 109 return false; 110 } 111 112 DPRINTF(Ethernet, "EtherLink packet sent: len=%d\n", pkt->length); 113 DDUMP(EthernetData, pkt->data, pkt->length); 114 115 packet = pkt; 116 int delay = (int)ceil(((double)pkt->length * ticks_per_byte) + 1.0); 117 DPRINTF(Ethernet, "EtherLink scheduling packet: delay=%d, (rate=%f)\n", 118 delay, ticks_per_byte); 119 event.schedule(curTick + delay); 120 121 return true; 122} 123 124BEGIN_DECLARE_SIM_OBJECT_PARAMS(EtherLink) 125 126 SimObjectParam<EtherInt *> interface1; 127 SimObjectParam<EtherInt *> interface2; 128 Param<int> link_speed; 129 SimObjectParam<EtherDump *> packet_dump; 130 131END_DECLARE_SIM_OBJECT_PARAMS(EtherLink) 132 133BEGIN_INIT_SIM_OBJECT_PARAMS(EtherLink) 134 135 INIT_PARAM(interface1, "interface 1"), 136 INIT_PARAM(interface2, "interface 2"), 137 INIT_PARAM_DFLT(link_speed, "link speed in bits per second", 100000000), 138 INIT_PARAM_DFLT(packet_dump, "object to dump network packets to", NULL) 139 140END_INIT_SIM_OBJECT_PARAMS(EtherLink) 141 142CREATE_SIM_OBJECT(EtherLink) 143{ 144 return new EtherLink(getInstanceName(), interface1, interface2, link_speed, 145 packet_dump); 146} 147 148REGISTER_SIM_OBJECT("EtherLink", EtherLink) 149