Andy Grover | 13796bf | 2009-02-24 15:30:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2006 Oracle. All rights reserved. |
| 3 | * |
| 4 | * This software is available to you under a choice of one of two |
| 5 | * licenses. You may choose to be licensed under the terms of the GNU |
| 6 | * General Public License (GPL) Version 2, available from the file |
| 7 | * COPYING in the main directory of this source tree, or the |
| 8 | * OpenIB.org BSD license below: |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or |
| 11 | * without modification, are permitted provided that the following |
| 12 | * conditions are met: |
| 13 | * |
| 14 | * - Redistributions of source code must retain the above |
| 15 | * copyright notice, this list of conditions and the following |
| 16 | * disclaimer. |
| 17 | * |
| 18 | * - Redistributions in binary form must reproduce the above |
| 19 | * copyright notice, this list of conditions and the following |
| 20 | * disclaimer in the documentation and/or other materials |
| 21 | * provided with the distribution. |
| 22 | * |
| 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 24 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 25 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 26 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 27 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 28 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 29 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 30 | * SOFTWARE. |
| 31 | * |
| 32 | */ |
| 33 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 34 | #include <linux/slab.h> |
Andy Grover | 13796bf | 2009-02-24 15:30:24 +0000 | [diff] [blame] | 35 | #include <linux/in.h> |
| 36 | |
Sowmini Varadhan | 0cb4396 | 2016-06-13 09:44:26 -0700 | [diff] [blame] | 37 | #include "rds_single_path.h" |
Andy Grover | 13796bf | 2009-02-24 15:30:24 +0000 | [diff] [blame] | 38 | #include "rds.h" |
| 39 | #include "loop.h" |
| 40 | |
| 41 | static DEFINE_SPINLOCK(loop_conns_lock); |
| 42 | static LIST_HEAD(loop_conns); |
| 43 | |
| 44 | /* |
| 45 | * This 'loopback' transport is a special case for flows that originate |
| 46 | * and terminate on the same machine. |
| 47 | * |
| 48 | * Connection build-up notices if the destination address is thought of |
| 49 | * as a local address by a transport. At that time it decides to use the |
| 50 | * loopback transport instead of the bound transport of the sending socket. |
| 51 | * |
| 52 | * The loopback transport's sending path just hands the sent rds_message |
| 53 | * straight to the receiving path via an embedded rds_incoming. |
| 54 | */ |
| 55 | |
| 56 | /* |
| 57 | * Usually a message transits both the sender and receiver's conns as it |
| 58 | * flows to the receiver. In the loopback case, though, the receive path |
| 59 | * is handed the sending conn so the sense of the addresses is reversed. |
| 60 | */ |
| 61 | static int rds_loop_xmit(struct rds_connection *conn, struct rds_message *rm, |
| 62 | unsigned int hdr_off, unsigned int sg, |
| 63 | unsigned int off) |
| 64 | { |
Neil Horman | 6094628 | 2011-03-02 06:28:22 +0000 | [diff] [blame] | 65 | struct scatterlist *sgp = &rm->data.op_sg[sg]; |
| 66 | int ret = sizeof(struct rds_header) + |
| 67 | be32_to_cpu(rm->m_inc.i_hdr.h_len); |
| 68 | |
Andy Grover | 77dd550 | 2010-03-22 15:22:04 -0700 | [diff] [blame] | 69 | /* Do not send cong updates to loopback */ |
| 70 | if (rm->m_inc.i_hdr.h_flags & RDS_FLAG_CONG_BITMAP) { |
| 71 | rds_cong_map_updated(conn->c_fcong, ~(u64) 0); |
Neil Horman | 6094628 | 2011-03-02 06:28:22 +0000 | [diff] [blame] | 72 | ret = min_t(int, ret, sgp->length - conn->c_xmit_data_off); |
| 73 | goto out; |
Andy Grover | 77dd550 | 2010-03-22 15:22:04 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Andy Grover | 13796bf | 2009-02-24 15:30:24 +0000 | [diff] [blame] | 76 | BUG_ON(hdr_off || sg || off); |
| 77 | |
| 78 | rds_inc_init(&rm->m_inc, conn, conn->c_laddr); |
Andy Grover | d37c935 | 2010-01-19 18:14:56 -0800 | [diff] [blame] | 79 | /* For the embedded inc. Matching put is in loop_inc_free() */ |
| 80 | rds_message_addref(rm); |
Andy Grover | 13796bf | 2009-02-24 15:30:24 +0000 | [diff] [blame] | 81 | |
| 82 | rds_recv_incoming(conn, conn->c_laddr, conn->c_faddr, &rm->m_inc, |
Cong Wang | 6114eab | 2011-11-25 23:14:40 +0800 | [diff] [blame] | 83 | GFP_KERNEL); |
Andy Grover | 13796bf | 2009-02-24 15:30:24 +0000 | [diff] [blame] | 84 | |
| 85 | rds_send_drop_acked(conn, be64_to_cpu(rm->m_inc.i_hdr.h_sequence), |
| 86 | NULL); |
| 87 | |
| 88 | rds_inc_put(&rm->m_inc); |
Neil Horman | 6094628 | 2011-03-02 06:28:22 +0000 | [diff] [blame] | 89 | out: |
| 90 | return ret; |
Andy Grover | 13796bf | 2009-02-24 15:30:24 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Andy Grover | d37c935 | 2010-01-19 18:14:56 -0800 | [diff] [blame] | 93 | /* |
| 94 | * See rds_loop_xmit(). Since our inc is embedded in the rm, we |
| 95 | * make sure the rm lives at least until the inc is done. |
| 96 | */ |
| 97 | static void rds_loop_inc_free(struct rds_incoming *inc) |
| 98 | { |
Joshua Houghton | 5c3da57 | 2016-06-18 15:46:31 +0000 | [diff] [blame] | 99 | struct rds_message *rm = container_of(inc, struct rds_message, m_inc); |
| 100 | |
| 101 | rds_message_put(rm); |
Andy Grover | d37c935 | 2010-01-19 18:14:56 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Andy Grover | 13796bf | 2009-02-24 15:30:24 +0000 | [diff] [blame] | 104 | /* we need to at least give the thread something to succeed */ |
Sowmini Varadhan | 2da43c4 | 2016-06-30 16:11:15 -0700 | [diff] [blame] | 105 | static int rds_loop_recv_path(struct rds_conn_path *cp) |
Andy Grover | 13796bf | 2009-02-24 15:30:24 +0000 | [diff] [blame] | 106 | { |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | struct rds_loop_connection { |
| 111 | struct list_head loop_node; |
| 112 | struct rds_connection *conn; |
| 113 | }; |
| 114 | |
| 115 | /* |
| 116 | * Even the loopback transport needs to keep track of its connections, |
| 117 | * so it can call rds_conn_destroy() on them on exit. N.B. there are |
| 118 | * 1+ loopback addresses (127.*.*.*) so it's not a bug to have |
| 119 | * multiple loopback conns allocated, although rather useless. |
| 120 | */ |
| 121 | static int rds_loop_conn_alloc(struct rds_connection *conn, gfp_t gfp) |
| 122 | { |
| 123 | struct rds_loop_connection *lc; |
| 124 | unsigned long flags; |
| 125 | |
Dan Carpenter | f0229ea | 2012-03-21 20:44:09 +0000 | [diff] [blame] | 126 | lc = kzalloc(sizeof(struct rds_loop_connection), gfp); |
Andy Grover | 8690bfa | 2010-01-12 11:56:44 -0800 | [diff] [blame] | 127 | if (!lc) |
Andy Grover | 13796bf | 2009-02-24 15:30:24 +0000 | [diff] [blame] | 128 | return -ENOMEM; |
| 129 | |
| 130 | INIT_LIST_HEAD(&lc->loop_node); |
| 131 | lc->conn = conn; |
| 132 | conn->c_transport_data = lc; |
| 133 | |
| 134 | spin_lock_irqsave(&loop_conns_lock, flags); |
| 135 | list_add_tail(&lc->loop_node, &loop_conns); |
| 136 | spin_unlock_irqrestore(&loop_conns_lock, flags); |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | static void rds_loop_conn_free(void *arg) |
| 142 | { |
| 143 | struct rds_loop_connection *lc = arg; |
Pavel Emelyanov | 58c490b | 2010-11-02 01:52:05 +0000 | [diff] [blame] | 144 | unsigned long flags; |
| 145 | |
Andy Grover | 13796bf | 2009-02-24 15:30:24 +0000 | [diff] [blame] | 146 | rdsdebug("lc %p\n", lc); |
Pavel Emelyanov | 58c490b | 2010-11-02 01:52:05 +0000 | [diff] [blame] | 147 | spin_lock_irqsave(&loop_conns_lock, flags); |
Andy Grover | 13796bf | 2009-02-24 15:30:24 +0000 | [diff] [blame] | 148 | list_del(&lc->loop_node); |
Pavel Emelyanov | 58c490b | 2010-11-02 01:52:05 +0000 | [diff] [blame] | 149 | spin_unlock_irqrestore(&loop_conns_lock, flags); |
Andy Grover | 13796bf | 2009-02-24 15:30:24 +0000 | [diff] [blame] | 150 | kfree(lc); |
| 151 | } |
| 152 | |
Sowmini Varadhan | b04e855 | 2016-06-30 16:11:16 -0700 | [diff] [blame] | 153 | static int rds_loop_conn_path_connect(struct rds_conn_path *cp) |
Andy Grover | 13796bf | 2009-02-24 15:30:24 +0000 | [diff] [blame] | 154 | { |
Sowmini Varadhan | b04e855 | 2016-06-30 16:11:16 -0700 | [diff] [blame] | 155 | rds_connect_complete(cp->cp_conn); |
Andy Grover | 13796bf | 2009-02-24 15:30:24 +0000 | [diff] [blame] | 156 | return 0; |
| 157 | } |
| 158 | |
Sowmini Varadhan | 226f7a7 | 2016-06-30 16:11:10 -0700 | [diff] [blame] | 159 | static void rds_loop_conn_path_shutdown(struct rds_conn_path *cp) |
Andy Grover | 13796bf | 2009-02-24 15:30:24 +0000 | [diff] [blame] | 160 | { |
| 161 | } |
| 162 | |
| 163 | void rds_loop_exit(void) |
| 164 | { |
| 165 | struct rds_loop_connection *lc, *_lc; |
| 166 | LIST_HEAD(tmp_list); |
| 167 | |
| 168 | /* avoid calling conn_destroy with irqs off */ |
| 169 | spin_lock_irq(&loop_conns_lock); |
| 170 | list_splice(&loop_conns, &tmp_list); |
| 171 | INIT_LIST_HEAD(&loop_conns); |
| 172 | spin_unlock_irq(&loop_conns_lock); |
| 173 | |
| 174 | list_for_each_entry_safe(lc, _lc, &tmp_list, loop_node) { |
| 175 | WARN_ON(lc->conn->c_passive); |
| 176 | rds_conn_destroy(lc->conn); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * This is missing .xmit_* because loop doesn't go through generic |
| 182 | * rds_send_xmit() and doesn't call rds_recv_incoming(). .listen_stop and |
| 183 | * .laddr_check are missing because transport.c doesn't iterate over |
| 184 | * rds_loop_transport. |
| 185 | */ |
| 186 | struct rds_transport rds_loop_transport = { |
| 187 | .xmit = rds_loop_xmit, |
Sowmini Varadhan | 2da43c4 | 2016-06-30 16:11:15 -0700 | [diff] [blame] | 188 | .recv_path = rds_loop_recv_path, |
Andy Grover | 13796bf | 2009-02-24 15:30:24 +0000 | [diff] [blame] | 189 | .conn_alloc = rds_loop_conn_alloc, |
| 190 | .conn_free = rds_loop_conn_free, |
Sowmini Varadhan | b04e855 | 2016-06-30 16:11:16 -0700 | [diff] [blame] | 191 | .conn_path_connect = rds_loop_conn_path_connect, |
Sowmini Varadhan | 226f7a7 | 2016-06-30 16:11:10 -0700 | [diff] [blame] | 192 | .conn_path_shutdown = rds_loop_conn_path_shutdown, |
Andy Grover | 13796bf | 2009-02-24 15:30:24 +0000 | [diff] [blame] | 193 | .inc_copy_to_user = rds_message_inc_copy_to_user, |
Andy Grover | d37c935 | 2010-01-19 18:14:56 -0800 | [diff] [blame] | 194 | .inc_free = rds_loop_inc_free, |
Andy Grover | 13796bf | 2009-02-24 15:30:24 +0000 | [diff] [blame] | 195 | .t_name = "loopback", |
| 196 | }; |