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)
rdsdebug(char * fmt,...)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
rds_conn_net(struct rds_connection * conn)178 struct net *rds_conn_net(struct rds_connection *conn)
179 {
180 return read_pnet(&conn->c_net);
181 }
182
183 static inline
rds_conn_net_set(struct rds_connection * conn,struct net * net)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; /* socket that owns us; counted
324 * reference, dropped by
325 * __rds_put_mr_final()
326 */
327 struct rds_transport *r_trans;
328 void *r_trans_private;
329 };
330
rds_rdma_make_cookie(u32 r_key,u32 offset)331 static inline rds_rdma_cookie_t rds_rdma_make_cookie(u32 r_key, u32 offset)
332 {
333 return r_key | (((u64) offset) << 32);
334 }
335
rds_rdma_cookie_key(rds_rdma_cookie_t cookie)336 static inline u32 rds_rdma_cookie_key(rds_rdma_cookie_t cookie)
337 {
338 return cookie;
339 }
340
rds_rdma_cookie_offset(rds_rdma_cookie_t cookie)341 static inline u32 rds_rdma_cookie_offset(rds_rdma_cookie_t cookie)
342 {
343 return cookie >> 32;
344 }
345
346 /* atomic operation types */
347 #define RDS_ATOMIC_TYPE_CSWP 0
348 #define RDS_ATOMIC_TYPE_FADD 1
349
350 /*
351 * m_sock_item and m_conn_item are on lists that are serialized under
352 * conn->c_lock. m_sock_item has additional meaning in that once it is empty
353 * the message will not be put back on the retransmit list after being sent.
354 * messages that are canceled while being sent rely on this.
355 *
356 * m_inc is used by loopback so that it can pass an incoming message straight
357 * back up into the rx path. It embeds a wire header which is also used by
358 * the send path, which is kind of awkward.
359 *
360 * m_sock_item indicates the message's presence on a socket's send or receive
361 * queue. m_rs will point to that socket.
362 *
363 * m_daddr is used by cancellation to prune messages to a given destination.
364 *
365 * The RDS_MSG_ON_SOCK and RDS_MSG_ON_CONN flags are used to avoid lock
366 * nesting. As paths iterate over messages on a sock, or conn, they must
367 * also lock the conn, or sock, to remove the message from those lists too.
368 * Testing the flag to determine if the message is still on the lists lets
369 * us avoid testing the list_head directly. That means each path can use
370 * the message's list_head to keep it on a local list while juggling locks
371 * without confusing the other path.
372 *
373 * m_ack_seq is an optional field set by transports who need a different
374 * sequence number range to invalidate. They can use this in a callback
375 * that they pass to rds_send_drop_acked() to see if each message has been
376 * acked. The HAS_ACK_SEQ flag can be used to detect messages which haven't
377 * had ack_seq set yet.
378 */
379 #define RDS_MSG_ON_SOCK 1
380 #define RDS_MSG_ON_CONN 2
381 #define RDS_MSG_HAS_ACK_SEQ 3
382 #define RDS_MSG_ACK_REQUIRED 4
383 #define RDS_MSG_RETRANSMITTED 5
384 #define RDS_MSG_MAPPED 6
385 #define RDS_MSG_PAGEVEC 7
386 #define RDS_MSG_FLUSH 8
387
388 struct rds_znotifier {
389 struct mmpin z_mmp;
390 u32 z_cookie;
391 };
392
393 struct rds_msg_zcopy_info {
394 struct list_head rs_zcookie_next;
395 union {
396 struct rds_znotifier znotif;
397 struct rds_zcopy_cookies zcookies;
398 };
399 };
400
401 struct rds_msg_zcopy_queue {
402 struct list_head zcookie_head;
403 spinlock_t lock; /* protects zcookie_head queue */
404 };
405
rds_message_zcopy_queue_init(struct rds_msg_zcopy_queue * q)406 static inline void rds_message_zcopy_queue_init(struct rds_msg_zcopy_queue *q)
407 {
408 spin_lock_init(&q->lock);
409 INIT_LIST_HEAD(&q->zcookie_head);
410 }
411
412 struct rds_iov_vector {
413 struct rds_iovec *iov;
414 int len;
415 };
416
417 struct rds_iov_vector_arr {
418 struct rds_iov_vector *vec;
419 int len;
420 int indx;
421 int incr;
422 };
423
424 struct rds_message {
425 refcount_t m_refcount;
426 struct list_head m_sock_item;
427 struct list_head m_conn_item;
428 struct rds_incoming m_inc;
429 u64 m_ack_seq;
430 struct in6_addr m_daddr;
431 unsigned long m_flags;
432
433 /* Never access m_rs without holding m_rs_lock.
434 * Lock nesting is
435 * rm->m_rs_lock
436 * -> rs->rs_lock
437 */
438 spinlock_t m_rs_lock;
439 wait_queue_head_t m_flush_wait;
440
441 struct rds_sock *m_rs;
442
443 /* cookie to send to remote, in rds header */
444 rds_rdma_cookie_t m_rdma_cookie;
445
446 unsigned int m_used_sgs;
447 unsigned int m_total_sgs;
448
449 void *m_final_op;
450
451 /* Unpins the ops' user pages and frees the message from
452 * process context when the final put happens in atomic
453 * context: dirtying the pages on unpin can sleep.
454 */
455 struct work_struct m_unpin_work;
456
457 struct {
458 struct rm_atomic_op {
459 int op_type;
460 union {
461 struct {
462 uint64_t compare;
463 uint64_t swap;
464 uint64_t compare_mask;
465 uint64_t swap_mask;
466 } op_m_cswp;
467 struct {
468 uint64_t add;
469 uint64_t nocarry_mask;
470 } op_m_fadd;
471 };
472
473 u32 op_rkey;
474 u64 op_remote_addr;
475 unsigned int op_notify:1;
476 unsigned int op_recverr:1;
477 unsigned int op_mapped:1;
478 unsigned int op_silent:1;
479 unsigned int op_active:1;
480 unsigned int op_unpin_deferred:1;
481 struct scatterlist *op_sg;
482 struct rds_notifier *op_notifier;
483
484 struct rds_mr *op_rdma_mr;
485 } atomic;
486 struct rm_rdma_op {
487 u32 op_rkey;
488 u64 op_remote_addr;
489 unsigned int op_write:1;
490 unsigned int op_fence:1;
491 unsigned int op_notify:1;
492 unsigned int op_recverr:1;
493 unsigned int op_mapped:1;
494 unsigned int op_silent:1;
495 unsigned int op_active:1;
496 unsigned int op_unpin_deferred:1;
497 unsigned int op_bytes;
498 unsigned int op_nents;
499 unsigned int op_count;
500 struct scatterlist *op_sg;
501 struct rds_notifier *op_notifier;
502
503 struct rds_mr *op_rdma_mr;
504
505 u64 op_odp_addr;
506 struct rds_mr *op_odp_mr;
507 } rdma;
508 struct rm_data_op {
509 unsigned int op_active:1;
510 unsigned int op_nents;
511 unsigned int op_count;
512 unsigned int op_dmasg;
513 unsigned int op_dmaoff;
514 struct rds_znotifier *op_mmp_znotifier;
515 struct scatterlist *op_sg;
516 } data;
517 };
518
519 struct rds_conn_path *m_conn_path;
520 };
521
522 /*
523 * The RDS notifier is used (optionally) to tell the application about
524 * completed RDMA operations. Rather than keeping the whole rds message
525 * around on the queue, we allocate a small notifier that is put on the
526 * socket's notifier_list. Notifications are delivered to the application
527 * through control messages.
528 */
529 struct rds_notifier {
530 struct list_head n_list;
531 uint64_t n_user_token;
532 int n_status;
533 };
534
535 /* Available as part of RDS core, so doesn't need to participate
536 * in get_preferred transport etc
537 */
538 #define RDS_TRANS_LOOP 3
539
540 struct rds_transport {
541 char t_name[TRANSNAMSIZ];
542 struct list_head t_item;
543 struct module *t_owner;
544 unsigned int t_prefer_loopback:1,
545 t_mp_capable:1;
546 unsigned int t_type;
547
548 int (*laddr_check)(struct net *net, const struct in6_addr *addr,
549 __u32 scope_id);
550 int (*conn_alloc)(struct rds_connection *conn, gfp_t gfp);
551 void (*conn_free)(void *data);
552
553 /*
554 * conn_slots_available is invoked when a previously unavailable
555 * connection slot becomes available again. rds_tcp_accept_one_path may
556 * return -ENOBUFS if it cannot find an available slot, and then stashes
557 * the new socket in "rds_tcp_accepted_sock". This function re-issues
558 * `rds_tcp_accept_one_path`, which picks up the stashed socket and
559 * continuing where it left with "-ENOBUFS" last time. This ensures
560 * messages received on the new socket are not discarded when no
561 * connection path was available at the time.
562 */
563 void (*conn_slots_available)(struct rds_connection *conn, bool fan_out);
564 int (*conn_path_connect)(struct rds_conn_path *cp);
565
566 /*
567 * conn_shutdown stops traffic on the given connection. Once
568 * it returns the connection can not call rds_recv_incoming().
569 * This will only be called once after conn_connect returns
570 * non-zero success and will The caller serializes this with
571 * the send and connecting paths (xmit_* and conn_*). The
572 * transport is responsible for other serialization, including
573 * rds_recv_incoming(). This is called in process context but
574 * should try hard not to block.
575 */
576 void (*conn_path_shutdown)(struct rds_conn_path *conn);
577 void (*xmit_path_prepare)(struct rds_conn_path *cp);
578 void (*xmit_path_complete)(struct rds_conn_path *cp);
579
580 /*
581 * .xmit is called by rds_send_xmit() to tell the transport to send
582 * part of a message. The caller serializes on the send_sem so this
583 * doesn't need to be reentrant for a given conn. The header must be
584 * sent before the data payload. .xmit must be prepared to send a
585 * message with no data payload. .xmit should return the number of
586 * bytes that were sent down the connection, including header bytes.
587 * Returning 0 tells the caller that it doesn't need to perform any
588 * additional work now. This is usually the case when the transport has
589 * filled the sending queue for its connection and will handle
590 * triggering the rds thread to continue the send when space becomes
591 * available. Returning -EAGAIN tells the caller to retry the send
592 * immediately. Returning -ENOMEM tells the caller to retry the send at
593 * some point in the future.
594 */
595 int (*xmit)(struct rds_connection *conn, struct rds_message *rm,
596 unsigned int hdr_off, unsigned int sg, unsigned int off);
597 int (*xmit_rdma)(struct rds_connection *conn, struct rm_rdma_op *op);
598 int (*xmit_atomic)(struct rds_connection *conn, struct rm_atomic_op *op);
599 int (*recv_path)(struct rds_conn_path *cp);
600 int (*inc_copy_to_user)(struct rds_incoming *inc, struct iov_iter *to);
601 void (*inc_free)(struct rds_incoming *inc);
602
603 int (*cm_handle_connect)(struct rdma_cm_id *cm_id,
604 struct rdma_cm_event *event, bool isv6);
605 int (*cm_initiate_connect)(struct rdma_cm_id *cm_id, bool isv6);
606 void (*cm_connect_complete)(struct rds_connection *conn,
607 struct rdma_cm_event *event);
608
609 unsigned int (*stats_info_copy)(struct rds_info_iterator *iter,
610 unsigned int avail);
611 void (*exit)(void);
612 void *(*get_mr)(struct scatterlist *sg, unsigned long nr_sg,
613 struct rds_sock *rs, u32 *key_ret,
614 struct rds_connection *conn,
615 u64 start, u64 length, int need_odp);
616 void (*sync_mr)(void *trans_private, int direction);
617 void (*free_mr)(void *trans_private, int invalidate);
618 void (*flush_mrs)(void);
619 bool (*t_unloading)(struct rds_connection *conn);
620 u8 (*get_tos_map)(u8 tos);
621 };
622
623 /* Bind hash table key length. It is the sum of the size of a struct
624 * in6_addr, a scope_id and a port.
625 */
626 #define RDS_BOUND_KEY_LEN \
627 (sizeof(struct in6_addr) + sizeof(__u32) + sizeof(__be16))
628
629 struct rds_sock {
630 struct sock rs_sk;
631
632 u64 rs_user_addr;
633 u64 rs_user_bytes;
634
635 /*
636 * bound_addr used for both incoming and outgoing, no INADDR_ANY
637 * support.
638 */
639 struct rhash_head rs_bound_node;
640 u8 rs_bound_key[RDS_BOUND_KEY_LEN];
641 struct sockaddr_in6 rs_bound_sin6;
642 #define rs_bound_addr rs_bound_sin6.sin6_addr
643 #define rs_bound_addr_v4 rs_bound_sin6.sin6_addr.s6_addr32[3]
644 #define rs_bound_port rs_bound_sin6.sin6_port
645 #define rs_bound_scope_id rs_bound_sin6.sin6_scope_id
646 struct in6_addr rs_conn_addr;
647 #define rs_conn_addr_v4 rs_conn_addr.s6_addr32[3]
648 __be16 rs_conn_port;
649 struct rds_transport *rs_transport;
650
651 /*
652 * rds_sendmsg caches the conn it used the last time around.
653 * This helps avoid costly lookups.
654 */
655 struct rds_connection *rs_conn;
656
657 /* flag indicating we were congested or not */
658 int rs_congested;
659 /* seen congestion (ENOBUFS) when sending? */
660 int rs_seen_congestion;
661
662 /* rs_lock protects all these adjacent members before the newline */
663 spinlock_t rs_lock;
664 struct list_head rs_send_queue;
665 u32 rs_snd_bytes;
666 int rs_rcv_bytes;
667 struct list_head rs_notify_queue; /* currently used for failed RDMAs */
668
669 /* Congestion wake_up. If rs_cong_monitor is set, we use cong_mask
670 * to decide whether the application should be woken up.
671 * If not set, we use rs_cong_track to find out whether a cong map
672 * update arrived.
673 */
674 uint64_t rs_cong_mask;
675 uint64_t rs_cong_notify;
676 struct list_head rs_cong_list;
677 unsigned long rs_cong_track;
678
679 /*
680 * rs_recv_lock protects the receive queue, and is
681 * used to serialize with rds_release.
682 */
683 rwlock_t rs_recv_lock;
684 struct list_head rs_recv_queue;
685
686 /* just for stats reporting */
687 struct list_head rs_item;
688
689 /* these have their own lock */
690 spinlock_t rs_rdma_lock;
691 struct rb_root rs_rdma_keys;
692
693 /* Socket options - in case there will be more */
694 unsigned char rs_recverr,
695 rs_cong_monitor;
696 u32 rs_hash_initval;
697
698 /* Socket receive path trace points*/
699 u8 rs_rx_traces;
700 u8 rs_rx_trace[RDS_MSG_RX_DGRAM_TRACE_MAX];
701 struct rds_msg_zcopy_queue rs_zcookie_queue;
702 u8 rs_tos;
703 };
704
rds_sk_to_rs(const struct sock * sk)705 static inline struct rds_sock *rds_sk_to_rs(const struct sock *sk)
706 {
707 return container_of(sk, struct rds_sock, rs_sk);
708 }
rds_rs_to_sk(struct rds_sock * rs)709 static inline struct sock *rds_rs_to_sk(struct rds_sock *rs)
710 {
711 return &rs->rs_sk;
712 }
713
714 /*
715 * The stack assigns sk_sndbuf and sk_rcvbuf to twice the specified value
716 * to account for overhead. We don't account for overhead, we just apply
717 * the number of payload bytes to the specified value.
718 */
rds_sk_sndbuf(struct rds_sock * rs)719 static inline int rds_sk_sndbuf(struct rds_sock *rs)
720 {
721 return rds_rs_to_sk(rs)->sk_sndbuf / 2;
722 }
rds_sk_rcvbuf(struct rds_sock * rs)723 static inline int rds_sk_rcvbuf(struct rds_sock *rs)
724 {
725 return rds_rs_to_sk(rs)->sk_rcvbuf / 2;
726 }
727
728 struct rds_statistics {
729 u64 s_conn_reset;
730 u64 s_recv_drop_bad_checksum;
731 u64 s_recv_drop_old_seq;
732 u64 s_recv_drop_no_sock;
733 u64 s_recv_drop_dead_sock;
734 u64 s_recv_deliver_raced;
735 u64 s_recv_delivered;
736 u64 s_recv_queued;
737 u64 s_recv_immediate_retry;
738 u64 s_recv_delayed_retry;
739 u64 s_recv_ack_required;
740 u64 s_recv_rdma_bytes;
741 u64 s_recv_ping;
742 u64 s_send_queue_empty;
743 u64 s_send_queue_full;
744 u64 s_send_lock_contention;
745 u64 s_send_lock_queue_raced;
746 u64 s_send_immediate_retry;
747 u64 s_send_delayed_retry;
748 u64 s_send_drop_acked;
749 u64 s_send_ack_required;
750 u64 s_send_queued;
751 u64 s_send_rdma;
752 u64 s_send_rdma_bytes;
753 u64 s_send_pong;
754 u64 s_page_remainder_hit;
755 u64 s_page_remainder_miss;
756 u64 s_copy_to_user;
757 u64 s_copy_from_user;
758 u64 s_cong_update_queued;
759 u64 s_cong_update_received;
760 u64 s_cong_send_error;
761 u64 s_cong_send_blocked;
762 u64 s_recv_bytes_added_to_socket;
763 u64 s_recv_bytes_removed_from_socket;
764 u64 s_send_stuck_rm;
765 u64 s_mprds_catchup_tx0_retries;
766 };
767
768 /* af_rds.c */
769 void rds_sock_addref(struct rds_sock *rs);
770 void rds_sock_put(struct rds_sock *rs);
771 void rds_wake_sk_sleep(struct rds_sock *rs);
__rds_wake_sk_sleep(struct sock * sk)772 static inline void __rds_wake_sk_sleep(struct sock *sk)
773 {
774 wait_queue_head_t *waitq = sk_sleep(sk);
775
776 if (!sock_flag(sk, SOCK_DEAD) && waitq)
777 wake_up(waitq);
778 }
779 extern wait_queue_head_t rds_poll_waitq;
780
781
782 /* bind.c */
783 int rds_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int addr_len);
784 void rds_remove_bound(struct rds_sock *rs);
785 struct rds_sock *rds_find_bound(const struct in6_addr *addr, __be16 port,
786 __u32 scope_id);
787 int rds_bind_lock_init(void);
788 void rds_bind_lock_destroy(void);
789
790 /* cong.c */
791 int rds_cong_get_maps(struct rds_connection *conn);
792 void rds_cong_add_conn(struct rds_connection *conn);
793 void rds_cong_remove_conn(struct rds_connection *conn);
794 void rds_cong_set_bit(struct rds_cong_map *map, __be16 port);
795 void rds_cong_clear_bit(struct rds_cong_map *map, __be16 port);
796 int rds_cong_wait(struct rds_cong_map *map, __be16 port, int nonblock, struct rds_sock *rs);
797 void rds_cong_queue_updates(struct rds_cong_map *map);
798 void rds_cong_map_updated(struct rds_cong_map *map, uint64_t);
799 int rds_cong_updated_since(unsigned long *recent);
800 void rds_cong_add_socket(struct rds_sock *);
801 void rds_cong_remove_socket(struct rds_sock *);
802 void rds_cong_exit(void);
803 struct rds_message *rds_cong_update_alloc(struct rds_connection *conn);
804
805 /* connection.c */
806 extern u32 rds_gen_num;
807 int rds_conn_init(void);
808 void rds_conn_exit(void);
809 struct rds_connection *rds_conn_create(struct net *net,
810 const struct in6_addr *laddr,
811 const struct in6_addr *faddr,
812 struct rds_transport *trans,
813 u8 tos, gfp_t gfp,
814 int dev_if);
815 struct rds_connection *rds_conn_create_outgoing(struct net *net,
816 const struct in6_addr *laddr,
817 const struct in6_addr *faddr,
818 struct rds_transport *trans,
819 u8 tos, gfp_t gfp, int dev_if);
820 void rds_conn_shutdown(struct rds_conn_path *cpath);
821 void rds_conn_destroy(struct rds_connection *conn);
822 void rds_conn_drop(struct rds_connection *conn);
823 void rds_conn_path_drop(struct rds_conn_path *cpath, bool destroy);
824 void rds_conn_connect_if_down(struct rds_connection *conn);
825 void rds_conn_path_connect_if_down(struct rds_conn_path *cp);
826 void rds_check_all_paths(struct rds_connection *conn);
827 void rds_for_each_conn_info(struct socket *sock, unsigned int len,
828 struct rds_info_iterator *iter,
829 struct rds_info_lengths *lens,
830 int (*visitor)(struct rds_connection *, void *),
831 u64 *buffer,
832 size_t item_len);
833
834 __printf(2, 3)
835 void __rds_conn_path_error(struct rds_conn_path *cp, const char *, ...);
836 #define rds_conn_path_error(cp, fmt...) \
837 __rds_conn_path_error(cp, KERN_WARNING "RDS: " fmt)
838
839 static inline int
rds_conn_path_transition(struct rds_conn_path * cp,int old,int new)840 rds_conn_path_transition(struct rds_conn_path *cp, int old, int new)
841 {
842 return atomic_cmpxchg(&cp->cp_state, old, new) == old;
843 }
844
845 static inline int
rds_conn_transition(struct rds_connection * conn,int old,int new)846 rds_conn_transition(struct rds_connection *conn, int old, int new)
847 {
848 WARN_ON(conn->c_trans->t_mp_capable);
849 return rds_conn_path_transition(&conn->c_path[0], old, new);
850 }
851
852 static inline int
rds_conn_path_state(struct rds_conn_path * cp)853 rds_conn_path_state(struct rds_conn_path *cp)
854 {
855 return atomic_read(&cp->cp_state);
856 }
857
858 static inline int
rds_conn_state(struct rds_connection * conn)859 rds_conn_state(struct rds_connection *conn)
860 {
861 WARN_ON(conn->c_trans->t_mp_capable);
862 return rds_conn_path_state(&conn->c_path[0]);
863 }
864
865 static inline int
rds_conn_path_up(struct rds_conn_path * cp)866 rds_conn_path_up(struct rds_conn_path *cp)
867 {
868 return atomic_read(&cp->cp_state) == RDS_CONN_UP;
869 }
870
871 static inline int
rds_conn_path_down(struct rds_conn_path * cp)872 rds_conn_path_down(struct rds_conn_path *cp)
873 {
874 return atomic_read(&cp->cp_state) == RDS_CONN_DOWN;
875 }
876
877 static inline int
rds_conn_up(struct rds_connection * conn)878 rds_conn_up(struct rds_connection *conn)
879 {
880 WARN_ON(conn->c_trans->t_mp_capable);
881 return rds_conn_path_up(&conn->c_path[0]);
882 }
883
884 static inline int
rds_conn_path_connecting(struct rds_conn_path * cp)885 rds_conn_path_connecting(struct rds_conn_path *cp)
886 {
887 return atomic_read(&cp->cp_state) == RDS_CONN_CONNECTING;
888 }
889
890 static inline int
rds_conn_connecting(struct rds_connection * conn)891 rds_conn_connecting(struct rds_connection *conn)
892 {
893 WARN_ON(conn->c_trans->t_mp_capable);
894 return rds_conn_path_connecting(&conn->c_path[0]);
895 }
896
897 /* message.c */
898 struct rds_message *rds_message_alloc(unsigned int nents, gfp_t gfp);
899 struct scatterlist *rds_message_alloc_sgs(struct rds_message *rm, int nents);
900 int rds_message_copy_from_user(struct rds_message *rm, struct iov_iter *from,
901 bool zcopy);
902 struct rds_message *rds_message_map_pages(unsigned long *page_addrs, unsigned int total_len);
903 void rds_message_populate_header(struct rds_header *hdr, __be16 sport,
904 __be16 dport, u64 seq);
905 int rds_message_add_extension(struct rds_header *hdr,
906 unsigned int type, const void *data);
907 int rds_message_next_extension(struct rds_header *hdr,
908 unsigned int *pos, void *buf, unsigned int *buflen);
909 int rds_message_add_rdma_dest_extension(struct rds_header *hdr, u32 r_key, u32 offset);
910 int rds_message_inc_copy_to_user(struct rds_incoming *inc, struct iov_iter *to);
911 void rds_message_addref(struct rds_message *rm);
912 void rds_message_put(struct rds_message *rm);
913 void rds_message_wait(struct rds_message *rm);
914 void rds_message_unmapped(struct rds_message *rm);
915 void rds_notify_msg_zcopy_purge(struct rds_msg_zcopy_queue *info);
916
rds_message_make_checksum(struct rds_header * hdr)917 static inline void rds_message_make_checksum(struct rds_header *hdr)
918 {
919 hdr->h_csum = 0;
920 hdr->h_csum = ip_fast_csum((void *) hdr, sizeof(*hdr) >> 2);
921 }
922
rds_message_verify_checksum(const struct rds_header * hdr)923 static inline int rds_message_verify_checksum(const struct rds_header *hdr)
924 {
925 return !hdr->h_csum || ip_fast_csum((void *) hdr, sizeof(*hdr) >> 2) == 0;
926 }
927
928
929 /* page.c */
930 int rds_page_remainder_alloc(struct scatterlist *scat, unsigned long bytes,
931 gfp_t gfp);
932 void rds_page_exit(void);
933
934 /* recv.c */
935 void rds_inc_init(struct rds_incoming *inc, struct rds_connection *conn,
936 struct in6_addr *saddr);
937 void rds_inc_path_init(struct rds_incoming *inc, struct rds_conn_path *conn,
938 struct in6_addr *saddr);
939 void rds_inc_put(struct rds_incoming *inc);
940 void rds_recv_incoming(struct rds_connection *conn, struct in6_addr *saddr,
941 struct in6_addr *daddr,
942 struct rds_incoming *inc, gfp_t gfp);
943 int rds_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
944 int msg_flags);
945 void rds_clear_recv_queue(struct rds_sock *rs);
946 int rds_notify_queue_get(struct rds_sock *rs, struct msghdr *msg);
947 void rds_inc_info_copy(struct rds_incoming *inc,
948 struct rds_info_iterator *iter,
949 __be32 saddr, __be32 daddr, int flip);
950 void rds6_inc_info_copy(struct rds_incoming *inc,
951 struct rds_info_iterator *iter,
952 struct in6_addr *saddr, struct in6_addr *daddr,
953 int flip);
954
955 /* send.c */
956 int rds_sendmsg(struct socket *sock, struct msghdr *msg, size_t payload_len);
957 void rds_send_path_reset(struct rds_conn_path *conn);
958 int rds_send_xmit(struct rds_conn_path *cp);
959 struct sockaddr_in;
960 void rds_send_drop_to(struct rds_sock *rs, struct sockaddr_in6 *dest);
961 typedef int (*is_acked_func)(struct rds_message *rm, uint64_t ack);
962 void rds_send_drop_acked(struct rds_connection *conn, u64 ack,
963 is_acked_func is_acked);
964 void rds_send_path_drop_acked(struct rds_conn_path *cp, u64 ack,
965 is_acked_func is_acked);
966 void rds_send_ping(struct rds_connection *conn, int cp_index);
967 int rds_send_pong(struct rds_conn_path *cp, __be16 dport);
968
969 /* rdma.c */
970 void rds_rdma_unuse(struct rds_sock *rs, u32 r_key, int force);
971 int rds_get_mr(struct rds_sock *rs, sockptr_t optval, int optlen);
972 int rds_get_mr_for_dest(struct rds_sock *rs, sockptr_t optval, int optlen);
973 int rds_free_mr(struct rds_sock *rs, sockptr_t optval, int optlen);
974 void rds_rdma_drop_keys(struct rds_sock *rs);
975 int rds_rdma_extra_size(struct rds_rdma_args *args,
976 struct rds_iov_vector *iov);
977 int rds_cmsg_rdma_dest(struct rds_sock *rs, struct rds_message *rm,
978 struct cmsghdr *cmsg);
979 int rds_cmsg_rdma_args(struct rds_sock *rs, struct rds_message *rm,
980 struct cmsghdr *cmsg,
981 struct rds_iov_vector *vec);
982 int rds_cmsg_rdma_map(struct rds_sock *rs, struct rds_message *rm,
983 struct cmsghdr *cmsg);
984 void rds_rdma_free_op(struct rm_rdma_op *ro);
985 void rds_atomic_free_op(struct rm_atomic_op *ao);
986 void rds_rdma_op_unpin_pages(struct rm_rdma_op *ro);
987 void rds_atomic_op_unpin_page(struct rm_atomic_op *ao);
988 void rds_rdma_send_complete(struct rds_message *rm, int wc_status);
989 void rds_atomic_send_complete(struct rds_message *rm, int wc_status);
990 int rds_cmsg_atomic(struct rds_sock *rs, struct rds_message *rm,
991 struct cmsghdr *cmsg);
992
993 void __rds_put_mr_final(struct kref *kref);
994
rds_destroy_pending(struct rds_connection * conn)995 static inline bool rds_destroy_pending(struct rds_connection *conn)
996 {
997 return !check_net(rds_conn_net(conn)) ||
998 (conn->c_trans->t_unloading && conn->c_trans->t_unloading(conn));
999 }
1000
1001 enum {
1002 ODP_NOT_NEEDED,
1003 ODP_ZEROBASED,
1004 ODP_VIRTUAL
1005 };
1006
1007 /* stats.c */
1008 DECLARE_PER_CPU_SHARED_ALIGNED(struct rds_statistics, rds_stats);
1009 #define rds_stats_inc_which(which, member) do { \
1010 per_cpu(which, get_cpu()).member++; \
1011 put_cpu(); \
1012 } while (0)
1013 #define rds_stats_inc(member) rds_stats_inc_which(rds_stats, member)
1014 #define rds_stats_add_which(which, member, count) do { \
1015 per_cpu(which, get_cpu()).member += count; \
1016 put_cpu(); \
1017 } while (0)
1018 #define rds_stats_add(member, count) rds_stats_add_which(rds_stats, member, count)
1019 int rds_stats_init(void);
1020 void rds_stats_exit(void);
1021 void rds_stats_info_copy(struct rds_info_iterator *iter,
1022 uint64_t *values, const char *const *names,
1023 size_t nr);
1024
1025 /* sysctl.c */
1026 int rds_sysctl_init(void);
1027 void rds_sysctl_exit(void);
1028 extern unsigned long rds_sysctl_sndbuf_min;
1029 extern unsigned long rds_sysctl_sndbuf_default;
1030 extern unsigned long rds_sysctl_sndbuf_max;
1031 extern unsigned long rds_sysctl_reconnect_min_jiffies;
1032 extern unsigned long rds_sysctl_reconnect_max_jiffies;
1033 extern unsigned int rds_sysctl_max_unacked_packets;
1034 extern unsigned int rds_sysctl_max_unacked_bytes;
1035 extern unsigned int rds_sysctl_ping_enable;
1036 extern unsigned long rds_sysctl_trace_flags;
1037 extern unsigned int rds_sysctl_trace_level;
1038
1039 /* threads.c */
1040 int rds_threads_init(void);
1041 void rds_threads_exit(void);
1042 extern struct workqueue_struct *rds_wq;
1043 void rds_queue_reconnect(struct rds_conn_path *cp);
1044 void rds_connect_worker(struct work_struct *);
1045 void rds_shutdown_worker(struct work_struct *);
1046 void rds_send_worker(struct work_struct *);
1047 void rds_recv_worker(struct work_struct *);
1048 void rds_connect_path_complete(struct rds_conn_path *conn, int curr);
1049 void rds_connect_complete(struct rds_connection *conn);
1050 int rds_addr_cmp(const struct in6_addr *a1, const struct in6_addr *a2);
1051
1052 /* transport.c */
1053 void rds_trans_register(struct rds_transport *trans);
1054 void rds_trans_unregister(struct rds_transport *trans);
1055 struct rds_transport *rds_trans_get_preferred(struct net *net,
1056 const struct in6_addr *addr,
1057 __u32 scope_id);
1058 void rds_trans_put(struct rds_transport *trans);
1059 unsigned int rds_trans_stats_info_copy(struct rds_info_iterator *iter,
1060 unsigned int avail);
1061 struct rds_transport *rds_trans_get(int t_type);
1062
1063 #endif
1064