Presentation is loading. Please wait.

Presentation is loading. Please wait.

What is the Objective of Networking?

Similar presentations


Presentation on theme: "What is the Objective of Networking?"— Presentation transcript:

1 What is the Objective of Networking?
Enable communication between applications on different computers Web (Lecture 20) Peer to Peer (Lecture 21) Audio/Video (Lecture 24) Funky research stuff (Lecture 27) Must understand application needs/demands (Lecture 4) Traffic data rate Traffic pattern (bursty or constant bit rate) Traffic target (multipoint or single destination, mobile or fixed) Delay sensitivity Loss sensitivity Some example applications: video, audio, telnet, ftp, http, rpc – what are their needs Lecture 1: Introduction

2 What Is a Network? Collection of nodes and links that connect them
This is vague. Why? Consider different networks: Internet Andrew Telephone Your house Others – sensor nets, cell phones, … Class focuses on Internet, but explores important common issues and challenges Lecture 1: Introduction

3 Challenges for Networks
Geographic scope The Internet vs. Andrew Scale The Internet vs. your home network Application types vs. video conferencing Trust and Administration Corporate network – one network “provider” Internet – 17,000 network providers Lecture 1: Introduction

4 Basic Building Block: Links
Node Link Node Electrical questions Voltage, frequency, … Wired or wireless? Link-layer issues: How to send data? When to talk – can either side talk at once? What to say – low-level format? Lecture 5 Lecture 1: Introduction

5 Basic Building Block: Links
… But what if we want more hosts? (Lectures 6 & 7) Scalability?! One wire Wires for everybody! Lecture 1: Introduction

6 Multiplexing Need to share network resources How? Switched network
Party “A” gets resources sometimes Party “B” gets them sometimes Interior nodes act as “Switches” Lecture 1: Introduction

7 Back in the Old Days… Lecture 1: Introduction

8 Circuit Switching Source first establishes a connection (circuit) to the destination Each switch along the way stores info about connection (and possibly allocates resources) Source sends the data over the circuit No need to include the destination address with the data since the switches know the path The connection is explicitly torn down Example: telephone network (analog) Lecture 1: Introduction

9 Circuit Switching Discussion
Positives Fast and simple data transfer, once the circuit has been established Predictable performance since the circuit provides isolation from other users E.g. guaranteed bandwidth Negatives How about bursty traffic Circuit will be idle for significant periods of time Circuit establishment takes time How about users with different bandwidth needs do they have to use multiple circuits Alternative: packet switching Lecture 1: Introduction

10 Packet Switching Source sends information as self-contained packets that have an address. Source may have to break up single message in multiple Each packet travels independently to the destination host. Switches use the address in the packet to determine how to forward the packets Store and forward Analogy: a letter in surface mail. Lecture 1: Introduction

11 Packet Switching – Statistical Multiplexing
Lecture 1: Introduction

12 Packet Switching Discussion
Efficient Can send from any input that is ready General Multiple types of applications Accommodates bursty traffic Addition of queues Store and forward Packets are self contained units Can use alternate paths – reordering Contention (i.e. no isolation) Congestion Delay Lecture 1: Introduction

13 Internet[work] A collection of interconnected networks
Host: network endpoints (computer, PDA, light switch, …) Router: node that connects networks What if want to communicate between hosts on different Ethernets? Internet as interoperability layer Lecture 1: Introduction

14 Challenge Many differences between networks
Address formats Performance – bandwidth/latency Packet size Loss rate/pattern/handling Routing How to translate between various network technologies? Lecture 1: Introduction

15 “The Internet” Internet vs. internet
The Internet: the interconnected set of networks of the Internet Service Providers (ISPs) providing data communications services. About 17,000 different networks make up the Internet 100,000,000s of hosts Lecture 1: Introduction

16 Challenges of the Internet
Scale & Heterogeneity 18,000+ administrative domains, Thousands of applications Lots of users/hosts Fast links, slow links, satellite links, cellular links, carrier pigeons Diversity of network technologies Adversarial environment Oh, and let’s make it easy to use… All participating networks have to follow a common set of rules Lecture 1: Introduction

17 Need naming and routing Lectures 8-13
How To Find Nodes? Internet Computer 1 Computer 2 Need naming and routing Lectures 8-13 Lecture 1: Introduction

18 What’s the IP address for www.cmu.edu?
Naming What’s the IP address for It is Computer 1 Local DNS Server Translates human readable names to logical endpoints Lecture 1: Introduction

19 Routers send packet towards destination
Routing Routers send packet towards destination H R H R H R R R R H R H: Hosts R: Routers R H Lecture 1: Introduction

20 Network Service Model What is the service model?
Ethernet/Internet: best-effort – packets can get lost, etc. What if you want more? Performance guarantees (QoS) Reliability Corruption Lost packets Flow and congestion control Fragmentation In-order delivery Etc… Lecture 1: Introduction

21 What if the Data gets Corrupted?
Problem: Data Corruption GET index.html GET inrex.html Internet Solution: Add a checksum X 0,9 9 6,7,8 21 4,5 7 1,2,3 6 Lecture 1: Introduction

22 What if Network is Overloaded?
Problem: Network Overload Solution: Buffering and Congestion Control Short bursts: buffer What if buffer overflows? Packets dropped Sender adjusts rate until load = resources  “congestion control” Lecture 1: Introduction

23 What if the Data gets Lost?
Problem: Lost Data GET index.html Internet Solution: Timeout and Retransmit GET index.html GET index.html Internet GET index.html Lecture 1: Introduction

24 What if the Data Doesn’t Fit?
Problem: Packet size On Ethernet, max IP packet is 1.5kbytes Typical web page is 10kbytes Solution: Fragment data across packets ml x.ht inde GET GET index.html Lecture 1: Introduction

25 What if the Data is Out of Order?
Problem: Out of Order ml inde x.ht GET GET x.htindeml Solution: Add Sequence Numbers ml 4 inde 2 x.ht 3 GET 1 GET index.html Lecture 1: Introduction

26 Meeting Application Demands
Sometimes network can do it E.g., Quality of Service Benefits of circuit switching in packet-switched net Hard in the Internet, easy in restricted contexts Lecture 20 OR hosts can do it E.g., end-to-end Transport protocols TCP performs end-to-end retransmission of lost packets to give the illusion of a reliable underlying network. Lectures 16-19 Lecture 1: Introduction

