diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2021-07-14 09:50:26 +0200 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2021-09-15 09:50:23 +0200 |
commit | a093533c33e1d1e497765a505f12de8cee47171e (patch) | |
tree | 82285e7498cd586d249c143282537bf12010cbe7 | |
parent | Respect install_sysconfdir (diff) | |
download | systemd-a093533c33e1d1e497765a505f12de8cee47171e.tar.xz systemd-a093533c33e1d1e497765a505f12de8cee47171e.zip |
network: introduce NetworkConfigSource and NetworkConfigState
These will be used in later commits.
-rw-r--r-- | src/network/networkd-util.c | 52 | ||||
-rw-r--r-- | src/network/networkd-util.h | 97 |
2 files changed, 142 insertions, 7 deletions
diff --git a/src/network/networkd-util.c b/src/network/networkd-util.c index d7906e0cb5..7ac34c18f3 100644 --- a/src/network/networkd-util.c +++ b/src/network/networkd-util.c @@ -10,46 +10,84 @@ #include "string-util.h" #include "web-util.h" -static const char* const address_family_table[_ADDRESS_FAMILY_MAX] = { +/* This is used in log messages, and never used in parsing settings. So, upper cases are OK. */ +static const char * const network_config_source_table[_NETWORK_CONFIG_SOURCE_MAX] = { + [NETWORK_CONFIG_SOURCE_FOREIGN] = "foreign", + [NETWORK_CONFIG_SOURCE_STATIC] = "static", + [NETWORK_CONFIG_SOURCE_IPV4LL] = "IPv4LL", + [NETWORK_CONFIG_SOURCE_DHCP4] = "DHCPv4", + [NETWORK_CONFIG_SOURCE_DHCP6] = "DHCPv6", + [NETWORK_CONFIG_SOURCE_DHCP6PD] = "DHCPv6-PD", + [NETWORK_CONFIG_SOURCE_NDISC] = "NDisc", +}; + +DEFINE_STRING_TABLE_LOOKUP_TO_STRING(network_config_source, NetworkConfigSource); + +int network_config_state_to_string_alloc(NetworkConfigState s, char **ret) { + static const struct { + NetworkConfigState state; + const char *str; + } map[] = { + { .state = NETWORK_CONFIG_STATE_PROBING, .str = "probing", }, + { .state = NETWORK_CONFIG_STATE_REQUESTING, .str = "requesting", }, + { .state = NETWORK_CONFIG_STATE_CONFIGURING, .str = "configuring", }, + { .state = NETWORK_CONFIG_STATE_CONFIGURED, .str = "configured", }, + { .state = NETWORK_CONFIG_STATE_MARKED, .str = "marked", }, + { .state = NETWORK_CONFIG_STATE_REMOVING, .str = "removing", }, + }; + _cleanup_free_ char *buf = NULL; + + assert(ret); + + for (size_t i = 0; i < ELEMENTSOF(map); i++) + if (FLAGS_SET(s, map[i].state) && + !strextend_with_separator(&buf, ",", map[i].str)) + return -ENOMEM; + + *ret = TAKE_PTR(buf); + return 0; +} + +static const char * const address_family_table[_ADDRESS_FAMILY_MAX] = { [ADDRESS_FAMILY_NO] = "no", [ADDRESS_FAMILY_YES] = "yes", [ADDRESS_FAMILY_IPV4] = "ipv4", [ADDRESS_FAMILY_IPV6] = "ipv6", }; -static const char* const routing_policy_rule_address_family_table[_ADDRESS_FAMILY_MAX] = { +static const char * const routing_policy_rule_address_family_table[_ADDRESS_FAMILY_MAX] = { [ADDRESS_FAMILY_YES] = "both", [ADDRESS_FAMILY_IPV4] = "ipv4", [ADDRESS_FAMILY_IPV6] = "ipv6", }; -static const char* const nexthop_address_family_table[_ADDRESS_FAMILY_MAX] = { +static const char * const nexthop_address_family_table[_ADDRESS_FAMILY_MAX] = { [ADDRESS_FAMILY_IPV4] = "ipv4", [ADDRESS_FAMILY_IPV6] = "ipv6", }; -static const char* const duplicate_address_detection_address_family_table[_ADDRESS_FAMILY_MAX] = { +static const char * const duplicate_address_detection_address_family_table[_ADDRESS_FAMILY_MAX] = { [ADDRESS_FAMILY_NO] = "none", [ADDRESS_FAMILY_YES] = "both", [ADDRESS_FAMILY_IPV4] = "ipv4", [ADDRESS_FAMILY_IPV6] = "ipv6", }; -static const char* const dhcp_deprecated_address_family_table[_ADDRESS_FAMILY_MAX] = { +static const char * const dhcp_deprecated_address_family_table[_ADDRESS_FAMILY_MAX] = { [ADDRESS_FAMILY_NO] = "none", [ADDRESS_FAMILY_YES] = "both", [ADDRESS_FAMILY_IPV4] = "v4", [ADDRESS_FAMILY_IPV6] = "v6", }; -static const char* const ip_masquerade_address_family_table[_ADDRESS_FAMILY_MAX] = { +static const char * const ip_masquerade_address_family_table[_ADDRESS_FAMILY_MAX] = { [ADDRESS_FAMILY_NO] = "no", [ADDRESS_FAMILY_YES] = "both", [ADDRESS_FAMILY_IPV4] = "ipv4", [ADDRESS_FAMILY_IPV6] = "ipv6", }; -static const char* const dhcp_lease_server_type_table[_SD_DHCP_LEASE_SERVER_TYPE_MAX] = { +static const char * const dhcp_lease_server_type_table[_SD_DHCP_LEASE_SERVER_TYPE_MAX] = { [SD_DHCP_LEASE_DNS] = "DNS servers", [SD_DHCP_LEASE_NTP] = "NTP servers", [SD_DHCP_LEASE_SIP] = "SIP servers", diff --git a/src/network/networkd-util.h b/src/network/networkd-util.h index 997bcee4b4..39463cadbd 100644 --- a/src/network/networkd-util.h +++ b/src/network/networkd-util.h @@ -19,11 +19,108 @@ typedef struct NetworkConfigSection { char filename[]; } NetworkConfigSection; +typedef enum NetworkConfigSource { + NETWORK_CONFIG_SOURCE_FOREIGN, /* configured by kernel */ + NETWORK_CONFIG_SOURCE_STATIC, + NETWORK_CONFIG_SOURCE_IPV4LL, + NETWORK_CONFIG_SOURCE_DHCP4, + NETWORK_CONFIG_SOURCE_DHCP6, + NETWORK_CONFIG_SOURCE_DHCP6PD, + NETWORK_CONFIG_SOURCE_NDISC, + _NETWORK_CONFIG_SOURCE_MAX, + _NETWORK_CONFIG_SOURCE_INVALID = -EINVAL, +} NetworkConfigSource; + +typedef enum NetworkConfigState { + NETWORK_CONFIG_STATE_PROBING = 1 << 0, /* address is probing by IPv4ACD */ + NETWORK_CONFIG_STATE_REQUESTING = 1 << 1, /* request is queued */ + NETWORK_CONFIG_STATE_CONFIGURING = 1 << 2, /* e.g. address_configure() is called, but no responce is received yet */ + NETWORK_CONFIG_STATE_CONFIGURED = 1 << 3, /* e.g. address_configure() is called and received a response from kernel. + * Note that address may not be ready yet, so please use address_is_ready() + * to check whether the address can be usable or not. */ + NETWORK_CONFIG_STATE_MARKED = 1 << 4, /* used GC'ing the old config */ + NETWORK_CONFIG_STATE_REMOVING = 1 << 5, /* e.g. address_remove() is called, but no responce is received yet */ +} NetworkConfigState; + CONFIG_PARSER_PROTOTYPE(config_parse_link_local_address_family); CONFIG_PARSER_PROTOTYPE(config_parse_address_family_with_kernel); CONFIG_PARSER_PROTOTYPE(config_parse_ip_masquerade); CONFIG_PARSER_PROTOTYPE(config_parse_mud_url); +const char *network_config_source_to_string(NetworkConfigSource s) _const_; + +int network_config_state_to_string_alloc(NetworkConfigState s, char **ret); + +#define DEFINE_NETWORK_CONFIG_STATE_FUNCTIONS(type, name) \ + static inline void name##_update_state( \ + type *t, \ + NetworkConfigState mask, \ + NetworkConfigState value) { \ + \ + assert(t); \ + \ + t->state = (t->state & ~mask) | (value & mask); \ + } \ + static inline bool name##_exists(type *t) { \ + assert(t); \ + \ + if ((t->state & (NETWORK_CONFIG_STATE_CONFIGURING | \ + NETWORK_CONFIG_STATE_CONFIGURED)) == 0) \ + return false; /* Not assigned yet. */ \ + if (FLAGS_SET(t->state, NETWORK_CONFIG_STATE_REMOVING)) \ + return false; /* Already removing. */ \ + return true; \ + } \ + static inline void name##_enter_requesting(type *t) { \ + name##_update_state(t, \ + NETWORK_CONFIG_STATE_REQUESTING, \ + NETWORK_CONFIG_STATE_REQUESTING); \ + } \ + static inline void name##_cancel_requesting(type *t) { \ + name##_update_state(t, \ + NETWORK_CONFIG_STATE_REQUESTING, \ + 0); \ + } \ + static inline bool name##_is_requesting(type *t) { \ + return FLAGS_SET(t->state, NETWORK_CONFIG_STATE_REQUESTING); \ + } \ + static inline void name##_enter_configuring(type *t) { \ + name##_update_state(t, \ + NETWORK_CONFIG_STATE_REQUESTING | \ + NETWORK_CONFIG_STATE_CONFIGURING, \ + NETWORK_CONFIG_STATE_CONFIGURING); \ + } \ + static inline void name##_enter_configured(type *t) { \ + name##_update_state(t, \ + NETWORK_CONFIG_STATE_CONFIGURING | \ + NETWORK_CONFIG_STATE_CONFIGURED, \ + NETWORK_CONFIG_STATE_CONFIGURED); \ + } \ + static inline void name##_mark(type *t) { \ + name##_update_state(t, \ + NETWORK_CONFIG_STATE_MARKED, \ + NETWORK_CONFIG_STATE_MARKED); \ + } \ + static inline void name##_unmark(type *t) { \ + name##_update_state(t, NETWORK_CONFIG_STATE_MARKED, 0); \ + } \ + static inline bool name##_is_marked(type *t) { \ + assert(t); \ + return FLAGS_SET(t->state, NETWORK_CONFIG_STATE_MARKED); \ + } \ + static inline void name##_enter_removing(type *t) { \ + name##_update_state(t, \ + NETWORK_CONFIG_STATE_MARKED | \ + NETWORK_CONFIG_STATE_REMOVING, \ + NETWORK_CONFIG_STATE_REMOVING); \ + } \ + static inline void name##_enter_removed(type *t) { \ + name##_update_state(t, \ + NETWORK_CONFIG_STATE_CONFIGURED | \ + NETWORK_CONFIG_STATE_REMOVING, \ + 0); \ + } + const char *address_family_to_string(AddressFamily b) _const_; AddressFamily address_family_from_string(const char *s) _pure_; |