Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…n-interfaces into refactor
  • Loading branch information
andrej committed Oct 13, 2022
2 parents c1db1a0 + 8da7ffb commit 21b1cfd
Show file tree
Hide file tree
Showing 9 changed files with 489 additions and 136 deletions.
19 changes: 7 additions & 12 deletions src/interfaces/src/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,20 +276,15 @@ int sr_plugin_init_cb(sr_session_ctx_t* running_session, void** private_data)
},
};

connection = sr_session_get_connection(running_session);
error = sr_session_start(connection, SR_DS_STARTUP, &startup_session);
if (error) {
SRPLG_LOG_ERR(PLUGIN_NAME, "sr_session_start() error (%d): %s", error, sr_strerror(error));
goto error_out;
}
// get connection
SRPC_SAFE_CALL_PTR(connection, sr_session_get_connection(running_session), error_out);

// start a session
SRPC_SAFE_CALL_ERR(error, sr_session_start(connection, SR_DS_STARTUP, &startup_session), error_out);

ctx->startup_session = startup_session;

error = srpc_check_empty_datastore(startup_session, INTERFACES_INTERFACES_INTERFACE_YANG_PATH, &empty_startup);
if (error) {
SRPLG_LOG_ERR(PLUGIN_NAME, "Failed checking datastore contents: %d", error);
goto error_out;
}
SRPC_SAFE_CALL_ERR(error, srpc_check_empty_datastore(startup_session, INTERFACES_INTERFACES_INTERFACE_YANG_PATH, &empty_startup), error_out);

if (empty_startup) {
SRPLG_LOG_INF(PLUGIN_NAME, "Startup datastore is empty");
Expand Down Expand Up @@ -338,7 +333,7 @@ int sr_plugin_init_cb(sr_session_ctx_t* running_session, void** private_data)
if (op->cb) {
error = sr_oper_get_subscribe(running_session, op->module, op->path, op->cb, *private_data, SR_SUBSCR_DEFAULT, &subscription);
if (error) {
SRPLG_LOG_ERR(PLUGIN_NAME, "sr_oper_get_subscribe() error (%d): %s", error, sr_strerror(error));
SRPLG_LOG_ERR(PLUGIN_NAME, "sr_oper_get_subscribe() error for \"%s\" (%d): %s", op->path, error, sr_strerror(error));
goto error_out;
}
}
Expand Down
63 changes: 59 additions & 4 deletions src/interfaces/src/plugin/api/interfaces/interface/ipv4/change.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
#include "change.h"
#include "libyang/tree_data.h"
#include "netlink/route/link.h"
#include "plugin/common.h"
#include "plugin/context.h"
#include "sysrepo_types.h"

#include <linux/limits.h>
#include <stdint.h>
#include <stdlib.h>
#include <sysrepo.h>

int interfaces_interface_ipv4_change_neighbor_init(void* priv)
Expand Down Expand Up @@ -75,23 +82,71 @@ int interfaces_interface_ipv4_change_mtu_init(void* priv)

int interfaces_interface_ipv4_change_mtu(void* priv, sr_session_ctx_t* session, const srpc_change_ctx_t* change_ctx)
{
int error = 0;
int error = SR_ERR_OK;
void* error_ptr = 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 };

SRPLG_LOG_DBG(PLUGIN_NAME, "Node Name: %s; Previous Value: %s, Value: %s; Operation: %d", node_name, change_ctx->previous_value, node_value, change_ctx->operation);
// app context
interfaces_ctx_t* ctx = priv;

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

// libnl
struct rtnl_link* current_link = NULL;
struct rtnl_link* request_link = 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);

// create request
request_link = rtnl_link_alloc();
rtnl_link_set_name(request_link, rtnl_link_get_name(current_link));
SRPC_SAFE_CALL_ERR(error, rtnl_link_set_type(request_link, rtnl_link_get_type(current_link)), error_out);

switch (change_ctx->operation) {
case SR_OP_CREATED:
case SR_OP_MODIFIED: {
// convert to int
uint16_t mtu = (uint16_t)atoi(node_value);

// set data
rtnl_link_set_mtu(request_link, mtu);
break;
case SR_OP_MODIFIED:
break;
}
case SR_OP_DELETED:
// set default MTU
rtnl_link_set_mtu(request_link, 1500);
break;
case SR_OP_MOVED:
break;
}

// apply changes
SRPC_SAFE_CALL_ERR(error, rtnl_link_change(mod_ctx->nl_ctx.socket, current_link, request_link, 0), error_out);

goto out;

error_out:
if (error < 0) {
SRPLG_LOG_ERR(PLUGIN_NAME, "nl_geterror(): %s", nl_geterror(error));
}
error = -1;

out:
return error;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@
int interfaces_interface_ipv4_change_neighbor_init(void* priv);
int interfaces_interface_ipv4_change_neighbor(void* priv, sr_session_ctx_t* session, const srpc_change_ctx_t* change_ctx);
void interfaces_interface_ipv4_change_neighbor_free(void* priv);

int interfaces_interface_ipv4_change_address_init(void* priv);
int interfaces_interface_ipv4_change_address(void* priv, sr_session_ctx_t* session, const srpc_change_ctx_t* change_ctx);
void interfaces_interface_ipv4_change_address_free(void* priv);

int interfaces_interface_ipv4_change_mtu_init(void* priv);
int interfaces_interface_ipv4_change_mtu(void* priv, sr_session_ctx_t* session, const srpc_change_ctx_t* change_ctx);
void interfaces_interface_ipv4_change_mtu_free(void* priv);

