Skip to content

Commit

Permalink
Merge branch 'telekom:refactor' into refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jkuless authored Oct 25, 2022
2 parents 073ecc4 + 84c9943 commit 234175d
Show file tree
Hide file tree
Showing 10 changed files with 600 additions and 23 deletions.
26 changes: 22 additions & 4 deletions CMakeModules/FindNL.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,30 @@ find_path(NL_INCLUDE_DIRS netlink/netlink.h
/usr/include/libnl3
/usr/local/include
/usr/local/include/libnl3
${CMAKE_INCLUDE_PATH}
${CMAKE_PREFIX_PATH}/include/libnl3
)

find_library(NL_LIBRARY NAMES nl nl-3)
find_library(NL_ROUTE_LIBRARY NAMES nl-route nl-route-3)
find_library(NL_NETFILTER_LIBRARY NAMES nl-nf nl-nf-3)
find_library(NL_GENL_LIBRARY NAMES nl-genl nl-genl-3)
find_library(
NL_LIBRARY
NAMES nl nl-3
PATHS /usr/lib /usr/lib64 /usr/local/lib /usr/local/lib64 /opt/local/lib /sw/lib ${CMAKE_LIBRARY_PATH} ${CMAKE_INSTALL_PREFIX}/lib
)
find_library(
NL_ROUTE_LIBRARY
NAMES nl-route nl-route-3
PATHS /usr/lib /usr/lib64 /usr/local/lib /usr/local/lib64 /opt/local/lib /sw/lib ${CMAKE_LIBRARY_PATH} ${CMAKE_INSTALL_PREFIX}/lib
)
find_library(
NL_NETFILTER_LIBRARY
NAMES nl-nf nl-nf-3
PATHS /usr/lib /usr/lib64 /usr/local/lib /usr/local/lib64 /opt/local/lib /sw/lib ${CMAKE_LIBRARY_PATH} ${CMAKE_INSTALL_PREFIX}/lib
)
find_library(
NL_GENL_LIBRARY
NAMES nl-genl nl-genl-3
PATHS /usr/lib /usr/lib64 /usr/local/lib /usr/local/lib64 /opt/local/lib /sw/lib ${CMAKE_LIBRARY_PATH} ${CMAKE_INSTALL_PREFIX}/lib
)

