opendaylight(Li) l2switch 源代码分析(3)--packethandler

本文详细介绍了OpenDaylight l2switch的PacketHandler工程,包括config、implementation和model目录的结构与功能。重点分析了packet-handler的pom.xml,model目录中的yang文件以及implementation中的service实现,解释了如何处理和解析不同类型的网络包。
摘要由CSDN通过智能技术生成
本文介绍packethander工程,该工程用于对进入到controller的packet进行解析并分发到不同的
模块进行处理。
工程分为四个部分:
config文件夹:packethander的配置信息,如plugin的部署顺序,初始化时的配置。
implementation文件夹:service的具体实现,包含了java和yang。
model文件夹:定义了packet相关的yang。
pom.xml


一、pom.xml
packethander的pom.xml文件很简单,主要包含一下两部分:
  <parent>
    <groupId>org.opendaylight.l2switch</groupId>
    <artifactId>l2switch-parent</artifactId>
    <version>0.4.0-SNAPSHOT</version>
    <relativePath>../parent</relativePath>
  </parent>
该配置指向了之前讲的parent工程,说明packethander继承了parent,所以很多配置都可以省略。

  <modules>
    <module>model</module>
    <module>implementation</module>
    <module>config</module>
  </modules>
该配置指向了config,implementation和model三个文件夹,maven会去这三个文件夹下面去找
pom.xml文件。


二、model
该目录包含了五个yang文件和一个pom.xml.

1.arp-packet.yang
该文件主要定义了一个grouping和一个notification。
grouping arp-packet-fields {
leaf xxx {
}

leaf xxx {
}
......
uses bp
在 C++ 中获取网络性能指标需要使用系统调用或网络库提供的 API。以下是一些常用的函数: 1. 时延(延迟):使用 ping 命令可以获取网络延迟信息。在 C++ 中,可以使用 system() 函数调用 ping 命令并解析输出结果。例如: ```cpp #include <iostream> #include <cstdio> #include <cstring> using namespace std; float getDelay(const char* ipAddress) { char cmd[100]; sprintf(cmd, "ping -c 1 %s | grep 'time=' | awk '{print $7}'", ipAddress); FILE* fp = popen(cmd, "r"); if (!fp) { cerr << "Error: Failed to execute ping command." << endl; return -1; } char output[100]; fgets(output, sizeof(output), fp); pclose(fp); return atof(output); } ``` 2. 速率:可以使用系统调用获取接口的最大带宽,例如在 Linux 上,可以读取 `/sys/class/net/eth0/speed` 文件获取接口的最大速率。例如: ```cpp #include <iostream> #include <fstream> #include <string> using namespace std; float getMaxBandwidth() { ifstream ifs("/sys/class/net/eth0/speed"); if (!ifs.is_open()) { cerr << "Error: Failed to open file." << endl; return -1; } string line; getline(ifs, line); ifs.close(); return stof(line); } ``` 3. 吞吐量:可以使用网络库(如 libpcap 或 WinPcap)捕获网络数据包并计算吞吐量。例如: ```cpp #include <iostream> #include <pcap.h> using namespace std; void packetHandler(u_char* userData, const struct pcap_pkthdr* pkthdr, const u_char* packetData) { int* pktCount = reinterpret_cast<int*>(userData); (*pktCount)++; } int main() { char errbuf[PCAP_ERRBUF_SIZE]; pcap_t* descr = pcap_open_live("eth0", BUFSIZ, 0, -1, errbuf); if (!descr) { cerr << "Error: " << errbuf << endl; return -1; } int pktCount = 0; pcap_loop(descr, -1, packetHandler, reinterpret_cast<u_char*>(&pktCount)); pcap_close(descr); cout << "Packets received: " << pktCount << endl; return 0; } ``` 4. 丢包率:可以使用 ping 命令的丢包统计信息。例如: ```cpp #include <iostream> #include <cstdio> #include <cstring> using namespace std; float getPacketLoss(const char* ipAddress) { char cmd[100]; sprintf(cmd, "ping -c 10 %s | grep 'packet loss' | awk '{print $6}'", ipAddress); FILE* fp = popen(cmd, "r"); if (!fp) { cerr << "Error: Failed to execute ping command." << endl; return -1; } char output[100]; fgets(output, sizeof(output), fp); pclose(fp); return atof(output); } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值