这是用户在 2024-5-10 15:28 为 https://app.immersivetranslate.com/pdf-pro/277a7668-9bcd-4184-a4aa-1a516528e3c9 保存的双语快照页面,由 沉浸式翻译 提供双语支持。了解如何保存?
2024_05_10_73d2d172242addade300g

NETWORK SIMULATOR  网络模拟器

ns-3 Tutorial ns-3 教程
Release ns-3. 41 发布 ns-3.41
ns-3 project ns-3 项目
Feb 09, 2024 2024 年 2 月 9 日

CONTENTS 目录

1 Quick Start ..... 3
1 快速入门 ..... 3

1.1 Brief Summary ..... 3
1.1 简要总结 ..... 3

1.2 Prerequisites ..... 3
1.2 先决条件 ..... 3

1.3 Downloading ns-3 ..... 3
1.3 下载 ns-3 ..... 3

1.4 Building and testing ns-3 . ..... 4
1.4 构建和测试 ns-3 ..... 4

2 Introduction ..... 7
2 简介 ..... 7

2.1 About ns-3 ..... 7
2.1 关于 ns-3 ..... 7

2.2 For ns-2 Users ..... 8
2.2 对于 ns-2 用户 ..... 8

2.3 Contributing ..... 8
2.3 贡献 ..... 8

2.4 Tutorial Organization ..... 9
2.4 教程组织 ..... 9

3 Resources ..... 11
3 资源 ..... 11

3.1 The Web ..... 11
3.1 网页 ..... 11

3.2 Git ..... 11
3.3 CMake ..... 11
3.4 Development Environment ..... 12
3.4 开发环境 ..... 12

3.5 Socket Programming . ..... 12
3.5 套接字编程 . ..... 12

4 Getting Started ..... 13
4 入门 ..... 13

4.1 Overview ..... 13
4.1 概述 ..... 13

4.2 Prerequisites ..... 13
4.2 先决条件 ..... 13

4.3 Downloading ns-3 using Git ..... 15
4.3 使用 Git 下载 ns-3 ..... 15

4.4 Building ns-3 ..... 18
4.4 构建 ns-3 ..... 18

4.5 Testing ns-3 ..... 41
4.5 测试 ns-3 ..... 41

4.6 Running a Script ..... 45
4.6 运行脚本 ..... 45

5 Conceptual Overview ..... 49
5 概念概述 ..... 49

5.1 Key Abstractions ..... 49
5.1 关键抽象 ..... 49

5.2 A First ns-3 Script ..... 51
5.2 第一个 ns-3 脚本 ..... 51

5.3 Ns-3 Source Code ..... 60
5.3 Ns-3 源代码 ..... 60

6 Tweaking ..... 61
6 调整 ..... 61

6.1 Using the Logging Module ..... 61
使用日志模块 ..... 61

6.2 Using Command Line Arguments ..... 66
使用命令行参数 ..... 66

6.3 Using the Tracing System ..... 71
使用跟踪系统 ..... 71

7 Building Topologies ..... 77
7 建筑拓扑 ..... 77

7.1 Building a Bus Network Topology . . ..... 77
7.1 构建总线网络拓扑 . . ..... 77

7.2 Models, Attributes and Reality .. ..... 86
7.2 模型、属性和现实 .. ..... 86

7.3 Building a Wireless Network Topology ..... 87
7.3 构建无线网络拓扑 ..... 87

7.4 Queues in ns-3 ..... 95
7.4 ns-3 中的队列 ..... 95

8 Tracing ..... 99
8 追踪 ..... 99

8.1 Background ..... 99
8.1 背景 ..... 99

8.2 Overview ..... 101
8.2 概述 ..... 101

8.3 Real Example ..... 115
8.3 真实例子 ..... 115

8.4 Trace Helpers ..... 131
8.4 跟踪助手 ..... 131

8.5 Summary ..... 144
8.5 摘要 ..... 144

9 Data Collection ..... 145
9 数据收集 ..... 145

9.1 Motivation ..... 145
9.1 动机 ..... 145

9.2 Example Code ..... 145
9.2 示例代码 ..... 145

9.3 GnuplotHelper ..... 147
9.3 Gnuplot 助手 ..... 147

9.4 Supported Trace Types ..... 150
9.4 支持的跟踪类型 ..... 150

9.5 FileHelper ..... 150
9.6 Summary ..... 151
9.6 摘要 ..... 151

10 Conclusion ..... 153
10 结论 ..... 153

10.1 Futures ..... 153
10.1 未来 ..... 153

10.2 Closing ..... 153
10.2 结束 ..... 153
This is the Tutorial. Primary documentation for the ns-3 project is organized as follows:
这是 教程。ns-3 项目的主要文档组织如下:
  • Several guides that are version controlled for each release (the latest release) and development tree:
    每个发布版本(最新版本)和开发树都有版本控制的几个指南:
  • Tutorial (this document)
    教程(本文档)
  • Installation Guide 安装指南
  • Manual 手册
  • Model Library 模型库
  • Contributing Guide 贡献指南
  • ns-3 Doxygen: Documentation of the public APIs of the simulator
    ns-3 Doxygen:模拟器公共 API 的文档
  • ns-3 wiki ns-3 维基
This document is written in reStructuredText for Sphinx and is maintained in the doc/tutorial directory of ns-3's source code. Source file column width is 100 columns.
本文档是使用 reStructuredText 编写的,用于 Sphinx,并在 ns-3 源代码的 doc/tutorial 目录中进行维护。源文件列宽为 100 列。

ns-3 Tutorial, Release ns-3.41
ns-3 教程,版本 ns-3.41

QUICK START 快速入门

