CSE 30264 Computer Networks

34
CSE 30264 Computer Networks Prof. Aaron Striegel Department of Computer Science & Engineering University of Notre Dame Lecture 14 – February 23, 2010

description

CSE 30264 Computer Networks. Prof. Aaron Striegel Department of Computer Science & Engineering University of Notre Dame Lecture 14 – February 23, 2010. Today’s Lecture. Mid-Term Exam Summary Project 2 Transport RPC Mid-Term Exam. Application. Transport. Network. Data. Physical. - PowerPoint PPT Presentation

Transcript of CSE 30264 Computer Networks

Page 1: CSE 30264 Computer Networks

CSE 30264

Computer Networks

Prof. Aaron StriegelDepartment of Computer Science & Engineering

University of Notre Dame

Lecture 14 – February 23, 2010

Page 2: CSE 30264 Computer Networks

CSE 30264 2

Today’s Lecture

• Mid-Term Exam Summary• Project 2• Transport

– RPC• Mid-Term Exam

Spring 2010

Physical

Data

Network

Transport

Application

Thursday: Guest lecture by Dr. Poellabauer

Page 3: CSE 30264 Computer Networks

CSE 30264 3

Mid-Term Exam

• Will hand out later in class– Average -> 83%– Maximum points: 104

• Want to earn back points?– Pick one problem– Well written, well illustrated discussion of the problem– Gain back up to half of points lost on that problem

Spring 2010

Page 4: CSE 30264 Computer Networks

CSE 30264 4

Project 2• By popular demand

– Turn in by Tuesday, March 23 @ 5 PM

– Wiki will update later today• Extra credit

– Bonus 10 points– Turn in by Thursday,

March 18 @ 5 PM– Add note in README

• Down side– Project 3 is coming next Tuesday

Spring 2010

Page 5: CSE 30264 Computer Networks

CSE 30264 5

Tip / Tricks – Project 2

• Split your code into blocks and test separately– Send as a file

Spring 2010

Client Test Open up socket Connect to server Read file from disk Send buffer to server Read response Display to screen

Page 6: CSE 30264 Computer Networks

CSE 30264 6

Tips / Tricks – Project 2

• Split your code– Write your handler in a thread friendly

Spring 2010

