Skip to content

Commit c4f6d39

Browse files
committed
Merge branch 'dev' of github.com:AndreasAakesson/IncludeOS into dev
2 parents 97df469 + c3ed857 commit c4f6d39

5 files changed

Lines changed: 94 additions & 42 deletions

File tree

examples/websocket/config.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
{
22
"net": [
3-
["10.0.0.42", "255.255.255.0", "10.0.0.1"]
3+
{
4+
"iface": 0,
5+
"config": "static",
6+
"address": "10.0.0.42",
7+
"netmask": "255.255.255.0",
8+
"gateway": "10.0.0.1"
9+
}
410
]
511
}
12+
13+

src/net/configure.cpp

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,17 @@ using Addresses = std::vector<ip4::Addr>;
2929
template <typename T>
3030
Addresses parse_iface(T& obj)
3131
{
32-
Expects(obj.IsArray());
32+
Expects(obj.HasMember("address"));
33+
Expects(obj.HasMember("netmask"));
34+
Expects(obj.HasMember("gateway"));
3335

34-
// Iface can either be empty: [] or contain 3 to 4 IP's (see Inet static config)
35-
if(not obj.Empty())
36-
{
37-
Expects(obj.Size() >= 3 and obj.Size() <= 4);
38-
39-
Addresses addresses;
40-
41-
for(auto& addr : obj.GetArray())
42-
{
43-
addresses.emplace_back(std::string{addr.GetString()});
44-
}
45-
46-
return addresses;
47-
}
36+
ip4::Addr address{obj["address"].GetString()};
37+
ip4::Addr netmask{obj["netmask"].GetString()};
38+
ip4::Addr gateway{obj["gateway"].GetString()};
39+
ip4::Addr dns = (not obj.HasMember("dns")) ? gateway : ip4::Addr{obj["dns"].GetString()};
4840

49-
return {};
41+
Addresses addresses{address, netmask, gateway, dns};
42+
return addresses;
5043
}
5144

5245
inline void config_stack(Inet<IP4>& stack, const Addresses& addrs)
@@ -69,23 +62,45 @@ void configure(const rapidjson::Value& net)
6962

7063
Expects(net.IsArray() && "Member net is not an array");
7164

72-
int N = 0;
73-
for(auto& val : net.GetArray())
65+
auto configs = net.GetArray();
66+
if(configs.Size() > Super_stack::inet().ip4_stacks().size())
67+
MYINFO("! WARNING: Found more configs than there are interfaces");
68+
// Iterate all interfaces in config
69+
for(auto& val : configs)
7470
{
75-
if(N >= static_cast<int>(Super_stack::inet().ip4_stacks().size()))
76-
break;
71+
Expects(val.HasMember("iface")
72+
&& "Missing member iface - don't know which interface to configure");
73+
74+
auto N = val["iface"].GetInt();
7775

7876
auto& stack = Super_stack::get<IP4>(N);
79-
// static configs
80-
if(val.IsArray())
77+
78+
// if config is not set, just ignore
79+
if(not val.HasMember("config"))
80+
continue;
81+
82+
std::string method = val["config"].GetString();
83+
84+
if(method == "dhcp")
85+
{
86+
stack.negotiate_dhcp(5.0);
87+
}
88+
else if(method == "static")
8189
{
8290
config_stack(stack, parse_iface(val));
8391
}
84-
else if(val.IsString() and strcmp(val.GetString(), "dhcp") == 0)
92+
else if(method == "dhcp-with-fallback")
8593
{
86-
stack.negotiate_dhcp(5.0);
94+
auto addresses = parse_iface(val);
95+
auto static_cfg = [addresses, &stack] (bool timeout)
96+
{
97+
if(timeout) {
98+
MYINFO("DHCP timeout (%s) - falling back to static configuration", stack.ifname().c_str());
99+
config_stack(stack, addresses);
100+
}
101+
};
102+
stack.negotiate_dhcp(5.0, static_cfg);
87103
}
88-
++N;
89104
}
90105