This section is optional, for readers who want to get up and running as quickly as possible. Readers may skip forward to the Introduction chapter, followed by the Getting Started chapter, for a lengthier coverage of all of this material.
本节是可选的,适用于希望尽快启动和运行 的读者。读者可以直接跳转到介绍章节,然后是入门章节,以获取更详尽的所有材料内容。

1.1 Brief Summary 1.1 简要总结

is a discrete-event simulator typically run from the command line. It is written directly in C++, not in a high-level modeling language; simulation events are simply C++ function calls, organized by a scheduler.
是一个通常从命令行运行的离散事件模拟器。它直接用 C++编写,而不是用高级建模语言;模拟事件只是 C++函数调用,由调度程序组织。
An - 3 user will obtain the source code (see below), compile it into shared (or static) libraries, and link the libraries to main() programs that he or she authors. The main() program is where the specific simulation scenario configuration is performed and where the simulator is run and stopped. Several example programs are provided, which can be modified or copied to create new simulation scenarios. Users also often edit the - 3 library code (and rebuild the libraries) to change its behavior.
- 3 用户将获取 源代码(见下文),将其编译为共享(或静态)库,并将库链接到他或她编写的 main()程序中。main()程序是执行特定模拟场景配置并运行和停止模拟器的地方。提供了几个示例程序,可以修改或复制以创建新的模拟场景。用户经常编辑 - 3 库代码(并重新构建库)以更改其行为。
has optional Python bindings for authoring scenario configuration programs in Python (and using a Python-based workflow); this quick start does not cover those aspects.
具有用于在 Python 中编写场景配置程序(并使用基于 Python 的工作流)的可选 Python 绑定;本快速入门不涵盖这些方面。

1.2 Prerequisites 1.2 先决条件

has various optional extensions, but the main features just require a C++ compiler (g++ or clang++), Python (version 3.6 or above), CMake and a build-system (e.g. make, ninja, Xcode). We focus in this chapter only on getting up and running on a system supported by a recent C++ compiler and Python runtime support.
具有各种可选扩展,但主要功能仅需要 C++编译器(g++或 clang++)、Python(版本 3.6 或更高版本)、CMake 和构建系统(例如 make、ninja、Xcode)。本章仅关注在支持最新 C++编译器和 Python 运行时支持的系统上使 运行起来。
For Linux, use either g++ or clang++ compilers. For macOS, use clang++ (available in Xcode or Xcode Command Line Tools). For Windows, Msys2 tools with the MinGW64 toolchain can be used (since ns-3.37) for most use cases. For releases earlier than ns-3.37, or for use of emulation modes or Python bindings, we recommend to either use a Linux virtual machine, or the Windows Subsystem for Linux.
对于 Linux,可以使用 g++或 clang++编译器。对于 macOS,使用 clang++(在 Xcode 或 Xcode 命令行工具中可用)。对于 Windows,Msys2 工具与 MinGW64 工具链可以用于大多数用例(自 ns-3.37 起)。对于早于 ns-3.37 的版本,或者使用仿真模式或 Python 绑定,我们建议使用 Linux 虚拟机或 Windows 子系统 for Linux。

1.3 Downloading ns-3 1.3 下载 ns-3

is distributed in source code only (some binary packages exist but they are not maintained by the open source project). There are two main ways to obtain the source code: 1) downloading the latest release as a source code archive from the main web site, or 2) cloning the Git repository from GitLab.com. These two options are described next; either one or the other download option (but not both) should be followed.
仅以源代码形式分发(存在一些二进制包,但它们不受开源项目维护)。有两种主要方法可以获取源代码:1)从主 网站下载最新版本作为源代码存档,或者 2)从 GitLab.com 克隆 Git 存储库。接下来将介绍这两种选项;应遵循其中一种下载选项(但不是两者同时)。

1.3.1 Downloading the Latest Release
1.3.1 下载最新版本

  1. Download the latest release from https://www.nsnam.org/releases/latest
    从 https://www.nsnam.org/releases/latest 下载最新版本
  2. Unpack it in a working directory of your choice.
    将其解压缩到您选择的工作目录中。
$ tar xjf ns-allinone-3.41.tar.bz2
  1. Change into the directory directly; e.g.
    直接切换到 目录;例如
$ cd ns-allinone-3.41/ns-3.41
The ns-allinone directory has some additional components but we are skipping over them here; one can work directly from the source code directory. The rest of the tutorial describes the additional components.
ns-allinone 目录中有一些额外的组件,但我们在这里跳过它们;可以直接从 源代码目录中工作。本教程的其余部分描述了额外的组件。

1.3.2 Cloning ns-3 from GitLab.com
1.3.2 从 GitLab.com 克隆 ns-3

You can perform a Git clone in the usual way:
您可以按照通常的方式执行 Git 克隆:
If you are content to work with the tip of the development tree; you need only to into ns-3-dev; the master branch is checked out by default.
如果您满足于使用开发树的最新版本;您只需要 到 ns-3-dev;默认情况下将检出主分支。
If instead you want to try the most recent release (version 3.41 as of this writing), you can checkout a branch corresponding to that git tag:
如果您想尝试最新的发布版本(截至本文撰写时为版本 3.41),您可以检出与该 git 标签对应的分支:
$ git checkout -b ns-3.41-branch ns-3.41

1.4 Building and testing ns-3
1.4 构建和测试 ns-3

