aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/sys_util.h
diff options
context:
space:
mode:
authorYohanPipereau <ypiperea@cisco.com>2019-03-06 14:01:58 +0100
committerYohanPipereau <ypiperea@cisco.com>2019-03-29 14:39:36 +0100
commita760dfb253161911fc3aa3c8b879c461d53ade6e (patch)
tree0e79953f4ed5615879a58f49a74df3f9c6739a42 /src/plugins/sys_util.h
parent2b9b6b9b130b75799a40989c0ebe5040fa3e45fb (diff)
Sweetcomb global cleanup
-Merge IETF and Openconfig to use SCVPP 2 -Move L2 bridge from sc_vpp_interface to sc_vpp_v3po -Implement tav2 dump -Make openconfig-interfaces functions static -Try one more dispatch after failure in VAPI_CALL -Add error return code for scvpp -Remove unused length maccros -Return appropriate error code for interface dump when interface not found -Improve scvpp test suite -Change get_interface_id prototype -Use interface_dump_iface in openconfig_interface. -No more vapi types in openconfig_interfaces.c -Move openconfig_local_routing VAPI operations to sc_vpp_ip -Implement a multiple dump with a stack data structure -Comment out state_cb code from openconfig_local_routing to use new functions later. -Rename YANG model to their fully qualified name : <module>@<revision> -Remove headers almost empty and put registration declaration in sc_model.h -Shorten vapi context global variable name -Reorganize scvpp unit test suite -Add instructions to Makefile to install/uninstall YANG models in sysrepo. -Add this new instructions to README.md. -Reimplement interface_dump_all -Use a common message at INFO Log Level to know when sysrepo callbacks are triggered -Remove old structure to perform dump of all interfaces -Reimplement get_interface_name and add scvpp test for it -Clean sys_util -Use UNUSED maccro everywhere to have lighter prototypes -Have ietf-interfaces:interfaces-state work with new dump function -Add setup and teardown for NAT tests -Remove unused tapv2 dump -Remove useless sysrepo module callback -Remove xpath_find_first_key usage in openconfig-interfaces -Remove xpath_find_first_key in oc_local_routing and in the rest of sweetcomb -Reorganize scvpp include dir and fix scvpp_test new warnings -Fix scvpp tests for ip routes -Factorize scvpp nat and test return code of its function -Correct test_dump_if_all if there is an existing hardware interface -Implement a per-prefix dump in scvpp -free changes iterator in ietf-interfaces -Add new XPATH in oc local-routing -Introduce helper methods for sysrepo config callbacks -Factorize config callback -Refactor the openconfig-local-routing config callback -Use common foreach_change to iterate over changes in all models -Create a sample directory gathering example of configurations supposed to work with sweetcomb -Fix state callback of oc-local-routing -Add new sample for get operation on next-hop -foreach_elt maccro condition forgets to read one element Change-Id: I8e87fce577a00337977588f057a6e095a20f457c Signed-off-by: YohanPipereau <ypiperea@cisco.com>
Diffstat (limited to 'src/plugins/sys_util.h')
-rw-r--r--src/plugins/sys_util.h163
1 files changed, 145 insertions, 18 deletions
diff --git a/src/plugins/sys_util.h b/src/plugins/sys_util.h
index d449712..692a012 100644
--- a/src/plugins/sys_util.h
+++ b/src/plugins/sys_util.h
@@ -19,20 +19,71 @@
#include <sysrepo.h>
#include <sysrepo/xpath.h>
+#include <sysrepo/plugins.h>
+
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include <string.h>
+
+#include <scvpp/comm.h>
+
+/* BEGIN sysrepo utils */
+
+#define foreach_change(ds, it, oper, old, new) \
+ while( (event != SR_EV_ABORT) && \
+ sr_get_change_next(ds, it, &oper, &old, &new) == SR_ERR_OK)
#define XPATH_SIZE 2000
+#define NOT_AVAL "NOT AVAILABLE"
+
+static inline int
+get_xpath_key(char *dst, char *xpath, char *node, char *key, int length,
+ sr_xpath_ctx_t *state) {
+ char *tmp;
+
+ tmp = sr_xpath_key_value(xpath, node, key, state);
+ if (!tmp) {
+ SRP_LOG_ERR("%s %s not found.", node, key);
+ return SR_ERR_INVAL_ARG;
+ }
+ strncpy(dst, tmp, length);
+ sr_xpath_recover(state);
+
+ return 0;
+}
+
+/* END of sysrepo utils */
typedef struct
{
- char xpath_root[XPATH_SIZE];
- sr_val_t * values;
- size_t values_cnt;
-} sysr_values_ctx_t;
+ uint8_t address[4];
+} sc_ipv4_addr;
-char* xpath_find_first_key(const char *xpath, char *key, sr_xpath_ctx_t *state);
-void log_recv_event(sr_notif_event_t event, const char *msg);
-void log_recv_oper(sr_change_oper_t oper, const char *msg);
-int ip_prefix_split(const char* ip_prefix);
+/**
+ * @brief Extract IP "AAA.BBB.CCC.DDD" from an IP prefix: "AAA.BBB.CCC.DDD/XX"
+ * @param prefix - IPv4 or IPv6 prefix:
+ * @return prefix length
+ */
+static inline int ip_prefix_split(const char* ip_prefix)
+{
+ //find the slash
+ char* slash = strchr(ip_prefix, '/');
+ if (NULL == slash)
+ return -1;
+
+ //extract subnet mask length
+ char * eptr = NULL;
+ uint8_t plen = strtoul(slash + 1, &eptr, 10);
+ if (*eptr || plen <= 0)
+ return -1;
+
+ //keep just the address part
+ *slash = '\0'; //replace '/' with 0
+
+ //return prefix length
+ return plen;
+}
/**
* @brief Get IPv4 host address from IPv4 prefix.
@@ -43,13 +94,61 @@ int ip_prefix_split(const char* ip_prefix);
* @param[out] prefix Get Prefix length, optional value. Can be NULL.
* @return -1 when failure, 0 on success.
*/
-int get_address_from_prefix(char* dst, const char* src, size_t length,
- uint8_t* prefix_length);
+static inline int
+prefix2address(char *dst, const char *src, uint8_t *prefix_length)
+{
+ if (!src || !dst)
+ return -1;
-typedef struct
+ char *p = strchr(src, '/');
+ if (!p)
+ return -1; // '/' not found
+
+ if ((p - src + 1) > VPP_IP4_ADDRESS_STRING_LEN) //+ 1 needed for \0
+ return -1;
+
+ strncpy(dst, src, VPP_IP4_ADDRESS_STRING_LEN);
+
+ if (!prefix_length)
+ *prefix_length = atoi(++p);
+
+ return 0;
+}
+
+/**
+ * @brief Helper function for converting netmask (ex: 255.255.255.0)
+ * to prefix length (ex: 24).
+ * @param netmask - string of netmask "AAA.BBB.CCC.DDD"
+ * @return prefix length
+ */
+static inline uint8_t netmask_to_prefix(const char *netmask)
{
- uint8_t address[4];
-} sc_ipv4_addr;
+ in_addr_t n = 0;
+ uint8_t i = 0;
+
+ inet_pton(AF_INET, netmask, &n);
+
+ while (n > 0) {
+ n = n >> 1;
+ i++;
+ }
+
+ return i;
+}
+
+/**
+ * @brief Convert prefix length to netmask
+ * @param prefix - prefix length (ex: 24)
+ * @return netmask - integer (ex: 111111111111111111111111100000000b )
+ */
+static inline uint32_t prefix2mask(int prefix)
+{
+ if (prefix) {
+ return htonl(~((1 << (32 - prefix)) - 1));
+ } else {
+ return htonl(0);
+ }
+}
/**
* @brief Get IPv4 broadcast IP address form IPv4 network address.
@@ -59,8 +158,32 @@ typedef struct
* @param[in] prefix Prefix number.
* @return -1 when failure, 0 on success.
*/
-int get_network_broadcast(sc_ipv4_addr *broadcast, const sc_ipv4_addr *network,
- uint8_t prefix_length);
+static inline int get_network_broadcast(sc_ipv4_addr *broadcast, const sc_ipv4_addr *network,
+ uint8_t prefix_length)
+{
+ uint8_t mask = ~0;
+ uint8_t tmp_p = prefix_length;
+ int i;
+
+ ARG_CHECK2(-1, network, broadcast);
+
+ if (32 < prefix_length) {
+ SRP_LOG_ERR_MSG("Prefix length to big.");
+ return -1;
+ }
+
+ for (i = 0; i < 4 ; i++) {
+ broadcast->address[i] = network->address[i] |
+ (mask >> (tmp_p > 8 ? 8 : tmp_p));
+ if (tmp_p >= 8) {
+ tmp_p -= 8;
+ } else {
+ tmp_p = 0;
+ }
+ }
+
+ return 0;
+}
/**
* @brief Get last IPv4 address from the IP range.
@@ -70,8 +193,12 @@ int get_network_broadcast(sc_ipv4_addr *broadcast, const sc_ipv4_addr *network,
* @param[in] prefix Prefix number.
* @return -1 when failure, 0 on success.
*/
-int get_last_ip_address(sc_ipv4_addr *last_ip_address,
- const sc_ipv4_addr *first_ip_address,
- uint8_t prefix_length);
+static inline int get_last_ip_address(sc_ipv4_addr* last_ip_address,
+ const sc_ipv4_addr* first_ip_address,
+ uint8_t prefix_length)
+{
+ return get_network_broadcast(last_ip_address, first_ip_address,
+ prefix_length);
+}
#endif /* __SYS_UTIL_H__ */