Print this page
    
NEX-16052 NFS clients cannot reconnect to server after network loss (autofs)
Reviewed by: Yuri Pankov <yuri.pankov@nexenta.com>
Reviewed by: Joyce McIntosh <joyce.mcintosh@nexenta.com>
Reviewed by: Rick McNeal <rick.mcneal@nexenta.com>
Reviewed by: Gordon Ross <gordon.ross@nexenta.com>
NEX-4123 xdrmblk_getpos() is unreliable
Reviewed by: Josef 'Jeff' Sipek <josef.sipek@nexenta.com>
Reviewed by: Yuri Pankov <yuri.pankov@nexenta.com>
re #13613 rb4516 Tunables needs volatile keyword
    
      
        | Split | 
	Close | 
      
      | Expand all | 
      | Collapse all | 
    
    
          --- old/usr/src/uts/common/rpc/clnt_cots.c
          +++ new/usr/src/uts/common/rpc/clnt_cots.c
   1    1  /*
   2    2   * CDDL HEADER START
   3    3   *
   4    4   * The contents of this file are subject to the terms of the
   5    5   * Common Development and Distribution License (the "License").
   6    6   * You may not use this file except in compliance with the License.
   7    7   *
   8    8   * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9    9   * or http://www.opensolaris.org/os/licensing.
  10   10   * See the License for the specific language governing permissions
  11   11   * and limitations under the License.
  12   12   *
  
    | 
      ↓ open down ↓ | 
    12 lines elided | 
    
      ↑ open up ↑ | 
  
  13   13   * When distributing Covered Code, include this CDDL HEADER in each
  14   14   * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15   15   * If applicable, add the following below this CDDL HEADER, with the
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  
  22   22  /*
  23      - * Copyright 2016 Nexenta Systems, Inc.  All rights reserved.
       23 + * Copyright 2018 Nexenta Systems, Inc.  All rights reserved.
  24   24   * Copyright (c) 2016 by Delphix. All rights reserved.
  25   25   */
  26   26  
  27   27  /*
  28   28   * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  29   29   * Use is subject to license terms.
  30   30   */
  31   31  
  32   32  /*
  33   33   * Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T
  34   34   *              All Rights Reserved
  35   35   */
  36   36  
  37   37  /*
  38   38   * Portions of this source code were derived from Berkeley 4.3 BSD
  39   39   * under license from the Regents of the University of California.
  40   40   */
  41   41  
  42   42  
  43   43  /*
  44   44   * Implements a kernel based, client side RPC over Connection Oriented
  45   45   * Transports (COTS).
  46   46   */
  47   47  
  48   48  /*
  49   49   * Much of this file has been re-written to let NFS work better over slow
  50   50   * transports. A description follows.
  51   51   *
  52   52   * One of the annoying things about kRPC/COTS is that it will temporarily
  53   53   * create more than one connection between a client and server. This
  54   54   * happens because when a connection is made, the end-points entry in the
  55   55   * linked list of connections (headed by cm_hd), is removed so that other
  56   56   * threads don't mess with it. Went ahead and bit the bullet by keeping
  57   57   * the endpoint on the connection list and introducing state bits,
  58   58   * condition variables etc. to the connection entry data structure (struct
  59   59   * cm_xprt).
  60   60   *
  61   61   * Here is a summary of the changes to cm-xprt:
  62   62   *
  63   63   *      x_ctime is the timestamp of when the endpoint was last
  64   64   *      connected or disconnected. If an end-point is ever disconnected
  65   65   *      or re-connected, then any outstanding RPC request is presumed
  66   66   *      lost, telling clnt_cots_kcallit that it needs to re-send the
  67   67   *      request, not just wait for the original request's reply to
  68   68   *      arrive.
  69   69   *
  70   70   *      x_thread flag which tells us if a thread is doing a connection attempt.
  71   71   *
  72   72   *      x_waitdis flag which tells us we are waiting a disconnect ACK.
  73   73   *
  74   74   *      x_needdis flag which tells us we need to send a T_DISCONN_REQ
  75   75   *      to kill the connection.
  76   76   *
  77   77   *      x_needrel flag which tells us we need to send a T_ORDREL_REQ to
  78   78   *      gracefully close the connection.
  79   79   *
  80   80   *      #defined bitmasks for the all the b_* bits so that more
  81   81   *      efficient (and at times less clumsy) masks can be used to
  82   82   *      manipulated state in cases where multiple bits have to
  83   83   *      set/cleared/checked in the same critical section.
  84   84   *
  85   85   *      x_conn_cv and x_dis-_cv are new condition variables to let
  86   86   *      threads knows when the connection attempt is done, and to let
  87   87   *      the connecting thread know when the disconnect handshake is
  88   88   *      done.
  89   89   *
  90   90   * Added the CONN_HOLD() macro so that all reference holds have the same
  91   91   * look and feel.
  92   92   *
  93   93   * In the private (cku_private) portion of the client handle,
  94   94   *
  95   95   *      cku_flags replaces the cku_sent a boolean. cku_flags keeps
  96   96   *      track of whether a request as been sent, and whether the
  97   97   *      client's handles call record is on the dispatch list (so that
  98   98   *      the reply can be matched by XID to the right client handle).
  99   99   *      The idea of CKU_ONQUEUE is that we can exit clnt_cots_kcallit()
 100  100   *      and still have the response find the right client handle so
 101  101   *      that the retry of CLNT_CALL() gets the result. Testing, found
 102  102   *      situations where if the timeout was increased, performance
 103  103   *      degraded. This was due to us hitting a window where the thread
 104  104   *      was back in rfscall() (probably printing server not responding)
 105  105   *      while the response came back but no place to put it.
 106  106   *
 107  107   *      cku_ctime is just a cache of x_ctime. If they match,
 108  108   *      clnt_cots_kcallit() won't to send a retry (unless the maximum
 109  109   *      receive count limit as been reached). If the don't match, then
 110  110   *      we assume the request has been lost, and a retry of the request
 111  111   *      is needed.
 112  112   *
 113  113   *      cku_recv_attempts counts the number of receive count attempts
 114  114   *      after one try is sent on the wire.
 115  115   *
 116  116   * Added the clnt_delay() routine so that interruptible and
 117  117   * noninterruptible delays are possible.
 118  118   *
 119  119   * CLNT_MIN_TIMEOUT has been bumped to 10 seconds from 3. This is used to
 120  120   * control how long the client delays before returned after getting
 121  121   * ECONNREFUSED. At 3 seconds, 8 client threads per mount really does bash
 122  122   * a server that may be booting and not yet started nfsd.
 123  123   *
 124  124   * CLNT_MAXRECV_WITHOUT_RETRY is a new macro (value of 3) (with a tunable)
 125  125   * Why don't we just wait forever (receive an infinite # of times)?
 126  126   * Because the server may have rebooted. More insidious is that some
 127  127   * servers (ours) will drop NFS/TCP requests in some cases. This is bad,
 128  128   * but it is a reality.
 129  129   *
 130  130   * The case of a server doing orderly release really messes up the
 131  131   * client's recovery, especially if the server's TCP implementation is
 132  132   * buggy.  It was found was that the kRPC/COTS client was breaking some
 133  133   * TPI rules, such as not waiting for the acknowledgement of a
 134  134   * T_DISCON_REQ (hence the added case statements T_ERROR_ACK, T_OK_ACK and
 135  135   * T_DISCON_REQ in clnt_dispatch_notifyall()).
 136  136   *
 137  137   * One of things that we've seen is that a kRPC TCP endpoint goes into
 138  138   * TIMEWAIT and a thus a reconnect takes a long time to satisfy because
 139  139   * that the TIMEWAIT state takes a while to finish.  If a server sends a
 140  140   * T_ORDREL_IND, there is little point in an RPC client doing a
 141  141   * T_ORDREL_REQ, because the RPC request isn't going to make it (the
 142  142   * server is saying that it won't accept any more data). So kRPC was
 143  143   * changed to send a T_DISCON_REQ when we get a T_ORDREL_IND. So now the
 144  144   * connection skips the TIMEWAIT state and goes straight to a bound state
 145  145   * that kRPC can quickly switch to connected.
 146  146   *
 147  147   * Code that issues TPI request must use waitforack() to wait for the
 148  148   * corresponding ack (assuming there is one) in any future modifications.
 149  149   * This works around problems that may be introduced by breaking TPI rules
 150  150   * (by submitting new calls before earlier requests have been acked) in the
 151  151   * case of a signal or other early return.  waitforack() depends on
 152  152   * clnt_dispatch_notifyconn() to issue the wakeup when the ack
 153  153   * arrives, so adding new TPI calls may require corresponding changes
 154  154   * to clnt_dispatch_notifyconn(). Presently, the timeout period is based on
 155  155   * CLNT_MIN_TIMEOUT which is 10 seconds. If you modify this value, be sure
 156  156   * not to set it too low or TPI ACKS will be lost.
 157  157   */
 158  158  
 159  159  #include <sys/param.h>
 160  160  #include <sys/types.h>
 161  161  #include <sys/user.h>
 162  162  #include <sys/systm.h>
 163  163  #include <sys/sysmacros.h>
 164  164  #include <sys/proc.h>
 165  165  #include <sys/socket.h>
 166  166  #include <sys/file.h>
 167  167  #include <sys/stream.h>
 168  168  #include <sys/strsubr.h>
 169  169  #include <sys/stropts.h>
 170  170  #include <sys/strsun.h>
 171  171  #include <sys/timod.h>
 172  172  #include <sys/tiuser.h>
 173  173  #include <sys/tihdr.h>
 174  174  #include <sys/t_kuser.h>
 175  175  #include <sys/fcntl.h>
 176  176  #include <sys/errno.h>
 177  177  #include <sys/kmem.h>
 178  178  #include <sys/debug.h>
 179  179  #include <sys/systm.h>
 180  180  #include <sys/kstat.h>
 181  181  #include <sys/t_lock.h>
 182  182  #include <sys/ddi.h>
 183  183  #include <sys/cmn_err.h>
 184  184  #include <sys/time.h>
 185  185  #include <sys/isa_defs.h>
 186  186  #include <sys/callb.h>
 187  187  #include <sys/sunddi.h>
 188  188  #include <sys/atomic.h>
 189  189  #include <sys/sdt.h>
 190  190  
 191  191  #include <netinet/in.h>
 192  192  #include <netinet/tcp.h>
 193  193  
 194  194  #include <rpc/types.h>
 195  195  #include <rpc/xdr.h>
 196  196  #include <rpc/auth.h>
 197  197  #include <rpc/clnt.h>
 198  198  #include <rpc/rpc_msg.h>
 199  199  
 200  200  #define COTS_DEFAULT_ALLOCSIZE  2048
 201  201  
 202  202  #define WIRE_HDR_SIZE   20      /* serialized call header, sans proc number */
 203  203  #define MSG_OFFSET      128     /* offset of call into the mblk */
 204  204  
 205  205  const char *kinet_ntop6(uchar_t *, char *, size_t);
 206  206  
 207  207  static int      clnt_cots_ksettimers(CLIENT *, struct rpc_timers *,
 208  208      struct rpc_timers *, int, void(*)(int, int, caddr_t), caddr_t, uint32_t);
 209  209  static enum clnt_stat   clnt_cots_kcallit(CLIENT *, rpcproc_t, xdrproc_t,
 210  210      caddr_t, xdrproc_t, caddr_t, struct timeval);
 211  211  static void     clnt_cots_kabort(CLIENT *);
 212  212  static void     clnt_cots_kerror(CLIENT *, struct rpc_err *);
 213  213  static bool_t   clnt_cots_kfreeres(CLIENT *, xdrproc_t, caddr_t);
 214  214  static void     clnt_cots_kdestroy(CLIENT *);
 215  215  static bool_t   clnt_cots_kcontrol(CLIENT *, int, char *);
 216  216  
 217  217  
 218  218  /* List of transports managed by the connection manager. */
 219  219  struct cm_xprt {
 220  220          TIUSER          *x_tiptr;       /* transport handle */
 221  221          queue_t         *x_wq;          /* send queue */
 222  222          clock_t         x_time;         /* last time we handed this xprt out */
 223  223          clock_t         x_ctime;        /* time we went to CONNECTED */
 224  224          int             x_tidu_size;    /* TIDU size of this transport */
 225  225          union {
 226  226              struct {
 227  227                  unsigned int
 228  228  #ifdef  _BIT_FIELDS_HTOL
 229  229                  b_closing:      1,      /* we've sent a ord rel on this conn */
 230  230                  b_dead:         1,      /* transport is closed or disconn */
 231  231                  b_doomed:       1,      /* too many conns, let this go idle */
 232  232                  b_connected:    1,      /* this connection is connected */
 233  233  
 234  234                  b_ordrel:       1,      /* do an orderly release? */
 235  235                  b_thread:       1,      /* thread doing connect */
 236  236                  b_waitdis:      1,      /* waiting for disconnect ACK */
 237  237                  b_needdis:      1,      /* need T_DISCON_REQ */
 238  238  
 239  239                  b_needrel:      1,      /* need T_ORDREL_REQ */
 240  240                  b_early_disc:   1,      /* got a T_ORDREL_IND or T_DISCON_IND */
 241  241                                          /* disconnect during connect */
 242  242  
 243  243                  b_pad:          22;
 244  244  
 245  245  #endif
 246  246  
 247  247  #ifdef  _BIT_FIELDS_LTOH
 248  248                  b_pad:          22,
 249  249  
 250  250                  b_early_disc:   1,      /* got a T_ORDREL_IND or T_DISCON_IND */
 251  251                                          /* disconnect during connect */
 252  252                  b_needrel:      1,      /* need T_ORDREL_REQ */
 253  253  
 254  254                  b_needdis:      1,      /* need T_DISCON_REQ */
 255  255                  b_waitdis:      1,      /* waiting for disconnect ACK */
 256  256                  b_thread:       1,      /* thread doing connect */
 257  257                  b_ordrel:       1,      /* do an orderly release? */
 258  258  
 259  259                  b_connected:    1,      /* this connection is connected */
 260  260                  b_doomed:       1,      /* too many conns, let this go idle */
 261  261                  b_dead:         1,      /* transport is closed or disconn */
 262  262                  b_closing:      1;      /* we've sent a ord rel on this conn */
 263  263  #endif
 264  264              } bit;          unsigned int word;
 265  265  
 266  266  #define x_closing       x_state.bit.b_closing
 267  267  #define x_dead          x_state.bit.b_dead
 268  268  #define x_doomed        x_state.bit.b_doomed
 269  269  #define x_connected     x_state.bit.b_connected
 270  270  
 271  271  #define x_ordrel        x_state.bit.b_ordrel
 272  272  #define x_thread        x_state.bit.b_thread
 273  273  #define x_waitdis       x_state.bit.b_waitdis
 274  274  #define x_needdis       x_state.bit.b_needdis
 275  275  
 276  276  #define x_needrel       x_state.bit.b_needrel
 277  277  #define x_early_disc    x_state.bit.b_early_disc
 278  278  
 279  279  #define x_state_flags   x_state.word
 280  280  
 281  281  #define X_CLOSING       0x80000000
 282  282  #define X_DEAD          0x40000000
 283  283  #define X_DOOMED        0x20000000
 284  284  #define X_CONNECTED     0x10000000
 285  285  
 286  286  #define X_ORDREL        0x08000000
 287  287  #define X_THREAD        0x04000000
 288  288  #define X_WAITDIS       0x02000000
 289  289  #define X_NEEDDIS       0x01000000
 290  290  
 291  291  #define X_NEEDREL       0x00800000
 292  292  #define X_EARLYDISC     0x00400000
 293  293  
 294  294  #define X_BADSTATES     (X_CLOSING | X_DEAD | X_DOOMED)
 295  295  
 296  296          }               x_state;
 297  297          int             x_ref;          /* number of users of this xprt */
 298  298          int             x_family;       /* address family of transport */
 299  299          dev_t           x_rdev;         /* device number of transport */
 300  300          struct cm_xprt  *x_next;
 301  301  
 302  302          struct netbuf   x_server;       /* destination address */
 303  303          struct netbuf   x_src;          /* src address (for retries) */
 304  304          kmutex_t        x_lock;         /* lock on this entry */
 305  305          kcondvar_t      x_cv;           /* to signal when can be closed */
 306  306          kcondvar_t      x_conn_cv;      /* to signal when connection attempt */
 307  307                                          /* is complete */
 308  308          kstat_t         *x_ksp;
 309  309  
 310  310          kcondvar_t      x_dis_cv;       /* to signal when disconnect attempt */
 311  311                                          /* is complete */
 312  312          zoneid_t        x_zoneid;       /* zone this xprt belongs to */
 313  313  };
 314  314  
 315  315  typedef struct cm_kstat_xprt {
 316  316          kstat_named_t   x_wq;
 317  317          kstat_named_t   x_server;
 318  318          kstat_named_t   x_family;
 319  319          kstat_named_t   x_rdev;
 320  320          kstat_named_t   x_time;
 321  321          kstat_named_t   x_state;
 322  322          kstat_named_t   x_ref;
 323  323          kstat_named_t   x_port;
 324  324  } cm_kstat_xprt_t;
 325  325  
 326  326  static cm_kstat_xprt_t cm_kstat_template = {
 327  327          { "write_queue", KSTAT_DATA_UINT32 },
 328  328          { "server",     KSTAT_DATA_STRING },
 329  329          { "addr_family", KSTAT_DATA_UINT32 },
 330  330          { "device",     KSTAT_DATA_UINT32 },
 331  331          { "time_stamp", KSTAT_DATA_UINT32 },
 332  332          { "status",     KSTAT_DATA_UINT32 },
 333  333          { "ref_count",  KSTAT_DATA_INT32 },
 334  334          { "port",       KSTAT_DATA_UINT32 },
 335  335  };
 336  336  
 337  337  /*
 338  338   * The inverse of this is connmgr_release().
 339  339   */
 340  340  #define CONN_HOLD(Cm_entry)     {\
 341  341          mutex_enter(&(Cm_entry)->x_lock);       \
 342  342          (Cm_entry)->x_ref++;    \
 343  343          mutex_exit(&(Cm_entry)->x_lock);        \
 344  344  }
 345  345  
 346  346  
 347  347  /*
 348  348   * Private data per rpc handle.  This structure is allocated by
 349  349   * clnt_cots_kcreate, and freed by clnt_cots_kdestroy.
 350  350   */
 351  351  typedef struct cku_private_s {
 352  352          CLIENT                  cku_client;     /* client handle */
 353  353          calllist_t              cku_call;       /* for dispatching calls */
 354  354          struct rpc_err          cku_err;        /* error status */
 355  355  
 356  356          struct netbuf           cku_srcaddr;    /* source address for retries */
 357  357          int                     cku_addrfmly;  /* for binding port */
 358  358          struct netbuf           cku_addr;       /* remote address */
 359  359          dev_t                   cku_device;     /* device to use */
 360  360          uint_t                  cku_flags;
 361  361  #define CKU_ONQUEUE             0x1
 362  362  #define CKU_SENT                0x2
 363  363  
 364  364          bool_t                  cku_progress;   /* for CLSET_PROGRESS */
 365  365          uint32_t                cku_xid;        /* current XID */
 366  366          clock_t                 cku_ctime;      /* time stamp of when */
 367  367                                                  /* connection was created */
 368  368          uint_t                  cku_recv_attempts;
 369  369          XDR                     cku_outxdr;     /* xdr routine for output */
 370  370          XDR                     cku_inxdr;      /* xdr routine for input */
 371  371          char                    cku_rpchdr[WIRE_HDR_SIZE + 4];
 372  372                                                  /* pre-serialized rpc header */
 373  373  
 374  374          uint_t                  cku_outbuflen;  /* default output mblk length */
 375  375          struct cred             *cku_cred;      /* credentials */
 376  376          bool_t                  cku_nodelayonerr;
 377  377                                                  /* for CLSET_NODELAYONERR */
 378  378          int                     cku_useresvport; /* Use reserved port */
 379  379          struct rpc_cots_client  *cku_stats;     /* stats for zone */
 380  380  } cku_private_t;
 381  381  
 382  382  static struct cm_xprt *connmgr_wrapconnect(struct cm_xprt *,
 383  383          const struct timeval *, struct netbuf *, int, struct netbuf *,
 384  384          struct rpc_err *, bool_t, bool_t, cred_t *);
 385  385  
 386  386  static bool_t   connmgr_connect(struct cm_xprt *, queue_t *, struct netbuf *,
 387  387                                  int, calllist_t *, int *, bool_t reconnect,
 388  388                                  const struct timeval *, bool_t, cred_t *);
 389  389  
 390  390  static void     *connmgr_opt_getoff(mblk_t *mp, t_uscalar_t offset,
 391  391                                  t_uscalar_t length, uint_t align_size);
 392  392  static bool_t   connmgr_setbufsz(calllist_t *e, queue_t *wq, cred_t *cr);
 393  393  static bool_t   connmgr_getopt_int(queue_t *wq, int level, int name, int *val,
 394  394                                  calllist_t *e, cred_t *cr);
 395  395  static bool_t   connmgr_setopt_int(queue_t *wq, int level, int name, int val,
 396  396                                  calllist_t *e, cred_t *cr);
 397  397  static bool_t   connmgr_setopt(queue_t *, int, int, calllist_t *, cred_t *cr);
 398  398  static void     connmgr_sndrel(struct cm_xprt *);
 399  399  static void     connmgr_snddis(struct cm_xprt *);
 400  400  static void     connmgr_close(struct cm_xprt *);
 401  401  static void     connmgr_release(struct cm_xprt *);
 402  402  static struct cm_xprt *connmgr_wrapget(struct netbuf *, const struct timeval *,
 403  403          cku_private_t *);
 404  404  
 405  405  static struct cm_xprt *connmgr_get(struct netbuf *, const struct timeval *,
 406  406          struct netbuf *, int, struct netbuf *, struct rpc_err *, dev_t,
 407  407          bool_t, int, cred_t *);
 408  408  
 409  409  static void connmgr_cancelconn(struct cm_xprt *);
 410  410  static enum clnt_stat connmgr_cwait(struct cm_xprt *, const struct timeval *,
 411  411          bool_t);
 412  412  static void connmgr_dis_and_wait(struct cm_xprt *);
 413  413  
 414  414  static int      clnt_dispatch_send(queue_t *, mblk_t *, calllist_t *, uint_t,
 415  415                                          uint_t);
 416  416  
 417  417  static int clnt_delay(clock_t, bool_t);
 418  418  
 419  419  static int waitforack(calllist_t *, t_scalar_t, const struct timeval *, bool_t);
 420  420  
 421  421  /*
 422  422   * Operations vector for TCP/IP based RPC
 423  423   */
 424  424  static struct clnt_ops tcp_ops = {
 425  425          clnt_cots_kcallit,      /* do rpc call */
 426  426          clnt_cots_kabort,       /* abort call */
 427  427          clnt_cots_kerror,       /* return error status */
 428  428          clnt_cots_kfreeres,     /* free results */
 429  429          clnt_cots_kdestroy,     /* destroy rpc handle */
 430  430          clnt_cots_kcontrol,     /* the ioctl() of rpc */
 431  431          clnt_cots_ksettimers,   /* set retry timers */
 432  432  };
 433  433  
 434  434  static int rpc_kstat_instance = 0;  /* keeps the current instance */
 435  435                                  /* number for the next kstat_create */
 436  436  
 437  437  static struct cm_xprt *cm_hd = NULL;
 438  438  static kmutex_t connmgr_lock;   /* for connection mngr's list of transports */
 439  439  
 440  440  extern kmutex_t clnt_max_msg_lock;
 441  441  
 442  442  static calllist_t *clnt_pending = NULL;
 443  443  extern kmutex_t clnt_pending_lock;
 444  444  
 445  445  static int clnt_cots_hash_size = DEFAULT_HASH_SIZE;
 446  446  
 447  447  static call_table_t *cots_call_ht;
 448  448  
 449  449  static const struct rpc_cots_client {
 450  450          kstat_named_t   rccalls;
 451  451          kstat_named_t   rcbadcalls;
 452  452          kstat_named_t   rcbadxids;
 453  453          kstat_named_t   rctimeouts;
 454  454          kstat_named_t   rcnewcreds;
 455  455          kstat_named_t   rcbadverfs;
 456  456          kstat_named_t   rctimers;
 457  457          kstat_named_t   rccantconn;
 458  458          kstat_named_t   rcnomem;
 459  459          kstat_named_t   rcintrs;
 460  460  } cots_rcstat_tmpl = {
 461  461          { "calls",      KSTAT_DATA_UINT64 },
 462  462          { "badcalls",   KSTAT_DATA_UINT64 },
 463  463          { "badxids",    KSTAT_DATA_UINT64 },
 464  464          { "timeouts",   KSTAT_DATA_UINT64 },
 465  465          { "newcreds",   KSTAT_DATA_UINT64 },
 466  466          { "badverfs",   KSTAT_DATA_UINT64 },
  
    | 
      ↓ open down ↓ | 
    433 lines elided | 
    
      ↑ open up ↑ | 
  
 467  467          { "timers",     KSTAT_DATA_UINT64 },
 468  468          { "cantconn",   KSTAT_DATA_UINT64 },
 469  469          { "nomem",      KSTAT_DATA_UINT64 },
 470  470          { "interrupts", KSTAT_DATA_UINT64 }
 471  471  };
 472  472  
 473  473  #define COTSRCSTAT_INCR(p, x)   \
 474  474          atomic_inc_64(&(p)->x.value.ui64)
 475  475  
 476  476  #define CLNT_MAX_CONNS  1       /* concurrent connections between clnt/srvr */
 477      -int clnt_max_conns = CLNT_MAX_CONNS;
      477 +volatile int clnt_max_conns = CLNT_MAX_CONNS;
 478  478  
 479  479  #define CLNT_MIN_TIMEOUT        10      /* seconds to wait after we get a */
 480  480                                          /* connection reset */
 481  481  #define CLNT_MIN_CONNTIMEOUT    5       /* seconds to wait for a connection */
 482  482  
 483  483  
 484  484  int clnt_cots_min_tout = CLNT_MIN_TIMEOUT;
 485  485  int clnt_cots_min_conntout = CLNT_MIN_CONNTIMEOUT;
 486  486  
 487  487  /*
 488  488   * Limit the number of times we will attempt to receive a reply without
 489  489   * re-sending a response.
 490  490   */
 491  491  #define CLNT_MAXRECV_WITHOUT_RETRY      3
 492  492  uint_t clnt_cots_maxrecv        = CLNT_MAXRECV_WITHOUT_RETRY;
 493  493  
 494  494  uint_t *clnt_max_msg_sizep;
 495  495  void (*clnt_stop_idle)(queue_t *wq);
 496  496  
 497  497  #define ptoh(p)         (&((p)->cku_client))
 498  498  #define htop(h)         ((cku_private_t *)((h)->cl_private))
 499  499  
 500  500  /*
 501  501   * Times to retry
 502  502   */
 503  503  #define REFRESHES       2       /* authentication refreshes */
 504  504  
 505  505  /*
 506  506   * The following is used to determine the global default behavior for
 507  507   * COTS when binding to a local port.
 508  508   *
 509  509   * If the value is set to 1 the default will be to select a reserved
 510  510   * (aka privileged) port, if the value is zero the default will be to
 511  511   * use non-reserved ports.  Users of kRPC may override this by using
 512  512   * CLNT_CONTROL() and CLSET_BINDRESVPORT.
 513  513   */
 514  514  int clnt_cots_do_bindresvport = 1;
 515  515  
 516  516  static zone_key_t zone_cots_key;
 517  517  
 518  518  /*
 519  519   * Defaults TCP send and receive buffer size for RPC connections.
 520  520   * These values can be tuned by /etc/system.
 521  521   */
 522  522  int rpc_send_bufsz = 1024*1024;
 523  523  int rpc_recv_bufsz = 1024*1024;
 524  524  /*
 525  525   * To use system-wide default for TCP send and receive buffer size,
 526  526   * use /etc/system to set rpc_default_tcp_bufsz to 1:
 527  527   *
 528  528   * set rpcmod:rpc_default_tcp_bufsz=1
 529  529   */
 530  530  int rpc_default_tcp_bufsz = 0;
 531  531  
 532  532  /*
 533  533   * We need to do this after all kernel threads in the zone have exited.
 534  534   */
 535  535  /* ARGSUSED */
 536  536  static void
 537  537  clnt_zone_destroy(zoneid_t zoneid, void *unused)
 538  538  {
 539  539          struct cm_xprt **cmp;
 540  540          struct cm_xprt *cm_entry;
 541  541          struct cm_xprt *freelist = NULL;
 542  542  
 543  543          mutex_enter(&connmgr_lock);
 544  544          cmp = &cm_hd;
 545  545          while ((cm_entry = *cmp) != NULL) {
 546  546                  if (cm_entry->x_zoneid == zoneid) {
 547  547                          *cmp = cm_entry->x_next;
 548  548                          cm_entry->x_next = freelist;
 549  549                          freelist = cm_entry;
 550  550                  } else {
 551  551                          cmp = &cm_entry->x_next;
 552  552                  }
 553  553          }
 554  554          mutex_exit(&connmgr_lock);
 555  555          while ((cm_entry = freelist) != NULL) {
 556  556                  freelist = cm_entry->x_next;
 557  557                  connmgr_close(cm_entry);
 558  558          }
 559  559  }
 560  560  
 561  561  int
 562  562  clnt_cots_kcreate(dev_t dev, struct netbuf *addr, int family, rpcprog_t prog,
 563  563      rpcvers_t vers, uint_t max_msgsize, cred_t *cred, CLIENT **ncl)
 564  564  {
 565  565          CLIENT *h;
 566  566          cku_private_t *p;
 567  567          struct rpc_msg call_msg;
 568  568          struct rpcstat *rpcstat;
 569  569  
 570  570          RPCLOG(8, "clnt_cots_kcreate: prog %u\n", prog);
 571  571  
 572  572          rpcstat = zone_getspecific(rpcstat_zone_key, rpc_zone());
 573  573          ASSERT(rpcstat != NULL);
 574  574  
 575  575          /* Allocate and intialize the client handle. */
 576  576          p = kmem_zalloc(sizeof (*p), KM_SLEEP);
 577  577  
 578  578          h = ptoh(p);
 579  579  
 580  580          h->cl_private = (caddr_t)p;
 581  581          h->cl_auth = authkern_create();
 582  582          h->cl_ops = &tcp_ops;
 583  583  
 584  584          cv_init(&p->cku_call.call_cv, NULL, CV_DEFAULT, NULL);
 585  585          mutex_init(&p->cku_call.call_lock, NULL, MUTEX_DEFAULT, NULL);
 586  586  
 587  587          /*
 588  588           * If the current sanity check size in rpcmod is smaller
 589  589           * than the size needed, then increase the sanity check.
 590  590           */
 591  591          if (max_msgsize != 0 && clnt_max_msg_sizep != NULL &&
 592  592              max_msgsize > *clnt_max_msg_sizep) {
 593  593                  mutex_enter(&clnt_max_msg_lock);
 594  594                  if (max_msgsize > *clnt_max_msg_sizep)
 595  595                          *clnt_max_msg_sizep = max_msgsize;
 596  596                  mutex_exit(&clnt_max_msg_lock);
 597  597          }
 598  598  
 599  599          p->cku_outbuflen = COTS_DEFAULT_ALLOCSIZE;
 600  600  
 601  601          /* Preserialize the call message header */
 602  602  
 603  603          call_msg.rm_xid = 0;
 604  604          call_msg.rm_direction = CALL;
 605  605          call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
 606  606          call_msg.rm_call.cb_prog = prog;
 607  607          call_msg.rm_call.cb_vers = vers;
 608  608  
 609  609          xdrmem_create(&p->cku_outxdr, p->cku_rpchdr, WIRE_HDR_SIZE, XDR_ENCODE);
 610  610  
 611  611          if (!xdr_callhdr(&p->cku_outxdr, &call_msg)) {
 612  612                  XDR_DESTROY(&p->cku_outxdr);
 613  613                  RPCLOG0(1, "clnt_cots_kcreate - Fatal header serialization "
 614  614                      "error\n");
 615  615                  auth_destroy(h->cl_auth);
 616  616                  kmem_free(p, sizeof (cku_private_t));
 617  617                  RPCLOG0(1, "clnt_cots_kcreate: create failed error EINVAL\n");
 618  618                  return (EINVAL);                /* XXX */
 619  619          }
 620  620          XDR_DESTROY(&p->cku_outxdr);
 621  621  
 622  622          /*
 623  623           * The zalloc initialized the fields below.
 624  624           * p->cku_xid = 0;
 625  625           * p->cku_flags = 0;
 626  626           * p->cku_srcaddr.len = 0;
 627  627           * p->cku_srcaddr.maxlen = 0;
 628  628           */
 629  629  
 630  630          p->cku_cred = cred;
 631  631          p->cku_device = dev;
 632  632          p->cku_addrfmly = family;
 633  633          p->cku_addr.buf = kmem_zalloc(addr->maxlen, KM_SLEEP);
 634  634          p->cku_addr.maxlen = addr->maxlen;
 635  635          p->cku_addr.len = addr->len;
 636  636          bcopy(addr->buf, p->cku_addr.buf, addr->len);
 637  637          p->cku_stats = rpcstat->rpc_cots_client;
 638  638          p->cku_useresvport = -1; /* value is has not been set */
 639  639  
 640  640          *ncl = h;
 641  641          return (0);
 642  642  }
 643  643  
 644  644  /*ARGSUSED*/
 645  645  static void
 646  646  clnt_cots_kabort(CLIENT *h)
 647  647  {
 648  648  }
 649  649  
 650  650  /*
 651  651   * Return error info on this handle.
 652  652   */
 653  653  static void
 654  654  clnt_cots_kerror(CLIENT *h, struct rpc_err *err)
 655  655  {
 656  656          /* LINTED pointer alignment */
 657  657          cku_private_t *p = htop(h);
 658  658  
 659  659          *err = p->cku_err;
 660  660  }
 661  661  
 662  662  /*ARGSUSED*/
 663  663  static bool_t
 664  664  clnt_cots_kfreeres(CLIENT *h, xdrproc_t xdr_res, caddr_t res_ptr)
 665  665  {
 666  666          xdr_free(xdr_res, res_ptr);
 667  667  
 668  668          return (TRUE);
 669  669  }
 670  670  
 671  671  static bool_t
 672  672  clnt_cots_kcontrol(CLIENT *h, int cmd, char *arg)
 673  673  {
 674  674          cku_private_t *p = htop(h);
 675  675  
 676  676          switch (cmd) {
 677  677          case CLSET_PROGRESS:
 678  678                  p->cku_progress = TRUE;
 679  679                  return (TRUE);
 680  680  
 681  681          case CLSET_XID:
 682  682                  if (arg == NULL)
 683  683                          return (FALSE);
 684  684  
 685  685                  p->cku_xid = *((uint32_t *)arg);
 686  686                  return (TRUE);
 687  687  
 688  688          case CLGET_XID:
 689  689                  if (arg == NULL)
 690  690                          return (FALSE);
 691  691  
 692  692                  *((uint32_t *)arg) = p->cku_xid;
 693  693                  return (TRUE);
 694  694  
 695  695          case CLSET_NODELAYONERR:
 696  696                  if (arg == NULL)
 697  697                          return (FALSE);
 698  698  
 699  699                  if (*((bool_t *)arg) == TRUE) {
 700  700                          p->cku_nodelayonerr = TRUE;
 701  701                          return (TRUE);
 702  702                  }
 703  703                  if (*((bool_t *)arg) == FALSE) {
 704  704                          p->cku_nodelayonerr = FALSE;
 705  705                          return (TRUE);
 706  706                  }
 707  707                  return (FALSE);
 708  708  
 709  709          case CLGET_NODELAYONERR:
 710  710                  if (arg == NULL)
 711  711                          return (FALSE);
 712  712  
 713  713                  *((bool_t *)arg) = p->cku_nodelayonerr;
 714  714                  return (TRUE);
 715  715  
 716  716          case CLSET_BINDRESVPORT:
 717  717                  if (arg == NULL)
 718  718                          return (FALSE);
 719  719  
 720  720                  if (*(int *)arg != 1 && *(int *)arg != 0)
 721  721                          return (FALSE);
 722  722  
 723  723                  p->cku_useresvport = *(int *)arg;
 724  724  
 725  725                  return (TRUE);
 726  726  
 727  727          case CLGET_BINDRESVPORT:
 728  728                  if (arg == NULL)
 729  729                          return (FALSE);
 730  730  
 731  731                  *(int *)arg = p->cku_useresvport;
 732  732  
 733  733                  return (TRUE);
 734  734  
 735  735          default:
 736  736                  return (FALSE);
 737  737          }
 738  738  }
 739  739  
 740  740  /*
 741  741   * Destroy rpc handle.  Frees the space used for output buffer,
 742  742   * private data, and handle structure.
 743  743   */
 744  744  static void
 745  745  clnt_cots_kdestroy(CLIENT *h)
 746  746  {
 747  747          /* LINTED pointer alignment */
 748  748          cku_private_t *p = htop(h);
 749  749          calllist_t *call = &p->cku_call;
 750  750  
 751  751          RPCLOG(8, "clnt_cots_kdestroy h: %p\n", (void *)h);
 752  752          RPCLOG(8, "clnt_cots_kdestroy h: xid=0x%x\n", p->cku_xid);
 753  753  
 754  754          if (p->cku_flags & CKU_ONQUEUE) {
 755  755                  RPCLOG(64, "clnt_cots_kdestroy h: removing call for xid 0x%x "
 756  756                      "from dispatch list\n", p->cku_xid);
 757  757                  call_table_remove(call);
 758  758          }
 759  759  
 760  760          if (call->call_reply)
 761  761                  freemsg(call->call_reply);
 762  762          cv_destroy(&call->call_cv);
 763  763          mutex_destroy(&call->call_lock);
 764  764  
 765  765          kmem_free(p->cku_srcaddr.buf, p->cku_srcaddr.maxlen);
 766  766          kmem_free(p->cku_addr.buf, p->cku_addr.maxlen);
 767  767          kmem_free(p, sizeof (*p));
 768  768  }
 769  769  
 770  770  static int clnt_cots_pulls;
 771  771  #define RM_HDR_SIZE     4       /* record mark header size */
 772  772  
 773  773  /*
 774  774   * Call remote procedure.
 775  775   */
 776  776  static enum clnt_stat
 777  777  clnt_cots_kcallit(CLIENT *h, rpcproc_t procnum, xdrproc_t xdr_args,
 778  778      caddr_t argsp, xdrproc_t xdr_results, caddr_t resultsp, struct timeval wait)
 779  779  {
 780  780          /* LINTED pointer alignment */
 781  781          cku_private_t *p = htop(h);
 782  782          calllist_t *call = &p->cku_call;
 783  783          XDR *xdrs;
 784  784          struct rpc_msg reply_msg;
 785  785          mblk_t *mp;
 786  786  #ifdef  RPCDEBUG
 787  787          clock_t time_sent;
 788  788  #endif
 789  789          struct netbuf *retryaddr;
 790  790          struct cm_xprt *cm_entry = NULL;
 791  791          queue_t *wq;
 792  792          int len, waitsecs, max_waitsecs;
 793  793          int mpsize;
 794  794          int refreshes = REFRESHES;
 795  795          int interrupted;
 796  796          int tidu_size;
 797  797          enum clnt_stat status;
 798  798          struct timeval cwait;
 799  799          bool_t delay_first = FALSE;
 800  800          clock_t ticks, now;
 801  801  
 802  802          RPCLOG(2, "clnt_cots_kcallit, procnum %u\n", procnum);
 803  803          COTSRCSTAT_INCR(p->cku_stats, rccalls);
 804  804  
 805  805          RPCLOG(2, "clnt_cots_kcallit: wait.tv_sec: %ld\n", wait.tv_sec);
 806  806          RPCLOG(2, "clnt_cots_kcallit: wait.tv_usec: %ld\n", wait.tv_usec);
 807  807          /*
 808  808           * Bug ID 1240234:
 809  809           * Look out for zero length timeouts. We don't want to
 810  810           * wait zero seconds for a connection to be established.
 811  811           */
 812  812          if (wait.tv_sec < clnt_cots_min_conntout) {
 813  813                  cwait.tv_sec = clnt_cots_min_conntout;
 814  814                  cwait.tv_usec = 0;
 815  815                  RPCLOG(8, "clnt_cots_kcallit: wait.tv_sec (%ld) too low,",
 816  816                      wait.tv_sec);
 817  817                  RPCLOG(8, " setting to: %d\n", clnt_cots_min_conntout);
 818  818          } else {
 819  819                  cwait = wait;
 820  820          }
 821  821  
 822  822  call_again:
 823  823          if (cm_entry) {
 824  824                  connmgr_release(cm_entry);
 825  825                  cm_entry = NULL;
 826  826          }
 827  827  
 828  828          mp = NULL;
 829  829  
 830  830          /*
 831  831           * If the call is not a retry, allocate a new xid and cache it
 832  832           * for future retries.
 833  833           * Bug ID 1246045:
 834  834           * Treat call as a retry for purposes of binding the source
 835  835           * port only if we actually attempted to send anything on
 836  836           * the previous call.
 837  837           */
 838  838          if (p->cku_xid == 0) {
 839  839                  p->cku_xid = alloc_xid();
 840  840                  call->call_zoneid = rpc_zoneid();
 841  841  
 842  842                  /*
 843  843                   * We need to ASSERT here that our xid != 0 because this
 844  844                   * determines whether or not our call record gets placed on
 845  845                   * the hash table or the linked list.  By design, we mandate
 846  846                   * that RPC calls over cots must have xid's != 0, so we can
 847  847                   * ensure proper management of the hash table.
 848  848                   */
 849  849                  ASSERT(p->cku_xid != 0);
 850  850  
 851  851                  retryaddr = NULL;
 852  852                  p->cku_flags &= ~CKU_SENT;
 853  853  
 854  854                  if (p->cku_flags & CKU_ONQUEUE) {
 855  855                          RPCLOG(8, "clnt_cots_kcallit: new call, dequeuing old"
 856  856                              " one (%p)\n", (void *)call);
 857  857                          call_table_remove(call);
 858  858                          p->cku_flags &= ~CKU_ONQUEUE;
 859  859                          RPCLOG(64, "clnt_cots_kcallit: removing call from "
 860  860                              "dispatch list because xid was zero (now 0x%x)\n",
 861  861                              p->cku_xid);
 862  862                  }
 863  863  
 864  864                  if (call->call_reply != NULL) {
 865  865                          freemsg(call->call_reply);
 866  866                          call->call_reply = NULL;
 867  867                  }
 868  868          } else if (p->cku_srcaddr.buf == NULL || p->cku_srcaddr.len == 0) {
 869  869                  retryaddr = NULL;
 870  870  
 871  871          } else if (p->cku_flags & CKU_SENT) {
 872  872                  retryaddr = &p->cku_srcaddr;
 873  873  
 874  874          } else {
 875  875                  /*
 876  876                   * Bug ID 1246045: Nothing was sent, so set retryaddr to
 877  877                   * NULL and let connmgr_get() bind to any source port it
 878  878                   * can get.
 879  879                   */
 880  880                  retryaddr = NULL;
 881  881          }
 882  882  
 883  883          RPCLOG(64, "clnt_cots_kcallit: xid = 0x%x", p->cku_xid);
 884  884          RPCLOG(64, " flags = 0x%x\n", p->cku_flags);
 885  885  
 886  886          p->cku_err.re_status = RPC_TIMEDOUT;
 887  887          p->cku_err.re_errno = p->cku_err.re_terrno = 0;
 888  888  
 889  889          cm_entry = connmgr_wrapget(retryaddr, &cwait, p);
 890  890  
 891  891          if (cm_entry == NULL) {
 892  892                  RPCLOG(1, "clnt_cots_kcallit: can't connect status %s\n",
 893  893                      clnt_sperrno(p->cku_err.re_status));
 894  894  
 895  895                  /*
 896  896                   * The reasons why we fail to create a connection are
 897  897                   * varied. In most cases we don't want the caller to
 898  898                   * immediately retry. This could have one or more
 899  899                   * bad effects. This includes flooding the net with
 900  900                   * connect requests to ports with no listener; a hard
 901  901                   * kernel loop due to all the "reserved" TCP ports being
 902  902                   * in use.
 903  903                   */
 904  904                  delay_first = TRUE;
 905  905  
 906  906                  /*
 907  907                   * Even if we end up returning EINTR, we still count a
 908  908                   * a "can't connect", because the connection manager
 909  909                   * might have been committed to waiting for or timing out on
 910  910                   * a connection.
 911  911                   */
 912  912                  COTSRCSTAT_INCR(p->cku_stats, rccantconn);
 913  913                  switch (p->cku_err.re_status) {
 914  914                  case RPC_INTR:
 915  915                          p->cku_err.re_errno = EINTR;
 916  916  
 917  917                          /*
 918  918                           * No need to delay because a UNIX signal(2)
 919  919                           * interrupted us. The caller likely won't
 920  920                           * retry the CLNT_CALL() and even if it does,
 921  921                           * we assume the caller knows what it is doing.
 922  922                           */
 923  923                          delay_first = FALSE;
 924  924                          break;
 925  925  
 926  926                  case RPC_TIMEDOUT:
 927  927                          p->cku_err.re_errno = ETIMEDOUT;
 928  928  
 929  929                          /*
 930  930                           * No need to delay because timed out already
 931  931                           * on the connection request and assume that the
 932  932                           * transport time out is longer than our minimum
 933  933                           * timeout, or least not too much smaller.
 934  934                           */
 935  935                          delay_first = FALSE;
 936  936                          break;
 937  937  
 938  938                  case RPC_SYSTEMERROR:
 939  939                  case RPC_TLIERROR:
 940  940                          /*
 941  941                           * We want to delay here because a transient
 942  942                           * system error has a better chance of going away
 943  943                           * if we delay a bit. If it's not transient, then
 944  944                           * we don't want end up in a hard kernel loop
 945  945                           * due to retries.
 946  946                           */
 947  947                          ASSERT(p->cku_err.re_errno != 0);
 948  948                          break;
 949  949  
 950  950  
 951  951                  case RPC_CANTCONNECT:
 952  952                          /*
 953  953                           * RPC_CANTCONNECT is set on T_ERROR_ACK which
 954  954                           * implies some error down in the TCP layer or
 955  955                           * below. If cku_nodelayonerror is set then we
 956  956                           * assume the caller knows not to try too hard.
 957  957                           */
 958  958                          RPCLOG0(8, "clnt_cots_kcallit: connection failed,");
 959  959                          RPCLOG0(8, " re_status=RPC_CANTCONNECT,");
 960  960                          RPCLOG(8, " re_errno=%d,", p->cku_err.re_errno);
 961  961                          RPCLOG(8, " cku_nodelayonerr=%d", p->cku_nodelayonerr);
 962  962                          if (p->cku_nodelayonerr == TRUE)
 963  963                                  delay_first = FALSE;
 964  964  
 965  965                          p->cku_err.re_errno = EIO;
 966  966  
 967  967                          break;
 968  968  
 969  969                  case RPC_XPRTFAILED:
 970  970                          /*
 971  971                           * We want to delay here because we likely
 972  972                           * got a refused connection.
 973  973                           */
 974  974                          if (p->cku_err.re_errno == 0)
 975  975                                  p->cku_err.re_errno = EIO;
 976  976  
 977  977                          RPCLOG(1, "clnt_cots_kcallit: transport failed: %d\n",
 978  978                              p->cku_err.re_errno);
 979  979  
 980  980                          break;
 981  981  
 982  982                  default:
 983  983                          /*
 984  984                           * We delay here because it is better to err
 985  985                           * on the side of caution. If we got here then
 986  986                           * status could have been RPC_SUCCESS, but we
 987  987                           * know that we did not get a connection, so
 988  988                           * force the rpc status to RPC_CANTCONNECT.
 989  989                           */
 990  990                          p->cku_err.re_status = RPC_CANTCONNECT;
 991  991                          p->cku_err.re_errno = EIO;
 992  992                          break;
 993  993                  }
 994  994                  if (delay_first == TRUE)
 995  995                          ticks = clnt_cots_min_tout * drv_usectohz(1000000);
 996  996                  goto cots_done;
 997  997          }
 998  998  
 999  999          /*
1000 1000           * If we've never sent any request on this connection (send count
1001 1001           * is zero, or the connection has been reset), cache the
1002 1002           * the connection's create time and send a request (possibly a retry)
1003 1003           */
1004 1004          if ((p->cku_flags & CKU_SENT) == 0 ||
1005 1005              p->cku_ctime != cm_entry->x_ctime) {
1006 1006                  p->cku_ctime = cm_entry->x_ctime;
1007 1007  
1008 1008          } else if ((p->cku_flags & CKU_SENT) && (p->cku_flags & CKU_ONQUEUE) &&
1009 1009              (call->call_reply != NULL ||
1010 1010              p->cku_recv_attempts < clnt_cots_maxrecv)) {
1011 1011  
1012 1012                  /*
1013 1013                   * If we've sent a request and our call is on the dispatch
1014 1014                   * queue and we haven't made too many receive attempts, then
1015 1015                   * don't re-send, just receive.
1016 1016                   */
1017 1017                  p->cku_recv_attempts++;
1018 1018                  goto read_again;
1019 1019          }
1020 1020  
1021 1021          /*
1022 1022           * Now we create the RPC request in a STREAMS message.  We have to do
1023 1023           * this after the call to connmgr_get so that we have the correct
1024 1024           * TIDU size for the transport.
1025 1025           */
1026 1026          tidu_size = cm_entry->x_tidu_size;
1027 1027          len = MSG_OFFSET + MAX(tidu_size, RM_HDR_SIZE + WIRE_HDR_SIZE);
1028 1028  
1029 1029          while ((mp = allocb(len, BPRI_MED)) == NULL) {
1030 1030                  if (strwaitbuf(len, BPRI_MED)) {
1031 1031                          p->cku_err.re_status = RPC_SYSTEMERROR;
1032 1032                          p->cku_err.re_errno = ENOSR;
1033 1033                          COTSRCSTAT_INCR(p->cku_stats, rcnomem);
1034 1034                          goto cots_done;
1035 1035                  }
1036 1036          }
1037 1037          xdrs = &p->cku_outxdr;
1038 1038          xdrmblk_init(xdrs, mp, XDR_ENCODE, tidu_size);
1039 1039          mpsize = MBLKSIZE(mp);
1040 1040          ASSERT(mpsize >= len);
1041 1041          ASSERT(mp->b_rptr == mp->b_datap->db_base);
1042 1042  
1043 1043          /*
1044 1044           * If the size of mblk is not appreciably larger than what we
1045 1045           * asked, then resize the mblk to exactly len bytes. The reason for
1046 1046           * this: suppose len is 1600 bytes, the tidu is 1460 bytes
1047 1047           * (from TCP over ethernet), and the arguments to the RPC require
1048 1048           * 2800 bytes. Ideally we want the protocol to render two
1049 1049           * ~1400 byte segments over the wire. However if allocb() gives us a 2k
1050 1050           * mblk, and we allocate a second mblk for the remainder, the protocol
1051 1051           * module may generate 3 segments over the wire:
1052 1052           * 1460 bytes for the first, 448 (2048 - 1600) for the second, and
1053 1053           * 892 for the third. If we "waste" 448 bytes in the first mblk,
1054 1054           * the XDR encoding will generate two ~1400 byte mblks, and the
1055 1055           * protocol module is more likely to produce properly sized segments.
1056 1056           */
1057 1057          if ((mpsize >> 1) <= len)
1058 1058                  mp->b_rptr += (mpsize - len);
1059 1059  
1060 1060          /*
1061 1061           * Adjust b_rptr to reserve space for the non-data protocol headers
1062 1062           * any downstream modules might like to add, and for the
1063 1063           * record marking header.
1064 1064           */
1065 1065          mp->b_rptr += (MSG_OFFSET + RM_HDR_SIZE);
1066 1066  
1067 1067          if (h->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS) {
1068 1068                  /* Copy in the preserialized RPC header information. */
1069 1069                  bcopy(p->cku_rpchdr, mp->b_rptr, WIRE_HDR_SIZE);
1070 1070  
1071 1071                  /* Use XDR_SETPOS() to set the b_wptr to past the RPC header. */
1072 1072                  XDR_SETPOS(xdrs, (uint_t)(mp->b_rptr - mp->b_datap->db_base +
1073 1073                      WIRE_HDR_SIZE));
1074 1074  
1075 1075                  ASSERT((mp->b_wptr - mp->b_rptr) == WIRE_HDR_SIZE);
1076 1076  
1077 1077                  /* Serialize the procedure number and the arguments. */
1078 1078                  if ((!XDR_PUTINT32(xdrs, (int32_t *)&procnum)) ||
1079 1079                      (!AUTH_MARSHALL(h->cl_auth, xdrs, p->cku_cred)) ||
1080 1080                      (!(*xdr_args)(xdrs, argsp))) {
1081 1081                          XDR_DESTROY(xdrs);
1082 1082                          p->cku_err.re_status = RPC_CANTENCODEARGS;
1083 1083                          p->cku_err.re_errno = EIO;
1084 1084                          goto cots_done;
1085 1085                  }
1086 1086  
1087 1087                  (*(uint32_t *)(mp->b_rptr)) = p->cku_xid;
1088 1088          } else {
1089 1089                  uint32_t *uproc = (uint32_t *)&p->cku_rpchdr[WIRE_HDR_SIZE];
1090 1090                  IXDR_PUT_U_INT32(uproc, procnum);
1091 1091  
1092 1092                  (*(uint32_t *)(&p->cku_rpchdr[0])) = p->cku_xid;
1093 1093  
1094 1094                  /* Use XDR_SETPOS() to set the b_wptr. */
1095 1095                  XDR_SETPOS(xdrs, (uint_t)(mp->b_rptr - mp->b_datap->db_base));
1096 1096  
1097 1097                  /* Serialize the procedure number and the arguments. */
1098 1098                  if (!AUTH_WRAP(h->cl_auth, p->cku_rpchdr, WIRE_HDR_SIZE+4,
1099 1099                      xdrs, xdr_args, argsp)) {
1100 1100                          XDR_DESTROY(xdrs);
1101 1101                          p->cku_err.re_status = RPC_CANTENCODEARGS;
1102 1102                          p->cku_err.re_errno = EIO;
1103 1103                          goto cots_done;
1104 1104                  }
1105 1105          }
1106 1106  
1107 1107          XDR_DESTROY(xdrs);
1108 1108  
1109 1109          RPCLOG(2, "clnt_cots_kcallit: connected, sending call, tidu_size %d\n",
1110 1110              tidu_size);
1111 1111  
1112 1112          wq = cm_entry->x_wq;
1113 1113          waitsecs = 0;
1114 1114  
1115 1115  dispatch_again:
1116 1116          status = clnt_dispatch_send(wq, mp, call, p->cku_xid,
1117 1117              (p->cku_flags & CKU_ONQUEUE));
1118 1118  
1119 1119          if ((status == RPC_CANTSEND) && (call->call_reason == ENOBUFS)) {
1120 1120                  /*
1121 1121                   * QFULL condition, allow some time for queue to drain
1122 1122                   * and try again. Give up after waiting for all timeout
1123 1123                   * specified for the call, or zone is going away.
1124 1124                   */
1125 1125                  max_waitsecs = wait.tv_sec ? wait.tv_sec : clnt_cots_min_tout;
1126 1126                  if ((waitsecs++ < max_waitsecs) &&
1127 1127                      !(zone_status_get(curproc->p_zone) >=
1128 1128                      ZONE_IS_SHUTTING_DOWN)) {
1129 1129  
1130 1130                          /* wait 1 sec for queue to drain */
1131 1131                          if (clnt_delay(drv_usectohz(1000000),
1132 1132                              h->cl_nosignal) == EINTR) {
1133 1133                                  p->cku_err.re_errno = EINTR;
1134 1134                                  p->cku_err.re_status = RPC_INTR;
1135 1135  
1136 1136                                  goto cots_done;
1137 1137                          }
1138 1138  
1139 1139                          /* and try again */
1140 1140                          goto dispatch_again;
1141 1141                  }
1142 1142                  p->cku_err.re_status = status;
1143 1143                  p->cku_err.re_errno = call->call_reason;
1144 1144                  DTRACE_PROBE(krpc__e__clntcots__kcallit__cantsend);
1145 1145  
1146 1146                  goto cots_done;
1147 1147          }
1148 1148  
1149 1149          if (waitsecs) {
1150 1150                  /* adjust timeout to account for time wait to send */
1151 1151                  wait.tv_sec -= waitsecs;
1152 1152                  if (wait.tv_sec < 0) {
1153 1153                          /* pick up reply on next retry */
1154 1154                          wait.tv_sec = 0;
1155 1155                  }
1156 1156                  DTRACE_PROBE2(clnt_cots__sendwait, CLIENT *, h,
1157 1157                      int, waitsecs);
1158 1158          }
1159 1159  
1160 1160          RPCLOG(64, "clnt_cots_kcallit: sent call for xid 0x%x\n",
1161 1161              (uint_t)p->cku_xid);
1162 1162          p->cku_flags = (CKU_ONQUEUE|CKU_SENT);
1163 1163          p->cku_recv_attempts = 1;
1164 1164  
1165 1165  #ifdef  RPCDEBUG
1166 1166          time_sent = ddi_get_lbolt();
1167 1167  #endif
1168 1168  
1169 1169          /*
1170 1170           * Wait for a reply or a timeout.  If there is no error or timeout,
1171 1171           * (both indicated by call_status), call->call_reply will contain
1172 1172           * the RPC reply message.
1173 1173           */
1174 1174  read_again:
1175 1175          mutex_enter(&call->call_lock);
1176 1176          interrupted = 0;
1177 1177          if (call->call_status == RPC_TIMEDOUT) {
1178 1178                  /*
1179 1179                   * Indicate that the lwp is not to be stopped while waiting
1180 1180                   * for this network traffic.  This is to avoid deadlock while
1181 1181                   * debugging a process via /proc and also to avoid recursive
1182 1182                   * mutex_enter()s due to NFS page faults while stopping
1183 1183                   * (NFS holds locks when it calls here).
1184 1184                   */
1185 1185                  clock_t cv_wait_ret;
1186 1186                  clock_t timout;
1187 1187                  clock_t oldlbolt;
1188 1188  
1189 1189                  klwp_t *lwp = ttolwp(curthread);
1190 1190  
1191 1191                  if (lwp != NULL)
1192 1192                          lwp->lwp_nostop++;
1193 1193  
1194 1194                  oldlbolt = ddi_get_lbolt();
1195 1195                  timout = wait.tv_sec * drv_usectohz(1000000) +
1196 1196                      drv_usectohz(wait.tv_usec) + oldlbolt;
1197 1197                  /*
1198 1198                   * Iterate until the call_status is changed to something
1199 1199                   * other that RPC_TIMEDOUT, or if cv_timedwait_sig() returns
1200 1200                   * something <=0 zero. The latter means that we timed
1201 1201                   * out.
1202 1202                   */
1203 1203                  if (h->cl_nosignal)
1204 1204                          while ((cv_wait_ret = cv_timedwait(&call->call_cv,
1205 1205                              &call->call_lock, timout)) > 0 &&
1206 1206                              call->call_status == RPC_TIMEDOUT)
1207 1207                                  ;
1208 1208                  else
1209 1209                          while ((cv_wait_ret = cv_timedwait_sig(
1210 1210                              &call->call_cv,
1211 1211                              &call->call_lock, timout)) > 0 &&
1212 1212                              call->call_status == RPC_TIMEDOUT)
1213 1213                                  ;
1214 1214  
1215 1215                  switch (cv_wait_ret) {
1216 1216                  case 0:
1217 1217                          /*
1218 1218                           * If we got out of the above loop with
1219 1219                           * cv_timedwait_sig() returning 0, then we were
1220 1220                           * interrupted regardless what call_status is.
1221 1221                           */
1222 1222                          interrupted = 1;
1223 1223                          break;
1224 1224                  case -1:
1225 1225                          /* cv_timedwait_sig() timed out */
1226 1226                          break;
1227 1227                  default:
1228 1228  
1229 1229                          /*
1230 1230                           * We were cv_signaled(). If we didn't
1231 1231                           * get a successful call_status and returned
1232 1232                           * before time expired, delay up to clnt_cots_min_tout
1233 1233                           * seconds so that the caller doesn't immediately
1234 1234                           * try to call us again and thus force the
1235 1235                           * same condition that got us here (such
1236 1236                           * as a RPC_XPRTFAILED due to the server not
1237 1237                           * listening on the end-point.
1238 1238                           */
1239 1239                          if (call->call_status != RPC_SUCCESS) {
1240 1240                                  clock_t curlbolt;
1241 1241                                  clock_t diff;
1242 1242  
1243 1243                                  curlbolt = ddi_get_lbolt();
1244 1244                                  ticks = clnt_cots_min_tout *
1245 1245                                      drv_usectohz(1000000);
1246 1246                                  diff = curlbolt - oldlbolt;
1247 1247                                  if (diff < ticks) {
1248 1248                                          delay_first = TRUE;
1249 1249                                          if (diff > 0)
1250 1250                                                  ticks -= diff;
1251 1251                                  }
1252 1252                          }
1253 1253                          break;
1254 1254                  }
1255 1255  
1256 1256                  if (lwp != NULL)
1257 1257                          lwp->lwp_nostop--;
1258 1258          }
1259 1259          /*
1260 1260           * Get the reply message, if any.  This will be freed at the end
1261 1261           * whether or not an error occurred.
1262 1262           */
1263 1263          mp = call->call_reply;
1264 1264          call->call_reply = NULL;
1265 1265  
1266 1266          /*
1267 1267           * call_err is the error info when the call is on dispatch queue.
1268 1268           * cku_err is the error info returned to the caller.
1269 1269           * Sync cku_err with call_err for local message processing.
1270 1270           */
1271 1271  
1272 1272          status = call->call_status;
1273 1273          p->cku_err = call->call_err;
1274 1274          mutex_exit(&call->call_lock);
1275 1275  
1276 1276          if (status != RPC_SUCCESS) {
1277 1277                  switch (status) {
1278 1278                  case RPC_TIMEDOUT:
1279 1279                          now = ddi_get_lbolt();
1280 1280                          if (interrupted) {
1281 1281                                  COTSRCSTAT_INCR(p->cku_stats, rcintrs);
1282 1282                                  p->cku_err.re_status = RPC_INTR;
1283 1283                                  p->cku_err.re_errno = EINTR;
1284 1284                                  RPCLOG(1, "clnt_cots_kcallit: xid 0x%x",
1285 1285                                      p->cku_xid);
1286 1286                                  RPCLOG(1, "signal interrupted at %ld", now);
1287 1287                                  RPCLOG(1, ", was sent at %ld\n", time_sent);
1288 1288                          } else {
1289 1289                                  COTSRCSTAT_INCR(p->cku_stats, rctimeouts);
1290 1290                                  p->cku_err.re_errno = ETIMEDOUT;
1291 1291                                  RPCLOG(1, "clnt_cots_kcallit: timed out at %ld",
1292 1292                                      now);
1293 1293                                  RPCLOG(1, ", was sent at %ld\n", time_sent);
1294 1294                          }
1295 1295                          break;
1296 1296  
1297 1297                  case RPC_XPRTFAILED:
1298 1298                          if (p->cku_err.re_errno == 0)
1299 1299                                  p->cku_err.re_errno = EIO;
1300 1300  
1301 1301                          RPCLOG(1, "clnt_cots_kcallit: transport failed: %d\n",
1302 1302                              p->cku_err.re_errno);
1303 1303                          break;
1304 1304  
1305 1305                  case RPC_SYSTEMERROR:
1306 1306                          ASSERT(p->cku_err.re_errno);
1307 1307                          RPCLOG(1, "clnt_cots_kcallit: system error: %d\n",
1308 1308                              p->cku_err.re_errno);
1309 1309                          break;
1310 1310  
1311 1311                  default:
1312 1312                          p->cku_err.re_status = RPC_SYSTEMERROR;
1313 1313                          p->cku_err.re_errno = EIO;
1314 1314                          RPCLOG(1, "clnt_cots_kcallit: error: %s\n",
1315 1315                              clnt_sperrno(status));
1316 1316                          break;
1317 1317                  }
1318 1318                  if (p->cku_err.re_status != RPC_TIMEDOUT) {
1319 1319  
1320 1320                          if (p->cku_flags & CKU_ONQUEUE) {
1321 1321                                  call_table_remove(call);
1322 1322                                  p->cku_flags &= ~CKU_ONQUEUE;
1323 1323                          }
1324 1324  
1325 1325                          RPCLOG(64, "clnt_cots_kcallit: non TIMEOUT so xid 0x%x "
1326 1326                              "taken off dispatch list\n", p->cku_xid);
1327 1327                          if (call->call_reply) {
1328 1328                                  freemsg(call->call_reply);
1329 1329                                  call->call_reply = NULL;
1330 1330                          }
1331 1331                  } else if (wait.tv_sec != 0) {
1332 1332                          /*
1333 1333                           * We've sent the request over TCP and so we have
1334 1334                           * every reason to believe it will get
1335 1335                           * delivered. In which case returning a timeout is not
1336 1336                           * appropriate.
1337 1337                           */
1338 1338                          if (p->cku_progress == TRUE &&
1339 1339                              p->cku_recv_attempts < clnt_cots_maxrecv) {
1340 1340                                  p->cku_err.re_status = RPC_INPROGRESS;
1341 1341                          }
1342 1342                  }
1343 1343                  goto cots_done;
1344 1344          }
1345 1345  
1346 1346          xdrs = &p->cku_inxdr;
1347 1347          xdrmblk_init(xdrs, mp, XDR_DECODE, 0);
1348 1348  
1349 1349          reply_msg.rm_direction = REPLY;
1350 1350          reply_msg.rm_reply.rp_stat = MSG_ACCEPTED;
1351 1351          reply_msg.acpted_rply.ar_stat = SUCCESS;
1352 1352  
1353 1353          reply_msg.acpted_rply.ar_verf = _null_auth;
1354 1354          /*
1355 1355           *  xdr_results will be done in AUTH_UNWRAP.
1356 1356           */
1357 1357          reply_msg.acpted_rply.ar_results.where = NULL;
1358 1358          reply_msg.acpted_rply.ar_results.proc = xdr_void;
1359 1359  
1360 1360          if (xdr_replymsg(xdrs, &reply_msg)) {
1361 1361                  enum clnt_stat re_status;
1362 1362  
1363 1363                  _seterr_reply(&reply_msg, &p->cku_err);
1364 1364  
1365 1365                  re_status = p->cku_err.re_status;
1366 1366                  if (re_status == RPC_SUCCESS) {
1367 1367                          /*
1368 1368                           * Reply is good, check auth.
1369 1369                           */
1370 1370                          if (!AUTH_VALIDATE(h->cl_auth,
1371 1371                              &reply_msg.acpted_rply.ar_verf)) {
1372 1372                                  COTSRCSTAT_INCR(p->cku_stats, rcbadverfs);
1373 1373                                  RPCLOG0(1, "clnt_cots_kcallit: validation "
1374 1374                                      "failure\n");
1375 1375                                  freemsg(mp);
1376 1376                                  (void) xdr_rpc_free_verifier(xdrs, &reply_msg);
1377 1377                                  XDR_DESTROY(xdrs);
1378 1378                                  mutex_enter(&call->call_lock);
1379 1379                                  if (call->call_reply == NULL)
1380 1380                                          call->call_status = RPC_TIMEDOUT;
1381 1381                                  mutex_exit(&call->call_lock);
1382 1382                                  goto read_again;
1383 1383                          } else if (!AUTH_UNWRAP(h->cl_auth, xdrs,
1384 1384                              xdr_results, resultsp)) {
1385 1385                                  RPCLOG0(1, "clnt_cots_kcallit: validation "
1386 1386                                      "failure (unwrap)\n");
1387 1387                                  p->cku_err.re_status = RPC_CANTDECODERES;
1388 1388                                  p->cku_err.re_errno = EIO;
1389 1389                          }
1390 1390                  } else {
1391 1391                          /* set errno in case we can't recover */
1392 1392                          if (re_status != RPC_VERSMISMATCH &&
1393 1393                              re_status != RPC_AUTHERROR &&
1394 1394                              re_status != RPC_PROGVERSMISMATCH)
1395 1395                                  p->cku_err.re_errno = EIO;
1396 1396  
1397 1397                          if (re_status == RPC_AUTHERROR) {
1398 1398                                  /*
1399 1399                                   * Maybe our credential need to be refreshed
1400 1400                                   */
1401 1401                                  if (cm_entry) {
1402 1402                                          /*
1403 1403                                           * There is the potential that the
1404 1404                                           * cm_entry has/will be marked dead,
1405 1405                                           * so drop the connection altogether,
1406 1406                                           * force REFRESH to establish new
1407 1407                                           * connection.
1408 1408                                           */
1409 1409                                          connmgr_cancelconn(cm_entry);
1410 1410                                          cm_entry = NULL;
1411 1411                                  }
1412 1412  
1413 1413                                  (void) xdr_rpc_free_verifier(xdrs,
1414 1414                                      &reply_msg);
1415 1415                                  XDR_DESTROY(xdrs);
1416 1416  
1417 1417                                  if (p->cku_flags & CKU_ONQUEUE) {
1418 1418                                          call_table_remove(call);
1419 1419                                          p->cku_flags &= ~CKU_ONQUEUE;
1420 1420                                  }
1421 1421                                  RPCLOG(64,
1422 1422                                      "clnt_cots_kcallit: AUTH_ERROR, xid"
1423 1423                                      " 0x%x removed off dispatch list\n",
1424 1424                                      p->cku_xid);
1425 1425                                  if (call->call_reply) {
1426 1426                                          freemsg(call->call_reply);
1427 1427                                          call->call_reply = NULL;
1428 1428                                  }
1429 1429  
1430 1430                                  if ((refreshes > 0) &&
1431 1431                                      AUTH_REFRESH(h->cl_auth, &reply_msg,
1432 1432                                      p->cku_cred)) {
1433 1433                                          refreshes--;
1434 1434                                          freemsg(mp);
1435 1435                                          mp = NULL;
1436 1436  
1437 1437                                          COTSRCSTAT_INCR(p->cku_stats,
1438 1438                                              rcbadcalls);
1439 1439                                          COTSRCSTAT_INCR(p->cku_stats,
1440 1440                                              rcnewcreds);
1441 1441                                          goto call_again;
1442 1442                                  }
1443 1443  
1444 1444                                  /*
1445 1445                                   * We have used the client handle to
1446 1446                                   * do an AUTH_REFRESH and the RPC status may
1447 1447                                   * be set to RPC_SUCCESS; Let's make sure to
1448 1448                                   * set it to RPC_AUTHERROR.
1449 1449                                   */
1450 1450                                  p->cku_err.re_status = RPC_AUTHERROR;
1451 1451  
1452 1452                                  /*
1453 1453                                   * Map recoverable and unrecoverable
1454 1454                                   * authentication errors to appropriate errno
1455 1455                                   */
1456 1456                                  switch (p->cku_err.re_why) {
1457 1457                                  case AUTH_TOOWEAK:
1458 1458                                          /*
1459 1459                                           * This could be a failure where the
1460 1460                                           * server requires use of a reserved
1461 1461                                           * port,  check and optionally set the
1462 1462                                           * client handle useresvport trying
1463 1463                                           * one more time. Next go round we
1464 1464                                           * fall out with the tooweak error.
1465 1465                                           */
1466 1466                                          if (p->cku_useresvport != 1) {
1467 1467                                                  p->cku_useresvport = 1;
1468 1468                                                  p->cku_xid = 0;
1469 1469                                                  freemsg(mp);
1470 1470                                                  mp = NULL;
1471 1471                                                  goto call_again;
1472 1472                                          }
1473 1473                                          /* FALLTHRU */
1474 1474                                  case AUTH_BADCRED:
1475 1475                                  case AUTH_BADVERF:
1476 1476                                  case AUTH_INVALIDRESP:
1477 1477                                  case AUTH_FAILED:
1478 1478                                  case RPCSEC_GSS_NOCRED:
1479 1479                                  case RPCSEC_GSS_FAILED:
1480 1480                                                  p->cku_err.re_errno = EACCES;
1481 1481                                                  break;
1482 1482                                  case AUTH_REJECTEDCRED:
1483 1483                                  case AUTH_REJECTEDVERF:
1484 1484                                  default:        p->cku_err.re_errno = EIO;
1485 1485                                                  break;
1486 1486                                  }
1487 1487                                  RPCLOG(1, "clnt_cots_kcallit : authentication"
1488 1488                                      " failed with RPC_AUTHERROR of type %d\n",
1489 1489                                      (int)p->cku_err.re_why);
1490 1490                                  goto cots_done;
1491 1491                          }
1492 1492                  }
1493 1493          } else {
1494 1494                  /* reply didn't decode properly. */
1495 1495                  p->cku_err.re_status = RPC_CANTDECODERES;
1496 1496                  p->cku_err.re_errno = EIO;
1497 1497                  RPCLOG0(1, "clnt_cots_kcallit: decode failure\n");
1498 1498          }
1499 1499  
1500 1500          (void) xdr_rpc_free_verifier(xdrs, &reply_msg);
1501 1501          XDR_DESTROY(xdrs);
1502 1502  
1503 1503          if (p->cku_flags & CKU_ONQUEUE) {
1504 1504                  call_table_remove(call);
1505 1505                  p->cku_flags &= ~CKU_ONQUEUE;
1506 1506          }
1507 1507  
1508 1508          RPCLOG(64, "clnt_cots_kcallit: xid 0x%x taken off dispatch list",
1509 1509              p->cku_xid);
1510 1510          RPCLOG(64, " status is %s\n", clnt_sperrno(p->cku_err.re_status));
1511 1511  cots_done:
1512 1512          if (cm_entry)
1513 1513                  connmgr_release(cm_entry);
1514 1514  
1515 1515          if (mp != NULL)
1516 1516                  freemsg(mp);
1517 1517          if ((p->cku_flags & CKU_ONQUEUE) == 0 && call->call_reply) {
1518 1518                  freemsg(call->call_reply);
1519 1519                  call->call_reply = NULL;
1520 1520          }
1521 1521          if (p->cku_err.re_status != RPC_SUCCESS) {
1522 1522                  RPCLOG0(1, "clnt_cots_kcallit: tail-end failure\n");
1523 1523                  COTSRCSTAT_INCR(p->cku_stats, rcbadcalls);
1524 1524          }
1525 1525  
1526 1526          /*
1527 1527           * No point in delaying if the zone is going away.
1528 1528           */
1529 1529          if (delay_first == TRUE &&
1530 1530              !(zone_status_get(curproc->p_zone) >= ZONE_IS_SHUTTING_DOWN)) {
1531 1531                  if (clnt_delay(ticks, h->cl_nosignal) == EINTR) {
1532 1532                          p->cku_err.re_errno = EINTR;
1533 1533                          p->cku_err.re_status = RPC_INTR;
1534 1534                  }
1535 1535          }
1536 1536          return (p->cku_err.re_status);
1537 1537  }
1538 1538  
1539 1539  /*
1540 1540   * Kinit routine for cots.  This sets up the correct operations in
1541 1541   * the client handle, as the handle may have previously been a clts
1542 1542   * handle, and clears the xid field so there is no way a new call
1543 1543   * could be mistaken for a retry.  It also sets in the handle the
1544 1544   * information that is passed at create/kinit time but needed at
1545 1545   * call time, as cots creates the transport at call time - device,
1546 1546   * address of the server, protocol family.
1547 1547   */
1548 1548  void
1549 1549  clnt_cots_kinit(CLIENT *h, dev_t dev, int family, struct netbuf *addr,
1550 1550      int max_msgsize, cred_t *cred)
1551 1551  {
1552 1552          /* LINTED pointer alignment */
1553 1553          cku_private_t *p = htop(h);
1554 1554          calllist_t *call = &p->cku_call;
1555 1555  
1556 1556          h->cl_ops = &tcp_ops;
1557 1557          if (p->cku_flags & CKU_ONQUEUE) {
1558 1558                  call_table_remove(call);
1559 1559                  p->cku_flags &= ~CKU_ONQUEUE;
1560 1560                  RPCLOG(64, "clnt_cots_kinit: removing call for xid 0x%x from"
1561 1561                      " dispatch list\n", p->cku_xid);
1562 1562          }
1563 1563  
1564 1564          if (call->call_reply != NULL) {
1565 1565                  freemsg(call->call_reply);
1566 1566                  call->call_reply = NULL;
1567 1567          }
1568 1568  
1569 1569          call->call_bucket = NULL;
1570 1570          call->call_hash = 0;
1571 1571  
1572 1572          /*
1573 1573           * We don't clear cku_flags here, because clnt_cots_kcallit()
1574 1574           * takes care of handling the cku_flags reset.
1575 1575           */
1576 1576          p->cku_xid = 0;
1577 1577          p->cku_device = dev;
1578 1578          p->cku_addrfmly = family;
1579 1579          p->cku_cred = cred;
1580 1580  
1581 1581          if (p->cku_addr.maxlen < addr->len) {
1582 1582                  if (p->cku_addr.maxlen != 0 && p->cku_addr.buf != NULL)
1583 1583                          kmem_free(p->cku_addr.buf, p->cku_addr.maxlen);
1584 1584                  p->cku_addr.buf = kmem_zalloc(addr->maxlen, KM_SLEEP);
1585 1585                  p->cku_addr.maxlen = addr->maxlen;
1586 1586          }
1587 1587  
1588 1588          p->cku_addr.len = addr->len;
1589 1589          bcopy(addr->buf, p->cku_addr.buf, addr->len);
1590 1590  
1591 1591          /*
1592 1592           * If the current sanity check size in rpcmod is smaller
1593 1593           * than the size needed, then increase the sanity check.
1594 1594           */
1595 1595          if (max_msgsize != 0 && clnt_max_msg_sizep != NULL &&
1596 1596              max_msgsize > *clnt_max_msg_sizep) {
1597 1597                  mutex_enter(&clnt_max_msg_lock);
1598 1598                  if (max_msgsize > *clnt_max_msg_sizep)
1599 1599                          *clnt_max_msg_sizep = max_msgsize;
1600 1600                  mutex_exit(&clnt_max_msg_lock);
1601 1601          }
1602 1602  }
1603 1603  
1604 1604  /*
1605 1605   * ksettimers is a no-op for cots, with the exception of setting the xid.
1606 1606   */
1607 1607  /* ARGSUSED */
1608 1608  static int
1609 1609  clnt_cots_ksettimers(CLIENT *h, struct rpc_timers *t, struct rpc_timers *all,
1610 1610      int minimum, void (*feedback)(int, int, caddr_t), caddr_t arg, uint32_t xid)
1611 1611  {
1612 1612          /* LINTED pointer alignment */
1613 1613          cku_private_t *p = htop(h);
1614 1614  
1615 1615          if (xid)
1616 1616                  p->cku_xid = xid;
1617 1617          COTSRCSTAT_INCR(p->cku_stats, rctimers);
1618 1618          return (0);
1619 1619  }
1620 1620  
1621 1621  extern void rpc_poptimod(struct vnode *);
1622 1622  extern int kstr_push(struct vnode *, char *);
1623 1623  
1624 1624  int
1625 1625  conn_kstat_update(kstat_t *ksp, int rw)
1626 1626  {
1627 1627          struct cm_xprt *cm_entry;
1628 1628          struct cm_kstat_xprt *cm_ksp_data;
1629 1629          uchar_t *b;
1630 1630          char *fbuf;
1631 1631  
1632 1632          if (rw == KSTAT_WRITE)
1633 1633                  return (EACCES);
1634 1634          if (ksp == NULL || ksp->ks_private == NULL)
1635 1635                  return (EIO);
1636 1636          cm_entry  = (struct cm_xprt *)ksp->ks_private;
1637 1637          cm_ksp_data = (struct cm_kstat_xprt *)ksp->ks_data;
1638 1638  
1639 1639          cm_ksp_data->x_wq.value.ui32 = (uint32_t)(uintptr_t)cm_entry->x_wq;
1640 1640          cm_ksp_data->x_family.value.ui32 = cm_entry->x_family;
1641 1641          cm_ksp_data->x_rdev.value.ui32 = (uint32_t)cm_entry->x_rdev;
1642 1642          cm_ksp_data->x_time.value.ui32 = cm_entry->x_time;
1643 1643          cm_ksp_data->x_ref.value.ui32 = cm_entry->x_ref;
1644 1644          cm_ksp_data->x_state.value.ui32 = cm_entry->x_state_flags;
1645 1645  
1646 1646          if (cm_entry->x_server.buf) {
1647 1647                  fbuf = cm_ksp_data->x_server.value.str.addr.ptr;
1648 1648                  if (cm_entry->x_family == AF_INET &&
1649 1649                      cm_entry->x_server.len ==
1650 1650                      sizeof (struct sockaddr_in)) {
1651 1651                          struct sockaddr_in  *sa;
1652 1652                          sa = (struct sockaddr_in *)
1653 1653                                  cm_entry->x_server.buf;
1654 1654                          b = (uchar_t *)&sa->sin_addr;
1655 1655                          (void) sprintf(fbuf,
1656 1656                              "%d.%d.%d.%d", b[0] & 0xFF, b[1] & 0xFF,
1657 1657                              b[2] & 0xFF, b[3] & 0xFF);
1658 1658                          cm_ksp_data->x_port.value.ui32 = ntohs(sa->sin_port);
1659 1659                  } else if (cm_entry->x_family == AF_INET6 &&
1660 1660                                  cm_entry->x_server.len >=
1661 1661                                  sizeof (struct sockaddr_in6)) {
1662 1662                          /* extract server IP address & port */
1663 1663                          struct sockaddr_in6 *sin6;
1664 1664                          sin6 = (struct sockaddr_in6 *)cm_entry->x_server.buf;
1665 1665                          (void) kinet_ntop6((uchar_t *)&sin6->sin6_addr, fbuf,
1666 1666                                  INET6_ADDRSTRLEN);
1667 1667                          cm_ksp_data->x_port.value.ui32 = ntohs(sin6->sin6_port);
1668 1668                  } else {
1669 1669                          struct sockaddr_in  *sa;
1670 1670  
1671 1671                          sa = (struct sockaddr_in *)cm_entry->x_server.buf;
1672 1672                          b = (uchar_t *)&sa->sin_addr;
1673 1673                          (void) sprintf(fbuf,
1674 1674                              "%d.%d.%d.%d", b[0] & 0xFF, b[1] & 0xFF,
1675 1675                              b[2] & 0xFF, b[3] & 0xFF);
1676 1676                  }
1677 1677                  KSTAT_NAMED_STR_BUFLEN(&cm_ksp_data->x_server) =
1678 1678                      strlen(fbuf) + 1;
1679 1679          }
1680 1680  
1681 1681          return (0);
1682 1682  }
1683 1683  
1684 1684  
1685 1685  /*
1686 1686   * We want a version of delay which is interruptible by a UNIX signal
1687 1687   * Return EINTR if an interrupt occured.
1688 1688   */
1689 1689  static int
1690 1690  clnt_delay(clock_t ticks, bool_t nosignal)
1691 1691  {
1692 1692          if (nosignal == TRUE) {
1693 1693                  delay(ticks);
1694 1694                  return (0);
1695 1695          }
1696 1696          return (delay_sig(ticks));
1697 1697  }
1698 1698  
1699 1699  /*
1700 1700   * Wait for a connection until a timeout, or until we are
1701 1701   * signalled that there has been a connection state change.
1702 1702   */
1703 1703  static enum clnt_stat
1704 1704  connmgr_cwait(struct cm_xprt *cm_entry, const struct timeval *waitp,
1705 1705      bool_t nosignal)
1706 1706  {
1707 1707          bool_t interrupted;
1708 1708          clock_t timout, cv_stat;
1709 1709          enum clnt_stat clstat;
1710 1710          unsigned int old_state;
1711 1711  
1712 1712          ASSERT(MUTEX_HELD(&connmgr_lock));
1713 1713          /*
1714 1714           * We wait for the transport connection to be made, or an
1715 1715           * indication that it could not be made.
1716 1716           */
1717 1717          clstat = RPC_TIMEDOUT;
1718 1718          interrupted = FALSE;
1719 1719  
1720 1720          old_state = cm_entry->x_state_flags;
1721 1721          /*
1722 1722           * Now loop until cv_timedwait{_sig} returns because of
1723 1723           * a signal(0) or timeout(-1) or cv_signal(>0). But it may be
1724 1724           * cv_signalled for various other reasons too. So loop
1725 1725           * until there is a state change on the connection.
1726 1726           */
1727 1727  
1728 1728          timout = waitp->tv_sec * drv_usectohz(1000000) +
1729 1729              drv_usectohz(waitp->tv_usec) + ddi_get_lbolt();
1730 1730  
1731 1731          if (nosignal) {
1732 1732                  while ((cv_stat = cv_timedwait(&cm_entry->x_conn_cv,
1733 1733                      &connmgr_lock, timout)) > 0 &&
1734 1734                      cm_entry->x_state_flags == old_state)
1735 1735                          ;
1736 1736          } else {
1737 1737                  while ((cv_stat = cv_timedwait_sig(&cm_entry->x_conn_cv,
1738 1738                      &connmgr_lock, timout)) > 0 &&
1739 1739                      cm_entry->x_state_flags == old_state)
1740 1740                          ;
1741 1741  
1742 1742                  if (cv_stat == 0) /* got intr signal? */
1743 1743                          interrupted = TRUE;
1744 1744          }
1745 1745  
1746 1746          if ((cm_entry->x_state_flags & (X_BADSTATES|X_CONNECTED)) ==
1747 1747              X_CONNECTED) {
1748 1748                  clstat = RPC_SUCCESS;
1749 1749          } else {
1750 1750                  if (interrupted == TRUE)
1751 1751                          clstat = RPC_INTR;
1752 1752                  RPCLOG(1, "connmgr_cwait: can't connect, error: %s\n",
1753 1753                      clnt_sperrno(clstat));
1754 1754          }
1755 1755  
1756 1756          return (clstat);
1757 1757  }
1758 1758  
1759 1759  /*
1760 1760   * Primary interface for how RPC grabs a connection.
1761 1761   */
1762 1762  static struct cm_xprt *
1763 1763  connmgr_wrapget(
1764 1764          struct netbuf *retryaddr,
1765 1765          const struct timeval *waitp,
1766 1766          cku_private_t *p)
1767 1767  {
1768 1768          struct cm_xprt *cm_entry;
1769 1769  
1770 1770          cm_entry = connmgr_get(retryaddr, waitp, &p->cku_addr, p->cku_addrfmly,
1771 1771              &p->cku_srcaddr, &p->cku_err, p->cku_device,
1772 1772              p->cku_client.cl_nosignal, p->cku_useresvport, p->cku_cred);
1773 1773  
1774 1774          if (cm_entry == NULL) {
1775 1775                  /*
1776 1776                   * Re-map the call status to RPC_INTR if the err code is
1777 1777                   * EINTR. This can happen if calls status is RPC_TLIERROR.
1778 1778                   * However, don't re-map if signalling has been turned off.
1779 1779                   * XXX Really need to create a separate thread whenever
1780 1780                   * there isn't an existing connection.
1781 1781                   */
1782 1782                  if (p->cku_err.re_errno == EINTR) {
1783 1783                          if (p->cku_client.cl_nosignal == TRUE)
1784 1784                                  p->cku_err.re_errno = EIO;
1785 1785                          else
1786 1786                                  p->cku_err.re_status = RPC_INTR;
1787 1787                  }
1788 1788          }
1789 1789  
1790 1790          return (cm_entry);
1791 1791  }
1792 1792  
1793 1793  /*
1794 1794   * Obtains a transport to the server specified in addr.  If a suitable transport
1795 1795   * does not already exist in the list of cached transports, a new connection
1796 1796   * is created, connected, and added to the list. The connection is for sending
1797 1797   * only - the reply message may come back on another transport connection.
1798 1798   *
1799 1799   * To implement round-robin load balancing with multiple client connections,
1800 1800   * the last entry on the list is always selected. Once the entry is selected
1801 1801   * it's re-inserted to the head of the list.
1802 1802   */
1803 1803  static struct cm_xprt *
1804 1804  connmgr_get(
1805 1805          struct netbuf   *retryaddr,
1806 1806          const struct timeval    *waitp, /* changed to a ptr to converse stack */
1807 1807          struct netbuf   *destaddr,
1808 1808          int             addrfmly,
1809 1809          struct netbuf   *srcaddr,
1810 1810          struct rpc_err  *rpcerr,
1811 1811          dev_t           device,
1812 1812          bool_t          nosignal,
1813 1813          int             useresvport,
1814 1814          cred_t          *cr)
1815 1815  {
1816 1816          struct cm_xprt *cm_entry;
1817 1817          struct cm_xprt *lru_entry;
1818 1818          struct cm_xprt **cmp, **prev;
1819 1819          queue_t *wq;
1820 1820          TIUSER *tiptr;
1821 1821          int i;
1822 1822          int retval;
1823 1823          int tidu_size;
1824 1824          bool_t  connected;
1825 1825          zoneid_t zoneid = rpc_zoneid();
1826 1826  
1827 1827          /*
1828 1828           * If the call is not a retry, look for a transport entry that
1829 1829           * goes to the server of interest.
1830 1830           */
1831 1831          mutex_enter(&connmgr_lock);
1832 1832  
1833 1833          if (retryaddr == NULL) {
1834 1834  use_new_conn:
1835 1835                  i = 0;
1836 1836                  cm_entry = lru_entry = NULL;
1837 1837  
1838 1838                  prev = cmp = &cm_hd;
1839 1839                  while ((cm_entry = *cmp) != NULL) {
1840 1840                          ASSERT(cm_entry != cm_entry->x_next);
1841 1841                          /*
1842 1842                           * Garbage collect conections that are marked
1843 1843                           * for needs disconnect.
1844 1844                           */
1845 1845                          if (cm_entry->x_needdis) {
1846 1846                                  CONN_HOLD(cm_entry);
1847 1847                                  connmgr_dis_and_wait(cm_entry);
1848 1848                                  connmgr_release(cm_entry);
1849 1849                                  /*
1850 1850                                   * connmgr_lock could have been
1851 1851                                   * dropped for the disconnect
1852 1852                                   * processing so start over.
1853 1853                                   */
1854 1854                                  goto use_new_conn;
1855 1855                          }
1856 1856  
1857 1857                          /*
1858 1858                           * Garbage collect the dead connections that have
1859 1859                           * no threads working on them.
1860 1860                           */
1861 1861                          if ((cm_entry->x_state_flags & (X_DEAD|X_THREAD)) ==
1862 1862                              X_DEAD) {
1863 1863                                  mutex_enter(&cm_entry->x_lock);
1864 1864                                  if (cm_entry->x_ref != 0) {
1865 1865                                          /*
1866 1866                                           * Currently in use.
1867 1867                                           * Cleanup later.
1868 1868                                           */
1869 1869                                          cmp = &cm_entry->x_next;
1870 1870                                          mutex_exit(&cm_entry->x_lock);
1871 1871                                          continue;
1872 1872                                  }
1873 1873                                  mutex_exit(&cm_entry->x_lock);
1874 1874                                  *cmp = cm_entry->x_next;
1875 1875                                  mutex_exit(&connmgr_lock);
1876 1876                                  connmgr_close(cm_entry);
1877 1877                                  mutex_enter(&connmgr_lock);
1878 1878                                  goto use_new_conn;
1879 1879                          }
1880 1880  
1881 1881  
1882 1882                          if ((cm_entry->x_state_flags & X_BADSTATES) == 0 &&
1883 1883                              cm_entry->x_zoneid == zoneid &&
1884 1884                              cm_entry->x_rdev == device &&
1885 1885                              destaddr->len == cm_entry->x_server.len &&
1886 1886                              bcmp(destaddr->buf, cm_entry->x_server.buf,
1887 1887                              destaddr->len) == 0) {
1888 1888                                  /*
1889 1889                                   * If the matching entry isn't connected,
1890 1890                                   * attempt to reconnect it.
1891 1891                                   */
1892 1892                                  if (cm_entry->x_connected == FALSE) {
1893 1893                                          /*
1894 1894                                           * We don't go through trying
1895 1895                                           * to find the least recently
1896 1896                                           * used connected because
1897 1897                                           * connmgr_reconnect() briefly
1898 1898                                           * dropped the connmgr_lock,
1899 1899                                           * allowing a window for our
1900 1900                                           * accounting to be messed up.
1901 1901                                           * In any case, a re-connected
1902 1902                                           * connection is as good as
1903 1903                                           * a LRU connection.
1904 1904                                           */
1905 1905                                          return (connmgr_wrapconnect(cm_entry,
1906 1906                                              waitp, destaddr, addrfmly, srcaddr,
1907 1907                                              rpcerr, TRUE, nosignal, cr));
1908 1908                                  }
1909 1909                                  i++;
1910 1910  
1911 1911                                  /* keep track of the last entry */
1912 1912                                  lru_entry = cm_entry;
1913 1913                                  prev = cmp;
1914 1914                          }
1915 1915                          cmp = &cm_entry->x_next;
1916 1916                  }
1917 1917  
1918 1918                  if (i > clnt_max_conns) {
1919 1919                          RPCLOG(8, "connmgr_get: too many conns, dooming entry"
1920 1920                              " %p\n", (void *)lru_entry->x_tiptr);
1921 1921                          lru_entry->x_doomed = TRUE;
1922 1922                          goto use_new_conn;
1923 1923                  }
1924 1924  
1925 1925                  /*
1926 1926                   * If we are at the maximum number of connections to
1927 1927                   * the server, hand back the least recently used one.
1928 1928                   */
1929 1929                  if (i == clnt_max_conns) {
1930 1930                          /*
1931 1931                           * Copy into the handle the source address of
1932 1932                           * the connection, which we will use in case of
1933 1933                           * a later retry.
1934 1934                           */
1935 1935                          if (srcaddr->len != lru_entry->x_src.len) {
1936 1936                                  if (srcaddr->len > 0)
1937 1937                                          kmem_free(srcaddr->buf,
1938 1938                                              srcaddr->maxlen);
1939 1939                                  srcaddr->buf = kmem_zalloc(
1940 1940                                      lru_entry->x_src.len, KM_SLEEP);
1941 1941                                  srcaddr->maxlen = srcaddr->len =
1942 1942                                      lru_entry->x_src.len;
1943 1943                          }
1944 1944                          bcopy(lru_entry->x_src.buf, srcaddr->buf, srcaddr->len);
1945 1945                          RPCLOG(2, "connmgr_get: call going out on %p\n",
1946 1946                              (void *)lru_entry);
1947 1947                          lru_entry->x_time = ddi_get_lbolt();
1948 1948                          CONN_HOLD(lru_entry);
1949 1949  
1950 1950                          if ((i > 1) && (prev != &cm_hd)) {
1951 1951                                  /*
1952 1952                                   * remove and re-insert entry at head of list.
1953 1953                                   */
1954 1954                                  *prev = lru_entry->x_next;
1955 1955                                  lru_entry->x_next = cm_hd;
1956 1956                                  cm_hd = lru_entry;
1957 1957                          }
1958 1958  
1959 1959                          mutex_exit(&connmgr_lock);
1960 1960                          return (lru_entry);
1961 1961                  }
1962 1962  
1963 1963          } else {
1964 1964                  /*
1965 1965                   * This is the retry case (retryaddr != NULL).  Retries must
1966 1966                   * be sent on the same source port as the original call.
1967 1967                   */
1968 1968  
1969 1969                  /*
1970 1970                   * Walk the list looking for a connection with a source address
1971 1971                   * that matches the retry address.
1972 1972                   */
1973 1973  start_retry_loop:
1974 1974                  cmp = &cm_hd;
1975 1975                  while ((cm_entry = *cmp) != NULL) {
1976 1976                          ASSERT(cm_entry != cm_entry->x_next);
1977 1977  
1978 1978                          /*
1979 1979                           * determine if this connection matches the passed
1980 1980                           * in retry address.  If it does not match, advance
1981 1981                           * to the next element on the list.
1982 1982                           */
1983 1983                          if (zoneid != cm_entry->x_zoneid ||
1984 1984                              device != cm_entry->x_rdev ||
1985 1985                              retryaddr->len != cm_entry->x_src.len ||
1986 1986                              bcmp(retryaddr->buf, cm_entry->x_src.buf,
1987 1987                              retryaddr->len) != 0) {
1988 1988                                  cmp = &cm_entry->x_next;
1989 1989                                  continue;
1990 1990                          }
1991 1991                          /*
1992 1992                           * Garbage collect conections that are marked
1993 1993                           * for needs disconnect.
1994 1994                           */
1995 1995                          if (cm_entry->x_needdis) {
1996 1996                                  CONN_HOLD(cm_entry);
1997 1997                                  connmgr_dis_and_wait(cm_entry);
1998 1998                                  connmgr_release(cm_entry);
1999 1999                                  /*
2000 2000                                   * connmgr_lock could have been
2001 2001                                   * dropped for the disconnect
2002 2002                                   * processing so start over.
2003 2003                                   */
2004 2004                                  goto start_retry_loop;
2005 2005                          }
2006 2006                          /*
2007 2007                           * Garbage collect the dead connections that have
2008 2008                           * no threads working on them.
2009 2009                           */
2010 2010                          if ((cm_entry->x_state_flags & (X_DEAD|X_THREAD)) ==
2011 2011                              X_DEAD) {
2012 2012                                  mutex_enter(&cm_entry->x_lock);
2013 2013                                  if (cm_entry->x_ref != 0) {
2014 2014                                          /*
2015 2015                                           * Currently in use.
2016 2016                                           * Cleanup later.
2017 2017                                           */
2018 2018                                          cmp = &cm_entry->x_next;
2019 2019                                          mutex_exit(&cm_entry->x_lock);
2020 2020                                          continue;
2021 2021                                  }
2022 2022                                  mutex_exit(&cm_entry->x_lock);
2023 2023                                  *cmp = cm_entry->x_next;
2024 2024                                  mutex_exit(&connmgr_lock);
2025 2025                                  connmgr_close(cm_entry);
2026 2026                                  mutex_enter(&connmgr_lock);
2027 2027                                  goto start_retry_loop;
2028 2028                          }
2029 2029  
2030 2030                          /*
2031 2031                           * Sanity check: if the connection with our source
2032 2032                           * port is going to some other server, something went
2033 2033                           * wrong, as we never delete connections (i.e. release
2034 2034                           * ports) unless they have been idle.  In this case,
2035 2035                           * it is probably better to send the call out using
2036 2036                           * a new source address than to fail it altogether,
2037 2037                           * since that port may never be released.
2038 2038                           */
2039 2039                          if (destaddr->len != cm_entry->x_server.len ||
2040 2040                              bcmp(destaddr->buf, cm_entry->x_server.buf,
2041 2041                              destaddr->len) != 0) {
2042 2042                                  RPCLOG(1, "connmgr_get: tiptr %p"
2043 2043                                      " is going to a different server"
2044 2044                                      " with the port that belongs"
2045 2045                                      " to us!\n", (void *)cm_entry->x_tiptr);
2046 2046                                  retryaddr = NULL;
2047 2047                                  goto use_new_conn;
2048 2048                          }
2049 2049  
2050 2050                          /*
2051 2051                           * If the connection of interest is not connected and we
2052 2052                           * can't reconnect it, then the server is probably
2053 2053                           * still down.  Return NULL to the caller and let it
2054 2054                           * retry later if it wants to.  We have a delay so the
2055 2055                           * machine doesn't go into a tight retry loop.  If the
2056 2056                           * entry was already connected, or the reconnected was
2057 2057                           * successful, return this entry.
2058 2058                           */
2059 2059                          if (cm_entry->x_connected == FALSE) {
2060 2060                                  return (connmgr_wrapconnect(cm_entry,
2061 2061                                      waitp, destaddr, addrfmly, NULL,
2062 2062                                      rpcerr, TRUE, nosignal, cr));
2063 2063                          } else {
2064 2064                                  CONN_HOLD(cm_entry);
2065 2065  
2066 2066                                  cm_entry->x_time = ddi_get_lbolt();
2067 2067                                  mutex_exit(&connmgr_lock);
2068 2068                                  RPCLOG(2, "connmgr_get: found old "
2069 2069                                      "transport %p for retry\n",
2070 2070                                      (void *)cm_entry);
2071 2071                                  return (cm_entry);
2072 2072                          }
2073 2073                  }
2074 2074  
2075 2075                  /*
2076 2076                   * We cannot find an entry in the list for this retry.
2077 2077                   * Either the entry has been removed temporarily to be
2078 2078                   * reconnected by another thread, or the original call
2079 2079                   * got a port but never got connected,
2080 2080                   * and hence the transport never got put in the
2081 2081                   * list.  Fall through to the "create new connection" code -
2082 2082                   * the former case will fail there trying to rebind the port,
2083 2083                   * and the later case (and any other pathological cases) will
2084 2084                   * rebind and reconnect and not hang the client machine.
2085 2085                   */
2086 2086                  RPCLOG0(8, "connmgr_get: no entry in list for retry\n");
2087 2087          }
2088 2088          /*
2089 2089           * Set up a transport entry in the connection manager's list.
2090 2090           */
2091 2091          cm_entry = (struct cm_xprt *)
2092 2092              kmem_zalloc(sizeof (struct cm_xprt), KM_SLEEP);
2093 2093  
2094 2094          cm_entry->x_server.buf = kmem_zalloc(destaddr->len, KM_SLEEP);
2095 2095          bcopy(destaddr->buf, cm_entry->x_server.buf, destaddr->len);
2096 2096          cm_entry->x_server.len = cm_entry->x_server.maxlen = destaddr->len;
2097 2097  
2098 2098          cm_entry->x_state_flags = X_THREAD;
2099 2099          cm_entry->x_ref = 1;
2100 2100          cm_entry->x_family = addrfmly;
2101 2101          cm_entry->x_rdev = device;
2102 2102          cm_entry->x_zoneid = zoneid;
2103 2103          mutex_init(&cm_entry->x_lock, NULL, MUTEX_DEFAULT, NULL);
2104 2104          cv_init(&cm_entry->x_cv, NULL, CV_DEFAULT, NULL);
2105 2105          cv_init(&cm_entry->x_conn_cv, NULL, CV_DEFAULT, NULL);
2106 2106          cv_init(&cm_entry->x_dis_cv, NULL, CV_DEFAULT, NULL);
2107 2107  
2108 2108          /*
2109 2109           * Note that we add this partially initialized entry to the
2110 2110           * connection list. This is so that we don't have connections to
2111 2111           * the same server.
2112 2112           *
2113 2113           * Note that x_src is not initialized at this point. This is because
2114 2114           * retryaddr might be NULL in which case x_src is whatever
2115 2115           * t_kbind/bindresvport gives us. If another thread wants a
2116 2116           * connection to the same server, seemingly we have an issue, but we
2117 2117           * don't. If the other thread comes in with retryaddr == NULL, then it
2118 2118           * will never look at x_src, and it will end up waiting in
2119 2119           * connmgr_cwait() for the first thread to finish the connection
2120 2120           * attempt. If the other thread comes in with retryaddr != NULL, then
2121 2121           * that means there was a request sent on a connection, in which case
2122 2122           * the the connection should already exist. Thus the first thread
2123 2123           * never gets here ... it finds the connection it its server in the
2124 2124           * connection list.
2125 2125           *
2126 2126           * But even if theory is wrong, in the retryaddr != NULL case, the 2nd
2127 2127           * thread will skip us because x_src.len == 0.
2128 2128           */
2129 2129          cm_entry->x_next = cm_hd;
2130 2130          cm_hd = cm_entry;
2131 2131          mutex_exit(&connmgr_lock);
2132 2132  
2133 2133          /*
2134 2134           * Either we didn't find an entry to the server of interest, or we
2135 2135           * don't have the maximum number of connections to that server -
2136 2136           * create a new connection.
2137 2137           */
2138 2138          RPCLOG0(8, "connmgr_get: creating new connection\n");
2139 2139          rpcerr->re_status = RPC_TLIERROR;
2140 2140  
2141 2141          i = t_kopen(NULL, device, FREAD|FWRITE|FNDELAY, &tiptr, zone_kcred());
2142 2142          if (i) {
2143 2143                  RPCLOG(1, "connmgr_get: can't open cots device, error %d\n", i);
2144 2144                  rpcerr->re_errno = i;
2145 2145                  connmgr_cancelconn(cm_entry);
2146 2146                  return (NULL);
2147 2147          }
2148 2148          rpc_poptimod(tiptr->fp->f_vnode);
2149 2149  
2150 2150          if (i = strioctl(tiptr->fp->f_vnode, I_PUSH, (intptr_t)"rpcmod", 0,
2151 2151              K_TO_K, kcred, &retval)) {
2152 2152                  RPCLOG(1, "connmgr_get: can't push cots module, %d\n", i);
2153 2153                  (void) t_kclose(tiptr, 1);
2154 2154                  rpcerr->re_errno = i;
2155 2155                  connmgr_cancelconn(cm_entry);
2156 2156                  return (NULL);
2157 2157          }
2158 2158  
2159 2159          if (i = strioctl(tiptr->fp->f_vnode, RPC_CLIENT, 0, 0, K_TO_K,
2160 2160              kcred, &retval)) {
2161 2161                  RPCLOG(1, "connmgr_get: can't set client status with cots "
2162 2162                      "module, %d\n", i);
2163 2163                  (void) t_kclose(tiptr, 1);
2164 2164                  rpcerr->re_errno = i;
2165 2165                  connmgr_cancelconn(cm_entry);
2166 2166                  return (NULL);
2167 2167          }
2168 2168  
2169 2169          mutex_enter(&connmgr_lock);
2170 2170  
2171 2171          wq = tiptr->fp->f_vnode->v_stream->sd_wrq->q_next;
2172 2172          cm_entry->x_wq = wq;
2173 2173  
2174 2174          mutex_exit(&connmgr_lock);
2175 2175  
2176 2176          if (i = strioctl(tiptr->fp->f_vnode, I_PUSH, (intptr_t)"timod", 0,
2177 2177              K_TO_K, kcred, &retval)) {
2178 2178                  RPCLOG(1, "connmgr_get: can't push timod, %d\n", i);
2179 2179                  (void) t_kclose(tiptr, 1);
2180 2180                  rpcerr->re_errno = i;
2181 2181                  connmgr_cancelconn(cm_entry);
2182 2182                  return (NULL);
2183 2183          }
2184 2184  
2185 2185          /*
2186 2186           * If the caller has not specified reserved port usage then
2187 2187           * take the system default.
2188 2188           */
2189 2189          if (useresvport == -1)
2190 2190                  useresvport = clnt_cots_do_bindresvport;
2191 2191  
2192 2192          if ((useresvport || retryaddr != NULL) &&
2193 2193              (addrfmly == AF_INET || addrfmly == AF_INET6)) {
2194 2194                  bool_t alloc_src = FALSE;
2195 2195  
2196 2196                  if (srcaddr->len != destaddr->len) {
2197 2197                          kmem_free(srcaddr->buf, srcaddr->maxlen);
2198 2198                          srcaddr->buf = kmem_zalloc(destaddr->len, KM_SLEEP);
2199 2199                          srcaddr->maxlen = destaddr->len;
2200 2200                          srcaddr->len = destaddr->len;
2201 2201                          alloc_src = TRUE;
2202 2202                  }
2203 2203  
2204 2204                  if ((i = bindresvport(tiptr, retryaddr, srcaddr, TRUE)) != 0) {
2205 2205                          (void) t_kclose(tiptr, 1);
2206 2206                          RPCLOG(1, "connmgr_get: couldn't bind, retryaddr: "
2207 2207                              "%p\n", (void *)retryaddr);
2208 2208  
2209 2209                          /*
2210 2210                           * 1225408: If we allocated a source address, then it
2211 2211                           * is either garbage or all zeroes. In that case
2212 2212                           * we need to clear srcaddr.
2213 2213                           */
2214 2214                          if (alloc_src == TRUE) {
2215 2215                                  kmem_free(srcaddr->buf, srcaddr->maxlen);
2216 2216                                  srcaddr->maxlen = srcaddr->len = 0;
2217 2217                                  srcaddr->buf = NULL;
2218 2218                          }
2219 2219                          rpcerr->re_errno = i;
2220 2220                          connmgr_cancelconn(cm_entry);
2221 2221                          return (NULL);
2222 2222                  }
2223 2223          } else {
2224 2224                  if ((i = t_kbind(tiptr, NULL, NULL)) != 0) {
2225 2225                          RPCLOG(1, "clnt_cots_kcreate: t_kbind: %d\n", i);
2226 2226                          (void) t_kclose(tiptr, 1);
2227 2227                          rpcerr->re_errno = i;
2228 2228                          connmgr_cancelconn(cm_entry);
2229 2229                          return (NULL);
2230 2230                  }
2231 2231          }
2232 2232  
2233 2233          {
2234 2234                  /*
2235 2235                   * Keep the kernel stack lean. Don't move this call
2236 2236                   * declaration to the top of this function because a
2237 2237                   * call is declared in connmgr_wrapconnect()
2238 2238                   */
2239 2239                  calllist_t call;
2240 2240  
2241 2241                  bzero(&call, sizeof (call));
2242 2242                  cv_init(&call.call_cv, NULL, CV_DEFAULT, NULL);
2243 2243  
2244 2244                  /*
2245 2245                   * This is a bound end-point so don't close it's stream.
2246 2246                   */
2247 2247                  connected = connmgr_connect(cm_entry, wq, destaddr, addrfmly,
2248 2248                      &call, &tidu_size, FALSE, waitp, nosignal, cr);
2249 2249                  *rpcerr = call.call_err;
2250 2250                  cv_destroy(&call.call_cv);
2251 2251  
2252 2252          }
2253 2253  
2254 2254          mutex_enter(&connmgr_lock);
2255 2255  
2256 2256          /*
2257 2257           * Set up a transport entry in the connection manager's list.
2258 2258           */
2259 2259          cm_entry->x_src.buf = kmem_zalloc(srcaddr->len, KM_SLEEP);
2260 2260          bcopy(srcaddr->buf, cm_entry->x_src.buf, srcaddr->len);
2261 2261          cm_entry->x_src.len = cm_entry->x_src.maxlen = srcaddr->len;
2262 2262  
2263 2263          cm_entry->x_tiptr = tiptr;
2264 2264          cm_entry->x_time = ddi_get_lbolt();
2265 2265  
2266 2266          if (tiptr->tp_info.servtype == T_COTS_ORD)
2267 2267                  cm_entry->x_ordrel = TRUE;
2268 2268          else
2269 2269                  cm_entry->x_ordrel = FALSE;
2270 2270  
2271 2271          cm_entry->x_tidu_size = tidu_size;
2272 2272  
  
    | 
      ↓ open down ↓ | 
    1785 lines elided | 
    
      ↑ open up ↑ | 
  
2273 2273          if (cm_entry->x_early_disc) {
2274 2274                  /*
2275 2275                   * We need to check if a disconnect request has come
2276 2276                   * while we are connected, if so, then we need to
2277 2277                   * set rpcerr->re_status appropriately before returning
2278 2278                   * NULL to caller.
2279 2279                   */
2280 2280                  if (rpcerr->re_status == RPC_SUCCESS)
2281 2281                          rpcerr->re_status = RPC_XPRTFAILED;
2282 2282                  cm_entry->x_connected = FALSE;
     2283 +                cm_entry->x_dead = TRUE;
2283 2284          } else
2284 2285                  cm_entry->x_connected = connected;
2285 2286  
2286 2287          /*
2287 2288           * There could be a discrepancy here such that
2288 2289           * x_early_disc is TRUE yet connected is TRUE as well
2289 2290           * and the connection is actually connected. In that case
2290 2291           * lets be conservative and declare the connection as not
2291 2292           * connected.
2292 2293           */
2293 2294          cm_entry->x_early_disc = FALSE;
2294 2295          cm_entry->x_needdis = (cm_entry->x_connected == FALSE);
2295 2296          cm_entry->x_ctime = ddi_get_lbolt();
2296 2297  
2297 2298          /*
2298 2299           * Notify any threads waiting that the connection attempt is done.
2299 2300           */
2300 2301          cm_entry->x_thread = FALSE;
2301 2302          cv_broadcast(&cm_entry->x_conn_cv);
2302 2303  
2303 2304          if (cm_entry->x_connected == FALSE) {
2304 2305                  mutex_exit(&connmgr_lock);
2305 2306                  connmgr_release(cm_entry);
2306 2307                  return (NULL);
2307 2308          }
2308 2309  
2309 2310          mutex_exit(&connmgr_lock);
2310 2311  
2311 2312          return (cm_entry);
2312 2313  }
2313 2314  
2314 2315  /*
2315 2316   * Keep the cm_xprt entry on the connecton list when making a connection. This
2316 2317   * is to prevent multiple connections to a slow server from appearing.
2317 2318   * We use the bit field x_thread to tell if a thread is doing a connection
2318 2319   * which keeps other interested threads from messing with connection.
2319 2320   * Those other threads just wait if x_thread is set.
2320 2321   *
2321 2322   * If x_thread is not set, then we do the actual work of connecting via
2322 2323   * connmgr_connect().
2323 2324   *
2324 2325   * mutex convention: called with connmgr_lock held, returns with it released.
2325 2326   */
2326 2327  static struct cm_xprt *
2327 2328  connmgr_wrapconnect(
2328 2329          struct cm_xprt  *cm_entry,
2329 2330          const struct timeval    *waitp,
2330 2331          struct netbuf   *destaddr,
2331 2332          int             addrfmly,
2332 2333          struct netbuf   *srcaddr,
2333 2334          struct rpc_err  *rpcerr,
2334 2335          bool_t          reconnect,
2335 2336          bool_t          nosignal,
2336 2337          cred_t          *cr)
2337 2338  {
2338 2339          ASSERT(MUTEX_HELD(&connmgr_lock));
2339 2340          /*
2340 2341           * Hold this entry as we are about to drop connmgr_lock.
2341 2342           */
2342 2343          CONN_HOLD(cm_entry);
2343 2344  
2344 2345          /*
2345 2346           * If there is a thread already making a connection for us, then
2346 2347           * wait for it to complete the connection.
2347 2348           */
2348 2349          if (cm_entry->x_thread == TRUE) {
2349 2350                  rpcerr->re_status = connmgr_cwait(cm_entry, waitp, nosignal);
2350 2351  
2351 2352                  if (rpcerr->re_status != RPC_SUCCESS) {
2352 2353                          mutex_exit(&connmgr_lock);
2353 2354                          connmgr_release(cm_entry);
2354 2355                          return (NULL);
2355 2356                  }
2356 2357          } else {
2357 2358                  bool_t connected;
2358 2359                  calllist_t call;
2359 2360  
2360 2361                  cm_entry->x_thread = TRUE;
2361 2362  
2362 2363                  while (cm_entry->x_needrel == TRUE) {
2363 2364                          cm_entry->x_needrel = FALSE;
2364 2365  
2365 2366                          connmgr_sndrel(cm_entry);
2366 2367                          delay(drv_usectohz(1000000));
2367 2368  
2368 2369                          mutex_enter(&connmgr_lock);
2369 2370                  }
2370 2371  
2371 2372                  /*
2372 2373                   * If we need to send a T_DISCON_REQ, send one.
2373 2374                   */
2374 2375                  connmgr_dis_and_wait(cm_entry);
2375 2376  
2376 2377                  mutex_exit(&connmgr_lock);
2377 2378  
2378 2379                  bzero(&call, sizeof (call));
2379 2380                  cv_init(&call.call_cv, NULL, CV_DEFAULT, NULL);
2380 2381  
2381 2382                  connected = connmgr_connect(cm_entry, cm_entry->x_wq,
2382 2383                      destaddr, addrfmly, &call, &cm_entry->x_tidu_size,
2383 2384                      reconnect, waitp, nosignal, cr);
2384 2385  
2385 2386                  *rpcerr = call.call_err;
2386 2387                  cv_destroy(&call.call_cv);
2387 2388  
2388 2389                  mutex_enter(&connmgr_lock);
2389 2390  
2390 2391  
  
    | 
      ↓ open down ↓ | 
    98 lines elided | 
    
      ↑ open up ↑ | 
  
2391 2392                  if (cm_entry->x_early_disc) {
2392 2393                          /*
2393 2394                           * We need to check if a disconnect request has come
2394 2395                           * while we are connected, if so, then we need to
2395 2396                           * set rpcerr->re_status appropriately before returning
2396 2397                           * NULL to caller.
2397 2398                           */
2398 2399                          if (rpcerr->re_status == RPC_SUCCESS)
2399 2400                                  rpcerr->re_status = RPC_XPRTFAILED;
2400 2401                          cm_entry->x_connected = FALSE;
     2402 +                        cm_entry->x_dead = TRUE;
2401 2403                  } else
2402 2404                          cm_entry->x_connected = connected;
2403 2405  
2404 2406                  /*
2405 2407                   * There could be a discrepancy here such that
2406 2408                   * x_early_disc is TRUE yet connected is TRUE as well
2407 2409                   * and the connection is actually connected. In that case
2408 2410                   * lets be conservative and declare the connection as not
2409 2411                   * connected.
2410 2412                   */
2411 2413  
2412 2414                  cm_entry->x_early_disc = FALSE;
2413 2415                  cm_entry->x_needdis = (cm_entry->x_connected == FALSE);
2414 2416  
2415 2417  
2416 2418                  /*
2417 2419                   * connmgr_connect() may have given up before the connection
2418 2420                   * actually timed out. So ensure that before the next
2419 2421                   * connection attempt we do a disconnect.
2420 2422                   */
2421 2423                  cm_entry->x_ctime = ddi_get_lbolt();
2422 2424                  cm_entry->x_thread = FALSE;
2423 2425  
2424 2426                  cv_broadcast(&cm_entry->x_conn_cv);
2425 2427  
2426 2428                  if (cm_entry->x_connected == FALSE) {
2427 2429                          mutex_exit(&connmgr_lock);
2428 2430                          connmgr_release(cm_entry);
2429 2431                          return (NULL);
2430 2432                  }
2431 2433          }
2432 2434  
2433 2435          if (srcaddr != NULL) {
2434 2436                  /*
2435 2437                   * Copy into the handle the
2436 2438                   * source address of the
2437 2439                   * connection, which we will use
2438 2440                   * in case of a later retry.
2439 2441                   */
2440 2442                  if (srcaddr->len != cm_entry->x_src.len) {
2441 2443                          if (srcaddr->maxlen > 0)
2442 2444                                  kmem_free(srcaddr->buf, srcaddr->maxlen);
2443 2445                          srcaddr->buf = kmem_zalloc(cm_entry->x_src.len,
2444 2446                              KM_SLEEP);
2445 2447                          srcaddr->maxlen = srcaddr->len =
2446 2448                              cm_entry->x_src.len;
2447 2449                  }
2448 2450                  bcopy(cm_entry->x_src.buf, srcaddr->buf, srcaddr->len);
2449 2451          }
2450 2452          cm_entry->x_time = ddi_get_lbolt();
2451 2453          mutex_exit(&connmgr_lock);
2452 2454          return (cm_entry);
2453 2455  }
2454 2456  
2455 2457  /*
2456 2458   * If we need to send a T_DISCON_REQ, send one.
2457 2459   */
2458 2460  static void
2459 2461  connmgr_dis_and_wait(struct cm_xprt *cm_entry)
2460 2462  {
2461 2463          ASSERT(MUTEX_HELD(&connmgr_lock));
2462 2464          for (;;) {
2463 2465                  while (cm_entry->x_needdis == TRUE) {
2464 2466                          RPCLOG(8, "connmgr_dis_and_wait: need "
2465 2467                              "T_DISCON_REQ for connection 0x%p\n",
2466 2468                              (void *)cm_entry);
2467 2469                          cm_entry->x_needdis = FALSE;
2468 2470                          cm_entry->x_waitdis = TRUE;
2469 2471  
2470 2472                          connmgr_snddis(cm_entry);
2471 2473  
2472 2474                          mutex_enter(&connmgr_lock);
2473 2475                  }
2474 2476  
2475 2477                  if (cm_entry->x_waitdis == TRUE) {
2476 2478                          clock_t timout;
2477 2479  
2478 2480                          RPCLOG(8, "connmgr_dis_and_wait waiting for "
2479 2481                              "T_DISCON_REQ's ACK for connection %p\n",
2480 2482                              (void *)cm_entry);
2481 2483  
2482 2484                          timout = clnt_cots_min_conntout * drv_usectohz(1000000);
2483 2485  
2484 2486                          /*
2485 2487                           * The TPI spec says that the T_DISCON_REQ
2486 2488                           * will get acknowledged, but in practice
2487 2489                           * the ACK may never get sent. So don't
2488 2490                           * block forever.
2489 2491                           */
2490 2492                          (void) cv_reltimedwait(&cm_entry->x_dis_cv,
2491 2493                              &connmgr_lock, timout, TR_CLOCK_TICK);
2492 2494                  }
2493 2495                  /*
2494 2496                   * If we got the ACK, break. If we didn't,
2495 2497                   * then send another T_DISCON_REQ.
2496 2498                   */
2497 2499                  if (cm_entry->x_waitdis == FALSE) {
2498 2500                          break;
2499 2501                  } else {
2500 2502                          RPCLOG(8, "connmgr_dis_and_wait: did"
2501 2503                              "not get T_DISCON_REQ's ACK for "
2502 2504                              "connection  %p\n", (void *)cm_entry);
2503 2505                          cm_entry->x_needdis = TRUE;
2504 2506                  }
2505 2507          }
2506 2508  }
2507 2509  
2508 2510  static void
2509 2511  connmgr_cancelconn(struct cm_xprt *cm_entry)
2510 2512  {
2511 2513          /*
2512 2514           * Mark the connection table entry as dead; the next thread that
2513 2515           * goes through connmgr_release() will notice this and deal with it.
2514 2516           */
2515 2517          mutex_enter(&connmgr_lock);
2516 2518          cm_entry->x_dead = TRUE;
2517 2519  
2518 2520          /*
2519 2521           * Notify any threads waiting for the connection that it isn't
2520 2522           * going to happen.
2521 2523           */
2522 2524          cm_entry->x_thread = FALSE;
2523 2525          cv_broadcast(&cm_entry->x_conn_cv);
2524 2526          mutex_exit(&connmgr_lock);
2525 2527  
2526 2528          connmgr_release(cm_entry);
2527 2529  }
2528 2530  
2529 2531  static void
2530 2532  connmgr_close(struct cm_xprt *cm_entry)
2531 2533  {
2532 2534          mutex_enter(&cm_entry->x_lock);
2533 2535          while (cm_entry->x_ref != 0) {
2534 2536                  /*
2535 2537                   * Must be a noninterruptible wait.
2536 2538                   */
2537 2539                  cv_wait(&cm_entry->x_cv, &cm_entry->x_lock);
2538 2540          }
2539 2541  
2540 2542          if (cm_entry->x_tiptr != NULL)
2541 2543                  (void) t_kclose(cm_entry->x_tiptr, 1);
2542 2544  
2543 2545          mutex_exit(&cm_entry->x_lock);
2544 2546          if (cm_entry->x_ksp != NULL) {
2545 2547                  mutex_enter(&connmgr_lock);
2546 2548                  cm_entry->x_ksp->ks_private = NULL;
2547 2549                  mutex_exit(&connmgr_lock);
2548 2550  
2549 2551                  /*
2550 2552                   * Must free the buffer we allocated for the
2551 2553                   * server address in the update function
2552 2554                   */
2553 2555                  if (((struct cm_kstat_xprt *)(cm_entry->x_ksp->ks_data))->
2554 2556                      x_server.value.str.addr.ptr != NULL)
2555 2557                          kmem_free(((struct cm_kstat_xprt *)(cm_entry->x_ksp->
2556 2558                              ks_data))->x_server.value.str.addr.ptr,
2557 2559                              INET6_ADDRSTRLEN);
2558 2560                  kmem_free(cm_entry->x_ksp->ks_data,
2559 2561                      cm_entry->x_ksp->ks_data_size);
2560 2562                  kstat_delete(cm_entry->x_ksp);
2561 2563          }
2562 2564  
2563 2565          mutex_destroy(&cm_entry->x_lock);
2564 2566          cv_destroy(&cm_entry->x_cv);
2565 2567          cv_destroy(&cm_entry->x_conn_cv);
2566 2568          cv_destroy(&cm_entry->x_dis_cv);
2567 2569  
2568 2570          if (cm_entry->x_server.buf != NULL)
2569 2571                  kmem_free(cm_entry->x_server.buf, cm_entry->x_server.maxlen);
2570 2572          if (cm_entry->x_src.buf != NULL)
2571 2573                  kmem_free(cm_entry->x_src.buf, cm_entry->x_src.maxlen);
2572 2574          kmem_free(cm_entry, sizeof (struct cm_xprt));
2573 2575  }
2574 2576  
2575 2577  /*
2576 2578   * Called by KRPC after sending the call message to release the connection
2577 2579   * it was using.
2578 2580   */
2579 2581  static void
2580 2582  connmgr_release(struct cm_xprt *cm_entry)
2581 2583  {
2582 2584          mutex_enter(&cm_entry->x_lock);
2583 2585          cm_entry->x_ref--;
2584 2586          if (cm_entry->x_ref == 0)
2585 2587                  cv_signal(&cm_entry->x_cv);
2586 2588          mutex_exit(&cm_entry->x_lock);
2587 2589  }
2588 2590  
2589 2591  /*
2590 2592   * Set TCP receive and xmit buffer size for RPC connections.
2591 2593   */
2592 2594  static bool_t
2593 2595  connmgr_setbufsz(calllist_t *e, queue_t *wq, cred_t *cr)
2594 2596  {
2595 2597          int ok = FALSE;
2596 2598          int val;
2597 2599  
2598 2600          if (rpc_default_tcp_bufsz)
2599 2601                  return (FALSE);
2600 2602  
2601 2603          /*
2602 2604           * Only set new buffer size if it's larger than the system
2603 2605           * default buffer size. If smaller buffer size is needed
2604 2606           * then use /etc/system to set rpc_default_tcp_bufsz to 1.
2605 2607           */
2606 2608          ok = connmgr_getopt_int(wq, SOL_SOCKET, SO_RCVBUF, &val, e, cr);
2607 2609          if ((ok == TRUE) && (val < rpc_send_bufsz)) {
2608 2610                  ok = connmgr_setopt_int(wq, SOL_SOCKET, SO_RCVBUF,
2609 2611                      rpc_send_bufsz, e, cr);
2610 2612                  DTRACE_PROBE2(krpc__i__connmgr_rcvbufsz,
2611 2613                      int, ok, calllist_t *, e);
2612 2614          }
2613 2615  
2614 2616          ok = connmgr_getopt_int(wq, SOL_SOCKET, SO_SNDBUF, &val, e, cr);
2615 2617          if ((ok == TRUE) && (val < rpc_recv_bufsz)) {
2616 2618                  ok = connmgr_setopt_int(wq, SOL_SOCKET, SO_SNDBUF,
2617 2619                      rpc_recv_bufsz, e, cr);
2618 2620                  DTRACE_PROBE2(krpc__i__connmgr_sndbufsz,
2619 2621                      int, ok, calllist_t *, e);
2620 2622          }
2621 2623          return (TRUE);
2622 2624  }
2623 2625  
2624 2626  /*
2625 2627   * Given an open stream, connect to the remote.  Returns true if connected,
2626 2628   * false otherwise.
2627 2629   */
2628 2630  static bool_t
2629 2631  connmgr_connect(
2630 2632          struct cm_xprt          *cm_entry,
2631 2633          queue_t                 *wq,
2632 2634          struct netbuf           *addr,
2633 2635          int                     addrfmly,
2634 2636          calllist_t              *e,
2635 2637          int                     *tidu_ptr,
2636 2638          bool_t                  reconnect,
2637 2639          const struct timeval    *waitp,
2638 2640          bool_t                  nosignal,
2639 2641          cred_t                  *cr)
2640 2642  {
2641 2643          mblk_t *mp;
2642 2644          struct T_conn_req *tcr;
2643 2645          struct T_info_ack *tinfo;
2644 2646          int interrupted, error;
2645 2647          int tidu_size, kstat_instance;
2646 2648  
2647 2649          /* if it's a reconnect, flush any lingering data messages */
2648 2650          if (reconnect)
2649 2651                  (void) putctl1(wq, M_FLUSH, FLUSHRW);
2650 2652  
2651 2653          /*
2652 2654           * Note: if the receiver uses SCM_UCRED/getpeerucred the pid will
2653 2655           * appear as -1.
2654 2656           */
2655 2657          mp = allocb_cred(sizeof (*tcr) + addr->len, cr, NOPID);
2656 2658          if (mp == NULL) {
2657 2659                  /*
2658 2660                   * This is unfortunate, but we need to look up the stats for
2659 2661                   * this zone to increment the "memory allocation failed"
2660 2662                   * counter.  curproc->p_zone is safe since we're initiating a
2661 2663                   * connection and not in some strange streams context.
2662 2664                   */
2663 2665                  struct rpcstat *rpcstat;
2664 2666  
2665 2667                  rpcstat = zone_getspecific(rpcstat_zone_key, rpc_zone());
2666 2668                  ASSERT(rpcstat != NULL);
2667 2669  
2668 2670                  RPCLOG0(1, "connmgr_connect: cannot alloc mp for "
2669 2671                      "sending conn request\n");
2670 2672                  COTSRCSTAT_INCR(rpcstat->rpc_cots_client, rcnomem);
2671 2673                  e->call_status = RPC_SYSTEMERROR;
2672 2674                  e->call_reason = ENOSR;
2673 2675                  return (FALSE);
2674 2676          }
2675 2677  
2676 2678          /* Set TCP buffer size for RPC connections if needed */
2677 2679          if (addrfmly == AF_INET || addrfmly == AF_INET6)
2678 2680                  (void) connmgr_setbufsz(e, wq, cr);
2679 2681  
2680 2682          mp->b_datap->db_type = M_PROTO;
2681 2683          tcr = (struct T_conn_req *)mp->b_rptr;
2682 2684          bzero(tcr, sizeof (*tcr));
2683 2685          tcr->PRIM_type = T_CONN_REQ;
2684 2686          tcr->DEST_length = addr->len;
2685 2687          tcr->DEST_offset = sizeof (struct T_conn_req);
2686 2688          mp->b_wptr = mp->b_rptr + sizeof (*tcr);
2687 2689  
2688 2690          bcopy(addr->buf, mp->b_wptr, tcr->DEST_length);
2689 2691          mp->b_wptr += tcr->DEST_length;
2690 2692  
2691 2693          RPCLOG(8, "connmgr_connect: sending conn request on queue "
2692 2694              "%p", (void *)wq);
2693 2695          RPCLOG(8, " call %p\n", (void *)wq);
2694 2696          /*
2695 2697           * We use the entry in the handle that is normally used for
2696 2698           * waiting for RPC replies to wait for the connection accept.
2697 2699           */
2698 2700          if (clnt_dispatch_send(wq, mp, e, 0, 0) != RPC_SUCCESS) {
2699 2701                  DTRACE_PROBE(krpc__e__connmgr__connect__cantsend);
2700 2702                  freemsg(mp);
2701 2703                  return (FALSE);
2702 2704          }
2703 2705  
2704 2706          mutex_enter(&clnt_pending_lock);
2705 2707  
2706 2708          /*
2707 2709           * We wait for the transport connection to be made, or an
2708 2710           * indication that it could not be made.
2709 2711           */
2710 2712          interrupted = 0;
2711 2713  
2712 2714          /*
2713 2715           * waitforack should have been called with T_OK_ACK, but the
2714 2716           * present implementation needs to be passed T_INFO_ACK to
2715 2717           * work correctly.
2716 2718           */
2717 2719          error = waitforack(e, T_INFO_ACK, waitp, nosignal);
2718 2720          if (error == EINTR)
2719 2721                  interrupted = 1;
2720 2722          if (zone_status_get(curproc->p_zone) >= ZONE_IS_EMPTY) {
2721 2723                  /*
2722 2724                   * No time to lose; we essentially have been signaled to
2723 2725                   * quit.
2724 2726                   */
2725 2727                  interrupted = 1;
2726 2728          }
2727 2729  #ifdef RPCDEBUG
2728 2730          if (error == ETIME)
2729 2731                  RPCLOG0(8, "connmgr_connect: giving up "
2730 2732                      "on connection attempt; "
2731 2733                      "clnt_dispatch notifyconn "
2732 2734                      "diagnostic 'no one waiting for "
2733 2735                      "connection' should not be "
2734 2736                      "unexpected\n");
2735 2737  #endif
2736 2738          if (e->call_prev)
2737 2739                  e->call_prev->call_next = e->call_next;
2738 2740          else
2739 2741                  clnt_pending = e->call_next;
2740 2742          if (e->call_next)
2741 2743                  e->call_next->call_prev = e->call_prev;
2742 2744          mutex_exit(&clnt_pending_lock);
2743 2745  
2744 2746          if (e->call_status != RPC_SUCCESS || error != 0) {
2745 2747                  if (interrupted)
2746 2748                          e->call_status = RPC_INTR;
2747 2749                  else if (error == ETIME)
2748 2750                          e->call_status = RPC_TIMEDOUT;
2749 2751                  else if (error == EPROTO) {
2750 2752                          e->call_status = RPC_SYSTEMERROR;
2751 2753                          e->call_reason = EPROTO;
2752 2754                  }
2753 2755  
2754 2756                  RPCLOG(8, "connmgr_connect: can't connect, status: "
2755 2757                      "%s\n", clnt_sperrno(e->call_status));
2756 2758  
2757 2759                  if (e->call_reply) {
2758 2760                          freemsg(e->call_reply);
2759 2761                          e->call_reply = NULL;
2760 2762                  }
2761 2763  
2762 2764                  return (FALSE);
2763 2765          }
2764 2766          /*
2765 2767           * The result of the "connection accept" is a T_info_ack
2766 2768           * in the call_reply field.
2767 2769           */
2768 2770          ASSERT(e->call_reply != NULL);
2769 2771          mp = e->call_reply;
2770 2772          e->call_reply = NULL;
2771 2773          tinfo = (struct T_info_ack *)mp->b_rptr;
2772 2774  
2773 2775          tidu_size = tinfo->TIDU_size;
2774 2776          tidu_size -= (tidu_size % BYTES_PER_XDR_UNIT);
2775 2777          if (tidu_size > COTS_DEFAULT_ALLOCSIZE || (tidu_size <= 0))
2776 2778                  tidu_size = COTS_DEFAULT_ALLOCSIZE;
2777 2779          *tidu_ptr = tidu_size;
2778 2780  
2779 2781          freemsg(mp);
2780 2782  
2781 2783          /*
2782 2784           * Set up the pertinent options.  NODELAY is so the transport doesn't
2783 2785           * buffer up RPC messages on either end.  This may not be valid for
2784 2786           * all transports.  Failure to set this option is not cause to
2785 2787           * bail out so we return success anyway.  Note that lack of NODELAY
2786 2788           * or some other way to flush the message on both ends will cause
2787 2789           * lots of retries and terrible performance.
2788 2790           */
2789 2791          if (addrfmly == AF_INET || addrfmly == AF_INET6) {
2790 2792                  (void) connmgr_setopt(wq, IPPROTO_TCP, TCP_NODELAY, e, cr);
2791 2793                  if (e->call_status == RPC_XPRTFAILED)
2792 2794                          return (FALSE);
2793 2795          }
2794 2796  
2795 2797          /*
2796 2798           * Since we have a connection, we now need to figure out if
2797 2799           * we need to create a kstat. If x_ksp is not NULL then we
2798 2800           * are reusing a connection and so we do not need to create
2799 2801           * another kstat -- lets just return.
2800 2802           */
2801 2803          if (cm_entry->x_ksp != NULL)
2802 2804                  return (TRUE);
2803 2805  
2804 2806          /*
2805 2807           * We need to increment rpc_kstat_instance atomically to prevent
2806 2808           * two kstats being created with the same instance.
2807 2809           */
2808 2810          kstat_instance = atomic_inc_32_nv((uint32_t *)&rpc_kstat_instance);
2809 2811  
2810 2812          if ((cm_entry->x_ksp = kstat_create_zone("unix", kstat_instance,
2811 2813              "rpc_cots_connections", "rpc", KSTAT_TYPE_NAMED,
2812 2814              (uint_t)(sizeof (cm_kstat_xprt_t) / sizeof (kstat_named_t)),
2813 2815              KSTAT_FLAG_VIRTUAL, cm_entry->x_zoneid)) == NULL) {
2814 2816                  return (TRUE);
2815 2817          }
2816 2818  
2817 2819          cm_entry->x_ksp->ks_lock = &connmgr_lock;
2818 2820          cm_entry->x_ksp->ks_private = cm_entry;
2819 2821          cm_entry->x_ksp->ks_data_size = ((INET6_ADDRSTRLEN * sizeof (char))
2820 2822              + sizeof (cm_kstat_template));
2821 2823          cm_entry->x_ksp->ks_data = kmem_alloc(cm_entry->x_ksp->ks_data_size,
2822 2824              KM_SLEEP);
2823 2825          bcopy(&cm_kstat_template, cm_entry->x_ksp->ks_data,
2824 2826              cm_entry->x_ksp->ks_data_size);
2825 2827          ((struct cm_kstat_xprt *)(cm_entry->x_ksp->ks_data))->
2826 2828              x_server.value.str.addr.ptr =
2827 2829              kmem_alloc(INET6_ADDRSTRLEN, KM_SLEEP);
2828 2830  
2829 2831          cm_entry->x_ksp->ks_update = conn_kstat_update;
2830 2832          kstat_install(cm_entry->x_ksp);
2831 2833          return (TRUE);
2832 2834  }
2833 2835  
2834 2836  /*
2835 2837   * Verify that the specified offset falls within the mblk and
2836 2838   * that the resulting pointer is aligned.
2837 2839   * Returns NULL if not.
2838 2840   *
2839 2841   * code from fs/sockfs/socksubr.c
2840 2842   */
2841 2843  static void *
2842 2844  connmgr_opt_getoff(mblk_t *mp, t_uscalar_t offset,
2843 2845      t_uscalar_t length, uint_t align_size)
2844 2846  {
2845 2847          uintptr_t ptr1, ptr2;
2846 2848  
2847 2849          ASSERT(mp && mp->b_wptr >= mp->b_rptr);
2848 2850          ptr1 = (uintptr_t)mp->b_rptr + offset;
2849 2851          ptr2 = (uintptr_t)ptr1 + length;
2850 2852          if (ptr1 < (uintptr_t)mp->b_rptr || ptr2 > (uintptr_t)mp->b_wptr) {
2851 2853                  return (NULL);
2852 2854          }
2853 2855          if ((ptr1 & (align_size - 1)) != 0) {
2854 2856                  return (NULL);
2855 2857          }
2856 2858          return ((void *)ptr1);
2857 2859  }
2858 2860  
2859 2861  static bool_t
2860 2862  connmgr_getopt_int(queue_t *wq, int level, int name, int *val,
2861 2863      calllist_t *e, cred_t *cr)
2862 2864  {
2863 2865          mblk_t *mp;
2864 2866          struct opthdr *opt, *opt_res;
2865 2867          struct T_optmgmt_req *tor;
2866 2868          struct T_optmgmt_ack *opt_ack;
2867 2869          struct timeval waitp;
2868 2870          int error;
2869 2871  
2870 2872          mp = allocb_cred(sizeof (struct T_optmgmt_req) +
2871 2873              sizeof (struct opthdr) + sizeof (int), cr, NOPID);
2872 2874          if (mp == NULL)
2873 2875                  return (FALSE);
2874 2876  
2875 2877          mp->b_datap->db_type = M_PROTO;
2876 2878          tor = (struct T_optmgmt_req *)(mp->b_rptr);
2877 2879          tor->PRIM_type = T_SVR4_OPTMGMT_REQ;
2878 2880          tor->MGMT_flags = T_CURRENT;
2879 2881          tor->OPT_length = sizeof (struct opthdr) + sizeof (int);
2880 2882          tor->OPT_offset = sizeof (struct T_optmgmt_req);
2881 2883  
2882 2884          opt = (struct opthdr *)(mp->b_rptr + sizeof (struct T_optmgmt_req));
2883 2885          opt->level = level;
2884 2886          opt->name = name;
2885 2887          opt->len = sizeof (int);
2886 2888          mp->b_wptr += sizeof (struct T_optmgmt_req) + sizeof (struct opthdr) +
2887 2889              sizeof (int);
2888 2890  
2889 2891          /*
2890 2892           * We will use this connection regardless
2891 2893           * of whether or not the option is readable.
2892 2894           */
2893 2895          if (clnt_dispatch_send(wq, mp, e, 0, 0) != RPC_SUCCESS) {
2894 2896                  DTRACE_PROBE(krpc__e__connmgr__getopt__cantsend);
2895 2897                  freemsg(mp);
2896 2898                  return (FALSE);
2897 2899          }
2898 2900  
2899 2901          mutex_enter(&clnt_pending_lock);
2900 2902  
2901 2903          waitp.tv_sec = clnt_cots_min_conntout;
2902 2904          waitp.tv_usec = 0;
2903 2905          error = waitforack(e, T_OPTMGMT_ACK, &waitp, 1);
2904 2906  
2905 2907          if (e->call_prev)
2906 2908                  e->call_prev->call_next = e->call_next;
2907 2909          else
2908 2910                  clnt_pending = e->call_next;
2909 2911          if (e->call_next)
2910 2912                  e->call_next->call_prev = e->call_prev;
2911 2913          mutex_exit(&clnt_pending_lock);
2912 2914  
2913 2915          /* get reply message */
2914 2916          mp = e->call_reply;
2915 2917          e->call_reply = NULL;
2916 2918  
2917 2919          if ((!mp) || (e->call_status != RPC_SUCCESS) || (error != 0)) {
2918 2920  
2919 2921                  DTRACE_PROBE4(krpc__e__connmgr_getopt, int, name,
2920 2922                      int, e->call_status, int, error, mblk_t *, mp);
2921 2923  
2922 2924                  if (mp)
2923 2925                          freemsg(mp);
2924 2926                  return (FALSE);
2925 2927          }
2926 2928  
2927 2929          opt_ack = (struct T_optmgmt_ack *)mp->b_rptr;
2928 2930          opt_res = (struct opthdr *)connmgr_opt_getoff(mp, opt_ack->OPT_offset,
2929 2931              opt_ack->OPT_length, __TPI_ALIGN_SIZE);
2930 2932  
2931 2933          if (!opt_res) {
2932 2934                  DTRACE_PROBE4(krpc__e__connmgr_optres, mblk_t *, mp, int, name,
2933 2935                      int, opt_ack->OPT_offset, int, opt_ack->OPT_length);
2934 2936                  freemsg(mp);
2935 2937                  return (FALSE);
2936 2938          }
2937 2939          *val = *(int *)&opt_res[1];
2938 2940  
2939 2941          DTRACE_PROBE2(connmgr_getopt__ok, int, name, int, *val);
2940 2942  
2941 2943          freemsg(mp);
2942 2944          return (TRUE);
2943 2945  }
2944 2946  
2945 2947  /*
2946 2948   * Called by connmgr_connect to set an option on the new stream.
2947 2949   */
2948 2950  static bool_t
2949 2951  connmgr_setopt_int(queue_t *wq, int level, int name, int val,
2950 2952      calllist_t *e, cred_t *cr)
2951 2953  {
2952 2954          mblk_t *mp;
2953 2955          struct opthdr *opt;
2954 2956          struct T_optmgmt_req *tor;
2955 2957          struct timeval waitp;
2956 2958          int error;
2957 2959  
2958 2960          mp = allocb_cred(sizeof (struct T_optmgmt_req) +
2959 2961              sizeof (struct opthdr) + sizeof (int), cr, NOPID);
2960 2962          if (mp == NULL) {
2961 2963                  RPCLOG0(1, "connmgr_setopt: cannot alloc mp for option "
2962 2964                      "request\n");
2963 2965                  return (FALSE);
2964 2966          }
2965 2967  
2966 2968          mp->b_datap->db_type = M_PROTO;
2967 2969          tor = (struct T_optmgmt_req *)(mp->b_rptr);
2968 2970          tor->PRIM_type = T_SVR4_OPTMGMT_REQ;
2969 2971          tor->MGMT_flags = T_NEGOTIATE;
2970 2972          tor->OPT_length = sizeof (struct opthdr) + sizeof (int);
2971 2973          tor->OPT_offset = sizeof (struct T_optmgmt_req);
2972 2974  
2973 2975          opt = (struct opthdr *)(mp->b_rptr + sizeof (struct T_optmgmt_req));
2974 2976          opt->level = level;
2975 2977          opt->name = name;
2976 2978          opt->len = sizeof (int);
2977 2979          *(int *)((char *)opt + sizeof (*opt)) = val;
2978 2980          mp->b_wptr += sizeof (struct T_optmgmt_req) + sizeof (struct opthdr) +
2979 2981              sizeof (int);
2980 2982  
2981 2983          /*
2982 2984           * We will use this connection regardless
2983 2985           * of whether or not the option is settable.
2984 2986           */
2985 2987          if (clnt_dispatch_send(wq, mp, e, 0, 0) != RPC_SUCCESS) {
2986 2988                  DTRACE_PROBE(krpc__e__connmgr__setopt__cantsend);
2987 2989                  freemsg(mp);
2988 2990                  return (FALSE);
2989 2991          }
2990 2992  
2991 2993          mutex_enter(&clnt_pending_lock);
2992 2994  
2993 2995          waitp.tv_sec = clnt_cots_min_conntout;
2994 2996          waitp.tv_usec = 0;
2995 2997          error = waitforack(e, T_OPTMGMT_ACK, &waitp, 1);
2996 2998  
2997 2999          if (e->call_prev)
2998 3000                  e->call_prev->call_next = e->call_next;
2999 3001          else
3000 3002                  clnt_pending = e->call_next;
3001 3003          if (e->call_next)
3002 3004                  e->call_next->call_prev = e->call_prev;
3003 3005          mutex_exit(&clnt_pending_lock);
3004 3006  
3005 3007          if (e->call_reply != NULL) {
3006 3008                  freemsg(e->call_reply);
3007 3009                  e->call_reply = NULL;
3008 3010          }
3009 3011  
3010 3012          if (e->call_status != RPC_SUCCESS || error != 0) {
3011 3013                  RPCLOG(1, "connmgr_setopt: can't set option: %d\n", name);
3012 3014                  return (FALSE);
3013 3015          }
3014 3016          RPCLOG(8, "connmgr_setopt: successfully set option: %d\n", name);
3015 3017          return (TRUE);
3016 3018  }
3017 3019  
3018 3020  static bool_t
3019 3021  connmgr_setopt(queue_t *wq, int level, int name, calllist_t *e, cred_t *cr)
3020 3022  {
3021 3023          return (connmgr_setopt_int(wq, level, name, 1, e, cr));
3022 3024  }
3023 3025  
3024 3026  #ifdef  DEBUG
3025 3027  
3026 3028  /*
3027 3029   * This is a knob to let us force code coverage in allocation failure
3028 3030   * case.
3029 3031   */
3030 3032  static int      connmgr_failsnd;
3031 3033  #define CONN_SND_ALLOC(Size, Pri)       \
3032 3034          ((connmgr_failsnd-- > 0) ? NULL : allocb(Size, Pri))
3033 3035  
3034 3036  #else
3035 3037  
3036 3038  #define CONN_SND_ALLOC(Size, Pri)       allocb(Size, Pri)
3037 3039  
3038 3040  #endif
3039 3041  
3040 3042  /*
3041 3043   * Sends an orderly release on the specified queue.
3042 3044   * Entered with connmgr_lock. Exited without connmgr_lock
3043 3045   */
3044 3046  static void
3045 3047  connmgr_sndrel(struct cm_xprt *cm_entry)
3046 3048  {
3047 3049          struct T_ordrel_req *torr;
3048 3050          mblk_t *mp;
3049 3051          queue_t *q = cm_entry->x_wq;
3050 3052          ASSERT(MUTEX_HELD(&connmgr_lock));
3051 3053          mp = CONN_SND_ALLOC(sizeof (struct T_ordrel_req), BPRI_LO);
3052 3054          if (mp == NULL) {
3053 3055                  cm_entry->x_needrel = TRUE;
3054 3056                  mutex_exit(&connmgr_lock);
3055 3057                  RPCLOG(1, "connmgr_sndrel: cannot alloc mp for sending ordrel "
3056 3058                      "to queue %p\n", (void *)q);
3057 3059                  return;
3058 3060          }
3059 3061          mutex_exit(&connmgr_lock);
3060 3062  
3061 3063          mp->b_datap->db_type = M_PROTO;
3062 3064          torr = (struct T_ordrel_req *)(mp->b_rptr);
3063 3065          torr->PRIM_type = T_ORDREL_REQ;
3064 3066          mp->b_wptr = mp->b_rptr + sizeof (struct T_ordrel_req);
3065 3067  
3066 3068          RPCLOG(8, "connmgr_sndrel: sending ordrel to queue %p\n", (void *)q);
3067 3069          put(q, mp);
3068 3070  }
3069 3071  
3070 3072  /*
3071 3073   * Sends an disconnect on the specified queue.
3072 3074   * Entered with connmgr_lock. Exited without connmgr_lock
3073 3075   */
3074 3076  static void
3075 3077  connmgr_snddis(struct cm_xprt *cm_entry)
3076 3078  {
3077 3079          struct T_discon_req *tdis;
3078 3080          mblk_t *mp;
3079 3081          queue_t *q = cm_entry->x_wq;
3080 3082  
3081 3083          ASSERT(MUTEX_HELD(&connmgr_lock));
3082 3084          mp = CONN_SND_ALLOC(sizeof (*tdis), BPRI_LO);
3083 3085          if (mp == NULL) {
3084 3086                  cm_entry->x_needdis = TRUE;
3085 3087                  mutex_exit(&connmgr_lock);
3086 3088                  RPCLOG(1, "connmgr_snddis: cannot alloc mp for sending discon "
3087 3089                      "to queue %p\n", (void *)q);
3088 3090                  return;
3089 3091          }
3090 3092          mutex_exit(&connmgr_lock);
3091 3093  
3092 3094          mp->b_datap->db_type = M_PROTO;
3093 3095          tdis = (struct T_discon_req *)mp->b_rptr;
3094 3096          tdis->PRIM_type = T_DISCON_REQ;
3095 3097          mp->b_wptr = mp->b_rptr + sizeof (*tdis);
3096 3098  
3097 3099          RPCLOG(8, "connmgr_snddis: sending discon to queue %p\n", (void *)q);
3098 3100          put(q, mp);
3099 3101  }
3100 3102  
3101 3103  /*
3102 3104   * Sets up the entry for receiving replies, and calls rpcmod's write put proc
3103 3105   * (through put) to send the call.
3104 3106   */
3105 3107  static int
3106 3108  clnt_dispatch_send(queue_t *q, mblk_t *mp, calllist_t *e, uint_t xid,
3107 3109      uint_t queue_flag)
3108 3110  {
3109 3111          ASSERT(e != NULL);
3110 3112  
3111 3113          e->call_status = RPC_TIMEDOUT;  /* optimistic, eh? */
3112 3114          e->call_reason = 0;
3113 3115          e->call_wq = q;
3114 3116          e->call_xid = xid;
3115 3117          e->call_notified = FALSE;
3116 3118  
3117 3119          if (!canput(q)) {
3118 3120                  e->call_status = RPC_CANTSEND;
3119 3121                  e->call_reason = ENOBUFS;
3120 3122                  return (RPC_CANTSEND);
3121 3123          }
3122 3124  
3123 3125          /*
3124 3126           * If queue_flag is set then the calllist_t is already on the hash
3125 3127           * queue.  In this case just send the message and return.
3126 3128           */
3127 3129          if (queue_flag) {
3128 3130                  put(q, mp);
3129 3131                  return (RPC_SUCCESS);
3130 3132  
3131 3133          }
3132 3134  
3133 3135          /*
3134 3136           * Set up calls for RPC requests (with XID != 0) on the hash
3135 3137           * queue for fast lookups and place other calls (i.e.
3136 3138           * connection management) on the linked list.
3137 3139           */
3138 3140          if (xid != 0) {
3139 3141                  RPCLOG(64, "clnt_dispatch_send: putting xid 0x%x on "
3140 3142                      "dispatch list\n", xid);
3141 3143                  e->call_hash = call_hash(xid, clnt_cots_hash_size);
3142 3144                  e->call_bucket = &cots_call_ht[e->call_hash];
3143 3145                  call_table_enter(e);
3144 3146          } else {
3145 3147                  mutex_enter(&clnt_pending_lock);
3146 3148                  if (clnt_pending)
3147 3149                          clnt_pending->call_prev = e;
3148 3150                  e->call_next = clnt_pending;
3149 3151                  e->call_prev = NULL;
3150 3152                  clnt_pending = e;
3151 3153                  mutex_exit(&clnt_pending_lock);
3152 3154          }
3153 3155  
3154 3156          put(q, mp);
3155 3157          return (RPC_SUCCESS);
3156 3158  }
3157 3159  
3158 3160  /*
3159 3161   * Called by rpcmod to notify a client with a clnt_pending call that its reply
3160 3162   * has arrived.  If we can't find a client waiting for this reply, we log
3161 3163   * the error and return.
3162 3164   */
3163 3165  bool_t
3164 3166  clnt_dispatch_notify(mblk_t *mp, zoneid_t zoneid)
3165 3167  {
3166 3168          calllist_t *e = NULL;
3167 3169          call_table_t *chtp;
3168 3170          uint32_t xid;
3169 3171          uint_t hash;
3170 3172  
3171 3173          if ((IS_P2ALIGNED(mp->b_rptr, sizeof (uint32_t))) &&
3172 3174              (mp->b_wptr - mp->b_rptr) >= sizeof (xid))
3173 3175                  xid = *((uint32_t *)mp->b_rptr);
3174 3176          else {
3175 3177                  int i = 0;
3176 3178                  unsigned char *p = (unsigned char *)&xid;
3177 3179                  unsigned char *rptr;
3178 3180                  mblk_t *tmp = mp;
3179 3181  
3180 3182                  /*
3181 3183                   * Copy the xid, byte-by-byte into xid.
3182 3184                   */
3183 3185                  while (tmp) {
3184 3186                          rptr = tmp->b_rptr;
3185 3187                          while (rptr < tmp->b_wptr) {
3186 3188                                  *p++ = *rptr++;
3187 3189                                  if (++i >= sizeof (xid))
3188 3190                                          goto done_xid_copy;
3189 3191                          }
3190 3192                          tmp = tmp->b_cont;
3191 3193                  }
3192 3194  
3193 3195                  /*
3194 3196                   * If we got here, we ran out of mblk space before the
3195 3197                   * xid could be copied.
3196 3198                   */
3197 3199                  ASSERT(tmp == NULL && i < sizeof (xid));
3198 3200  
3199 3201                  RPCLOG0(1,
3200 3202                      "clnt_dispatch_notify: message less than size of xid\n");
3201 3203                  return (FALSE);
3202 3204  
3203 3205          }
3204 3206  done_xid_copy:
3205 3207  
3206 3208          hash = call_hash(xid, clnt_cots_hash_size);
3207 3209          chtp = &cots_call_ht[hash];
3208 3210          /* call_table_find returns with the hash bucket locked */
3209 3211          call_table_find(chtp, xid, e);
3210 3212  
3211 3213          if (e != NULL) {
3212 3214                  /*
3213 3215                   * Found thread waiting for this reply
3214 3216                   */
3215 3217                  mutex_enter(&e->call_lock);
3216 3218  
3217 3219                  /*
3218 3220                   * verify that the reply is coming in on
3219 3221                   * the same zone that it was sent from.
3220 3222                   */
3221 3223                  if (e->call_zoneid != zoneid) {
3222 3224                          mutex_exit(&e->call_lock);
3223 3225                          mutex_exit(&chtp->ct_lock);
3224 3226                          RPCLOG0(1, "clnt_dispatch_notify: incorrect zoneid\n");
3225 3227                          return (FALSE);
3226 3228                  }
3227 3229  
3228 3230                  if (e->call_reply)
3229 3231                          /*
3230 3232                           * This can happen under the following scenario:
3231 3233                           * clnt_cots_kcallit() times out on the response,
3232 3234                           * rfscall() repeats the CLNT_CALL() with
3233 3235                           * the same xid, clnt_cots_kcallit() sends the retry,
3234 3236                           * thereby putting the clnt handle on the pending list,
3235 3237                           * the first response arrives, signalling the thread
3236 3238                           * in clnt_cots_kcallit(). Before that thread is
3237 3239                           * dispatched, the second response arrives as well,
3238 3240                           * and clnt_dispatch_notify still finds the handle on
3239 3241                           * the pending list, with call_reply set. So free the
3240 3242                           * old reply now.
3241 3243                           *
3242 3244                           * It is also possible for a response intended for
3243 3245                           * an RPC call with a different xid to reside here.
3244 3246                           * This can happen if the thread that owned this
3245 3247                           * client handle prior to the current owner bailed
3246 3248                           * out and left its call record on the dispatch
3247 3249                           * queue.  A window exists where the response can
3248 3250                           * arrive before the current owner dispatches its
3249 3251                           * RPC call.
3250 3252                           *
3251 3253                           * In any case, this is the very last point where we
3252 3254                           * can safely check the call_reply field before
3253 3255                           * placing the new response there.
3254 3256                           */
3255 3257                          freemsg(e->call_reply);
3256 3258                  e->call_reply = mp;
3257 3259                  e->call_status = RPC_SUCCESS;
3258 3260                  e->call_notified = TRUE;
3259 3261                  cv_signal(&e->call_cv);
3260 3262                  mutex_exit(&e->call_lock);
3261 3263                  mutex_exit(&chtp->ct_lock);
3262 3264                  return (TRUE);
3263 3265          } else {
3264 3266                  zone_t *zone;
3265 3267                  struct rpcstat *rpcstat;
3266 3268  
3267 3269                  mutex_exit(&chtp->ct_lock);
3268 3270                  RPCLOG(65, "clnt_dispatch_notify: no caller for reply 0x%x\n",
3269 3271                      xid);
3270 3272                  /*
3271 3273                   * This is unfortunate, but we need to lookup the zone so we
3272 3274                   * can increment its "rcbadxids" counter.
3273 3275                   */
3274 3276                  zone = zone_find_by_id(zoneid);
3275 3277                  if (zone == NULL) {
3276 3278                          /*
3277 3279                           * The zone went away...
3278 3280                           */
3279 3281                          return (FALSE);
3280 3282                  }
3281 3283                  rpcstat = zone_getspecific(rpcstat_zone_key, zone);
3282 3284                  if (zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN) {
3283 3285                          /*
3284 3286                           * Not interested
3285 3287                           */
3286 3288                          zone_rele(zone);
3287 3289                          return (FALSE);
3288 3290                  }
3289 3291                  COTSRCSTAT_INCR(rpcstat->rpc_cots_client, rcbadxids);
3290 3292                  zone_rele(zone);
3291 3293          }
3292 3294          return (FALSE);
3293 3295  }
3294 3296  
3295 3297  /*
3296 3298   * Called by rpcmod when a non-data indication arrives.  The ones in which we
3297 3299   * are interested are connection indications and options acks.  We dispatch
3298 3300   * based on the queue the indication came in on.  If we are not interested in
3299 3301   * what came in, we return false to rpcmod, who will then pass it upstream.
3300 3302   */
3301 3303  bool_t
3302 3304  clnt_dispatch_notifyconn(queue_t *q, mblk_t *mp)
3303 3305  {
3304 3306          calllist_t *e;
3305 3307          int type;
3306 3308  
3307 3309          ASSERT((q->q_flag & QREADR) == 0);
3308 3310  
3309 3311          type = ((union T_primitives *)mp->b_rptr)->type;
3310 3312          RPCLOG(8, "clnt_dispatch_notifyconn: prim type: [%s]\n",
3311 3313              rpc_tpiprim2name(type));
3312 3314          mutex_enter(&clnt_pending_lock);
3313 3315          for (e = clnt_pending; /* NO CONDITION */; e = e->call_next) {
3314 3316                  if (e == NULL) {
3315 3317                          mutex_exit(&clnt_pending_lock);
3316 3318                          RPCLOG(1, "clnt_dispatch_notifyconn: no one waiting "
3317 3319                              "for connection on queue 0x%p\n", (void *)q);
3318 3320                          return (FALSE);
3319 3321                  }
3320 3322                  if (e->call_wq == q)
3321 3323                          break;
3322 3324          }
3323 3325  
3324 3326          switch (type) {
3325 3327          case T_CONN_CON:
3326 3328                  /*
3327 3329                   * The transport is now connected, send a T_INFO_REQ to get
3328 3330                   * the tidu size.
3329 3331                   */
3330 3332                  mutex_exit(&clnt_pending_lock);
3331 3333                  ASSERT(mp->b_datap->db_lim - mp->b_datap->db_base >=
3332 3334                      sizeof (struct T_info_req));
3333 3335                  mp->b_rptr = mp->b_datap->db_base;
3334 3336                  ((union T_primitives *)mp->b_rptr)->type = T_INFO_REQ;
3335 3337                  mp->b_wptr = mp->b_rptr + sizeof (struct T_info_req);
3336 3338                  mp->b_datap->db_type = M_PCPROTO;
3337 3339                  put(q, mp);
3338 3340                  return (TRUE);
3339 3341          case T_INFO_ACK:
3340 3342          case T_OPTMGMT_ACK:
3341 3343                  e->call_status = RPC_SUCCESS;
3342 3344                  e->call_reply = mp;
3343 3345                  e->call_notified = TRUE;
3344 3346                  cv_signal(&e->call_cv);
3345 3347                  break;
3346 3348          case T_ERROR_ACK:
3347 3349                  e->call_status = RPC_CANTCONNECT;
3348 3350                  e->call_reply = mp;
3349 3351                  e->call_notified = TRUE;
3350 3352                  cv_signal(&e->call_cv);
3351 3353                  break;
3352 3354          case T_OK_ACK:
3353 3355                  /*
3354 3356                   * Great, but we are really waiting for a T_CONN_CON
3355 3357                   */
3356 3358                  freemsg(mp);
3357 3359                  break;
3358 3360          default:
3359 3361                  mutex_exit(&clnt_pending_lock);
3360 3362                  RPCLOG(1, "clnt_dispatch_notifyconn: bad type %d\n", type);
3361 3363                  return (FALSE);
3362 3364          }
3363 3365  
3364 3366          mutex_exit(&clnt_pending_lock);
3365 3367          return (TRUE);
3366 3368  }
3367 3369  
3368 3370  /*
3369 3371   * Called by rpcmod when the transport is (or should be) going away.  Informs
3370 3372   * all callers waiting for replies and marks the entry in the connection
3371 3373   * manager's list as unconnected, and either closing (close handshake in
3372 3374   * progress) or dead.
3373 3375   */
3374 3376  void
3375 3377  clnt_dispatch_notifyall(queue_t *q, int32_t msg_type, int32_t reason)
3376 3378  {
3377 3379          calllist_t *e;
3378 3380          call_table_t *ctp;
3379 3381          struct cm_xprt *cm_entry;
3380 3382          int have_connmgr_lock;
3381 3383          int i;
3382 3384  
3383 3385          ASSERT((q->q_flag & QREADR) == 0);
3384 3386  
3385 3387          RPCLOG(1, "clnt_dispatch_notifyall on queue %p", (void *)q);
3386 3388          RPCLOG(1, " received a notifcation prim type [%s]",
3387 3389              rpc_tpiprim2name(msg_type));
3388 3390          RPCLOG(1, " and reason %d\n", reason);
3389 3391  
3390 3392          /*
3391 3393           * Find the transport entry in the connection manager's list, close
3392 3394           * the transport and delete the entry.  In the case where rpcmod's
3393 3395           * idle timer goes off, it sends us a T_ORDREL_REQ, indicating we
3394 3396           * should gracefully close the connection.
3395 3397           */
3396 3398          have_connmgr_lock = 1;
3397 3399          mutex_enter(&connmgr_lock);
3398 3400          for (cm_entry = cm_hd; cm_entry; cm_entry = cm_entry->x_next) {
3399 3401                  ASSERT(cm_entry != cm_entry->x_next);
3400 3402                  if (cm_entry->x_wq == q) {
3401 3403                          ASSERT(MUTEX_HELD(&connmgr_lock));
3402 3404                          ASSERT(have_connmgr_lock == 1);
3403 3405                          switch (msg_type) {
3404 3406                          case T_ORDREL_REQ:
3405 3407  
3406 3408                                  if (cm_entry->x_dead) {
3407 3409                                          RPCLOG(1, "idle timeout on dead "
3408 3410                                              "connection: %p\n",
3409 3411                                              (void *)cm_entry);
3410 3412                                          if (clnt_stop_idle != NULL)
3411 3413                                                  (*clnt_stop_idle)(q);
3412 3414                                          break;
3413 3415                                  }
3414 3416  
3415 3417                                  /*
3416 3418                                   * Only mark the connection as dead if it is
3417 3419                                   * connected and idle.
3418 3420                                   * An unconnected connection has probably
3419 3421                                   * gone idle because the server is down,
3420 3422                                   * and when it comes back up there will be
3421 3423                                   * retries that need to use that connection.
3422 3424                                   */
3423 3425                                  if (cm_entry->x_connected ||
3424 3426                                      cm_entry->x_doomed) {
3425 3427                                          if (cm_entry->x_ordrel) {
3426 3428                                                  if (cm_entry->x_closing ==
3427 3429                                                      TRUE) {
3428 3430                                                          /*
3429 3431                                                           * The connection is
3430 3432                                                           * obviously wedged due
3431 3433                                                           * to a bug or problem
3432 3434                                                           * with the transport.
3433 3435                                                           * Mark it as dead.
3434 3436                                                           * Otherwise we can
3435 3437                                                           * leak connections.
3436 3438                                                           */
3437 3439                                                          cm_entry->x_dead = TRUE;
3438 3440                                                          mutex_exit(
3439 3441                                                              &connmgr_lock);
3440 3442                                                          have_connmgr_lock = 0;
3441 3443                                                          if (clnt_stop_idle !=
3442 3444                                                              NULL)
3443 3445                                                          (*clnt_stop_idle)(q);
3444 3446                                                          break;
3445 3447                                                  }
3446 3448                                                  cm_entry->x_closing = TRUE;
3447 3449                                                  connmgr_sndrel(cm_entry);
3448 3450                                                  have_connmgr_lock = 0;
3449 3451                                          } else {
3450 3452                                                  cm_entry->x_dead = TRUE;
3451 3453                                                  mutex_exit(&connmgr_lock);
3452 3454                                                  have_connmgr_lock = 0;
3453 3455                                                  if (clnt_stop_idle != NULL)
3454 3456                                                          (*clnt_stop_idle)(q);
3455 3457                                          }
3456 3458                                  } else {
3457 3459                                          /*
3458 3460                                           * We don't mark the connection
3459 3461                                           * as dead, but we turn off the
3460 3462                                           * idle timer.
3461 3463                                           */
3462 3464                                          mutex_exit(&connmgr_lock);
3463 3465                                          have_connmgr_lock = 0;
3464 3466                                          if (clnt_stop_idle != NULL)
3465 3467                                                  (*clnt_stop_idle)(q);
3466 3468                                          RPCLOG(1, "clnt_dispatch_notifyall:"
3467 3469                                              " ignoring timeout from rpcmod"
3468 3470                                              " (q %p) because we are not "
3469 3471                                              " connected\n", (void *)q);
3470 3472                                  }
3471 3473                                  break;
3472 3474                          case T_ORDREL_IND:
3473 3475                                  /*
3474 3476                                   * If this entry is marked closing, then we are
3475 3477                                   * completing a close handshake, and the
3476 3478                                   * connection is dead.  Otherwise, the server is
3477 3479                                   * trying to close. Since the server will not
3478 3480                                   * be sending any more RPC replies, we abort
3479 3481                                   * the connection, including flushing
3480 3482                                   * any RPC requests that are in-transit.
3481 3483                                   * In either case, mark the entry as dead so
3482 3484                                   * that it can be closed by the connection
3483 3485                                   * manager's garbage collector.
3484 3486                                   */
3485 3487                                  cm_entry->x_dead = TRUE;
3486 3488                                  if (cm_entry->x_closing) {
3487 3489                                          mutex_exit(&connmgr_lock);
3488 3490                                          have_connmgr_lock = 0;
3489 3491                                          if (clnt_stop_idle != NULL)
3490 3492                                                  (*clnt_stop_idle)(q);
3491 3493                                  } else {
3492 3494                                          /*
3493 3495                                           * if we're getting a disconnect
3494 3496                                           * before we've finished our
3495 3497                                           * connect attempt, mark it for
3496 3498                                           * later processing
3497 3499                                           */
3498 3500                                          if (cm_entry->x_thread)
3499 3501                                                  cm_entry->x_early_disc = TRUE;
3500 3502                                          else
3501 3503                                                  cm_entry->x_connected = FALSE;
3502 3504                                          cm_entry->x_waitdis = TRUE;
3503 3505                                          connmgr_snddis(cm_entry);
3504 3506                                          have_connmgr_lock = 0;
3505 3507                                  }
3506 3508                                  break;
3507 3509  
3508 3510                          case T_ERROR_ACK:
3509 3511                          case T_OK_ACK:
3510 3512                                  cm_entry->x_waitdis = FALSE;
3511 3513                                  cv_signal(&cm_entry->x_dis_cv);
3512 3514                                  mutex_exit(&connmgr_lock);
3513 3515                                  return;
3514 3516  
3515 3517                          case T_DISCON_REQ:
3516 3518                                  if (cm_entry->x_thread)
3517 3519                                          cm_entry->x_early_disc = TRUE;
3518 3520                                  else
3519 3521                                          cm_entry->x_connected = FALSE;
3520 3522                                  cm_entry->x_waitdis = TRUE;
3521 3523  
3522 3524                                  connmgr_snddis(cm_entry);
3523 3525                                  have_connmgr_lock = 0;
3524 3526                                  break;
3525 3527  
3526 3528                          case T_DISCON_IND:
3527 3529                          default:
3528 3530                                  /*
3529 3531                                   * if we're getting a disconnect before
3530 3532                                   * we've finished our connect attempt,
3531 3533                                   * mark it for later processing
3532 3534                                   */
3533 3535                                  if (cm_entry->x_closing) {
3534 3536                                          cm_entry->x_dead = TRUE;
3535 3537                                          mutex_exit(&connmgr_lock);
3536 3538                                          have_connmgr_lock = 0;
3537 3539                                          if (clnt_stop_idle != NULL)
3538 3540                                                  (*clnt_stop_idle)(q);
3539 3541                                  } else {
3540 3542                                          if (cm_entry->x_thread) {
3541 3543                                                  cm_entry->x_early_disc = TRUE;
3542 3544                                          } else {
3543 3545                                                  cm_entry->x_dead = TRUE;
3544 3546                                                  cm_entry->x_connected = FALSE;
3545 3547                                          }
3546 3548                                  }
3547 3549                                  break;
3548 3550                          }
3549 3551                          break;
3550 3552                  }
3551 3553          }
3552 3554  
3553 3555          if (have_connmgr_lock)
3554 3556                  mutex_exit(&connmgr_lock);
3555 3557  
3556 3558          if (msg_type == T_ERROR_ACK || msg_type == T_OK_ACK) {
3557 3559                  RPCLOG(1, "clnt_dispatch_notifyall: (wq %p) could not find "
3558 3560                      "connmgr entry for discon ack\n", (void *)q);
3559 3561                  return;
3560 3562          }
3561 3563  
3562 3564          /*
3563 3565           * Then kick all the clnt_pending calls out of their wait.  There
3564 3566           * should be no clnt_pending calls in the case of rpcmod's idle
3565 3567           * timer firing.
3566 3568           */
3567 3569          for (i = 0; i < clnt_cots_hash_size; i++) {
3568 3570                  ctp = &cots_call_ht[i];
3569 3571                  mutex_enter(&ctp->ct_lock);
3570 3572                  for (e = ctp->ct_call_next;
3571 3573                      e != (calllist_t *)ctp;
3572 3574                      e = e->call_next) {
3573 3575                          if (e->call_wq == q && e->call_notified == FALSE) {
3574 3576                                  RPCLOG(1,
3575 3577                                      "clnt_dispatch_notifyall for queue %p ",
3576 3578                                      (void *)q);
3577 3579                                  RPCLOG(1, "aborting clnt_pending call %p\n",
3578 3580                                      (void *)e);
3579 3581  
3580 3582                                  if (msg_type == T_DISCON_IND)
3581 3583                                          e->call_reason = reason;
3582 3584                                  e->call_notified = TRUE;
3583 3585                                  e->call_status = RPC_XPRTFAILED;
3584 3586                                  cv_signal(&e->call_cv);
3585 3587                          }
3586 3588                  }
3587 3589                  mutex_exit(&ctp->ct_lock);
3588 3590          }
3589 3591  
3590 3592          mutex_enter(&clnt_pending_lock);
3591 3593          for (e = clnt_pending; e; e = e->call_next) {
3592 3594                  /*
3593 3595                   * Only signal those RPC handles that haven't been
3594 3596                   * signalled yet. Otherwise we can get a bogus call_reason.
3595 3597                   * This can happen if thread A is making a call over a
3596 3598                   * connection. If the server is killed, it will cause
3597 3599                   * reset, and reason will default to EIO as a result of
3598 3600                   * a T_ORDREL_IND. Thread B then attempts to recreate
3599 3601                   * the connection but gets a T_DISCON_IND. If we set the
3600 3602                   * call_reason code for all threads, then if thread A
3601 3603                   * hasn't been dispatched yet, it will get the wrong
3602 3604                   * reason. The bogus call_reason can make it harder to
3603 3605                   * discriminate between calls that fail because the
3604 3606                   * connection attempt failed versus those where the call
3605 3607                   * may have been executed on the server.
3606 3608                   */
3607 3609                  if (e->call_wq == q && e->call_notified == FALSE) {
3608 3610                          RPCLOG(1, "clnt_dispatch_notifyall for queue %p ",
3609 3611                              (void *)q);
3610 3612                          RPCLOG(1, " aborting clnt_pending call %p\n",
3611 3613                              (void *)e);
3612 3614  
3613 3615                          if (msg_type == T_DISCON_IND)
3614 3616                                  e->call_reason = reason;
3615 3617                          e->call_notified = TRUE;
3616 3618                          /*
3617 3619                           * Let the caller timeout, else it will retry
3618 3620                           * immediately.
3619 3621                           */
3620 3622                          e->call_status = RPC_XPRTFAILED;
3621 3623  
3622 3624                          /*
3623 3625                           * We used to just signal those threads
3624 3626                           * waiting for a connection, (call_xid = 0).
3625 3627                           * That meant that threads waiting for a response
3626 3628                           * waited till their timeout expired. This
3627 3629                           * could be a long time if they've specified a
3628 3630                           * maximum timeout. (2^31 - 1). So we
3629 3631                           * Signal all threads now.
3630 3632                           */
3631 3633                          cv_signal(&e->call_cv);
3632 3634                  }
3633 3635          }
3634 3636          mutex_exit(&clnt_pending_lock);
3635 3637  }
3636 3638  
3637 3639  
3638 3640  /*ARGSUSED*/
3639 3641  /*
3640 3642   * after resuming a system that's been suspended for longer than the
3641 3643   * NFS server's idle timeout (svc_idle_timeout for Solaris 2), rfscall()
3642 3644   * generates "NFS server X not responding" and "NFS server X ok" messages;
3643 3645   * here we reset inet connections to cause a re-connect and avoid those
3644 3646   * NFS messages.  see 4045054
3645 3647   */
3646 3648  boolean_t
3647 3649  connmgr_cpr_reset(void *arg, int code)
3648 3650  {
3649 3651          struct cm_xprt *cxp;
3650 3652  
3651 3653          if (code == CB_CODE_CPR_CHKPT)
3652 3654                  return (B_TRUE);
3653 3655  
3654 3656          if (mutex_tryenter(&connmgr_lock) == 0)
3655 3657                  return (B_FALSE);
3656 3658          for (cxp = cm_hd; cxp; cxp = cxp->x_next) {
3657 3659                  if ((cxp->x_family == AF_INET || cxp->x_family == AF_INET6) &&
3658 3660                      cxp->x_connected == TRUE) {
3659 3661                          if (cxp->x_thread)
3660 3662                                  cxp->x_early_disc = TRUE;
3661 3663                          else
3662 3664                                  cxp->x_connected = FALSE;
3663 3665                          cxp->x_needdis = TRUE;
3664 3666                  }
3665 3667          }
3666 3668          mutex_exit(&connmgr_lock);
3667 3669          return (B_TRUE);
3668 3670  }
3669 3671  
3670 3672  void
3671 3673  clnt_cots_stats_init(zoneid_t zoneid, struct rpc_cots_client **statsp)
3672 3674  {
3673 3675  
3674 3676          *statsp = (struct rpc_cots_client *)rpcstat_zone_init_common(zoneid,
3675 3677              "unix", "rpc_cots_client", (const kstat_named_t *)&cots_rcstat_tmpl,
3676 3678              sizeof (cots_rcstat_tmpl));
3677 3679  }
3678 3680  
3679 3681  void
3680 3682  clnt_cots_stats_fini(zoneid_t zoneid, struct rpc_cots_client **statsp)
3681 3683  {
3682 3684          rpcstat_zone_fini_common(zoneid, "unix", "rpc_cots_client");
3683 3685          kmem_free(*statsp, sizeof (cots_rcstat_tmpl));
3684 3686  }
3685 3687  
3686 3688  void
3687 3689  clnt_cots_init(void)
3688 3690  {
3689 3691          mutex_init(&connmgr_lock, NULL, MUTEX_DEFAULT, NULL);
3690 3692          mutex_init(&clnt_pending_lock, NULL, MUTEX_DEFAULT, NULL);
3691 3693  
3692 3694          if (clnt_cots_hash_size < DEFAULT_MIN_HASH_SIZE)
3693 3695                  clnt_cots_hash_size = DEFAULT_MIN_HASH_SIZE;
3694 3696  
3695 3697          cots_call_ht = call_table_init(clnt_cots_hash_size);
3696 3698          zone_key_create(&zone_cots_key, NULL, NULL, clnt_zone_destroy);
3697 3699  }
3698 3700  
3699 3701  void
3700 3702  clnt_cots_fini(void)
3701 3703  {
3702 3704          (void) zone_key_delete(zone_cots_key);
3703 3705  }
3704 3706  
3705 3707  /*
3706 3708   * Wait for TPI ack, returns success only if expected ack is received
3707 3709   * within timeout period.
3708 3710   */
3709 3711  
3710 3712  static int
3711 3713  waitforack(calllist_t *e, t_scalar_t ack_prim, const struct timeval *waitp,
3712 3714      bool_t nosignal)
3713 3715  {
3714 3716          union T_primitives *tpr;
3715 3717          clock_t timout;
3716 3718          int cv_stat = 1;
3717 3719  
3718 3720          ASSERT(MUTEX_HELD(&clnt_pending_lock));
3719 3721          while (e->call_reply == NULL) {
3720 3722                  if (waitp != NULL) {
3721 3723                          timout = waitp->tv_sec * drv_usectohz(MICROSEC) +
3722 3724                              drv_usectohz(waitp->tv_usec);
3723 3725                          if (nosignal)
3724 3726                                  cv_stat = cv_reltimedwait(&e->call_cv,
3725 3727                                      &clnt_pending_lock, timout, TR_CLOCK_TICK);
3726 3728                          else
3727 3729                                  cv_stat = cv_reltimedwait_sig(&e->call_cv,
3728 3730                                      &clnt_pending_lock, timout, TR_CLOCK_TICK);
3729 3731                  } else {
3730 3732                          if (nosignal)
3731 3733                                  cv_wait(&e->call_cv, &clnt_pending_lock);
3732 3734                          else
3733 3735                                  cv_stat = cv_wait_sig(&e->call_cv,
3734 3736                                      &clnt_pending_lock);
3735 3737                  }
3736 3738                  if (cv_stat == -1)
3737 3739                          return (ETIME);
3738 3740                  if (cv_stat == 0)
3739 3741                          return (EINTR);
3740 3742                  /*
3741 3743                   * if we received an error from the server and we know a reply
3742 3744                   * is not going to be sent, do not wait for the full timeout,
3743 3745                   * return now.
3744 3746                   */
3745 3747                  if (e->call_status == RPC_XPRTFAILED)
3746 3748                          return (e->call_reason);
3747 3749          }
3748 3750          tpr = (union T_primitives *)e->call_reply->b_rptr;
3749 3751          if (tpr->type == ack_prim)
3750 3752                  return (0); /* Success */
3751 3753  
3752 3754          if (tpr->type == T_ERROR_ACK) {
3753 3755                  if (tpr->error_ack.TLI_error == TSYSERR)
3754 3756                          return (tpr->error_ack.UNIX_error);
3755 3757                  else
3756 3758                          return (t_tlitosyserr(tpr->error_ack.TLI_error));
3757 3759          }
3758 3760  
3759 3761          return (EPROTO); /* unknown or unexpected primitive */
3760 3762  }
  
    | 
      ↓ open down ↓ | 
    1350 lines elided | 
    
      ↑ open up ↑ | 
  
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX