NS-3中Config::Connect与Config::ConnectWithoutContext的区别

本文通过CSMA网络ping实例,详细解析了ns-3中配置连接(Connect)与无上下文连接(ConnectWithoutContext)的功能,以及如何通过Tracing实现数据收集与分析。具体展示了如何配置Tracing源,以及如何使用回调函数处理接收到的数据包和ping RTT。通过CSMA网络拓扑结构,直观地展示了数据包的传输路径和ping RTT的计算过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

void ns3::Config::ConnectWithoutContext (std::string      path,const CallbackBase &cb )

This function will attempt to find all trace sources which match the input path and will then connect the input callback to them.


void ns3::Config::Connect (std::string      path,const CallbackBase &cb ) 

This function will attempt to find all trace sources which match the input path and will then connect the input callback to them in such a way that the callback will receive an extra context string upon trace event notification.


以一个csma网络 ping的例子来解释,见: /src/csma/examples/csma-ping.cc

V4Ping :  http://www.nsnam.org/doxygen/classns3_1_1_v4_ping.html#af207b6dc0ed793798083c34a1c388770

TraceSources defined for this type:

  • Rtt: The rtt calculated by the ping.

This object is accessible through the following paths with Config::Set and Config::Connect:

  • /NodeList/[i]/ApplicationList/[i]/$ns3::V4Ping

PacketSink :http://www.nsnam.org/doxygen/classns3_1_1_packet_sink.html

TraceSources defined for this type:

  • Rx: A packet has been received

This object is accessible through the following paths with Config::Set and Config::Connect:

  • /NodeList/[i]/ApplicationList/[i]/$ns3::PacketSink

// Network topology
//
//       n0    n1   n2   n3
//       |     |    |    |
//     =====================
//
//  node n0,n1,n3 pings to node n2
//  node n0 generates protocol 2 (IGMP) to node n3

...


static void SinkRx (Ptr<const Packet> p, const Address &ad)
{
  std::cout << *p << std::endl;
}

static void PingRtt (std::string context, Time rtt)
{
  std::cout << context << " " << rtt << std::endl;
}
...

int main (int argc, char * argv[])

{

...

  // then, print what the packet sink receives.
  Config::ConnectWithoutContext ("/NodeList/3/ApplicationList/0/$ns3::PacketSink/Rx",
                                 MakeCallback (&SinkRx));
  // finally, print the ping rtts.
  Config::Connect ("/NodeList/*/ApplicationList/*/$ns3::V4Ping/Rtt",
                   MakeCallback (&PingRtt));

...


}



执行: gpf@guopengfei:~/ns-312/ns-allinone-3.12.1/ns-3.12.1$ ./waf --run src/csma/examples/csma-ping

结果:

