rdma_transport.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright (c) 2009 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/module.h>
  34. #include <rdma/rdma_cm.h>
  35. #include "rds_single_path.h"
  36. #include "rdma_transport.h"
  37. #include "ib.h"
  38. static struct rdma_cm_id *rds_rdma_listen_id;
  39. int rds_rdma_cm_event_handler(struct rdma_cm_id *cm_id,
  40. struct rdma_cm_event *event)
  41. {
  42. /* this can be null in the listening path */
  43. struct rds_connection *conn = cm_id->context;
  44. struct rds_transport *trans;
  45. int ret = 0;
  46. rdsdebug("conn %p id %p handling event %u (%s)\n", conn, cm_id,
  47. event->event, rdma_event_msg(event->event));
  48. if (cm_id->device->node_type == RDMA_NODE_IB_CA)
  49. trans = &rds_ib_transport;
  50. /* Prevent shutdown from tearing down the connection
  51. * while we're executing. */
  52. if (conn) {
  53. mutex_lock(&conn->c_cm_lock);
  54. /* If the connection is being shut down, bail out
  55. * right away. We return 0 so cm_id doesn't get
  56. * destroyed prematurely */
  57. if (rds_conn_state(conn) == RDS_CONN_DISCONNECTING) {
  58. /* Reject incoming connections while we're tearing
  59. * down an existing one. */
  60. if (event->event == RDMA_CM_EVENT_CONNECT_REQUEST)
  61. ret = 1;
  62. goto out;
  63. }
  64. }
  65. switch (event->event) {
  66. case RDMA_CM_EVENT_CONNECT_REQUEST:
  67. ret = trans->cm_handle_connect(cm_id, event);
  68. break;
  69. case RDMA_CM_EVENT_ADDR_RESOLVED:
  70. /* XXX do we need to clean up if this fails? */
  71. ret = rdma_resolve_route(cm_id,
  72. RDS_RDMA_RESOLVE_TIMEOUT_MS);
  73. break;
  74. case RDMA_CM_EVENT_ROUTE_RESOLVED:
  75. /* Connection could have been dropped so make sure the
  76. * cm_id is valid before proceeding
  77. */
  78. if (conn) {
  79. struct rds_ib_connection *ibic;
  80. ibic = conn->c_transport_data;
  81. if (ibic && ibic->i_cm_id == cm_id)
  82. ret = trans->cm_initiate_connect(cm_id);
  83. else
  84. rds_conn_drop(conn);
  85. }
  86. break;
  87. case RDMA_CM_EVENT_ESTABLISHED:
  88. trans->cm_connect_complete(conn, event);
  89. break;
  90. case RDMA_CM_EVENT_REJECTED:
  91. rdsdebug("Connection rejected: %s\n",
  92. rdma_reject_msg(cm_id, event->status));
  93. /* FALLTHROUGH */
  94. case RDMA_CM_EVENT_ADDR_ERROR:
  95. case RDMA_CM_EVENT_ROUTE_ERROR:
  96. case RDMA_CM_EVENT_CONNECT_ERROR:
  97. case RDMA_CM_EVENT_UNREACHABLE:
  98. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  99. case RDMA_CM_EVENT_ADDR_CHANGE:
  100. if (conn)
  101. rds_conn_drop(conn);
  102. break;
  103. case RDMA_CM_EVENT_DISCONNECTED:
  104. rdsdebug("DISCONNECT event - dropping connection "
  105. "%pI4->%pI4\n", &conn->c_laddr,
  106. &conn->c_faddr);
  107. rds_conn_drop(conn);
  108. break;
  109. case RDMA_CM_EVENT_TIMEWAIT_EXIT:
  110. if (conn) {
  111. pr_info("RDS: RDMA_CM_EVENT_TIMEWAIT_EXIT event: dropping connection %pI4->%pI4\n",
  112. &conn->c_laddr, &conn->c_faddr);
  113. rds_conn_drop(conn);
  114. }
  115. break;
  116. default:
  117. /* things like device disconnect? */
  118. printk(KERN_ERR "RDS: unknown event %u (%s)!\n",
  119. event->event, rdma_event_msg(event->event));
  120. break;
  121. }
  122. out:
  123. if (conn)
  124. mutex_unlock(&conn->c_cm_lock);
  125. rdsdebug("id %p event %u (%s) handling ret %d\n", cm_id, event->event,
  126. rdma_event_msg(event->event), ret);
  127. return ret;
  128. }
  129. static int rds_rdma_listen_init(void)
  130. {
  131. struct sockaddr_in sin;
  132. struct rdma_cm_id *cm_id;
  133. int ret;
  134. cm_id = rdma_create_id(&init_net, rds_rdma_cm_event_handler, NULL,
  135. RDMA_PS_TCP, IB_QPT_RC);
  136. if (IS_ERR(cm_id)) {
  137. ret = PTR_ERR(cm_id);
  138. printk(KERN_ERR "RDS/RDMA: failed to setup listener, "
  139. "rdma_create_id() returned %d\n", ret);
  140. return ret;
  141. }
  142. sin.sin_family = AF_INET;
  143. sin.sin_addr.s_addr = (__force u32)htonl(INADDR_ANY);
  144. sin.sin_port = (__force u16)htons(RDS_PORT);
  145. /*
  146. * XXX I bet this binds the cm_id to a device. If we want to support
  147. * fail-over we'll have to take this into consideration.
  148. */
  149. ret = rdma_bind_addr(cm_id, (struct sockaddr *)&sin);
  150. if (ret) {
  151. printk(KERN_ERR "RDS/RDMA: failed to setup listener, "
  152. "rdma_bind_addr() returned %d\n", ret);
  153. goto out;
  154. }
  155. ret = rdma_listen(cm_id, 128);
  156. if (ret) {
  157. printk(KERN_ERR "RDS/RDMA: failed to setup listener, "
  158. "rdma_listen() returned %d\n", ret);
  159. goto out;
  160. }
  161. rdsdebug("cm %p listening on port %u\n", cm_id, RDS_PORT);
  162. rds_rdma_listen_id = cm_id;
  163. cm_id = NULL;
  164. out:
  165. if (cm_id)
  166. rdma_destroy_id(cm_id);
  167. return ret;
  168. }
  169. static void rds_rdma_listen_stop(void)
  170. {
  171. if (rds_rdma_listen_id) {
  172. rdsdebug("cm %p\n", rds_rdma_listen_id);
  173. rdma_destroy_id(rds_rdma_listen_id);
  174. rds_rdma_listen_id = NULL;
  175. }
  176. }
  177. static int rds_rdma_init(void)
  178. {
  179. int ret;
  180. ret = rds_ib_init();
  181. if (ret)
  182. goto out;
  183. ret = rds_rdma_listen_init();
  184. if (ret)
  185. rds_ib_exit();
  186. out:
  187. return ret;
  188. }
  189. module_init(rds_rdma_init);
  190. static void rds_rdma_exit(void)
  191. {
  192. /* stop listening first to ensure no new connections are attempted */
  193. rds_rdma_listen_stop();
  194. rds_ib_exit();
  195. }
  196. module_exit(rds_rdma_exit);
  197. MODULE_AUTHOR("Oracle Corporation <rds-devel@oss.oracle.com>");
  198. MODULE_DESCRIPTION("RDS: IB transport");
  199. MODULE_LICENSE("Dual BSD/GPL");