91106
MYINFO("Configuration complete");
Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,34 @@
11
{
22
"net": [
3-
["10.0.0.42", "255.255.255.0", "10.0.0.1"],
4-
["10.0.0.43", "255.255.255.0", "10.0.0.1", "8.8.8.8"],
5-
"none",
6-
"dhcp"
3+
{
4+
"iface": 0,
5+
"config": "static",
6+
"address": "10.0.0.42",
7+
"netmask": "255.255.255.0",
8+
"gateway": "10.0.0.1"
9+
},
10+
{
11+
"iface": 1,
12+
"config": "static",
13+
"address": "10.0.0.43",
14+
"netmask": "255.255.255.0",
15+
"gateway": "10.0.0.1",
16+
"dns": "8.8.8.8"
17+
},
18+
{
19+
"iface": 2,
20+
"config": "none"
21+
},
22+
{
23+
"iface": 3,
24+
"config": "dhcp"
25+
},
26+
{
27+
"iface": 5,
28+
"config": "dhcp-with-fallback",
29+
"address": "10.0.0.44",
30+
"netmask": "255.255.255.0",
31+
"gateway": "10.0.0.1"
32+
}
733
]
834
}

test/net/integration/configure/service.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
// limitations under the License.
1717

1818
#include <service>
19-
#include <net/configure.hpp>
2019
#include <net/super_stack.hpp>
2120
#include <config>
2221

@@ -25,14 +24,7 @@ void Service::start()
2524
using namespace net;
2625

2726
auto& stacks = Super_stack::inet().ip4_stacks();
28-
CHECKSERT(stacks.size() == 5, "There is 5 interfaces");
29-
30-
const auto& cfg = Config::get();
31-
using namespace rapidjson;
32-
Document doc;
33-
doc.Parse(cfg.data());
34-
35-
net::configure(doc["net"]);
27+
CHECKSERT(stacks.size() == 6, "There are 6 interfaces");
3628

3729
INFO("Test", "Verify eth0");
3830
CHECKSERT(stacks[0] != nullptr, "eth0 is initialized");
@@ -56,7 +48,7 @@ void Service::start()
5648
CHECKSERT(stacks[2] != nullptr, "eth2 is initialized");
5749

5850
auto& eth2 = *stacks[2];
59-
const ip4::Addr EMPTY{0};
51+
static const ip4::Addr EMPTY{0};
6052
CHECKSERT(eth2.ip_addr() == EMPTY, "IP address is 0.0.0.0");
6153
CHECKSERT(eth2.netmask() == EMPTY, "Netmask is 0.0.0.0");
6254
CHECKSERT(eth2.gateway() == EMPTY, "Gateway is 0.0.0.0");
@@ -68,5 +60,15 @@ void Service::start()
6860
INFO("Test", "Verify eth4");
6961
CHECKSERT(stacks[4] == nullptr, "eth4 is uninitialized");
7062

71-
printf("SUCCESS\n");
63+
auto& eth5 = *stacks[5];
64+
auto verify_eth5 = [](auto& eth5) {
65+
INFO("Test", "Verify eth5");
66+
CHECKSERT(eth5.ip_addr() != EMPTY, "IP not empty (%s)", eth5.ip_addr().str().c_str());
67+
CHECKSERT(eth5.netmask() != EMPTY, "Netmask not empty (%s)", eth5.netmask().str().c_str());
68+
CHECKSERT(eth5.gateway() != EMPTY, "Gateway not empty (%s)", eth5.gateway().str().c_str());
69+
CHECKSERT(eth5.dns_addr() != EMPTY, "DNS addr not empty (%s)", eth5.dns_addr().str().c_str());
70+
printf("SUCCESS\n");
71+
};
72+
eth5.on_config(verify_eth5);
73+
7274
}

test/net/integration/configure/vm.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
{"device" : "virtio", "mac" : "c0:01:0a:00:00:3a"},
66
{"device" : "virtio", "mac" : "c0:01:0a:00:00:4a"},
77
{"device" : "virtio", "mac" : "c0:01:0a:00:00:5a"},
8-
{"device" : "virtio", "mac" : "c0:01:0a:00:00:6a"}
8+
{"device" : "virtio", "mac" : "c0:01:0a:00:00:6a"},
9+
{"device" : "virtio", "mac" : "c0:01:0a:00:00:7a"}
910
],
1011
"mem" : 256
1112
}

0 commit comments

Comments
 (0)