Allow providing options to configure network stacks

This commit is contained in:
Avi Kivity
2014-09-17 17:44:58 +03:00
parent f81760aba7
commit 3168a2fc7c
4 changed files with 65 additions and 23 deletions

View File

@@ -36,7 +36,7 @@ wrap_syscall(T result) {
}
reactor::reactor()
: _network_stack(network_stack_registry::create())
: _network_stack(network_stack_registry::create({}))
, _epollfd(file_desc::epoll_create(EPOLL_CLOEXEC))
, _io_eventfd()
, _io_context(0)
@@ -47,8 +47,8 @@ reactor::reactor()
void reactor::configure(boost::program_options::variables_map vm) {
_network_stack = vm.count("network-stack")
? network_stack_registry::create(sstring(vm["network-stack"].as<std::string>()))
: network_stack_registry::create();
? network_stack_registry::create(sstring(vm["network-stack"].as<std::string>()), vm)
: network_stack_registry::create(vm);
_handle_sigint = !vm.count("no-handle-interrupt");
}
@@ -458,8 +458,10 @@ posix_network_stack::listen(socket_address sa, listen_options opt) {
}
void network_stack_registry::register_stack(sstring name,
std::function<std::unique_ptr<network_stack> ()> create, bool make_default) {
boost::program_options::options_description opts,
std::function<std::unique_ptr<network_stack> (options opts)> create, bool make_default) {
_map()[name] = std::move(create);
options_description().add(opts);
if (make_default) {
_default() = name;
}
@@ -478,13 +480,13 @@ std::vector<sstring> network_stack_registry::list() {
}
std::unique_ptr<network_stack>
network_stack_registry::create() {
return create(_default());
network_stack_registry::create(options opts) {
return create(_default(), opts);
}
std::unique_ptr<network_stack>
network_stack_registry::create(sstring name) {
return _map()[name]();
network_stack_registry::create(sstring name, options opts) {
return _map()[name](opts);
}
boost::program_options::options_description
@@ -498,9 +500,14 @@ reactor::get_options_description() {
format_separated(net_stack_names.begin(), net_stack_names.end(), ", ")).c_str())
("no-handle-interrupt", "ignore SIGINT (for gdb)")
;
opts.add(network_stack_registry::options_description());
return opts;
}
network_stack_registrator<posix_network_stack> nsr_posix{"posix", true};
network_stack_registrator nsr_posix{"posix",
boost::program_options::options_description(),
posix_network_stack::create,
true
};
reactor engine;

View File

@@ -232,10 +232,13 @@ public:
};
class network_stack_registry {
public:
using options = boost::program_options::variables_map;
private:
static std::unordered_map<sstring,
std::function<std::unique_ptr<network_stack> ()>>& _map() {
std::function<std::unique_ptr<network_stack> (options opts)>>& _map() {
static std::unordered_map<sstring,
std::function<std::unique_ptr<network_stack> ()>> map;
std::function<std::unique_ptr<network_stack> (options opts)>> map;
return map;
}
static sstring& _default() {
@@ -243,25 +246,38 @@ class network_stack_registry {
return def;
}
public:
static boost::program_options::options_description& options_description() {
static boost::program_options::options_description opts;
return opts;
}
static void register_stack(sstring name,
std::function<std::unique_ptr<network_stack> ()> create, bool make_default = false);
boost::program_options::options_description opts,
std::function<std::unique_ptr<network_stack> (options opts)> create,
bool make_default = false);
static sstring default_stack();
static std::vector<sstring> list();
static std::unique_ptr<network_stack> create();
static std::unique_ptr<network_stack> create(sstring name);
static std::unique_ptr<network_stack> create(options opts);
static std::unique_ptr<network_stack> create(sstring name, options opts);
};
template <typename NetworkStack>
class network_stack_registrator {
public:
explicit network_stack_registrator(sstring name, bool make_default = false) {
network_stack_registry::register_stack(name, std::make_unique<NetworkStack>, make_default);
using options = boost::program_options::variables_map;
explicit network_stack_registrator(sstring name,
boost::program_options::options_description opts,
std::function<std::unique_ptr<network_stack> (options opts)> factory,
bool make_default = false) {
network_stack_registry::register_stack(name, opts, factory, make_default);
}
};
class posix_network_stack : public network_stack {
public:
posix_network_stack(boost::program_options::variables_map opts) {}
virtual server_socket listen(socket_address sa, listen_options opts) override;
static std::unique_ptr<network_stack> create(boost::program_options::variables_map opts) {
return std::unique_ptr<network_stack>(new posix_network_stack(opts));
}
};
class readable_eventfd {

View File

@@ -46,14 +46,16 @@ class native_network_stack : public network_stack {
ipv4 _inet;
using tcp4 = tcp<ipv4_traits>;
public:
explicit native_network_stack(std::unique_ptr<device> dev);
explicit native_network_stack() : native_network_stack(create_virtio_net_device("tap0")) {}
explicit native_network_stack(boost::program_options::variables_map opts);
virtual server_socket listen(socket_address sa, listen_options opt) override;
static std::unique_ptr<network_stack> create(boost::program_options::variables_map opts) {
return std::make_unique<native_network_stack>(opts);
}
friend class native_server_socket_impl<tcp4>;
};
native_network_stack::native_network_stack(std::unique_ptr<device> dev)
: _netif(std::move(dev))
native_network_stack::native_network_stack(boost::program_options::variables_map opts)
: _netif(create_virtio_net_device(opts["tap-device"].as<std::string>()))
, _inet(&_netif) {
_netif.run();
_inet.set_host_address(ipv4_address("192.168.122.2"));
@@ -144,6 +146,19 @@ native_connected_socket_impl<Protocol>::output() {
std::unique_ptr<native_network_stack> native_network_stack::_s;
network_stack_registrator<native_network_stack> nns_registrator{"native"};
boost::program_options::options_description nns_options() {
boost::program_options::options_description opts(
"Native networking stack options");
opts.add_options()
("tap-device",
boost::program_options::value<std::string>()->default_value("tap0"),
"tap device to connect to")
;
return opts;
}
network_stack_registrator nns_registrator{
"native", nns_options(), native_network_stack::create
};
}

View File

@@ -6,11 +6,15 @@
#define STACK_HH_
#include "core/reactor.hh"
#include <boost/program_options.hpp>
namespace net {
boost::program_options::options_description
native_network_stack_program_options();
std::unique_ptr<network_stack>
create_native_network_stack();
create_native_network_stack(boost::program_options::options_description opts);
}