1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2024-12-22 13:33:56 +03:00

networkd: introduce an AddressFamilyBoolean enum type

This introduces am AddressFamilyBoolean type that works more or less
like a booleaan, but can optionally turn on/off things for ipv4 and ipv6
independently. THis also ports the DHCP field over to it.
This commit is contained in:
Lennart Poettering 2015-01-13 19:48:19 +01:00
parent 66518acd40
commit cb9fc36a12
8 changed files with 69 additions and 46 deletions

View File

@ -220,8 +220,8 @@
<term><varname>DHCP=</varname></term> <term><varname>DHCP=</varname></term>
<listitem> <listitem>
<para>Enables DHCPv4 and/or DHCPv6 support. Accepts <para>Enables DHCPv4 and/or DHCPv6 support. Accepts
<literal>both</literal>, <literal>none</literal>, <literal>yes</literal>, <literal>no</literal>,
<literal>v4</literal> or <literal>v6</literal>.</para> <literal>ipv4</literal> or <literal>ipv6</literal>.</para>
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry> <varlistentry>

View File

@ -10,5 +10,5 @@ Virtualization=container
Name=host0 Name=host0
[Network] [Network]
DHCP=both DHCP=yes
IPv4LL=yes IPv4LL=yes

View File

@ -586,7 +586,7 @@ int dhcp4_configure(Link *link) {
assert(link); assert(link);
assert(link->network); assert(link->network);
assert(IN_SET(link->network->dhcp, DHCP_SUPPORT_BOTH, DHCP_SUPPORT_V4)); assert(IN_SET(link->network->dhcp, ADDRESS_FAMILY_YES, ADDRESS_FAMILY_IPV4));
r = sd_dhcp_client_new(&link->dhcp_client); r = sd_dhcp_client_new(&link->dhcp_client);
if (r < 0) if (r < 0)

View File

@ -42,7 +42,7 @@ static bool link_dhcp6_enabled(Link *link) {
if (!link->network) if (!link->network)
return false; return false;
return IN_SET(link->network->dhcp, DHCP_SUPPORT_V6, DHCP_SUPPORT_BOTH); return IN_SET(link->network->dhcp, ADDRESS_FAMILY_IPV6, ADDRESS_FAMILY_YES);
} }
static bool link_dhcp4_enabled(Link *link) { static bool link_dhcp4_enabled(Link *link) {
@ -52,7 +52,7 @@ static bool link_dhcp4_enabled(Link *link) {
if (!link->network) if (!link->network)
return false; return false;
return IN_SET(link->network->dhcp, DHCP_SUPPORT_V4, DHCP_SUPPORT_BOTH); return IN_SET(link->network->dhcp, ADDRESS_FAMILY_IPV4, ADDRESS_FAMILY_YES);
} }
static bool link_dhcp4_server_enabled(Link *link) { static bool link_dhcp4_server_enabled(Link *link) {
@ -1331,7 +1331,7 @@ static int link_initialized_and_synced(sd_rtnl *rtnl, sd_rtnl_message *m,
if (network->ipv4ll) if (network->ipv4ll)
log_link_debug(link, "ignoring IPv4LL for loopback link"); log_link_debug(link, "ignoring IPv4LL for loopback link");
if (network->dhcp != DHCP_SUPPORT_NONE) if (network->dhcp != ADDRESS_FAMILY_NO)
log_link_debug(link, "ignoring DHCP clients for loopback link"); log_link_debug(link, "ignoring DHCP clients for loopback link");
if (network->dhcp_server) if (network->dhcp_server)

View File

@ -654,3 +654,35 @@ int manager_address_pool_acquire(Manager *m, int family, unsigned prefixlen, uni
return 0; return 0;
} }
const char *address_family_boolean_to_string(AddressFamilyBoolean b) {
if (b == ADDRESS_FAMILY_YES ||
b == ADDRESS_FAMILY_NO)
return yes_no(b == ADDRESS_FAMILY_YES);
if (b == ADDRESS_FAMILY_IPV4)
return "ipv4";
if (b == ADDRESS_FAMILY_IPV6)
return "ipv6";
return NULL;
}
AddressFamilyBoolean address_family_boolean_from_string(const char *s) {
int r;
/* Make this a true superset of a boolean */
r = parse_boolean(s);
if (r > 0)
return ADDRESS_FAMILY_YES;
if (r == 0)
return ADDRESS_FAMILY_NO;
if (streq(s, "ipv4"))
return ADDRESS_FAMILY_IPV4;
if (streq(s, "ipv6"))
return ADDRESS_FAMILY_IPV6;
return _ADDRESS_FAMILY_BOOLEAN_INVALID;
}

View File

@ -84,7 +84,7 @@ static int network_load_one(Manager *manager, const char *filename) {
if (!network->filename) if (!network->filename)
return log_oom(); return log_oom();
network->dhcp = DHCP_SUPPORT_NONE; network->dhcp = ADDRESS_FAMILY_NO;
network->dhcp_ntp = true; network->dhcp_ntp = true;
network->dhcp_dns = true; network->dhcp_dns = true;
network->dhcp_hostname = true; network->dhcp_hostname = true;
@ -470,15 +470,6 @@ int config_parse_tunnel(const char *unit,
return 0; return 0;
} }
static const char* const dhcp_support_table[_DHCP_SUPPORT_MAX] = {
[DHCP_SUPPORT_NONE] = "none",
[DHCP_SUPPORT_BOTH] = "both",
[DHCP_SUPPORT_V4] = "v4",
[DHCP_SUPPORT_V6] = "v6",
};
DEFINE_STRING_TABLE_LOOKUP(dhcp_support, DHCPSupport);
int config_parse_dhcp( int config_parse_dhcp(
const char* unit, const char* unit,
const char *filename, const char *filename,
@ -491,34 +482,34 @@ int config_parse_dhcp(
void *data, void *data,
void *userdata) { void *userdata) {
DHCPSupport *dhcp = data; AddressFamilyBoolean *dhcp = data, s;
int k;
assert(filename); assert(filename);
assert(lvalue); assert(lvalue);
assert(rvalue); assert(rvalue);
assert(data); assert(data);
/* Our enum shall be a superset of booleans, hence first try s = address_family_boolean_from_string(rvalue);
* to parse as boolean, and then as enum */ if (s < 0) {
k = parse_boolean(rvalue); /* Previously, we had a slightly different enum here,
if (k > 0) * support its values for compatbility. */
*dhcp = DHCP_SUPPORT_BOTH;
else if (k == 0)
*dhcp = DHCP_SUPPORT_NONE;
else {
DHCPSupport s;
s = dhcp_support_from_string(rvalue); if (streq(rvalue, "none"))
if (s < 0){ s = ADDRESS_FAMILY_NO;
log_syntax(unit, LOG_ERR, filename, line, -s, "Failed to parse DHCP option, ignoring: %s", rvalue); else if (streq(rvalue, "v4"))
s = ADDRESS_FAMILY_IPV4;
else if (streq(rvalue, "v6"))
s = ADDRESS_FAMILY_IPV6;
else if (streq(rvalue, "both"))
s = ADDRESS_FAMILY_YES;
else {
log_syntax(unit, LOG_ERR, filename, line, s, "Failed to parse DHCP option, ignoring: %s", rvalue);
return 0; return 0;
} }
*dhcp = s;
} }
*dhcp = s;
return 0; return 0;
} }

View File

@ -54,14 +54,14 @@ typedef struct Manager Manager;
typedef struct AddressPool AddressPool; typedef struct AddressPool AddressPool;
typedef struct FdbEntry FdbEntry; typedef struct FdbEntry FdbEntry;
typedef enum DHCPSupport { typedef enum AddressFamilyBoolean {
DHCP_SUPPORT_NONE, ADDRESS_FAMILY_NO,
DHCP_SUPPORT_BOTH, ADDRESS_FAMILY_YES,
DHCP_SUPPORT_V4, ADDRESS_FAMILY_IPV4,
DHCP_SUPPORT_V6, ADDRESS_FAMILY_IPV6,
_DHCP_SUPPORT_MAX, _ADDRESS_FAMILY_BOOLEAN_MAX,
_DHCP_SUPPORT_INVALID = -1, _ADDRESS_FAMILY_BOOLEAN_INVALID = -1,
} DHCPSupport; } AddressFamilyBoolean;
typedef enum LLMNRSupport { typedef enum LLMNRSupport {
LLMNR_SUPPORT_NO, LLMNR_SUPPORT_NO,
@ -102,7 +102,7 @@ struct Network {
NetDev *bridge; NetDev *bridge;
NetDev *bond; NetDev *bond;
Hashmap *stacked_netdevs; Hashmap *stacked_netdevs;
DHCPSupport dhcp; AddressFamilyBoolean dhcp;
bool dhcp_dns; bool dhcp_dns;
bool dhcp_ntp; bool dhcp_ntp;
bool dhcp_mtu; bool dhcp_mtu;
@ -368,9 +368,6 @@ int config_parse_fdb_vlan_id(const char *unit, const char *filename, unsigned li
/* DHCP support */ /* DHCP support */
const char* dhcp_support_to_string(DHCPSupport i) _const_;
DHCPSupport dhcp_support_from_string(const char *s) _pure_;
int config_parse_dhcp(const char *unit, const char *filename, unsigned line, int config_parse_dhcp(const char *unit, const char *filename, unsigned line,
const char *section, unsigned section_line, const char *lvalue, const char *section, unsigned section_line, const char *lvalue,
int ltype, const char *rvalue, void *data, void *userdata); int ltype, const char *rvalue, void *data, void *userdata);
@ -391,3 +388,6 @@ int address_pool_new_from_string(Manager *m, AddressPool **ret, int family, cons
void address_pool_free(AddressPool *p); void address_pool_free(AddressPool *p);
int address_pool_acquire(AddressPool *p, unsigned prefixlen, union in_addr_union *found); int address_pool_acquire(AddressPool *p, unsigned prefixlen, union in_addr_union *found);
const char *address_family_boolean_to_string(AddressFamilyBoolean b) _const_;
AddressFamilyBoolean address_family_boolean_from_string(const char *s) _const_;

View File

@ -13,7 +13,7 @@ int main(int argc, char **argv) {
test_table(bond_mode, NETDEV_BOND_MODE); test_table(bond_mode, NETDEV_BOND_MODE);
/* test_table(link_state, LINK_STATE); -- not a reversible mapping */ /* test_table(link_state, LINK_STATE); -- not a reversible mapping */
test_table(link_operstate, LINK_OPERSTATE); test_table(link_operstate, LINK_OPERSTATE);
test_table(dhcp_support, DHCP_SUPPORT); test_table(address_family_boolean, ADDRESS_FAMILY_BOOLEAN);
test_table(netdev_kind, NETDEV_KIND); test_table(netdev_kind, NETDEV_KIND);
test_table(dhcp6_message_status, DHCP6_STATUS); test_table(dhcp6_message_status, DHCP6_STATUS);
test_table(duplex, DUP); test_table(duplex, DUP);