1 /*
2  * Copyright (c) 2005 Voltaire Inc.  All rights reserved.
3  * Copyright (c) 2005 Intel Corporation.  All rights reserved.
4  * Copyright (c) 2016 Chelsio Communications.  All rights reserved.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenIB.org BSD license below:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
20  *      - Redistributions in binary form must reproduce the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer in the documentation and/or other materials
23  *        provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  */
34 
35 #if !defined(RDMA_CM_H)
36 #define RDMA_CM_H
37 
38 #include <linux/socket.h>
39 #include <linux/in6.h>
40 #include <rdma/ib_addr.h>
41 #include <rdma/ib_sa.h>
42 
43 /*
44  * Upon receiving a device removal event, users must destroy the associated
45  * RDMA identifier and release all resources allocated with the device.
46  */
47 enum rdma_cm_event_type {
48 	RDMA_CM_EVENT_ADDR_RESOLVED,
49 	RDMA_CM_EVENT_ADDR_ERROR,
50 	RDMA_CM_EVENT_ROUTE_RESOLVED,
51 	RDMA_CM_EVENT_ROUTE_ERROR,
52 	RDMA_CM_EVENT_CONNECT_REQUEST,
53 	RDMA_CM_EVENT_CONNECT_RESPONSE,
54 	RDMA_CM_EVENT_CONNECT_ERROR,
55 	RDMA_CM_EVENT_UNREACHABLE,
56 	RDMA_CM_EVENT_REJECTED,
57 	RDMA_CM_EVENT_ESTABLISHED,
58 	RDMA_CM_EVENT_DISCONNECTED,
59 	RDMA_CM_EVENT_DEVICE_REMOVAL,
60 	RDMA_CM_EVENT_MULTICAST_JOIN,
61 	RDMA_CM_EVENT_MULTICAST_ERROR,
62 	RDMA_CM_EVENT_ADDR_CHANGE,
63 	RDMA_CM_EVENT_TIMEWAIT_EXIT
64 };
65 
66 enum rdma_port_space {
67 	RDMA_PS_SDP   = 0x0001,
68 	RDMA_PS_IPOIB = 0x0002,
69 	RDMA_PS_TCP   = 0x0106,
70 	RDMA_PS_UDP   = 0x0111,
71 	RDMA_PS_SCTP  = 0x0183
72 };
73 
74 struct rdma_addr {
75 	struct sockaddr_storage src_addr;
76 	struct sockaddr_storage dst_addr;
77 	struct rdma_dev_addr dev_addr;
78 };
79 
80 struct rdma_route {
81 	struct rdma_addr addr;
82 	struct ib_sa_path_rec *path_rec;
83 	int num_paths;
84 };
85 
86 struct rdma_conn_param {
87 	const void *private_data;
88 	u8 private_data_len;
89 	u8 responder_resources;
90 	u8 initiator_depth;
91 	u8 flow_control;
92 	u8 retry_count;		/* ignored when accepting */
93 	u8 rnr_retry_count;
94 	/* Fields below ignored if a QP is created on the rdma_cm_id. */
95 	u8 srq;
96 	u32 qp_num;
97 };
98 
99 struct rdma_ud_param {
100 	const void *private_data;
101 	u8 private_data_len;
102 	struct ib_ah_attr ah_attr;
103 	u32 qp_num;
104 	u32 qkey;
105 };
106 
107 struct rdma_cm_event {
108 	enum rdma_cm_event_type	 event;
109 	int			 status;
110 	union {
111 		struct rdma_conn_param	conn;
112 		struct rdma_ud_param	ud;
113 	} param;
114 };
115 
116 struct rdma_cm_id;
117 
118 /**
119  * rdma_cm_event_handler - Callback used to report user events.
120  *
121  * Notes: Users may not call rdma_destroy_id from this callback to destroy
122  *   the passed in id, or a corresponding listen id.  Returning a
123  *   non-zero value from the callback will destroy the passed in id.
124  */
125 typedef int (*rdma_cm_event_handler)(struct rdma_cm_id *id,
126 				     struct rdma_cm_event *event);
127 
128 struct rdma_cm_id {
129 	struct ib_device	*device;
130 	void			*context;
131 	struct ib_qp		*qp;
132 	rdma_cm_event_handler	 event_handler;
133 	struct rdma_route	 route;
134 	enum rdma_port_space	 ps;
135 	u8			 port_num;
136 };
137 
138 /**
139  * rdma_create_id - Create an RDMA identifier.
140  *
141  * @event_handler: User callback invoked to report events associated with the
142  *   returned rdma_id.
143  * @context: User specified context associated with the id.
144  * @ps: RDMA port space.
145  */
146 struct rdma_cm_id *rdma_create_id(rdma_cm_event_handler event_handler,
147 				  void *context, enum rdma_port_space ps);
148 
149 /**
150   * rdma_destroy_id - Destroys an RDMA identifier.
151   *
152   * @id: RDMA identifier.
153   *
154   * Note: calling this function has the effect of canceling in-flight
155   * asynchronous operations associated with the id.
156   */
157 void rdma_destroy_id(struct rdma_cm_id *id);
158 
159 /**
160  * rdma_bind_addr - Bind an RDMA identifier to a source address and
161  *   associated RDMA device, if needed.
162  *
163  * @id: RDMA identifier.
164  * @addr: Local address information.  Wildcard values are permitted.
165  *
166  * This associates a source address with the RDMA identifier before calling
167  * rdma_listen.  If a specific local address is given, the RDMA identifier will
168  * be bound to a local RDMA device.
169  */
170 int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr);
171 
172 /**
173  * rdma_resolve_addr - Resolve destination and optional source addresses
174  *   from IP addresses to an RDMA address.  If successful, the specified
175  *   rdma_cm_id will be bound to a local device.
176  *
177  * @id: RDMA identifier.
178  * @src_addr: Source address information.  This parameter may be NULL.
179  * @dst_addr: Destination address information.
180  * @timeout_ms: Time to wait for resolution to complete.
181  */
182 int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
183 		      struct sockaddr *dst_addr, int timeout_ms);
184 
185 /**
186  * rdma_resolve_route - Resolve the RDMA address bound to the RDMA identifier
187  *   into route information needed to establish a connection.
188  *
189  * This is called on the client side of a connection.
190  * Users must have first called rdma_resolve_addr to resolve a dst_addr
191  * into an RDMA address before calling this routine.
192  */
193 int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms);
194 
195 /**
196  * rdma_create_qp - Allocate a QP and associate it with the specified RDMA
197  * identifier.
198  *
199  * QPs allocated to an rdma_cm_id will automatically be transitioned by the CMA
200  * through their states.
201  */
202 int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
203 		   struct ib_qp_init_attr *qp_init_attr);
204 
205 /**
206  * rdma_destroy_qp - Deallocate the QP associated with the specified RDMA
207  * identifier.
208  *
209  * Users must destroy any QP associated with an RDMA identifier before
210  * destroying the RDMA ID.
211  */
212 void rdma_destroy_qp(struct rdma_cm_id *id);
213 
214 /**
215  * rdma_init_qp_attr - Initializes the QP attributes for use in transitioning
216  *   to a specified QP state.
217  * @id: Communication identifier associated with the QP attributes to
218  *   initialize.
219  * @qp_attr: On input, specifies the desired QP state.  On output, the
220  *   mandatory and desired optional attributes will be set in order to
221  *   modify the QP to the specified state.
222  * @qp_attr_mask: The QP attribute mask that may be used to transition the
223  *   QP to the specified state.
224  *
225  * Users must set the @qp_attr->qp_state to the desired QP state.  This call
226  * will set all required attributes for the given transition, along with
227  * known optional attributes.  Users may override the attributes returned from
228  * this call before calling ib_modify_qp.
229  *
230  * Users that wish to have their QP automatically transitioned through its
231  * states can associate a QP with the rdma_cm_id by calling rdma_create_qp().
232  */
233 int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
234 		       int *qp_attr_mask);
235 
236 /**
237  * rdma_connect - Initiate an active connection request.
238  * @id: Connection identifier to connect.
239  * @conn_param: Connection information used for connected QPs.
240  *
241  * Users must have resolved a route for the rdma_cm_id to connect with
242  * by having called rdma_resolve_route before calling this routine.
243  *
244  * This call will either connect to a remote QP or obtain remote QP
245  * information for unconnected rdma_cm_id's.  The actual operation is
246  * based on the rdma_cm_id's port space.
247  */
248 int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param);
249 
250 /**
251  * rdma_listen - This function is called by the passive side to
252  *   listen for incoming connection requests.
253  *
254  * Users must have bound the rdma_cm_id to a local address by calling
255  * rdma_bind_addr before calling this routine.
256  */
257 int rdma_listen(struct rdma_cm_id *id, int backlog);
258 
259 /**
260  * rdma_accept - Called to accept a connection request or response.
261  * @id: Connection identifier associated with the request.
262  * @conn_param: Information needed to establish the connection.  This must be
263  *   provided if accepting a connection request.  If accepting a connection
264  *   response, this parameter must be NULL.
265  *
266  * Typically, this routine is only called by the listener to accept a connection
267  * request.  It must also be called on the active side of a connection if the
268  * user is performing their own QP transitions.
269  *
270  * In the case of error, a reject message is sent to the remote side and the
271  * state of the qp associated with the id is modified to error, such that any
272  * previously posted receive buffers would be flushed.
273  */
274 int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param);
275 
276 /**
277  * rdma_notify - Notifies the RDMA CM of an asynchronous event that has
278  * occurred on the connection.
279  * @id: Connection identifier to transition to established.
280  * @event: Asynchronous event.
281  *
282  * This routine should be invoked by users to notify the CM of relevant
283  * communication events.  Events that should be reported to the CM and
284  * when to report them are:
285  *
286  * IB_EVENT_COMM_EST - Used when a message is received on a connected
287  *    QP before an RTU has been received.
288  */
289 int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event);
290 
291 /**
292  * rdma_reject - Called to reject a connection request or response.
293  */
294 int rdma_reject(struct rdma_cm_id *id, const void *private_data,
295 		u8 private_data_len);
296 
297 /**
298  * rdma_disconnect - This function disconnects the associated QP and
299  *   transitions it into the error state.
300  */
301 int rdma_disconnect(struct rdma_cm_id *id);
302 
303 /**
304  * rdma_join_multicast - Join the multicast group specified by the given
305  *   address.
306  * @id: Communication identifier associated with the request.
307  * @addr: Multicast address identifying the group to join.
308  * @context: User-defined context associated with the join request, returned
309  * to the user through the private_data pointer in multicast events.
310  */
311 int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
312 			void *context);
313 
314 /**
315  * rdma_leave_multicast - Leave the multicast group specified by the given
316  *   address.
317  */
318 void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr);
319 
320 /**
321  * rdma_set_service_type - Set the type of service associated with a
322  *   connection identifier.
323  * @id: Communication identifier to associated with service type.
324  * @tos: Type of service.
325  *
326  * The type of service is interpretted as a differentiated service
327  * field (RFC 2474).  The service type should be specified before
328  * performing route resolution, as existing communication on the
329  * connection identifier may be unaffected.  The type of service
330  * requested may not be supported by the network to all destinations.
331  */
332 void rdma_set_service_type(struct rdma_cm_id *id, int tos);
333 
334 int rdma_cma_any_addr(struct sockaddr *addr);
335 int rdma_find_cmid_laddr(struct sockaddr_in *local_addr,
336 		unsigned short dev_type, void **cm_id);
337 #endif /* RDMA_CM_H */
338