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/universe.hh"
45 #include "sim/system.hh"
49 EtherLink::EtherLink(const string
&name
, EtherInt
*i1
, EtherInt
*i2
,
50 Tick speed
, Tick dly
, EtherDump
*dump
)
53 double rate
= ((double)ticksPerSecond
* 8.0) / (double)speed
;
54 Tick delay
= US2Ticks(dly
);
56 link1
= new Link(name
+ ".link1", rate
, delay
, dump
);
57 link2
= new Link(name
+ ".link2", rate
, delay
, dump
);
59 int1
= new Interface(name
+ ".int1", link1
, link2
);
60 int2
= new Interface(name
+ ".int2", link2
, link1
);
68 EtherLink::~EtherLink()
77 EtherLink::Interface::Interface(const string
&name
, Link
*tx
, Link
*rx
)
78 : EtherInt(name
), txlink(tx
)
84 EtherLink::Link::Link(const string
&name
, double rate
, Tick delay
,
86 : objName(name
), txint(NULL
), rxint(NULL
), ticksPerByte(rate
),
87 linkDelay(delay
), dump(d
), doneEvent(this)
91 EtherLink::serialize(ostream
&os
)
93 nameOut(os
, name() + ".link1");
95 nameOut(os
, name() + ".link2");
100 EtherLink::unserialize(Checkpoint
*cp
, const string
§ion
)
102 link1
->unserialize(cp
, section
+ ".link1");
103 link2
->unserialize(cp
, section
+ ".link2");
107 EtherLink::Link::txComplete(PacketPtr
&packet
)
109 DPRINTF(Ethernet
, "packet received: len=%d\n", packet
->length
);
110 DDUMP(EthernetData
, packet
->data
, packet
->length
);
111 rxint
->sendPacket(packet
);
114 class LinkDelayEvent
: public Event
117 EtherLink::Link
*link
;
120 // non-scheduling version for createForUnserialize()
121 LinkDelayEvent(EtherLink::Link
*link
);
124 LinkDelayEvent(EtherLink::Link
*link
, PacketPtr
&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
);
136 EtherLink::Link::txDone()
142 DPRINTF(Ethernet
, "packet delayed: delay=%d\n", linkDelay
);
143 new LinkDelayEvent(this, packet
, curTick
+ linkDelay
);
155 EtherLink::Link::transmit(PacketPtr
&pkt
)
158 DPRINTF(Ethernet
, "packet not sent, link busy\n");
162 DPRINTF(Ethernet
, "packet sent: len=%d\n", pkt
->length
);
163 DDUMP(EthernetData
, pkt
->data
, pkt
->length
);
166 Tick delay
= (Tick
)ceil(((double)pkt
->length
* ticksPerByte
) + 1.0);
167 DPRINTF(Ethernet
, "scheduling packet: delay=%d, (rate=%f)\n",
168 delay
, ticksPerByte
);
169 doneEvent
.schedule(curTick
+ delay
);
175 EtherLink::Link::serialize(ostream
&os
)
177 bool packet_exists
= packet
;
178 SERIALIZE_SCALAR(packet_exists
);
180 bool event_scheduled
= doneEvent
.scheduled();
181 SERIALIZE_SCALAR(event_scheduled
);
182 if (event_scheduled
) {
183 Tick event_time
= doneEvent
.when();
184 SERIALIZE_SCALAR(event_time
);
188 nameOut(os
, csprintf("%s.packet", name()));
189 packet
->serialize(os
);
194 EtherLink::Link::unserialize(Checkpoint
*cp
, const string
§ion
)
197 UNSERIALIZE_SCALAR(packet_exists
);
199 packet
= new EtherPacket
;
200 packet
->unserialize(cp
, csprintf("%s.packet", section
));
203 bool event_scheduled
;
204 UNSERIALIZE_SCALAR(event_scheduled
);
205 if (event_scheduled
) {
207 UNSERIALIZE_SCALAR(event_time
);
208 doneEvent
.schedule(event_time
);
212 LinkDelayEvent::LinkDelayEvent(EtherLink::Link
*l
)
213 : Event(&mainEventQueue
), link(l
)
215 setFlags(AutoSerialize
);
216 setFlags(AutoDelete
);
219 LinkDelayEvent::LinkDelayEvent(EtherLink::Link
*l
, PacketPtr
&p
, Tick when
)
220 : Event(&mainEventQueue
), link(l
), packet(p
)
222 setFlags(AutoSerialize
);
223 setFlags(AutoDelete
);
228 LinkDelayEvent::process()
230 link
->txComplete(packet
);
234 LinkDelayEvent::serialize(ostream
&os
)
236 paramOut(os
, "type", string("LinkDelayEvent"));
237 Event::serialize(os
);
238 SERIALIZE_OBJPTR(link
);
240 nameOut(os
, csprintf("%s.packet", name()));
241 packet
->serialize(os
);
246 LinkDelayEvent::unserialize(Checkpoint
*cp
, const string
§ion
)
248 Event::unserialize(cp
, section
);
249 packet
= new EtherPacket
;
250 packet
->unserialize(cp
, csprintf("%s.packet", section
));
255 LinkDelayEvent::createForUnserialize(Checkpoint
*cp
, const string
§ion
)
257 EtherLink::Link
*link
;
258 UNSERIALIZE_OBJPTR(link
);
259 return new LinkDelayEvent(link
);
262 REGISTER_SERIALIZEABLE("LinkDelayEvent", LinkDelayEvent
)
264 BEGIN_DECLARE_SIM_OBJECT_PARAMS(EtherLink
)
266 SimObjectParam
<EtherInt
*> interface1
;
267 SimObjectParam
<EtherInt
*> interface2
;
268 Param
<Tick
> link_speed
;
269 Param
<Tick
> link_delay
;
270 SimObjectParam
<EtherDump
*> packet_dump
;
272 END_DECLARE_SIM_OBJECT_PARAMS(EtherLink
)
274 BEGIN_INIT_SIM_OBJECT_PARAMS(EtherLink
)
276 INIT_PARAM(interface1
, "interface 1"),
277 INIT_PARAM(interface2
, "interface 2"),
278 INIT_PARAM_DFLT(link_speed
, "link speed in bits per second", 100000000),
279 INIT_PARAM_DFLT(link_delay
, "transmit delay of packets in us", 0),
280 INIT_PARAM_DFLT(packet_dump
, "object to dump network packets to", NULL
)
282 END_INIT_SIM_OBJECT_PARAMS(EtherLink
)
284 CREATE_SIM_OBJECT(EtherLink
)
286 return new EtherLink(getInstanceName(), interface1
, interface2
, link_speed
,
287 link_delay
, packet_dump
);
290 REGISTER_SIM_OBJECT("EtherLink", EtherLink
)