xref: /linux/net/rds/rds.h (revision ca220141fa8ebae09765a242076b2b77338106b0)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _RDS_RDS_H
3 #define _RDS_RDS_H
4 
5 #include <net/sock.h>
6 #include <linux/scatterlist.h>
7 #include <linux/highmem.h>
8 #include <rdma/rdma_cm.h>
9 #include <linux/mutex.h>
10 #include <linux/rds.h>
11 #include <linux/rhashtable.h>
12 #include <linux/refcount.h>
13 #include <linux/in6.h>
14 
15 #include "info.h"
16 
17 /*
18  * RDS Network protocol version
19  */
20 #define RDS_PROTOCOL_3_0	0x0300
21 #define RDS_PROTOCOL_3_1	0x0301
22 #define RDS_PROTOCOL_4_0	0x0400
23 #define RDS_PROTOCOL_4_1	0x0401
24 #define RDS_PROTOCOL_VERSION	RDS_PROTOCOL_3_1
25 #define RDS_PROTOCOL_MAJOR(v)	((v) >> 8)
26 #define RDS_PROTOCOL_MINOR(v)	((v) & 255)
27 #define RDS_PROTOCOL(maj, min)	(((maj) << 8) | min)
28 #define RDS_PROTOCOL_COMPAT_VERSION	RDS_PROTOCOL_3_1
29 
30 /* The following ports, 16385, 18634, 18635, are registered with IANA as
31  * the ports to be used for RDS over TCP and UDP.  Currently, only RDS over
32  * TCP and RDS over IB/RDMA are implemented.  18634 is the historical value
33  * used for the RDMA_CM listener port.  RDS/TCP uses port 16385.  After
34  * IPv6 work, RDMA_CM also uses 16385 as the listener port.  18634 is kept
35  * to ensure compatibility with older RDS modules.  Those ports are defined
36  * in each transport's header file.
37  */
38 #define RDS_PORT	18634
39 
40 #ifdef ATOMIC64_INIT
41 #define KERNEL_HAS_ATOMIC64
42 #endif
43 #ifdef RDS_DEBUG
44 #define rdsdebug(fmt, args...) pr_debug("%s(): " fmt, __func__ , ##args)
45 #else
46 /* sigh, pr_debug() causes unused variable warnings */
47 static inline __printf(1, 2)
48 void rdsdebug(char *fmt, ...)
49 {
50 }
51 #endif
52 
53 #define RDS_FRAG_SHIFT	12
54 #define RDS_FRAG_SIZE	((unsigned int)(1 << RDS_FRAG_SHIFT))
55 
56 /* Used to limit both RDMA and non-RDMA RDS message to 1MB */
57 #define RDS_MAX_MSG_SIZE	((unsigned int)(1 << 20))
58 
59 #define RDS_CONG_MAP_BYTES	(65536 / 8)
60 #define RDS_CONG_MAP_PAGES	(PAGE_ALIGN(RDS_CONG_MAP_BYTES) / PAGE_SIZE)
61 #define RDS_CONG_MAP_PAGE_BITS	(PAGE_SIZE * 8)
62 
63 struct rds_cong_map {
64 	struct rb_node		m_rb_node;
65 	struct in6_addr		m_addr;
66 	wait_queue_head_t	m_waitq;
67 	struct list_head	m_conn_list;
68 	unsigned long		m_page_addrs[RDS_CONG_MAP_PAGES];
69 };
70 
71 
72 /*
73  * This is how we will track the connection state:
74  * A connection is always in one of the following
75  * states. Updates to the state are atomic and imply
76  * a memory barrier.
77  */
78 enum {
79 	RDS_CONN_DOWN = 0,
80 	RDS_CONN_CONNECTING,
81 	RDS_CONN_DISCONNECTING,
82 	RDS_CONN_UP,
83 	RDS_CONN_RESETTING,
84 	RDS_CONN_ERROR,
85 };
86 
87 /* Bits for c_flags */
88 #define RDS_LL_SEND_FULL	0
89 #define RDS_RECONNECT_PENDING	1
90 #define RDS_IN_XMIT		2
91 #define RDS_RECV_REFILL		3
92 #define	RDS_DESTROY_PENDING	4
93 
94 /* Max number of multipaths per RDS connection. Must be a power of 2 */
95 #define	RDS_MPATH_WORKERS	8
96 #define	RDS_MPATH_HASH(rs, n) (jhash_1word(ntohs((rs)->rs_bound_port), \
97 			       (rs)->rs_hash_initval) & ((n) - 1))
98 
99 #define IS_CANONICAL(laddr, faddr) (htonl(laddr) < htonl(faddr))
100 
101 /* Per mpath connection state */
102 struct rds_conn_path {
103 	struct rds_connection	*cp_conn;
104 	struct rds_message	*cp_xmit_rm;
105 	unsigned long		cp_xmit_sg;
106 	unsigned int		cp_xmit_hdr_off;
107 	unsigned int		cp_xmit_data_off;
108 	unsigned int		cp_xmit_atomic_sent;
109 	unsigned int		cp_xmit_rdma_sent;
110 	unsigned int		cp_xmit_data_sent;
111 
112 	spinlock_t		cp_lock;		/* protect msg queues */
113 	u64			cp_next_tx_seq;
114 	struct list_head	cp_send_queue;
115 	struct list_head	cp_retrans;
116 
117 	u64			cp_next_rx_seq;
118 
119 	void			*cp_transport_data;
120 
121 	struct workqueue_struct	*cp_wq;
122 	atomic_t		cp_state;
123 	unsigned long		cp_send_gen;
124 	unsigned long		cp_flags;
125 	unsigned long		cp_reconnect_jiffies;
126 	struct delayed_work	cp_send_w;
127 	struct delayed_work	cp_recv_w;
128 	struct delayed_work	cp_conn_w;
129 	struct work_struct	cp_down_w;
130 	struct mutex		cp_cm_lock;	/* protect cp_state & cm */
131 	wait_queue_head_t	cp_waitq;
132 
133 	unsigned int		cp_unacked_packets;
134 	unsigned int		cp_unacked_bytes;
135 	unsigned int		cp_index;
136 };
137 
138 /* One rds_connection per RDS address pair */
139 struct rds_connection {
140 	struct hlist_node	c_hash_node;
141 	struct in6_addr		c_laddr;
142 	struct in6_addr		c_faddr;
143 	int			c_dev_if; /* ifindex used for this conn */
144 	int			c_bound_if; /* ifindex of c_laddr */
145 	unsigned int		c_loopback:1,
146 				c_isv6:1,
147 				c_ping_triggered:1,
148 				c_pad_to_32:29;
149 	int			c_npaths;
150 	bool			c_with_sport_idx;
151 	struct rds_connection	*c_passive;
152 	struct rds_transport	*c_trans;
153 
154 	struct rds_cong_map	*c_lcong;
155 	struct rds_cong_map	*c_fcong;
156 
157 	/* Protocol version */
158 	unsigned int		c_proposed_version;
159 	unsigned int		c_version;
160 	possible_net_t		c_net;
161 
162 	/* TOS */
163 	u8			c_tos;
164 
165 	struct list_head	c_map_item;
166 	unsigned long		c_map_queued;
167 
168 	struct rds_conn_path	*c_path;
169 	wait_queue_head_t	c_hs_waitq; /* handshake waitq */
170 
171 	u32			c_my_gen_num;
172 	u32			c_peer_gen_num;
173 
174 	u64			c_cp0_mprds_catchup_tx_seq;
175 };
176 
177 static inline
178 struct net *rds_conn_net(struct rds_connection *conn)
179 {
180 	return read_pnet(&conn->c_net);
181 }
182 
183 static inline
184 void rds_conn_net_set(struct rds_connection *conn, struct net *net)
185 {
186 	write_pnet(&conn->c_net, net);
187 }
188 
189 #define RDS_FLAG_CONG_BITMAP		0x01
190 #define RDS_FLAG_ACK_REQUIRED		0x02
191 #define RDS_FLAG_RETRANSMITTED		0x04
192 #define RDS_FLAG_EXTHDR_EXTENSION	0x20
193 #define RDS_MAX_ADV_CREDIT		255
194 
195 /* RDS_FLAG_PROBE_PORT is the reserved sport used for sending a ping
196  * probe to exchange control information before establishing a connection.
197  * Currently the control information that is exchanged is the number of
198  * supported paths. If the peer is a legacy (older kernel revision) peer,
199  * it would return a pong message without additional control information
200  * that would then alert the sender that the peer was an older rev.
201  */
202 #define RDS_FLAG_PROBE_PORT	1
203 #define	RDS_HS_PROBE(sport, dport) \
204 		((sport == RDS_FLAG_PROBE_PORT && dport == 0) || \
205 		 (sport == 0 && dport == RDS_FLAG_PROBE_PORT))
206 /*
207  * Maximum space available for extension headers.
208  */
209 #define RDS_HEADER_EXT_SPACE	16
210 
211 struct rds_header {
212 	__be64	h_sequence;
213 	__be64	h_ack;
214 	__be32	h_len;
215 	__be16	h_sport;
216 	__be16	h_dport;
217 	u8	h_flags;
218 	u8	h_credit;
219 	u8	h_padding[4];
220 	__sum16	h_csum;
221 
222 	u8	h_exthdr[RDS_HEADER_EXT_SPACE];
223 };
224 
225 /*
226  * Reserved - indicates end of extensions
227  */
228 #define RDS_EXTHDR_NONE		0
229 
230 /*
231  * This extension header is included in the very
232  * first message that is sent on a new connection,
233  * and identifies the protocol level. This will help
234  * rolling updates if a future change requires breaking
235  * the protocol.
236  * NB: This is no longer true for IB, where we do a version
237  * negotiation during the connection setup phase (protocol
238  * version information is included in the RDMA CM private data).
239  */
240 #define RDS_EXTHDR_VERSION	1
241 struct rds_ext_header_version {
242 	__be32			h_version;
243 };
244 
245 /*
246  * This extension header is included in the RDS message
247  * chasing an RDMA operation.
248  */
249 #define RDS_EXTHDR_RDMA		2
250 struct rds_ext_header_rdma {
251 	__be32			h_rdma_rkey;
252 };
253 
254 /*
255  * This extension header tells the peer about the
256  * destination <R_Key,offset> of the requested RDMA
257  * operation.
258  */
259 #define RDS_EXTHDR_RDMA_DEST	3
260 struct rds_ext_header_rdma_dest {
261 	__be32			h_rdma_rkey;
262 	__be32			h_rdma_offset;
263 };
264 
265 /*
266  * This extension header tells the peer about delivered RDMA byte count.
267  */
268 #define RDS_EXTHDR_RDMA_BYTES	4
269 
270 struct rds_ext_header_rdma_bytes {
271 	__be32		h_rdma_bytes;	/* byte count */
272 	u8		h_rflags;	/* direction of RDMA, write or read */
273 	u8		h_pad[3];
274 };
275 
276 #define RDS_FLAG_RDMA_WR_BYTES	0x01
277 #define RDS_FLAG_RDMA_RD_BYTES	0x02
278 
279 /* Extension header announcing number of paths.
280  * Implicit length = 2 bytes.
281  */
282 #define RDS_EXTHDR_NPATHS	5
283 #define RDS_EXTHDR_GEN_NUM	6
284 #define RDS_EXTHDR_SPORT_IDX    8
285 
286 #define __RDS_EXTHDR_MAX	16 /* for now */
287 
288 #define RDS_RX_MAX_TRACES	(RDS_MSG_RX_DGRAM_TRACE_MAX + 1)
289 #define	RDS_MSG_RX_HDR		0
290 #define	RDS_MSG_RX_START	1
291 #define	RDS_MSG_RX_END		2
292 #define	RDS_MSG_RX_CMSG		3
293 
294 /* The following values are whitelisted for usercopy */
295 struct rds_inc_usercopy {
296 	rds_rdma_cookie_t	rdma_cookie;
297 	ktime_t			rx_tstamp;
298 };
299 
300 struct rds_incoming {
301 	refcount_t		i_refcount;
302 	struct list_head	i_item;
303 	struct rds_connection	*i_conn;
304 	struct rds_conn_path	*i_conn_path;
305 	struct rds_header	i_hdr;
306 	unsigned long		i_rx_jiffies;
307 	struct in6_addr		i_saddr;
308 
309 	struct rds_inc_usercopy i_usercopy;
310 	u64			i_rx_lat_trace[RDS_RX_MAX_TRACES];
311 };
312 
313 struct rds_mr {
314 	struct rb_node		r_rb_node;
315 	struct kref		r_kref;
316 	u32			r_key;
317 
318 	/* A copy of the creation flags */
319 	unsigned int		r_use_once:1;
320 	unsigned int		r_invalidate:1;
321 	unsigned int		r_write:1;
322 
323 	struct rds_sock		*r_sock; /* back pointer to the socket that owns us */
324 	struct rds_transport	*r_trans;
325 	void			*r_trans_private;
326 };
327 
328 static inline rds_rdma_cookie_t rds_rdma_make_cookie(u32 r_key, u32 offset)
329 {
330 	return r_key | (((u64) offset) << 32);
331 }
332 
333 static inline u32 rds_rdma_cookie_key(rds_rdma_cookie_t cookie)
334 {
335 	return cookie;
336 }
337 
338 static inline u32 rds_rdma_cookie_offset(rds_rdma_cookie_t cookie)
339 {
340 	return cookie >> 32;
341 }
342 
343 /* atomic operation types */
344 #define RDS_ATOMIC_TYPE_CSWP		0
345 #define RDS_ATOMIC_TYPE_FADD		1
346 
347 /*
348  * m_sock_item and m_conn_item are on lists that are serialized under
349  * conn->c_lock.  m_sock_item has additional meaning in that once it is empty
350  * the message will not be put back on the retransmit list after being sent.
351  * messages that are canceled while being sent rely on this.
352  *
353  * m_inc is used by loopback so that it can pass an incoming message straight
354  * back up into the rx path.  It embeds a wire header which is also used by
355  * the send path, which is kind of awkward.
356  *
357  * m_sock_item indicates the message's presence on a socket's send or receive
358  * queue.  m_rs will point to that socket.
359  *
360  * m_daddr is used by cancellation to prune messages to a given destination.
361  *
362  * The RDS_MSG_ON_SOCK and RDS_MSG_ON_CONN flags are used to avoid lock
363  * nesting.  As paths iterate over messages on a sock, or conn, they must
364  * also lock the conn, or sock, to remove the message from those lists too.
365  * Testing the flag to determine if the message is still on the lists lets
366  * us avoid testing the list_head directly.  That means each path can use
367  * the message's list_head to keep it on a local list while juggling locks
368  * without confusing the other path.
369  *
370  * m_ack_seq is an optional field set by transports who need a different
371  * sequence number range to invalidate.  They can use this in a callback
372  * that they pass to rds_send_drop_acked() to see if each message has been
373  * acked.  The HAS_ACK_SEQ flag can be used to detect messages which haven't
374  * had ack_seq set yet.
375  */
376 #define RDS_MSG_ON_SOCK		1
377 #define RDS_MSG_ON_CONN		2
378 #define RDS_MSG_HAS_ACK_SEQ	3
379 #define RDS_MSG_ACK_REQUIRED	4
380 #define RDS_MSG_RETRANSMITTED	5
381 #define RDS_MSG_MAPPED		6
382 #define RDS_MSG_PAGEVEC		7
383 #define RDS_MSG_FLUSH		8
384 
385 struct rds_znotifier {
386 	struct mmpin		z_mmp;
387 	u32			z_cookie;
388 };
389 
390 struct rds_msg_zcopy_info {
391 	struct list_head rs_zcookie_next;
392 	union {
393 		struct rds_znotifier znotif;
394 		struct rds_zcopy_cookies zcookies;
395 	};
396 };
397 
398 struct rds_msg_zcopy_queue {
399 	struct list_head zcookie_head;
400 	spinlock_t lock; /* protects zcookie_head queue */
401 };
402 
403 static inline void rds_message_zcopy_queue_init(struct rds_msg_zcopy_queue *q)
404 {
405 	spin_lock_init(&q->lock);
406 	INIT_LIST_HEAD(&q->zcookie_head);
407 }
408 
409 struct rds_iov_vector {
410 	struct rds_iovec *iov;
411 	int               len;
412 };
413 
414 struct rds_iov_vector_arr {
415 	struct rds_iov_vector *vec;
416 	int                    len;
417 	int                    indx;
418 	int                    incr;
419 };
420 
421 struct rds_message {
422 	refcount_t		m_refcount;
423 	struct list_head	m_sock_item;
424 	struct list_head	m_conn_item;
425 	struct rds_incoming	m_inc;
426 	u64			m_ack_seq;
427 	struct in6_addr		m_daddr;
428 	unsigned long		m_flags;
429 
430 	/* Never access m_rs without holding m_rs_lock.
431 	 * Lock nesting is
432 	 *  rm->m_rs_lock
433 	 *   -> rs->rs_lock
434 	 */
435 	spinlock_t		m_rs_lock;
436 	wait_queue_head_t	m_flush_wait;
437 
438 	struct rds_sock		*m_rs;
439 
440 	/* cookie to send to remote, in rds header */
441 	rds_rdma_cookie_t	m_rdma_cookie;
442 
443 	unsigned int		m_used_sgs;
444 	unsigned int		m_total_sgs;
445 
446 	void			*m_final_op;
447 
448 	struct {
449 		struct rm_atomic_op {
450 			int			op_type;
451 			union {
452 				struct {
453 					uint64_t	compare;
454 					uint64_t	swap;
455 					uint64_t	compare_mask;
456 					uint64_t	swap_mask;
457 				} op_m_cswp;
458 				struct {
459 					uint64_t	add;
460 					uint64_t	nocarry_mask;
461 				} op_m_fadd;
462 			};
463 
464 			u32			op_rkey;
465 			u64			op_remote_addr;
466 			unsigned int		op_notify:1;
467 			unsigned int		op_recverr:1;
468 			unsigned int		op_mapped:1;
469 			unsigned int		op_silent:1;
470 			unsigned int		op_active:1;
471 			struct scatterlist	*op_sg;
472 			struct rds_notifier	*op_notifier;
473 
474 			struct rds_mr		*op_rdma_mr;
475 		} atomic;
476 		struct rm_rdma_op {
477 			u32			op_rkey;
478 			u64			op_remote_addr;
479 			unsigned int		op_write:1;
480 			unsigned int		op_fence:1;
481 			unsigned int		op_notify:1;
482 			unsigned int		op_recverr:1;
483 			unsigned int		op_mapped:1;
484 			unsigned int		op_silent:1;
485 			unsigned int		op_active:1;
486 			unsigned int		op_bytes;
487 			unsigned int		op_nents;
488 			unsigned int		op_count;
489 			struct scatterlist	*op_sg;
490 			struct rds_notifier	*op_notifier;
491 
492 			struct rds_mr		*op_rdma_mr;
493 
494 			u64			op_odp_addr;
495 			struct rds_mr		*op_odp_mr;
496 		} rdma;
497 		struct rm_data_op {
498 			unsigned int		op_active:1;
499 			unsigned int		op_nents;
500 			unsigned int		op_count;
501 			unsigned int		op_dmasg;
502 			unsigned int		op_dmaoff;
503 			struct rds_znotifier	*op_mmp_znotifier;
504 			struct scatterlist	*op_sg;
505 		} data;
506 	};
507 
508 	struct rds_conn_path *m_conn_path;
509 };
510 
511 /*
512  * The RDS notifier is used (optionally) to tell the application about
513  * completed RDMA operations. Rather than keeping the whole rds message
514  * around on the queue, we allocate a small notifier that is put on the
515  * socket's notifier_list. Notifications are delivered to the application
516  * through control messages.
517  */
518 struct rds_notifier {
519 	struct list_head	n_list;
520 	uint64_t		n_user_token;
521 	int			n_status;
522 };
523 
524 /* Available as part of RDS core, so doesn't need to participate
525  * in get_preferred transport etc
526  */
527 #define	RDS_TRANS_LOOP	3
528 
529 struct rds_transport {
530 	char			t_name[TRANSNAMSIZ];
531 	struct list_head	t_item;
532 	struct module		*t_owner;
533 	unsigned int		t_prefer_loopback:1,
534 				t_mp_capable:1;
535 	unsigned int		t_type;
536 
537 	int (*laddr_check)(struct net *net, const struct in6_addr *addr,
538 			   __u32 scope_id);
539 	int (*conn_alloc)(struct rds_connection *conn, gfp_t gfp);
540 	void (*conn_free)(void *data);
541 
542 	/*
543 	 * conn_slots_available is invoked when a previously unavailable
544 	 * connection slot becomes available again. rds_tcp_accept_one_path may
545 	 * return -ENOBUFS if it cannot find an available slot, and then stashes
546 	 * the new socket in "rds_tcp_accepted_sock". This function re-issues
547 	 * `rds_tcp_accept_one_path`, which picks up the stashed socket and
548 	 * continuing where it left with "-ENOBUFS" last time.  This ensures
549 	 * messages received on the new socket are not discarded when no
550 	 * connection path was available at the time.
551 	 */
552 	void (*conn_slots_available)(struct rds_connection *conn, bool fan_out);
553 	int (*conn_path_connect)(struct rds_conn_path *cp);
554 
555 	/*
556 	 * conn_shutdown stops traffic on the given connection.  Once
557 	 * it returns the connection can not call rds_recv_incoming().
558 	 * This will only be called once after conn_connect returns
559 	 * non-zero success and will The caller serializes this with
560 	 * the send and connecting paths (xmit_* and conn_*).  The
561 	 * transport is responsible for other serialization, including
562 	 * rds_recv_incoming().  This is called in process context but
563 	 * should try hard not to block.
564 	 */
565 	void (*conn_path_shutdown)(struct rds_conn_path *conn);
566 	void (*xmit_path_prepare)(struct rds_conn_path *cp);
567 	void (*xmit_path_complete)(struct rds_conn_path *cp);
568 
569 	/*
570 	 * .xmit is called by rds_send_xmit() to tell the transport to send
571 	 * part of a message.  The caller serializes on the send_sem so this
572 	 * doesn't need to be reentrant for a given conn.  The header must be
573 	 * sent before the data payload.  .xmit must be prepared to send a
574 	 * message with no data payload.  .xmit should return the number of
575 	 * bytes that were sent down the connection, including header bytes.
576 	 * Returning 0 tells the caller that it doesn't need to perform any
577 	 * additional work now.  This is usually the case when the transport has
578 	 * filled the sending queue for its connection and will handle
579 	 * triggering the rds thread to continue the send when space becomes
580 	 * available.  Returning -EAGAIN tells the caller to retry the send
581 	 * immediately.  Returning -ENOMEM tells the caller to retry the send at
582 	 * some point in the future.
583 	 */
584 	int (*xmit)(struct rds_connection *conn, struct rds_message *rm,
585 		    unsigned int hdr_off, unsigned int sg, unsigned int off);
586 	int (*xmit_rdma)(struct rds_connection *conn, struct rm_rdma_op *op);
587 	int (*xmit_atomic)(struct rds_connection *conn, struct rm_atomic_op *op);
588 	int (*recv_path)(struct rds_conn_path *cp);
589 	int (*inc_copy_to_user)(struct rds_incoming *inc, struct iov_iter *to);
590 	void (*inc_free)(struct rds_incoming *inc);
591 
592 	int (*cm_handle_connect)(struct rdma_cm_id *cm_id,
593 				 struct rdma_cm_event *event, bool isv6);
594 	int (*cm_initiate_connect)(struct rdma_cm_id *cm_id, bool isv6);
595 	void (*cm_connect_complete)(struct rds_connection *conn,
596 				    struct rdma_cm_event *event);
597 
598 	unsigned int (*stats_info_copy)(struct rds_info_iterator *iter,
599 					unsigned int avail);
600 	void (*exit)(void);
601 	void *(*get_mr)(struct scatterlist *sg, unsigned long nr_sg,
602 			struct rds_sock *rs, u32 *key_ret,
603 			struct rds_connection *conn,
604 			u64 start, u64 length, int need_odp);
605 	void (*sync_mr)(void *trans_private, int direction);
606 	void (*free_mr)(void *trans_private, int invalidate);
607 	void (*flush_mrs)(void);
608 	bool (*t_unloading)(struct rds_connection *conn);
609 	u8 (*get_tos_map)(u8 tos);
610 };
611 
612 /* Bind hash table key length.  It is the sum of the size of a struct
613  * in6_addr, a scope_id  and a port.
614  */
615 #define RDS_BOUND_KEY_LEN \
616 	(sizeof(struct in6_addr) + sizeof(__u32) + sizeof(__be16))
617 
618 struct rds_sock {
619 	struct sock		rs_sk;
620 
621 	u64			rs_user_addr;
622 	u64			rs_user_bytes;
623 
624 	/*
625 	 * bound_addr used for both incoming and outgoing, no INADDR_ANY
626 	 * support.
627 	 */
628 	struct rhash_head	rs_bound_node;
629 	u8			rs_bound_key[RDS_BOUND_KEY_LEN];
630 	struct sockaddr_in6	rs_bound_sin6;
631 #define rs_bound_addr		rs_bound_sin6.sin6_addr
632 #define rs_bound_addr_v4	rs_bound_sin6.sin6_addr.s6_addr32[3]
633 #define rs_bound_port		rs_bound_sin6.sin6_port
634 #define rs_bound_scope_id	rs_bound_sin6.sin6_scope_id
635 	struct in6_addr		rs_conn_addr;
636 #define rs_conn_addr_v4		rs_conn_addr.s6_addr32[3]
637 	__be16			rs_conn_port;
638 	struct rds_transport    *rs_transport;
639 
640 	/*
641 	 * rds_sendmsg caches the conn it used the last time around.
642 	 * This helps avoid costly lookups.
643 	 */
644 	struct rds_connection	*rs_conn;
645 
646 	/* flag indicating we were congested or not */
647 	int			rs_congested;
648 	/* seen congestion (ENOBUFS) when sending? */
649 	int			rs_seen_congestion;
650 
651 	/* rs_lock protects all these adjacent members before the newline */
652 	spinlock_t		rs_lock;
653 	struct list_head	rs_send_queue;
654 	u32			rs_snd_bytes;
655 	int			rs_rcv_bytes;
656 	struct list_head	rs_notify_queue;	/* currently used for failed RDMAs */
657 
658 	/* Congestion wake_up. If rs_cong_monitor is set, we use cong_mask
659 	 * to decide whether the application should be woken up.
660 	 * If not set, we use rs_cong_track to find out whether a cong map
661 	 * update arrived.
662 	 */
663 	uint64_t		rs_cong_mask;
664 	uint64_t		rs_cong_notify;
665 	struct list_head	rs_cong_list;
666 	unsigned long		rs_cong_track;
667 
668 	/*
669 	 * rs_recv_lock protects the receive queue, and is
670 	 * used to serialize with rds_release.
671 	 */
672 	rwlock_t		rs_recv_lock;
673 	struct list_head	rs_recv_queue;
674 
675 	/* just for stats reporting */
676 	struct list_head	rs_item;
677 
678 	/* these have their own lock */
679 	spinlock_t		rs_rdma_lock;
680 	struct rb_root		rs_rdma_keys;
681 
682 	/* Socket options - in case there will be more */
683 	unsigned char		rs_recverr,
684 				rs_cong_monitor;
685 	u32			rs_hash_initval;
686 
687 	/* Socket receive path trace points*/
688 	u8			rs_rx_traces;
689 	u8			rs_rx_trace[RDS_MSG_RX_DGRAM_TRACE_MAX];
690 	struct rds_msg_zcopy_queue rs_zcookie_queue;
691 	u8			rs_tos;
692 };
693 
694 static inline struct rds_sock *rds_sk_to_rs(const struct sock *sk)
695 {
696 	return container_of(sk, struct rds_sock, rs_sk);
697 }
698 static inline struct sock *rds_rs_to_sk(struct rds_sock *rs)
699 {
700 	return &rs->rs_sk;
701 }
702 
703 /*
704  * The stack assigns sk_sndbuf and sk_rcvbuf to twice the specified value
705  * to account for overhead.  We don't account for overhead, we just apply
706  * the number of payload bytes to the specified value.
707  */
708 static inline int rds_sk_sndbuf(struct rds_sock *rs)
709 {
710 	return rds_rs_to_sk(rs)->sk_sndbuf / 2;
711 }
712 static inline int rds_sk_rcvbuf(struct rds_sock *rs)
713 {
714 	return rds_rs_to_sk(rs)->sk_rcvbuf / 2;
715 }
716 
717 struct rds_statistics {
718 	u64	s_conn_reset;
719 	u64	s_recv_drop_bad_checksum;
720 	u64	s_recv_drop_old_seq;
721 	u64	s_recv_drop_no_sock;
722 	u64	s_recv_drop_dead_sock;
723 	u64	s_recv_deliver_raced;
724 	u64	s_recv_delivered;
725 	u64	s_recv_queued;
726 	u64	s_recv_immediate_retry;
727 	u64	s_recv_delayed_retry;
728 	u64	s_recv_ack_required;
729 	u64	s_recv_rdma_bytes;
730 	u64	s_recv_ping;
731 	u64	s_send_queue_empty;
732 	u64	s_send_queue_full;
733 	u64	s_send_lock_contention;
734 	u64	s_send_lock_queue_raced;
735 	u64	s_send_immediate_retry;
736 	u64	s_send_delayed_retry;
737 	u64	s_send_drop_acked;
738 	u64	s_send_ack_required;
739 	u64	s_send_queued;
740 	u64	s_send_rdma;
741 	u64	s_send_rdma_bytes;
742 	u64	s_send_pong;
743 	u64	s_page_remainder_hit;
744 	u64	s_page_remainder_miss;
745 	u64	s_copy_to_user;
746 	u64	s_copy_from_user;
747 	u64	s_cong_update_queued;
748 	u64	s_cong_update_received;
749 	u64	s_cong_send_error;
750 	u64	s_cong_send_blocked;
751 	u64	s_recv_bytes_added_to_socket;
752 	u64	s_recv_bytes_removed_from_socket;
753 	u64	s_send_stuck_rm;
754 	u64	s_mprds_catchup_tx0_retries;
755 };
756 
757 /* af_rds.c */
758 void rds_sock_addref(struct rds_sock *rs);
759 void rds_sock_put(struct rds_sock *rs);
760 void rds_wake_sk_sleep(struct rds_sock *rs);
761 static inline void __rds_wake_sk_sleep(struct sock *sk)
762 {
763 	wait_queue_head_t *waitq = sk_sleep(sk);
764 
765 	if (!sock_flag(sk, SOCK_DEAD) && waitq)
766 		wake_up(waitq);
767 }
768 extern wait_queue_head_t rds_poll_waitq;
769 
770 
771 /* bind.c */
772 int rds_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int addr_len);
773 void rds_remove_bound(struct rds_sock *rs);
774 struct rds_sock *rds_find_bound(const struct in6_addr *addr, __be16 port,
775 				__u32 scope_id);
776 int rds_bind_lock_init(void);
777 void rds_bind_lock_destroy(void);
778 
779 /* cong.c */
780 int rds_cong_get_maps(struct rds_connection *conn);
781 void rds_cong_add_conn(struct rds_connection *conn);
782 void rds_cong_remove_conn(struct rds_connection *conn);
783 void rds_cong_set_bit(struct rds_cong_map *map, __be16 port);
784 void rds_cong_clear_bit(struct rds_cong_map *map, __be16 port);
785 int rds_cong_wait(struct rds_cong_map *map, __be16 port, int nonblock, struct rds_sock *rs);
786 void rds_cong_queue_updates(struct rds_cong_map *map);
787 void rds_cong_map_updated(struct rds_cong_map *map, uint64_t);
788 int rds_cong_updated_since(unsigned long *recent);
789 void rds_cong_add_socket(struct rds_sock *);
790 void rds_cong_remove_socket(struct rds_sock *);
791 void rds_cong_exit(void);
792 struct rds_message *rds_cong_update_alloc(struct rds_connection *conn);
793 
794 /* connection.c */
795 extern u32 rds_gen_num;
796 int rds_conn_init(void);
797 void rds_conn_exit(void);
798 struct rds_connection *rds_conn_create(struct net *net,
799 				       const struct in6_addr *laddr,
800 				       const struct in6_addr *faddr,
801 				       struct rds_transport *trans,
802 				       u8 tos, gfp_t gfp,
803 				       int dev_if);
804 struct rds_connection *rds_conn_create_outgoing(struct net *net,
805 						const struct in6_addr *laddr,
806 						const struct in6_addr *faddr,
807 						struct rds_transport *trans,
808 						u8 tos, gfp_t gfp, int dev_if);
809 void rds_conn_shutdown(struct rds_conn_path *cpath);
810 void rds_conn_destroy(struct rds_connection *conn);
811 void rds_conn_drop(struct rds_connection *conn);
812 void rds_conn_path_drop(struct rds_conn_path *cpath, bool destroy);
813 void rds_conn_connect_if_down(struct rds_connection *conn);
814 void rds_conn_path_connect_if_down(struct rds_conn_path *cp);
815 void rds_check_all_paths(struct rds_connection *conn);
816 void rds_for_each_conn_info(struct socket *sock, unsigned int len,
817 			  struct rds_info_iterator *iter,
818 			  struct rds_info_lengths *lens,
819 			  int (*visitor)(struct rds_connection *, void *),
820 			  u64 *buffer,
821 			  size_t item_len);
822 
823 __printf(2, 3)
824 void __rds_conn_path_error(struct rds_conn_path *cp, const char *, ...);
825 #define rds_conn_path_error(cp, fmt...) \
826 	__rds_conn_path_error(cp, KERN_WARNING "RDS: " fmt)
827 
828 static inline int
829 rds_conn_path_transition(struct rds_conn_path *cp, int old, int new)
830 {
831 	return atomic_cmpxchg(&cp->cp_state, old, new) == old;
832 }
833 
834 static inline int
835 rds_conn_transition(struct rds_connection *conn, int old, int new)
836 {
837 	WARN_ON(conn->c_trans->t_mp_capable);
838 	return rds_conn_path_transition(&conn->c_path[0], old, new);
839 }
840 
841 static inline int
842 rds_conn_path_state(struct rds_conn_path *cp)
843 {
844 	return atomic_read(&cp->cp_state);
845 }
846 
847 static inline int
848 rds_conn_state(struct rds_connection *conn)
849 {
850 	WARN_ON(conn->c_trans->t_mp_capable);
851 	return rds_conn_path_state(&conn->c_path[0]);
852 }
853 
854 static inline int
855 rds_conn_path_up(struct rds_conn_path *cp)
856 {
857 	return atomic_read(&cp->cp_state) == RDS_CONN_UP;
858 }
859 
860 static inline int
861 rds_conn_path_down(struct rds_conn_path *cp)
862 {
863 	return atomic_read(&cp->cp_state) == RDS_CONN_DOWN;
864 }
865 
866 static inline int
867 rds_conn_up(struct rds_connection *conn)
868 {
869 	WARN_ON(conn->c_trans->t_mp_capable);
870 	return rds_conn_path_up(&conn->c_path[0]);
871 }
872 
873 static inline int
874 rds_conn_path_connecting(struct rds_conn_path *cp)
875 {
876 	return atomic_read(&cp->cp_state) == RDS_CONN_CONNECTING;
877 }
878 
879 static inline int
880 rds_conn_connecting(struct rds_connection *conn)
881 {
882 	WARN_ON(conn->c_trans->t_mp_capable);
883 	return rds_conn_path_connecting(&conn->c_path[0]);
884 }
885 
886 /* message.c */
887 struct rds_message *rds_message_alloc(unsigned int nents, gfp_t gfp);
888 struct scatterlist *rds_message_alloc_sgs(struct rds_message *rm, int nents);
889 int rds_message_copy_from_user(struct rds_message *rm, struct iov_iter *from,
890 			       bool zcopy);
891 struct rds_message *rds_message_map_pages(unsigned long *page_addrs, unsigned int total_len);
892 void rds_message_populate_header(struct rds_header *hdr, __be16 sport,
893 				 __be16 dport, u64 seq);
894 int rds_message_add_extension(struct rds_header *hdr,
895 			      unsigned int type, const void *data);
896 int rds_message_next_extension(struct rds_header *hdr,
897 			       unsigned int *pos, void *buf, unsigned int *buflen);
898 int rds_message_add_rdma_dest_extension(struct rds_header *hdr, u32 r_key, u32 offset);
899 int rds_message_inc_copy_to_user(struct rds_incoming *inc, struct iov_iter *to);
900 void rds_message_addref(struct rds_message *rm);
901 void rds_message_put(struct rds_message *rm);
902 void rds_message_wait(struct rds_message *rm);
903 void rds_message_unmapped(struct rds_message *rm);
904 void rds_notify_msg_zcopy_purge(struct rds_msg_zcopy_queue *info);
905 
906 static inline void rds_message_make_checksum(struct rds_header *hdr)
907 {
908 	hdr->h_csum = 0;
909 	hdr->h_csum = ip_fast_csum((void *) hdr, sizeof(*hdr) >> 2);
910 }
911 
912 static inline int rds_message_verify_checksum(const struct rds_header *hdr)
913 {
914 	return !hdr->h_csum || ip_fast_csum((void *) hdr, sizeof(*hdr) >> 2) == 0;
915 }
916 
917 
918 /* page.c */
919 int rds_page_remainder_alloc(struct scatterlist *scat, unsigned long bytes,
920 			     gfp_t gfp);
921 void rds_page_exit(void);
922 
923 /* recv.c */
924 void rds_inc_init(struct rds_incoming *inc, struct rds_connection *conn,
925 		  struct in6_addr *saddr);
926 void rds_inc_path_init(struct rds_incoming *inc, struct rds_conn_path *conn,
927 		       struct in6_addr *saddr);
928 void rds_inc_put(struct rds_incoming *inc);
929 void rds_recv_incoming(struct rds_connection *conn, struct in6_addr *saddr,
930 		       struct in6_addr *daddr,
931 		       struct rds_incoming *inc, gfp_t gfp);
932 int rds_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
933 		int msg_flags);
934 void rds_clear_recv_queue(struct rds_sock *rs);
935 int rds_notify_queue_get(struct rds_sock *rs, struct msghdr *msg);
936 void rds_inc_info_copy(struct rds_incoming *inc,
937 		       struct rds_info_iterator *iter,
938 		       __be32 saddr, __be32 daddr, int flip);
939 void rds6_inc_info_copy(struct rds_incoming *inc,
940 			struct rds_info_iterator *iter,
941 			struct in6_addr *saddr, struct in6_addr *daddr,
942 			int flip);
943 
944 /* send.c */
945 int rds_sendmsg(struct socket *sock, struct msghdr *msg, size_t payload_len);
946 void rds_send_path_reset(struct rds_conn_path *conn);
947 int rds_send_xmit(struct rds_conn_path *cp);
948 struct sockaddr_in;
949 void rds_send_drop_to(struct rds_sock *rs, struct sockaddr_in6 *dest);
950 typedef int (*is_acked_func)(struct rds_message *rm, uint64_t ack);
951 void rds_send_drop_acked(struct rds_connection *conn, u64 ack,
952 			 is_acked_func is_acked);
953 void rds_send_path_drop_acked(struct rds_conn_path *cp, u64 ack,
954 			      is_acked_func is_acked);
955 void rds_send_ping(struct rds_connection *conn, int cp_index);
956 int rds_send_pong(struct rds_conn_path *cp, __be16 dport);
957 
958 /* rdma.c */
959 void rds_rdma_unuse(struct rds_sock *rs, u32 r_key, int force);
960 int rds_get_mr(struct rds_sock *rs, sockptr_t optval, int optlen);
961 int rds_get_mr_for_dest(struct rds_sock *rs, sockptr_t optval, int optlen);
962 int rds_free_mr(struct rds_sock *rs, sockptr_t optval, int optlen);
963 void rds_rdma_drop_keys(struct rds_sock *rs);
964 int rds_rdma_extra_size(struct rds_rdma_args *args,
965 			struct rds_iov_vector *iov);
966 int rds_cmsg_rdma_dest(struct rds_sock *rs, struct rds_message *rm,
967 			  struct cmsghdr *cmsg);
968 int rds_cmsg_rdma_args(struct rds_sock *rs, struct rds_message *rm,
969 			  struct cmsghdr *cmsg,
970 			  struct rds_iov_vector *vec);
971 int rds_cmsg_rdma_map(struct rds_sock *rs, struct rds_message *rm,
972 			  struct cmsghdr *cmsg);
973 void rds_rdma_free_op(struct rm_rdma_op *ro);
974 void rds_atomic_free_op(struct rm_atomic_op *ao);
975 void rds_rdma_send_complete(struct rds_message *rm, int wc_status);
976 void rds_atomic_send_complete(struct rds_message *rm, int wc_status);
977 int rds_cmsg_atomic(struct rds_sock *rs, struct rds_message *rm,
978 		    struct cmsghdr *cmsg);
979 
980 void __rds_put_mr_final(struct kref *kref);
981 
982 static inline bool rds_destroy_pending(struct rds_connection *conn)
983 {
984 	return !check_net(rds_conn_net(conn)) ||
985 	       (conn->c_trans->t_unloading && conn->c_trans->t_unloading(conn));
986 }
987 
988 enum {
989 	ODP_NOT_NEEDED,
990 	ODP_ZEROBASED,
991 	ODP_VIRTUAL
992 };
993 
994 /* stats.c */
995 DECLARE_PER_CPU_SHARED_ALIGNED(struct rds_statistics, rds_stats);
996 #define rds_stats_inc_which(which, member) do {		\
997 	per_cpu(which, get_cpu()).member++;		\
998 	put_cpu();					\
999 } while (0)
1000 #define rds_stats_inc(member) rds_stats_inc_which(rds_stats, member)
1001 #define rds_stats_add_which(which, member, count) do {		\
1002 	per_cpu(which, get_cpu()).member += count;	\
1003 	put_cpu();					\
1004 } while (0)
1005 #define rds_stats_add(member, count) rds_stats_add_which(rds_stats, member, count)
1006 int rds_stats_init(void);
1007 void rds_stats_exit(void);
1008 void rds_stats_info_copy(struct rds_info_iterator *iter,
1009 			 uint64_t *values, const char *const *names,
1010 			 size_t nr);
1011 
1012 /* sysctl.c */
1013 int rds_sysctl_init(void);
1014 void rds_sysctl_exit(void);
1015 extern unsigned long rds_sysctl_sndbuf_min;
1016 extern unsigned long rds_sysctl_sndbuf_default;
1017 extern unsigned long rds_sysctl_sndbuf_max;
1018 extern unsigned long rds_sysctl_reconnect_min_jiffies;
1019 extern unsigned long rds_sysctl_reconnect_max_jiffies;
1020 extern unsigned int  rds_sysctl_max_unacked_packets;
1021 extern unsigned int  rds_sysctl_max_unacked_bytes;
1022 extern unsigned int  rds_sysctl_ping_enable;
1023 extern unsigned long rds_sysctl_trace_flags;
1024 extern unsigned int  rds_sysctl_trace_level;
1025 
1026 /* threads.c */
1027 int rds_threads_init(void);
1028 void rds_threads_exit(void);
1029 extern struct workqueue_struct *rds_wq;
1030 void rds_queue_reconnect(struct rds_conn_path *cp);
1031 void rds_connect_worker(struct work_struct *);
1032 void rds_shutdown_worker(struct work_struct *);
1033 void rds_send_worker(struct work_struct *);
1034 void rds_recv_worker(struct work_struct *);
1035 void rds_connect_path_complete(struct rds_conn_path *conn, int curr);
1036 void rds_connect_complete(struct rds_connection *conn);
1037 int rds_addr_cmp(const struct in6_addr *a1, const struct in6_addr *a2);
1038 
1039 /* transport.c */
1040 void rds_trans_register(struct rds_transport *trans);
1041 void rds_trans_unregister(struct rds_transport *trans);
1042 struct rds_transport *rds_trans_get_preferred(struct net *net,
1043 					      const struct in6_addr *addr,
1044 					      __u32 scope_id);
1045 void rds_trans_put(struct rds_transport *trans);
1046 unsigned int rds_trans_stats_info_copy(struct rds_info_iterator *iter,
1047 				       unsigned int avail);
1048 struct rds_transport *rds_trans_get(int t_type);
1049 
1050 #endif
1051