27 Networks Implement Many Functions
Link Multiplexing Routing Addressing/naming (locating peers) Reliability Flow control Fragmentation Etc…. Lecture 1: Introduction

28 Next Lecture How to determine split of functionality
Across protocol layers Across network nodes Read two papers on the motivations for the Internet architecture: “The design philosophy of the DARPA Internet Protocols”, Dave Clark, SIGCOMM 88 “End-to-end arguments in system design”, Saltzer, Reed, and Clark, ACM Transactions on Computer Systems, November 1984 Lecture 1: Introduction

29 Lots of Functions Needed
Link Multiplexing Routing Addressing/naming (locating peers) Reliability Flow control Fragmentation Etc…. Lecture 1: Introduction

30 What is Layering? Modular approach to network functionality Example:
Application Application-to-application channels Host-to-host connectivity Link hardware Lecture 1: Introduction

31 Protocols Lecture 1: Introduction 8-30-05 Module in layered structure
An agreement between parties on how communication should take place Protocols define: Interface to higher layers (API) Interface to peer (syntax & semantics) Actions taken on receipt of a messages Format and order of messages Error handling, termination, ordering of requests, etc. Example: Buying airline ticket Friendly greeting Muttered reply Destination? Pittsburgh Thank you Lecture 1: Introduction

32 Layering User A User B Layering: technique to simplify complex systems
Application Transport Network Link Host Host Layering: technique to simplify complex systems Lecture 1: Introduction

33 Layering Characteristics
Each layer relies on services from layer below and exports services to layer above Interface defines interaction Hides implementation - layers can change without disturbing other layers (black box) Lecture 1: Introduction

34 The Internet Engineering Task Force
Standardization is key to network interoperability The hardware/software of communicating parties are often not built by the same vendor  yet they can communicate because they use the same protocol Internet Engineering Task Force Based on working groups that focus on specific issues Request for Comments Document that provides information or defines standard Requests feedback from the community Can be “promoted” to standard under certain conditions consensus in the committee interoperating implementations Project 1 will look at the Internet Relay Chat (IRC) RFC Lecture 1: Introduction

35 E.g.: OSI Model: 7 Protocol Layers
Physical: how to transmit bits Data link: how to transmit frames Network: how to route packets Transport: how to send packets end2end Session: how to tie flows together Presentation: byte ordering, security Application: everything else TCP/IP has been amazingly successful, and it’s not based on a rigid OSI model. The OSI model has been very successful at shaping thought Lecture 1: Introduction

36 OSI Layers and Locations
Application Presentation Session Transport Network Data Link Physical Host Bridge/Switch Router/Gateway Host Lecture 1: Introduction

37 IP Layering Relatively simple Lecture 1: Introduction 8-30-05
Application Transport Network Link Physical Host Bridge/Switch Router/Gateway Host Lecture 1: Introduction

38 The Internet Protocol Suite
FTP HTTP NV TFTP Applications UDP TCP TCP UDP Waist IP Data Link NET1 NET2 NETn Physical The Hourglass Model The waist facilitates interoperability Lecture 1: Introduction

39 Layer Encapsulation User A User B Lecture 1: Introduction 8-30-05
Get index.html Connection ID Source/Destination Link Address Lecture 1: Introduction

40 Protocol Demultiplexing
Multiple choices at each layer FTP HTTP NV TFTP TCP UDP Network IP TCP/UDP IPX IP Type Field Protocol Field Port Number NET1 NET2 NETn Lecture 1: Introduction

41 Multiplexing and Demultiplexing
There may be multiple implementations of each layer. How does the receiver know what version of a layer to use? Each header includes a demultiplexing field that is used to identify the next layer. Filled in by the sender Used by the receiver Multiplexing occurs at multiple layers. E.g., IP, TCP, … TCP TCP IP IP V/HL TOS Length ID Flags/Offset TTL Prot. H. Checksum Source IP address Destination IP address Options.. Lecture 1: Introduction

42 Is Layering Harmful? Layer N may duplicate lower level functionality (e.g., error recovery) Layers may need same info (timestamp, MTU) Strict adherence to layering may hurt performance Some layers are not always cleanly separated. Inter-layer dependencies in implementations for performance reasons Some dependencies in the standards (header checksums) Interfaces are not really standardized. It would be hard to mix and match layers from independent implementations, e.g., windows network apps on unix (w/out compatibility library) Many cross-layer assumptions, e.g. buffer management Lecture 1: Introduction

43 Today’s Lecture Layers and protocols
Design principles in internetworks Lecture 1: Introduction

44 Goals [Clark88] Connect existing networks Survivability
initially ARPANET and ARPA packet radio network Survivability ensure communication service even in the presence of network and router failures Support multiple types of services Must accommodate a variety of networks Allow distributed management Allow host attachment with a low level of effort Be cost effective Allow resource accountability Lecture 1: Introduction

45 Priorities The effects of the order of items in that list are still felt today E.g., resource accounting is a hard, current research topic Let’s look at them in detail Lecture 1: Introduction

46 0. Connecting Existing Networks
Many differences between networks Address formats Performance – bandwidth/latency Packet size Loss rate/pattern/handling Routing How to internetwork various network technologies What are some choices? Lecture 1: Introduction

47 Address Formats Map one address format to another?
Bad idea  many translations needed Provide one common format Map lower level addresses to common format Lecture 1: Introduction

48 Different Packet Sizes
Define a maximum packet size over all networks? Either inefficient or high threshold to support Implement fragmentation/re-assembly Who is doing fragmentation? Who is doing re-assembly? Lecture 1: Introduction

49 Gateway Alternatives Translation Standardization
Difficulty in dealing with different features supported by networks Scales poorly with number of network types (N^2 conversions) Standardization “IP over everything” Minimal assumptions about network Hourglass design Lecture 1: Introduction

50 1. Survivability If network disrupted and reconfigured:
Communicating entities should not care! No higher-level state reconfiguration How to achieve such reliability? Where can communication state be stored? Network Host Failure handing Replication “Fate sharing” Switches Maintain state Stateless Host trust Less More Lecture 1: Introduction

