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 | |
| 37 | #include "rds.h" |
| 38 | #include "loop.h" |
| 39 | |
| 40 | static DEFINE_SPINLOCK(loop_conns_lock); |
| 41 | static LIST_HEAD(loop_conns); |
| 42 | |
| 43 | /* |
| 44 | * This 'loopback' transport is a special case for flows that originate |
| 45 | * and terminate on the same machine. |
| 46 | * |
| 47 | * Connection build-up notices if the destination address is thought of |
| 48 | * as a local address by a transport. At that time it decides to use the |
| 49 | * loopback transport instead of the bound transport of the sending socket. |
| 50 | * |
| 51 | * The loopback transport's sending path just hands the sent rds_message |
| 52 | * straight to the receiving path via an embedded rds_incoming. |
| 53 | */ |
| 54 | |
| 55 | /* |
| 56 | * Usually a message transits both the sender and receiver's conns as it |
| 57 | * flows to the receiver. In the loopback case, though, the receive path |
| 58 | * is handed the sending conn so the sense of the addresses is reversed. |
| 59 | */ |
| 60 | static int rds_loop_xmit(struct rds_connection *conn, struct rds_message *rm, |
| 61 | unsigned int hdr_off, unsigned int sg, |
| 62 | unsigned int off) |
| 63 | { |
| 64 | BUG_ON(hdr_off || sg || off); |
| 65 | |
| 66 | rds_inc_init(&rm->m_inc, conn, conn->c_laddr); |
Andy Grover | d37c935 | 2010-01-19 18:14:56 -0800 | [diff] [blame^] | 67 | /* For the embedded inc. Matching put is in loop_inc_free() */ |
| 68 | rds_message_addref(rm); |
Andy Grover | 13796bf | 2009-02-24 15:30:24 +0000 | [diff] [blame] | 69 | |
| 70 | rds_recv_incoming(conn, conn->c_laddr, conn->c_faddr, &rm->m_inc, |
| 71 | GFP_KERNEL, KM_USER0); |
| 72 | |
| 73 | rds_send_drop_acked(conn, be64_to_cpu(rm->m_inc.i_hdr.h_sequence), |
| 74 | NULL); |
| 75 | |
| 76 | rds_inc_put(&rm->m_inc); |
| 77 | |
| 78 | return sizeof(struct rds_header) + be32_to_cpu(rm->m_inc.i_hdr.h_len); |
| 79 | } |
| 80 | |
Andy Grover | d37c935 | 2010-01-19 18:14:56 -0800 | [diff] [blame^] | 81 | /* |
| 82 | * See rds_loop_xmit(). Since our inc is embedded in the rm, we |
| 83 | * make sure the rm lives at least until the inc is done. |
| 84 | */ |
| 85 | static void rds_loop_inc_free(struct rds_incoming *inc) |
| 86 | { |
| 87 | struct rds_message *rm = container_of(inc, struct rds_message, m_inc); |
| 88 | rds_message_put(rm); |
| 89 | } |
| 90 | |
Andy Grover | 13796bf | 2009-02-24 15:30:24 +0000 | [diff] [blame] | 91 | static int rds_loop_xmit_cong_map(struct rds_connection *conn, |
| 92 | struct rds_cong_map *map, |
| 93 | unsigned long offset) |
| 94 | { |
Andy Grover | 13796bf | 2009-02-24 15:30:24 +0000 | [diff] [blame] | 95 | BUG_ON(offset); |
| 96 | BUG_ON(map != conn->c_lcong); |
| 97 | |
Andy Grover | 13796bf | 2009-02-24 15:30:24 +0000 | [diff] [blame] | 98 | rds_cong_map_updated(conn->c_fcong, ~(u64) 0); |
| 99 | |
| 100 | return sizeof(struct rds_header) + RDS_CONG_MAP_BYTES; |
| 101 | } |
| 102 | |
| 103 | /* we need to at least give the thread something to succeed */ |
| 104 | static int rds_loop_recv(struct rds_connection *conn) |
| 105 | { |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | struct rds_loop_connection { |
| 110 | struct list_head loop_node; |
| 111 | struct rds_connection *conn; |
| 112 | }; |
| 113 | |
| 114 | /* |
| 115 | * Even the loopback transport needs to keep track of its connections, |
| 116 | * so it can call rds_conn_destroy() on them on exit. N.B. there are |
| 117 | * 1+ loopback addresses (127.*.*.*) so it's not a bug to have |
| 118 | * multiple loopback conns allocated, although rather useless. |
| 119 | */ |
| 120 | static int rds_loop_conn_alloc(struct rds_connection *conn, gfp_t gfp) |
| 121 | { |
| 122 | struct rds_loop_connection *lc; |
| 123 | unsigned long flags; |
| 124 | |
| 125 | lc = kzalloc(sizeof(struct rds_loop_connection), GFP_KERNEL); |
Andy Grover | 8690bfa | 2010-01-12 11:56:44 -0800 | [diff] [blame] | 126 | if (!lc) |
Andy Grover | 13796bf | 2009-02-24 15:30:24 +0000 | [diff] [blame] | 127 | return -ENOMEM; |
| 128 | |
| 129 | INIT_LIST_HEAD(&lc->loop_node); |
| 130 | lc->conn = conn; |
| 131 | conn->c_transport_data = lc; |
| 132 | |
| 133 | spin_lock_irqsave(&loop_conns_lock, flags); |
| 134 | list_add_tail(&lc->loop_node, &loop_conns); |
| 135 | spin_unlock_irqrestore(&loop_conns_lock, flags); |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | static void rds_loop_conn_free(void *arg) |
| 141 | { |
| 142 | struct rds_loop_connection *lc = arg; |
| 143 | rdsdebug("lc %p\n", lc); |
| 144 | list_del(&lc->loop_node); |
| 145 | kfree(lc); |
| 146 | } |
| 147 | |
| 148 | static int rds_loop_conn_connect(struct rds_connection *conn) |
| 149 | { |
| 150 | rds_connect_complete(conn); |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | static void rds_loop_conn_shutdown(struct rds_connection *conn) |
| 155 | { |
| 156 | } |
| 157 | |
| 158 | void rds_loop_exit(void) |
| 159 | { |
| 160 | struct rds_loop_connection *lc, *_lc; |
| 161 | LIST_HEAD(tmp_list); |
| 162 | |
| 163 | /* avoid calling conn_destroy with irqs off */ |
| 164 | spin_lock_irq(&loop_conns_lock); |
| 165 | list_splice(&loop_conns, &tmp_list); |
| 166 | INIT_LIST_HEAD(&loop_conns); |
| 167 | spin_unlock_irq(&loop_conns_lock); |
| 168 | |
| 169 | list_for_each_entry_safe(lc, _lc, &tmp_list, loop_node) { |
| 170 | WARN_ON(lc->conn->c_passive); |
| 171 | rds_conn_destroy(lc->conn); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * This is missing .xmit_* because loop doesn't go through generic |
| 177 | * rds_send_xmit() and doesn't call rds_recv_incoming(). .listen_stop and |
| 178 | * .laddr_check are missing because transport.c doesn't iterate over |
| 179 | * rds_loop_transport. |
| 180 | */ |
| 181 | struct rds_transport rds_loop_transport = { |
| 182 | .xmit = rds_loop_xmit, |
| 183 | .xmit_cong_map = rds_loop_xmit_cong_map, |
| 184 | .recv = rds_loop_recv, |
| 185 | .conn_alloc = rds_loop_conn_alloc, |
| 186 | .conn_free = rds_loop_conn_free, |
| 187 | .conn_connect = rds_loop_conn_connect, |
| 188 | .conn_shutdown = rds_loop_conn_shutdown, |
| 189 | .inc_copy_to_user = rds_message_inc_copy_to_user, |
Andy Grover | d37c935 | 2010-01-19 18:14:56 -0800 | [diff] [blame^] | 190 | .inc_free = rds_loop_inc_free, |
Andy Grover | 13796bf | 2009-02-24 15:30:24 +0000 | [diff] [blame] | 191 | .t_name = "loopback", |
| 192 | }; |