etherlink.cc revision 13766
12SN/A/* 21762SN/A * Copyright (c) 2015 ARM Limited 32SN/A * All rights reserved 42SN/A * 52SN/A * The license below extends only to copyright in the software and shall 62SN/A * not be construed as granting a license to any other intellectual 72SN/A * property including but not limited to intellectual property relating 82SN/A * to a hardware implementation of the functionality of the software 92SN/A * licensed hereunder. You may use the software subject to the license 102SN/A * terms below provided that you ensure that this notice is replicated 112SN/A * unmodified and in its entirety in all distributions of the software, 122SN/A * modified or unmodified, in source code or in binary form. 132SN/A * 142SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan 152SN/A * All rights reserved. 162SN/A * 172SN/A * Redistribution and use in source and binary forms, with or without 182SN/A * modification, are permitted provided that the following conditions are 192SN/A * met: redistributions of source code must retain the above copyright 202SN/A * notice, this list of conditions and the following disclaimer; 212SN/A * redistributions in binary form must reproduce the above copyright 222SN/A * notice, this list of conditions and the following disclaimer in the 232SN/A * documentation and/or other materials provided with the distribution; 242SN/A * neither the name of the copyright holders nor the names of its 252SN/A * contributors may be used to endorse or promote products derived from 262SN/A * this software without specific prior written permission. 272665Ssaidi@eecs.umich.edu * 282665Ssaidi@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 292665Ssaidi@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 302SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 312SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 324183Sgblack@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 332439SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 348229Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 352680Sktlim@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 368232Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 378229Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 384183Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 398784Sgblack@eecs.umich.edu * 404183Sgblack@eecs.umich.edu * Authors: Nathan Binkert 412SN/A * Ron Dreslinski 4210417Sandreas.hansson@arm.com */ 432201SN/A 448784Sgblack@eecs.umich.edu/* @file 458589Sgblack@eecs.umich.edu * Device module for modelling a fixed bandwidth full duplex ethernet link 468589Sgblack@eecs.umich.edu */ 478589Sgblack@eecs.umich.edu 488589Sgblack@eecs.umich.edu#include "dev/net/etherlink.hh" 498589Sgblack@eecs.umich.edu 502201SN/A#include <cmath> 512612SN/A#include <deque> 5210417Sandreas.hansson@arm.com#include <string> 532612SN/A#include <vector> 546815SLisa.Hsu@amd.com 552612SN/A#include "base/random.hh" 565004Sgblack@eecs.umich.edu#include "base/trace.hh" 5710417Sandreas.hansson@arm.com#include "debug/Ethernet.hh" 588545Ssaidi@eecs.umich.edu#include "debug/EthernetData.hh" 598545Ssaidi@eecs.umich.edu#include "dev/net/etherdump.hh" 608545Ssaidi@eecs.umich.edu#include "dev/net/etherint.hh" 618545Ssaidi@eecs.umich.edu#include "dev/net/etherpkt.hh" 6210417Sandreas.hansson@arm.com#include "params/EtherLink.hh" 634183Sgblack@eecs.umich.edu#include "sim/core.hh" 648589Sgblack@eecs.umich.edu#include "sim/serialize.hh" 658784Sgblack@eecs.umich.edu#include "sim/system.hh" 668784Sgblack@eecs.umich.edu 678784Sgblack@eecs.umich.eduusing namespace std; 688784Sgblack@eecs.umich.edu 698589Sgblack@eecs.umich.eduEtherLink::EtherLink(const Params *p) 704183Sgblack@eecs.umich.edu : SimObject(p) 714434Ssaidi@eecs.umich.edu{ 724183Sgblack@eecs.umich.edu link[0] = new Link(name() + ".link0", this, 0, p->speed, 735004Sgblack@eecs.umich.edu p->delay, p->delay_var, p->dump); 7410417Sandreas.hansson@arm.com link[1] = new Link(name() + ".link1", this, 1, p->speed, 755004Sgblack@eecs.umich.edu p->delay, p->delay_var, p->dump); 765004Sgblack@eecs.umich.edu 775004Sgblack@eecs.umich.edu interface[0] = new Interface(name() + ".int0", link[0], link[1]); 78 interface[1] = new Interface(name() + ".int1", link[1], link[0]); 79} 80 81 82EtherLink::~EtherLink() 83{ 84 delete link[0]; 85 delete link[1]; 86 87 delete interface[0]; 88 delete interface[1]; 89} 90 91EtherInt* 92EtherLink::getEthPort(const std::string &if_name, int idx) 93{ 94 Interface *i; 95 if (if_name == "int0") 96 i = interface[0]; 97 else if (if_name == "int1") 98 i = interface[1]; 99 else 100 return NULL; 101 if (i->getPeer()) 102 panic("interface already connected to\n"); 103 104 return i; 105} 106 107 108EtherLink::Interface::Interface(const string &name, Link *tx, Link *rx) 109 : EtherInt(name), txlink(tx) 110{ 111 tx->setTxInt(this); 112 rx->setRxInt(this); 113} 114 115EtherLink::Link::Link(const string &name, EtherLink *p, int num, 116 double rate, Tick delay, Tick delay_var, EtherDump *d) 117 : objName(name), parent(p), number(num), txint(NULL), rxint(NULL), 118 ticksPerByte(rate), linkDelay(delay), delayVar(delay_var), dump(d), 119 doneEvent([this]{ txDone(); }, name), 120 txQueueEvent([this]{ processTxQueue(); }, name) 121{ } 122 123void 124EtherLink::serialize(CheckpointOut &cp) const 125{ 126 link[0]->serialize("link0", cp); 127 link[1]->serialize("link1", cp); 128} 129 130void 131EtherLink::unserialize(CheckpointIn &cp) 132{ 133 link[0]->unserialize("link0", cp); 134 link[1]->unserialize("link1", cp); 135} 136 137void 138EtherLink::Link::txComplete(EthPacketPtr packet) 139{ 140 DPRINTF(Ethernet, "packet received: len=%d\n", packet->length); 141 DDUMP(EthernetData, packet->data, packet->length); 142 rxint->sendPacket(packet); 143} 144 145void 146EtherLink::Link::txDone() 147{ 148 if (dump) 149 dump->dump(packet); 150 151 if (linkDelay > 0) { 152 DPRINTF(Ethernet, "packet delayed: delay=%d\n", linkDelay); 153 txQueue.emplace_back(std::make_pair(curTick() + linkDelay, packet)); 154 if (!txQueueEvent.scheduled()) 155 parent->schedule(txQueueEvent, txQueue.front().first); 156 } else { 157 assert(txQueue.empty()); 158 txComplete(packet); 159 } 160 161 packet = 0; 162 assert(!busy()); 163 164 txint->sendDone(); 165} 166 167void 168EtherLink::Link::processTxQueue() 169{ 170 auto cur(txQueue.front()); 171 txQueue.pop_front(); 172 173 // Schedule a new event to process the next packet in the queue. 174 if (!txQueue.empty()) { 175 auto next(txQueue.front()); 176 assert(next.first > curTick()); 177 parent->schedule(txQueueEvent, next.first); 178 } 179 180 assert(cur.first == curTick()); 181 txComplete(cur.second); 182} 183 184bool 185EtherLink::Link::transmit(EthPacketPtr pkt) 186{ 187 if (busy()) { 188 DPRINTF(Ethernet, "packet not sent, link busy\n"); 189 return false; 190 } 191 192 DPRINTF(Ethernet, "packet sent: len=%d\n", pkt->length); 193 DDUMP(EthernetData, pkt->data, pkt->length); 194 195 packet = pkt; 196 Tick delay = (Tick)ceil(((double)pkt->simLength * ticksPerByte) + 1.0); 197 if (delayVar != 0) 198 delay += random_mt.random<Tick>(0, delayVar); 199 200 DPRINTF(Ethernet, "scheduling packet: delay=%d, (rate=%f)\n", 201 delay, ticksPerByte); 202 parent->schedule(doneEvent, curTick() + delay); 203 204 return true; 205} 206 207void 208EtherLink::Link::serialize(const string &base, CheckpointOut &cp) const 209{ 210 bool packet_exists = packet != nullptr; 211 paramOut(cp, base + ".packet_exists", packet_exists); 212 if (packet_exists) 213 packet->serialize(base + ".packet", cp); 214 215 bool event_scheduled = doneEvent.scheduled(); 216 paramOut(cp, base + ".event_scheduled", event_scheduled); 217 if (event_scheduled) { 218 Tick event_time = doneEvent.when(); 219 paramOut(cp, base + ".event_time", event_time); 220 } 221 222 const size_t tx_queue_size(txQueue.size()); 223 paramOut(cp, base + ".tx_queue_size", tx_queue_size); 224 unsigned idx(0); 225 for (const auto &pe : txQueue) { 226 paramOut(cp, csprintf("%s.txQueue[%i].tick", base, idx), pe.first); 227 pe.second->serialize(csprintf("%s.txQueue[%i].packet", base, idx), cp); 228 229 ++idx; 230 } 231} 232 233void 234EtherLink::Link::unserialize(const string &base, CheckpointIn &cp) 235{ 236 bool packet_exists; 237 paramIn(cp, base + ".packet_exists", packet_exists); 238 if (packet_exists) { 239 packet = make_shared<EthPacketData>(); 240 packet->unserialize(base + ".packet", cp); 241 } 242 243 bool event_scheduled; 244 paramIn(cp, base + ".event_scheduled", event_scheduled); 245 if (event_scheduled) { 246 Tick event_time; 247 paramIn(cp, base + ".event_time", event_time); 248 parent->schedule(doneEvent, event_time); 249 } 250 251 size_t tx_queue_size; 252 if (optParamIn(cp, base + ".tx_queue_size", tx_queue_size)) { 253 for (size_t idx = 0; idx < tx_queue_size; ++idx) { 254 Tick tick; 255 EthPacketPtr delayed_packet = make_shared<EthPacketData>(); 256 257 paramIn(cp, csprintf("%s.txQueue[%i].tick", base, idx), tick); 258 delayed_packet->unserialize( 259 csprintf("%s.txQueue[%i].packet", base, idx), cp); 260 261 fatal_if(!txQueue.empty() && txQueue.back().first > tick, 262 "Invalid txQueue packet order in EtherLink!\n"); 263 txQueue.emplace_back(std::make_pair(tick, delayed_packet)); 264 } 265 266 if (!txQueue.empty()) 267 parent->schedule(txQueueEvent, txQueue.front().first); 268 } else { 269 // We can't reliably convert in-flight packets from old 270 // checkpoints. In fact, gem5 hasn't been able to load these 271 // packets for at least two years before the format change. 272 warn("Old-style EtherLink serialization format detected, " 273 "in-flight packets may have been dropped.\n"); 274 } 275} 276 277EtherLink * 278EtherLinkParams::create() 279{ 280 return new EtherLink(this); 281} 282