dist_etherlink.cc revision 11263
14019Sstever@eecs.umich.edu/*
23187Srdreslin@umich.edu * Copyright (c) 2015 ARM Limited
33187Srdreslin@umich.edu * All rights reserved
43187Srdreslin@umich.edu *
53187Srdreslin@umich.edu * The license below extends only to copyright in the software and shall
63187Srdreslin@umich.edu * not be construed as granting a license to any other intellectual
73187Srdreslin@umich.edu * property including but not limited to intellectual property relating
83187Srdreslin@umich.edu * to a hardware implementation of the functionality of the software
93187Srdreslin@umich.edu * licensed hereunder.  You may use the software subject to the license
103187Srdreslin@umich.edu * terms below provided that you ensure that this notice is replicated
113187Srdreslin@umich.edu * unmodified and in its entirety in all distributions of the software,
123187Srdreslin@umich.edu * modified or unmodified, in source code or in binary form.
133187Srdreslin@umich.edu *
143187Srdreslin@umich.edu * Redistribution and use in source and binary forms, with or without
153187Srdreslin@umich.edu * modification, are permitted provided that the following conditions are
163187Srdreslin@umich.edu * met: redistributions of source code must retain the above copyright
173187Srdreslin@umich.edu * notice, this list of conditions and the following disclaimer;
183187Srdreslin@umich.edu * redistributions in binary form must reproduce the above copyright
193187Srdreslin@umich.edu * notice, this list of conditions and the following disclaimer in the
203187Srdreslin@umich.edu * documentation and/or other materials provided with the distribution;
213187Srdreslin@umich.edu * neither the name of the copyright holders nor the names of its
223187Srdreslin@umich.edu * contributors may be used to endorse or promote products derived from
233187Srdreslin@umich.edu * this software without specific prior written permission.
243187Srdreslin@umich.edu *
253187Srdreslin@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
263187Srdreslin@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
273187Srdreslin@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
283187Srdreslin@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
293187Srdreslin@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
303187Srdreslin@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
313187Srdreslin@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
323187Srdreslin@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
333187Srdreslin@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
343187Srdreslin@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
353187Srdreslin@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
363187Srdreslin@umich.edu *
374444Ssaidi@eecs.umich.edu * Authors: Gabor Dozsa
383187Srdreslin@umich.edu */
393208Srdreslin@umich.edu
403187Srdreslin@umich.edu/* @file
413187Srdreslin@umich.edu * Device module for a full duplex ethernet link for multi gem5 simulations.
423187Srdreslin@umich.edu */
433187Srdreslin@umich.edu
443187Srdreslin@umich.edu#include "dev/net/multi_etherlink.hh"
453187Srdreslin@umich.edu
463187Srdreslin@umich.edu#include <arpa/inet.h>
473187Srdreslin@umich.edu#include <sys/socket.h>
484444Ssaidi@eecs.umich.edu#include <unistd.h>
493187Srdreslin@umich.edu
503187Srdreslin@umich.edu#include <cmath>
513187Srdreslin@umich.edu#include <deque>
523187Srdreslin@umich.edu#include <string>
533196Srdreslin@umich.edu#include <vector>
543196Srdreslin@umich.edu
554019Sstever@eecs.umich.edu#include "base/random.hh"
563187Srdreslin@umich.edu#include "base/trace.hh"
573187Srdreslin@umich.edu#include "debug/EthernetData.hh"
583187Srdreslin@umich.edu#include "debug/MultiEthernet.hh"
594467Sstever@eecs.umich.edu#include "debug/MultiEthernetPkt.hh"
604467Sstever@eecs.umich.edu#include "dev/net/etherdump.hh"
613187Srdreslin@umich.edu#include "dev/net/etherint.hh"
623187Srdreslin@umich.edu#include "dev/net/etherlink.hh"
633257Srdreslin@umich.edu#include "dev/net/etherobject.hh"
643208Srdreslin@umich.edu#include "dev/net/etherpkt.hh"
653187Srdreslin@umich.edu#include "dev/net/multi_iface.hh"
666978SLisa.Hsu@amd.com#include "dev/net/tcp_iface.hh"
673187Srdreslin@umich.edu#include "params/EtherLink.hh"
683187Srdreslin@umich.edu#include "sim/core.hh"
693187Srdreslin@umich.edu#include "sim/serialize.hh"
703187Srdreslin@umich.edu#include "sim/system.hh"
713187Srdreslin@umich.edu
723187Srdreslin@umich.eduusing namespace std;
733187Srdreslin@umich.edu
743187Srdreslin@umich.eduMultiEtherLink::MultiEtherLink(const Params *p)
753187Srdreslin@umich.edu    : EtherObject(p)
764467Sstever@eecs.umich.edu{
773187Srdreslin@umich.edu    DPRINTF(MultiEthernet,"MultiEtherLink::MultiEtherLink() "
783187Srdreslin@umich.edu            "link delay:%llu\n", p->delay);
793187Srdreslin@umich.edu
803187Srdreslin@umich.edu    txLink = new TxLink(name() + ".link0", this, p->speed, p->delay_var,
813187Srdreslin@umich.edu                        p->dump);
823187Srdreslin@umich.edu    rxLink = new RxLink(name() + ".link1", this, p->delay, p->dump);
833187Srdreslin@umich.edu
843187Srdreslin@umich.edu    // create the multi (TCP) interface to talk to the peer gem5 processes.
853187Srdreslin@umich.edu    multiIface = new TCPIface(p->server_name, p->server_port, p->multi_rank,
863187Srdreslin@umich.edu                              p->sync_start, p->sync_repeat, this);
873187Srdreslin@umich.edu
883341Srdreslin@umich.edu    localIface = new LocalIface(name() + ".int0", txLink, rxLink, multiIface);
893341Srdreslin@umich.edu}
903257Srdreslin@umich.edu
91MultiEtherLink::~MultiEtherLink()
92{
93    delete txLink;
94    delete rxLink;
95    delete localIface;
96    delete multiIface;
97}
98
99EtherInt*
100MultiEtherLink::getEthPort(const std::string &if_name, int idx)
101{
102    if (if_name != "int0") {
103        return nullptr;
104    } else {
105        panic_if(localIface->getPeer(), "interface already connected to");
106    }
107    return localIface;
108}
109
110void MultiEtherLink::memWriteback()
111{
112    DPRINTF(MultiEthernet,"MultiEtherLink::memWriteback() called\n");
113    multiIface->drainDone();
114}
115
116void
117MultiEtherLink::serialize(CheckpointOut &cp) const
118{
119    multiIface->serialize("multiIface", cp);
120    txLink->serialize("txLink", cp);
121    rxLink->serialize("rxLink", cp);
122}
123
124void
125MultiEtherLink::unserialize(CheckpointIn &cp)
126{
127    multiIface->unserialize("multiIface", cp);
128    txLink->unserialize("txLink", cp);
129    rxLink->unserialize("rxLink", cp);
130}
131
132void
133MultiEtherLink::init()
134{
135    DPRINTF(MultiEthernet,"MultiEtherLink::init() called\n");
136    multiIface->initRandom();
137}
138
139void
140MultiEtherLink::startup()
141{
142    DPRINTF(MultiEthernet,"MultiEtherLink::startup() called\n");
143    multiIface->startPeriodicSync();
144}
145
146void
147MultiEtherLink::RxLink::setMultiInt(MultiIface *m)
148{
149    assert(!multiIface);
150    multiIface = m;
151    // Spawn a new receiver thread that will process messages
152    // coming in from peer gem5 processes.
153    // The receive thread will also schedule a (receive) doneEvent
154    // for each incoming data packet.
155    multiIface->spawnRecvThread(&doneEvent, linkDelay);
156}
157
158void
159MultiEtherLink::RxLink::rxDone()
160{
161    assert(!busy());
162
163    // retrieve the packet that triggered the receive done event
164    packet = multiIface->packetIn();
165
166    if (dump)
167        dump->dump(packet);
168
169    DPRINTF(MultiEthernetPkt, "MultiEtherLink::MultiLink::rxDone() "
170            "packet received: len=%d\n", packet->length);
171    DDUMP(EthernetData, packet->data, packet->length);
172
173    localIface->sendPacket(packet);
174
175    packet = nullptr;
176}
177
178void
179MultiEtherLink::TxLink::txDone()
180{
181    if (dump)
182        dump->dump(packet);
183
184    packet = nullptr;
185    assert(!busy());
186
187    localIface->sendDone();
188}
189
190bool
191MultiEtherLink::TxLink::transmit(EthPacketPtr pkt)
192{
193    if (busy()) {
194        DPRINTF(MultiEthernet, "packet not sent, link busy\n");
195        return false;
196    }
197
198    packet = pkt;
199    Tick delay = (Tick)ceil(((double)pkt->length * ticksPerByte) + 1.0);
200    if (delayVar != 0)
201        delay += random_mt.random<Tick>(0, delayVar);
202
203    // send the packet to the peers
204    assert(multiIface);
205    multiIface->packetOut(pkt, delay);
206
207    // schedule the send done event
208    parent->schedule(doneEvent, curTick() + delay);
209
210    return true;
211}
212
213void
214MultiEtherLink::Link::serialize(const string &base, CheckpointOut &cp) const
215{
216    bool packet_exists = (packet != nullptr);
217    paramOut(cp, base + ".packet_exists", packet_exists);
218    if (packet_exists)
219        packet->serialize(base + ".packet", cp);
220
221    bool event_scheduled = event->scheduled();
222    paramOut(cp, base + ".event_scheduled", event_scheduled);
223    if (event_scheduled) {
224        Tick event_time = event->when();
225        paramOut(cp, base + ".event_time", event_time);
226    }
227}
228
229void
230MultiEtherLink::Link::unserialize(const string &base, CheckpointIn &cp)
231{
232    bool packet_exists;
233    paramIn(cp, base + ".packet_exists", packet_exists);
234    if (packet_exists) {
235        packet = make_shared<EthPacketData>(16384);
236        packet->unserialize(base + ".packet", cp);
237    }
238
239    bool event_scheduled;
240    paramIn(cp, base + ".event_scheduled", event_scheduled);
241    if (event_scheduled) {
242        Tick event_time;
243        paramIn(cp, base + ".event_time", event_time);
244        parent->schedule(*event, event_time);
245    }
246}
247
248MultiEtherLink::LocalIface::LocalIface(const std::string &name,
249                                       TxLink *tx,
250                                       RxLink *rx,
251                                       MultiIface *m) :
252    EtherInt(name), txLink(tx)
253{
254    tx->setLocalInt(this);
255    rx->setLocalInt(this);
256    tx->setMultiInt(m);
257    rx->setMultiInt(m);
258}
259
260MultiEtherLink *
261MultiEtherLinkParams::create()
262{
263    return new MultiEtherLink(this);
264}
265
266
267