rdma_transport.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Copyright (c) 2009, 2018 Oracle and/or its affiliates. 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. /* Global IPv4 and IPv6 RDS RDMA listener cm_id */
  39. static struct rdma_cm_id *rds_rdma_listen_id;
  40. #if IS_ENABLED(CONFIG_IPV6)
  41. static struct rdma_cm_id *rds6_rdma_listen_id;
  42. #endif
  43. static int rds_rdma_cm_event_handler_cmn(struct rdma_cm_id *cm_id,
  44. struct rdma_cm_event *event,
  45. bool isv6)
  46. {
  47. /* this can be null in the listening path */
  48. struct rds_connection *conn = cm_id->context;
  49. struct rds_transport *trans;
  50. int ret = 0;
  51. rdsdebug("conn %p id %p handling event %u (%s)\n", conn, cm_id,
  52. event->event, rdma_event_msg(event->event));
  53. if (cm_id->device->node_type == RDMA_NODE_IB_CA)
  54. trans = &rds_ib_transport;
  55. /* Prevent shutdown from tearing down the connection
  56. * while we're executing. */
  57. if (conn) {
  58. mutex_lock(&conn->c_cm_lock);
  59. /* If the connection is being shut down, bail out
  60. * right away. We return 0 so cm_id doesn't get
  61. * destroyed prematurely */
  62. if (rds_conn_state(conn) == RDS_CONN_DISCONNECTING) {
  63. /* Reject incoming connections while we're tearing
  64. * down an existing one. */
  65. if (event->event == RDMA_CM_EVENT_CONNECT_REQUEST)
  66. ret = 1;
  67. goto out;
  68. }
  69. }
  70. switch (event->event) {
  71. case RDMA_CM_EVENT_CONNECT_REQUEST:
  72. ret = trans->cm_handle_connect(cm_id, event, isv6);
  73. break;
  74. case RDMA_CM_EVENT_ADDR_RESOLVED:
  75. /* XXX do we need to clean up if this fails? */
  76. ret = rdma_resolve_route(cm_id,
  77. RDS_RDMA_RESOLVE_TIMEOUT_MS);
  78. break;
  79. case RDMA_CM_EVENT_ROUTE_RESOLVED:
  80. /* Connection could have been dropped so make sure the
  81. * cm_id is valid before proceeding
  82. */
  83. if (conn) {
  84. struct rds_ib_connection *ibic;
  85. ibic = conn->c_transport_data;
  86. if (ibic && ibic->i_cm_id == cm_id)
  87. ret = trans->cm_initiate_connect(cm_id, isv6);
  88. else
  89. rds_conn_drop(conn);
  90. }
  91. break;
  92. case RDMA_CM_EVENT_ESTABLISHED:
  93. trans->cm_connect_complete(conn, event);
  94. break;
  95. case RDMA_CM_EVENT_REJECTED:
  96. rdsdebug("Connection rejected: %s\n",
  97. rdma_reject_msg(cm_id, event->status));
  98. /* FALLTHROUGH */
  99. case RDMA_CM_EVENT_ADDR_ERROR:
  100. case RDMA_CM_EVENT_ROUTE_ERROR:
  101. case RDMA_CM_EVENT_CONNECT_ERROR:
  102. case RDMA_CM_EVENT_UNREACHABLE:
  103. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  104. case RDMA_CM_EVENT_ADDR_CHANGE:
  105. if (conn)
  106. rds_conn_drop(conn);
  107. break;
  108. case RDMA_CM_EVENT_DISCONNECTED:
  109. rdsdebug("DISCONNECT event - dropping connection "
  110. "%pI6c->%pI6c\n", &conn->c_laddr,
  111. &conn->c_faddr);
  112. rds_conn_drop(conn);
  113. break;
  114. case RDMA_CM_EVENT_TIMEWAIT_EXIT:
  115. if (conn) {
  116. pr_info("RDS: RDMA_CM_EVENT_TIMEWAIT_EXIT event: dropping connection %pI6c->%pI6c\n",
  117. &conn->c_laddr, &conn->c_faddr);
  118. rds_conn_drop(conn);
  119. }
  120. break;
  121. default:
  122. /* things like device disconnect? */
  123. printk(KERN_ERR "RDS: unknown event %u (%s)!\n",
  124. event->event, rdma_event_msg(event->event));
  125. break;
  126. }
  127. out:
  128. if (conn)
  129. mutex_unlock(&conn->c_cm_lock);
  130. rdsdebug("id %p event %u (%s) handling ret %d\n", cm_id, event->event,
  131. rdma_event_msg(event->event), ret);
  132. return ret;
  133. }
  134. int rds_rdma_cm_event_handler(struct rdma_cm_id *cm_id,
  135. struct rdma_cm_event *event)
  136. {
  137. return rds_rdma_cm_event_handler_cmn(cm_id, event, false);
  138. }
  139. #if IS_ENABLED(CONFIG_IPV6)
  140. int rds6_rdma_cm_event_handler(struct rdma_cm_id *cm_id,
  141. struct rdma_cm_event *event)
  142. {
  143. return rds_rdma_cm_event_handler_cmn(cm_id, event, true);
  144. }
  145. #endif
  146. static int rds_rdma_listen_init_common(rdma_cm_event_handler handler,
  147. struct sockaddr *sa,
  148. struct rdma_cm_id **ret_cm_id)
  149. {
  150. struct rdma_cm_id *cm_id;
  151. int ret;
  152. cm_id = rdma_create_id(&init_net, handler, NULL,
  153. RDMA_PS_TCP, IB_QPT_RC);
  154. if (IS_ERR(cm_id)) {
  155. ret = PTR_ERR(cm_id);
  156. printk(KERN_ERR "RDS/RDMA: failed to setup listener, "
  157. "rdma_create_id() returned %d\n", ret);
  158. return ret;
  159. }
  160. /*
  161. * XXX I bet this binds the cm_id to a device. If we want to support
  162. * fail-over we'll have to take this into consideration.
  163. */
  164. ret = rdma_bind_addr(cm_id, sa);
  165. if (ret) {
  166. printk(KERN_ERR "RDS/RDMA: failed to setup listener, "
  167. "rdma_bind_addr() returned %d\n", ret);
  168. goto out;
  169. }
  170. ret = rdma_listen(cm_id, 128);
  171. if (ret) {
  172. printk(KERN_ERR "RDS/RDMA: failed to setup listener, "
  173. "rdma_listen() returned %d\n", ret);
  174. goto out;
  175. }
  176. rdsdebug("cm %p listening on port %u\n", cm_id, RDS_PORT);
  177. *ret_cm_id = cm_id;
  178. cm_id = NULL;
  179. out:
  180. if (cm_id)
  181. rdma_destroy_id(cm_id);
  182. return ret;
  183. }
  184. /* Initialize the RDS RDMA listeners. We create two listeners for
  185. * compatibility reason. The one on RDS_PORT is used for IPv4
  186. * requests only. The one on RDS_CM_PORT is used for IPv6 requests
  187. * only. So only IPv6 enabled RDS module will communicate using this
  188. * port.
  189. */
  190. static int rds_rdma_listen_init(void)
  191. {
  192. int ret;
  193. #if IS_ENABLED(CONFIG_IPV6)
  194. struct sockaddr_in6 sin6;
  195. #endif
  196. struct sockaddr_in sin;
  197. sin.sin_family = PF_INET;
  198. sin.sin_addr.s_addr = htonl(INADDR_ANY);
  199. sin.sin_port = htons(RDS_PORT);
  200. ret = rds_rdma_listen_init_common(rds_rdma_cm_event_handler,
  201. (struct sockaddr *)&sin,
  202. &rds_rdma_listen_id);
  203. if (ret != 0)
  204. return ret;
  205. #if IS_ENABLED(CONFIG_IPV6)
  206. sin6.sin6_family = PF_INET6;
  207. sin6.sin6_addr = in6addr_any;
  208. sin6.sin6_port = htons(RDS_CM_PORT);
  209. sin6.sin6_scope_id = 0;
  210. sin6.sin6_flowinfo = 0;
  211. ret = rds_rdma_listen_init_common(rds6_rdma_cm_event_handler,
  212. (struct sockaddr *)&sin6,
  213. &rds6_rdma_listen_id);
  214. /* Keep going even when IPv6 is not enabled in the system. */
  215. if (ret != 0)
  216. rdsdebug("Cannot set up IPv6 RDMA listener\n");
  217. #endif
  218. return 0;
  219. }
  220. static void rds_rdma_listen_stop(void)
  221. {
  222. if (rds_rdma_listen_id) {
  223. rdsdebug("cm %p\n", rds_rdma_listen_id);
  224. rdma_destroy_id(rds_rdma_listen_id);
  225. rds_rdma_listen_id = NULL;
  226. }
  227. #if IS_ENABLED(CONFIG_IPV6)
  228. if (rds6_rdma_listen_id) {
  229. rdsdebug("cm %p\n", rds6_rdma_listen_id);
  230. rdma_destroy_id(rds6_rdma_listen_id);
  231. rds6_rdma_listen_id = NULL;
  232. }
  233. #endif
  234. }
  235. static int rds_rdma_init(void)
  236. {
  237. int ret;
  238. ret = rds_ib_init();
  239. if (ret)
  240. goto out;
  241. ret = rds_rdma_listen_init();
  242. if (ret)
  243. rds_ib_exit();
  244. out:
  245. return ret;
  246. }
  247. module_init(rds_rdma_init);
  248. static void rds_rdma_exit(void)
  249. {
  250. /* stop listening first to ensure no new connections are attempted */
  251. rds_rdma_listen_stop();
  252. rds_ib_exit();
  253. }
  254. module_exit(rds_rdma_exit);
  255. MODULE_AUTHOR("Oracle Corporation <rds-devel@oss.oracle.com>");
  256. MODULE_DESCRIPTION("RDS: IB transport");
  257. MODULE_LICENSE("Dual BSD/GPL");