Skip to contentSkip to conversation

Netlink helper functions refactoring

PATCH Superseded Martin Doucha 2 participants 8 messages Oct 13, 2023 – Nov 13, 2023 Library code

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Netlink helper functions refactoring

Let's refactor the rtnetlink helper functions for generic use where possible. The Netlink context structure can be used for other Netlink protocols like NETLINK_CRYPTO or NETLINK_NETFILTER. One of the exceptions is
the NETLINK_KOBJECT_UEVENT protocol which returns raw data without the usual headers.

The first patch replaces the tst_rtnl_* and RTNL_* prefix with tst_netlink_* and NETLINK_* respectively to signify that the functions can be used for generic netlink communication. The attribute handling functions are specific to the NETLINK_ROUTE protocol so they keep the old prefix.

The last patch then replaces old netlink send/receive helper functions with the context-based helpers in crypto test code.

Coming soon is a new netfilter CVE test which will also use the refactored code.

Martin Doucha (3):
tst_rtnetlink: Refactor helper function for generic use tst_netlink_destroy_context(): Allow safely passing NULL context
crypto: Replace old netlink helper functions with netlink contexts

 doc/network-c-api.txt                   | 159 +++++++++++-----------
 include/tst_crypto.h                    |  67 +---------
 include/tst_netdevice.h                 |   2 +-
 include/tst_netlink.h                   | 171 ++++++++++++++----------
 include/tst_rtnetlink.h                 | 108 ---------------
 lib/tst_crypto.c                        |  90 +++----------
 lib/tst_netdevice.c                     |  95 ++++++-------
 lib/{tst_rtnetlink.c => tst_netlink.c}  |  88 ++++++------
 testcases/cve/tcindex01.c               |   4 +-
 testcases/kernel/crypto/crypto_user01.c |  58 +++-----
 testcases/kernel/crypto/crypto_user02.c |  17 +--
 testcases/kernel/crypto/pcrypt_aead01.c |  10 +-
 12 files changed, 331 insertions(+), 538 deletions(-)
 delete mode 100644 include/tst_rtnetlink.h
 rename lib/{tst_rtnetlink.c => tst_netlink.c} (77%)
Signature · 5 lines
2.42.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

PATCH 1/3 tst_rtnetlink: Refactor helper function for generic use

The netlink context structure can be used for any netlink protocol, not just for NETLINK_ROUTE. Change the RTNL prefix to NETLINK on all helper functions which don't use NETLINK_ROUTE features and add a new protocol parameter to tst_netlink_create_context().

---
 doc/network-c-api.txt     | 155 +++++++++++++++++++-------------------
 include/tst_rtnetlink.h   |  85 ++++++++++-----------
 lib/tst_netdevice.c       |  93 ++++++++++++-----------
 lib/tst_rtnetlink.c       |  83 ++++++++++----------
 testcases/cve/tcindex01.c |   2 +-
 5 files changed, 212 insertions(+), 206 deletions(-)
doc/network-c-api.txt +79 -76
index 3bf2a1f8a..25e4bbd18 100644
@@ -143,7 +143,7 @@ static void setup(void)
 When opening a localhost socket isn't enough and the test needs special device
 or routing configuration, the netdevice library can create the required network
 setup without calling external programs. Internally, the netdevice functions
-use a rtnetlink socket to communicate with the kernel.
+use a netlink socket to communicate with the kernel.
 
 All of these functions will call +tst_brk()+ on failure, unless stated
 otherwise. Error values described below are returned only during test cleanup
@@ -274,12 +274,12 @@ static void setup(void)
 }
 -------------------------------------------------------------------------------
 
-3 rtnetlink API
+3 Netlink API
 ---------------
 
 +#include "tst_rtnetlink.h"+
 
-The rtnetlink library provides helper functions for constructing and sending
+The netlink library provides helper functions for constructing and sending
 arbitrary messages and parsing kernel responses.
 
 All of the functions below will call +tst_brk()+ on failure, unless stated
