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/compiler.h"
23#include "pedigree/kernel/machine/Network.h"
24#include "pedigree/kernel/process/Mutex.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/String.h"
29#include "pedigree/kernel/utilities/Tree.h"
30#include "pedigree/kernel/utilities/Vector.h"
31
32#include <config.h>
33
34// lwIP network interface type
35struct netif;
36
42class EXPORTED_PUBLIC NetworkStack {
43 public:
51 class EXPORTED_PUBLIC DeviceLease {
52 public:
54 DeviceLease(DeviceLease&& other);
56
57 DeviceLease& operator=(DeviceLease&& other);
58
59 Network* device() const {
60 return m_Device;
61 }
62
63 struct netif* interface() const {
64 return m_Interface;
65 }
66
67 explicit operator bool() const {
68 return m_Device != nullptr;
69 }
70
71 private:
72 friend class NetworkStack;
73
74 DeviceLease(Network* device, struct netif* interface, OperationBarrier::Lease&& lease);
75
76 DeviceLease(const DeviceLease&) = delete;
77 DeviceLease& operator=(const DeviceLease&) = delete;
78
79 Network* m_Device;
80 struct netif* m_Interface;
82 };
83
85 virtual ~NetworkStack();
86
89 return *__atomic_load_n(&stack, __ATOMIC_ACQUIRE);
90 }
91
92 static NetworkStack* instanceIfAvailable() {
93 return __atomic_load_n(&stack, __ATOMIC_ACQUIRE);
94 }
95
97 void receive(size_t nBytes, uintptr_t packet, Network* pCard, uint32_t offset);
98
100 void receive(size_t nBytes, uintptr_t packet, const DeviceLease& device, uint32_t offset);
101
103 void registerDevice(Network* pDevice);
104
106 MUST_USE_RESULT bool acquireDevice(size_t n, DeviceLease& lease);
107
109 MUST_USE_RESULT bool acquireDevice(Network* pDevice, DeviceLease& lease);
110
112 void deRegisterDevice(Network* pDevice);
113
116 return m_MemPool;
117 }
118
119 private:
120 struct DeviceRegistration;
121
122 static NetworkStack* stack;
123
126
129
130#if THREADS || UTILITY_LINUX
131 Mutex m_Lock;
132#endif
133
136
139};
140
141#endif
Definition Mutex.h:56
MemoryPool & getMemPool()
size_t m_NextInterfaceNumber
MemoryPool m_MemPool
Tree< Network *, DeviceRegistration * > m_Registrations
static NetworkStack & instance()
Vector< Network * > m_Children
A key/value dictionary.
Definition Tree.h:33
A vector / dynamic array.
Definition Vector.h:33
Definition netif.h:244