diff mbox series

utils/sctp: bugfix for testlib/sctputil.h

Message ID 20230721071831.83546-1-limin154@huawei.com
State Changes Requested
Headers show
Series utils/sctp: bugfix for testlib/sctputil.h | expand

Commit Message

Li Min July 21, 2023, 7:18 a.m. UTC
The socket is created and bound immediately without waiting for the handshake after close(sk).
"bind(): errno=EADDRINUSE(98): Address already in use" may be reported.
Use SO_REUSEPORT to allow multiple sockets to be bound to the same port.

Signed-off-by: Min Li <limin154@huawei.com>
---
 utils/sctp/testlib/sctputil.h | 9 +++++++++
 1 file changed, 9 insertions(+)

--
2.33.0

Comments

Petr Vorel July 20, 2023, 3:26 p.m. UTC | #1
Hi Li,

> The socket is created and bound immediately without waiting for the handshake after close(sk).
> "bind(): errno=EADDRINUSE(98): Address already in use" may be reported.
> Use SO_REUSEPORT to allow multiple sockets to be bound to the same port.

How this can happen? Running tests too quickly one after the other?
Or what is different on your SUT? I'm not sure if it's good idea to always test
with SO_REUSEADDR and SO_REUSEPORT.

> Signed-off-by: Min Li <limin154@huawei.com>
> ---
>  utils/sctp/testlib/sctputil.h | 9 +++++++++
>  1 file changed, 9 insertions(+)

> diff --git a/utils/sctp/testlib/sctputil.h b/utils/sctp/testlib/sctputil.h
> index 176d623f0..b51a3f9b0 100644
> --- a/utils/sctp/testlib/sctputil.h
> +++ b/utils/sctp/testlib/sctputil.h
> @@ -133,6 +133,15 @@ extern int TST_CNT;
>  static inline int test_socket(int domain, int type, int protocol)
>  {
>  	int sk = socket(domain, type, protocol);
> +	int true_const=1;
What is this unused variable for?

> +
> +	if(setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &true_const, sizeof(int))){
> +		printf("set addr err\n");
Besides missing spaces in if (if (..) { ), we have SAFE_SETSOCKOPT().
Or tst_brkm(TWARN, ...) could be used (we don't use printf directly in LTP).

Kind regards,
Petr

> +	}
> +
> +	if(setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &true_const, sizeof(int))){
> +		printf("set port err\n");
> +	}

>  	if (sk == -1) {
>  		if (errno == EAFNOSUPPORT)
Richard Palethorpe Aug. 29, 2023, 10:25 a.m. UTC | #2
Hello,

Petr Vorel <pvorel@suse.cz> writes:

> Hi Li,
>
>> The socket is created and bound immediately without waiting for the handshake after close(sk).
>> "bind(): errno=EADDRINUSE(98): Address already in use" may be reported.
>> Use SO_REUSEPORT to allow multiple sockets to be bound to the same port.
>
> How this can happen? Running tests too quickly one after the other?
> Or what is different on your SUT? I'm not sure if it's good idea to always test
> with SO_REUSEADDR and SO_REUSEPORT.
>
>> Signed-off-by: Min Li <limin154@huawei.com>
>> ---
>>  utils/sctp/testlib/sctputil.h | 9 +++++++++
>>  1 file changed, 9 insertions(+)
>
>> diff --git a/utils/sctp/testlib/sctputil.h b/utils/sctp/testlib/sctputil.h
>> index 176d623f0..b51a3f9b0 100644
>> --- a/utils/sctp/testlib/sctputil.h
>> +++ b/utils/sctp/testlib/sctputil.h
>> @@ -133,6 +133,15 @@ extern int TST_CNT;
>>  static inline int test_socket(int domain, int type, int protocol)
>>  {
>>  	int sk = socket(domain, type, protocol);
>> +	int true_const=1;
> What is this unused variable for?

It's used as the socket option value.

>
>> +
>> +	if(setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &true_const, sizeof(int))){
>> +		printf("set addr err\n");
> Besides missing spaces in if (if (..) { ), we have SAFE_SETSOCKOPT().
> Or tst_brkm(TWARN, ...) could be used (we don't use printf directly in
> LTP).

+1 for SAFE_SETSOCKOPT

>
> Kind regards,
> Petr
>
>> +	}
>> +
>> +	if(setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &true_const, sizeof(int))){
>> +		printf("set port err\n");
>> +	}

We don't need to set it twice.

Setting patch as changes requested.

>
>>  	if (sk == -1) {
>>  		if (errno == EAFNOSUPPORT)
diff mbox series

Patch

diff --git a/utils/sctp/testlib/sctputil.h b/utils/sctp/testlib/sctputil.h
index 176d623f0..b51a3f9b0 100644
--- a/utils/sctp/testlib/sctputil.h
+++ b/utils/sctp/testlib/sctputil.h
@@ -133,6 +133,15 @@  extern int TST_CNT;
 static inline int test_socket(int domain, int type, int protocol)
 {
 	int sk = socket(domain, type, protocol);
+	int true_const=1;
+
+	if(setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &true_const, sizeof(int))){
+		printf("set addr err\n");
+	}
+
+	if(setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &true_const, sizeof(int))){
+		printf("set port err\n");
+	}

 	if (sk == -1) {
 		if (errno == EAFNOSUPPORT)