Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2006, 2017 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 <net/net_namespace.h>
0037 #include <net/netns/generic.h>
0038 #include <linux/ipv6.h>
0039 
0040 #include "rds_single_path.h"
0041 #include "rds.h"
0042 #include "loop.h"
0043 
0044 static DEFINE_SPINLOCK(loop_conns_lock);
0045 static LIST_HEAD(loop_conns);
0046 static atomic_t rds_loop_unloading = ATOMIC_INIT(0);
0047 
0048 static void rds_loop_set_unloading(void)
0049 {
0050     atomic_set(&rds_loop_unloading, 1);
0051 }
0052 
0053 static bool rds_loop_is_unloading(struct rds_connection *conn)
0054 {
0055     return atomic_read(&rds_loop_unloading) != 0;
0056 }
0057 
0058 /*
0059  * This 'loopback' transport is a special case for flows that originate
0060  * and terminate on the same machine.
0061  *
0062  * Connection build-up notices if the destination address is thought of
0063  * as a local address by a transport.  At that time it decides to use the
0064  * loopback transport instead of the bound transport of the sending socket.
0065  *
0066  * The loopback transport's sending path just hands the sent rds_message
0067  * straight to the receiving path via an embedded rds_incoming.
0068  */
0069 
0070 /*
0071  * Usually a message transits both the sender and receiver's conns as it
0072  * flows to the receiver.  In the loopback case, though, the receive path
0073  * is handed the sending conn so the sense of the addresses is reversed.
0074  */
0075 static int rds_loop_xmit(struct rds_connection *conn, struct rds_message *rm,
0076              unsigned int hdr_off, unsigned int sg,
0077              unsigned int off)
0078 {
0079     struct scatterlist *sgp = &rm->data.op_sg[sg];
0080     int ret = sizeof(struct rds_header) +
0081             be32_to_cpu(rm->m_inc.i_hdr.h_len);
0082 
0083     /* Do not send cong updates to loopback */
0084     if (rm->m_inc.i_hdr.h_flags & RDS_FLAG_CONG_BITMAP) {
0085         rds_cong_map_updated(conn->c_fcong, ~(u64) 0);
0086         ret = min_t(int, ret, sgp->length - conn->c_xmit_data_off);
0087         goto out;
0088     }
0089 
0090     BUG_ON(hdr_off || sg || off);
0091 
0092     rds_inc_init(&rm->m_inc, conn, &conn->c_laddr);
0093     /* For the embedded inc. Matching put is in loop_inc_free() */
0094     rds_message_addref(rm);
0095 
0096     rds_recv_incoming(conn, &conn->c_laddr, &conn->c_faddr, &rm->m_inc,
0097               GFP_KERNEL);
0098 
0099     rds_send_drop_acked(conn, be64_to_cpu(rm->m_inc.i_hdr.h_sequence),
0100                 NULL);
0101 
0102     rds_inc_put(&rm->m_inc);
0103 out:
0104     return ret;
0105 }
0106 
0107 /*
0108  * See rds_loop_xmit(). Since our inc is embedded in the rm, we
0109  * make sure the rm lives at least until the inc is done.
0110  */
0111 static void rds_loop_inc_free(struct rds_incoming *inc)
0112 {
0113     struct rds_message *rm = container_of(inc, struct rds_message, m_inc);
0114 
0115     rds_message_put(rm);
0116 }
0117 
0118 /* we need to at least give the thread something to succeed */
0119 static int rds_loop_recv_path(struct rds_conn_path *cp)
0120 {
0121     return 0;
0122 }
0123 
0124 struct rds_loop_connection {
0125     struct list_head loop_node;
0126     struct rds_connection *conn;
0127 };
0128 
0129 /*
0130  * Even the loopback transport needs to keep track of its connections,
0131  * so it can call rds_conn_destroy() on them on exit. N.B. there are
0132  * 1+ loopback addresses (127.*.*.*) so it's not a bug to have
0133  * multiple loopback conns allocated, although rather useless.
0134  */
0135 static int rds_loop_conn_alloc(struct rds_connection *conn, gfp_t gfp)
0136 {
0137     struct rds_loop_connection *lc;
0138     unsigned long flags;
0139 
0140     lc = kzalloc(sizeof(struct rds_loop_connection), gfp);
0141     if (!lc)
0142         return -ENOMEM;
0143 
0144     INIT_LIST_HEAD(&lc->loop_node);
0145     lc->conn = conn;
0146     conn->c_transport_data = lc;
0147 
0148     spin_lock_irqsave(&loop_conns_lock, flags);
0149     list_add_tail(&lc->loop_node, &loop_conns);
0150     spin_unlock_irqrestore(&loop_conns_lock, flags);
0151 
0152     return 0;
0153 }
0154 
0155 static void rds_loop_conn_free(void *arg)
0156 {
0157     struct rds_loop_connection *lc = arg;
0158     unsigned long flags;
0159 
0160     rdsdebug("lc %p\n", lc);
0161     spin_lock_irqsave(&loop_conns_lock, flags);
0162     list_del(&lc->loop_node);
0163     spin_unlock_irqrestore(&loop_conns_lock, flags);
0164     kfree(lc);
0165 }
0166 
0167 static int rds_loop_conn_path_connect(struct rds_conn_path *cp)
0168 {
0169     rds_connect_complete(cp->cp_conn);
0170     return 0;
0171 }
0172 
0173 static void rds_loop_conn_path_shutdown(struct rds_conn_path *cp)
0174 {
0175 }
0176 
0177 void rds_loop_exit(void)
0178 {
0179     struct rds_loop_connection *lc, *_lc;
0180     LIST_HEAD(tmp_list);
0181 
0182     rds_loop_set_unloading();
0183     synchronize_rcu();
0184     /* avoid calling conn_destroy with irqs off */
0185     spin_lock_irq(&loop_conns_lock);
0186     list_splice(&loop_conns, &tmp_list);
0187     INIT_LIST_HEAD(&loop_conns);
0188     spin_unlock_irq(&loop_conns_lock);
0189 
0190     list_for_each_entry_safe(lc, _lc, &tmp_list, loop_node) {
0191         WARN_ON(lc->conn->c_passive);
0192         rds_conn_destroy(lc->conn);
0193     }
0194 }
0195 
0196 static void rds_loop_kill_conns(struct net *net)
0197 {
0198     struct rds_loop_connection *lc, *_lc;
0199     LIST_HEAD(tmp_list);
0200 
0201     spin_lock_irq(&loop_conns_lock);
0202     list_for_each_entry_safe(lc, _lc, &loop_conns, loop_node)  {
0203         struct net *c_net = read_pnet(&lc->conn->c_net);
0204 
0205         if (net != c_net)
0206             continue;
0207         list_move_tail(&lc->loop_node, &tmp_list);
0208     }
0209     spin_unlock_irq(&loop_conns_lock);
0210 
0211     list_for_each_entry_safe(lc, _lc, &tmp_list, loop_node) {
0212         WARN_ON(lc->conn->c_passive);
0213         rds_conn_destroy(lc->conn);
0214     }
0215 }
0216 
0217 static void __net_exit rds_loop_exit_net(struct net *net)
0218 {
0219     rds_loop_kill_conns(net);
0220 }
0221 
0222 static struct pernet_operations rds_loop_net_ops = {
0223     .exit = rds_loop_exit_net,
0224 };
0225 
0226 int rds_loop_net_init(void)
0227 {
0228     return register_pernet_device(&rds_loop_net_ops);
0229 }
0230 
0231 void rds_loop_net_exit(void)
0232 {
0233     unregister_pernet_device(&rds_loop_net_ops);
0234 }
0235 
0236 /*
0237  * This is missing .xmit_* because loop doesn't go through generic
0238  * rds_send_xmit() and doesn't call rds_recv_incoming().  .listen_stop and
0239  * .laddr_check are missing because transport.c doesn't iterate over
0240  * rds_loop_transport.
0241  */
0242 struct rds_transport rds_loop_transport = {
0243     .xmit           = rds_loop_xmit,
0244     .recv_path      = rds_loop_recv_path,
0245     .conn_alloc     = rds_loop_conn_alloc,
0246     .conn_free      = rds_loop_conn_free,
0247     .conn_path_connect  = rds_loop_conn_path_connect,
0248     .conn_path_shutdown = rds_loop_conn_path_shutdown,
0249     .inc_copy_to_user   = rds_message_inc_copy_to_user,
0250     .inc_free       = rds_loop_inc_free,
0251     .t_name         = "loopback",
0252     .t_type         = RDS_TRANS_LOOP,
0253     .t_unloading        = rds_loop_is_unloading,
0254 };