Remote Procedure Calls, TLI, STREAMS 13 RPC, TLI, STREAMS Hannes Lubich, 2003 2005 Page 1 Goals of this Lecture Understand the design of remote procedure calls in general and the design and programming of the Sun Remote Procedure Call Package in particular. Understand the general concept of Unix STREAMS and their use for designing modular communication drivers. Understand the the general concept of the Transport Layer Interface (TLI) as a generalisation of the original socket interface. 13 RPC, TLI, STREAMS Hannes Lubich, 2003 2005 Page 2 1
General RPC Design Client Process Server Process Local Procedure Call Client Routines Client "Stub" (Un)-Marshalling Server Routines Server- "Stub" Local Procedure Call System Call Network Routines Local Kernel Network Communication Network Routines Remote Kernel Data Conversion And Notification W.R. Stevens: UNIX Network Programming, Figure 18.1 13 RPC, TLI, STREAMS Hannes Lubich, 2003 2005 Page 3 Sun RPC - Published 1988 - RPC-Protocol 2, RFC-1057 - RPC-Data Description Language (XDR), RFC-1014 - Source code for Unix is available: "rpcgen", XDR Coding Library, Run-time Library - Now termed ONC (Open Network Computing) 13 RPC, TLI, STREAMS Hannes Lubich, 2003 2005 Page 4 2
Parameter Passing - Call by value - Call by reference - Sun RPC only allows one (structured) argument or result value. - Multiple arguments or results must be packed into a single struct. 13 RPC, TLI, STREAMS Hannes Lubich, 2003 2005 Page 5 Server / Client Binding - Finding a remote server - Finding the required service on the remote server - Sun RPC uses the standard Unix method for finding servers on the Internet (gethostbyname, DNS), some implementations also support the clnt_broadcast() library call to find servers. - All server programs, program versions and remote procedures are identified with unique numbers. - A portmapper process (Linux: rpcbind) using port 111 on each system serves as the central local registry for available RPC services. It is possible to have server programs started via the inetd daemon (rpcgen compile option and a corresponding entry in /etc/inetd.conf). 13 RPC, TLI, STREAMS Hannes Lubich, 2003 2005 Page 6 3
The RPC Portmapping Sequence 1) The server creates a socket and registers program number, program version, procedures, and port number. 2) The client contacts the port mapper, requesting a program, version and remote procedure number. If known, the portmapper returns the corresponding port number. 3,4) The client can now invoke the remote procedure with the server. portmapper Daemon (1) (2) (3) (4) RPC Client RPC Server Local System Remote System W.R. Stevens: UNIX Network Programming, Figure 18.4 13 RPC, TLI, STREAMS Hannes Lubich, 2003 2005 Page 7 Selecting the Transport Protocol - RPC is independent from transport services /protocols - Mapping to popular transport services /protocols - Sun RPC supports the use of TCP and UDP In case of UDP, the size of arguments or results may not exceed 8192 bytes 13 RPC, TLI, STREAMS Hannes Lubich, 2003 2005 Page 8 4
Exception Handling - In addition to local errors, further errors may occur within the server system and during data transport. - Cancellation of operations on the server by the client - Termination of the client before the server has finished - Sun RPC uses automatic re-sending of packets in case of UDP, and recognises lost connections in TCP (keepalive option on socket, see setsockopt). - Sun RPC does not support a separate control channel. 13 RPC, TLI, STREAMS Hannes Lubich, 2003 2005 Page 9 Call Semantics - Procedure is executed exactly once - Procedure is executed at most once - Procedure is executed at least once (only acceptable for idempotent operations) - Sun RPC uses unique transaction numbers for each call. - The server maintains a cache of recent requests, and the results returned, and re-sends the associated results in case of duplicate remote procedure calls. 13 RPC, TLI, STREAMS Hannes Lubich, 2003 2005 Page 10 5
Security Considerations - Security within the local system - Security within the network subsystem - Security within the RPC package - Security within RPC-based applications - Sun RPC supports 3 security levels: Null, Unix, DES - Null is the default value. - Unix contains a timestamp, the hostname of the sending system, as well as the user ID and group ID of the client process, and all groups the client process belongs to. - Usage of the DES level is described in RFC-1057. 13 RPC, TLI, STREAMS Hannes Lubich, 2003 2005 Page 11 Data Representation (XDR) I - Byte Ordering: big endian - Signed Integer Format: 2 s complement - Floating Point Format: proprietary - Supported Data Types: 32 bit logical value (boolean) Signed / unsigned 32 bit integer Signed / unsigned 64 bit integer 32 and 64 bit floating point values ASCII character set 13 RPC, TLI, STREAMS Hannes Lubich, 2003 2005 Page 12 6
Data Representation (XDR) II - Further Supported Data Types: Enumeration Structs Fixed-length single-dimensional array Variable-length single-dimensional array Discriminated union Fixed-length opaque data Variable-length opaque data 13 RPC, TLI, STREAMS Hannes Lubich, 2003 2005 Page 13 RPC Programming Example Server Procedures Server dir_proc.c cc list_svc Server Stub list_svc.c RPC Specification list.x rpcgen list.h list_xdr.c RPC Runtime Library Client Main Program rls.c list_clnt.c Client Stub cc Client rls W.R. Stevens: UNIX Network Programming, Figure 18.3 13 RPC, TLI, STREAMS Hannes Lubich, 2003 2005 Page 14 7
The XDR Specification: list.x /* list.x: Remote directory listing protocol */ const MAXNAMELEN = 255; /* maximum length of a name */ typedef string nametype<maxnamelen>; /* a directory entry */ typedef struct namenode *namelist; /* a link in the listing */ /* A node in the directory listing */ struct namenode { nametype name; /* name of directory entry */ namelist next; }; /* next entry */ /* The result of a READDIR operation */ union readdir_res switch (int errno) { case 0: namelist list; /* no error: return dir listing */ default: void; }; /* error; nothing to return */ program DIRPROG { version DIRVERS { readdir_res READDIR (nametype) = 1; } = 1; } = 0x33333333; 13 RPC, TLI, STREAMS Hannes Lubich, 2003 2005 Page 15 Program Number Registration Distributed namespace administration See /etc/rpc (exact path and filename depending on Unix variant used) 0x00000000 0x1FFFFFFF: administered by ONC 0x20000000 0x3FFFFFFF: defined by user 0x40000000 0x5FFFFFFF: dynamically assigned 0x60000000 0xFFFFFFFF: reserved for future use 13 RPC, TLI, STREAMS Hannes Lubich, 2003 2005 Page 16 8
/* rls.c: remote directory listing client */ #include <stdio.h> #include <rpc/rpc.h> #include "list.h" extern int errno; main (ac, av) int ac; char **av; { CLIENT *cl; char *server; char *dir; readdir_res *result; namelist nl; if (ac!= 3) { fprintf (stderr "usage: %s host directory\n", av[0]); exit (1); } /* Remember what our command line argument refers to */ server = av[1]; dir = av[2]; The Client Program: rls.c I 13 RPC, TLI, STREAMS Hannes Lubich, 2003 2005 Page 17 The Client Program: rls.c II /* Create client handle used for calling MESSAGEPROC on the * server designated on the command line. We tell the RPC * package to use the TCP protocol when contacting the server.*/ cl = clnt_create (server, DIRPROG, DIRVERS, "tcp"); if (cl == NULL) { /* Couldn't establish connection with server. * Print error message and die. */ clnt_pcreateerror (server); exit (1); } /* Call the remote procedure readdir on the server side */ result = readdir_1 (&dir, cl); if (result == NULL) { /* An error occurred while calling the server. * Print error message and die. */ clnt_perror (cl, server); exit (1); } 13 RPC, TLI, STREAMS Hannes Lubich, 2003 2005 Page 18 9
The Client Program: rls.c III } /* Okay, we successfully called the remote procedure. */ if (result -> errno!= 0) { /* A remote system error occurred. * Print error message and die. */ errno = result -> errno; perror (dir); exit (1); } /* Successfully got a directory listing. * Print it out. */ for (nl = result->readdir_res_u.list; nl!= NULL; nl = nl->next) printf ("%s\n", nl -> name); exit (0); 13 RPC, TLI, STREAMS Hannes Lubich, 2003 2005 Page 19 The Server Procedure: dir_proc.c I /* dir_proc.c: remote readdir implementation */ #include <rpc/rpc.h> #include <sys/dir.h> #include <stdio.h> #include "list.h" extern int errno; extern char *malloc(); extern char *strdup(); readdir_res * readdir_1_svc (dirname) nametype *dirname; { DIR *dirp; structdirect *d; namelist nl; namelist *nlp; static readdir_res res; /* must be static */ fprintf (stderr, "RPC-INVOKE-REQUEST: list directory %s\n", *dirname); /* Open directory */ dirp = opendir (*dirname); if (dirp == NULL) { res.errno = errno; fprintf (stderr, "RPC-ERROR: errno == %d\n", errno); return (&res); } 13 RPC, TLI, STREAMS Hannes Lubich, 2003 2005 Page 20 10
The Server Procedure: dir_proc.c II } /* Free previous result */ xdr_free (xdr_readdir_res, &res); /* Collect directory entries * Memory allocated here will be freed by xdr_free() * next time readdir_1_svc is called */ nlp = &res.readdir_res_u.list; while (d = readdir (dirp)) { nl = *nlp = (namenode *) malloc (sizeof (namenode)); nl -> name = strdup (d -> d_name); nlp = & nl -> next; } *nlp = NULL; /* Return the result */ res.errno = 0; closedir (dirp); fprintf (stderr, "RPC-INVOKE-RESULT: sending directory result\n"); return (&res); 13 RPC, TLI, STREAMS Hannes Lubich, 2003 2005 Page 21 all: rls: list_svc: list.h: rls.o: list_clnt.o: list_xdr.o: list_svc.o: dir_proc.o: The Makefile rls list_svc rls.o list_clnt.o list_xdr.o cc rls.o list_clnt.o list_xdr.o -o rls list_svc.o dir_proc.o list_xdr.o cc list_svc.o dir_proc.o list_xdr.o -o list_svc list.x rpcgen list.x rls.c list.h cc -c rls.c list_clnt.c list.h cc -c list_clnt.c list_xdr.c list.h cc -c list_xdr.c list_svc.c list.h cc -c list_svc.c dir_proc.c list.h cc -c dir_proc.c neat: rm -f *.o core a.out list.h \ list_clnt.c list_xdr.c list_svc.c clean: rm -f rls list_svc *.o core \ a.out list.h list_clnt.c list_xdr.c list_svc.c 13 RPC, TLI, STREAMS Hannes Lubich, 2003 2005 Page 22 11
Layer Integration RPC Application Presentation Session Cntl Transport Network Data Link Physical NFS RPC XDR RPC Library TCP UDP IP Ethernet Link Logical Link Control Ethernet Token Ring S.G. Kochan/P.H. Wood: UNIX Networking, Figure 4.1 13 RPC, TLI, STREAMS Hannes Lubich, 2003 2005 Page 23 Use and Limits of RPC - Servers are mostly stateless: all operations are independent from each other robust against errors within the client, server or network - Performance (local versus remote procedures) - Service strategies (single server, 1 server per client, ) - Distribution strategies (where to put servers) 13 RPC, TLI, STREAMS Hannes Lubich, 2003 2005 Page 24 12
The Transport Layer Interface (TLI) Developed by AT&T as a common communications interface First released as part of System V Release 3 (1986) Part of the System V Interface Definition (SVID) First release without a provider module (i.e. without connection to TCP/IP) Further development of TLI with Sun Microsystems 13 RPC, TLI, STREAMS Hannes Lubich, 2003 2005 Page 25 Basic Concepts of TLI Common communications interface for all data transport mechanisms below (TCP/IP, OSI, Novell, ) Corresponds to the interface between session layer (5) and transport layer (4) of the OSI reference model. TLI is only an interface to the lower protocol layers and translates TLI requests into the native protocol operations of the underlying service. TLI must be explicitly bound to every program wishing to use it. 13 RPC, TLI, STREAMS Hannes Lubich, 2003 2005 Page 26 13
The Functional Model of TLI Calling TLI Functions application TLI functions user program kernel Operating system protocol software Calling Operating System Functions D. Comer, Internetworking with TCP/IP, Vol. III 13 RPC, TLI, STREAMS Hannes Lubich, 2003 2005 Page 27 TLI Terminology Transport Endpoint (TEP): - Actual State - Actual Event - Implementation as a finite state machine Communication Descriptors: - Provided as file descriptors - Device special files are used to select a transport service (e.g. /dev/tcp) Addressing: - No pre-defined format - When using TCP/IP as the transport service, the known structure sockaddr_in is used (Quelle: M. Vogt, TIK, ETHZ) 13 RPC, TLI, STREAMS Hannes Lubich, 2003 2005 Page 28 14
Client TLI Call Sequence Server t_open t_bind t_connect t_open t_bind t_listen t_open t_bind t_accept t_snd t_rcv t_close D. Comer, Internetworking with TCP/IP, Vol. III t_rcv t_snd t_close 13 RPC, TLI, STREAMS Hannes Lubich, 2003 2005 Page 29 TLI versus Sockets TLI Interface User address space Sockets Complete subsystem Kernel address space t_listen + t_accept listen + accept (Quelle: M. Vogt, TIK, ETHZ) 13 RPC, TLI, STREAMS Hannes Lubich, 2003 2005 Page 30 15
STREAMS Designed by D. Ritchie for System V Release 3 Programming environment for the development of communication protocols Toolbox design with re-usable modules Provides implementation of commonly used modules Mapping of existing communication services (pipes, terminal-i/o etc.) onto STREAMS services Not supported by some Unix variants (e.g. Linux) 13 RPC, TLI, STREAMS Hannes Lubich, 2003 2005 Page 31 STREAM Components Stream Head: interface between kernel address space and user address space STREAMS Device Driver: mapping between a generic data format and the specific data format required for a device Messages: data travelling through a stream requiring memory allocation etc. Module: self-contained data processing step within a STREAM 13 RPC, TLI, STREAMS Hannes Lubich, 2003 2005 Page 32 16
Design of a STREAM fd[0] fd[1] q_info head head q_info open() close()... Stream functions read queue write queue q_next read queue q_next write queue Further modules can be introduced to a stream (push) or removed from a stream (pop). open() close()... Stream functions Each module has a read and a write queue and provides two procedures: put (calling a neighbour module when passing data) und service (flow control). The Streams Scheduler calls each module cyclically. There are special modules for communication (e.g. multiplexers). 13 RPC, TLI, STREAMS Hannes Lubich, 2003 2005 Page 33 Summary: RPC, STREAMS, TLI User-accessible API RPC TLI User Space Sockets STREAMS Kernel Space 13 RPC, TLI, STREAMS Hannes Lubich, 2003 2005 Page 34 17