Skip to content

Commit 762405e

Browse files
committed
Merge branch 'mptcp-next'
Mat Martineau says: ==================== mptcp: Protocol in-use tracking and code cleanup Here's a collection of commits from the MPTCP tree: Patches 1-4 and 6 contain miscellaneous code cleanup for more consistent use of helper functions, existing local variables, and better naming. Patches 5, 7, and 9 add sock_prot_inuse tracking for MPTCP and an associated self test. Patch 8 modifies the mptcp_connect self test tool to exit on SIGUSR1 when in "slow mode". ==================== Signed-off-by: David S. Miller <[email protected]>
2 parents fb59bf2 + e04a30f commit 762405e

File tree

9 files changed

+95
-32
lines changed

9 files changed

+95
-32
lines changed

net/mptcp/options.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,8 +1594,7 @@ void mptcp_write_options(struct tcphdr *th, __be32 *ptr, struct tcp_sock *tp,
15941594
TCPOLEN_MPTCP_PRIO,
15951595
opts->backup, TCPOPT_NOP);
15961596

1597-
MPTCP_INC_STATS(sock_net((const struct sock *)tp),
1598-
MPTCP_MIB_MPPRIOTX);
1597+
MPTCP_INC_STATS(sock_net(ssk), MPTCP_MIB_MPPRIOTX);
15991598
}
16001599

16011600
mp_capable_done:

net/mptcp/pm_netlink.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,7 +1143,7 @@ void mptcp_pm_nl_subflow_chk_stale(const struct mptcp_sock *msk, struct sock *ss
11431143
if (!tcp_rtx_and_write_queues_empty(ssk)) {
11441144
subflow->stale = 1;
11451145
__mptcp_retransmit_pending_data(sk);
1146-
MPTCP_INC_STATS(sock_net(sk), MPTCP_MIB_SUBFLOWSTALE);
1146+
MPTCP_INC_STATS(net, MPTCP_MIB_SUBFLOWSTALE);
11471147
}
11481148
unlock_sock_fast(ssk, slow);
11491149

@@ -1903,8 +1903,7 @@ static int mptcp_nl_cmd_set_flags(struct sk_buff *skb, struct genl_info *info)
19031903
}
19041904

19051905
if (token)
1906-
return mptcp_userspace_pm_set_flags(sock_net(skb->sk),
1907-
token, &addr, &remote, bkup);
1906+
return mptcp_userspace_pm_set_flags(net, token, &addr, &remote, bkup);
19081907

19091908
spin_lock_bh(&pernet->lock);
19101909
entry = __lookup_addr(pernet, &addr.addr, lookup_by_id);

net/mptcp/protocol.c

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -923,9 +923,8 @@ static void mptcp_check_for_eof(struct mptcp_sock *msk)
923923
static struct sock *mptcp_subflow_recv_lookup(const struct mptcp_sock *msk)
924924
{
925925
struct mptcp_subflow_context *subflow;
926-
struct sock *sk = (struct sock *)msk;
927926

928-
sock_owned_by_me(sk);
927+
msk_owned_by_me(msk);
929928

930929
mptcp_for_each_subflow(msk, subflow) {
931930
if (READ_ONCE(subflow->data_avail))
@@ -1408,7 +1407,7 @@ static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk)
14081407
u64 linger_time;
14091408
long tout = 0;
14101409

1411-
sock_owned_by_me(sk);
1410+
msk_owned_by_me(msk);
14121411

14131412
if (__mptcp_check_fallback(msk)) {
14141413
if (!msk->first)
@@ -1890,7 +1889,7 @@ static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied)
18901889
u32 time, advmss = 1;
18911890
u64 rtt_us, mstamp;
18921891

1893-
sock_owned_by_me(sk);
1892+
msk_owned_by_me(msk);
18941893

18951894
if (copied <= 0)
18961895
return;
@@ -2217,7 +2216,7 @@ static struct sock *mptcp_subflow_get_retrans(struct mptcp_sock *msk)
22172216
struct mptcp_subflow_context *subflow;
22182217
int min_stale_count = INT_MAX;
22192218

2220-
sock_owned_by_me((const struct sock *)msk);
2219+
msk_owned_by_me(msk);
22212220

22222221
if (__mptcp_check_fallback(msk))
22232222
return NULL;
@@ -2724,8 +2723,8 @@ static int mptcp_init_sock(struct sock *sk)
27242723
mptcp_ca_reset(sk);
27252724

27262725
sk_sockets_allocated_inc(sk);
2727-
sk->sk_rcvbuf = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[1]);
2728-
sk->sk_sndbuf = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_wmem[1]);
2726+
sk->sk_rcvbuf = READ_ONCE(net->ipv4.sysctl_tcp_rmem[1]);
2727+
sk->sk_sndbuf = READ_ONCE(net->ipv4.sysctl_tcp_wmem[1]);
27292728

