introduction to network simulator ns2 - springer978-0-387-71760-9/1.pdf · viii preface modules,...

15
Introduction to Network Simulator NS2

Upload: hoangtuyen

Post on 20-Jun-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

Introduction to NetworkSimulator NS2

Teerawat Issariyakul • Ekram Hossain

Introduction to NetworkSimulator NS2

123

Teerawat Issariyakul Ekram HossainTOT Public Company Limited Department of Electrical &89/2 Moo 3 Chaengwattana Rd. Computer EngineeringThungsonghong, Laksi University of ManitobaBangkok, Thailand 10210 75A Chancellor’s [email protected] Winnipeg MB R3T [email protected] Canada

[email protected]

ISBN: 978-0-387-71759-3 e-ISBN: 978-0-387-71760-9DOI: 10.1007/978-0-387-71760-9

Library of Congress Control Number: 2008928147

c© 2009 Springer Science+Business Media, LLCAll rights reserved. This work may not be translated or copied in whole or in part without the writtenpermission of the publisher (Springer Science+Business Media, LLC, 233 Spring Street, New York, NY10013, USA), except for brief excerpts in connection with reviews or scholarly analysis. Use in connec-tion with any form of information storage and retrieval, electronic adaptation, computer software, or bysimilar or dissimilar methodology now known or hereafter developed is forbidden.The use in this publication of trade names, trademarks, service marks, and similar terms, even if they arenot identified as such, is not to be taken as an expression of opinion as to whether or not they are subjectto proprietary rights.

Printed on acid-free paper

springer.com

To our families

Preface

NS2 is an open-source event-driven simulator designed specifically for researchin computer communication networks. Since its inception in 1989, NS2 hascontinuously gained tremendous interest from industry, academia, and govern-ment. Having been under constant investigation and enhancement for years,NS2 now contains modules for numerous network components such as routing,transport layer protocol, application, etc. To investigate network performance,researchers can simply use an easy-to-use scripting language to configure a net-work, and observe results generated by NS2. Undoubtedly, NS2 has becomethe most widely used open source network simulator, and one of the mostwidely used network simulators.

Unfortunately, most research needs simulation modules which are beyondthe scope of the built-in NS2 modules. Incorporating these modules into NS2requires profound understanding of NS2 architecture. Currently, most NS2beginners rely on online tutorials. Most of the available information mainlyexplains how to configure a network and collect results, but does not includesufficient information for building additional modules in NS2. Despite its de-tails about NS2 modules, the formal documentation of NS2 is mainly writtenas a reference book, and does not provide much information for beginners. Thelack of guidelines for extending NS2 is perhaps the greatest obstacle, whichdiscourages numerous researchers from using NS2. At this moment, there isno guide book which can help the beginners understand the architecture ofNS2 in depth.

The objective of this textbook is to act as a primer for NS2 beginners.The book provides information required to install NS2, run simple examples,modify the existing NS2 modules, and create as well as incorporate new mod-ules into NS2. To this end, the details of several built-in NS2 modules areexplained in a comprehensive manner.

NS2 by itself contains numerous modules. As time elapses, researchers keepdeveloping new NS2 modules. This book does not include the details of all NS2

VIII Preface

modules, but does so for selected modules necessary to understand the basicsof NS2. For example, it leaves out the widely used modules such as wirelessnode or web caching. We believe that once the basics of NS2 are grasped,the readers can go through other documentations, and readily understand thedetails of other NS2 components. The details of Network AniMator (NAM)and Xgraph are also omitted here. We understand that these two tools arenice to have and could greatly facilitate simulation and analysis of computernetworks. However, we believe that they are not essential to the understandingof the NS2 concept, and their information are widely available through mostof the online tutorials.

