2 * Copyright (c) 2004-2005 The Regents of The University of Michigan
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.
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.
28 * Authors: Nathan Binkert
31 #ifndef __DEV_PKTFIFO_HH__
32 #define __DEV_PKTFIFO_HH__
38 #include "dev/etherpkt.hh"
39 #include "sim/serialize.hh"
43 struct PacketFifoEntry
55 PacketFifoEntry(const PacketFifoEntry &s)
56 : packet(s.packet), number(s.number), slack(s.slack), priv(s.priv)
60 PacketFifoEntry(EthPacketPtr p, uint64_t n)
61 : packet(p), number(n), slack(0), priv(-1)
73 void serialize(const std::string &base, std::ostream &os);
74 void unserialize(const std::string &base, Checkpoint *cp,
75 const std::string §ion);
82 typedef std::list<PacketFifoEntry> fifo_list;
83 typedef fifo_list::iterator iterator;
86 std::list<PacketFifoEntry> fifo;
93 explicit PacketFifo(int max)
94 : _counter(0), _maxsize(max), _size(0), _reserved(0) {}
95 virtual ~PacketFifo() {}
97 int packets() const { return fifo.size(); }
98 int maxsize() const { return _maxsize; }
99 int size() const { return _size; }
100 int reserved() const { return _reserved; }
101 int avail() const { return _maxsize - _size - _reserved; }
102 bool empty() const { return size() <= 0; }
103 bool full() const { return avail() <= 0; }
105 int reserve(int len = 0)
108 assert(avail() >= 0);
112 iterator begin() { return fifo.begin(); }
113 iterator end() { return fifo.end(); }
115 EthPacketPtr front() { return fifo.begin()->packet; }
117 bool push(EthPacketPtr ptr)
120 assert(_reserved <= ptr->length);
121 if (avail() < ptr->length - _reserved)
124 _size += ptr->length;
126 PacketFifoEntry entry;
128 entry.number = _counter++;
129 fifo.push_back(entry);
139 iterator entry = fifo.begin();
140 _size -= entry->packet->length;
141 _size -= entry->slack;
142 entry->packet = NULL;
148 for (iterator i = begin(); i != end(); ++i)
155 void remove(iterator i)
157 if (i != fifo.begin()) {
160 assert(prev != fifo.end());
161 prev->slack += i->packet->length;
162 prev->slack += i->slack;
164 _size -= i->packet->length;
172 bool copyout(void *dest, int offset, int len);
174 int countPacketsBefore(iterator i)
178 return i->number - fifo.begin()->number;
181 int countPacketsAfter(iterator i)
183 iterator end = fifo.end();
186 return (--end)->number - i->number;
192 for (iterator i = begin(); i != end(); ++i)
193 total += i->packet->length + i->slack;
196 panic("total (%d) is not == to size (%d)\n", total, _size);
200 * Serialization stuff
203 void serialize(const std::string &base, std::ostream &os);
204 void unserialize(const std::string &base,
205 Checkpoint *cp, const std::string §ion);
208 #endif // __DEV_PKTFIFO_HH__