diff mbox series

[bpf-next,3/8] selftests/xsk: implement a function that generates MAC addresses

Message ID 20230918093304.367826-4-tushar.vyavahare@intel.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series Add a test for SHARED_UMEM feature | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR success PR summary
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf-next, async
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 9 this patch: 9
netdev/cc_maintainers warning 12 maintainers not CCed: linux-kselftest@vger.kernel.org shuah@kernel.org martin.lau@linux.dev jolsa@kernel.org haoluo@google.com kpsingh@kernel.org sdf@google.com john.fastabend@gmail.com yonghong.song@linux.dev andrii@kernel.org mykolal@fb.com song@kernel.org
netdev/build_clang success Errors and warnings before: 9 this patch: 9
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 9 this patch: 9
netdev/checkpatch warning WARNING: line length of 100 exceeds 80 columns WARNING: line length of 89 exceeds 80 columns WARNING: line length of 95 exceeds 80 columns WARNING: line length of 97 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-VM_Test-0 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-5 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-1 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-4 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-2 success Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-6 success Logs for test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-18 success Logs for test_progs_no_alu32_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for test_progs_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-24 success Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-8 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-14 success Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-16 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-19 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-22 success Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for test_progs_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-26 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-28 success Logs for veristat
bpf/vmtest-bpf-next-VM_Test-12 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-25 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-11 success Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for test_maps on s390x with gcc

Commit Message

Tushar Vyavahare Sept. 18, 2023, 9:32 a.m. UTC
Move the src_mac and dst_mac fields from the ifobject structure to the
xsk_socket_info structure to achieve per-socket MAC address assignment.
Implement the function called generate_mac_addresses() to generate MAC
addresses based on the required number by the framework.

Require this in order to steer traffic to various sockets in subsequent
patches.

Signed-off-by: Tushar Vyavahare <tushar.vyavahare@intel.com>
---
 tools/testing/selftests/bpf/xskxceiver.c | 45 ++++++++++++++----------
 tools/testing/selftests/bpf/xskxceiver.h |  8 +++--
 2 files changed, 33 insertions(+), 20 deletions(-)

Comments

