2 * Copyright (c) 2003 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 std::string
&name
, EtherInt
*i1
, EtherInt
*i2
,
50 Tick speed
, EtherDump
*dump
)
53 double rate
= ((double)ticksPerSecond
* 8.0) / (double)speed
;
55 link1
= new Link(name
+ ".link1", rate
, dump
);
56 link2
= new Link(name
+ ".link2", rate
, dump
);
58 int1
= new Interface(name
+ ".int1", link1
, link2
);
59 int2
= new Interface(name
+ ".int2", link2
, link1
);
67 EtherLink::~EtherLink()
76 EtherLink::Interface::Interface(const std::string
&name
, Link
*tx
, Link
*rx
)
77 : EtherInt(name
), txlink(tx
)
83 EtherLink::Link::Link(const std::string
&name
, double rate
, EtherDump
*d
)
84 : objName(name
), txint(NULL
), rxint(NULL
), ticks_per_byte(rate
),
85 dump(d
), event(&mainEventQueue
, this)
89 EtherLink::serialize(ostream
&os
)
91 nameOut(os
, name() + ".link1");
93 nameOut(os
, name() + ".link2");
98 EtherLink::unserialize(Checkpoint
*cp
, const string
§ion
)
100 link1
->unserialize(cp
, section
+ ".link1");
101 link2
->unserialize(cp
, section
+ ".link2");
105 EtherLink::Link::txDone()
110 DPRINTF(Ethernet
, "EtherLink packet received: len=%d\n", packet
->length
);
111 DDUMP(EthernetData
, packet
->data
, packet
->length
);
113 rxint
->sendPacket(packet
);
122 EtherLink::Link::transmit(PacketPtr
&pkt
)
125 DPRINTF(Ethernet
, "EtherLink packet not sent, link busy\n");
129 DPRINTF(Ethernet
, "EtherLink packet sent: len=%d\n", pkt
->length
);
130 DDUMP(EthernetData
, pkt
->data
, pkt
->length
);
133 int delay
= (int)ceil(((double)pkt
->length
* ticks_per_byte
) + 1.0);
134 DPRINTF(Ethernet
, "EtherLink scheduling packet: delay=%d, (rate=%f)\n",
135 delay
, ticks_per_byte
);
136 event
.schedule(curTick
+ delay
);
142 EtherLink::Link::serialize(ostream
&os
)
144 bool packet_exists
= packet
;
145 SERIALIZE_SCALAR(packet_exists
);
147 bool event_scheduled
= event
.scheduled();
148 SERIALIZE_SCALAR(event_scheduled
);
149 if (event_scheduled
) {
150 Tick event_time
= event
.when();
151 SERIALIZE_SCALAR(event_time
);
155 nameOut(os
, csprintf("%s.packet", name()));
156 packet
->serialize(os
);
161 EtherLink::Link::unserialize(Checkpoint
*cp
, const string
§ion
)
164 UNSERIALIZE_SCALAR(packet_exists
);
166 packet
= new EtherPacket
;
167 packet
->unserialize(cp
, csprintf("%s.packet", section
));
170 bool event_scheduled
;
171 UNSERIALIZE_SCALAR(event_scheduled
);
172 if (event_scheduled
) {
174 UNSERIALIZE_SCALAR(event_time
);
175 event
.schedule(event_time
);
179 BEGIN_DECLARE_SIM_OBJECT_PARAMS(EtherLink
)
181 SimObjectParam
<EtherInt
*> interface1
;
182 SimObjectParam
<EtherInt
*> interface2
;
183 Param
<Tick
> link_speed
;
184 SimObjectParam
<EtherDump
*> packet_dump
;
186 END_DECLARE_SIM_OBJECT_PARAMS(EtherLink
)
188 BEGIN_INIT_SIM_OBJECT_PARAMS(EtherLink
)
190 INIT_PARAM(interface1
, "interface 1"),
191 INIT_PARAM(interface2
, "interface 2"),
192 INIT_PARAM_DFLT(link_speed
, "link speed in bits per second", 100000000),
193 INIT_PARAM_DFLT(packet_dump
, "object to dump network packets to", NULL
)
195 END_INIT_SIM_OBJECT_PARAMS(EtherLink
)
197 CREATE_SIM_OBJECT(EtherLink
)
199 return new EtherLink(getInstanceName(), interface1
, interface2
, link_speed
,
203 REGISTER_SIM_OBJECT("EtherLink", EtherLink
)