etherlink.cc revision 558
1/* 2 * Copyright (c) 2003 The Regents of The University of Michigan 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; 9 * redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution; 12 * neither the name of the copyright holders nor the names of its 13 * contributors may be used to endorse or promote products derived from 14 * this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29/* @file 30 * Device module for modelling a fixed bandwidth full duplex ethernet link 31 */ 32 33#include <cmath> 34#include <deque> 35#include <string> 36#include <vector> 37 38#include "base/trace.hh" 39#include "dev/etherdump.hh" 40#include "dev/etherint.hh" 41#include "dev/etherlink.hh" 42#include "dev/etherpkt.hh" 43#include "sim/builder.hh" 44#include "sim/universe.hh" 45#include "sim/system.hh" 46 47using namespace std; 48 49EtherLink::EtherLink(const std::string &name, EtherInt *i1, EtherInt *i2, 50 Tick speed, EtherDump *dump) 51 : SimObject(name) 52{ 53 double rate = ((double)ticksPerSecond * 8.0) / (double)speed; 54 55 link1 = new Link(name + ".link1", rate, dump); 56 link2 = new Link(name + ".link2", rate, dump); 57 58 int1 = new Interface(name + ".int1", link1, link2); 59 int2 = new Interface(name + ".int2", link2, link1); 60 61 int1->setPeer(i1); 62 i1->setPeer(int1); 63 int2->setPeer(i2); 64 i2->setPeer(int2); 65} 66 67EtherLink::~EtherLink() 68{ 69 delete link1; 70 delete link2; 71 72 delete int1; 73 delete int2; 74} 75 76EtherLink::Interface::Interface(const std::string &name, Link *tx, Link *rx) 77 : EtherInt(name), txlink(tx) 78{ 79 tx->setTxInt(this); 80 rx->setRxInt(this); 81} 82 83EtherLink::Link::Link(const std::string &name, double rate, EtherDump *d) 84 : objName(name), txint(NULL), rxint(NULL), ticks_per_byte(rate), 85 dump(d), event(&mainEventQueue, this) 86{} 87 88void 89EtherLink::serialize(ostream &os) 90{ 91 link1->serialize(os); 92 link2->serialize(os); 93} 94 95void 96EtherLink::unserialize(Checkpoint *cp, const string §ion) 97{ 98 link1->unserialize(cp, section); 99 link2->unserialize(cp, section); 100} 101 102void 103EtherLink::Link::txDone() 104{ 105 if (dump) 106 dump->dump(packet); 107 108 DPRINTF(Ethernet, "EtherLink packet received: len=%d\n", packet->length); 109 DDUMP(EthernetData, packet->data, packet->length); 110 111 rxint->sendPacket(packet); 112 113 packet = 0; 114 assert(!busy()); 115 116 txint->sendDone(); 117} 118 119bool 120EtherLink::Link::transmit(PacketPtr &pkt) 121{ 122 if (busy()) { 123 DPRINTF(Ethernet, "EtherLink packet not sent, link busy\n"); 124 return false; 125 } 126 127 DPRINTF(Ethernet, "EtherLink packet sent: len=%d\n", pkt->length); 128 DDUMP(EthernetData, pkt->data, pkt->length); 129 130 packet = pkt; 131 int delay = (int)ceil(((double)pkt->length * ticks_per_byte) + 1.0); 132 DPRINTF(Ethernet, "EtherLink scheduling packet: delay=%d, (rate=%f)\n", 133 delay, ticks_per_byte); 134 event.schedule(curTick + delay); 135 136 return true; 137} 138 139void 140EtherLink::Link::serialize(ostream &os) 141{ 142 bool packetExists = false; 143 if (packet) packetExists = true; 144 SERIALIZE_SCALAR(packetExists); 145 if (packetExists) { 146 nameOut(os, csprintf("%s.linkPacket", name())); 147 packet->serialize(os); 148 } 149 150 bool event_scheduled = event.scheduled(); 151 SERIALIZE_SCALAR(event_scheduled); 152 if (event_scheduled) { 153 SERIALIZE_SCALAR(event.when()); 154 } 155} 156 157void 158EtherLink::Link::unserialize(Checkpoint *cp, const string §ion) 159{ 160 bool event_scheduled, packetExists; 161 Tick eventTime; 162 UNSERIALIZE_SCALAR(packetExists); 163 if (packetExists) { 164 packet = new EtherPacket; 165 packet->unserialize(cp, csprintf("%s.linkPacket", section)); 166 } 167 168 UNSERIALIZE_SCALAR(event_scheduled); 169 if (event_scheduled) { 170 UNSERIALIZE_SCALAR(eventTime); 171 event.schedule(eventTime); 172 } 173} 174 175BEGIN_DECLARE_SIM_OBJECT_PARAMS(EtherLink) 176 177 SimObjectParam<EtherInt *> interface1; 178 SimObjectParam<EtherInt *> interface2; 179 Param<Tick> link_speed; 180 SimObjectParam<EtherDump *> packet_dump; 181 182END_DECLARE_SIM_OBJECT_PARAMS(EtherLink) 183 184BEGIN_INIT_SIM_OBJECT_PARAMS(EtherLink) 185 186 INIT_PARAM(interface1, "interface 1"), 187 INIT_PARAM(interface2, "interface 2"), 188 INIT_PARAM_DFLT(link_speed, "link speed in bits per second", 100000000), 189 INIT_PARAM_DFLT(packet_dump, "object to dump network packets to", NULL) 190 191END_INIT_SIM_OBJECT_PARAMS(EtherLink) 192 193CREATE_SIM_OBJECT(EtherLink) 194{ 195 return new EtherLink(getInstanceName(), interface1, interface2, link_speed, 196 packet_dump); 197} 198 199REGISTER_SIM_OBJECT("EtherLink", EtherLink) 200