Skip to contentSkip to conversation

net: Introduce TST_GET_UNUSED_PORT() macro into C API

PATCH New Petr Vorel 2 participants 3 messages Apr 23, 2019 – May 20, 2019 Library code

lore
Share & tools
lore

Petr Vorel

author
#0 lore raw

PATCH 1/1 net: Introduce TST_GET_UNUSED_PORT() macro into C API

into both new and legacy API.
Therefore tst_get_unused_port() was ported into new C API.

Adding TST_GET_UNUSED_PORT() was needed to add support for printing file and line in error messages passed tst_brkm().

+ deleted lib/tst_net.c (net functions for legacy C API).
+ move tst_* function prototypes in headers to the end (partly sort alphabetically).

---
Hi,

TST_GET_UNUSED_PORT() is needed for route tests, which I'm porting into new C API. But also Steve would benefit from it in his sendmmsg rewrite. Tested on travis [2].

Kind regards,
Petr

[1] https://github.com/linux-test-project/ltp/pull/515#pullrequestreview-223235708
[2] https://travis-ci.org/pevik/ltp/builds/523638339

---
 include/old/old_safe_net.h                    |  3 +
 include/old/test.h                            |  7 --
 include/safe_net_fn.h                         | 13 +--
 include/tst_safe_net.h                        |  9 +-
 lib/safe_net.c                                | 66 ++++++++++++++
 lib/tst_net.c                                 | 89 -------------------
 testcases/kernel/syscalls/bind/bind01.c       |  2 +-
 testcases/kernel/syscalls/connect/connect01.c |  2 +-
 testcases/kernel/syscalls/sendmsg/sendmsg01.c |  2 +-
 tools/apicmds/ltpapicmd.c                     |  2 +-
 10 files changed, 87 insertions(+), 108 deletions(-)
 delete mode 100644 lib/tst_net.c
include/old/old_safe_net.h +3 -0
index 609787b32..639094a94 100644
@@ -44,4 +44,7 @@
 	safe_getsockname(__FILE__, __LINE__, (cleanup_fn), sockfd, addr, \
 			 addrlen)
 
+#define TST_GET_UNUSED_PORT(cleanup_fn, family, type) \
+	tst_get_unused_port(__FILE__, __LINE__, (cleanup_fn), family, type)
+
 #endif /* OLD_SAFE_NET_H__ */
include/old/test.h +0 -7
index 0738237e9..604254eea 100644
@@ -195,13 +195,6 @@ void tst_mkfs_(const char *file, const int lineno, void (cleanup_fn)(void),
 	       const char *dev, const char *fs_type,
 	       const char *const fs_opts[], const char *const extra_opts[]);
 
-/* lib/tst_net.c
- *
- * Return unused port
- */
-unsigned short tst_get_unused_port(void (cleanup_fn)(void),
-	unsigned short family, int type);
-
 /* lib/tst_res.c
  * tst_strsig converts signal's value to corresponding string.
  * tst_strerrno converts errno to corresponding string.
include/safe_net_fn.h +8 -5
index 3183b2a1c..fdbb3791c 100644
@@ -25,11 +25,6 @@
 #include <arpa/inet.h>
 #include <sys/un.h>
 
-char *tst_sock_addr(const struct sockaddr *sa, socklen_t salen, char *res,
-		    size_t len);
-
-int tst_getsockport(const char *file, const int lineno, int sockfd);
-
 int safe_socket(const char *file, const int lineno, void (cleanup_fn)(void),
 		int domain, int type, int protocol);
 
@@ -72,4 +67,12 @@ int safe_getsockname(const char *file, const int lineno,
 int safe_gethostname(const char *file, const int lineno,
 		     char *name, size_t size);
 
+int tst_getsockport(const char *file, const int lineno, int sockfd);
+
+unsigned short tst_get_unused_port(const char *file, const int lineno,
+	      void (cleanup_fn)(void), unsigned short family, int type);
+
+char *tst_sock_addr(const struct sockaddr *sa, socklen_t salen, char *res,
+		    size_t len);
+
 #endif /* SAFE_NET_FN_H__ */
include/tst_safe_net.h +6 -3
index 83a2f27bf..d49a36073 100644
@@ -26,9 +26,6 @@
 
 #include "safe_net_fn.h"
 