This textbook can be used by researchers who need to use NS2 for commu-nication network performance evaluation based on simulation. Also, it can beused as a reference textbook for laboratory works for a senior undergraduatelevel course or a graduate level course on telecommunication networks offeredin Electrical and Computer Engineering and Computer Science Programs.Potential courses include “Network Simulation and Modeling”, “ComputerNetworks”, “Data Communications”, “Wireless Communications and Net-working”, “Special Topics on Telecommunications”. In a fifteen-class course,we suggest the first class for an introduction to programming (Appendix A),and other 14 classes for each of the 14 chapters. Alternately, the instruc-tor may allocate 10 classes for teaching and 5 classes for term projects. Inthis case, we suggest that the materials presented in this book are taughtin the following order: Chapters 1–2, 3, 12, 4–5, 6, 7–8, 9–11, 13 and 14.When the schedule is really tight, we suggest the readers to go through Chap-ters 2, 4–7, and 9–10. The readers may start by getting to know NS2 in Chap-ter 2, and learn the main concepts of NS2 in Chapters 4–5. Chapters 6–7 and9–10 present the details of most widely used NS2 modules. From time to time,the readers may need to visit Chapter 3, 8, and 12 for further information.If tracing is required, the readers may also have to go through Chapter 13.Finally, Chapter 14 would be useful for those who need to extend NS2 beyondit scopes.

We recommend the readers who intend to go through the entire book toproceed chapter by chapter. A summary of all the chapters in this book isprovided below.

As the opening chapter, Chapter 1 gives an introduction to computernetworks and network simulation. The emphasis is on event-driven simulationfrom which NS2 is developed.

An overview of Network Simulator 2 (NS2) is discussed in Chapter 2.Here, we briefly show the two-language NS2 architecture, NS2 directory andthe conventions used in this book, and NS2 installation guidelines for UNIXand Windows systems. We also present a three-step simulation formulationas well as a simple example of NS2 simulation. Finally, we demonstrate howto use the make utility to incorporate new modules into NS2.

Chapter 3 explains the details of the NS2 two language structure, whichconsists of the following six main C++ classes: Tcl, Instvar, TclObject,

Preface IX

TclClass, TclCommand, and EmbeddedTcl. Chapters 4–5 present the verymain simulation concept of NS2. While Chapter 4 explains implementation ofevent-driven simulation in NS2, Chapter 5 focuses on network objects as wellas packet forwarding mechanism.

Chapters 6–11 present the following six most widely used NS2 modules.First, nodes (Chapter 6) act as routers and computer hosts. Secondly, links,particularly SimpleLink objects (Chapter 7), deliver packets from one net-work object to another. They model packet transmission time as well as packetbuffering. Thirdly, packets (Chapter 8) contain necessary information in itsheader. Fourthly, agents (Chapters 9–10) are responsible for generating pack-ets. NS2 has two main transport-layer agents: TCP and UDP agents. Finally,applications (Chapter 11) model the user demand for data transmission.

Chapter 12 presents three helper modules: timers, random number gen-erators, and error models. It also discusses the concepts of two bit-wise op-erations, namely, bit masking and bit shifting, which are used throughoutNS2.

Chapter 13 summarizes the post-simulation process, which consists ofthree main parts: debugging, variable and packet tracing, and result compila-tion.

After discussing all the NS components, Chapter 14 demonstrates howa new module is developed and integrated into NS2 through two followingexamples: Automatic Repeat reQuest (ARQ) and packet schedulers.

Appendices A and B provide programming details which could be usefulfor the beginners. These details include an introduction to Tcl, OTcl, andAWK programming languages as well as a review of the polymorphism OOPconcept.

As the final words, we would like to express sincere gratitude to our col-leagues, especially, Surachai Chieochan, at the University of Manitoba, andthe colleagues at TOT Public Company Limited, Bangkok, Thailand, for theircontinuous support. Last but not the least, we would like to acknowledge ourfamilies as well as our partners – Wannasorn and Rumana – for their incessantmoral support and patient understanding throughout this endeavor.

TOT Public Company Limited Teerawat IssariyakulUniversity of Manitoba Ekram HossainJuly 2008

Contents

