/*1* Copyright (c) 2006 Oracle. All rights reserved.2*3* This software is available to you under a choice of one of two4* licenses. You may choose to be licensed under the terms of the GNU5* General Public License (GPL) Version 2, available from the file6* COPYING in the main directory of this source tree, or the7* OpenIB.org BSD license below:8*9* Redistribution and use in source and binary forms, with or10* without modification, are permitted provided that the following11* conditions are met:12*13* - Redistributions of source code must retain the above14* copyright notice, this list of conditions and the following15* disclaimer.16*17* - Redistributions in binary form must reproduce the above18* copyright notice, this list of conditions and the following19* disclaimer in the documentation and/or other materials20* provided with the distribution.21*22* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,23* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF24* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND25* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS26* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN27* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN28* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE29* SOFTWARE.30*31*/32#include <linux/kernel.h>33#include <linux/slab.h>34#include <linux/in.h>3536#include "rds.h"37#include "loop.h"3839static DEFINE_SPINLOCK(loop_conns_lock);40static LIST_HEAD(loop_conns);4142/*43* This 'loopback' transport is a special case for flows that originate44* and terminate on the same machine.45*46* Connection build-up notices if the destination address is thought of47* as a local address by a transport. At that time it decides to use the48* loopback transport instead of the bound transport of the sending socket.49*50* The loopback transport's sending path just hands the sent rds_message51* straight to the receiving path via an embedded rds_incoming.52*/5354/*55* Usually a message transits both the sender and receiver's conns as it56* flows to the receiver. In the loopback case, though, the receive path57* is handed the sending conn so the sense of the addresses is reversed.58*/59static int rds_loop_xmit(struct rds_connection *conn, struct rds_message *rm,60unsigned int hdr_off, unsigned int sg,61unsigned int off)62{63struct scatterlist *sgp = &rm->data.op_sg[sg];64int ret = sizeof(struct rds_header) +65be32_to_cpu(rm->m_inc.i_hdr.h_len);6667/* Do not send cong updates to loopback */68if (rm->m_inc.i_hdr.h_flags & RDS_FLAG_CONG_BITMAP) {69rds_cong_map_updated(conn->c_fcong, ~(u64) 0);70ret = min_t(int, ret, sgp->length - conn->c_xmit_data_off);71goto out;72}7374BUG_ON(hdr_off || sg || off);7576rds_inc_init(&rm->m_inc, conn, conn->c_laddr);77/* For the embedded inc. Matching put is in loop_inc_free() */78rds_message_addref(rm);7980rds_recv_incoming(conn, conn->c_laddr, conn->c_faddr, &rm->m_inc,81GFP_KERNEL, KM_USER0);8283rds_send_drop_acked(conn, be64_to_cpu(rm->m_inc.i_hdr.h_sequence),84NULL);8586rds_inc_put(&rm->m_inc);87out:88return ret;89}9091/*92* See rds_loop_xmit(). Since our inc is embedded in the rm, we93* make sure the rm lives at least until the inc is done.94*/95static void rds_loop_inc_free(struct rds_incoming *inc)96{97struct rds_message *rm = container_of(inc, struct rds_message, m_inc);98rds_message_put(rm);99}100101/* we need to at least give the thread something to succeed */102static int rds_loop_recv(struct rds_connection *conn)103{104return 0;105}106107struct rds_loop_connection {108struct list_head loop_node;109struct rds_connection *conn;110};111112/*113* Even the loopback transport needs to keep track of its connections,114* so it can call rds_conn_destroy() on them on exit. N.B. there are115* 1+ loopback addresses (127.*.*.*) so it's not a bug to have116* multiple loopback conns allocated, although rather useless.117*/118static int rds_loop_conn_alloc(struct rds_connection *conn, gfp_t gfp)119{120struct rds_loop_connection *lc;121unsigned long flags;122123lc = kzalloc(sizeof(struct rds_loop_connection), GFP_KERNEL);124if (!lc)125return -ENOMEM;126127INIT_LIST_HEAD(&lc->loop_node);128lc->conn = conn;129conn->c_transport_data = lc;130131spin_lock_irqsave(&loop_conns_lock, flags);132list_add_tail(&lc->loop_node, &loop_conns);133spin_unlock_irqrestore(&loop_conns_lock, flags);134135return 0;136}137138static void rds_loop_conn_free(void *arg)139{140struct rds_loop_connection *lc = arg;141unsigned long flags;142143rdsdebug("lc %p\n", lc);144spin_lock_irqsave(&loop_conns_lock, flags);145list_del(&lc->loop_node);146spin_unlock_irqrestore(&loop_conns_lock, flags);147kfree(lc);148}149150static int rds_loop_conn_connect(struct rds_connection *conn)151{152rds_connect_complete(conn);153return 0;154}155156static void rds_loop_conn_shutdown(struct rds_connection *conn)157{158}159160void rds_loop_exit(void)161{162struct rds_loop_connection *lc, *_lc;163LIST_HEAD(tmp_list);164165/* avoid calling conn_destroy with irqs off */166spin_lock_irq(&loop_conns_lock);167list_splice(&loop_conns, &tmp_list);168INIT_LIST_HEAD(&loop_conns);169spin_unlock_irq(&loop_conns_lock);170171list_for_each_entry_safe(lc, _lc, &tmp_list, loop_node) {172WARN_ON(lc->conn->c_passive);173rds_conn_destroy(lc->conn);174}175}176177/*178* This is missing .xmit_* because loop doesn't go through generic179* rds_send_xmit() and doesn't call rds_recv_incoming(). .listen_stop and180* .laddr_check are missing because transport.c doesn't iterate over181* rds_loop_transport.182*/183struct rds_transport rds_loop_transport = {184.xmit = rds_loop_xmit,185.recv = rds_loop_recv,186.conn_alloc = rds_loop_conn_alloc,187.conn_free = rds_loop_conn_free,188.conn_connect = rds_loop_conn_connect,189.conn_shutdown = rds_loop_conn_shutdown,190.inc_copy_to_user = rds_message_inc_copy_to_user,191.inc_free = rds_loop_inc_free,192.t_name = "loopback",193};194195196