5ebc4fba52ee8c35081054e24c16909794cbdde5
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Authors: Nathan Binkert
33 * Device module for modelling a fixed bandwidth full duplex ethernet link
41 #include "base/random.hh"
42 #include "base/trace.hh"
43 #include "debug/Ethernet.hh"
44 #include "debug/EthernetData.hh"
45 #include "dev/etherdump.hh"
46 #include "dev/etherint.hh"
47 #include "dev/etherlink.hh"
48 #include "dev/etherpkt.hh"
49 #include "params/EtherLink.hh"
50 #include "sim/core.hh"
51 #include "sim/serialize.hh"
52 #include "sim/system.hh"
56 EtherLink::EtherLink(const Params
*p
)
59 link
[0] = new Link(name() + ".link0", this, 0, p
->speed
,
60 p
->delay
, p
->delay_var
, p
->dump
);
61 link
[1] = new Link(name() + ".link1", this, 1, p
->speed
,
62 p
->delay
, p
->delay_var
, p
->dump
);
64 interface
[0] = new Interface(name() + ".int0", link
[0], link
[1]);
65 interface
[1] = new Interface(name() + ".int1", link
[1], link
[0]);
69 EtherLink::~EtherLink()
79 EtherLink::getEthPort(const std::string
&if_name
, int idx
)
82 if (if_name
== "int0")
84 else if (if_name
== "int1")
89 panic("interface already connected to\n");
95 EtherLink::Interface::Interface(const string
&name
, Link
*tx
, Link
*rx
)
96 : EtherInt(name
), txlink(tx
)
102 EtherLink::Link::Link(const string
&name
, EtherLink
*p
, int num
,
103 double rate
, Tick delay
, Tick delay_var
, EtherDump
*d
)
104 : objName(name
), parent(p
), number(num
), txint(NULL
), rxint(NULL
),
105 ticksPerByte(rate
), linkDelay(delay
), delayVar(delay_var
), dump(d
),
110 EtherLink::serialize(ostream
&os
)
112 link
[0]->serialize("link0", os
);
113 link
[1]->serialize("link1", os
);
117 EtherLink::unserialize(Checkpoint
*cp
, const string
§ion
)
119 link
[0]->unserialize("link0", cp
, section
);
120 link
[1]->unserialize("link1", cp
, section
);
124 EtherLink::Link::txComplete(EthPacketPtr packet
)
126 DPRINTF(Ethernet
, "packet received: len=%d\n", packet
->length
);
127 DDUMP(EthernetData
, packet
->data
, packet
->length
);
128 rxint
->sendPacket(packet
);
131 class LinkDelayEvent
: public Event
134 EtherLink::Link
*link
;
138 // non-scheduling version for createForUnserialize()
140 LinkDelayEvent(EtherLink::Link
*link
, EthPacketPtr pkt
);
144 virtual void serialize(ostream
&os
);
145 virtual void unserialize(Checkpoint
*cp
, const string
§ion
);
146 static Serializable
*createForUnserialize(Checkpoint
*cp
,
147 const string
§ion
);
151 EtherLink::Link::txDone()
157 DPRINTF(Ethernet
, "packet delayed: delay=%d\n", linkDelay
);
158 Event
*event
= new LinkDelayEvent(this, packet
);
159 parent
->schedule(event
, curTick() + linkDelay
);
171 EtherLink::Link::transmit(EthPacketPtr pkt
)
174 DPRINTF(Ethernet
, "packet not sent, link busy\n");
178 DPRINTF(Ethernet
, "packet sent: len=%d\n", pkt
->length
);
179 DDUMP(EthernetData
, pkt
->data
, pkt
->length
);
182 Tick delay
= (Tick
)ceil(((double)pkt
->length
* ticksPerByte
) + 1.0);
184 delay
+= random_mt
.random
<Tick
>(0, delayVar
);
186 DPRINTF(Ethernet
, "scheduling packet: delay=%d, (rate=%f)\n",
187 delay
, ticksPerByte
);
188 parent
->schedule(doneEvent
, curTick() + delay
);
194 EtherLink::Link::serialize(const string
&base
, ostream
&os
)
196 bool packet_exists
= packet
;
197 paramOut(os
, base
+ ".packet_exists", packet_exists
);
199 packet
->serialize(base
+ ".packet", os
);
201 bool event_scheduled
= doneEvent
.scheduled();
202 paramOut(os
, base
+ ".event_scheduled", event_scheduled
);
203 if (event_scheduled
) {
204 Tick event_time
= doneEvent
.when();
205 paramOut(os
, base
+ ".event_time", event_time
);
211 EtherLink::Link::unserialize(const string
&base
, Checkpoint
*cp
,
212 const string
§ion
)
215 paramIn(cp
, section
, base
+ ".packet_exists", packet_exists
);
217 packet
= new EthPacketData(16384);
218 packet
->unserialize(base
+ ".packet", cp
, section
);
221 bool event_scheduled
;
222 paramIn(cp
, section
, base
+ ".event_scheduled", event_scheduled
);
223 if (event_scheduled
) {
225 paramIn(cp
, section
, base
+ ".event_time", event_time
);
226 parent
->schedule(doneEvent
, event_time
);
230 LinkDelayEvent::LinkDelayEvent()
231 : Event(Default_Pri
, AutoSerialize
| AutoDelete
), link(NULL
)
235 LinkDelayEvent::LinkDelayEvent(EtherLink::Link
*l
, EthPacketPtr p
)
236 : Event(Default_Pri
, AutoSerialize
| AutoDelete
), link(l
), packet(p
)
241 LinkDelayEvent::process()
243 link
->txComplete(packet
);
247 LinkDelayEvent::serialize(ostream
&os
)
249 paramOut(os
, "type", string("LinkDelayEvent"));
250 Event::serialize(os
);
252 EtherLink
*parent
= link
->parent
;
253 bool number
= link
->number
;
254 SERIALIZE_OBJPTR(parent
);
255 SERIALIZE_SCALAR(number
);
257 packet
->serialize("packet", os
);
262 LinkDelayEvent::unserialize(Checkpoint
*cp
, const string
§ion
)
264 Event::unserialize(cp
, section
);
268 UNSERIALIZE_OBJPTR(parent
);
269 UNSERIALIZE_SCALAR(number
);
271 link
= parent
->link
[number
];
273 packet
= new EthPacketData(16384);
274 packet
->unserialize("packet", cp
, section
);
279 LinkDelayEvent::createForUnserialize(Checkpoint
*cp
, const string
§ion
)
281 return new LinkDelayEvent();
284 REGISTER_SERIALIZEABLE("LinkDelayEvent", LinkDelayEvent
)
287 EtherLinkParams::create()
289 return new EtherLink(this);