int interfaces_interface_ipv4_change_forwarding_init(void* priv);
int interfaces_interface_ipv4_change_forwarding(void* priv, sr_session_ctx_t* session, const srpc_change_ctx_t* change_ctx);
void interfaces_interface_ipv4_change_forwarding_free(void* priv);

int interfaces_interface_ipv4_change_enabled_init(void* priv);
int interfaces_interface_ipv4_change_enabled(void* priv, sr_session_ctx_t* session, const srpc_change_ctx_t* change_ctx);
void interfaces_interface_ipv4_change_enabled_free(void* priv);
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/src/plugin/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct interfaces_nl_ctx_s {
struct nl_sock* socket;
struct nl_cache* link_cache;
struct nl_cache* addr_cache;
struct nl_cache* neigh_cache;
struct nl_cache_mngr* link_cache_manager;
};

Expand Down
6 changes: 1 addition & 5 deletions src/interfaces/src/plugin/startup/load.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,7 @@ static int interfaces_startup_load_interface(void* priv, sr_session_ctx_t* sessi
interfaces_ctx_t* ctx = (interfaces_ctx_t*)priv;
interfaces_interface_hash_element_t* interface_head = NULL;

error = interfaces_load_interface(ctx, &interface_head);
if (error) {
SRPLG_LOG_ERR(PLUGIN_NAME, "interfaces_load_interface() error (%d)", error);
goto error_out;
}
SRPC_SAFE_CALL_ERR(error, interfaces_load_interface(ctx, &interface_head), error_out);

goto out;

Expand Down
13 changes: 2 additions & 11 deletions src/interfaces/src/plugin/startup/store.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ int interfaces_startup_store(interfaces_ctx_t* ctx, sr_session_ctx_t* session)
int error = 0;
sr_data_t* subtree = NULL;

error = sr_get_subtree(session, INTERFACES_INTERFACES_CONTAINER_YANG_PATH, 0, &subtree);
if (error) {
SRPLG_LOG_ERR(PLUGIN_NAME, "sr_get_subtree() error (%d): %s", error, sr_strerror(error));
goto error_out;
}
SRPC_SAFE_CALL_ERR(error, sr_get_subtree(session, INTERFACES_INTERFACES_CONTAINER_YANG_PATH, 0, &subtree), error_out);

srpc_startup_store_t store_values[] = {
{
Expand Down Expand Up @@ -86,17 +82,12 @@ static int interfaces_startup_store_interface(void* priv, const struct lyd_node*
case srpc_check_status_non_existant:
SRPLG_LOG_INF(PLUGIN_NAME, "Storing interface array");

error = interfaces_store_interface(ctx, if_hash);
if (error) {
SRPLG_LOG_ERR(PLUGIN_NAME, "interfaces_store_interface() failed (%d)", error);
goto error_out;
}
SRPC_SAFE_CALL_ERR(error, interfaces_store_interface(ctx, if_hash), error_out);
break;
case srpc_check_status_equal:
SRPLG_LOG_ERR(PLUGIN_NAME, "Startup interface array is already applied on the system");
break;
case srpc_check_status_partial:
/* should not be returned - treat as an error */
SRPLG_LOG_ERR(PLUGIN_NAME, "Error loading current interface array");
goto error_out;
}
Expand Down
11 changes: 6 additions & 5 deletions src/interfaces/src/plugin/subscription/change.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// change API
#include "plugin/api/interfaces/change.h"
#include "plugin/api/interfaces/interface/change.h"
#include "plugin/api/interfaces/interface/ipv4/change.h"

int interfaces_subscription_change_interfaces_interface(sr_session_ctx_t* session, uint32_t subscription_id, const char* module_name, const char* xpath, sr_event_t event, uint32_t request_id, void* private_data)
{
Expand All @@ -22,11 +23,7 @@ int interfaces_subscription_change_interfaces_interface(sr_session_ctx_t* sessio
SRPLG_LOG_ERR(PLUGIN_NAME, "Aborting changes for %s", xpath);
goto error_out;
} else if (event == SR_EV_DONE) {
error = sr_copy_config(ctx->startup_session, IETF_INTERFACES_YANG_MODULE, SR_DS_RUNNING, 0);
if (error) {
SRPLG_LOG_ERR(PLUGIN_NAME, "sr_copy_config() error (%d): %s", error, sr_strerror(error));
goto error_out;
}
SRPC_SAFE_CALL_ERR(error, sr_copy_config(ctx->startup_session, IETF_INTERFACES_YANG_MODULE, SR_DS_RUNNING, 0), error_out);
} else if (event == SR_EV_CHANGE) {
// name
SRPC_SAFE_CALL_ERR_COND(rc, rc < 0, snprintf(change_xpath_buffer, sizeof(change_xpath_buffer), "%s/name", xpath), error_out);
Expand All @@ -47,6 +44,10 @@ int interfaces_subscription_change_interfaces_interface(sr_session_ctx_t* sessio
// link-up-down-trap-enable
SRPC_SAFE_CALL_ERR_COND(rc, rc < 0, snprintf(change_xpath_buffer, sizeof(change_xpath_buffer), "%s/link-up-down-trap-enable", xpath), error_out);
SRPC_SAFE_CALL_ERR(rc, srpc_iterate_changes(ctx, session, change_xpath_buffer, interfaces_interface_change_link_up_down_trap_enable, interfaces_change_interface_init, interfaces_change_interface_free), error_out);

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

goto out;
Expand Down
Loading

0 comments on commit 21b1cfd

Please sign in to comment.