Transcript RPC
CS6223: Distributed Systems
Remote Procedure Call (RPC)
An Example of Local Procedure Call
/* save a string to file msg.dat */
savemsg(char *msg) {
FILE *fp;
fp = fopen("msg.dat", "w+");
fprintf(fp, "%s\n", msg);
fclose (fp);
}
main() {
char *str = "This is an example of LPC.";
savemsg(str);
}
2
Moving the local procedure to a remote machine
server:
client:
…………
savemsg_1(char **argp; struct svc_req *rqstp)
{
FILE *fp;
fp = fopen("msg.dat", "w+");
fprintf(fp, "%s\n", *argp);
fclose (fp);
}
main() {
char *str = "This is an example of RPC.";
char *remote_host = "sus1.cs.cityu.edu.hk";
CLIENT *clnt;
clnt = clnt_create(remote_host, MSGPROG, MSGVER, "netpath");
if (clnt == (CLIENT *) NULL) {
clnt_pcreateerror(host); exit(1);
}
savemsg_1(&str, clnt);
}
3
Remote Procedure Call
1984: Birrell & Nelson
– Mechanism to call procedures on other machines
– Process on machine A can call a procedure on machine B
• A is suspended
• Execution continues on B
• When B returns, control passed back to A
Goal: Make a remote procedure call looking the same as a
local call to programmers.
4
RPC implementation
The RPC compiler auto-generates stub/skeleton routines to make an
RPC to the user as if it is a local call. What stub routines do:
client
client client
program stub
server
message passing
server service
skeleton routines
• marshalling / unmarshalling parameters and return values
• handling different data formats between different machines
• detecting and handling failures of client/server processes and the
networks
5
Compilation in SUN RPC
service routines
server skeleton (main)
interface
definition
RPC
compiler
C
compiler
server
executable
C
compiler
client
executable
data conversion
client stub
client program (main)
6
Demo of RPC
Interface file msg.x:
program MSGPROG {
version MSGVER{
int SAVEMSG(string)= 1;
string READMSG(int)= 2;
} = 2;
} = 345678;
Generate stub routines by:
rpcgen –a msg.x
Compile program by:
cc –o object xxx.c
7
Steps in a RPC
1. Client calls stub (push parameters onto stack)
client program
(msg_client.c)
service routines
client stub
(msg_clnt.c)
server skeleton
network routines
network routines
8
Steps in a RPC
2. Clnt_stub marshals parameters to message &
makes an OS call (client blocked)
client program
service routines
client stub
(msg_clnt.c)
server skeleton
network routines
network routines
9
Steps in a RPC
3. Network message sent to server
client program
service routines
client stub
server skeleton
network routines
network routines
10
Steps in a RPC
4. Deliver message to server stub & unblock server
client program
service routines
client stub
server skeleton
(msg_svr.c)
network routines
network routines
11
Steps in a RPC
5. Svr-stub unmarshals parameters &
calls service routine (local call)
client program
service routines
(msg_server.c)
client stub
server skeleton
(msg_svr.c)
network routines
network routines
12
Steps in a RPC
6. Return to the stub from service routine
client program
service routines
client stub
server skeleton
network routines
network routines
13
Steps in a RPC
7. Svr_stub marshals return-value &
requests OS to send out message
client program
service routines
client stub
server skeleton
network routines
network routines
14
Steps in a RPC
8. Transfer message over network
client program
service routines
client stub
server skeleton
network routines
network routines
15
Steps in a RPC
9. Deliver message to client (unblock client)
client program
service routines
client stub
server skeleton
network routines
network routines
16
Steps in a RPC
10. Clnt_stub unmarshals return-value &
returns to client program…
client program
service routines
client stub
server skeleton
network routines
network routines
17
Compilation in SUN RPC
service routines
server skeleton (main)
interface
definition
RPC
compiler
C
compiler
server
executable
C
compiler
client
executable
data conversion
client stub
client program (main)
18
Writing the programs
Programmers need to write two pieces of programs:
• Client program
– Specify server’s location
– Parameters and return value of RPC are pointers
• Service routines
– Generally the same as local procedures, except the parameters and
parameter types
19
Interface definition (1)
• Define machine/OS/language-independent data types and data structures
• Specify interfaces: program # and version #
• Define remote procedures: procedure #, parameters, return type
constant and type definitions /*optional */
program IDENTIFIER {
version VERSION_ID {
procedure list
} = value;
…
} = value;
program MSGPROG {
version MSGVER{
int SAVEMSG(string)= 1;
string READMSG(int)= 2;
} = 3;
} = 345678;
20
Interface definition (2)
•
•
•
•
one program block in an interface definition
one or more sets of versions in the program block
one or more sets of procedures in each version block
one argument in each procedure by default
– If more than one parameter is required, use a struct to group them
– “–N” option in “rpcgen” allows multiple arguments
21
Incompatible data representation (1)
•
Different byte ordering
–
–
Big endian: Most significant byte in low memory (e.g. Sun Sparcs)
Little endian: Most significant byte in high memory (e.g. Pentiums)
main() {
unsigned int n;
char *a = (char *) &n;
n = 0x11223344;
printf("%02x, %02x, %02x, %02x\n",
a[0], a[1], a[2], a[3]);
Output on a Sparc:
11, 22, 33, 44
Output on a Pentium:
44, 33, 22, 11
}
The problem was solved in the IP protocol suite by forcing everyone
to use big endian byte ordering for all 16 and 32 bit fields by htons
(host-to-network-short) and htonl functions.
22
Incompatible data representation (2)
•
•
•
•
Different sizes of integers and other types
Different floating point representations
Different character sets
Alignment requirements
Need standard data representation and associated encoding
/decoding functions to enable communication between
heterogeneous systems
– e.g. Sun’s RPC uses XDR (eXternal Data Representation)
23
Interface Definition Language (IDL)
SUN XDR (eXternal Data Representation)
• A subset of ASN.1 (ITU), a standard data representation
for message exchanges between clients and servers.
• Data types and structs are translated into C language by
rpcgen (see XXX.h, generated by rpcgen).
• A set of encode/decode routines (see XXX_xdr.c) to
convert between XDR and the local representations.
24
Student registration system: regist.x
const MAXSUBJ = 64;
typedef char id[8];
typedef char code[8];
struct registargs {
code subj_code;
id stud_id;
};
program REGISTPROG {
version REGISTVER {
int REGIST(registargs) = 1;
int DEREGIST(registargs) = 2;
regist_status VIEW_STATUS(id) = 3;
} = 1;
} = 100023;
struct status {
code subj_code;
int regist_stat;
};
struct regist_status {
int
total_regist;
struct status subjs_status<MAXSUBJ>;
};
25
Important data types
simple data types: similar to C
array
fixed-length:
variable-length:
type-name identifier[n];
type-name identifier<n>;
type-name identifier<>;
array of strings
string identifier<n>;
string identifier<>;
different from array of char in representation (explained later)
26
Important data types
constant/enumeration/type defintion/struct: similar to C
const MAXSIZE = 512;
enum state { BUSY=1, IDLE=2, TRANSIT=3 };
typedef long counter;
typedef char code[8];
struct status {
code subj_code;
int
regist_stat;
};
27
Data representation/alignment (1)
• The basic data item size is 4 bytes. All variables or data
structures should be aligned in 4-bytes.
• Variable-length objects, e.g., variable-length arrays,
structures, and strings, have a length in front of them.
28
Data representation/alignment (2)
• simple data types
– int, unsigned int, char, bool, float, etc.: 4 bytes
– double, etc.: 8 bytes
• array
– sequence of representations of individual elements
– variable-length array has a 4-byte length in the front
• string
– an integer of string length, followed by a sequence of chars, one for a byte
– a residue of (n mod 4) bytes are stuffed to make the total byte count a
multiple of 4, e.g. string “exam-paper_01” is represented as:
4 bytes
13
4 bytes
4 bytes
e
x a
m
-
p a
4 bytes
4 bytes
p
e
r
_
0
1
\0
\0
\0
29