All of lore.kernel.org
 help / color / mirror / Atom feed
From: Magnus Karlsson <magnus.karlsson@gmail.com>
To: magnus.karlsson@intel.com, bjorn@kernel.org, ast@kernel.org,
	daniel@iogearbox.net, netdev@vger.kernel.org,
	maciej.fijalkowski@intel.com
Cc: jonathan.lemon@gmail.com, ciara.loftus@intel.com,
	bpf@vger.kernel.org, yhs@fb.com, andrii@kernel.org
Subject: [PATCH bpf-next v3 12/16] selftests: xsk: simplify cleanup of ifobjects
Date: Wed, 25 Aug 2021 11:37:18 +0200	[thread overview]
Message-ID: <20210825093722.10219-13-magnus.karlsson@gmail.com> (raw)
In-Reply-To: <20210825093722.10219-1-magnus.karlsson@gmail.com>

From: Magnus Karlsson <magnus.karlsson@intel.com>

Simpify the cleanup of ifobjects right before the program exits by
introducing functions for creating and destroying these objects.

Signed-off-by: Magnus Karlsson <magnus.karlsson@intel.com>
---
 tools/testing/selftests/bpf/xdpxceiver.c | 72 +++++++++++++-----------
 tools/testing/selftests/bpf/xdpxceiver.h |  1 -
 2 files changed, 40 insertions(+), 33 deletions(-)

diff --git a/tools/testing/selftests/bpf/xdpxceiver.c b/tools/testing/selftests/bpf/xdpxceiver.c
index bc7d6bbbb867..5e586a696742 100644
--- a/tools/testing/selftests/bpf/xdpxceiver.c
+++ b/tools/testing/selftests/bpf/xdpxceiver.c
@@ -1039,62 +1039,70 @@ static void run_pkt_test(int mode, int type)
 	}
 }
 
