Transcript RPC

Adv. Network Programming
RPC (Remote Procedure Call)
Kubilay Akgül
RPC (Remote Procedure Call)







Two Paradigms for Distributed Programs.
Smilarities and Differences Conventional
Procedure Calls and RPC
SUN RPC
Remote Program Identification
RPC Mapping, Inet.d
Call Semantics
RPCGEN
ADVANCED NETWORK PROGRAMING -- RPC
2
Two Paradigms
A programmer can use one of two approaches:
 Communication Oriented Design
- Focus on communication program. (Message format and syntax)
- Design client server components. (Reactions to messages)
•May miss important subtitles.
•Fundemental errors under stress.
•Few programmers have experience with protocol design
•Concentration on communication.
•Resulting programs will be difficult to understand.
ADVANCED NETWORK PROGRAMING -- RPC
3
Two Paradigms

Application Oriented Design
- Focus on application.
- Design a conventional program that works correctly.
- Build, compile and test it.
- Devide the program into pieces. (Procedure level)
- Communication level and data representation is transparent.
 RPC
ADVANCED NETWORK PROGRAMING -- RPC
4
Conventional Programs and RPC

Smilarities
CALL
 Caller transfers control to called procedure.
 Caller suspends.
 Only one thread of execution continues at any given time.
 Values in all variables are frozen.
RESPONSE
 Control flows back to the caller
 A called procedure may call another procedure. (Server becomes a
client)
ADVANCED NETWORK PROGRAMING -- RPC
5
Conventional Programs and RPC

Differences

Server Procedures
- RPC procedures must exist and be waiting to compute a response.
Data Flow
- RPC may accept or return arbitrary amount of data.
Network Delays
- Protocol, lost packets, data representation.
Adress Space
- Conventional programs can pass pointers as argument.
Environment
- RPC does not have direct access to callers I/O descriptors, OS
functions...




ADVANCED NETWORK PROGRAMING -- RPC
6
SUN Microsystems’ RPC Definition








SUN defined specific form of RPC  SUNRPC
Wide acceptance, many applications. (NFS)
Defines format of messages and arguments.
Protocol  TCP , UDP.
Data Representation  XDR.
Uses structure instead of multiple arguments.
At most one remote procedure in a remote program can be invoked at a
given time  Automatic Mutual Exclusion
A complier tool helps programmers to build distributed programs
automatically  RPCGEN
ADVANCED NETWORK PROGRAMING -- RPC
7
Remote Program and Procedure Identification
Program no



Description
Program Number
0x00000000 - 0x1fffffff
defined by SUN
- Each program must be assigned a unique 32 - bit integer
0x20000000 - 0x3fffffff
defined by system mng.
Procedure Number
0x40000000 - 0x5fffffff
customer written appl.
- Each procedure in a remote program must have an integer. (1,2,3...)
0x60000000 - 0xffffffff
reserved
Version number
- Multiple versions may run at the same time.
You can change details with same version number.
Easy to migrate.
(prog, ver, proc)
ADVANCED NETWORK PROGRAMING -- RPC
8
RPC Mapping


UDP and TCP  16 bit protocol port numbers.
RCP  32 bit program numbers.
Servers uses well known ports.
RPC servers use ephemeral ports. They takes different ports after each
time they restart.
Machine address + Program no  ?  correct server port
ADVANCED NETWORK PROGRAMING -- RPC
9
PORT MAPPER, RPCBIND


Uses a database of dynamic port mapping. (TCP/UDP port = 111)
Starts in multiuser mode.
• Server’s main function calls svc_create
- Determines protocols supported by clients
- Creates end-points, Binds port to TCP and UDP.
- Contacts RPCBIND and registers itself (port, prog, ver).
- Goes to sleep.
• Client calls clnt_create (server_IP, prog_no, ver, protocol)
- Usually makes a UDP connection to RPCBIND on servers
machine.
ADVANCED NETWORK PROGRAMING -- RPC
10
PORT MAPPER, RPCBIND
Uses /etc/rpc
Solaris % rpcinfo -p
program
vers
proto port
100000
4
tcp
111
100000 Solaris3% rpcinfo
tcp -d sprayd
111 1
100000
2
tcp
111
sprayd
100000 ... unregisters
4
udp demon.
111
100000
3
udp
111
100000
2
udp
111
....
-HUP inetd
824377344Solaris1% pkilludp
42972
824377344... inetd
1 reads the
tcp/etc/inetd.conf
40849
again.
V2  historical (just TCP UDP)
v3,4  newer protocols
service
rpcbind
rpcbind
rpcbind
rpcbind
rpcbind
rpcbind
server
server
file
and registers sprayd
ADVANCED NETWORK PROGRAMING -- RPC
11
RPC - Inetd


