12SN/A/*
21762SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan
32SN/A * All rights reserved.
42SN/A *
52SN/A * Redistribution and use in source and binary forms, with or without
62SN/A * modification, are permitted provided that the following conditions are
72SN/A * met: redistributions of source code must retain the above copyright
82SN/A * notice, this list of conditions and the following disclaimer;
92SN/A * redistributions in binary form must reproduce the above copyright
102SN/A * notice, this list of conditions and the following disclaimer in the
112SN/A * documentation and/or other materials provided with the distribution;
122SN/A * neither the name of the copyright holders nor the names of its
132SN/A * contributors may be used to endorse or promote products derived from
142SN/A * this software without specific prior written permission.
152SN/A *
162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665SN/A *
282665SN/A * Authors: Nathan Binkert
292SN/A */
302SN/A
312SN/A/* @file
322SN/A * Device module for modelling an ethernet hub
332SN/A */
3411263Sandreas.sandberg@arm.com#include "dev/net/etherbus.hh"
352SN/A
36146SN/A#include <cmath>
372SN/A#include <deque>
382SN/A#include <string>
392SN/A#include <vector>
402SN/A
4112334Sgabeblack@google.com#include "base/logging.hh"
42146SN/A#include "base/trace.hh"
438232SN/A#include "debug/Ethernet.hh"
448232SN/A#include "debug/EthernetData.hh"
4511263Sandreas.sandberg@arm.com#include "dev/net/etherdump.hh"
4611263Sandreas.sandberg@arm.com#include "dev/net/etherint.hh"
4711263Sandreas.sandberg@arm.com#include "dev/net/etherpkt.hh"
484762SN/A#include "params/EtherBus.hh"
494167SN/A#include "sim/core.hh"
502SN/A
512SN/Ausing namespace std;
522SN/A
534981SN/AEtherBus::EtherBus(const Params *p)
5413766Sgabeblack@google.com    : SimObject(p), ticksPerByte(p->speed), loopback(p->loopback),
5512130Sspwilson2@wisc.edu      event([this]{ txDone(); }, "ethernet bus completion"),
5612130Sspwilson2@wisc.edu      sender(0), dump(p->dump)
571634SN/A{
581634SN/A}
592SN/A
602SN/Avoid
612SN/AEtherBus::txDone()
622SN/A{
632SN/A    devlist_t::iterator i = devlist.begin();
642SN/A    devlist_t::iterator end = devlist.end();
652SN/A
662SN/A    DPRINTF(Ethernet, "ethernet packet received: length=%d\n", packet->length);
672SN/A    DDUMP(EthernetData, packet->data, packet->length);
682SN/A
692SN/A    while (i != end) {
702SN/A        if (loopback || *i != sender)
712SN/A            (*i)->sendPacket(packet);
722SN/A        ++i;
732SN/A    }
742SN/A
752SN/A    sender->sendDone();
762SN/A
772SN/A    if (dump)
782SN/A        dump->dump(packet);
792SN/A
802SN/A    sender = 0;
812SN/A    packet = 0;
822SN/A}
832SN/A
8413784Sgabeblack@google.comPort &
8513784Sgabeblack@google.comEtherBus::getPort(const std::string &if_name, PortID idx)
864981SN/A{
874981SN/A    panic("Etherbus doesn't work\n");
884981SN/A}
892SN/A
902SN/Abool
912566SN/AEtherBus::send(EtherInt *sndr, EthPacketPtr &pkt)
922SN/A{
932SN/A    if (busy()) {
947823SN/A        DPRINTF(Ethernet, "ethernet packet not sent, bus busy\n", curTick());
952SN/A        return false;
962SN/A    }
972SN/A
982SN/A    DPRINTF(Ethernet, "ethernet packet sent: length=%d\n", pkt->length);
992SN/A    DDUMP(EthernetData, pkt->data, pkt->length);
1002SN/A
1012SN/A    packet = pkt;
1022SN/A    sender = sndr;
10311701Smichael.lebeane@amd.com    int delay = (int)ceil(((double)pkt->simLength * ticksPerByte) + 1.0);
1042SN/A    DPRINTF(Ethernet, "scheduling packet: delay=%d, (rate=%f)\n",
1051634SN/A            delay, ticksPerByte);
1067823SN/A    schedule(event, curTick() + delay);
1072SN/A
1082SN/A    return true;
1092SN/A}
1102SN/A
1114762SN/AEtherBus *
1124762SN/AEtherBusParams::create()
1132SN/A{
1144981SN/A    return new EtherBus(this);
1152SN/A}
116