gem5  v19.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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  * Authors: Nathan Binkert
41  * Ron Dreslinski
42  */
43 
44 /* @file
45  * Device module for modelling a fixed bandwidth full duplex ethernet link
46  */
47 
48 #include "dev/net/etherlink.hh"
49 
50 #include <cmath>
51 #include <deque>
52 #include <string>
53 #include <vector>
54 
55 #include "base/random.hh"
56 #include "base/trace.hh"
57 #include "debug/Ethernet.hh"
58 #include "debug/EthernetData.hh"
59 #include "dev/net/etherdump.hh"
60 #include "dev/net/etherint.hh"
61 #include "dev/net/etherpkt.hh"
62 #include "params/EtherLink.hh"
63 #include "sim/core.hh"
64 #include "sim/serialize.hh"
65 #include "sim/system.hh"
66 
67 using namespace std;
68 
70  : SimObject(p)
71 {
72  link[0] = new Link(name() + ".link0", this, 0, p->speed,
73  p->delay, p->delay_var, p->dump);
74  link[1] = new Link(name() + ".link1", this, 1, p->speed,
75  p->delay, p->delay_var, p->dump);
76 
77  interface[0] = new Interface(name() + ".int0", link[0], link[1]);
78  interface[1] = new Interface(name() + ".int1", link[1], link[0]);
79 }
80 
81 
83 {
84  delete link[0];
85  delete link[1];
86 
87  delete interface[0];
88  delete interface[1];
89 }
90 
91 Port &
92 EtherLink::getPort(const std::string &if_name, PortID idx)
93 {
94  if (if_name == "int0")
95  return *interface[0];
96  else if (if_name == "int1")
97  return *interface[1];
98  return SimObject::getPort(if_name, idx);
99 }
100 
101 
103  : EtherInt(name), txlink(tx)
104 {
105  tx->setTxInt(this);
106  rx->setRxInt(this);
107 }
108 
109 EtherLink::Link::Link(const string &name, EtherLink *p, int num,
110  double rate, Tick delay, Tick delay_var, EtherDump *d)
111  : objName(name), parent(p), number(num), txint(NULL), rxint(NULL),
112  ticksPerByte(rate), linkDelay(delay), delayVar(delay_var), dump(d),
113  doneEvent([this]{ txDone(); }, name),
114  txQueueEvent([this]{ processTxQueue(); }, name)
115 { }
116 
117 void
119 {
120  link[0]->serialize("link0", cp);
121  link[1]->serialize("link1", cp);
122 }
123 
124 void
126 {
127  link[0]->unserialize("link0", cp);
128  link[1]->unserialize("link1", cp);
129 }
130 
131 void
133 {
134  DPRINTF(Ethernet, "packet received: len=%d\n", packet->length);
135  DDUMP(EthernetData, packet->data, packet->length);
136  rxint->sendPacket(packet);
137 }
138 
139 void
141 {
142  if (dump)
143  dump->dump(packet);
144 
145  if (linkDelay > 0) {
146  DPRINTF(Ethernet, "packet delayed: delay=%d\n", linkDelay);
147  txQueue.emplace_back(std::make_pair(curTick() + linkDelay, packet));
148  if (!txQueueEvent.scheduled())
149  parent->schedule(txQueueEvent, txQueue.front().first);
150  } else {
151  assert(txQueue.empty());
153  }
154 
155  packet = 0;
156  assert(!busy());
157 
158  txint->sendDone();
159 }
160 
161 void
163 {
164  auto cur(txQueue.front());
165  txQueue.pop_front();
166 
167  // Schedule a new event to process the next packet in the queue.
168  if (!txQueue.empty()) {
169  auto next(txQueue.front());
170  assert(next.first > curTick());
171  parent->schedule(txQueueEvent, next.first);
172  }
173 
174  assert(cur.first == curTick());
175  txComplete(cur.second);
176 }
177 
178 bool
180 {
181  if (busy()) {
182  DPRINTF(Ethernet, "packet not sent, link busy\n");
183  return false;
184  }
185 
186  DPRINTF(Ethernet, "packet sent: len=%d\n", pkt->length);
187  DDUMP(EthernetData, pkt->data, pkt->length);
188 
189  packet = pkt;
190  Tick delay = (Tick)ceil(((double)pkt->simLength * ticksPerByte) + 1.0);
191  if (delayVar != 0)
192  delay += random_mt.random<Tick>(0, delayVar);
193 
194  DPRINTF(Ethernet, "scheduling packet: delay=%d, (rate=%f)\n",
195  delay, ticksPerByte);
196  parent->schedule(doneEvent, curTick() + delay);
197 
198  return true;
199 }
200 
201 void
203 {
204  bool packet_exists = packet != nullptr;
205  paramOut(cp, base + ".packet_exists", packet_exists);
206  if (packet_exists)
207  packet->serialize(base + ".packet", cp);
208 
209  bool event_scheduled = doneEvent.scheduled();
210  paramOut(cp, base + ".event_scheduled", event_scheduled);
211  if (event_scheduled) {
212  Tick event_time = doneEvent.when();
213  paramOut(cp, base + ".event_time", event_time);
214  }
215 
216  const size_t tx_queue_size(txQueue.size());
217  paramOut(cp, base + ".tx_queue_size", tx_queue_size);
218  unsigned idx(0);
219  for (const auto &pe : txQueue) {
220  paramOut(cp, csprintf("%s.txQueue[%i].tick", base, idx), pe.first);
221  pe.second->serialize(csprintf("%s.txQueue[%i].packet", base, idx), cp);
222 
223  ++idx;
224  }
225 }
226 
227 void
229 {
230  bool packet_exists;
231  paramIn(cp, base + ".packet_exists", packet_exists);
232  if (packet_exists) {
233  packet = make_shared<EthPacketData>();
234  packet->unserialize(base + ".packet", cp);
235  }
236 
237  bool event_scheduled;
238  paramIn(cp, base + ".event_scheduled", event_scheduled);
239  if (event_scheduled) {
240  Tick event_time;
241  paramIn(cp, base + ".event_time", event_time);
242  parent->schedule(doneEvent, event_time);
243  }
244 
245  size_t tx_queue_size = 0;
246  if (optParamIn(cp, base + ".tx_queue_size", tx_queue_size)) {
247  for (size_t idx = 0; idx < tx_queue_size; ++idx) {
248  Tick tick;
249  EthPacketPtr delayed_packet = make_shared<EthPacketData>();
250 
251  paramIn(cp, csprintf("%s.txQueue[%i].tick", base, idx), tick);
252  delayed_packet->unserialize(
253  csprintf("%s.txQueue[%i].packet", base, idx), cp);
254 
255  fatal_if(!txQueue.empty() && txQueue.back().first > tick,
256  "Invalid txQueue packet order in EtherLink!\n");
257  txQueue.emplace_back(std::make_pair(tick, delayed_packet));
258  }
259 
260  if (!txQueue.empty())
261  parent->schedule(txQueueEvent, txQueue.front().first);
262  } else {
263  // We can't reliably convert in-flight packets from old
264  // checkpoints. In fact, gem5 hasn't been able to load these
265  // packets for at least two years before the format change.
266  warn("Old-style EtherLink serialization format detected, "
267  "in-flight packets may have been dropped.\n");
268  }
269 }
270 
271 EtherLink *
272 EtherLinkParams::create()
273 {
274  return new EtherLink(this);
275 }
#define DPRINTF(x,...)
Definition: trace.hh:229
const std::string & name() const
Return port name (for DPRINTF).
Definition: etherint.hh:61
Ports are used to interface objects to each other.
Definition: port.hh:60
virtual Port & getPort(const std::string &if_name, PortID idx=InvalidPortID)
Get a port with a given name and index.
Definition: sim_object.cc:126
Tick when() const
Get the time that the event is scheduled.
Definition: eventq.hh:401
#define DDUMP(x, data, count)
Definition: trace.hh:228
Overload hash function for BasicBlockRange type.
Definition: vec_reg.hh:586
Definition: cprintf.cc:42
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:83
Tick curTick()
The current simulated tick.
Definition: core.hh:47
std::string csprintf(const char *format, const Args &...args)
Definition: cprintf.hh:162
bool scheduled() const
Determine if the current event is scheduled.
Definition: eventq.hh:385
uint64_t Tick
Tick count type.
Definition: types.hh:63
void dump(EthPacketPtr &pkt)
Definition: etherdump.hh:59
void paramOut(CheckpointOut &cp, const string &name, ExtMachInst const &machInst)
Definition: types.cc:40
Bitfield< 9 > d
Bitfield< 51, 12 > base
Definition: pagetable.hh:142
Bitfield< 0 > pe
Definition: misc.hh:606
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition: logging.hh:203
std::shared_ptr< EthPacketData > EthPacketPtr
Definition: etherpkt.hh:90
virtual const std::string name() const
Definition: sim_object.hh:120
bool sendPacket(EthPacketPtr packet)
Definition: etherint.hh:72
std::ostream CheckpointOut
Definition: serialize.hh:68
Random random_mt
Definition: random.cc:100
void schedule(Event &event, Tick when)
Definition: eventq.hh:744
void paramIn(CheckpointIn &cp, const string &name, ExtMachInst &machInst)
Definition: types.cc:71
int16_t PortID
Port index/ID type, and a symbolic name for an invalid port id.
Definition: types.hh:237
#define warn(...)
Definition: logging.hh:212
void dump()
Dump all statistics data to the registered outputs.
Definition: statistics.cc:561
Bitfield< 0 > p
bool optParamIn(CheckpointIn &cp, const std::string &name, T &param, bool warn=true)
Definition: serialize.hh:394
Abstract superclass for simulation objects.
Definition: sim_object.hh:96

Generated on Fri Feb 28 2020 16:27:00 for gem5 by doxygen 1.8.13