马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
1、今天要实现lwip的多个ip配置,原来以为必要本身修改许多核心代码
2、查阅资料才发现,lwip已经把接口留出来了
/** Define this to 1 and define LWIP_ARP_FILTER_NETIF_FN(pbuf, netif, type)
* to a filter function that returns the correct netif when using multiple
* netifs on one hardware interface where the netif's low-level receive
* routine cannot decide for the correct netif (e.g. when mapping multiple
* IP addresses to one hardware interface).
*/
/** 将此定义为1,并将LWIP_ARP_FILTER_NETIF_FN(pbuf, netif, type)定义为一个过滤函数,该函数在利用单个硬件接口上的多个netif时返回正确的netif。当netif的低级吸收例程无法确定正确的netif时(例如,当将多个IP地址映射到单个硬件接口时),必要利用此过滤函数。
*/
#ifndef LWIP_ARP_FILTER_NETIF
#define LWIP_ARP_FILTER_NETIF 0
#endif
3、所以我们只必要定义好宏
#define LWIP_ARP_FILTER_NETIF 1 //多ip的支持
和写好
struct netif * LWIP_ARP_FILTER_NETIF_FN(struct pbuf *p, struct netif *netifIn, u16_t type)
函数即可
4、我的操作
4.1> 在lwipopt.h定义使能宏LWIP_ARP_FILTER_NETIF ,以支持多ip的操作
4.2、编写LWIP_ARP_FILTER_NETIF_FN函数,以能根据ip能找到对应的网卡
- /*
- *********************************************************************************************************
- * Function Name : LWIP_ARP_FILTER_NETIF_FN
- * Description : 多ip,识别正确的网卡
- * Input : None
- * Output : None
- * Return : None
- *********************************************************************************************************
- */
- #include "lwip/prot/etharp.h"
- struct netif * LWIP_ARP_FILTER_NETIF_FN(struct pbuf *p, struct netif *netifIn, u16_t type)
- {
- struct netif *netif = NULL;
- struct etharp_hdr *hdr = NULL;
- struct ip_hdr *iphdr = NULL;
- ip_addr_t dest/*, src*/;
- switch (type)
- {
- /*ARP*/
- case 0x0806:
- hdr = (struct etharp_hdr *)((unsigned char*)p->payload + 14);
- memcpy(&dest, &(hdr->dipaddr), sizeof(ip4_addr_t));
- //memcpy(&src, &hdr->sipaddr, sizeof (ip4_addr_t));
- for (netif = netif_list; netif != NULL; netif = netif->next)
- {
- if (netif_is_up(netif))
- {
- if (ip4_addr_cmp(&dest,&(netif->ip_addr)))
- {
- break;
- }
- }
- }
- break;
- /*IP*/
- case 0x0800:
- iphdr = (struct ip_hdr *) ((unsigned char*)p->payload + 14);
- ip_addr_copy_from_ip4(dest, iphdr->dest);
- //ip_addr_copy_from_ip4(src, iphdr->src);
- for (netif = netif_list; netif != NULL; netif = netif->next)
- {
- if (netif_is_up(netif))
- {
- if (ip4_addr_cmp(&dest, &(netif->ip_addr)))
- {
- break;
- }
- }
- }
- break;
- default:
- netif = netif_list;
- break;
- }
- netifIn = netif;
- if(netif==NULL)
- {
- pbuf_free(p);
- }
- return netif;
-
- }
复制代码 根据ip去匹配netif,arp必要单独处理,因为ip通信首先都要发送arp去找物理地址,不加这个arp处理,会出现你ping不通ip。所以假如知道物理地址,不加这个arp处理也可以,直接本身填mac即可,加到你本身的静态arp表中。
大家留意:

这个一定要加上,我刚开始没加,结果刚开始通,运行一段时间导致不通了,就是因为这里没开释申请的内存,导致无法处理新的数据
4.3、增长多ip的设置
- //开始虚拟多ip--1
- IP4_ADDR(&ipaddr_v[1], 192,168,20,48);
- IP4_ADDR(&netmask_v[1], 255,255,255,0);
- IP4_ADDR(&gw_v[1], 192, 168, 20,1);
- netif_add(&g_netif_v[1], &ipaddr_v[1], &netmask_v[1], &gw_v[1], NULL, ðernetif_init, &tcpip_input); //网卡初始化和网卡输入
- netif_set_up(&g_netif_v[1]);
- //开始虚拟多ip--2
- IP4_ADDR(&ipaddr_v[2], 192,168,30,48);
- IP4_ADDR(&netmask_v[2], 255,255,255,0);
- IP4_ADDR(&gw_v[2], 192, 168, 30,1);
- netif_add(&g_netif_v[2], &ipaddr_v[2], &netmask_v[2], &gw_v[2], NULL, ðernetif_init, &tcpip_input); //网卡初始化和网卡输入
- netif_set_up(&g_netif_v[2]);
复制代码 4.4、然后运行测试,OK
5、测试结果
刚开始测试结果是这样,我没在意,以为是硬件照旧那边题目,ping移植有超时,大概卡顿较大
有时,还会出现几十上百ms的延迟,特别是多个电脑同时ping时。
6、ping超时,卡顿较大原因
1> 原来多个neif_add时:
会每次都初始化网卡,这里网卡在初始化时,会创建分析网络停止数据包的使命,所以存在多个使命去读网卡数据,造成数据的竞争,从而导致大概出现超时,卡顿较大
2> 我们只有一个网卡,所以只需初始化1次分析网络停止数据包的使命
改了之后就OK了
像下面这篇文章这样:
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |