lecture 20 remote procedure call external data representation

77
Lecture 20 Remote Procedure Call eXternal Data Representation CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger

Upload: ralph

Post on 12-Jan-2016

37 views

Category:

Documents


1 download

DESCRIPTION

Lecture 20 Remote Procedure Call eXternal Data Representation. CPE 401 / 601 Computer Network Systems. slides are modified from Dave Hollinger. Distributed Program Design. Typical Sockets Approach. Communication-Oriented Design Design protocol first - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 20 Remote Procedure Call eXternal Data Representation

Lecture 20

Remote Procedure CalleXternal Data

Representation

CPE 401 / 601Computer Network Systems

slides are modified from Dave Hollinger

Page 2: Lecture 20 Remote Procedure Call eXternal Data Representation

Distributed Program Design

Communication-Oriented Design Design protocol first Build programs that adhere to the protocol

Application-Oriented Design Build application(s) Divide programs up and add communication

protocols

RPC Overview 2

Typical Typical SocketsSocketsApproachApproach

RPCRPC

Page 3: Lecture 20 Remote Procedure Call eXternal Data Representation

RPC: Remote Procedure Call

Call a procedure (subroutine) that is running on another machine.

Issues: identifying and accessing the remote

procedure parameters return value

RPC Overview 3

Page 4: Lecture 20 Remote Procedure Call eXternal Data Representation

Remote Subroutine

RPC Overview 4

blah, blah, blah

bar = foo(a,b);

blah, blah, blah

blah, blah, blah

bar = foo(a,b);

blah, blah, blah

int foo(int x, int y ) { if (x>100)

return(y-2); else if (x>10)

return(y-x); else

return(x+y);}

int foo(int x, int y ) { if (x>100)

return(y-2); else if (x>10)

return(y-x); else

return(x+y);}

ClientClient

ServerServer

protocol

Page 5: Lecture 20 Remote Procedure Call eXternal Data Representation

Sun RPC

There are a number of popular RPC specifications.

Sun RPC (ONC RPC) is widely used.

NFS (Network File System) is RPC based.

Rich set of support tools.

RPC Overview 5

Page 6: Lecture 20 Remote Procedure Call eXternal Data Representation

Sun RPC Organization

RPC Overview 6

Procedure 1Procedure 1 Procedure 2Procedure 2 Procedure 3Procedure 3

Shared Global DataShared Global Data

Remote ProgramRemote Program

Page 7: Lecture 20 Remote Procedure Call eXternal Data Representation

Procedure Arguments

To reduce the complexity of the interface specification, Sun RPC includes support for a single

argument to a remote procedure• Typically the single argument is a structure that

contains a number of values.• Newer versions can handle multiple args

RPC Overview 7

Page 8: Lecture 20 Remote Procedure Call eXternal Data Representation

Procedure Identification

Each procedure is identified by: Hostname (IP Address) Program identifier (32 bit integer) Procedure identifier (32 bit integer)

• usually start at 1 and are numbered sequentially Program version identifies

• for testing and migration• typically start at 1 and are numbered sequentially

RPC Overview 8

Page 9: Lecture 20 Remote Procedure Call eXternal Data Representation

Program Identifiers

Each remote program has a unique ID.

Sun divided up the IDs:

0x00000000 - 0x1fffffff

0x20000000 - 0x3fffffff

0x40000000 - 0x5fffffff

0x60000000 - 0xffffffff

RPC Overview 9

SunSun

SysAdmin SysAdmin

TransientTransient

ReservedReserved

Page 10: Lecture 20 Remote Procedure Call eXternal Data Representation

Iterative Server

Sun RPC specifies that at most one remote procedure within a program can be invoked at any given time. If a 2nd procedure is called, the call blocks

until the 1st procedure has completed.

Having an iterative server is useful for applications that may share data among procedures. Eg: database to avoid insert/delete/modify

collisionsRPC Overview 10

Page 11: Lecture 20 Remote Procedure Call eXternal Data Representation

Call Semantics

What does it mean to call a local procedure? the procedure is run exactly one time.

What does it mean to call a remote procedure? It might not mean "run exactly once"!

RPC Overview 11

Page 12: Lecture 20 Remote Procedure Call eXternal Data Representation

Remote Call Semantics

To act like a local procedure exactly one invocation per call

• a reliable transport (TCP) is necessary.

Sun RPC does not support reliable call semantics !

"At Least Once" Semantics if we get a response (a return value)

"Zero or More" Semantics if we don't hear back from the remote

subroutineRPC Overview 12

Page 13: Lecture 20 Remote Procedure Call eXternal Data Representation

Remote Procedure deposit()

deposit(DavesAccount,$100)

Always remember that you don't know how many times the remote procedure was run!

The net can duplicate the request (UDP).

RPC Overview 13

Page 14: Lecture 20 Remote Procedure Call eXternal Data Representation

Network Communication

The actual network communication is nothing new it's just TCP/IP.

Many RPC implementations are built upon the sockets library. the RPC library does all the work!

We are just using a different API, the underlying stuff is the same!

RPC Overview 14

Page 15: Lecture 20 Remote Procedure Call eXternal Data Representation

Dynamic Port Mapping Servers typically do not use well known

protocol ports! Clients know the Program ID

and host IP address

RPC includes support for looking up the port number of a remote program. A port lookup service runs on each host

RPC servers register "I'm program 17 and I'm looking for requests

on port 1736”

RPC Overview 15

Page 16: Lecture 20 Remote Procedure Call eXternal Data Representation

The portmapper

Each system which will support RPC servers runs a port mapper server provides a central registry for RPC services

Servers tell the port mapper what services they offer

Clients ask a remote port mapper for the port number corresponding to Remote Program ID

The portmapper is itself an RPC server! is available on a well-known port (111)

Netprog: RPC Overview 16

Page 17: Lecture 20 Remote Procedure Call eXternal Data Representation

RPC Programming RPC library is a collection of tools for

automating the creation of clients and servers clients are processes that call remote procedures servers are processes that include procedure(s)

that can be called by clients RPC library

XDR routines RPC run time library

• call rpc service• register with portmapper• dispatch incoming request to correct procedure

Program Generator RPC Overview 17

Page 18: Lecture 20 Remote Procedure Call eXternal Data Representation

RPC Run-time Library

High- and Low-level functions that can be used by clients and servers

High-level functions provide simple access to RPC services High-Level RPC library calls support UDP

only• must use lower-level RPC library functions to use

TCP High-Level library calls do not support any

kind of authentication

RPC Overview 18

Page 19: Lecture 20 Remote Procedure Call eXternal Data Representation

Low-level RPC Library

Full control over all Inter-Process Communication options TCP & UDP Timeout values Asynchronous procedure calls

Multi-tasking Servers

Broadcasting

RPC Overview 19

Page 20: Lecture 20 Remote Procedure Call eXternal Data Representation

RPCGEN

There is a tool for automating the creation of RPC clients and servers.

The program rpcgen does most of the work for you

The input to rpcgen is a protocol definition in the form of a list of remote procedures

and parameter types

RPC Overview 20

Page 21: Lecture 20 Remote Procedure Call eXternal Data Representation

RPCGEN

RPC Overview 21

Input FileInput File

rpcgen

Client Stubs XDR filters header file Server skeleton

C Source CodeC Source Code

ProtocolProtocolDescriptionDescription

Page 22: Lecture 20 Remote Procedure Call eXternal Data Representation

rpcgen Output Files

> rpcgen –C foo.x

foo_clnt.c (client stubs)foo_svc.c (server main)foo_xdr.c (xdr filters)foo.h (shared header file)

RPC Overview 22

Page 23: Lecture 20 Remote Procedure Call eXternal Data Representation

Client Creation

> gcc -o fooclient foomain.c foo_clnt.c foo_xdr.c -lnsl

foomain.c is the client main() (and possibly other functions) that call rpc services via the client stub functions in foo_clnt.c

The client stubs use the xdr functions

RPC Overview 23

Page 24: Lecture 20 Remote Procedure Call eXternal Data Representation

Server Creation

gcc -o fooserver fooservices.c foo_svc.c foo_xdr.c –lrpcsvc -lnsl

fooservices.c contains the definitions of the actual remote procedures.

RPC Overview 24

Page 25: Lecture 20 Remote Procedure Call eXternal Data Representation

Example Protocol Definition

struct twonums {

int a;

int b;

};

program UIDPROG {

version UIDVERS {

int RGETUID(string<20>) = 1;

string RGETLOGIN( int ) = 2;

int RADD(twonums) = 3;

} = 1;

} = 0x20000001;RPC Overview 25

Page 26: Lecture 20 Remote Procedure Call eXternal Data Representation
Page 27: Lecture 20 Remote Procedure Call eXternal Data Representation

XDR: External Data Representation

XDR 27

Process AProcess A

XDR Encode/DecodeXDR Encode/Decode

TransportTransport

Process AProcess A

XDR Encode/DecodeXDR Encode/Decode

TransportTransport

Page 28: Lecture 20 Remote Procedure Call eXternal Data Representation

XDR

Powerful paradigm for creation and transfer of complex data structures

XDR provides a service associated with the OSI Presentation Layer. Common data representation Library

• not part of the O.S. Easy to port to new architectures Independence from transport layer

XDR 28

Page 29: Lecture 20 Remote Procedure Call eXternal Data Representation

Data Conversion

Asymmetric Data Conversion client always converts to the server’s data

representation.

Symmetric Data Conversion both client and server convert to/from some

standard representation.

XDR is Symmetric Data Conversion

XDR 29

Page 30: Lecture 20 Remote Procedure Call eXternal Data Representation

Implicit vs. Explicit Typing

Explicit typing each piece of data includes information

about the type

Implicit typing the sender and receiver must agree on the

order and type of all data

XDR uses Implicit Typing

XDR 30

Page 31: Lecture 20 Remote Procedure Call eXternal Data Representation

XDR Data Types

boolean char short int long float double

XDR 31

enumeration structure string fixed length array (1-D) variable length array (1-D) union opaque data

Page 32: Lecture 20 Remote Procedure Call eXternal Data Representation

XDR Programming

XDR libraries are based on a stream paradigm

The process of converting local data to XDR also puts the data in the XDR stream

When extracting an item from a stream, conversion is done to the local representation

Streams can be attached to a file, pipe, socket or memory Individual data items are added to (removed from)

the stream one at a time

XDR 32

Page 33: Lecture 20 Remote Procedure Call eXternal Data Representation

Conversion Terminology

Converting from local representation to XDR representation is called Encoding.

Converting from XDR representation to local representation is called Decoding.

XDR 33

SenderSender ReceiverReceiverXDRXDRENCODEENCODE DECODEDECODE

Page 34: Lecture 20 Remote Procedure Call eXternal Data Representation

XDR Stream Creation

There are a number of stream creation functions in the XDR library

xdrmem_create • destination for encoding -or- source for decoding

is a chunk of memory

xdrstdio_create • destination for encoding -or- source for decoding

is a file descriptor.

XDR 34

Page 35: Lecture 20 Remote Procedure Call eXternal Data Representation

XDR filters

The XDR library includes an extensive set of filters that perform encoding/decoding operations

Each XDR stream includes an attribute that determines the specific operation to be performed by a filter encoding or decoding

XDR 35

Page 36: Lecture 20 Remote Procedure Call eXternal Data Representation

XDR Filters

The filter to encode/decode an integer is called xdr_int:

bool_t xdr_int(XDR *xdrs, int *ip);

the return value indicates success. xdrs is a pointer to an XDR stream ip is a pointer to an integer.

