Skip to content

Commit 900f65d

Browse files
nealcardwelldavem330
authored andcommitted
tcp: move duplicate code from tcp_v4_init_sock()/tcp_v6_init_sock()
This commit moves the (substantial) common code shared between tcp_v4_init_sock() and tcp_v6_init_sock() to a new address-family independent function, tcp_init_sock(). Centralizing this functionality should help avoid drift issues, e.g. where the IPv4 side is updated without a corresponding update to IPv6. There was already some drift: IPv4 initialized snd_cwnd to TCP_INIT_CWND, while the IPv6 side was still initializing snd_cwnd to 2 (in this case it should not matter, since snd_cwnd is also initialized in tcp_init_metrics(), but the general risks and maintenance overhead remain). When diffing the old and new code, note that new tcp_init_sock() function uses the order of steps from the tcp_v4_init_sock() implementation (the order is slightly different in tcp_v6_init_sock()). Signed-off-by: Neal Cardwell <[email protected]> Acked-by: Eric Dumazet <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent e66e9a3 commit 900f65d

File tree

4 files changed

+68
-99
lines changed

4 files changed

+68
-99
lines changed

include/net/tcp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ extern void tcp_enter_loss(struct sock *sk, int how);
395395
extern void tcp_clear_retrans(struct tcp_sock *tp);
396396
extern void tcp_update_metrics(struct sock *sk);
397397
extern void tcp_close(struct sock *sk, long timeout);
398+
extern void tcp_init_sock(struct sock *sk);
398399
extern unsigned int tcp_poll(struct file * file, struct socket *sock,
399400
struct poll_table_struct *wait);
400401
extern int tcp_getsockopt(struct sock *sk, int level, int optname,

net/ipv4/tcp.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,70 @@ static int retrans_to_secs(u8 retrans, int timeout, int rto_max)
363363
return period;
364364
}
365365

366+
/* Address-family independent initialization for a tcp_sock.
367+
*
368+
* NOTE: A lot of things set to zero explicitly by call to
369+
* sk_alloc() so need not be done here.
370+
*/
371+
void tcp_init_sock(struct sock *sk)
372+
{
373+
struct inet_connection_sock *icsk = inet_csk(sk);
374+
struct tcp_sock *tp = tcp_sk(sk);
375+
376+
skb_queue_head_init(&tp->out_of_order_queue);
377+
tcp_init_xmit_timers(sk);
378+
tcp_prequeue_init(tp);
379+
380+
icsk->icsk_rto = TCP_TIMEOUT_INIT;
381+
tp->mdev = TCP_TIMEOUT_INIT;
382+
383+
/* So many TCP implementations out there (incorrectly) count the
384+
* initial SYN frame in their delayed-ACK and congestion control
385+
* algorithms that we must have the following bandaid to talk
386+
* efficiently to them. -DaveM
387+
*/
388+
tp->snd_cwnd = TCP_INIT_CWND;
389+
390+
/* See draft-stevens-tcpca-spec-01 for discussion of the
391+
* initialization of these values.
392+
*/
393+
tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
394+
tp->snd_cwnd_clamp = ~0;
395+
tp->mss_cache = TCP_MSS_DEFAULT;
396+
397+
tp->reordering = sysctl_tcp_reordering;
398+
icsk->icsk_ca_ops = &tcp_init_congestion_ops;
399+
400+
sk->sk_state = TCP_CLOSE;
401+
402+
sk->sk_write_space = sk_stream_write_space;
403+
sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
404+
405+
icsk->icsk_sync_mss = tcp_sync_mss;
406+
407+
/* TCP Cookie Transactions */
408+
if (sysctl_tcp_cookie_size > 0) {
409+
/* Default, cookies without s_data_payload. */
410+
tp->cookie_values =
411+
kzalloc(sizeof(*tp->cookie_values),
412+
sk->sk_allocation);
413+
if (tp->cookie_values != NULL)
414+
kref_init(&tp->cookie_values->kref);
415+
}
416+
/* Presumed zeroed, in order of appearance:
417+
* cookie_in_always, cookie_out_never,
418+
* s_data_constant, s_data_in, s_data_out
419+
*/
420+
sk->sk_sndbuf = sysctl_tcp_wmem[1];
421+
sk->sk_rcvbuf = sysctl_tcp_rmem[1];
422+
423+
local_bh_disable();
424+
sock_update_memcg(sk);
425+
sk_sockets_allocated_inc(sk);
426+
local_bh_enable();
427+
}
428+
EXPORT_SYMBOL(tcp_init_sock);
429+
366430
/*
367431
* Wait for a TCP event.
368432
*

net/ipv4/tcp_ipv4.c

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1890,62 +1890,14 @@ static int tcp_v4_init_sock(struct sock *sk)
18901890
struct inet_connection_sock *icsk = inet_csk(sk);
18911891
struct tcp_sock *tp = tcp_sk(sk);
18921892

1893-
skb_queue_head_init(&tp->out_of_order_queue);
1894-
tcp_init_xmit_timers(sk);
1895-
tcp_prequeue_init(tp);
1896-
1897-
icsk->icsk_rto = TCP_TIMEOUT_INIT;
1898-
tp->mdev = TCP_TIMEOUT_INIT;
1899-
1900-
/* So many TCP implementations out there (incorrectly) count the
1901-
* initial SYN frame in their delayed-ACK and congestion control
1902-
* algorithms that we must have the following bandaid to talk
1903-
* efficiently to them. -DaveM
1904-
*/
1905-
tp->snd_cwnd = TCP_INIT_CWND;
1906-
1907-
/* See draft-stevens-tcpca-spec-01 for discussion of the
1908-
* initialization of these values.
1909-
*/
1910-
tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
1911-
tp->snd_cwnd_clamp = ~0;
1912-
tp->mss_cache = TCP_MSS_DEFAULT;
1913-
1914-
tp->reordering = sysctl_tcp_reordering;
1915-
icsk->icsk_ca_ops = &tcp_init_congestion_ops;
1916-
1917-
sk->sk_state = TCP_CLOSE;
1918-
1919-
sk->sk_write_space = sk_stream_write_space;
1920-
sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
1893+
tcp_init_sock(sk);
19211894