27302729
return 0;
27312730
}
@@ -2892,6 +2891,12 @@ static __poll_t mptcp_check_readable(struct mptcp_sock *msk)
28922891
return EPOLLIN | EPOLLRDNORM;
28932892
}
28942893

2894+
static void mptcp_listen_inuse_dec(struct sock *sk)
2895+
{
2896+
if (inet_sk_state_load(sk) == TCP_LISTEN)
2897+
sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
2898+
}
2899+
28952900
bool __mptcp_close(struct sock *sk, long timeout)
28962901
{
28972902
struct mptcp_subflow_context *subflow;
@@ -2901,6 +2906,7 @@ bool __mptcp_close(struct sock *sk, long timeout)
29012906
sk->sk_shutdown = SHUTDOWN_MASK;
29022907

29032908
if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) {
2909+
mptcp_listen_inuse_dec(sk);
29042910
inet_sk_state_store(sk, TCP_CLOSE);
29052911
goto cleanup;
29062912
}
@@ -3001,6 +3007,7 @@ static int mptcp_disconnect(struct sock *sk, int flags)
30013007
if (msk->fastopening)
30023008
return 0;
30033009

3010+
mptcp_listen_inuse_dec(sk);
30043011
inet_sk_state_store(sk, TCP_CLOSE);
30053012

30063013
mptcp_stop_timer(sk);
@@ -3639,31 +3646,34 @@ static int mptcp_stream_connect(struct socket *sock, struct sockaddr *uaddr,
36393646
static int mptcp_listen(struct socket *sock, int backlog)
36403647
{
36413648
struct mptcp_sock *msk = mptcp_sk(sock->sk);
3649+
struct sock *sk = sock->sk;
36423650
struct socket *ssock;
36433651
int err;
36443652

36453653
pr_debug("msk=%p", msk);
36463654

3647-
lock_sock(sock->sk);
3655+
lock_sock(sk);
36483656
ssock = __mptcp_nmpc_socket(msk);
36493657
if (!ssock) {
36503658
err = -EINVAL;
36513659
goto unlock;
36523660
}
36533661

36543662
mptcp_token_destroy(msk);
3655-
inet_sk_state_store(sock->sk, TCP_LISTEN);
3656-
sock_set_flag(sock->sk, SOCK_RCU_FREE);
3663+
inet_sk_state_store(sk, TCP_LISTEN);
3664+
sock_set_flag(sk, SOCK_RCU_FREE);
36573665

36583666
err = ssock->ops->listen(ssock, backlog);
3659-
inet_sk_state_store(sock->sk, inet_sk_state_load(ssock->sk));
3660-
if (!err)
3661-
mptcp_copy_inaddrs(sock->sk, ssock->sk);
3667+
inet_sk_state_store(sk, inet_sk_state_load(ssock->sk));
3668+
if (!err) {
3669+
sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
3670+
mptcp_copy_inaddrs(sk, ssock->sk);
3671+
}
36623672

36633673
mptcp_event_pm_listener(ssock->sk, MPTCP_EVENT_LISTENER_CREATED);
36643674

36653675
unlock:
3666-
release_sock(sock->sk);
3676+
release_sock(sk);
36673677
return err;
36683678
}
36693679

net/mptcp/protocol.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ static inline void mptcp_token_init_request(struct request_sock *req)
754754

755755
int mptcp_token_new_request(struct request_sock *req);
756756
void mptcp_token_destroy_request(struct request_sock *req);
757-
int mptcp_token_new_connect(struct sock *sk);
757+
int mptcp_token_new_connect(struct sock *ssk);
758758
void mptcp_token_accept(struct mptcp_subflow_request_sock *r,
759759
struct mptcp_sock *msk);
760760
bool mptcp_token_exists(u32 token);

net/mptcp/sockopt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
static struct sock *__mptcp_tcp_fallback(struct mptcp_sock *msk)
2020
{
21-
sock_owned_by_me((const struct sock *)msk);
21+
msk_owned_by_me(msk);
2222

2323
if (likely(!__mptcp_check_fallback(msk)))
2424
return NULL;

net/mptcp/token.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ int mptcp_token_new_request(struct request_sock *req)
134134

135135
/**
136136
* mptcp_token_new_connect - create new key/idsn/token for subflow
137-
* @sk: the socket that will initiate a connection
137+
* @ssk: the socket that will initiate a connection
138138
*
139139
* This function is called when a new outgoing mptcp connection is
140140
* initiated.
@@ -148,11 +148,12 @@ int mptcp_token_new_request(struct request_sock *req)
148148
*
149149
* returns 0 on success.
150150
*/
151-
int mptcp_token_new_connect(struct sock *sk)
151+
int mptcp_token_new_connect(struct sock *ssk)
152152
{
153-
struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(sk);
153+
struct mptcp_subflow_context *subflow = mptcp_subflow_ctx(ssk);
154154
struct mptcp_sock *msk = mptcp_sk(subflow->conn);
155155
int retries = MPTCP_TOKEN_MAX_RETRIES;
156+
struct sock *sk = subflow->conn;
156157
struct token_bucket *bucket;
157158

158159
again:
@@ -169,12 +170,13 @@ int mptcp_token_new_connect(struct sock *sk)
169170
}
170171

171172
pr_debug("ssk=%p, local_key=%llu, token=%u, idsn=%llu\n",
172-
sk, subflow->local_key, subflow->token, subflow->idsn);
173+
ssk, subflow->local_key, subflow->token, subflow->idsn);
173174

174175
WRITE_ONCE(msk->token, subflow->token);
175176
__sk_nulls_add_node_rcu((struct sock *)msk, &bucket->msk_chain);
176177
bucket->chain_len++;
177178
spin_unlock_bh(&bucket->lock);
179+
sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
178180
return 0;
179181
}
180182

