LINUX中socket与VRF怎么用
发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,这篇文章给大家分享的是有关LINUX中socket与VRF怎么用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。LINUX socket与VRF实验环境如下图所示:配置如下:
千家信息网最后更新 2025年12月02日LINUX中socket与VRF怎么用
这篇文章给大家分享的是有关LINUX中socket与VRF怎么用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
LINUX socket与VRF
实验环境如下图所示:
配置如下:
#!/bin/bashsudo ip netns add ns1 sudo ip link add ns1veth2 type veth peer name eth0 netns ns1sudo ip netns add ns2sudo ip link add ns2veth2 type veth peer name eth0 netns ns2sudo ip link set ns1veth2 master vrftestsudo ip link set ns2veth2 master vrftestsudo ip link set ns2veth2 upsudo ip link set ns1veth2 upsudo ip addr add 1.1.1.254/24 dev ns1veth2 sudo ip addr add 2.2.2.254/24 dev ns2veth2 sudo ip netns exec ns2 ip addr add 2.2.2.1/24 dev eth0 sudo ip netns exec ns1 ip addr add 1.1.1.1/24 dev eth0 sudo ip netns exec ns1 ip link set eth0 upsudo ip netns exec ns1 ip link set lo upsudo ip netns exec ns1 ip route add default via 1.1.1.254 dev eth0sudo ip netns exec ns2 ip link set eth0 upsudo ip netns exec ns2 ip link set lo upsudo ip netns exec ns2 ip route add default via 2.2.2.254 dev eth0
实验使用c语言写了两个套接字交互程序:
服务器:vrfs
#include#include #include #include #include #include #include #include #define MAXLINE 4096int main(int argc, char** argv){ int listenfd, connfd; struct sockaddr_in servaddr; char buff[4096]; int n; int on = 1; if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ){ printf("create socket error: %s(errno: %d)\n",strerror(errno),errno); exit(0); } setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)); setsockopt(listenfd, SOL_SOCKET, SO_REUSEPORT, (void *)&on, sizeof(on)); memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(6666); if(argc == 2){ printf("vrf device name: %s\r\n", argv[1]); if(0 > setsockopt(listenfd, SOL_SOCKET, SO_BINDTODEVICE, argv[1], strlen(argv[1])+1)){ printf("bind socket master dev error: %s(errno: %d)\n",strerror(errno),errno); exit(0); } } if( bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1){ printf("bind socket error: %s(errno: %d)\n",strerror(errno),errno); exit(0); } if( listen(listenfd, 10) == -1){ printf("listen socket error: %s(errno: %d)\n",strerror(errno),errno); exit(0); } printf("======waiting for client's request======\n"); while(1){ if((connfd = accept(listenfd, (struct sockaddr*)NULL, NULL)) == -1){ printf("accept socket error: %s(errno: %d)",strerror(errno),errno); continue; } n = recv(connfd, buff, MAXLINE, 0); buff[n] = '\0'; printf("recv msg from client: %s\n", buff); close(connfd); } close(listenfd);}
客户端程序:vrfc
#include#include #include #include #include #include #include #include #define MAXLINE 4096#include int main(int argc, char** argv){ int sockfd, n; char *sendline = "hello vrf"; struct sockaddr_in servaddr; if( argc != 2){ printf("usage: ./client [master device]\n"); exit(0); } if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){ printf("create socket error: %s(errno: %d)\n", strerror(errno),errno); exit(0); } memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(6666); if( inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0){ printf("inet_pton error for %s\n",argv[1]); exit(0); } if(argc == 3){ printf("vrf device name: %s\r\n", argv[2]); if(0 > setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE, argv[2], strlen(argv[2])+1)){ printf("bind socket master dev error: %s(errno: %d)\n",strerror(errno),errno); exit(0); } } if( connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0){ printf("connect error: %s(errno: %d)\n",strerror(errno),errno); exit(0); } printf("send msg to server: hello vrf\n"); if( send(sockfd, sendline, strlen(sendline), 0) < 0) { printf("send msg error: %s(errno: %d)\n", strerror(errno), errno); exit(0); } close(sockfd); exit(0);}
实验一:惊群效应
在默认VRF环境下,启动两个进程,监听相同的端口和地址:程序中套接口使用了SO_REUSEADDR和SO_REUSEPORT。查看内核如何处理惊群效应。
console1:
admin@ubuntu:~/vrfsocket$ for i in {0..9}; do ./vrfc 127.0.0.1; done send msg to server: hello vrfsend msg to server: hello vrfsend msg to server: hello vrfsend msg to server: hello vrfsend msg to server: hello vrfsend msg to server: hello vrfsend msg to server: hello vrfsend msg to server: hello vrfsend msg to server: hello vrfsend msg to server: hello vrfadmin@ubuntu:~/vrfsocket$console2:
admin@ubuntu:~/vrfsocket$ ./vrfs ======waiting for client's request======recv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrf
console3:
admin@ubuntu:~/vrfsocket$ ./vrfs ======waiting for client's request======recv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrf
结论
新内核似乎已经能够处理惊群效应了,收到请求时不再通知所有监听该端口的服务器程序,而是会进行一定的负载均衡调度处理。
实验二:启动两个服务器,一个绑定VRF,一个不绑定,客户端不绑定VRF
console1:
admin@ubuntu:~/vrfsocket$ for i in {0..9}; do sudo ./vrfc 127.0.0.1; donesend msg to server: hello vrfsend msg to server: hello vrfsend msg to server: hello vrfsend msg to server: hello vrfsend msg to server: hello vrfsend msg to server: hello vrfsend msg to server: hello vrfsend msg to server: hello vrfsend msg to server: hello vrfsend msg to server: hello vrfadmin@ubuntu:~/vrfsocket$console2:
root@ubuntu:/home/admin/vrfsocket# ./vrfs======waiting for client's request======recv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrf
console3:
root@ubuntu:/home/admin/vrfsocket# ./vrfs vrftestvrf device name: vrftest======waiting for client's request======
结论:服务器监听套接字绑定VRF后,不再处理默认VRF中的请求
实验三:启动两个服务器,一个绑定VRF,一个不绑定,客户端绑定VRF
console1:
admin@ubuntu:~/vrfsocket$ for i in {0..9}; do sudo ./vrfc 1.1.1.254 vrftest; donevrf device name: vrftestsend msg to server: hello vrfvrf device name: vrftestsend msg to server: hello vrfvrf device name: vrftestsend msg to server: hello vrfvrf device name: vrftestsend msg to server: hello vrfvrf device name: vrftestsend msg to server: hello vrfvrf device name: vrftestsend msg to server: hello vrfvrf device name: vrftestsend msg to server: hello vrfvrf device name: vrftestsend msg to server: hello vrfvrf device name: vrftestsend msg to server: hello vrfvrf device name: vrftestsend msg to server: hello vrfadmin@ubuntu:~/vrfsocket$console2:在root用户下运行
root@ubuntu:/home/admin/vrfsocket# ./vrfs======waiting for client's request======
console3:在root用户下运行。
root@ubuntu:/home/admin/vrfsocket# ./vrfs vrftestvrf device name: vrftest======waiting for client's request======recv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrf
结论:服务器监听套接字不绑定VRF,不能处理非默认VRF中的请求
实验四:设置sudo sysctl -w net.ipv4.tcp_l3mdev_accept=1
启动两个服务器,一个绑定VRF,一个不绑定,客户端不绑定VRF
console1:
admin@ubuntu:~/vrfsocket$ for i in {0..9}; do sudo ./vrfc 127.0.0.1; donesend msg to server: hello vrfsend msg to server: hello vrfsend msg to server: hello vrfsend msg to server: hello vrfsend msg to server: hello vrfsend msg to server: hello vrfsend msg to server: hello vrfsend msg to server: hello vrfsend msg to server: hello vrfsend msg to server: hello vrfadmin@ubuntu:~/vrfsocket$console2:
root@ubuntu:/home/admin/vrfsocket# ./vrfs======waiting for client's request======recv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrf
console3:
root@ubuntu:/home/admin/vrfsocket# ./vrfs vrftestvrf device name: vrftest======waiting for client's request======
启动一个服务器,绑定VRF,客户端不绑定VRF
console1:
admin@ubuntu:~/vrfsocket$ for i in {0..9}; do sudo ./vrfc 127.0.0.1; doneconnect error: Connection refused(errno: 111)connect error: Connection refused(errno: 111)connect error: Connection refused(errno: 111)connect error: Connection refused(errno: 111)connect error: Connection refused(errno: 111)connect error: Connection refused(errno: 111)connect error: Connection refused(errno: 111)connect error: Connection refused(errno: 111)connect error: Connection refused(errno: 111)connect error: Connection refused(errno: 111)admin@ubuntu:~/vrfsocket$console3:
root@ubuntu:/home/admin/vrfsocket# ./vrfs vrftestvrf device name: vrftest======waiting for client's request======
启动一个服务器,绑定VRF,客户端绑定VRF
console1:
admin@ubuntu:~/vrfsocket$ for i in {0..9}; do sudo ./vrfc 1.1.1.254 vrftest; donevrf device name: vrftestsend msg to server: hello vrfvrf device name: vrftestsend msg to server: hello vrfvrf device name: vrftestsend msg to server: hello vrfvrf device name: vrftestsend msg to server: hello vrfvrf device name: vrftestsend msg to server: hello vrfvrf device name: vrftestsend msg to server: hello vrfvrf device name: vrftestsend msg to server: hello vrfvrf device name: vrftestsend msg to server: hello vrfvrf device name: vrftestsend msg to server: hello vrfvrf device name: vrftestsend msg to server: hello vrfadmin@ubuntu:~/vrfsocket$console3:
root@ubuntu:/home/admin/vrfsocket# ./vrfs vrftestvrf device name: vrftest======waiting for client's request======recv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrf
启动两个服务器,一个绑定VRF,一个不绑定,客户端绑定VRF
console1:
admin@ubuntu:~/vrfsocket$ for i in {0..9}; do sudo ./vrfc 1.1.1.254 vrftest; donevrf device name: vrftestsend msg to server: hello vrfvrf device name: vrftestsend msg to server: hello vrfvrf device name: vrftestsend msg to server: hello vrfvrf device name: vrftestsend msg to server: hello vrfvrf device name: vrftestsend msg to server: hello vrfvrf device name: vrftestsend msg to server: hello vrfvrf device name: vrftestsend msg to server: hello vrfvrf device name: vrftestsend msg to server: hello vrfvrf device name: vrftestsend msg to server: hello vrfvrf device name: vrftestsend msg to server: hello vrfadmin@ubuntu:~/vrfsocket$console2:
root@ubuntu:/home/admin/vrfsocket# ./vrfs======waiting for client's request======recv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrfrecv msg from client: hello vrf
console3:
root@ubuntu:/home/admin/vrfsocket# ./vrfs vrftestvrf device name: vrftest======waiting for client's request======
在打开sudo sysctl -w net.ipv4.tcp_l3mdev_accept=1后,默认VRF中的监听套接字能够处理所有VRF中的请求,且优先级高于其它的VRF的监听套接字。
| 序号 | 结论 |
|---|---|
| 1 | 多个服务器器监听同一地址和端口,内核会进行负载均衡,选择唤醒其中一个进程处理请求。 |
| 2 | 默认VRF中的服务器进程不能处理非默认VRF中的请求,非默认VRF中的服务器进程不能处理其它VRF中的请求 |
| 3 | 开启net.ipv4.tcp_l3mdev_accept=1后,默认VRF中的服务器进程可以处理任意VRF中的请求,且优先级最高 |
| 4 | 开启net.ipv4.tcp_l3mdev_accept=1后,非默认VRF中的服务器进程不能处理其它VRF中的请求,在处理本VRF中的流量时,优先级低于默认VRF中的进程。 |
感谢各位的阅读!关于"LINUX中socket与VRF怎么用"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
服务器
服务
处理
客户
客户端
进程
监听
两个
实验
套接字
程序
结论
优先级
内核
效应
端口
均衡
内容
地址
更多
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
杭州云象网络技术有限公司纯利润
王者荣耀战队能跨服务器加吗
html5注册用什么数据库
前端开发用什么服务器
给发网络安全的信息说明什么
服务器的信号传输方式
数据库创建date字段
计算机网络技术大连理工大学
smpm软件开发
外派软件开发人员
郑州云启网络技术有限公司
组织开展网络安全教育
税务网络安全通知
sql语言数据库查询
国外服务器空间
万方镜像系统数据库
南京漫居互联网科技有限公司
饥荒服务器未响应
数据库表添加主键sql
全国网络安全监督检查中心
蓝蝴蝶校园网搜索不到服务器
网络安全业务平台架构
银行业 网络安全
eos服务器节点
大学计算机数据库ppt
上海软通软件开发
医院网络安全硬件
如何用数据库判断三元催化好坏
珠海考试软件开发外包
公司员工数据库表设计