rdma_transport.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 <rdma/rdma_cm.h>
  34. #include "rdma_transport.h"
  35. static struct rdma_cm_id *rds_iw_listen_id;
  36. int rds_rdma_cm_event_handler(struct rdma_cm_id *cm_id,
  37. struct rdma_cm_event *event)
  38. {
  39. /* this can be null in the listening path */
  40. struct rds_connection *conn = cm_id->context;
  41. struct rds_transport *trans;
  42. int ret = 0;
  43. rdsdebug("conn %p id %p handling event %u\n", conn, cm_id,
  44. event->event);
  45. if (cm_id->device->node_type == RDMA_NODE_RNIC)
  46. trans = &rds_iw_transport;
  47. else
  48. trans = &rds_ib_transport;
  49. /* Prevent shutdown from tearing down the connection
  50. * while we're executing. */
  51. if (conn) {
  52. mutex_lock(&conn->c_cm_lock);
  53. /* If the connection is being shut down, bail out
  54. * right away. We return 0 so cm_id doesn't get
  55. * destroyed prematurely */
  56. if (rds_conn_state(conn) == RDS_CONN_DISCONNECTING) {
  57. /* Reject incoming connections while we're tearing
  58. * down an existing one. */
  59. if (event->event == RDMA_CM_EVENT_CONNECT_REQUEST)
  60. ret = 1;
  61. goto out;
  62. }
  63. }
  64. switch (event->event) {
  65. case RDMA_CM_EVENT_CONNECT_REQUEST:
  66. ret = trans->cm_handle_connect(cm_id, event);
  67. break;
  68. case RDMA_CM_EVENT_ADDR_RESOLVED:
  69. /* XXX do we need to clean up if this fails? */
  70. ret = rdma_resolve_route(cm_id,
  71. RDS_RDMA_RESOLVE_TIMEOUT_MS);
  72. break;
  73. case RDMA_CM_EVENT_ROUTE_RESOLVED:
  74. /* XXX worry about racing with listen acceptance */
  75. ret = trans->cm_initiate_connect(cm_id);
  76. break;
  77. case RDMA_CM_EVENT_ESTABLISHED:
  78. trans->cm_connect_complete(conn, event);
  79. break;
  80. case RDMA_CM_EVENT_ADDR_ERROR:
  81. case RDMA_CM_EVENT_ROUTE_ERROR:
  82. case RDMA_CM_EVENT_CONNECT_ERROR:
  83. case RDMA_CM_EVENT_UNREACHABLE:
  84. case RDMA_CM_EVENT_REJECTED:
  85. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  86. case RDMA_CM_EVENT_ADDR_CHANGE:
  87. if (conn)
  88. rds_conn_drop(conn);
  89. break;
  90. case RDMA_CM_EVENT_DISCONNECTED:
  91. printk(KERN_WARNING "RDS/IW: DISCONNECT event - dropping connection "
  92. "%pI4->%pI4\n", &conn->c_laddr,
  93. &conn->c_faddr);
  94. rds_conn_drop(conn);
  95. break;
  96. default:
  97. /* things like device disconnect? */
  98. printk(KERN_ERR "unknown event %u\n", event->event);
  99. BUG();
  100. break;
  101. }
  102. out:
  103. if (conn)
  104. mutex_unlock(&conn->c_cm_lock);
  105. rdsdebug("id %p event %u handling ret %d\n", cm_id, event->event, ret);
  106. return ret;
  107. }
  108. static int __init rds_rdma_listen_init(void)
  109. {
  110. struct sockaddr_in sin;
  111. struct rdma_cm_id *cm_id;
  112. int ret;
  113. cm_id = rdma_create_id(rds_rdma_cm_event_handler, NULL, RDMA_PS_TCP);
  114. if (IS_ERR(cm_id)) {
  115. ret = PTR_ERR(cm_id);
  116. printk(KERN_ERR "RDS/IW: failed to setup listener, "
  117. "rdma_create_id() returned %d\n", ret);
  118. goto out;
  119. }
  120. sin.sin_family = PF_INET,
  121. sin.sin_addr.s_addr = (__force u32)htonl(INADDR_ANY);
  122. sin.sin_port = (__force u16)htons(RDS_PORT);
  123. /*
  124. * XXX I bet this binds the cm_id to a device. If we want to support
  125. * fail-over we'll have to take this into consideration.
  126. */
  127. ret = rdma_bind_addr(cm_id, (struct sockaddr *)&sin);
  128. if (ret) {
  129. printk(KERN_ERR "RDS/IW: failed to setup listener, "
  130. "rdma_bind_addr() returned %d\n", ret);
  131. goto out;
  132. }
  133. ret = rdma_listen(cm_id, 128);
  134. if (ret) {
  135. printk(KERN_ERR "RDS/IW: failed to setup listener, "
  136. "rdma_listen() returned %d\n", ret);
  137. goto out;
  138. }
  139. rdsdebug("cm %p listening on port %u\n", cm_id, RDS_PORT);
  140. rds_iw_listen_id = cm_id;
  141. cm_id = NULL;
  142. out:
  143. if (cm_id)
  144. rdma_destroy_id(cm_id);
  145. return ret;
  146. }
  147. static void rds_rdma_listen_stop(void)
  148. {
  149. if (rds_iw_listen_id) {
  150. rdsdebug("cm %p\n", rds_iw_listen_id);
  151. rdma_destroy_id(rds_iw_listen_id);
  152. rds_iw_listen_id = NULL;
  153. }
  154. }
  155. int __init rds_rdma_init(void)
  156. {
  157. int ret;
  158. ret = rds_rdma_listen_init();
  159. if (ret)
  160. goto out;
  161. ret = rds_iw_init();
  162. if (ret)
  163. goto err_iw_init;
  164. ret = rds_ib_init();
  165. if (ret)
  166. goto err_ib_init;
  167. goto out;
  168. err_ib_init:
  169. rds_iw_exit();
  170. err_iw_init:
  171. rds_rdma_listen_stop();
  172. out:
  173. return ret;
  174. }
  175. void rds_rdma_exit(void)
  176. {
  177. /* stop listening first to ensure no new connections are attempted */
  178. rds_rdma_listen_stop();
  179. rds_ib_exit();
  180. rds_iw_exit();
  181. }