\section{Remote Procedure Call}

% -----------------------------------------------------------------
\begin{slide}{}

   \subsection{Overview}

   \pichere{rpc_timeline.eps,height=4in}

\end{slide}
% -----------------------------------------------------------------
\begin{slide}{}

   \pichere{rpc_stub.eps,height=3.5in}

   RPC protocol consists of three basic functions
   \begin{sitem}
      \item BLAST: fragments and reassembles large messages
      \item CHAN: synchronizes request and reply messages
      \item SELECT: dispatches request messages to the correct process
   \end{sitem}

   We consider RPC stubs later.

\end{slide}
% -----------------------------------------------------------------
\begin{slide}{}

   \subsection{Bulk Transfer (BLAST)}

   Unlike AAL and IP in that it tries to recover from lost fragments;
   persistent, but does not guarantee delivery. Strategy is to use
   {\it selective retransmission} ({\it partial acknowledgements}).

   \pichere{blast_timeline.eps,height=5in}

\end{slide}
% -----------------------------------------------------------------
\begin{slide}{}

   Sender:
   \begin{sitem}
      \item After sending all fragments, set timer DONE
      \item If receive SRR, send missing fragments and reset DONE
      \item If timer DONE expires, free fragments
   \end{sitem}

   Receiver:
   \begin{sitem}
      \item When first fragment arrives, set timer LAST\_FRAG
      \item When all fragments present, reassemble and pass up
      \item Four exceptional conditions
      \begin{sitem}
         \item if last fragment arrives but message not complete
         \begin{sitem}
            \item send SRR and set timer RETRY
         \end{sitem}
         \item if timer LAST\_FRAG expires
         \begin{sitem}
            \item send SRR and set timer RETRY
         \end{sitem}
         \item if timer RETRY expires for first or second time
         \begin{sitem}
            \item send SRR and set timer RETRY
         \end{sitem}
         \item if timer RETRY expires for third time
         \begin{sitem}
            \item give up and free partial message
         \end{sitem}
      \end{sitem}
   \end{sitem}
\end{slide}
% -----------------------------------------------------------------
\begin{slide}{}   

   BLAST Header Format

   \pichere{blast_format.eps,height=3in}

   \begin{sitem}
      \item {\tt MID} must protect against wrap around
      \item {\tt Type} = {\tt DATA} or {\tt SRR}
      \item {\tt NumFrags} indicates number of fragments in message
      \item {\tt FragMask} distinguishes among fragments:
      \begin{sitem}
         \item if {\tt Type=DATA}, identifies this fragment
         \item if {\tt Type=SRR}, identifies missing fragments
      \end{sitem}
   \end{sitem}
\end{slide}
% -----------------------------------------------------------------
\begin{slide}{}   

   \subsection{Request/Reply (CHAN)}

   Guarantees message delivery, and synchronizes client with server;
   i.e., blocks client until reply received. Supports {\it at-most-once}
   semantics.

   Simple case:
   \pichere{chan_timeline1.eps,height=2.5in}

   Implicit Acknowledgements:
   \pichere{chan_timeline2.eps,height=2.5in}
\end{slide}
% -----------------------------------------------------------------
\begin{slide}{}   

   Complications:
   \begin{sitem}
      \item Lost message (request, reply, or ACK)
      \begin{sitem}
         \item set RETRANSMIT timer
         \item use message id (MID) field to distinguish
      \end{sitem}
      \item Slow (long running) server
      \begin{sitem}
         \item client periodically sends ``are you alive'' probe, or
         \item server periodically sends ``I'm alive'' notice
      \end{sitem}
      \item Want to support multiple outstanding calls
      \begin{sitem}
         \item use channel id (CID) field to distinguish
      \end{sitem}
      \item Machines crash and reboot
      \begin{sitem}
         \item use boot id (BID) field to distinguish
      \end{sitem}      
   \end{sitem}
\end{slide}
% -----------------------------------------------------------------
\begin{slide}{}   

   CHAN Header Format

   \begin{verbatim}
   typedef struct {
       u_short  Type;    /* REQ, REP, ACK, PROBE */
       u_short  CID;     /* unique channel id */
       int      MID;     /* unique message id */
       int      BID;     /* unique boot id */
       int      Length;  /* length of message */
       int      ProtNum; /* high-level protocol */
   } ChanHdr;
   \end{verbatim}

   CHAN Session State

   \begin{verbatim}
   typedef struct {
       u_char    type;         /* CLIENT or SERVER */
       u_char    status;       /* BUSY or IDLE */
       int       retries;      /* number of retries */
       int       timeout;      /* timeout value */
       XkReturn  ret_val;      /* return value */
       Msg       *request;     /* request message */
       Msg       *reply;       /* reply message */
       Semaphore reply_sem;    /* client semaphore */
       int       mid;          /* message id */
       int       bid;          /* boot id */
   } ChanState;
   \end{verbatim}