51 Fate Sharing Connection State State No State Lose state information for an entity if (and only if?) the entity itself is lost. Examples: OK to lose TCP state if one endpoint crashes NOT okay to lose if an intermediate router reboots Is this still true in today’s network? NATs and firewalls Lecture 1: Introduction

52 Soft-State Basic behavior Penalty for timeout – poor performance
Announce state Refresh state Timeout state Penalty for timeout – poor performance Robust way to identify communication flows Possible mechanism to provide non-best effort service Helps survivability Lecture 1: Introduction

53 End-to-End Argument Deals with where to place functionality Argument:
Inside the network (in switching elements) At the edges Argument: There are functions that can only be correctly implemented by the endpoints – do not try to completely implement these elsewhere Lecture 1: Introduction

54 Example: Reliable File Transfer
Host A Host B Appl. Appl. OK OS OS Solution 1: make each step reliable, and then concatenate them Solution 2: end-to-end check and retry Lecture 1: Introduction

55 E2E Example: File Transfer
If network guaranteed reliable delivery The receiver has to do the check anyway! E.g., network card may malfunction Full functionality can only be entirely implemented at application layer; no need for reliability from lower layers Is there any need to implement reliability at lower layers? Lecture 1: Introduction

56 Discussion Yes, but only to improve performance
If network is highly unreliable Adding some level of reliability helps performance, not correctness Don’t try to achieve perfect reliability! Implementing a functionality at a lower level should have minimum performance impact on the applications that do not use the functionality Lecture 1: Introduction

57 2. Types of Service Best effort delivery
All packets are treated the same Relatively simple core network elements Building block from which other services (such as reliable data stream) can be built Contributes to scalability of network No QoS support assumed from below Accommodates more networks Hard to implement without network support QoS is an ongoing debate… Lecture 1: Introduction

58 Types of Service TCP vs. UDP
Elastic apps that need reliability: remote login or Inelastic, loss-tolerant apps: real-time voice or video Others in between, or with stronger requirements Biggest cause of delay variation: reliable delivery Today’s net: ~100ms RTT Reliable delivery can add seconds. Original Internet model: “TCP/IP” one layer First app was remote login… But then came debugging, voice, etc. These differences caused the layer split, added UDP Lecture 1: Introduction

59 3. Varieties of Networks Minimum set of assumptions for underlying net
Minimum packet size Reasonable delivery odds, but not 100% Some form of addressing unless point to point Important non-assumptions: Perfect reliability Broadcast, multicast Priority handling of traffic Internal knowledge of delays, speeds, failures, etc. Much engineering then only has to be done once Lecture 1: Introduction

60 The “Other” goals 4. Management 5. Attaching a host
Each network owned and managed separately Will see this in BGP routing especially 5. Attaching a host Not awful; DHCP and related autoconfiguration technologies helping. 6. Cost effectiveness Economies of scale won out Internet cheaper than most dedicated networks Packet overhead less important by the year But… Lecture 1: Introduction

61 7. Accountability Huge problem. Accounting Accountability and security
Billing? (mostly flat-rate. But phones are moving that way too - people like it!) Inter-provider payments Hornet’s nest. Complicated. Political. Hard. Accountability and security Worms, viruses, etc. Partly a host problem. But hosts very trusted. Authentication Purely optional. Many philosophical issues of privacy vs. security. Greedy sources aren’t handled well Lecture 1: Introduction

62 Other IP Design Weaknesses
Weak administration and management tools Incremental deployment difficult at times Result of no centralized control No more “flag” days Are active networks the solution? Lecture 1: Introduction

63 Summary: Internet Architecture
Packet-switched datagram network IP is the “compatibility layer” Hourglass architecture All hosts and routers run IP Stateless architecture No per flow state inside network TCP UDP IP Satellite Ethernet ATM Lecture 1: Introduction

64 Summary: Minimalist Approach
Dumb network IP provide minimal functionalities to support connectivity Addressing, forwarding, routing Smart end system Transport layer or application performs more sophisticated functionalities Flow control, error control, congestion control Advantages Accommodate heterogeneous technologies (Ethernet, modem, satellite, wireless) Support diverse applications (telnet, ftp, Web, X windows) Decentralized network administration Beginning to show age Unclear what the solution will be  probably IPv6 Lecture 1: Introduction

65 A Client-Server Exchange
A server process and one or more client processes Server manages some resource. Server provides service by manipulating resource for clients. 1. Client sends request Client process Server process Resource 4. Client handles response 3. Server sends response 2. Server handles request Note: clients and servers are processes running on hosts (can be the same or different hosts). Lecture 1: Introduction

66 Network Applications Access to Network via Program Interface
Client Machine Server Machine Client Appl. Server Appl. OS + Network APIs OS + Network APIs Socket Socket Internet OS Network Interface OS Network Interface Access to Network via Program Interface Sockets make network I/O look like files Call system functions to control and communicate Network code handles issues of routing, segmentation. Lecture 1: Introduction

67 Internet Connections (TCP/IP)
Two common paradigms for clients and servers communication Datagrams (UDP protocol SOCK_DGRAM) Connections (TCP protocol, SOCK_STREAM) Connections are point-to-point, full-duplex (2-way communication), and reliable. (TODAY’S TOPIC!) Client socket address :3479 Server socket address :80 Client Server (port 80) Connection socket pair ( :3479, :80) Client host address Server host address Note: 3479 is an ephemeral port allocated by the kernel Note: 80 is a well-known port associated with Web servers Lecture 1: Introduction

68 Clients Examples of client programs How does a client find the server?
Web browsers, ftp, telnet, ssh How does a client find the server? The IP address in the server socket address identifies the host (more precisely, an adaptor on the host) The (well-known) port in the server socket address identifies the service, and thus implicitly identifies the server process that performs that service. Examples of well known ports Port 7: Echo server Port 23: Telnet server Port 25: Mail server Port 80: Web server Lecture 1: Introduction

69 Using Ports to Identify Services
Server host Client host Service request for :80 (i.e., the Web server) Web server (port 80) Client Kernel Echo server (port 7) Web server (port 80) Service request for :7 (i.e., the echo server) Client Kernel Echo server (port 7) Lecture 1: Introduction

