From 703fd26289e9ddf1e9a18bed8370efcebc6fcce6 Mon Sep 17 00:00:00 2001 From: ali-aqrabawi Date: Fri, 23 Feb 2024 12:48:40 +0300 Subject: [PATCH] Make, cmd_gen: fix make lib build, update cmd_gen --- Makefile | 66 ++++++++++--------- src/iproute2_sysrepo.c | 4 +- .../{iproute2_cmdgen.c => change_cmdgen.c} | 24 +++---- src/lib/cmdgen.h | 39 +++++++++++ src/lib/iproute2_cmdgen.h | 29 -------- tests/run_tests.sh | 2 +- yang/iproute2-cmdgen-extensions.yang | 2 +- yang/iproute2-ip-link.yang | 12 ++-- yang/iproute2-ip-nexthop.yang | 18 ++--- 9 files changed, 105 insertions(+), 91 deletions(-) rename src/lib/{iproute2_cmdgen.c => change_cmdgen.c} (92%) create mode 100644 src/lib/cmdgen.h delete mode 100644 src/lib/iproute2_cmdgen.h diff --git a/Makefile b/Makefile index c5a59cd..bb99360 100644 --- a/Makefile +++ b/Makefile @@ -25,44 +25,46 @@ IPR2_SR_OBJ = $(IPR2_SR_SRC:.c=.o) all: $(IPR2_SR_OBJ) $(IPR2_SR_LIB_OBJ) iproute2/config.mk - @set -e; \ - for i in $(SUBDIRS); do \ - $(MAKE) -C $$i || exit 1; done && \ - echo "Building $(BIN)/$(EXEC)" && \ - rm iproute2/ip/rtmon.o - rm iproute2/ip/ip.o - rm iproute2/bridge/bridge.o - rm iproute2/tc/tc.o - @echo "" - @echo "Patching conflicting symbols" - objcopy --redefine-sym print_linkinfo=br_print_linkinfo iproute2/bridge/link.o - objcopy --redefine-sym print_linkinfo=br_print_linkinfo iproute2/bridge/monitor.o - @echo "" - $(CC) -o $(BIN)/$(EXEC) $(IPR2_SR_OBJ) $(IPR2_SR_LIB_OBJ) `find iproute2/ip -name '*.[o]'` `find iproute2/bridge -name '*.[o]'` `find iproute2/tc -name '*.[o]'` `find iproute2/lib -name '*.[o]'` $(LDFLAGS) - @echo "" - @echo "Make complete" + @set -e; \ + for i in $(SUBDIRS); do \ + $(MAKE) -C $$i || exit 1; done && \ + echo "Building $(BIN)/$(EXEC)" && \ + rm iproute2/ip/rtmon.o + rm iproute2/ip/ip.o + rm iproute2/bridge/bridge.o + rm iproute2/tc/tc.o + @echo "" + @echo "Patching conflicting symbols" + objcopy --redefine-sym print_linkinfo=br_print_linkinfo iproute2/bridge/link.o + objcopy --redefine-sym print_linkinfo=br_print_linkinfo iproute2/bridge/monitor.o + @echo "" + $(CC) -o $(BIN)/$(EXEC) $(IPR2_SR_OBJ) $(IPR2_SR_LIB_OBJ) `find iproute2/ip -name '*.[o]'` `find iproute2/bridge -name '*.[o]'` `find iproute2/tc -name '*.[o]'` `find iproute2/lib -name '*.[o]'` $(LDFLAGS) + @echo "" + @echo "Make complete" clean: - @set -e; \ - for i in $(SUBDIRS); do \ - $(MAKE) -C $$i clean || exit 1; done && \ - rm -f iproute2/config.mk && \ - rm -f $(IPR2_SR_OBJ) && \ - rm -f $(IPR2_SR_LIB_OBJ) && \ - rm -f $(BIN)/$(EXEC) + @set -e; \ + for i in $(SUBDIRS); do \ + $(MAKE) -C $$i clean || exit 1; done && \ + rm -f iproute2/config.mk && \ + rm -f $(IPR2_SR_OBJ) && \ + rm -f $(IPR2_SR_LIB_OBJ) && \ + rm -f $(BIN)/$(EXEC) iproute2/config.mk: - @set -e; \ - cd iproute2 && \ - ./configure && \ - cd .. + @set -e; \ + cd iproute2 && \ + ./configure && \ + cd .. check: - yanglint yang/*.yang + yanglint yang/*.yang -$(IPR2_SR_LIB_OBJ): $(IPR2_SR_LIB_SRC) - $(CC) -c $< -o $@ -Iiproute2/ip -Iiproute2/bridge -Iiproute2/tc -Iiproute2/include +src/lib/%.o: src/lib/%.c + $(CC) -c $< -o $@ -Iiproute2/ip -Iiproute2/bridge -Iiproute2/tc -Iiproute2/include + + +src/%.o: $(IPR2_SR_SRC) $(IPR2_SR_LIB_OBJ) + $(CC) -c $< -o $@ -Iiproute2/ip -Iiproute2/bridge -Iiproute2/tc -Iiproute2/include -$(IPR2_SR_OBJ): $(IPR2_SR_SRC) - $(CC) -c $< -o $@ -Iiproute2/ip -Iiproute2/bridge -Iiproute2/tc -Iiproute2/include diff --git a/src/iproute2_sysrepo.c b/src/iproute2_sysrepo.c index b0f07c0..e602b8a 100644 --- a/src/iproute2_sysrepo.c +++ b/src/iproute2_sysrepo.c @@ -39,8 +39,8 @@ #include "br_common.h" /* sysrepo */ +#include "lib/cmdgen.h" #include -#include "lib/iproute2_cmdgen.h" #ifndef LIBDIR #define LIBDIR "/usr/lib" @@ -352,7 +352,7 @@ int ip_sr_config_change_cb_apply(const struct lyd_node *change_dnode) return SR_ERR_INVAL_ARG; } - ipr2_cmds = lyd2cmd_argv(change_dnode); + ipr2_cmds = lyd2cmds_argv(change_dnode); if (ipr2_cmds == NULL){ fprintf(stderr, "%s: failed to generate commands for the change \n", diff --git a/src/lib/iproute2_cmdgen.c b/src/lib/change_cmdgen.c similarity index 92% rename from src/lib/iproute2_cmdgen.c rename to src/lib/change_cmdgen.c index f3cac21..86c3a44 100644 --- a/src/lib/iproute2_cmdgen.c +++ b/src/lib/change_cmdgen.c @@ -14,7 +14,7 @@ #include #include -#include "iproute2_cmdgen.h" +#include "cmdgen.h" #define CMD_LINE_SIZE 1024 @@ -139,7 +139,7 @@ oper_t get_operation(struct lyd_node *dnode) } /* - * return true if the node has ir2cgen:cmd-start extension. + * return true if the node has ipr2cgen:cmd-start extension. */ int is_startcmd_node(struct lyd_node *dnode) { @@ -215,7 +215,8 @@ void parse_command(const char *command, int *argc, char ***argv) free(cmd_copy); } -void add_command(char *cmd_line, struct cmd_args **cmds, int *cmd_idx, char **oper2cmd_prefix) +void add_command(char *cmd_line, struct cmd_args **cmds, int *cmd_idx, + char **oper2cmd_prefix, const struct lyd_node *start_dnode) { int argc; char **argv; @@ -233,12 +234,13 @@ void add_command(char *cmd_line, struct cmd_args **cmds, int *cmd_idx, char **op } dup_argv(&((cmds)[*cmd_idx]->argv), argv, argc); (cmds)[*cmd_idx]->argc = argc; + (cmds)[*cmd_idx]->cmd_start_dnode = start_dnode; (*cmd_idx)++; memset(cmd_line, 0, CMD_LINE_SIZE); free_argv(argv, argc); } -struct cmd_args **lyd2cmd_argv(const struct lyd_node *change_node) +struct cmd_args **lyd2cmds_argv(const struct lyd_node *change_node) { char *result; int cmd_idx = 0; @@ -263,19 +265,19 @@ struct cmd_args **lyd2cmd_argv(const struct lyd_node *change_node) // first get the add, update, delete cmds prefixis from schema extensions if (get_extension(CMD_ADD_EXT, change_node, &oper2cmd_prefix[ADD_OPR]) != EXIT_SUCCESS) { fprintf(stderr, "%s: cmd-add extension is missing from root container " - "make sure root container has ir2cgen:cmd-add\n", + "make sure root container has ipr2cgen:cmd-add\n", __func__); return NULL; } if (get_extension(CMD_DELETE_EXT, change_node, &oper2cmd_prefix[DELETE_OPR]) != EXIT_SUCCESS) { fprintf(stderr, "%s: cmd-delete extension is missing from root container " - "make sure root container has ir2cgen:cmd-delete\n", + "make sure root container has ipr2cgen:cmd-delete\n", __func__); return NULL; } if (get_extension(CMD_UPDATE_EXT, change_node, &oper2cmd_prefix[UPDATE_OPR]) != EXIT_SUCCESS) { - fprintf(stderr, "%s: ir2cgen:cmd-update extension is missing from root container " - "make sure root container has ir2cgen:cmd-update\n", + fprintf(stderr, "%s: ipr2cgen:cmd-update extension is missing from root container " + "make sure root container has ipr2cgen:cmd-update\n", __func__); return NULL; } @@ -285,11 +287,11 @@ struct cmd_args **lyd2cmd_argv(const struct lyd_node *change_node) struct lyd_node *next; LYD_TREE_DFS_BEGIN(change_node, next) { - // if this is startcmd node (schema has ir2cgen:cmd-start), then start building a new command + // if this is startcmd node (schema has ipr2cgen:cmd-start), then start building a new command if (is_startcmd_node(next)) { // check if the cmd is not empty (first cmd) if (cmd_line[0] != 0) - add_command(cmd_line, cmds, &cmd_idx, oper2cmd_prefix); + add_command(cmd_line, cmds, &cmd_idx, oper2cmd_prefix, next); // prepare for new command op_val = get_operation(next); @@ -340,7 +342,7 @@ struct cmd_args **lyd2cmd_argv(const struct lyd_node *change_node) LYD_TREE_DFS_END(change_node, next); } if (cmd_line[0] != 0) - add_command(cmd_line, cmds, &cmd_idx, oper2cmd_prefix); + add_command(cmd_line, cmds, &cmd_idx, oper2cmd_prefix,next); return cmds; } diff --git a/src/lib/cmdgen.h b/src/lib/cmdgen.h new file mode 100644 index 0000000..500a22a --- /dev/null +++ b/src/lib/cmdgen.h @@ -0,0 +1,39 @@ +/* SPDX-License-Identifier: AGPL-3.0-or-later */ +#ifndef IPROUTE2_SYSREPO_CMDGEN_H +#define IPROUTE2_SYSREPO_CMDGEN_H + +#include + +#define CMDS_ARRAY_SIZE 1024 + +/** + * @brief data struct to store the int argc,char **argv pair. + * + * this struct can be easily used by + * iproute2::do_cmd(argc,argv) + */ +struct cmd_args{ + int argc; + char **argv; + const struct lyd_node *cmd_start_dnode; +}; + +/** + * @brief generate argc,argv commands out of the lyd_node (diff). + * + * + * @param[in] node Data node diff to generate the argc, argv. + * @return Array of cmd_args struct. + */ +struct cmd_args** lyd2cmds_argv(const struct lyd_node *change_node); + +/** + * @brief generate argc,argv rollback command out of the lyd_node (diff). + * + * + * @param[in] node Data node diff to generate the argc, argv. + * @return Array of cmd_args struct. + */ +struct cmd_args* lyd2rollbackcmd_argv(const struct lyd_node *change_node); + +#endif// IPROUTE2_SYSREPO_CMDGEN_H diff --git a/src/lib/iproute2_cmdgen.h b/src/lib/iproute2_cmdgen.h deleted file mode 100644 index 679ef54..0000000 --- a/src/lib/iproute2_cmdgen.h +++ /dev/null @@ -1,29 +0,0 @@ -/* SPDX-License-Identifier: AGPL-3.0-or-later */ -#ifndef IPROUTE2_SYSREPO_IPROUTE2_CMDGEN_H -#define IPROUTE2_SYSREPO_IPROUTE2_CMDGEN_H - -#include - -#define CMDS_ARRAY_SIZE 1024 - -/** - * @brief data struct to store the int argc,char **argv pair. - * - * this struct can be easily used by - * iproute2::do_cmd(argc,argv) - */ -struct cmd_args{ - int argc; - char **argv; -}; - -/** - * @brief generate argc,argv commands out of the lyd_node (diff). - * this lyd_node can be obtained from sysrepo API sr_get_change_diff(session) or libyang's lyd_lyd_diff_tree() - * - * @param[in] node Data node diff to generate the argc, argv. - * @return Array of cmd_args struct. - */ -struct cmd_args** lyd2cmd_argv(const struct lyd_node *change_node); - -#endif// IPROUTE2_SYSREPO_IPROUTE2_CMDGEN_H diff --git a/tests/run_tests.sh b/tests/run_tests.sh index 1303d3f..46fe2ad 100755 --- a/tests/run_tests.sh +++ b/tests/run_tests.sh @@ -20,7 +20,7 @@ done sysrepo_pid=$! sleep 0.5 -# Iterate over all .sh files in the current directory +# Iterate over all case/*.sh files for test_script in tests/cases/*.sh; do # Check if the file is executable and has the .sh extension if [ -x "$test_script" ] && [[ "$test_script" == *.sh ]]; then diff --git a/yang/iproute2-cmdgen-extensions.yang b/yang/iproute2-cmdgen-extensions.yang index f82e320..90eed5d 100644 --- a/yang/iproute2-cmdgen-extensions.yang +++ b/yang/iproute2-cmdgen-extensions.yang @@ -1,7 +1,7 @@ module iproute2-cmdgen-extensions { yang-version 1.1; namespace "urn:okda:iproute2:cmdgen:extensions"; - prefix "ir2cgen"; + prefix "ipr2cgen"; organization "Okda networks"; diff --git a/yang/iproute2-ip-link.yang b/yang/iproute2-ip-link.yang index 2f4461d..db5b493 100644 --- a/yang/iproute2-ip-link.yang +++ b/yang/iproute2-ip-link.yang @@ -3,7 +3,7 @@ module iproute2-ip-link { namespace "urn:okda:iproute2:ip:link"; prefix "iplink"; - import iproute2-cmdgen-extensions { prefix ir2cgen; } + import iproute2-cmdgen-extensions { prefix ipr2cgen; } //import ietf-inet-types { prefix inet; } //import ietf-yang-types { prefix yang; } organization @@ -130,16 +130,16 @@ module iproute2-ip-link { container links { - ir2cgen:cmd-add "ip link add"; - ir2cgen:cmd-update "ip link set"; - ir2cgen:cmd-delete "ip link delete"; + ipr2cgen:cmd-add "ip link add"; + ipr2cgen:cmd-update "ip link set"; + ipr2cgen:cmd-delete "ip link delete"; description "ip-link - network device configuration"; list link { - ir2cgen:cmd-start; + ipr2cgen:cmd-start; key "name"; description "ip-link - network device"; leaf name { - ir2cgen:value-only-on-update; + ipr2cgen:value-only-on-update; type string; description "link name"; } diff --git a/yang/iproute2-ip-nexthop.yang b/yang/iproute2-ip-nexthop.yang index 7657d55..2301ed8 100644 --- a/yang/iproute2-ip-nexthop.yang +++ b/yang/iproute2-ip-nexthop.yang @@ -5,7 +5,7 @@ module iproute2-ip-nexthop { import ietf-inet-types { prefix inet; } import iproute2-ip-link { prefix iplink; } - import iproute2-cmdgen-extensions { prefix ir2cgen; } + import iproute2-cmdgen-extensions { prefix ipr2cgen; } organization "Okda Networks"; @@ -27,18 +27,18 @@ module iproute2-ip-nexthop { } container nexthops{ - ir2cgen:cmd-add "ip nexthop add"; - ir2cgen:cmd-update "ip nexthop replace"; - ir2cgen:cmd-delete "ip nexthop del"; + ipr2cgen:cmd-add "ip nexthop add"; + ipr2cgen:cmd-update "ip nexthop replace"; + ipr2cgen:cmd-delete "ip nexthop del"; description "iproute2 nexthops configurations"; list nexthop{ - ir2cgen:cmd-start; + ipr2cgen:cmd-start; key "id"; description "nexthop configuraton"; leaf id{ - ir2cgen:value-only-on-update; + ipr2cgen:value-only-on-update; description "nexthop id"; type uint32; } @@ -68,13 +68,13 @@ module iproute2-ip-nexthop { } leaf blackhole { - ir2cgen:flag; + ipr2cgen:flag; description "specify if this is blackhole nexthop, which discards all traffic sent to it."; type boolean; when "not(../dev) and not(../ipv6) and not(../ipv4)"; } leaf onlink { - ir2cgen:flag; + ipr2cgen:flag; type boolean; description "Indicates that the nexthop is directly reachable on the link."; } @@ -141,7 +141,7 @@ module iproute2-ip-nexthop { } } leaf fdb { - ir2cgen:flag; + ipr2cgen:flag; type boolean; description "nexthop and nexthop groups for use with layer-2 fdb entries. A fdb nexthop group can only have fdb nexthops. Example: Used to represent a vxlan remote