inter-process communication: rpchome.eng.iastate.edu/~guan/course/cpre-450-550/... · inter-process...

41
CprE 450-550 Dr. Yong Guan Department of Electrical and Computer Engineering & Information Assurance Center Iowa State University Inter-process Communication: RPC

Upload: others

Post on 24-Aug-2020

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

Dr. Yong Guan

Department of Electrical and Computer Engineering& Information Assurance CenterIowa State University

Inter-process Communication: RPC

Page 2: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

Outline for Today’s TalkInter-process Communication: RPC

Remote Procedure Call

Page 3: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

Readings for Today’s Lecture

Chapter 4 of “Distributed Systems: Principles and Paradigms”

Online resources and man page

Page 4: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

Middleware

Purpose of Middleware in Distributed Systems

RPC(Remote Procedure Call)•Introduction•RPC mechanisms•Design issues•Programming with RPC•Case study: SUN RPC (Live demo)

Java RMICORBA

Page 5: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

Purpose of Middleware in Distributed Systems

Middleware

Application

Distributed System

Server Server ServerServer

•Principles of transparency – Hide Lower level complexities•Ease the Application Development •Allows to focus on Application functionality

Page 6: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

Building Distributed Programs: Two Paradigms

Communication-Oriented Design Start with communication protocol Design message format and syntax Design client and server components

by specifying how they react to incoming messages

Focus on communication instead of application!

Design protocol first, then build applications that adhere to the protocol

Example: socket

Application-Oriented Design Start with application Design, build, test conventional

implementation Partition program

Focus on application program structure

Make communication “transparent” Example: RPC

Properties:

Paradigms:

Page 7: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

7

Introduction-RPC1984: Birrell & Nelson

Mechanisms to call procedures on other machinesProcesses on machine A can call procedures on machine B

A is suspendedExecution continues on BWhen B returns, control passed back to A

Goal: it appears to the programmer that a normal call is taking place

Page 8: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

Conventional Procedure Call

a) Parameter passing in a local procedure call: the stack before the call to read

b) The stack while the called procedure is active

count = read (fd, buf, bytes)

call-by-value

call-by-reference

Page 9: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

Remote Procedure Call

Allow programs to call procedures located on other machines• Information can be transported from the caller to the callee in the

parameters and come back in the procedure result.• RPC enables clients to communicate with servers by calling procedures

in a similar way to the conventional use of procedure calls in high-level languages.

• Transparency: no message passing is visible to the programmer.

Challenges:• Different machines• Different address spaces• Both machines can crash

Page 10: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

RPC Model: Client (caller) and Server (callee)Principle of RPC between a client and server program

call remoteproc A

call remoteproc B

respond to caller

exit

main programon machine 1

procedure Aon machine 2

procedure Bon machine 3

respond to caller

Page 11: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

Asynchronous RPC

a) The interconnection between client and server in a traditional RPCb) The interaction using asynchronous RPC

2-12

Page 12: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

Structure of an RPC Call

client

client stub

RPC library(network routines)

server

server stub

RPC library(Network Routines)

1

2

3

8

7

6 5

49

10

packingparameters

unpackingparameters

packing /unpacking

result

RPC

Packaging/unpackaging of arguments into network messages is called marshaling/unmarshaling.

Page 13: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

Steps of a Remote Procedure Call

1. Client procedure calls client stub in normal way2. Client stub builds message, calls local OS3. Client's OS sends message to remote OS4. Remote OS gives message to server stub5. Server stub unpacks parameters, calls server6. Server does work, returns result to the stub7. Server stub packs it in message, calls local OS8. Server's OS sends message to client's OS9. Client's OS gives message to client stub10. Stub unpacks result, returns to client

Page 14: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

Remote Procedure Call CS-4513, D-Term 2007 14

RPC StubsA client-side stub is a function that looks to the client as if it were a callable server function