1 Simulation of Computer Networks . . . . . . . . . . . . . . . . . . . . . . . . . 11.1 Computer Networks and the Layering Concept . . . . . . . . . . . . . . 1

1.1.1 Layering Concept . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21.1.2 OSI and TCP/IP Reference Models . . . . . . . . . . . . . . . . . 3

1.2 System Modeling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51.2.1 Analytical Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61.2.2 Simulation Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

1.3 Basics of Computer Network Simulation . . . . . . . . . . . . . . . . . . . . 61.3.1 Simulation: The Formal Definition . . . . . . . . . . . . . . . . . . 71.3.2 Elements of Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

1.4 Time-Dependent Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91.4.1 Time-Driven Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . 91.4.2 Event-Driven Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . 10

1.5 A Simulation Example: A Single-Channel Queuing System . . . . 121.6 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

2 Introduction to Network Simulator 2 (NS2) . . . . . . . . . . . . . . . 192.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192.2 Basic Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202.3 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

2.3.1 Installing an All-In-One NS2 Suite on Unix-BasedSystems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

2.3.2 Installing an All-In-One NS2 Suite onWindows-Based Systems . . . . . . . . . . . . . . . . . . . . . . . . . . 22

2.4 Directories and Convention . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232.4.1 Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232.4.2 Convention . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

2.5 Running NS2 Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262.5.1 NS2 Program Invocation . . . . . . . . . . . . . . . . . . . . . . . . . . 262.5.2 Main NS2 Simulation Steps . . . . . . . . . . . . . . . . . . . . . . . . 26

2.6 A Simulation Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

XII Contents

2.7 Including C++ Modules into NS2 and the make Utility . . . . . . 332.7.1 An Invocation of a Make Utility . . . . . . . . . . . . . . . . . . . . 332.7.2 A Make Descriptor File . . . . . . . . . . . . . . . . . . . . . . . . . . . 332.7.3 NS2 Descriptor File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

2.8 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

3 Linkage Between OTcl and C++ in NS2 . . . . . . . . . . . . . . . . . . . 373.1 The Two-Language Concept in NS2 . . . . . . . . . . . . . . . . . . . . . . . . 383.2 Class Tcl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

3.2.1 Obtain a Reference to the Tcl Instance . . . . . . . . . . . . . . 423.2.2 Invoking a Tcl Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . 433.2.3 Pass or Receive Results to/from the Interpreter . . . . . . 443.2.4 Reporting Error and Quitting the Program . . . . . . . . . . 453.2.5 Retrieve the Reference to TclObjects . . . . . . . . . . . . . . . 45

3.3 Class InstVar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463.3.1 Real and Integer Variables . . . . . . . . . . . . . . . . . . . . . . . . . 473.3.2 Bandwidth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473.3.3 Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483.3.4 Boolean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

3.4 Class TclObject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493.4.1 Reference to a TclObject . . . . . . . . . . . . . . . . . . . . . . . . . . 503.4.2 Creating and Destroying a Shadow TclObject . . . . . . . . 503.4.3 Binding Variables in the Compiled and Interpreted

Hierarchies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543.4.4 OTcl Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56

3.5 Class TclClass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613.5.1 An Overview of Class TclClass . . . . . . . . . . . . . . . . . . . . 613.5.2 TclObject Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623.5.3 Naming Convention for Class TclClass . . . . . . . . . . . . . 623.5.4 Instantiation of Mapping Variables . . . . . . . . . . . . . . . . . 63

3.6 Class TclCommand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633.6.1 Invoking a TclCommand . . . . . . . . . . . . . . . . . . . . . . . . . . 633.6.2 Creating a TclCommand . . . . . . . . . . . . . . . . . . . . . . . . . . 643.6.3 Defining Your Own TclCommand . . . . . . . . . . . . . . . . . . 65

3.7 Class EmbeddedTcl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663.8 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

4 Implementation of Discrete-Event Simulation in NS2 . . . . . . 694.1 NS2 Simulation Concept . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 704.2 Events and Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

