138a34c9349267c99ce1ddbd0b6e985147415d355Dmitry V. Levin/*
277c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
377c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes * Copyright (c) 2017 The strace developers.
438a34c9349267c99ce1ddbd0b6e985147415d355Dmitry V. Levin * All rights reserved.
538a34c9349267c99ce1ddbd0b6e985147415d355Dmitry V. Levin *
638a34c9349267c99ce1ddbd0b6e985147415d355Dmitry V. Levin * Redistribution and use in source and binary forms, with or without
738a34c9349267c99ce1ddbd0b6e985147415d355Dmitry V. Levin * modification, are permitted provided that the following conditions
838a34c9349267c99ce1ddbd0b6e985147415d355Dmitry V. Levin * are met:
938a34c9349267c99ce1ddbd0b6e985147415d355Dmitry V. Levin * 1. Redistributions of source code must retain the above copyright
1038a34c9349267c99ce1ddbd0b6e985147415d355Dmitry V. Levin *    notice, this list of conditions and the following disclaimer.
1138a34c9349267c99ce1ddbd0b6e985147415d355Dmitry V. Levin * 2. Redistributions in binary form must reproduce the above copyright
1238a34c9349267c99ce1ddbd0b6e985147415d355Dmitry V. Levin *    notice, this list of conditions and the following disclaimer in the
1338a34c9349267c99ce1ddbd0b6e985147415d355Dmitry V. Levin *    documentation and/or other materials provided with the distribution.
1438a34c9349267c99ce1ddbd0b6e985147415d355Dmitry V. Levin * 3. The name of the author may not be used to endorse or promote products
1538a34c9349267c99ce1ddbd0b6e985147415d355Dmitry V. Levin *    derived from this software without specific prior written permission.
1638a34c9349267c99ce1ddbd0b6e985147415d355Dmitry V. Levin *
1738a34c9349267c99ce1ddbd0b6e985147415d355Dmitry V. Levin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1838a34c9349267c99ce1ddbd0b6e985147415d355Dmitry V. Levin * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1938a34c9349267c99ce1ddbd0b6e985147415d355Dmitry V. Levin * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2038a34c9349267c99ce1ddbd0b6e985147415d355Dmitry V. Levin * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2138a34c9349267c99ce1ddbd0b6e985147415d355Dmitry V. Levin * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2238a34c9349267c99ce1ddbd0b6e985147415d355Dmitry V. Levin * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2338a34c9349267c99ce1ddbd0b6e985147415d355Dmitry V. Levin * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2438a34c9349267c99ce1ddbd0b6e985147415d355Dmitry V. Levin * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2538a34c9349267c99ce1ddbd0b6e985147415d355Dmitry V. Levin * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2638a34c9349267c99ce1ddbd0b6e985147415d355Dmitry V. Levin * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2738a34c9349267c99ce1ddbd0b6e985147415d355Dmitry V. Levin */
2838a34c9349267c99ce1ddbd0b6e985147415d355Dmitry V. Levin
2977c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes#include "tests.h"
307a12974e9537c4ebaecada2a56f97dfa201f03dbDmitry V. Levin
3177c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes#include <stdio.h>
32bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes#include <stddef.h>
3377c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes#include "test_nlattr.h"
34bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes#include <linux/pkt_sched.h>
3577c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes#include <linux/rtnetlink.h>
367a12974e9537c4ebaecada2a56f97dfa201f03dbDmitry V. Levin
3777c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughesstatic void
3877c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughesinit_tcmsg(struct nlmsghdr *const nlh, const unsigned int msg_len)
3977c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes{
4077c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	SET_STRUCT(struct nlmsghdr, nlh,
4177c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes		.nlmsg_len = msg_len,
4277c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes		.nlmsg_type = RTM_GETQDISC,
4377c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes		.nlmsg_flags = NLM_F_DUMP
4477c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	);
4577c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes
4677c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	struct tcmsg *const msg = NLMSG_DATA(nlh);
4777c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	SET_STRUCT(struct tcmsg, msg,
4877c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes		.tcm_family = AF_UNIX,
4977c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes		.tcm_ifindex = ifindex_lo()
5077c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	);
517a12974e9537c4ebaecada2a56f97dfa201f03dbDmitry V. Levin
5277c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes}
537a12974e9537c4ebaecada2a56f97dfa201f03dbDmitry V. Levin
5477c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughesstatic void
5577c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughesprint_tcmsg(const unsigned int msg_len)
567a12974e9537c4ebaecada2a56f97dfa201f03dbDmitry V. Levin{
5777c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	printf("{len=%u, type=RTM_GETQDISC, flags=NLM_F_DUMP"
5877c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	       ", seq=0, pid=0}, {tcm_family=AF_UNIX"
5977c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	       ", tcm_ifindex=" IFINDEX_LO_STR
6077c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	       ", tcm_handle=0, tcm_parent=0, tcm_info=0}",
6177c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	       msg_len);
6277c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes}
6377c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes
6477c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughesint
6577c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughesmain(void)
6677c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes{
6777c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	skip_if_unavailable("/proc/self/fd/");
6877c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes
6977c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	const int fd = create_nl_socket(NETLINK_ROUTE);
7077c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	const unsigned int hdrlen = sizeof(struct tcmsg);
7177c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	void *nlh0 = tail_alloc(NLMSG_SPACE(hdrlen));
727a12974e9537c4ebaecada2a56f97dfa201f03dbDmitry V. Levin
7377c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	static char pattern[4096];
7477c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
757a12974e9537c4ebaecada2a56f97dfa201f03dbDmitry V. Levin
7677c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	const unsigned int nla_type = 0xffff & NLA_TYPE_MASK;
7777c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	char nla_type_str[256];
7877c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	sprintf(nla_type_str, "%#x /* TCA_??? */", nla_type);
7977c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	TEST_NLATTR_(fd, nlh0, hdrlen,
8077c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes		     init_tcmsg, print_tcmsg,
8177c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes		     nla_type, nla_type_str,
8277c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes		     4, pattern, 4,
8377c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes		     print_quoted_hex(pattern, 4));
847a12974e9537c4ebaecada2a56f97dfa201f03dbDmitry V. Levin
85bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes	static const struct tc_stats st = {
86bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes		.bytes = 0xabcdcdbefeadefac,
87bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes		.packets = 0xbcdeaefd,
88bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes		.drops = 0xcdedafed,
89bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes		.overlimits = 0xdcdbefad,
90bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes		.bps = 0xefaebfad,
91bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes		.pps = 0xfefbaedb,
92bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes		.qlen = 0xabcdefab,
93bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes		.backlog = 0xbdeabeab
94bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes	};
95bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes	char buf[offsetofend(struct tc_stats, backlog)];
96bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes	memcpy(buf, &st, sizeof(buf));
97bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
98bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes			   init_tcmsg, print_tcmsg,
99bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes			   TCA_STATS, pattern, buf,
100bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes			   PRINT_FIELD_U("{", st, bytes);
101bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes			   PRINT_FIELD_U(", ", st, packets);
102bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes			   PRINT_FIELD_U(", ", st, drops);
103bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes			   PRINT_FIELD_U(", ", st, overlimits);
104bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes			   PRINT_FIELD_U(", ", st, bps);
105bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes			   PRINT_FIELD_U(", ", st, pps);
106bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes			   PRINT_FIELD_U(", ", st, qlen);
107bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes			   PRINT_FIELD_U(", ", st, backlog);
108bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes			   printf("}"));
109bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes
110bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes	static const struct tc_estimator est = {
111bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes		.interval = 0xcd,
112bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes		.ewma_log = 0xab
113bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes	};
114bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
115bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes			   init_tcmsg, print_tcmsg,
116bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes			   TCA_RATE, pattern, est,
117bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes			   PRINT_FIELD_D("{", est, interval);
118bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes			   PRINT_FIELD_U(", ", est, ewma_log);
119bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes			   printf("}"));
120bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes
12177c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	puts("+++ exited with 0 +++");
12277c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes	return 0;
1237a12974e9537c4ebaecada2a56f97dfa201f03dbDmitry V. Levin}
124