etherbus.cc revision 11701
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
41146SN/A#include "base/trace.hh"
428232SN/A#include "debug/Ethernet.hh"
438232SN/A#include "debug/EthernetData.hh"
4411263Sandreas.sandberg@arm.com#include "dev/net/etherdump.hh"
4511263Sandreas.sandberg@arm.com#include "dev/net/etherint.hh"
4611263Sandreas.sandberg@arm.com#include "dev/net/etherpkt.hh"
474762SN/A#include "params/EtherBus.hh"
484167SN/A#include "sim/core.hh"
492SN/A
502SN/Ausing namespace std;
512SN/A
524981SN/AEtherBus::EtherBus(const Params *p)
534981SN/A    : EtherObject(p), ticksPerByte(p->speed), loopback(p->loopback),
545606SN/A      event(this), sender(0), dump(p->dump)
551634SN/A{
561634SN/A}
572SN/A
582SN/Avoid
592SN/AEtherBus::txDone()
602SN/A{
612SN/A    devlist_t::iterator i = devlist.begin();
622SN/A    devlist_t::iterator end = devlist.end();
632SN/A
642SN/A    DPRINTF(Ethernet, "ethernet packet received: length=%d\n", packet->length);
652SN/A    DDUMP(EthernetData, packet->data, packet->length);
662SN/A
672SN/A    while (i != end) {
682SN/A        if (loopback || *i != sender)
692SN/A            (*i)->sendPacket(packet);
702SN/A        ++i;
712SN/A    }
722SN/A
732SN/A    sender->sendDone();
742SN/A
752SN/A    if (dump)
762SN/A        dump->dump(packet);
772SN/A
782SN/A    sender = 0;
792SN/A    packet = 0;
802SN/A}
812SN/A
824981SN/AEtherInt*
834981SN/AEtherBus::getEthPort(const std::string &if_name, int idx)
844981SN/A{
854981SN/A    panic("Etherbus doesn't work\n");
864981SN/A}
872SN/A
882SN/Abool
892566SN/AEtherBus::send(EtherInt *sndr, EthPacketPtr &pkt)
902SN/A{
912SN/A    if (busy()) {
927823SN/A        DPRINTF(Ethernet, "ethernet packet not sent, bus busy\n", curTick());
932SN/A        return false;
942SN/A    }
952SN/A
962SN/A    DPRINTF(Ethernet, "ethernet packet sent: length=%d\n", pkt->length);
972SN/A    DDUMP(EthernetData, pkt->data, pkt->length);
982SN/A
992SN/A    packet = pkt;
1002SN/A    sender = sndr;
10111701Smichael.lebeane@amd.com    int delay = (int)ceil(((double)pkt->simLength * ticksPerByte) + 1.0);
1022SN/A    DPRINTF(Ethernet, "scheduling packet: delay=%d, (rate=%f)\n",
1031634SN/A            delay, ticksPerByte);
1047823SN/A    schedule(event, curTick() + delay);
1052SN/A
1062SN/A    return true;
1072SN/A}
1082SN/A
1094762SN/AEtherBus *
1104762SN/AEtherBusParams::create()
1112SN/A{
1124981SN/A    return new EtherBus(this);
1132SN/A}
114