Adv. Network Programming RPC (Remote Procedure Call)

23
Adv. Network Programming RPC (Remote Procedure Call) Kubilay Akgül

description

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. - PowerPoint PPT Presentation

Transcript of Adv. Network Programming RPC (Remote Procedure Call)

Page 1: Adv. Network Programming RPC (Remote Procedure Call)

Adv. Network Programming

RPC (Remote Procedure Call)

Kubilay Akgül

Page 2: Adv. Network Programming RPC (Remote Procedure Call)

ADVANCED NETWORK PROGRAMING -- RPC

2

RPC (Remote Procedure RPC (Remote Procedure Call)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

Page 3: Adv. Network Programming RPC (Remote Procedure Call)

ADVANCED NETWORK PROGRAMING -- RPC

3

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.

Page 4: Adv. Network Programming RPC (Remote Procedure Call)

ADVANCED NETWORK PROGRAMING -- RPC

4

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

Page 5: Adv. Network Programming RPC (Remote Procedure Call)

ADVANCED NETWORK PROGRAMING -- RPC

5

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)

Page 6: Adv. Network Programming RPC (Remote Procedure Call)

ADVANCED NETWORK PROGRAMING -- RPC

6

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...

Page 7: Adv. Network Programming RPC (Remote Procedure Call)

ADVANCED NETWORK PROGRAMING -- RPC

7

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

Page 8: Adv. Network Programming RPC (Remote Procedure Call)

ADVANCED NETWORK PROGRAMING -- RPC

8

Remote Program and Procedure Identification

Program Number

- Each program must be assigned a unique 32 - bit integer Procedure Number

- Each procedure in a remote program must have an integer. (1,2,3...) Version number

- Multiple versions may run at the same time.

You can change details with same version number.

Easy to migrate.

(prog, ver, proc)

Program no Description

0x00000000 - 0x1fffffff defined by SUN

0x20000000 - 0x3fffffff defined by system mng.

0x40000000 - 0x5fffffff customer written appl.

0x60000000 - 0xffffffff reserved

Page 9: Adv. Network Programming RPC (Remote Procedure Call)

ADVANCED NETWORK PROGRAMING -- RPC

9

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

Page 10: Adv. Network Programming RPC (Remote Procedure Call)

ADVANCED NETWORK PROGRAMING -- RPC

10

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.

Page 11: Adv. Network Programming RPC (Remote Procedure Call)

ADVANCED NETWORK PROGRAMING -- RPC

11

PORT MAPPER, RPCBIND

Solaris % rpcinfo -p

program vers proto port service100000 4 tcp 111 rpcbind100000 3 tcp 111 rpcbind100000 2 tcp 111 rpcbind100000 4 udp 111 rpcbind100000 3 udp 111 rpcbind100000 2 udp 111 rpcbind....824377344 1 udp 42972 server824377344 1 tcp 40849 server

Uses /etc/rpc

Solaris % rpcinfo -d sprayd 1

... unregisters sprayd demon.

Solaris % pkill -HUP inetd

... inetd reads the /etc/inetd.conf file and registers sprayd again.

V2 historical (just TCP UDP)v3,4 newer protocols

Page 12: Adv. Network Programming RPC (Remote Procedure Call)

ADVANCED NETWORK PROGRAMING -- RPC

12

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.

Page 13: Adv. Network Programming RPC (Remote Procedure Call)

ADVANCED NETWORK PROGRAMING -- RPC

13

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

Page 14: Adv. Network Programming RPC (Remote Procedure Call)

ADVANCED NETWORK PROGRAMING -- RPC

14

Call Semantics - Scenarios

TCPReply received: Exactly Once

No reply: At most once (server may have crashed)

UDP without server cashReply received: at least once

No reply: zero or more

UDP with server cashReply received: Exactly Once

No reply: At most once

Page 15: Adv. Network Programming RPC (Remote Procedure Call)

ADVANCED NETWORK PROGRAMING -- RPC

15

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

Page 16: Adv. Network Programming RPC (Remote Procedure Call)

ADVANCED NETWORK PROGRAMING -- RPC

16

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.

PROC A

Client Stub

Server Stub

Proc B

Computer 1 Computer 2

Page 17: Adv. Network Programming RPC (Remote Procedure Call)

ADVANCED NETWORK PROGRAMING -- RPC

17

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 Declarations of constants and types used in the code generated for both client and server.

- Q_xdr.c XDR procedure calls used in the client and server to marshal arg.

- Q_clnt.c Client side stub procedure.

- Q_svc.c Server side stub procedure.

Page 18: Adv. Network Programming RPC (Remote Procedure Call)

ADVANCED NETWORK PROGRAMING -- RPC

18

RPC Example

Page 19: Adv. Network Programming RPC (Remote Procedure Call)

ADVANCED NETWORK PROGRAMING -- RPC

19

RPC Example

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 */

• Specification file

Page 20: Adv. Network Programming RPC (Remote Procedure Call)

ADVANCED NETWORK PROGRAMING -- RPC

20

RPC Example

#include "unpipc.h" /* our header */#include "square.h" /* generated by rpcgen */int main(int argc, char **argv){

CLIENT *cl;square_in in;square_out *outp;

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);

}

Client.c (Client main function)

Client handle

Page 21: Adv. Network Programming RPC (Remote Procedure Call)

ADVANCED NETWORK PROGRAMING -- RPC

21

RPC Example

#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);

}

Server Procedure

Page 22: Adv. Network Programming RPC (Remote Procedure Call)

ADVANCED NETWORK PROGRAMING -- RPC

22

RPC Example

Solaris % rpcgen -C square.x

Solaris % cc -c client.c -o client.o

Solaris % cc -c square_clnt.c -o square_clnt.o

Solaris % cc -c square_xdr.c -o square_xdr.o

Solaris % cc -o client client.o square_clnt.o square_xdr.o libunpipc.a -lnsl

Client executable

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

Page 23: Adv. Network Programming RPC (Remote Procedure Call)

ADVANCED NETWORK PROGRAMING -- RPC

23

RPC Example