Skip to content

Commit 461343d

Browse files
authored
Merge pull request #2003 from KristianJerpetjon/0.13.x
tcp: fix for checking that ack is within window
2 parents 5dedaac + c865b20 commit 461343d

27 files changed

Lines changed: 894 additions & 105 deletions

api/fs/common.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <memory>
2323
#include <string>
24+
#include <pmr>
2425
#include <vector>
2526
#include "path.hpp"
2627
#include <common>
@@ -35,12 +36,12 @@ namespace fs {
3536
/**
3637
* @brief Shared vector used as a buffer within the filesystem subsystem
3738
*/
38-
using buffer_t = std::shared_ptr<std::vector<uint8_t>>;
39+
using buffer_t = os::mem::buf_ptr;
3940

4041
/** Construct a shared vector **/
4142
template <typename... Args>
4243
buffer_t construct_buffer(Args&&... args) {
43-
return std::make_shared<std::vector<uint8_t>> (std::forward<Args> (args)...);
44+
return std::make_shared<os::mem::buffer> (std::forward<Args> (args)...);
4445
}
4546

4647
/** Container types **/

api/hw/block_device.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <cstdint>
2323
#include <delegate>
2424
#include <memory>
25+
#include <pmr>
2526
#include <vector>
2627

2728
namespace hw {
@@ -32,7 +33,7 @@ namespace hw {
3233
class Block_device {
3334
public:
3435
using block_t = uint64_t;
35-
using buffer_t = std::shared_ptr<std::vector<uint8_t>>;
36+
using buffer_t = os::mem::buf_ptr;
3637
using on_read_func = delegate<void(buffer_t)>;
3738
using on_write_func = delegate<void(bool error)>;
3839

@@ -149,7 +150,7 @@ class Block_device {
149150
* This functionality is not enabled by default, nor always supported
150151
**/
151152
virtual void write(block_t blk, buffer_t, on_write_func) = 0;
152-
153+
153154
virtual bool write_sync(block_t blk, buffer_t) = 0;
154155

155156
/**

api/net/stream.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <cstddef>
2424
#include <delegate>
2525
#include <memory>
26+
#include <pmr>
2627
#include <vector>
2728
#include <net/socket.hpp>
2829

@@ -34,13 +35,13 @@ namespace net {
3435
*/
3536
class Stream {
3637
public:
37-
using buffer_t = std::shared_ptr<std::vector<uint8_t>>;
38+
using buffer_t = os::mem::buf_ptr;
3839
using ptr = Stream_ptr;
3940

4041
/** Construct a shared vector used by streams **/
4142
template <typename... Args>
4243
static buffer_t construct_buffer(Args&&... args) {
43-
return std::make_shared<std::vector<uint8_t>> (std::forward<Args> (args)...);
44+
return std::make_shared<os::mem::buffer> (std::forward<Args> (args)...);
4445
}
4546

4647
/** Called when the stream is ready to be used. */

api/net/tcp/common.hpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
#include <net/addr.hpp>
2323
#include <net/checksum.hpp>
2424
#include <chrono>
25-
#include <vector>
25+
2626
#include <util/units.hpp>
27+
#include <vector>
28+
#include <pmr>
2729

2830
namespace net {
2931
namespace tcp {
@@ -50,8 +52,9 @@ namespace net {
5052
static const std::chrono::milliseconds default_dack_timeout {40};
5153

5254
using namespace util::literals;
53-
static constexpr size_t default_min_bufsize {4_KiB};
54-
static constexpr size_t default_max_bufsize {256_KiB};
55+
static constexpr size_t default_min_bufsize {4_KiB};
56+
static constexpr size_t default_max_bufsize {256_KiB};
57+
static constexpr size_t default_total_bufsize {64_MiB};
5558

5659
using Address = net::Addr;
5760

@@ -62,12 +65,12 @@ namespace net {
6265
using seq_t = uint32_t;
6366

6467
/** A shared buffer pointer */
65-
using buffer_t = std::shared_ptr<std::vector<uint8_t>>;
68+
using buffer_t = os::mem::buf_ptr;
6669

6770
/** Construct a shared vector used in TCP **/
6871
template <typename... Args>
6972
buffer_t construct_buffer(Args&&... args) {
70-
return std::make_shared<std::vector<uint8_t>> (std::forward<Args> (args)...);
73+
return std::make_shared<os::mem::buffer> (std::forward<Args> (args)...);
7174
}
7275

7376
class Connection;

api/net/tcp/connection.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#include <delegate>
3232
#include <util/timer.hpp>
3333

34+
#include <util/alloc_pmr.hpp>
35+
3436
namespace net {
3537
// Forward declaration of the TCP object
3638
class TCP;
@@ -625,6 +627,7 @@ class Connection {
625627

626628
/** The given read request */
627629
std::unique_ptr<Read_request> read_request;
630+
os::mem::Pmr_pool::Resource_ptr bufalloc{nullptr};
628631

629632
/** Queue for write requests to process */
630633
Write_queue writeq;

api/net/tcp/read_buffer.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ namespace tcp {
3434
*/
3535
class Read_buffer {
3636
public:
37+
using Alloc = os::mem::buffer::allocator_type;
3738
/**
3839
* @brief Construct a read buffer.
3940
* Min and max need to be power of 2.
@@ -44,6 +45,8 @@ class Read_buffer {
4445
*/
4546
Read_buffer(const seq_t start, const size_t min, const size_t max);
4647

48+
Read_buffer(const seq_t start, const size_t min, const size_t max, const Alloc& alloc);
49+
4750
/**
4851
* @brief Insert data into the buffer relative to the sequence number.
4952
*

api/net/tcp/read_request.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ class Read_request {
3131
using Buffer_ptr = std::unique_ptr<Read_buffer>;
3232
using Buffer_queue = std::deque<Buffer_ptr>;
3333
using ReadCallback = delegate<void(buffer_t)>;
34+
using Alloc = os::mem::buffer::allocator_type;
3435
static constexpr size_t buffer_limit = 2;
3536
ReadCallback callback;
3637

37-
Read_request(seq_t start, size_t min, size_t max, ReadCallback cb);
38+
Read_request(seq_t start, size_t min, size_t max, ReadCallback cb, Alloc&& alloc = Alloc());
3839

3940
size_t insert(seq_t seq, const uint8_t* data, size_t n, bool psh = false);
4041

@@ -57,6 +58,7 @@ class Read_request {
5758

5859
private:
5960
Buffer_queue buffers;
61+
Alloc alloc;
6062

6163
Read_buffer* get_buffer(const seq_t seq);
6264

api/net/tcp/tcp.hpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <net/socket.hpp>
3232
#include <net/ip4/ip4.hpp>
3333
#include <util/bitops.hpp>
34+
#include <util/alloc_pmr.hpp>
3435

3536
namespace net {
3637

@@ -370,6 +371,22 @@ namespace net {
370371
uint16_t max_syn_backlog() const
371372
{ return max_syn_backlog_; }
372373

374+
/**
375+
* @brief Set the maximum allowed memory
376+
* to be used by this TCP.
377+
*
378+
* @param[in] size The limit in bytes
379+
*/
380+
void set_total_bufsize(const size_t size)
381+
{
382+
total_bufsize_ = size;
383+
mempool_.set_total_capacity(total_bufsize_);
384+
}
385+
386+
const os::mem::Pmr_pool& mempool() {
387+
return mempool_;
388+
}
389+
373390
/**
374391
* @brief Sets the minimum buffer size.
375392
*
@@ -532,6 +549,12 @@ namespace net {
532549
Listeners listeners_;
533550
Connections connections_;
534551

552+
size_t total_bufsize_;
553+
os::mem::Pmr_pool mempool_;
554+
555+
size_t min_bufsize_;
556+
size_t max_bufsize_;
557+
535558
Port_utils& ports_;
536559

537560
downstream network_layer_out_;
@@ -557,9 +580,6 @@ namespace net {
557580
/** Maximum SYN queue backlog */
558581
uint16_t max_syn_backlog_;
559582

560-
size_t min_bufsize_ {tcp::default_min_bufsize};
561-
size_t max_bufsize_ {tcp::default_max_bufsize};
562-
563583
/** Stats */
564584
uint64_t* bytes_rx_ = nullptr;
565585
uint64_t* bytes_tx_ = nullptr;

api/pmr

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
#if __has_include(<memory_resource>)
4+
#include <memory_resource>
5+
#else
6+
#include <experimental/memory_resource>
7+
#include <experimental/vector>
8+
namespace std {
9+
namespace pmr = std::experimental::pmr;
10+
}
11+
#endif
12+
13+
namespace os::mem {
14+
using buffer = std::pmr::vector<uint8_t>;
15+
using buf_ptr = std::shared_ptr<buffer>;
16+
}

api/util/alloc_buddy.hpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,7 @@
2121
#include <common>
2222
#include <sstream>
2323
#include <array>
24-
#if __has_include(<memory_resource>)
25-
#include <memory_resource>
26-
#else
27-
#include <experimental/memory_resource>
28-
namespace std::pmr {
29-
using memory_resource = std::experimental::pmr::memory_resource;
30-
}
31-
#endif
24+
#include <pmr>
3225
#include <stdlib.h>
3326
#include <math.h>
3427

0 commit comments

Comments
 (0)