4.2.1 An Overview of Events and Handlers . . . . . . . . . . . . . . . 704.2.2 Class NsObject: A Child Class of Class Handler . . . . . 714.2.3 Classes Packet and AtEvent: Child Classes of Class

Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724.3 The Scheduler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74

4.3.1 Main Components of the Scheduler . . . . . . . . . . . . . . . . . 74

Contents XIII

4.3.2 Data Encapsulation and Polymorphism Concepts . . . . . 754.3.3 Main Functions of the Scheduler . . . . . . . . . . . . . . . . . . . 754.3.4 Dynamics of the Unique ID of an Event . . . . . . . . . . . . . 774.3.5 Scheduling-Dispatching Mechanism . . . . . . . . . . . . . . . . . 784.3.6 Null Event and Dummy Event Scheduling . . . . . . . . . . . 78

4.4 The Simulator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 804.4.1 Main Components of a Simulation . . . . . . . . . . . . . . . . . . 814.4.2 Retrieving the Instance of the Simulator . . . . . . . . . . . . 824.4.3 Simulator Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . 834.4.4 Running Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83

4.5 Instprocs of OTcl Class Simulator . . . . . . . . . . . . . . . . . . . . . . . . . 844.6 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84

5 Network Objects: Creation, Configuration, and PacketForwarding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 875.1 Overview of NS2 Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87

5.1.1 Functionality-Based Classification of NS2 Modules . . . . 875.1.2 C++ Class Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88

5.2 NsObjects: A Network Object Template . . . . . . . . . . . . . . . . . . . . 905.2.1 Class NsObject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 905.2.2 Packet Forwarding Mechanism of NsObjects . . . . . . . . . 91

5.3 Connectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 915.3.1 Class Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 925.3.2 OTcl Configuration Commands . . . . . . . . . . . . . . . . . . . . 935.3.3 Packet Forwarding Mechanism . . . . . . . . . . . . . . . . . . . . . 96

5.4 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98

6 Nodes as Routers or Computer Hosts . . . . . . . . . . . . . . . . . . . . . 996.1 An Overview of Nodes in NS2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99

6.1.1 Architecture of a Node . . . . . . . . . . . . . . . . . . . . . . . . . . . . 996.1.2 Related Instproc of Class Node . . . . . . . . . . . . . . . . . . . . 1016.1.3 Default Nodes and Node Configuration Interface . . . . . 102

6.2 Routing Mechanism in NS2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1036.3 Route Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1056.4 Classifiers: Multi-target Packet Forwarders . . . . . . . . . . . . . . . . . . 107

6.4.1 Class Classifier and Its Main Components . . . . . . . . 1076.4.2 Hash Classifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1116.4.3 Port Classifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1166.4.4 Installing Classifiers in a Node . . . . . . . . . . . . . . . . . . . . . 116

6.5 Routing Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1186.5.1 An Overview of Routing Modules . . . . . . . . . . . . . . . . . . 1186.5.2 C++ Class RoutingModule . . . . . . . . . . . . . . . . . . . . . . . . 1216.5.3 OTcl Class RtModule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1246.5.4 C++ Class BaseRoutingModuleand OTcl class

RtModule/Base . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125

XIV Contents

6.6 Node Object Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1256.6.1 Relationship Among Instvars module list ,

reg module , rtnotif , and ptnotif . . . . . . . . . . . . . . 1266.6.2 Adding/Deleting a Routing Entry . . . . . . . . . . . . . . . . . . 1276.6.3 Agent Attachment/Detachment . . . . . . . . . . . . . . . . . . . . 1276.6.4 Node Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1286.6.5 Route Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133

6.7 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137

7 Link and Buffer Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1397.1 Introduction to SimpleLink Objects . . . . . . . . . . . . . . . . . . . . . . . . 139

7.1.1 Main Components of a SimpleLink . . . . . . . . . . . . . . . . . 1397.1.2 Instprocs for Configuring a SimpleLink Object . . . . . . 1407.1.3 The Constructor of Class SimpleLink . . . . . . . . . . . . . . . 142

