Skip to content

Commit

Permalink
interfaces-plugin: refactor IPv4 and IPv6 data code
Browse files Browse the repository at this point in the history
  • Loading branch information
zinccyy committed Oct 31, 2022
1 parent 060b0c5 commit 41e6ada
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 146 deletions.
2 changes: 2 additions & 0 deletions src/interfaces/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ set(
# data
src/plugin/data/interfaces/interface.c
src/plugin/data/interfaces/interface_state.c
src/plugin/data/interfaces/interface/ipv4.c
src/plugin/data/interfaces/interface/ipv6.c
src/plugin/data/interfaces/interface/ipv4/address.c
src/plugin/data/interfaces/interface/ipv4/neighbor.c
src/plugin/data/interfaces/interface/ipv6/address.c
Expand Down
11 changes: 6 additions & 5 deletions src/interfaces/src/plugin/api/interfaces/interface/ipv4/load.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#include "load.h"
#include "netlink/route/link.h"
#include "plugin/data/interfaces/interface.h"
#include "plugin/data/interfaces/interface/ipv4.h"

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

// enabled by default
SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_element_set_ipv4_enabled(element, 1), error_out);
SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_element_ipv4_set_enabled(ipv4, 1), error_out);

goto out;

Expand All @@ -19,7 +20,7 @@ int interfaces_interface_ipv4_load_enabled(interfaces_ctx_t* ctx, interfaces_int
return error;
}

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

Expand All @@ -28,13 +29,13 @@ int interfaces_interface_ipv4_load_forwarding(interfaces_ctx_t* ctx, interfaces_
return error;
}

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

const unsigned int mtu = rtnl_link_get_mtu(link);

SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_element_set_ipv4_mtu(element, (uint16_t)mtu), error_out);
SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_element_ipv4_set_mtu(ipv4, (uint16_t)mtu), error_out);

goto out;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#include "plugin/data/interfaces/interface/linked_list.h"
#include "plugin/types.h"

int interfaces_interface_ipv4_load_enabled(interfaces_ctx_t* ctx, interfaces_interface_hash_element_t** element, struct rtnl_link* link);
int interfaces_interface_ipv4_load_forwarding(interfaces_ctx_t* ctx, interfaces_interface_hash_element_t** element, struct rtnl_link* link);
int interfaces_interface_ipv4_load_mtu(interfaces_ctx_t* ctx, interfaces_interface_hash_element_t** element, struct rtnl_link* link);
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);

#endif // INTERFACES_PLUGIN_API_INTERFACES_INTERFACE_IPV4_LOAD_H
69 changes: 21 additions & 48 deletions src/interfaces/src/plugin/api/interfaces/interface/ipv6/load.c
Original file line number Diff line number Diff line change
@@ -1,75 +1,48 @@
#include "load.h"
#include "netlink/route/link.h"
#include "plugin/data/interfaces/interface.h"
#include "plugin/data/interfaces/interface/ipv6.h"

int interfaces_add_address_ipv6(interfaces_interface_ipv6_address_element_t **address, char *ip, char *netmask)
int interfaces_interface_ipv6_load_enabled(interfaces_ctx_t* ctx, interfaces_interface_ipv6_t* ipv6, struct rtnl_link* link)
{
int error = 0;
uint8_t prefix_length = 0;

interfaces_interface_ipv6_address_element_t* new_element = NULL;
// enabled by default
SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_element_ipv6_set_enabled(ipv6, 1), error_out);

new_element = interfaces_interface_ipv6_address_element_new();
goto out;

interfaces_interface_ipv6_address_element_set_ip(&new_element, ip);
SRPC_SAFE_CALL_ERR(error, interfaces_interface_ipv4_address_netmask2prefix(netmask, prefix_length), out);
interfaces_interface_ipv6_address_element_set_prefix_length(&new_element, prefix_length);
interfaces_interface_ipv6_address_add_element(address, new_element);
error_out:
error = -1;

out:
return error;
}

int interfaces_add_neighbor_ipv6(interfaces_interface_ipv6_neighbor_element_t** neighbor, char *dst_addr, char *ll_addr)
{
interfaces_interface_ipv6_neighbor_element_t* new_element = NULL;

new_element = interfaces_interface_ipv6_neighbor_element_new();

interfaces_interface_ipv6_neighbor_element_set_ip(&new_element, dst_addr);
interfaces_interface_ipv6_neighbor_element_set_link_layer_address(&new_element, ll_addr);
interfaces_interface_ipv6_neighbor_add_element(neighbor, new_element);

return 0;
}

