etherlink.cc revision 11071
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 <cmath>
49#include <deque>
50#include <string>
51#include <vector>
52
53#include "base/random.hh"
54#include "base/trace.hh"
55#include "debug/Ethernet.hh"
56#include "debug/EthernetData.hh"
57#include "dev/etherdump.hh"
58#include "dev/etherint.hh"
59#include "dev/etherlink.hh"
60#include "dev/etherpkt.hh"
61#include "params/EtherLink.hh"
62#include "sim/core.hh"
63#include "sim/serialize.hh"
64#include "sim/system.hh"
65
66using namespace std;
67
68EtherLink::EtherLink(const Params *p)
69    : EtherObject(p)
70{
71    link[0] = new Link(name() + ".link0", this, 0, p->speed,
72                       p->delay, p->delay_var, p->dump);
73    link[1] = new Link(name() + ".link1", this, 1, p->speed,
74                       p->delay, p->delay_var, p->dump);
75
76    interface[0] = new Interface(name() + ".int0", link[0], link[1]);
77    interface[1] = new Interface(name() + ".int1", link[1], link[0]);
78}
79
80
81EtherLink::~EtherLink()
82{
83    delete link[0];
84    delete link[1];
85
86    delete interface[0];
87    delete interface[1];
88}
89
90EtherInt*
91EtherLink::getEthPort(const std::string &if_name, int idx)
92{
93    Interface *i;
94    if (if_name == "int0")
95        i = interface[0];
96    else if (if_name == "int1")
97        i = interface[1];
98    else
99        return NULL;
100    if (i->getPeer())
101        panic("interface already connected to\n");
102
103    return i;
104}
105
106
107EtherLink::Interface::Interface(const string &name, Link *tx, Link *rx)
108    : EtherInt(name), txlink(tx)
109{
110    tx->setTxInt(this);
111    rx->setRxInt(this);
112}
113
114EtherLink::Link::Link(const string &name, EtherLink *p, int num,
115                      double rate, Tick delay, Tick delay_var, EtherDump *d)
116    : objName(name), parent(p), number(num), txint(NULL), rxint(NULL),
117      ticksPerByte(rate), linkDelay(delay), delayVar(delay_var), dump(d),
118      doneEvent(this), txQueueEvent(this)
119{ }
120
121void
122EtherLink::serialize(CheckpointOut &cp) const
123{
124    link[0]->serialize("link0", cp);
125    link[1]->serialize("link1", cp);
126}
127
128void
129EtherLink::unserialize(CheckpointIn &cp)
130{
131    link[0]->unserialize("link0", cp);
132    link[1]->unserialize("link1", cp);
133}
134
135void
136EtherLink::Link::txComplete(EthPacketPtr packet)
137{
138    DPRINTF(Ethernet, "packet received: len=%d\n", packet->length);
139    DDUMP(EthernetData, packet->data, packet->length);
140    rxint->sendPacket(packet);
141}
142
143void
144EtherLink::Link::txDone()
145{
146    if (dump)
147        dump->dump(packet);
148
149    if (linkDelay > 0) {
150        DPRINTF(Ethernet, "packet delayed: delay=%d\n", linkDelay);
151        txQueue.emplace_back(std::make_pair(curTick() + linkDelay, packet));
152        if (!txQueueEvent.scheduled())
153            parent->schedule(txQueueEvent, txQueue.front().first);
154    } else {
155        assert(txQueue.empty());
156        txComplete(packet);
157    }
158
159    packet = 0;
160    assert(!busy());
161
162    txint->sendDone();
163}
164
165void
166EtherLink::Link::processTxQueue()
167{
168    auto cur(txQueue.front());
169    txQueue.pop_front();
170
171    // Schedule a new event to process the next packet in the queue.
172    if (!txQueue.empty()) {
173        auto next(txQueue.front());
174        assert(next.first > curTick());
175        parent->schedule(txQueueEvent, next.first);
176    }
177
178    assert(cur.first == curTick());
179    txComplete(cur.second);
180}
181
182bool
183EtherLink::Link::transmit(EthPacketPtr pkt)
184{
185    if (busy()) {
186        DPRINTF(Ethernet, "packet not sent, link busy\n");
187        return false;
188    }
189
190    DPRINTF(Ethernet, "packet sent: len=%d\n", pkt->length);
191    DDUMP(EthernetData, pkt->data, pkt->length);
192
193    packet = pkt;
194    Tick delay = (Tick)ceil(((double)pkt->length * ticksPerByte) + 1.0);
195    if (delayVar != 0)
196        delay += random_mt.random<Tick>(0, delayVar);
197
198    DPRINTF(Ethernet, "scheduling packet: delay=%d, (rate=%f)\n",
199            delay, ticksPerByte);
200    parent->schedule(doneEvent, curTick() + delay);
201
202    return true;
203}
204
205void
206EtherLink::Link::serialize(const string &base, CheckpointOut &cp) const
207{
208    bool packet_exists = packet != nullptr;
209    paramOut(cp, base + ".packet_exists", packet_exists);
210    if (packet_exists)
211        packet->serialize(base + ".packet", cp);
212
213    bool event_scheduled = doneEvent.scheduled();
214    paramOut(cp, base + ".event_scheduled", event_scheduled);
215    if (event_scheduled) {
216        Tick event_time = doneEvent.when();
217        paramOut(cp, base + ".event_time", event_time);
218    }
219
220    const size_t tx_queue_size(txQueue.size());
221    paramOut(cp, base + ".tx_queue_size", tx_queue_size);
222    unsigned idx(0);
223    for (const auto &pe : txQueue) {
224        paramOut(cp, csprintf("%s.txQueue[%i].tick", base, idx), pe.first);
225        pe.second->serialize(csprintf("%s.txQueue[%i].packet", base, idx), cp);
226
227        ++idx;
228    }
229}
230
231void
232EtherLink::Link::unserialize(const string &base, CheckpointIn &cp)
233{
234    bool packet_exists;
235    paramIn(cp, base + ".packet_exists", packet_exists);
236    if (packet_exists) {
237        packet = make_shared<EthPacketData>(16384);
238        packet->unserialize(base + ".packet", cp);
239    }
240
241    bool event_scheduled;
242    paramIn(cp, base + ".event_scheduled", event_scheduled);
243    if (event_scheduled) {
244        Tick event_time;
245        paramIn(cp, base + ".event_time", event_time);
246        parent->schedule(doneEvent, event_time);
247    }
248
249    size_t tx_queue_size;
250    if (optParamIn(cp, base + ".tx_queue_size", tx_queue_size)) {
251        for (size_t idx = 0; idx < tx_queue_size; ++idx) {
252            Tick tick;
253            EthPacketPtr delayed_packet = make_shared<EthPacketData>(16384);
254
255            paramIn(cp, csprintf("%s.txQueue[%i].tick", base, idx), tick);
256            delayed_packet->unserialize(
257                csprintf("%s.txQueue[%i].packet", base, idx), cp);
258
259            fatal_if(!txQueue.empty() && txQueue.back().first > tick,
260                     "Invalid txQueue packet order in EtherLink!\n");
261            txQueue.emplace_back(std::make_pair(tick, delayed_packet));
262        }
263
264        if (!txQueue.empty())
265            parent->schedule(txQueueEvent, txQueue.front().first);
266    } else {
267        // We can't reliably convert in-flight packets from old
268        // checkpoints. In fact, gem5 hasn't been able to load these
269        // packets for at least two years before the format change.
270        warn("Old-style EtherLink serialization format detected, "
271             "in-flight packets may have been dropped.\n");
272    }
273}
274
275EtherLink *
276EtherLinkParams::create()
277{
278    return new EtherLink(this);
279}
280