7.2 Modeling Packet Departure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1437.2.1 Packet Departure Mechanism . . . . . . . . . . . . . . . . . . . . . . 1437.2.2 C++ Class LinkDelay . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144

7.3 Buffer Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1467.3.1 Class PacketQueue: A Model for Packet Buffering . . . . 1477.3.2 Queue Handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1487.3.3 Queue Blocking and Callback Mechanism . . . . . . . . . . . 1497.3.4 Class DropTail: A Child Class of Class Queue . . . . . . . 151

7.4 A Sample Two-Node Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1527.4.1 Network Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1537.4.2 Packet Flow Mechanism . . . . . . . . . . . . . . . . . . . . . . . . . . . 154

7.5 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154

8 Packets, Packet Headers, and Header Format . . . . . . . . . . . . . . 1578.1 An Overview of Packet Modeling Principle . . . . . . . . . . . . . . . . . . 157

8.1.1 Packet Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1578.1.2 A Packet as an Event: A Delayed Packet Reception

Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1608.1.3 A Linked List of Packets . . . . . . . . . . . . . . . . . . . . . . . . . . 1618.1.4 Free Packet List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161

8.2 Packet Allocation and Deallocation . . . . . . . . . . . . . . . . . . . . . . . . 1638.2.1 Packet Allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1638.2.2 Packet Deallocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166

8.3 Packet Header . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1688.3.1 An Overview of First Level Packet Composition:

Offseting Protocol Specific Header on thePacket Header . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169

8.3.2 Common Packet Header . . . . . . . . . . . . . . . . . . . . . . . . . . . 1708.3.3 IP Packet Header . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1718.3.4 Packet Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1728.3.5 Protocol Specific Headers . . . . . . . . . . . . . . . . . . . . . . . . . 174

Contents XV

8.3.6 Packet Header Access Mechanism . . . . . . . . . . . . . . . . . . 1788.3.7 Packet Header Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . 1808.3.8 Protocol Specific Header Composition and Packet

Header Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1838.4 Data Payload . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1878.5 Customizing Packets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191

8.5.1 Creating Your Own Packet . . . . . . . . . . . . . . . . . . . . . . . . 1918.5.2 Activate/Deactivate a Protocol Specific Header . . . . . . 192

8.6 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194

9 Transport Control Protocols Part 1 – An Overview andUser Datagram Protocol implementation . . . . . . . . . . . . . . . . . . 1979.1 UDP and TCP Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197

9.1.1 UDP Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1979.1.2 TCP Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198

9.2 Basic Agents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2039.2.1 Applications, Agents, and a Low-level Network . . . . . . . 2039.2.2 Agent Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2059.2.3 Internal Mechanism for Agents . . . . . . . . . . . . . . . . . . . . . 2069.2.4 Guidelines to Define a New Transport Layer Agent . . . 210

9.3 UDP (User Datagram Protocol) and Null Agents . . . . . . . . . . . . 2109.3.1 Null (Receiving) Agents . . . . . . . . . . . . . . . . . . . . . . . . . . . 2119.3.2 UDP (Sending) Agent . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2119.3.3 Setting Up a UDP Connection . . . . . . . . . . . . . . . . . . . . . 215

9.4 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215

10 Transport Control Protocols Part 2 –Transmission Control Protocol (TCP) . . . . . . . . . . . . . . . . . . . . . 21710.1 An Overview of TCP Agents in NS2 . . . . . . . . . . . . . . . . . . . . . . . 217

10.1.1 Setting Up a TCP Connection . . . . . . . . . . . . . . . . . . . . . 21710.1.2 Packet Transmission and Acknowledgment

Mechanism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21810.1.3 TCP Header . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21910.1.4 Defining TCP Sender and Receiver . . . . . . . . . . . . . . . . . 219

10.2 TCP Receiver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22210.2.1 Class Acker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22410.2.2 Class TcpSink . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228