unsigned int interfaces_get_ipv6_mtu(struct rtnl_link* link, interfaces_interface_t* interface)
{
unsigned int mtu = 0;

mtu = rtnl_link_get_mtu(link);

interface->ipv6.mtu = mtu;

return 0;

return error;
}

unsigned int interfaces_get_ipv6_enabled(interfaces_interface_t* interface)
int interfaces_interface_ipv6_load_forwarding(interfaces_ctx_t* ctx, interfaces_interface_ipv6_t* ipv6, struct rtnl_link* link)
{
int error = 0;
int enabled = 0;

const char *ipv6_base = "/proc/sys/net/ipv6/conf";

SRPC_SAFE_CALL_ERR(error, read_from_proc_file(ipv6_base, interface->name, "disable_ipv6", &enabled), out);
// TODO: implement

interface->ipv6.enabled = enabled;

out:
return error;
}

unsigned int interfaces_get_ipv6_forwarding(interfaces_interface_t* interface)
int interfaces_interface_ipv6_load_mtu(interfaces_ctx_t* ctx, interfaces_interface_ipv6_t* ipv6, struct rtnl_link* link)
{
int error = 0;
int forwarding = 0;

const char *ipv6_base = "/proc/sys/net/ipv6/conf";

SRPC_SAFE_CALL_ERR(error, read_from_proc_file(ipv6_base, interface->name, "forwarding", &forwarding), out);
const unsigned int mtu = rtnl_link_get_mtu(link);

interface->ipv6.forwarding = forwarding;
SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_element_ipv6_set_mtu(ipv6, (uint16_t)mtu), error_out);

goto out;

error_out:
error = -1;

out:

return error;
}

19 changes: 6 additions & 13 deletions src/interfaces/src/plugin/api/interfaces/interface/ipv6/load.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
#ifndef INTERFACES_PLUGIN_API_INTERFACES_IPV6_LOAD_H
#define INTERFACES_PLUGIN_API_INTERFACES_IPV6_LOAD_H
#ifndef INTERFACES_PLUGIN_API_INTERFACES_INTERFACE_IPV6_LOAD_H
#define INTERFACES_PLUGIN_API_INTERFACES_INTERFACE_IPV6_LOAD_H

#include "plugin/context.h"
#include "plugin/data/interfaces/interface.h"
#include "plugin/data/interfaces/interface/linked_list.h"
#include "plugin/types.h"

int interfaces_add_address_ipv6(interfaces_interface_ipv6_address_element_t **address, char *ip, char *netmask);

int interfaces_add_neighbor_ipv6(interfaces_interface_ipv6_neighbor_element_t** neighbor, char *dst_addr, char *ll_addr);

unsigned int interfaces_get_ipv6_mtu(struct rtnl_link* link, interfaces_interface_t* interface);

unsigned int interfaces_get_ipv6_enabled(interfaces_interface_t* interface);

unsigned int interfaces_get_ipv6_forwarding(interfaces_interface_t* interface);

#endif /* INTERFACES_PLUGIN_API_INTERFACES_IPV6_LOAD_H */
int interfaces_interface_ipv6_load_enabled(interfaces_ctx_t* ctx, interfaces_interface_ipv6_t* ipv6, struct rtnl_link* link);
int interfaces_interface_ipv6_load_forwarding(interfaces_ctx_t* ctx, interfaces_interface_ipv6_t* ipv6, struct rtnl_link* link);
int interfaces_interface_ipv6_load_mtu(interfaces_ctx_t* ctx, interfaces_interface_ipv6_t* ipv6, struct rtnl_link* link);

#endif // INTERFACES_PLUGIN_API_INTERFACES_INTERFACE_IPV6_LOAD_H
64 changes: 5 additions & 59 deletions src/interfaces/src/plugin/data/interfaces/interface.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "interface.h"
#include "interface/ipv4.h"
#include "libyang/tree_data.h"
#include "plugin/common.h"
#include "srpc/ly_tree.h"
Expand Down Expand Up @@ -176,18 +177,18 @@ int interfaces_interface_hash_from_ly(interfaces_interface_hash_element_t** if_h
}

