Skip to content

Commit

Permalink
interfaces-plugin: implement base for enabled IPv4 leaf change
Browse files Browse the repository at this point in the history
zinccyy committed Oct 6, 2022
1 parent fbc1e3c commit b194860
Showing 2 changed files with 46 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/interfaces/src/plugin/api/interfaces/change.c
Original file line number Diff line number Diff line change
@@ -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);
}
40 changes: 37 additions & 3 deletions src/interfaces/src/plugin/api/interfaces/interface/ipv4/change.c
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit b194860

Please sign in to comment.