Once you have obtained the source either by downloading a release or by cloning a Git repository, the next step is to configure the build using the CMake build system. The below commands make use of a Python wrapper around CMake, called ns3, that simplifies the command-line syntax, resembling Waf syntax. There are several options to control the build, but enabling the example programs and the tests, for a default build profile (with asserts enabled and and support for logging) is what is usually done at first:
一旦您通过下载发布版或克隆 Git 存储库获取了源代码,下一步是使用 CMake 构建系统配置构建。下面的命令使用了一个名为 ns3 的 Python 包装器,简化了命令行语法,类似于 Waf 语法。有几个选项可以控制构建过程,但通常首先要做的是启用示例程序和测试,以进行默认构建配置(启用断言和支持 日志记录):
$ ./ns3 configure --enable-examples --enable-tests
Then, use to build :
然后,使用 构建
build  构建
Once complete, you can run the unit tests to check your build:
构建完成后,您可以运行单元测试来检查您的构建:
All tests should either PASS or be SKIPped. At this point, you have a working simulator. From here, you can start to run programs (look in the examples directory). To run the first tutorial program, whose source code is located at examples/tutorial/first.cc, use ns 3 to run it (by doing so, the shared libraries are found automatically):
所有测试应该要么通过,要么被跳过。此时,您有一个可工作的 模拟器。从这里,您可以开始运行程序(查看示例目录)。要运行第一个教程程序,其源代码位于 examples/tutorial/first.cc,请使用 ns 3 来运行它(通过这样做, 共享库会被自动找到):

run first  运行 first
To view possible command-line options, specify the -PrintHelp argument:
要查看可能的命令行选项,请指定-PrintHelp 参数:
$ ./ns3 run 'first --PrintHelp'
$ ./ns3 运行'first --PrintHelp'
To continue reading about the conceptual model and architecture of , the tutorial chapter Conceptual Overview would be the next natural place to skip to, or you can learn more about the project and the various build options by continuing directly with the Introduction and Getting Started chapters.
要继续阅读 的概念模型和架构,教程章节概念概述将是下一个自然的跳转位置,或者您可以通过直接继续阅读介绍和入门章节来了解有关该项目和各种构建选项的更多信息。

INTRODUCTION 介绍

The simulator is a discrete-event network simulator targeted primarily for research and educational use. The ns-3 project, started in 2006, is an open-source project developing .
模拟器是一个离散事件网络模拟器,主要用于研究和教育目的。ns-3 项目始于 2006 年,是一个开源项目,正在开发
The purpose of this tutorial is to introduce new users to the system in a structured way. It is sometimes difficult for new users to glean essential information from detailed manuals and to convert this information into working simulations. In this tutorial, we will build several example simulations, introducing and explaining key concepts and features as we go.
本教程的目的是以结构化的方式向新 用户介绍系统。新用户有时很难从详细的手册中获取基本信息,并将这些信息转化为可工作的模拟。在本教程中,我们将构建几个示例模拟,逐步介绍和解释关键概念和特性。
As the tutorial unfolds, we will introduce the full documentation and provide pointers to source code for those interested in delving deeper into the workings of the system.
随着教程的展开,我们将介绍完整的 文档,并为那些对深入了解系统工作原理感兴趣的人提供源代码指针。
We also provide a quick start guide for those who are comfortable diving right in without too much documentation.
我们还为那些喜欢立即开始而不需要太多文档的人提供了快速入门指南。
A few key points are worth noting at the onset:
在开始阶段值得注意的几个关键点:
  • is open-source, and the project strives to maintain an open environment for researchers to contribute and share their software.
    是开源的,该项目致力于维护一个开放的环境,让研究人员可以贡献和分享他们的软件。
  • is not a backwards-compatible extension of ns-2; it is a new simulator. The two simulators are both written in C++ but is a new simulator that does not support the APIs.
    不是 ns-2 的向后兼容扩展;它是一个新的模拟器。这两个模拟器都是用 C++编写的,但 是一个不支持 API 的新模拟器。

2.1 About ns-3 2.1 关于 ns-3

has been developed to provide an open, extensible network simulation platform, for networking research and education. In brief, provides models of how packet data networks work and perform, and provides a simulation engine for users to conduct simulation experiments. Some of the reasons to use include to perform studies that are more difficult or not possible to perform with real systems, to study system behavior in a highly controlled, reproducible environment, and to learn about how networks work. Users will note that the available model set in focuses on modeling how Internet protocols and networks work, but is not limited to Internet systems; several users are using to model non-Internet-based systems.
已经开发出来,为网络研究和教育提供一个开放的、可扩展的网络模拟平台。简而言之, 提供了关于数据包网络如何工作和执行的模型,并为用户提供了一个进行模拟实验的模拟引擎。使用 的一些原因包括进行更难或无法通过真实系统进行的研究,研究系统在高度可控、可重现的环境中的行为,以及了解网络如何工作。用户会注意到 中提供的可用模型集中在模拟互联网协议和网络的工作方式,但 并不局限于互联网系统;一些用户正在使用 来模拟非互联网系统。
Many simulation tools exist for network simulation studies. Below are a few distinguishing features of in contrast to other tools.
许多网络模拟研究的仿真工具存在。以下是 与其他工具相比的一些显著特点。
  • is designed as a set of libraries that can be combined together and also with other external software libraries. While some simulation platforms provide users with a single, integrated graphical user interface environment in which all tasks are carried out, is more modular in this regard. Several external animators and data analysis and visualization tools can be used with . However, users should expect to work at the command line and with C++ and/or Python software development tools.
    被设计为一组可以组合在一起,也可以与其他外部软件库一起使用的库。虽然一些仿真平台为用户提供了一个集成的图形用户界面环境,其中执行所有任务, 在这方面更具模块化。可以与几个外部动画制作工具、数据分析和可视化工具一起使用 。但是,用户应该期望在命令行和使用 C++ 和/或 Python 软件开发工具时工作。
  • ns-3 is primarily used on Linux or macOS systems, although support exists for BSD systems and also for Windows frameworks that can build Linux code, such as Windows Subsystem for Linux, or Cygwin. Native Windows Visual Studio is not presently supported although a developer is working on future support. Windows users may also use a Linux virtual machine.
    ns-3 主要用于 Linux 或 macOS 系统,尽管支持存在于 BSD 系统中,也支持可以构建 Linux 代码的 Windows 框架,如 Windows Subsystem for Linux 或 Cygwin。目前不支持原生 Windows Visual Studio,尽管有开发人员正在努力支持未来的支持。Windows 用户也可以使用 Linux 虚拟机。
  • is not an officially supported software product of any company. Support for is done on a best-effort basis on the ns-3-users forum (ns-3-users @googlegroups.com).
    不是任何公司的官方支持软件产品。对 的支持是在 ns-3-users 论坛(ns-3-users@googlegroups.com)上尽力而为地进行的。

