From 6ce174b395a999bc5fd78b6f29301bb8f0ee7ea3 Mon Sep 17 00:00:00 2001 From: Mateo Cindric Date: Mon, 31 Oct 2022 21:17:56 +0000 Subject: [PATCH] interfaces-plugin: fix load issues --- .../api/interfaces/interface/ipv4/load.c | 6 ++- .../api/interfaces/interface/ipv6/load.c | 4 +- .../plugin/api/interfaces/interface/load.c | 4 +- .../src/plugin/api/interfaces/load.c | 17 ++++++- .../src/plugin/data/interfaces/interface.c | 4 +- src/interfaces/src/plugin/startup/load.c | 49 ++++++++++++++++++- 6 files changed, 76 insertions(+), 8 deletions(-) diff --git a/src/interfaces/src/plugin/api/interfaces/interface/ipv4/load.c b/src/interfaces/src/plugin/api/interfaces/interface/ipv4/load.c index 8d1b0ff7..a0641b40 100644 --- a/src/interfaces/src/plugin/api/interfaces/interface/ipv4/load.c +++ b/src/interfaces/src/plugin/api/interfaces/interface/ipv4/load.c @@ -4,11 +4,13 @@ #include "netlink/route/neighbour.h" #include "plugin/api/interfaces/interface/ipv4/address/load.h" #include "plugin/api/interfaces/interface/ipv4/neighbor/load.h" +#include "plugin/common.h" #include "plugin/context.h" #include "plugin/data/interfaces/interface.h" #include "plugin/data/interfaces/interface/ipv4.h" #include "plugin/data/interfaces/interface/ipv4/address.h" #include "plugin/types.h" +#include "sysrepo.h" int interfaces_interface_ipv4_load_enabled(interfaces_ctx_t* ctx, interfaces_interface_ipv4_t* ipv4, struct rtnl_link* link) { @@ -91,7 +93,7 @@ int interfaces_interface_ipv4_load_address(interfaces_ctx_t* ctx, interfaces_int } // iterate - SRPC_SAFE_CALL_PTR(addr_iter, (struct rtnl_addr*)nl_cache_get_next((struct nl_object*)addr_iter), error_out); + addr_iter = (struct rtnl_addr*)nl_cache_get_next((struct nl_object*)addr_iter); } goto out; @@ -146,7 +148,7 @@ int interfaces_interface_ipv4_load_neighbor(interfaces_ctx_t* ctx, interfaces_in } // iterate - SRPC_SAFE_CALL_PTR(neigh_iter, (struct rtnl_neigh*)nl_cache_get_next((struct nl_object*)neigh_iter), error_out); + neigh_iter = (struct rtnl_neigh*)nl_cache_get_next((struct nl_object*)neigh_iter); } goto out; diff --git a/src/interfaces/src/plugin/api/interfaces/interface/ipv6/load.c b/src/interfaces/src/plugin/api/interfaces/interface/ipv6/load.c index 3fa43d73..c467fd93 100644 --- a/src/interfaces/src/plugin/api/interfaces/interface/ipv6/load.c +++ b/src/interfaces/src/plugin/api/interfaces/interface/ipv6/load.c @@ -86,7 +86,7 @@ int interfaces_interface_ipv6_load_address(interfaces_ctx_t* ctx, interfaces_int } // iterate - SRPC_SAFE_CALL_PTR(addr_iter, (struct rtnl_addr*)nl_cache_get_next((struct nl_object*)addr_iter), error_out); + addr_iter = (struct rtnl_addr*)nl_cache_get_next((struct nl_object*)addr_iter); } goto out; @@ -141,7 +141,7 @@ int interfaces_interface_ipv6_load_neighbor(interfaces_ctx_t* ctx, interfaces_in } // iterate - SRPC_SAFE_CALL_PTR(neigh_iter, (struct rtnl_neigh*)nl_cache_get_next((struct nl_object*)neigh_iter), error_out); + neigh_iter = (struct rtnl_neigh*)nl_cache_get_next((struct nl_object*)neigh_iter); } goto out; diff --git a/src/interfaces/src/plugin/api/interfaces/interface/load.c b/src/interfaces/src/plugin/api/interfaces/interface/load.c index 109841a7..d45c38c7 100644 --- a/src/interfaces/src/plugin/api/interfaces/interface/load.c +++ b/src/interfaces/src/plugin/api/interfaces/interface/load.c @@ -1,5 +1,7 @@ #include "load.h" +#include "plugin/common.h" #include "plugin/data/interfaces/interface.h" +#include "sysrepo.h" #include #include @@ -31,7 +33,7 @@ int interfaces_interface_load_type(interfaces_ctx_t* ctx, interfaces_interface_h const char* nl_if_type = NULL; const char* ly_if_type = NULL; - // 2. interface type - nl version -> convert to libyang version + // interface type - nl version -> convert to libyang version nl_if_type = rtnl_link_get_type(link); SRPC_SAFE_CALL_ERR(error, interfaces_interface_type_nl2ly(nl_if_type, &ly_if_type), error_out); diff --git a/src/interfaces/src/plugin/api/interfaces/load.c b/src/interfaces/src/plugin/api/interfaces/load.c index c010ee12..2eb825de 100644 --- a/src/interfaces/src/plugin/api/interfaces/load.c +++ b/src/interfaces/src/plugin/api/interfaces/load.c @@ -44,6 +44,8 @@ int interfaces_load_interface(interfaces_ctx_t* ctx, interfaces_interface_hash_e // temp data interfaces_interface_hash_element_t* new_element = NULL; + uint8_t element_added = 0; + struct rtnl_link* link_iter = NULL; // init hash @@ -61,6 +63,7 @@ int interfaces_load_interface(interfaces_ctx_t* ctx, interfaces_interface_hash_e while (link_iter) { // allocate new element SRPC_SAFE_CALL_PTR(new_element, interfaces_interface_hash_element_new(), error_out); + element_added = 0; // load interface data SRPC_SAFE_CALL_ERR(error, interfaces_interface_load_name(ctx, &new_element, link_iter), error_out); @@ -81,8 +84,16 @@ int interfaces_load_interface(interfaces_ctx_t* ctx, interfaces_interface_hash_e SRPC_SAFE_CALL_ERR(error, interfaces_interface_ipv4_load_address(ctx, &new_element->interface.ipv4, link_iter), error_out); SRPC_SAFE_CALL_ERR(error, interfaces_interface_ipv4_load_neighbor(ctx, &new_element->interface.ipv4, link_iter), error_out); + // load IPv6 address and neighbor lists + SRPC_SAFE_CALL_ERR(error, interfaces_interface_ipv6_load_address(ctx, &new_element->interface.ipv6, link_iter), error_out); + SRPC_SAFE_CALL_ERR(error, interfaces_interface_ipv6_load_neighbor(ctx, &new_element->interface.ipv6, link_iter), error_out); + + // add element to the hash + SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_add_element(if_hash, new_element), error_out); + element_added = 1; + // iterate - SRPC_SAFE_CALL_PTR(link_iter, (struct rtnl_link*)nl_cache_get_next((struct nl_object*)link_iter), error_out); + link_iter = (struct rtnl_link*)nl_cache_get_next((struct nl_object*)link_iter); } goto out; @@ -90,6 +101,10 @@ int interfaces_load_interface(interfaces_ctx_t* ctx, interfaces_interface_hash_e error_out: error = -1; + if (!element_added) { + interfaces_interface_hash_element_free(&new_element); + } + out: // dealloc nl_ctx data diff --git a/src/interfaces/src/plugin/data/interfaces/interface.c b/src/interfaces/src/plugin/data/interfaces/interface.c index 086376c4..8689497e 100644 --- a/src/interfaces/src/plugin/data/interfaces/interface.c +++ b/src/interfaces/src/plugin/data/interfaces/interface.c @@ -476,7 +476,9 @@ int interfaces_interface_type_nl2ly(const char* nl_type, const char** ly_type) int error = 0; if (nl_type == NULL) { - return -1; + // fix for now - investigate more - lo interface has type == NULL + *ly_type = "iana-if-type:softwareLoopback"; + return 0; } if (strcmp(nl_type, "veth") == 0) { diff --git a/src/interfaces/src/plugin/startup/load.c b/src/interfaces/src/plugin/startup/load.c index 39f96bc2..0be82f13 100644 --- a/src/interfaces/src/plugin/startup/load.c +++ b/src/interfaces/src/plugin/startup/load.c @@ -1,8 +1,12 @@ #include "load.h" #include "plugin/common.h" +#include "plugin/data/interfaces/interface.h" #include "plugin/ly_tree.h" #include "plugin/api/interfaces/load.h" +#include "plugin/types.h" +#include "src/uthash.h" +#include "src/utlist.h" #include "srpc/common.h" #include @@ -81,14 +85,57 @@ static int interfaces_startup_load_interface(void* priv, sr_session_ctx_t* sessi int error = 0; interfaces_ctx_t* ctx = (interfaces_ctx_t*)priv; interfaces_interface_hash_element_t* interface_head = NULL; + // interfaces_interface_hash_element_t *iter = NULL, *tmp = NULL; + + // interfaces_interface_ipv4_address_element_t* v4_addr_iter = NULL; + // interfaces_interface_ipv6_address_element_t* v6_addr_iter = NULL; + + // interfaces_interface_ipv4_neighbor_element_t* v4_neigh_iter = NULL; + // interfaces_interface_ipv6_neighbor_element_t* v6_neigh_iter = NULL; SRPC_SAFE_CALL_ERR(error, interfaces_load_interface(ctx, &interface_head), error_out); + // HASH_ITER(hh, interface_head, iter, tmp) + // { + // SRPLG_LOG_INF(PLUGIN_NAME, "Name: %s", iter->interface.name); + // SRPLG_LOG_INF(PLUGIN_NAME, "Type: %s", iter->interface.type); + // SRPLG_LOG_INF(PLUGIN_NAME, "Enabled: %d", iter->interface.enabled); + + // // v4 + // SRPLG_LOG_INF(PLUGIN_NAME, "v4 MTU: %d", iter->interface.ipv4.mtu); + // SRPLG_LOG_INF(PLUGIN_NAME, "v4 Enabled: %d", iter->interface.ipv4.enabled); + // SRPLG_LOG_INF(PLUGIN_NAME, "v4 Forwarding: %d", iter->interface.ipv4.forwarding); + + // LL_FOREACH(iter->interface.ipv4.address, v4_addr_iter) + // { + // SRPLG_LOG_INF(PLUGIN_NAME, "v4 Address : %s/%d", v4_addr_iter->address.ip, v4_addr_iter->address.subnet.prefix_length); + // } + // LL_FOREACH(iter->interface.ipv4.neighbor, v4_neigh_iter) + // { + // SRPLG_LOG_INF(PLUGIN_NAME, "v4 Neighbor : %s : %s", v4_neigh_iter->neighbor.ip, v4_neigh_iter->neighbor.link_layer_address); + // } + + // // v6 + // SRPLG_LOG_INF(PLUGIN_NAME, "v6 MTU: %d", iter->interface.ipv6.mtu); + // SRPLG_LOG_INF(PLUGIN_NAME, "v6 Enabled: %d", iter->interface.ipv6.enabled); + // SRPLG_LOG_INF(PLUGIN_NAME, "v6 Forwarding: %d", iter->interface.ipv6.forwarding); + + // LL_FOREACH(iter->interface.ipv6.address, v6_addr_iter) + // { + // SRPLG_LOG_INF(PLUGIN_NAME, "v6 Address : %s/%d", v6_addr_iter->address.ip, v6_addr_iter->address.prefix_length); + // } + // LL_FOREACH(iter->interface.ipv6.neighbor, v6_neigh_iter) + // { + // SRPLG_LOG_INF(PLUGIN_NAME, "v6 Neighbor : %s : %s", v6_neigh_iter->neighbor.ip, v6_neigh_iter->neighbor.link_layer_address); + // } + // } + goto out; error_out: error = -1; out: + interfaces_interface_hash_free(&interface_head); + return error; } -