loop.c 5.8 KB

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