RPCgen created servers may be invoked by inetd.
/etc/inetd.conf file needs ti be updated.
...
Rstatd/2-4 tli rpc/datagram_V wait root /usr/.../rpc.rstatd rpc.rstatd
...
Inetd
- creates endpoint, registers to RPCBIND.
- listens for ports, fork, exec, waits until server terminates.
Server
- std. input is a xti endpoint (no need to re-register).
- waits for ~2 minutes before terminate ( to prevent new fork and exec)
- generates SIGCHLD to inetd.
ADVANCED NETWORK PROGRAMING -- RPC
12
Call Semantics

Procedure calls can be placed into one of these categories:
1. Exactly once
2. At most once
3. At least once
4. Zero or more
ADVANCED NETWORK PROGRAMING -- RPC
13
Call Semantics - Scenarios

TCP
Reply received: Exactly Once
No reply: At most once (server may have crashed)

UDP without server cash
Reply received: at least once
No reply: zero or more

UDP with server cash
Reply received: Exactly Once
No reply: At most once
ADVANCED NETWORK PROGRAMING -- RPC
14
Stub Procedure Consept & RPCGEN

Sun RPC specifications are extensive and complicated without a
software tool.
Assistance:
1. XDR library routines: internal data items  XDR standard
2. RPC untime library: correctly make call, receive call, registration...
3. RPCgen: A program generater
ADVANCED NETWORK PROGRAMING -- RPC
15
Stub Procedures

Additional procedures added to program to implement RPC.

Client Side Stub : They replaces the called procedure.
Server Side Stub : They replaces the caller procedure.

Computer 1
Computer 2
PROC A
Server
Stub
Client
Stub
Proc B
ADVANCED NETWORK PROGRAMING -- RPC
16
RPCGEN



Rpcgen reads an input file that contains a specification of remote
program.
IT produces four output (source code) files.
If specification file has name Q.x all output files will begin with Q.
- Q.h
- Q_xdr.c
- Q_clnt.c
- Q_svc.c
Declarations of constants and types used in the code generated for
both client and server.
XDR procedure calls used in the client and server to marshal arg.
Client side stub procedure.
Server side stub procedure.
ADVANCED NETWORK PROGRAMING -- RPC
17
RPC Example
ADVANCED NETWORK PROGRAMING -- RPC
18
RPC Example
• Specification file
struct square_in {
/* input (argument) */
long
arg1;
};
struct square_out {
/* output (result) */
long
res1;
};
program SQUARE_PROG {
version SQUARE_VERS {
square_out SQUAREPROC(square_in) = 1;
/* procedure number = 1 */
} = 1;
/* version number */
} = 0x31230000;
/* program number */
ADVANCED NETWORK PROGRAMING -- RPC
19
RPC Example
Client.c (Client main function)
#include "unpipc.h"
#include "square.h"
int main(int argc, char **argv)
{
CLIENT
*cl;
square_in
in;
square_out
*outp;
Client
handle
/* our header */
/* generated by rpcgen */
if (argc != 3)
err_quit("usage: client <hostname> <integer-value>");
cl = clnt_create(argv[1], SQUARE_PROG, SQUARE_VERS, "tcp");
in.arg1 = atol(argv[2]);
if ( (outp = squareproc_1(&in, cl)) == NULL)
err_quit("%s", clnt_sperror(cl, argv[1]));
printf("result: %ld\n", outp->res1);
exit(0);
}
ADVANCED NETWORK PROGRAMING -- RPC
20
RPC Example
Server Procedure
#include "unpipc.h"
#include "square.h"
square_out *
squareproc_1_svc(square_in *inp, struct svc_req *rqstp)
{
static square_out out;
out.res1 = inp->arg1 * inp->arg1;
return(&out);
}
ADVANCED NETWORK PROGRAMING -- RPC
21
RPC Example
Client executable
Solaris %
Solaris %
Solaris %
Solaris %
Solaris %
rpcgen -C square.x
cc -c client.c -o client.o
cc -c square_clnt.c -o square_clnt.o
cc -c square_xdr.c -o square_xdr.o
cc -o client client.o square_clnt.o square_xdr.o libunpipc.a -lnsl
Server executable
Solaris % cc -c server.c -o server.o
Solaris % cc -c square_svc.c -o square.svc.o
Solaris % cc -o server server.o square_svc.o square_xdr.o libunpipc.a -lnsl
ADVANCED NETWORK PROGRAMING -- RPC
22
RPC Example
ADVANCED NETWORK PROGRAMING -- RPC
23