XDR 36

Page 37: Lecture 20 Remote Procedure Call eXternal Data Representation

xdr_int() encode vs. decode

XDR 37

int count;xdr_int(xstream,&count);

int count;xdr_int(xstream,&count);

Source Encodes

Destination decodes

Page 38: Lecture 20 Remote Procedure Call eXternal Data Representation

Complex Data Filters

The XDR library includes a set of filters designed to translate complex C data structures to and

from XDR representation Many of these filters use simpler filters to

convert individual components xdr_array()

for encoding/decoding a variable length array

xdr_string() string conversion filter has a maximum size

XDR 38

Page 39: Lecture 20 Remote Procedure Call eXternal Data Representation

xdr_array() (cont.)

XDR 39

SourceArray

Dest.Array

elproc()

0 1 2 3

0 1 2 3

Page 40: Lecture 20 Remote Procedure Call eXternal Data Representation

XDR and RPC

RPC uses XDR, but the XDR filters are built for you automatically!

You define a complex data type a code generator writes an XDR filter that can

encode/decode your data type

Sometimes you need to understand XDR anyway...

XDR 40

Page 41: Lecture 20 Remote Procedure Call eXternal Data Representation
Page 42: Lecture 20 Remote Procedure Call eXternal Data Representation

RPC Programming with rpcgenIssues:

Protocol Definition File Client Programming

• Creating an "RPC Handle" to a server• Calling client stubs

Server Programming• Writing Remote Procedures

RPC Programming 42

Page 43: Lecture 20 Remote Procedure Call eXternal Data Representation

Protocol Definition File

Description of the interface of the remote procedures. Almost function prototypes

Definition of any data structures used in the calls (argument types & return types)

Can also include shared C code (shared by client and server).

Netprog: RPC Programming 43

Page 44: Lecture 20 Remote Procedure Call eXternal Data Representation

XDR the language

XDR data types are not C data types! There is a mapping from XDR types to C

types

• that's most of what rpcgen does.

Most of the XDR syntax is just like C Arrays, strings are different.

RPC Programming 44

Page 45: Lecture 20 Remote Procedure Call eXternal Data Representation

XDR Arrays

Fixed Length arrays look just like C code:

int foo[100]

Variable Length arrays look like this:

int foo<> or int foo<MAXSIZE>

RPC Programming 45

Implicit maximum size is 232-1

Page 46: Lecture 20 Remote Procedure Call eXternal Data Representation

What gets sent on the networkint x[n]

RPC Programming 46

x0 x1

int y<m>int y<m>

xn-1x2 . . .

y0 y1 . . .k

k is actual array sizek m

y2 yk

Page 47: Lecture 20 Remote Procedure Call eXternal Data Representation

XDR String Type

Look like variable length arrays:string s<100>

What is sent: length followed by sequence of ASCII chars:

RPC Programming 47

. . .n s0s1s2s3 Sn-1

n is actual string length (sent as int)

Page 48: Lecture 20 Remote Procedure Call eXternal Data Representation

Linked Lists!

struct foo {

int x;

foo *next;

}

The generated XDR filter uses xdr_pointer() to encode/decode the stuff pointed to by a pointer

RPC Programming 48

rpcgen recognizes this as a linked list

Page 49: Lecture 20 Remote Procedure Call eXternal Data Representation

Declaring The Program

program SIMP_PROG {

version SIMP_VERSION {

type1 PROC1(operands1) = 1;

type2 PROC2(operands2) = 2;

} = 1;

} = 40000000;

RPC Programming 49

Keywords Generated Symbolic Constants

Used to generate stub and procedure names

Color Code:

Page 50: Lecture 20 Remote Procedure Call eXternal Data Representation

Procedure Numbers

Procedure #0 is created for you automatically. Start at procedure #1!

Procedure #0 is a dummy procedure that can help debug things sort of an RPC ping server

RPC Programming 50

Page 51: Lecture 20 Remote Procedure Call eXternal Data Representation

