From b19486023ce1d2ed89aa4863e5368811af54df72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateo=20Cindri=C4=87?= Date: Thu, 6 Oct 2022 16:13:15 +0200 Subject: [PATCH] interfaces-plugin: implement base for enabled IPv4 leaf change --- .../src/plugin/api/interfaces/change.c | 9 +++++ .../api/interfaces/interface/ipv4/change.c | 40 +++++++++++++++++-- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/interfaces/src/plugin/api/interfaces/change.c b/src/interfaces/src/plugin/api/interfaces/change.c index 896699bd..fc8fa98b 100644 --- a/src/interfaces/src/plugin/api/interfaces/change.c +++ b/src/interfaces/src/plugin/api/interfaces/change.c @@ -1,4 +1,5 @@ #include "change.h" +#include "netlink/route/addr.h" #include "plugin/common.h" #include "plugin/context.h" @@ -25,6 +26,9 @@ int interfaces_change_interface_init(void* priv) // allocate link cache SRPC_SAFE_CALL_ERR(error, rtnl_link_alloc_cache(mod_ctx->nl_ctx.socket, AF_UNSPEC, &mod_ctx->nl_ctx.link_cache), error_out); + // allocate address cache + SRPC_SAFE_CALL_ERR(error, rtnl_addr_alloc_cache(mod_ctx->nl_ctx.socket, &mod_ctx->nl_ctx.addr_cache), error_out); + goto out; error_out: @@ -91,6 +95,11 @@ void interfaces_change_interface_free(void* priv) if (mod_ctx->nl_ctx.link_cache) { nl_cache_put(mod_ctx->nl_ctx.link_cache); } + + if (mod_ctx->nl_ctx.addr_cache) { + nl_cache_put(mod_ctx->nl_ctx.addr_cache); + } + if (mod_ctx->nl_ctx.socket) { nl_socket_free(mod_ctx->nl_ctx.socket); } diff --git a/src/interfaces/src/plugin/api/interfaces/interface/ipv4/change.c b/src/interfaces/src/plugin/api/interfaces/interface/ipv4/change.c index bdcd19ad..9469376f 100644 --- a/src/interfaces/src/plugin/api/interfaces/interface/ipv4/change.c +++ b/src/interfaces/src/plugin/api/interfaces/interface/ipv4/change.c @@ -1,5 +1,6 @@ #include "change.h" #include "libyang/tree_data.h" +#include "netlink/cache.h" #include "netlink/route/link.h" #include "plugin/common.h" #include "plugin/context.h" @@ -67,7 +68,7 @@ int interfaces_interface_ipv4_change_address(void* priv, sr_session_ctx_t* sessi break; } - return -1; + return error; } void interfaces_interface_ipv4_change_address_free(void* priv) @@ -196,12 +197,22 @@ int interfaces_interface_ipv4_change_enabled(void* priv, sr_session_ctx_t* sessi { int error = 0; void* error_ptr = NULL; + interfaces_ctx_t* ctx = priv; + interfaces_mod_changes_ctx_t* mod_ctx = &ctx->mod_ctx; + + // sysrepo + sr_conn_ctx_t* conn = NULL; + sr_session_ctx_t* running_session = NULL; + const char* node_name = LYD_NAME(change_ctx->node); const char* node_value = lyd_get_value(change_ctx->node); char path_buffer[PATH_MAX] = { 0 }; char interface_name_buffer[100] = { 0 }; + struct rtnl_link* current_link = NULL; + struct rtnl_addr* addr_iter = NULL; + // IPv4 can be disabled by deleting all IPv4 addresses associated with the interface SRPLG_LOG_INF(PLUGIN_NAME, "Node Name: %s; Previous Value: %s, Value: %s; Operation: %d", node_name, change_ctx->previous_value, node_value, change_ctx->operation); @@ -214,12 +225,31 @@ int interfaces_interface_ipv4_change_enabled(void* priv, sr_session_ctx_t* sessi SRPLG_LOG_INF(PLUGIN_NAME, "Node Path: %s; Interface Name: %s", path_buffer, interface_name_buffer); + // get link + SRPC_SAFE_CALL_PTR(current_link, rtnl_link_get_by_name(mod_ctx->nl_ctx.link_cache, interface_name_buffer), error_out); + + // get address iterator + SRPC_SAFE_CALL_PTR(addr_iter, (struct rtnl_addr*)nl_cache_get_first(mod_ctx->nl_ctx.addr_cache), error_out); + + // get connection + SRPC_SAFE_CALL_PTR(conn, sr_session_get_connection(session), error_out); + + // start running session + SRPC_SAFE_CALL_ERR(error, sr_session_start(conn, SR_DS_RUNNING, &running_session), error_out); + switch (change_ctx->operation) { case SR_OP_CREATED: - break; case SR_OP_MODIFIED: + // if IPv4 disabled - delete all v4 addresses on the interface + if (!strcmp(node_value, "false")) { + // configure path buffer + SRPC_SAFE_CALL_ERR_COND(error, error < 0, snprintf(path_buffer, sizeof(path_buffer), "%s[name=\"%s\"]/ietf-ip:ipv4/address", INTERFACES_INTERFACES_LIST_YANG_PATH, interface_name_buffer), error_out); + + SRPLG_LOG_INF(PLUGIN_NAME, "Deleting every IPv4 address for interface %s: path = %s", interface_name_buffer, path_buffer); + } break; case SR_OP_DELETED: + // default value = true, don't do anything break; case SR_OP_MOVED: break; @@ -231,7 +261,11 @@ int interfaces_interface_ipv4_change_enabled(void* priv, sr_session_ctx_t* sessi error = -1; out: - return -1; + if (running_session) { + sr_session_stop(running_session); + } + + return error; } void interfaces_interface_ipv4_change_enabled_free(void* priv)