void * handleClient (void * pData){ int clientSocket; nClientSocket = *( (int *) pData);

// Handle client

Page 7: CSE 30264 Computer Networks

CSE 30264 7

Tips / Tricks – Project 2

• Split your code– Write your handler in a thread friendly

Spring 2010

int main ( … ){ // Server socket magic // Listen while(1) { cSocket = accept (…); handleClient(&cSocket);

Page 8: CSE 30264 Computer Networks

CSE 30264 8

Tips / Tricks – Project 2

• Split your code– Test your XML parsing separately– Read in a file and parse the results– Go simple XML

Spring 2010

<xml version … /><add CalName=“Test” Month=“3” Day=“16” … />

Page 9: CSE 30264 Computer Networks

CSE 30264 9

Project 2 – Other Notes

• Client side– Support two modes

• Send the file and display the response (easier)• Respond to arguments / build XML

– You can write two different clients if that makes things easier

• Server side– Your server does not need to persist data after restart– Files / storage are great but not required– You could have two different servers as well, one for

multi-thread, one for single threadSpring 2010

Page 10: CSE 30264 Computer Networks

Spring 2010 CSE30264 10

Remote Procedure Call

OutlineConcept of RPCSunRPC

Page 11: CSE 30264 Computer Networks

Spring 2010 CSE30264 11

RPC Timeline

Client Server

Request

ReplyComputing

Blocked

Blocked

Blocked

Page 12: CSE 30264 Computer Networks

Spring 2010 CSE30264 12

RPC

• There exists a way for processes to represent a task: procedure/function.

main() main()

func() func()

func(12); func(12); return 5;return 5;

Client

Server

Page 13: CSE 30264 Computer Networks

Spring 2010 CSE30264 13

RPC

main()

func()

stub(12); return 5;

Client

Serverskeleton

stub

func(12); return 5;

Request Reply

Page 14: CSE 30264 Computer Networks

Spring 2010 CSE30264 14

Stubs

• Stub is a function with the same interface as func(); it converts the function call into a network response and a network response into a function return.

• Skeleton converts requests into function calls and function returns into network replies.

• RPC System: used to generate both stub and skeleton (automatically).

Page 15: CSE 30264 Computer Networks

Spring 2010 CSE30264 15

SunRPC

• Widely used Remote Procedure Call system:– Sun Microsystems, implemented in most Unix systems– NFS distributed file system is based on Sun RPC

• Designed to call remote C procedures. • Platform-independent.

Page 16: CSE 30264 Computer Networks

Spring 2010 CSE30264 16

SunRPC Header Format

• XID (transaction id)• Server does not remember

last XID it serviced • Problem if client

retransmits request while reply is in transit

Data

Data

MsgType = CALL

XID

RPCVersion = 2(a)

Program

Version

Procedure

Credentials (variable)

Verifier (variable)

MsgType = REPLY

XID

Status = ACCEPTED(b)

0 31 0 31

Page 17: CSE 30264 Computer Networks

Spring 2010 CSE30264 17

RPC• One computer can be server for multiple procedures:

– server may host several programs (identified by program number)– each program may have several subsequent versions (version number)– each version may contain one or more procedures (procedure number)

• Program numbers are 32-bit hexadecimal values (0x21212100)– As user, you can choose any program number between 0x20000000 and

0x3FFFFFFF, but they have to be unique (not several programs with same number on same machine)

• Version and procedure numbers are integers (1,2,...)

(prog, vers, proc)

Page 18: CSE 30264 Computer Networks

Spring 2010 CSE30264 18

Sun RPC Program Numbers

FROM TO VALUES ASSIGNED BY0x00000000 0x1fffffffSun Microsystems, Inc.0x20000000 0x3fffffffSystem manager at a user’s site0x40000000 0x5fffffffTransient (temporary)0x60000000 0x7fffffffReserved0x80000000 0x9fffffffReserved0xa0000000 0xbfffffffReserved0xc0000000 0xdfffffffReserved0xe0000000 0xffffffff Reserved

Page 19: CSE 30264 Computer Networks

Spring 2010 CSE30264 19

Sun RPC Program Numbers

NAME NUMBER DESCRIPTIONportmap 100000 port mapperrstatd 100001 rstat, rup, perfmeterrusersd 100002 remote usersnfs 100003 network file systemypserv 100004 yp (NIS)mountd 100005 mount, showmountdbxd 100006 DMXprog (debugger)ypbind 100007 NIS binder...

Page 20: CSE 30264 Computer Networks

Spring 2010 CSE30264 20

Writing an RPC Program

client.c

add.x

serverproc.c

add_clnt.c

add.h

add_xdr.c

add_scv.c

client.o

add_clnt.o

add_xdr.o

add_svc.o

serverproc.o

server

client

You writethese files

rpcgen generatesthese files

You compileevery C file

You obtain a client & server

Page 21: CSE 30264 Computer Networks

Spring 2010 CSE30264 21

Example• Step 1: Write a specification file (add.x)

struct add_in { /* Arguments of procedure */

long arg1;long arg2;

};

typedef long add_out; /* Return value */

program ADD_PROG {version ADD_VERS

{add_out ADD_PROC(add_in) = 1 /* Procedure# = 1

*/} = 1; /* Version# = 1 */

} = 0x3434000; /* Program# = 0x3434000 */

Page 22: CSE 30264 Computer Networks

Spring 2009 CSE30264 22

Example

• Contains specifications of:– ‘add_in’ (arguments)– typedef ‘add_out’ (return values)– program ‘ADD_PROG’ (0x3434000)– 1 version ‘ADD_VERS’ (1)– 1 procedure ‘ADD_PROC’ (1)

• Your procedures can only take ONE input argument and return ONE output return value (use structures for more):– more readable (structure entries should have meaningful names)

Page 23: CSE 30264 Computer Networks

Spring 2009 CSE30264 23

Example

• Step 2: generate stubsrpcgen add.x

• ‘add.h’ contains declarations:#define ADD_PROG 0x34340000 /* Program nb */#define ADD_VERS 1 /* Version nb */#define ADD_PROC 1 /* Procedure nb */add_out * add_proc_1 (add_in *, CLIENT *);add_out * add_proc_1_svc (add_in *, struct svc_req *);– add_proc_1 is the stub (called by client)– add_proc_1_svc is the actual procedure that you will write and run

at the server

Page 24: CSE 30264 Computer Networks

Spring 2009 CSE30264 24

Example

• ‘add_clnt.c’: implementation of ‘add_proc_1’• ‘add_svc.c’: contains program which calls your

procedure ‘add_proc_1_svc’ when request received

• ‘add_xdr.c’: marshall/unmarshall routines

Page 25: CSE 30264 Computer Networks

Spring 2009 CSE30264 25

Example

• Step 3: write server procedure: ‘serverproc.c’

#include “add.h”add_out *add_proc_1_svc(add_in *in, struct svc_req *rqstp) {

static add_out out;out = in->arg1 + in->arg2;return(&out);

}– rqstp contains some information about the requester (IP address,

etc.)

Page 26: CSE 30264 Computer Networks

Spring 2009 CSE30264 26

Example

• Step 4: compile the server• Need to compile together your procedure, the (generated)

server program, the (generated) marshall/unmarshall procedures and the nsl library (contains the RPC runtime).$ gcc -c serverproc.c$ gcc -c add_svc.c$ gcc -c add_xdr.c$ gcc -o server serverproc.o add_svc.o add_xdr.o -lnsl

• To start the server:./server

Page 27: CSE 30264 Computer Networks

Spring 2009 CSE30264 27

Example• Step 5: write a client program client.c

#include “add.h”int main(int argc, char **argv) {

CLIENT *cl;add_in in;add_out *out;

if (argc!=4) {printf(“Usage:...\n”); return 1;}

cl = clnt_create(argv[1], ADD_PROG, ADD_VERS, “tcp”);in.arg1 = atol(argv[2]);in.arg2 = atol(argv[3]);out = add_proc_1(&in, cl);if (out == NULL) {printf(“Error: %s\n”, clnt_sperror(cl, argv[1])); }else { printf{“We received the result: %ld\n”, * out); }clnt_destroy(cl);return 0;

}