Procedure Names

Rpcgen converts to lower case and prepends underscore and version number:

rtype PROCNAME(arg)

Client stub:rtype *proc_1(arg *, CLIENT *);

Server procedure: rtype *proc_1_svc(arg *,

struct svc_req *);RPC Programming 51

Page 52: Lecture 20 Remote Procedure Call eXternal Data Representation

Client Programming

You can find the porgram numbers currently used with "rpcinfo –p hostname“

Create RPC handle. Establishes the address of the server.

RPC handle is passed to client stubs (generated by rpcgen).

Type is CLIENT * RPC Programming 52

Page 53: Lecture 20 Remote Procedure Call eXternal Data Representation

clnt_create

CLIENT *clnt_create(

char *host,

u_long prog,

u_long vers,

char *proto);

RPC Programming 53

Hostname of server

Program number

Version number

Can be "tcp" or "udp"

Page 54: Lecture 20 Remote Procedure Call eXternal Data Representation

Calling Client Stubs

Remember: Return value is a pointer to what you expect. Argument is passed as a pointer. If you are passing a string, you must pass a char**

When in doubt – look at the ".h" file generated by rpcgen

RPC Programming 54

Page 55: Lecture 20 Remote Procedure Call eXternal Data Representation

Server Procedures

Rpcgen writes most of the server. You need to provide the actual remote

procedures. Look in the ".h" file for prototypes. Run "rpcgen –C –Ss" to generate

(empty) remote procedures!

RPC Programming 55

Page 56: Lecture 20 Remote Procedure Call eXternal Data Representation

Running rpcgen

Command line options vary from one OS to another.

Sun/BSD/Linux – you need to use "-C" to get ANSI C code!

Rpcgen can help write the files you need to write: To generate sample server code: "-Ss" To generate sample client code: "-Sc"

RPC Programming 56

Page 57: Lecture 20 Remote Procedure Call eXternal Data Representation

RPC without rpcgen

Can do asynchronous RPC Callbacks Single process is both client and server.

Write your own dispatcher and provide concurrency

Can establish control over many network parameters protocols, timeouts, resends, etc.

Netprog: RPC Programming 57

Page 58: Lecture 20 Remote Procedure Call eXternal Data Representation

rpcinfo

rpcinfo –p host prints a list of all registered programs on host.

rpcinfo –[ut] host program#

makes a call to procedure #0 of the specified RPC program (RPC ping)

RPC Programming 58

u : UDPt : TCP

Page 59: Lecture 20 Remote Procedure Call eXternal Data Representation

Sample Code

Simple integer add and subtract

ulookup look up username and uid

varray variable length array example

linkedlist arg is linked list

RPC Programming 59

Page 60: Lecture 20 Remote Procedure Call eXternal Data Representation

Example simp

Standalone program simp.c Takes 2 integers from command line and

prints out the sum and difference. Functions:

int add( int x, int y );

int subtract( int x, int y );

RPC Programming 60

Page 61: Lecture 20 Remote Procedure Call eXternal Data Representation

Splitting simp.c

Move the functions add() and subtract() to the server.

Change simp.c to be an RPC client Calls stubs add_1() , subtract_1()

Create server that serves up 2 remote procedures add_1_svc() and subtract_1_svc()

RPC Programming 61

Page 62: Lecture 20 Remote Procedure Call eXternal Data Representation

Protocol Definition: simp.x

struct operands { int x; int y;};program SIMP_PROG { version SIMP_VERSION { int ADD(operands) = 1; int SUB(operands) = 2; } = VERSION_NUMBER;} = 555555555;

RPC Programming 62

Page 63: Lecture 20 Remote Procedure Call eXternal Data Representation

rpcgen –C simp.x

RPC Programming 63

simp.xsimp.x

rpcgen

simp_clnt.csimp_clnt.c

simp_xdr.csimp_xdr.csimp.hsimp.h

