forked from cloudius-systems/osv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet_channel.cc
158 lines (145 loc) · 4.2 KB
/
net_channel.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* Copyright (C) 2014 Cloudius Systems, Ltd.
*
* This work is open source software, licensed under the terms of the
* BSD license as described in the LICENSE file in the top-level directory.
*/
#include <osv/net_channel.hh>
#include <osv/poll.h>
#include <bsd/sys/sys/mbuf.h>
#include <bsd/sys/net/ethernet.h>
#include <bsd/sys/netinet/ip.h>
#include <bsd/sys/netinet/ip.h>
#include <bsd/sys/netinet/tcp.h>
#include <bsd/sys/net/ethernet.h>
#include <osv/debug.hh>
std::ostream& operator<<(std::ostream& os, in_addr ia)
{
auto x = ntohl(ia.s_addr);
return osv::fprintf(os, "%d.%d.%d.%d",
(x >> 24) & 255, (x >> 16) & 255, (x >> 8) & 255, x & 255);
}
std::ostream& operator<<(std::ostream& os, ipv4_tcp_conn_id id)
{
return osv::fprintf(os, "{ ipv4 %s:%d -> %s:%d }", id.src_addr, id.src_port, id.dst_addr, id.dst_port);
}
void net_channel::process_queue()
{
mbuf* m;
while (_queue.pop(m)) {
_process_packet(m);
}
}
void net_channel::wake_pollers()
{
WITH_LOCK(osv::rcu_read_lock) {
auto& pl = *_pollers.read();
for (pollreq* pr : pl) {
// net_channel is self synchronizing
pr->_awake.store(true, std::memory_order_relaxed);
pr->_poll_thread->wake();
}
}
}
void net_channel::add_poller(pollreq& pr)
{
WITH_LOCK(_pollers_mutex) {
auto old = _pollers.read_by_owner();
std::unique_ptr<std::vector<pollreq*>> neww{new std::vector<pollreq*>};
if (old) {
*neww = *old;
}
neww->push_back(&pr);
_pollers.assign(neww.release());
osv::rcu_dispose(old);
}
}
void net_channel::del_poller(pollreq& pr)
{
WITH_LOCK(_pollers_mutex) {
auto old = _pollers.read_by_owner();
std::unique_ptr<std::vector<pollreq*>> neww{new std::vector<pollreq*>};
if (old) {
*neww = *old;
}
neww->erase(std::remove(neww->begin(), neww->end(), &pr), neww->end());
_pollers.assign(neww.release());
osv::rcu_dispose(old);
}
}
classifier::classifier()
: _ipv4_tcp_channels(new ipv4_tcp_channels)
{
}
void classifier::add(ipv4_tcp_conn_id id, net_channel* channel)
{
WITH_LOCK(_mtx) {
auto old = _ipv4_tcp_channels.read_by_owner();
std::unique_ptr<ipv4_tcp_channels> neww{new ipv4_tcp_channels(*old)};
neww->emplace(id, channel);
_ipv4_tcp_channels.assign(neww.release());
osv::rcu_dispose(old);
}
}
void classifier::remove(ipv4_tcp_conn_id id)
{
WITH_LOCK(_mtx) {
auto old = _ipv4_tcp_channels.read_by_owner();
std::unique_ptr<ipv4_tcp_channels> neww{new ipv4_tcp_channels(*old)};
neww->erase(id);
_ipv4_tcp_channels.assign(neww.release());
osv::rcu_dispose(old);
}
}
bool classifier::post_packet(mbuf* m)
{
WITH_LOCK(osv::rcu_read_lock) {
if (auto nc = classify_ipv4_tcp(m)) {
nc->push(m);
// FIXME: find a way to batch wakes
nc->wake();
return true;
}
}
return false;
}
// must be called with rcu lock held
net_channel* classifier::classify_ipv4_tcp(mbuf* m)
{
caddr_t h = m->m_hdr.mh_data;
if (unsigned(m->m_hdr.mh_len) < ETHER_HDR_LEN + sizeof(ip)) {
return nullptr;
}
auto ether_hdr = reinterpret_cast<ether_header*>(h);
if (ntohs(ether_hdr->ether_type) != ETHERTYPE_IP) {
return nullptr;
}
h += ETHER_HDR_LEN;
auto ip_hdr = reinterpret_cast<ip*>(h);
unsigned ip_size = ip_hdr->ip_hl << 2;
if (ip_size < sizeof(ip)) {
return nullptr;
}
if (ip_hdr->ip_p != IPPROTO_TCP) {
return nullptr;
}
if (ntohs(ip_hdr->ip_off) & ~IP_DF) {
return nullptr;
}
auto src_addr = ip_hdr->ip_src;
auto dst_addr = ip_hdr->ip_dst;
h += ip_size;
auto tcp_hdr = reinterpret_cast<tcphdr*>(h);
if (tcp_hdr->th_flags & (TH_SYN | TH_FIN | TH_RST)) {
return nullptr;
}
auto src_port = ntohs(tcp_hdr->th_sport);
auto dst_port = ntohs(tcp_hdr->th_dport);
auto id = ipv4_tcp_conn_id{src_addr, dst_addr, src_port, dst_port};
auto ht = _ipv4_tcp_channels.read();
auto i = ht->find(id);
if (i == ht->end()) {
return nullptr;
}
return i->second;
}