if (NL_INCLUDE_DIRS AND NL_LIBRARY)
set(NL_FOUND TRUE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,22 @@ int interfaces_interface_ipv4_address_change_netmask(void* priv, sr_session_ctx_
error = -1;

out:
if (request_addr) {
rtnl_addr_put(request_addr);
}

if (delete_addr) {
rtnl_addr_put(delete_addr);
}

if (old_local_addr) {
nl_addr_put(old_local_addr);
}

if (local_addr) {
nl_addr_put(local_addr);
}

return error;
}

Expand Down Expand Up @@ -231,6 +247,22 @@ int interfaces_interface_ipv4_address_change_prefix_length(void* priv, sr_sessio
error = -1;

out:
if (request_addr) {
rtnl_addr_put(request_addr);
}

if (delete_addr) {
rtnl_addr_put(delete_addr);
}

if (old_local_addr) {
nl_addr_put(old_local_addr);
}

if (local_addr) {
nl_addr_put(local_addr);
}

return error;
}

Expand Down Expand Up @@ -403,6 +435,10 @@ int interfaces_interface_ipv4_address_change_ip(void* priv, sr_session_ctx_t* se
sr_session_stop(running_session);
}

if (request_addr) {
rtnl_addr_put(request_addr);
}

// re-initialize mod_ctx data
mod_ctx->mod_data.ipv4.address.prefix_length = 0;
mod_ctx->mod_data.ipv4.address.prefix_set = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
#include "plugin/context.h"

#include <assert.h>
#include <linux/netlink.h>
#include <sysrepo.h>

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

static int interfaces_interface_ipv4_address_get_link_layer_address(void* priv, sr_session_ctx_t* session, const srpc_change_ctx_t* change_ctx);
static int interfaces_interface_ipv4_neighbor_get_link_layer_address(void* priv, sr_session_ctx_t* session, const srpc_change_ctx_t* change_ctx);

int interfaces_interface_ipv4_neighbor_change_link_layer_address_init(void* priv)
{
Expand All @@ -21,22 +22,83 @@ int interfaces_interface_ipv4_neighbor_change_link_layer_address_init(void* priv
int interfaces_interface_ipv4_neighbor_change_link_layer_address(void* priv, sr_session_ctx_t* session, const srpc_change_ctx_t* change_ctx)
{
int error = 0;
void* error_ptr = 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 };
char ip_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_neigh* request_neigh = NULL;
struct rtnl_link* current_link = NULL;
struct nl_addr* dst_addr = NULL;
struct nl_addr* ll_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);

// get IP
SRPC_SAFE_CALL_ERR(error, srpc_extract_xpath_key_value(path_buffer, "neighbor", "ip", ip_buffer, sizeof(ip_buffer)), error_out);

SRPLG_LOG_INF(PLUGIN_NAME, "Node Path: %s; Interface Name: %s; Neighbor IP: %s", path_buffer, interface_name_buffer, ip_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);

switch (change_ctx->operation) {
case SR_OP_CREATED:
// not used - used only in IP change callback
break;
case SR_OP_MODIFIED:
// change lladdr
request_neigh = rtnl_neigh_alloc();

// set interface
rtnl_neigh_set_ifindex(request_neigh, rtnl_link_get_ifindex(current_link));

// parse destination and LL address
SRPC_SAFE_CALL_ERR(error, nl_addr_parse(ip_buffer, AF_INET, &dst_addr), error_out);
SRPC_SAFE_CALL_ERR(error, nl_addr_parse(node_value, AF_LLC, &ll_addr), error_out);

// set destination and LL address
SRPC_SAFE_CALL_ERR(error, rtnl_neigh_set_dst(request_neigh, dst_addr), error_out);
rtnl_neigh_set_lladdr(request_neigh, ll_addr);

// change neighbor
SRPC_SAFE_CALL_ERR(error, rtnl_neigh_add(mod_ctx->nl_ctx.socket, request_neigh, NLM_F_REPLACE), error_out);

break;
case SR_OP_DELETED:
// not used - used only in IP change callback
break;
case SR_OP_MOVED:
break;
}

goto out;

error_out:
error = -1;

out:
if (request_neigh) {
rtnl_neigh_put(request_neigh);
}

return error;
}

Expand All @@ -55,17 +117,11 @@ int interfaces_interface_ipv4_neighbor_change_ip(void* priv, sr_session_ctx_t* s
int error = 0;
void* error_ptr = NULL;

// sysrepo
// sr_val_t* link_layer_address_val = NULL;
sr_conn_ctx_t* conn_ctx = NULL;
sr_session_ctx_t* running_session = 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 };
// char address_buffer[100] = { 0 };

// app context
interfaces_ctx_t* ctx = priv;
Expand All @@ -92,12 +148,6 @@ int interfaces_interface_ipv4_neighbor_change_ip(void* priv, sr_session_ctx_t* s
// 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 connection
SRPC_SAFE_CALL_PTR(conn_ctx, sr_session_get_connection(session), error_out);

// start a running DS session - fetching data about prefix when deleting the address
SRPC_SAFE_CALL_ERR(error, sr_session_start(conn_ctx, SR_DS_RUNNING, &running_session), error_out);

switch (change_ctx->operation) {
case SR_OP_CREATED:
// new neighbor
Expand All @@ -108,7 +158,7 @@ int interfaces_interface_ipv4_neighbor_change_ip(void* priv, sr_session_ctx_t* s

// get prefix length by using prefix-length or netmask leafs
SRPC_SAFE_CALL_ERR_COND(error, error < 0, snprintf(path_buffer, sizeof(path_buffer), INTERFACES_INTERFACES_INTERFACE_YANG_PATH "[name=\"%s\"]/ietf-ip:ipv4/neighbor[ip=\"%s\"]/link-layer-address", interface_name_buffer, node_value), error_out);
SRPC_SAFE_CALL_ERR(error, srpc_iterate_changes(ctx, session, path_buffer, interfaces_interface_ipv4_address_get_link_layer_address, NULL, NULL), error_out);
SRPC_SAFE_CALL_ERR(error, srpc_iterate_changes(ctx, session, path_buffer, interfaces_interface_ipv4_neighbor_get_link_layer_address, NULL, NULL), error_out);

SRPLG_LOG_INF(PLUGIN_NAME, "Recieved link-layer-address %s for neighbor address %s", mod_ctx->mod_data.ipv4.neighbor.link_layer_address, node_value);

Expand Down Expand Up @@ -160,9 +210,6 @@ int interfaces_interface_ipv4_neighbor_change_ip(void* priv, sr_session_ctx_t* s
error = -1;

out:
if (running_session) {
sr_session_stop(running_session);
}

// re-initialize mod_ctx data
if (mod_ctx->mod_data.ipv4.neighbor.link_layer_address) {
Expand All @@ -171,14 +218,18 @@ int interfaces_interface_ipv4_neighbor_change_ip(void* priv, sr_session_ctx_t* s
mod_ctx->mod_data.ipv4.neighbor.link_layer_address = NULL;
mod_ctx->mod_data.ipv4.neighbor.link_layer_set = false;

if (request_neigh) {
rtnl_neigh_put(request_neigh);
}

return error;
}

void interfaces_interface_ipv4_neighbor_change_ip_free(void* priv)
{
}

static int interfaces_interface_ipv4_address_get_link_layer_address(void* priv, sr_session_ctx_t* session, const srpc_change_ctx_t* change_ctx)
static int interfaces_interface_ipv4_neighbor_get_link_layer_address(void* priv, sr_session_ctx_t* session, const srpc_change_ctx_t* change_ctx)
{
int error = 0;

Expand Down
Loading

0 comments on commit 234175d

Please sign in to comment.