70 Servers Servers are long-running processes (daemons).
Created at boot-time (typically) by the init process (process 1) Run continuously until the machine is turned off. Each server waits for requests to arrive on a well-known port associated with a particular service. Port 7: echo server Port 23: telnet server Port 25: mail server Port 80: HTTP server A machine that runs a server process is also often referred to as a “server.” See /etc/services for a comprehensive list of the services available on a Linux machine. Lecture 1: Introduction

71 Overview of the Sockets Interface
Client Server socket socket bind open_listenfd open_clientfd listen Connection request connect accept read close EOF read write Client / Server Session Await connection request from next client Lecture 1: Introduction

72 Sockets What is a socket?
To the kernel, a socket is an endpoint of communication. To an application, a socket is a file descriptor that lets the application read/write from/to the network. Remember: All Unix I/O devices, including networks, are modeled as files. Clients and servers communicate with each by reading from and writing to socket descriptors. The main distinction between regular file I/O and socket I/O is how the application “opens” the socket descriptors. Lecture 1: Introduction

73 Socket Programming Cliches
Network Byte Ordering Network is big-endian, host may be big- or little- endian Functions work on 16-bit (short) and 32-bit (long) values htons() / htonl() : convert host byte order to network byte order ntohs() / ntohl(): convert network byte order to host byte order Use these to convert network addresses, ports, … Structure Casts You will see a lot of ‘structure casts’ struct sockaddr_in serveraddr; /* fill in serveraddr with an address */ /* Connect takes (struct sockaddr *) as its second argument */ connect(clientfd, (struct sockaddr *) &serveraddr, sizeof(serveraddr)); Lecture 1: Introduction

74 Socket Programming Help
man is your friend (aka RTFM) man accept man select Etc. The manual page will tell you: What #include<> directives you need at the top of your source code The type of each argument The possible return values The possible errors (in errno) Lecture 1: Introduction

75 Socket Address Structures
Generic socket address: For address arguments to connect, bind, and accept. Internet-specific socket address: Must cast (sockaddr_in *) to (sockaddr *) for connect, bind, and accept. struct sockaddr { unsigned short sa_family; /* protocol family */ char sa_data[14]; /* address data. */ }; struct sockaddr_in { unsigned short sin_family; /* address family (always AF_INET) */ unsigned short sin_port; /* port num in network byte order */ struct in_addr sin_addr; /* IP addr in network byte order */ unsigned char sin_zero[8]; /* pad to sizeof(struct sockaddr) */ }; Lecture 1: Introduction

76 Reliable I/O (RIO) Summary
I/O Package Developed by David O’Hallaron (csapp.{h,c}) Allows mix of buffered and unbuffered I/O Important Functions rio_writen(int fd, void *buf, size_t n) Writes n bytes from buffer buf to file fd. rio_readlineb(rio_t *rp, void *buf, size_t maxn) Read complete text line from file rp into buffer buf. Line must be terminated by newline (\n) character Up to maximum of maxn bytes Used Here For Illustrative Purposes Only You may want to use read()/write() for your projects instead You will need to check error returns Reading a whole line won’t always make sense (more later) NOTE: RIO functions capitalize first letter!! You must fix this! Accept() .vs. accept() Lecture 1: Introduction

77 Echo Client Main Routine
#include "csapp.h" /* usage: ./echoclient host port */ int main(int argc, char **argv) { int clientfd, port; char *host, buf[MAXLINE]; rio_t rio; host = argv[1]; port = atoi(argv[2]); clientfd = Open_clientfd(host, port); Rio_readinitb(&rio, clientfd); while (Fgets(buf, MAXLINE, stdin) != NULL) { Rio_writen(clientfd, buf, strlen(buf)); Rio_readlineb(&rio, buf, MAXLINE); Fputs(buf, stdout); } Close(clientfd); exit(0); Send line to server Receive line from server Lecture 1: Introduction

78 Client-side Programming
Lecture 1: Introduction

