(root)/
strace-6.5/
tests/
nlattr_ifla_xdp.c
       1  /*
       2   * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
       3   * Copyright (c) 2017-2021 The strace developers.
       4   * All rights reserved.
       5   *
       6   * SPDX-License-Identifier: GPL-2.0-or-later
       7   */
       8  
       9  #include "tests.h"
      10  
      11  #include <stdio.h>
      12  #include "test_nlattr.h"
      13  #include <linux/if.h>
      14  #include <linux/if_arp.h>
      15  #include <linux/if_link.h>
      16  #include <linux/rtnetlink.h>
      17  
      18  #define XLAT_MACROS_ONLY
      19  # include "xlat/rtnl_ifla_xdp_attrs.h"
      20  #undef XLAT_MACROS_ONLY
      21  
      22  #define IFLA_ATTR IFLA_XDP
      23  #include "nlattr_ifla.h"
      24  
      25  #ifndef FD9_PATH
      26  # define FD9_PATH ""
      27  #endif
      28  
      29  int
      30  main(void)
      31  {
      32  	skip_if_unavailable("/proc/self/fd/");
      33  
      34  	const int32_t num = 0xabacdbcd;
      35  	const int fd = create_nl_socket(NETLINK_ROUTE);
      36  	void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen),
      37  				   NLA_HDRLEN + sizeof(num));
      38  
      39  	static char pattern[4096];
      40  	fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
      41  
      42  	TEST_NESTED_NLATTR_OBJECT(fd, nlh0, hdrlen,
      43  				  init_ifinfomsg, print_ifinfomsg,
      44  				  IFLA_XDP_FD, pattern, num,
      45  				  printf("%d", num));
      46  
      47  	static const struct {
      48  		uint8_t val;
      49  		const char *str;
      50  	} attach_types[] = {
      51  		{ ARG_STR(XDP_ATTACHED_NONE) },
      52  		{ ARG_STR(XDP_ATTACHED_MULTI) },
      53  		{ ARG_STR(0x5)  " /* XDP_ATTACHED_??? */" },
      54  		{ ARG_STR(0xfe) " /* XDP_ATTACHED_??? */" },
      55  	};
      56  
      57  	for (size_t i = 0; i < ARRAY_SIZE(attach_types); i++) {
      58  		TEST_NESTED_NLATTR_OBJECT(fd, nlh0, hdrlen,
      59  					  init_ifinfomsg, print_ifinfomsg,
      60  					  IFLA_XDP_ATTACHED, pattern,
      61  					  attach_types[i].val,
      62  					  printf("%s", attach_types[i].str));
      63  	}
      64  
      65  	const uint32_t flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
      66  	TEST_NESTED_NLATTR_OBJECT(fd, nlh0, hdrlen,
      67  				  init_ifinfomsg, print_ifinfomsg,
      68  				  IFLA_XDP_FLAGS, pattern, flags,
      69  				  printf("XDP_FLAGS_UPDATE_IF_NOEXIST"));
      70  
      71  	static const struct {
      72  		uint32_t val;
      73  		const char *str;
      74  	} attrs[] = {
      75  		{ ARG_STR(IFLA_XDP_PROG_ID) },
      76  		{ ARG_STR(IFLA_XDP_DRV_PROG_ID) },
      77  		{ ARG_STR(IFLA_XDP_SKB_PROG_ID) },
      78  		{ ARG_STR(IFLA_XDP_HW_PROG_ID) },
      79  	};
      80  
      81  	for (size_t i = 0; i < ARRAY_SIZE(attrs); i++) {
      82  		TEST_NESTED_NLATTR_OBJECT_EX_(fd, nlh0, hdrlen,
      83  					      init_ifinfomsg, print_ifinfomsg,
      84  					      attrs[i].val, attrs[i].str,
      85  					      pattern, num,
      86  					      print_quoted_hex, 1,
      87  					      printf("%u", num));
      88  	}
      89  
      90  	/* IFLA_XDP_EXPECTED_FD */
      91  	TEST_NESTED_NLATTR_OBJECT_EX(fd, nlh0, hdrlen,
      92  				     init_ifinfomsg, print_ifinfomsg,
      93  				     IFLA_XDP_EXPECTED_FD, pattern, num, 1,
      94  				     printf("%d", num));
      95  
      96  	int exp_fd = 9;
      97  	TEST_NESTED_NLATTR_OBJECT_EX(fd, nlh0, hdrlen,
      98  				     init_ifinfomsg, print_ifinfomsg,
      99  				     IFLA_XDP_EXPECTED_FD, pattern, exp_fd, 1,
     100  				     printf("9" FD9_PATH));
     101  
     102  	puts("+++ exited with 0 +++");
     103  	return 0;
     104  }