Magnus Karlsson Sept. 21, 2023, 6:43 a.m. UTC | #1
On Mon, 18 Sept 2023 at 11:13, Tushar Vyavahare
<tushar.vyavahare@intel.com> wrote:
>
> Move the src_mac and dst_mac fields from the ifobject structure to the
> xsk_socket_info structure to achieve per-socket MAC address assignment.
> Implement the function called generate_mac_addresses() to generate MAC
> addresses based on the required number by the framework.
>
> Require this in order to steer traffic to various sockets in subsequent
> patches.
>
> Signed-off-by: Tushar Vyavahare <tushar.vyavahare@intel.com>
> ---
>  tools/testing/selftests/bpf/xskxceiver.c | 45 ++++++++++++++----------
>  tools/testing/selftests/bpf/xskxceiver.h |  8 +++--
>  2 files changed, 33 insertions(+), 20 deletions(-)
>
> diff --git a/tools/testing/selftests/bpf/xskxceiver.c b/tools/testing/selftests/bpf/xskxceiver.c
> index ad1f7f078f5f..9f241f503eed 100644
> --- a/tools/testing/selftests/bpf/xskxceiver.c
> +++ b/tools/testing/selftests/bpf/xskxceiver.c
> @@ -104,9 +104,6 @@
>  #include "../kselftest.h"
>  #include "xsk_xdp_common.h"
>
> -static const char *MAC1 = "\x00\x0A\x56\x9E\xEE\x62";
> -static const char *MAC2 = "\x00\x0A\x56\x9E\xEE\x61";
> -
>  static bool opt_verbose;
>  static bool opt_print_tests;
>  static enum test_mode opt_mode = TEST_MODE_ALL;
> @@ -145,6 +142,18 @@ static void report_failure(struct test_spec *test)
>         test->fail = true;
>  }
>
> +static void generate_mac_addresses(const u8 *g_mac, u32 num_macs)
> +{
> +       u32 i, j;
> +
> +       for (i = 0; i < num_macs; i++) {
> +               for (j = 0; j < ETH_ALEN; j++)
> +                       macs[i][j] = g_mac[j];
> +
> +               macs[i][ETH_ALEN - 1] += i;
> +       }
> +}
> +
>  /* The payload is a word consisting of a packet sequence number in the upper
>   * 16-bits and a intra packet data sequence number in the lower 16 bits. So the 3rd packet's
>   * 5th word of data will contain the number (2<<16) | 4 as they are numbered from 0.
> @@ -159,10 +168,10 @@ static void write_payload(void *dest, u32 pkt_nb, u32 start, u32 size)
>                 ptr[i] = htonl(pkt_nb << 16 | (i + start));
>  }
>
> -static void gen_eth_hdr(struct ifobject *ifobject, struct ethhdr *eth_hdr)
> +static void gen_eth_hdr(struct xsk_socket_info *xsk, struct ethhdr *eth_hdr)
>  {
> -       memcpy(eth_hdr->h_dest, ifobject->dst_mac, ETH_ALEN);
> -       memcpy(eth_hdr->h_source, ifobject->src_mac, ETH_ALEN);
> +       memcpy(eth_hdr->h_dest, xsk->dst_mac, ETH_ALEN);
> +       memcpy(eth_hdr->h_source, xsk->src_mac, ETH_ALEN);
>         eth_hdr->h_proto = htons(ETH_P_LOOPBACK);
>  }
>
> @@ -445,6 +454,9 @@ static void __test_spec_init(struct test_spec *test, struct ifobject *ifobj_tx,
>                                 ifobj->xsk_arr[j].pkt_stream = test->tx_pkt_stream_default;
>                         else
>                                 ifobj->xsk_arr[j].pkt_stream = test->rx_pkt_stream_default;
> +
> +                       memcpy(ifobj->xsk_arr[j].src_mac, macs[j * 2 + i % 2], ETH_ALEN);
> +                       memcpy(ifobj->xsk_arr[j].dst_mac, macs[j * 2 + (i + 1) % 2], ETH_ALEN);
>                 }
>         }
>
> @@ -726,16 +738,16 @@ static void pkt_stream_cancel(struct pkt_stream *pkt_stream)
>         pkt_stream->current_pkt_nb--;
>  }
>
> -static void pkt_generate(struct ifobject *ifobject, u64 addr, u32 len, u32 pkt_nb,
> -                        u32 bytes_written)
> +static void pkt_generate(struct xsk_socket_info *xsk, struct xsk_umem_info *umem, u64 addr, u32 len,
> +                        u32 pkt_nb, u32 bytes_written)
>  {
> -       void *data = xsk_umem__get_data(ifobject->umem->buffer, addr);
> +       void *data = xsk_umem__get_data(umem->buffer, addr);
>
>         if (len < MIN_PKT_SIZE)
>                 return;
>
>         if (!bytes_written) {
> -               gen_eth_hdr(ifobject, data);
> +               gen_eth_hdr(xsk, data);
>
>                 len -= PKT_HDR_SIZE;
>                 data += PKT_HDR_SIZE;
> @@ -1209,7 +1221,7 @@ static int __send_pkts(struct ifobject *ifobject, struct pollfd *fds, bool timeo
>                                 tx_desc->options = 0;
>                         }
>                         if (pkt->valid)
> -                               pkt_generate(ifobject, tx_desc->addr, tx_desc->len, pkt->pkt_nb,
> +                               pkt_generate(xsk, umem, tx_desc->addr, tx_desc->len, pkt->pkt_nb,
>                                              bytes_written);
>                         bytes_written += tx_desc->len;
>
> @@ -2120,15 +2132,11 @@ static bool hugepages_present(void)
>         return true;
>  }
>
> -static void init_iface(struct ifobject *ifobj, const char *dst_mac, const char *src_mac,
> -                      thread_func_t func_ptr)
> +static void init_iface(struct ifobject *ifobj, thread_func_t func_ptr)
>  {
>         LIBBPF_OPTS(bpf_xdp_query_opts, query_opts);
>         int err;
>
> -       memcpy(ifobj->dst_mac, dst_mac, ETH_ALEN);
> -       memcpy(ifobj->src_mac, src_mac, ETH_ALEN);
> -
>         ifobj->func_ptr = func_ptr;
>
>         err = xsk_load_xdp_programs(ifobj);
> @@ -2391,6 +2399,7 @@ int main(int argc, char **argv)
>                 ksft_exit_xfail();
>         }
>
> +       generate_mac_addresses(g_mac, NUM_MAC_ADDRESSES);

Why pass down a global variable?

>         shared_netdev = (ifobj_tx->ifindex == ifobj_rx->ifindex);
>         ifobj_tx->shared_umem = shared_netdev;
>         ifobj_rx->shared_umem = shared_netdev;
> @@ -2404,8 +2413,8 @@ int main(int argc, char **argv)
>                         modes++;
>         }
>
> -       init_iface(ifobj_rx, MAC1, MAC2, worker_testapp_validate_rx);
> -       init_iface(ifobj_tx, MAC2, MAC1, worker_testapp_validate_tx);
> +       init_iface(ifobj_rx, worker_testapp_validate_rx);
> +       init_iface(ifobj_tx, worker_testapp_validate_tx);
>
>         test_spec_init(&test, ifobj_tx, ifobj_rx, 0, &tests[0]);
>         tx_pkt_stream_default = pkt_stream_generate(ifobj_tx->umem, DEFAULT_PKT_CNT, MIN_PKT_SIZE);
> diff --git a/tools/testing/selftests/bpf/xskxceiver.h b/tools/testing/selftests/bpf/xskxceiver.h
> index 06b82b39c59c..e003f9ca4692 100644
> --- a/tools/testing/selftests/bpf/xskxceiver.h
> +++ b/tools/testing/selftests/bpf/xskxceiver.h
> @@ -59,6 +59,7 @@
>  #define HUGEPAGE_SIZE (2 * 1024 * 1024)
>  #define PKT_DUMP_NB_TO_PRINT 16
>  #define RUN_ALL_TESTS UINT_MAX
> +#define NUM_MAC_ADDRESSES 4
>
>  #define print_verbose(x...) do { if (opt_verbose) ksft_print_msg(x); } while (0)
>
> @@ -90,6 +91,8 @@ struct xsk_socket_info {
>         struct pkt_stream *pkt_stream;
>         u32 outstanding_tx;
>         u32 rxqsize;
> +       u8 dst_mac[ETH_ALEN];
> +       u8 src_mac[ETH_ALEN];
>  };
>
>  struct pkt {
> @@ -140,8 +143,6 @@ struct ifobject {
>         bool unaligned_supp;
>         bool multi_buff_supp;
>         bool multi_buff_zc_supp;
> -       u8 dst_mac[ETH_ALEN];
> -       u8 src_mac[ETH_ALEN];
>  };
>
>  struct test_spec {
> @@ -168,4 +169,7 @@ pthread_mutex_t pacing_mutex = PTHREAD_MUTEX_INITIALIZER;
>
>  int pkts_in_flight;
>
> +static const u8 g_mac[] = {0x55, 0x44, 0x33, 0x22, 0x11, 0x00};
> +static u8 macs[NUM_MAC_ADDRESSES][ETH_ALEN] = {0};

I do not think you need this last array. You already have added
dst_mac and src_mac in the xsk_socket_info that you can use, so no
need for this.

> +
>  #endif                         /* XSKXCEIVER_H_ */
> --
> 2.34.1
>
>
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/xskxceiver.c b/tools/testing/selftests/bpf/xskxceiver.c
index ad1f7f078f5f..9f241f503eed 100644
--- a/tools/testing/selftests/bpf/xskxceiver.c
+++ b/tools/testing/selftests/bpf/xskxceiver.c
@@ -104,9 +104,6 @@ 
 #include "../kselftest.h"
 #include "xsk_xdp_common.h"
 