79 Echo Client: open_clientfd
int open_clientfd(char *hostname, int port) { int clientfd; struct hostent *hp; struct sockaddr_in serveraddr; if ((clientfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) return -1; /* check errno for cause of error */ /* Fill in the server's IP address and port */ if ((hp = gethostbyname(hostname)) == NULL) return -2; /* check h_errno for cause of error */ bzero((char *) &serveraddr, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; serveraddr.sin_port = htons(port); bcopy((char *)hp->h_addr, (char *)&serveraddr.sin_addr.s_addr, hp->h_length); /* Establish a connection with the server */ if (connect(clientfd, (struct sockaddr *) &serveraddr, sizeof(serveraddr)) < 0) return -1; return clientfd; } This function opens a connection from the client to the server at hostname:port Lecture 1: Introduction

80 Echo Client: open_clientfd (socket)
socket creates a socket descriptor on the client. AF_INET: indicates that the socket is associated with Internet protocols. SOCK_STREAM: selects a reliable byte stream connection. int clientfd; /* socket descriptor */ if ((clientfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) return -1; /* check errno for cause of error */ ... (more) Lecture 1: Introduction

81 Echo Client: open_clientfd (gethostbyname)
The client then builds the server’s Internet address. int clientfd; /* socket descriptor */ struct hostent *hp; /* DNS host entry */ struct sockaddr_in serveraddr; /* server’s IP address */ ... /* fill in the server's IP address and port */ if ((hp = gethostbyname(hostname)) == NULL) return -2; /* check h_errno for cause of error */ bzero((char *) &serveraddr, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; bcopy((char *)hp->h_addr, (char *)&serveraddr.sin_addr.s_addr, hp->h_length); serveraddr.sin_port = htons(port); Lecture 1: Introduction

82 Echo Client: open_clientfd (connect)
Finally the client creates a connection with the server. Client process suspends (blocks) until the connection is created. After resuming, the client is ready to begin exchanging messages with the server via Unix I/O calls on descriptor sockfd. int clientfd; /* socket descriptor */ struct sockaddr_in serveraddr; /* server address */ typedef struct sockaddr SA; /* generic sockaddr */ ... /* Establish a connection with the server */ if (connect(clientfd, (SA *)&serveraddr, sizeof(serveraddr)) < 0) return -1; return clientfd; } Lecture 1: Introduction

83 Server-side Programming
Lecture 1: Introduction

84 Servers and sockets – 1 isn’t enough
Server must be able to handle multiple requests Where should pending connections be queued up? client 1 server client 2 call connect call connect call accept ret connect ret accept call read write ret read close close call accept ret accept ret connect write call read ret read close close Lecture 1: Introduction

85 Connected vs. Listening Descriptors
End point for client connection requests. Created once and exists for lifetime of the server. Connected descriptor End point of the connection between client and server. A new descriptor is created each time the server accepts a connection request from a client. Exists only as long as it takes to service client. Why the distinction? Allows for concurrent servers that can communicate over many client connections simultaneously. Lecture 1: Introduction

86 Echo Server: accept Illustrated
listenfd(3) 1. Server blocks in accept, waiting for connection request on listening descriptor listenfd. Client Server clientfd Connection request listenfd(3) Client Server 2. Client makes connection request by calling and blocking in connect. clientfd listenfd(3) 3. Server returns connfd from accept. Client returns from connect. Connection is now established between clientfd and connfd. Client Server clientfd connfd(4) Lecture 1: Introduction

87 Echo Server: Main Loop The server loops endlessly, waiting for connection requests, then reading input from the client, and echoing the input back to the client. main() { /* create and configure the listening socket */ while(1) { /* Accept(): wait for a connection request */ /* echo(): read and echo input lines from client til EOF */ /* Close(): close the connection */ } Lecture 1: Introduction

88 Echo Server: open_listenfd
int open_listenfd(int port) { int listenfd, optval=1; struct sockaddr_in serveraddr; /* Create a socket descriptor */ if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) return -1; /* Eliminates "Address already in use" error from bind. */ if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval , sizeof(int)) < 0) ... (more) Lecture 1: Introduction

89 Echo Server: open_listenfd (cont)
... /* Listenfd will be an endpoint for all requests to port on any IP address for this host */ bzero((char *) &serveraddr, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; serveraddr.sin_addr.s_addr = htonl(INADDR_ANY); serveraddr.sin_port = htons((unsigned short)port); if (bind(listenfd, (SA *)&serveraddr, sizeof(serveraddr)) < 0) return -1; /* Make it a listening socket ready to accept connection requests */ if (listen(listenfd, LISTENQ) < 0) return listenfd; } Lecture 1: Introduction

90 Echo Server: open_listenfd (socket)
socket creates a socket descriptor on the server. AF_INET: indicates that the socket is associated with Internet protocols. SOCK_STREAM: selects a reliable (TCP) byte stream connection. int listenfd; /* listening socket descriptor */ /* Create a socket descriptor */ if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) return -1; Lecture 1: Introduction

91 Echo Server: open_listenfd (initialize socket address)
Next, we initialize the socket with the server’s Internet address (IP address and port) IP addr and port stored in network (big-endian) byte order htonl() converts longs from host byte order to network byte order. htons() converts shorts from host byte order to network byte order. struct sockaddr_in serveraddr; /* server's socket addr */ ... /* listenfd will be an endpoint for all requests to port on any IP address for this host */ bzero((char *) &serveraddr, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; serveraddr.sin_addr.s_addr = htonl(INADDR_ANY); serveraddr.sin_port = htons((unsigned short)port); Lecture 1: Introduction

92 Echo Server: open_listenfd (bind)
bind associates the socket with the socket address we just created. int listenfd; /* listening socket */ struct sockaddr_in serveraddr; /* server’s socket addr */ ... /* listenfd will be an endpoint for all requests to port on any IP address for this host */ if (bind(listenfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0) return -1; Lecture 1: Introduction

93 Echo Server: open_listenfd (listen)
listen indicates that this socket will accept connection (connect) requests from clients. We’re finally ready to enter the main server loop that accepts and processes client connection requests. int listenfd; /* listening socket */ ... /* Make it a listening socket ready to accept connection requests */ if (listen(listenfd, LISTENQ) < 0) return -1; return listenfd; } Lecture 1: Introduction

94 Echo Server: accept accept() blocks waiting for a connection request.
accept returns a connected descriptor (connfd) with the same properties as the listening descriptor (listenfd) Returns when the connection between client and server is created and ready for I/O transfers. All I/O with the client will be done via the connected socket. accept also fills in client’s IP address. int listenfd; /* listening descriptor */ int connfd; /* connected descriptor */ struct sockaddr_in clientaddr; int clientlen; clientlen = sizeof(clientaddr); connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen); Lecture 1: Introduction

95 Echo Server: Main Routine
int main(int argc, char **argv) { int listenfd, connfd, port, clientlen; struct sockaddr_in clientaddr; struct hostent *hp; char *haddrp; port = atoi(argv[1]); /* the server listens on a port passed on the command line */ listenfd = open_listenfd(port); while (1) { clientlen = sizeof(clientaddr); connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen); hp = Gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr, sizeof(clientaddr.sin_addr.s_addr), AF_INET); haddrp = inet_ntoa(clientaddr.sin_addr); printf("Fd %d connected to %s (%s:%s)\n", connfd, hp->h_name, haddrp, ntohs(clientaddr.sin_port)); echo(connfd); Close(connfd); } Lecture 1: Introduction

96 Echo Server: Identifying the Client
The server can determine the domain name, IP address, and port of the client. struct hostent *hp; /* pointer to DNS host entry */ char *haddrp; /* pointer to dotted decimal string */ hp = Gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr, sizeof(clientaddr.sin_addr.s_addr), AF_INET); haddrp = inet_ntoa(clientaddr.sin_addr); printf("Fd %d connected to %s (%s:%s)\n", connfd, hp->h_name, haddrp, ntohs(clientaddr.sin_port)); Lecture 1: Introduction

