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 "dev/etherdump.hh"
44 #include "dev/etherint.hh"
45 #include "dev/etherlink.hh"
46 #include "dev/etherpkt.hh"
47 #include "sim/builder.hh"
48 #include "sim/serialize.hh"
49 #include "sim/system.hh"
50 #include "sim/root.hh"
54 EtherLink::EtherLink(const string
&name
, EtherInt
*peer0
, EtherInt
*peer1
,
55 double rate
, Tick delay
, Tick delayVar
, EtherDump
*dump
)
58 link
[0] = new Link(name
+ ".link0", this, 0, rate
, delay
, delayVar
, dump
);
59 link
[1] = new Link(name
+ ".link1", this, 1, rate
, delay
, delayVar
, dump
);
61 interface
[0] = new Interface(name
+ ".int0", link
[0], link
[1]);
62 interface
[1] = new Interface(name
+ ".int1", link
[1], link
[0]);
64 interface
[0]->setPeer(peer0
);
65 peer0
->setPeer(interface
[0]);
66 interface
[1]->setPeer(peer1
);
67 peer1
->setPeer(interface
[1]);
70 EtherLink::~EtherLink()
79 EtherLink::Interface::Interface(const string
&name
, Link
*tx
, Link
*rx
)
80 : EtherInt(name
), txlink(tx
)
86 EtherLink::Link::Link(const string
&name
, EtherLink
*p
, int num
,
87 double rate
, Tick delay
, Tick delay_var
, EtherDump
*d
)
88 : objName(name
), parent(p
), number(num
), txint(NULL
), rxint(NULL
),
89 ticksPerByte(rate
), linkDelay(delay
), delayVar(delay_var
), dump(d
),
94 EtherLink::serialize(ostream
&os
)
96 link
[0]->serialize("link0", os
);
97 link
[1]->serialize("link1", os
);
101 EtherLink::unserialize(Checkpoint
*cp
, const string
§ion
)
103 link
[0]->unserialize("link0", cp
, section
);
104 link
[1]->unserialize("link1", cp
, section
);
108 EtherLink::Link::txComplete(EthPacketPtr 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
;
122 // non-scheduling version for createForUnserialize()
124 LinkDelayEvent(EtherLink::Link
*link
, EthPacketPtr pkt
, Tick when
);
128 virtual void serialize(ostream
&os
);
129 virtual void unserialize(Checkpoint
*cp
, const string
§ion
);
130 static Serializable
*createForUnserialize(Checkpoint
*cp
,
131 const string
§ion
);
135 EtherLink::Link::txDone()
141 DPRINTF(Ethernet
, "packet delayed: delay=%d\n", linkDelay
);
142 new LinkDelayEvent(this, packet
, curTick
+ linkDelay
);
154 EtherLink::Link::transmit(EthPacketPtr pkt
)
157 DPRINTF(Ethernet
, "packet not sent, link busy\n");
161 DPRINTF(Ethernet
, "packet sent: len=%d\n", pkt
->length
);
162 DDUMP(EthernetData
, pkt
->data
, pkt
->length
);
165 Tick delay
= (Tick
)ceil(((double)pkt
->length
* ticksPerByte
) + 1.0);
168 delay
+= var
.uniform(0, delayVar
);
170 DPRINTF(Ethernet
, "scheduling packet: delay=%d, (rate=%f)\n",
171 delay
, ticksPerByte
);
172 doneEvent
.schedule(curTick
+ delay
);
178 EtherLink::Link::serialize(const string
&base
, ostream
&os
)
180 bool packet_exists
= packet
;
181 paramOut(os
, base
+ ".packet_exists", packet_exists
);
183 packet
->serialize(base
+ ".packet", os
);
185 bool event_scheduled
= doneEvent
.scheduled();
186 paramOut(os
, base
+ ".event_scheduled", event_scheduled
);
187 if (event_scheduled
) {
188 Tick event_time
= doneEvent
.when();
189 paramOut(os
, base
+ ".event_time", event_time
);
195 EtherLink::Link::unserialize(const string
&base
, Checkpoint
*cp
,
196 const string
§ion
)
199 paramIn(cp
, section
, base
+ ".packet_exists", packet_exists
);
201 packet
= new EthPacketData(16384);
202 packet
->unserialize(base
+ ".packet", cp
, section
);
205 bool event_scheduled
;
206 paramIn(cp
, section
, base
+ ".event_scheduled", event_scheduled
);
207 if (event_scheduled
) {
209 paramIn(cp
, section
, base
+ ".event_time", event_time
);
210 doneEvent
.schedule(event_time
);
214 LinkDelayEvent::LinkDelayEvent()
215 : Event(&mainEventQueue
), link(NULL
)
217 setFlags(AutoSerialize
);
218 setFlags(AutoDelete
);
221 LinkDelayEvent::LinkDelayEvent(EtherLink::Link
*l
, EthPacketPtr p
, Tick when
)
222 : Event(&mainEventQueue
), link(l
), packet(p
)
224 setFlags(AutoSerialize
);
225 setFlags(AutoDelete
);
230 LinkDelayEvent::process()
232 link
->txComplete(packet
);
236 LinkDelayEvent::serialize(ostream
&os
)
238 paramOut(os
, "type", string("LinkDelayEvent"));
239 Event::serialize(os
);
241 EtherLink
*parent
= link
->parent
;
242 bool number
= link
->number
;
243 SERIALIZE_OBJPTR(parent
);
244 SERIALIZE_SCALAR(number
);
246 packet
->serialize("packet", os
);
251 LinkDelayEvent::unserialize(Checkpoint
*cp
, const string
§ion
)
253 Event::unserialize(cp
, section
);
257 UNSERIALIZE_OBJPTR(parent
);
258 UNSERIALIZE_SCALAR(number
);
260 link
= parent
->link
[number
];
262 packet
= new EthPacketData(16384);
263 packet
->unserialize("packet", cp
, section
);
268 LinkDelayEvent::createForUnserialize(Checkpoint
*cp
, const string
§ion
)
270 return new LinkDelayEvent();
273 REGISTER_SERIALIZEABLE("LinkDelayEvent", LinkDelayEvent
)
275 BEGIN_DECLARE_SIM_OBJECT_PARAMS(EtherLink
)
277 SimObjectParam
<EtherInt
*> int1
;
278 SimObjectParam
<EtherInt
*> int2
;
281 Param
<Tick
> delay_var
;
282 SimObjectParam
<EtherDump
*> dump
;
284 END_DECLARE_SIM_OBJECT_PARAMS(EtherLink
)
286 BEGIN_INIT_SIM_OBJECT_PARAMS(EtherLink
)
288 INIT_PARAM(int1
, "interface 1"),
289 INIT_PARAM(int2
, "interface 2"),
290 INIT_PARAM(speed
, "link speed in bits per second"),
291 INIT_PARAM(delay
, "transmit delay of packets in us"),
292 INIT_PARAM(delay_var
, "Difference in amount of time to traverse wire"),
293 INIT_PARAM(dump
, "object to dump network packets to")
295 END_INIT_SIM_OBJECT_PARAMS(EtherLink
)
297 CREATE_SIM_OBJECT(EtherLink
)
299 return new EtherLink(getInstanceName(), int1
, int2
, speed
, delay
, delay_var
,
303 REGISTER_SIM_OBJECT("EtherLink", EtherLink
)