gem5  v20.1.0.0
etherlink.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 ARM Limited
3  * All rights reserved
4  *
5  * The license below extends only to copyright in the software and shall
6  * not be construed as granting a license to any other intellectual
7  * property including but not limited to intellectual property relating
8  * to a hardware implementation of the functionality of the software
9  * licensed hereunder. You may use the software subject to the license
10  * terms below provided that you ensure that this notice is replicated
11  * unmodified and in its entirety in all distributions of the software,
12  * modified or unmodified, in source code or in binary form.
13  *
14  * Copyright (c) 2002-2005 The Regents of The University of Michigan
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions are
19  * met: redistributions of source code must retain the above copyright
20  * notice, this list of conditions and the following disclaimer;
21  * redistributions in binary form must reproduce the above copyright
22  * notice, this list of conditions and the following disclaimer in the
23  * documentation and/or other materials provided with the distribution;
24  * neither the name of the copyright holders nor the names of its
25  * contributors may be used to endorse or promote products derived from
26  * this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39  */
40 
41 /* @file
42  * Device module for modelling a fixed bandwidth full duplex ethernet link
43  */
44 
45 #include "dev/net/etherlink.hh"
46 
47 #include <cmath>
48 #include <deque>
49 #include <string>
50 #include <vector>
51 
52 #include "base/random.hh"
53 #include "base/trace.hh"
54 #include "debug/Ethernet.hh"
55 #include "debug/EthernetData.hh"
56 #include "dev/net/etherdump.hh"
57 #include "dev/net/etherint.hh"
58 #include "dev/net/etherpkt.hh"
59 #include "params/EtherLink.hh"
60 #include "sim/core.hh"
61 #include "sim/serialize.hh"
62 #include "sim/system.hh"
63 
64 using namespace std;
65 
67  : SimObject(p)
68 {
69  link[0] = new Link(name() + ".link0", this, 0, p->speed,
70  p->delay, p->delay_var, p->dump);
71  link[1] = new Link(name() + ".link1", this, 1, p->speed,
72  p->delay, p->delay_var, p->dump);
73 
74  interface[0] = new Interface(name() + ".int0", link[0], link[1]);
75  interface[1] = new Interface(name() + ".int1", link[1], link[0]);
76 }
77 
78 
80 {
81  delete link[0];
82  delete link[1];
83 
84  delete interface[0];
85  delete interface[1];
86 }
87 
88 Port &
89 EtherLink::getPort(const std::string &if_name, PortID idx)
90 {
91  if (if_name == "int0")
92  return *interface[0];
93  else if (if_name == "int1")
94  return *interface[1];
95  return SimObject::getPort(if_name, idx);
96 }
97 
98 
100  : EtherInt(name), txlink(tx)
101 {
102  tx->setTxInt(this);
103  rx->setRxInt(this);
104 }
105 
106 EtherLink::Link::Link(const string &name, EtherLink *p, int num,
107  double rate, Tick delay, Tick delay_var, EtherDump *d)
108  : objName(name), parent(p), number(num), txint(NULL), rxint(NULL),
109  ticksPerByte(rate), linkDelay(delay), delayVar(delay_var), dump(d),
110  doneEvent([this]{ txDone(); }, name),
111  txQueueEvent([this]{ processTxQueue(); }, name)
112 { }
113 
114 void
116 {
117  link[0]->serialize("link0", cp);
118  link[1]->serialize("link1", cp);
119 }
120 
121 void
123 {
124  link[0]->unserialize("link0", cp);
125  link[1]->unserialize("link1", cp);
126 }
127 
128 void
130 {
131  DPRINTF(Ethernet, "packet received: len=%d\n", packet->length);
132  DDUMP(EthernetData, packet->data, packet->length);
133  rxint->sendPacket(packet);
134 }
135 
136 void
138 {
139  if (dump)
140  dump->dump(packet);
141 
142  if (linkDelay > 0) {
143  DPRINTF(Ethernet, "packet delayed: delay=%d\n", linkDelay);
144  txQueue.emplace_back(std::make_pair(curTick() + linkDelay, packet));
145  if (!txQueueEvent.scheduled())
146  parent->schedule(txQueueEvent, txQueue.front().first);
147  } else {
148  assert(txQueue.empty());
149  txComplete(packet);
150  }
151 
152  packet = 0;
153  assert(!busy());
154 
155  txint->sendDone();
156 }
157 
158 void
160 {
161  auto cur(txQueue.front());
162  txQueue.pop_front();
163 
164  // Schedule a new event to process the next packet in the queue.
165  if (!txQueue.empty()) {
166  auto next(txQueue.front());
167  assert(next.first > curTick());
168  parent->schedule(txQueueEvent, next.first);
169  }
170 
171  assert(cur.first == curTick());
172  txComplete(cur.second);
173 }
174 
175 bool
177 {
178  if (busy()) {
179  DPRINTF(Ethernet, "packet not sent, link busy\n");
180  return false;
181  }
182 
183  DPRINTF(Ethernet, "packet sent: len=%d\n", pkt->length);
184  DDUMP(EthernetData, pkt->data, pkt->length);
185 
186  packet = pkt;
187  Tick delay = (Tick)ceil(((double)pkt->simLength * ticksPerByte) + 1.0);
188  if (delayVar != 0)
189  delay += random_mt.random<Tick>(0, delayVar);
190 
191  DPRINTF(Ethernet, "scheduling packet: delay=%d, (rate=%f)\n",
192  delay, ticksPerByte);
193  parent->schedule(doneEvent, curTick() + delay);
194 
195  return true;
196 }
197 
198 void
200 {
201  bool packet_exists = packet != nullptr;
202  paramOut(cp, base + ".packet_exists", packet_exists);
203  if (packet_exists)
204  packet->serialize(base + ".packet", cp);
205 
206  bool event_scheduled = doneEvent.scheduled();
207  paramOut(cp, base + ".event_scheduled", event_scheduled);
208  if (event_scheduled) {
209  Tick event_time = doneEvent.when();
210  paramOut(cp, base + ".event_time", event_time);
211  }
212 
213  const size_t tx_queue_size(txQueue.size());
214  paramOut(cp, base + ".tx_queue_size", tx_queue_size);
215  unsigned idx(0);
216  for (const auto &pe : txQueue) {
217  paramOut(cp, csprintf("%s.txQueue[%i].tick", base, idx), pe.first);
218  pe.second->serialize(csprintf("%s.txQueue[%i].packet", base, idx), cp);
219 
220  ++idx;
221  }
222 }
223 
224 void
226 {
227  bool packet_exists;
228  paramIn(cp, base + ".packet_exists", packet_exists);
229  if (packet_exists) {
230  packet = make_shared<EthPacketData>();
231  packet->unserialize(base + ".packet", cp);
232  }
233 
234  bool event_scheduled;
235  paramIn(cp, base + ".event_scheduled", event_scheduled);
236  if (event_scheduled) {
237  Tick event_time;
238  paramIn(cp, base + ".event_time", event_time);
239  parent->schedule(doneEvent, event_time);
240  }
241 
242  size_t tx_queue_size = 0;
243  if (optParamIn(cp, base + ".tx_queue_size", tx_queue_size)) {
244  for (size_t idx = 0; idx < tx_queue_size; ++idx) {
245  Tick tick;
246  EthPacketPtr delayed_packet = make_shared<EthPacketData>();
247 
248  paramIn(cp, csprintf("%s.txQueue[%i].tick", base, idx), tick);
249  delayed_packet->unserialize(
250  csprintf("%s.txQueue[%i].packet", base, idx), cp);
251 
252  fatal_if(!txQueue.empty() && txQueue.back().first > tick,
253  "Invalid txQueue packet order in EtherLink!\n");
254  txQueue.emplace_back(std::make_pair(tick, delayed_packet));
255  }
256 
257  if (!txQueue.empty())
258  parent->schedule(txQueueEvent, txQueue.front().first);
259  } else {
260  // We can't reliably convert in-flight packets from old
261  // checkpoints. In fact, gem5 hasn't been able to load these
262  // packets for at least two years before the format change.
263  warn("Old-style EtherLink serialization format detected, "
264  "in-flight packets may have been dropped.\n");
265  }
266 }
267 
268 EtherLink *
269 EtherLinkParams::create()
270 {
271  return new EtherLink(this);
272 }
warn
#define warn(...)
Definition: logging.hh:239
system.hh
serialize.hh
EtherInt
Definition: etherint.hh:47
random.hh
etherint.hh
etherdump.hh
Tick
uint64_t Tick
Tick count type.
Definition: types.hh:63
PortID
int16_t PortID
Port index/ID type, and a symbolic name for an invalid port id.
Definition: types.hh:237
X86ISA::base
Bitfield< 51, 12 > base
Definition: pagetable.hh:141
paramOut
void paramOut(CheckpointOut &cp, const string &name, ExtMachInst const &machInst)
Definition: types.cc:38
random_mt
Random random_mt
Definition: random.cc:96
cp
Definition: cprintf.cc:40
SimObject::getPort
virtual Port & getPort(const std::string &if_name, PortID idx=InvalidPortID)
Get a port with a given name and index.
Definition: sim_object.cc:123
DPRINTF
#define DPRINTF(x,...)
Definition: trace.hh:234
ArmISA::d
Bitfield< 9 > d
Definition: miscregs_types.hh:60
optParamIn
bool optParamIn(CheckpointIn &cp, const std::string &name, T &param, bool warn=true)
This function is used for restoring optional parameters from the checkpoint.
Definition: serialize.hh:507
Port
Ports are used to interface objects to each other.
Definition: port.hh:56
core.hh
name
const std::string & name()
Definition: trace.cc:50
DDUMP
#define DDUMP(x, data, count)
DPRINTF is a debugging trace facility that allows one to selectively enable tracing statements.
Definition: trace.hh:233
SimObject::name
virtual const std::string name() const
Definition: sim_object.hh:133
EthPacketPtr
std::shared_ptr< EthPacketData > EthPacketPtr
Definition: etherpkt.hh:87
std
Overload hash function for BasicBlockRange type.
Definition: vec_reg.hh:587
Stats::dump
void dump()
Dump all statistics data to the registered outputs.
Definition: statistics.cc:560
X86ISA::pe
Bitfield< 0 > pe
Definition: misc.hh:604
paramIn
void paramIn(CheckpointIn &cp, const string &name, ExtMachInst &machInst)
Definition: types.cc:69
etherpkt.hh
Random::random
std::enable_if< std::is_integral< T >::value, T >::type random()
Use the SFINAE idiom to choose an implementation based on whether the type is integral or floating po...
Definition: random.hh:86
CheckpointOut
std::ostream CheckpointOut
Definition: serialize.hh:63
EtherDump
Definition: etherdump.hh:45
trace.hh
MipsISA::p
Bitfield< 0 > p
Definition: pra_constants.hh:323
fatal_if
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition: logging.hh:219
CheckpointIn
Definition: serialize.hh:67
csprintf
std::string csprintf(const char *format, const Args &...args)
Definition: cprintf.hh:158
curTick
Tick curTick()
The current simulated tick.
Definition: core.hh:45
SimObject
Abstract superclass for simulation objects.
Definition: sim_object.hh:92

Generated on Wed Sep 30 2020 14:02:11 for gem5 by doxygen 1.8.17