Skip to content

Commit 16f0c6e

Browse files
authored
Merge pull request #871 from AndreasAakesson/dev
Changed Packet to be sent as unique_ptr instead of shared_ptr
2 parents aa9b7ed + 794d336 commit 16f0c6e

28 files changed

Lines changed: 335 additions & 363 deletions

api/kernel/cpuid.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace CPUID
3131
DTES64, // 64-Bit Debug Store Area
3232
MONITOR, // MONITOR/MWAIT
3333
DS_CPL, // CPL Qualified Debug Store
34-
VMX, // Virtual Machine Extensions
34+
VMX, // Virtual Machine Extensions (VT-x)
3535
SMX, // Safer Mode Extensions
3636
EST, // Enhanced SpeedStep Technology
3737
TM2, // Thermal Monitor 2
@@ -94,6 +94,10 @@ namespace CPUID
9494
PDPE1GB, // 1 GB Pages
9595
RDTSCP, // RDTSCP and IA32_TSC_AUX
9696
LM, // Long mode (64-bit Architecture)
97+
98+
SVM, // Secture Virtual Machines (AMD-V)
99+
// aka. AMD-V (AMD's virtualization extension)
100+
SSE4A, // SSE4a
97101
};
98102

99103
bool is_amd_cpu();

api/net/buffer_store.hpp

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@
2121