97 Echo Server: echo The server uses RIO to read and echo text lines until EOF (end-of-file) is encountered. EOF notification caused by client calling close(clientfd). IMPORTANT: EOF is a condition, not a particular data byte. void echo(int connfd) { size_t n; char buf[MAXLINE]; rio_t rio; Rio_readinitb(&rio, connfd); while((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0) { printf("server received %d bytes\n", n); Rio_writen(connfd, buf, n); } Receive line from client Send line to client Lecture 1: Introduction

98 Running Echo Client/Server
echo]$ ./echoservers 15441 fd 4 connected to BRYANT-TP2.VLSI.CS.CMU.EDU ( :3507) echo]$ ./echoservers 15441 echo]$ ./echoservers 15441 fd 4 connected to BRYANT-TP2.VLSI.CS.CMU.EDU ( :3507) Server received 12 (12 total) bytes on fd 4 echo]$ ./echoclient bryant.vlsi.cs.cmu.edu 15441 hello world echo]$ ./echoclient bryant.vlsi.cs.cmu.edu 15441 hello world echo]$ ./echoclient bryant.vlsi.cs.cmu.edu 15441 Lecture 1: Introduction

99 Types of Server Implementations
Lecture 1: Introduction

100 Iterative Servers Iterative servers process one request at a time.
client 1 server client 2 call connect call connect call accept ret connect ret accept call read write ret read close close call accept ret accept ret connect write call read ret read close close Lecture 1: Introduction

101 Fundamental Flaw of Iterative Servers
client 1 server client 2 call accept call connect ret connect ret accept call fgets call read Server blocks waiting for data from Client 1 call connect User goes out to lunch Client 1 blocks waiting for user to type in data Client 2 blocks waiting to complete its connection request until after lunch! Solution: use concurrent servers instead. Concurrent servers use multiple concurrent flows to serve multiple clients at the same time. Lecture 1: Introduction

102 Concurrent Servers Concurrent servers handle multiple requests concurrently. client 1 server client 2 call accept call connect call connect ret connect ret accept call fgets call read (don’t block) call accept User goes out to lunch Client 1 blocks waiting for user to type in data ret connect call fgets ret accept write call read call read write end read Lecture 1: Introduction close close

103 Possible Mechanisms for Creating Concurrent Flows
1. Processes Kernel automatically interleaves multiple logical flows. Each flow has its own private address space. 2. I/O multiplexing with select() User manually interleaves multiple logical flows. Each flow shares the same address space. Popular for high-performance server designs. 3. Threads Our Focus Lecture 1: Introduction

104 Event-Based Concurrent Servers Using I/O Multiplexing
Maintain a pool of connected descriptors. Repeat the following forever: Use the Unix select function to block until: (a) New connection request arrives on the listening descriptor. (b) New data arrives on an existing connected descriptor. If (a), add the new connection to the pool of connections. If (b), read any available data from the connection Close connection on EOF and remove it from the pool. Lecture 1: Introduction

105 The select Function Lecture 1: Introduction 8-30-05
select() sleeps until one or more file descriptors in the set readset ready for reading or one or more descriptors in writeset ready for writing #include <sys/select.h> int select(int maxfdp1, fd_set *readset, fd_set *writeset, NULL, NULL); readset Opaque bit vector (max FD_SETSIZE bits) that indicates membership in a descriptor set. On Linux machines, FD_SETSIZE = 1024 If bit k is 1, then descriptor k is a member of the descriptor set. When call select, should have readset indicate which descriptors to test writeset writeset is similar but refers to descriptors ready for writing maxfdp1 Maximum descriptor in descriptor set plus 1. Tests descriptors 0, 1, 2, ..., maxfdp1 - 1 for set membership. Lecture 1: Introduction select() returns the number of ready descriptors and keeps on each bit of readset for which corresponding descriptor is ready

106 Macros for Manipulating Set Descriptors
void FD_ZERO(fd_set *fdset); Turn off all bits in fdset. void FD_SET(int fd, fd_set *fdset); Turn on bit fd in fdset. void FD_CLR(int fd, fd_set *fdset); Turn off bit fd in fdset. int FD_ISSET(int fd, *fdset); Is bit fd in fdset turned on? Lecture 1: Introduction

107 Event-based Concurrent Echo Server
/* * echoservers.c - A concurrent echo server based on select */ #include "csapp.h" typedef struct { /* represents a pool of connected descriptors */ int maxfd; /* largest descriptor in read_set */ fd_set read_set; /* set of all active descriptors */ fd_set ready_set; /* subset of descriptors ready for reading */ int nready; /* number of ready descriptors from select */ int maxi; /* highwater index into client array */ int clientfd[FD_SETSIZE]; /* set of active descriptors */ rio_t clientrio[FD_SETSIZE]; /* set of active read buffers */ } pool; int byte_cnt = 0; /* counts total bytes received by server */ Lecture 1: Introduction

