1 /*
2  * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
3  * Copyright (c) 2017-2018 The strace developers.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "tests.h"
30 
31 #include <stdio.h>
32 #include "test_nlattr.h"
33 #include <linux/if.h>
34 #include <linux/if_arp.h>
35 #ifdef HAVE_LINUX_IF_LINK_H
36 # include <linux/if_link.h>
37 #endif
38 #include <linux/rtnetlink.h>
39 
40 #if !HAVE_DECL_IFLA_XDP
41 enum { IFLA_XDP = 43 };
42 #endif
43 #ifndef IFLA_XDP_FD
44 # define IFLA_XDP_FD 1
45 #endif
46 
47 #ifndef IFLA_XDP_ATTACHED
48 # define IFLA_XDP_ATTACHED 2
49 #endif
50 
51 #ifndef IFLA_XDP_PROG_ID
52 # define IFLA_XDP_PROG_ID 4
53 #endif
54 
55 #ifndef IFLA_XDP_DRV_PROG_ID
56 # define IFLA_XDP_DRV_PROG_ID 5
57 #endif
58 
59 #ifndef IFLA_XDP_SKB_PROG_ID
60 # define IFLA_XDP_SKB_PROG_ID 6
61 #endif
62 
63 #ifndef IFLA_XDP_HW_PROG_ID
64 # define IFLA_XDP_HW_PROG_ID 7
65 #endif
66 
67 #ifndef XDP_ATTACHED_NONE
68 # define XDP_ATTACHED_NONE 0
69 #endif
70 
71 #ifndef XDP_ATTACHED_MULTI
72 # define XDP_ATTACHED_MULTI 4
73 #endif
74 
75 #define IFLA_ATTR IFLA_XDP
76 #include "nlattr_ifla.h"
77 
78 int
main(void)79 main(void)
80 {
81 	skip_if_unavailable("/proc/self/fd/");
82 
83 	const int32_t num = 0xabacdbcd;
84 	const int fd = create_nl_socket(NETLINK_ROUTE);
85 	void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen),
86 				   NLA_HDRLEN + sizeof(num));
87 
88 	static char pattern[4096];
89 	fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
90 
91 	TEST_NESTED_NLATTR_OBJECT(fd, nlh0, hdrlen,
92 				  init_ifinfomsg, print_ifinfomsg,
93 				  IFLA_XDP_FD, pattern, num,
94 				  printf("%d", num));
95 
96 	static const struct {
97 		uint8_t val;
98 		const char *str;
99 	} attach_types[] = {
100 		{ ARG_STR(XDP_ATTACHED_NONE) },
101 		{ ARG_STR(XDP_ATTACHED_MULTI) },
102 		{ ARG_STR(0x5)  " /* XDP_ATTACHED_??? */" },
103 		{ ARG_STR(0xfe) " /* XDP_ATTACHED_??? */" },
104 	};
105 
106 	for (size_t i = 0; i < ARRAY_SIZE(attach_types); i++) {
107 		TEST_NESTED_NLATTR_OBJECT(fd, nlh0, hdrlen,
108 					  init_ifinfomsg, print_ifinfomsg,
109 					  IFLA_XDP_ATTACHED, pattern,
110 					  attach_types[i].val,
111 					  printf("%s", attach_types[i].str));
112 	}
113 
114 #ifdef XDP_FLAGS_UPDATE_IF_NOEXIST
115 	const uint32_t flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
116 	TEST_NESTED_NLATTR_OBJECT(fd, nlh0, hdrlen,
117 				  init_ifinfomsg, print_ifinfomsg,
118 				  IFLA_XDP_FLAGS, pattern, flags,
119 				  printf("XDP_FLAGS_UPDATE_IF_NOEXIST"));
120 #endif
121 
122 	static const struct {
123 		uint32_t val;
124 		const char *str;
125 	} attrs[] = {
126 		{ ARG_STR(IFLA_XDP_PROG_ID) },
127 		{ ARG_STR(IFLA_XDP_DRV_PROG_ID) },
128 		{ ARG_STR(IFLA_XDP_SKB_PROG_ID) },
129 		{ ARG_STR(IFLA_XDP_HW_PROG_ID) },
130 	};
131 
132 	for (size_t i = 0; i < ARRAY_SIZE(attrs); i++) {
133 		TEST_NESTED_NLATTR_OBJECT_EX_(fd, nlh0, hdrlen,
134 					      init_ifinfomsg, print_ifinfomsg,
135 					      attrs[i].val, attrs[i].str,
136 					      pattern, num,
137 					      print_quoted_hex, 1,
138 					      printf("%u", num));
139 	}
140 
141 	puts("+++ exited with 0 +++");
142 	return 0;
143 }
144