2 * Copyright (c) 2002-2004 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.
30 * Device module for modelling a fixed bandwidth full duplex ethernet link
38 #include "base/trace.hh"
39 #include "dev/etherdump.hh"
40 #include "dev/etherint.hh"
41 #include "dev/etherlink.hh"
42 #include "dev/etherpkt.hh"
43 #include "sim/builder.hh"
44 #include "sim/serialize.hh"
45 #include "sim/system.hh"
46 #include "sim/universe.hh"
50 EtherLink::EtherLink(const string
&name
, EtherInt
*i1
, EtherInt
*i2
,
51 Tick speed
, Tick dly
, EtherDump
*dump
)
54 double rate
= ((double)ticksPerSecond
* 8.0) / (double)speed
;
55 Tick delay
= US2Ticks(dly
);
57 link1
= new Link(name
+ ".link1", rate
, delay
, dump
);
58 link2
= new Link(name
+ ".link2", rate
, delay
, dump
);
60 int1
= new Interface(name
+ ".int1", link1
, link2
);
61 int2
= new Interface(name
+ ".int2", link2
, link1
);
69 EtherLink::~EtherLink()
78 EtherLink::Interface::Interface(const string
&name
, Link
*tx
, Link
*rx
)
79 : EtherInt(name
), txlink(tx
)
85 EtherLink::Link::Link(const string
&name
, double rate
, Tick delay
,
87 : objName(name
), txint(NULL
), rxint(NULL
), ticksPerByte(rate
),
88 linkDelay(delay
), dump(d
), doneEvent(this)
92 EtherLink::serialize(ostream
&os
)
94 nameOut(os
, name() + ".link1");
96 nameOut(os
, name() + ".link2");
101 EtherLink::unserialize(Checkpoint
*cp
, const string
§ion
)
103 link1
->unserialize(cp
, section
+ ".link1");
104 link2
->unserialize(cp
, section
+ ".link2");
108 EtherLink::Link::txComplete(PacketPtr packet
)
110 DPRINTF(Ethernet
, "packet received: len=%d\n", packet
->length
);
111 DDUMP(EthernetData
, packet
->data
, packet
->length
);
112 rxint
->sendPacket(packet
);
115 class LinkDelayEvent
: public Event
118 EtherLink::Link
*link
;
121 // non-scheduling version for createForUnserialize()
122 LinkDelayEvent(EtherLink::Link
*link
);
125 LinkDelayEvent(EtherLink::Link
*link
, PacketPtr pkt
, Tick when
);
129 virtual void serialize(ostream
&os
);
130 virtual void unserialize(Checkpoint
*cp
, const string
§ion
);
131 static Serializable
*createForUnserialize(Checkpoint
*cp
,
132 const string
§ion
);
137 EtherLink::Link::txDone()
143 DPRINTF(Ethernet
, "packet delayed: delay=%d\n", linkDelay
);
144 new LinkDelayEvent(this, packet
, curTick
+ linkDelay
);
156 EtherLink::Link::transmit(PacketPtr pkt
)
159 DPRINTF(Ethernet
, "packet not sent, link busy\n");
163 DPRINTF(Ethernet
, "packet sent: len=%d\n", pkt
->length
);
164 DDUMP(EthernetData
, pkt
->data
, pkt
->length
);
167 Tick delay
= (Tick
)ceil(((double)pkt
->length
* ticksPerByte
) + 1.0);
168 DPRINTF(Ethernet
, "scheduling packet: delay=%d, (rate=%f)\n",
169 delay
, ticksPerByte
);
170 doneEvent
.schedule(curTick
+ delay
);
176 EtherLink::Link::serialize(ostream
&os
)
178 bool packet_exists
= packet
;
179 SERIALIZE_SCALAR(packet_exists
);
181 bool event_scheduled
= doneEvent
.scheduled();
182 SERIALIZE_SCALAR(event_scheduled
);
183 if (event_scheduled
) {
184 Tick event_time
= doneEvent
.when();
185 SERIALIZE_SCALAR(event_time
);
189 packet
->serialize("packet", os
);
193 EtherLink::Link::unserialize(Checkpoint
*cp
, const string
§ion
)
196 UNSERIALIZE_SCALAR(packet_exists
);
198 packet
= new PacketData(16384);
199 packet
->unserialize("packet", cp
, section
);
202 bool event_scheduled
;
203 UNSERIALIZE_SCALAR(event_scheduled
);
204 if (event_scheduled
) {
206 UNSERIALIZE_SCALAR(event_time
);
207 doneEvent
.schedule(event_time
);
211 LinkDelayEvent::LinkDelayEvent(EtherLink::Link
*l
)
212 : Event(&mainEventQueue
), link(l
)
214 setFlags(AutoSerialize
);
215 setFlags(AutoDelete
);
218 LinkDelayEvent::LinkDelayEvent(EtherLink::Link
*l
, PacketPtr p
, Tick when
)
219 : Event(&mainEventQueue
), link(l
), packet(p
)
221 setFlags(AutoSerialize
);
222 setFlags(AutoDelete
);
227 LinkDelayEvent::process()
229 link
->txComplete(packet
);
233 LinkDelayEvent::serialize(ostream
&os
)
235 paramOut(os
, "type", string("LinkDelayEvent"));
236 Event::serialize(os
);
237 SERIALIZE_OBJPTR(link
);
239 packet
->serialize("packet", os
);
244 LinkDelayEvent::unserialize(Checkpoint
*cp
, const string
§ion
)
246 Event::unserialize(cp
, section
);
247 packet
= new PacketData(16384);
248 packet
->unserialize("packet", cp
, section
);
253 LinkDelayEvent::createForUnserialize(Checkpoint
*cp
, const string
§ion
)
255 EtherLink::Link
*link
;
256 UNSERIALIZE_OBJPTR(link
);
257 return new LinkDelayEvent(link
);
260 REGISTER_SERIALIZEABLE("LinkDelayEvent", LinkDelayEvent
)
262 BEGIN_DECLARE_SIM_OBJECT_PARAMS(EtherLink
)
264 SimObjectParam
<EtherInt
*> int1
;
265 SimObjectParam
<EtherInt
*> int2
;
268 SimObjectParam
<EtherDump
*> dump
;
270 END_DECLARE_SIM_OBJECT_PARAMS(EtherLink
)
272 BEGIN_INIT_SIM_OBJECT_PARAMS(EtherLink
)
274 INIT_PARAM(int1
, "interface 1"),
275 INIT_PARAM(int2
, "interface 2"),
276 INIT_PARAM_DFLT(speed
, "link speed in bits per second", 100000000),
277 INIT_PARAM_DFLT(delay
, "transmit delay of packets in us", 0),
278 INIT_PARAM_DFLT(dump
, "object to dump network packets to", NULL
)
280 END_INIT_SIM_OBJECT_PARAMS(EtherLink
)
282 CREATE_SIM_OBJECT(EtherLink
)
284 return new EtherLink(getInstanceName(), int1
, int2
, speed
, delay
, dump
);
287 REGISTER_SIM_OBJECT("EtherLink", EtherLink
)