simp_svc.csimp_svc.cClient Stubs

XDR filtersheader file

Server skeleton

Page 64: Lecture 20 Remote Procedure Call eXternal Data Representation

xdr_operands XDR filter

bool_t xdr_operands( XDR *xdrs, operands *objp){

if (!xdr_int(xdrs, &objp->x))

return (FALSE);

if (!xdr_int(xdrs, &objp->y))

return (FALSE);

return (TRUE);

}

RPC Programming 64

Page 65: Lecture 20 Remote Procedure Call eXternal Data Representation

simpclient.c

This was the main program – is now the client.

Reads 2 ints from the command line. Creates a RPC handle. Calls the remote add and subtract

procedures. Prints the results.

RPC Programming 65

Page 66: Lecture 20 Remote Procedure Call eXternal Data Representation

simpservice.c

The server main is in simp_svc.c. simpservice.c is what we write – it

holds the add and subtract procedures that simp_svc will call when it gets RPC requests.

The only thing you need to do is to match the name/parameters that simp_svc expects (check simp.h!).

RPC Programming 66

Page 67: Lecture 20 Remote Procedure Call eXternal Data Representation

Userlookup program

Provide access to passwd database via remote procedures:getpwnam BYNAMEgetpwuid BYNUM

RPC Programming 67

Unix library functions Remote Procedures

Page 68: Lecture 20 Remote Procedure Call eXternal Data Representation

userlookup.x

typedef string username<10>;

program ULKUP_PROG {

version ULKUP_VERSION {

int byname(username) = 1;

username bynum(int) = 2;

} = 1;

} = 555555556;

RPC Programming 68

Page 69: Lecture 20 Remote Procedure Call eXternal Data Representation

Problem with userlookup

It's hard to tell if there are errors: What if there is no user with the name

passed to byname() ? What if the username passed is not a valid

username?

RPC Programming 69

Page 70: Lecture 20 Remote Procedure Call eXternal Data Representation

Better userlookup.h

%#define NOTFOUND 0%#define FOUND 1

typedef string username<10>;

struct uname_retval { int found;

username name;};

RPC Programming 70

Page 71: Lecture 20 Remote Procedure Call eXternal Data Representation

Better userlookup.h (cont.)

struct uid_retval { int found; int uid; };program ULKUP_PROG { version ULKUP_VERSION { uid_retval byname(username) = 1; uname_retval bynum(int) = 2; } = 1;} = 555555556;

RPC Programming 71

Page 72: Lecture 20 Remote Procedure Call eXternal Data Representation

Varray example

Variable length array (determined at run time).

Remote procedure that returns the sum of the elements in an array of int.

RPC Programming 72

Page 73: Lecture 20 Remote Procedure Call eXternal Data Representation

varray.x

typedef int iarray<>;

program VADD_PROG { version VADD_VERSION { int VADD(iarray) = 1; } = 1;} = 555575555;

RPC Programming 73

Page 74: Lecture 20 Remote Procedure Call eXternal Data Representation

iarray

typedef struct {

u_int iarray_len;

int *iarray_val;

} iarray;

RPC Programming 74

typedef int iarray<>;

rpcgenrpcgen

Page 75: Lecture 20 Remote Procedure Call eXternal Data Representation

vadd_1_svc()

int * vadd_1_svc(iarray *argp, struct svc_req *rqstp) { static int result; int i; result=0; for (i=0;i<argp->iarray_len;i++) result += argp->iarray_val[i];

return (&result);}

RPC Programming 75

Page 76: Lecture 20 Remote Procedure Call eXternal Data Representation

linkedlist

Linked list of int.

Remote procedure computes sum of the integers in the list.

RPC Programming 76

Page 77: Lecture 20 Remote Procedure Call eXternal Data Representation

ll.x

struct foo { int x; foo *next;};

program LL_PROG { version LL_VERSION { int SUM(foo) = 1; } = VERSION_NUMBER;} = 555553555;

RPC Programming 77