Skip to content

Commit

Permalink
interfaces-plugin: add startup context
Browse files Browse the repository at this point in the history
  • Loading branch information
zinccyy committed Oct 31, 2022
1 parent 41e6ada commit 7f8be4d
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/interfaces/src/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ int sr_plugin_init_cb(sr_session_ctx_t* running_session, void** private_data)
// start a session
SRPC_SAFE_CALL_ERR(error, sr_session_start(connection, SR_DS_STARTUP, &startup_session), error_out);

ctx->startup_session = startup_session;
ctx->startup_ctx.startup_session = startup_session;

SRPC_SAFE_CALL_ERR(error, srpc_check_empty_datastore(startup_session, INTERFACES_INTERFACES_INTERFACE_YANG_PATH, &empty_startup), error_out);

Expand Down
12 changes: 12 additions & 0 deletions src/interfaces/src/plugin/api/interfaces/interface/ipv4/load.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,15 @@ int interfaces_interface_ipv4_load_mtu(interfaces_ctx_t* ctx, interfaces_interfa

return error;
}

int interfaces_interface_ipv4_load_address(interfaces_ctx_t* ctx, interfaces_interface_ipv4_t* ipv4, struct rtnl_link* link)
{
int error = 0;
return error;
}

int interfaces_interface_ipv4_load_neighbor(interfaces_ctx_t* ctx, interfaces_interface_ipv4_t* ipv4, struct rtnl_link* link)
{
int error = 0;
return error;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@
int interfaces_interface_ipv4_load_enabled(interfaces_ctx_t* ctx, interfaces_interface_ipv4_t* ipv4, struct rtnl_link* link);
int interfaces_interface_ipv4_load_forwarding(interfaces_ctx_t* ctx, interfaces_interface_ipv4_t* ipv4, struct rtnl_link* link);
int interfaces_interface_ipv4_load_mtu(interfaces_ctx_t* ctx, interfaces_interface_ipv4_t* ipv4, struct rtnl_link* link);
int interfaces_interface_ipv4_load_address(interfaces_ctx_t* ctx, interfaces_interface_ipv4_t* ipv4, struct rtnl_link* link);
int interfaces_interface_ipv4_load_neighbor(interfaces_ctx_t* ctx, interfaces_interface_ipv4_t* ipv4, struct rtnl_link* link);

#endif // INTERFACES_PLUGIN_API_INTERFACES_INTERFACE_IPV4_LOAD_H
41 changes: 30 additions & 11 deletions src/interfaces/src/plugin/api/interfaces/load.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "interface/ipv4/load.h"
#include "interface/ipv6/load.h"
#include "plugin/common.h"
#include "plugin/context.h"
#include "plugin/data/interfaces/interface.h"
#include "plugin/types.h"
#include "read.h"
Expand Down Expand Up @@ -36,23 +37,25 @@
int interfaces_load_interface(interfaces_ctx_t* ctx, interfaces_interface_hash_element_t** if_hash)
{
int error = 0;
struct nl_sock* socket = NULL;
struct nl_cache* link_cache = NULL;
struct rtnl_link* link_iter = NULL;

// ctx
interfaces_startup_ctx_t* startup_ctx = &ctx->startup_ctx;
interfaces_nl_ctx_t* nl_ctx = &startup_ctx->nl_ctx;

// temp data
interfaces_interface_hash_element_t* new_element = NULL;
struct rtnl_link* link_iter = NULL;

// init hash
*if_hash = interfaces_interface_hash_new();

// socket + cache
SRPC_SAFE_CALL_PTR(socket, nl_socket_alloc(), error_out);
SRPC_SAFE_CALL_ERR(error, nl_connect(socket, NETLINK_ROUTE), error_out);
SRPC_SAFE_CALL_ERR(error, rtnl_link_alloc_cache(socket, AF_UNSPEC, &link_cache), error_out);
SRPC_SAFE_CALL_PTR(nl_ctx->socket, nl_socket_alloc(), error_out);
SRPC_SAFE_CALL_ERR(error, nl_connect(nl_ctx->socket, NETLINK_ROUTE), error_out);
SRPC_SAFE_CALL_ERR(error, rtnl_link_alloc_cache(nl_ctx->socket, AF_UNSPEC, &nl_ctx->link_cache), error_out);

// get link iterator
SRPC_SAFE_CALL_PTR(link_iter, (struct rtnl_link*)nl_cache_get_first(link_cache), error_out);
SRPC_SAFE_CALL_PTR(link_iter, (struct rtnl_link*)nl_cache_get_first(nl_ctx->link_cache), error_out);

// iterate links
while (link_iter) {
Expand All @@ -65,6 +68,18 @@ int interfaces_load_interface(interfaces_ctx_t* ctx, interfaces_interface_hash_e
SRPC_SAFE_CALL_ERR(error, interfaces_interface_load_enabled(ctx, &new_element, link_iter), error_out);

// load interface IPv4 data
SRPC_SAFE_CALL_ERR(error, interfaces_interface_ipv4_load_enabled(ctx, &new_element->interface.ipv4, link_iter), error_out);
SRPC_SAFE_CALL_ERR(error, interfaces_interface_ipv4_load_forwarding(ctx, &new_element->interface.ipv4, link_iter), error_out);
SRPC_SAFE_CALL_ERR(error, interfaces_interface_ipv4_load_mtu(ctx, &new_element->interface.ipv4, link_iter), error_out);

// load interface IPv6 data
SRPC_SAFE_CALL_ERR(error, interfaces_interface_ipv6_load_enabled(ctx, &new_element->interface.ipv6, link_iter), error_out);
SRPC_SAFE_CALL_ERR(error, interfaces_interface_ipv6_load_forwarding(ctx, &new_element->interface.ipv6, link_iter), error_out);
SRPC_SAFE_CALL_ERR(error, interfaces_interface_ipv6_load_mtu(ctx, &new_element->interface.ipv6, link_iter), error_out);

// load IPv4 address and neighbor lists
SRPC_SAFE_CALL_ERR(error, interfaces_interface_ipv4_load_address(ctx, &new_element->interface.ipv4, link_iter), error_out);
SRPC_SAFE_CALL_ERR(error, interfaces_interface_ipv4_load_neighbor(ctx, &new_element->interface.ipv4, link_iter), error_out);
}

goto out;
Expand All @@ -73,13 +88,17 @@ int interfaces_load_interface(interfaces_ctx_t* ctx, interfaces_interface_hash_e
error = -1;

out:
if (socket != NULL) {
nl_socket_free(socket);
// dealloc nl_ctx data

if (nl_ctx->socket != NULL) {
nl_socket_free(nl_ctx->socket);
}

if (link_cache != NULL) {
nl_cache_free(link_cache);
if (nl_ctx->link_cache != NULL) {
nl_cache_free(nl_ctx->link_cache);
}

// address and neighbor caches should be freed by their functions (_load_address and _load_neighbor)

return error;
}
11 changes: 10 additions & 1 deletion src/interfaces/src/plugin/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ typedef struct interfaces_ctx_s interfaces_ctx_t;
typedef struct interfaces_state_changes_ctx_s interfaces_state_changes_ctx_t;
typedef struct interfaces_mod_changes_ctx_s interfaces_mod_changes_ctx_t;
typedef struct interfaces_oper_ctx_s interfaces_oper_ctx_t;
typedef struct interfaces_startup_ctx_s interfaces_startup_ctx_t;
typedef struct interfaces_features_ctx_s interfaces_features_ctx_t;

struct interfaces_features_ctx_s {
Expand Down Expand Up @@ -83,10 +84,18 @@ struct interfaces_oper_ctx_s {
interfaces_state_changes_ctx_t state_changes_ctx;
};

struct interfaces_ctx_s {
struct interfaces_startup_ctx_s {
// startup DS
sr_session_ctx_t* startup_session;

// libnl context
interfaces_nl_ctx_t nl_ctx;
};

struct interfaces_ctx_s {
// startup data
interfaces_startup_ctx_t startup_ctx;

// module changes data
interfaces_mod_changes_ctx_t mod_ctx;

Expand Down

0 comments on commit 7f8be4d

Please sign in to comment.