···11+/*22+ * Copyright (c) 2006 Oracle. All rights reserved.33+ *44+ * This software is available to you under a choice of one of two55+ * licenses. You may choose to be licensed under the terms of the GNU66+ * General Public License (GPL) Version 2, available from the file77+ * COPYING in the main directory of this source tree, or the88+ * OpenIB.org BSD license below:99+ *1010+ * Redistribution and use in source and binary forms, with or1111+ * without modification, are permitted provided that the following1212+ * conditions are met:1313+ *1414+ * - Redistributions of source code must retain the above1515+ * copyright notice, this list of conditions and the following1616+ * disclaimer.1717+ *1818+ * - Redistributions in binary form must reproduce the above1919+ * copyright notice, this list of conditions and the following2020+ * disclaimer in the documentation and/or other materials2121+ * provided with the distribution.2222+ *2323+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,2424+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF2525+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND2626+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS2727+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN2828+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN2929+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE3030+ * SOFTWARE.3131+ *3232+ */3333+#include <linux/kernel.h>3434+#include <linux/in.h>3535+3636+#include "rds.h"3737+#include "loop.h"3838+3939+static DEFINE_SPINLOCK(loop_conns_lock);4040+static LIST_HEAD(loop_conns);4141+4242+/*4343+ * This 'loopback' transport is a special case for flows that originate4444+ * and terminate on the same machine.4545+ *4646+ * Connection build-up notices if the destination address is thought of4747+ * as a local address by a transport. At that time it decides to use the4848+ * loopback transport instead of the bound transport of the sending socket.4949+ *5050+ * The loopback transport's sending path just hands the sent rds_message5151+ * straight to the receiving path via an embedded rds_incoming.5252+ */5353+5454+/*5555+ * Usually a message transits both the sender and receiver's conns as it5656+ * flows to the receiver. In the loopback case, though, the receive path5757+ * is handed the sending conn so the sense of the addresses is reversed.5858+ */5959+static int rds_loop_xmit(struct rds_connection *conn, struct rds_message *rm,6060+ unsigned int hdr_off, unsigned int sg,6161+ unsigned int off)6262+{6363+ BUG_ON(hdr_off || sg || off);6464+6565+ rds_inc_init(&rm->m_inc, conn, conn->c_laddr);6666+ rds_message_addref(rm); /* for the inc */6767+6868+ rds_recv_incoming(conn, conn->c_laddr, conn->c_faddr, &rm->m_inc,6969+ GFP_KERNEL, KM_USER0);7070+7171+ rds_send_drop_acked(conn, be64_to_cpu(rm->m_inc.i_hdr.h_sequence),7272+ NULL);7373+7474+ rds_inc_put(&rm->m_inc);7575+7676+ return sizeof(struct rds_header) + be32_to_cpu(rm->m_inc.i_hdr.h_len);7777+}7878+7979+static int rds_loop_xmit_cong_map(struct rds_connection *conn,8080+ struct rds_cong_map *map,8181+ unsigned long offset)8282+{8383+ unsigned long i;8484+8585+ BUG_ON(offset);8686+ BUG_ON(map != conn->c_lcong);8787+8888+ for (i = 0; i < RDS_CONG_MAP_PAGES; i++) {8989+ memcpy((void *)conn->c_fcong->m_page_addrs[i],9090+ (void *)map->m_page_addrs[i], PAGE_SIZE);9191+ }9292+9393+ rds_cong_map_updated(conn->c_fcong, ~(u64) 0);9494+9595+ return sizeof(struct rds_header) + RDS_CONG_MAP_BYTES;9696+}9797+9898+/* we need to at least give the thread something to succeed */9999+static int rds_loop_recv(struct rds_connection *conn)100100+{101101+ return 0;102102+}103103+104104+struct rds_loop_connection {105105+ struct list_head loop_node;106106+ struct rds_connection *conn;107107+};108108+109109+/*110110+ * Even the loopback transport needs to keep track of its connections,111111+ * so it can call rds_conn_destroy() on them on exit. N.B. there are112112+ * 1+ loopback addresses (127.*.*.*) so it's not a bug to have113113+ * multiple loopback conns allocated, although rather useless.114114+ */115115+static int rds_loop_conn_alloc(struct rds_connection *conn, gfp_t gfp)116116+{117117+ struct rds_loop_connection *lc;118118+ unsigned long flags;119119+120120+ lc = kzalloc(sizeof(struct rds_loop_connection), GFP_KERNEL);121121+ if (lc == NULL)122122+ return -ENOMEM;123123+124124+ INIT_LIST_HEAD(&lc->loop_node);125125+ lc->conn = conn;126126+ conn->c_transport_data = lc;127127+128128+ spin_lock_irqsave(&loop_conns_lock, flags);129129+ list_add_tail(&lc->loop_node, &loop_conns);130130+ spin_unlock_irqrestore(&loop_conns_lock, flags);131131+132132+ return 0;133133+}134134+135135+static void rds_loop_conn_free(void *arg)136136+{137137+ struct rds_loop_connection *lc = arg;138138+ rdsdebug("lc %p\n", lc);139139+ list_del(&lc->loop_node);140140+ kfree(lc);141141+}142142+143143+static int rds_loop_conn_connect(struct rds_connection *conn)144144+{145145+ rds_connect_complete(conn);146146+ return 0;147147+}148148+149149+static void rds_loop_conn_shutdown(struct rds_connection *conn)150150+{151151+}152152+153153+void rds_loop_exit(void)154154+{155155+ struct rds_loop_connection *lc, *_lc;156156+ LIST_HEAD(tmp_list);157157+158158+ /* avoid calling conn_destroy with irqs off */159159+ spin_lock_irq(&loop_conns_lock);160160+ list_splice(&loop_conns, &tmp_list);161161+ INIT_LIST_HEAD(&loop_conns);162162+ spin_unlock_irq(&loop_conns_lock);163163+164164+ list_for_each_entry_safe(lc, _lc, &tmp_list, loop_node) {165165+ WARN_ON(lc->conn->c_passive);166166+ rds_conn_destroy(lc->conn);167167+ }168168+}169169+170170+/*171171+ * This is missing .xmit_* because loop doesn't go through generic172172+ * rds_send_xmit() and doesn't call rds_recv_incoming(). .listen_stop and173173+ * .laddr_check are missing because transport.c doesn't iterate over174174+ * rds_loop_transport.175175+ */176176+struct rds_transport rds_loop_transport = {177177+ .xmit = rds_loop_xmit,178178+ .xmit_cong_map = rds_loop_xmit_cong_map,179179+ .recv = rds_loop_recv,180180+ .conn_alloc = rds_loop_conn_alloc,181181+ .conn_free = rds_loop_conn_free,182182+ .conn_connect = rds_loop_conn_connect,183183+ .conn_shutdown = rds_loop_conn_shutdown,184184+ .inc_copy_to_user = rds_message_inc_copy_to_user,185185+ .inc_purge = rds_message_inc_purge,186186+ .inc_free = rds_message_inc_free,187187+ .t_name = "loopback",188188+};