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
*peer0
, EtherInt
*peer1
,
51 Tick speed
, Tick dly
, EtherDump
*dump
)
54 double rate
= ((double)ticksPerSecond
* 8.0) / (double)speed
;
55 Tick delay
= dly
* Clock::Int::us
;
57 link
[0] = new Link(name
+ ".link0", this, 0, rate
, delay
, dump
);
58 link
[1] = new Link(name
+ ".link1", this, 1, rate
, delay
, dump
);
60 interface
[0] = new Interface(name
+ ".int0", link
[0], link
[1]);
61 interface
[1] = new Interface(name
+ ".int1", link
[1], link
[0]);
63 interface
[0]->setPeer(peer0
);
64 peer0
->setPeer(interface
[0]);
65 interface
[1]->setPeer(peer1
);
66 peer1
->setPeer(interface
[1]);
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
, EtherLink
*p
, int num
,
86 double rate
, Tick delay
, EtherDump
*d
)
87 : objName(name
), parent(p
), number(num
), txint(NULL
), rxint(NULL
),
88 ticksPerByte(rate
), linkDelay(delay
), dump(d
),
93 EtherLink::serialize(ostream
&os
)
95 link
[0]->serialize("link0", os
);
96 link
[1]->serialize("link1", os
);
100 EtherLink::unserialize(Checkpoint
*cp
, const string
§ion
)
102 link
[0]->unserialize("link0", cp
, section
);
103 link
[1]->unserialize("link1", cp
, section
);
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
;
121 // non-scheduling version for createForUnserialize()
123 LinkDelayEvent(EtherLink::Link
*link
, PacketPtr pkt
, Tick when
);
127 virtual void serialize(ostream
&os
);
128 virtual void unserialize(Checkpoint
*cp
, const string
§ion
);
129 static Serializable
*createForUnserialize(Checkpoint
*cp
,
130 const string
§ion
);
134 EtherLink::Link::txDone()
140 DPRINTF(Ethernet
, "packet delayed: delay=%d\n", linkDelay
);
141 new LinkDelayEvent(this, packet
, curTick
+ linkDelay
);
153 EtherLink::Link::transmit(PacketPtr pkt
)
156 DPRINTF(Ethernet
, "packet not sent, link busy\n");
160 DPRINTF(Ethernet
, "packet sent: len=%d\n", pkt
->length
);
161 DDUMP(EthernetData
, pkt
->data
, pkt
->length
);
164 Tick delay
= (Tick
)ceil(((double)pkt
->length
* ticksPerByte
) + 1.0);
165 DPRINTF(Ethernet
, "scheduling packet: delay=%d, (rate=%f)\n",
166 delay
, ticksPerByte
);
167 doneEvent
.schedule(curTick
+ delay
);
173 EtherLink::Link::serialize(const string
&base
, ostream
&os
)
175 bool packet_exists
= packet
;
176 paramOut(os
, base
+ ".packet_exists", packet_exists
);
178 packet
->serialize(base
+ ".packet", os
);
180 bool event_scheduled
= doneEvent
.scheduled();
181 paramOut(os
, base
+ ".event_scheduled", event_scheduled
);
182 if (event_scheduled
) {
183 Tick event_time
= doneEvent
.when();
184 paramOut(os
, base
+ ".event_time", event_time
);
190 EtherLink::Link::unserialize(const string
&base
, Checkpoint
*cp
,
191 const string
§ion
)
194 paramIn(cp
, section
, base
+ ".packet_exists", packet_exists
);
196 packet
= new PacketData(16384);
197 packet
->unserialize(base
+ ".packet", cp
, section
);
200 bool event_scheduled
;
201 paramIn(cp
, section
, base
+ ".event_scheduled", event_scheduled
);
202 if (event_scheduled
) {
204 paramIn(cp
, section
, base
+ ".event_time", event_time
);
205 doneEvent
.schedule(event_time
);
209 LinkDelayEvent::LinkDelayEvent()
210 : Event(&mainEventQueue
), link(NULL
)
212 setFlags(AutoSerialize
);
213 setFlags(AutoDelete
);
216 LinkDelayEvent::LinkDelayEvent(EtherLink::Link
*l
, PacketPtr p
, Tick when
)
217 : Event(&mainEventQueue
), link(l
), packet(p
)
219 setFlags(AutoSerialize
);
220 setFlags(AutoDelete
);
225 LinkDelayEvent::process()
227 link
->txComplete(packet
);
231 LinkDelayEvent::serialize(ostream
&os
)
233 paramOut(os
, "type", string("LinkDelayEvent"));
234 Event::serialize(os
);
236 EtherLink
*parent
= link
->parent
;
237 bool number
= link
->number
;
238 SERIALIZE_OBJPTR(parent
);
239 SERIALIZE_SCALAR(number
);
241 packet
->serialize("packet", os
);
246 LinkDelayEvent::unserialize(Checkpoint
*cp
, const string
§ion
)
248 Event::unserialize(cp
, section
);
252 UNSERIALIZE_OBJPTR(parent
);
253 UNSERIALIZE_SCALAR(number
);
255 link
= parent
->link
[number
];
257 packet
= new PacketData(16384);
258 packet
->unserialize("packet", cp
, section
);
263 LinkDelayEvent::createForUnserialize(Checkpoint
*cp
, const string
§ion
)
265 return new LinkDelayEvent();
268 REGISTER_SERIALIZEABLE("LinkDelayEvent", LinkDelayEvent
)
270 BEGIN_DECLARE_SIM_OBJECT_PARAMS(EtherLink
)
272 SimObjectParam
<EtherInt
*> int1
;
273 SimObjectParam
<EtherInt
*> int2
;
276 SimObjectParam
<EtherDump
*> dump
;
278 END_DECLARE_SIM_OBJECT_PARAMS(EtherLink
)
280 BEGIN_INIT_SIM_OBJECT_PARAMS(EtherLink
)
282 INIT_PARAM(int1
, "interface 1"),
283 INIT_PARAM(int2
, "interface 2"),
284 INIT_PARAM_DFLT(speed
, "link speed in bits per second", 100000000),
285 INIT_PARAM_DFLT(delay
, "transmit delay of packets in us", 0),
286 INIT_PARAM_DFLT(dump
, "object to dump network packets to", NULL
)
288 END_INIT_SIM_OBJECT_PARAMS(EtherLink
)
290 CREATE_SIM_OBJECT(EtherLink
)
292 return new EtherLink(getInstanceName(), int1
, int2
, speed
, delay
, dump
);
295 REGISTER_SIM_OBJECT("EtherLink", EtherLink
)