diff --git a/src/interfaces/CMakeLists.txt b/src/interfaces/CMakeLists.txt index 349b4fd1..c24be20b 100644 --- a/src/interfaces/CMakeLists.txt +++ b/src/interfaces/CMakeLists.txt @@ -32,6 +32,7 @@ set( src/plugin/api/interfaces/read.c src/plugin/api/interfaces/store.c src/plugin/api/interfaces/change.c + src/plugin/api/interfaces/interface/load.c src/plugin/api/interfaces/interface/change.c src/plugin/api/interfaces/interface/ipv6/load.c src/plugin/api/interfaces/interface/ipv6/change.c diff --git a/src/interfaces/src/plugin/api/interfaces/interface/load.c b/src/interfaces/src/plugin/api/interfaces/interface/load.c new file mode 100644 index 00000000..109841a7 --- /dev/null +++ b/src/interfaces/src/plugin/api/interfaces/interface/load.c @@ -0,0 +1,66 @@ +#include "load.h" +#include "plugin/data/interfaces/interface.h" + +#include +#include + +int interfaces_interface_load_name(interfaces_ctx_t* ctx, interfaces_interface_hash_element_t** element, struct rtnl_link* link) +{ + int error = 0; + + const char* nl_if_name = NULL; + + nl_if_name = rtnl_link_get_name(link); + + // set element values + SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_element_set_name(element, nl_if_name), error_out); + + goto out; + +error_out: + error = -1; + +out: + return error; +} + +int interfaces_interface_load_type(interfaces_ctx_t* ctx, interfaces_interface_hash_element_t** element, struct rtnl_link* link) +{ + int error = 0; + + const char* nl_if_type = NULL; + const char* ly_if_type = NULL; + + // 2. 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); + SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_element_set_type(element, ly_if_type), error_out); + + goto out; + +error_out: + error = -1; + +out: + + return error; +} + +int interfaces_interface_load_enabled(interfaces_ctx_t* ctx, interfaces_interface_hash_element_t** element, struct rtnl_link* link) +{ + int error = 0; + + uint8_t nl_enabled = (rtnl_link_get_operstate(link) == IF_OPER_UP || rtnl_link_get_operstate(link) == IF_OPER_UNKNOWN); + + SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_element_set_enabled(element, nl_enabled), error_out); + + goto out; + +error_out: + error = -1; + +out: + + return error; +} diff --git a/src/interfaces/src/plugin/api/interfaces/interface/load.h b/src/interfaces/src/plugin/api/interfaces/interface/load.h new file mode 100644 index 00000000..0920c2af --- /dev/null +++ b/src/interfaces/src/plugin/api/interfaces/interface/load.h @@ -0,0 +1,13 @@ +#ifndef INTERFACES_PLUGIN_API_INTERFACES_INTERFACE_LOAD_H +#define INTERFACES_PLUGIN_API_INTERFACES_INTERFACE_LOAD_H + +#include "plugin/context.h" +#include "plugin/types.h" + +#include + +int interfaces_interface_load_name(interfaces_ctx_t* ctx, interfaces_interface_hash_element_t** element, struct rtnl_link* link); +int interfaces_interface_load_type(interfaces_ctx_t* ctx, interfaces_interface_hash_element_t** element, struct rtnl_link* link); +int interfaces_interface_load_enabled(interfaces_ctx_t* ctx, interfaces_interface_hash_element_t** element, struct rtnl_link* link); + +#endif // INTERFACES_PLUGIN_API_INTERFACES_INTERFACE_LOAD_H \ No newline at end of file diff --git a/src/interfaces/src/plugin/api/interfaces/load.c b/src/interfaces/src/plugin/api/interfaces/load.c index aa53c9b9..e2ee6234 100644 --- a/src/interfaces/src/plugin/api/interfaces/load.c +++ b/src/interfaces/src/plugin/api/interfaces/load.c @@ -8,6 +8,9 @@ #include "utils/memory.h" #include "utlist.h" +// load APIs +#include "interface/load.h" + #include #include #include @@ -30,18 +33,6 @@ #include -enum interfaces_load_exit_status { - interfaces_load_failure = -1, - interfaces_load_success = 0, - interfaces_load_continue = 1, -}; - -/* - Interface Data Loading (name, type, enabled etc.) -*/ - -static int interfaces_load_interface_info(struct rtnl_link* link, interfaces_interface_hash_element_t** new_element); - int interfaces_load_interface(interfaces_ctx_t* ctx, interfaces_interface_hash_element_t** if_hash) { int error = 0; @@ -65,13 +56,15 @@ int interfaces_load_interface(interfaces_ctx_t* ctx, interfaces_interface_hash_e // iterate links while (link_iter) { - // extract link info and add a new element to the interfaces hash - // allocate new element SRPC_SAFE_CALL_PTR(new_element, interfaces_interface_hash_element_new(), error_out); // load interface data - SRPC_SAFE_CALL_ERR(error, interfaces_load_interface_info(link_iter, &new_element), error_out); + SRPC_SAFE_CALL_ERR(error, interfaces_interface_load_name(ctx, &new_element, link_iter), error_out); + SRPC_SAFE_CALL_ERR(error, interfaces_interface_load_type(ctx, &new_element, link_iter), error_out); + SRPC_SAFE_CALL_ERR(error, interfaces_interface_load_enabled(ctx, &new_element, link_iter), error_out); + + // load interface IPv4 data } goto out; @@ -88,38 +81,5 @@ int interfaces_load_interface(interfaces_ctx_t* ctx, interfaces_interface_hash_e nl_cache_free(link_cache); } - return error; -} - -static int interfaces_load_interface_info(struct rtnl_link* link, interfaces_interface_hash_element_t** new_element) -{ - int error = 0; - - const char* nl_if_name = NULL; - const char* nl_if_type = NULL; - const char* ly_if_type = NULL; - uint8_t nl_enabled = 0; - - // 1. interface name - nl_if_name = rtnl_link_get_name(link); - - // 2. 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); - - // enabled - nl_enabled = (rtnl_link_get_operstate(link) == IF_OPER_UP || rtnl_link_get_operstate(link) == IF_OPER_UNKNOWN); - - // set element values - SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_element_set_name(new_element, nl_if_name), error_out); - SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_element_set_type(new_element, ly_if_type), error_out); - SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_element_set_enabled(new_element, nl_enabled), error_out); - - goto out; - -error_out: - error = -1; - -out: return error; } \ No newline at end of file