xref: /linux-6.15/include/linux/sunrpc/xprt.h (revision e1df5202)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  *  linux/include/linux/sunrpc/xprt.h
4  *
5  *  Declarations for the RPC transport interface.
6  *
7  *  Copyright (C) 1995, 1996 Olaf Kirch <[email protected]>
8  */
9 
10 #ifndef _LINUX_SUNRPC_XPRT_H
11 #define _LINUX_SUNRPC_XPRT_H
12 
13 #include <linux/uio.h>
14 #include <linux/socket.h>
15 #include <linux/in.h>
16 #include <linux/ktime.h>
17 #include <linux/kref.h>
18 #include <linux/sunrpc/sched.h>
19 #include <linux/sunrpc/xdr.h>
20 #include <linux/sunrpc/msg_prot.h>
21 
22 #define RPC_MIN_SLOT_TABLE	(2U)
23 #define RPC_DEF_SLOT_TABLE	(16U)
24 #define RPC_MAX_SLOT_TABLE_LIMIT	(65536U)
25 #define RPC_MAX_SLOT_TABLE	RPC_MAX_SLOT_TABLE_LIMIT
26 
27 #define RPC_CWNDSHIFT		(8U)
28 #define RPC_CWNDSCALE		(1U << RPC_CWNDSHIFT)
29 #define RPC_INITCWND		RPC_CWNDSCALE
30 #define RPC_MAXCWND(xprt)	((xprt)->max_reqs << RPC_CWNDSHIFT)
31 #define RPCXPRT_CONGESTED(xprt) ((xprt)->cong >= (xprt)->cwnd)
32 
33 /*
34  * This describes a timeout strategy
35  */
36 struct rpc_timeout {
37 	unsigned long		to_initval,		/* initial timeout */
38 				to_maxval,		/* max timeout */
39 				to_increment;		/* if !exponential */
40 	unsigned int		to_retries;		/* max # of retries */
41 	unsigned char		to_exponential;
42 };
43 
44 enum rpc_display_format_t {
45 	RPC_DISPLAY_ADDR = 0,
46 	RPC_DISPLAY_PORT,
47 	RPC_DISPLAY_PROTO,
48 	RPC_DISPLAY_HEX_ADDR,
49 	RPC_DISPLAY_HEX_PORT,
50 	RPC_DISPLAY_NETID,
51 	RPC_DISPLAY_MAX,
52 };
53 
54 struct rpc_task;
55 struct rpc_xprt;
56 struct xprt_class;
57 struct seq_file;
58 struct svc_serv;
59 struct net;
60 #include <linux/lwq.h>
61 
62 /*
63  * This describes a complete RPC request
64  */
65 struct rpc_rqst {
66 	/*
67 	 * This is the user-visible part
68 	 */
69 	struct rpc_xprt *	rq_xprt;		/* RPC client */
70 	struct xdr_buf		rq_snd_buf;		/* send buffer */
71 	struct xdr_buf		rq_rcv_buf;		/* recv buffer */
72 
73 	/*
74 	 * This is the private part
75 	 */
76 	struct rpc_task *	rq_task;	/* RPC task data */
77 	struct rpc_cred *	rq_cred;	/* Bound cred */
78 	__be32			rq_xid;		/* request XID */
79 	int			rq_cong;	/* has incremented xprt->cong */
80 	u32			rq_seqno;	/* gss seq no. used on req. */
81 	int			rq_enc_pages_num;
82 	struct page		**rq_enc_pages;	/* scratch pages for use by
83 						   gss privacy code */
84 	void (*rq_release_snd_buf)(struct rpc_rqst *); /* release rq_enc_pages */
85 
86 	union {
87 		struct list_head	rq_list;	/* Slot allocation list */
88 		struct rb_node		rq_recv;	/* Receive queue */
89 	};
90 
91 	struct list_head	rq_xmit;	/* Send queue */
92 	struct list_head	rq_xmit2;	/* Send queue */
93 
94 	void			*rq_buffer;	/* Call XDR encode buffer */
95 	size_t			rq_callsize;
96 	void			*rq_rbuffer;	/* Reply XDR decode buffer */
97 	size_t			rq_rcvsize;
98 	size_t			rq_xmit_bytes_sent;	/* total bytes sent */
99 	size_t			rq_reply_bytes_recvd;	/* total reply bytes */
100 							/* received */
101 
102 	struct xdr_buf		rq_private_buf;		/* The receive buffer
103 							 * used in the softirq.
104 							 */
105 	unsigned long		rq_majortimeo;	/* major timeout alarm */
106 	unsigned long		rq_minortimeo;	/* minor timeout alarm */
107 	unsigned long		rq_timeout;	/* Current timeout value */
108 	ktime_t			rq_rtt;		/* round-trip time */
109 	unsigned int		rq_retries;	/* # of retries */
110 	unsigned int		rq_connect_cookie;
111 						/* A cookie used to track the
112 						   state of the transport
113 						   connection */
114 	atomic_t		rq_pin;
115 
116 	/*
117 	 * Partial send handling
118 	 */
119 	u32			rq_bytes_sent;	/* Bytes we have sent */
120 
121 	ktime_t			rq_xtime;	/* transmit time stamp */
122 	int			rq_ntrans;
123 
124 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
125 	struct lwq_node		rq_bc_list;	/* Callback service list */
126 	unsigned long		rq_bc_pa_state;	/* Backchannel prealloc state */
127 	struct list_head	rq_bc_pa_list;	/* Backchannel prealloc list */
128 #endif /* CONFIG_SUNRPC_BACKCHANEL */
129 };
130 #define rq_svec			rq_snd_buf.head
131 #define rq_slen			rq_snd_buf.len
132 
133 /* RPC transport layer security policies */
134 enum xprtsec_policies {
135 	RPC_XPRTSEC_NONE = 0,
136 	RPC_XPRTSEC_TLS_ANON,
137 	RPC_XPRTSEC_TLS_X509,
138 };
139 
140 struct xprtsec_parms {
141 	enum xprtsec_policies	policy;
142 
143 	/* authentication material */
144 	key_serial_t		cert_serial;
145 	key_serial_t		privkey_serial;
146 };
147 
148 struct rpc_xprt_ops {
149 	void		(*set_buffer_size)(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize);
150 	int		(*reserve_xprt)(struct rpc_xprt *xprt, struct rpc_task *task);
151 	void		(*release_xprt)(struct rpc_xprt *xprt, struct rpc_task *task);
152 	void		(*alloc_slot)(struct rpc_xprt *xprt, struct rpc_task *task);
153 	void		(*free_slot)(struct rpc_xprt *xprt,
154 				     struct rpc_rqst *req);
155 	void		(*rpcbind)(struct rpc_task *task);
156 	void		(*set_port)(struct rpc_xprt *xprt, unsigned short port);
157 	void		(*connect)(struct rpc_xprt *xprt, struct rpc_task *task);
158 	int		(*get_srcaddr)(struct rpc_xprt *xprt, char *buf,
159 				       size_t buflen);
160 	unsigned short	(*get_srcport)(struct rpc_xprt *xprt);
161 	int		(*buf_alloc)(struct rpc_task *task);
162 	void		(*buf_free)(struct rpc_task *task);
163 	int		(*prepare_request)(struct rpc_rqst *req,
164 					   struct xdr_buf *buf);
165 	int		(*send_request)(struct rpc_rqst *req);
166 	void		(*wait_for_reply_request)(struct rpc_task *task);
167 	void		(*timer)(struct rpc_xprt *xprt, struct rpc_task *task);
168 	void		(*release_request)(struct rpc_task *task);
169 	void		(*close)(struct rpc_xprt *xprt);
170 	void		(*destroy)(struct rpc_xprt *xprt);
171 	void		(*set_connect_timeout)(struct rpc_xprt *xprt,
172 					unsigned long connect_timeout,
173 					unsigned long reconnect_timeout);
174 	void		(*print_stats)(struct rpc_xprt *xprt, struct seq_file *seq);
175 	int		(*enable_swap)(struct rpc_xprt *xprt);
176 	void		(*disable_swap)(struct rpc_xprt *xprt);
177 	void		(*inject_disconnect)(struct rpc_xprt *xprt);
178 	int		(*bc_setup)(struct rpc_xprt *xprt,
179 				    unsigned int min_reqs);
180 	size_t		(*bc_maxpayload)(struct rpc_xprt *xprt);
181 	unsigned int	(*bc_num_slots)(struct rpc_xprt *xprt);
182 	void		(*bc_free_rqst)(struct rpc_rqst *rqst);
183 	void		(*bc_destroy)(struct rpc_xprt *xprt,
184 				      unsigned int max_reqs);
185 };
186 
187 /*
188  * RPC transport identifiers
189  *
190  * To preserve compatibility with the historical use of raw IP protocol
191  * id's for transport selection, UDP and TCP identifiers are specified
192  * with the previous values. No such restriction exists for new transports,
193  * except that they may not collide with these values (17 and 6,
194  * respectively).
195  */
196 #define XPRT_TRANSPORT_BC       (1 << 31)
197 enum xprt_transports {
198 	XPRT_TRANSPORT_UDP	= IPPROTO_UDP,
199 	XPRT_TRANSPORT_TCP	= IPPROTO_TCP,
200 	XPRT_TRANSPORT_BC_TCP	= IPPROTO_TCP | XPRT_TRANSPORT_BC,
201 	XPRT_TRANSPORT_RDMA	= 256,
202 	XPRT_TRANSPORT_BC_RDMA	= XPRT_TRANSPORT_RDMA | XPRT_TRANSPORT_BC,
203 	XPRT_TRANSPORT_LOCAL	= 257,
204 	XPRT_TRANSPORT_TCP_TLS	= 258,
205 };
206 
207 struct rpc_sysfs_xprt;
208 struct rpc_xprt {
209 	struct kref		kref;		/* Reference count */
210 	const struct rpc_xprt_ops *ops;		/* transport methods */
211 	unsigned int		id;		/* transport id */
212 
213 	const struct rpc_timeout *timeout;	/* timeout parms */
214 	struct sockaddr_storage	addr;		/* server address */
215 	size_t			addrlen;	/* size of server address */
216 	int			prot;		/* IP protocol */
217 
218 	unsigned long		cong;		/* current congestion */
219 	unsigned long		cwnd;		/* congestion window */
220 
221 	size_t			max_payload;	/* largest RPC payload size,
222 						   in bytes */
223 
224 	struct rpc_wait_queue	binding;	/* requests waiting on rpcbind */
225 	struct rpc_wait_queue	sending;	/* requests waiting to send */
226 	struct rpc_wait_queue	pending;	/* requests in flight */
227 	struct rpc_wait_queue	backlog;	/* waiting for slot */
228 	struct list_head	free;		/* free slots */
229 	unsigned int		max_reqs;	/* max number of slots */
230 	unsigned int		min_reqs;	/* min number of slots */
231 	unsigned int		num_reqs;	/* total slots */
232 	unsigned long		state;		/* transport state */
233 	unsigned char		resvport   : 1,	/* use a reserved port */
234 				reuseport  : 1; /* reuse port on reconnect */
235 	atomic_t		swapper;	/* we're swapping over this
236 						   transport */
237 	unsigned int		bind_index;	/* bind function index */
238 
239 	/*
240 	 * Multipath
241 	 */
242 	struct list_head	xprt_switch;
243 
244 	/*
245 	 * Connection of transports
246 	 */
247 	unsigned long		bind_timeout,
248 				reestablish_timeout;
249 	struct xprtsec_parms	xprtsec;
250 	unsigned int		connect_cookie;	/* A cookie that gets bumped
251 						   every time the transport
252 						   is reconnected */
253 
254 	/*
255 	 * Disconnection of idle transports
256 	 */
257 	struct work_struct	task_cleanup;
258 	struct timer_list	timer;
259 	unsigned long		last_used,
260 				idle_timeout,
261 				connect_timeout,
262 				max_reconnect_timeout;
263 
264 	/*
265 	 * Send stuff
266 	 */
267 	atomic_long_t		queuelen;
268 	spinlock_t		transport_lock;	/* lock transport info */
269 	spinlock_t		reserve_lock;	/* lock slot table */
270 	spinlock_t		queue_lock;	/* send/receive queue lock */
271 	u32			xid;		/* Next XID value to use */
272 	struct rpc_task *	snd_task;	/* Task blocked in send */
273 
274 	struct list_head	xmit_queue;	/* Send queue */
275 	atomic_long_t		xmit_queuelen;
276 
277 	struct svc_xprt		*bc_xprt;	/* NFSv4.1 backchannel */
278 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
279 	struct svc_serv		*bc_serv;       /* The RPC service which will */
280 						/* process the callback */
281 	unsigned int		bc_alloc_max;
282 	unsigned int		bc_alloc_count;	/* Total number of preallocs */
283 	atomic_t		bc_slot_count;	/* Number of allocated slots */
284 	spinlock_t		bc_pa_lock;	/* Protects the preallocated
285 						 * items */
286 	struct list_head	bc_pa_list;	/* List of preallocated
287 						 * backchannel rpc_rqst's */
288 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
289 
290 	struct rb_root		recv_queue;	/* Receive queue */
291 
292 	struct {
293 		unsigned long		bind_count,	/* total number of binds */
294 					connect_count,	/* total number of connects */
295 					connect_start,	/* connect start timestamp */
296 					connect_time,	/* jiffies waiting for connect */
297 					sends,		/* how many complete requests */
298 					recvs,		/* how many complete requests */
299 					bad_xids,	/* lookup_rqst didn't find XID */
300 					max_slots;	/* max rpc_slots used */
301 
302 		unsigned long long	req_u,		/* average requests on the wire */
303 					bklog_u,	/* backlog queue utilization */
304 					sending_u,	/* send q utilization */
305 					pending_u;	/* pend q utilization */
306 	} stat;
307 
308 	struct net		*xprt_net;
309 	netns_tracker		ns_tracker;
310 	const char		*servername;
311 	const char		*address_strings[RPC_DISPLAY_MAX];
312 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
313 	struct dentry		*debugfs;		/* debugfs directory */
314 #endif
315 	struct rcu_head		rcu;
316 	const struct xprt_class	*xprt_class;
317 	struct rpc_sysfs_xprt	*xprt_sysfs;
318 	bool			main; /*mark if this is the 1st transport */
319 };
320 
321 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
322 /*
323  * Backchannel flags
324  */
325 #define	RPC_BC_PA_IN_USE	0x0001		/* Preallocated backchannel */
326 						/* buffer in use */
327 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
328 
329 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
330 static inline int bc_prealloc(struct rpc_rqst *req)
331 {
332 	return test_bit(RPC_BC_PA_IN_USE, &req->rq_bc_pa_state);
333 }
334 #else
335 static inline int bc_prealloc(struct rpc_rqst *req)
336 {
337 	return 0;
338 }
339 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
340 
341 #define XPRT_CREATE_INFINITE_SLOTS	(1U)
342 #define XPRT_CREATE_NO_IDLE_TIMEOUT	(1U << 1)
343 
344 struct xprt_create {
345 	int			ident;		/* XPRT_TRANSPORT identifier */
346 	struct net *		net;
347 	struct sockaddr *	srcaddr;	/* optional local address */
348 	struct sockaddr *	dstaddr;	/* remote peer address */
349 	size_t			addrlen;
350 	const char		*servername;
351 	struct svc_xprt		*bc_xprt;	/* NFSv4.1 backchannel */
352 	struct rpc_xprt_switch	*bc_xps;
353 	unsigned int		flags;
354 	struct xprtsec_parms	xprtsec;
355 	unsigned long		connect_timeout;
356 	unsigned long		reconnect_timeout;
357 };
358 
359 struct xprt_class {
360 	struct list_head	list;
361 	int			ident;		/* XPRT_TRANSPORT identifier */
362 	struct rpc_xprt *	(*setup)(struct xprt_create *);
363 	struct module		*owner;
364 	char			name[32];
365 	const char *		netid[];
366 };
367 
368 /*
369  * Generic internal transport functions
370  */
371 struct rpc_xprt		*xprt_create_transport(struct xprt_create *args);
372 void			xprt_connect(struct rpc_task *task);
373 unsigned long		xprt_reconnect_delay(const struct rpc_xprt *xprt);
374 void			xprt_reconnect_backoff(struct rpc_xprt *xprt,
375 					       unsigned long init_to);
376 void			xprt_reserve(struct rpc_task *task);
377 void			xprt_retry_reserve(struct rpc_task *task);
378 int			xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task);
379 int			xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task);
380 void			xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task);
381 void			xprt_free_slot(struct rpc_xprt *xprt,
382 				       struct rpc_rqst *req);
383 bool			xprt_prepare_transmit(struct rpc_task *task);
384 void			xprt_request_enqueue_transmit(struct rpc_task *task);
385 int			xprt_request_enqueue_receive(struct rpc_task *task);
386 void			xprt_request_wait_receive(struct rpc_task *task);
387 void			xprt_request_dequeue_xprt(struct rpc_task *task);
388 bool			xprt_request_need_retransmit(struct rpc_task *task);
389 void			xprt_transmit(struct rpc_task *task);
390 void			xprt_end_transmit(struct rpc_task *task);
391 int			xprt_adjust_timeout(struct rpc_rqst *req);
392 void			xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task);
393 void			xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task);
394 void			xprt_release(struct rpc_task *task);
395 struct rpc_xprt *	xprt_get(struct rpc_xprt *xprt);
396 void			xprt_put(struct rpc_xprt *xprt);
397 struct rpc_xprt *	xprt_alloc(struct net *net, size_t size,
398 				unsigned int num_prealloc,
399 				unsigned int max_req);
400 void			xprt_free(struct rpc_xprt *);
401 void			xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task);
402 bool			xprt_wake_up_backlog(struct rpc_xprt *xprt, struct rpc_rqst *req);
403 void			xprt_cleanup_ids(void);
404 
405 static inline int
406 xprt_enable_swap(struct rpc_xprt *xprt)
407 {
408 	return xprt->ops->enable_swap(xprt);
409 }
410 
411 static inline void
412 xprt_disable_swap(struct rpc_xprt *xprt)
413 {
414 	xprt->ops->disable_swap(xprt);
415 }
416 
417 /*
418  * Transport switch helper functions
419  */
420 int			xprt_register_transport(struct xprt_class *type);
421 int			xprt_unregister_transport(struct xprt_class *type);
422 int			xprt_find_transport_ident(const char *);
423 void			xprt_wait_for_reply_request_def(struct rpc_task *task);
424 void			xprt_wait_for_reply_request_rtt(struct rpc_task *task);
425 void			xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status);
426 void			xprt_wait_for_buffer_space(struct rpc_xprt *xprt);
427 bool			xprt_write_space(struct rpc_xprt *xprt);
428 void			xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result);
429 struct rpc_rqst *	xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid);
430 void			xprt_update_rtt(struct rpc_task *task);
431 void			xprt_complete_rqst(struct rpc_task *task, int copied);
432 void			xprt_pin_rqst(struct rpc_rqst *req);
433 void			xprt_unpin_rqst(struct rpc_rqst *req);
434 void			xprt_release_rqst_cong(struct rpc_task *task);
435 bool			xprt_request_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req);
436 void			xprt_disconnect_done(struct rpc_xprt *xprt);
437 void			xprt_force_disconnect(struct rpc_xprt *xprt);
438 void			xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie);
439 
440 bool			xprt_lock_connect(struct rpc_xprt *, struct rpc_task *, void *);
441 void			xprt_unlock_connect(struct rpc_xprt *, void *);
442 void			xprt_release_write(struct rpc_xprt *, struct rpc_task *);
443 
444 /*
445  * Reserved bit positions in xprt->state
446  */
447 #define XPRT_LOCKED		(0)
448 #define XPRT_CONNECTED		(1)
449 #define XPRT_CONNECTING		(2)
450 #define XPRT_CLOSE_WAIT		(3)
451 #define XPRT_BOUND		(4)
452 #define XPRT_BINDING		(5)
453 #define XPRT_CLOSING		(6)
454 #define XPRT_OFFLINE		(7)
455 #define XPRT_REMOVE		(8)
456 #define XPRT_CONGESTED		(9)
457 #define XPRT_CWND_WAIT		(10)
458 #define XPRT_WRITE_SPACE	(11)
459 #define XPRT_SND_IS_COOKIE	(12)
460 
461 static inline void xprt_set_connected(struct rpc_xprt *xprt)
462 {
463 	set_bit(XPRT_CONNECTED, &xprt->state);
464 }
465 
466 static inline void xprt_clear_connected(struct rpc_xprt *xprt)
467 {
468 	clear_bit(XPRT_CONNECTED, &xprt->state);
469 }
470 
471 static inline int xprt_connected(struct rpc_xprt *xprt)
472 {
473 	return test_bit(XPRT_CONNECTED, &xprt->state);
474 }
475 
476 static inline int xprt_test_and_set_connected(struct rpc_xprt *xprt)
477 {
478 	return test_and_set_bit(XPRT_CONNECTED, &xprt->state);
479 }
480 
481 static inline int xprt_test_and_clear_connected(struct rpc_xprt *xprt)
482 {
483 	return test_and_clear_bit(XPRT_CONNECTED, &xprt->state);
484 }
485 
486 static inline void xprt_clear_connecting(struct rpc_xprt *xprt)
487 {
488 	smp_mb__before_atomic();
489 	clear_bit(XPRT_CONNECTING, &xprt->state);
490 	smp_mb__after_atomic();
491 }
492 
493 static inline int xprt_connecting(struct rpc_xprt *xprt)
494 {
495 	return test_bit(XPRT_CONNECTING, &xprt->state);
496 }
497 
498 static inline int xprt_test_and_set_connecting(struct rpc_xprt *xprt)
499 {
500 	return test_and_set_bit(XPRT_CONNECTING, &xprt->state);
501 }
502 
503 static inline void xprt_set_bound(struct rpc_xprt *xprt)
504 {
505 	test_and_set_bit(XPRT_BOUND, &xprt->state);
506 }
507 
508 static inline int xprt_bound(struct rpc_xprt *xprt)
509 {
510 	return test_bit(XPRT_BOUND, &xprt->state);
511 }
512 
513 static inline void xprt_clear_bound(struct rpc_xprt *xprt)
514 {
515 	clear_bit(XPRT_BOUND, &xprt->state);
516 }
517 
518 static inline void xprt_clear_binding(struct rpc_xprt *xprt)
519 {
520 	smp_mb__before_atomic();
521 	clear_bit(XPRT_BINDING, &xprt->state);
522 	smp_mb__after_atomic();
523 }
524 
525 static inline int xprt_test_and_set_binding(struct rpc_xprt *xprt)
526 {
527 	return test_and_set_bit(XPRT_BINDING, &xprt->state);
528 }
529 
530 void xprt_set_offline_locked(struct rpc_xprt *xprt, struct rpc_xprt_switch *xps);
531 void xprt_set_online_locked(struct rpc_xprt *xprt, struct rpc_xprt_switch *xps);
532 void xprt_delete_locked(struct rpc_xprt *xprt, struct rpc_xprt_switch *xps);
533 #endif /* _LINUX_SUNRPC_XPRT_H */
534