The Pedigree Project 0.1
NetworkStack.h
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#ifndef MACHINE_NETWORK_STACK_H
21#define MACHINE_NETWORK_STACK_H
22#include "pedigree/kernel/Atomic.h"
23#include "pedigree/kernel/compiler.h"
24#include "pedigree/kernel/machine/Network.h"
25#include "pedigree/kernel/process/OperationBarrier.h"
26#include "pedigree/kernel/processor/types.h"
27#include "pedigree/kernel/utilities/MemoryPool.h"
28#include "pedigree/kernel/utilities/RequestQueue.h"
29#include "pedigree/kernel/utilities/String.h"
30#include "pedigree/kernel/utilities/Tree.h"
31#include "pedigree/kernel/utilities/Vector.h"
32
33#include <config.h>
34
35// lwIP network interface type
36struct netif;
37
43class EXPORTED_PUBLIC NetworkStack : public RequestQueue {
44 public:
52 class EXPORTED_PUBLIC DeviceLease {
53 public:
55 DeviceLease(DeviceLease&& other);
57
58 DeviceLease& operator=(DeviceLease&& other);
59
60 Network* device() const {
61 return m_Device;
62 }
63
64 struct netif* interface() const {
65 return m_Interface;
66 }
67
68 explicit operator bool() const {
69 return m_Device != nullptr;
70 }
71
72 private:
73 friend class NetworkStack;
74
75 DeviceLease(Network* device, struct netif* interface, OperationBarrier::Lease&& lease);
76
77 DeviceLease(const DeviceLease&) = delete;
78 DeviceLease& operator=(const DeviceLease&) = delete;
79
80 Network* m_Device;
81 struct netif* m_Interface;
83 };
84
86 virtual ~NetworkStack();
87
90 return *__atomic_load_n(&stack, __ATOMIC_ACQUIRE);
91 }
92
93 static NetworkStack* instanceIfAvailable() {
94 return __atomic_load_n(&stack, __ATOMIC_ACQUIRE);
95 }
96
98 void receive(size_t nBytes, uintptr_t packet, Network* pCard, uint32_t offset);
99
101 void registerDevice(Network* pDevice);
102
104 MUST_USE_RESULT bool acquireDevice(size_t n, DeviceLease& lease);
105
107 MUST_USE_RESULT bool acquireDevice(Network* pDevice, DeviceLease& lease);
108
110 void deRegisterDevice(Network* pDevice);
111
114 return m_MemPool;
115 }
116
117#if HOSTED && PEDIGREE_HOSTED_SMOKE_TESTS
118 enum class HostedReceiveEvent {
119 Queued,
120 BeforeDispatch,
121 Delivered,
122 DiscardedStale,
123 Cancelled,
124 };
125
126 using HostedReceiveHook = void (*)(HostedReceiveEvent event, uintptr_t buffer, Network* card,
127 size_t generation);
128
129 static void setHostedReceiveHook(HostedReceiveHook hook);
130 static size_t getHostedRegistrationGeneration(Network* card);
131 static size_t getHostedReceiveRequestCapacity();
132#endif
133
134 private:
135 struct DeviceRegistration;
136
137 static NetworkStack* stack;
138
139#if HOSTED && PEDIGREE_HOSTED_SMOKE_TESTS
140 static HostedReceiveHook m_HostedReceiveHook;
141#endif
142
143 virtual uint64_t executeRequest(uint64_t p1, uint64_t p2, uint64_t p3, uint64_t p4, uint64_t p5,
144 uint64_t p6, uint64_t p7, uint64_t p8);
145 virtual void cancelRequest(const Request& request);
146
148 static void cancelReceive(uintptr_t buffer, Network* card, size_t generation);
149
150 static constexpr size_t ReceiveRequestCapacity = 256;
151
154
157
158#if THREADS || UTILITY_LINUX
159 Mutex m_Lock;
160#endif
161
164
167
170
171 PreallocatedRequest m_ReceiveRequests[ReceiveRequestCapacity];
172
175
178};
179
180#endif
Definition Mutex.h:56
MemoryPool & getMemPool()
size_t m_NextDeviceGeneration
size_t m_NextInterfaceNumber
Atomic< size_t > m_NextReceiveRequest
MemoryPool m_MemPool
Tree< Network *, DeviceRegistration * > m_Registrations
static NetworkStack & instance()
Vector< Network * > m_Children
Tree< Network *, struct netif * > m_Interfaces
virtual void cancelRequest(const Request &request)
virtual uint64_t executeRequest(uint64_t p1, uint64_t p2, uint64_t p3, uint64_t p4, uint64_t p5, uint64_t p6, uint64_t p7, uint64_t p8)=0
A key/value dictionary.
Definition Tree.h:33
A vector / dynamic array.
Definition Vector.h:33
Definition netif.h:244