@@ -291,7 +291,7 @@ stage.
 
 [source,c]
 -------------------------------------------------------------------------------
-struct tst_rtnl_context;
+struct tst_netlink_context;
 
 struct tst_rtnl_attr_list {
 	unsigned short type;
@@ -300,7 +300,7 @@ struct tst_rtnl_attr_list {
 	const struct tst_rtnl_attr_list *sublist;
 };
 
-struct tst_rtnl_message {
+struct tst_netlink_message {
 	struct nlmsghdr *header;
 	struct nlmsgerr *err;
 	void *payload;
@@ -308,10 +308,10 @@ struct tst_rtnl_message {
 };
 -------------------------------------------------------------------------------
 
-+struct tst_rtnl_context+ is an opaque rtnetlink socket with buffer for
++struct tst_netlink_context+ is an opaque netlink socket with buffer for
 constructing and sending arbitrary messages using the functions described
-below. Create a new context using +RTNL_CREATE_CONTEXT()+, then free it using
-+RTNL_DESTROY_CONTEXT()+ when you're done with it.
+below. Create a new context using +NETLINK_CREATE_CONTEXT()+, then free it
+using +NETLINK_DESTROY_CONTEXT()+ when you're done with it.
 
 +struct tst_rtnl_attr_list+ is a helper structure for defining complex
 rtnetlink message attribute payloads, including nested attribute lists. Every
@@ -331,10 +331,10 @@ negative +len+.
   to the +len+ field. If you do not want to add nested attributes, set
   +sublist+ to +NULL+.
 
-+struct tst_rtnl_message+ is a structure holding partially parsed rtnetlink
-messages received from the kernel. +RTNL_RECV()+ returns an array of these
++struct tst_netlink_message+ is a structure holding partially parsed netlink
+messages received from the kernel. +NETLINK_RECV()+ returns an array of these
 structures with the last item having +NULL+ in the +header+ field. Call
-+RTNL_FREE_MESSAGE()+ to free a message list returned by +RTNL_RECV()+.
++NETLINK_FREE_MESSAGE()+ to free a message list returned by +NETLINK_RECV()+.
 
 - +header+ is the netlink header structure of the message. +NULL+ in the header
   field terminates a list of messages.
@@ -349,83 +349,86 @@ structures with the last item having +NULL+ in the +header+ field. Call
 3.2 Sending and receiving messages
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
-- +struct tst_rtnl_context *RTNL_CREATE_CONTEXT(void)+ – Creates a new
-  rtnetlink communication context for use with the functions described below.
-  Returns +NULL+ on error.
+- +struct tst_netlink_context *NETLINK_CREATE_CONTEXT(int protocol)+ – Creates
+  a new netlink communication context with given netlink protocol for use
+  with the functions described below. Returns +NULL+ on error.
 
-- +void RTNL_FREE_MESSAGE(struct tst_rtnl_message *msg)+ – Frees an array of
-  messages returned by +RTNL_RECV()+.
+- +void NETLINK_FREE_MESSAGE(struct tst_netlink_message *msg)+ – Frees
+  an array of messages returned by +NETLINK_RECV()+.
 
-- +void RTNL_DESTROY_CONTEXT(struct tst_rtnl_context *ctx)+ – Closes a
-  communication context created by +RTNL_CREATE_CONTEXT()+.
+- +void NETLINK_DESTROY_CONTEXT(struct tst_netlink_context *ctx)+ – Closes a
+  communication context created by +NETLINK_CREATE_CONTEXT()+.
 
-- +int RTNL_SEND(struct tst_rtnl_context *ctx)+ – Sends all messages waiting
-  in +ctx+ buffer to the kernel. If there are multiple messages to send, a new
-  +NLMSG_DONE+ message will be added automatically. Returns the number of
-  bytes sent on success. Return 0 or negative value on error.
+- +int NETLINK_SEND(struct tst_netlink_context *ctx)+ – Sends all messages
+  waiting in +ctx+ buffer to the kernel. If there are multiple messages
+  to send, a new +NLMSG_DONE+ message will be added automatically. Returns
+  the number of bytes sent on success. Return 0 or negative value on error.
 
-- +int RTNL_SEND_VALIDATE(struct tst_rtnl_context *ctx)+ – Sends all messages
-  just like +RTNL_SEND()+, then receives the response from the kernel and
-  validates results of requests sent with the +NLM_F_ACK+ flag. This function
-  calls +tst_brk()+ as usual if communication fails but it will return error
-  status without terminating the test if one of the received messages contains
-  error code. See +RTNL_CHECK_ACKS()+ below for explanation of the return
-  value.
+- +int NETLINK_SEND_VALIDATE(struct tst_netlink_context *ctx)+ – Sends all
+  messages just like +NETLINK_SEND()+, then receives the response from
+  the kernel and validates results of requests sent with the +NLM_F_ACK+ flag.
+  This function calls +tst_brk()+ as usual if communication fails but it will
+  return error status without terminating the test if one of the received
+  messages contains error code. See +NETLINK_CHECK_ACKS()+ below for
+  explanation of the return value.
 
-- +int RTNL_WAIT(struct tst_rtnl_context *ctx)+ – Waits until data becomes
-  available to read from the rtnetlink socket (timeout: 1 second). Returns 1
+- +int NETLINK_WAIT(struct tst_netlink_context *ctx)+ – Waits until data becomes
+  available to read from the netlink socket (timeout: 1 second). Returns 1
   if there is data to read, 0 on timeout or -1 on error.
 
-- +struct tst_rtnl_message *RTNL_RECV(struct tst_rtnl_context *ctx)+ – Receives
-  rtnetlink messages from the kernel. The messages are received in non-blocking
-  mode so calling +RTNL_WAIT()+ first is recommended. Returns an array of
-  partially parsed messages terminated by an item with +NULL+ in the +header+
-  field. On error or when there are no messages to receive, returns +NULL+.
-  Call +RTNL_FREE_MESSAGE()+ to free the returned data.
-
-- +int RTNL_CHECK_ACKS(struct tst_rtnl_context *ctx, struct tst_rtnl_message
-  *response)+ – Validate results of requests sent with the +NLM_F_ACK+ flag.
-  Do not call +RTNL_ADD_MESSAGE()+ between +RTNL_SEND()+ and
-  +RTNL_CHECK_ACKS()+ because it will reset the state of +ctx+ and prevent
-  result validation. Returns 1 if all messages sent with the +NLM_F_ACK+ flag
-  have a corresponding message in +response+ and the error code is 0. If any
-  of the expected response messages is missing, this function will call
-  +tst_brk()+ (or return 0 during test cleanup phase). If any of the response
-  messages has non-zero error code, this function will return 0 and store the
-  first non-zero error code in global variable +tst_rtnl_errno+ (sign-flipped
-  just like regular libc +errno+).
+- +struct tst_netlink_message *NETLINK_RECV(struct tst_netlink_context *ctx)+ –
+  Receives netlink messages from the kernel. The messages are received
+  in non-blocking mode so calling +NETLINK_WAIT()+ first is recommended.
+  Returns an array of partially parsed messages terminated by an item with
+  +NULL+ in the +header+ field. On error or when there are no messages
+  to receive, returns +NULL+. Call +NETLINK_FREE_MESSAGE()+ to free
+  the returned data.
+
+- +int NETLINK_CHECK_ACKS(struct tst_netlink_context *ctx,
+  struct tst_netlink_message *response)+ – Validate results of requests sent
+  with the +NLM_F_ACK+ flag. Do not call +NETLINK_ADD_MESSAGE()+ between
+  +NETLINK_SEND()+ and +NETLINK_CHECK_ACKS()+ because it will reset the state
+  of +ctx+ and prevent result validation. Returns 1 if all messages sent
+  with the +NLM_F_ACK+ flag have a corresponding message in +response+ and
+  the error code is 0. If any of the expected response messages is missing,
+  this function will call +tst_brk()+ (or return 0 during test cleanup phase).
+  If any of the response messages has non-zero error code, this function will
+  return 0 and store the first non-zero error code in global variable
+  +tst_netlink_errno+ (sign-flipped just like regular libc +errno+).
 
 3.3 Creating messages
 ~~~~~~~~~~~~~~~~~~~~~
 
-- +int RTNL_ADD_MESSAGE(struct tst_rtnl_context *ctx, const struct nlmsghdr
-  *header, const void *payload, size_t payload_size)+ – Adds new rtnetlink
-  message to +ctx+ buffer. You need to provide message +header+ and optional
-  +payload+. +payload_size+ is the size of +payload+ data in bytes. If you
-  don't want to add any payload data, set +payload+ to +NULL+ and
+- +int NETLINK_ADD_MESSAGE(struct tst_netlink_context *ctx, const struct
+  nlmsghdr *header, const void *payload, size_t payload_size)+ – Adds new
+  netlink message to +ctx+ buffer. You need to provide message +header+ and
+  optional +payload+. +payload_size+ is the size of +payload+ data in bytes.
+  If you don't want to add any payload data, set +payload+ to +NULL+ and
   +payload_size+ to 0. This function will automatically fill the +nlmsg_len+,
   +nlmsg_seq+ and +nlmsg_pid+ fields of the new message header. You don't need
   to set those. It'll also automatically add +NLM_F_MULTI+ flag when needed.
   Returns 1 on success, 0 on error. Note that the first call of
-  +RTNL_ADD_MESSAGE()+ after +RTNL_SEND()+ will reset the state of +ctx+
-  and +RTNL_CHECK_ACKS()+ will not work correctly until the next +RTNL_SEND()+.
-
-- +int RTNL_ADD_ATTR(struct tst_rtnl_context *ctx, unsigned short type, const
-  void *data, unsigned short len)+ – Adds new attribute to the last message
-  in +ctx+ buffer. See +RTNL_ADD_MESSAGE()+. You need to provide attribute
-  +type+ which will be stored in +struct rtattr.rta_type+, optional payload
-  +data+ and payload size +len+ in bytes. If you don't want to add any payload,
-  set +data+ to +NULL+ and +len+ to 0. Returns 1 on success, 0 on error.
-
-- +int RTNL_ADD_ATTR_STRING(struct tst_rtnl_context *ctx, unsigned short type,
-  const char *data)+ – Adds new string attribute to the last message in +ctx+
-  buffer. Parameters and return value are the same as for +RTNL_ADD_ATTR()+,
-  except the payload length is calculated using +strlen()+.
-
-- +int RTNL_ADD_ATTR_LIST(struct tst_rtnl_context *ctx, const struct
+  +NETLINK_ADD_MESSAGE()+ after +NETLINK_SEND()+ will reset the state of +ctx+
+  and +NETLINK_CHECK_ACKS()+ will not work correctly until the next
+  +NETLINK_SEND()+.
+
+- +int RTNL_ADD_ATTR(struct tst_netlink_context *ctx, unsigned short type,
+  const void *data, unsigned short len)+ – Adds new attribute to the last
+  message in +ctx+ buffer. See +NETLINK_ADD_MESSAGE()+. You need to provide
+  attribute +type+ which will be stored in +struct rtattr.rta_type+, optional
+  payload +data+ and payload size +len+ in bytes. If you don't want to add any
+  payload, set +data+ to +NULL+ and +len+ to 0. Returns 1 on success,
+  0 on error.
+
+- +int RTNL_ADD_ATTR_STRING(struct tst_netlink_context *ctx, unsigned short
+  type, const char *data)+ – Adds new string attribute to the last message
+  in +ctx+ buffer. Parameters and return value are the same as for
+  +RTNL_ADD_ATTR()+, except the payload length is calculated using +strlen()+.
+
+- +int RTNL_ADD_ATTR_LIST(struct tst_netlink_context *ctx, const struct
   tst_rtnl_attr_list *list)+ – Adds a list of attributes to the last message
   in +ctx+ buffer. See description of +struct tst_rtnl_attr_list+ and
-  +RTNL_ADD_MESSAGE()+ above.  Returns the number of added attributes on
+  +NETLINK_ADD_MESSAGE()+ above.  Returns the number of added attributes on
   success (nested attributes are not counted), -1 on error.
 
 Example Usage
@@ -447,7 +450,7 @@ Example Usage
 
 void setup(void)
 {
-	struct tst_rtnl_context *ctx;
+	struct tst_netlink_context *ctx;
 	int index, ret;
 	in_addr_t addr;
 
@@ -465,12 +468,12 @@ void setup(void)
 	index = NETDEV_INDEX_BY_NAME("ltp_veth1");
 	info.ifa_index = index;
 
-	ctx = RTNL_CREATE_CONTEXT();
-	RTNL_ADD_MESSAGE(ctx, &header, &info, sizeof(info));
+	ctx = NETLINK_CREATE_CONTEXT(NETLINK_ROUTE);
+	NETLINK_ADD_MESSAGE(ctx, &header, &info, sizeof(info));
 	addr = inet_addr("192.168.123.45");
 	RTNL_ADD_ATTR(ctx, IFA_LOCAL, &addr, sizeof(addr));
-	ret = RTNL_SEND_VALIDATE(ctx);
-	RTNL_DESTROY_CONTEXT(ctx);
+	ret = NETLINK_SEND_VALIDATE(ctx);
+	NETLINK_DESTROY_CONTEXT(ctx);
 
 	if (!ret) {
 		tst_brk(TBROK, "Failed to set ltp_veth1 address");
include/tst_rtnetlink.h +43 -42

143 lines not on this page · raw message

lib/tst_netdevice.c +47 -46

273 lines not on this page · raw message

lib/tst_rtnetlink.c +42 -41

230 lines not on this page · raw message

testcases/cve/tcindex01.c +1 -1
index 91bfafb53..eabad4188 100644
@@ -95,7 +95,7 @@ static void run(void)
 		1, "tcindex");
 	ret = tst_netdev_add_traffic_filter(__FILE__, __LINE__, 0, DEVNAME,
 		qd_handle, 10, ETH_P_IP, 1, "tcindex", f_config);
-	TST_ERR = tst_rtnl_errno;
+	TST_ERR = tst_netlink_errno;
 	NETDEV_REMOVE_QDISC(DEVNAME, AF_UNSPEC, TC_H_ROOT, qd_handle, "htb");
 
 	if (ret)
Signature · 5 lines
2.42.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v1 1/3
#2 lore raw

Re: [LTP] [PATCH 1/3] tst_rtnetlink: Refactor helper function for generic use

Hi Martin,

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

PATCH 2/3 tst_netlink_destroy_context(): Allow safely passing NULL context

In reply to Martin Doucha
---
 lib/tst_rtnetlink.c | 3 +++
 1 file changed, 3 insertions(+)
lib/tst_rtnetlink.c +3 -0
index eacfdced1..bf782ffb5 100644
@@ -55,6 +55,9 @@ static int netlink_grow_buffer(const char *file, const int lineno,
 void tst_netlink_destroy_context(const char *file, const int lineno,
 	struct tst_netlink_context *ctx)
 {
+	if (!ctx)
+		return;
+
 	safe_close(file, lineno, NULL, ctx->socket);
 	free(ctx->buffer);
 	free(ctx);
Signature · 5 lines
2.42.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v1 2/3
#4 lore raw

Re: [LTP] [PATCH 2/3] tst_netlink_destroy_context(): Allow safely passing NULL context

Hi Martin,

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

PATCH 3/3 crypto: Replace old netlink helper functions with netlink contexts

In reply to Martin Doucha

Finish refactoring of netlink helper functions for generic use and replace the very limited netlink send/receive functions used by the crypto tests with the new context-based helpers.

---

All three crypto tests have been verified on affected kernels.

 doc/network-c-api.txt                   |   4 +-
 include/tst_crypto.h                    |  67 +---------
 include/tst_netdevice.h                 |   2 +-
 include/tst_netlink.h                   | 171 ++++++++++++++----------
 include/tst_rtnetlink.h                 | 109 ---------------
 lib/tst_crypto.c                        |  90 +++----------
 lib/tst_netdevice.c                     |   2 +-
 lib/{tst_rtnetlink.c => tst_netlink.c}  |   2 +-
 testcases/cve/tcindex01.c               |   2 +-
 testcases/kernel/crypto/crypto_user01.c |  58 +++-----
 testcases/kernel/crypto/crypto_user02.c |  17 +--
 testcases/kernel/crypto/pcrypt_aead01.c |  10 +-
 12 files changed, 159 insertions(+), 375 deletions(-)
 delete mode 100644 include/tst_rtnetlink.h
 rename lib/{tst_rtnetlink.c => tst_netlink.c} (99%)
doc/network-c-api.txt +2 -2
index 25e4bbd18..d04dd6aa3 100644
@@ -277,7 +277,7 @@ static void setup(void)
 3 Netlink API
 ---------------
 
-+#include "tst_rtnetlink.h"+
++#include "tst_netlink.h"+
 
 The netlink library provides helper functions for constructing and sending
 arbitrary messages and parsing kernel responses.
@@ -443,7 +443,7 @@ Example Usage
 #include <arpa/inet.h>
 
 #include "tst_test.h"
-#include "tst_rtnetlink.h"
+#include "tst_netlink.h"
 #include "tst_netdevice.h"
 
 ...
include/tst_crypto.h +7 -60

96 lines not on this page · raw message

include/tst_netdevice.h +1 -1
index 5e62ba065..4239d0960 100644
@@ -5,7 +5,7 @@
 #ifndef TST_NETDEVICE_H
 #define TST_NETDEVICE_H
 
-#include "tst_rtnetlink.h"
+#include "tst_netlink.h"
 
 /* Find device index for given network interface name. */
 int tst_netdev_index_by_name(const char *file, const int lineno,
include/tst_netlink.h +97 -74

184 lines not on this page · raw message

include/tst_rtnetlink.h deleted +0 -109

109 lines not on this page · raw message

lib/tst_crypto.c +15 -75

117 lines not on this page · raw message

lib/tst_netdevice.c +1 -1
index 5873b3d58..6f86b8089 100644
@@ -12,7 +12,7 @@
 
 #define TST_NO_DEFAULT_MAIN
 #include "tst_test.h"
-#include "tst_rtnetlink.h"
+#include "tst_netlink.h"
 #include "tst_netdevice.h"
 
 static struct tst_netlink_context *create_request(const char *file,
lib/tst_netlink.c renamed from lib/tst_rtnetlink.c +1 -1
similarity index 99%
rename from lib/tst_rtnetlink.c
rename to lib/tst_netlink.c
index bf782ffb5..bd05df81a 100644
@@ -13,7 +13,7 @@
 #include <sys/poll.h>
 #define TST_NO_DEFAULT_MAIN
 #include "tst_test.h"
-#include "tst_rtnetlink.h"
+#include "tst_netlink.h"
 
 struct tst_netlink_context {
 	int socket;
testcases/cve/tcindex01.c +1 -1
index eabad4188..c7dfbdee0 100644
@@ -24,7 +24,7 @@
 #include <linux/pkt_sched.h>
 #include <linux/pkt_cls.h>
 #include "tst_test.h"
-#include "tst_rtnetlink.h"
+#include "tst_netlink.h"
 #include "tst_netdevice.h"
 #include "lapi/sched.h"
 #include "lapi/if_ether.h"
testcases/kernel/crypto/crypto_user01.c +20 -38

98 lines not on this page · raw message

testcases/kernel/crypto/crypto_user02.c +9 -8
index afaff5d18..89cbb9bc5 100644
@@ -52,7 +52,7 @@ static const char * const ALGORITHM_CANDIDATES[] = {
 };
 
 static const char* algorithm = NULL;
-static struct tst_crypto_session ses = TST_CRYPTO_SESSION_INIT;
+static struct tst_netlink_context *ctx;
 
 
 static void setup(void)
@@ -60,7 +60,8 @@ static void setup(void)
 	int rc;
 	unsigned i;
 	struct crypto_user_alg alg;
-	tst_crypto_open(&ses);
+
+	ctx = NETLINK_CREATE_CONTEXT(NETLINK_CRYPTO);
 
 	/* find an algorithm, that is not in use */
 	for (i = 0; i < ARRAY_SIZE(ALGORITHM_CANDIDATES); ++i) {
@@ -68,12 +69,12 @@ static void setup(void)
 		strcpy(alg.cru_driver_name, ALGORITHM_CANDIDATES[i]);
 
 		/* try to add it, to see if it is valid */
-		rc = tst_crypto_add_alg(&ses, &alg);
+		rc = tst_crypto_add_alg(ctx, &alg);
 		if (rc != 0)
 			continue;
 
 		/* it also has to be deletable */
-		rc = tst_crypto_del_alg(&ses, &alg);
+		rc = tst_crypto_del_alg(ctx, &alg, 1000);
 		if (rc == 0) {
 			algorithm = ALGORITHM_CANDIDATES[i];
 			break;
@@ -103,9 +104,9 @@ static void run(void)
 
 		if (pid == 0) {
 			/* Child process: execute CRYPTO_MSG_NEWALG. */
-			tst_crypto_open(&ses);
+			ctx = NETLINK_CREATE_CONTEXT(NETLINK_CRYPTO);
 			for (;;) {
-				TEST(tst_crypto_add_alg(&ses, &alg));
+				TEST(tst_crypto_add_alg(ctx, &alg));
 				if (TST_RET && TST_RET != -EEXIST)
 					tst_brk(TBROK | TRERRNO,
 						"unexpected error from tst_crypto_add_alg()");
@@ -123,7 +124,7 @@ static void run(void)
 		SAFE_WAIT(&status);
 		if (!WIFSIGNALED(status) || WTERMSIG(status) != SIGKILL)
 			tst_brk(TBROK, "child %s", tst_strstatus(status));
-		TEST(tst_crypto_del_alg(&ses, &alg));
+		TEST(tst_crypto_del_alg(ctx, &alg, 1000));
 		if (TST_RET && TST_RET != -ENOENT)
 			tst_brk(TBROK | TRERRNO,
 				"unexpected error from tst_crypto_del_alg()");
@@ -134,7 +135,7 @@ static void run(void)
 
 static void cleanup(void)
 {
-	tst_crypto_close(&ses);
+	NETLINK_DESTROY_CONTEXT(ctx);
 }
 
 static struct tst_test test = {
testcases/kernel/crypto/pcrypt_aead01.c +5 -5
index 3b4f5d8d1..3979f317a 100644
@@ -26,11 +26,11 @@
 
 #define ATTEMPTS 10000
 
-static struct tst_crypto_session ses = TST_CRYPTO_SESSION_INIT;
+static struct tst_netlink_context *ctx;
 
 void setup(void)
 {
-	tst_crypto_open(&ses);
+	ctx = NETLINK_CREATE_CONTEXT(NETLINK_CRYPTO);
 }
 
 void run(void)
@@ -43,7 +43,7 @@ void run(void)
 	};
 
 	for (i = 0; i < ATTEMPTS; ++i) {
-		TEST(tst_crypto_add_alg(&ses, &a));
+		TEST(tst_crypto_add_alg(ctx, &a));
 		if (TST_RET && TST_RET == -ENOENT) {
 			tst_brk(TCONF | TRERRNO,
 				"pcrypt, hmac, sha256, cbc or aes not supported");
@@ -51,7 +51,7 @@ void run(void)
 		if (TST_RET && TST_RET != -EEXIST)
 			tst_brk(TBROK | TRERRNO, "add_alg");
 
-		TEST(tst_crypto_del_alg(&ses, &a));
+		TEST(tst_crypto_del_alg(ctx, &a, 1000));
 		if (TST_RET)
 			tst_brk(TBROK | TRERRNO, "del_alg");
 
@@ -67,7 +67,7 @@ void run(void)
 
 void cleanup(void)
 {
-	tst_crypto_close(&ses);
+	NETLINK_DESTROY_CONTEXT(ctx);
 }
 
 static struct tst_test test = {
Signature · 5 lines
2.42.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v1 3/3
#6 lore raw

Re: [LTP] [PATCH 3/3] crypto: Replace old netlink helper functions with netlink contexts

Hi Martin,

...

Quoted 20 lines

+++ b/include/tst_crypto.h

include/tst_crypto.h +0 -15
@@ -13,67 +13,13 @@
 #define TST_CRYPTO_H
 
 #include "lapi/cryptouser.h"
-
-/**
- * A reference to a crypto session and associated state.
- *
- * Holds state relevant to a netlink crypto connection. The seq_num is used
- * to tag each message sent to the netlink layer and is automatically
- * incremented by the tst_crypto_ functions. When the netlink layer sends a
- * response (ack) it will use the sequences number from the request.
- *
- * Some functions, such as delete ALG, may return EBUSY in which case it is
- * safe to retry them. The retries field allows you to set the number of
- * times this should be done. If set to zero the operation will only be tried
- * once. For operations which do not return EBUSY, the field is ignored.
- *
- * Use TST_CRYPTO_SESSION_INIT to statically initialize this struct with sane

include/tst_crypto.h line 40

Quoted 10 lines
include/tst_crypto.h +0 -10
- * defaults.
- */
-struct tst_crypto_session {
-	/** File descriptor for the netlink socket */
-	int fd;
-	/** A sequence number used to identify responses from the kernel. */
-	uint32_t seq_num;
-	/** Number of times some operations will be retried. */
-	uint32_t retries;
-};

include/tst_crypto.h still contains "@relates tst_crypto_session". I guess this should be replaced with "@relates tst_netlink_context", right?

I can fix this before merge.

...

include/tst_crypto.h line 50

Quoted 9 lines
include/tst_crypto.h +0 -9
-/**
- * Default static definition of tst_crypto_session.
- *
- * @relates tst_crypto_session
- */
-#define TST_CRYPTO_SESSION_INIT {\
-	.fd = 0,                 \
-	.seq_num = 0,            \
-	.retries = 1000          \

I quite like this macro which had the default values.
But atm only retries 1000 is what needs to be specified, thus understand that you didn't introduce any macro wrapper).

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#7 lore raw
In reply to Martin Doucha

Hi all,

Quoted 1 line
 doc/network-c-api.txt                   | 159 +++++++++++-----------

Unfortunately, Martin based his patchset on the LTP before I merged my patchset which renamed files in doc.

Therefore it's needed to download the patchset manually and modify it in order to apply:

s/network-c-api.txt/C-Test-Network-API.asciidoc/g

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp