1 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */ 2 /* 3 * Copyright (c) 2005 Voltaire Inc. All rights reserved. 4 * Copyright (c) 2005 Intel Corporation. All rights reserved. 5 */ 6 7 #ifndef RDMA_CM_H 8 #define RDMA_CM_H 9 10 #include <linux/socket.h> 11 #include <linux/in6.h> 12 #include <rdma/ib_addr.h> 13 #include <rdma/ib_sa.h> 14 #include <uapi/rdma/rdma_user_cm.h> 15 16 /* 17 * Upon receiving a device removal event, users must destroy the associated 18 * RDMA identifier and release all resources allocated with the device. 19 */ 20 enum rdma_cm_event_type { 21 RDMA_CM_EVENT_ADDR_RESOLVED, 22 RDMA_CM_EVENT_ADDR_ERROR, 23 RDMA_CM_EVENT_ROUTE_RESOLVED, 24 RDMA_CM_EVENT_ROUTE_ERROR, 25 RDMA_CM_EVENT_CONNECT_REQUEST, 26 RDMA_CM_EVENT_CONNECT_RESPONSE, 27 RDMA_CM_EVENT_CONNECT_ERROR, 28 RDMA_CM_EVENT_UNREACHABLE, 29 RDMA_CM_EVENT_REJECTED, 30 RDMA_CM_EVENT_ESTABLISHED, 31 RDMA_CM_EVENT_DISCONNECTED, 32 RDMA_CM_EVENT_DEVICE_REMOVAL, 33 RDMA_CM_EVENT_MULTICAST_JOIN, 34 RDMA_CM_EVENT_MULTICAST_ERROR, 35 RDMA_CM_EVENT_ADDR_CHANGE, 36 RDMA_CM_EVENT_TIMEWAIT_EXIT, 37 RDMA_CM_EVENT_ADDRINFO_RESOLVED, 38 RDMA_CM_EVENT_ADDRINFO_ERROR, 39 RDMA_CM_EVENT_USER, 40 RDMA_CM_EVENT_INTERNAL, 41 }; 42 43 const char *__attribute_const__ rdma_event_msg(enum rdma_cm_event_type event); 44 45 #define RDMA_IB_IP_PS_MASK 0xFFFFFFFFFFFF0000ULL 46 #define RDMA_IB_IP_PS_TCP 0x0000000001060000ULL 47 #define RDMA_IB_IP_PS_UDP 0x0000000001110000ULL 48 #define RDMA_IB_IP_PS_IB 0x00000000013F0000ULL 49 50 struct rdma_addr { 51 struct sockaddr_storage src_addr; 52 struct sockaddr_storage dst_addr; 53 struct rdma_dev_addr dev_addr; 54 }; 55 56 struct rdma_route { 57 struct rdma_addr addr; 58 struct sa_path_rec *path_rec; 59 60 /* Optional path records of primary path */ 61 struct sa_path_rec *path_rec_inbound; 62 struct sa_path_rec *path_rec_outbound; 63 64 /* 65 * 0 - No primary nor alternate path is available 66 * 1 - Only primary path is available 67 * 2 - Both primary and alternate path are available 68 */ 69 int num_pri_alt_paths; 70 71 unsigned int num_service_recs; 72 struct sa_service_rec *service_recs; 73 }; 74 75 struct rdma_conn_param { 76 const void *private_data; 77 u8 private_data_len; 78 u8 responder_resources; 79 u8 initiator_depth; 80 u8 flow_control; 81 u8 retry_count; /* ignored when accepting */ 82 u8 rnr_retry_count; 83 /* Fields below ignored if a QP is created on the rdma_cm_id. */ 84 u8 srq; 85 u32 qp_num; 86 u32 qkey; 87 }; 88 89 struct rdma_ud_param { 90 const void *private_data; 91 u8 private_data_len; 92 struct rdma_ah_attr ah_attr; 93 u32 qp_num; 94 u32 qkey; 95 }; 96 97 struct rdma_cm_event { 98 enum rdma_cm_event_type event; 99 int status; 100 union { 101 struct rdma_conn_param conn; 102 struct rdma_ud_param ud; 103 u64 arg; 104 } param; 105 struct rdma_ucm_ece ece; 106 }; 107 108 struct rdma_cm_id; 109 110 /** 111 * rdma_cm_event_handler - Callback used to report user events. 112 * 113 * Notes: Users may not call rdma_destroy_id from this callback to destroy 114 * the passed in id, or a corresponding listen id. Returning a 115 * non-zero value from the callback will destroy the passed in id. 116 */ 117 typedef int (*rdma_cm_event_handler)(struct rdma_cm_id *id, 118 struct rdma_cm_event *event); 119 120 struct rdma_cm_id { 121 struct ib_device *device; 122 void *context; 123 struct ib_qp *qp; 124 rdma_cm_event_handler event_handler; 125 struct rdma_route route; 126 enum rdma_ucm_port_space ps; 127 enum ib_qp_type qp_type; 128 u32 port_num; 129 struct work_struct net_work; 130 }; 131 132 struct rdma_cm_id * 133 __rdma_create_kernel_id(struct net *net, rdma_cm_event_handler event_handler, 134 void *context, enum rdma_ucm_port_space ps, 135 enum ib_qp_type qp_type, const char *caller); 136 struct rdma_cm_id *rdma_create_user_id(rdma_cm_event_handler event_handler, 137 void *context, 138 enum rdma_ucm_port_space ps, 139 enum ib_qp_type qp_type); 140 141 /** 142 * rdma_create_id - Create an RDMA identifier. 143 * 144 * @net: The network namespace in which to create the new id. 145 * @event_handler: User callback invoked to report events associated with the 146 * returned rdma_id. 147 * @context: User specified context associated with the id. 148 * @ps: RDMA port space. 149 * @qp_type: type of queue pair associated with the id. 150 * 151 * Returns a new rdma_cm_id. The id holds a reference on the network 152 * namespace until it is destroyed. 153 * 154 * The event handler callback serializes on the id's mutex and is 155 * allowed to sleep. 156 */ 157 #define rdma_create_id(net, event_handler, context, ps, qp_type) \ 158 __rdma_create_kernel_id(net, event_handler, context, ps, qp_type, \ 159 KBUILD_MODNAME) 160 161 /** 162 * rdma_destroy_id - Destroys an RDMA identifier. 163 * 164 * @id: RDMA identifier. 165 * 166 * Note: calling this function has the effect of canceling in-flight 167 * asynchronous operations associated with the id. 168 */ 169 void rdma_destroy_id(struct rdma_cm_id *id); 170 171 /** 172 * rdma_restrict_node_type - Restrict an RDMA identifier to specific 173 * RDMA device node type. 174 * 175 * @id: RDMA identifier. 176 * @node_type: The device node type. Only RDMA_NODE_UNSPECIFIED (default), 177 * RDMA_NODE_RNIC and RDMA_NODE_IB_CA are allowed 178 * 179 * This allows the caller to restrict the possible devices 180 * used to iWarp (RDMA_NODE_RNIC) or InfiniBand/RoCEv1/RoCEv2 (RDMA_NODE_IB_CA). 181 * 182 * It needs to be called before the RDMA identifier is bound 183 * to an device, which mean it should be called before 184 * rdma_bind_addr(), rdma_bind_addr() and rdma_listen(). 185 */ 186 int rdma_restrict_node_type(struct rdma_cm_id *id, u8 node_type); 187 188 /** 189 * rdma_bind_addr - Bind an RDMA identifier to a source address and 190 * associated RDMA device, if needed. 191 * 192 * @id: RDMA identifier. 193 * @addr: Local address information. Wildcard values are permitted. 194 * 195 * This associates a source address with the RDMA identifier before calling 196 * rdma_listen. If a specific local address is given, the RDMA identifier will 197 * be bound to a local RDMA device. 198 */ 199 int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr); 200 201 /** 202 * rdma_resolve_addr - Resolve destination and optional source addresses 203 * from IP addresses to an RDMA address. If successful, the specified 204 * rdma_cm_id will be bound to a local device. 205 * 206 * @id: RDMA identifier. 207 * @src_addr: Source address information. This parameter may be NULL. 208 * @dst_addr: Destination address information. 209 * @timeout_ms: Time to wait for resolution to complete. 210 */ 211 int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr, 212 const struct sockaddr *dst_addr, 213 unsigned long timeout_ms); 214 215 /** 216 * rdma_resolve_route - Resolve the RDMA address bound to the RDMA identifier 217 * into route information needed to establish a connection. 218 * 219 * This is called on the client side of a connection. 220 * Users must have first called rdma_resolve_addr to resolve a dst_addr 221 * into an RDMA address before calling this routine. 222 */ 223 int rdma_resolve_route(struct rdma_cm_id *id, unsigned long timeout_ms); 224 225 /** 226 * rdma_resolve_ib_service - Resolve the IB service record of the 227 * service with the given service ID or name. 228 * 229 * This function is optional in the rdma cm flow. It is called on the client 230 * side of a connection, before calling rdma_resolve_route. The resolution 231 * can be done once per rdma_cm_id. 232 */ 233 int rdma_resolve_ib_service(struct rdma_cm_id *id, 234 struct rdma_ucm_ib_service *ibs); 235 236 /** 237 * rdma_create_qp - Allocate a QP and associate it with the specified RDMA 238 * identifier. 239 * 240 * QPs allocated to an rdma_cm_id will automatically be transitioned by the CMA 241 * through their states. 242 */ 243 int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd, 244 struct ib_qp_init_attr *qp_init_attr); 245 246 /** 247 * rdma_destroy_qp - Deallocate the QP associated with the specified RDMA 248 * identifier. 249 * 250 * Users must destroy any QP associated with an RDMA identifier before 251 * destroying the RDMA ID. 252 */ 253 void rdma_destroy_qp(struct rdma_cm_id *id); 254 255 /** 256 * rdma_init_qp_attr - Initializes the QP attributes for use in transitioning 257 * to a specified QP state. 258 * @id: Communication identifier associated with the QP attributes to 259 * initialize. 260 * @qp_attr: On input, specifies the desired QP state. On output, the 261 * mandatory and desired optional attributes will be set in order to 262 * modify the QP to the specified state. 263 * @qp_attr_mask: The QP attribute mask that may be used to transition the 264 * QP to the specified state. 265 * 266 * Users must set the @qp_attr->qp_state to the desired QP state. This call 267 * will set all required attributes for the given transition, along with 268 * known optional attributes. Users may override the attributes returned from 269 * this call before calling ib_modify_qp. 270 * 271 * Users that wish to have their QP automatically transitioned through its 272 * states can associate a QP with the rdma_cm_id by calling rdma_create_qp(). 273 */ 274 int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr, 275 int *qp_attr_mask); 276 277 int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param); 278 int rdma_connect_locked(struct rdma_cm_id *id, 279 struct rdma_conn_param *conn_param); 280 281 int rdma_connect_ece(struct rdma_cm_id *id, struct rdma_conn_param *conn_param, 282 struct rdma_ucm_ece *ece); 283 284 /** 285 * rdma_listen - This function is called by the passive side to 286 * listen for incoming connection requests. 287 * 288 * Users must have bound the rdma_cm_id to a local address by calling 289 * rdma_bind_addr before calling this routine. 290 */ 291 int rdma_listen(struct rdma_cm_id *id, int backlog); 292 293 int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param); 294 295 void rdma_lock_handler(struct rdma_cm_id *id); 296 void rdma_unlock_handler(struct rdma_cm_id *id); 297 int rdma_accept_ece(struct rdma_cm_id *id, struct rdma_conn_param *conn_param, 298 struct rdma_ucm_ece *ece); 299 300 /** 301 * rdma_notify - Notifies the RDMA CM of an asynchronous event that has 302 * occurred on the connection. 303 * @id: Connection identifier to transition to established. 304 * @event: Asynchronous event. 305 * 306 * This routine should be invoked by users to notify the CM of relevant 307 * communication events. Events that should be reported to the CM and 308 * when to report them are: 309 * 310 * IB_EVENT_COMM_EST - Used when a message is received on a connected 311 * QP before an RTU has been received. 312 */ 313 int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event); 314 315 /** 316 * rdma_reject - Called to reject a connection request or response. 317 */ 318 int rdma_reject(struct rdma_cm_id *id, const void *private_data, 319 u8 private_data_len, u8 reason); 320 321 /** 322 * rdma_disconnect - This function disconnects the associated QP and 323 * transitions it into the error state. 324 */ 325 int rdma_disconnect(struct rdma_cm_id *id); 326 327 /** 328 * rdma_join_multicast - Join the multicast group specified by the given 329 * address. 330 * @id: Communication identifier associated with the request. 331 * @addr: Multicast address identifying the group to join. 332 * @join_state: Multicast JoinState bitmap requested by port. 333 * Bitmap is based on IB_SA_MCMEMBER_REC_JOIN_STATE bits. 334 * @context: User-defined context associated with the join request, returned 335 * to the user through the private_data pointer in multicast events. 336 */ 337 int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr, 338 u8 join_state, void *context); 339 340 /** 341 * rdma_leave_multicast - Leave the multicast group specified by the given 342 * address. 343 */ 344 void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr); 345 346 /** 347 * rdma_set_service_type - Set the type of service associated with a 348 * connection identifier. 349 * @id: Communication identifier to associated with service type. 350 * @tos: Type of service. 351 * 352 * The type of service is interpretted as a differentiated service 353 * field (RFC 2474). The service type should be specified before 354 * performing route resolution, as existing communication on the 355 * connection identifier may be unaffected. The type of service 356 * requested may not be supported by the network to all destinations. 357 */ 358 void rdma_set_service_type(struct rdma_cm_id *id, int tos); 359 360 /** 361 * rdma_set_reuseaddr - Allow the reuse of local addresses when binding 362 * the rdma_cm_id. 363 * @id: Communication identifier to configure. 364 * @reuse: Value indicating if the bound address is reusable. 365 * 366 * Reuse must be set before an address is bound to the id. 367 */ 368 int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse); 369 370 /** 371 * rdma_set_afonly - Specify that listens are restricted to the 372 * bound address family only. 373 * @id: Communication identifer to configure. 374 * @afonly: Value indicating if listens are restricted. 375 * 376 * Must be set before identifier is in the listening state. 377 */ 378 int rdma_set_afonly(struct rdma_cm_id *id, int afonly); 379 380 int rdma_set_ack_timeout(struct rdma_cm_id *id, u8 timeout); 381 382 int rdma_set_min_rnr_timer(struct rdma_cm_id *id, u8 min_rnr_timer); 383 /** 384 * rdma_get_service_id - Return the IB service ID for a specified address. 385 * @id: Communication identifier associated with the address. 386 * @addr: Address for the service ID. 387 */ 388 __be64 rdma_get_service_id(struct rdma_cm_id *id, struct sockaddr *addr); 389 390 /** 391 * rdma_reject_msg - return a pointer to a reject message string. 392 * @id: Communication identifier that received the REJECT event. 393 * @reason: Value returned in the REJECT event status field. 394 */ 395 const char *__attribute_const__ rdma_reject_msg(struct rdma_cm_id *id, 396 int reason); 397 /** 398 * rdma_consumer_reject_data - return the consumer reject private data and 399 * length, if any. 400 * @id: Communication identifier that received the REJECT event. 401 * @ev: RDMA CM reject event. 402 * @data_len: Pointer to the resulting length of the consumer data. 403 */ 404 const void *rdma_consumer_reject_data(struct rdma_cm_id *id, 405 struct rdma_cm_event *ev, u8 *data_len); 406 407 /** 408 * rdma_read_gids - Return the SGID and DGID used for establishing 409 * connection. This can be used after rdma_resolve_addr() 410 * on client side. This can be use on new connection 411 * on server side. This is applicable to IB, RoCE, iWarp. 412 * If cm_id is not bound yet to the RDMA device, it doesn't 413 * copy and SGID or DGID to the given pointers. 414 * @id: Communication identifier whose GIDs are queried. 415 * @sgid: Pointer to SGID where SGID will be returned. It is optional. 416 * @dgid: Pointer to DGID where DGID will be returned. It is optional. 417 * Note: This API should not be used by any new ULPs or new code. 418 * Instead, users interested in querying GIDs should refer to path record 419 * of the rdma_cm_id to query the GIDs. 420 * This API is provided for compatibility for existing users. 421 */ 422 423 void rdma_read_gids(struct rdma_cm_id *cm_id, union ib_gid *sgid, 424 union ib_gid *dgid); 425 426 struct iw_cm_id *rdma_iw_cm_id(struct rdma_cm_id *cm_id); 427 428 #endif /* RDMA_CM_H */ 429