Skip to content

Commit

Permalink
interfaces-plugin: reorganize interface info loading code into interf…
Browse files Browse the repository at this point in the history
…ace load API
  • Loading branch information
zinccyy committed Oct 31, 2022
1 parent e4a93e7 commit 34a04b5
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 48 deletions.
1 change: 1 addition & 0 deletions src/interfaces/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ set(
src/plugin/api/interfaces/read.c
src/plugin/api/interfaces/store.c
src/plugin/api/interfaces/change.c
src/plugin/api/interfaces/interface/load.c
src/plugin/api/interfaces/interface/change.c
src/plugin/api/interfaces/interface/ipv6/load.c
src/plugin/api/interfaces/interface/ipv6/change.c
Expand Down
66 changes: 66 additions & 0 deletions src/interfaces/src/plugin/api/interfaces/interface/load.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "load.h"
#include "plugin/data/interfaces/interface.h"

#include <linux/if.h>
#include <srpc.h>

int interfaces_interface_load_name(interfaces_ctx_t* ctx, interfaces_interface_hash_element_t** element, struct rtnl_link* link)
{
int error = 0;

const char* nl_if_name = NULL;

nl_if_name = rtnl_link_get_name(link);

// set element values
SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_element_set_name(element, nl_if_name), error_out);

goto out;

error_out:
error = -1;

out:
return error;
}

int interfaces_interface_load_type(interfaces_ctx_t* ctx, interfaces_interface_hash_element_t** element, struct rtnl_link* link)
{
int error = 0;

const char* nl_if_type = NULL;
const char* ly_if_type = NULL;

// 2. interface type - nl version -> convert to libyang version
nl_if_type = rtnl_link_get_type(link);

SRPC_SAFE_CALL_ERR(error, interfaces_interface_type_nl2ly(nl_if_type, &ly_if_type), error_out);
SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_element_set_type(element, ly_if_type), error_out);

goto out;

error_out:
error = -1;

out:

return error;
}

int interfaces_interface_load_enabled(interfaces_ctx_t* ctx, interfaces_interface_hash_element_t** element, struct rtnl_link* link)
{
int error = 0;

uint8_t nl_enabled = (rtnl_link_get_operstate(link) == IF_OPER_UP || rtnl_link_get_operstate(link) == IF_OPER_UNKNOWN);

SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_element_set_enabled(element, nl_enabled), error_out);

goto out;

error_out:
error = -1;

out:

return error;
}
13 changes: 13 additions & 0 deletions src/interfaces/src/plugin/api/interfaces/interface/load.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef INTERFACES_PLUGIN_API_INTERFACES_INTERFACE_LOAD_H
#define INTERFACES_PLUGIN_API_INTERFACES_INTERFACE_LOAD_H

#include "plugin/context.h"
#include "plugin/types.h"

#include <netlink/route/link.h>

int interfaces_interface_load_name(interfaces_ctx_t* ctx, interfaces_interface_hash_element_t** element, struct rtnl_link* link);
int interfaces_interface_load_type(interfaces_ctx_t* ctx, interfaces_interface_hash_element_t** element, struct rtnl_link* link);
int interfaces_interface_load_enabled(interfaces_ctx_t* ctx, interfaces_interface_hash_element_t** element, struct rtnl_link* link);

#endif // INTERFACES_PLUGIN_API_INTERFACES_INTERFACE_LOAD_H
56 changes: 8 additions & 48 deletions src/interfaces/src/plugin/api/interfaces/load.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#include "utils/memory.h"
#include "utlist.h"

// load APIs
#include "interface/load.h"

#include <errno.h>
#include <linux/if.h>
#include <linux/limits.h>
Expand All @@ -30,18 +33,6 @@

#include <sysrepo.h>

enum interfaces_load_exit_status {
interfaces_load_failure = -1,
interfaces_load_success = 0,
interfaces_load_continue = 1,
};

/*
Interface Data Loading (name, type, enabled etc.)
*/

static int interfaces_load_interface_info(struct rtnl_link* link, interfaces_interface_hash_element_t** new_element);

int interfaces_load_interface(interfaces_ctx_t* ctx, interfaces_interface_hash_element_t** if_hash)
{
int error = 0;
Expand All @@ -65,13 +56,15 @@ int interfaces_load_interface(interfaces_ctx_t* ctx, interfaces_interface_hash_e

// iterate links
while (link_iter) {
// extract link info and add a new element to the interfaces hash

// allocate new element
SRPC_SAFE_CALL_PTR(new_element, interfaces_interface_hash_element_new(), error_out);

// load interface data
SRPC_SAFE_CALL_ERR(error, interfaces_load_interface_info(link_iter, &new_element), error_out);
SRPC_SAFE_CALL_ERR(error, interfaces_interface_load_name(ctx, &new_element, link_iter), error_out);
SRPC_SAFE_CALL_ERR(error, interfaces_interface_load_type(ctx, &new_element, link_iter), error_out);
SRPC_SAFE_CALL_ERR(error, interfaces_interface_load_enabled(ctx, &new_element, link_iter), error_out);

// load interface IPv4 data
}

goto out;
Expand All @@ -88,38 +81,5 @@ int interfaces_load_interface(interfaces_ctx_t* ctx, interfaces_interface_hash_e
nl_cache_free(link_cache);
}

return error;
}

static int interfaces_load_interface_info(struct rtnl_link* link, interfaces_interface_hash_element_t** new_element)
{
int error = 0;

const char* nl_if_name = NULL;
const char* nl_if_type = NULL;
const char* ly_if_type = NULL;
uint8_t nl_enabled = 0;

// 1. interface name
nl_if_name = rtnl_link_get_name(link);

// 2. interface type - nl version -> convert to libyang version
nl_if_type = rtnl_link_get_type(link);
SRPC_SAFE_CALL_ERR(error, interfaces_interface_type_nl2ly(nl_if_type, &ly_if_type), error_out);

// enabled
nl_enabled = (rtnl_link_get_operstate(link) == IF_OPER_UP || rtnl_link_get_operstate(link) == IF_OPER_UNKNOWN);

// set element values
SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_element_set_name(new_element, nl_if_name), error_out);
SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_element_set_type(new_element, ly_if_type), error_out);
SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_element_set_enabled(new_element, nl_enabled), error_out);

goto out;

error_out:
error = -1;

out:
return error;
}

0 comments on commit 34a04b5

Please sign in to comment.