i.e., same API as the server’s implementation of the functionA server-side stub looks like a caller to the server

i.e., like a hunk of code invoking the server functionThe client program thinks it’s invoking the server

but it’s calling into the client-side stubThe server program thinks it’s called by the client

but it’s really called by the server-side stubThe stubs send messages to each other to make the RPC happen transparently (almost!)

Page 15: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

Remote Procedure Call CS-4513, D-Term 2007 15

Marshalling ArgumentsMarshalling is the packing of function parameters into a message packet

the RPC stubs call type-specific functions to marshal or unmarshal the parameters of an RPC

Client stub marshals the arguments into a messageServer stub unmarshals the arguments and uses them to invoke the service function

on return:the server stub marshals return valuesthe client stub unmarshals return values, and returns to the client program

Page 16: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

Remote Procedure Call CS-4513, D-Term 2007 16

RPC ModelA server defines the service interface using an interface definition language (IDL)

the IDL specifies the names, parameters, and types for all client-callable server procedures

A stub compiler reads the IDL declarations and produces two stub functions for each server function

Server-side and client-sideLinking:–

Server programmer implements the service’s functions and links with the server-side stubsClient programmer implements the client program and links it with client-side stubs

Operation:–Stubs manage all of the details of remote communication between client and server

Page 17: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

RPCs: Issues of Design

•What are semantics of parameter passing?•E.g., pass by reference?

•Binding - How to bind (locate & connect) to servers?

•Exception Handling

•Performance and security

•Data Representation - How to handle heterogeneity?•OS, language, architecture, …

Page 18: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

Parameter Passing

Passing value parameters Parameter marshalling

Passing reference parameters• How to pass pointers (i.e., references), that are not meaningful on the server• Possible solution:

Case: pointer to an array• If the size of the array is known, we can copy the array into a message and

send it to the server.• In this case, call-by-reference is replaced by copy/restore.• We still cannot handle the most general case of a pointer to an arbitrary data

structures (complex graph).

Page 19: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

Passing Value Parameters

Steps involved in doing remote computation through RPC

2-8

Page 20: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

Remote Procedure Call CS-4513, D-Term 2007 20

RPC BindingBinding is the process of connecting the client to the server

the server, when it starts up, exports its interfaceidentifies itself to a network name servertells RPC runtime that it is alive and ready to accept calls

the client, before issuing any calls, imports the serverRPC runtime uses the name server to find the location of the server and establish a connection

The import and export operations are explicit in the server and client programs

Page 21: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

Locating ServersBroadcast requests

Broadcast call and process incoming replies

Name serversServer registers with name server

stubclient

name server

stubserver

register

stubclient

name server

stubserver

publishsubscribe

Publish & Subscribe

Occurs only once

May occur more than once

Flexible

Page 22: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

Remote Procedure Call CS-4513, D-Term 2007 22

Result (RPC Advantages)Writing applications is simplified

RPC hides all networks codesProgrammers don’t have to worry about details (sockets, port numbers, byte ordering)

The hard work of building messages, formatting, uniform representation, etc., is buried in the stubs

Client and server designers can concentrate on the semantics of application

Programs behave in familiar way – Familiar Procedure call interface

Page 23: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

Case Study: SUN RPC

rpcgen compiler

Takes IDL as input and generate stub functions.

Format of messages, arguments, and results.

XDR (eXternal Data Representation).

RPC run-time library

How to locate server and server application port.

shared data

proc A proc B proc C

remote program

Page 24: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

Defines service interface between client and serverA language for specifying –

•operations (procedures or functions)•parameters to these operations,•other optional data types

/** date.x - Specification of remote date, time, date and time service.*/

program DATE_PROG {version DATE_VERS {

string DATE(long) = 1; /* procedure number = 1 */} = 1; /* version number = 1 */

} = 0x31234567

Interface definition language (IDL)

Page 25: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

Conceptually, each procedure on a computer is identified by pair : ( prog, proc )

