113796bf9SAndy Grover /* 213796bf9SAndy Grover * Copyright (c) 2006 Oracle. All rights reserved. 313796bf9SAndy Grover * 413796bf9SAndy Grover * This software is available to you under a choice of one of two 513796bf9SAndy Grover * licenses. You may choose to be licensed under the terms of the GNU 613796bf9SAndy Grover * General Public License (GPL) Version 2, available from the file 713796bf9SAndy Grover * COPYING in the main directory of this source tree, or the 813796bf9SAndy Grover * OpenIB.org BSD license below: 913796bf9SAndy Grover * 1013796bf9SAndy Grover * Redistribution and use in source and binary forms, with or 1113796bf9SAndy Grover * without modification, are permitted provided that the following 1213796bf9SAndy Grover * conditions are met: 1313796bf9SAndy Grover * 1413796bf9SAndy Grover * - Redistributions of source code must retain the above 1513796bf9SAndy Grover * copyright notice, this list of conditions and the following 1613796bf9SAndy Grover * disclaimer. 1713796bf9SAndy Grover * 1813796bf9SAndy Grover * - Redistributions in binary form must reproduce the above 1913796bf9SAndy Grover * copyright notice, this list of conditions and the following 2013796bf9SAndy Grover * disclaimer in the documentation and/or other materials 2113796bf9SAndy Grover * provided with the distribution. 2213796bf9SAndy Grover * 2313796bf9SAndy Grover * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 2413796bf9SAndy Grover * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 2513796bf9SAndy Grover * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 2613796bf9SAndy Grover * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 2713796bf9SAndy Grover * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 2813796bf9SAndy Grover * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 2913796bf9SAndy Grover * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 3013796bf9SAndy Grover * SOFTWARE. 3113796bf9SAndy Grover * 3213796bf9SAndy Grover */ 3313796bf9SAndy Grover #include <linux/kernel.h> 345a0e3ad6STejun Heo #include <linux/slab.h> 3513796bf9SAndy Grover #include <linux/in.h> 3613796bf9SAndy Grover 3713796bf9SAndy Grover #include "rds.h" 3813796bf9SAndy Grover #include "loop.h" 3913796bf9SAndy Grover 4013796bf9SAndy Grover static DEFINE_SPINLOCK(loop_conns_lock); 4113796bf9SAndy Grover static LIST_HEAD(loop_conns); 4213796bf9SAndy Grover 4313796bf9SAndy Grover /* 4413796bf9SAndy Grover * This 'loopback' transport is a special case for flows that originate 4513796bf9SAndy Grover * and terminate on the same machine. 4613796bf9SAndy Grover * 4713796bf9SAndy Grover * Connection build-up notices if the destination address is thought of 4813796bf9SAndy Grover * as a local address by a transport. At that time it decides to use the 4913796bf9SAndy Grover * loopback transport instead of the bound transport of the sending socket. 5013796bf9SAndy Grover * 5113796bf9SAndy Grover * The loopback transport's sending path just hands the sent rds_message 5213796bf9SAndy Grover * straight to the receiving path via an embedded rds_incoming. 5313796bf9SAndy Grover */ 5413796bf9SAndy Grover 5513796bf9SAndy Grover /* 5613796bf9SAndy Grover * Usually a message transits both the sender and receiver's conns as it 5713796bf9SAndy Grover * flows to the receiver. In the loopback case, though, the receive path 5813796bf9SAndy Grover * is handed the sending conn so the sense of the addresses is reversed. 5913796bf9SAndy Grover */ 6013796bf9SAndy Grover static int rds_loop_xmit(struct rds_connection *conn, struct rds_message *rm, 6113796bf9SAndy Grover unsigned int hdr_off, unsigned int sg, 6213796bf9SAndy Grover unsigned int off) 6313796bf9SAndy Grover { 6477dd550eSAndy Grover /* Do not send cong updates to loopback */ 6577dd550eSAndy Grover if (rm->m_inc.i_hdr.h_flags & RDS_FLAG_CONG_BITMAP) { 6677dd550eSAndy Grover rds_cong_map_updated(conn->c_fcong, ~(u64) 0); 6777dd550eSAndy Grover return sizeof(struct rds_header) + RDS_CONG_MAP_BYTES; 6877dd550eSAndy Grover } 6977dd550eSAndy Grover 7013796bf9SAndy Grover BUG_ON(hdr_off || sg || off); 7113796bf9SAndy Grover 7213796bf9SAndy Grover rds_inc_init(&rm->m_inc, conn, conn->c_laddr); 73d37c9359SAndy Grover /* For the embedded inc. Matching put is in loop_inc_free() */ 74d37c9359SAndy Grover rds_message_addref(rm); 7513796bf9SAndy Grover 7613796bf9SAndy Grover rds_recv_incoming(conn, conn->c_laddr, conn->c_faddr, &rm->m_inc, 7713796bf9SAndy Grover GFP_KERNEL, KM_USER0); 7813796bf9SAndy Grover 7913796bf9SAndy Grover rds_send_drop_acked(conn, be64_to_cpu(rm->m_inc.i_hdr.h_sequence), 8013796bf9SAndy Grover NULL); 8113796bf9SAndy Grover 8213796bf9SAndy Grover rds_inc_put(&rm->m_inc); 8313796bf9SAndy Grover 8413796bf9SAndy Grover return sizeof(struct rds_header) + be32_to_cpu(rm->m_inc.i_hdr.h_len); 8513796bf9SAndy Grover } 8613796bf9SAndy Grover 87d37c9359SAndy Grover /* 88d37c9359SAndy Grover * See rds_loop_xmit(). Since our inc is embedded in the rm, we 89d37c9359SAndy Grover * make sure the rm lives at least until the inc is done. 90d37c9359SAndy Grover */ 91d37c9359SAndy Grover static void rds_loop_inc_free(struct rds_incoming *inc) 92d37c9359SAndy Grover { 93d37c9359SAndy Grover struct rds_message *rm = container_of(inc, struct rds_message, m_inc); 94d37c9359SAndy Grover rds_message_put(rm); 95d37c9359SAndy Grover } 96d37c9359SAndy Grover 9713796bf9SAndy Grover /* we need to at least give the thread something to succeed */ 9813796bf9SAndy Grover static int rds_loop_recv(struct rds_connection *conn) 9913796bf9SAndy Grover { 10013796bf9SAndy Grover return 0; 10113796bf9SAndy Grover } 10213796bf9SAndy Grover 10313796bf9SAndy Grover struct rds_loop_connection { 10413796bf9SAndy Grover struct list_head loop_node; 10513796bf9SAndy Grover struct rds_connection *conn; 10613796bf9SAndy Grover }; 10713796bf9SAndy Grover 10813796bf9SAndy Grover /* 10913796bf9SAndy Grover * Even the loopback transport needs to keep track of its connections, 11013796bf9SAndy Grover * so it can call rds_conn_destroy() on them on exit. N.B. there are 11113796bf9SAndy Grover * 1+ loopback addresses (127.*.*.*) so it's not a bug to have 11213796bf9SAndy Grover * multiple loopback conns allocated, although rather useless. 11313796bf9SAndy Grover */ 11413796bf9SAndy Grover static int rds_loop_conn_alloc(struct rds_connection *conn, gfp_t gfp) 11513796bf9SAndy Grover { 11613796bf9SAndy Grover struct rds_loop_connection *lc; 11713796bf9SAndy Grover unsigned long flags; 11813796bf9SAndy Grover 11913796bf9SAndy Grover lc = kzalloc(sizeof(struct rds_loop_connection), GFP_KERNEL); 1208690bfa1SAndy Grover if (!lc) 12113796bf9SAndy Grover return -ENOMEM; 12213796bf9SAndy Grover 12313796bf9SAndy Grover INIT_LIST_HEAD(&lc->loop_node); 12413796bf9SAndy Grover lc->conn = conn; 12513796bf9SAndy Grover conn->c_transport_data = lc; 12613796bf9SAndy Grover 12713796bf9SAndy Grover spin_lock_irqsave(&loop_conns_lock, flags); 12813796bf9SAndy Grover list_add_tail(&lc->loop_node, &loop_conns); 12913796bf9SAndy Grover spin_unlock_irqrestore(&loop_conns_lock, flags); 13013796bf9SAndy Grover 13113796bf9SAndy Grover return 0; 13213796bf9SAndy Grover } 13313796bf9SAndy Grover 13413796bf9SAndy Grover static void rds_loop_conn_free(void *arg) 13513796bf9SAndy Grover { 13613796bf9SAndy Grover struct rds_loop_connection *lc = arg; 137*58c490baSPavel Emelyanov unsigned long flags; 138*58c490baSPavel Emelyanov 13913796bf9SAndy Grover rdsdebug("lc %p\n", lc); 140*58c490baSPavel Emelyanov spin_lock_irqsave(&loop_conns_lock, flags); 14113796bf9SAndy Grover list_del(&lc->loop_node); 142*58c490baSPavel Emelyanov spin_unlock_irqrestore(&loop_conns_lock, flags); 14313796bf9SAndy Grover kfree(lc); 14413796bf9SAndy Grover } 14513796bf9SAndy Grover 14613796bf9SAndy Grover static int rds_loop_conn_connect(struct rds_connection *conn) 14713796bf9SAndy Grover { 14813796bf9SAndy Grover rds_connect_complete(conn); 14913796bf9SAndy Grover return 0; 15013796bf9SAndy Grover } 15113796bf9SAndy Grover 15213796bf9SAndy Grover static void rds_loop_conn_shutdown(struct rds_connection *conn) 15313796bf9SAndy Grover { 15413796bf9SAndy Grover } 15513796bf9SAndy Grover 15613796bf9SAndy Grover void rds_loop_exit(void) 15713796bf9SAndy Grover { 15813796bf9SAndy Grover struct rds_loop_connection *lc, *_lc; 15913796bf9SAndy Grover LIST_HEAD(tmp_list); 16013796bf9SAndy Grover 16113796bf9SAndy Grover /* avoid calling conn_destroy with irqs off */ 16213796bf9SAndy Grover spin_lock_irq(&loop_conns_lock); 16313796bf9SAndy Grover list_splice(&loop_conns, &tmp_list); 16413796bf9SAndy Grover INIT_LIST_HEAD(&loop_conns); 16513796bf9SAndy Grover spin_unlock_irq(&loop_conns_lock); 16613796bf9SAndy Grover 16713796bf9SAndy Grover list_for_each_entry_safe(lc, _lc, &tmp_list, loop_node) { 16813796bf9SAndy Grover WARN_ON(lc->conn->c_passive); 16913796bf9SAndy Grover rds_conn_destroy(lc->conn); 17013796bf9SAndy Grover } 17113796bf9SAndy Grover } 17213796bf9SAndy Grover 17313796bf9SAndy Grover /* 17413796bf9SAndy Grover * This is missing .xmit_* because loop doesn't go through generic 17513796bf9SAndy Grover * rds_send_xmit() and doesn't call rds_recv_incoming(). .listen_stop and 17613796bf9SAndy Grover * .laddr_check are missing because transport.c doesn't iterate over 17713796bf9SAndy Grover * rds_loop_transport. 17813796bf9SAndy Grover */ 17913796bf9SAndy Grover struct rds_transport rds_loop_transport = { 18013796bf9SAndy Grover .xmit = rds_loop_xmit, 18113796bf9SAndy Grover .recv = rds_loop_recv, 18213796bf9SAndy Grover .conn_alloc = rds_loop_conn_alloc, 18313796bf9SAndy Grover .conn_free = rds_loop_conn_free, 18413796bf9SAndy Grover .conn_connect = rds_loop_conn_connect, 18513796bf9SAndy Grover .conn_shutdown = rds_loop_conn_shutdown, 18613796bf9SAndy Grover .inc_copy_to_user = rds_message_inc_copy_to_user, 187d37c9359SAndy Grover .inc_free = rds_loop_inc_free, 18813796bf9SAndy Grover .t_name = "loopback", 18913796bf9SAndy Grover }; 190