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