www.it-ebooks.info TCP/IP ARCHITECTURE, DESIGN, AND IMPLEMENTATION IN LINUX www.it-ebooks.info Press Operating CommitteeChair Linda Shafer formerDirector,SoftwareQualityInstituteTheUniversityofTexasatAustinEditor-in-Chief Alan Clements ProfessorUniversityofTeessideBoard Members Mark J. Christensen, IndependentConsultant Phillip Laplante, AssociateProfessorSoftwareEngineering,PennStateUniversity Richard Thayer, ProfessorEmeritus,CaliforniaStateUniversity,Sacramento Donald F. Shafer, ChiefTechnologyOffi cer,AthensGroup,Inc. James Conrad ,AssociateProfessorUNCCharlotte Janet Wilson, Product Manager, CSPressIEEE Computer Society Publications The world-renowned IEEE Computer Society publishes, promotes, and distributes a wide variety of authoritative computer science and engineering texts. These books are available from most retail outlets. Visit the CS Store at http: //computer.org/cspress for a list of products. IEEE Computer Society / Wiley Partnership The IEEE Computer Society and Wiley partnership allows the CS Press authored book program to produce a number of exciting new titles in areas of computer science, computing and networking with a special focus on software engineering.
IEEE Computer Society members continue to receive a 15% discount on these titles when purchased through Wiley or at wiley.com/ieeecs To submit questions about the program or send proposals please e-mail jwilson@computer.org or write to Books, IEEE Computer Society, 10662 Los Vaqueros Circle, Los Alamitos, CA 90720-1314. Tele-phone +1-714-821-8380. AdditionalinformationregardingtheComputerSocietyauthoredbookprogramcanalsobeaccessedfromourwebsiteathttp: //computer.org/cspress. www.it-ebooks.info TCP/IP ARCHITECTURE, DESIGN, AND IMPLEMENTATION IN LINUX Sameer Seth M. Ajaykumar Venkatesulu A JOHN WILEY & SONS, INC., PUBLICATION www.it-ebooks.info Copyright 2008 by IEEE Computer Society. Published by John Wiley & Sons, Inc., Hoboken, New Jersey. All rights reserved.
Published simultaneously in Canada No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning, or otherwise, except as permitted under Section 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, Inc., 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 750-4470, or on the web at www.copyright.com. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permission. Limit of Liability/Disclaimer of Warranty: While the publisher and author have used their best efforts in preparing this book, they make no representations or warranties with respect to the accuracy or completeness of the contents of this book and specifi cally disclaim any implied warranties of merchantability or fi tness for a particular purpose. No warranty may be created or extended by sales representatives or written sales materials. The advice and strategies contained herein may not be suitable for your situation. You should consult with a professional where appropriate.
Neither the publisher nor author shall be liable for any loss of profi t or any other commercial damages, including but not limited to special, incidental, consequential, or other damages. For general information on our other products and services or for technical support please contact our Customer Care Department within the United States at (800) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002. Wiley also publishes its books in a variety of electronic formats. Some content that appears in print, may not be available in electronic formats. For more information about Wiley products, visit our web site at www.wiley.com. LibraryofCongressCataloging-in-PublicationDataisavailable. ISBN 978-0470-14773-3 Printed in the United States of America 10 9 8 7 6 5 4 3 2 1 www.it-ebooks.info CONTENTS
Preface xxi
Acknowledgments xxvii
1 INTRODUCTION 1.1 Overview of TCP/IP Stack 1.1.1 Moving Down the Stack 1.1.2 Moving Up the Stack 1.2 Source Code Organization for Linux 2.4.20 1.2.1 Source Code Organization for Networking Code 1.3 TCP/IP Stack and Kernel Control Paths 1.4 Linux Kernel Until Version 2.4 Is Non-preemptible 1.4.1 System Call on Linux 1.4.2 Adding New System Call 1.5 Linux Process and Thread 1.5.1
fork() 17 1.5.2 Thread 1.5.3 Kernel Threads 1.6 Kernel Synchronization Mechanism 1.6.1 Semaphore 1.6.2 Atomic Operations 1.6.3 Spin Lock 1.7 Application Interfaces for TCP/IP Programming 1.7.1 Server Application 1.7.2 Client Application 1.7.3 Socket Options 1.7.4 Option Values 1.8 Shutdown 1.8.1 Kernel Shutdown Implementation 1.8.2 Send Shutdown 1.8.3 Receive Shutdown 1.9 I/O 1.9.1
read() 38 1.9.2
write() 38
v www.it-ebooks.info
vi CONTENTS 1.9.3
recv() 38 1.9.4
send() 39 1.9.5
select() 39 1.10 TCP State 1.10.1 Partial Close 1.10.2 tcpdump Output for Partial Close 1.11 Summary
2 PROTOCOL FUNDAMENTALS 2.1 TCP 2.1.1 TCP Header 2.2 TCP Options (RFC 1323) 2.2.1 mss Option 2.2.2 Window-Scaling Option 2.2.3 Timestamp Option 2.2.4 Selective Acknowledgment Option 2.3 TCP Data Flow 2.3.1 ACKing of Data Segments 2.4 Delayed Acknowledgment 2.5 Nagles Algorithm (RFC 896) 2.6 TCP Sliding Window Protocol 2.7 Maximizing TCP Throughput 2.8 TCP Timers 2.8.1 Retransmission Timer 2.8.2 Persistent Timer 2.8.3 Keepalive Timer 2.8.4 TIME_WAIT Timer 2.9 TCP Congestion Control 2.10 TCP Performance and Reliability 2.10.1 RTTD 2.10.2 SACK/DSACK 2.10.3 Window Scaling 2.11 IP (Internet Protocol) 2.11.1 IP Header 2.12 Routing 2.13
netstat 90 2.14
traceroute 92 2.14.1
traceroute Mechanism 2.15 ICMP 2.16
ping 95 2.17 ARP/RARP 2.18 Summary www.it-ebooks.info CONTENTS
vii3 KERNEL IMPLEMENTATION OF SOCKETS 3.1 Socket Layer 3.2 VFS and Socket 3.3 Protocol Socket Registration 3.4
struct inet_protosw 107 3.5 Socket Organization in the Kernel 3.6 Socket 3.7
inet_create 110 3.7.1 Sock 3.8 Flow Diagram for Socket Call 3.9 Summary
4 KERNEL IMPLEMENTATION OF TCP CONNECTION SETUP 4.1 Connection Setup 4.1.1 Server Side Setup 4.1.2 Server Side Operations 4.2 Bind 4.2.1 Data Structures Related to Socket BIND 4.2.2 Hash Buckets for tcp Bind 4.2.3
tcp_ehash 125 4.2.4
tcp_listening_hash 125 4.2.5
tcp_bhash 125 4.2.6
tcp_hashinfo 126 4.2.7
tcp_bind_hashbucket 129 4.2.8
tcp_bind_bucket 129 4.2.9
bind() 130 4.2.10
sys_bind() 130 4.2.11
sockfd_lookup() 130 4.2.12
fget() 131 4.2.13
inet_bind() 131 4.2.14
tcp_v4_get_port() 133 4.2.15
tcp_bind_confl ict() 135 4.3 Listen 4.3.1
Next page