使用多个 NIC 广播 UDP 数据包 [英] Broadcasting UDP packets using multiple NICs

查看:34
本文介绍了使用多个 NIC 广播 UDP 数据包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Linux 中的相机控制器构建一个嵌入式系统(不是实时的).我在让网络做我想做的事情时遇到问题.该系统有 3 个 NIC、1 个 100base-T 和 2 个千兆端口.我将较慢的连接到相机(这是它所支持的全部),而较快的则是与其他机器的点对点连接.我正在尝试从相机获取图像,进行一些处理,然后使用 UDP 将其广播到其他每个 NIC.

I'm building an embedded system for a camera controller in Linux (not real-time). I'm having a problem getting the networking to do what I want it to do. The system has 3 NICs, 1 100base-T and 2 gigabit ports. I hook the slower one up to the camera (that's all it supports) and the faster ones are point-to-point connections to other machines. What I am attempting to do is get an image from the camera, do a little processing, then broadcast it using UDP to each of the other NICs.

这是我的网络配置:

eth0: addr: 192.168.1.200 Bcast 192.168.1.255 掩码:255.255.255.0(这是100base-t)
eth1:地址:192.168.2.100 Bcast 192.168.2.255 掩码:255.255.255.0
eth2:地址:192.168.3.100 Bcast 192.168.3.255 掩码:255.255.255.0

eth0: addr: 192.168.1.200 Bcast 192.168.1.255 Mask: 255.255.255.0 (this is the 100base-t)
eth1: addr: 192.168.2.100 Bcast 192.168.2.255 Mask: 255.255.255.0
eth2: addr: 192.168.3.100 Bcast 192.168.3.255 Mask: 255.255.255.0

图像以专有协议从 eth0 传入,因此它是原始套接字.我可以将它广播到 eth1 或 eth2 就好了.但是当我尝试将它一个接一个地广播给两者时,我在 eth0 上遇到了很多网络故障和错误.

The image is coming in off eth0 in a proprietary protocol, so it's a raw socket. I can broadcast it to eth1 or eth2 just fine. But when I try to broadcast it to both, one after the other, I get lots of network hiccups and errors on eth0.

我像这样初始化 UDP 套接字:

I initialize the UDP sockets like this:

sock2=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); // Or sock3
sa.sin_family=AF_INET;
sa.sin_port=htons(8000);
inet_aton("192.168.2.255",&sa.sin_addr); // Or 192.168.3.255
setsockopt(sock2, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast));
bind(sock2,(sockaddr*)&sa,sizeof(sa));

sendto(sock2,&data,sizeof(data),0,(sockaddr*)&sa,sizeof(sa)); // sizeof(data)<1100 bytes

我分别为每个套接字执行此操作,并分别调用 sendto.当我做一个或另一个时,这很好.当我尝试同时发送时,eth0 开始收到坏包.

I do this for each socket separately, and call sendto separately. When I do one or the other, it's fine. When I try to send on both, eth0 starts getting bad packets.

关于为什么会发生这种情况的任何想法?是不是配置错误,有没有更好的办法?

Any ideas on why this is happening? Is it a configuration error, is there a better way to do this?

感谢您的所有帮助,我一直在尝试一些事情并对此进行更多研究.严格来说,这个问题似乎不是广播的.我用单播命令替换了广播代码,它具有相同的行为.我想我更了解这种行为,但不了解如何解决它.

Thanks for all the help, I've been trying some things and looking into this more. The issue does not appear to be broadcasting, strictly speaking. I replaced the broadcast code with a unicast command and it has the same behavior. I think I understand the behavior better, but not how to fix it.

这就是正在发生的事情.在 eth0 上,我应该每 50 毫秒获取一次图像.当我在 eth1(或 2)上发送图像时,发送图像大约需要 1.5 毫秒.当我尝试同时发送 eth1 和 eth2 时,大约需要 45 毫秒,偶尔会跳到 90 毫秒.当超过 50ms 窗口时,eth0 的缓冲区开始构建.当然,当缓冲区已满时,我会丢失数据包.

Here is what is happening. On eth0 I am supposed to get an image every 50ms. When I send out an image on eth1 (or 2) it takes about 1.5ms to send the image. When I try to send on both eth1 and eth2 at the same time it takes about 45ms, occasionally jumping to 90ms. When this goes beyond the 50ms window, eth0's buffer starts to build. I lose packets when the buffer gets full, of course.

所以我修改后的问题.为什么从 1 个以太网端口变为 2 个以太网端口会从 1.5 毫秒变为 45 毫秒?

So my revised question. Why would it go from 1.5ms to 45ms just by going from one ethernet port to two?

这是我的初始化代码:

sock[i]=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
sa[i].sin_family=AF_INET;
sa[i].sin_port=htons(8000);
inet_aton(ip,&sa[i].sin_addr);

//If Broadcasting
char buffer[]="eth1" // or eth2
setsockopt(sock[i],SOL_SOCKET,SO_BINDTODEVICE,buffer,5);
int b=1;
setsockopt(sock[i],SOL_SOCKET,SO_BROADCAST,&b,sizeof(b));

这是我的发送代码:

for(i=0;i<65;i++) {
  sendto(sock[0],&data[i],sizeof(data),0,sa[0],sizeof(sa[0]));
  sendto(sock[1],&data[i],sizeof(data),0,sa[1],sizeof(sa[1]));
}

这是非常基本的.

有什么想法吗?感谢您的大力帮助!

Any ideas? Thanks for all your great help!

保罗

推荐答案

已经很久了,但是我找到了我的问题的答案,所以我想我会把它放在这里以防其他人找到它.

It's been a long time, but I found the answer to my question, so I thought I would put it here in case anyone else ever finds it.

p>

这两个千兆以太网端口实际上位于 PCI-express 总线之外的 PCI 桥上.PCI-express 总线位于主板内部,但它是连接卡的 PCI 总线.网桥和总线没有足够的带宽来真正快速地发送图像.仅启用一个 NIC 时,数据被发送到缓冲区,对我来说看起来非常快,但实际通过总线、从卡输出到线路需要更长的时间.第二个 NIC 较慢,因为缓冲区已满.尽管更改缓冲区大小掩盖了问题,但它实际上并没有更快地发送数据,而且我仍然在第三个 NIC 上丢包.

The two Gigabit Ethernet ports were actually on a PCI bridge off the PCI-express bus. The PCI-express bus was internal to the motherboard, but it was a PCI bus going to the cards. The bridge and the bus did not have enough bandwidth to actually send out the images that fast. With only one NIC enabled the data was sent to the buffer and it looked very quick to me, but it took much longer to actually get through the bus, out the card, and on to the wire. The second NIC was slower because the buffer was full. Although changing the buffer size masked the problem, it did not actually send the data out any faster and I was still getting dropped packets on the third NIC.

最后,100Base-T 卡实际上是内置在主板上的,因此有更快的总线连接到它,导致整体带宽比千兆端口更快.通过将相机切换到千兆线和其中一个千兆线到100Base-T线我能满足要求.

In the end, the 100Base-T card was actually built onto the motherboard, therefore had a faster bus to it, resulting in overall faster bandwidth than the gigabit ports.. By switching the camera to a gigabit line and one of the gigabit lines to the 100Base-T line I was able to meet the requirements.

奇怪.

这篇关于使用多个 NIC 广播 UDP 数据包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