loop.c 5.5 KB

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