Home | History | Annotate | Download | only in tests-mx32
      1 /*
      2  * Copyright (c) 2017 JingPiao Chen <chenjingpiao (at) 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 <string.h>
     33 #include <arpa/inet.h>
     34 #include <net/if.h>
     35 #include <netinet/tcp.h>
     36 #include "test_nlattr.h"
     37 #include <linux/inet_diag.h>
     38 #include <linux/sock_diag.h>
     39 
     40 static const char * const sk_meminfo_strs[] = {
     41 	"SK_MEMINFO_RMEM_ALLOC",
     42 	"SK_MEMINFO_RCVBUF",
     43 	"SK_MEMINFO_WMEM_ALLOC",
     44 	"SK_MEMINFO_SNDBUF",
     45 	"SK_MEMINFO_FWD_ALLOC",
     46 	"SK_MEMINFO_WMEM_QUEUED",
     47 	"SK_MEMINFO_OPTMEM",
     48 	"SK_MEMINFO_BACKLOG",
     49 	"SK_MEMINFO_DROPS",
     50 };
     51 
     52 static const char address[] = "10.11.12.13";
     53 
     54 static void
     55 init_inet_diag_msg(struct nlmsghdr *const nlh, const unsigned int msg_len)
     56 {
     57 	SET_STRUCT(struct nlmsghdr, nlh,
     58 		.nlmsg_len = msg_len,
     59 		.nlmsg_type = SOCK_DIAG_BY_FAMILY,
     60 		.nlmsg_flags = NLM_F_DUMP
     61 	);
     62 
     63 	struct inet_diag_msg *const msg = NLMSG_DATA(nlh);
     64 	SET_STRUCT(struct inet_diag_msg, msg,
     65 		.idiag_family = AF_INET,
     66 		.idiag_state = TCP_LISTEN,
     67 		.id.idiag_if = ifindex_lo()
     68 	);
     69 
     70 	if (!inet_pton(AF_INET, address, msg->id.idiag_src) ||
     71 	    !inet_pton(AF_INET, address, msg->id.idiag_dst))
     72 		perror_msg_and_skip("inet_pton");
     73 }
     74 
     75 static void
     76 print_inet_diag_msg(const unsigned int msg_len)
     77 {
     78 	printf("{len=%u, type=SOCK_DIAG_BY_FAMILY"
     79 	       ", flags=NLM_F_DUMP, seq=0, pid=0}, {idiag_family=AF_INET"
     80 	       ", idiag_state=TCP_LISTEN, idiag_timer=0, idiag_retrans=0"
     81 	       ", id={idiag_sport=htons(0), idiag_dport=htons(0)"
     82 	       ", idiag_src=inet_addr(\"%s\")"
     83 	       ", idiag_dst=inet_addr(\"%s\")"
     84 	       ", idiag_if=" IFINDEX_LO_STR
     85 	       ", idiag_cookie=[0, 0]}"
     86 	       ", idiag_expires=0, idiag_rqueue=0, idiag_wqueue=0"
     87 	       ", idiag_uid=0, idiag_inode=0}",
     88 	       msg_len, address, address);
     89 }
     90 
     91 static void
     92 print_uint(const unsigned int *p, size_t i)
     93 {
     94 	if (i >= ARRAY_SIZE(sk_meminfo_strs))
     95 		printf("[%zu /* SK_MEMINFO_??? */", i);
     96 	else
     97 		printf("[%s", sk_meminfo_strs[i]);
     98 
     99 	printf("] = %u", *p);
    100 }
    101 
    102 int
    103 main(void)
    104 {
    105 	skip_if_unavailable("/proc/self/fd/");
    106 
    107 	static const struct inet_diag_meminfo minfo = {
    108 		.idiag_rmem = 0xfadcacdb,
    109 		.idiag_wmem = 0xbdabcada,
    110 		.idiag_fmem = 0xbadbfafb,
    111 		.idiag_tmem = 0xfdacdadf
    112 	};
    113 	static const struct tcpvegas_info vegas = {
    114 		.tcpv_enabled = 0xfadcacdb,
    115 		.tcpv_rttcnt = 0xbdabcada,
    116 		.tcpv_rtt = 0xbadbfafb,
    117 		.tcpv_minrtt = 0xfdacdadf
    118 	};
    119 	static const struct tcp_dctcp_info dctcp = {
    120 		.dctcp_enabled = 0xfdac,
    121 		.dctcp_ce_state = 0xfadc,
    122 		.dctcp_alpha = 0xbdabcada,
    123 		.dctcp_ab_ecn = 0xbadbfafb,
    124 		.dctcp_ab_tot = 0xfdacdadf
    125 	};
    126 	static const struct tcp_bbr_info bbr = {
    127 		.bbr_bw_lo = 0xfdacdadf,
    128 		.bbr_bw_hi = 0xfadcacdb,
    129 		.bbr_min_rtt = 0xbdabcada,
    130 		.bbr_pacing_gain = 0xbadbfafb,
    131 		.bbr_cwnd_gain = 0xfdacdadf
    132 	};
    133 	static const uint32_t mem[] = { 0xaffacbad, 0xffadbcab };
    134 	static uint32_t bigmem[SK_MEMINFO_VARS + 1];
    135 	static const uint32_t mark = 0xabdfadca;
    136 	static const uint8_t shutdown = 0xcd;
    137 
    138 	const int fd = create_nl_socket(NETLINK_SOCK_DIAG);
    139 	const unsigned int hdrlen = sizeof(struct inet_diag_msg);
    140 	void *const nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen),
    141 					 NLA_HDRLEN +
    142 					 MAX(sizeof(bigmem), DEFAULT_STRLEN));
    143 
    144 	static char pattern[4096];
    145 	fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
    146 
    147 	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
    148 			   init_inet_diag_msg, print_inet_diag_msg,
    149 			   INET_DIAG_MEMINFO, pattern, minfo,
    150 			   PRINT_FIELD_U("{", minfo, idiag_rmem);
    151 			   PRINT_FIELD_U(", ", minfo, idiag_wmem);
    152 			   PRINT_FIELD_U(", ", minfo, idiag_fmem);
    153 			   PRINT_FIELD_U(", ", minfo, idiag_tmem);
    154 			   printf("}"));
    155 
    156 	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
    157 			   init_inet_diag_msg, print_inet_diag_msg,
    158 			   INET_DIAG_VEGASINFO, pattern, vegas,
    159 			   PRINT_FIELD_U("{", vegas, tcpv_enabled);
    160 			   PRINT_FIELD_U(", ", vegas, tcpv_rttcnt);
    161 			   PRINT_FIELD_U(", ", vegas, tcpv_rtt);
    162 			   PRINT_FIELD_U(", ", vegas, tcpv_minrtt);
    163 			   printf("}"));
    164 
    165 
    166 	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
    167 			   init_inet_diag_msg, print_inet_diag_msg,
    168 			   INET_DIAG_DCTCPINFO, pattern, dctcp,
    169 			   PRINT_FIELD_U("{", dctcp, dctcp_enabled);
    170 			   PRINT_FIELD_U(", ", dctcp, dctcp_ce_state);
    171 			   PRINT_FIELD_U(", ", dctcp, dctcp_alpha);
    172 			   PRINT_FIELD_U(", ", dctcp, dctcp_ab_ecn);
    173 			   PRINT_FIELD_U(", ", dctcp, dctcp_ab_tot);
    174 			   printf("}"));
    175 
    176 	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
    177 			   init_inet_diag_msg, print_inet_diag_msg,
    178 			   INET_DIAG_BBRINFO, pattern, bbr,
    179 			   PRINT_FIELD_X("{", bbr, bbr_bw_lo);
    180 			   PRINT_FIELD_X(", ", bbr, bbr_bw_hi);
    181 			   PRINT_FIELD_U(", ", bbr, bbr_min_rtt);
    182 			   PRINT_FIELD_U(", ", bbr, bbr_pacing_gain);
    183 			   PRINT_FIELD_U(", ", bbr, bbr_cwnd_gain);
    184 			   printf("}"));
    185 
    186 	TEST_NLATTR_ARRAY(fd, nlh0, hdrlen,
    187 			  init_inet_diag_msg, print_inet_diag_msg,
    188 			  INET_DIAG_SKMEMINFO, pattern, mem, print_uint);
    189 
    190 	memcpy(bigmem, pattern, sizeof(bigmem));
    191 
    192 	TEST_NLATTR_ARRAY(fd, nlh0, hdrlen,
    193 			  init_inet_diag_msg, print_inet_diag_msg,
    194 			  INET_DIAG_SKMEMINFO, pattern, bigmem, print_uint);
    195 
    196 	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
    197 			   init_inet_diag_msg, print_inet_diag_msg,
    198 			   INET_DIAG_MARK, pattern, mark,
    199 			   printf("%u", mark));
    200 
    201 	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
    202 			   init_inet_diag_msg, print_inet_diag_msg,
    203 			   INET_DIAG_CLASS_ID, pattern, mark,
    204 			   printf("%u", mark));
    205 
    206 	TEST_NLATTR(fd, nlh0, hdrlen,
    207 		    init_inet_diag_msg, print_inet_diag_msg, INET_DIAG_SHUTDOWN,
    208 		    sizeof(shutdown), &shutdown, sizeof(shutdown),
    209 		    printf("%u", shutdown));
    210 
    211 	char *const str = tail_alloc(DEFAULT_STRLEN);
    212 	fill_memory_ex(str, DEFAULT_STRLEN, '0', 10);
    213 	TEST_NLATTR(fd, nlh0, hdrlen,
    214 		    init_inet_diag_msg, print_inet_diag_msg, INET_DIAG_CONG,
    215 		    DEFAULT_STRLEN, str, DEFAULT_STRLEN,
    216 		    printf("\"%.*s\"...", DEFAULT_STRLEN, str));
    217 	str[DEFAULT_STRLEN - 1] = '\0';
    218 	TEST_NLATTR(fd, nlh0, hdrlen,
    219 		    init_inet_diag_msg, print_inet_diag_msg, INET_DIAG_CONG,
    220 		    DEFAULT_STRLEN, str, DEFAULT_STRLEN,
    221 		    printf("\"%s\"", str));
    222 
    223 	puts("+++ exited with 0 +++");
    224 	return 0;
    225 }
    226