1 /*
2 * linux/include/linux/sunrpc/svc_xprt.h
3 *
4 * RPC server transport I/O
5 */
6
7 #ifndef SUNRPC_SVC_XPRT_H
8 #define SUNRPC_SVC_XPRT_H
9
10 #include <linux/sunrpc/svc.h>
11
12 struct module;
13
14 struct svc_xprt_ops {
15 struct svc_xprt *(*xpo_create)(struct svc_serv *,
16 struct net *net,
17 struct sockaddr *, int,
18 int);
19 struct svc_xprt *(*xpo_accept)(struct svc_xprt *);
20 int (*xpo_has_wspace)(struct svc_xprt *);
21 int (*xpo_recvfrom)(struct svc_rqst *);
22 void (*xpo_prep_reply_hdr)(struct svc_rqst *);
23 int (*xpo_sendto)(struct svc_rqst *);
24 void (*xpo_release_rqst)(struct svc_rqst *);
25 void (*xpo_detach)(struct svc_xprt *);
26 void (*xpo_free)(struct svc_xprt *);
27 int (*xpo_secure_port)(struct svc_rqst *);
28 void (*xpo_kill_temp_xprt)(struct svc_xprt *);
29 };
30
31 struct svc_xprt_class {
32 const char *xcl_name;
33 struct module *xcl_owner;
34 struct svc_xprt_ops *xcl_ops;
35 struct list_head xcl_list;
36 u32 xcl_max_payload;
37 int xcl_ident;
38 };
39
40 /*
41 * This is embedded in an object that wants a callback before deleting
42 * an xprt; intended for use by NFSv4.1, which needs to know when a
43 * client's tcp connection (and hence possibly a backchannel) goes away.
44 */
45 struct svc_xpt_user {
46 struct list_head list;
47 void (*callback)(struct svc_xpt_user *);
48 };
49
50 struct svc_xprt {
51 struct svc_xprt_class *xpt_class;
52 struct svc_xprt_ops *xpt_ops;
53 struct kref xpt_ref;
54 struct list_head xpt_list;
55 struct list_head xpt_ready;
56 unsigned long xpt_flags;
57 #define XPT_BUSY 0 /* enqueued/receiving */
58 #define XPT_CONN 1 /* conn pending */
59 #define XPT_CLOSE 2 /* dead or dying */
60 #define XPT_DATA 3 /* data pending */
61 #define XPT_TEMP 4 /* connected transport */
62 #define XPT_DEAD 6 /* transport closed */
63 #define XPT_CHNGBUF 7 /* need to change snd/rcv buf sizes */
64 #define XPT_DEFERRED 8 /* deferred request pending */
65 #define XPT_OLD 9 /* used for xprt aging mark+sweep */
66 #define XPT_LISTENER 10 /* listening endpoint */
67 #define XPT_CACHE_AUTH 11 /* cache auth info */
68 #define XPT_LOCAL 12 /* connection from loopback interface */
69 #define XPT_KILL_TEMP 13 /* call xpo_kill_temp_xprt before closing */
70
71 struct svc_serv *xpt_server; /* service for transport */
72 atomic_t xpt_reserved; /* space on outq that is rsvd */
73 atomic_t xpt_nr_rqsts; /* Number of requests */
74 struct mutex xpt_mutex; /* to serialize sending data */
75 spinlock_t xpt_lock; /* protects sk_deferred
76 * and xpt_auth_cache */
77 void *xpt_auth_cache;/* auth cache */
78 struct list_head xpt_deferred; /* deferred requests that need
79 * to be revisted */
80 struct sockaddr_storage xpt_local; /* local address */
81 size_t xpt_locallen; /* length of address */
82 struct sockaddr_storage xpt_remote; /* remote peer's address */
83 size_t xpt_remotelen; /* length of address */
84 struct rpc_wait_queue xpt_bc_pending; /* backchannel wait queue */
85 struct list_head xpt_users; /* callbacks on free */
86
87 struct net *xpt_net;
88 struct rpc_xprt *xpt_bc_xprt; /* NFSv4.1 backchannel */
89 struct rpc_xprt_switch *xpt_bc_xps; /* NFSv4.1 backchannel */
90 };
91
unregister_xpt_user(struct svc_xprt * xpt,struct svc_xpt_user * u)92 static inline void unregister_xpt_user(struct svc_xprt *xpt, struct svc_xpt_user *u)
93 {
94 spin_lock(&xpt->xpt_lock);
95 list_del_init(&u->list);
96 spin_unlock(&xpt->xpt_lock);
97 }
98
register_xpt_user(struct svc_xprt * xpt,struct svc_xpt_user * u)99 static inline int register_xpt_user(struct svc_xprt *xpt, struct svc_xpt_user *u)
100 {
101 spin_lock(&xpt->xpt_lock);
102 if (test_bit(XPT_CLOSE, &xpt->xpt_flags)) {
103 /*
104 * The connection is about to be deleted soon (or,
105 * worse, may already be deleted--in which case we've
106 * already notified the xpt_users).
107 */
108 spin_unlock(&xpt->xpt_lock);
109 return -ENOTCONN;
110 }
111 list_add(&u->list, &xpt->xpt_users);
112 spin_unlock(&xpt->xpt_lock);
113 return 0;
114 }
115
116 int svc_reg_xprt_class(struct svc_xprt_class *);
117 void svc_unreg_xprt_class(struct svc_xprt_class *);
118 void svc_xprt_init(struct net *, struct svc_xprt_class *, struct svc_xprt *,
119 struct svc_serv *);
120 int svc_create_xprt(struct svc_serv *, const char *, struct net *,
121 const int, const unsigned short, int);
122 void svc_xprt_do_enqueue(struct svc_xprt *xprt);
123 void svc_xprt_enqueue(struct svc_xprt *xprt);
124 void svc_xprt_put(struct svc_xprt *xprt);
125 void svc_xprt_copy_addrs(struct svc_rqst *rqstp, struct svc_xprt *xprt);
126 void svc_close_xprt(struct svc_xprt *xprt);
127 int svc_port_is_privileged(struct sockaddr *sin);
128 int svc_print_xprts(char *buf, int maxlen);
129 struct svc_xprt *svc_find_xprt(struct svc_serv *serv, const char *xcl_name,
130 struct net *net, const sa_family_t af,
131 const unsigned short port);
132 int svc_xprt_names(struct svc_serv *serv, char *buf, const int buflen);
133 void svc_add_new_perm_xprt(struct svc_serv *serv, struct svc_xprt *xprt);
134 void svc_age_temp_xprts_now(struct svc_serv *, struct sockaddr *);
135
svc_xprt_get(struct svc_xprt * xprt)136 static inline void svc_xprt_get(struct svc_xprt *xprt)
137 {
138 kref_get(&xprt->xpt_ref);
139 }
svc_xprt_set_local(struct svc_xprt * xprt,const struct sockaddr * sa,const size_t salen)140 static inline void svc_xprt_set_local(struct svc_xprt *xprt,
141 const struct sockaddr *sa,
142 const size_t salen)
143 {
144 memcpy(&xprt->xpt_local, sa, salen);
145 xprt->xpt_locallen = salen;
146 }
svc_xprt_set_remote(struct svc_xprt * xprt,const struct sockaddr * sa,const size_t salen)147 static inline void svc_xprt_set_remote(struct svc_xprt *xprt,
148 const struct sockaddr *sa,
149 const size_t salen)
150 {
151 memcpy(&xprt->xpt_remote, sa, salen);
152 xprt->xpt_remotelen = salen;
153 }
svc_addr_port(const struct sockaddr * sa)154 static inline unsigned short svc_addr_port(const struct sockaddr *sa)
155 {
156 const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
157 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)sa;
158
159 switch (sa->sa_family) {
160 case AF_INET:
161 return ntohs(sin->sin_port);
162 case AF_INET6:
163 return ntohs(sin6->sin6_port);
164 }
165
166 return 0;
167 }
168
svc_addr_len(const struct sockaddr * sa)169 static inline size_t svc_addr_len(const struct sockaddr *sa)
170 {
171 switch (sa->sa_family) {
172 case AF_INET:
173 return sizeof(struct sockaddr_in);
174 case AF_INET6:
175 return sizeof(struct sockaddr_in6);
176 }
177 BUG();
178 }
179
svc_xprt_local_port(const struct svc_xprt * xprt)180 static inline unsigned short svc_xprt_local_port(const struct svc_xprt *xprt)
181 {
182 return svc_addr_port((const struct sockaddr *)&xprt->xpt_local);
183 }
184
svc_xprt_remote_port(const struct svc_xprt * xprt)185 static inline unsigned short svc_xprt_remote_port(const struct svc_xprt *xprt)
186 {
187 return svc_addr_port((const struct sockaddr *)&xprt->xpt_remote);
188 }
189
__svc_print_addr(const struct sockaddr * addr,char * buf,const size_t len)190 static inline char *__svc_print_addr(const struct sockaddr *addr,
191 char *buf, const size_t len)
192 {
193 const struct sockaddr_in *sin = (const struct sockaddr_in *)addr;
194 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)addr;
195
196 switch (addr->sa_family) {
197 case AF_INET:
198 snprintf(buf, len, "%pI4, port=%u", &sin->sin_addr,
199 ntohs(sin->sin_port));
200 break;
201
202 case AF_INET6:
203 snprintf(buf, len, "%pI6, port=%u",
204 &sin6->sin6_addr,
205 ntohs(sin6->sin6_port));
206 break;
207
208 default:
209 snprintf(buf, len, "unknown address type: %d", addr->sa_family);
210 break;
211 }
212
213 return buf;
214 }
215 #endif /* SUNRPC_SVC_XPRT_H */
216