1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  *  linux/include/linux/sunrpc/clnt.h
4  *
5  *  Declarations for the high-level RPC client interface
6  *
7  *  Copyright (C) 1995, 1996, Olaf Kirch <okir@monad.swb.de>
8  */
9 
10 #ifndef _LINUX_SUNRPC_CLNT_H
11 #define _LINUX_SUNRPC_CLNT_H
12 
13 #include <linux/types.h>
14 #include <linux/socket.h>
15 #include <linux/in.h>
16 #include <linux/in6.h>
17 #include <linux/refcount.h>
18 
19 #include <linux/sunrpc/msg_prot.h>
20 #include <linux/sunrpc/sched.h>
21 #include <linux/sunrpc/xprt.h>
22 #include <linux/sunrpc/auth.h>
23 #include <linux/sunrpc/stats.h>
24 #include <linux/sunrpc/xdr.h>
25 #include <linux/sunrpc/timer.h>
26 #include <linux/sunrpc/rpc_pipe_fs.h>
27 #include <asm/signal.h>
28 #include <linux/path.h>
29 #include <net/ipv6.h>
30 #include <linux/sunrpc/xprtmultipath.h>
31 
32 struct rpc_inode;
33 struct rpc_sysfs_client;
34 
35 /*
36  * The high-level client handle
37  */
38 struct rpc_clnt {
39 	refcount_t		cl_count;	/* Number of references */
40 	unsigned int		cl_clid;	/* client id */
41 	struct list_head	cl_clients;	/* Global list of clients */
42 	struct list_head	cl_tasks;	/* List of tasks */
43 	spinlock_t		cl_lock;	/* spinlock */
44 	struct rpc_xprt __rcu *	cl_xprt;	/* transport */
45 	const struct rpc_procinfo *cl_procinfo;	/* procedure info */
46 	u32			cl_prog,	/* RPC program number */
47 				cl_vers,	/* RPC version number */
48 				cl_maxproc;	/* max procedure number */
49 
50 	struct rpc_auth *	cl_auth;	/* authenticator */
51 	struct rpc_stat *	cl_stats;	/* per-program statistics */
52 	struct rpc_iostats *	cl_metrics;	/* per-client statistics */
53 
54 	unsigned int		cl_softrtry : 1,/* soft timeouts */
55 				cl_softerr  : 1,/* Timeouts return errors */
56 				cl_discrtry : 1,/* disconnect before retry */
57 				cl_noretranstimeo: 1,/* No retransmit timeouts */
58 				cl_autobind : 1,/* use getport() */
59 				cl_chatty   : 1;/* be verbose */
60 
61 	struct rpc_rtt *	cl_rtt;		/* RTO estimator data */
62 	const struct rpc_timeout *cl_timeout;	/* Timeout strategy */
63 
64 	atomic_t		cl_swapper;	/* swapfile count */
65 	int			cl_nodelen;	/* nodename length */
66 	char 			cl_nodename[UNX_MAXNODENAME+1];
67 	struct rpc_pipe_dir_head cl_pipedir_objects;
68 	struct rpc_clnt *	cl_parent;	/* Points to parent of clones */
69 	struct rpc_rtt		cl_rtt_default;
70 	struct rpc_timeout	cl_timeout_default;
71 	const struct rpc_program *cl_program;
72 	const char *		cl_principal;	/* use for machine cred */
73 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
74 	struct dentry		*cl_debugfs;	/* debugfs directory */
75 #endif
76 	struct rpc_sysfs_client *cl_sysfs;	/* sysfs directory */
77 	/* cl_work is only needed after cl_xpi is no longer used,
78 	 * and that are of similar size
79 	 */
80 	union {
81 		struct rpc_xprt_iter	cl_xpi;
82 		struct work_struct	cl_work;
83 	};
84 	const struct cred	*cl_cred;
85 	unsigned int		cl_max_connect; /* max number of transports not to the same IP */
86 };
87 
88 /*
89  * General RPC program info
90  */
91 #define RPC_MAXVERSION		4
92 struct rpc_program {
93 	const char *		name;		/* protocol name */
94 	u32			number;		/* program number */
95 	unsigned int		nrvers;		/* number of versions */
96 	const struct rpc_version **	version;	/* version array */
97 	struct rpc_stat *	stats;		/* statistics */
98 	const char *		pipe_dir_name;	/* path to rpc_pipefs dir */
99 };
100 
101 struct rpc_version {
102 	u32			number;		/* version number */
103 	unsigned int		nrprocs;	/* number of procs */
104 	const struct rpc_procinfo *procs;	/* procedure array */
105 	unsigned int		*counts;	/* call counts */
106 };
107 
108 /*
109  * Procedure information
110  */
111 struct rpc_procinfo {
112 	u32			p_proc;		/* RPC procedure number */
113 	kxdreproc_t		p_encode;	/* XDR encode function */
114 	kxdrdproc_t		p_decode;	/* XDR decode function */
115 	unsigned int		p_arglen;	/* argument hdr length (u32) */
116 	unsigned int		p_replen;	/* reply hdr length (u32) */
117 	unsigned int		p_timer;	/* Which RTT timer to use */
118 	u32			p_statidx;	/* Which procedure to account */
119 	const char *		p_name;		/* name of procedure */
120 };
121 
122 struct rpc_create_args {
123 	struct net		*net;
124 	int			protocol;
125 	struct sockaddr		*address;
126 	size_t			addrsize;
127 	struct sockaddr		*saddress;
128 	const struct rpc_timeout *timeout;
129 	const char		*servername;
130 	const char		*nodename;
131 	const struct rpc_program *program;
132 	u32			prognumber;	/* overrides program->number */
133 	u32			version;
134 	rpc_authflavor_t	authflavor;
135 	u32			nconnect;
136 	unsigned long		flags;
137 	char			*client_name;
138 	struct svc_xprt		*bc_xprt;	/* NFSv4.1 backchannel */
139 	const struct cred	*cred;
140 	unsigned int		max_connect;
141 };
142 
143 struct rpc_add_xprt_test {
144 	void (*add_xprt_test)(struct rpc_clnt *clnt,
145 		struct rpc_xprt *xprt,
146 		void *calldata);
147 	void *data;
148 };
149 
150 /* Values for "flags" field */
151 #define RPC_CLNT_CREATE_HARDRTRY	(1UL << 0)
152 #define RPC_CLNT_CREATE_AUTOBIND	(1UL << 2)
153 #define RPC_CLNT_CREATE_NONPRIVPORT	(1UL << 3)
154 #define RPC_CLNT_CREATE_NOPING		(1UL << 4)
155 #define RPC_CLNT_CREATE_DISCRTRY	(1UL << 5)
156 #define RPC_CLNT_CREATE_QUIET		(1UL << 6)
157 #define RPC_CLNT_CREATE_INFINITE_SLOTS	(1UL << 7)
158 #define RPC_CLNT_CREATE_NO_IDLE_TIMEOUT	(1UL << 8)
159 #define RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT	(1UL << 9)
160 #define RPC_CLNT_CREATE_SOFTERR		(1UL << 10)
161 #define RPC_CLNT_CREATE_REUSEPORT	(1UL << 11)
162 
163 struct rpc_clnt *rpc_create(struct rpc_create_args *args);
164 struct rpc_clnt	*rpc_bind_new_program(struct rpc_clnt *,
165 				const struct rpc_program *, u32);
166 struct rpc_clnt *rpc_clone_client(struct rpc_clnt *);
167 struct rpc_clnt *rpc_clone_client_set_auth(struct rpc_clnt *,
168 				rpc_authflavor_t);
169 int		rpc_switch_client_transport(struct rpc_clnt *,
170 				struct xprt_create *,
171 				const struct rpc_timeout *);
172 
173 void		rpc_shutdown_client(struct rpc_clnt *);
174 void		rpc_release_client(struct rpc_clnt *);
175 void		rpc_task_release_transport(struct rpc_task *);
176 void		rpc_task_release_client(struct rpc_task *);
177 struct rpc_xprt	*rpc_task_get_xprt(struct rpc_clnt *clnt,
178 		struct rpc_xprt *xprt);
179 
180 int		rpcb_create_local(struct net *);
181 void		rpcb_put_local(struct net *);
182 int		rpcb_register(struct net *, u32, u32, int, unsigned short);
183 int		rpcb_v4_register(struct net *net, const u32 program,
184 				 const u32 version,
185 				 const struct sockaddr *address,
186 				 const char *netid);
187 void		rpcb_getport_async(struct rpc_task *);
188 
189 void rpc_prepare_reply_pages(struct rpc_rqst *req, struct page **pages,
190 			     unsigned int base, unsigned int len,
191 			     unsigned int hdrsize);
192 void		rpc_call_start(struct rpc_task *);
193 int		rpc_call_async(struct rpc_clnt *clnt,
194 			       const struct rpc_message *msg, int flags,
195 			       const struct rpc_call_ops *tk_ops,
196 			       void *calldata);
197 int		rpc_call_sync(struct rpc_clnt *clnt,
198 			      const struct rpc_message *msg, int flags);
199 struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred,
200 			       int flags);
201 int		rpc_restart_call_prepare(struct rpc_task *);
202 int		rpc_restart_call(struct rpc_task *);
203 void		rpc_setbufsize(struct rpc_clnt *, unsigned int, unsigned int);
204 struct net *	rpc_net_ns(struct rpc_clnt *);
205 size_t		rpc_max_payload(struct rpc_clnt *);
206 size_t		rpc_max_bc_payload(struct rpc_clnt *);
207 unsigned int	rpc_num_bc_slots(struct rpc_clnt *);
208 void		rpc_force_rebind(struct rpc_clnt *);
209 size_t		rpc_peeraddr(struct rpc_clnt *, struct sockaddr *, size_t);
210 const char	*rpc_peeraddr2str(struct rpc_clnt *, enum rpc_display_format_t);
211 int		rpc_localaddr(struct rpc_clnt *, struct sockaddr *, size_t);
212 
213 int 		rpc_clnt_iterate_for_each_xprt(struct rpc_clnt *clnt,
214 			int (*fn)(struct rpc_clnt *, struct rpc_xprt *, void *),
215 			void *data);
216 
217 int 		rpc_clnt_test_and_add_xprt(struct rpc_clnt *clnt,
218 			struct rpc_xprt_switch *xps,
219 			struct rpc_xprt *xprt,
220 			void *dummy);
221 int		rpc_clnt_add_xprt(struct rpc_clnt *, struct xprt_create *,
222 			int (*setup)(struct rpc_clnt *,
223 				struct rpc_xprt_switch *,
224 				struct rpc_xprt *,
225 				void *),
226 			void *data);
227 void		rpc_set_connect_timeout(struct rpc_clnt *clnt,
228 			unsigned long connect_timeout,
229 			unsigned long reconnect_timeout);
230 
231 int		rpc_clnt_setup_test_and_add_xprt(struct rpc_clnt *,
232 			struct rpc_xprt_switch *,
233 			struct rpc_xprt *,
234 			void *);
235 
236 const char *rpc_proc_name(const struct rpc_task *task);
237 
238 void rpc_clnt_xprt_switch_put(struct rpc_clnt *);
239 void rpc_clnt_xprt_switch_add_xprt(struct rpc_clnt *, struct rpc_xprt *);
240 bool rpc_clnt_xprt_switch_has_addr(struct rpc_clnt *clnt,
241 			const struct sockaddr *sap);
242 void rpc_cleanup_clids(void);
243 
rpc_reply_expected(struct rpc_task * task)244 static inline int rpc_reply_expected(struct rpc_task *task)
245 {
246 	return (task->tk_msg.rpc_proc != NULL) &&
247 		(task->tk_msg.rpc_proc->p_decode != NULL);
248 }
249 
rpc_task_close_connection(struct rpc_task * task)250 static inline void rpc_task_close_connection(struct rpc_task *task)
251 {
252 	if (task->tk_xprt)
253 		xprt_force_disconnect(task->tk_xprt);
254 }
255 #endif /* _LINUX_SUNRPC_CLNT_H */
256