Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2006, 2018 Oracle and/or its affiliates. All rights reserved.
0003  *
0004  * This software is available to you under a choice of one of two
0005  * licenses.  You may choose to be licensed under the terms of the GNU
0006  * General Public License (GPL) Version 2, available from the file
0007  * COPYING in the main directory of this source tree, or the
0008  * OpenIB.org BSD license below:
0009  *
0010  *     Redistribution and use in source and binary forms, with or
0011  *     without modification, are permitted provided that the following
0012  *     conditions are met:
0013  *
0014  *      - Redistributions of source code must retain the above
0015  *        copyright notice, this list of conditions and the following
0016  *        disclaimer.
0017  *
0018  *      - Redistributions in binary form must reproduce the above
0019  *        copyright notice, this list of conditions and the following
0020  *        disclaimer in the documentation and/or other materials
0021  *        provided with the distribution.
0022  *
0023  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0024  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0025  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0026  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
0027  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
0028  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
0029  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0030  * SOFTWARE.
0031  *
0032  */
0033 #include <linux/kernel.h>
0034 #include <linux/slab.h>
0035 #include <linux/in.h>
0036 #include <linux/module.h>
0037 #include <net/tcp.h>
0038 #include <net/net_namespace.h>
0039 #include <net/netns/generic.h>
0040 #include <net/addrconf.h>
0041 
0042 #include "rds.h"
0043 #include "tcp.h"
0044 
0045 /* only for info exporting */
0046 static DEFINE_SPINLOCK(rds_tcp_tc_list_lock);
0047 static LIST_HEAD(rds_tcp_tc_list);
0048 
0049 /* rds_tcp_tc_count counts only IPv4 connections.
0050  * rds6_tcp_tc_count counts both IPv4 and IPv6 connections.
0051  */
0052 static unsigned int rds_tcp_tc_count;
0053 #if IS_ENABLED(CONFIG_IPV6)
0054 static unsigned int rds6_tcp_tc_count;
0055 #endif
0056 
0057 /* Track rds_tcp_connection structs so they can be cleaned up */
0058 static DEFINE_SPINLOCK(rds_tcp_conn_lock);
0059 static LIST_HEAD(rds_tcp_conn_list);
0060 static atomic_t rds_tcp_unloading = ATOMIC_INIT(0);
0061 
0062 static struct kmem_cache *rds_tcp_conn_slab;
0063 
0064 static int rds_tcp_skbuf_handler(struct ctl_table *ctl, int write,
0065                  void *buffer, size_t *lenp, loff_t *fpos);
0066 
0067 static int rds_tcp_min_sndbuf = SOCK_MIN_SNDBUF;
0068 static int rds_tcp_min_rcvbuf = SOCK_MIN_RCVBUF;
0069 
0070 static struct ctl_table rds_tcp_sysctl_table[] = {
0071 #define RDS_TCP_SNDBUF  0
0072     {
0073         .procname       = "rds_tcp_sndbuf",
0074         /* data is per-net pointer */
0075         .maxlen         = sizeof(int),
0076         .mode           = 0644,
0077         .proc_handler   = rds_tcp_skbuf_handler,
0078         .extra1     = &rds_tcp_min_sndbuf,
0079     },
0080 #define RDS_TCP_RCVBUF  1
0081     {
0082         .procname       = "rds_tcp_rcvbuf",
0083         /* data is per-net pointer */
0084         .maxlen         = sizeof(int),
0085         .mode           = 0644,
0086         .proc_handler   = rds_tcp_skbuf_handler,
0087         .extra1     = &rds_tcp_min_rcvbuf,
0088     },
0089     { }
0090 };
0091 
0092 u32 rds_tcp_write_seq(struct rds_tcp_connection *tc)
0093 {
0094     /* seq# of the last byte of data in tcp send buffer */
0095     return tcp_sk(tc->t_sock->sk)->write_seq;
0096 }
0097 
0098 u32 rds_tcp_snd_una(struct rds_tcp_connection *tc)
0099 {
0100     return tcp_sk(tc->t_sock->sk)->snd_una;
0101 }
0102 
0103 void rds_tcp_restore_callbacks(struct socket *sock,
0104                    struct rds_tcp_connection *tc)
0105 {
0106     rdsdebug("restoring sock %p callbacks from tc %p\n", sock, tc);
0107     write_lock_bh(&sock->sk->sk_callback_lock);
0108 
0109     /* done under the callback_lock to serialize with write_space */
0110     spin_lock(&rds_tcp_tc_list_lock);
0111     list_del_init(&tc->t_list_item);
0112 #if IS_ENABLED(CONFIG_IPV6)
0113     rds6_tcp_tc_count--;
0114 #endif
0115     if (!tc->t_cpath->cp_conn->c_isv6)
0116         rds_tcp_tc_count--;
0117     spin_unlock(&rds_tcp_tc_list_lock);
0118 
0119     tc->t_sock = NULL;
0120 
0121     sock->sk->sk_write_space = tc->t_orig_write_space;
0122     sock->sk->sk_data_ready = tc->t_orig_data_ready;
0123     sock->sk->sk_state_change = tc->t_orig_state_change;
0124     sock->sk->sk_user_data = NULL;
0125 
0126     write_unlock_bh(&sock->sk->sk_callback_lock);
0127 }
0128 
0129 /*
0130  * rds_tcp_reset_callbacks() switches the to the new sock and
0131  * returns the existing tc->t_sock.
0132  *
0133  * The only functions that set tc->t_sock are rds_tcp_set_callbacks
0134  * and rds_tcp_reset_callbacks.  Send and receive trust that
0135  * it is set.  The absence of RDS_CONN_UP bit protects those paths
0136  * from being called while it isn't set.
0137  */
0138 void rds_tcp_reset_callbacks(struct socket *sock,
0139                  struct rds_conn_path *cp)
0140 {
0141     struct rds_tcp_connection *tc = cp->cp_transport_data;
0142     struct socket *osock = tc->t_sock;
0143 
0144     if (!osock)
0145         goto newsock;
0146 
0147     /* Need to resolve a duelling SYN between peers.
0148      * We have an outstanding SYN to this peer, which may
0149      * potentially have transitioned to the RDS_CONN_UP state,
0150      * so we must quiesce any send threads before resetting
0151      * cp_transport_data. We quiesce these threads by setting
0152      * cp_state to something other than RDS_CONN_UP, and then
0153      * waiting for any existing threads in rds_send_xmit to
0154      * complete release_in_xmit(). (Subsequent threads entering
0155      * rds_send_xmit() will bail on !rds_conn_up().
0156      *
0157      * However an incoming syn-ack at this point would end up
0158      * marking the conn as RDS_CONN_UP, and would again permit
0159      * rds_send_xmi() threads through, so ideally we would
0160      * synchronize on RDS_CONN_UP after lock_sock(), but cannot
0161      * do that: waiting on !RDS_IN_XMIT after lock_sock() may
0162      * end up deadlocking with tcp_sendmsg(), and the RDS_IN_XMIT
0163      * would not get set. As a result, we set c_state to
0164      * RDS_CONN_RESETTTING, to ensure that rds_tcp_state_change
0165      * cannot mark rds_conn_path_up() in the window before lock_sock()
0166      */
0167     atomic_set(&cp->cp_state, RDS_CONN_RESETTING);
0168     wait_event(cp->cp_waitq, !test_bit(RDS_IN_XMIT, &cp->cp_flags));
0169     lock_sock(osock->sk);
0170     /* reset receive side state for rds_tcp_data_recv() for osock  */
0171     cancel_delayed_work_sync(&cp->cp_send_w);
0172     cancel_delayed_work_sync(&cp->cp_recv_w);
0173     if (tc->t_tinc) {
0174         rds_inc_put(&tc->t_tinc->ti_inc);
0175         tc->t_tinc = NULL;
0176     }
0177     tc->t_tinc_hdr_rem = sizeof(struct rds_header);
0178     tc->t_tinc_data_rem = 0;
0179     rds_tcp_restore_callbacks(osock, tc);
0180     release_sock(osock->sk);
0181     sock_release(osock);
0182 newsock:
0183     rds_send_path_reset(cp);
0184     lock_sock(sock->sk);
0185     rds_tcp_set_callbacks(sock, cp);
0186     release_sock(sock->sk);
0187 }
0188 
0189 /* Add tc to rds_tcp_tc_list and set tc->t_sock. See comments
0190  * above rds_tcp_reset_callbacks for notes about synchronization
0191  * with data path
0192  */
0193 void rds_tcp_set_callbacks(struct socket *sock, struct rds_conn_path *cp)
0194 {
0195     struct rds_tcp_connection *tc = cp->cp_transport_data;
0196 
0197     rdsdebug("setting sock %p callbacks to tc %p\n", sock, tc);
0198     write_lock_bh(&sock->sk->sk_callback_lock);
0199 
0200     /* done under the callback_lock to serialize with write_space */
0201     spin_lock(&rds_tcp_tc_list_lock);
0202     list_add_tail(&tc->t_list_item, &rds_tcp_tc_list);
0203 #if IS_ENABLED(CONFIG_IPV6)
0204     rds6_tcp_tc_count++;
0205 #endif
0206     if (!tc->t_cpath->cp_conn->c_isv6)
0207         rds_tcp_tc_count++;
0208     spin_unlock(&rds_tcp_tc_list_lock);
0209 
0210     /* accepted sockets need our listen data ready undone */
0211     if (sock->sk->sk_data_ready == rds_tcp_listen_data_ready)
0212         sock->sk->sk_data_ready = sock->sk->sk_user_data;
0213 
0214     tc->t_sock = sock;
0215     tc->t_cpath = cp;
0216     tc->t_orig_data_ready = sock->sk->sk_data_ready;
0217     tc->t_orig_write_space = sock->sk->sk_write_space;
0218     tc->t_orig_state_change = sock->sk->sk_state_change;
0219 
0220     sock->sk->sk_user_data = cp;
0221     sock->sk->sk_data_ready = rds_tcp_data_ready;
0222     sock->sk->sk_write_space = rds_tcp_write_space;
0223     sock->sk->sk_state_change = rds_tcp_state_change;
0224 
0225     write_unlock_bh(&sock->sk->sk_callback_lock);
0226 }
0227 
0228 /* Handle RDS_INFO_TCP_SOCKETS socket option.  It only returns IPv4
0229  * connections for backward compatibility.
0230  */
0231 static void rds_tcp_tc_info(struct socket *rds_sock, unsigned int len,
0232                 struct rds_info_iterator *iter,
0233                 struct rds_info_lengths *lens)
0234 {
0235     struct rds_info_tcp_socket tsinfo;
0236     struct rds_tcp_connection *tc;
0237     unsigned long flags;
0238 
0239     spin_lock_irqsave(&rds_tcp_tc_list_lock, flags);
0240 
0241     if (len / sizeof(tsinfo) < rds_tcp_tc_count)
0242         goto out;
0243 
0244     list_for_each_entry(tc, &rds_tcp_tc_list, t_list_item) {
0245         struct inet_sock *inet = inet_sk(tc->t_sock->sk);
0246 
0247         if (tc->t_cpath->cp_conn->c_isv6)
0248             continue;
0249 
0250         tsinfo.local_addr = inet->inet_saddr;
0251         tsinfo.local_port = inet->inet_sport;
0252         tsinfo.peer_addr = inet->inet_daddr;
0253         tsinfo.peer_port = inet->inet_dport;
0254 
0255         tsinfo.hdr_rem = tc->t_tinc_hdr_rem;
0256         tsinfo.data_rem = tc->t_tinc_data_rem;
0257         tsinfo.last_sent_nxt = tc->t_last_sent_nxt;
0258         tsinfo.last_expected_una = tc->t_last_expected_una;
0259         tsinfo.last_seen_una = tc->t_last_seen_una;
0260         tsinfo.tos = tc->t_cpath->cp_conn->c_tos;
0261 
0262         rds_info_copy(iter, &tsinfo, sizeof(tsinfo));
0263     }
0264 
0265 out:
0266     lens->nr = rds_tcp_tc_count;
0267     lens->each = sizeof(tsinfo);
0268 
0269     spin_unlock_irqrestore(&rds_tcp_tc_list_lock, flags);
0270 }
0271 
0272 #if IS_ENABLED(CONFIG_IPV6)
0273 /* Handle RDS6_INFO_TCP_SOCKETS socket option. It returns both IPv4 and
0274  * IPv6 connections. IPv4 connection address is returned in an IPv4 mapped
0275  * address.
0276  */
0277 static void rds6_tcp_tc_info(struct socket *sock, unsigned int len,
0278                  struct rds_info_iterator *iter,
0279                  struct rds_info_lengths *lens)
0280 {
0281     struct rds6_info_tcp_socket tsinfo6;
0282     struct rds_tcp_connection *tc;
0283     unsigned long flags;
0284 
0285     spin_lock_irqsave(&rds_tcp_tc_list_lock, flags);
0286 
0287     if (len / sizeof(tsinfo6) < rds6_tcp_tc_count)
0288         goto out;
0289 
0290     list_for_each_entry(tc, &rds_tcp_tc_list, t_list_item) {
0291         struct sock *sk = tc->t_sock->sk;
0292         struct inet_sock *inet = inet_sk(sk);
0293 
0294         tsinfo6.local_addr = sk->sk_v6_rcv_saddr;
0295         tsinfo6.local_port = inet->inet_sport;
0296         tsinfo6.peer_addr = sk->sk_v6_daddr;
0297         tsinfo6.peer_port = inet->inet_dport;
0298 
0299         tsinfo6.hdr_rem = tc->t_tinc_hdr_rem;
0300         tsinfo6.data_rem = tc->t_tinc_data_rem;
0301         tsinfo6.last_sent_nxt = tc->t_last_sent_nxt;
0302         tsinfo6.last_expected_una = tc->t_last_expected_una;
0303         tsinfo6.last_seen_una = tc->t_last_seen_una;
0304 
0305         rds_info_copy(iter, &tsinfo6, sizeof(tsinfo6));
0306     }
0307 
0308 out:
0309     lens->nr = rds6_tcp_tc_count;
0310     lens->each = sizeof(tsinfo6);
0311 
0312     spin_unlock_irqrestore(&rds_tcp_tc_list_lock, flags);
0313 }
0314 #endif
0315 
0316 int rds_tcp_laddr_check(struct net *net, const struct in6_addr *addr,
0317             __u32 scope_id)
0318 {
0319     struct net_device *dev = NULL;
0320 #if IS_ENABLED(CONFIG_IPV6)
0321     int ret;
0322 #endif
0323 
0324     if (ipv6_addr_v4mapped(addr)) {
0325         if (inet_addr_type(net, addr->s6_addr32[3]) == RTN_LOCAL)
0326             return 0;
0327         return -EADDRNOTAVAIL;
0328     }
0329 
0330     /* If the scope_id is specified, check only those addresses
0331      * hosted on the specified interface.
0332      */
0333     if (scope_id != 0) {
0334         rcu_read_lock();
0335         dev = dev_get_by_index_rcu(net, scope_id);
0336         /* scope_id is not valid... */
0337         if (!dev) {
0338             rcu_read_unlock();
0339             return -EADDRNOTAVAIL;
0340         }
0341         rcu_read_unlock();
0342     }
0343 #if IS_ENABLED(CONFIG_IPV6)
0344     ret = ipv6_chk_addr(net, addr, dev, 0);
0345     if (ret)
0346         return 0;
0347 #endif
0348     return -EADDRNOTAVAIL;
0349 }
0350 
0351 static void rds_tcp_conn_free(void *arg)
0352 {
0353     struct rds_tcp_connection *tc = arg;
0354     unsigned long flags;
0355 
0356     rdsdebug("freeing tc %p\n", tc);
0357 
0358     spin_lock_irqsave(&rds_tcp_conn_lock, flags);
0359     if (!tc->t_tcp_node_detached)
0360         list_del(&tc->t_tcp_node);
0361     spin_unlock_irqrestore(&rds_tcp_conn_lock, flags);
0362 
0363     kmem_cache_free(rds_tcp_conn_slab, tc);
0364 }
0365 
0366 static int rds_tcp_conn_alloc(struct rds_connection *conn, gfp_t gfp)
0367 {
0368     struct rds_tcp_connection *tc;
0369     int i, j;
0370     int ret = 0;
0371 
0372     for (i = 0; i < RDS_MPATH_WORKERS; i++) {
0373         tc = kmem_cache_alloc(rds_tcp_conn_slab, gfp);
0374         if (!tc) {
0375             ret = -ENOMEM;
0376             goto fail;
0377         }
0378         mutex_init(&tc->t_conn_path_lock);
0379         tc->t_sock = NULL;
0380         tc->t_tinc = NULL;
0381         tc->t_tinc_hdr_rem = sizeof(struct rds_header);
0382         tc->t_tinc_data_rem = 0;
0383 
0384         conn->c_path[i].cp_transport_data = tc;
0385         tc->t_cpath = &conn->c_path[i];
0386         tc->t_tcp_node_detached = true;
0387 
0388         rdsdebug("rds_conn_path [%d] tc %p\n", i,
0389              conn->c_path[i].cp_transport_data);
0390     }
0391     spin_lock_irq(&rds_tcp_conn_lock);
0392     for (i = 0; i < RDS_MPATH_WORKERS; i++) {
0393         tc = conn->c_path[i].cp_transport_data;
0394         tc->t_tcp_node_detached = false;
0395         list_add_tail(&tc->t_tcp_node, &rds_tcp_conn_list);
0396     }
0397     spin_unlock_irq(&rds_tcp_conn_lock);
0398 fail:
0399     if (ret) {
0400         for (j = 0; j < i; j++)
0401             rds_tcp_conn_free(conn->c_path[j].cp_transport_data);
0402     }
0403     return ret;
0404 }
0405 
0406 static bool list_has_conn(struct list_head *list, struct rds_connection *conn)
0407 {
0408     struct rds_tcp_connection *tc, *_tc;
0409 
0410     list_for_each_entry_safe(tc, _tc, list, t_tcp_node) {
0411         if (tc->t_cpath->cp_conn == conn)
0412             return true;
0413     }
0414     return false;
0415 }
0416 
0417 static void rds_tcp_set_unloading(void)
0418 {
0419     atomic_set(&rds_tcp_unloading, 1);
0420 }
0421 
0422 static bool rds_tcp_is_unloading(struct rds_connection *conn)
0423 {
0424     return atomic_read(&rds_tcp_unloading) != 0;
0425 }
0426 
0427 static void rds_tcp_destroy_conns(void)
0428 {
0429     struct rds_tcp_connection *tc, *_tc;
0430     LIST_HEAD(tmp_list);
0431 
0432     /* avoid calling conn_destroy with irqs off */
0433     spin_lock_irq(&rds_tcp_conn_lock);
0434     list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) {
0435         if (!list_has_conn(&tmp_list, tc->t_cpath->cp_conn))
0436             list_move_tail(&tc->t_tcp_node, &tmp_list);
0437     }
0438     spin_unlock_irq(&rds_tcp_conn_lock);
0439 
0440     list_for_each_entry_safe(tc, _tc, &tmp_list, t_tcp_node)
0441         rds_conn_destroy(tc->t_cpath->cp_conn);
0442 }
0443 
0444 static void rds_tcp_exit(void);
0445 
0446 static u8 rds_tcp_get_tos_map(u8 tos)
0447 {
0448     /* all user tos mapped to default 0 for TCP transport */
0449     return 0;
0450 }
0451 
0452 struct rds_transport rds_tcp_transport = {
0453     .laddr_check        = rds_tcp_laddr_check,
0454     .xmit_path_prepare  = rds_tcp_xmit_path_prepare,
0455     .xmit_path_complete = rds_tcp_xmit_path_complete,
0456     .xmit           = rds_tcp_xmit,
0457     .recv_path      = rds_tcp_recv_path,
0458     .conn_alloc     = rds_tcp_conn_alloc,
0459     .conn_free      = rds_tcp_conn_free,
0460     .conn_path_connect  = rds_tcp_conn_path_connect,
0461     .conn_path_shutdown = rds_tcp_conn_path_shutdown,
0462     .inc_copy_to_user   = rds_tcp_inc_copy_to_user,
0463     .inc_free       = rds_tcp_inc_free,
0464     .stats_info_copy    = rds_tcp_stats_info_copy,
0465     .exit           = rds_tcp_exit,
0466     .get_tos_map        = rds_tcp_get_tos_map,
0467     .t_owner        = THIS_MODULE,
0468     .t_name         = "tcp",
0469     .t_type         = RDS_TRANS_TCP,
0470     .t_prefer_loopback  = 1,
0471     .t_mp_capable       = 1,
0472     .t_unloading        = rds_tcp_is_unloading,
0473 };
0474 
0475 static unsigned int rds_tcp_netid;
0476 
0477 /* per-network namespace private data for this module */
0478 struct rds_tcp_net {
0479     struct socket *rds_tcp_listen_sock;
0480     struct work_struct rds_tcp_accept_w;
0481     struct ctl_table_header *rds_tcp_sysctl;
0482     struct ctl_table *ctl_table;
0483     int sndbuf_size;
0484     int rcvbuf_size;
0485 };
0486 
0487 /* All module specific customizations to the RDS-TCP socket should be done in
0488  * rds_tcp_tune() and applied after socket creation.
0489  */
0490 bool rds_tcp_tune(struct socket *sock)
0491 {
0492     struct sock *sk = sock->sk;
0493     struct net *net = sock_net(sk);
0494     struct rds_tcp_net *rtn;
0495 
0496     tcp_sock_set_nodelay(sock->sk);
0497     lock_sock(sk);
0498     /* TCP timer functions might access net namespace even after
0499      * a process which created this net namespace terminated.
0500      */
0501     if (!sk->sk_net_refcnt) {
0502         if (!maybe_get_net(net)) {
0503             release_sock(sk);
0504             return false;
0505         }
0506         sk->sk_net_refcnt = 1;
0507         netns_tracker_alloc(net, &sk->ns_tracker, GFP_KERNEL);
0508         sock_inuse_add(net, 1);
0509     }
0510     rtn = net_generic(net, rds_tcp_netid);
0511     if (rtn->sndbuf_size > 0) {
0512         sk->sk_sndbuf = rtn->sndbuf_size;
0513         sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
0514     }
0515     if (rtn->rcvbuf_size > 0) {
0516         sk->sk_rcvbuf = rtn->rcvbuf_size;
0517         sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
0518     }
0519     release_sock(sk);
0520     return true;
0521 }
0522 
0523 static void rds_tcp_accept_worker(struct work_struct *work)
0524 {
0525     struct rds_tcp_net *rtn = container_of(work,
0526                            struct rds_tcp_net,
0527                            rds_tcp_accept_w);
0528 
0529     while (rds_tcp_accept_one(rtn->rds_tcp_listen_sock) == 0)
0530         cond_resched();
0531 }
0532 
0533 void rds_tcp_accept_work(struct sock *sk)
0534 {
0535     struct net *net = sock_net(sk);
0536     struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
0537 
0538     queue_work(rds_wq, &rtn->rds_tcp_accept_w);
0539 }
0540 
0541 static __net_init int rds_tcp_init_net(struct net *net)
0542 {
0543     struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
0544     struct ctl_table *tbl;
0545     int err = 0;
0546 
0547     memset(rtn, 0, sizeof(*rtn));
0548 
0549     /* {snd, rcv}buf_size default to 0, which implies we let the
0550      * stack pick the value, and permit auto-tuning of buffer size.
0551      */
0552     if (net == &init_net) {
0553         tbl = rds_tcp_sysctl_table;
0554     } else {
0555         tbl = kmemdup(rds_tcp_sysctl_table,
0556                   sizeof(rds_tcp_sysctl_table), GFP_KERNEL);
0557         if (!tbl) {
0558             pr_warn("could not set allocate sysctl table\n");
0559             return -ENOMEM;
0560         }
0561         rtn->ctl_table = tbl;
0562     }
0563     tbl[RDS_TCP_SNDBUF].data = &rtn->sndbuf_size;
0564     tbl[RDS_TCP_RCVBUF].data = &rtn->rcvbuf_size;
0565     rtn->rds_tcp_sysctl = register_net_sysctl(net, "net/rds/tcp", tbl);
0566     if (!rtn->rds_tcp_sysctl) {
0567         pr_warn("could not register sysctl\n");
0568         err = -ENOMEM;
0569         goto fail;
0570     }
0571 
0572 #if IS_ENABLED(CONFIG_IPV6)
0573     rtn->rds_tcp_listen_sock = rds_tcp_listen_init(net, true);
0574 #else
0575     rtn->rds_tcp_listen_sock = rds_tcp_listen_init(net, false);
0576 #endif
0577     if (!rtn->rds_tcp_listen_sock) {
0578         pr_warn("could not set up IPv6 listen sock\n");
0579 
0580 #if IS_ENABLED(CONFIG_IPV6)
0581         /* Try IPv4 as some systems disable IPv6 */
0582         rtn->rds_tcp_listen_sock = rds_tcp_listen_init(net, false);
0583         if (!rtn->rds_tcp_listen_sock) {
0584 #endif
0585             unregister_net_sysctl_table(rtn->rds_tcp_sysctl);
0586             rtn->rds_tcp_sysctl = NULL;
0587             err = -EAFNOSUPPORT;
0588             goto fail;
0589 #if IS_ENABLED(CONFIG_IPV6)
0590         }
0591 #endif
0592     }
0593     INIT_WORK(&rtn->rds_tcp_accept_w, rds_tcp_accept_worker);
0594     return 0;
0595 
0596 fail:
0597     if (net != &init_net)
0598         kfree(tbl);
0599     return err;
0600 }
0601 
0602 static void rds_tcp_kill_sock(struct net *net)
0603 {
0604     struct rds_tcp_connection *tc, *_tc;
0605     LIST_HEAD(tmp_list);
0606     struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
0607     struct socket *lsock = rtn->rds_tcp_listen_sock;
0608 
0609     rtn->rds_tcp_listen_sock = NULL;
0610     rds_tcp_listen_stop(lsock, &rtn->rds_tcp_accept_w);
0611     spin_lock_irq(&rds_tcp_conn_lock);
0612     list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) {
0613         struct net *c_net = read_pnet(&tc->t_cpath->cp_conn->c_net);
0614 
0615         if (net != c_net)
0616             continue;
0617         if (!list_has_conn(&tmp_list, tc->t_cpath->cp_conn)) {
0618             list_move_tail(&tc->t_tcp_node, &tmp_list);
0619         } else {
0620             list_del(&tc->t_tcp_node);
0621             tc->t_tcp_node_detached = true;
0622         }
0623     }
0624     spin_unlock_irq(&rds_tcp_conn_lock);
0625     list_for_each_entry_safe(tc, _tc, &tmp_list, t_tcp_node)
0626         rds_conn_destroy(tc->t_cpath->cp_conn);
0627 }
0628 
0629 static void __net_exit rds_tcp_exit_net(struct net *net)
0630 {
0631     struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
0632 
0633     rds_tcp_kill_sock(net);
0634 
0635     if (rtn->rds_tcp_sysctl)
0636         unregister_net_sysctl_table(rtn->rds_tcp_sysctl);
0637 
0638     if (net != &init_net)
0639         kfree(rtn->ctl_table);
0640 }
0641 
0642 static struct pernet_operations rds_tcp_net_ops = {
0643     .init = rds_tcp_init_net,
0644     .exit = rds_tcp_exit_net,
0645     .id = &rds_tcp_netid,
0646     .size = sizeof(struct rds_tcp_net),
0647 };
0648 
0649 void *rds_tcp_listen_sock_def_readable(struct net *net)
0650 {
0651     struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
0652     struct socket *lsock = rtn->rds_tcp_listen_sock;
0653 
0654     if (!lsock)
0655         return NULL;
0656 
0657     return lsock->sk->sk_user_data;
0658 }
0659 
0660 /* when sysctl is used to modify some kernel socket parameters,this
0661  * function  resets the RDS connections in that netns  so that we can
0662  * restart with new parameters.  The assumption is that such reset
0663  * events are few and far-between.
0664  */
0665 static void rds_tcp_sysctl_reset(struct net *net)
0666 {
0667     struct rds_tcp_connection *tc, *_tc;
0668 
0669     spin_lock_irq(&rds_tcp_conn_lock);
0670     list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) {
0671         struct net *c_net = read_pnet(&tc->t_cpath->cp_conn->c_net);
0672 
0673         if (net != c_net || !tc->t_sock)
0674             continue;
0675 
0676         /* reconnect with new parameters */
0677         rds_conn_path_drop(tc->t_cpath, false);
0678     }
0679     spin_unlock_irq(&rds_tcp_conn_lock);
0680 }
0681 
0682 static int rds_tcp_skbuf_handler(struct ctl_table *ctl, int write,
0683                  void *buffer, size_t *lenp, loff_t *fpos)
0684 {
0685     struct net *net = current->nsproxy->net_ns;
0686     int err;
0687 
0688     err = proc_dointvec_minmax(ctl, write, buffer, lenp, fpos);
0689     if (err < 0) {
0690         pr_warn("Invalid input. Must be >= %d\n",
0691             *(int *)(ctl->extra1));
0692         return err;
0693     }
0694     if (write)
0695         rds_tcp_sysctl_reset(net);
0696     return 0;
0697 }
0698 
0699 static void rds_tcp_exit(void)
0700 {
0701     rds_tcp_set_unloading();
0702     synchronize_rcu();
0703     rds_info_deregister_func(RDS_INFO_TCP_SOCKETS, rds_tcp_tc_info);
0704 #if IS_ENABLED(CONFIG_IPV6)
0705     rds_info_deregister_func(RDS6_INFO_TCP_SOCKETS, rds6_tcp_tc_info);
0706 #endif
0707     unregister_pernet_device(&rds_tcp_net_ops);
0708     rds_tcp_destroy_conns();
0709     rds_trans_unregister(&rds_tcp_transport);
0710     rds_tcp_recv_exit();
0711     kmem_cache_destroy(rds_tcp_conn_slab);
0712 }
0713 module_exit(rds_tcp_exit);
0714 
0715 static int rds_tcp_init(void)
0716 {
0717     int ret;
0718 
0719     rds_tcp_conn_slab = kmem_cache_create("rds_tcp_connection",
0720                           sizeof(struct rds_tcp_connection),
0721                           0, 0, NULL);
0722     if (!rds_tcp_conn_slab) {
0723         ret = -ENOMEM;
0724         goto out;
0725     }
0726 
0727     ret = rds_tcp_recv_init();
0728     if (ret)
0729         goto out_slab;
0730 
0731     ret = register_pernet_device(&rds_tcp_net_ops);
0732     if (ret)
0733         goto out_recv;
0734 
0735     rds_trans_register(&rds_tcp_transport);
0736 
0737     rds_info_register_func(RDS_INFO_TCP_SOCKETS, rds_tcp_tc_info);
0738 #if IS_ENABLED(CONFIG_IPV6)
0739     rds_info_register_func(RDS6_INFO_TCP_SOCKETS, rds6_tcp_tc_info);
0740 #endif
0741 
0742     goto out;
0743 out_recv:
0744     rds_tcp_recv_exit();
0745 out_slab:
0746     kmem_cache_destroy(rds_tcp_conn_slab);
0747 out:
0748     return ret;
0749 }
0750 module_init(rds_tcp_init);
0751 
0752 MODULE_AUTHOR("Oracle Corporation <rds-devel@oss.oracle.com>");
0753 MODULE_DESCRIPTION("RDS: TCP transport");
0754 MODULE_LICENSE("Dual BSD/GPL");