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