FreeNOS
NetworkQueue.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Niek Linnenbank
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <FreeNOS/User.h>
19 #include <Log.h>
20 #include <String.h>
21 #include "NetworkQueue.h"
22 
24  const Size queueSize)
25 {
26  assert(queueSize <= MaxPackets);
27 
32 
33  // Ensure size is page aligned
35  {
37  }
38 
39  // Allocate memory pages for the payload
41  if (result != API::Success)
42  {
43  ERROR("failed to allocate payload buffer: result = " << (int) result);
44  }
45  else
46  {
47  // Allocate packet objects
48  for (Size i = 0; i < queueSize; i++)
49  {
50  Packet *packet = new Packet;
51  packet->size = 0;
52  packet->data = (u8 *) (m_payloadRange.virt + (i * PayloadBufferSize));
53  m_free.insertAt(i, packet);
54  }
55  }
56 }
57 
59 {
60  for (Size i = 0; i < m_free.size(); i++)
61  {
62  Packet *p = m_free.get(i);
63  if (p)
64  {
65  delete p;
66  }
67  }
68 
70 }
71 
73 {
74  for (Size i = 0; i < m_free.size(); i++)
75  {
76  Packet *p = m_free.get(i);
77  if (p)
78  {
79  p->size = 0;
80  m_free.remove(i);
81  return p;
82  }
83  }
84  return ZERO;
85 }
86 
88 {
89  packet->size = 0;
90  m_free.insert(packet);
91 }
92 
94 {
95  m_data.insert(packet);
96 }
97 
99 {
100  for (Size i = 0; i < m_data.size(); i++)
101  {
102  Packet *p = m_data.get(i);
103  if (p)
104  {
105  m_data.remove(i);
106  return p;
107  }
108  }
109  return ZERO;
110 }
111 
113 {
114  return m_data.count() > 0;
115 }
116 
118 {
119  String s;
120 
121  for (Size i = 0; i < pkt.size; i++)
122  {
123  if ((i % 16) == 0)
124  s << "\r\n";
125 
126  const uint val = pkt.data[i];
127  s << Number::Hex << val;
128 
129  if (val >= 0x10)
130  s << " ";
131  else
132  s << " ";
133  }
134  s << "\r\n";
135 
136  log.append(*s);
137  return log;
138 }
NetworkQueue::NetworkQueue
NetworkQueue(const Size packetSize, const Size queueSize=MaxPackets)
Constructor.
Definition: NetworkQueue.cpp:23
operator<<
Log & operator<<(Log &log, const NetworkQueue::Packet &pkt)
Definition: NetworkQueue.cpp:117
API::Result
Result
Enumeration of generic kernel API result codes.
Definition: API.h:68
NetworkQueue::pop
Packet * pop()
Retrieve packet with data.
Definition: NetworkQueue.cpp:98
Memory::Writable
@ Writable
Definition: Memory.h:42
NetworkQueue::Packet
struct NetworkQueue::Packet Packet
Represents a network packet.
String
Abstraction of strings.
Definition: String.h:41
Memory::User
@ User
Definition: Memory.h:44
PAGESIZE
#define PAGESIZE
ARM uses 4K pages.
Definition: ARMConstant.h:97
NetworkQueue::MaxPackets
static const Size MaxPackets
Maximum number of packets available.
Definition: NetworkQueue.h:45
NetworkQueue::m_free
Index< Packet, MaxPackets > m_free
Contains unused packets.
Definition: NetworkQueue.h:103
NetworkQueue::m_data
Index< Packet, MaxPackets > m_data
Contains packets with data.
Definition: NetworkQueue.h:106
NetworkQueue.h
NetworkQueue::Packet::data
u8 * data
Definition: NetworkQueue.h:53
NetworkQueue::release
void release(Packet *packet)
Put unused packet back.
Definition: NetworkQueue.cpp:87
NetworkQueue::Packet::size
Size size
Definition: NetworkQueue.h:52
VMCtl
API::Result VMCtl(const ProcessID procID, const MemoryOperation op, Memory::Range *range=ZERO)
Prototype for user applications.
Definition: VMCtl.h:61
NetworkQueue::hasData
bool hasData() const
Check if data packets are available.
Definition: NetworkQueue.cpp:112
Memory::Readable
@ Readable
Definition: Memory.h:41
NetworkQueue::get
Packet * get()
Get unused packet.
Definition: NetworkQueue.cpp:72
Log.h
uint
unsigned int uint
Unsigned integer number.
Definition: Types.h:44
Log
Logging class.
Definition: Log.h:96
SELF
#define SELF
Definition: ProcessID.h:35
Log::append
void append(const char *str)
Append to buffered output.
Definition: Log.cpp:53
Release
@ Release
Definition: VMCtl.h:40
NetworkQueue::push
void push(Packet *packet)
Enqueue packet with data.
Definition: NetworkQueue.cpp:93
Memory::Range::phys
Address phys
Physical address.
Definition: Memory.h:58
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
NetworkQueue::~NetworkQueue
virtual ~NetworkQueue()
Destructor.
Definition: NetworkQueue.cpp:58
assert
#define assert(exp)
Insert program diagnostics.
Definition: assert.h:60
ERROR
#define ERROR(msg)
Output an error message.
Definition: Log.h:61
u8
unsigned char u8
Unsigned 8-bit number.
Definition: Types.h:59
String.h
API::Success
@ Success
Definition: API.h:70
NetworkQueue::PayloadBufferSize
static const Size PayloadBufferSize
Size of payload memory buffer.
Definition: NetworkQueue.h:42
Memory::Range::virt
Address virt
Virtual address.
Definition: Memory.h:57
NetworkQueue::Packet
Represents a network packet.
Definition: NetworkQueue.h:50
Memory::Range::size
Size size
Size in number of bytes.
Definition: Memory.h:59
Memory::Range::access
Access access
Page access flags.
Definition: Memory.h:60
ZERO
#define ZERO
Zero value.
Definition: Macros.h:43
MapContiguous
@ MapContiguous
Definition: VMCtl.h:37
Number::Hex
@ Hex
Decimal: 0-10.
Definition: Types.h:171
NetworkQueue::m_payloadRange
Memory::Range m_payloadRange
Defines the memory range of mapped payload data.
Definition: NetworkQueue.h:109