Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  *  linux/include/linux/sunrpc/xprt.h
0004  *
0005  *  Declarations for the RPC transport interface.
0006  *
0007  *  Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
0008  */
0009 
0010 #ifndef _LINUX_SUNRPC_XPRT_H
0011 #define _LINUX_SUNRPC_XPRT_H
0012 
0013 #include <linux/uio.h>
0014 #include <linux/socket.h>
0015 #include <linux/in.h>
0016 #include <linux/ktime.h>
0017 #include <linux/kref.h>
0018 #include <linux/sunrpc/sched.h>
0019 #include <linux/sunrpc/xdr.h>
0020 #include <linux/sunrpc/msg_prot.h>
0021 
0022 #define RPC_MIN_SLOT_TABLE  (2U)
0023 #define RPC_DEF_SLOT_TABLE  (16U)
0024 #define RPC_MAX_SLOT_TABLE_LIMIT    (65536U)
0025 #define RPC_MAX_SLOT_TABLE  RPC_MAX_SLOT_TABLE_LIMIT
0026 
0027 #define RPC_CWNDSHIFT       (8U)
0028 #define RPC_CWNDSCALE       (1U << RPC_CWNDSHIFT)
0029 #define RPC_INITCWND        RPC_CWNDSCALE
0030 #define RPC_MAXCWND(xprt)   ((xprt)->max_reqs << RPC_CWNDSHIFT)
0031 #define RPCXPRT_CONGESTED(xprt) ((xprt)->cong >= (xprt)->cwnd)
0032 
0033 /*
0034  * This describes a timeout strategy
0035  */
0036 struct rpc_timeout {
0037     unsigned long       to_initval,     /* initial timeout */
0038                 to_maxval,      /* max timeout */
0039                 to_increment;       /* if !exponential */
0040     unsigned int        to_retries;     /* max # of retries */
0041     unsigned char       to_exponential;
0042 };
0043 
0044 enum rpc_display_format_t {
0045     RPC_DISPLAY_ADDR = 0,
0046     RPC_DISPLAY_PORT,
0047     RPC_DISPLAY_PROTO,
0048     RPC_DISPLAY_HEX_ADDR,
0049     RPC_DISPLAY_HEX_PORT,
0050     RPC_DISPLAY_NETID,
0051     RPC_DISPLAY_MAX,
0052 };
0053 
0054 struct rpc_task;
0055 struct rpc_xprt;
0056 struct xprt_class;
0057 struct seq_file;
0058 struct svc_serv;
0059 struct net;
0060 
0061 /*
0062  * This describes a complete RPC request
0063  */
0064 struct rpc_rqst {
0065     /*
0066      * This is the user-visible part
0067      */
0068     struct rpc_xprt *   rq_xprt;        /* RPC client */
0069     struct xdr_buf      rq_snd_buf;     /* send buffer */
0070     struct xdr_buf      rq_rcv_buf;     /* recv buffer */
0071 
0072     /*
0073      * This is the private part
0074      */
0075     struct rpc_task *   rq_task;    /* RPC task data */
0076     struct rpc_cred *   rq_cred;    /* Bound cred */
0077     __be32          rq_xid;     /* request XID */
0078     int         rq_cong;    /* has incremented xprt->cong */
0079     u32         rq_seqno;   /* gss seq no. used on req. */
0080     int         rq_enc_pages_num;
0081     struct page     **rq_enc_pages; /* scratch pages for use by
0082                            gss privacy code */
0083     void (*rq_release_snd_buf)(struct rpc_rqst *); /* release rq_enc_pages */
0084 
0085     union {
0086         struct list_head    rq_list;    /* Slot allocation list */
0087         struct rb_node      rq_recv;    /* Receive queue */
0088     };
0089 
0090     struct list_head    rq_xmit;    /* Send queue */
0091     struct list_head    rq_xmit2;   /* Send queue */
0092 
0093     void            *rq_buffer; /* Call XDR encode buffer */
0094     size_t          rq_callsize;
0095     void            *rq_rbuffer;    /* Reply XDR decode buffer */
0096     size_t          rq_rcvsize;
0097     size_t          rq_xmit_bytes_sent; /* total bytes sent */
0098     size_t          rq_reply_bytes_recvd;   /* total reply bytes */
0099                             /* received */
0100 
0101     struct xdr_buf      rq_private_buf;     /* The receive buffer
0102                              * used in the softirq.
0103                              */
0104     unsigned long       rq_majortimeo;  /* major timeout alarm */
0105     unsigned long       rq_minortimeo;  /* minor timeout alarm */
0106     unsigned long       rq_timeout; /* Current timeout value */
0107     ktime_t         rq_rtt;     /* round-trip time */
0108     unsigned int        rq_retries; /* # of retries */
0109     unsigned int        rq_connect_cookie;
0110                         /* A cookie used to track the
0111                            state of the transport
0112                            connection */
0113     atomic_t        rq_pin;
0114     
0115     /*
0116      * Partial send handling
0117      */
0118     u32         rq_bytes_sent;  /* Bytes we have sent */
0119 
0120     ktime_t         rq_xtime;   /* transmit time stamp */
0121     int         rq_ntrans;
0122 
0123 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
0124     struct list_head    rq_bc_list; /* Callback service list */
0125     unsigned long       rq_bc_pa_state; /* Backchannel prealloc state */
0126     struct list_head    rq_bc_pa_list;  /* Backchannel prealloc list */
0127 #endif /* CONFIG_SUNRPC_BACKCHANEL */
0128 };
0129 #define rq_svec         rq_snd_buf.head
0130 #define rq_slen         rq_snd_buf.len
0131 
0132 struct rpc_xprt_ops {
0133     void        (*set_buffer_size)(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize);
0134     int     (*reserve_xprt)(struct rpc_xprt *xprt, struct rpc_task *task);
0135     void        (*release_xprt)(struct rpc_xprt *xprt, struct rpc_task *task);
0136     void        (*alloc_slot)(struct rpc_xprt *xprt, struct rpc_task *task);
0137     void        (*free_slot)(struct rpc_xprt *xprt,
0138                      struct rpc_rqst *req);
0139     void        (*rpcbind)(struct rpc_task *task);
0140     void        (*set_port)(struct rpc_xprt *xprt, unsigned short port);
0141     void        (*connect)(struct rpc_xprt *xprt, struct rpc_task *task);
0142     int     (*get_srcaddr)(struct rpc_xprt *xprt, char *buf,
0143                        size_t buflen);
0144     unsigned short  (*get_srcport)(struct rpc_xprt *xprt);
0145     int     (*buf_alloc)(struct rpc_task *task);
0146     void        (*buf_free)(struct rpc_task *task);
0147     int     (*prepare_request)(struct rpc_rqst *req,
0148                        struct xdr_buf *buf);
0149     int     (*send_request)(struct rpc_rqst *req);
0150     void        (*wait_for_reply_request)(struct rpc_task *task);
0151     void        (*timer)(struct rpc_xprt *xprt, struct rpc_task *task);
0152     void        (*release_request)(struct rpc_task *task);
0153     void        (*close)(struct rpc_xprt *xprt);
0154     void        (*destroy)(struct rpc_xprt *xprt);
0155     void        (*set_connect_timeout)(struct rpc_xprt *xprt,
0156                     unsigned long connect_timeout,
0157                     unsigned long reconnect_timeout);
0158     void        (*print_stats)(struct rpc_xprt *xprt, struct seq_file *seq);
0159     int     (*enable_swap)(struct rpc_xprt *xprt);
0160     void        (*disable_swap)(struct rpc_xprt *xprt);
0161     void        (*inject_disconnect)(struct rpc_xprt *xprt);
0162     int     (*bc_setup)(struct rpc_xprt *xprt,
0163                     unsigned int min_reqs);
0164     size_t      (*bc_maxpayload)(struct rpc_xprt *xprt);
0165     unsigned int    (*bc_num_slots)(struct rpc_xprt *xprt);
0166     void        (*bc_free_rqst)(struct rpc_rqst *rqst);
0167     void        (*bc_destroy)(struct rpc_xprt *xprt,
0168                       unsigned int max_reqs);
0169 };
0170 
0171 /*
0172  * RPC transport identifiers
0173  *
0174  * To preserve compatibility with the historical use of raw IP protocol
0175  * id's for transport selection, UDP and TCP identifiers are specified
0176  * with the previous values. No such restriction exists for new transports,
0177  * except that they may not collide with these values (17 and 6,
0178  * respectively).
0179  */
0180 #define XPRT_TRANSPORT_BC       (1 << 31)
0181 enum xprt_transports {
0182     XPRT_TRANSPORT_UDP  = IPPROTO_UDP,
0183     XPRT_TRANSPORT_TCP  = IPPROTO_TCP,
0184     XPRT_TRANSPORT_BC_TCP   = IPPROTO_TCP | XPRT_TRANSPORT_BC,
0185     XPRT_TRANSPORT_RDMA = 256,
0186     XPRT_TRANSPORT_BC_RDMA  = XPRT_TRANSPORT_RDMA | XPRT_TRANSPORT_BC,
0187     XPRT_TRANSPORT_LOCAL    = 257,
0188 };
0189 
0190 struct rpc_sysfs_xprt;
0191 struct rpc_xprt {
0192     struct kref     kref;       /* Reference count */
0193     const struct rpc_xprt_ops *ops;     /* transport methods */
0194     unsigned int        id;     /* transport id */
0195 
0196     const struct rpc_timeout *timeout;  /* timeout parms */
0197     struct sockaddr_storage addr;       /* server address */
0198     size_t          addrlen;    /* size of server address */
0199     int         prot;       /* IP protocol */
0200 
0201     unsigned long       cong;       /* current congestion */
0202     unsigned long       cwnd;       /* congestion window */
0203 
0204     size_t          max_payload;    /* largest RPC payload size,
0205                            in bytes */
0206 
0207     struct rpc_wait_queue   binding;    /* requests waiting on rpcbind */
0208     struct rpc_wait_queue   sending;    /* requests waiting to send */
0209     struct rpc_wait_queue   pending;    /* requests in flight */
0210     struct rpc_wait_queue   backlog;    /* waiting for slot */
0211     struct list_head    free;       /* free slots */
0212     unsigned int        max_reqs;   /* max number of slots */
0213     unsigned int        min_reqs;   /* min number of slots */
0214     unsigned int        num_reqs;   /* total slots */
0215     unsigned long       state;      /* transport state */
0216     unsigned char       resvport   : 1, /* use a reserved port */
0217                 reuseport  : 1; /* reuse port on reconnect */
0218     atomic_t        swapper;    /* we're swapping over this
0219                            transport */
0220     unsigned int        bind_index; /* bind function index */
0221 
0222     /*
0223      * Multipath
0224      */
0225     struct list_head    xprt_switch;
0226 
0227     /*
0228      * Connection of transports
0229      */
0230     unsigned long       bind_timeout,
0231                 reestablish_timeout;
0232     unsigned int        connect_cookie; /* A cookie that gets bumped
0233                            every time the transport
0234                            is reconnected */
0235 
0236     /*
0237      * Disconnection of idle transports
0238      */
0239     struct work_struct  task_cleanup;
0240     struct timer_list   timer;
0241     unsigned long       last_used,
0242                 idle_timeout,
0243                 connect_timeout,
0244                 max_reconnect_timeout;
0245 
0246     /*
0247      * Send stuff
0248      */
0249     atomic_long_t       queuelen;
0250     spinlock_t      transport_lock; /* lock transport info */
0251     spinlock_t      reserve_lock;   /* lock slot table */
0252     spinlock_t      queue_lock; /* send/receive queue lock */
0253     u32         xid;        /* Next XID value to use */
0254     struct rpc_task *   snd_task;   /* Task blocked in send */
0255 
0256     struct list_head    xmit_queue; /* Send queue */
0257     atomic_long_t       xmit_queuelen;
0258 
0259     struct svc_xprt     *bc_xprt;   /* NFSv4.1 backchannel */
0260 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
0261     struct svc_serv     *bc_serv;       /* The RPC service which will */
0262                         /* process the callback */
0263     unsigned int        bc_alloc_max;
0264     unsigned int        bc_alloc_count; /* Total number of preallocs */
0265     atomic_t        bc_slot_count;  /* Number of allocated slots */
0266     spinlock_t      bc_pa_lock; /* Protects the preallocated
0267                          * items */
0268     struct list_head    bc_pa_list; /* List of preallocated
0269                          * backchannel rpc_rqst's */
0270 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
0271 
0272     struct rb_root      recv_queue; /* Receive queue */
0273 
0274     struct {
0275         unsigned long       bind_count, /* total number of binds */
0276                     connect_count,  /* total number of connects */
0277                     connect_start,  /* connect start timestamp */
0278                     connect_time,   /* jiffies waiting for connect */
0279                     sends,      /* how many complete requests */
0280                     recvs,      /* how many complete requests */
0281                     bad_xids,   /* lookup_rqst didn't find XID */
0282                     max_slots;  /* max rpc_slots used */
0283 
0284         unsigned long long  req_u,      /* average requests on the wire */
0285                     bklog_u,    /* backlog queue utilization */
0286                     sending_u,  /* send q utilization */
0287                     pending_u;  /* pend q utilization */
0288     } stat;
0289 
0290     struct net      *xprt_net;
0291     netns_tracker       ns_tracker;
0292     const char      *servername;
0293     const char      *address_strings[RPC_DISPLAY_MAX];
0294 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
0295     struct dentry       *debugfs;       /* debugfs directory */
0296 #endif
0297     struct rcu_head     rcu;
0298     const struct xprt_class *xprt_class;
0299     struct rpc_sysfs_xprt   *xprt_sysfs;
0300     bool            main; /*mark if this is the 1st transport */
0301 };
0302 
0303 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
0304 /*
0305  * Backchannel flags
0306  */
0307 #define RPC_BC_PA_IN_USE    0x0001      /* Preallocated backchannel */
0308                         /* buffer in use */
0309 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
0310 
0311 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
0312 static inline int bc_prealloc(struct rpc_rqst *req)
0313 {
0314     return test_bit(RPC_BC_PA_IN_USE, &req->rq_bc_pa_state);
0315 }
0316 #else
0317 static inline int bc_prealloc(struct rpc_rqst *req)
0318 {
0319     return 0;
0320 }
0321 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
0322 
0323 #define XPRT_CREATE_INFINITE_SLOTS  (1U)
0324 #define XPRT_CREATE_NO_IDLE_TIMEOUT (1U << 1)
0325 
0326 struct xprt_create {
0327     int         ident;      /* XPRT_TRANSPORT identifier */
0328     struct net *        net;
0329     struct sockaddr *   srcaddr;    /* optional local address */
0330     struct sockaddr *   dstaddr;    /* remote peer address */
0331     size_t          addrlen;
0332     const char      *servername;
0333     struct svc_xprt     *bc_xprt;   /* NFSv4.1 backchannel */
0334     struct rpc_xprt_switch  *bc_xps;
0335     unsigned int        flags;
0336 };
0337 
0338 struct xprt_class {
0339     struct list_head    list;
0340     int         ident;      /* XPRT_TRANSPORT identifier */
0341     struct rpc_xprt *   (*setup)(struct xprt_create *);
0342     struct module       *owner;
0343     char            name[32];
0344     const char *        netid[];
0345 };
0346 
0347 /*
0348  * Generic internal transport functions
0349  */
0350 struct rpc_xprt     *xprt_create_transport(struct xprt_create *args);
0351 void            xprt_connect(struct rpc_task *task);
0352 unsigned long       xprt_reconnect_delay(const struct rpc_xprt *xprt);
0353 void            xprt_reconnect_backoff(struct rpc_xprt *xprt,
0354                            unsigned long init_to);
0355 void            xprt_reserve(struct rpc_task *task);
0356 void            xprt_retry_reserve(struct rpc_task *task);
0357 int         xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task);
0358 int         xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task);
0359 void            xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task);
0360 void            xprt_free_slot(struct rpc_xprt *xprt,
0361                        struct rpc_rqst *req);
0362 bool            xprt_prepare_transmit(struct rpc_task *task);
0363 void            xprt_request_enqueue_transmit(struct rpc_task *task);
0364 int         xprt_request_enqueue_receive(struct rpc_task *task);
0365 void            xprt_request_wait_receive(struct rpc_task *task);
0366 void            xprt_request_dequeue_xprt(struct rpc_task *task);
0367 bool            xprt_request_need_retransmit(struct rpc_task *task);
0368 void            xprt_transmit(struct rpc_task *task);
0369 void            xprt_end_transmit(struct rpc_task *task);
0370 int         xprt_adjust_timeout(struct rpc_rqst *req);
0371 void            xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task);
0372 void            xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task);
0373 void            xprt_release(struct rpc_task *task);
0374 struct rpc_xprt *   xprt_get(struct rpc_xprt *xprt);
0375 void            xprt_put(struct rpc_xprt *xprt);
0376 struct rpc_xprt *   xprt_alloc(struct net *net, size_t size,
0377                 unsigned int num_prealloc,
0378                 unsigned int max_req);
0379 void            xprt_free(struct rpc_xprt *);
0380 void            xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task);
0381 bool            xprt_wake_up_backlog(struct rpc_xprt *xprt, struct rpc_rqst *req);
0382 void            xprt_cleanup_ids(void);
0383 
0384 static inline int
0385 xprt_enable_swap(struct rpc_xprt *xprt)
0386 {
0387     return xprt->ops->enable_swap(xprt);
0388 }
0389 
0390 static inline void
0391 xprt_disable_swap(struct rpc_xprt *xprt)
0392 {
0393     xprt->ops->disable_swap(xprt);
0394 }
0395 
0396 /*
0397  * Transport switch helper functions
0398  */
0399 int         xprt_register_transport(struct xprt_class *type);
0400 int         xprt_unregister_transport(struct xprt_class *type);
0401 int         xprt_find_transport_ident(const char *);
0402 void            xprt_wait_for_reply_request_def(struct rpc_task *task);
0403 void            xprt_wait_for_reply_request_rtt(struct rpc_task *task);
0404 void            xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status);
0405 void            xprt_wait_for_buffer_space(struct rpc_xprt *xprt);
0406 bool            xprt_write_space(struct rpc_xprt *xprt);
0407 void            xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result);
0408 struct rpc_rqst *   xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid);
0409 void            xprt_update_rtt(struct rpc_task *task);
0410 void            xprt_complete_rqst(struct rpc_task *task, int copied);
0411 void            xprt_pin_rqst(struct rpc_rqst *req);
0412 void            xprt_unpin_rqst(struct rpc_rqst *req);
0413 void            xprt_release_rqst_cong(struct rpc_task *task);
0414 bool            xprt_request_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req);
0415 void            xprt_disconnect_done(struct rpc_xprt *xprt);
0416 void            xprt_force_disconnect(struct rpc_xprt *xprt);
0417 void            xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie);
0418 
0419 bool            xprt_lock_connect(struct rpc_xprt *, struct rpc_task *, void *);
0420 void            xprt_unlock_connect(struct rpc_xprt *, void *);
0421 void            xprt_release_write(struct rpc_xprt *, struct rpc_task *);
0422 
0423 /*
0424  * Reserved bit positions in xprt->state
0425  */
0426 #define XPRT_LOCKED     (0)
0427 #define XPRT_CONNECTED      (1)
0428 #define XPRT_CONNECTING     (2)
0429 #define XPRT_CLOSE_WAIT     (3)
0430 #define XPRT_BOUND      (4)
0431 #define XPRT_BINDING        (5)
0432 #define XPRT_CLOSING        (6)
0433 #define XPRT_OFFLINE        (7)
0434 #define XPRT_REMOVE     (8)
0435 #define XPRT_CONGESTED      (9)
0436 #define XPRT_CWND_WAIT      (10)
0437 #define XPRT_WRITE_SPACE    (11)
0438 #define XPRT_SND_IS_COOKIE  (12)
0439 
0440 static inline void xprt_set_connected(struct rpc_xprt *xprt)
0441 {
0442     set_bit(XPRT_CONNECTED, &xprt->state);
0443 }
0444 
0445 static inline void xprt_clear_connected(struct rpc_xprt *xprt)
0446 {
0447     clear_bit(XPRT_CONNECTED, &xprt->state);
0448 }
0449 
0450 static inline int xprt_connected(struct rpc_xprt *xprt)
0451 {
0452     return test_bit(XPRT_CONNECTED, &xprt->state);
0453 }
0454 
0455 static inline int xprt_test_and_set_connected(struct rpc_xprt *xprt)
0456 {
0457     return test_and_set_bit(XPRT_CONNECTED, &xprt->state);
0458 }
0459 
0460 static inline int xprt_test_and_clear_connected(struct rpc_xprt *xprt)
0461 {
0462     return test_and_clear_bit(XPRT_CONNECTED, &xprt->state);
0463 }
0464 
0465 static inline void xprt_clear_connecting(struct rpc_xprt *xprt)
0466 {
0467     smp_mb__before_atomic();
0468     clear_bit(XPRT_CONNECTING, &xprt->state);
0469     smp_mb__after_atomic();
0470 }
0471 
0472 static inline int xprt_connecting(struct rpc_xprt *xprt)
0473 {
0474     return test_bit(XPRT_CONNECTING, &xprt->state);
0475 }
0476 
0477 static inline int xprt_test_and_set_connecting(struct rpc_xprt *xprt)
0478 {
0479     return test_and_set_bit(XPRT_CONNECTING, &xprt->state);
0480 }
0481 
0482 static inline void xprt_set_bound(struct rpc_xprt *xprt)
0483 {
0484     test_and_set_bit(XPRT_BOUND, &xprt->state);
0485 }
0486 
0487 static inline int xprt_bound(struct rpc_xprt *xprt)
0488 {
0489     return test_bit(XPRT_BOUND, &xprt->state);
0490 }
0491 
0492 static inline void xprt_clear_bound(struct rpc_xprt *xprt)
0493 {
0494     clear_bit(XPRT_BOUND, &xprt->state);
0495 }
0496 
0497 static inline void xprt_clear_binding(struct rpc_xprt *xprt)
0498 {
0499     smp_mb__before_atomic();
0500     clear_bit(XPRT_BINDING, &xprt->state);
0501     smp_mb__after_atomic();
0502 }
0503 
0504 static inline int xprt_test_and_set_binding(struct rpc_xprt *xprt)
0505 {
0506     return test_and_set_bit(XPRT_BINDING, &xprt->state);
0507 }
0508 
0509 void xprt_set_offline_locked(struct rpc_xprt *xprt, struct rpc_xprt_switch *xps);
0510 void xprt_set_online_locked(struct rpc_xprt *xprt, struct rpc_xprt_switch *xps);
0511 void xprt_delete_locked(struct rpc_xprt *xprt, struct rpc_xprt_switch *xps);
0512 #endif /* _LINUX_SUNRPC_XPRT_H */