Projects
Eulaceura:Mainline:GA
libnl3
_service:obs_scm:backport-add-some-tests-about-...
Sign Up
Log In
Username
Password
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File _service:obs_scm:backport-add-some-tests-about-addr-class-rule-neigh-qdisc.patch of Package libnl3
From 6b39fd0906c4f572b39c538b60790bd6ffe94341 Mon Sep 17 00:00:00 2001 From: chengyechun <chengyechun1@huawei.com> Date: Tue, 21 Nov 2023 10:11:00 +0800 Subject: [PATCH] backport-add-some-tests-about-addr-class-rule-neigh-qdisc Conflict:NA Reference:https://gitee.com/src-openeuler/libnl3/commit/cbf611d151d1ceb63331041e35a5a54184a51eaf/https://gitee.com/src-openeuler/libnl3/commit/501d5c2bec60070e78024bb98917970d207de06b --- tests/params.h | 5 ++ tests/test-add-delete-addr.c | 39 +++++++++ tests/test-add-delete-class.c | 142 +++++++++++++++++++++++++++++++ tests/test-add-delete-neigh.c | 41 +++++++++ tests/test-add-delete-qdisc.c | 156 ++++++++++++++++++++++++++++++++++ tests/test-add-delete-route.c | 43 ++++++++++ tests/test-genl-connect.c | 37 ++++++++ tests/test-link.c | 68 +++++++++++++++ 8 files changed, 531 insertions(+) create mode 100644 tests/params.h create mode 100644 tests/test-add-delete-addr.c create mode 100644 tests/test-add-delete-class.c create mode 100644 tests/test-add-delete-neigh.c create mode 100644 tests/test-add-delete-qdisc.c create mode 100644 tests/test-add-delete-route.c create mode 100644 tests/test-genl-connect.c create mode 100644 tests/test-link.c diff --git a/tests/params.h b/tests/params.h new file mode 100644 index 0000000..e3cab17 --- /dev/null +++ b/tests/params.h @@ -0,0 +1,5 @@ +#define DST_ADDR "addr" +#define IP "ip" +#define NEXTHOP "dev=1,via=2" +#define DEV_NAME "dev_name" + diff --git a/tests/test-add-delete-addr.c b/tests/test-add-delete-addr.c new file mode 100644 index 0000000..3fc4c5c --- /dev/null +++ b/tests/test-add-delete-addr.c @@ -0,0 +1,39 @@ +#include <netlink/cli/utils.h> +#include <netlink/cli/addr.h> +#include <netlink/cli/link.h> +#include <linux/netlink.h> +#include <stdio.h> +#include <params.h> + + +int main(int argc, char *argv[]) +{ + struct nl_sock *sock; + struct rtnl_addr *addr; + struct nl_cache *link_cache; + int err = 0, nlflags = NLM_F_CREATE; + + sock = nl_cli_alloc_socket(); + nl_cli_connect(sock, NETLINK_ROUTE); + link_cache = nl_cli_link_alloc_cache(sock); + addr = nl_cli_addr_alloc(); + + nl_cli_addr_parse_local(addr, IP); + nl_cli_addr_parse_dev(addr, link_cache, DEV_NAME); + + if ((err = rtnl_addr_add(sock, addr, nlflags)) < 0) { + printf("Unable to add route: %s", nl_geterror(err)); + goto END; + } + + if ((err = rtnl_addr_delete(sock, addr, nlflags)) < 0) { + printf("Unable to add route: %s", nl_geterror(err)); + goto END; + } + +END: + rtnl_addr_put(addr); + nl_cache_put(link_cache); + nl_socket_free(sock); + return err; +} diff --git a/tests/test-add-delete-class.c b/tests/test-add-delete-class.c new file mode 100644 index 0000000..6ee0e16 --- /dev/null +++ b/tests/test-add-delete-class.c @@ -0,0 +1,142 @@ +#include <netlink/cli/utils.h> +#include <netlink/cli/tc.h> +#include <netlink/cli/class.h> +#include <netlink/cli/link.h> +#include <netlink-private/route/tc-api.h> +#include <linux/netlink.h> +#include <stdio.h> +#include <params.h> + +static int default_yes = 0, deleted = 0, interactive = 0; +static struct nl_sock *sk; + +static int test_add_class() +{ + struct rtnl_class *class; + struct rtnl_tc *tc; + struct nl_cache *link_cache; + struct nl_cli_tc_module *tm; + struct rtnl_tc_ops *ops; + int err = 0, flags = NLM_F_CREATE | NLM_F_EXCL; + char kind[] = "htb"; + char *rate[] = {DEV_NAME, "root", "htb", "--rate=100mbit"}; + + sk = nl_cli_alloc_socket(); + nl_cli_connect(sk, NETLINK_ROUTE); + link_cache = nl_cli_link_alloc_cache(sk); + class = nl_cli_class_alloc(); + tc = (struct rtnl_tc *) class; + + nl_cli_tc_parse_dev(tc, link_cache, DEV_NAME); + nl_cli_tc_parse_parent(tc, "root"); + if (!rtnl_tc_get_ifindex(tc)) { + printf("You must specify a network device (--dev=XXX)\n"); + err = -1; + goto END; + } + if (!rtnl_tc_get_parent(tc)) { + printf("You must specify a parent (--parent=XXX)\n"); + err = -1; + goto END; + } + + rtnl_tc_set_kind(tc, kind); + if (!(ops = rtnl_tc_get_ops(tc))) { + printf("Unknown class \"%s\"\n", kind); + err = -1; + goto END; + } + if (!(tm = nl_cli_tc_lookup(ops))) { + printf("class type \"%s\" not supported.\n", kind); + err = -1; + goto END; + } + tm->tm_parse_argv(tc, 4, rate); + + if ((err = rtnl_class_add(sk, class, flags)) < 0) { + printf("Unable to add class: %s\n", nl_geterror(err)); + goto END; + } + +END: + nl_cache_mngt_unprovide(link_cache); + nl_cache_put(link_cache); + rtnl_class_put(class); + nl_socket_free(sk); + return err; +} + + +static void delete_cb(struct nl_object *obj, void *arg) +{ + struct rtnl_class *class = nl_object_priv(obj); + struct nl_dump_params params = { + .dp_type = NL_DUMP_LINE, + .dp_fd = stdout, + }; + int err; + + if (interactive && !nl_cli_confirm(obj, ¶ms, default_yes)) + return; + + if ((err = rtnl_class_delete(sk, class)) < 0) + nl_cli_fatal(err, "Unable to delete class: %s\n", nl_geterror(err)); + + deleted++; +} + +static int test_delete_class() +{ + struct rtnl_class *class; + struct rtnl_tc *tc; + struct nl_cache *link_cache, *class_cache; + struct nl_cli_tc_module *tm; + struct rtnl_tc_ops *ops; + char kind[] = "htb"; + int err = 0; + + sk = nl_cli_alloc_socket(); + nl_cli_connect(sk, NETLINK_ROUTE); + link_cache = nl_cli_link_alloc_cache(sk); + class = nl_cli_class_alloc(); + tc = (struct rtnl_tc *) class; + + nl_cli_tc_parse_dev(tc, link_cache, DEV_NAME); + nl_cli_tc_parse_parent(tc, "root"); + if (!rtnl_tc_get_ifindex(tc)) { + printf("You must specify a network device (--dev=XXX)\n"); + err = -1; + goto END; + } + if (!rtnl_tc_get_parent(tc)) { + printf("You must specify a parent (--parent=XXX)\n"); + err = -1; + goto END; + } + rtnl_tc_set_kind(tc, kind); + if (!(ops = rtnl_tc_get_ops(tc))) { + printf("Unknown class \"%s\"\n", kind); + err = -1; + goto END; + } + class_cache = nl_cli_class_alloc_cache(sk, rtnl_tc_get_ifindex(tc)); + nl_cache_foreach_filter(class_cache, OBJ_CAST(class), delete_cb, NULL); + +END: + nl_cache_put(link_cache); + nl_socket_free(sk); + rtnl_class_put(class); + return err; +} + +int main(int argc, char *argv[]) +{ + int err = 0; + if ((err = test_add_class()) < 0) { + printf("Unable to add class\n"); + } + if ((err = test_delete_class()) < 0) { + printf("Unable to delete class"); + } + return err; +} diff --git a/tests/test-add-delete-neigh.c b/tests/test-add-delete-neigh.c new file mode 100644 index 0000000..e4bc63a --- /dev/null +++ b/tests/test-add-delete-neigh.c @@ -0,0 +1,41 @@ +#include <netlink/cli/utils.h> +#include <netlink/cli/neigh.h> +#include <netlink/cli/link.h> +#include <linux/netlink.h> +#include <stdio.h> +#include <params.h> + + +int main(int argc, char *argv[]) +{ + struct nl_sock *sk; + struct rtnl_neigh *neigh; + struct nl_cache *link_cache; + int err = 0, ok = 0, nlflags = NLM_F_REPLACE | NLM_F_CREATE; + char lladdr[] = "AA:BB:CC:DD:EE:FF"; + + sk = nl_cli_alloc_socket(); + nl_cli_connect(sk, NETLINK_ROUTE); + link_cache = nl_cli_link_alloc_cache(sk); + neigh = nl_cli_neigh_alloc(); + + nl_cli_neigh_parse_dst(neigh, DST_ADDR); + nl_cli_neigh_parse_lladdr(neigh, lladdr); + nl_cli_neigh_parse_dev(neigh, link_cache, DEV_NAME); + + if ((err = rtnl_neigh_add(sk, neigh, nlflags)) < 0){ + printf("Unable to add neighbour: %s\n",nl_geterror(err)); + goto END; + } + + if ((err = rtnl_neigh_delete(sk, neigh, nlflags)) < 0){ + printf("Unable to add neighbour: %s\n",nl_geterror(err)); + goto END; + } + +END: + nl_socket_free(sk); + nl_cache_put(link_cache); + rtnl_neigh_put(neigh); + return err; +} diff --git a/tests/test-add-delete-qdisc.c b/tests/test-add-delete-qdisc.c new file mode 100644 index 0000000..29502db --- /dev/null +++ b/tests/test-add-delete-qdisc.c @@ -0,0 +1,156 @@ +#include <netlink/cli/utils.h> +#include <netlink/cli/tc.h> +#include <netlink/cli/qdisc.h> +#include <netlink/cli/link.h> +#include <netlink-private/route/tc-api.h> +#include <linux/netlink.h> +#include <stdio.h> +#include <params.h> + + +static int default_yes = 0, deleted = 0, interactive = 0; +static struct nl_sock *sk; + +static void delete_cb(struct nl_object *obj, void *arg) +{ + struct rtnl_qdisc *qdisc = nl_object_priv(obj); + struct nl_dump_params params = { + .dp_type = NL_DUMP_LINE, + .dp_fd = stdout, + }; + int err; + + /* Ignore default qdiscs, unable to delete */ + if (rtnl_tc_get_handle((struct rtnl_tc *) qdisc) == 0) + return; + + if (interactive && !nl_cli_confirm(obj, ¶ms, default_yes)) + return; + + if ((err = rtnl_qdisc_delete(sk, qdisc)) < 0) { + nl_cli_fatal(err, "Unable to delete qdisc: %s\n", nl_geterror(err)); + } + deleted++; +} + +static int test_delete_qdisc() +{ + struct rtnl_qdisc *qdisc; + struct rtnl_tc *tc; + struct nl_cache *link_cache, *qdisc_cache; + struct nl_cli_tc_module *tm; + struct rtnl_tc_ops *ops; + char kind[] = "htb"; + int err = 0; + + sk = nl_cli_alloc_socket(); + nl_cli_connect(sk, NETLINK_ROUTE); + link_cache = nl_cli_link_alloc_cache(sk); + qdisc_cache = nl_cli_qdisc_alloc_cache(sk); + qdisc = nl_cli_qdisc_alloc(); + tc = (struct rtnl_tc *) qdisc; + nl_cli_tc_parse_dev(tc, link_cache, DEV_NAME); + nl_cli_tc_parse_parent(tc, "root"); + + if (!rtnl_tc_get_ifindex(tc)) { + printf("You must specify a network device (--dev=XXX)"); + goto END; + } + + if (!rtnl_tc_get_parent(tc)) { + printf("You must specify a parent"); + goto END; + } + + rtnl_tc_set_kind(tc, kind); + if (!(ops = rtnl_tc_get_ops(tc))) { + printf("Unknown qdisc \"%s\"", kind); + goto END; + } + + if (!(tm = nl_cli_tc_lookup(ops))) { + nl_cli_fatal(ENOTSUP, "Qdisc type \"%s\" not supported.", kind); + goto END; + } + + + nl_cache_foreach_filter(qdisc_cache, OBJ_CAST(qdisc), delete_cb, NULL); + +END: + nl_cache_put(link_cache); + nl_cache_put(qdisc_cache); + rtnl_qdisc_put(qdisc); + nl_socket_free(sk); + return err; +} + +static int test_add_qdisc() +{ + struct rtnl_qdisc *qdisc; + struct rtnl_tc *tc; + struct nl_cache *link_cache; + struct nl_cli_tc_module *tm; + struct rtnl_tc_ops *ops; + char kind[] = "htb"; + int err = 0, flags = NLM_F_CREATE | NLM_F_EXCL; + + if (!(sk = nl_socket_alloc())){ + printf("Unable to allocate netlink socket\n"); + return -1; + } + if ((err = nl_connect(sk, NETLINK_ROUTE)) < 0) { + printf("Unable to connect netlink socket: %s\n", nl_geterror(err)); + nl_socket_free(sk); + return -1; + } + link_cache = nl_cli_link_alloc_cache(sk); + qdisc = nl_cli_qdisc_alloc(); + tc = (struct rtnl_tc *) qdisc; + + nl_cli_tc_parse_dev(tc, link_cache, DEV_NAME); + nl_cli_tc_parse_parent(tc, "root"); + + if (!rtnl_tc_get_ifindex(tc)){ + printf("You must specify a network device (--dev=XXX)\n"); + goto END; + } + + if (!rtnl_tc_get_parent(tc)){ + printf("You must specify a parent\n"); + goto END; + } + + rtnl_tc_set_kind(tc, kind); + if (!(ops = rtnl_tc_get_ops(tc))){ + printf("Unknown qdisc \"%s\"\n", kind); + goto END; + } + if (!(tm = nl_cli_tc_lookup(ops))){ + nl_cli_fatal(ENOTSUP, "Qdisc type \"%s\" not supported.\n", kind); + goto END; + } + + if ((err = rtnl_qdisc_add(sk, qdisc, flags)) < 0){ + printf("Unable to add qdisc: %s\n", nl_geterror(err)); + goto END; + } + +END: + nl_cache_mngt_unprovide(link_cache); + nl_cache_put(link_cache); + rtnl_qdisc_put(qdisc); + nl_socket_free(sk); + return err; +} + +int main(int args, char *argv[]) +{ + int err = 0; + if ((err = test_add_qdisc()) < 0) { + printf("Unable to add qdisc:%s", nl_geterror(err)); + } + if ((err = test_delete_qdisc()) < 0) { + printf("Unable to delete qdisc:%s", nl_geterror(err)); + } + return err; +} diff --git a/tests/test-add-delete-route.c b/tests/test-add-delete-route.c new file mode 100644 index 0000000..756fb46 --- /dev/null +++ b/tests/test-add-delete-route.c @@ -0,0 +1,43 @@ +#include <netlink/cli/utils.h> +#include <netlink/cli/route.h> +#include <netlink/cli/link.h> +#include <linux/netlink.h> +#include <stdio.h> +#include <params.h> + + +int main(int argc, char argv[]) +{ + struct nl_sock *sk; + struct rtnl_route *route; + struct nl_cache *link_cache, *route_cache; + char dst_addr[] = DST_ADDR; + char nexthop[] = NEXTHOP; + int err = 0; + + sk = nl_cli_alloc_socket(); + nl_cli_connect(sk, NETLINK_ROUTE); + link_cache = nl_cli_link_alloc_cache(sk); + route_cache = nl_cli_route_alloc_cache(sk, 0); + route = nl_cli_route_alloc(); + + nl_cli_route_parse_dst(route, dst_addr); + nl_cli_route_parse_nexthop(route, nexthop, link_cache); + + if ((err = rtnl_route_add(sk, route, NLM_F_EXCL)) < 0) { + printf("Unable to add route: %s", nl_geterror(err)); + goto END; + } + + if ((err = rtnl_route_delete(sk, route, NLM_F_EXCL)) < 0) { + printf("Unable to delete route: %s", nl_geterror(err)); + goto END; + } + +END: + rtnl_route_put(route); + nl_cache_put(link_cache); + nl_cache_put(route_cache); + nl_socket_free(sk); + return err; +} diff --git a/tests/test-genl-connect.c b/tests/test-genl-connect.c new file mode 100644 index 0000000..9522e1f --- /dev/null +++ b/tests/test-genl-connect.c @@ -0,0 +1,37 @@ +#include <linux/genetlink.h> +#include <netlink/socket.h> +#include <netlink/cli/utils.h> +#include <stdio.h> + + +int main(int argc, char *argv[]) +{ + struct nl_sock *sk; + struct nl_cache *family_cache; + struct nl_dump_params params = { + .dp_type = NL_DUMP_LINE, + .dp_fd = stdout, + }; + int err = 0; + + sk = nl_socket_alloc(); + if ((err = genl_connect(sk)) < 0) { + printf("Unable create socket: %s\n", nl_geterror(err)); + goto END; + } + nl_socket_enable_auto_ack(sk); + + if (nl_socket_get_fd(sk) < 0) { + printf("vaild socket\n"); + err = -1; + goto END; + } + nl_socket_set_buffer_size(sk, 32655, 32655); + family_cache = nl_cli_alloc_cache(sk, "generic netlink family", genl_ctrl_alloc_cache); + nl_cache_dump(family_cache, ¶ms); + +END: + nl_socket_free(sk); + nl_cache_put(family_cache); + return err; +} diff --git a/tests/test-link.c b/tests/test-link.c new file mode 100644 index 0000000..e5c415e --- /dev/null +++ b/tests/test-link.c @@ -0,0 +1,68 @@ +#include <netlink/socket.h> +#include <netlink/netlink.h> +#include <netlink/cli/utils.h> +#include <netlink/cli/link.h> +#include <netlink/route/link.h> +#include <unistd.h> + + +static int self_def_cb = NL_CB_DEBUG; + + +int main(int argc, char *argv[]) +{ + struct nl_sock *sk; + struct nl_cache *link_cache; + struct rtnl_link *link; + struct nl_addr *addr; + struct nl_cb *cb; + int err = 0, ifindex, pid; + char *buf; + + cb = nl_cb_alloc(self_def_cb); + pid = getpid(); + + if (!(sk = nl_socket_alloc_cb(cb))) { + nl_cli_fatal(ENOBUFS, "Unable to allocate netlink socket\n"); + } + nl_cli_connect(sk, NETLINK_ROUTE); + nl_socket_disable_seq_check(sk); + nl_socket_disable_auto_ack(sk); + nl_socket_set_local_port(sk, pid); + nl_join_groups(sk, pid); + nl_socket_drop_membership(sk, pid); + nl_socket_set_peer_port(sk, 0); + + link_cache = nl_cli_link_alloc_cache(sk); + link = nl_cli_link_alloc(); + + if (err = nl_socket_get_peer_port(sk)){ + printf("peer_port %d\n", err); + goto END; + } + if (err = nl_socket_use_seq(sk)) + printf("sk->s_seq_next %d\n", err); + + if ((ifindex = rtnl_link_get_ifindex(link)) == 0){ + printf("ifindex is not set, %d\n", ifindex); + rtnl_link_set_ifindex(link, 1); + }; + + if (rtnl_link_get(link_cache, 1)){ + printf("now link is cached\n"); + }else{ + nl_cache_add(link_cache, (struct nl_object *)link); + }; + + if ((err = rtnl_link_add(sk, link, AF_INET)) < 0){ + printf("Unable to add link %s\n", nl_geterror(err)); + goto END; + } + +END: + nl_cb_put(cb); + nl_socket_free(sk); + nl_cache_put(link_cache); + rtnl_link_put(link); + return err; +} -- 2.33.0
Locations
Projects
Search
Status Monitor
Help
Open Build Service
OBS Manuals
API Documentation
OBS Portal
Reporting a Bug
Contact
Mailing List
Forums
Chat (IRC)
Twitter
Open Build Service (OBS)
is an
openSUSE project
.
浙ICP备2022010568号-2