Bfcai Ds Lecture 2 1
Bfcai Ds Lecture 2 1
3/5/2023
RPC
1984: Birrell & Nelson
• Mechanism to call procedures on other machines
3/5/2023
How do regular
procedure calls work
in programming
languages?
Regular procedure calls
Machine instructions for call & return but the
compiler really makes the procedure call abstraction
work:
• Parameter passing
• Local variables
• Return data
3/5/2023
Regular procedure calls
You write:
x = f(a, “test”, 5);
3/5/2023
Implementing RPC
No architectural support for remote procedure calls
Simulate it with tools we have
(local procedure calls)
3/5/2023
Implementing RPC
The trick:
3/5/2023
Stub functions
1. Client calls stub (params on stack)
server stub
client stub
(skeleton)
client server
Stub functions
2. Stub marshals params to net message
server stub
client stub
(skeleton)
client server
Stub functions
3. Network message sent to server
server stub
client stub
(skeleton)
client server
Stub functions
4. Receive message: send to stub
server stub
client stub
(skeleton)
client server
Stub functions
5. Unmarshal parameters, call server func
server stub
client stub
(skeleton)
client server
Stub functions
6. Return from server function
server stub
client stub
(skeleton)
client server
Stub functions
7. Marshal return value and send message
server stub
client stub
(skeleton)
client server
Stub functions
8. Transfer message over network
server stub
client stub
(skeleton)
client server
Stub functions
9. Receive message: direct to stub
server stub
client stub
(skeleton)
client server
Stub functions
10. Unmarshal return, return to client code
server stub
client stub
(skeleton)
client server
Benefits
• Procedure call interface
3/5/2023
RPC has issues
Parameter passing
Pass by value
• Easy: just copy data to network message
Pass by reference
• Makes no sense without shared memory
3/5/2023
Passing Value Parameters
3/5/2023
Pass by reference?
1. Copy items referenced to message buffer
2. Ship them over
3. Unmarshal data at server
4. Pass local pointer to server stub function
5. Send new values back
3/5/2023
Representing data
No such thing as
incompatibility problems on local system
3/5/2023
Representing data
IP (headers) forced all to use big endian byte ordering
for 16 and 32 bit values
• Most significant byte in low memory
• Sparc, 680x0, MIPS, PowerPC G5
• x86/Pentiums use little endian
3/5/2023
Representing data
Need standard encoding to enable communication
between heterogeneous systems
• e.g. Sun’s RPC uses XDR (eXternal Data Representation)
• ASN.1 (ISO Abstract Syntax Notation)
3/5/2023
Representing data
Implicit typing
• only values are transmitted, not data types or parameter
info
• e.g., Sun XDR
Explicit typing
• Type is transmitted with each value
• e.g., ISO’s ASN.1, XML
3/5/2023
Where to bind?
Need to locate host and correct server process
3/5/2023
Where to bind? – Solution 1
Maintain centralized DB that can locate a
host that provides a particular service
(Birrell & Nelson’s 1984 proposal)
3/5/2023
Where to bind? – Solution 2
A server on each host maintains a DB of locally
provided services
3/5/2023
Transport protocol
Which one?
3/5/2023
When things go wrong
• Local procedure calls do not fail
• If they core dump, entire process dies
3/5/2023
When things go wrong
• Semantics of remote procedure calls
• Local procedure call: exactly once
3/5/2023
More issues
Performance
• RPC is slower … a lot slower
Security
• messages visible over network
• Authenticate client
• Authenticate server
3/5/2023
Programming with RPC
Language support
• Most programming languages (C, C++, Java, …) have no
concept of remote procedure calls
• Language compilers will not generate client and server
stubs
Common solution:
• Use a separate compiler to generate stubs (pre-
compiler)
3/5/2023
Interface Definition Language
• Allow programmer to specify remote procedure
interfaces
(names, parameters, return values)
3/5/2023
RPC compiler
client code (main)
client stub
server skeleton
Code you write
3/5/2023
Writing the program
Client code has to be modified
• Initialize RPC-related options
• Transport type
• Locate server/service
• Handle failure of remote procedure call
Server functions
• Generally need little or no modification
3/5/2023
RPC API
What kind of services does an RPC system need?
• Name service operations
• Export/lookup binding information (ports, machines)
• Support dynamic ports
• Binding operations
• Establish client/server communications using appropriate
protocol (establish endpoints)
• Endpoint operations
• Listen for requests, export endpoint to name server
3/5/2023
RPC API
What kind of services does an RPC system need?
• Security operations
• Authenticate client/server
• Internationalization operations
• Marshaling/data conversion operations
• Stub memory management
• Dealing with “reference” data, temporary buffers
• Program ID operations
• Allow applications to access IDs of RPC interfaces
3/5/2023
RPC in Practice - DCE RPC
• The Distributed Computing Environment (DCE) RPC is
developed by the Open Software Foundation
(OSF)/Open Group.
• DCE is a middleware executing as an abstraction
layer between (network) operating systems and
distributed applications.
• Microsoft derived its version of RPC from DCE RPC
(e.g., MIDL compiler, etc.)
• DCE includes a number of services:
• Distributed file service
• Directory service
• Security service
3/5/2023
• Distributed time service
DCE RPC
• The main goal of the DCE RPC is to make it possible
for a client to access a remote service by simply
calling a local procedure.
• Developing a RPC application:
• Writing a client and a server: Let the developer
concentrate on only the client- and server-specific code;
let the RPC system (generators and libraries) do the rest.
Interface Definition Language (IDL) is used to specify the
variables (data) and functions (methods).
• Binding a client to a server
• Performing an RPC
3/5/2023
Writing a Client and a Server
2-14
3/5/2023
Sun RPC/ONC (Open Network Computing) RPC
3/5/2023
Sun/ONC RPC
3/5/2023
Interface Definition Language
3/5/2023
Files interface in ONC IDL
const MAX = 1000;
typedef int FileIdentifier;
typedef int FilePointer;
typedef int Length; struct readargs {
struct Data { FileIdentifier f;
int length; FilePointer position;
char buffer[MAX]; Length length;
}; };
struct writeargs {
FileIdentifier f; program FILEREADWRITE {
FilePointer position; version VERSION {
Data data; void WRITE(writeargs)=1; 1
}; Data READ(readargs)=2; 2
}=2;
} = 9999;
Sun/ONC RPC
• The interface compiler rpcgen can be used to generate
the following from the interface definition.
client stub procedures (sum_clnt.c)
server main procedure, dispatcher (system calls) and server
stub procedures (sum_svc.c)
XDR marshalling and unmarshalling procedures used by
dispatcher and client, server stub procedures. (sum_xdr.c)
• Binding:
portmapper records program number, version number, and
port number.
If there are multiple instance running on different machines,
clients make multicast remote procedure calls by
broadcasting them to all the port mappers.
3/5/2023
ONC RPC Interface Compiler
ONC RPC
• Authentication:
Additional fields are provided in the request-reply
message.
Server program should check the authentication and then
execute.
Different authentication protocols can be supported -
none, UNIX style, shared key, Kerberos style.
A field in RPC header indicates which style is used.
3/5/2023
Example (ONC RPC)
• long sum(long) example
client localhost 10
result: 55
• Need RPC specification file (sum.x)
defines procedure name, arguments & results
• Run (interface compiler) rpcgen sum.x
generates sum.h, sum_clnt.c, sum_xdr.c, sum_svc.c
sum_clnt.c & sum_svc.c: Stub routines for client & server
sum_xdr.c: XDR (External Data Representation) code takes
care of data type conversions
3/5/2023
RPC IDL File (sum.x)
struct sum_in {
long arg1;
};
struct sum_out {
long res1;
};
program SUM_PROG {
version SUM_VERS {
sum_out SUMPROC(sum_in) = 1; /* procedure number = 1*/
} = 1; /* version number = 1 */
} = 0x32123000; /* program number */
3/5/2023
Example (Sun RPC)
• Program-number is usually assigned as follows:
0x00000000 - 0x1fffffff defined by SUN
0x20000000 - 0x3fffffff defined by user
0x40000000 - 0x5fffffff transient
0x60000000 - 0xffffffff reserved
3/5/2023
RPC Client Code (rsum.c)
#include ''sum.h''
main(int argc, char* argv[]) {
CLIENT* cl; sum_in in; sum_out *outp;
// create RPC client handle; need to know server's address
cl = clnt_create(argv[1], SUM_PROG, SUM_VERS, ''tcp'');
in.arg1 = atol(argv[2]); // number to be squared
// Call RPC; note convention of RPC function naming
if ( (outp = sumproc_1(&in, cl)) == NULL)
err_quit(''%s'', clnt_sperror(cl, argv[1]);
printf(''result: %ld\n'', outp->res1);
}
3/5/2023
RPC Server Code (sum_serv.c)
#include "sum.h"
sum_out* sumproc_1_svc (sum_in *inp, struct svc_req *rqstp)
{ // server function has different name than client call
static sum_out out;
int i;
out.res1 = inp->arg1;
for (i = inp->arg1 - 1; i > 0; i--)
out.res1 += i;
return(&out);
}
// server's main() is generated by rpcgen
3/5/2023
Compilation Linking
• rpcgen sum.x
• cc -c rsum.c -o rsum.o
• cc -c sum_clnt.c -o sum_clnt.o
• cc -c sum_xdr.c -o sum_xdr.o
• cc -o client rsum.o sum_clnt.o sum_xdr.o
• cc -c sum_serv.c -o sum_serv.o
• cc -c sum_svc.c -o sum_svc.o
• cc -o server sum_serv.o sum_svc.o sum_xdr.o
3/5/2023
Internal Details of ONC RPC
• Initialization
Server runs: register RPC with port mapper on server host
(rpcinfo –p)
Client runs: clnt_create contacts server's port mapper and
establishes TCP/UDP connection with server
• Client
Client calls local procedure (client stub: sumproc_1), that is
generated by rpcgen. Client stub packages arguments, puts
them in standard format (XDR), and prepares network
messages (marshaling).
Network messages are sent to remote system by the client
stub.
Network transfer is accomplished with TCP or UDP.
3/5/2023
Internal Details of ONC RPC
• Server
Server stub (generated by rpcgen) unmarshals arguments
from network messages. Server stub executes local
procedure (sumproc_1_svc) passing arguments received
from network messages.
When server procedure is finished, it returns to server stub
with return values.
Server stub converts return values (XDR), marshals them
into network messages, and sends them back to client
• Back to Client
Client stub reads network messages from kernel
Client stub returns results to client function
3/5/2023
Sending data over the network
62
Stream of bytes
struct item { char name[64];
unsigned long id; int number_in_stock; float rating; double
price;
} scratcher = {
3/5/2023 63
Representing data
No such thing as
incompatibility problems on local system
3/5/2023 64
Representing data
IP (headers) forced all to use big endian byte ordering for 16- and 32-bit values
Big endian: Most significant byte in low memory
IP headers use big endian
– SPARC < V9, Motorola 680x0, older PowerPC
main() {
unsigned int n; Output on an Intel CPU:
44, 33, 22, 11
char *a = (char *)&n;
3/5/2023 65
Representing data: serialization
Need standard encoding to enable communication between
heterogeneous systems
•Serialization
– Convert data into a pointerless format: an array of bytes
•Examples
– XDR (eXternal Data Representation), used by ONC RPC
– JSON (JavaScript Object Notation)
– W3C XML Schema Language
– ASN.1 (ISO Abstract Syntax Notation)
– Google Protocol Buffers
3/5/2023 66
Serializing data
Implicit typing
–only values are transmitted, not data types or parameter info
–e.g., ONC XDR (RFC 4506)
Explicit typing
–Type is transmitted with each value
–e.g., ISO’s ASN.1, XML, protocol buffers, JSON
Serialization: converting an object into a sequence of bytes that can be sent over
a network
3/5/2023 67
XML: eXtensible Markup Language
<ShoppingCart>
<Items>
<Item>
<ItemID> 00120 </ItemID>
<Item> Bear Claw Black Telescopic Back Scratcher </Item>
<Price> 5.99 </Price>
/<Item>
<item>
<ItemID> 00121 </ItemID>
<Item> Scalp Massager </Item>
<Price> 5.95 </Price>
/<Item>
/<Items>
/<ShoppingCart>
Benefits: Problems:
– Human-readable –Verbose: transmit more data than needed
– Human-editable –Longer parsing time
– Interleaves structure with text (data) –Data conversion always required for
numbers
3/5/2023 68
JSON: JavaScript Object Notation
• Lightweight (relatively efficient) data interchange format
– Introduced as the “fat-free alternative to XML”
– Based on JavaScript
• Human writeable and readable
• Self-describing (explicitly typed)
• Language independent
• Easy to parse
• Currently converters for 50+ languages
• Includes support for RPC invocation via JSON-RPC
3/5/2023 69
JSON Example
{"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem[ :"
"{value": "New," "onclick": "CreateNewDoc,}")(
"{value:" "Open", "onclick": "OpenDoc,}")(
"{value:" "Close", "onclick": "CloseDoc}")(
]
}
}}
from json.org/example.html
3/5/2023 70
Google Protocol Buffers
• Efficient mechanism for serializing structured data
– Much simpler, smaller, and faster than XML
• Language independent
• Define messages
– Each message is a set of names and types
3/5/2023 72
Example (from the Developer Guide)
3/5/2023 73
Efficiency example (from the Developer Guide)
<person> person{
<name>John Doe</name> name: "John Doe"
<email>[email protected]</email> email: "[email protected]"
/<person> }
3/5/2023 74
Advantages of Remote Procedure Call
Some of the advantages of RPC are as follows −
• Remote procedure calls support process oriented
and thread oriented models.
• The internal message passing mechanism of RPC is
hidden from the user.
• The effort to re-write and re-develop the code is
minimum in remote procedure calls.
• Remote procedure calls can be used in distributed
environment as well as the local environment.
• Many of the protocol layers are omitted by RPC to
improve performance.
3/5/2023
Disadvantages of Remote Procedure Call
3/5/2023