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.
30 * Device module for modelling a fixed bandwidth full duplex ethernet link
38 #include "base/random.hh"
39 #include "base/trace.hh"
40 #include "dev/etherdump.hh"
41 #include "dev/etherint.hh"
42 #include "dev/etherlink.hh"
43 #include "dev/etherpkt.hh"
44 #include "sim/builder.hh"
45 #include "sim/serialize.hh"
46 #include "sim/system.hh"
47 #include "sim/root.hh"
51 EtherLink::EtherLink(const string
&name
, EtherInt
*peer0
, EtherInt
*peer1
,
52 double rate
, Tick delay
, Tick delayVar
, EtherDump
*dump
)
55 link
[0] = new Link(name
+ ".link0", this, 0, rate
, delay
, delayVar
, dump
);
56 link
[1] = new Link(name
+ ".link1", this, 1, rate
, delay
, delayVar
, dump
);
58 interface
[0] = new Interface(name
+ ".int0", link
[0], link
[1]);
59 interface
[1] = new Interface(name
+ ".int1", link
[1], link
[0]);
61 interface
[0]->setPeer(peer0
);
62 peer0
->setPeer(interface
[0]);
63 interface
[1]->setPeer(peer1
);
64 peer1
->setPeer(interface
[1]);
67 EtherLink::~EtherLink()
76 EtherLink::Interface::Interface(const string
&name
, Link
*tx
, Link
*rx
)
77 : EtherInt(name
), txlink(tx
)
83 EtherLink::Link::Link(const string
&name
, EtherLink
*p
, int num
,
84 double rate
, Tick delay
, Tick delay_var
, EtherDump
*d
)
85 : objName(name
), parent(p
), number(num
), txint(NULL
), rxint(NULL
),
86 ticksPerByte(rate
), linkDelay(delay
), delayVar(delay_var
), dump(d
),
91 EtherLink::serialize(ostream
&os
)
93 link
[0]->serialize("link0", os
);
94 link
[1]->serialize("link1", os
);
98 EtherLink::unserialize(Checkpoint
*cp
, const string
§ion
)
100 link
[0]->unserialize("link0", cp
, section
);
101 link
[1]->unserialize("link1", cp
, section
);
105 EtherLink::Link::txComplete(PacketPtr packet
)
107 DPRINTF(Ethernet
, "packet received: len=%d\n", packet
->length
);
108 DDUMP(EthernetData
, packet
->data
, packet
->length
);
109 rxint
->sendPacket(packet
);
112 class LinkDelayEvent
: public Event
115 EtherLink::Link
*link
;
119 // non-scheduling version for createForUnserialize()
121 LinkDelayEvent(EtherLink::Link
*link
, PacketPtr pkt
, Tick when
);
125 virtual void serialize(ostream
&os
);
126 virtual void unserialize(Checkpoint
*cp
, const string
§ion
);
127 static Serializable
*createForUnserialize(Checkpoint
*cp
,
128 const string
§ion
);
132 EtherLink::Link::txDone()
138 DPRINTF(Ethernet
, "packet delayed: delay=%d\n", linkDelay
);
139 new LinkDelayEvent(this, packet
, curTick
+ linkDelay
);
151 EtherLink::Link::transmit(PacketPtr pkt
)
154 DPRINTF(Ethernet
, "packet not sent, link busy\n");
158 DPRINTF(Ethernet
, "packet sent: len=%d\n", pkt
->length
);
159 DDUMP(EthernetData
, pkt
->data
, pkt
->length
);
162 Tick delay
= (Tick
)ceil(((double)pkt
->length
* ticksPerByte
) + 1.0);
165 delay
+= var
.uniform(0, delayVar
);
167 DPRINTF(Ethernet
, "scheduling packet: delay=%d, (rate=%f)\n",
168 delay
, ticksPerByte
);
169 doneEvent
.schedule(curTick
+ delay
);
175 EtherLink::Link::serialize(const string
&base
, ostream
&os
)
177 bool packet_exists
= packet
;
178 paramOut(os
, base
+ ".packet_exists", packet_exists
);
180 packet
->serialize(base
+ ".packet", os
);
182 bool event_scheduled
= doneEvent
.scheduled();
183 paramOut(os
, base
+ ".event_scheduled", event_scheduled
);
184 if (event_scheduled
) {
185 Tick event_time
= doneEvent
.when();
186 paramOut(os
, base
+ ".event_time", event_time
);
192 EtherLink::Link::unserialize(const string
&base
, Checkpoint
*cp
,
193 const string
§ion
)
196 paramIn(cp
, section
, base
+ ".packet_exists", packet_exists
);
198 packet
= new PacketData(16384);
199 packet
->unserialize(base
+ ".packet", cp
, section
);
202 bool event_scheduled
;
203 paramIn(cp
, section
, base
+ ".event_scheduled", event_scheduled
);
204 if (event_scheduled
) {
206 paramIn(cp
, section
, base
+ ".event_time", event_time
);
207 doneEvent
.schedule(event_time
);
211 LinkDelayEvent::LinkDelayEvent()
212 : Event(&mainEventQueue
), link(NULL
)
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
);
238 EtherLink
*parent
= link
->parent
;
239 bool number
= link
->number
;
240 SERIALIZE_OBJPTR(parent
);
241 SERIALIZE_SCALAR(number
);
243 packet
->serialize("packet", os
);
248 LinkDelayEvent::unserialize(Checkpoint
*cp
, const string
§ion
)
250 Event::unserialize(cp
, section
);
254 UNSERIALIZE_OBJPTR(parent
);
255 UNSERIALIZE_SCALAR(number
);
257 link
= parent
->link
[number
];
259 packet
= new PacketData(16384);
260 packet
->unserialize("packet", cp
, section
);
265 LinkDelayEvent::createForUnserialize(Checkpoint
*cp
, const string
§ion
)
267 return new LinkDelayEvent();
270 REGISTER_SERIALIZEABLE("LinkDelayEvent", LinkDelayEvent
)
272 BEGIN_DECLARE_SIM_OBJECT_PARAMS(EtherLink
)
274 SimObjectParam
<EtherInt
*> int1
;
275 SimObjectParam
<EtherInt
*> int2
;
278 Param
<Tick
> delay_var
;
279 SimObjectParam
<EtherDump
*> dump
;
281 END_DECLARE_SIM_OBJECT_PARAMS(EtherLink
)
283 BEGIN_INIT_SIM_OBJECT_PARAMS(EtherLink
)
285 INIT_PARAM(int1
, "interface 1"),
286 INIT_PARAM(int2
, "interface 2"),
287 INIT_PARAM(speed
, "link speed in bits per second"),
288 INIT_PARAM(delay
, "transmit delay of packets in us"),
289 INIT_PARAM(delay_var
, "Difference in amount of time to traverse wire"),
290 INIT_PARAM(dump
, "object to dump network packets to")
292 END_INIT_SIM_OBJECT_PARAMS(EtherLink
)
294 CREATE_SIM_OBJECT(EtherLink
)
296 return new EtherLink(getInstanceName(), int1
, int2
, speed
, delay
, delay_var
,
300 REGISTER_SIM_OBJECT("EtherLink", EtherLink
)