Merge pull request #83 from ifupdown-ng/feature/alias-keywords-for-iface

inherit/template system improvements
This commit is contained in:
Ariadne Conill 2020-09-24 15:24:06 -06:00 committed by GitHub
commit d177bfa553
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 61 additions and 5 deletions

View file

@ -30,7 +30,7 @@ print_interface(struct lif_interface *iface)
if (iface->is_auto) if (iface->is_auto)
printf("auto %s\n", iface->ifname); printf("auto %s\n", iface->ifname);
printf("iface %s\n", iface->ifname); printf("%s %s\n", iface->is_template ? "template" : "iface", iface->ifname);
struct lif_node *iter; struct lif_node *iter;
LIF_DICT_FOREACH(iter, &iface->vars) LIF_DICT_FOREACH(iter, &iface->vars)

View file

@ -96,6 +96,12 @@ acquire_state_lock(const char *state_path, const char *lifname)
bool bool
skip_interface(struct lif_interface *iface, const char *ifname) skip_interface(struct lif_interface *iface, const char *ifname)
{ {
if (iface->is_template)
{
fprintf(stderr, "%s: cannot change state on %s (template interface)\n", argv0, ifname);
return false;
}
if (exec_opts.force) if (exec_opts.force)
return false; return false;

View file

@ -8,3 +8,18 @@
# compatibility with legacy setups, and may be disabled for performance # compatibility with legacy setups, and may be disabled for performance
# improvements in setups where only ifupdown-ng executors are used. # improvements in setups where only ifupdown-ng executors are used.
allow_addon_scripts = 1 allow_addon_scripts = 1
# allow_any_iface_as_template:
# Enable any interface to act as a template for another interface.
# This is presently the default, but is deprecated. An admin may choose
# to disable this setting in order to require inheritance from specified
# templates.
allow_any_iface_as_template = 1
# implicit_template_conversion:
# In some legacy configs, a template may be declared as an iface, and
# ifupdown-ng automatically converts those declarations to a proper
# template. If this setting is disabled, inheritance will continue to
# work against non-template interfaces without converting them to a
# template.
implicit_template_conversion = 1

View file

@ -47,6 +47,10 @@ with an address of *203.0.113.2* and gateway of *203.0.113.1*.
associated with the declaration will be stored inside associated with the declaration will be stored inside
_object_. _object_.
*template* _object_
Begins a new declaration for _object_, like *iface*, except
that _object_ is defined as a *template*.
# SUPPORTED KEYWORDS FOR OBJECT TRIPLES # SUPPORTED KEYWORDS FOR OBJECT TRIPLES
Any keyword may be used inside an interface declaration block, but Any keyword may be used inside an interface declaration block, but
@ -79,9 +83,10 @@ the system will only respond to certain keywords by default:
Interfaces associated with the parent are taken down at Interfaces associated with the parent are taken down at
the same time as the parent. the same time as the parent.
*inherit* _interface_ *inherit* _object_
Designates that the parent interface should inherit Designates that the configured interface should inherit
configuration data from _interface_. configuration data from _object_. Normally _object_
must be a *template*.
*use* _option_ *use* _option_
Designates that an option should be used. See _OPTIONS_ Designates that an option should be used. See _OPTIONS_

View file

@ -20,6 +20,7 @@
struct lif_config_file lif_config = { struct lif_config_file lif_config = {
.allow_addon_scripts = true, .allow_addon_scripts = true,
.allow_any_iface_as_template = true,
}; };
static bool static bool
@ -43,6 +44,8 @@ set_bool_value(const char *key, const char *value, void *opaque)
static struct lif_config_handler handlers[] = { static struct lif_config_handler handlers[] = {
{"allow_addon_scripts", set_bool_value, &lif_config.allow_addon_scripts}, {"allow_addon_scripts", set_bool_value, &lif_config.allow_addon_scripts},
{"allow_any_iface_as_template", set_bool_value, &lif_config.allow_any_iface_as_template},
{"implicit_template_conversion", set_bool_value, &lif_config.implicit_template_conversion},
}; };
bool bool

View file

@ -20,6 +20,8 @@
struct lif_config_file { struct lif_config_file {
bool allow_addon_scripts; bool allow_addon_scripts;
bool allow_any_iface_as_template;
bool implicit_template_conversion;
}; };
extern struct lif_config_file lif_config; extern struct lif_config_file lif_config;

View file

@ -162,7 +162,9 @@ handle_auto(struct lif_dict *collection, const char *filename, size_t lineno, ch
return false; return false;
} }
if (!cur_iface->is_template)
cur_iface->is_auto = true; cur_iface->is_auto = true;
return true; return true;
} }
@ -236,6 +238,15 @@ handle_iface(struct lif_dict *collection, const char *filename, size_t lineno, c
return false; return false;
} }
/* mark the cur_iface as a template iface if `template` keyword
* is used.
*/
if (!strcmp(token, "template"))
{
cur_iface->is_auto = false;
cur_iface->is_template = true;
}
/* in original ifupdown config, we can have "inet loopback" /* in original ifupdown config, we can have "inet loopback"
* or "inet dhcp" or such to designate hints. lets pick up * or "inet dhcp" or such to designate hints. lets pick up
* those hints here. * those hints here.
@ -336,7 +347,9 @@ static const struct parser_keyword keywords[] = {
{"gateway", handle_gateway}, {"gateway", handle_gateway},
{"iface", handle_iface}, {"iface", handle_iface},
{"inherit", handle_inherit}, {"inherit", handle_inherit},
{"interface", handle_iface},
{"source", handle_source}, {"source", handle_source},
{"template", handle_iface},
{"use", handle_use}, {"use", handle_use},
}; };

View file

@ -16,6 +16,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "libifupdown/interface.h" #include "libifupdown/interface.h"
#include "libifupdown/config-file.h"
bool bool
lif_address_parse(struct lif_address *address, const char *presentation) lif_address_parse(struct lif_address *address, const char *presentation)
@ -239,6 +240,13 @@ lif_interface_collection_inherit(struct lif_interface *interface, struct lif_dic
if (parent == NULL) if (parent == NULL)
return false; return false;
if (!lif_config.allow_any_iface_as_template && !parent->is_template)
return false;
/* maybe convert any interface we are inheriting from into a template */
if (lif_config.implicit_template_conversion)
parent->is_template = true;
lif_dict_add(&interface->vars, "inherit", strdup(ifname)); lif_dict_add(&interface->vars, "inherit", strdup(ifname));
interface->is_bond = parent->is_bond; interface->is_bond = parent->is_bond;
interface->is_bridge = parent->is_bridge; interface->is_bridge = parent->is_bridge;

View file

@ -50,6 +50,7 @@ struct lif_interface {
bool is_auto; bool is_auto;
bool is_bridge; bool is_bridge;
bool is_bond; bool is_bond;
bool is_template;
struct lif_dict vars; struct lif_dict vars;

View file

@ -341,6 +341,9 @@ handle_dependents(const struct lif_execute_opts *opts, struct lif_interface *par
bool bool
lif_lifecycle_run(const struct lif_execute_opts *opts, struct lif_interface *iface, struct lif_dict *collection, struct lif_dict *state, const char *lifname, bool up) lif_lifecycle_run(const struct lif_execute_opts *opts, struct lif_interface *iface, struct lif_dict *collection, struct lif_dict *state, const char *lifname, bool up)
{ {
if (iface->is_template)
return false;
if (lifname == NULL) if (lifname == NULL)
lifname = iface->ifname; lifname = iface->ifname;