dev: Move network devices to src/dev/net/
[gem5.git] / src / dev / net / pktfifo.cc
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 #include "dev/net/pktfifo.hh"
32
33 #include "base/misc.hh"
34
35 using namespace std;
36
37 bool
38 PacketFifo::copyout(void *dest, unsigned offset, unsigned len)
39 {
40 char *data = (char *)dest;
41 if (offset + len >= size())
42 return false;
43
44 iterator i = fifo.begin();
45 iterator end = fifo.end();
46 while (len > 0) {
47 EthPacketPtr &pkt = i->packet;
48 while (offset >= pkt->length) {
49 offset -= pkt->length;
50 ++i;
51 }
52
53 if (i == end)
54 panic("invalid fifo");
55
56 unsigned size = min(pkt->length - offset, len);
57 memcpy(data, pkt->data, size);
58 offset = 0;
59 len -= size;
60 data += size;
61 ++i;
62 }
63
64 return true;
65 }
66
67
68 void
69 PacketFifoEntry::serialize(const string &base, CheckpointOut &cp) const
70 {
71 packet->serialize(base + ".packet", cp);
72 paramOut(cp, base + ".slack", slack);
73 paramOut(cp, base + ".number", number);
74 paramOut(cp, base + ".priv", priv);
75 }
76
77 void
78 PacketFifoEntry::unserialize(const string &base, CheckpointIn &cp)
79 {
80 packet = make_shared<EthPacketData>(16384);
81 packet->unserialize(base + ".packet", cp);
82 paramIn(cp, base + ".slack", slack);
83 paramIn(cp, base + ".number", number);
84 paramIn(cp, base + ".priv", priv);
85 }
86
87 void
88 PacketFifo::serialize(const string &base, CheckpointOut &cp) const
89 {
90 paramOut(cp, base + ".size", _size);
91 paramOut(cp, base + ".maxsize", _maxsize);
92 paramOut(cp, base + ".reserved", _reserved);
93 paramOut(cp, base + ".packets", fifo.size());
94
95 int i = 0;
96 for (const auto &entry : fifo)
97 entry.serialize(csprintf("%s.entry%d", base, i++), cp);
98 }
99
100 void
101 PacketFifo::unserialize(const string &base, CheckpointIn &cp)
102 {
103 paramIn(cp, base + ".size", _size);
104 // paramIn(cp, base + ".maxsize", _maxsize);
105 paramIn(cp, base + ".reserved", _reserved);
106 int fifosize;
107 paramIn(cp, base + ".packets", fifosize);
108
109 fifo.clear();
110
111 for (int i = 0; i < fifosize; ++i) {
112 PacketFifoEntry entry;
113 entry.unserialize(csprintf("%s.entry%d", base, i), cp);
114 fifo.push_back(entry);
115 }
116 }