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/pkt_sched.h>
34 #include <linux/rtnetlink.h>
35
36 #ifndef TCA_STAB
37 # define TCA_STAB 8
38 #endif
39 #if !HAVE_DECL_TCA_STAB_DATA
40 enum { TCA_STAB_DATA = 2 };
41 #endif
42
43 const unsigned int hdrlen = sizeof(struct tcmsg);
44
45 static void
init_tcmsg(struct nlmsghdr * const nlh,const unsigned int msg_len)46 init_tcmsg(struct nlmsghdr *const nlh, const unsigned int msg_len)
47 {
48 SET_STRUCT(struct nlmsghdr, nlh,
49 .nlmsg_len = msg_len,
50 .nlmsg_type = RTM_GETQDISC,
51 .nlmsg_flags = NLM_F_DUMP
52 );
53
54 struct tcmsg *const msg = NLMSG_DATA(nlh);
55 SET_STRUCT(struct tcmsg, msg,
56 .tcm_family = AF_UNIX,
57 .tcm_ifindex = ifindex_lo()
58 );
59
60 struct nlattr *const nla = NLMSG_ATTR(nlh, sizeof(*msg));
61 SET_STRUCT(struct nlattr, nla,
62 .nla_len = msg_len - NLMSG_SPACE(hdrlen),
63 .nla_type = TCA_STAB
64 );
65 }
66
67 static void
print_tcmsg(const unsigned int msg_len)68 print_tcmsg(const unsigned int msg_len)
69 {
70 printf("{len=%u, type=RTM_GETQDISC, flags=NLM_F_DUMP"
71 ", seq=0, pid=0}, {tcm_family=AF_UNIX"
72 ", tcm_ifindex=" IFINDEX_LO_STR
73 ", tcm_handle=0, tcm_parent=0, tcm_info=0}"
74 ", {{nla_len=%u, nla_type=TCA_STAB}",
75 msg_len, msg_len - NLMSG_SPACE(hdrlen));
76 }
77
78 static void
print_uint16(const uint16_t * p,size_t idx)79 print_uint16(const uint16_t *p, size_t idx)
80 {
81 printf("%u", *p);
82 }
83
84 int
main(void)85 main(void)
86 {
87 skip_if_unavailable("/proc/self/fd/");
88
89 const int fd = create_nl_socket(NETLINK_ROUTE);
90 void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), NLA_HDRLEN + 4
91 #ifdef HAVE_STRUCT_TC_SIZESPEC
92 - 4 + sizeof(struct tc_sizespec)
93 #endif
94 );
95
96 static char pattern[4096];
97 fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
98
99 #ifdef HAVE_STRUCT_TC_SIZESPEC
100 static const struct tc_sizespec s = {
101 .cell_log = 0xab,
102 .size_log = 0xcd,
103 .cell_align = 0xefab,
104 .overhead = 0xcdadeefa,
105 .linklayer = 0xefbaafeb,
106 .mpu = 0xfebfaefb,
107 .mtu = 0xacdbefab,
108 .tsize = 0xbdeaabed
109 };
110 TEST_NESTED_NLATTR_OBJECT(fd, nlh0, hdrlen,
111 init_tcmsg, print_tcmsg,
112 TCA_STAB_BASE, pattern, s,
113 PRINT_FIELD_U("{", s, cell_log);
114 PRINT_FIELD_U(", ", s, size_log);
115 PRINT_FIELD_D(", ", s, cell_align);
116 PRINT_FIELD_D(", ", s, overhead);
117 PRINT_FIELD_U(", ", s, linklayer);
118 PRINT_FIELD_U(", ", s, mpu);
119 PRINT_FIELD_U(", ", s, mtu);
120 PRINT_FIELD_U(", ", s, tsize);
121 printf("}"));
122 #endif
123
124 uint16_t data[2] = { 0xacbd, 0xefba };
125 TEST_NESTED_NLATTR_ARRAY(fd, nlh0, hdrlen,
126 init_tcmsg, print_tcmsg,
127 TCA_STAB_DATA, pattern, data, print_uint16);
128
129 puts("+++ exited with 0 +++");
130 return 0;
131 }
132