etherbus.cc revision 8232
11689SN/A/*
213590Srekai.gonzalezalberquilla@arm.com * Copyright (c) 2002-2005 The Regents of The University of Michigan
39920Syasuko.eckert@amd.com * All rights reserved.
47944SGiacomo.Gabrielli@arm.com *
57944SGiacomo.Gabrielli@arm.com * Redistribution and use in source and binary forms, with or without
67944SGiacomo.Gabrielli@arm.com * modification, are permitted provided that the following conditions are
77944SGiacomo.Gabrielli@arm.com * met: redistributions of source code must retain the above copyright
87944SGiacomo.Gabrielli@arm.com * notice, this list of conditions and the following disclaimer;
97944SGiacomo.Gabrielli@arm.com * redistributions in binary form must reproduce the above copyright
107944SGiacomo.Gabrielli@arm.com * notice, this list of conditions and the following disclaimer in the
117944SGiacomo.Gabrielli@arm.com * documentation and/or other materials provided with the distribution;
127944SGiacomo.Gabrielli@arm.com * neither the name of the copyright holders nor the names of its
137944SGiacomo.Gabrielli@arm.com * contributors may be used to endorse or promote products derived from
147944SGiacomo.Gabrielli@arm.com * this software without specific prior written permission.
152326SN/A *
161689SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
171689SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
181689SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
191689SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
201689SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
211689SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
221689SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
231689SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
241689SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
251689SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
261689SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
271689SN/A *
281689SN/A * Authors: Nathan Binkert
291689SN/A */
301689SN/A
311689SN/A/* @file
321689SN/A * Device module for modelling an ethernet hub
331689SN/A */
341689SN/A
351689SN/A#include <cmath>
361689SN/A#include <deque>
371689SN/A#include <string>
381689SN/A#include <vector>
391689SN/A
402665Ssaidi@eecs.umich.edu#include "base/trace.hh"
412665Ssaidi@eecs.umich.edu#include "debug/Ethernet.hh"
422831Sksewell@umich.edu#include "debug/EthernetData.hh"
431689SN/A#include "dev/etherbus.hh"
441689SN/A#include "dev/etherdump.hh"
459944Smatt.horsnell@ARM.com#include "dev/etherint.hh"
469944Smatt.horsnell@ARM.com#include "dev/etherpkt.hh"
479944Smatt.horsnell@ARM.com#include "params/EtherBus.hh"
482064SN/A#include "sim/core.hh"
491060SN/A
501060SN/Ausing namespace std;
5113449Sgabeblack@google.com
522292SN/AEtherBus::EtherBus(const Params *p)
531717SN/A    : EtherObject(p), ticksPerByte(p->speed), loopback(p->loopback),
548232Snate@binkert.org      event(this), sender(0), dump(p->dump)
554762Snate@binkert.org{
566221Snate@binkert.org}
574762Snate@binkert.org
581060SN/Avoid
598737Skoansin.tan@gmail.comEtherBus::txDone()
608737Skoansin.tan@gmail.com{
618737Skoansin.tan@gmail.com    devlist_t::iterator i = devlist.begin();
625529Snate@binkert.org    devlist_t::iterator end = devlist.end();
631061SN/A
6413429Srekai.gonzalezalberquilla@arm.com    DPRINTF(Ethernet, "ethernet packet received: length=%d\n", packet->length);
655606Snate@binkert.org    DDUMP(EthernetData, packet->data, packet->length);
668581Ssteve.reinhardt@amd.com
678581Ssteve.reinhardt@amd.com    while (i != end) {
681060SN/A        if (loopback || *i != sender)
692292SN/A            (*i)->sendPacket(packet);
702292SN/A        ++i;
712292SN/A    }
722292SN/A
732292SN/A    sender->sendDone();
742292SN/A
752326SN/A    if (dump)
762292SN/A        dump->dump(packet);
772292SN/A
782292SN/A    sender = 0;
792292SN/A    packet = 0;
802292SN/A}
812292SN/A
825336Shines@cs.fsu.eduEtherInt*
832292SN/AEtherBus::getEthPort(const std::string &if_name, int idx)
844873Sstever@eecs.umich.edu{
852292SN/A    panic("Etherbus doesn't work\n");
862292SN/A}
872292SN/A
884329Sktlim@umich.edubool
895529Snate@binkert.orgEtherBus::send(EtherInt *sndr, EthPacketPtr &pkt)
904329Sktlim@umich.edu{
914329Sktlim@umich.edu    if (busy()) {
924329Sktlim@umich.edu        DPRINTF(Ethernet, "ethernet packet not sent, bus busy\n", curTick());
9313561Snikos.nikoleris@arm.com        return false;
942292SN/A    }
952292SN/A
962292SN/A    DPRINTF(Ethernet, "ethernet packet sent: length=%d\n", pkt->length);
972292SN/A    DDUMP(EthernetData, pkt->data, pkt->length);
982292SN/A
992292SN/A    packet = pkt;
1005529Snate@binkert.org    sender = sndr;
1011060SN/A    int delay = (int)ceil(((double)pkt->length * ticksPerByte) + 1.0);
1029920Syasuko.eckert@amd.com    DPRINTF(Ethernet, "scheduling packet: delay=%d, (rate=%f)\n",
10312109SRekai.GonzalezAlberquilla@arm.com            delay, ticksPerByte);
1049920Syasuko.eckert@amd.com    schedule(event, curTick() + delay);
10512109SRekai.GonzalezAlberquilla@arm.com
10612109SRekai.GonzalezAlberquilla@arm.com    return true;
10713610Sgiacomo.gabrielli@arm.com}
10812109SRekai.GonzalezAlberquilla@arm.com
1091060SN/AEtherBus *
1101060SN/AEtherBusParams::create()
1111060SN/A{
1122326SN/A    return new EtherBus(this);
1131060SN/A}
1141060SN/A