19221895
icsk->icsk_af_ops = &ipv4_specific;
1923-
icsk->icsk_sync_mss = tcp_sync_mss;
1896+
19241897
#ifdef CONFIG_TCP_MD5SIG
19251898
tp->af_specific = &tcp_sock_ipv4_specific;
19261899
#endif
19271900

1928-
/* TCP Cookie Transactions */
1929-
if (sysctl_tcp_cookie_size > 0) {
1930-
/* Default, cookies without s_data_payload. */
1931-
tp->cookie_values =
1932-
kzalloc(sizeof(*tp->cookie_values),
1933-
sk->sk_allocation);
1934-
if (tp->cookie_values != NULL)
1935-
kref_init(&tp->cookie_values->kref);
1936-
}
1937-
/* Presumed zeroed, in order of appearance:
1938-
* cookie_in_always, cookie_out_never,
1939-
* s_data_constant, s_data_in, s_data_out
1940-
*/
1941-
sk->sk_sndbuf = sysctl_tcp_wmem[1];
1942-
sk->sk_rcvbuf = sysctl_tcp_rmem[1];
1943-
1944-
local_bh_disable();
1945-
sock_update_memcg(sk);
1946-
sk_sockets_allocated_inc(sk);
1947-
local_bh_enable();
1948-
19491901
return 0;
19501902
}
19511903

net/ipv6/tcp_ipv6.c

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1831,62 +1831,14 @@ static int tcp_v6_init_sock(struct sock *sk)
18311831
struct inet_connection_sock *icsk = inet_csk(sk);
18321832
struct tcp_sock *tp = tcp_sk(sk);
18331833

1834-
skb_queue_head_init(&tp->out_of_order_queue);
1835-
tcp_init_xmit_timers(sk);
1836-
tcp_prequeue_init(tp);
1837-
1838-
icsk->icsk_rto = TCP_TIMEOUT_INIT;
1839-
tp->mdev = TCP_TIMEOUT_INIT;
1840-
1841-
/* So many TCP implementations out there (incorrectly) count the
1842-
* initial SYN frame in their delayed-ACK and congestion control
1843-
* algorithms that we must have the following bandaid to talk
1844-
* efficiently to them. -DaveM
1845-
*/
1846-
tp->snd_cwnd = 2;
1847-
1848-
/* See draft-stevens-tcpca-spec-01 for discussion of the
1849-
* initialization of these values.
1850-
*/
1851-
tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
1852-
tp->snd_cwnd_clamp = ~0;
1853-
tp->mss_cache = TCP_MSS_DEFAULT;
1854-
1855-
tp->reordering = sysctl_tcp_reordering;
1856-
1857-
sk->sk_state = TCP_CLOSE;
1834+
tcp_init_sock(sk);
18581835

18591836
icsk->icsk_af_ops = &ipv6_specific;
1860-
icsk->icsk_ca_ops = &tcp_init_congestion_ops;
1861-
icsk->icsk_sync_mss = tcp_sync_mss;
1862-
sk->sk_write_space = sk_stream_write_space;
1863-
sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
18641837

18651838
#ifdef CONFIG_TCP_MD5SIG
18661839
tp->af_specific = &tcp_sock_ipv6_specific;
18671840
#endif
18681841

1869-
/* TCP Cookie Transactions */
1870-
if (sysctl_tcp_cookie_size > 0) {
1871-
/* Default, cookies without s_data_payload. */
1872-
tp->cookie_values =
1873-
kzalloc(sizeof(*tp->cookie_values),
1874-
sk->sk_allocation);
1875-
if (tp->cookie_values != NULL)
1876-
kref_init(&tp->cookie_values->kref);
1877-
}
1878-
/* Presumed zeroed, in order of appearance:
1879-
* cookie_in_always, cookie_out_never,
1880-
* s_data_constant, s_data_in, s_data_out
1881-
*/
1882-
sk->sk_sndbuf = sysctl_tcp_wmem[1];
1883-
sk->sk_rcvbuf = sysctl_tcp_rmem[1];
1884-
1885-
local_bh_disable();
1886-
sock_update_memcg(sk);
1887-
sk_sockets_allocated_inc(sk);
1888-
local_bh_enable();
1889-
18901842
return 0;
18911843
}
18921844

0 commit comments

Comments
 (0)