Netlink Sockets

netlinksocket接口作为IOCTL通信方法的更灵活替代,允许双向通信并配置系统设置。它通过socket API在用户空间打开并注册,与内核的netlink套接字进行交互,简化了网络管理和配置过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The netlink socket interface( AF_NETLINK socket ) was created as a more flexible alternative to the awkward IOCTL communication method between userspace processes and the kernel. The IOCTL handlers cannot send asynchronous messages to userspace from the kernel, whereas netlink sockets can. In order to use IOCTL, there is another level of complexity: you need to define IOCTL numbers. The operation model of netlink is quite simple: you open and register a netlink socket in userspace using the socket API, and this netlink socket handles bidirectional communication with a kernel netlink socket, usually sending messages to configure various system settings and getting responses back from the kernel.

There are two userspace packages for controlling TCP/IP networking and handling network devices: net-tools and iproute2. The iproute2 package includes commands like the following:

  1. ip: For management of network tables and network interfaces
  2. tc: For traffic control management
  3. ss: For dumping socket statistics
  4. lnstat: For dumping linux network statistics
  5. bridge: For management of bridge addresses and devices

The iproute2 package is based mostly on sending requests to the kernel from userspace and getting replies back over netlink sockets. There are a few exceptions where IOCTLs are used in iproute2. For example, the ip tuntap command uses IOCTLs to add/remove a TUN/TAP device. If you look at the TUN/TAP software driver code, you’ll find that it defines some IOCTL handlers, but it does not use the rtnetlink sockets.

The net-tools package is based on IOCTLs and includes known commands like these:

  1. ifconifg
  2. arp
  3. route
  4. netstat
  5. hostname
  6. rarp

One of the drawbacks of the netlink protocol is that the number of protocol families is limited to 32 (MAX_LINKS). This is one of the main reasons that the generic netlink family was created—to provide support for adding a higher number of families. It acts as a netlink multiplexer and works with a single netlink family (NETLINK_GENERIC). The generic netlink protocol is based on the netlink protocol and uses its API.

/ # lldpd -dddd -I eth0 1970-01-01T00:31:54 [ DBG/main] lldpd 2024-08-10 starting... 1970-01-01T00:31:54 [ DBG/main] creating control socket 1970-01-01T00:31:54 [ DBG/control] create control socket /var/run/lldpd.socket 1970-01-01T00:31:54 [ DBG/control] listen to control socket /var/run/lldpd.socket 1970-01-01T00:31:54 [ DBG/main] invoking lldpcli for default configuration locations 1970-01-01T00:31:54 [ DBG/main] invoke /sbin/lldpcli -sddd 1970-01-01T00:31:54 [ DBG/main] get OS/LSB release information 1970-01-01T00:31:54 [ DBG/localchassis] grab OS release 1970-01-01T00:31:54 [ DBG/main] initialize privilege separation 1970-01-01T00:31:54 [INFO/privsep] no libcap support, running monitor as root 1970-01-01T00:31:54 [ DBG/privsep] dropping privileges 1970-01-01T00:31:54 [ DBG/privsep] received command 0 1970-01-01T00:31:54 [ DBG/main] invoke /sbin/lldpcli -sddd 1970-01-01T00:31:54 [ DBG/privsep] monitor ready 1970-01-01T00:31:54 [ DBG/main] get an ioctl socket 1970-01-01T00:31:54 [ DBG/main] set system capabilities 1970-01-01T00:31:54 [ DBG/main] initialize protocols 1970-01-01T00:31:54 [INFO/main] protocol LLDP enabled 1970-01-01T00:31:54 [ DBG/main] start main loop 1970-01-01T00:31:54 [ DBG/event] initialize libevent 1970-01-01T00:31:54 [INFO/event] libevent 2.1.12-stable initialized with epoll method 1970-01-01T00:31:54 [ DBG/event] register loop timer 1970-01-01T00:31:54 [ DBG/event] register Unix socket 1970-01-01T00:31:54 [ DBG/event] monitor the monitor process 1970-01-01T00:31:54 [ DBG/event] register signals 1970-01-01T00:31:54 [ DBG/loop] start new loop 1970-01-01T00:31:54 [ DBG/loop] update information for local ports 1970-01-01T00:31:54 [ DBG/localchassis] update information for local ports 1970-01-01T00:31:54 [ DBG/netlink] initialize netlink subsystem 1970-01-01T00:31:54 [ DBG/netlink] listening on interface changes 1970-01-01T00:31:54 [ DBG/netlink] opening netlink sockets 1... 1970-01-01T00:31:54 [ DBG/netlink] opening netlink sockets 2... 1970-01-01T00:31:54 [ DBG/lldpctl] cannot find configuration file/directory /etc/lldpd.conf 1970-01-01T00:31:54 [ DBG/lldpctl] cannot find configuration file/directory /etc/lldpd.d 1970-01-01T00:31:54 [ DBG/lldpctl] connect to lldpd / # ifconfig eth0 Link encap:Ethernet HWaddr 82:47:5A:7D:B1:30 inet addr:192.168.1.107 Bcast:192.168.1.255 Mask:255.255.255.0 UP BROADCAST RUNNING ALLMULTI MULTICAST MTU:1500 Metric:1 RX packets:4712 errors:0 dropped:1419 overruns:0 frame:0 TX packets:31833 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:751176 (733.5 KiB) TX bytes:4419818 (4.2 MiB) Interrupt:27 Base address:0x2000 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:36065 errors:0 dropped:0 overruns:0 frame:0 TX packets:36065 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1 RX bytes:2032278 (1.9 MiB) TX bytes:2032278 (1.9 MiB) / # 为什么没有LLDP enabled on eth0打印,没有[ DBG/netlink] interface eth0 up, index 2, address [MAC]打印
最新发布
07-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值