Skip to content

Commit

Permalink
interfaces-plugin: implement IPv4 address IP create functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
zinccyy committed Oct 6, 2022
1 parent b1354f9 commit c62c6f4
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#include "change.h"
#include "plugin/common.h"
#include "plugin/context.h"

#include <sysrepo.h>

#include <netlink/addr.h>
#include <netlink/route/addr.h>
#include <netlink/route/link.h>

int interfaces_interface_ipv4_address_change_netmask_init(void* priv)
{
int error = 0;
Expand Down Expand Up @@ -76,25 +81,86 @@ int interfaces_interface_ipv4_address_change_ip_init(void* priv)
int interfaces_interface_ipv4_address_change_ip(void* priv, sr_session_ctx_t* session, const srpc_change_ctx_t* change_ctx)
{
int error = 0;
void* error_ptr = NULL;

// sysrepo
sr_val_t* prefix_val = NULL;
sr_val_t* netmask_val = NULL;

// strings and buffers
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 };

// app context
interfaces_ctx_t* ctx = priv;

// mod changes context
interfaces_mod_changes_ctx_t* mod_ctx = &ctx->mod_ctx;

// libnl
struct rtnl_addr* request_addr = NULL;
struct rtnl_link* current_link = NULL;
struct nl_addr* local_addr = NULL;

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);

// get node path
SRPC_SAFE_CALL_PTR(error_ptr, lyd_path(change_ctx->node, LYD_PATH_STD, path_buffer, sizeof(path_buffer)), error_out);

// get interface name
SRPC_SAFE_CALL_ERR(error, srpc_extract_xpath_key_value(path_buffer, "interface", "name", interface_name_buffer, sizeof(interface_name_buffer)), error_out);

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 interface index
const int ifindex = rtnl_link_get_ifindex(current_link);

switch (change_ctx->operation) {
case SR_OP_CREATED:
// new address
request_addr = rtnl_addr_alloc();

// parse local address
SRPC_SAFE_CALL_ERR(error, nl_addr_parse(node_value, AF_INET, &local_addr), error_out);

// set temporary prefix-length - "default"
nl_addr_set_prefixlen(local_addr, 24);

// set to route address
SRPC_SAFE_CALL_ERR(error, rtnl_addr_set_local(request_addr, local_addr), error_out);

// set interface
rtnl_addr_set_ifindex(request_addr, ifindex);

// add address
SRPC_SAFE_CALL_ERR(error, rtnl_addr_add(mod_ctx->nl_ctx.socket, request_addr, 0), error_out);

break;
case SR_OP_MODIFIED:
// should be impossible - address IP can only be created and deleted
SRPLG_LOG_ERR(PLUGIN_NAME, "Unsuported operation MODIFY for ietf-ip:ipv4/address/ip");
goto error_out;
break;
case SR_OP_DELETED:
// fetch info about prefix-length/netmask and use the pair address/prefix to delete address
// prefix is needed to find the appropriate address
break;
case SR_OP_MOVED:
break;
}

return -1;
goto out;

error_out:
error = -1;

out:
return error;
}

void interfaces_interface_ipv4_address_change_ip_free(void* priv)
Expand Down
4 changes: 0 additions & 4 deletions src/interfaces/src/plugin/subscription/change.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ int interfaces_subscription_change_interfaces_interface(sr_session_ctx_t* sessio
SRPC_SAFE_CALL_ERR_COND(rc, rc < 0, snprintf(change_xpath_buffer, sizeof(change_xpath_buffer), "%s/ipv4/enabled", xpath), error_out);
SRPC_SAFE_CALL_ERR(rc, srpc_iterate_changes(ctx, session, change_xpath_buffer, interfaces_interface_ipv4_change_enabled, interfaces_change_interface_init, interfaces_change_interface_free), error_out);

// ipv4/address
SRPC_SAFE_CALL_ERR_COND(rc, rc < 0, snprintf(change_xpath_buffer, sizeof(change_xpath_buffer), "%s/ipv4/address", xpath), error_out);
SRPC_SAFE_CALL_ERR(rc, srpc_iterate_changes(ctx, session, change_xpath_buffer, interfaces_interface_ipv4_change_address, interfaces_change_interface_init, interfaces_change_interface_free), error_out);

// ipv4/address/ip
SRPC_SAFE_CALL_ERR_COND(rc, rc < 0, snprintf(change_xpath_buffer, sizeof(change_xpath_buffer), "%s/ipv4/address/ip", xpath), error_out);
SRPC_SAFE_CALL_ERR(rc, srpc_iterate_changes(ctx, session, change_xpath_buffer, interfaces_interface_ipv4_address_change_ip, interfaces_change_interface_init, interfaces_change_interface_free), error_out);
Expand Down

0 comments on commit c62c6f4

Please sign in to comment.