Waf: Entering directory `/home/gpf/ns-312/ns-allinone-3.12.1/ns-3.12.1/build'
Waf: Leaving directory `/home/gpf/ns-312/ns-allinone-3.12.1/ns-3.12.1/build'
'build' finished successfully (5.522s)
ns3::Ipv4Header (tos 0x0 ttl 64 id 0 protocol 2 offset (bytes) 0 flags [none] length: 1220 192.168.1.1 > 192.168.1.4) Payload (size=1200)
/NodeList/0/ApplicationList/1/$ns3::V4Ping/Rtt +12766594.0ns
/NodeList/1/ApplicationList/0/$ns3::V4Ping/Rtt +25762594.0ns
/NodeList/3/ApplicationList/1/$ns3::V4Ping/Rtt +38792594.0ns
ns3::Ipv4Header (tos 0x0 ttl 64 id 2 protocol 2 offset (bytes) 0 flags [none] length: 1220 192.168.1.1 > 192.168.1.4) Payload (size=1200)
ns3::Ipv4Header (tos 0x0 ttl 64 id 3 protocol 2 offset (bytes) 0 flags [none] length: 1220 192.168.1.1 > 192.168.1.4) Payload (size=1200)
/NodeList/0/ApplicationList/1/$ns3::V4Ping/Rtt +4352998.0ns
/NodeList/1/ApplicationList/0/$ns3::V4Ping/Rtt +8709998.0ns
/NodeList/3/ApplicationList/1/$ns3::V4Ping/Rtt +13800998.0ns
ns3::Ipv4Header (tos 0x0 ttl 64 id 5 protocol 2 offset (bytes) 0 flags [none] length: 1220 192.168.1.1 > 192.168.1.4) Payload (size=1200)
/NodeList/0/ApplicationList/1/$ns3::V4Ping/Rtt +4352998.0ns
/NodeList/1/ApplicationList/0/$ns3::V4Ping/Rtt +9059998.0ns
/NodeList/3/ApplicationList/1/$ns3::V4Ping/Rtt +13493998.0ns
ns3::Ipv4Header (tos 0x0 ttl 64 id 7 protocol 2 offset (bytes) 0 flags [none] length: 1220 192.168.1.1 > 192.168.1.4) Payload (size=1200)
ns3::Ipv4Header (tos 0x0 ttl 64 id 8 protocol 2 offset (bytes) 0 flags [none] length: 1220 192.168.1.1 > 192.168.1.4) Payload (size=1200)
ns3::Ipv4Header (tos 0x0 ttl 64 id 9 protocol 2 offset (bytes) 0 flags [none] length: 1220 192.168.1.1 > 192.168.1.4) Payload (size=1200)
ns3::Ipv4Header (tos 0x0 ttl 64 id 10 protocol 2 offset (bytes) 0 flags [none] length: 1220 192.168.1.1 > 192.168.1.4) Payload (size=1200)
ns3::Ipv4Header (tos 0x0 ttl 64 id 11 protocol 2 offset (bytes) 0 flags [none] length: 1220 192.168.1.1 > 192.168.1.4) Payload (size=1200)
ns3::Ipv4Header (tos 0x0 ttl 64 id 12 protocol 2 offset (bytes) 0 flags [none] length: 1220 192.168.1.1 > 192.168.1.4) Payload (size=1200)
ns3::Ipv4Header (tos 0x0 ttl 64 id 13 protocol 2 offset (bytes) 0 flags [none] length: 1220 192.168.1.1 > 192.168.1.4) Payload (size=1200)
ns3::Ipv4Header (tos 0x0 ttl 64 id 14 protocol 2 offset (bytes) 0 flags [none] length: 1220 192.168.1.1 > 192.168.1.4) Payload (size=1200)
ns3::Ipv4Header (tos 0x0 ttl 64 id 15 protocol 2 offset (bytes) 0 flags [none] length: 1220 192.168.1.1 > 192.168.1.4) Payload (size=1200)
ns3::Ipv4Header (tos 0x0 ttl 64 id 16 protocol 2 offset (bytes) 0 flags [none] length: 1220 192.168.1.1 > 192.168.1.4) Payload (size=1200)



