Blame src/ccapi/doc/CCAPI-Windows-Design.html

Packit fd8b60
Packit fd8b60
<html xmlns="https://www.w3.org/1999/xhtml">
Packit fd8b60
<head>
Packit fd8b60
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
Packit fd8b60
<title>Windows CCAPI RPC design</title>
Packit fd8b60
<style type="text/css">
Packit fd8b60
Packit fd8b60
.style2 {color: 0}
Packit fd8b60
.style3 {font-family: "Courier New", Courier, monospace}
Packit fd8b60
.style5 {color: #CC3300}
Packit fd8b60
.style6 {color: #999999}
Packit fd8b60
.style7 {color: #000099}
Packit fd8b60
-->
Packit fd8b60
</style>
Packit fd8b60
</head>
Packit fd8b60
Packit fd8b60
<body>
Packit fd8b60

Proposed RPC design for Windows CCAPI clients and server

Packit fd8b60

The proposal is for a single user; the solution is replicated for each user logged onto the PC.

Packit fd8b60

Conventions & clarifications

Packit fd8b60

"Client" and "server" refer to the CCAPI client and server.

Packit fd8b60

The CCAPI client acts as both an RPC client and RPC server and the CCAPI server acts as both an RPC client and RPC server.

Packit fd8b60
    Packit fd8b60
      
  • The RPC call from the CCAPI client to the CCAPI server is called the "request." In this mode, the CCAPI client is the RPC client and the CCAPI server is the RPC server.
  • Packit fd8b60
      
  • The RPC call from the CCAPI server to the CCAPI client is called the "reply." In this mode, the CCAPI client is the RPC server and the CCAPI server is the RPC client.
  • Packit fd8b60
    Packit fd8b60

    The Windows username is referred to below as "<USER>."

    Packit fd8b60

    The Windows Logon Security Identifier is referred to as "<LSID>."

    Packit fd8b60

    <UUID> means a thread-specific UUID.

    Packit fd8b60

    <SST> means server start time, a time_t.

    Packit fd8b60

    A description of client and server authentication has not been added yet.

    Packit fd8b60

    Design Requirements

    Packit fd8b60
      Packit fd8b60
        
    • The server's OS-independent code is single threaded, because it must operate on platforms that do not allow multiple threads.
    • Packit fd8b60
        
    • The client and server must be able to maintain connections, where state is maintained between individual messages.
    • Packit fd8b60
        
    • Individual messages must be handled in a single threaded server.
    • Packit fd8b60
        
    • The server must be able to detect when a client dies, so that any connection state can be cleaned up.
    • Packit fd8b60
      Packit fd8b60

      Design

      Packit fd8b60

      The server and each client create an RPC endpoint. The server's endpoint is CCS_<LSID> and the client's endpoint is CCAPI_<UUID>, where each client geta a UUID.

      Packit fd8b60

      On Windows, the server's ccs_pipe_t type is a char* and is set to the client UUID.

      Packit fd8b60

      How is the request handled in the server and the reply sent to the client?

      Packit fd8b60

      One straightforward way is for the reply to be the returned data in the request RPC call (an [out] parameter). That is, data passed from the RPC server to the RPC client. The request handler calls ccs_server_handle_request. Eventually, the server code calls ccs_os_server_send_reply, which saves the reply somewhere. When the server eventually returns to the request handler, the handler returns the saved reply to the client.

      Packit fd8b60

      But this doesn't work. If two clients A and B ask for the same lock, A will acquire the lock and B will have to wait. But if the single threaded server waits for B's lock, it will never handle A's unlock message. Therefore the server must return to B's request handler and not send a reply to B. So this method will not work.

      Packit fd8b60

      Instead, there are listener and worker threads in Windows-specific code.

      Packit fd8b60

      The client's cci_os_ipc function waits for ccs_reply. The client sends the request, including its UUID, from which the server can construct the endpoint on which to call ccs_reply.

      Packit fd8b60

      The server's listener thread listens for RPC requests. The request handler puts each request/reply endpoint in a queue and returns to the client.

      Packit fd8b60

      The server's worker thread removes items from the queue, calls ccs_server_handle_request. ccs_server_handle_request takes both the request data and the client UUID . Eventually ccs_os_server_send_reply is called, with the reply data and client UUID in the reply_pipe. ccs_os_server_send_reply calls ccs_reply on the client's endpoint, which sends the reply to the client.

      Packit fd8b60

      Is there any security issue with the client listening for RPC calls from the server?

      Packit fd8b60

      Connections

      Packit fd8b60

      If the client wants state to be maintained on the server, the client creates a connection. When the connection is closed, the server cleans up any state associated with the connection.

      Packit fd8b60

      Any given thread in an application process could want to create a connection. When cci_ipc_thread_init is called, the connection thread-local variables are initialized. New connections are created when cci_os_ipc() (via _cci_ipc_send) is called and no connection was previously established. Basically we lazily establish connections so the client doesn't talk to the server until it has to.

      Packit fd8b60

      Detecting client exit

      Packit fd8b60

      The server must be able to detect when clients disappear, so the server can free any resources that had been held for the client.

      Packit fd8b60

      The Windows RPC API does not appear to provide a notification for an endpoint disappearing. It does provide a way to ask if an endpoint is listening. This is useful for polling, but we want a better performing solution than that.

      Packit fd8b60

      The client has an isAlive function on its endpoint.

      Packit fd8b60

      To detect the client disappearing without using polling, the server makes an asynchronous call to the isAlive function on the client's endpoint. The isAlive function never returns. When the client exits for any reason, its endpoint will be closed and the server's function call will return an error. The asynchronous call on the server means no additional threads are used.

      Packit fd8b60

      Windows provides a number of notification methods to signal I/O completion. Among them are I/O completion ports and callback functions. I chose callback functions because they appear to consume fewer resources.

      Packit fd8b60

      RPC Endpoint / Function summary

      Packit fd8b60
        Packit fd8b60
          
      • The server creates one CCS_<LSID> endpoint to listen for connection requests and client requests.
      • Packit fd8b60
            It has the functions
        Packit fd8b60
            
          Packit fd8b60
                
        • ccs_rpc_connect(msgtype, UUIDlen, <UUID>, status)
        • Packit fd8b60
                
        • ccs_rpc_request(msgtype, UUIDlen, <UUID>, msglen, msg, SST, status) called by client. NB: The windows server sets the in_client_pipe to the in_reply_pipe.
        • Packit fd8b60
            
          Packit fd8b60
              
          Packit fd8b60
            
          Packit fd8b60
            
        • Each client thread creates a CCAPI_<UUID> endpoint. It has the functions
        • Packit fd8b60
              
            Packit fd8b60
                  
          • isAlive [function never returns.]
          • Packit fd8b60
                  
          • ccs_rpc_request_reply(msgtype, SST, replylen, reply, status)
          • Packit fd8b60
                  
          • ccs_rpc_connect_reply(msgtype, SST, status
          • Packit fd8b60
                
            Packit fd8b60
              
            Packit fd8b60
            Packit fd8b60

            Windows-specific implementation details

            Packit fd8b60

            Client CCAPI library initialization:

            Packit fd8b60

            This code runs when the CCAPI DLL is loaded.

            Packit fd8b60
              Packit fd8b60
                
            • ?
            • Packit fd8b60
              Packit fd8b60

              Client initialization:

              Packit fd8b60

              This code runs when cci_os_ipc_thread_init is called:

              Packit fd8b60
                Packit fd8b60
                  
              • Generate <UUID> and save in thread-specific storage. This serves as the client ID / ccs_pipe_t.
              • Packit fd8b60
                  
              • Create client endpoint.
              • Packit fd8b60
                  
              • Listen on client endpoint.
              • Packit fd8b60
                  
              • Create canonical server connection endpoint from the <LSID>, which the client and server should have in common.
              • Packit fd8b60
                  
              • Test if server is listening to the CCS_<LSID> endpoint.
              • Packit fd8b60
                      
                  Packit fd8b60
                          
                • If not, quit. (! Start it?)
                • Packit fd8b60
                        
                  Packit fd8b60
                    
                  Packit fd8b60
                    
                • Call ccs_connect(<UUID>) on the CCS_<LSID> endpoint.
                • Packit fd8b60
                    
                • Save SST in thread-specific storage.
                • Packit fd8b60
                  Packit fd8b60

                  Server initialization:

                  Packit fd8b60

                  [old]

                  Packit fd8b60
                    Packit fd8b60
                      
                  • Server is initialized by client starting a new process. There should be only one server process per Windows username.
                  • Packit fd8b60
                    Packit fd8b60

                    [new]

                    Packit fd8b60
                      Packit fd8b60
                        
                    • Server is started by kfwlogon (as is done currently).
                    • Packit fd8b60
                        
                    • Capture server start time (SST).
                    • Packit fd8b60
                        
                    • Start listener thread, create listener endpoint, listen on CCS_<LSID> endpoint.
                    • Packit fd8b60
                      Packit fd8b60

                      Establishing a connection:

                      Packit fd8b60
                        Packit fd8b60
                          
                      • Client calls ccs_connect(<UUID>) on server's CCS_<LSID> endpoint.
                      • Packit fd8b60
                          
                      • Client gets back and stores SST in thread-specific storage.
                      • Packit fd8b60
                          
                      • If new connection, server ...
                      • Packit fd8b60
                            
                          Packit fd8b60
                                
                        • adds connection to connection table
                        • Packit fd8b60
                                
                        • calls isAlive on CCAPI_<UUID>.
                        • Packit fd8b60
                                  
                            Packit fd8b60
                                      
                          • NB: isAlive never returns.
                          • Packit fd8b60
                                    
                            Packit fd8b60
                                  
                            Packit fd8b60
                                
                            Packit fd8b60
                              
                            Packit fd8b60
                            Packit fd8b60

                            Client request:

                            Packit fd8b60

                            The server's reply to the client's request is not synchronous.

                            Packit fd8b60
                              Packit fd8b60
                                
                            • Client calls ccs_rpc_request(msglen, msg, msgtype, UUIDlen, <UUID>, SST, status) on server's endpoint.
                            • Packit fd8b60
                                
                            • Server listen thread receives message, queues request.
                            • Packit fd8b60
                                
                            • Server worker thread dequeues request, processes, calls ccs_rpc_reply(replylen, reply, msgtype, status) on CCAPI_<UUID>.
                            • Packit fd8b60
                                
                            • Server checks SST. If server's SST is different, it means server has restarted since client created connection.
                            • Packit fd8b60
                                
                            • Client receives reply.
                            • Packit fd8b60
                              Packit fd8b60

                              Detecting client exit

                              Packit fd8b60
                                Packit fd8b60
                                  
                              • When connection created, client created an endpoint.
                              • Packit fd8b60
                                  
                              • Server calls isAlive on client's endpoint.
                              • Packit fd8b60
                                  
                              • When isAlive returns, the server's notification callback will be called. Call back routine queues a DISCONNECT pseudo-message. When the server's worker thread handles the DISCONNECT, it will release connection resources.
                              • Packit fd8b60
                                Packit fd8b60

                                Detecting server exit

                                Packit fd8b60
                                  Packit fd8b60
                                    
                                • Client's call to ccs_rpc_request will return an error if the server has gone away.
                                • Packit fd8b60
                                  Packit fd8b60

                                   

                                  Packit fd8b60

                                  ------

                                  Packit fd8b60
                                    Stop: 
                                  Packit fd8b60
                                  Start: 

                                  Packit fd8b60
                                  </body>
                                  Packit fd8b60
                                  </html>