prog: 32-bit integer identifying remote programproc: integer identifying procedure

Set of program numbers partitioned into 4 sets.

0x00000000 - 0x1fffffff assigned by SUN0x20000000 - 0x3fffffff assigned by local system manager0x40000000 - 0x5fffffff temporary0x60000000 - 0xffffffff reserved

Multiple remote program versions can be identified:( prog, version, proc )

Identifying Remote Programs and Procedures

Page 26: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

RPC Interaction

2. Client contacts portmapper to determine service port.3. Client contacts server on the specified service port.

Page 27: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

Remote Programs and Protocol Ports

Dynamic port mapping: RPC port mapper

caller remote programport

program_id vs. port_id

(32 bit) (16 bit)

RPC program

xx

portmapper

RPC program registers

(xx, p)

p 111

port currently usedby this RPC program

well-known portfor port manager

You must know theserver address first.

Page 28: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

Comparison: Binding service in DCE RPC

DCE: A directory server help to locate RPC server.SUN: You should specify the RPC server.

2-15

Page 29: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

XDR - eXternal Data Representation

XDR is a universally used standard from Sun Microsystems used to represent data in a network canonical form.

A set of conversion functions are used to encode and decode data; for example, xdr_int( ) is used to encode and decode integers. Data is converted into a network canonical form (a standard form) to be presented in a meaningful format to the receiving host.

Conversion functions exist for all standard data types. However, for complex structures, RPCGEN is used to generate conversion routines.

Page 30: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

Sun RPC Message Format: XDR Specification

enum msg_type { /* RPC message type constants */CALL = 0;REPLY = 1;

};

struct rpc_msg { /* format of a RPC message */unsigned int mesgid; /* used to match reply to call */union switch (msg_type mesgt) {

case CALL : call_body cbody;case REPLY: reply_body rbody;

} body;};

struct call_body { /* format of RPC CALL */u_int rpcvers; /* which version of RPC? */u_int rprog; /* remote program number */u_int rprogvers; /* version number of remote prog */u_int rproc; /* number of remote procedure */opaque_auth cred; /* credentials for called auth. */opaque_auth verf; /* authentication verifier *//* ARGS */

};

Page 31: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

Creating Distributed Applications with Sun RPC Example: Remote Dictionary Using rpcgen

Procedure call structure:

main

init_dic

insertw deletew

lookupwnextin

rdict1.crdict2.c

Page 32: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

Specification for rpcgen

Interface:*.x file

Specify:constantsdata typesremote programs, their procedures, types of parameters

/* rdict.x *//* RPC declarations for dictionary program */const MAXWORD = 50;const DICTSIZ = 100;struct example { /* unused; rpcgen would */int exfield1; /* generate XDR routines */char exfield2; /* to convert this structure.*/

};

/* RDICTPROG: remote program that provides insert, delete, and lookup */

program RDICTPROG { /* name (not used) */version RDICTVERS { /* version declarat.*/

int INITW(void) = 1;/* first procedure */int INSERTW(string)= 2;/* second proc.... */int DELETEW(string)= 3;int LOOKUP(string) = 4;

} = 1; /* version definit.*/} = 0x30090949; /* program no */

/* (must be unique)*/

Page 33: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

Program Generation

rpcgen rdict.x

rdict.h• constants, data types• definitions for remote procedures

rdict_xdr.c• XDR conversion routines

rdict_clnt.c• client stub

rdict_svc.c• server stub

rdict.x

rdict_clnt.c

rdict.h

rdict_xdr.c

rdict_svc.c

rdict_sif.c rdict2.c

cc

cc

rdict_cif.c rdict1.c

rdict (client)

rdictd

rpcgen

Page 34: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

Example: Date.x/** date.x - Specification of remote date, time, date and time service.*/

/** Define 1 procedure :* date_1() accepts a long and returns a string. */

