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_bind_addr - Bind an RDMA identifier to a source address and 173 * associated RDMA device, if needed. 174 * 175 * @id: RDMA identifier. 176 * @addr: Local address information. Wildcard values are permitted. 177 * 178 * This associates a source address with the RDMA identifier before calling 179 * rdma_listen. If a specific local address is given, the RDMA identifier will 180 * be bound to a local RDMA device. 181 */ 182 int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr); 183 184 /** 185 * rdma_resolve_addr - Resolve destination and optional source addresses 186 * from IP addresses to an RDMA address. If successful, the specified 187 * rdma_cm_id will be bound to a local device. 188 * 189 * @id: RDMA identifier. 190 * @src_addr: Source address information. This parameter may be NULL. 191 * @dst_addr: Destination address information. 192 * @timeout_ms: Time to wait for resolution to complete. 193 */ 194 int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr, 195 const struct sockaddr *dst_addr, 196 unsigned long timeout_ms); 197 198 /** 199 * rdma_resolve_route - Resolve the RDMA address bound to the RDMA identifier 200 * into route information needed to establish a connection. 201 * 202 * This is called on the client side of a connection. 203 * Users must have first called rdma_resolve_addr to resolve a dst_addr 204 * into an RDMA address before calling this routine. 205 */ 206 int rdma_resolve_route(struct rdma_cm_id *id, unsigned long timeout_ms); 207 208 /** 209 * rdma_resolve_ib_service - Resolve the IB service record of the 210 * service with the given service ID or name. 211 * 212 * This function is optional in the rdma cm flow. It is called on the client 213 * side of a connection, before calling rdma_resolve_route. The resolution 214 * can be done once per rdma_cm_id. 215 */ 216 int rdma_resolve_ib_service(struct rdma_cm_id *id, 217 struct rdma_ucm_ib_service *ibs); 218 219 /** 220 * rdma_create_qp - Allocate a QP and associate it with the specified RDMA 221 * identifier. 222 * 223 * QPs allocated to an rdma_cm_id will automatically be transitioned by the CMA 224 * through their states. 225 */ 226 int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd, 227 struct ib_qp_init_attr *qp_init_attr); 228 229 /** 230 * rdma_destroy_qp - Deallocate the QP associated with the specified RDMA 231 * identifier. 232 * 233 * Users must destroy any QP associated with an RDMA identifier before 234 * destroying the RDMA ID. 235 */ 236 void rdma_destroy_qp(struct rdma_cm_id *id); 237 238 /** 239 * rdma_init_qp_attr - Initializes the QP attributes for use in transitioning 240 * to a specified QP state. 241 * @id: Communication identifier associated with the QP attributes to 242 * initialize. 243 * @qp_attr: On input, specifies the desired QP state. On output, the 244 * mandatory and desired optional attributes will be set in order to 245 * modify the QP to the specified state. 246 * @qp_attr_mask: The QP attribute mask that may be used to transition the 247 * QP to the specified state. 248 * 249 * Users must set the @qp_attr->qp_state to the desired QP state. This call 250 * will set all required attributes for the given transition, along with 251 * known optional attributes. Users may override the attributes returned from 252 * this call before calling ib_modify_qp. 253 * 254 * Users that wish to have their QP automatically transitioned through its 255 * states can associate a QP with the rdma_cm_id by calling rdma_create_qp(). 256 */ 257 int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr, 258 int *qp_attr_mask); 259 260 int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param); 261 int rdma_connect_locked(struct rdma_cm_id *id, 262 struct rdma_conn_param *conn_param); 263 264 int rdma_connect_ece(struct rdma_cm_id *id, struct rdma_conn_param *conn_param, 265 struct rdma_ucm_ece *ece); 266 267 /** 268 * rdma_listen - This function is called by the passive side to 269 * listen for incoming connection requests. 270 * 271 * Users must have bound the rdma_cm_id to a local address by calling 272 * rdma_bind_addr before calling this routine. 273 */ 274 int rdma_listen(struct rdma_cm_id *id, int backlog); 275 276 int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param); 277 278 void rdma_lock_handler(struct rdma_cm_id *id); 279 void rdma_unlock_handler(struct rdma_cm_id *id); 280 int rdma_accept_ece(struct rdma_cm_id *id, struct rdma_conn_param *conn_param, 281 struct rdma_ucm_ece *ece); 282 283 /** 284 * rdma_notify - Notifies the RDMA CM of an asynchronous event that has 285 * occurred on the connection. 286 * @id: Connection identifier to transition to established. 287 * @event: Asynchronous event. 288 * 289 * This routine should be invoked by users to notify the CM of relevant 290 * communication events. Events that should be reported to the CM and 291 * when to report them are: 292 * 293 * IB_EVENT_COMM_EST - Used when a message is received on a connected 294 * QP before an RTU has been received. 295 */ 296 int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event); 297 298 /** 299 * rdma_reject - Called to reject a connection request or response. 300 */ 301 int rdma_reject(struct rdma_cm_id *id, const void *private_data, 302 u8 private_data_len, u8 reason); 303 304 /** 305 * rdma_disconnect - This function disconnects the associated QP and 306 * transitions it into the error state. 307 */ 308 int rdma_disconnect(struct rdma_cm_id *id); 309 310 /** 311 * rdma_join_multicast - Join the multicast group specified by the given 312 * address. 313 * @id: Communication identifier associated with the request. 314 * @addr: Multicast address identifying the group to join. 315 * @join_state: Multicast JoinState bitmap requested by port. 316 * Bitmap is based on IB_SA_MCMEMBER_REC_JOIN_STATE bits. 317 * @context: User-defined context associated with the join request, returned 318 * to the user through the private_data pointer in multicast events. 319 */ 320 int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr, 321 u8 join_state, void *context); 322 323 /** 324 * rdma_leave_multicast - Leave the multicast group specified by the given 325 * address. 326 */ 327 void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr); 328 329 /** 330 * rdma_set_service_type - Set the type of service associated with a 331 * connection identifier. 332 * @id: Communication identifier to associated with service type. 333 * @tos: Type of service. 334 * 335 * The type of service is interpretted as a differentiated service 336 * field (RFC 2474). The service type should be specified before 337 * performing route resolution, as existing communication on the 338 * connection identifier may be unaffected. The type of service 339 * requested may not be supported by the network to all destinations. 340 */ 341 void rdma_set_service_type(struct rdma_cm_id *id, int tos); 342 343 /** 344 * rdma_set_reuseaddr - Allow the reuse of local addresses when binding 345 * the rdma_cm_id. 346 * @id: Communication identifier to configure. 347 * @reuse: Value indicating if the bound address is reusable. 348 * 349 * Reuse must be set before an address is bound to the id. 350 */ 351 int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse); 352 353 /** 354 * rdma_set_afonly - Specify that listens are restricted to the 355 * bound address family only. 356 * @id: Communication identifer to configure. 357 * @afonly: Value indicating if listens are restricted. 358 * 359 * Must be set before identifier is in the listening state. 360 */ 361 int rdma_set_afonly(struct rdma_cm_id *id, int afonly); 362 363 int rdma_set_ack_timeout(struct rdma_cm_id *id, u8 timeout); 364 365 int rdma_set_min_rnr_timer(struct rdma_cm_id *id, u8 min_rnr_timer); 366 /** 367 * rdma_get_service_id - Return the IB service ID for a specified address. 368 * @id: Communication identifier associated with the address. 369 * @addr: Address for the service ID. 370 */ 371 __be64 rdma_get_service_id(struct rdma_cm_id *id, struct sockaddr *addr); 372 373 /** 374 * rdma_reject_msg - return a pointer to a reject message string. 375 * @id: Communication identifier that received the REJECT event. 376 * @reason: Value returned in the REJECT event status field. 377 */ 378 const char *__attribute_const__ rdma_reject_msg(struct rdma_cm_id *id, 379 int reason); 380 /** 381 * rdma_consumer_reject_data - return the consumer reject private data and 382 * length, if any. 383 * @id: Communication identifier that received the REJECT event. 384 * @ev: RDMA CM reject event. 385 * @data_len: Pointer to the resulting length of the consumer data. 386 */ 387 const void *rdma_consumer_reject_data(struct rdma_cm_id *id, 388 struct rdma_cm_event *ev, u8 *data_len); 389 390 /** 391 * rdma_read_gids - Return the SGID and DGID used for establishing 392 * connection. This can be used after rdma_resolve_addr() 393 * on client side. This can be use on new connection 394 * on server side. This is applicable to IB, RoCE, iWarp. 395 * If cm_id is not bound yet to the RDMA device, it doesn't 396 * copy and SGID or DGID to the given pointers. 397 * @id: Communication identifier whose GIDs are queried. 398 * @sgid: Pointer to SGID where SGID will be returned. It is optional. 399 * @dgid: Pointer to DGID where DGID will be returned. It is optional. 400 * Note: This API should not be used by any new ULPs or new code. 401 * Instead, users interested in querying GIDs should refer to path record 402 * of the rdma_cm_id to query the GIDs. 403 * This API is provided for compatibility for existing users. 404 */ 405 406 void rdma_read_gids(struct rdma_cm_id *cm_id, union ib_gid *sgid, 407 union ib_gid *dgid); 408 409 struct iw_cm_id *rdma_iw_cm_id(struct rdma_cm_id *cm_id); 410 411 #endif /* RDMA_CM_H */ 412