From 3ede2c65ab1f4ca3f1eb627a0ef150067e300c15 Mon Sep 17 00:00:00 2001 From: "Kiedrzyn, PatrykX" Date: Mon, 23 Jul 2018 11:50:12 +0200 Subject: vlan: add vlan action support --- tests/test-flower-filter-with-actions.c | 154 ++++++++++++++++++++++-- 1 file changed, 143 insertions(+), 11 deletions(-) --- a/tests/test-flower-filter-with-actions.c +++ b/tests/test-flower-filter-with-actions.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -106,7 +107,7 @@ char *run_command(const char *cmd) return output; } -static int run_test(void) +static int check_if_flower_is_created_correctly(void) { char *output = NULL; int ret = 0; @@ -141,16 +142,13 @@ static int run_test(void) err: if (ret) printf(output); - else - printf("ok\n"); free(output); return ret; } - -static int setup_flower_classifier(struct nl_sock *sock, - struct nl_cache *link_cache) +static int test_flower_classifier_is_created(struct nl_sock *sock, + struct nl_cache *link_cache) { struct rtnl_cls *cls; struct rtnl_act *act; @@ -227,16 +225,135 @@ static int setup_flower_classifier(struc goto err_cls_add; } - err = run_test(); + err = check_if_flower_is_created_correctly(); + if (err) + printf("Flower classifier was not created correctly\n"); + + rtnl_cls_delete(sock, cls, 0); +err_cls_add: +err_action_add: + rtnl_act_put(act); +err_action: +err_vlan_prio: +err_vlan_id: +err_indev: +err_link_name: +err_cache: + rtnl_cls_put(cls); +err_classifier: + return err; +} + +static int check_if_flower_with_vlan_is_created_correctly(void) +{ + char *output = NULL; + int ret = 0; + + output = run_command("tc -s -d filter show dev d0 ingress"); + + if (!strstr(output, "vlan push id 777 protocol 802.1Q priority 5")) { + printf("unexpected vlan action options\n"); + ret = -1; + goto err; + } + +err: + if (ret) + printf(output); + + free(output); + return ret; +} + +static int test_flower_classifier_with_action_vlan_is_created(struct nl_sock + *sock, + struct nl_cache + *link_cache) +{ + struct rtnl_cls *cls; + struct rtnl_act *act; + int err = 0; + int ifindex; + + cls = rtnl_cls_alloc(); + if (!cls) { + printf("Unable to allocate classifier\n"); + err = -1; + goto err_classifier; + } + + err = rtnl_tc_set_kind(TC_CAST(cls), "flower"); + if (err) { + printf("Unable to set kind to flower classifier\n"); + goto err_classifier; + } + + rtnl_cls_set_prio(cls, 1); + rtnl_cls_set_protocol(cls, ETH_P_8021Q); + + err = nl_cache_refill(sock, link_cache); + if (err) { + printf("Unable to refill cache\n"); + goto err_cache; + } + + ifindex = rtnl_link_name2i(link_cache, "d0"); + if (!ifindex) { + printf("Unable to get link name\n"); + err = -1; + goto err_link_name; + } + + rtnl_tc_set_ifindex(TC_CAST(cls), ifindex); + rtnl_tc_set_handle(TC_CAST(cls), 1); + rtnl_tc_set_parent(TC_CAST(cls), TC_HANDLE(0xffff, 0xfff1)); + err = rtnl_flower_set_indev(cls, "d0"); + if (err) { + printf("Unable to set indev\n"); + goto err_indev; + } + err = rtnl_flower_set_vlan_id(cls, 7); + if (err) { + printf("Unable to set vlan_id: %d\n", err); + goto err_vlan_id; + } + err = rtnl_flower_set_vlan_prio(cls, 1); + if (err) { + printf("Unable to set vlan_prio: %d\n", err); + goto err_vlan_prio; + } + + act = rtnl_act_alloc(); + if (!act) { + printf("Unable to allocate action\n"); + err = -1; + goto err_action; + } + + rtnl_tc_set_kind(TC_CAST(act), "vlan"); + rtnl_vlan_set_action(act, TCA_VLAN_ACT_PUSH); + rtnl_vlan_set_id(act, 777); + rtnl_vlan_set_prio(act, 5); + rtnl_vlan_set_proto(act, ETH_P_8021Q); + + err = rtnl_flower_add_action(cls, act); if (err) { - printf("test case failed\n"); + printf("Unable to add action"); + goto err_action_add; } - err = rtnl_cls_delete(sock, cls, 0); + err = rtnl_cls_add(sock, cls, NLM_F_CREATE); if (err) { - printf("Unable to delete classifier: %d\n", err); + printf("Unable to create classifier: %d\n", err); + goto err_cls_add; } + err = check_if_flower_with_vlan_is_created_correctly(); + if (err) + printf + ("Flower classifier with vlan was not created correctly\n"); + + rtnl_cls_delete(sock, cls, 0); err_cls_add: err_action_add: rtnl_act_put(act); @@ -310,8 +427,23 @@ int main(void) } qdisc_add_ingress(sock, created); - err = setup_flower_classifier(sock, link_cache); + err = test_flower_classifier_is_created(sock, link_cache); + + if (err) + goto err_test_failed; + + printf("test_flower_classifier_is_created passed\n"); + + err = + test_flower_classifier_with_action_vlan_is_created(sock, + link_cache); + + if (err) + goto err_test_failed; + + printf("test_flower_classifier_with_action_vlan_is_created passed\n"); +err_test_failed: rtnl_link_put(created); err_get_link: err_cache: