Skip to content

Commit

Permalink
Merge pull request #308 from pwieczorkiewicz/nanny-mix-2
Browse files Browse the repository at this point in the history
  • Loading branch information
mtomaschewski committed Jul 10, 2014
2 parents e5aa1f5 + 68ffd4e commit 44d711a
Show file tree
Hide file tree
Showing 11 changed files with 246 additions and 212 deletions.
4 changes: 2 additions & 2 deletions client/ifreload.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ ni_do_ifreload(int argc, char **argv)
}

/* Build the up tree */
if (ni_fsm_build_hierarchy(fsm) < 0) {
if (ni_fsm_build_hierarchy(fsm, TRUE) < 0) {
ni_error("ifreload: unable to build device hierarchy");
/* Severe error we always explicitly return */
status = NI_WICKED_RC_ERROR;
Expand Down Expand Up @@ -292,7 +292,7 @@ ni_do_ifreload(int argc, char **argv)
if (marked.count) {
/* And trigger up */
ni_debug_application("Reloading all changed devices");
ni_ifup_pull_in_children(&marked);
ni_fsm_pull_in_children(&marked);
if (ni_fsm_start_matching_workers(fsm, &marked)) {
/* Execute the up run */
if (ni_fsm_schedule(fsm) != 0)
Expand Down
2 changes: 0 additions & 2 deletions client/ifreload.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,4 @@

extern int ni_do_ifreload(int argc, char **argv);

extern void ni_ifup_pull_in_children(ni_ifworker_array_t *);

#endif /* __WICKED_CLIENT_IFRELOAD_H__ */
1 change: 0 additions & 1 deletion client/ifstatus.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,5 @@
#define __WICKED_CLIENT_IFSTATUS_H__

extern int ni_do_ifstatus(int argc, char **argv);
extern void ni_ifup_pull_in_children(ni_ifworker_array_t *);

#endif /* __WICKED_CLIENT_IFSTATUS_H__ */
108 changes: 42 additions & 66 deletions client/ifup.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,35 +45,6 @@
#include "appconfig.h"
#include "ifup.h"

static void
__ni_ifup_pull_in_children(ni_ifworker_t *w, ni_ifworker_array_t *array)
{
unsigned int i;

for (i = 0; i < w->children.count; i++) {
ni_ifworker_t *child = w->children.data[i];

if (ni_ifworker_array_index(array, child) < 0)
ni_ifworker_array_append(array, child);
__ni_ifup_pull_in_children(child, array);
}
}

void
ni_ifup_pull_in_children(ni_ifworker_array_t *array)
{
unsigned int i;

if (!array)
return;

for (i = 0; i < array->count; i++) {
ni_ifworker_t *w = array->data[i];

__ni_ifup_pull_in_children(w, array);
}
}

static xml_node_t *
__ni_ifup_generate_match_type_dev(ni_netdev_t *dev)
{
Expand Down Expand Up @@ -107,6 +78,7 @@ __ni_ifup_generate_match_dev(xml_node_t *node, ni_ifworker_t *w)
* (dev is probably a not ready one just using our name),
* but this info is lost in translation... isn't it?
*/
#if 0 /* Unsupported - new condition to be introduced to match agains device slave and its link-type */
if (w->device && ni_string_eq(w->name, w->device->name)) {
xml_node_t * ret = NULL;

Expand All @@ -115,6 +87,7 @@ __ni_ifup_generate_match_dev(xml_node_t *node, ni_ifworker_t *w)
return ret;
}
}
#endif
return xml_node_new_element(NI_NANNY_IFPOLICY_MATCH_DEV, node, w->name);
}

Expand All @@ -127,14 +100,14 @@ __ni_ifup_generate_match(ni_ifworker_t *w)
if (!(match = xml_node_new(NI_NANNY_IFPOLICY_MATCH, NULL)))
return NULL;

if (!__ni_ifup_generate_match_dev(match, w)) {
xml_node_free(match);
return NULL;
}

if (w->children.count) {
op = xml_node_new(NI_NANNY_IFPOLICY_MATCH_COND_OR, match);

if (!__ni_ifup_generate_match_dev(op, w)) {
xml_node_free(match);
return NULL;
}

for (i = 0; i < w->children.count; i++) {
ni_ifworker_t *child = w->children.data[i];

Expand All @@ -144,21 +117,15 @@ __ni_ifup_generate_match(ni_ifworker_t *w)
}
}

} else {
if (!__ni_ifup_generate_match_dev(match, w)) {
xml_node_free(match);
return NULL;
}
}

return match;
}

static ni_bool_t
ni_ifup_hire_nanny(ni_ifworker_t *w)
ni_ifup_start_policy(ni_ifworker_t *w)
{
xml_node_t *ifcfg = NULL, *policy = NULL;
ni_netdev_t *dev;
unsigned int i;
ni_bool_t rv = FALSE;
char *pname;

Expand Down Expand Up @@ -203,32 +170,15 @@ ni_ifup_hire_nanny(ni_ifworker_t *w)
goto error;
#endif

dev = w->device;
if (dev) {
ni_debug_application("%s: enabling device for nanny", w->name);
if (!ni_nanny_call_device_enable(w->name))
goto error;
}

ni_debug_application("%s: adding policy %s to nanny", w->name,
xml_node_get_attr(policy, NI_NANNY_IFPOLICY_NAME));

if (ni_nanny_addpolicy_node(policy, w->config.origin) <= 0) {
ni_nanny_call_device_disable(w->name);
if (ni_nanny_addpolicy_node(policy, w->config.origin) <= 0)
goto error;
}

ni_debug_application("%s: nanny hired!", w->name);
ni_ifworker_success(w);

/* Append policies for all children in case they contain some special options */
for (i = 0; i < w->children.count; i++) {
ni_ifworker_t *child = w->children.data[i];

if (!ni_ifup_hire_nanny(child))
ni_error("%s: unable to apply configuration to nanny", child->name);
}

rv = TRUE;

error:
Expand All @@ -239,25 +189,48 @@ ni_ifup_hire_nanny(ni_ifworker_t *w)
}

static ni_bool_t
ni_ifup_start_policies(ni_ifworker_array_t *array, ni_bool_t set_persistent)
ni_ifup_hire_nanny(ni_ifworker_array_t *array, ni_bool_t set_persistent)
{
unsigned int i;
ni_bool_t rv = TRUE;

/* Send policies to nanny */
for (i = 0; i < array->count; i++) {
ni_ifworker_t *w = array->data[i];

if (set_persistent)
ni_client_state_set_persistent(w->config.node);

if (!ni_ifup_hire_nanny(w)) {
if (!ni_ifup_start_policy(w)) {
ni_error("%s: unable to apply configuration to nanny", w->name);
rv = FALSE;
}
else
ni_info("%s: configuration applied to nanny", w->name);
}

/* Enable devices with policies */
for (i = 0; i < array->count; i++) {
ni_ifworker_t *w = array->data[i];
ni_netdev_t *dev = w->device;

/* Ignore non-existing device */
if (!dev)
continue;

if (w->failed) {
ni_debug_application("%s: disabling failed device for nanny", w->name);
ni_nanny_call_device_disable(w->name);
}
else {
ni_debug_application("%s: enabling device for nanny", w->name);
if (!ni_nanny_call_device_enable(w->name)) {
ni_error("%s: unable to enable device", w->name);
rv = FALSE;
}
}
}

if (0 == array->count)
printf("ifup: no matching interfaces\n");

Expand Down Expand Up @@ -528,7 +501,7 @@ ni_do_ifup_nanny(int argc, char **argv)
if (ni_wait_for_interfaces)
fsm->worker_timeout = ni_wait_for_interfaces;

if (ni_fsm_build_hierarchy(fsm) < 0) {
if (ni_fsm_build_hierarchy(fsm, TRUE) < 0) {
ni_error("ifup: unable to build device hierarchy");
/* Severe error we always explicitly return */
status = NI_WICKED_RC_ERROR;
Expand All @@ -549,7 +522,10 @@ ni_do_ifup_nanny(int argc, char **argv)
ni_fsm_get_matching_workers(fsm, &ifmatch, &ifmarked);
}

if (!ni_ifup_start_policies(&ifmarked, set_persistent))
ni_fsm_pull_in_children(&ifmarked);
ni_ifworkers_flatten(&ifmarked);

if (!ni_ifup_hire_nanny(&ifmarked, set_persistent))
status = NI_WICKED_RC_NOT_CONFIGURED;

/* Wait for device-up events */
Expand Down Expand Up @@ -779,7 +755,7 @@ ni_do_ifup_direct(int argc, char **argv)
else
ni_wait_for_interfaces *= 1000; /* in msec */

if (ni_fsm_build_hierarchy(fsm) < 0) {
if (ni_fsm_build_hierarchy(fsm, TRUE) < 0) {
ni_error("ifup: unable to build device hierarchy");
/* Severe error we always explicitly return */
status = NI_WICKED_RC_ERROR;
Expand All @@ -799,7 +775,7 @@ ni_do_ifup_direct(int argc, char **argv)
ni_fsm_get_matching_workers(fsm, &ifmatch, &ifmarked);
}

ni_ifup_pull_in_children(&ifmarked);
ni_fsm_pull_in_children(&ifmarked);

/* Mark and start selected workers */
if (ifmarked.count)
Expand Down
11 changes: 10 additions & 1 deletion include/wicked/fsm.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ typedef struct ni_ifworker_array {
unsigned int count;
ni_ifworker_t ** data;
} ni_ifworker_array_t;
#define NI_IFWORKER_ARRAY_INIT { .count = 0, .data = NULL }

typedef struct ni_fsm_transition ni_fsm_transition_t;

Expand Down Expand Up @@ -256,6 +257,7 @@ extern ni_fsm_policy_t * ni_fsm_policy_by_name(ni_fsm_t *, const char *);
extern ni_bool_t ni_fsm_policy_remove(ni_fsm_t *, ni_fsm_policy_t *);
extern unsigned int ni_fsm_policy_get_applicable_policies(ni_fsm_t *, ni_ifworker_t *,
const ni_fsm_policy_t **, unsigned int);
extern ni_bool_t ni_fsm_exists_applicable_policy(ni_fsm_t *, ni_ifworker_t *);
extern xml_node_t * ni_fsm_policy_transform_document(xml_node_t *, const ni_fsm_policy_t * const *, unsigned int);
extern const char * ni_fsm_policy_name(const ni_fsm_policy_t *);
extern xml_location_t * ni_fsm_policy_location(const ni_fsm_policy_t *);
Expand All @@ -271,7 +273,7 @@ extern unsigned int ni_fsm_get_matching_workers(ni_fsm_t *, ni_ifmatcher_t *, n
extern unsigned int ni_fsm_mark_matching_workers(ni_fsm_t *, ni_ifworker_array_t *, const ni_ifmarker_t *);
extern unsigned int ni_fsm_start_matching_workers(ni_fsm_t *, ni_ifworker_array_t *);
extern void ni_fsm_reset_matching_workers(ni_fsm_t *, ni_ifworker_array_t *, const ni_uint_range_t *, ni_bool_t);
extern int ni_fsm_build_hierarchy(ni_fsm_t *);
extern int ni_fsm_build_hierarchy(ni_fsm_t *, ni_bool_t);
extern ni_bool_t ni_fsm_workers_from_xml(ni_fsm_t *, xml_node_t *, const char *);
extern unsigned int ni_fsm_fail_count(ni_fsm_t *);
extern ni_ifworker_t * ni_fsm_ifworker_by_object_path(ni_fsm_t *, const char *);
Expand All @@ -284,6 +286,7 @@ extern ni_ifworker_t * ni_fsm_recv_new_modem(ni_fsm_t *fsm, ni_dbus_object_t *o
extern ni_ifworker_t * ni_fsm_recv_new_modem_path(ni_fsm_t *fsm, const char *path);
extern ni_bool_t ni_fsm_destroy_worker(ni_fsm_t *fsm, ni_ifworker_t *w);
extern void ni_ifworkers_flatten(ni_ifworker_array_t *);
extern void ni_fsm_pull_in_children(ni_ifworker_array_t *);

extern ni_ifworker_type_t ni_ifworker_type_from_string(const char *);
extern const char * ni_ifworker_type_to_string(ni_ifworker_type_t);
Expand Down Expand Up @@ -414,4 +417,10 @@ ni_ifworker_is_factory_device(ni_ifworker_t *w)
w->device_api.factory_method);
}

static inline ni_bool_t
ni_ifworker_can_delete(const ni_ifworker_t *w)
{
return !!ni_dbus_object_get_service_for_method(w->object, "deleteDevice");
}

#endif /* __CLIENT_FSM_H__ */
Loading

0 comments on commit 44d711a

Please sign in to comment.