2222
#include <stdexcept>
2323
#include <vector>
24-
#include <net/inet_common.hpp>
25-
#include <util/membitmap.hpp>
26-
27-
namespace net{
2824

25+
namespace net
26+
{
2927
/**
3028
* Network buffer storage for uniformly sized buffers.
3129
*
@@ -62,13 +60,6 @@ namespace net{
6260

6361
inline size_t available() const noexcept
6462
{ return available_.size(); }
65-
66-
void lock(void* addr) {
67-
auto* buffer = (buffer_t) addr;
68-
assert(is_from_pool(buffer));
69-
locked.set( buffer_id(buffer) );
70-
}
71-
void unlock_and_release(buffer_t addr);
7263

7364
private:
7465
buffer_t pool_begin() const noexcept {
@@ -80,12 +71,11 @@ namespace net{
8071
size_t buffer_id(buffer_t addr) const {
8172
return (addr - pool_) / bufsize_;
8273
}
83-
74+
8475
size_t poolsize_;
85-
const size_t bufsize_;
76+
size_t bufsize_;
8677
buffer_t pool_;
8778
std::vector<buffer_t> available_;
88-
MemBitmap locked;
8979

9080
BufferStore(BufferStore&) = delete;
9181
BufferStore(BufferStore&&) = delete;

api/net/inet4.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,9 @@ namespace net {
7777
// get buffer (as packet + data)
7878
auto* ptr = (Packet*) bufstore_.get_buffer();
7979
// place packet at front of buffer
80-
new (ptr) Packet(nic_.bufsize(), size,
81-
delegate<void(void*)>::from<BufferStore, &BufferStore::release> (&bufstore_));
80+
new (ptr) Packet(nic_.bufsize(), size, &bufstore_);
8281
// regular shared_ptr that calls delete on Packet
83-
return std::shared_ptr<Packet>(ptr);
82+
return Packet_ptr(ptr);
8483
}
8584

8685
/** MTU retreived from Nic on construction */

api/net/inet_common.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#define NET_INET_COMMON_HPP
2222

2323
#include <delegate>
24+
#include <net/packet.hpp>
2425

2526
namespace net {
2627
// Packet must be forward declared to avoid circular dependency
@@ -30,7 +31,7 @@ namespace net {
3031

3132
using LinkLayer = Ethernet;
3233

33-
using Packet_ptr = std::shared_ptr<Packet>;
34+
using Packet_ptr = std::unique_ptr<Packet>;
3435

3536
// Downstream / upstream delegates
3637
using downstream = delegate<void(Packet_ptr)>;
@@ -48,6 +49,14 @@ namespace net {
4849
return std::static_pointer_cast<T>(packet);
4950
}
5051

52+
53+
template<typename Derived, typename Base>
54+
auto static_unique_ptr_cast( std::unique_ptr<Base>&& p )
55+
{
56+
auto* d = static_cast<Derived *>(p.release());
57+
return std::unique_ptr<Derived>(d);
58+
}
59+
5160
} //< namespace net
5261

5362
#endif //< NET_INET_COMMON_HPP

api/net/ip4/packet_arp.hpp

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
88
// You may obtain a copy of the License at
9-
//
9+
//
1010
// http://www.apache.org/licenses/LICENSE-2.0
11-
//
11+
//
1212
// Unless required by applicable law or agreed to in writing, software
1313
// distributed under the License is distributed on an "AS IS" BASIS,
1414
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -24,62 +24,61 @@
2424

2525
namespace net
2626
{
27-
class PacketArp : public Packet,
28-
public std::enable_shared_from_this<PacketArp>
27+
class PacketArp : public Packet
2928
{
3029
public:
3130
Arp::header& header() const
3231
{
3332
return *(Arp::header*) buffer();
3433
}
35-
34+
3635
static const size_t headers_size = sizeof(Arp::header);
37-
36+
3837
/** initializes to a default, empty Arp packet, given
3938
a valid MTU-sized buffer */
4039
void init(Ethernet::addr local_mac, IP4::addr local_ip)
41-
{
40+
{
4241
auto& hdr = header();
4342
hdr.ethhdr.type = Ethernet::ETH_ARP;
4443
hdr.htype = Arp::H_htype_eth;
4544
hdr.ptype = Arp::H_ptype_ip4;
4645
hdr.hlen_plen = Arp::H_hlen_plen;
47-
46+
4847
hdr.dipaddr = next_hop();
4948
hdr.sipaddr = local_ip;
5049
hdr.shwaddr = local_mac;
5150
}
52-
51+
5352
void set_dest_mac(Ethernet::addr mac) {
5453
header().dhwaddr = mac;
55-
header().ethhdr.dest = mac;
56-
}
57-
54+
header().ethhdr.dest = mac;
55+
}
56+
5857
void set_opcode(Arp::Opcode op) {
5958
header().opcode = op;
6059
}
61-
60+
6261
void set_dest_ip(IP4::addr ip) {
6362
header().dipaddr = ip;
6463
}
65-
64+
6665
IP4::addr source_ip() const {
6766
return header().sipaddr;
6867
}
69-
68+
7069
IP4::addr dest_ip() const {
7170
return header().dipaddr;
7271
}
73-
72+
7473
Ethernet::addr source_mac() const {
7574
return header().ethhdr.src;
7675
};
77-
76+
7877
Ethernet::addr dest_mac() const {
7978
return header().ethhdr.dest;
8079
};
81-
82-
80+
81+
8382
};
8483
}
8584

api/net/ip4/packet_ip4.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626

2727
namespace net {
2828

29-
class PacketIP4 : public Packet, // might work as upcast:
30-
public std::enable_shared_from_this<PacketIP4>
29+
class PacketIP4 : public Packet
3130
{
3231
public:
3332
static constexpr size_t DEFAULT_TTL {64};

api/net/ip4/packet_udp.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323

2424
namespace net
2525
{
26-
class PacketUDP : public PacketIP4, // might work as upcast:
27-
public std::enable_shared_from_this<PacketUDP>
26+
class PacketUDP : public PacketIP4
2827
{
2928
public:
3029

api/net/ip4/udp.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "../inet.hpp"
2424
#include "ip4.hpp"
2525
#include <cstring>
26+
#include <net/packet.hpp>
2627

2728
namespace net {
2829

@@ -35,7 +36,7 @@ namespace net {
3536
using addr_t = IP4::addr;
3637
using port_t = uint16_t;
3738

38-
using Packet_ptr = std::shared_ptr<PacketUDP>;
39+
using Packet_ptr = std::unique_ptr<PacketUDP, std::default_delete<net::Packet>>;
3940
using Stack = Inet<LinkLayer, IP4>;
4041

4142
typedef delegate<void()> sendto_handler;

api/net/packet.hpp

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,17 @@
2020

2121
#include "buffer_store.hpp"
2222
#include "ip4/addr.hpp"
23+
#include <delegate>
2324
#include <cassert>
2425

25-
namespace net {
26-
void default_packet_deleter(void* p);
26+
namespace net
27+
{
28+
class Packet;
29+
using Packet_ptr = std::unique_ptr<Packet>;
2730

28-
class Packet : public std::enable_shared_from_this<Packet>
31+
class Packet
2932
{
3033
public:
31-
using deleter_t = delegate<void(void*)>;
32-
3334
/**
3435
* Construct, using existing buffer.
3536
*
@@ -41,25 +42,30 @@ namespace net {
4142
Packet(
4243
uint16_t cap,
4344
uint16_t len,
44-
deleter_t del = default_packet_deleter) noexcept
45+
BufferStore* bs) noexcept
4546
: capacity_ (cap),
4647
size_ (len),
47-
deleter_ {del}
48+
bufstore (bs)
4849
{}
49-
~Packet() {
50-
deleter_(this);
50+
51+
virtual ~Packet()
52+
{
53+
if (bufstore)
54+
bufstore->release(this);
55+
else
56+
delete[] (uint8_t*) this;
5157
}
5258

5359
/** Get the buffer */
5460
BufferStore::buffer_t buffer() const noexcept
5561
{ return (BufferStore::buffer_t) buf_; }
5662

5763
/** Get the network packet length - i.e. the number of populated bytes */
58-
inline uint16_t size() const noexcept
64+
uint16_t size() const noexcept
5965
{ return size_; }
6066

6167
/** Get the size of the buffer. This is >= len(), usually MTU-size */
62-
inline uint16_t capacity() const noexcept
68+
uint16_t capacity() const noexcept
6369
{ return capacity_; }
6470

6571
void set_size(uint16_t new_size) noexcept {
@@ -77,27 +83,28 @@ namespace net {
7783
/* Add a packet to this packet chain. */
7884
void chain(Packet_ptr p) noexcept {
7985
if (!chain_) {
80-
chain_ = p;
81-
last_ = p;
86+
chain_ = std::move(p);
87+
last_ = chain_.get();
8288
} else {
83-
last_->chain(p);
84-
last_ = p->last_in_chain() ? p->last_in_chain() : p;
89+
auto* ptr = p.get();
90+
last_->chain(std::move(p));
91+
last_ = ptr->last_in_chain() ? ptr->last_in_chain() : ptr;
8592
assert(last_);
8693
}
8794
}
8895

8996
/* Get the last packet in the chain */
90-
Packet_ptr last_in_chain() noexcept
97+
Packet* last_in_chain() noexcept
9198
{ return last_; }
9299

93100
/* Get the tail, i.e. chain minus the first element */
94-
Packet_ptr tail() noexcept
95-
{ return chain_; }
101+
Packet* tail() noexcept
102+
{ return chain_.get(); }
96103

97104
/* Get the tail, and detach it from the head (for FIFO) */
98-
Packet_ptr detach_tail() noexcept
105+
auto detach_tail() noexcept
99106
{
100-
auto tail = chain_;
107+
auto tail = std::move(chain_);
101108
chain_ = 0;
102109
return tail;
103110
}
@@ -107,11 +114,11 @@ namespace net {
107114
* For a UDPv6 packet, the payload location is the start of
108115
* the UDPv6 header, and so on
109116
*/
110-
inline void set_payload(BufferStore::buffer_t location) noexcept
117+
void set_payload(BufferStore::buffer_t location) noexcept
111118
{ payload_ = location; }
112119

113120
/** Get the payload of the packet */
114-
inline BufferStore::buffer_t payload() const noexcept
121+
BufferStore::buffer_t payload() const noexcept
115122
{ return payload_; }
116123

117124
/**
@@ -120,21 +127,15 @@ namespace net {
120127
* Unfortunately, we can't upcast with std::static_pointer_cast
121128
* however, all classes derived from Packet should be good to use
122129
*/
123-
static Packet_ptr packet(Packet_ptr pckt) noexcept
124-
{ return *static_cast<Packet_ptr*>(&pckt); }
125-
126-
// custom deleter for Packet used by network stack
127-
void set_deleter(deleter_t cb) {
128-
this->deleter_ = cb;
129-
}
130+
//static Packet_ptr packet(Packet_ptr pckt) noexcept
131+
//{ return *static_cast<Packet_ptr*>(&pckt); }
130132

131133
// override delete to do nothing
132134
static void operator delete (void*) {}
133135

134136
private:
135-
/** Let's chain packets */
136-
Packet_ptr chain_ {0};
137-
Packet_ptr last_ {0};
137+
Packet_ptr chain_ {nullptr};
138+
Packet* last_ {nullptr};
138139

139140
/** Default constructor Deleted. See Packet(Packet&). */
140141
Packet() = delete;
@@ -158,16 +159,12 @@ namespace net {
158159

159160
uint16_t capacity_;
160161
uint16_t size_;
162+
BufferStore* bufstore;
161163
ip4::Addr next_hop4_;
162-
deleter_t deleter_;
163164
BufferStore::buffer_t payload_ {nullptr};
164165
BufferStore::buffer_t buf_[0];
165166
}; //< class Packet
166167

167-
inline void default_packet_deleter(void* p) {
168-
delete (Packet*) p;
169-
}
170-
171168
} //< namespace net
172169

173170
#endif

0 commit comments

Comments
 (0)