-#define TST_GETSOCKPORT(sockfd) \
-	tst_getsockport(__FILE__, __LINE__, sockfd)
-
 #define SAFE_SOCKET(domain, type, protocol) \
 	safe_socket(__FILE__, __LINE__, NULL, domain, type, protocol)
 
@@ -77,4 +74,10 @@
 #define SAFE_GETHOSTNAME(name, size) \
 	safe_gethostname(__FILE__, __LINE__, name, size)
 
+#define TST_GETSOCKPORT(sockfd) \
+	tst_getsockport(__FILE__, __LINE__, sockfd)
+
+#define TST_GET_UNUSED_PORT(family, type) \
+	tst_get_unused_port(__FILE__, __LINE__, NULL, family, type)
+
 #endif /* TST_SAFE_NET_H__ */
lib/safe_net.c +66 -0

69 lines not on this page · raw message

lib/tst_net.c deleted +0 -89

89 lines not on this page · raw message

testcases/kernel/syscalls/bind/bind01.c +1 -1
index 868749368..d2f50e806 100644
@@ -152,7 +152,7 @@ void setup(void)
 	/* initialize sockaddr's */
 	sin1.sin_family = AF_INET;
 	/* this port must be unused! */
-	sin1.sin_port = tst_get_unused_port(NULL, AF_INET, SOCK_STREAM);
+	sin1.sin_port = TST_GET_UNUSED_PORT(NULL, AF_INET, SOCK_STREAM);
 	sin1.sin_addr.s_addr = INADDR_ANY;
 
 	sin2.sin_family = AF_INET;
testcases/kernel/syscalls/connect/connect01.c +1 -1
index 34e260f59..0d7d15a83 100644
@@ -184,7 +184,7 @@ void setup(void)
 
 	sin2.sin_family = AF_INET;
 	/* this port must be unused! */
-	sin2.sin_port = tst_get_unused_port(NULL, AF_INET, SOCK_STREAM);
+	sin2.sin_port = TST_GET_UNUSED_PORT(NULL, AF_INET, SOCK_STREAM);
 	sin2.sin_addr.s_addr = INADDR_ANY;
 
 	sin3.sin_family = AF_INET;
testcases/kernel/syscalls/sendmsg/sendmsg01.c +1 -1
index 36f6914ff..cf6e74289 100644
@@ -662,7 +662,7 @@ static void setup5(void)
 	 * 5-tuple than already connected
 	 */
 	sin2 = sin1;
-	sin2.sin_port = tst_get_unused_port(cleanup, AF_INET, SOCK_STREAM);
+	sin2.sin_port = TST_GET_UNUSED_PORT(cleanup, AF_INET, SOCK_STREAM);
 }
 
 static void setup6(void)
tools/apicmds/ltpapicmd.c +1 -1
index 4b66c4226..d94061242 100644
@@ -213,7 +213,7 @@ unsigned short apicmd_get_unused_port(int argc, char *argv[])
 		if (!p[i]->cmd)
 			goto err;
 	}
-	return  tst_get_unused_port(NULL, p[0]->value, p[1]->value);
+	return  TST_GET_UNUSED_PORT(NULL, p[0]->value, p[1]->value);
 
 err:
 	fprintf(stderr, "Usage: tst_get_unused_port FAMILY TYPE\n"
Signature · 1 line
2.21.0
#1 lore raw

PATCH 1/1 net: Introduce TST_GET_UNUSED_PORT() macro into C API

Hi!
Looks good, acked.

Btw the ltpapicmd implementation should get either removed (in a case that it will be no longer used) or we should convert it to a standalone helper the testcases/lib/ directory. But that is something that should be done in a seprate patch...

Signature · 2 lines
Cyril Hrubis
[email protected]
author
#2 lore raw

PATCH 1/1 net: Introduce TST_GET_UNUSED_PORT() macro into C API

Hi Cyril,

Quoted 2 lines

Hi!
Looks good, acked.

Thanks, merged with your ack.

Quoted 4 lines

Btw the ltpapicmd implementation should get either removed (in a case that it will be no longer used) or we should convert it to a standalone helper the testcases/lib/ directory. But that is something that should be done in a seprate patch...

OK, going to push patch implementing it.

Kind regards,
Petr