Skip to content

Commit

Permalink
Implement TCP client on native-stack
Browse files Browse the repository at this point in the history
  • Loading branch information
syuu1228 committed Jan 7, 2015
1 parent 7b3b9e5 commit e9e1ed7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion net/native-stack.cc
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ native_network_stack::listen(socket_address sa, listen_options opts) {
future<connected_socket>
native_network_stack::connect(socket_address sa) {
assert(sa.as_posix_sockaddr().sa_family == AF_INET);
return make_ready_future<connected_socket>(connected_socket(nullptr));
return make_ready_future<connected_socket>(tcpv4_connect(_inet.get_tcp(), sa));
}

using namespace std::chrono_literals;
Expand Down
4 changes: 4 additions & 0 deletions net/tcp-stack.hh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

class listen_options;
class server_socket;
class connected_socket;

namespace net {

Expand All @@ -19,6 +20,9 @@ class tcp;
server_socket
tcpv4_listen(tcp<ipv4_traits>& tcpv4, uint16_t port, listen_options opts);

connected_socket
tcpv4_connect(tcp<ipv4_traits>& tcpv4, socket_address sa);

}

#endif
6 changes: 6 additions & 0 deletions net/tcp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,11 @@ tcpv4_listen(tcp<ipv4_traits>& tcpv4, uint16_t port, listen_options opts) {
tcpv4, port, opts));
}

connected_socket
tcpv4_connect(tcp<ipv4_traits>& tcpv4, socket_address sa) {
return connected_socket(std::make_unique<native_connected_socket_impl<tcp<ipv4_traits>>>(
tcpv4.connect(sa)));
}

}

28 changes: 27 additions & 1 deletion net/tcp.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <deque>
#include <chrono>
#include <experimental/optional>
#include <random>

#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
#include <cryptopp/md5.h>
Expand Down Expand Up @@ -300,6 +301,9 @@ private:
inet_type& _inet;
std::unordered_map<connid, lw_shared_ptr<tcb>, connid_hash> _tcbs;
std::unordered_map<uint16_t, listener*> _listening;
std::random_device _rd;
std::default_random_engine _e;
std::uniform_int_distribution<uint16_t> _port_dist{41952, 65535};
public:
class connection {
lw_shared_ptr<tcb> _tcb;
Expand Down Expand Up @@ -358,10 +362,11 @@ public:
friend class tcp;
};
public:
explicit tcp(inet_type& inet) : _inet(inet) {}
explicit tcp(inet_type& inet) : _inet(inet), _e(_rd()) {}
void received(packet p, ipaddr from, ipaddr to);
bool forward(forward_hash& out_hash_data, packet& p, size_t off);
listener listen(uint16_t port, size_t queue_length = 100);
connection connect(socket_address sa);
net::hw_features hw_features() { return _inet._inet.hw_features(); }
private:
void send(ipaddr from, ipaddr to, packet p);
Expand All @@ -374,6 +379,27 @@ auto tcp<InetTraits>::listen(uint16_t port, size_t queue_length) -> listener {
return listener(*this, port, queue_length);
}

template <typename InetTraits>
auto tcp<InetTraits>::connect(socket_address sa) -> connection {
uint16_t src_port;
connid id;
auto src_ip = _inet._inet.host_address();
auto dst_ip = ipv4_address(sa);
auto dst_port = net::ntoh(sa.u.in.sin_port);

do {
src_port = _port_dist(_e);
id = connid{src_ip, dst_ip, src_port, dst_port};
} while (_inet._inet.netif()->hash2cpu(id.hash()) != engine.cpu_id()
|| _tcbs.find(id) != _tcbs.end());

auto tcbp = make_lw_shared<tcb>(*this, id);
_tcbs.insert({id, tcbp});
tcbp->output();

return connection(tcbp);
}

template <typename InetTraits>
bool tcp<InetTraits>::forward(forward_hash& out_hash_data, packet& p, size_t off) {
auto th = p.get_header<tcp_hdr>(off);
Expand Down

0 comments on commit e9e1ed7

Please sign in to comment.