pktfifo.hh revision 2665
1/* 2 * Copyright (c) 2004-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 * Authors: Nathan Binkert 29 */ 30 31#ifndef __DEV_PKTFIFO_HH__ 32#define __DEV_PKTFIFO_HH__ 33 34#include <iosfwd> 35#include <list> 36#include <string> 37 38#include "dev/etherpkt.hh" 39#include "sim/serialize.hh" 40 41class Checkpoint; 42class PacketFifo 43{ 44 public: 45 typedef std::list<EthPacketPtr> fifo_list; 46 typedef fifo_list::iterator iterator; 47 48 protected: 49 std::list<EthPacketPtr> fifo; 50 int _maxsize; 51 int _size; 52 int _reserved; 53 54 public: 55 explicit PacketFifo(int max) : _maxsize(max), _size(0), _reserved(0) {} 56 virtual ~PacketFifo() {} 57 58 int packets() const { return fifo.size(); } 59 int maxsize() const { return _maxsize; } 60 int size() const { return _size; } 61 int reserved() const { return _reserved; } 62 int avail() const { return _maxsize - _size - _reserved; } 63 bool empty() const { return size() <= 0; } 64 bool full() const { return avail() <= 0; } 65 66 int reserve(int len = 0) 67 { 68 _reserved += len; 69 assert(avail() >= 0); 70 return _reserved; 71 } 72 73 iterator begin() { return fifo.begin(); } 74 iterator end() { return fifo.end(); } 75 76 EthPacketPtr front() { return fifo.front(); } 77 78 bool push(EthPacketPtr ptr) 79 { 80 assert(ptr->length); 81 assert(_reserved <= ptr->length); 82 assert(ptr->slack == 0); 83 if (avail() < ptr->length - _reserved) 84 return false; 85 86 _size += ptr->length; 87 fifo.push_back(ptr); 88 _reserved = 0; 89 return true; 90 } 91 92 void pop() 93 { 94 if (empty()) 95 return; 96 97 EthPacketPtr &packet = fifo.front(); 98 _size -= packet->length; 99 _size -= packet->slack; 100 packet->slack = 0; 101 packet = NULL; 102 fifo.pop_front(); 103 } 104 105 void clear() 106 { 107 for (iterator i = begin(); i != end(); ++i) 108 (*i)->slack = 0; 109 fifo.clear(); 110 _size = 0; 111 _reserved = 0; 112 } 113 114 void remove(iterator i) 115 { 116 EthPacketPtr &packet = *i; 117 if (i != fifo.begin()) { 118 iterator prev = i; 119 --prev; 120 assert(prev != fifo.end()); 121 (*prev)->slack += packet->length; 122 } else { 123 _size -= packet->length; 124 _size -= packet->slack; 125 } 126 127 packet->slack = 0; 128 packet = NULL; 129 fifo.erase(i); 130 } 131 132 bool copyout(void *dest, int offset, int len); 133 134 int countPacketsBefore(iterator end) 135 { 136 iterator i = fifo.begin(); 137 int count = 0; 138 139 while (i != end) { 140 ++count; 141 ++i; 142 } 143 144 return count; 145 } 146 147 int countPacketsAfter(iterator i) 148 { 149 iterator end = fifo.end(); 150 int count = 0; 151 152 while (i != end) { 153 ++count; 154 ++i; 155 } 156 157 return count; 158 } 159 160 161/** 162 * Serialization stuff 163 */ 164 public: 165 void serialize(const std::string &base, std::ostream &os); 166 void unserialize(const std::string &base, 167 Checkpoint *cp, const std::string §ion); 168}; 169 170#endif // __DEV_PKTFIFO_HH__ 171