xref: /aosp_15_r20/external/libnl/tests/test-create-vrf.c (revision 4dc78e53d49367fa8e61b07018507c90983a077d)
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 
3 #include "nl-default.h"
4 
5 #include <linux/netlink.h>
6 
7 #include <netlink/netlink.h>
8 #include <netlink/route/link.h>
9 #include <netlink/route/link/vrf.h>
10 
main(int argc,char * argv[])11 int main(int argc, char *argv[])
12 {
13 	struct nl_cache *link_cache;
14 	struct rtnl_link *link, *link2;
15 	struct nl_sock *sk;
16 	uint32_t tb_id;
17 	int err;
18 
19 	sk = nl_socket_alloc();
20 	if ((err = nl_connect(sk, NETLINK_ROUTE)) < 0) {
21 		nl_perror(err, "Unable to connect socket");
22 		return err;
23 	}
24 
25 	if (!(link = rtnl_link_vrf_alloc())) {
26 		fprintf(stderr, "Unable to allocate link");
27 		return -1;
28 	}
29 
30 	rtnl_link_set_name(link, "vrf-red");
31 
32 	if ((err = rtnl_link_vrf_set_tableid(link, 10)) < 0) {
33 		nl_perror(err, "Unable to set VRF table id");
34 		return err;
35 	}
36 
37 	if ((err = rtnl_link_add(sk, link, NLM_F_CREATE)) < 0) {
38 		nl_perror(err, "Unable to add link");
39 		return err;
40 	}
41 
42 	if ((err = rtnl_link_alloc_cache(sk, AF_UNSPEC, &link_cache)) < 0) {
43 		nl_perror(err, "Unable to allocate cache");
44 		return err;
45 	}
46 
47 	if (!(link2 = rtnl_link_get_by_name(link_cache, "vrf-red"))) {
48 		fprintf(stderr, "Unable to lookup vrf-red");
49 		return -1;
50 	}
51 
52 	if ((err = rtnl_link_vrf_get_tableid(link2, &tb_id)) < 0) {
53 		nl_perror(err, "Unable to get VRF table id");
54 		return err;
55 	}
56 
57 	if (tb_id != 10) {
58 		fprintf(stderr, "Mismatch with VRF table id\n");
59 	}
60 
61 	rtnl_link_put(link);
62 	nl_close(sk);
63 
64 	return 0;
65 }
66