260 likes | 311 Views
Chapter 17: Distributed Systems CSS503 Systems Programming. Prof. Munehiro Fukuda Computing & Software Systems University of Washington Bothell. Local-Area Network ( LAN ) Covering a campus or a site Ethernet, Token Ling, etc. Speed 10 – 100Mbps Fast and cheap broadcast Nodes:
E N D
Chapter 17: Distributed Systems CSS503 Systems Programming Prof. Munehiro Fukuda Computing & Software Systems University of Washington Bothell Chapter 17: Distributed Systems
Local-Area Network(LAN) Covering a campus or a site Ethernet, Token Ling, etc. Speed 10 – 100Mbps Fast and cheap broadcast Nodes: Desktops and laptops Peripherals A few servers Wide-Area Network(WAN) Links geographically separated sites P2P long-haul line connection Speed 1.544 – 45Mbps Broadcast emulated with multiple messages Nodes: Routers Network Structure Chapter 17: Distributed Systems
router router router router router Network Structure Cont’d computing nodes switch Layer 2: Mac Address Layer 3: IP Address servers servers Layer 3: IP Address switch Layer 2: Mac Address Supercomputer Layer 3: IP Address computing nodes Chapter 17: Distributed Systems
OSI 7 Layers Site A Site B Peer-to-peer interface Application protocol 7 Application rsh, ftp, Telnet Application Service interface Presentation protocol Dealing with heterogeneity (Java object serialization) and cryptography (SSL) Presentation 6 Presentation Session protocol 5 Session Dialog control (rarely supported) Session Transport protocol Transport 4 UDP, TCP Transport Network protocol Network Network 3 IP Data link protocol IEEE802.2 connection or connectionless Data link Data link 2 Ethernet Physical protocol Physical 1 Physical Network Chapter 17: Distributed Systems 3
Data Link LayerEthernet • CSMA/CD • Carrier sense multiple access with collision detection • Listening to the shared medium • Transmitting a data packet • Detecting collision on the medium • Deferring and retransmitting a packet in 2k–time base collision window • Frame format • MAC (Media Access Control) Addresses • unique, 48-bit unicast address assigned to each adapter • example: 8:0:e4:b1:2 • broadcast: all 1s, multicast first bit is 1 • Bandwidth: 10Mbps, 100Mbps, 1Gbps 1 listen 3. detect Ⅹ 2 transmit bytes 46 ~ 1500 8 6 6 2 4 12 Inter-frame gap Min: 64bytes ~ Max: 1518bytes Chapter 17: Distributed Systems
0 4 8 16 19 31 TOS Length V ersion HLen Ident Flags Offset TTL Protocol Checksum SourceAddr DestinationAddr Pad Options (variable) (variable) Data Network LayerInternet • Global addressing • IP address • Best-effort delivery (unreliable service) • Connectionless datagram-based • packets are lost, delivered out of order, delayed for a long time, and even duplicated. • Datagram format • TTL: time to live (#hops) • Protocol: TCP, UDP • Checksum • SourceAddr: source IP address • DestinationAddr: destination IP address frame type Ethernet preamble dest addr src addr 0x0800 CRC Chapter 17: Distributed Systems
Internet as a Focal Point FTP HTTP NV TFTP UDP TCP IP … NET NET NET 2 1 n Connection-oriented stream data delivery Connection-less datagram delivery Focal point for the architecture Internet Protocol Ethernet, FDDI, etc. Chapter 17: Distributed Systems 6
Internet as a Focal Point Cont’d H7 H8 H2 H1 H3 Network 4 (Ethernet) Network 1 (Ethernet) H4 H5 R3 R1 Network 3 Network 2 (FDDI) (point-to-point) H1 H8 R2 FTP FTP H6 TCP TCP R1 R2 R3 IP IP IP IP IP ETH ETH FDDI FDDI PPP PPP ETH ETH Network 3 Network 1 (Ethernet) Network 2 (FDDI) (point-to-point) Network 4 (Ethernet) Chapter 17: Distributed Systems
Transport LayerTCP (Connection-Oriented Stream) vs UDP (Connectionless Datagram) TCP message TCP message data data Seq 1 Seq 2 IP port IP port IP addr IP addr data IP port IP addr data data IP addr IP addr . . IP packet UDP packet Protocol OSI layer User/Kernel mode IP address IP port Message length Message delivery Unix socket type TCP Layer 4 User IP address IP port Any FIFO order SOCK_STREAM UDP 65507 bytes Unreliable SOCK_DATAGRAM IP Layer 3 Kernel No IP port 65535 bytes (practically 1500B) SOCK_RAW port 1 … 65535 IP address: 192.168.1.2 IP address: 192.168.1.3 Network address: 192.168.1.0 Chapter 16: Distributed Systems 8
agreed port any port socket socket message client server other ports Internet address = 138.37.94.248 Internet address = 138.37.88.249 Sockets and Ports Chapter 16: Distributed Systems
UDP/TCP Socket Initialization Client Socket: Server Socket: // Client assumes argv[1] and argv[2] as the server IP name // and port. char *server = argv[1]; int port = atoi( argv[2] ); // retrieve the server information from DNS. struct hostent* host = gethostbyname( server ); if ( host == NULL ) { fprintf( stderr, "cannot find %s.\n", server ); exit( -1 ); } // allocate a socket address data structure. struct sockaddr_in sendSockAddr; bzero( ( char* )&sendSockAddr, sizeof( sendSockAddr ) ); // initialize the address with Internet(AF_INET), the server // address(host), and the server port(port). sendSockAddr.sin_family = AF_INET; sendSockAddr.sin_addr.s_addr = inet_addr( inet_ntoa( *(struct in_addr*)*host->h_addr_list ) ); sendSockAddr.sin_port = htons( port ); // allocate a UDP(SOCK_DGRAM) or TCP(SOCK_STREAM) socket. int clientSd = socket( AF_INET, SOCK_DGRAM, 0 ); SOCK_STREAM, // Server assumes argv[1] as its local port. int port = atoi( argv[1] ); // allocate a socket address data structure. struct sockaddr_in acceptSockAddr; bzero( ( char* )&acceptSockAddr, sizeof( acceptSockAddr ) ); // initialize the address with Internet(AF_INET), the client // address(INADDR_ANY), and the server port(port). acceptSockAddr.sin_family = AF_INET; acceptSockAddr.sin_addr.s_addr = htonl( INADDR_ANY ); acceptSockAddr.sin_port = htons( port ); // allocate a UDP(SOCK_DGRAM) or TCP(SOCK_STREAM) socket. int serverSd = socket( AF_INET, SOCK_DGRAM, 0 ); SOCK_STREAM, // bind this socket to a given port. if( bind( serverSd, (struct sockaddr*)&acceptSockAddr, sizeof( acceptSockAddr ) ) < 0 ) { perror( "Cannot bind the local address to the server socket." ); exit( -1 ); } Chapter 16: Distributed Systems
UDP: User Datagram Protocol Connectionless May be lost No FIFO order Multicast feature Unix datagram Example: TFTP, rwho client server socket() socket() Create a sock descriptor (if a client needs to receive a response) bind() bind() Bind it to an IP address recvfrom() sendto() Blocks until data received sendto() recvfrom() Chapter 16: Distributed Systems 11
UDP Example Code socket() socket() socket() Port:12345 Port: 13579 Port:12345 bind() bind() struct sockaddr_in srcAddr; socklen_t addrlen = sizeof( srcAddr ); bzero( ( char *)&srcAddr, sizeof( srcAddr ) ); char buf[256]; int nRead = recvfrom( serverSd, buf, 256, 0, (struct sockaddr*)&srcAddr, &addrlen ); char buf[256]; int buf_len = sizeof( buf ); sendto( clientSd, buf, length, 0, (struct sockaddr *)sendSockAddr, sizeof( struct sockaddr ) ); recvfrom() recvfrom() sendto() Packets demultiplexed UDP M5, M4, M3, M2, M1 Chapter 17: Distributed Systems 12
TCP: Transport Control Protocol Connection-oriented Reliable FIFO order No Multicast feature Unix stream socket Example: ftp, http, rsh all major applications client server socket() socket() Create a sock descriptor bind() Bind it to an IP address liseten() Prepare a queue to pool requests accept() Wait for a connection Connection established connect() Blocks until connection established read() write() read() wrte() Chapter 17: Distributed Systems 13
Passing a TCP Connection to a Child socket() socket() socket() bind() listen( serverSd, 5 ); connect( clientSd, ( struct sockaddr* )sendSockAddr, sizeof( struct sockaddr ) ); listen() connect() connect() int newSd = 0; struct sockaddr_in newSockAddr; socklen_t newSockAddrSize = sizeof( newSockAddr ); while( true ) { newSd = accept( serverSd, (struct sockaddr*)&newSockAddr, &newSockAddrSize ) ); char buf1[256], buf2[1024]; write( clientSd, buf1, sizeof( buf ) ); write( clientSd, buf2, sizeof( buf ) ); accept() if ( fork( ) == 0 ) { close( serverSd ); char buf1[256], buf2[1024]; read( newSd, buf1, sizeof( buf1 ) ); read( newSd, buf2, sizeof( buf2 ) ); close( newSd ); exit( 0 ); } write() write() write() write() buf2, buf1 buf2, buf1 read() read() read() read() close( newsd); } Chapter 17: Distributed Systems
Active Open (Client) connect( int sd, struct sockaddr *name, socklen_t namelen ) Passive Open (Server) bind( int sd, struct sockaddr *name, socklen_t namelen ); listen( sd, 5 ); accept( sd, struct sockaddr *addr, socket_t *addrlen ); file Socket data structure Socket data structure Socket data structure type type type protocol protocol protocol so_pcb so_pcb so_pcb 0 0 so_rcv so_rcv so_rcv 1 1 so_snd so_snd so_snd 2 2 3 3 4 4 File structure table User file descriptor table User file descriptor table File structure table l_addr l_port f_addr f_port socket inpcb: Internet protocol control block Socket Implementation • Creating a socket: int socket(int domain, int type, int protocol) • domain =PF_INET, PF_UNIX • type = SOCK_STREAM, SOCK_DGRAM inode Client process Server process int fd = open( “fileA”, O_RDONLY, 0 ); packet copied copied packet packet packet packet packet l_addr l_port inpcb: Internet protocol control block f_addr f_port socket Chapter 17: Distributed Systems
Socket Structure Disk Process int fd = open(“fileA”, flags); read(fd, …); int sd = socket( AF_INET, SOCK_STREAM, 0 ); connect( sd, &sockaddr, sizof( sockaddr ) ); struct inode { length count: 1 direct[12] indirect[3] } struct file { count: 1 f_ops: file pointer f_data f_type: DTYPE_INODE } blocks Inode Table File System Process Control Block IP Implementation Internet Protocol Control Block stdin 0 1 2 3 4 struct file { count: 1 f_ops: file pointer f_data f_type: DTYPE_SOCKET } struct inpcb { inp_faddr inp_fport inp_laddr inp_lport inp_socket } stdout stderr struct socket { so_type: SOCK_STREAM count: 1 so_pcb so_snd so_rcv } mbuf { data } User File Descriptor Table Socket Structure Table File Structure Table Chapter 16: Distributed Systems 16
Sharing a Socket among Processes Process Control Block Process Control Block 0 1 2 3 4 0 1 2 3 4 stdin stdin stdout stdout stderr stderr User File Descriptor Table User File Descriptor Table Process 1 int sd = socket( AF_INET, SOCK_STREAM, 0 ); bind( sd, … ); listen( sd, 5 ); int new_sd = accept( sd, &sockaddr, sizeof( sockaddr ) ); if ( fork( ) == 0 ) { close( sd ); execl( “program2”, … ); } close( new_sd ); Internet Protocol Control Block Socket Structure Table struct inpcb { inp_faddr inp_fport inp_laddr inp_lport inp_socket } struct file { count: 1 f_ops: file pointer f_data f_type: DTYPE_SOCKET } struct socket { so_type: SOCK_STREAM count: 1 so_pcb so_snd so_rcv } X struct file { count: 1 f_ops: file pointer f_data f_type: DTYPE_SOCKET } fork and execl X struct socket { so_type: SOCK_STREAM count: 1 so_pcb so_snd so_rcv } struct inpcb { inp_faddr inp_fport inp_laddr inp_lport inp_socket } copied File Structure Table Process 2 int main(int argc, char** argv) { int fd = 4; read( fd, … ); } mbuf { data } Chapter 16: Distributed Systems 17
Socket Sharing Example: RSH Client Server inetd shell TCP connection request Command rsh ls- l rshd shell TCP connection Inherited all the way To a child Command ls -l Chapter 17: Distributed Systems
The source process completes a message transfer by itself. The destination process is blocked until the current process delivers a message to it. User Processes and Messaging Destination Process Source Process context switch user message Application Layer user message Current Process tcp header user data 1 tcp header user data 2 Transport Layer tcp header user data 1 tcp header user data 2 ip header tcp header user data 1 ip header tcp header user data 2 Network Layer ip header tcp header user data 1 ip header tcp header user data 2 eth frame ip header tcp header user data 1 eth frame ip header tcp header user data 2 Data Link Layer eth frame ip header tcp header user data 1 eth frame ip header tcp header user data 2 Chapter 17: Distributed Systems
Programming Assignment 4Inter-Segment UDP Broadcast Chapter 17: Distributed Systems
Programming Assignment 4Inter-Segment UDP Broadcast UDP message UDP message UDP message UDP message BroadcastServer.java BroadcastServer.java -32 -31 -30 2 BroadcastClient.java BroadcastServer.java -32 -31 -30 1 A’s IP -32 -31 -30 2 -32 -31 -30 2 A’s IP B’s IP A’s IP A’s IP UDP message UDP message B’s IP B’s IP -32 -31 -30 0 -32 -31 -30 0 UDP message UDP message UDP message UDP message UdpRelay.cpp (A) UdpRelay.cpp (B) BroadcastSocket.java BroadcastSocket.java BroadcastSocket.java BroadcastSocket.java TCP Link packet not looped Chapter 17: Distributed Systems
Programming Assignment 4Inter-Segment UDP Broadcast Chapter 17: Distributed Systems
Programming Assignment 4Inter-Segment UDP Broadcast UdpRelay.java commandThread relayOutThread User remote segment 1 read relayOutThread read remote segment 2 multicast write multicast local segment write relayInThread acceptThread recv remote segment 3 message new connection Chapter 17: Distributed Systems
Programming Assignment 4Inter-Segment UDP Broadcast TCP Link TCP Link UdpRelay UdpRelay UdpRelay BroadcastServer BroadcastClient BroadcastServer BroadcastServer Node 6 Node 5 Node 7 Node 2 Node 1 Node 3 Node 4 237.255.255.255 239.255.255.255 238.255.255.255 Chapter 17: Distributed Systems
Discussion • Under what circumstances is a token-passing network more effective than an Ethernet network? • Compare multi-process versus multi-threaded servers in terms of their pros and cons. • Solve textbook Ex 17.18: why does HTTP use TCP/IP? Why not UDP or any other performance improvement? Chapter 17: Distributed Systems