Skip to content

Commit 8c44e1a

Browse files
Jon Paul Maloydavem330
authored andcommitted
tipc: unify tipc_wait_for_sndpkt() and tipc_wait_for_sndmsg() functions
The functions tipc_wait_for_sndpkt() and tipc_wait_for_sndmsg() are very similar. The latter function is also called from two locations, and there will be more in the coming commits, which will all need to test on different conditions. Instead of making yet another duplicates of the function, we now introduce a new macro tipc_wait_for_cond() where the wakeup condition can be stated as an argument to the call. This macro replaces all current and future uses of the two functions, which can now be eliminated. Acked-by: Parthasarathy Bhuvaragan <[email protected]> Acked-by: Ying Xue <[email protected]> Signed-off-by: Jon Maloy <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent aa276dd commit 8c44e1a

File tree

1 file changed

+49
-59
lines changed

1 file changed

+49
-59
lines changed

net/tipc/socket.c

Lines changed: 49 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ static void tipc_write_space(struct sock *sk);
110110
static void tipc_sock_destruct(struct sock *sk);
111111
static int tipc_release(struct socket *sock);
112112
static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
113-
static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p);
114113
static void tipc_sk_timeout(unsigned long data);
115114
static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
116115
struct tipc_name_seq const *seq);
@@ -334,6 +333,49 @@ static int tipc_set_sk_state(struct sock *sk, int state)
334333
return res;
335334
}
336335

336+
static int tipc_sk_sock_err(struct socket *sock, long *timeout)
337+
{
338+
struct sock *sk = sock->sk;
339+
int err = sock_error(sk);
340+
int typ = sock->type;
341+
342+
if (err)
343+
return err;
344+
if (typ == SOCK_STREAM || typ == SOCK_SEQPACKET) {
345+
if (sk->sk_state == TIPC_DISCONNECTING)
346+
return -EPIPE;
347+
else if (!tipc_sk_connected(sk))
348+
return -ENOTCONN;
349+
}
350+
if (!*timeout)
351+
return -EAGAIN;
352+
if (signal_pending(current))
353+
return sock_intr_errno(*timeout);
354+
355+
return 0;
356+
}
357+
358+
#define tipc_wait_for_cond(sock_, timeout_, condition_) \
359+
({ \
360+
int rc_ = 0; \
361+
int done_ = 0; \
362+
\
363+
while (!(condition_) && !done_) { \
364+
struct sock *sk_ = sock->sk; \
365+
DEFINE_WAIT_FUNC(wait_, woken_wake_function); \
366+
\
367+
rc_ = tipc_sk_sock_err(sock_, timeout_); \
368+
if (rc_) \
369+
break; \
370+
prepare_to_wait(sk_sleep(sk_), &wait_, \
371+
TASK_INTERRUPTIBLE); \
372+
done_ = sk_wait_event(sk_, timeout_, \
373+
(condition_), &wait_); \
374+
remove_wait_queue(sk_sleep(sk_), &wait_); \
375+
} \
376+
rc_; \
377+
})
378+
337379
/**
338380
* tipc_sk_create - create a TIPC socket
339381
* @net: network namespace (must be default network)
@@ -721,7 +763,7 @@ static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
721763

722764
if (rc == -ELINKCONG) {
723765
tsk->link_cong = 1;
724-
rc = tipc_wait_for_sndmsg(sock, &timeo);
766+
rc = tipc_wait_for_cond(sock, &timeo, !tsk->link_cong);
725767
if (!rc)
726768
continue;
727769
}
@@ -830,31 +872,6 @@ static void tipc_sk_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb,
830872
kfree_skb(skb);
831873
}
832874

833-
static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
834-
{
835-
DEFINE_WAIT_FUNC(wait, woken_wake_function);
836-
struct sock *sk = sock->sk;
837-
struct tipc_sock *tsk = tipc_sk(sk);
838-
int done;
839-
840-
do {
841-
int err = sock_error(sk);
842-
if (err)
843-
return err;
844-
if (sk->sk_shutdown & SEND_SHUTDOWN)
845-
return -EPIPE;
846-
if (!*timeo_p)
847-
return -EAGAIN;
848-
if (signal_pending(current))
849-
return sock_intr_errno(*timeo_p);
850-
851-
add_wait_queue(sk_sleep(sk), &wait);
852-
done = sk_wait_event(sk, timeo_p, !tsk->link_cong, &wait);
853-
remove_wait_queue(sk_sleep(sk), &wait);
854-
} while (!done);
855-
return 0;
856-
}
857-
858875
/**
859876
* tipc_sendmsg - send message in connectionless manner
860877
* @sock: socket structure
@@ -970,7 +987,7 @@ static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz)
970987
}
971988
if (rc == -ELINKCONG) {
972989
tsk->link_cong = 1;
973-
rc = tipc_wait_for_sndmsg(sock, &timeo);
990+
rc = tipc_wait_for_cond(sock, &timeo, !tsk->link_cong);
974991
if (!rc)
975992
continue;
976993
}
@@ -985,36 +1002,6 @@ static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz)
9851002
return rc;
9861003
}
9871004

988-
static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
989-
{
990-
DEFINE_WAIT_FUNC(wait, woken_wake_function);
991-
struct sock *sk = sock->sk;
992-
struct tipc_sock *tsk = tipc_sk(sk);
993-
int done;
994-
995-
do {
996-
int err = sock_error(sk);
997-
if (err)
998-
return err;
999-
if (sk->sk_state == TIPC_DISCONNECTING)
1000-
return -EPIPE;
1001-
else if (!tipc_sk_connected(sk))
1002-
return -ENOTCONN;
1003-
if (!*timeo_p)
1004-
return -EAGAIN;
1005-
if (signal_pending(current))
1006-
return sock_intr_errno(*timeo_p);
1007-
1008-
add_wait_queue(sk_sleep(sk), &wait);
1009-
done = sk_wait_event(sk, timeo_p,
1010-
(!tsk->link_cong &&
1011-
!tsk_conn_cong(tsk)) ||
1012-
!tipc_sk_connected(sk), &wait);
1013-
remove_wait_queue(sk_sleep(sk), &wait);
1014-
} while (!done);
1015-
return 0;
1016-
}
1017-
10181005
/**
10191006
* tipc_send_stream - send stream-oriented data
10201007
* @sock: socket structure
@@ -1109,7 +1096,10 @@ static int __tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
11091096

11101097
tsk->link_cong = 1;
11111098
}
1112-
rc = tipc_wait_for_sndpkt(sock, &timeo);
1099+
rc = tipc_wait_for_cond(sock, &timeo,
1100+
(!tsk->link_cong &&
1101+
!tsk_conn_cong(tsk) &&
1102+
tipc_sk_connected(sk)));
11131103
} while (!rc);
11141104

11151105
__skb_queue_purge(&pktchain);

0 commit comments

Comments
 (0)