10.3 TCP Sender . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23010.4 TCP Packet Transmission Functions . . . . . . . . . . . . . . . . . . . . . . . 230

10.4.1 Function sendmsg(nbytes) . . . . . . . . . . . . . . . . . . . . . . . . 23110.4.2 Function send much(force,reason,maxburst) . . . . . . 23210.4.3 Function output(seqno,reason) . . . . . . . . . . . . . . . . . . 23410.4.4 Function send one() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236

10.5 ACK Processing Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23710.5.1 Function recv(p,h) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23810.5.2 Function recv newack helper(pkt) . . . . . . . . . . . . . . . . 239

XVI Contents

10.5.3 Function newack(pkt) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24110.6 Timer Related Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242

10.6.1 RTT Sample Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . 24210.6.2 RTT Estimation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24410.6.3 Overview of State Variables . . . . . . . . . . . . . . . . . . . . . . . 24410.6.4 Retransmission Timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24610.6.5 Function Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24710.6.6 Function rtt update(tao) . . . . . . . . . . . . . . . . . . . . . . . . 24810.6.7 Function rtt timeout() . . . . . . . . . . . . . . . . . . . . . . . . . . 25010.6.8 Function rtt backoff() . . . . . . . . . . . . . . . . . . . . . . . . . . 25110.6.9 Function set rtx timer()and Function

reset rtx timer(mild,backoff) . . . . . . . . . . . . . . . . . . 25210.6.10 Function newtimer(pkt) . . . . . . . . . . . . . . . . . . . . . . . . . . 25310.6.11 Function timeout(tno) . . . . . . . . . . . . . . . . . . . . . . . . . . . 253

10.7 Window Adjustment Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25510.7.1 Function opencwnd() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25610.7.2 Function slowdown(how) . . . . . . . . . . . . . . . . . . . . . . . . . . 256

10.8 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259

11 Application: User Demand Indicator . . . . . . . . . . . . . . . . . . . . . . . 26111.1 Relationship Between an Application and a Transport

Layer Agent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26111.2 Details of Class Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265

11.2.1 Functions of Classes Application and Agent . . . . . . . . 26511.2.2 Public Functions of Class Application . . . . . . . . . . . . . 26611.2.3 Related Public Functions of Class Agent . . . . . . . . . . . . 26711.2.4 OTcl Commands of Class Application . . . . . . . . . . . . . 268

11.3 Traffic Generators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26811.3.1 An Overview of Class TrafficGenerator . . . . . . . . . . . 26811.3.2 Main Mechanism of a Traffic Generator . . . . . . . . . . . . . 27011.3.3 Built-in Traffic Generators in NS2 . . . . . . . . . . . . . . . . . . 27211.3.4 Class CBR Traffic: An Example Traffic Generator . . . 275

11.4 Simulated Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27711.4.1 FTP (File Transfer Protocol) . . . . . . . . . . . . . . . . . . . . . . 27811.4.2 Telnet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278

11.5 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279

12 Related Helper Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28112.1 Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281

12.1.1 Implementation Concept of Timer in NS2 . . . . . . . . . . . 28112.1.2 OTcl Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28312.1.3 C++ Based Class Implementation . . . . . . . . . . . . . . . . . . 28512.1.4 Guidelines for Implementing Timers in NS2 . . . . . . . . . . 295

12.2 Implementation of Random Numbers in NS2 . . . . . . . . . . . . . . . . 29612.2.1 Random Number Generation . . . . . . . . . . . . . . . . . . . . . . 29612.2.2 Seeding a Random Number Generator . . . . . . . . . . . . . . 296

Contents XVII

12.2.3 OTcl and C++ Implementation . . . . . . . . . . . . . . . . . . . . 29812.2.4 Randomness in Simulation Scenarios . . . . . . . . . . . . . . . . 30012.2.5 Random Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30312.2.6 Guidelines for Random Number Generation in NS2 . . . 306

