Merge zizzer.eecs.umich.edu:/z/m5/Bitkeeper/m5
[gem5.git] / dev / etherbus.cc
1 /*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
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.
15 *
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.
27 */
28
29 /* @file
30 * Device module for modelling an ethernet hub
31 */
32
33 #include <cmath>
34 #include <deque>
35 #include <string>
36 #include <vector>
37
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/root.hh"
45
46 using namespace std;
47
48 EtherBus::EtherBus(const string &name, double speed, bool loop,
49 EtherDump *packet_dump)
50 : SimObject(name), ticksPerByte(speed), loopback(loop),
51 event(&mainEventQueue, this), sender(0), dump(packet_dump)
52 {
53 }
54
55 void
56 EtherBus::txDone()
57 {
58 devlist_t::iterator i = devlist.begin();
59 devlist_t::iterator end = devlist.end();
60
61 DPRINTF(Ethernet, "ethernet packet received: length=%d\n", packet->length);
62 DDUMP(EthernetData, packet->data, packet->length);
63
64 while (i != end) {
65 if (loopback || *i != sender)
66 (*i)->sendPacket(packet);
67 ++i;
68 }
69
70 sender->sendDone();
71
72 if (dump)
73 dump->dump(packet);
74
75 sender = 0;
76 packet = 0;
77 }
78
79 void
80 EtherBus::reg(EtherInt *dev)
81 { devlist.push_back(dev); }
82
83 bool
84 EtherBus::send(EtherInt *sndr, PacketPtr &pkt)
85 {
86 if (busy()) {
87 DPRINTF(Ethernet, "ethernet packet not sent, bus busy\n", curTick);
88 return false;
89 }
90
91 DPRINTF(Ethernet, "ethernet packet sent: length=%d\n", pkt->length);
92 DDUMP(EthernetData, pkt->data, pkt->length);
93
94 packet = pkt;
95 sender = sndr;
96 int delay = (int)ceil(((double)pkt->length * ticksPerByte) + 1.0);
97 DPRINTF(Ethernet, "scheduling packet: delay=%d, (rate=%f)\n",
98 delay, ticksPerByte);
99 event.schedule(curTick + delay);
100
101 return true;
102 }
103
104 BEGIN_DECLARE_SIM_OBJECT_PARAMS(EtherBus)
105
106 Param<bool> loopback;
107 Param<double> speed;
108 SimObjectParam<EtherDump *> packet_dump;
109
110 END_DECLARE_SIM_OBJECT_PARAMS(EtherBus)
111
112 BEGIN_INIT_SIM_OBJECT_PARAMS(EtherBus)
113
114 INIT_PARAM(loopback, "send the packet back to the sending interface"),
115 INIT_PARAM(speed, "bus speed in ticks per byte"),
116 INIT_PARAM(packet_dump, "object to dump network packets to")
117
118 END_INIT_SIM_OBJECT_PARAMS(EtherBus)
119
120 CREATE_SIM_OBJECT(EtherBus)
121 {
122 return new EtherBus(getInstanceName(), speed, loopback, packet_dump);
123 }
124
125 REGISTER_SIM_OBJECT("EtherBus", EtherBus)