-static const char *MAC1 = "\x00\x0A\x56\x9E\xEE\x62";
-static const char *MAC2 = "\x00\x0A\x56\x9E\xEE\x61";
-
 static bool opt_verbose;
 static bool opt_print_tests;
 static enum test_mode opt_mode = TEST_MODE_ALL;
@@ -145,6 +142,18 @@  static void report_failure(struct test_spec *test)
 	test->fail = true;
 }
 
+static void generate_mac_addresses(const u8 *g_mac, u32 num_macs)
+{
+	u32 i, j;
+
+	for (i = 0; i < num_macs; i++) {
+		for (j = 0; j < ETH_ALEN; j++)
+			macs[i][j] = g_mac[j];
+
+		macs[i][ETH_ALEN - 1] += i;
+	}
+}
+
 /* The payload is a word consisting of a packet sequence number in the upper
  * 16-bits and a intra packet data sequence number in the lower 16 bits. So the 3rd packet's
  * 5th word of data will contain the number (2<<16) | 4 as they are numbered from 0.
@@ -159,10 +168,10 @@  static void write_payload(void *dest, u32 pkt_nb, u32 start, u32 size)
 		ptr[i] = htonl(pkt_nb << 16 | (i + start));
 }
 
-static void gen_eth_hdr(struct ifobject *ifobject, struct ethhdr *eth_hdr)
+static void gen_eth_hdr(struct xsk_socket_info *xsk, struct ethhdr *eth_hdr)
 {
-	memcpy(eth_hdr->h_dest, ifobject->dst_mac, ETH_ALEN);
-	memcpy(eth_hdr->h_source, ifobject->src_mac, ETH_ALEN);
+	memcpy(eth_hdr->h_dest, xsk->dst_mac, ETH_ALEN);
+	memcpy(eth_hdr->h_source, xsk->src_mac, ETH_ALEN);
 	eth_hdr->h_proto = htons(ETH_P_LOOPBACK);
 }
 
@@ -445,6 +454,9 @@  static void __test_spec_init(struct test_spec *test, struct ifobject *ifobj_tx,
 				ifobj->xsk_arr[j].pkt_stream = test->tx_pkt_stream_default;
 			else
 				ifobj->xsk_arr[j].pkt_stream = test->rx_pkt_stream_default;
+
+			memcpy(ifobj->xsk_arr[j].src_mac, macs[j * 2 + i % 2], ETH_ALEN);
+			memcpy(ifobj->xsk_arr[j].dst_mac, macs[j * 2 + (i + 1) % 2], ETH_ALEN);
 		}
 	}
 
@@ -726,16 +738,16 @@  static void pkt_stream_cancel(struct pkt_stream *pkt_stream)
 	pkt_stream->current_pkt_nb--;
 }
 
-static void pkt_generate(struct ifobject *ifobject, u64 addr, u32 len, u32 pkt_nb,
-			 u32 bytes_written)
+static void pkt_generate(struct xsk_socket_info *xsk, struct xsk_umem_info *umem, u64 addr, u32 len,
+			 u32 pkt_nb, u32 bytes_written)
 {
-	void *data = xsk_umem__get_data(ifobject->umem->buffer, addr);
+	void *data = xsk_umem__get_data(umem->buffer, addr);
 
 	if (len < MIN_PKT_SIZE)
 		return;
 
 	if (!bytes_written) {
-		gen_eth_hdr(ifobject, data);
+		gen_eth_hdr(xsk, data);
 
 		len -= PKT_HDR_SIZE;
 		data += PKT_HDR_SIZE;
@@ -1209,7 +1221,7 @@  static int __send_pkts(struct ifobject *ifobject, struct pollfd *fds, bool timeo
 				tx_desc->options = 0;
 			}
 			if (pkt->valid)
-				pkt_generate(ifobject, tx_desc->addr, tx_desc->len, pkt->pkt_nb,
+				pkt_generate(xsk, umem, tx_desc->addr, tx_desc->len, pkt->pkt_nb,
 					     bytes_written);
 			bytes_written += tx_desc->len;
 
@@ -2120,15 +2132,11 @@  static bool hugepages_present(void)
 	return true;
 }
 
-static void init_iface(struct ifobject *ifobj, const char *dst_mac, const char *src_mac,
-		       thread_func_t func_ptr)
+static void init_iface(struct ifobject *ifobj, thread_func_t func_ptr)
 {
 	LIBBPF_OPTS(bpf_xdp_query_opts, query_opts);
 	int err;
 
-	memcpy(ifobj->dst_mac, dst_mac, ETH_ALEN);
-	memcpy(ifobj->src_mac, src_mac, ETH_ALEN);
-
 	ifobj->func_ptr = func_ptr;
 
 	err = xsk_load_xdp_programs(ifobj);
@@ -2391,6 +2399,7 @@  int main(int argc, char **argv)
 		ksft_exit_xfail();
 	}
 
+	generate_mac_addresses(g_mac, NUM_MAC_ADDRESSES);
 	shared_netdev = (ifobj_tx->ifindex == ifobj_rx->ifindex);
 	ifobj_tx->shared_umem = shared_netdev;
 	ifobj_rx->shared_umem = shared_netdev;
@@ -2404,8 +2413,8 @@  int main(int argc, char **argv)
 			modes++;
 	}
 
-	init_iface(ifobj_rx, MAC1, MAC2, worker_testapp_validate_rx);
-	init_iface(ifobj_tx, MAC2, MAC1, worker_testapp_validate_tx);
+	init_iface(ifobj_rx, worker_testapp_validate_rx);
+	init_iface(ifobj_tx, worker_testapp_validate_tx);
 
 	test_spec_init(&test, ifobj_tx, ifobj_rx, 0, &tests[0]);
 	tx_pkt_stream_default = pkt_stream_generate(ifobj_tx->umem, DEFAULT_PKT_CNT, MIN_PKT_SIZE);
diff --git a/tools/testing/selftests/bpf/xskxceiver.h b/tools/testing/selftests/bpf/xskxceiver.h
index 06b82b39c59c..e003f9ca4692 100644
--- a/tools/testing/selftests/bpf/xskxceiver.h
+++ b/tools/testing/selftests/bpf/xskxceiver.h
@@ -59,6 +59,7 @@ 
 #define HUGEPAGE_SIZE (2 * 1024 * 1024)
 #define PKT_DUMP_NB_TO_PRINT 16
 #define RUN_ALL_TESTS UINT_MAX
+#define NUM_MAC_ADDRESSES 4
 
 #define print_verbose(x...) do { if (opt_verbose) ksft_print_msg(x); } while (0)
 
@@ -90,6 +91,8 @@  struct xsk_socket_info {
 	struct pkt_stream *pkt_stream;
 	u32 outstanding_tx;
 	u32 rxqsize;
+	u8 dst_mac[ETH_ALEN];
+	u8 src_mac[ETH_ALEN];
 };
 
 struct pkt {
@@ -140,8 +143,6 @@  struct ifobject {
 	bool unaligned_supp;
 	bool multi_buff_supp;
 	bool multi_buff_zc_supp;
-	u8 dst_mac[ETH_ALEN];
-	u8 src_mac[ETH_ALEN];
 };
 
 struct test_spec {
@@ -168,4 +169,7 @@  pthread_mutex_t pacing_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 int pkts_in_flight;
 
+static const u8 g_mac[] = {0x55, 0x44, 0x33, 0x22, 0x11, 0x00};
+static u8 macs[NUM_MAC_ADDRESSES][ETH_ALEN] = {0};
+
 #endif				/* XSKXCEIVER_H_ */