-06-17_06:09:24.66584 time="2025-06-17T14:09:24+08:00" level=info msg="Providing metrics at localhost:9121/metrics" ==> /var/log/gitlab/prometheus/current <== 2025-06-17_06:09:26.82251 ts=2025-06-17T06:09:26.815Z caller=manager.go:292 level=error component="discovery manager scrape" msg="Cannot create service discovery" err="unable to read CA cert: unable to read file /var/run/secrets/kubernetes.io/serviceaccount/ca.crt: open /var/run/secrets/kubernetes.io/serviceaccount/ca.crt: no such file or directory" type=kubernetes config=kubernetes-pods 2025-06-17_06:09:26.82251 ts=2025-06-17T06:09:26.815Z caller=manager.go:292 level=error component="discovery manager scrape" msg="Cannot create service discovery" err="unable to read CA cert: unable to read file /var/run/secrets/kubernetes.io/serviceaccount/ca.crt: open /var/run/secrets/kubernetes.io/serviceaccount/ca.crt: no such file or directory" type=kubernetes config=kubernetes-cadvisor 2025-06-17_06:09:26.82252 ts=2025-06-17T06:09:26.815Z caller=manager.go:292 level=error component="discovery manager scrape" msg="Cannot create service discovery" err="unable to read CA cert: unable to read file /var/run/secrets/kubernetes.io/serviceaccount/ca.crt: open /var/run/secrets/kubernetes.io/serviceaccount/ca.crt: no such file or directory" type=kubernetes config=kubernetes-nodes 2025-06-17_06:09:26.82252 ts=2025-06-17T06:09:26.821Z caller=main.go:1441 level=info msg="updated GOGC" old=100 new=75 2025-06-17_06:09:26.82252 ts=2025-06-17T06:09:26.821Z caller=main.go:1452 level=info msg="Completed loading of configuration file" filename=/var/opt/gitlab/prometheus/prometheus.yml totalDuration=7.99528ms db_storage=6.104µs remote_storage=1.711µs web_handler=480ns query_engine=3.189µs scrape=168.192µs scrape_sd=305.836µs notify=26.207µs notify_sd=6.99µs rules=6.254412ms tracing=10.127µs 2025-06-17_06:09:26.82252 ts=2025-06-17T06:09:26.821Z caller=main.go:1182 level=info msg="Server is ready to receive web requests." 2025-06-17_06:09:26.82253 ts=2025-06-17T06:09:26.821Z caller=manager.go:164 level=info component="rule manager" msg="Starting rule manager..." 2025-06-17_06:09:31.84939 ts=2025-06-17T06:09:31.848Z caller=manager.go:186 level=error component="scrape manager" msg="error creating new scrape pool" err="error creating HTTP client: unable to read CA cert: unable to read file /var/run/secrets/kubernetes.io/serviceaccount/ca.crt: open /var/run/secrets/kubernetes.io/serviceaccount/ca.crt: no such file or directory" scrape_pool=kubernetes-cadvisor 2025-06-17_06:09:31.84942 ts=2025-06-17T06:09:31.848Z caller=manager.go:186 level=error component="scrape manager" msg="error creating new scrape pool" err="error creating HTTP client: unable to read CA cert: unable to read file /var/run/secrets/kubernetes.io/serviceaccount/ca.crt: open /var/run/secrets/kubernetes.io/serviceaccount/ca.crt: no such file or directory" scrape_pool=kubernetes-nodes 2025-06-17_06:09:31.84942 ts=2025-06-17T06:09:31.848Z caller=manager.go:186 level=error component="scrape manager" msg="error creating new scrape pool" err="error creating HTTP client: unable to read CA cert: unable to read file /var/run/secrets/kubernetes.io/serviceaccount/ca.crt: open /var/run/secrets/kubernetes.io/serviceaccount/ca.crt: no such file or directory" scrape_pool=kubernetes-pods ==> /var/log/gitlab/alertmanager/current <== 2025-06-17_06:09:06.82586 ts=2025-06-17T06:09:06.822Z caller=main.go:181 level=info msg="Starting Alertmanager" version="(version=0.27.0, branch=master, revision=0aa3c2aad14cff039931923ab16b26b7481783b5)" 2025-06-17_06:09:06.82589 ts=2025-06-17T06:09:06.822Z caller=main.go:182 level=info build_context="(go=go1.23.2, platform=linux/amd64, user=GitLab-Omnibus, date=, tags=unknown)" 2025-06-17_06:09:06.95589 ts=2025-06-17T06:09:06.944Z caller=cluster.go:186 level=info component=cluster msg="setting advertise address explicitly" addr=192.168.58.161 port=9094 2025-06-17_06:09:06.98836 ts=2025-06-17T06:09:06.972Z caller=cluster.go:683 level=info component=cluster msg="Waiting for gossip to settle..." interval=2s 2025-06-17_06:09:07.06211 ts=2025-06-17T06:09:07.060Z caller=coordinator.go:113 level=info component=configuration msg="Loading configuration file" file=/var/opt/gitlab/alertmanager/alertmanager.yml 2025-06-17_06:09:07.06213 ts=2025-06-17T06:09:07.060Z caller=coordinator.go:126 level=info component=configuration msg="Completed loading of configuration file" file=/var/opt/gitlab/alertmanager/alertmanager.yml 2025-06-17_06:09:07.10829 ts=2025-06-17T06:09:07.108Z caller=tls_config.go:313 level=info msg="Listening on" address=127.0.0.1:9093 2025-06-17_06:09:07.10831 ts=2025-06-17T06:09:07.108Z caller=tls_config.go:316 level=info msg="TLS is disabled." http2=false address=127.0.0.1:9093 2025-06-17_06:09:08.97340 ts=2025-06-17T06:09:08.973Z caller=cluster.go:708 level=info component=cluster msg="gossip not settled" polls=0 before=0 now=1 elapsed=2.000709881s 2025-06-17_06:09:17.00509 ts=2025-06-17T06:09:17.005Z caller=cluster.go:700 level=info component=cluster msg="gossip settled; proceeding" elapsed=10.032381279s ==> /var/log/gitlab/postgres-exporter/current <== 2025-06-17_06:03:31.65751 ts=2025-06-17T06:03:31.654Z caller=tls_config.go:277 level=info msg="TLS is disabled." http2=false address=127.0.0.1:9187 2025-06-17_06:03:42.90526 ts=2025-06-17T06:03:42.904Z caller=server.go:74 level=info msg="Established new database connection" fingerprint=/var/opt/gitlab/postgresql:5432 2025-06-17_06:03:43.05861 ts=2025-06-17T06:03:43.054Z caller=postgres_exporter.go:613 level=info msg="Semantic version changed" server=/var/opt/gitlab/postgresql:5432 from=0.0.0 to=14.17.0 2025-06-17_06:09:22.08609 ts=2025-06-17T06:09:22.084Z caller=main.go:86 level=warn msg="Error loading config" err="Error opening config file \"postgres_exporter.yml\": open postgres_exporter.yml: no such file or directory" 2025-06-17_06:09:22.08611 ts=2025-06-17T06:09:22.085Z caller=proc.go:272 msg="Excluded databases" databases=[] 2025-06-17_06:09:22.08611 ts=2025-06-17T06:09:22.085Z caller=main.go:99 level=warn msg="The extended queries.yaml config is DEPRECATED" file=/var/opt/gitlab/postgres-exporter/queries.yaml 2025-06-17_06:09:22.08611 ts=2025-06-17T06:09:22.085Z caller=tls_config.go:274 level=info msg="Listening on" address=127.0.0.1:9187 2025-06-17_06:09:22.08611 ts=2025-06-17T06:09:22.086Z caller=tls_config.go:277 level=info msg="TLS is disabled." http2=false address=127.0.0.1:9187 2025-06-17_06:09:42.90780 ts=2025-06-17T06:09:42.906Z caller=server.go:74 level=info msg="Established new database connection" fingerprint=/var/opt/gitlab/postgresql:5432 2025-06-17_06:09:42.95498 ts=2025-06-17T06:09:42.953Z caller=postgres_exporter.go:613 level=info msg="Semantic version changed" server=/var/opt/gitlab/postgresql:5432 from=0.0.0 to=14.17.0 ==> /var/log/gitlab/gitlab-workhorse/current <== {"correlation_id":"","duration_ms":0,"error":"badgateway: failed to receive response: dial unix /var/opt/gitlab/gitlab-rails/sockets/gitlab.socket: connect: connection refused","level":"error","method":"GET","msg":"","time":"2025-06-17T14:13:00+08:00","uri":""}
最新发布
06-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值