program DATE_PROG {version DATE_VERS {

string DATE(long) = 1; /* procedure number = 1 */} = 1; /* version number = 1 */

} = 0x31234567; /* program number */

Page 35: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

#include <stdio.h>#include <string.h>#include <time.h>#include <sys/types.h>#include <rpc/rpc.h> /* standard RPC include file */#include "date.h" /* this file is generated by rpcgen */

#define MAX_LEN 100long get_response(void);

long get_response(){

long choice;

printf("===========================================\n");printf(" Menu: \n");printf("-------------------------------------------\n");printf(" 1. Date\n");printf(" 2. Time\n");printf(" 3. Both\n");printf(" 4. Quit\n");printf("-------------------------------------------\n");printf(" Choice (1-4):");scanf("%ld",&choice);printf("===========================================\n");return(choice);

}

Client.c

Page 36: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550main(int argc, char **argv){

CLIENT *cl; /* RPC handle */char *server;char **sresult; /* return value from date_1() */char s[MAX_LEN]; /* character array to hold output */long response; /* user response */long *lresult; /* pointer to user response */

if (argc != 2) {fprintf(stderr, "usage: %s hostname\n", argv[0]);exit(1);

}server = argv[1];lresult = (&response);/** Create the client "handle."*/

if ( (cl = clnt_create(server, DATE_PROG, DATE_VERS, "udp")) == NULL) {clnt_pcreateerror(server);exit(2);

}response = get_response();while(response != 4) {

if ((sresult = date_1(lresult, cl)) == NULL) {clnt_perror(cl, server);exit(3);

}printf(" %s\n", *sresult);response = get_response();

}clnt_destroy(cl); /* done with the handle */exit(0);

}

Client.c (cont.)Pointer of data

type defined in x file

Specify server address

Create client handle

Call ProcName_VerNamewith client handle

Page 37: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550/** date_proc.c - remote procedures; called by server stub.*/

#include <rpc/rpc.h> /* standard RPC include file */#include <time.h>#include <sys/types.h>#include "date.h" /* this file is generated by rpcgen */

#define MAX_LEN 100/** Return the binary date and time.*/

char ** date_1(long *option){

struct tm *timeptr; /* Pointer to time structure */time_t clock; /* Clock value (in secs) */static char *ptr; /* Return string */static char err[] = "Invalid Response \0";static char s[MAX_LEN];

clock = time(0);timeptr = localtime(&clock);

switch(*option){case 1: strftime(s,MAX_LEN,"%A, %B %d, %Y",timeptr);

ptr=s;break;

case 2: strftime(s,MAX_LEN,"%T",timeptr);ptr=s;break;

case 3: strftime(s,MAX_LEN,"%A, %B %d, %Y - %T",timeptr);ptr=s;break;

default: ptr=err;break;

}return(&ptr);

}

Server.c

Pointers of datatype defined in x file

Page 38: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

Program Generation

IDL

Client stub

Headers

xdr routine

Server stub

Server Program

cc

cc

Client Program

Client Executable

Server Executable

rpcgencompiler

Page 39: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

Steps to Compile and Run

ssh to linux-4.ece.iastate.edu with your NetID and password

Save date.x, client.c and server.c into some directory.

Under the directory, execute:

rpcgen –k date.x (-k generates code in K&R C. Needed in this machine!)

gcc –o client client.c date_clnt.c

gcc –o server server.c date_svc.c

./server & (run server at back end)

./client localhost (run client with the server on localhost)

kill server_pid (kill the server)

Page 40: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

Project - 1

Remote System Monitoring using RPC –•Current System time•CPU Usage•Memory Usage •Load Procs per minute

Page 41: Inter-process Communication: RPChome.eng.iastate.edu/~guan/course/CprE-450-550/... · Inter-process Communication: RPC. CprE 450-550 Outline for Today’s Talk Inter-process Communication:

CprE 450-550

Thank you!