@@ -190,8 +192,10 @@ void mptcp_token_accept(struct mptcp_subflow_request_sock *req,
190192
struct mptcp_sock *msk)
191193
{
192194
struct mptcp_subflow_request_sock *pos;
195+
struct sock *sk = (struct sock *)msk;
193196
struct token_bucket *bucket;
194197

198+
sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
195199
bucket = token_bucket(req->token);
196200
spin_lock_bh(&bucket->lock);
197201

@@ -370,12 +374,14 @@ void mptcp_token_destroy_request(struct request_sock *req)
370374
*/
371375
void mptcp_token_destroy(struct mptcp_sock *msk)
372376
{
377+
struct sock *sk = (struct sock *)msk;
373378
struct token_bucket *bucket;
374379
struct mptcp_sock *pos;
375380

376381
if (sk_unhashed((struct sock *)msk))
377382
return;
378383

384+
sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
379385
bucket = token_bucket(msk->token);
380386
spin_lock_bh(&bucket->lock);
381387
pos = __token_lookup_msk(bucket, msk->token);

net/mptcp/token_test.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ static struct mptcp_sock *build_msk(struct kunit *test)
5757
KUNIT_EXPECT_NOT_ERR_OR_NULL(test, msk);
5858
refcount_set(&((struct sock *)msk)->sk_refcnt, 1);
5959
sock_net_set((struct sock *)msk, &init_net);
60+
61+
/* be sure the token helpers can dereference sk->sk_prot */
62+
((struct sock *)msk)->sk_prot = &tcp_prot;
6063
return msk;
6164
}
6265

tools/testing/selftests/net/mptcp/diag.sh

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ flush_pids()
1717
sleep 1.1
1818

1919
ip netns pids "${ns}" | xargs --no-run-if-empty kill -SIGUSR1 &>/dev/null
20+
21+
for _ in $(seq 10); do
22+
[ -z "$(ip netns pids "${ns}")" ] && break
23+
sleep 0.1
24+
done
2025
}
2126

2227
cleanup()
@@ -37,15 +42,20 @@ if [ $? -ne 0 ];then
3742
exit $ksft_skip
3843
fi
3944