+static struct ifobject *ifobject_create(void)
+{
+	struct ifobject *ifobj;
+
+	ifobj = calloc(1, sizeof(struct ifobject));
+	if (!ifobj)
+		return NULL;
+
+	ifobj->xsk_arr = calloc(2, sizeof(struct xsk_socket_info *));
+	if (!ifobj->xsk_arr)
+		goto out_xsk_arr;
+
+	ifobj->umem_arr = calloc(2, sizeof(struct xsk_umem_info *));
+	if (!ifobj->umem_arr)
+		goto out_umem_arr;
+
+	return ifobj;
+
+out_umem_arr:
+	free(ifobj->xsk_arr);
+out_xsk_arr:
+	free(ifobj);
+	return NULL;
+}
+
+static void ifobject_delete(struct ifobject *ifobj)
+{
+	free(ifobj->umem_arr);
+	free(ifobj->xsk_arr);
+	free(ifobj);
+}
+
 int main(int argc, char **argv)
 {
 	struct rlimit _rlim = { RLIM_INFINITY, RLIM_INFINITY };
-	bool failure = false;
 	int i, j;
 
 	if (setrlimit(RLIMIT_MEMLOCK, &_rlim))
 		exit_with_error(errno);
 
-	for (int i = 0; i < MAX_INTERFACES; i++) {
-		ifdict[i] = malloc(sizeof(struct ifobject));
+	for (i = 0; i < MAX_INTERFACES; i++) {
+		ifdict[i] = ifobject_create();
 		if (!ifdict[i])
-			exit_with_error(errno);
-
-		ifdict[i]->ifdict_index = i;
-		ifdict[i]->xsk_arr = calloc(2, sizeof(struct xsk_socket_info *));
-		if (!ifdict[i]->xsk_arr) {
-			failure = true;
-			goto cleanup;
-		}
-		ifdict[i]->umem_arr = calloc(2, sizeof(struct xsk_umem_info *));
-		if (!ifdict[i]->umem_arr) {
-			failure = true;
-			goto cleanup;
-		}
+			exit_with_error(ENOMEM);
 	}
 
 	setlocale(LC_ALL, "");
 
 	parse_command_line(argc, argv);
 
-	init_iface(ifdict[0], MAC1, MAC2, IP1, IP2, UDP_PORT1, UDP_PORT2, tx);
-	init_iface(ifdict[1], MAC2, MAC1, IP2, IP1, UDP_PORT2, UDP_PORT1, rx);
+	init_iface(ifdict[tx], MAC1, MAC2, IP1, IP2, UDP_PORT1, UDP_PORT2, tx);
+	init_iface(ifdict[rx], MAC2, MAC1, IP2, IP1, UDP_PORT2, UDP_PORT1, rx);
 
 	ksft_set_plan(TEST_MODE_MAX * TEST_TYPE_MAX);
 
-	for (i = 0; i < TEST_MODE_MAX; i++) {
+	for (i = 0; i < TEST_MODE_MAX; i++)
 		for (j = 0; j < TEST_TYPE_MAX; j++) {
 			run_pkt_test(i, j);
 			usleep(USLEEP_MAX);
 		}
-	}
 
-cleanup:
-	for (int i = 0; i < MAX_INTERFACES; i++) {
-		if (ifdict[i]->ns_fd != -1)
-			close(ifdict[i]->ns_fd);
-		free(ifdict[i]->xsk_arr);
-		free(ifdict[i]->umem_arr);
-		free(ifdict[i]);
-	}
-
-	if (failure)
-		exit_with_error(errno);
+	for (i = 0; i < MAX_INTERFACES; i++)
+		ifobject_delete(ifdict[i]);
 
 	ksft_exit_pass();
-
 	return 0;
 }
diff --git a/tools/testing/selftests/bpf/xdpxceiver.h b/tools/testing/selftests/bpf/xdpxceiver.h
index 1c5457e9f1d6..316c3565a99e 100644
--- a/tools/testing/selftests/bpf/xdpxceiver.h
+++ b/tools/testing/selftests/bpf/xdpxceiver.h
@@ -122,7 +122,6 @@ struct ifobject {
 	void *(*func_ptr)(void *arg);
 	struct flow_vector fv;
 	int ns_fd;
-	int ifdict_index;
 	u32 dst_ip;
 	u32 src_ip;
 	u16 src_port;
-- 
2.29.0


  parent reply	other threads:[~2021-08-25  9:38 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-25  9:37 [PATCH bpf-next v3 00/16] selftests: xsk: various simplifications Magnus Karlsson
2021-08-25  9:37 ` [PATCH bpf-next v3 01/16] selftests: xsk: remove color mode Magnus Karlsson
2021-08-25  9:37 ` [PATCH bpf-next v3 02/16] selftests: xsk: remove the num_tx_packets option Magnus Karlsson
2021-08-25  9:37 ` [PATCH bpf-next v3 03/16] selftests: xsk: remove unused variables Magnus Karlsson
2021-08-25  9:37 ` [PATCH bpf-next v3 04/16] selftests: xsk: return correct error codes Magnus Karlsson
2021-08-25  9:37 ` [PATCH bpf-next v3 05/16] selftests: xsk: simplify the retry code Magnus Karlsson
2021-08-25  9:37 ` [PATCH bpf-next v3 06/16] selftests: xsk: remove end-of-test packet Magnus Karlsson
2021-08-25  9:37 ` [PATCH bpf-next v3 07/16] selftests: xsk: disassociate umem size with packets sent Magnus Karlsson
2021-08-25  9:37 ` [PATCH bpf-next v3 08/16] selftests: xsk: rename worker_* functions that are not thread entry points Magnus Karlsson
2021-08-25  9:37 ` [PATCH bpf-next v3 09/16] selftests: xsk: simplify packet validation in xsk tests Magnus Karlsson
2021-08-25  9:37 ` [PATCH bpf-next v3 10/16] selftests: xsk: validate tx stats on tx thread Magnus Karlsson
2021-08-25 17:33   ` Alexei Starovoitov
2021-08-25  9:37 ` [PATCH bpf-next v3 11/16] selftests: xsk: decrease sending speed Magnus Karlsson
2021-08-25  9:37 ` Magnus Karlsson [this message]
2021-08-25  9:37 ` [PATCH bpf-next v3 13/16] selftests: xsk: generate packet directly in umem Magnus Karlsson
2021-08-25  9:37 ` [PATCH bpf-next v3 14/16] selftests: xsk: generate packets from specification Magnus Karlsson
2021-08-25  9:37 ` [PATCH bpf-next v3 15/16] selftests: xsk: make enums lower case Magnus Karlsson
2021-08-25  9:37 ` [PATCH bpf-next v3 16/16] selftests: xsk: preface options with opt Magnus Karlsson
2021-08-25 18:26 ` [PATCH bpf-next v3 00/16] selftests: xsk: various simplifications Maciej Fijalkowski
2021-08-25 19:32   ` Alexei Starovoitov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210825093722.10219-13-magnus.karlsson@gmail.com \
    --to=magnus.karlsson@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bjorn@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=ciara.loftus@intel.com \
    --cc=daniel@iogearbox.net \
    --cc=jonathan.lemon@gmail.com \
    --cc=maciej.fijalkowski@intel.com \
    --cc=magnus.karlsson@intel.com \
    --cc=netdev@vger.kernel.org \
    --cc=yhs@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.