xprt.h 16 KB

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