if (ipv4_enabled_node) {
SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_element_set_ipv4_enabled(&new_element, strcmp(lyd_get_value(ipv4_enabled_node), "true") == 0 ? 1 : 0), error_out);
SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_element_ipv4_set_enabled(&new_element->interface.ipv4, strcmp(lyd_get_value(ipv4_enabled_node), "true") == 0 ? 1 : 0), error_out);
}

if (ipv4_forwarding_node) {
SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_element_set_ipv4_forwarding(&new_element, strcmp(lyd_get_value(ipv4_forwarding_node), "true") == 0 ? 1 : 0), error_out);
SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_element_ipv4_set_forwarding(&new_element->interface.ipv4, strcmp(lyd_get_value(ipv4_forwarding_node), "true") == 0 ? 1 : 0), error_out);
}

if (ipv4_mtu_node) {
const char* mtu_str = lyd_get_value(ipv4_mtu_node);
uint16_t mtu = (uint16_t)atoi(mtu_str);

SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_element_set_ipv4_mtu(&new_element, mtu), error_out);
SRPC_SAFE_CALL_ERR(error, interfaces_interface_hash_element_ipv4_set_mtu(&new_element->interface.ipv4, mtu), error_out);
}

// init every list
Expand Down Expand Up @@ -432,6 +433,7 @@ int interfaces_interface_hash_element_set_encapsulation(interfaces_interface_has
(*el)->interface.encapsulation = encapsulation;
(*el)->interface.encapsulation.dot1q_vlan.outer_tag.tag_type = xstrdup(encapsulation.dot1q_vlan.outer_tag.tag_type);
(*el)->interface.encapsulation.dot1q_vlan.second_tag.tag_type = xstrdup(encapsulation.dot1q_vlan.second_tag.tag_type);

return 0;
}

Expand Down Expand Up @@ -469,62 +471,6 @@ int interfaces_interface_hash_element_set_parent_interface(interfaces_interface_
return 0;
}

int interfaces_interface_hash_element_set_ipv4(interfaces_interface_hash_element_t** el, interfaces_interface_ipv4_t ipv4)
{
(*el)->interface.ipv4 = ipv4;

return 0;
}

int interfaces_interface_hash_element_set_ipv4_enabled(interfaces_interface_hash_element_t** el, uint8_t enabled)
{
(*el)->interface.ipv4.enabled = enabled;

return 0;
}

int interfaces_interface_hash_element_set_ipv4_forwarding(interfaces_interface_hash_element_t** el, uint8_t forwarding)
{
(*el)->interface.ipv4.forwarding = forwarding;

return 0;
}

int interfaces_interface_hash_element_set_ipv4_mtu(interfaces_interface_hash_element_t** el, uint16_t mtu)
{
(*el)->interface.ipv4.mtu = mtu;

return 0;
}

int interfaces_interface_hash_element_set_ipv6(interfaces_interface_hash_element_t** el, interfaces_interface_ipv6_t ipv6)
{
(*el)->interface.ipv6 = ipv6;

return 0;
}

int interfaces_interface_hash_element_set_ipv6_enabled(interfaces_interface_hash_element_t** el, uint8_t enabled)
{
(*el)->interface.ipv6.enabled = enabled;

return 0;
}

int interfaces_interface_hash_element_set_ipv6_forwarding(interfaces_interface_hash_element_t** el, uint8_t forwarding)
{
(*el)->interface.ipv6.forwarding = forwarding;

return 0;
}

int interfaces_interface_hash_element_set_ipv6_mtu(interfaces_interface_hash_element_t** el, uint16_t mtu)
{
(*el)->interface.ipv6.mtu = mtu;

return 0;
}

