• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifdef HAVE_STRUCT_GNET_STATS_BASIC
32 
33 # include <stdio.h>
34 # include <stddef.h>
35 # include "test_nlattr.h"
36 # include <linux/gen_stats.h>
37 # include <linux/rtnetlink.h>
38 
39 const unsigned int hdrlen = sizeof(struct tcmsg);
40 
41 static void
init_tcmsg(struct nlmsghdr * const nlh,const unsigned int msg_len)42 init_tcmsg(struct nlmsghdr *const nlh, const unsigned int msg_len)
43 {
44 	SET_STRUCT(struct nlmsghdr, nlh,
45 		.nlmsg_len = msg_len,
46 		.nlmsg_type = RTM_GETQDISC,
47 		.nlmsg_flags = NLM_F_DUMP
48 	);
49 
50 	struct tcmsg *const msg = NLMSG_DATA(nlh);
51 	SET_STRUCT(struct tcmsg, msg,
52 		.tcm_family = AF_UNIX,
53 		.tcm_ifindex = ifindex_lo()
54 	);
55 
56 	struct nlattr *const nla = NLMSG_ATTR(nlh, sizeof(*msg));
57 	SET_STRUCT(struct nlattr, nla,
58 		.nla_len = msg_len - NLMSG_SPACE(hdrlen),
59 		.nla_type = TCA_STATS2
60 	);
61 }
62 
63 static void
print_tcmsg(const unsigned int msg_len)64 print_tcmsg(const unsigned int msg_len)
65 {
66 	printf("{len=%u, type=RTM_GETQDISC, flags=NLM_F_DUMP"
67 	       ", seq=0, pid=0}, {tcm_family=AF_UNIX"
68 	       ", tcm_ifindex=" IFINDEX_LO_STR
69 	       ", tcm_handle=0, tcm_parent=0, tcm_info=0}"
70 	       ", {{nla_len=%u, nla_type=TCA_STATS2}",
71 	       msg_len, msg_len - NLMSG_SPACE(hdrlen));
72 }
73 
74 int
main(void)75 main(void)
76 {
77 	skip_if_unavailable("/proc/self/fd/");
78 
79 	const int fd = create_nl_socket(NETLINK_ROUTE);
80 	void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), NLA_HDRLEN + 8 * 5);
81 
82 	static char pattern[4096];
83 	fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
84 
85 	static const struct gnet_stats_basic sb = {
86 		.bytes = 0xabcdebdafefeadeb,
87 		.packets = 0xbdcdeabf
88 	};
89 	char buf[offsetofend(struct gnet_stats_basic, packets)];
90 	memcpy(buf, &sb, sizeof(buf));
91 	TEST_NESTED_NLATTR_OBJECT(fd, nlh0, hdrlen,
92 				  init_tcmsg, print_tcmsg,
93 				  TCA_STATS_BASIC, pattern, buf,
94 				  PRINT_FIELD_U("{", sb, bytes);
95 				  PRINT_FIELD_U(", ", sb, packets);
96 				  printf("}"));
97 
98 #ifdef HAVE_STRUCT_GNET_STATS_RATE_EST
99 	static const struct gnet_stats_rate_est est = {
100 		.bps = 0xebcdaebd,
101 		.pps = 0xabdceade,
102 	};
103 	TEST_NESTED_NLATTR_OBJECT(fd, nlh0, hdrlen,
104 				  init_tcmsg, print_tcmsg,
105 				  TCA_STATS_RATE_EST, pattern, est,
106 				  PRINT_FIELD_U("{", est, bps);
107 				  PRINT_FIELD_U(", ", est, pps);
108 				  printf("}"));
109 #endif
110 
111 #ifdef HAVE_STRUCT_GNET_STATS_QUEUE
112 	static const struct gnet_stats_queue qstats = {
113 		.qlen = 0xabcdeabd,
114 		.backlog = 0xbcdaebad,
115 		.drops = 0xcdbeaedb,
116 		.requeues = 0xdebaefab,
117 		.overlimits = 0xefaebade
118 	};
119 	TEST_NESTED_NLATTR_OBJECT(fd, nlh0, hdrlen,
120 				  init_tcmsg, print_tcmsg,
121 				  TCA_STATS_QUEUE, pattern, qstats,
122 				  PRINT_FIELD_U("{", qstats, qlen);
123 				  PRINT_FIELD_U(", ", qstats, backlog);
124 				  PRINT_FIELD_U(", ", qstats, drops);
125 				  PRINT_FIELD_U(", ", qstats, requeues);
126 				  PRINT_FIELD_U(", ", qstats, overlimits);
127 				  printf("}"));
128 #endif
129 
130 #ifdef HAVE_STRUCT_GNET_STATS_RATE_EST64
131 	static const struct gnet_stats_rate_est64 est64 = {
132 		.bps = 0xacbdcdefafecaebf,
133 		.pps = 0xcdabeabdfeabceaf
134 	};
135 	TEST_NESTED_NLATTR_OBJECT(fd, nlh0, hdrlen,
136 				  init_tcmsg, print_tcmsg,
137 				  TCA_STATS_RATE_EST64, pattern, est64,
138 				  PRINT_FIELD_U("{", est64, bps);
139 				  PRINT_FIELD_U(", ", est64, pps);
140 				  printf("}"));
141 #endif
142 
143 	puts("+++ exited with 0 +++");
144 	return 0;
145 }
146 
147 #else
148 
149 SKIP_MAIN_UNDEFINED("HAVE_STRUCT_GNET_STATS_BASIC")
150 
151 #endif
152