添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

使用NETLINK的Vlan接口详情

4 人关注

I understand that new interface addition can be detected using RTE_NEWLINK message type in NETLINK. Netlink sends a message with which we can get index and name of the interface using (if_indextoname & if_nametoindex).

我的问题是,如果我们添加一个VLAN接口,它会发送一个带有接口名称的信息(例如:eth1.10)。这个VLAN号码是否只存在于接口名称中? 还是在NL_MSG结构中的任何地方都有。我不希望解析 接口名称并获得VLAN号码

我执行了下面的代码,添加了一个vlan子接口,但无法从属性结构中找到vlan信息。即使RTM_NEWLINK检测到了新的接口,它也没有打印任何东西。如果我在代码中寻找VLAN信息的位置不对,请纠正我。

 if(nl_msg_hdr->nlmsg_type == RTM_NEWLINK)
            struct ifinfomsg *ifi;
            struct rtattr *rt_attr;
            int    rtattrlen;
            ifi = (struct ifinfomsg *) NLMSG_DATA(nl_msg_hdr);
            printf("RTM_NEWLINK");
    for (;RTA_OK(rt_attr, rtattrlen); rt_attr = RTA_NEXT(rt_attr, rtattrlen)) {
            if (rt_attr->rta_type == IFLA_LINKINFO)
                            printf(" IFLA_LINKINFO \n");
            if (rt_attr->rta_type == IFLA_LINK)
                            printf(" IFLA_LINK \n");
            if (rt_attr->rta_type == IFLA_INFO_DATA)
                            printf(" IFLA_INFO_DATA\n");
            if (rt_attr->rta_type == IFLA_VLAN_ID)
                            printf(" IFLA_VLAN_ID\n");
x/200bx nl_msg_hdr - 200 bytes hex-dump (VLAN ID is 33 in my case)
0x7fffffffd250: 0xa4    0x04    0x00    0x00    0x10    0x00    0x00    0x00
0x7fffffffd258: 0x00    0x00    0x00    0x00    0x00    0x00    0x00    0x00
0x7fffffffd260: 0x00    0x00    0x01    0x00    0x27    0x00    0x00    0x00
0x7fffffffd268: 0x02    0x10    0x00    0x00    0xff    0xff    0xff    0xff
0x7fffffffd270: 0x0d    0x00    0x03    0x00    0x76    0x65    0x74    0x68
0x7fffffffd278: 0x31    0x2e    0x33    0x33    0x00    0x00    0x00    0x00
0x7fffffffd280: 0x08    0x00    0x0d    0x00    0x00    0x00    0x00    0x00
0x7fffffffd288: 0x05    0x00    0x10    0x00    0x02    0x00    0x00    0x00
0x7fffffffd290: 0x05    0x00    0x11    0x00    0x00    0x00    0x00    0x00
0x7fffffffd298: 0x08    0x00    0x04    0x00    0xdc    0x05    0x00    0x00
0x7fffffffd2a0: 0x08    0x00    0x1b    0x00    0x00    0x00    0x00    0x00
0x7fffffffd2a8: 0x08    0x00    0x1e    0x00    0x00    0x00    0x00    0x00
0x7fffffffd2b0: 0x08    0x00    0x1f    0x00    0x01    0x00    0x00    0x00
0x7fffffffd2b8: 0x08    0x00    0x20    0x00    0x01    0x00    0x00    0x00
0x7fffffffd2c0: 0x08    0x00    0x05    0x00    0x0a    0x00    0x00    0x00
0x7fffffffd2c8: 0x05    0x00    0x21    0x00    0x01    0x00    0x00    0x00
0x7fffffffd2d0: 0x09    0x00    0x06    0x00    0x6e    0x6f    0x6f    0x70
0x7fffffffd2d8: 0x00    0x00    0x00    0x00    0x24    0x00    0x0e    0x00
0x7fffffffd2e0: 0x00    0x00    0x00    0x00    0x00    0x00    0x00    0x00
0x7fffffffd2e8: 0x00    0x00    0x00    0x00    0x00    0x00    0x00    0x00
0x7fffffffd2f0: 0x00    0x00    0x00    0x00    0x00    0x00    0x00    0x00
0x7fffffffd2f8: 0x00    0x00    0x00    0x00    0x00    0x88    0xff    0xff
0x7fffffffd300: 0x0a    0x00    0x01    0x00    0x66    0x0f    0x70    0x37
0x7fffffffd308: 0x0b    0x27    0x00    0x00    0x0a    0x00    0x02    0x00
0x7fffffffd310: 0xff    0xff    0xff    0xff    0xff    0xff    0x00    0x00
    
linux
netlink
user1762571
user1762571
发布于 2015-06-23
1 个回答
svinota
svinota
发布于 2015-06-23
已采纳
0 人赞同

vlan信息在RTM_NEWLINK消息中可用。ifla_linkinfo / ifla_info_data / ifla_vlan_id。

# the message structure:
[ nlmsg fields ]
[ ifinfmsg fields ]
nla chain:
  [ IFLA_IFNAME ]
  [ IFLA_… ]
  [ IFLA_LINKINFO ]
  nla chain:
    [ IFLA_INFO_KIND ]
    [ IFLA_INFO_DATA ]
    nla chain:
      [ IFLA_VLAN_ID ]

以及相应的代码(Python样本中的pyroute2):

from pyroute2 import IPRoute
ip = IPRoute()
# assume `ifindex` contains VLAN interface index
nlmsg = ip.get_links(ifindex)[0]
vid = nlmsg.get_attr('IFLA_LINKINFO').\
    get_attr('IFLA_INFO_DATA').\
    get_attr('IFLA_VLAN_ID')
print(vid)