108 Event-based Concurrent Server (cont)
int main(int argc, char **argv) { int listenfd, connfd, clientlen = sizeof(struct sockaddr_in); struct sockaddr_in clientaddr; static pool pool; listenfd = Open_listenfd(argv[1]); init_pool(listenfd, &pool); while (1) { pool.ready_set = pool.read_set; pool.nready = Select(pool.maxfd+1, &pool.ready_set, NULL, NULL, NULL); if (FD_ISSET(listenfd, &pool.ready_set)) { connfd = Accept(listenfd, (SA *)&clientaddr,&clientlen); add_client(connfd, &pool); } check_clients(&pool); Lecture 1: Introduction

109 Event-based Concurrent Server (cont)
/* initialize the descriptor pool */ void init_pool(int listenfd, pool *p) { /* Initially, there are no connected descriptors */ int i; p->maxi = -1; for (i=0; i< FD_SETSIZE; i++) p->clientfd[i] = -1; /* Initially, listenfd is only member of select read set */ p->maxfd = listenfd; FD_ZERO(&p->read_set); FD_SET(listenfd, &p->read_set); } Lecture 1: Introduction

110 Event-based Concurrent Server (cont)
void add_client(int connfd, pool *p) /* add connfd to pool p */ { int i; p->nready--; for (i = 0; i < FD_SETSIZE; i++) /* Find available slot */ if (p->clientfd[i] < 0) { p->clientfd[i] = connfd; Rio_readinitb(&p->clientrio[i], connfd); FD_SET(connfd, &p->read_set); /* Add desc to read set */ if (connfd > p->maxfd) /* Update max descriptor num */ p->maxfd = connfd; if (i > p->maxi) /* Update pool high water mark */ p->maxi = i; break; } if (i == FD_SETSIZE) /* Couldn't find an empty slot */ app_error("add_client error: Too many clients"); Lecture 1: Introduction

111 Event-based Concurrent Server (cont)
void check_clients(pool *p) { /* echo line from ready descs in pool p */ int i, connfd, n; char buf[MAXLINE]; rio_t rio; for (i = 0; (i <= p->maxi) && (p->nready > 0); i++) { connfd = p->clientfd[i]; rio = p->clientrio[i]; /* If the descriptor is ready, echo a text line from it */ if ((connfd > 0) && (FD_ISSET(connfd, &p->ready_set))) { p->nready--; if ((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0) { byte_cnt += n; Rio_writen(connfd, buf, n); } else {/* EOF detected, remove descriptor from pool */ Close(connfd); FD_CLR(connfd, &p->read_set); p->clientfd[i] = -1; Lecture 1: Introduction

112 Pro and Cons of Event-Based Designs
+ One logical control flow. + Can single-step with a debugger. + No process or thread control overhead. Design of choice for high-performance Web servers and search engines. - Significantly more complex to code than process- or thread-based designs. - Can be vulnerable to denial of service attacks How? Lecture 1: Introduction

113 Attack #1 Overwhelm Server with Connections Defenses?
Limited to FD_SETSIZE – 4 (typically 1020) connections Defenses? Lecture 1: Introduction

114 Attack #2: Partial Lines
client 1 server client 2 connect accept User types “Hello world\n” Client sends “Hello world” call readlineb select detects input ready Client 2 blocks waiting to complete its connection request until after lunch! Server blocks waiting for “\n” from Client 1 connect Client gets attention of server by sending partial line Server blocks until line completed Lecture 1: Introduction

115 Flaky Client Sends everything up to newline
while (Fgets(buf, MAXLINE, stdin) != NULL) { Rio_writen(clientfd, buf, strlen(buf)-1); Fgets(buf, MAXLINE, stdin); /* Read & ignore line */ Rio_writen(clientfd, "\n", 1); Rio_readlineb(&rio, buf, MAXLINE); Fputs(buf, stdout); } Sends everything up to newline Doesn’t send newline until user types another line Meanwhile, server will block Lecture 1: Introduction

116 Implementing a Robust Server
Break Up Reading Line into Multiple Partial Reads Every time connection selected, read as much as is available Construct line in separate buffer for each connection Must Use Unix Read read(int fd, void *buf, size_t maxn) Read as many bytes as are available from file fd into buffer buf. Up to maximum of maxn bytes Cannot Use RIO Version rio_readn(int fd, void *buf, size_t n) Read n bytes into buffer buf. Blocks until all n read or EOF Lecture 1: Introduction

117 Robust Server Lecture 1: Introduction 8-30-05 /*
* echoserverub.c - A robust, concurrent echo server based on select */ #include "csapp.h" typedef struct { /* represents a pool of connected descriptors */ int maxfd; /* largest descriptor in read_set */ fd_set read_set; /* set of all active descriptors */ fd_set ready_set; /* subset of descriptors ready for reading */ int nready; /* number of ready descriptors from select */ int maxi; /* highwater index into client array */ int clientfd[FD_SETSIZE]; /* set of active descriptors */ char clientbuf[FD_SETSIZE][MAXBUF]; /* set of read buffers */ int clientcnt[FD_SETSIZE]; /* Count of characters in buffers */ } pool; int byte_cnt = 0; /* counts total bytes received by server */ Lecture 1: Introduction

118 Robust Server Loop Lecture 1: Introduction 8-30-05
void check_clients(pool *p) { int i, connfd, n; for (i = 0; (i <= p->maxi) && (p->nready > 0); i++) { connfd = p->clientfd[i]; char *buf = p->clientbuf[i]; /* Private buffer */ int cnt = p->clientcnt[i]; /* Number of chars read so far */ if ((connfd > 0) && (FD_ISSET(connfd, &p->ready_set))) { p->nready--; if ((n = Read(connfd, buf+cnt, MAXBUF-cnt)) != 0) { byte_cnt += n; cnt += n; if (buf[cnt-1] == '\n') { Write(connfd, buf, cnt); /* End of line */ p->clientcnt[i] = 0; } else p->clientcnt[i] = cnt; } Lecture 1: Introduction

119 NOTE (Just to complicate things…)
If a client sends x bytes of data in one write() call, it is NOT guaranteed that all x bytes will be received in a single read() call by the server. i.e., the following scenario is possible: Client writes “Hello world\n” to server Server’s select() notices & unblocks, server then calls read() read() returns “Hell” A subsequent call to read() returns “o w” A subsequent call to read() returns “orld\n” Server’s solution: maintain a buffer for each of your clients, and only process the buffer’s contents when a message has been received in full (note: the type of application determines what a ‘message’ is, and what indicates it has been fully received) Lecture 1: Introduction

120 Conceptual Model Maintain State Machine for Each Connection
First Version: State is just identity of connfd Second Version: State includes partial line + count of characters select Determines Which State Machine to Update First Version: Process entire line Second Version: Process as much of line as is available Design Issue Must set granularity of state machine to avoid server blocking Lecture 1: Introduction

121 For More Information W. Richard Stevens, Unix Network Programming: Networking APIs: Sockets and XTI, Volume 1, Second Edition, Prentice Hall, 1998. THE network programming bible. Complete versions of original echo client and server are developed in Computer Systems: A Programmer’s Perspective. Available from csapp.cs.cmu.edu Compile and run them for yourselves to see how they work. Feel free to borrow any of this code. But be careful---it isn’t sufficiently robust for our programming assignments Most routines exit when any kind of error encountered Lecture 1: Introduction

122 For More Information What’s inside the RIO wrappers
int Select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout) { int rc; if ((rc = select(n, readfds, writefds, exceptfds, timeout)) < 0) unix_error("Select error"); return rc; } Lecture 1: Introduction

123 GDB and Version Control (RCS/CVS)
Tools to help make programming task simpler GDB helps the debugging task RCS or CVS help with the task of maintaining your code across multiple revisions Neither system is magic Debugging Program defensively – check error returns, buffer sizes Build program in a modular fashion Print sensible error messages May want to do better than the built-in wrapper functions Version control Keep a clear idea of who is doing what Define interfaces between modules early and try not to change them too much Lecture 1: Introduction

124 Debugging with GDB Prepare program for debugging
Compile with “-g” (keep full symbol table) Don’t use compiler optimization (“-O”, “–O2”, …) Two main ways to run gdb On program directly gdb progname Once gdb is executing we can execute the program with: run args Can use shell-style redirection e.g. run < infile > /dev/null On a core (post-mortem) gdb progname core Useful for examining program state at the point of crash Extensive in-program documentation exists help (or help <topic> or help <command> ) Lecture 1: Introduction

125 Controlling Your Program With GDB
Stopping your program with breakpoints Program will run until it encounters a breakpoint To start running again: cont Break command format break foo.c:4 stops at the 4th source line of foo.c break 16 stops at the 16th source line of the current source file break main stops upon entry to the main() function Stop your program with SIGINT (CTRL-C) Useful if program hangs (sometimes) Stepping through your program step N command: steps through N source lines (default 1) next is like step but it treats function calls as a single line Hint: avoid writing mega-expressions Hard to step through foo(bar(tmp = baz(), tmp2 = baz2())) Lecture 1: Introduction

126 Examining the State of Your Program
backtrace ( bt for short) Shows stack trace (navigate procedures using up and down) bt full prints out all available local variables as well print EXP Print the value of expression Can print out values in variables x/<count><format><size> ADDR Examine a memory region at ADDR Count is the number of items to display (default: 1) Format is a single letter code o(octal), x(hex), d(decimal), u(unsigned decimal), t(binary), f(float), a(address), i(instruction), c(char) and s(string) Size is the size of the items (single letter code) b(byte), h(halfword), w(word), g(giant, 8 bytes) Lecture 1: Introduction

127 Version Control with RCS
Version control systems: Maintain multiple versions of a file Allow rollback to old versions Enforce documentation of changes Allows multiple programmers to work on a project without accidentally editing the same file Files must be ‘checked out’ for reading or writing RCS maintains a database of all revisions Make a subdirectory called ‘RCS’ in each working directory Otherwise RCS will do its business in your directory – ugly! If your file is called ‘assignment1/foo.c’, RCS keeps update history in ‘assignment1/RCS/foo.c,v’ Current version of ‘foo.c’ is maintained in ‘foo.c,v’ ‘deltas’ allow retrieval of older versions Lecture 1: Introduction

128 Creating RCS Files After making ‘RCS’ subdirectory…
Initialize RCS for your file mysource.c (assume you have already created it) by checking it in ( ci ) Can also create a blank file with rcs –i mysource.c Either way, this produces version 1.1 ~/tmp]$ ci mysource.c RCS/mysource.c,v <-- mysource.c enter description, terminated with single '.' or end of file: NOTE: This is NOT the log message! >> This source file contains a simple algorithm to solve the Halting Problem. >> . initial revision: 1.1 done Lecture 1: Introduction

129 Checking out files Lecture 1: Introduction 8-30-05
In previous example, after ci, mysource.c is gone! To retrieve mysource.c, use co command (‘check out’) Note: permissions don’t let us change mysource.c To change mysource.c, must acquire lock co –l mysource.c locks mysource.c so no one else can change it Use ci to check the code back in when done (adding a log message) ~/tmp]$ ls -l total 2 drwxr-xr-x 2 geoffl users Jan 19 15:39 RCS ~/tmp]$ co mysource.c RCS/mysource.c,v --> mysource.c revision 1.1 done total 3 -r--r--r geoffl users Jan 19 15:39 mysource.c Lecture 1: Introduction

130 Versions Each version of the file has a version number
“release.revision” format – e.g. 4.2 is release 4, revision 2 Doesn’t necessarily correspond to anything about real world version numbers By default, each ci of a changed file increments revision number by 1 Can use -r flag to specify version numbers Use this with co to retrieve old versions Use this with ci to specify what a new version should be called Note: can’t go backwards! ci –r1.8 mysource.c will check in mysource.c with version number 1.8 ci –r2 mysource.c will check in mysource.c with version 2.1 Lecture 1: Introduction

131 Version Control with CVS
Similar to RCS, but newer and with more functionality Like RCS, Maintains multiple versions of a file Allow rollback to old versions Enforce documentation of changes Allows multiple programmers to work on the same file at the same time Upon checkin, if conflicts exist, the user is notified and can resolve them manually using a diff program (i.e. diff on UNIX, ExamDiff on Windows) Easy to use on the UNIX command line Has a very, very friendly client for Windows users that integrates into your folder’s file management system called “TortoiseCVS” Lecture 1: Introduction

132 Version Control continued
RCS and CVS both maintain a database of all revisions Make a subdirectory called ‘RCS’ in each working directory Otherwise RCS will do its business in your directory – ugly! Note: CVS does this for you automatically If your file is called ‘assignment1/foo.c’, RCS keeps update history in ‘assignment1/RCS/foo.c,v’, CVS similarly Current version of ‘foo.c’ is maintained in ‘foo.c,v’ ‘deltas’ allow retrieval of older versions More information about CVS (my personal choice) for Windows users Lecture 1: Introduction

133 More information… GDB RCS
Official GDB homepage: GDB primer: RCS Look at man rcs, man rcsintro Official RCS homepage: Other useful features ci –l: check-in a version but keep the file and the lock ci –u: check-in a version but keep a read-only version of file rcsdiff: display differences between versions rcsmerge: merge changes in different versions of a file Note: you can break locks if necessary RCS will send to owner of broken lock Lecture 1: Introduction

134 Lecture 1: Introduction

135 More tips for the past If you want to work directly on windows, you can use MinGW or Cygwin, *BUT TEST ON UNIX* Most of the APIs you will use will have many functions that return -1 to indicate error. Unlike previous classes, you must be able to recover when appropriate, and handle these errors on a case-by-case basis. Make buffers for *each* of your clients; don’t process a command until you know you’ve received the entire thing. Lecture 1: Introduction

136 Finally… These projects are about implementing specifications.
Read the writeup at least twice before starting. PLAN on paper (even draw) BEFORE coding. PLAN, PLAN, PLAN. Then coding and debugging will be much easier, and you won’t have to delete hundreds of lines of useless code over and over again. Good luck! Lecture 1: Introduction


Download ppt "What is the Objective of Networking?"

Similar presentations


Ads by Google