4个虚拟机
multicast_source:组播源
multicast_destination1: 组播成员1
multicast_destination2: 组播成员2
multicat:OVS交换机
1. 拓扑图
2. 测试流程
2.1 OVS交换机
- dpdk脚本文件接管三张网卡
cd usertools_ptdnlab
sudo python dpdk_config.py
- OVS配置
#export PATH=$PATH:/usr/local/share/openvswitch/scripts
sudo ovs-ctl start #启动
sudo ovs-vsctl show #显示网桥信息
#创建数据通路为dpdk的网桥
sudo ovs-vsctl add-br br0 -- set bridge br0 datapath_type=netdev
#创建dpdk接管的(虚拟)端口
sudo ovs-vsctl add-port br0 dpdk0 -- set Interface dpdk0 type=dpdk options:dpdk-devargs=0000:00:09.0# pci号
#对应的删除端口的命令
sudo ovs-vsctl del-port br0 dpdk0
显示网桥信息:
- 添加流表
sudo ovs-ofctl -O OpenFlow13 add-flow br0 in_port=dpdk0,action=output:dpdk1,output:dpdk2
#删除流表
sudo ovs-ofctl -O OpenFlow13 del-flow br0 in_port=dpdk0
- 查看流表
sudo ovs-ofctl -O OpenFlow13 dump-flows br0
可见已经配置上了流表项。
2.2 组播源
- 给ens9网卡配置ip地址
sudo ifconfig ens9 down
sudo ip addr add 192.168.1.2/24 dev ens9
sudo ifconfig ens9 up
- 发送程序
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
struct in_addr localInterface;
struct sockaddr_in groupSock;
int sd;
char databuf[1024] = "Multicast test message lol!";
int datalen = sizeof(databuf);
int main (int argc, char *argv[ ])
{
sd = socket(AF_INET, SOCK_DGRAM, 0);
if(sd < 0) {
perror("Opening datagram socket error");
exit(1);
} else
printf("Opening the datagram socket...OK.\n");
memset((char *) &groupSock, 0, sizeof(groupSock));
groupSock.sin_family = AF_INET;
groupSock.sin_addr.s_addr = inet_addr("226.1.1.1");
groupSock.sin_port = htons(54321);
localInterface.s_addr = inet_addr("192.168.1.2");
if(setsockopt(sd, IPPROTO_IP, IP_MULTICAST_IF, (char *)&localInterface, sizeof(localInterface)) < 0) {
perror("Setting local interface error");
exit(1);
} else
printf("Setting the local interface...OK\n");
if(sendto(sd, databuf, datalen, 0, (struct sockaddr*)&groupSock, sizeof(groupSock)) < 0) {
perror("Sending datagram message error");}
else
printf("Sending datagram message...OK\n");
return 0;
}
2.3 组播成员
- 接收程序
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct sockaddr_in localSock;
struct ip_mreq group;
int sd;
int datalen;
char databuf[1500];
int main(int argc, char *argv[])
{
sd = socket(AF_INET, SOCK_DGRAM, 0);
if(sd < 0){
perror("Opening datagram socket error");
exit(1);
} else
printf("Opening datagram socket....OK.\n");
#if 1
int reuse = 1;
if(setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0){
perror("Setting SO_REUSEADDR error");
close(sd);
exit(1);
} else
printf("Setting SO_REUSEADDR...OK.\n");
#endif
memset((char *) &localSock, 0, sizeof(localSock));
localSock.sin_family = AF_INET;
localSock.sin_port = htons(54321);
localSock.sin_addr.s_addr = INADDR_ANY;
if(bind(sd, (struct sockaddr*)&localSock, sizeof(localSock))){
perror("Binding datagram socket error");
close(sd);
exit(1);
} else
printf("Binding datagram socket...OK.\n");
#if 0 //加入组播组
ip_mreq multiCast;
multiCast.imr_multiaddr.S_un.S_addr=inet_addr("234.2.2.2"); //组播组的IP地址。
multiCast.imr_interface.S_un.S_addr=INADDR_ANY; //本地某一网络设备接口的IP地址。
setsockopt(serSocket,IPPROTO_IP,IP_ADD_MEMBERSHIP,(char*)&multiCast,sizeof(multiCast));
#endif
group.imr_multiaddr.s_addr = inet_addr("226.1.1.1");
group.imr_interface.s_addr = inet_addr("192.168.1.3");
if(setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&group, sizeof(group)) < 0){
perror("Adding multicast group error");
close(sd);
exit(1);
} else
printf("Adding multicast group...OK.\n");
datalen = sizeof(databuf);
if(read(sd, databuf, datalen) < 0){
perror("Reading datagram message error");
close(sd);
exit(1);
} else {
printf("Reading datagram message...OK.\n");
printf("The message from multicast server is: %d\n", datalen);
}
return 0;
}
- 防火墙开放54321端口
sudo ufw allow 54321
sudo ufw reload
3. 测试结果
组播源侧:
组播成员侧:
wireshark中抓包能够看到multicast_2和multicast_3中有组播报文。