45+
get_msk_inuse()
46+
{
47+
ip netns exec $ns cat /proc/net/protocols | awk '$1~/^MPTCP$/{print $3}'
48+
}
49+
4050
__chk_nr()
4151
{
42-
local condition="$1"
52+
local command="$1"
4353
local expected=$2
4454
local msg nr
4555

4656
shift 2
4757
msg=$*
48-
nr=$(ss -inmHMN $ns | $condition)
58+
nr=$(eval $command)
4959

5060
printf "%-50s" "$msg"
5161
if [ $nr != $expected ]; then
@@ -57,9 +67,17 @@ __chk_nr()
5767
test_cnt=$((test_cnt+1))
5868
}
5969

70+
__chk_msk_nr()
71+
{
72+
local condition=$1
73+
shift 1
74+
75+
__chk_nr "ss -inmHMN $ns | $condition" $*
76+
}
77+
6078
chk_msk_nr()
6179
{
62-
__chk_nr "grep -c token:" $*
80+
__chk_msk_nr "grep -c token:" $*
6381
}
6482

6583
wait_msk_nr()
@@ -97,12 +115,12 @@ wait_msk_nr()
97115

98116
chk_msk_fallback_nr()
99117
{
100-
__chk_nr "grep -c fallback" $*
118+
__chk_msk_nr "grep -c fallback" $*
101119
}
102120

103121
chk_msk_remote_key_nr()
104122
{
105-
__chk_nr "grep -c remote_key" $*
123+
__chk_msk_nr "grep -c remote_key" $*
106124
}
107125

108126
__chk_listen()
@@ -142,6 +160,26 @@ chk_msk_listen()
142160
nr=$(ss -Ml $filter | wc -l)
143161
}
144162

163+
chk_msk_inuse()
164+
{
165+
local expected=$1
166+
local listen_nr
167+
168+
shift 1
169+
170+
listen_nr=$(ss -N "${ns}" -Ml | grep -c LISTEN)
171+
expected=$((expected + listen_nr))
172+
173+
for _ in $(seq 10); do
174+
if [ $(get_msk_inuse) -eq $expected ];then
175+
break
176+
fi
177+
sleep 0.1
178+
done
179+
180+
__chk_nr get_msk_inuse $expected $*
181+
}
182+
145183
# $1: ns, $2: port
146184
wait_local_port_listen()
147185
{
@@ -195,8 +233,10 @@ wait_connected $ns 10000
195233
chk_msk_nr 2 "after MPC handshake "
196234
chk_msk_remote_key_nr 2 "....chk remote_key"
197235
chk_msk_fallback_nr 0 "....chk no fallback"
236+
chk_msk_inuse 2 "....chk 2 msk in use"
198237
flush_pids
199238

239+
chk_msk_inuse 0 "....chk 0 msk in use after flush"
200240

201241
echo "a" | \
202242
timeout ${timeout_test} \
@@ -211,8 +251,11 @@ echo "b" | \
211251
127.0.0.1 >/dev/null &
212252
wait_connected $ns 10001
213253
chk_msk_fallback_nr 1 "check fallback"
254+
chk_msk_inuse 1 "....chk 1 msk in use"
214255
flush_pids
215256

257+
chk_msk_inuse 0 "....chk 0 msk in use after flush"
258+
216259
NR_CLIENTS=100
217260
for I in `seq 1 $NR_CLIENTS`; do
218261
echo "a" | \
@@ -232,6 +275,9 @@ for I in `seq 1 $NR_CLIENTS`; do
232275
done
233276

234277
wait_msk_nr $((NR_CLIENTS*2)) "many msk socket present"
278+
chk_msk_inuse $((NR_CLIENTS*2)) "....chk many msk in use"
235279
flush_pids
236280

281+
chk_msk_inuse 0 "....chk 0 msk in use after flush"
282+
237283
exit $ret

tools/testing/selftests/net/mptcp/mptcp_connect.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ static int copyfd_io_poll(int infd, int peerfd, int outfd,
627627
char rbuf[8192];
628628
ssize_t len;
629629

630-
if (fds.events == 0)
630+
if (fds.events == 0 || quit)
631631
break;
632632

633633
switch (poll(&fds, 1, poll_timeout)) {
@@ -733,7 +733,7 @@ static int copyfd_io_poll(int infd, int peerfd, int outfd,
733733
}
734734

735735
/* leave some time for late join/announce */
736-
if (cfg_remove)
736+
if (cfg_remove && !quit)
737737
usleep(cfg_wait);
738738

739739
return 0;

0 commit comments

Comments
 (0)