int interfaces_interface_type_nl2ly(const char* nl_type, const char** ly_type)
{
int error = 0;
Expand Down
18 changes: 0 additions & 18 deletions src/interfaces/src/plugin/data/interfaces/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,6 @@ int interfaces_interface_hash_element_set_loopback(interfaces_interface_hash_ele
int interfaces_interface_hash_element_set_max_frame_size(interfaces_interface_hash_element_t** el, uint32_t max_frame_size);
int interfaces_interface_hash_element_set_parent_interface(interfaces_interface_hash_element_t** el, const char* parent_interface);

/*
IPv4
*/

int interfaces_interface_hash_element_set_ipv4(interfaces_interface_hash_element_t** el, interfaces_interface_ipv4_t ipv4);
int interfaces_interface_hash_element_set_ipv4_enabled(interfaces_interface_hash_element_t** el, uint8_t enabled);
int interfaces_interface_hash_element_set_ipv4_forwarding(interfaces_interface_hash_element_t** el, uint8_t forwarding);
int interfaces_interface_hash_element_set_ipv4_mtu(interfaces_interface_hash_element_t** el, uint16_t mtu);

/*
IPv6
*/

int interfaces_interface_hash_element_set_ipv6(interfaces_interface_hash_element_t** el, interfaces_interface_ipv6_t ipv6);
int interfaces_interface_hash_element_set_ipv6_enabled(interfaces_interface_hash_element_t** el, uint8_t enabled);
int interfaces_interface_hash_element_set_ipv6_forwarding(interfaces_interface_hash_element_t** el, uint8_t forwarding);
int interfaces_interface_hash_element_set_ipv6_mtu(interfaces_interface_hash_element_t** el, uint16_t mtu);

/*
Helper functionality.
*/
Expand Down
22 changes: 22 additions & 0 deletions src/interfaces/src/plugin/data/interfaces/interface/ipv4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "ipv4.h"

int interfaces_interface_hash_element_ipv4_set_enabled(interfaces_interface_ipv4_t* ipv4, uint8_t enabled)
{
ipv4->enabled = enabled;

return 0;
}

int interfaces_interface_hash_element_ipv4_set_forwarding(interfaces_interface_ipv4_t* ipv4, uint8_t forwarding)
{
ipv4->forwarding = forwarding;

return 0;
}

int interfaces_interface_hash_element_ipv4_set_mtu(interfaces_interface_ipv4_t* ipv4, uint16_t mtu)
{
ipv4->mtu = mtu;

return 0;
}
10 changes: 10 additions & 0 deletions src/interfaces/src/plugin/data/interfaces/interface/ipv4.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef INTERFACES_PLUGIN_DATA_INTERFACES_INTERFACE_IPV4_H
#define INTERFACES_PLUGIN_DATA_INTERFACES_INTERFACE_IPV4_H

#include "plugin/types.h"

int interfaces_interface_hash_element_ipv4_set_enabled(interfaces_interface_ipv4_t* ipv4, uint8_t enabled);
int interfaces_interface_hash_element_ipv4_set_forwarding(interfaces_interface_ipv4_t* ipv4, uint8_t forwarding);
int interfaces_interface_hash_element_ipv4_set_mtu(interfaces_interface_ipv4_t* ipv4, uint16_t mtu);

#endif // INTERFACES_PLUGIN_DATA_INTERFACES_INTERFACE_IPV4_H
22 changes: 22 additions & 0 deletions src/interfaces/src/plugin/data/interfaces/interface/ipv6.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "ipv6.h"

int interfaces_interface_hash_element_ipv6_set_enabled(interfaces_interface_ipv6_t* ipv6, uint8_t enabled)
{
ipv6->enabled = enabled;

return 0;
}

int interfaces_interface_hash_element_ipv6_set_forwarding(interfaces_interface_ipv6_t* ipv6, uint8_t forwarding)
{
ipv6->forwarding = forwarding;

return 0;
}

int interfaces_interface_hash_element_ipv6_set_mtu(interfaces_interface_ipv6_t* ipv6, uint16_t mtu)
{
ipv6->mtu = mtu;

return 0;
}
10 changes: 10 additions & 0 deletions src/interfaces/src/plugin/data/interfaces/interface/ipv6.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef INTERFACES_PLUGIN_DATA_INTERFACES_INTERFACE_IPV6_H
#define INTERFACES_PLUGIN_DATA_INTERFACES_INTERFACE_IPV6_H

#include "plugin/types.h"

int interfaces_interface_hash_element_ipv6_set_enabled(interfaces_interface_ipv6_t* ipv6, uint8_t enabled);
int interfaces_interface_hash_element_ipv6_set_forwarding(interfaces_interface_ipv6_t* ipv6, uint8_t forwarding);
int interfaces_interface_hash_element_ipv6_set_mtu(interfaces_interface_ipv6_t* ipv6, uint16_t mtu);

#endif // INTERFACES_PLUGIN_DATA_INTERFACES_INTERFACE_IPV6_H

0 comments on commit 41e6ada

Please sign in to comment.