etherlink.cc revision 195
111986Sandreas.sandberg@arm.com/* 211986Sandreas.sandberg@arm.com * Copyright (c) 2003 The Regents of The University of Michigan 311986Sandreas.sandberg@arm.com * All rights reserved. 411986Sandreas.sandberg@arm.com * 511986Sandreas.sandberg@arm.com * Redistribution and use in source and binary forms, with or without 611986Sandreas.sandberg@arm.com * modification, are permitted provided that the following conditions are 711986Sandreas.sandberg@arm.com * met: redistributions of source code must retain the above copyright 811986Sandreas.sandberg@arm.com * notice, this list of conditions and the following disclaimer; 911986Sandreas.sandberg@arm.com * redistributions in binary form must reproduce the above copyright 1011986Sandreas.sandberg@arm.com * notice, this list of conditions and the following disclaimer in the 1114299Sbbruce@ucdavis.edu * documentation and/or other materials provided with the distribution; 1211986Sandreas.sandberg@arm.com * neither the name of the copyright holders nor the names of its 1311986Sandreas.sandberg@arm.com * contributors may be used to endorse or promote products derived from 1412391Sjason@lowepower.com * this software without specific prior written permission. 1512391Sjason@lowepower.com * 1611986Sandreas.sandberg@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1712391Sjason@lowepower.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1812391Sjason@lowepower.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1912391Sjason@lowepower.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2012391Sjason@lowepower.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2111986Sandreas.sandberg@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2211986Sandreas.sandberg@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2311986Sandreas.sandberg@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2412391Sjason@lowepower.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2512391Sjason@lowepower.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2612391Sjason@lowepower.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2712391Sjason@lowepower.com */ 2812391Sjason@lowepower.com 2912391Sjason@lowepower.com/* @file 3012391Sjason@lowepower.com * Device module for modelling a fixed bandwidth full duplex ethernet link 3112391Sjason@lowepower.com */ 3211986Sandreas.sandberg@arm.com 3312391Sjason@lowepower.com#include <cmath> 3412391Sjason@lowepower.com#include <deque> 3511986Sandreas.sandberg@arm.com#include <string> 3612391Sjason@lowepower.com#include <vector> 3714299Sbbruce@ucdavis.edu 3814299Sbbruce@ucdavis.edu#include "base/trace.hh" 3914299Sbbruce@ucdavis.edu#include "dev/etherdump.hh" 4012391Sjason@lowepower.com#include "dev/etherint.hh" 4112391Sjason@lowepower.com#include "dev/etherlink.hh" 4212391Sjason@lowepower.com#include "dev/etherpkt.hh" 4311986Sandreas.sandberg@arm.com#include "sim/builder.hh" 4412391Sjason@lowepower.com#include "sim/universe.hh" 4512391Sjason@lowepower.com 4612391Sjason@lowepower.comusing namespace std; 4712391Sjason@lowepower.com 4812391Sjason@lowepower.comEtherLink::EtherLink(const std::string &name, EtherInt *i1, EtherInt *i2, 4912391Sjason@lowepower.com Tick speed, EtherDump *dump) 5012391Sjason@lowepower.com : SimObject(name) 5112391Sjason@lowepower.com{ 5212391Sjason@lowepower.com double rate = ((double)ticksPerSecond * 8.0) / (double)speed; 5312391Sjason@lowepower.com 5412391Sjason@lowepower.com link1 = new Link(name + ".link1", rate, dump); 5512391Sjason@lowepower.com link2 = new Link(name + ".link2", rate, dump); 5612391Sjason@lowepower.com 5712391Sjason@lowepower.com int1 = new Interface(name + ".int1", link1, link2); 5812391Sjason@lowepower.com int2 = new Interface(name + ".int2", link2, link1); 5914299Sbbruce@ucdavis.edu 6014299Sbbruce@ucdavis.edu int1->setPeer(i1); 6114299Sbbruce@ucdavis.edu i1->setPeer(int1); 6214299Sbbruce@ucdavis.edu int2->setPeer(i2); 6314299Sbbruce@ucdavis.edu i2->setPeer(int2); 6414299Sbbruce@ucdavis.edu} 6514299Sbbruce@ucdavis.edu 6614299Sbbruce@ucdavis.eduEtherLink::~EtherLink() 6714299Sbbruce@ucdavis.edu{ 6814299Sbbruce@ucdavis.edu delete link1; 6914299Sbbruce@ucdavis.edu delete link2; 7014299Sbbruce@ucdavis.edu 7114299Sbbruce@ucdavis.edu delete int1; 7214299Sbbruce@ucdavis.edu delete int2; 7314299Sbbruce@ucdavis.edu} 7414299Sbbruce@ucdavis.edu 7514299Sbbruce@ucdavis.eduEtherLink::Interface::Interface(const std::string &name, Link *tx, Link *rx) 7614299Sbbruce@ucdavis.edu : EtherInt(name), txlink(tx) 7714299Sbbruce@ucdavis.edu{ 7814299Sbbruce@ucdavis.edu tx->setTxInt(this); 7914299Sbbruce@ucdavis.edu rx->setRxInt(this); 8014299Sbbruce@ucdavis.edu} 8114299Sbbruce@ucdavis.edu 8214299Sbbruce@ucdavis.eduEtherLink::Link::Link(const std::string &name, double rate, EtherDump *d) 8314299Sbbruce@ucdavis.edu : Serializeable(name), txint(NULL), rxint(NULL), ticks_per_byte(rate), 8414299Sbbruce@ucdavis.edu dump(d), event(&mainEventQueue, this) 8514299Sbbruce@ucdavis.edu{} 8614299Sbbruce@ucdavis.edu 8712391Sjason@lowepower.comvoid 8812391Sjason@lowepower.comEtherLink::Link::txDone() 8912391Sjason@lowepower.com{ 9012391Sjason@lowepower.com if (dump) 9112391Sjason@lowepower.com dump->dump(packet); 9212391Sjason@lowepower.com 9312391Sjason@lowepower.com DPRINTF(Ethernet, "EtherLink packet received: len=%d\n", packet->length); 9412391Sjason@lowepower.com DDUMP(EthernetData, packet->data, packet->length); 9512391Sjason@lowepower.com 9612391Sjason@lowepower.com rxint->sendPacket(packet); 9712391Sjason@lowepower.com 9812391Sjason@lowepower.com packet = 0; 9911986Sandreas.sandberg@arm.com assert(!busy()); 10011986Sandreas.sandberg@arm.com 10111986Sandreas.sandberg@arm.com txint->sendDone(); 10212391Sjason@lowepower.com} 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