\end{slide}
% -----------------------------------------------------------------
\begin{slide}{}   

   \subsection{Synchronous versus Asynchronous Protocols}

   Asynchronous Interface
   
   \begin{verbatim}
      xPush(Sessn s, Msg *msg)
      xPop(Sessn s, Msg *msg, void *hdr)
      xDemux(Protl hlp, Sessn s, Msg *msg)
   \end{verbatim}

   Synchronous Interface
   
   \begin{verbatim}
      xCall(Sessn s, Msg *req, Msg *rep)
      xCallPop(Sessn s, Msg *req, Msg *rep,
               void *hdr)
      xCallDemux(Protl hlp, Sessn s, Msg *req,
                 Msg *rep)
   \end{verbatim}

   CHAN is a Hybrid Protocol

   \begin{sitem}
      \item Synchronous from above: {\tt xCall}
      \item Asynchronous from below: {\tt xPop/xDemux}
   \end{sitem}
\end{slide}
% -----------------------------------------------------------------
\begin{slide}{}   

   \subsection{Dispatcher (SELECT)}

   Dispatches request messages to the appropriate procedure; fully
   synchronous counterpart to UDP.

   \pichere{select_blk.eps,height=3in}

   Address Space for Procedures
   
   \begin{sitem}
      \item Flat: unique id for each possible procedure
      \item Hierarchical: program + procedure within program
   \end{sitem}
\end{slide}
% -----------------------------------------------------------------
\begin{slide}{}   

   \subsection{Example Code}

   Client Side

   \begin{verbatim}
   static XkReturn 
   selectCall(Sessn self, Msg *req, Msg *rep)
   {
       SelectState *state=(SelectState *)self->state;
       char        *buf;
  
       buf = msgPush(req, HLEN);
       select_hdr_store(state->hdr, buf, HLEN);
       return xCall(xGetDown(self, 0), req, rep);
   }
   \end{verbatim}

   Server side

   \begin{verbatim}
   static XkReturn 
   selectCallPop(Sessn s, Sessn lls, Msg *req, 
                 Msg *rep, void *inHdr)
   {
       return xCallDemux(xGetUp(s), s, req, rep);
   }
   \end{verbatim}
\end{slide}
% -----------------------------------------------------------------
\begin{slide}{}   

   \subsection{Putting it All Together}

    Simple RPC Stack

   \pichere{rpc_stack1.eps,height=4in}
\end{slide}
% -----------------------------------------------------------------
\begin{slide}{}   

   A More Interesting RPC Stack

   \pichere{rpc_stack2.eps,height=7in}

\end{slide}
% -----------------------------------------------------------------
\begin{slide}{}   

   VCHAN: A Virtual Protocol

   \begin{verbatim}
   static XkReturn
   vchanCall(Sessn s, Msg *req, Msg *rep)
   {
       Sessn      chan;
       XkReturn   result;
       VchanState *state=(VchanState *)s->state;
 
       /* wait for an idle channel */
       semWait(&state->available);
       chan = state->stack[--state->tos];

       /* use the channel */
       result = xCall(chan, req, rep);

       /* free the channel */
       state->stack[state->tos++] = chan;
       semSignal(&state->available);

       return result;
   }
   \end{verbatim}
\end{slide}
% -----------------------------------------------------------------
\begin{slide}{}   

   SunRPC

   \pichere{sunrpc.eps,height=4in}

   \begin{sitem}
      \item IP implements BLAST-equivalent
      \begin{sitem}
         \item except no selective retransmit
      \end{sitem}
      \item SunRPC implements CHAN-equivalent
      \begin{sitem}
         \item except not at-most-once
      \end{sitem}
      \item UDP + SunRPC implement SELECT-equivalent
      \begin{sitem}
         \item UDP dispatches to program (ports bound to programs)
         \item SunRPC dispatches to procedure w/in program
      \end{sitem}
   \end{sitem}
\end{slide}
% -----------------------------------------------------------------
\begin{slide}{}   

   SunRPC Header Format

   \pichere{sunrpc_format.eps,height=3.5in}

   \begin{sitem}
      \item {\tt XID} (transaction id) is similar to CHAN's {\tt MID}
      \item Server does not remember last {\tt XID} it serviced
      \item Problem if client retransmits request while reply is in
   \end{sitem}
\end{slide}

