Skip to content

Commit b116650

Browse files
authored
Merge pull request #1237 from alfred-bratterud/dev
IP4 refactor, Arp improvements and POSIX cleanup
2 parents e66d299 + d31d97b commit b116650

43 files changed

Lines changed: 777 additions & 400 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

api/net/inet.hpp

Lines changed: 96 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#ifndef NET_INET_HPP
1919
#define NET_INET_HPP
2020

21+
#include <chrono>
22+
2123
#include <net/inet_common.hpp>
2224
#include <hw/mac_addr.hpp>
2325
#include <hw/nic.hpp>
@@ -44,25 +46,106 @@ namespace net {
4446
template <typename IPv>
4547
using resolve_func = delegate<void(typename IPv::addr)>;
4648

49+
50+
///
51+
/// NETWORK CONFIGURATION
52+
///
53+
54+
/** Get IP address of this interface **/
4755
virtual typename IPV::addr ip_addr() = 0;
56+
57+
/** Get netmask of this interface **/
4858
virtual typename IPV::addr netmask() = 0;
59+
60+
/** Get default gateway for this interface **/
4961
virtual typename IPV::addr gateway() = 0;
50-
virtual std::string ifname() const = 0;
51-
virtual MAC::Addr link_addr() = 0;
52-
virtual hw::Nic& nic() = 0;
5362

63+
/** Set default gateway for this interface */
64+
virtual void set_gateway(typename IPV::addr server) = 0;
65+
66+
/** Set DNS server for this interface */
67+
virtual void set_dns_server(typename IPV::addr server) = 0;
68+
69+
/** Configure network for this interface */
70+
virtual void network_config(typename IPV::addr ip,
71+
typename IPV::addr nmask,
72+
typename IPV::addr gateway,
73+
typename IPV::addr dnssrv = IPV::ADDR_ANY) = 0;
74+
75+
/** Reset network configuration for this interface */
76+
virtual void reset_config() = 0;
77+
78+
using dhcp_timeout_func = delegate<void(bool timed_out)>;
79+
80+
/** Use DHCP to configure this interface */
81+
virtual void negotiate_dhcp(double timeout = 10.0, dhcp_timeout_func = nullptr) = 0;
82+
83+
84+
///
85+
/// PROTOCOL OBJECTS
86+
///
87+
88+
/** Get the IP protocol object for this interface */
5489
virtual IPV& ip_obj() = 0;
90+
91+
/** Get the TCP protocol object for this interface */
5592
virtual TCP& tcp() = 0;
93+
94+
/** Get the UDP protocol object for this interface */
5695
virtual UDP& udp() = 0;
5796

97+
98+
///
99+
/// DNS
100+
///
101+
102+
/** DNS resolution */
103+
virtual void resolve(const std::string& hostname, resolve_func<IPV> func) = 0;
104+
105+
106+
///
107+
/// LINK LAYER
108+
///
109+
110+
/** Get the network interface device */
111+
virtual hw::Nic& nic() = 0;
112+
113+
/** Get interface name for this interface **/
114+
virtual std::string ifname() const = 0;
115+
116+
/** Get linklayer address for this interface **/
117+
virtual MAC::Addr link_addr() = 0;
118+
119+
/** Add cache entry to the link / IP address cache */
120+
virtual void cache_link_addr(typename IPV::addr, MAC::Addr) = 0;
121+
122+
/** Flush link / IP address cache */
123+
virtual void flush_link_cache() = 0;
124+
125+
/** Set the regular interval for link address cache flushing */
126+
virtual void set_link_cache_flush_interval(std::chrono::minutes);
127+
128+
129+
///
130+
/// ROUTING
131+
///
132+
133+
/** Set an IP forwarding delegate. E.g. used to enable routing */
58134
virtual void set_forward_delg(Forward_delg) = 0;
135+
136+
/** Assign boolean function to determine if we have route to a given IP */
59137
virtual void set_route_checker(Route_checker) = 0;
60-
virtual void cache_link_ip(typename IPV::addr, MAC::Addr) = 0;
61-
virtual void flush_link_ip_cache() = 0;
138+
139+
/** Get the IP forwarding delegate */
62140
virtual Forward_delg forward_delg() = 0;
63141

64-
virtual constexpr uint16_t MTU() const = 0;
65142

143+
///
144+
/// PACKET MANAGEMENT
145+
///
146+
147+
/** Get Maximum Transmission Unit **/
148+
virtual constexpr uint16_t MTU() const = 0;
66149

67150
/** Provision empty anonymous packet **/
68151
virtual Packet_ptr create_packet() = 0;
@@ -73,21 +156,6 @@ namespace net {
73156
/** Provision empty IP packet **/
74157
virtual typename IPV::IP_packet_ptr create_ip_packet(Protocol) = 0;
75158

76-
virtual void resolve(const std::string& hostname, resolve_func<IPV> func) = 0;
77-
78-
virtual void set_gateway(typename IPV::addr server) = 0;
79-
80-
virtual void set_dns_server(typename IPV::addr server) = 0;
81-
82-
virtual void network_config(typename IPV::addr ip,
83-
typename IPV::addr nmask,
84-
typename IPV::addr gateway,
85-
typename IPV::addr dnssrv = IPV::ADDR_ANY) = 0;
86-
virtual void reset_config() = 0;
87-
88-
using dhcp_timeout_func = delegate<void(bool timed_out)>;
89-
virtual void negotiate_dhcp(double timeout = 10.0, dhcp_timeout_func = nullptr) = 0;
90-
91159
/** Event triggered when there are available buffers in the transmit queue */
92160
virtual void on_transmit_queue_available(transmit_avail_delg del) = 0;
93161

@@ -97,8 +165,15 @@ namespace net {
97165
/** Number of buffers available in the bufstore */
98166
virtual size_t buffers_available() = 0;
99167

168+
/** Start TCP (e.g. after system suspension). */
100169
virtual void force_start_send_queues() = 0;
101170

171+
172+
///
173+
/// SMP
174+
///
175+
176+
/** Move this interface to the CPU executing the call */
102177
virtual void move_to_this_cpu() = 0;
103178
virtual int get_cpu_id() const noexcept = 0;
104179

api/net/inet4.hpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,15 @@ namespace net {
5656
IP4& ip_obj() override
5757
{ return ip4_; }
5858

59-
void cache_link_ip(IP4::addr ip, MAC::Addr mac) override
59+
void cache_link_addr(IP4::addr ip, MAC::Addr mac) override
6060
{ arp_.cache(ip, mac); }
6161

62-
void flush_link_ip_cache() override
62+
void flush_link_cache() override
6363
{ arp_.flush_cache(); }
6464

65+
void set_link_cache_flush_interval(std::chrono::minutes min) override
66+
{ arp_.set_cache_flush_interval(min); }
67+
6568
/** Get the TCP-object belonging to this stack */
6669
TCP& tcp() override { return tcp_; }
6770

@@ -75,19 +78,21 @@ namespace net {
7578
* Set the forwarding delegate used by this stack.
7679
* If set it will get all incoming packets not intended for this stack.
7780
*/
78-
void set_forward_delg(Forward_delg fwd) override { forward_packet_ = fwd; }
81+
void set_forward_delg(Forward_delg fwd) override {
82+
ip4_.set_packet_forwarding(fwd);
83+
}
7984

8085
/**
8186
* Assign a delegate that checks if we have a route to a given IP
8287
*/
8388
void set_route_checker(Route_checker delg) override
84-
{ arp_.set_route_checker(delg); }
89+
{ arp_.set_proxy_policy(delg); }
8590

8691
/**
8792
* Get the forwarding delegate used by this stack.
8893
*/
8994
Forward_delg forward_delg() override
90-
{ return forward_packet_; }
95+
{ return ip4_.forward_delg(); }
9196

9297

9398
Packet_ptr create_packet() override {
@@ -246,7 +251,7 @@ namespace net {
246251
ICMPv4 icmp_;
247252
UDP udp_;
248253
TCP tcp_;
249-
Forward_delg forward_packet_;
254+
250255
// we need this to store the cache per-stack
251256
DNSClient dns;
252257

api/net/inet_common.hpp

Lines changed: 84 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,39 @@ namespace net {
6666
return std::unique_ptr<Derived>(d);
6767
}
6868

69+
class Error {
70+
public:
71+
enum class Type : uint8_t {
72+
no_error,
73+
general_IO,
74+
ifdown,
75+
ICMP
76+
// Add more as needed
77+
};
78+
79+
virtual Type type()
80+
{ return t_; }
81+
82+
virtual const char* what()
83+
{ return msg_; }
84+
85+
virtual ~Error() = default;
86+
87+
Error() = default;
88+
89+
Error(Type t, const char* msg)
90+
: t_{t}, msg_{msg}
91+
{};
92+
93+
operator bool()
94+
{ return t_ != Type::no_error; }
95+
96+
private:
97+
Type t_ = Type::no_error;
98+
const char* msg_ = "No error";
99+
};
100+
101+
69102
/* RFC 6335 - IANA */
70103
namespace port_ranges
71104
{
@@ -84,15 +117,59 @@ namespace net {
84117
*/
85118
enum class Protocol : uint8_t {
86119
HOPOPT = 0,
87-
ICMPv4 = 1,
88-
IP4v4 = 4, // IPv4 encapsulation
89-
TCP = 6,
90-
UDP = 17,
91-
IPv6 = 41, // IPv6 encapsulation
92-
ICMPv6 = 58
93-
};
120+
ICMPv4 = 1,
121+
IP4v4 = 4, // IPv4 encapsulation
122+
TCP = 6,
123+
UDP = 17,
124+
IPv6 = 41, // IPv6 encapsulation
125+
ICMPv6 = 58
126+
};
94127

128+
/**
129+
* Explicit Congestion Notification (ECN) values for IPv4 and IPv6
130+
* Defined in RFC3168
131+
**/
132+
enum class ECN : uint8_t {
133+
NOT_ECT = 0b00, // Non-ECN transport
134+
ECT_0 = 0b01, // ECN-enabled transport 1
135+
ECT_1 = 0b10, // ECN-enabled transport 2
136+
CE = 0b11 // Congestion encountered
137+
};
95138

139+
/**
140+
* Differentiated Services Code Points, for IPv4 and IPv6
141+
* Defined in RFC2474
142+
* NOTE: Replaces IPv4 TOS field
143+
*
144+
* IANA list:
145+
* https://www.iana.org/assignments/dscp-registry/dscp-registry.xhtml
146+
*
147+
* 6 DSCP bits together with 2 ECN bits form one octet
148+
**/
149+
enum class DSCP : uint8_t {
150+
CS0 = 0b000000,
151+
CS1 = 0b001000,
152+
CS2 = 0b010000,
153+
CS3 = 0b011000,
154+
CS4 = 0b100000,
155+
CS5 = 0b101000,
156+
CS6 = 0b110000,
157+
CS7 = 0b111000,
158+
AF11 = 0b001010,
159+
AF12 = 0b001100,
160+
AF13 = 0b001110,
161+
AF21 = 0b010010,
162+
AF22 = 0b010100,
163+
AF23 = 0b010110,
164+
AF31 = 0b011010,
165+
AF32 = 0b011100,
166+
AF33 = 0b011110,
167+
AF41 = 0b100010,
168+
AF42 = 0b100100,
169+
AF43 = 0b100110,
170+
EF_PHB = 0b101110,
171+
VOICE_ADMIT = 0b101100
172+
};
96173

97174
inline uint16_t new_ephemeral_port() noexcept
98175
{ return port_ranges::DYNAMIC_START + rand() % (port_ranges::DYNAMIC_END - port_ranges::DYNAMIC_START); }

0 commit comments

Comments
 (0)