12.3 Built-in Error Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30712.3.1 OTcl Implementation: Error Model Configuration . . . . 30912.3.2 C++ Implementation: Error Model Simulation . . . . . . . 31212.3.3 Guidelines for Implementing a New Error Model

in NS2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32012.4 Bit Operations in NS2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321

12.4.1 Bit Masking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32112.4.2 Bit Shifting and Decimal Multiplication . . . . . . . . . . . . . 323

12.5 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324

13 Processing an NS2 Simulation: Debugging, Tracing,and Result Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32713.1 Debugging: A Process to Remove Programming Errors . . . . . . . 327

13.1.1 Types of Programming Errors . . . . . . . . . . . . . . . . . . . . . 32713.1.2 Debugging Guidelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329

13.2 Variable Tracing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33213.2.1 Activation Process for Variable Tracing . . . . . . . . . . . . . 33213.2.2 Instvar Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33413.2.3 TracedVar Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33513.2.4 Tracers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33513.2.5 Connections Among a TclObject, a TracedVar

Object, a Tracer, and a Trace File . . . . . . . . . . . . . . . . . . 33813.2.6 Trace File Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339

13.3 Packet Tracing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34113.3.1 OTcl Configuration Interfaces . . . . . . . . . . . . . . . . . . . . . . 34213.3.2 C++ Main Packet Tracing Class Trace . . . . . . . . . . . . . . 34713.3.3 C++ Helper Class BaseTrace . . . . . . . . . . . . . . . . . . . . 35013.3.4 Various Types of Packet Tracing Objects . . . . . . . . . . . . 35213.3.5 Packet Trace Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355

13.4 Compilation of Simulation Results . . . . . . . . . . . . . . . . . . . . . . . . . 35613.5 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360

14 Developing New Modules for NS2 . . . . . . . . . . . . . . . . . . . . . . . . . 36314.1 Automatic Repeat reQuest (ARQ) . . . . . . . . . . . . . . . . . . . . . . . . . 363

14.1.1 The Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36414.1.2 C++ Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36514.1.3 OTcl Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37114.1.4 ARQ Under a Delayed (Error-Free)

Feedback Channel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37414.2 Packet Scheduling for Multi-Flow Data Transmission . . . . . . . . . 376

14.2.1 The Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376

XVIII Contents

14.2.2 C++ Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37714.2.3 OTcl Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381

14.3 Chapter Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387

A Programming Essentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389A.1 Tcl Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389

A.1.1 Program Invocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389A.1.2 A Simple Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390A.1.3 Variables and Data Types . . . . . . . . . . . . . . . . . . . . . . . . . 391A.1.4 Input/Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394A.1.5 Mathematical Expressions . . . . . . . . . . . . . . . . . . . . . . . . . 396A.1.6 Control Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397A.1.7 Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399

A.2 Objected Oriented Tcl (OTcl) Programming . . . . . . . . . . . . . . . . 400A.2.1 Class and Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400A.2.2 Class Member Procedures and Variables . . . . . . . . . . . . 401A.2.3 Object Construction and the Constructor . . . . . . . . . . . 402A.2.4 Related Instprocs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403

A.3 AWK Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405A.3.1 Program Invocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405A.3.2 An AWK Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406A.3.3 AWK Programming Structure . . . . . . . . . . . . . . . . . . . . . 407A.3.4 Pattern Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407A.3.5 Basic Actions: Operators and Output . . . . . . . . . . . . . . . 408A.3.6 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410A.3.7 Control Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411

B A Review of the Polymorphism Conceptin OOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413B.1 Fundamentals of Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . 413B.2 Type Casting and Function Ambiguity . . . . . . . . . . . . . . . . . . . . . 416B.3 Virtual Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417B.4 Abstract Classes and Pure Virtual Functions . . . . . . . . . . . . . . . . 418B.5 Class Composition: An Application of Type Casting

Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419B.6 Programming Polymorphism with No Type Casting:

An Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420B.7 A Scalability Problem Caused by Non Type Casting

Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421B.8 The Class Composition Programming Concept . . . . . . . . . . . . . . 422

References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427

General Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429

Code Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433