diff options
author | 2022-03-30 22:29:28 +0200 | |
---|---|---|
committer | 2022-03-31 19:51:47 +0200 | |
commit | c46e5df56b67bb8ea7a068d39324c640084ead2b (patch) | |
tree | eddeb17785938e09bc42eec98ee09b8a28846de6 /ctrl/sysrepo-plugins/hicn-plugin/plugin/ietf | |
parent | 18fa668f25d3cc5463417ce7df6637e31578e898 (diff) |
feat: boostrap hicn 22.02
The current patch provides several new features, improvements,
bug fixes and also complete rewrite of entire components.
- lib
The hicn packet parser has been improved with a new packet
format fully based on UDP. The TCP header is still temporarily
supported but the UDP header will replace completely the new hicn
packet format. Improvements have been made to make sure every
packet parsing operation is made via this library. The current
new header can be used as header between the payload and the
UDP header or as trailer in the UDP surplus area to be tested
when UDP options will start to be used.
- hicn-light
The portable packet forwarder has been completely rewritten from
scratch with the twofold objective to improve performance and
code size but also to drop dependencies such as libparc which is
now removed by the current implementation.
- hicn control
the control library is the agent that is used to program the
packet forwarders via their binary API. This component has
benefited from significant improvements in terms of interaction
model which is now event driven and more robust to failures.
- VPP plugin has been updated to support VPP 22.02
- transport
Major improvement have been made to the RTC protocol, to the
support of IO modules and to the security sub system. Signed
manifests are the default data authenticity and integrity framework.
Confidentiality can be enabled by sharing the encryption key to the
prod/cons layer. The library has been tested with group key based
applications such as broadcast/multicast and real-time on-line
meetings with trusted server keys or MLS.
- testing
Unit testing has been introduced using GoogleTest. One third of
the code base is covered by unit testing with priority on
critical features. Functional testing has also been introduce
using Docker, linux bridging and Robot Framework to define
test with Less Code techniques to facilitate the extension
of the coverage.
Co-authored-by: Mauro Sardara <msardara@cisco.com>
Co-authored-by: Jordan Augé <jordan.auge+fdio@cisco.com>
Co-authored-by: Michele Papalini <micpapal@cisco.com>
Co-authored-by: Angelo Mantellini <manangel@cisco.com>
Co-authored-by: Jacques Samain <jsamain@cisco.com>
Co-authored-by: Olivier Roques <oroques+fdio@cisco.com>
Co-authored-by: Enrico Loparco <eloparco@cisco.com>
Co-authored-by: Giulio Grassi <gigrassi@cisco.com>
Change-Id: I75d0ef70f86d921e3ef503c99271216ff583c215
Signed-off-by: Luca Muscariello <muscariello@ieee.org>
Signed-off-by: Mauro Sardara <msardara@cisco.com>
Diffstat (limited to 'ctrl/sysrepo-plugins/hicn-plugin/plugin/ietf')
-rw-r--r-- | ctrl/sysrepo-plugins/hicn-plugin/plugin/ietf/ietf_interface.c | 838 | ||||
-rw-r--r-- | ctrl/sysrepo-plugins/hicn-plugin/plugin/ietf/ietf_interface.h | 3 |
2 files changed, 427 insertions, 414 deletions
diff --git a/ctrl/sysrepo-plugins/hicn-plugin/plugin/ietf/ietf_interface.c b/ctrl/sysrepo-plugins/hicn-plugin/plugin/ietf/ietf_interface.c index 3e0c90cf9..9a74e6280 100644 --- a/ctrl/sysrepo-plugins/hicn-plugin/plugin/ietf/ietf_interface.c +++ b/ctrl/sysrepo-plugins/hicn-plugin/plugin/ietf/ietf_interface.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016-2020 Cisco and/or its affiliates. + * Copyright (c) 2021 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: @@ -29,11 +29,9 @@ #include "ietf_interface.h" #include "../hicn_vpp_comm.h" - DEFINE_VAPI_MSG_IDS_INTERFACE_API_JSON; -typedef struct hicn_interface_ -{ +typedef struct hicn_interface_ { u32 sw_if_index; char interface_name[VPP_INTFC_NAME_LEN]; u8 l2_address[VPP_MAC_ADDRESS_LEN]; @@ -44,207 +42,202 @@ typedef struct hicn_interface_ u8 link_up_down; } hicnIntfc; -typedef struct _ietf_sw_interface_dump_ctx -{ +typedef struct _ietf_sw_interface_dump_ctx { u8 last_called; int num_ifs; int capacity; - hicnIntfc * intfcArray; + hicnIntfc *intfcArray; } ietf_sw_interface_dump_ctx; static i32 ietf_setInterfaceFlags(u32 sw_if_index, u8 admin_up_down); -static i32 ietf_interface_name2index(const char *name, u32* if_index); +static i32 ietf_interface_name2index(const char *name, u32 *if_index); static i32 ietf_interface_add_del_addr(u32 sw_if_index, u8 is_add, u8 is_ipv6, u8 del_all, u8 address_length, u8 address[VPP_IP6_ADDRESS_LEN]); -static int ietf_initSwInterfaceDumpCTX(ietf_sw_interface_dump_ctx * dctx); -static int ietf_freeSwInterfaceDumpCTX(ietf_sw_interface_dump_ctx * dctx); +static int ietf_initSwInterfaceDumpCTX(ietf_sw_interface_dump_ctx *dctx); +static int ietf_freeSwInterfaceDumpCTX(ietf_sw_interface_dump_ctx *dctx); /** * @brief Helper function for converting netmask into prefix length. */ -static uint8_t -netmask_to_prefix(const char *netmask) -{ - in_addr_t n = 0; - uint8_t i = 0; +static uint8_t netmask_to_prefix(const char *netmask) { + in_addr_t n = 0; + uint8_t i = 0; - inet_pton(AF_INET, netmask, &n); + inet_pton(AF_INET, netmask, &n); - while (n > 0) { - n = n >> 1; - i++; - } + while (n > 0) { + n = n >> 1; + i++; + } - return i; + return i; } /** - * @brief Helper function for converting IPv4/IPv6 address string into binary representation. + * @brief Helper function for converting IPv4/IPv6 address string into binary + * representation. */ -static int -ip_addr_str_to_binary(const char *ip_address_str, uint8_t *ip_address_bin, bool is_ipv6) -{ - struct in6_addr addr6 = { 0, }; - struct in_addr addr4 = { 0, }; - int ret = 0; - - if (is_ipv6) { - ret = inet_pton(AF_INET6, ip_address_str, &(addr6)); - if (1 == ret) - { - memcpy(ip_address_bin, &addr6, sizeof(addr6)); - } - } else { - ret = inet_pton(AF_INET, ip_address_str, &(addr4)); - if (1 == ret) - { - memcpy(ip_address_bin, &addr4, sizeof(addr4)); - } +static int ip_addr_str_to_binary(const char *ip_address_str, + uint8_t *ip_address_bin, bool is_ipv6) { + struct in6_addr addr6 = { + 0, + }; + struct in_addr addr4 = { + 0, + }; + int ret = 0; + + if (is_ipv6) { + ret = inet_pton(AF_INET6, ip_address_str, &(addr6)); + if (1 == ret) { + memcpy(ip_address_bin, &addr6, sizeof(addr6)); + } + } else { + ret = inet_pton(AF_INET, ip_address_str, &(addr4)); + if (1 == ret) { + memcpy(ip_address_bin, &addr4, sizeof(addr4)); } + } - return ret; + return ret; } /** * @brief Enable or disable given interface. */ -static int -interface_enable_disable(const char *if_name, bool enable) -{ - uint32_t if_index = ~0; - int rc = 0; - - - /* get interface index */ - rc = ietf_interface_name2index(if_name, &if_index); - if (0 != rc) { - SRP_LOG_ERRMSG("Invalid interface name"); - return SR_ERR_INVAL_ARG; - } +static int interface_enable_disable(const char *if_name, bool enable) { + uint32_t if_index = ~0; + int rc = 0; + + /* get interface index */ + rc = ietf_interface_name2index(if_name, &if_index); + if (0 != rc) { + SRP_LOG_ERRMSG("Invalid interface name"); + return SR_ERR_INVAL_ARG; + } - /* enable/disable interface */ - rc = ietf_setInterfaceFlags(if_index, (uint8_t)enable); - if (0 != rc) { - SRP_LOG_ERRMSG("Error by processing of the sw_interface_set_flags request"); - return SR_ERR_OPERATION_FAILED; - } else { - return SR_ERR_OK; - } + /* enable/disable interface */ + rc = ietf_setInterfaceFlags(if_index, (uint8_t)enable); + if (0 != rc) { + SRP_LOG_ERRMSG("Error by processing of the sw_interface_set_flags request"); + return SR_ERR_OPERATION_FAILED; + } else { + return SR_ERR_OK; + } } - -vapi_error_e call_sw_interface_add_del_address(struct vapi_ctx_s *ctx, - void *callback_ctx, - vapi_error_e rv, - bool is_last, - vapi_payload_sw_interface_add_del_address_reply *reply){ -if(!reply->retval){ - SRP_LOG_DBGMSG("Successfully done"); - return VAPI_OK; - }else - return VAPI_EUSER; +vapi_error_e call_sw_interface_add_del_address( + struct vapi_ctx_s *ctx, void *callback_ctx, vapi_error_e rv, bool is_last, + vapi_payload_sw_interface_add_del_address_reply *reply) { + if (!reply->retval) { + SRP_LOG_DBGMSG("Successfully done"); + return VAPI_OK; + } else + return VAPI_EUSER; } -vapi_error_e call_sw_interface_set_flags(struct vapi_ctx_s *ctx, - void *callback_ctx, - vapi_error_e rv, - bool is_last, - vapi_payload_sw_interface_set_flags_reply *reply){ -if(!reply->retval){ - SRP_LOG_DBGMSG("Successfully done"); - return VAPI_OK; - }else - return VAPI_EUSER; +vapi_error_e call_sw_interface_set_flags( + struct vapi_ctx_s *ctx, void *callback_ctx, vapi_error_e rv, bool is_last, + vapi_payload_sw_interface_set_flags_reply *reply) { + if (!reply->retval) { + SRP_LOG_DBGMSG("Successfully done"); + return VAPI_OK; + } else + return VAPI_EUSER; } - - /** - * @brief Callback to be called by any config change of "/ietf-interfaces:interfaces/interface/enabled" leaf. + * @brief Callback to be called by any config change of + * "/ietf-interfaces:interfaces/interface/enabled" leaf. */ -static int -ietf_interface_enable_disable_cb(sr_session_ctx_t *session, const char *module_name, const char *xpath, sr_event_t event, - uint32_t request_id, void *private_data) -{ - char *if_name = NULL; - sr_change_iter_t *iter = NULL; - sr_change_oper_t op = SR_OP_CREATED; - sr_val_t *old_val = NULL; - sr_val_t *new_val = NULL; - sr_xpath_ctx_t xpath_ctx = { 0, }; - int rc = SR_ERR_OK, op_rc = SR_ERR_OK; - - /* no-op for apply, we only care about SR_EV_ENABLED, SR_EV_VERIFY, SR_EV_ABORT */ - if (SR_EV_DONE == event) { - return SR_ERR_OK; - } +static int ietf_interface_enable_disable_cb(sr_session_ctx_t *session, + const char *module_name, + const char *xpath, sr_event_t event, + uint32_t request_id, + void *private_data) { + char *if_name = NULL; + sr_change_iter_t *iter = NULL; + sr_change_oper_t op = SR_OP_CREATED; + sr_val_t *old_val = NULL; + sr_val_t *new_val = NULL; + sr_xpath_ctx_t xpath_ctx = { + 0, + }; + int rc = SR_ERR_OK, op_rc = SR_ERR_OK; + + /* no-op for apply, we only care about SR_EV_ENABLED, SR_EV_VERIFY, + * SR_EV_ABORT */ + if (SR_EV_DONE == event) { + return SR_ERR_OK; + } - /* get changes iterator */ - rc = sr_get_changes_iter(session, xpath, &iter); - if (SR_ERR_OK != rc) { - SRP_LOG_ERRMSG("Unable to retrieve change iterator"); - return rc; - } + /* get changes iterator */ + rc = sr_get_changes_iter(session, xpath, &iter); + if (SR_ERR_OK != rc) { + SRP_LOG_ERRMSG("Unable to retrieve change iterator"); + return rc; + } - /* iterate over all changes */ - while ((SR_ERR_OK == op_rc || event == SR_EV_ABORT) && - (SR_ERR_OK == (rc = sr_get_change_next(session, iter, &op, &old_val, &new_val)))) { - - SRP_LOG_DBGMSG("A change detected"); - if_name = sr_xpath_key_value(new_val ? new_val->xpath : old_val->xpath, "interface", "name", &xpath_ctx); - switch (op) { - case SR_OP_CREATED: - case SR_OP_MODIFIED: - op_rc = interface_enable_disable(if_name, new_val->data.bool_val); - break; - case SR_OP_DELETED: - op_rc = interface_enable_disable(if_name, false /* !enable */); - break; - default: - break; - } - sr_xpath_recover(&xpath_ctx); - if (SR_ERR_INVAL_ARG == op_rc) { - sr_set_error(session, "Invalid interface name.", new_val ? new_val->xpath : old_val->xpath); - } - sr_free_val(old_val); - sr_free_val(new_val); + /* iterate over all changes */ + while ((SR_ERR_OK == op_rc || event == SR_EV_ABORT) && + (SR_ERR_OK == + (rc = sr_get_change_next(session, iter, &op, &old_val, &new_val)))) { + SRP_LOG_DBGMSG("A change detected"); + if_name = sr_xpath_key_value(new_val ? new_val->xpath : old_val->xpath, + "interface", "name", &xpath_ctx); + switch (op) { + case SR_OP_CREATED: + case SR_OP_MODIFIED: + op_rc = interface_enable_disable(if_name, new_val->data.bool_val); + break; + case SR_OP_DELETED: + op_rc = interface_enable_disable(if_name, false /* !enable */); + break; + default: + break; } - sr_free_change_iter(iter); + sr_xpath_recover(&xpath_ctx); + if (SR_ERR_INVAL_ARG == op_rc) { + sr_set_error(session, "Invalid interface name.", + new_val ? new_val->xpath : old_val->xpath); + } + sr_free_val(old_val); + sr_free_val(new_val); + } + sr_free_change_iter(iter); - return op_rc; + return op_rc; } /** * @brief Add or remove IPv4/IPv6 address to/from an interface. */ -static int -interface_ipv46_config_add_remove(const char *if_name, uint8_t *addr, uint8_t prefix, bool is_ipv6, bool add) -{ - uint32_t if_index = ~0; - int rc = 0; - - /* get interface index */ - rc = ietf_interface_name2index(if_name, &if_index); - if (0 != rc) { - SRP_LOG_ERRMSG("Invalid interface name"); - return SR_ERR_INVAL_ARG; - } +static int interface_ipv46_config_add_remove(const char *if_name, uint8_t *addr, + uint8_t prefix, bool is_ipv6, + bool add) { + uint32_t if_index = ~0; + int rc = 0; + + /* get interface index */ + rc = ietf_interface_name2index(if_name, &if_index); + if (0 != rc) { + SRP_LOG_ERRMSG("Invalid interface name"); + return SR_ERR_INVAL_ARG; + } - /* add del addr */ - rc = ietf_interface_add_del_addr(if_index, (uint8_t)add, (uint8_t)is_ipv6, 0, prefix, addr); - if (0 != rc) { - SRP_LOG_ERRMSG("Error by processing of the sw_interface_set_flags request"); - return SR_ERR_OPERATION_FAILED; - } else { - return SR_ERR_OK; - } + /* add del addr */ + rc = ietf_interface_add_del_addr(if_index, (uint8_t)add, (uint8_t)is_ipv6, 0, + prefix, addr); + if (0 != rc) { + SRP_LOG_ERRMSG("Error by processing of the sw_interface_set_flags request"); + return SR_ERR_OPERATION_FAILED; + } else { + return SR_ERR_OK; + } } -int ietf_initSwInterfaceDumpCTX(ietf_sw_interface_dump_ctx * dctx) -{ - if(dctx == NULL) - return -1; +int ietf_initSwInterfaceDumpCTX(ietf_sw_interface_dump_ctx *dctx) { + if (dctx == NULL) return -1; dctx->intfcArray = NULL; dctx->last_called = false; @@ -252,66 +245,56 @@ int ietf_initSwInterfaceDumpCTX(ietf_sw_interface_dump_ctx * dctx) dctx->num_ifs = 0; return 0; } -int ietf_freeSwInterfaceDumpCTX(ietf_sw_interface_dump_ctx * dctx) -{ - if(dctx == NULL) - return -1; - - if(dctx->intfcArray != NULL) - { - free(dctx->intfcArray); - } +int ietf_freeSwInterfaceDumpCTX(ietf_sw_interface_dump_ctx *dctx) { + if (dctx == NULL) return -1; + + if (dctx->intfcArray != NULL) { + free(dctx->intfcArray); + } return ietf_initSwInterfaceDumpCTX(dctx); } -vapi_error_e -ietf_sw_interface_dump_cb (struct vapi_ctx_s *ctx, void *callback_ctx, - vapi_error_e rv, bool is_last, - vapi_payload_sw_interface_details * reply) -{ +vapi_error_e ietf_sw_interface_dump_cb( + struct vapi_ctx_s *ctx, void *callback_ctx, vapi_error_e rv, bool is_last, + vapi_payload_sw_interface_details *reply) { ietf_sw_interface_dump_ctx *dctx = callback_ctx; - if (is_last) - { - dctx->last_called = true; + if (is_last) { + dctx->last_called = true; + } else { + if (dctx->capacity == 0 && dctx->intfcArray == NULL) { + dctx->capacity = 10; + dctx->intfcArray = + (hicnIntfc *)malloc(sizeof(hicnIntfc) * dctx->capacity); + } + if (dctx->num_ifs >= dctx->capacity - 1) { + dctx->capacity += 10; + dctx->intfcArray = (hicnIntfc *)realloc( + dctx->intfcArray, sizeof(hicnIntfc) * dctx->capacity); } - else - { - if(dctx->capacity == 0 && dctx->intfcArray == NULL) - { - dctx->capacity = 10; - dctx->intfcArray = (hicnIntfc*)malloc( sizeof(hicnIntfc)*dctx->capacity ); - } - if(dctx->num_ifs >= dctx->capacity-1) - { - - dctx->capacity += 10; - dctx->intfcArray = (hicnIntfc*)realloc(dctx->intfcArray, sizeof(hicnIntfc)*dctx->capacity ); - } - hicnIntfc * thisIntfc = &dctx->intfcArray[dctx->num_ifs]; + hicnIntfc *thisIntfc = &dctx->intfcArray[dctx->num_ifs]; - thisIntfc->sw_if_index = reply->sw_if_index; - memcpy(thisIntfc->interface_name, reply->interface_name, VPP_INTFC_NAME_LEN); - memcpy(thisIntfc->l2_address, reply->l2_address, VPP_MAC_ADDRESS_LEN ); - //thisIntfc->link_speed = reply->link_speed; + thisIntfc->sw_if_index = reply->sw_if_index; + memcpy(thisIntfc->interface_name, reply->interface_name, + VPP_INTFC_NAME_LEN); + memcpy(thisIntfc->l2_address, reply->l2_address, VPP_MAC_ADDRESS_LEN); + // thisIntfc->link_speed = reply->link_speed; #define ONE_MEGABIT (uint64_t)1000000 - switch (reply->link_speed << VNET_HW_INTERFACE_FLAG_SPEED_SHIFT) - { - default: - thisIntfc->link_speed = 0; - break; - } + switch (reply->link_speed << VNET_HW_INTERFACE_FLAG_SPEED_SHIFT) { + default: + thisIntfc->link_speed = 0; + break; + } - thisIntfc->link_mtu = reply->link_mtu; - thisIntfc->flags = reply->flags; + thisIntfc->link_mtu = reply->link_mtu; + thisIntfc->flags = reply->flags; - dctx->num_ifs += 1; - } + dctx->num_ifs += 1; + } return VAPI_OK; } -static i32 ietf_interface_name2index(const char *name, u32* if_index) -{ +static i32 ietf_interface_name2index(const char *name, u32 *if_index) { ARG_CHECK2(-1, name, if_index); i32 ret = -1; @@ -324,14 +307,14 @@ static i32 ietf_interface_name2index(const char *name, u32* if_index) dump->payload.name_filter_valid = true; memcpy(&dump->payload.name_filter, name, sizeof(dump->payload.name_filter)); - while (VAPI_EAGAIN == (rv = vapi_sw_interface_dump(g_vapi_ctx_instance, dump, ietf_sw_interface_dump_cb, &dctx))) + while (VAPI_EAGAIN == + (rv = vapi_sw_interface_dump(g_vapi_ctx_instance, dump, + ietf_sw_interface_dump_cb, &dctx))) ; int i = 0; - for (; i < dctx.num_ifs; ++i) - { - if (strcmp(dctx.intfcArray[i].interface_name, name) == 0) - { + for (; i < dctx.num_ifs; ++i) { + if (strcmp(dctx.intfcArray[i].interface_name, name) == 0) { *if_index = dctx.intfcArray[i].sw_if_index; ret = 0; break; @@ -342,11 +325,12 @@ static i32 ietf_interface_name2index(const char *name, u32* if_index) return ret; } -i32 ietf_interface_add_del_addr( u32 sw_if_index, u8 is_add, u8 is_ipv6, u8 del_all, - u8 address_length, u8 address[VPP_IP6_ADDRESS_LEN] ) -{ +i32 ietf_interface_add_del_addr(u32 sw_if_index, u8 is_add, u8 is_ipv6, + u8 del_all, u8 address_length, + u8 address[VPP_IP6_ADDRESS_LEN]) { i32 ret = -1; - vapi_msg_sw_interface_add_del_address *msg = vapi_alloc_sw_interface_add_del_address(g_vapi_ctx_instance); + vapi_msg_sw_interface_add_del_address *msg = + vapi_alloc_sw_interface_add_del_address(g_vapi_ctx_instance); msg->payload.sw_if_index = sw_if_index; msg->payload.is_add = is_add; msg->payload.prefix.address.af = is_ipv6 ? ADDRESS_IP6 : ADDRESS_IP4; @@ -354,7 +338,9 @@ i32 ietf_interface_add_del_addr( u32 sw_if_index, u8 is_add, u8 is_ipv6, u8 del_ msg->payload.prefix.len = address_length; memcpy(msg->payload.prefix.address.un.ip6, address, VPP_IP6_ADDRESS_LEN); - if(vapi_sw_interface_add_del_address(g_vapi_ctx_instance,msg,call_sw_interface_add_del_address,NULL)!=VAPI_OK){ + if (vapi_sw_interface_add_del_address(g_vapi_ctx_instance, msg, + call_sw_interface_add_del_address, + NULL) != VAPI_OK) { SRP_LOG_DBGMSG("Operation failed"); return SR_ERR_OPERATION_FAILED; } @@ -362,246 +348,274 @@ i32 ietf_interface_add_del_addr( u32 sw_if_index, u8 is_add, u8 is_ipv6, u8 del_ return ret; } -i32 ietf_setInterfaceFlags(u32 sw_if_index, u8 admin_up_down) -{ +i32 ietf_setInterfaceFlags(u32 sw_if_index, u8 admin_up_down) { i32 ret = -1; - vapi_msg_sw_interface_set_flags *msg = vapi_alloc_sw_interface_set_flags(g_vapi_ctx_instance); + vapi_msg_sw_interface_set_flags *msg = + vapi_alloc_sw_interface_set_flags(g_vapi_ctx_instance); msg->payload.sw_if_index = sw_if_index; - msg->payload.flags = admin_up_down? IF_STATUS_API_FLAG_ADMIN_UP : 0; + msg->payload.flags = admin_up_down ? IF_STATUS_API_FLAG_ADMIN_UP : 0; - if(vapi_sw_interface_set_flags(g_vapi_ctx_instance,msg,call_sw_interface_set_flags,NULL)!=VAPI_OK){ + if (vapi_sw_interface_set_flags(g_vapi_ctx_instance, msg, + call_sw_interface_set_flags, + NULL) != VAPI_OK) { SRP_LOG_DBGMSG("Operation failed"); return SR_ERR_OPERATION_FAILED; } - return ret; } - /** * @brief Modify existing IPv4/IPv6 config on an interface. */ -static int -interface_ipv46_config_modify(sr_session_ctx_t *session, const char *if_name, - sr_val_t *old_val, sr_val_t *new_val, bool is_ipv6) -{ - sr_xpath_ctx_t xpath_ctx = { 0, }; - char *addr_str = NULL; - uint8_t addr[16] = { 0, }; - uint8_t prefix = 0; - int rc = SR_ERR_OK; - - SRP_LOG_DBGMSG("Updating IP config on interface"); - - /* get old config to be deleted */ - if (SR_UINT8_T == old_val->type) { - prefix = old_val->data.uint8_val; - } else if (SR_STRING_T == old_val->type) { - prefix = netmask_to_prefix(old_val->data.string_val); - } else { - return SR_ERR_INVAL_ARG; - } - addr_str = sr_xpath_key_value((char*)old_val->xpath, "address", "ip", &xpath_ctx); - ip_addr_str_to_binary(addr_str, addr, is_ipv6); - sr_xpath_recover(&xpath_ctx); - - /* delete old IP config */ - rc = interface_ipv46_config_add_remove(if_name, addr, prefix, is_ipv6, false /* remove */); - if (SR_ERR_OK != rc) { - SRP_LOG_ERRMSG("Unable to remove old IP address config"); - return rc; - } - - /* update the config with the new value */ - if (sr_xpath_node_name_eq(new_val->xpath, "prefix-length")) { - prefix = new_val->data.uint8_val; - } else if (sr_xpath_node_name_eq(new_val->xpath, "netmask")) { - prefix = netmask_to_prefix(new_val->data.string_val); - } +static int interface_ipv46_config_modify(sr_session_ctx_t *session, + const char *if_name, sr_val_t *old_val, + sr_val_t *new_val, bool is_ipv6) { + sr_xpath_ctx_t xpath_ctx = { + 0, + }; + char *addr_str = NULL; + uint8_t addr[16] = { + 0, + }; + uint8_t prefix = 0; + int rc = SR_ERR_OK; + + SRP_LOG_DBGMSG("Updating IP config on interface"); + + /* get old config to be deleted */ + if (SR_UINT8_T == old_val->type) { + prefix = old_val->data.uint8_val; + } else if (SR_STRING_T == old_val->type) { + prefix = netmask_to_prefix(old_val->data.string_val); + } else { + return SR_ERR_INVAL_ARG; + } + addr_str = + sr_xpath_key_value((char *)old_val->xpath, "address", "ip", &xpath_ctx); + ip_addr_str_to_binary(addr_str, addr, is_ipv6); + sr_xpath_recover(&xpath_ctx); + + /* delete old IP config */ + rc = interface_ipv46_config_add_remove(if_name, addr, prefix, is_ipv6, + false /* remove */); + if (SR_ERR_OK != rc) { + SRP_LOG_ERRMSG("Unable to remove old IP address config"); + return rc; + } - /* set new IP config */ - rc = interface_ipv46_config_add_remove(if_name, addr, prefix, is_ipv6, true /* add */); - if (SR_ERR_OK != rc) { - SRP_LOG_ERRMSG("Unable to remove old IP address config"); - return rc; - } + /* update the config with the new value */ + if (sr_xpath_node_name_eq(new_val->xpath, "prefix-length")) { + prefix = new_val->data.uint8_val; + } else if (sr_xpath_node_name_eq(new_val->xpath, "netmask")) { + prefix = netmask_to_prefix(new_val->data.string_val); + } + /* set new IP config */ + rc = interface_ipv46_config_add_remove(if_name, addr, prefix, is_ipv6, + true /* add */); + if (SR_ERR_OK != rc) { + SRP_LOG_ERRMSG("Unable to remove old IP address config"); return rc; + } + + return rc; } /** - * @brief Callback to be called by any config change in subtrees "/ietf-interfaces:interfaces/interface/ietf-ip:ipv4/address" - * or "/ietf-interfaces:interfaces/interface/ietf-ip:ipv6/address". + * @brief Callback to be called by any config change in subtrees + * "/ietf-interfaces:interfaces/interface/ietf-ip:ipv4/address" or + * "/ietf-interfaces:interfaces/interface/ietf-ip:ipv6/address". */ -static int -ietf_interface_ipv46_address_change_cb(sr_session_ctx_t *session, const char *module_name, const char *xpath, sr_event_t event, - uint32_t request_id, void *private_data) -{ - sr_change_iter_t *iter = NULL; - sr_change_oper_t op = SR_OP_CREATED; - sr_val_t *old_val = NULL; - sr_val_t *new_val = NULL; - sr_xpath_ctx_t xpath_ctx = { 0 }; - bool is_ipv6 = false, has_addr = false, has_prefix = false; - uint8_t addr[16] = { 0, }; - uint8_t prefix = 0; - char *node_name = NULL, *if_name = NULL; - int rc = SR_ERR_OK, op_rc = SR_ERR_OK; - - /* no-op for apply, we only care about SR_EV_ENABLED, SR_EV_VERIFY, SR_EV_ABORT */ - if (SR_EV_DONE == event) { - return SR_ERR_OK; - } +static int ietf_interface_ipv46_address_change_cb( + sr_session_ctx_t *session, const char *module_name, const char *xpath, + sr_event_t event, uint32_t request_id, void *private_data) { + sr_change_iter_t *iter = NULL; + sr_change_oper_t op = SR_OP_CREATED; + sr_val_t *old_val = NULL; + sr_val_t *new_val = NULL; + sr_xpath_ctx_t xpath_ctx = {0}; + bool is_ipv6 = false, has_addr = false, has_prefix = false; + uint8_t addr[16] = { + 0, + }; + uint8_t prefix = 0; + char *node_name = NULL, *if_name = NULL; + int rc = SR_ERR_OK, op_rc = SR_ERR_OK; + + /* no-op for apply, we only care about SR_EV_ENABLED, SR_EV_VERIFY, + * SR_EV_ABORT */ + if (SR_EV_DONE == event) { + return SR_ERR_OK; + } - /* check whether we are handling ipv4 or ipv6 config */ - node_name = sr_xpath_node_idx((char*)xpath, 2, &xpath_ctx); - if (NULL != node_name && 0 == strcmp(node_name, "ipv6")) { - is_ipv6 = true; - } - sr_xpath_recover(&xpath_ctx); + /* check whether we are handling ipv4 or ipv6 config */ + node_name = sr_xpath_node_idx((char *)xpath, 2, &xpath_ctx); + if (NULL != node_name && 0 == strcmp(node_name, "ipv6")) { + is_ipv6 = true; + } + sr_xpath_recover(&xpath_ctx); - /* get changes iterator */ - rc = sr_get_changes_iter(session, xpath, &iter); - if (SR_ERR_OK != rc) { - // SRP_LOG_ERR("Unable to retrieve change iterator: %s", sr_strerror(rc)); - return rc; - } + /* get changes iterator */ + rc = sr_get_changes_iter(session, xpath, &iter); + if (SR_ERR_OK != rc) { + // SRP_LOG_ERR("Unable to retrieve change iterator: %s", sr_strerror(rc)); + return rc; + } - /* iterate over all changes */ - while ((SR_ERR_OK == op_rc || event == SR_EV_ABORT) && - (SR_ERR_OK == (rc = sr_get_change_next(session, iter, &op, &old_val, &new_val)))) { - - if_name = strdup(sr_xpath_key_value(new_val ? new_val->xpath : old_val->xpath, "interface", "name", &xpath_ctx)); - sr_xpath_recover(&xpath_ctx); - - switch (op) { - case SR_OP_CREATED: - if (SR_LIST_T == new_val->type) { - /* create on list item - reset state vars */ - has_addr = has_prefix = false; - } else { - if (sr_xpath_node_name_eq(new_val->xpath, "ip")) { - ip_addr_str_to_binary(new_val->data.string_val, addr, is_ipv6); - has_addr = true; - } else if (sr_xpath_node_name_eq(new_val->xpath, "prefix-length")) { - prefix = new_val->data.uint8_val; - has_prefix = true; - } else if (sr_xpath_node_name_eq(new_val->xpath, "netmask")) { - prefix = netmask_to_prefix(new_val->data.string_val); - has_prefix = true; - } - if (has_addr && has_prefix) { - op_rc = interface_ipv46_config_add_remove(if_name, addr, prefix, is_ipv6, true /* add */); - } - } - break; - case SR_OP_MODIFIED: - op_rc = interface_ipv46_config_modify(session, if_name, old_val, new_val, is_ipv6); - break; - case SR_OP_DELETED: - if (SR_LIST_T == old_val->type) { - /* delete on list item - reset state vars */ - has_addr = has_prefix = false; - } else { - if (sr_xpath_node_name_eq(old_val->xpath, "ip")) { - ip_addr_str_to_binary(old_val->data.string_val, addr, is_ipv6); - has_addr = true; - } else if (sr_xpath_node_name_eq(old_val->xpath, "prefix-length")) { - prefix = old_val->data.uint8_val; - has_prefix = true; - } else if (sr_xpath_node_name_eq(old_val->xpath, "netmask")) { - prefix = netmask_to_prefix(old_val->data.string_val); - has_prefix = true; - } - if (has_addr && has_prefix) { - op_rc = interface_ipv46_config_add_remove(if_name, addr, prefix, is_ipv6, false /* !add */); - } - } - break; - default: - break; + /* iterate over all changes */ + while ((SR_ERR_OK == op_rc || event == SR_EV_ABORT) && + (SR_ERR_OK == + (rc = sr_get_change_next(session, iter, &op, &old_val, &new_val)))) { + if_name = + strdup(sr_xpath_key_value(new_val ? new_val->xpath : old_val->xpath, + "interface", "name", &xpath_ctx)); + sr_xpath_recover(&xpath_ctx); + + switch (op) { + case SR_OP_CREATED: + if (SR_LIST_T == new_val->type) { + /* create on list item - reset state vars */ + has_addr = has_prefix = false; + } else { + if (sr_xpath_node_name_eq(new_val->xpath, "ip")) { + ip_addr_str_to_binary(new_val->data.string_val, addr, is_ipv6); + has_addr = true; + } else if (sr_xpath_node_name_eq(new_val->xpath, "prefix-length")) { + prefix = new_val->data.uint8_val; + has_prefix = true; + } else if (sr_xpath_node_name_eq(new_val->xpath, "netmask")) { + prefix = netmask_to_prefix(new_val->data.string_val); + has_prefix = true; + } + if (has_addr && has_prefix) { + op_rc = interface_ipv46_config_add_remove(if_name, addr, prefix, + is_ipv6, true /* add */); + } } - if (SR_ERR_INVAL_ARG == op_rc) { - sr_set_error(session, "Invalid interface name.", new_val ? new_val->xpath : old_val->xpath); + break; + case SR_OP_MODIFIED: + op_rc = interface_ipv46_config_modify(session, if_name, old_val, + new_val, is_ipv6); + break; + case SR_OP_DELETED: + if (SR_LIST_T == old_val->type) { + /* delete on list item - reset state vars */ + has_addr = has_prefix = false; + } else { + if (sr_xpath_node_name_eq(old_val->xpath, "ip")) { + ip_addr_str_to_binary(old_val->data.string_val, addr, is_ipv6); + has_addr = true; + } else if (sr_xpath_node_name_eq(old_val->xpath, "prefix-length")) { + prefix = old_val->data.uint8_val; + has_prefix = true; + } else if (sr_xpath_node_name_eq(old_val->xpath, "netmask")) { + prefix = netmask_to_prefix(old_val->data.string_val); + has_prefix = true; + } + if (has_addr && has_prefix) { + op_rc = interface_ipv46_config_add_remove( + if_name, addr, prefix, is_ipv6, false /* !add */); + } } - free(if_name); - sr_free_val(old_val); - sr_free_val(new_val); + break; + default: + break; + } + if (SR_ERR_INVAL_ARG == op_rc) { + sr_set_error(session, "Invalid interface name.", + new_val ? new_val->xpath : old_val->xpath); } - sr_free_change_iter(iter); + free(if_name); + sr_free_val(old_val); + sr_free_val(new_val); + } + sr_free_change_iter(iter); - return op_rc; + return op_rc; } /** - * @brief Callback to be called by any config change under "/ietf-interfaces:interfaces-state/interface" path. - * Does not provide any functionality, needed just to cover not supported config leaves. + * @brief Callback to be called by any config change under + * "/ietf-interfaces:interfaces-state/interface" path. Does not provide any + * functionality, needed just to cover not supported config leaves. */ -static int -ietf_interface_change_cb(sr_session_ctx_t *session, const char *module_name, const char *xpath, sr_event_t event, - uint32_t request_id, void *private_data) -{ - return SR_ERR_OK; +static int ietf_interface_change_cb(sr_session_ctx_t *session, + const char *module_name, const char *xpath, + sr_event_t event, uint32_t request_id, + void *private_data) { + return SR_ERR_OK; } - int ietf_subscribe_events(sr_session_ctx_t *session, - sr_subscription_ctx_t **subscription){ - - int rc = SR_ERR_OK; - SRP_LOG_DBGMSG("Subscriging ietf."); - - // - rc = sr_module_change_subscribe(session, "ietf-interfaces","/ietf-interfaces:interfaces/interface", ietf_interface_change_cb, - NULL, - 0, SR_SUBSCR_CTX_REUSE | SR_SUBSCR_ENABLED, subscription); - - if (rc != SR_ERR_OK) { - SRP_LOG_DBGMSG("Problem in subscription /ietf-interfaces:interfaces/interface\n"); - goto error; - } - - rc = sr_module_change_subscribe(session, "ietf-interfaces","/ietf-interfaces:interfaces/interface/enabled", ietf_interface_enable_disable_cb, - NULL, - 100, SR_SUBSCR_CTX_REUSE | SR_SUBSCR_ENABLED, subscription); - if (rc != SR_ERR_OK) { - SRP_LOG_DBGMSG("Problem in subscription /ietf-interfaces:interfaces/interface/enabled\n"); - goto error; - } - - //rc = sr_module_change_subscribe(session, "ietf-interfaces","/ietf-interfaces:interfaces/interface/ietf-ip:ipv4/address", ietf_interface_ipv46_address_change_cb, - rc = sr_module_change_subscribe(session, "ietf-interfaces","/ietf-interfaces:interfaces", ietf_interface_ipv46_address_change_cb, - NULL, - 99, SR_SUBSCR_CTX_REUSE | SR_SUBSCR_ENABLED, subscription); - - if (rc != SR_ERR_OK) { - SRP_LOG_DBGMSG("Problem in subscription /ietf-interfaces:interfaces/interface/ietf-ip:ipv4/address\n"); - goto error; - } + sr_subscription_ctx_t **subscription) { + int rc = SR_ERR_OK; + SRP_LOG_DBGMSG("Subscriging ietf."); + + // + rc = sr_module_change_subscribe( + session, "ietf-interfaces", "/ietf-interfaces:interfaces/interface", + ietf_interface_change_cb, NULL, 0, + SR_SUBSCR_CTX_REUSE | SR_SUBSCR_ENABLED, subscription); + + if (rc != SR_ERR_OK) { + SRP_LOG_DBGMSG( + "Problem in subscription /ietf-interfaces:interfaces/interface\n"); + goto error; + } - //rc = sr_module_change_subscribe(session, "ietf-interfaces","/ietf-interfaces:interfaces/interface/ietf-ip:ipv6/address", ietf_interface_ipv46_address_change_cb, - // NULL, - // 98, SR_SUBSCR_CTX_REUSE | SR_SUBSCR_ENABLED, subscription); + rc = sr_module_change_subscribe( + session, "ietf-interfaces", + "/ietf-interfaces:interfaces/interface/enabled", + ietf_interface_enable_disable_cb, NULL, 100, + SR_SUBSCR_CTX_REUSE | SR_SUBSCR_ENABLED, subscription); + if (rc != SR_ERR_OK) { + SRP_LOG_DBGMSG( + "Problem in subscription " + "/ietf-interfaces:interfaces/interface/enabled\n"); + goto error; + } - if (rc != SR_ERR_OK) { - SRP_LOG_DBGMSG("Problem in subscription /ietf-interfaces:interfaces/interface/ietf-ip:ipv6/address\n"); - goto error; - } + // rc = sr_module_change_subscribe(session, + // "ietf-interfaces","/ietf-interfaces:interfaces/interface/ietf-ip:ipv4/address", + // ietf_interface_ipv46_address_change_cb, + rc = sr_module_change_subscribe( + session, "ietf-interfaces", "/ietf-interfaces:interfaces", + ietf_interface_ipv46_address_change_cb, NULL, 99, + SR_SUBSCR_CTX_REUSE | SR_SUBSCR_ENABLED, subscription); + + if (rc != SR_ERR_OK) { + SRP_LOG_DBGMSG( + "Problem in subscription " + "/ietf-interfaces:interfaces/interface/ietf-ip:ipv4/address\n"); + goto error; + } - /* rc = sr_dp_get_items_subscribe(session, "/ietf-interfaces:interfaces-state", ietf_interface_state_cb, - NULL, SR_SUBSCR_CTX_REUSE, - subscription); - if (rc != SR_ERR_OK) { - SRP_LOG_DBG_MSG("Problem in subscription /ietf-interfaces:interfaces-state\n"); + // rc = sr_module_change_subscribe(session, + // "ietf-interfaces","/ietf-interfaces:interfaces/interface/ietf-ip:ipv6/address", + // ietf_interface_ipv46_address_change_cb, + // NULL, + // 98, SR_SUBSCR_CTX_REUSE | + // SR_SUBSCR_ENABLED, subscription); + + if (rc != SR_ERR_OK) { + SRP_LOG_DBGMSG( + "Problem in subscription " + "/ietf-interfaces:interfaces/interface/ietf-ip:ipv6/address\n"); goto error; - }*/ + } + /* rc = sr_dp_get_items_subscribe(session, + "/ietf-interfaces:interfaces-state", ietf_interface_state_cb, NULL, + SR_SUBSCR_CTX_REUSE, subscription); if (rc != SR_ERR_OK) { + SRP_LOG_DBG_MSG("Problem in subscription + /ietf-interfaces:interfaces-state\n"); goto error; + }*/ SRP_LOG_INFMSG("ietf initialized successfully."); return SR_ERR_OK; - error: +error: SRP_LOG_ERRMSG("Error by initialization of the ietf."); return rc; } diff --git a/ctrl/sysrepo-plugins/hicn-plugin/plugin/ietf/ietf_interface.h b/ctrl/sysrepo-plugins/hicn-plugin/plugin/ietf/ietf_interface.h index 0f6c7f8b4..cf5430464 100644 --- a/ctrl/sysrepo-plugins/hicn-plugin/plugin/ietf/ietf_interface.h +++ b/ctrl/sysrepo-plugins/hicn-plugin/plugin/ietf/ietf_interface.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018 HUACHENTEL and/or its affiliates. + * Copyright (c) 2021 HUACHENTEL and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: @@ -19,5 +19,4 @@ int ietf_subscribe_events(sr_session_ctx_t *session, sr_subscription_ctx_t **subscription); - #endif /* __IETF_INTERFACE_H__ */ |