Page 28: CSE 30264 Computer Networks

Spring 2009 CSE30264 28

Example• Create a client structure with clnt_create

#include <rpc/rpc.h>CLIENT *clnt_create(char *host, u_long prog, u_long vers, char

*proto);– host: name of server machine– prog, vers: program/version number– proto: transport protocol (“tcp” or “udp”)

• You can call add_proc_1 to send the RPC• When finished, destroy client structure (client structure

can be used multiple times without being destroyed and recreated).

Page 29: CSE 30264 Computer Networks

Spring 2009 CSE30264 29

Example

• Step 6: compile the client$ gcc -c client.c$ gcc -c add_clnt.c$ gcc -c add_xdr.c$ gcc -o client client.o add_clnt.o add_xdr.o -lnsl

Page 30: CSE 30264 Computer Networks

Spring 2009 CSE30264 30

Example

• Step 7: try it out– start your server: ./server– send a request:

./client machine.cse.nd.edu 8 34We received the result: 42

Page 31: CSE 30264 Computer Networks

Spring 2009 CSE30264 31

Mutual Exclusion

• Sun RPC: at most one remote procedure in a remote program can be invoked at a given time.

• Automatic mutual exclusion among procedures within a given remote program.

• No synchronization needed.• Some versions of rpcgen allow one to generate

server code which implement one-thread-per-client (Solaris does, Linux doesn’t).

Page 32: CSE 30264 Computer Networks

Spring 2009 CSE30264 32

Inside SunRPC

(un)marshalling routines

client program

client stub

RPC runtime

server procedure

server stub

RPC runtime

(un)marshalling routines

Client Server

Page 33: CSE 30264 Computer Networks

Spring 2009 CSE30264 33

Low-Level RPC: Port Mapper• Did you notice that we did not specify a port number for

our ‘add’ server?– Could be done with well-known port number for RPCs– But then we could not run multiple servers in the same machine

simultaneously• RPC: port mapper

– server running on port 111– when your server starts, it does not bind its socket to a specific

port; instead it ‘registers’ whatever port number it has been given by the system to the local port mapper

– when you create a client with ‘clnt_create’, you automatically contact the remote port mapper to ask for the port number of the server you want to contact

Page 34: CSE 30264 Computer Networks

Spring 2009 CSE30264 34

Port Mapper

Client Server

You can request the port mapper by hand:$ rpcinfo -p wizard.cse.nd.edu program vers proto port 100000 2 tcp 111 portmapper 100000 2 udp 111 portmapper 66443998 1 udp 32877 66443938 1 tcp 54211

Port mapper

S1: Create an unbound socket (assigned port=11293)

S2: Register: program0x3434000 version 1running on port 11293

S3: Portmap requested: on which port

is program 0x3434000 version 1?

S4: Portmap response:

port 11293

S5: Run procedure 1 withparameter: “0x18A6B332F0”

S6: Result is “0x6464”