Introduction
Inter Process Communication (IPC)
- Development in kernel is complex.
- Only core functionalities of operating system and performance critical codes are in kernel.
- User space applications and kernel space code.
- Through IPC user space application can communicate with kernel space program.Two kernel module can communicate with each other.
- Widely used and famous IPCs :
system call,IOCTL,proc filesystem,Netlink socket
IOCTL vs Netlink
-
User space application set/get information to/from kernel using IOTCL.
ifconfig、arp、 route 内部都是使用ioctl , ioctl对于每个操作都必须有一个唯一的ioctl编号。
-
ioctl cannot send asynchronous message from kernel to user space.
-
Non-trivial task to add IOCTL for new feature.
新添加ioctl 并非易事,因为需要同时修改 user space和kernel
-
Adding a protocol type in netlink.h is sufficient(足够) to support new type of Netlink socket. Socket APIs are compatible with BSD socket API. Thus easy to implement in user space application.
-
Netlink is asynchronous
-
Netlink socket supports multicast.
Netlink
Basic Functionalities
- Special IPC for transferring information between kernel and user space processes.
- Two kernel processes can communication with each other through netlink.
- Full duplex communication
- Use standard socket APIs in user space process.
- Support multiple protocol types. User can define his own type in
netlink.h- NETLINK_ROUTE
- NETLINK_FIREWALL
- NETLINK_ARPD
User space packages
iproute2usesNetlinkip: For management of network tables and network interfacestc: For traffic control managementbridge: For management of bridge addresses and devices
net-toolsstill usesIOCTL- ifconfig
- arp
- route
- netstat
API
socket
int socket(int domain, int type, int protocol)- socket domain (address family):
AF_NETLINK - type: SOCK_RAW or SOCK_DGRAM
- protocol: NETLINK_ROUTE, NETLINK_FIREWALL, NETLINK_ARPD
- socket domain (address family):
bind
- the netlink bind() API associates a local (source) socket address with the opened socket
|
|
-
nl _pid: filled with the calling process’ own pid -
nl_groups: 0 for unicast -
bind(fd, (struct sockaddr*)&nladdr, sizeof(nladdr)):
netlink demo
linux-learn-demo-code/netlink at main · charles-7777/linux-learn-demo-code
route show demo
|
|
route monitor demo
|
|
|
|
NETLINK_USER demo
|
|
|
|