blob: 9e1e046de17614d985c652c7d2bd0ded3a64a6e1 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Tom Tucker1d8206b92007-12-30 21:07:15 -06002/*
3 * linux/include/linux/sunrpc/svc_xprt.h
4 *
5 * RPC server transport I/O
6 */
7
8#ifndef SUNRPC_SVC_XPRT_H
9#define SUNRPC_SVC_XPRT_H
10
11#include <linux/sunrpc/svc.h>
Paul Gortmakerde477252011-05-26 13:46:22 -040012
13struct module;
Tom Tucker1d8206b92007-12-30 21:07:15 -060014
15struct svc_xprt_ops {
Tom Tuckerb700cbb2007-12-30 21:07:42 -060016 struct svc_xprt *(*xpo_create)(struct svc_serv *,
Pavel Emelyanov62832c02010-09-29 16:04:18 +040017 struct net *net,
Tom Tuckerb700cbb2007-12-30 21:07:42 -060018 struct sockaddr *, int,
19 int);
Tom Tucker38a417c2007-12-30 21:07:36 -060020 struct svc_xprt *(*xpo_accept)(struct svc_xprt *);
Tom Tucker323bee32007-12-30 21:07:31 -060021 int (*xpo_has_wspace)(struct svc_xprt *);
Tom Tucker5d137992007-12-30 21:07:23 -060022 int (*xpo_recvfrom)(struct svc_rqst *);
23 int (*xpo_sendto)(struct svc_rqst *);
Chuck Lever41205532020-03-02 14:45:53 -050024 int (*xpo_read_payload)(struct svc_rqst *, unsigned int,
25 unsigned int);
Tom Tucker5148bf42007-12-30 21:07:25 -060026 void (*xpo_release_rqst)(struct svc_rqst *);
Tom Tucker755ccea2007-12-30 21:07:27 -060027 void (*xpo_detach)(struct svc_xprt *);
28 void (*xpo_free)(struct svc_xprt *);
Chuck Lever989f8812018-03-27 10:49:38 -040029 void (*xpo_secure_port)(struct svc_rqst *rqstp);
Scott Mayhewea08e392016-11-11 13:16:22 -050030 void (*xpo_kill_temp_xprt)(struct svc_xprt *);
Tom Tucker1d8206b92007-12-30 21:07:15 -060031};
32
33struct svc_xprt_class {
34 const char *xcl_name;
35 struct module *xcl_owner;
Chuck Lever2412e922017-08-01 11:59:49 -040036 const struct svc_xprt_ops *xcl_ops;
Tom Tucker1d8206b92007-12-30 21:07:15 -060037 struct list_head xcl_list;
Tom Tucker49023152007-12-30 21:07:21 -060038 u32 xcl_max_payload;
Chuck Lever3c45ddf2014-07-16 15:38:32 -040039 int xcl_ident;
Tom Tucker1d8206b92007-12-30 21:07:15 -060040};
41
J. Bruce Fieldsedc7a892010-03-22 15:37:17 -040042/*
43 * This is embedded in an object that wants a callback before deleting
44 * an xprt; intended for use by NFSv4.1, which needs to know when a
45 * client's tcp connection (and hence possibly a backchannel) goes away.
46 */
47struct svc_xpt_user {
48 struct list_head list;
49 void (*callback)(struct svc_xpt_user *);
50};
51
Tom Tucker1d8206b92007-12-30 21:07:15 -060052struct svc_xprt {
53 struct svc_xprt_class *xpt_class;
Chuck Lever2412e922017-08-01 11:59:49 -040054 const struct svc_xprt_ops *xpt_ops;
Tom Tuckere1b31572007-12-30 21:07:46 -060055 struct kref xpt_ref;
Tom Tucker7a182082007-12-30 21:07:53 -060056 struct list_head xpt_list;
57 struct list_head xpt_ready;
Tom Tucker02fc6c32007-12-30 21:07:48 -060058 unsigned long xpt_flags;
59#define XPT_BUSY 0 /* enqueued/receiving */
60#define XPT_CONN 1 /* conn pending */
61#define XPT_CLOSE 2 /* dead or dying */
62#define XPT_DATA 3 /* data pending */
63#define XPT_TEMP 4 /* connected transport */
64#define XPT_DEAD 6 /* transport closed */
65#define XPT_CHNGBUF 7 /* need to change snd/rcv buf sizes */
66#define XPT_DEFERRED 8 /* deferred request pending */
67#define XPT_OLD 9 /* used for xprt aging mark+sweep */
Jeff Layton8d65ef72014-11-17 17:02:57 -050068#define XPT_LISTENER 10 /* listening endpoint */
69#define XPT_CACHE_AUTH 11 /* cache auth info */
70#define XPT_LOCAL 12 /* connection from loopback interface */
Scott Mayhew546125d2017-01-05 16:34:51 -050071#define XPT_KILL_TEMP 13 /* call xpo_kill_temp_xprt before closing */
Jeff Layton362142b22017-02-24 13:25:23 -050072#define XPT_CONG_CTRL 14 /* has congestion control */
Tom Tuckerbb5cf162007-12-30 21:07:50 -060073
Tom Tuckerbb5cf162007-12-30 21:07:50 -060074 struct svc_serv *xpt_server; /* service for transport */
Tom Tucker7a90e8c2007-12-30 21:07:55 -060075 atomic_t xpt_reserved; /* space on outq that is rsvd */
Trond Myklebustff3ac5c2016-06-24 10:55:50 -040076 atomic_t xpt_nr_rqsts; /* Number of requests */
Tom Tuckera50fea22007-12-30 21:07:59 -060077 struct mutex xpt_mutex; /* to serialize sending data */
Tom Tuckerdef13d72007-12-30 21:08:08 -060078 spinlock_t xpt_lock; /* protects sk_deferred
79 * and xpt_auth_cache */
80 void *xpt_auth_cache;/* auth cache */
Tom Tucker8c7b0172007-12-30 21:08:10 -060081 struct list_head xpt_deferred; /* deferred requests that need
82 * to be revisted */
Tom Tucker9dbc2402007-12-30 21:08:12 -060083 struct sockaddr_storage xpt_local; /* local address */
84 size_t xpt_locallen; /* length of address */
85 struct sockaddr_storage xpt_remote; /* remote peer's address */
86 size_t xpt_remotelen; /* length of address */
Chuck Leverece200d2018-03-27 10:51:00 -040087 char xpt_remotebuf[INET6_ADDRSTRLEN + 10];
J. Bruce Fieldsedc7a892010-03-22 15:37:17 -040088 struct list_head xpt_users; /* callbacks on free */
Pavel Emelyanov4fb85182010-09-27 14:00:49 +040089
90 struct net *xpt_net;
Trond Myklebust4df493a2019-04-09 12:13:37 -040091 const struct cred *xpt_cred;
J. Bruce Fieldsd75faea2010-11-30 19:15:01 -050092 struct rpc_xprt *xpt_bc_xprt; /* NFSv4.1 backchannel */
J. Bruce Fields39a9bea2016-05-17 12:38:21 -040093 struct rpc_xprt_switch *xpt_bc_xps; /* NFSv4.1 backchannel */
Tom Tucker1d8206b92007-12-30 21:07:15 -060094};
95
J. Bruce Fieldsedc7a892010-03-22 15:37:17 -040096static inline void unregister_xpt_user(struct svc_xprt *xpt, struct svc_xpt_user *u)
97{
98 spin_lock(&xpt->xpt_lock);
99 list_del_init(&u->list);
100 spin_unlock(&xpt->xpt_lock);
101}
102
J. Bruce Fields21b75b02010-10-26 10:07:17 -0400103static inline int register_xpt_user(struct svc_xprt *xpt, struct svc_xpt_user *u)
104{
105 spin_lock(&xpt->xpt_lock);
106 if (test_bit(XPT_CLOSE, &xpt->xpt_flags)) {
107 /*
108 * The connection is about to be deleted soon (or,
109 * worse, may already be deleted--in which case we've
110 * already notified the xpt_users).
111 */
112 spin_unlock(&xpt->xpt_lock);
113 return -ENOTCONN;
114 }
115 list_add(&u->list, &xpt->xpt_users);
116 spin_unlock(&xpt->xpt_lock);
117 return 0;
118}
119
Tom Tucker1d8206b92007-12-30 21:07:15 -0600120int svc_reg_xprt_class(struct svc_xprt_class *);
121void svc_unreg_xprt_class(struct svc_xprt_class *);
Stanislav Kinsburskybd4620d2011-12-06 14:19:10 +0300122void svc_xprt_init(struct net *, struct svc_xprt_class *, struct svc_xprt *,
Tom Tuckerbb5cf162007-12-30 21:07:50 -0600123 struct svc_serv *);
Pavel Emelyanovfc5d00b2010-09-29 16:03:50 +0400124int svc_create_xprt(struct svc_serv *, const char *, struct net *,
Trond Myklebust4df493a2019-04-09 12:13:37 -0400125 const int, const unsigned short, int,
126 const struct cred *);
Jeff Laytonb9e13cd2015-06-08 12:06:51 -0700127void svc_xprt_do_enqueue(struct svc_xprt *xprt);
Tom Tucker0f0257e2007-12-30 21:08:27 -0600128void svc_xprt_enqueue(struct svc_xprt *xprt);
Tom Tuckere1b31572007-12-30 21:07:46 -0600129void svc_xprt_put(struct svc_xprt *xprt);
Tom Tucker9dbc2402007-12-30 21:08:12 -0600130void svc_xprt_copy_addrs(struct svc_rqst *rqstp, struct svc_xprt *xprt);
Tom Tucker0f0257e2007-12-30 21:08:27 -0600131void svc_close_xprt(struct svc_xprt *xprt);
Tom Tucker0f0257e2007-12-30 21:08:27 -0600132int svc_port_is_privileged(struct sockaddr *sin);
Tom Tuckerdc9a16e2007-12-30 21:08:31 -0600133int svc_print_xprts(char *buf, int maxlen);
Chuck Lever156e6202009-03-18 20:45:58 -0400134struct svc_xprt *svc_find_xprt(struct svc_serv *serv, const char *xcl_name,
Stanislav Kinsbursky4cb54ca2012-01-20 16:50:53 +0400135 struct net *net, const sa_family_t af,
136 const unsigned short port);
Chuck Lever335c54b2009-04-23 19:32:25 -0400137int svc_xprt_names(struct svc_serv *serv, char *buf, const int buflen);
J. Bruce Fields39b55302012-08-14 15:50:34 -0400138void svc_add_new_perm_xprt(struct svc_serv *serv, struct svc_xprt *xprt);
Scott Mayhewc3d48792015-12-11 16:45:58 -0500139void svc_age_temp_xprts_now(struct svc_serv *, struct sockaddr *);
Tom Tucker0f0257e2007-12-30 21:08:27 -0600140
Tom Tuckere1b31572007-12-30 21:07:46 -0600141static inline void svc_xprt_get(struct svc_xprt *xprt)
142{
143 kref_get(&xprt->xpt_ref);
144}
Tom Tucker9dbc2402007-12-30 21:08:12 -0600145static inline void svc_xprt_set_local(struct svc_xprt *xprt,
Chuck Leverefb32882009-03-18 20:45:43 -0400146 const struct sockaddr *sa,
147 const size_t salen)
Tom Tucker9dbc2402007-12-30 21:08:12 -0600148{
149 memcpy(&xprt->xpt_local, sa, salen);
150 xprt->xpt_locallen = salen;
151}
152static inline void svc_xprt_set_remote(struct svc_xprt *xprt,
Chuck Leverefb32882009-03-18 20:45:43 -0400153 const struct sockaddr *sa,
154 const size_t salen)
Tom Tucker9dbc2402007-12-30 21:08:12 -0600155{
156 memcpy(&xprt->xpt_remote, sa, salen);
157 xprt->xpt_remotelen = salen;
Chuck Leverece200d2018-03-27 10:51:00 -0400158 snprintf(xprt->xpt_remotebuf, sizeof(xprt->xpt_remotebuf) - 1,
159 "%pISpc", sa);
Tom Tucker9dbc2402007-12-30 21:08:12 -0600160}
Chuck Leverece200d2018-03-27 10:51:00 -0400161
Chuck Leverefb32882009-03-18 20:45:43 -0400162static inline unsigned short svc_addr_port(const struct sockaddr *sa)
Tom Tucker9dbc2402007-12-30 21:08:12 -0600163{
Chuck Leverefb32882009-03-18 20:45:43 -0400164 const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
165 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)sa;
166
Tom Tucker9dbc2402007-12-30 21:08:12 -0600167 switch (sa->sa_family) {
168 case AF_INET:
Chuck Leverefb32882009-03-18 20:45:43 -0400169 return ntohs(sin->sin_port);
Tom Tucker9dbc2402007-12-30 21:08:12 -0600170 case AF_INET6:
Chuck Leverefb32882009-03-18 20:45:43 -0400171 return ntohs(sin6->sin6_port);
Tom Tucker9dbc2402007-12-30 21:08:12 -0600172 }
Chuck Leverefb32882009-03-18 20:45:43 -0400173
174 return 0;
Tom Tucker9dbc2402007-12-30 21:08:12 -0600175}
176
Chuck Leverabc5c442009-04-23 19:31:25 -0400177static inline size_t svc_addr_len(const struct sockaddr *sa)
Tom Tucker9dbc2402007-12-30 21:08:12 -0600178{
179 switch (sa->sa_family) {
180 case AF_INET:
181 return sizeof(struct sockaddr_in);
182 case AF_INET6:
183 return sizeof(struct sockaddr_in6);
184 }
J. Bruce Fieldsaf6d5722012-08-21 17:22:11 -0400185 BUG();
Tom Tucker9dbc2402007-12-30 21:08:12 -0600186}
187
Chuck Leverefb32882009-03-18 20:45:43 -0400188static inline unsigned short svc_xprt_local_port(const struct svc_xprt *xprt)
Tom Tucker9dbc2402007-12-30 21:08:12 -0600189{
Chuck Leverefb32882009-03-18 20:45:43 -0400190 return svc_addr_port((const struct sockaddr *)&xprt->xpt_local);
Tom Tucker9dbc2402007-12-30 21:08:12 -0600191}
192
Chuck Leverefb32882009-03-18 20:45:43 -0400193static inline unsigned short svc_xprt_remote_port(const struct svc_xprt *xprt)
Tom Tucker9dbc2402007-12-30 21:08:12 -0600194{
Chuck Leverefb32882009-03-18 20:45:43 -0400195 return svc_addr_port((const struct sockaddr *)&xprt->xpt_remote);
Tom Tucker9dbc2402007-12-30 21:08:12 -0600196}
Tom Tucker1d8206b92007-12-30 21:07:15 -0600197
Chuck Leverefb32882009-03-18 20:45:43 -0400198static inline char *__svc_print_addr(const struct sockaddr *addr,
199 char *buf, const size_t len)
Tom Tucker0f0257e2007-12-30 21:08:27 -0600200{
Chuck Leverefb32882009-03-18 20:45:43 -0400201 const struct sockaddr_in *sin = (const struct sockaddr_in *)addr;
202 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)addr;
203
Tom Tucker0f0257e2007-12-30 21:08:27 -0600204 switch (addr->sa_family) {
205 case AF_INET:
Chuck Leverefb32882009-03-18 20:45:43 -0400206 snprintf(buf, len, "%pI4, port=%u", &sin->sin_addr,
207 ntohs(sin->sin_port));
Tom Tucker0f0257e2007-12-30 21:08:27 -0600208 break;
209
210 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700211 snprintf(buf, len, "%pI6, port=%u",
Chuck Leverefb32882009-03-18 20:45:43 -0400212 &sin6->sin6_addr,
213 ntohs(sin6->sin6_port));
Tom Tucker0f0257e2007-12-30 21:08:27 -0600214 break;
215
216 default:
217 snprintf(buf, len, "unknown address type: %d", addr->sa_family);
218 break;
219 }
Chuck Leverefb32882009-03-18 20:45:43 -0400220
Tom Tucker0f0257e2007-12-30 21:08:27 -0600221 return buf;
222}
Tom Tucker1d8206b92007-12-30 21:07:15 -0600223#endif /* SUNRPC_SVC_XPRT_H */