diff --git a/src/interfaces/src/plugin.c b/src/interfaces/src/plugin.c index 8d5da0fa..3e776bf4 100644 --- a/src/interfaces/src/plugin.c +++ b/src/interfaces/src/plugin.c @@ -282,7 +282,7 @@ int sr_plugin_init_cb(sr_session_ctx_t* running_session, void** private_data) // start a session SRPC_SAFE_CALL_ERR(error, sr_session_start(connection, SR_DS_STARTUP, &startup_session), error_out); - ctx->startup_session = startup_session; + ctx->startup_ctx.startup_session = startup_session; SRPC_SAFE_CALL_ERR(error, srpc_check_empty_datastore(startup_session, INTERFACES_INTERFACES_INTERFACE_YANG_PATH, &empty_startup), error_out); 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 ed6c5ba8..efbce678 100644 --- a/src/interfaces/src/plugin/api/interfaces/interface/ipv4/load.c +++ b/src/interfaces/src/plugin/api/interfaces/interface/ipv4/load.c @@ -46,3 +46,15 @@ int interfaces_interface_ipv4_load_mtu(interfaces_ctx_t* ctx, interfaces_interfa return error; } + +int interfaces_interface_ipv4_load_address(interfaces_ctx_t* ctx, interfaces_interface_ipv4_t* ipv4, struct rtnl_link* link) +{ + int error = 0; + return error; +} + +int interfaces_interface_ipv4_load_neighbor(interfaces_ctx_t* ctx, interfaces_interface_ipv4_t* ipv4, struct rtnl_link* link) +{ + int error = 0; + return error; +} diff --git a/src/interfaces/src/plugin/api/interfaces/interface/ipv4/load.h b/src/interfaces/src/plugin/api/interfaces/interface/ipv4/load.h index 6adeff9f..d8bbd4b2 100644 --- a/src/interfaces/src/plugin/api/interfaces/interface/ipv4/load.h +++ b/src/interfaces/src/plugin/api/interfaces/interface/ipv4/load.h @@ -9,5 +9,7 @@ int interfaces_interface_ipv4_load_enabled(interfaces_ctx_t* ctx, interfaces_interface_ipv4_t* ipv4, struct rtnl_link* link); int interfaces_interface_ipv4_load_forwarding(interfaces_ctx_t* ctx, interfaces_interface_ipv4_t* ipv4, struct rtnl_link* link); int interfaces_interface_ipv4_load_mtu(interfaces_ctx_t* ctx, interfaces_interface_ipv4_t* ipv4, struct rtnl_link* link); +int interfaces_interface_ipv4_load_address(interfaces_ctx_t* ctx, interfaces_interface_ipv4_t* ipv4, struct rtnl_link* link); +int interfaces_interface_ipv4_load_neighbor(interfaces_ctx_t* ctx, interfaces_interface_ipv4_t* ipv4, struct rtnl_link* link); #endif // INTERFACES_PLUGIN_API_INTERFACES_INTERFACE_IPV4_LOAD_H diff --git a/src/interfaces/src/plugin/api/interfaces/load.c b/src/interfaces/src/plugin/api/interfaces/load.c index e2ee6234..aba97c39 100644 --- a/src/interfaces/src/plugin/api/interfaces/load.c +++ b/src/interfaces/src/plugin/api/interfaces/load.c @@ -2,6 +2,7 @@ #include "interface/ipv4/load.h" #include "interface/ipv6/load.h" #include "plugin/common.h" +#include "plugin/context.h" #include "plugin/data/interfaces/interface.h" #include "plugin/types.h" #include "read.h" @@ -36,23 +37,25 @@ int interfaces_load_interface(interfaces_ctx_t* ctx, interfaces_interface_hash_element_t** if_hash) { int error = 0; - struct nl_sock* socket = NULL; - struct nl_cache* link_cache = NULL; - struct rtnl_link* link_iter = NULL; + + // ctx + interfaces_startup_ctx_t* startup_ctx = &ctx->startup_ctx; + interfaces_nl_ctx_t* nl_ctx = &startup_ctx->nl_ctx; // temp data interfaces_interface_hash_element_t* new_element = NULL; + struct rtnl_link* link_iter = NULL; // init hash *if_hash = interfaces_interface_hash_new(); // socket + cache - SRPC_SAFE_CALL_PTR(socket, nl_socket_alloc(), error_out); - SRPC_SAFE_CALL_ERR(error, nl_connect(socket, NETLINK_ROUTE), error_out); - SRPC_SAFE_CALL_ERR(error, rtnl_link_alloc_cache(socket, AF_UNSPEC, &link_cache), error_out); + SRPC_SAFE_CALL_PTR(nl_ctx->socket, nl_socket_alloc(), error_out); + SRPC_SAFE_CALL_ERR(error, nl_connect(nl_ctx->socket, NETLINK_ROUTE), error_out); + SRPC_SAFE_CALL_ERR(error, rtnl_link_alloc_cache(nl_ctx->socket, AF_UNSPEC, &nl_ctx->link_cache), error_out); // get link iterator - SRPC_SAFE_CALL_PTR(link_iter, (struct rtnl_link*)nl_cache_get_first(link_cache), error_out); + SRPC_SAFE_CALL_PTR(link_iter, (struct rtnl_link*)nl_cache_get_first(nl_ctx->link_cache), error_out); // iterate links while (link_iter) { @@ -65,6 +68,18 @@ int interfaces_load_interface(interfaces_ctx_t* ctx, interfaces_interface_hash_e SRPC_SAFE_CALL_ERR(error, interfaces_interface_load_enabled(ctx, &new_element, link_iter), error_out); // load interface IPv4 data + SRPC_SAFE_CALL_ERR(error, interfaces_interface_ipv4_load_enabled(ctx, &new_element->interface.ipv4, link_iter), error_out); + SRPC_SAFE_CALL_ERR(error, interfaces_interface_ipv4_load_forwarding(ctx, &new_element->interface.ipv4, link_iter), error_out); + SRPC_SAFE_CALL_ERR(error, interfaces_interface_ipv4_load_mtu(ctx, &new_element->interface.ipv4, link_iter), error_out); + + // load interface IPv6 data + SRPC_SAFE_CALL_ERR(error, interfaces_interface_ipv6_load_enabled(ctx, &new_element->interface.ipv6, link_iter), error_out); + SRPC_SAFE_CALL_ERR(error, interfaces_interface_ipv6_load_forwarding(ctx, &new_element->interface.ipv6, link_iter), error_out); + SRPC_SAFE_CALL_ERR(error, interfaces_interface_ipv6_load_mtu(ctx, &new_element->interface.ipv6, link_iter), error_out); + + // load IPv4 address and neighbor lists + 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); } goto out; @@ -73,13 +88,17 @@ int interfaces_load_interface(interfaces_ctx_t* ctx, interfaces_interface_hash_e error = -1; out: - if (socket != NULL) { - nl_socket_free(socket); + // dealloc nl_ctx data + + if (nl_ctx->socket != NULL) { + nl_socket_free(nl_ctx->socket); } - if (link_cache != NULL) { - nl_cache_free(link_cache); + if (nl_ctx->link_cache != NULL) { + nl_cache_free(nl_ctx->link_cache); } + // address and neighbor caches should be freed by their functions (_load_address and _load_neighbor) + return error; } \ No newline at end of file diff --git a/src/interfaces/src/plugin/context.h b/src/interfaces/src/plugin/context.h index f34160c0..f9fe3e41 100644 --- a/src/interfaces/src/plugin/context.h +++ b/src/interfaces/src/plugin/context.h @@ -16,6 +16,7 @@ typedef struct interfaces_ctx_s interfaces_ctx_t; typedef struct interfaces_state_changes_ctx_s interfaces_state_changes_ctx_t; typedef struct interfaces_mod_changes_ctx_s interfaces_mod_changes_ctx_t; typedef struct interfaces_oper_ctx_s interfaces_oper_ctx_t; +typedef struct interfaces_startup_ctx_s interfaces_startup_ctx_t; typedef struct interfaces_features_ctx_s interfaces_features_ctx_t; struct interfaces_features_ctx_s { @@ -83,10 +84,18 @@ struct interfaces_oper_ctx_s { interfaces_state_changes_ctx_t state_changes_ctx; }; -struct interfaces_ctx_s { +struct interfaces_startup_ctx_s { // startup DS sr_session_ctx_t* startup_session; + // libnl context + interfaces_nl_ctx_t nl_ctx; +}; + +struct interfaces_ctx_s { + // startup data + interfaces_startup_ctx_t startup_ctx; + // module changes data interfaces_mod_changes_ctx_t mod_ctx;