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 an ethernet hub
38 #include "base/trace.hh"
39 #include "dev/etherbus.hh"
40 #include "dev/etherdump.hh"
41 #include "dev/etherint.hh"
42 #include "dev/etherpkt.hh"
43 #include "sim/builder.hh"
44 #include "sim/universe.hh"
48 EtherBus::EtherBus(const string
&name
, double rate
, bool loop
,
49 EtherDump
*packet_dump
)
50 : SimObject(name
), ticks_per_byte(rate
), loopback(loop
),
51 event(&mainEventQueue
, this),
52 sender(0), dump(packet_dump
)
58 devlist_t::iterator i
= devlist
.begin();
59 devlist_t::iterator end
= devlist
.end();
61 DPRINTF(Ethernet
, "ethernet packet received: length=%d\n", packet
->length
);
62 DDUMP(EthernetData
, packet
->data
, packet
->length
);
65 if (loopback
|| *i
!= sender
)
66 (*i
)->sendPacket(packet
);
80 EtherBus::reg(EtherInt
*dev
)
81 { devlist
.push_back(dev
); }
84 EtherBus::send(EtherInt
*sndr
, PacketPtr
&pkt
)
87 DPRINTF(Ethernet
, "ethernet packet not sent, bus busy\n", curTick
);
91 DPRINTF(Ethernet
, "ethernet packet sent: length=%d\n", pkt
->length
);
92 DDUMP(EthernetData
, pkt
->data
, pkt
->length
);
96 int delay
= (int)ceil(((double)pkt
->length
* ticks_per_byte
) + 1.0);
97 DPRINTF(Ethernet
, "scheduling packet: delay=%d, (rate=%f)\n",
98 delay
, ticks_per_byte
);
99 event
.schedule(curTick
+ delay
);
104 BEGIN_DECLARE_SIM_OBJECT_PARAMS(EtherBus
)
106 Param
<bool> loopback
;
108 SimObjectParam
<EtherDump
*> packet_dump
;
110 END_DECLARE_SIM_OBJECT_PARAMS(EtherBus
)
112 BEGIN_INIT_SIM_OBJECT_PARAMS(EtherBus
)
114 INIT_PARAM_DFLT(loopback
,
115 "send the packet back to the interface from which it came",
117 INIT_PARAM_DFLT(speed
, "bus speed in bits per second", 100000000),
118 INIT_PARAM_DFLT(packet_dump
, "object to dump network packets to", NULL
)
120 END_INIT_SIM_OBJECT_PARAMS(EtherBus
)
122 CREATE_SIM_OBJECT(EtherBus
)
124 double rate
= ((double)ticksPerSecond
* 8.0) / (double)speed
;
125 return new EtherBus(getInstanceName(), rate
, loopback
, packet_dump
);
128 REGISTER_SIM_OBJECT("EtherBus", EtherBus
)