2.2 For ns-2 Users
2.2 对于 ns-2 用户

For those familiar with (a popular tool that preceded ), the most visible outward change when moving to is the choice of scripting language. Programs in are scripted in OTcl and results of simulations can be visualized using the Network Animator nam. It is not possible to run a simulation in purely from C++ (i.e., as a main() program without any OTcl). Moreover, some components of are written in C++ and others in OTcl. In , the simulator is written entirely in C++, with optional Python bindings. Simulation scripts can therefore be written in C++ or in Python. New animators and visualizers are available and under current development. Since generates pcap packet trace files, other utilities can be used to analyze traces as well. In this tutorial, we will first concentrate on scripting directly in C++ and interpreting results via trace files.
对于熟悉 (一种先前 的流行工具)的人来说,移动到 时最明显的外部变化是脚本语言的选择。 中的程序使用 OTcl 脚本编写,模拟的结果可以使用网络动画器 nam 进行可视化。在 中无法纯粹从 C++(即,作为一个没有任何 OTcl 的 main()程序)运行模拟。此外, 的一些组件是用 C++编写的,另一些是用 OTcl 编写的。在 中,模拟器完全用 C++编写,可选的 Python 绑定。因此,模拟脚本可以用 C++或 Python 编写。新的动画制作者和可视化器可用并正在当前开发中。由于 生成 pcap 数据包跟踪文件,因此还可以使用其他实用程序来分析跟踪。在本教程中,我们将首先集中讨论直接在 C++中编写脚本并通过跟踪文件解释结果。
But there are similarities as well (both, for example, are based on C++ objects, and some code from has already been ported to . We will try to highlight differences between and as we proceed in this tutorial.
但也有相似之处(例如,两者都基于 C++对象,并且 的一些代码已经被移植到 中。我们将在本教程中继续进行时尝试突出 之间的差异。
A question that we often hear is "Should I still use or move to ?" In this author's opinion, unless the user is somehow vested in (either based on existing personal comfort with and knowledge of , or based on a specific simulation model that is only available in ), a user will be more productive with for the following reasons:
我们经常听到的一个问题是“我应该继续使用 还是转移到 ?”在本作者看来,除非用户在 方面有一定投入(无论是基于对 的现有个人舒适感和了解,还是基于仅在 中可用的特定模拟模型),用户将因以下原因而更具生产力使用
  • is actively maintained with an active, responsive users mailing list, while is only lightly maintained and has not seen significant development in its main code tree for over a decade.
    正在积极维护,并拥有一个活跃、响应迅速的用户邮件列表,而 只是轻度维护,并且在其主代码树上已经超过十年没有看到重大发展。
  • provides features not available in , such as a implementation code execution environment (allowing users to run real implementation code in the simulator)
    提供了 中没有的功能,比如实现代码执行环境(允许用户在模拟器中运行真实的实现代码)。
  • provides a lower base level of abstraction compared with , allowing it to align better with how real systems are put together. Some limitations found in (such as supporting multiple types of interfaces on nodes correctly) have been remedied in .
    提供了与 相比更低的抽象基准,使其能够更好地与实际系统的组合方式相匹配。在 中发现的一些限制(例如在节点上正确支持多种类型的接口)已在 中得到解决。
If in doubt, a good guideline would be to look at both simulators (as well as other simulators), and in particular the models available for your research, but keep in mind that your experience may be better in using the tool that is being actively developed and maintained .
如果有疑问,一个很好的指导原则是查看两个模拟器(以及其他模拟器),特别是您研究所需的模型,但请记住,您的体验可能更好地使用正在积极开发和维护的工具

2.3 Contributing 2.3 贡献

is a research and educational simulator, by and for the research community. It will rely on the ongoing contributions of the community to develop new models, debug or maintain existing ones, and share results. There are a few policies that we hope will encourage people to contribute to like they have for :
是一个研究和教育模拟器,由研究社区创建和使用。它将依赖社区持续贡献来开发新模型,调试或维护现有模型,并分享结果。我们希望有一些政策能够鼓励人们像为 贡献一样为 贡献:
  • Open source licensing based on GNU GPLv2 compatibility
    基于 GNU GPLv2 兼容性的开源许可证
  • An app store 一个应用商店
  • Contributed Code page, similar to 's popular Contributed Code page
    贡献代码页面,类似于 的热门贡献代码页面
  • Documentation on how to contribute
    如何贡献文档
  • Use of Git hosting at GitLab.com including issue tracker
    在 GitLab.com 上使用 Git 托管,包括问题跟踪器
We realize that if you are reading this document, contributing back to the project is probably not your foremost concern at this point, but we want you to be aware that contributing is in the spirit of the project and that even the act of dropping us a note about your early experience with (e.g. "this tutorial section was not clear..."), reports of stale documentation or comments in the code, etc. are much appreciated. The preferred way to submit patches is either to fork our project on GitLab.com and generate a Merge Request, or to open an issue on our issue tracker and append a patch.
我们意识到,如果您正在阅读本文档,对项目的贡献可能并非您目前的首要关注点,但我们希望您意识到,贡献是项目精神的体现,即使是给我们留言关于您对 的早期体验(例如,“这个教程部分不清楚…”)、过时文档的报告或代码中的评论等,都会受到高度赞赏。提交补丁的首选方式要么是在 GitLab.com 上 fork 我们的项目并生成合并请求,要么是在我们的问题跟踪器上打开一个问题并附上一个补丁。

2.4 Tutorial Organization
2.4 教程组织

The tutorial assumes that new users might initially follow a path such as the following:
本教程假定新用户最初可能会按照以下路径进行。
  • Try to download and build a copy;
    尝试下载并构建副本;
  • Try to run a few sample programs;
    尝试运行一些示例程序;
  • Look at simulation output, and try to adjust it.
    查看模拟输出,并尝试调整它。
As a result, we have tried to organize the tutorial along the above broad sequences of events.
因此,我们尝试沿着上述事件的广泛顺序组织教程。
RESOURCES 资源

3.1 The Web 3.1 网页

There are several important resources of which any user must be aware. The main web site is located at https: //www.nsnam.org and provides access to basic information about the system. Detailed documentation is available through the main web site at https://www.nsnam.org/documentation/. You can also find documents relating to the system architecture from this page.
有几个重要资源,任何 用户都必须了解。主要网站位于 https://www.nsnam.org,提供有关 系统的基本信息。详细文档可通过主网站 https://www.nsnam.org/documentation/获得。您还可以从此页面找到与系统架构相关的文档。
There is a Wiki that complements the main web site which you will find at https://www.nsnam.org/wiki/. You will find user and developer FAQs there, as well as troubleshooting guides, third-party contributed code, papers, etc.
有一个维基百科,它是主 网站的补充,您可以在 https://www.nsnam.org/wiki/找到。您将在那里找到用户和开发者的常见问题解答,以及故障排除指南、第三方贡献的代码、论文等。
The source code may be found and browsed at GitLab.com: https://gitlab.com/nsnam/. There you will find the current development tree in the repository named ns-3-dev. Past releases and experimental repositories of the core developers may also be found at the project's old Mercurial site at http://code.nsnam.org.
源代码可以在 GitLab.com 上找到和浏览:https://gitlab.com/nsnam/。在那里,您将在名为 ns-3-dev 的存储库中找到当前的开发树。核心开发人员的过去版本和实验性存储库也可以在该项目的旧 Mercurial 网站 http://code.nsnam.org 上找到。

3.2 Git

Complex software systems need some way to manage the organization and changes to the underlying code and documentation. There are many ways to perform this feat, and you may have heard of some of the systems that are currently used to do this. Until recently, the project used Mercurial as its source code management system, but in December 2018, switch to using Git. Although you do not need to know much about Git in order to complete this tutorial, we recommend becoming familiar with Git and using it to access the source code. GitLab.com provides resources to get started at: https://docs.gitlab.com/ee/gitlab-basics/.
复杂的软件系统需要一种管理基础代码和文档变更的方式。有许多方法可以完成这项任务,您可能已经听说过一些当前用于执行此操作的系统。直到最近, 项目使用 Mercurial 作为其源代码管理系统,但在 2018 年 12 月转而使用 Git。虽然您不需要对 Git 了解太多才能完成本教程,但我们建议您熟悉 Git 并使用它来访问源代码。GitLab.com 提供了入门资源:https://docs.gitlab.com/ee/gitlab-basics/。

3.3 CMake

Once you have source code downloaded to your local system, you will need to compile that source to produce usable programs. Just as in the case of source code management, there are many tools available to perform this function. Probably the most well known of these tools is make. Along with being the most well known, make is probably the most difficult to use in a very large and highly configurable system. Because of this, many alternatives have been developed.
一旦您将源代码下载到本地系统,您将需要编译该源代码以生成可用程序。就源代码管理而言,有许多可用工具来执行此功能。这些工具中最著名的可能是 make。除了是最著名的工具外,make 可能是在非常庞大和高度可配置的系统中使用最困难的工具。因此,已开发了许多替代方案。
The build system CMake is used on the project.
构建系统 CMake 用于 项目。
For those interested in the details of CMake, the CMake documents are available at https://cmake.org/cmake/help/ latest/index.html and the current code at https://gitlab.kitware.com/cmake/cmake.
对于那些对 CMake 细节感兴趣的人,CMake 文档可在 https://cmake.org/cmake/help/latest/index.html 上找到,当前代码可在 https://gitlab.kitware.com/cmake/cmake 上找到。

3.4 Development Environment
3.4 开发环境

As mentioned above, scripting in is done in C++ or Python. Most of the API is available in Python, but the models are written in C++ in either case. A working knowledge of C++ and object-oriented concepts is assumed in this document. We will take some time to review some of the more advanced concepts or possibly unfamiliar language features, idioms and design patterns as they appear. We don't want this tutorial to devolve into a C++ tutorial, though, so we do expect a basic command of the language. There are a wide number of sources of information on C++ available on the web or in print.
如上所述,在 中,脚本编写使用 C++ 或 Python 完成。大多数 API 可在 Python 中使用,但模型无论如何都是用 C++ 编写的。本文档假定您具有 C++ 和面向对象概念的工作知识。我们将花一些时间来审查一些更高级的概念或可能不熟悉的语言特性、习语和设计模式。我们不希望本教程演变成 C++ 教程,但我们确实期望您具备基本的语言掌握能力。网上或印刷品上有大量关于 C++ 的信息来源。
If you are new to C++, you may want to find a tutorial- or cookbook-based book or web site and work through at least the basic features of the language before proceeding. For instance, this tutorial.
如果您是 C++的新手,您可能希望在继续之前找到一个基于教程或食谱的书籍或网站,并至少学习一下语言的基本特性。例如,这个教程。
On Linux, the system uses several components of the GNU "toolchain" for development. A software toolchain is the set of programming tools available in the given environment. For a quick review of what is included in the GNU toolchain see, http://en.wikipedia.org/wiki/GNU_toolchain. ns-3 uses gcc, GNU binutils, and gdb. However, we do not use the GNU build system tools, neither make directly. We use CMake for these functions.
在 Linux 上, 系统使用 GNU“工具链”的几个组件进行开发。软件工具链是给定环境中可用的编程工具集。要快速查看 GNU 工具链中包含的内容,请参阅 http://en.wikipedia.org/wiki/GNU_toolchain。ns-3 使用 gcc、GNU binutils 和 gdb。但是,我们不直接使用 GNU 构建系统工具,也不直接使用 make。我们使用 CMake 来进行这些功能。
On macOS, the toolchain used is Xcode. ns-3 users on a Mac are strongly encouraged to install Xcode and the command-line tools packages from the Apple App Store, and to look at the installation guide for more information (https://www.nsnam.org/docs/installation/html/).
在 macOS 上,使用的工具链是 Xcode。强烈建议 Mac 上的 ns-3 用户安装 Xcode 和来自 Apple App Store 的命令行工具包,并查看 安装指南以获取更多信息(https://www.nsnam.org/docs/installation/html/)。
Typically an author will work in Linux or a Unix-like environment. For those running under Windows, there do exist environments which simulate the Linux environment to various degrees. The installation guide has information about Windows support (https://www.nsnam.org/docs/installation/html/windows.html).
通常, 作者会在 Linux 或类 Unix 环境中工作。对于在 Windows 下运行的用户,确实存在一些模拟 Linux 环境的工具。 安装指南中有关于 Windows 支持的信息(https://www.nsnam.org/docs/installation/html/windows.html)。

3.5 Socket Programming 3.5 套接字编程

We will assume a basic facility with the Berkeley Sockets API in the examples used in this tutorial. If you are new to sockets, we recommend reviewing the API and some common usage cases. For a good overview of programming TCP/IP sockets we recommend TCP/IP Sockets in C, Donahoo and Calvert.
我们假设在本教程中使用的示例中对 Berkeley Sockets API 有基本的了解。如果您对套接字不熟悉,我们建议您查阅 API 和一些常见的使用案例。要了解 TCP/IP 套接字编程的概述,我们推荐 Donahoo 和 Calvert 的《TCP/IP Sockets in C》。
There is an associated web site that includes source for the examples in the book, which you can find at: http://cs. baylor.edu/ donahoo/practical/CSockets/.
本书示例的源代码包含在关联的网站中,您可以在以下网址找到:http://cs.baylor.edu/donahoo/practical/CSockets/。
If you understand the first four chapters of the book (or for those who do not have access to a copy of the book, the echo clients and servers shown in the website above) you will be in good shape to understand the tutorial. There is a similar book on Multicast Sockets, Multicast Sockets, Makofske and Almeroth. that covers material you may need to understand if you look at the multicast examples in the distribution.
如果您理解了本书的前四章(或者对于那些无法获取本书副本的人,可以查看上述网站中显示的回显客户端和服务器),那么您将能够很好地理解本教程。有一本类似的关于多播套接字的书,Multicast Sockets, Makofske 和 Almeroth,它涵盖了您可能需要理解的材料,如果您查看分发中的多播示例。

GETTING STARTED 入门指南

This section is aimed at getting a user to a working state starting with a machine that may never have had installed. It covers supported platforms, prerequisites, ways to obtain , ways to build , and ways to verify your build and run simple programs.
本节旨在使用户从一个可能从未安装过 的机器开始,达到一个工作状态。它涵盖了支持的平台,先决条件,获取 的方法,构建 的方法,以及验证您的构建并运行简单程序的方法。

4.1 Overview 4.1 概述

is built as a system of software libraries that work together. User programs can be written that links with (or imports from) these libraries. User programs are written in either the C++ or Python programming languages.
被构建为一组共同工作的软件库。用户可以编写链接(或从中导入)这些库的用户程序。用户程序可用 C++ 或 Python 编程语言编写。
is distributed as source code, meaning that the target system needs to have a software development environment to build the libraries first, then build the user program. - 3 could in principle be distributed as pre-built libraries for selected systems, and in the future it may be distributed that way, but at present, many users actually do their work by editing itself, so having the source code around to rebuild the libraries is useful. If someone would like to undertake the job of making pre-built libraries and packages for operating systems, please contact the ns-developers mailing list.
被分发为源代码,这意味着目标系统需要具有软件开发环境来首先构建库,然后构建用户程序。原则上, - 3 可以作为预构建库分发给选定的系统,将来可能会以这种方式分发,但目前,许多用户实际上通过编辑 来完成工作,因此保留源代码以重新构建库是有用的。如果有人想要承担制作操作系统的预构建库和软件包的工作,请联系 ns-developers 邮件列表。
In the following, we'll look at three ways of downloading and building . The first is to download and build an official release from the main web site. The second is to fetch and build development copies of a basic installation. The third is to use an additional build tool to download more extensions for . We'll walk through each since the tools involved are slightly different.
在接下来,我们将看看下载和构建 的三种方法。第一种是从主网站下载和构建官方发布版。第二种是获取和构建基本 安装的开发副本。第三种是使用额外的构建工具下载更多 的扩展。我们将逐个介绍,因为涉及的工具略有不同。
Experienced Linux users may wonder at this point why is not provided like most other libraries using a package management tool? Although there exist some binary packages for various Linux distributions (e.g. Debian), most users end up editing and having to rebuild the libraries themselves, so having the source code available is more convenient. We will therefore focus on a source installation in this tutorial.
有经验的 Linux 用户可能会想知道为什么 不像大多数其他库一样使用软件包管理工具提供?尽管各种 Linux 发行版(例如 Debian)存在一些二进制软件包,但大多数用户最终会编辑并不得不重新构建 库,因此提供源代码更为方便。因此,在本教程中,我们将重点介绍源代码安装。
For most uses of , root permissions are not needed, and the use of a non-privileged user account is recommended.
对于 的大多数用途,不需要 root 权限,建议使用非特权用户帐户。

4.2 Prerequisites 4.2 先决条件

The entire set of available libraries has a number of dependencies on third-party libraries, but most of can be built and used with support for a few common (often installed by default) components: a C++ compiler, an installation of Python, a source code editor (such as vim, emacs, or Eclipse) and, if using the development repositories, an installation of Git source code control system. Most beginning users need not concern themselves if their configuration reports some missing optional features of , but for those wishing a full installation, the project provides an installation guide for various systems, available at https://www.nsnam.org/docs/installation/html/index.html.
可用的整套 库对第三方库有许多依赖,但大多数 可以构建和使用,支持一些常见的(通常默认安装的)组件:C++编译器,Python 安装,源代码编辑器(如 vim,emacs 或 Eclipse),如果使用开发存储库,则需要安装 Git 源代码控制系统。大多数初学者无需担心配置报告 的一些缺失可选功能,但对于希望进行完整安装的用户,该项目提供了各种系统的安装指南,网址为 https://www.nsnam.org/docs/installation/html/index.html。
As of the most recent release (ns-3.41), the following tools are needed to get started with :
截至最新的 发布(ns-3.41),以下工具是开始使用 所需的:
Prerequisite Package/version 包/版本
C ++ compiler C ++ 编译器 clang ++ or version 9 or greater)
clang ++ 或 版本 9 或更高版本)
Python python3 version  python3 版本
CMake cmake version  cmake 版本
Build system make, nin ja, xcodebuild (XCode)
make, ninja, xcodebuild (XCode)
Git any recent version (to access from GitLab.com)
任何最近的版本(从 GitLab.com 访问
tar any recent version (to unpack an ns-3 release)
任何最近的版本(用于解压 ns-3 发布版)
bunzip2 any recent version (to uncompress an release)
任何最近的版本(用于解压 发布版)
To check the default version of Python, type python - . To check the default version of , type - . If your installation is missing or too old, please consult the installation guide for guidance.
要检查 Python 的默认版本,请键入 python - 。要检查 的默认版本,请键入 - 。如果您的安装缺失或太旧,请参考 安装指南以获取帮助。
From this point forward, we are going to assume that the reader is working in Linux, macOS, or a Linux emulation environment, and has at least the above prerequisites.
从这一点开始,我们将假设读者正在使用 Linux、macOS 或 Linux 模拟环境,并且至少具备上述先决条件。
For example, do not use a directory path such as the below, because one of the parent directories contains a space in the directory name:
例如,不要使用类似下面这样的目录路径,因为其中一个父目录的目录名包含空格:
pwd
/home/user/5G simulations/ns-3-allinone/ns-3-dev
/home/user/5G 模拟/ns-3-allinone/ns-3-dev

4.2.1 Downloading a release of ns-3 as a source archive
4.2.1 下载 ns-3 的发布版本作为源代码存档

This option is for the new user who wishes to download and experiment with the most recently released and packaged version of . publishes its releases as compressed source archives, sometimes referred to as a tarball. A tarball is a particular format of software archive where multiple files are bundled together and the archive is usually compressed. The process for downloading via tarball is simple; you just have to pick a release, download it and uncompress it.
此选项适用于希望下载并尝试最近发布和打包的 版本的新用户。 将其发布为压缩的源代码存档,有时称为 tarball。 Tarball 是一种特定格式的软件存档,其中多个文件捆绑在一起,通常进行了压缩。 通过 tarball 下载 的过程很简单;您只需选择一个版本,下载并解压缩即可。
Let's assume that you, as a user, wish to build in a local directory called workspace. If you adopt the workspace directory approach, you can get a copy of a release by typing the following into your Linux shell (substitute the appropriate version numbers, of course)
假设您作为用户希望在名为 workspace 的本地目录中构建 。如果您采用 workspace 目录方法,可以通过在 Linux shell 中键入以下内容来获取发布的副本(当然要替换适当的版本号)
$cd
$ mkdir workspace
$ cd workspace
$ wget https://www.nsnam.org/release/ns-allinone-3.41.tar.bz2
$ tar xjf ns-allinone-3.41.tar.bz2
Notice the use above of the wget utility, which is a command-line tool to fetch objects from the web; if you do not have this installed, you can use a browser for this step.
请注意上面使用了 wget 实用程序,这是一个从网络获取对象的命令行工具;如果您没有安装此工具,可以使用浏览器完成此步骤。
Following these steps, if you change into the directory ns-allinone-3.41, you should see a number of files and directories
遵循这些步骤,如果您切换到 ns-allinone-3.41 目录,您应该会看到许多文件和目录
$ cd ns-allinone-3.41
ls
bake build.py constants.py netanim-3.109 ns-3.41 README.md util.py
烘烤 build.py 常量.py netanim-3.109 ns-3.41 README.md 工具.py
You are now ready to build the base distribution and may skip ahead to the section on building .
您现在可以开始构建基础 发行版,并可以直接跳转到构建 部分。

4.3 Downloading ns-3 using Git
4.3 使用 Git 下载 ns-3

The code is available in Git repositories on the GitLab.com service at https://gitlab.com/nsnam/. The group name nsnam organizes the various repositories used by the open source project.
代码可在 GitLab.com 服务上的 Git 仓库中找到,网址为 https://gitlab.com/nsnam/。nsnam 组织了这个开源项目使用的各种仓库。
The simplest way to get started using Git repositories is to fork or clone the ns-3-allinone environment. This is a set of scripts that manages the downloading and building of the most commonly used subsystems of for you. If you are new to Git, the terminology of fork and clone may be foreign to you; if so, we recommend that you simply clone (create your own replica) of the repository found on GitLab.com, as follows:
使用 Git 仓库开始的最简单方法是 fork 或 clone ns-3-allinone 环境。这是一组脚本,用于管理为您下载和构建 最常用的子系统。如果您对 Git 还不熟悉,fork 和 clone 这些术语可能对您来说很陌生;如果是这样,我们建议您简单地克隆(创建自己的副本)在 GitLab.com 上找到的仓库,方法如下:
mkdir workspace  创建工作空间
cd workspace  进入工作空间
$ git clone https://gitlab.com/nsnam/ns-3-allinone.git
$ git 克隆 https://gitlab.com/nsnam/ns-3-allinone.git
cd ns-3-allinone
At this point, your view of the ns-3-allinone directory is slightly different than described above with a release archive; it should look something like this:
此时,您对 ns-3-allinone 目录的视图与上述发布存档中稍有不同;它应该看起来像这样:
Note the presence of the download.py script, which will further fetch the and related sourcecode. At this point, you have a choice, to either download the most recent development snapshot of :
请注意 download.py 脚本的存在,该脚本将进一步获取 和相关源代码。此时,您可以选择要么下载 的最新开发快照:
$ python3 download.py
or to specify a release of , using the flag to specify a release number:
要么指定 的一个版本,使用 标志来指定版本号:
$ python3 download.py -n ns-3.41
After this step, the additional repositories of , bake, pybindgen, and netanim will be downloaded to the ns-3-allinone directory.
在此步骤之后, ,bake,pybindgen 和 netanim 的额外存储库将被下载到 ns-3-allinone 目录中。

4.3.1 Downloading ns-3 Using Bake
使用 Bake 下载 ns-3 4.3.1

The above two techniques (source archive, or ns-3-allinone repository via Git) are useful to get the most basic installation of with a few addons (pybindgen for generating Python bindings, and netanim for network animations). The third repository provided by default in ns-3-allinone is called bake.
以上两种技术(源存档,或通过 Git 的 ns-3-allinone 仓库)对于使用 进行最基本安装是有用的,还带有一些附加组件(用于生成 Python 绑定的 pybindgen,以及用于网络动画的 netanim)。ns-3-allinone 默认提供的第三个仓库称为 bake。
Bake is a tool for coordinated software building from multiple repositories, developed for the project. Bake can be used to fetch development versions of the software, and to download and build extensions to the base distribution, such as the Direct Code Execution environment, Network Simulation Cradle, ability to create new Python bindings, and various "apps". If you envision that your installation may use advanced or optional features, you may wish to follow this installation path.
Bake 是一个用于从多个仓库协调构建软件的工具,专为 项目开发。Bake 可用于获取 软件的开发版本,并下载和构建对基础 发行版的扩展,例如直接代码执行环境、网络模拟支架、创建新的 Python 绑定的能力,以及各种 “应用程序”。如果您预计您的 安装可能会使用高级或可选功能,您可能希望遵循这种安装路径。
In recent releases, Bake has been included in the release tarball. The configuration file included in the released version will allow one to download any software that was current at the time of the release. That is, for example, the version of Bake that is distributed with the ns-3.30 release can be used to fetch components for that release or earlier, but can't be used to fetch components for later releases (unless the bakeconf. package description file is updated).
在最近的 版本中,Bake 已经包含在发布的 tarball 中。发布版本中包含的配置文件将允许用户下载发布时的任何软件。也就是说,例如,与 ns-3.30 发布一起分发的 Bake 版本可以用于获取该 发布或更早版本的组件,但不能用于获取以后版本的组件(除非更新 bakeconf. 包描述文件)。
You can also get the most recent copy of bake by typing the following into your Linux shell (assuming you have installed Git):
您还可以通过在 Linux shell 中输入以下内容来获取 Bake 的最新副本(假设您已安装 Git):
$cd
$ mkdir workspace
$ cd workspace
$ git clone https://gitlab.com/nsnam/bake.git
As the git command executes, you should see something like the following displayed:
当执行 git 命令时,您应该看到类似以下内容的显示:
Cloning into 'bake'...
remote: Enumerating objects: 2086, done.
remote: Counting objects: 100% (2086/2086), done.
remote: Compressing objects: 100% (649/649), done.
remote: Total 2086 (delta 1404), reused 2078 (delta 1399)
Receiving objects: 100% (2086/2086), 2.68 MiB | 3.82 MiB/s, done.
Resolving deltas: 100% (1404/1404), done.
After the clone command completes, you should have a directory called bake, the contents of which should look something like the following:
克隆命令完成后,您应该有一个名为 bake 的目录,其内容应该类似于以下内容:
cd bake
bake bakeconf.xml bake.py doc examples generate-binary.py test TODO
Notice that you have downloaded some Python scripts, a Python module called bake, and an XML configuration file. The next step will be to use those scripts to download and build the distribution of your choice.
请注意,您已下载了一些 Python 脚本,一个名为 bake 的 Python 模块和一个 XML 配置文件。下一步将是使用这些脚本来下载和构建您选择的