rdma_transport.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 "rdma_transport.h"
  36. static struct rdma_cm_id *rds_rdma_listen_id;
  37. static char *rds_cm_event_strings[] = {
  38. #define RDS_CM_EVENT_STRING(foo) \
  39. [RDMA_CM_EVENT_##foo] = __stringify(RDMA_CM_EVENT_##foo)
  40. RDS_CM_EVENT_STRING(ADDR_RESOLVED),
  41. RDS_CM_EVENT_STRING(ADDR_ERROR),
  42. RDS_CM_EVENT_STRING(ROUTE_RESOLVED),
  43. RDS_CM_EVENT_STRING(ROUTE_ERROR),
  44. RDS_CM_EVENT_STRING(CONNECT_REQUEST),
  45. RDS_CM_EVENT_STRING(CONNECT_RESPONSE),
  46. RDS_CM_EVENT_STRING(CONNECT_ERROR),
  47. RDS_CM_EVENT_STRING(UNREACHABLE),
  48. RDS_CM_EVENT_STRING(REJECTED),
  49. RDS_CM_EVENT_STRING(ESTABLISHED),
  50. RDS_CM_EVENT_STRING(DISCONNECTED),
  51. RDS_CM_EVENT_STRING(DEVICE_REMOVAL),
  52. RDS_CM_EVENT_STRING(MULTICAST_JOIN),
  53. RDS_CM_EVENT_STRING(MULTICAST_ERROR),
  54. RDS_CM_EVENT_STRING(ADDR_CHANGE),
  55. RDS_CM_EVENT_STRING(TIMEWAIT_EXIT),
  56. #undef RDS_CM_EVENT_STRING
  57. };
  58. static char *rds_cm_event_str(enum rdma_cm_event_type type)
  59. {
  60. return rds_str_array(rds_cm_event_strings,
  61. ARRAY_SIZE(rds_cm_event_strings), type);
  62. };
  63. int rds_rdma_cm_event_handler(struct rdma_cm_id *cm_id,
  64. struct rdma_cm_event *event)
  65. {
  66. /* this can be null in the listening path */
  67. struct rds_connection *conn = cm_id->context;
  68. struct rds_transport *trans;
  69. int ret = 0;
  70. rdsdebug("conn %p id %p handling event %u (%s)\n", conn, cm_id,
  71. event->event, rds_cm_event_str(event->event));
  72. if (cm_id->device->node_type == RDMA_NODE_RNIC)
  73. trans = &rds_iw_transport;
  74. else
  75. trans = &rds_ib_transport;
  76. /* Prevent shutdown from tearing down the connection
  77. * while we're executing. */
  78. if (conn) {
  79. mutex_lock(&conn->c_cm_lock);
  80. /* If the connection is being shut down, bail out
  81. * right away. We return 0 so cm_id doesn't get
  82. * destroyed prematurely */
  83. if (rds_conn_state(conn) == RDS_CONN_DISCONNECTING) {
  84. /* Reject incoming connections while we're tearing
  85. * down an existing one. */
  86. if (event->event == RDMA_CM_EVENT_CONNECT_REQUEST)
  87. ret = 1;
  88. goto out;
  89. }
  90. }
  91. switch (event->event) {
  92. case RDMA_CM_EVENT_CONNECT_REQUEST:
  93. ret = trans->cm_handle_connect(cm_id, event);
  94. break;
  95. case RDMA_CM_EVENT_ADDR_RESOLVED:
  96. /* XXX do we need to clean up if this fails? */
  97. ret = rdma_resolve_route(cm_id,
  98. RDS_RDMA_RESOLVE_TIMEOUT_MS);
  99. break;
  100. case RDMA_CM_EVENT_ROUTE_RESOLVED:
  101. /* XXX worry about racing with listen acceptance */
  102. ret = trans->cm_initiate_connect(cm_id);
  103. break;
  104. case RDMA_CM_EVENT_ESTABLISHED:
  105. trans->cm_connect_complete(conn, event);
  106. break;
  107. case RDMA_CM_EVENT_ADDR_ERROR:
  108. case RDMA_CM_EVENT_ROUTE_ERROR:
  109. case RDMA_CM_EVENT_CONNECT_ERROR:
  110. case RDMA_CM_EVENT_UNREACHABLE:
  111. case RDMA_CM_EVENT_REJECTED:
  112. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  113. case RDMA_CM_EVENT_ADDR_CHANGE:
  114. if (conn)
  115. rds_conn_drop(conn);
  116. break;
  117. case RDMA_CM_EVENT_DISCONNECTED:
  118. rdsdebug("DISCONNECT event - dropping connection "
  119. "%pI4->%pI4\n", &conn->c_laddr,
  120. &conn->c_faddr);
  121. rds_conn_drop(conn);
  122. break;
  123. default:
  124. /* things like device disconnect? */
  125. printk(KERN_ERR "RDS: unknown event %u (%s)!\n",
  126. event->event, rds_cm_event_str(event->event));
  127. break;
  128. }
  129. out:
  130. if (conn)
  131. mutex_unlock(&conn->c_cm_lock);
  132. rdsdebug("id %p event %u (%s) handling ret %d\n", cm_id, event->event,
  133. rds_cm_event_str(event->event), ret);
  134. return ret;
  135. }
  136. static int rds_rdma_listen_init(void)
  137. {
  138. struct sockaddr_in sin;
  139. struct rdma_cm_id *cm_id;
  140. int ret;
  141. cm_id = rdma_create_id(rds_rdma_cm_event_handler, NULL, RDMA_PS_TCP,
  142. IB_QPT_RC);
  143. if (IS_ERR(cm_id)) {
  144. ret = PTR_ERR(cm_id);
  145. printk(KERN_ERR "RDS/RDMA: failed to setup listener, "
  146. "rdma_create_id() returned %d\n", ret);
  147. return ret;
  148. }
  149. sin.sin_family = AF_INET,
  150. sin.sin_addr.s_addr = (__force u32)htonl(INADDR_ANY);
  151. sin.sin_port = (__force u16)htons(RDS_PORT);
  152. /*
  153. * XXX I bet this binds the cm_id to a device. If we want to support
  154. * fail-over we'll have to take this into consideration.
  155. */
  156. ret = rdma_bind_addr(cm_id, (struct sockaddr *)&sin);
  157. if (ret) {
  158. printk(KERN_ERR "RDS/RDMA: failed to setup listener, "
  159. "rdma_bind_addr() returned %d\n", ret);
  160. goto out;
  161. }
  162. ret = rdma_listen(cm_id, 128);
  163. if (ret) {
  164. printk(KERN_ERR "RDS/RDMA: failed to setup listener, "
  165. "rdma_listen() returned %d\n", ret);
  166. goto out;
  167. }
  168. rdsdebug("cm %p listening on port %u\n", cm_id, RDS_PORT);
  169. rds_rdma_listen_id = cm_id;
  170. cm_id = NULL;
  171. out:
  172. if (cm_id)
  173. rdma_destroy_id(cm_id);
  174. return ret;
  175. }
  176. static void rds_rdma_listen_stop(void)
  177. {
  178. if (rds_rdma_listen_id) {
  179. rdsdebug("cm %p\n", rds_rdma_listen_id);
  180. rdma_destroy_id(rds_rdma_listen_id);
  181. rds_rdma_listen_id = NULL;
  182. }
  183. }
  184. static int rds_rdma_init(void)
  185. {
  186. int ret;
  187. ret = rds_rdma_listen_init();
  188. if (ret)
  189. goto out;
  190. ret = rds_iw_init();
  191. if (ret)
  192. goto err_iw_init;
  193. ret = rds_ib_init();
  194. if (ret)
  195. goto err_ib_init;
  196. goto out;
  197. err_ib_init:
  198. rds_iw_exit();
  199. err_iw_init:
  200. rds_rdma_listen_stop();
  201. out:
  202. return ret;
  203. }
  204. module_init(rds_rdma_init);
  205. static void rds_rdma_exit(void)
  206. {
  207. /* stop listening first to ensure no new connections are attempted */
  208. rds_rdma_listen_stop();
  209. rds_ib_exit();
  210. rds_iw_exit();
  211. }
  212. module_exit(rds_rdma_exit);
  213. MODULE_AUTHOR("Oracle Corporation <rds-devel@oss.oracle.com>");
  214. MODULE_DESCRIPTION("RDS: IB/iWARP transport");
  215. MODULE_LICENSE("Dual BSD/GPL");