Linux/Linux

Netlink 정리

i5 2023. 1. 6. 00:05
반응형

약어정리
USA (Userspace Application)
KS (Kernelspace)

 


USA and KS exchange Netlink msgs in well defined format

Any Netlink msg going from USA to KS or form KS or USA must be as per "Netlink standard msg format"

The netlink messages must be formatted as per the netlink standard message format.
A typical Netlink Msg is laid out in Memory as below:

이 다이그램은 전형적인 Netlink 메시지를 보여준다. 페이로드 메시지는 헤더에 Appending 될 것이다.


그러면 netlink message header는 어떻게 구성되어있을까?
심플하다.

struct nlmsghdr {
	u32 nlmsg_len;  /*Total length of msg = nlhdr + padding + payload */
    u16 nlmsg_type;
    u16 nlmsg_flags;
    u32 nlmsg_seq;
    u32 nlmsg_pid;
 };

The first filed of this netlink message header is the total length of this netlink message including the payload, the padding and the netlink message header itself.

including the payload, 헤더 길이 즉, 진짜 모든 길이이다.

 

나머지 필드에 대해서는 차차 알아보도록 하자.

 

 

Both parties, can exchange Multiple Netlink msg units cascaded one after the other. (cascade 종속)

 

첫번째 Netlink 메시지의 길이는 (1)이 될 것이며,

두번쨰 Netlink 메시지의 길이는 당연히 (2)가 될 것이다.

 

 

In order to deal with the netlink in the kernel space, all you need to understand is this header file.

 

nlmsg_type

4 standard types are defined in /usr/include/linux/netlink.h

1. NLMSG_NOOP

2. NLMSG_ERROR

3. NLMSG_DONE

4. NLMSG_OVERRUN

 

 

1. NLMSG_NOOP: When the otehr party receives thie msg, it does nothing except it replies with NLMSG_DONE telling the sender that all is fine ( = Is all ok?)  (No operation)

 여기서 other party가 의미하는건 Kernel space이거나 다른 User space를 말한다. 의미적으론, 이 Netlink 메시지 타입은 실질적으로 Netlink Ping을 의미하는 것과 같다. 그 이유는 하나의 party에서 다른 party로 alive한지 아닌지를 확인하는 메시지이기 때문이다.

when our user space application will send a netlink message with Natalie as type equal to knuckling
message and or operation, our Linux kernel module, which will be acting as a kernel routing table
manager, will reply back to the user space applications, saying that I'm fine right now.

 

 

2. NLMSG_ERROR: When the party receives this msg as a reply to the msg sent previously, it means that other party failed to perform requested action ( = negative feedback)

 

3. NLMSG_DONE: This is the last Netlink msg in the cascade of multiple Netlink msg units.

Remeber that Netlink message communication, there could be mutliple Netlink messages, which could be cascaded one after another. so, the last netlink message, which is present in that cascading list that last Netlink mesagge will have the netlink message type equal to NLMSG_DONE.

 

that this particular netlink message is the last netlink message present in this gene of netlink messages,

 

4. NLMSG_OVERRUN: Currelty not used in linux kernel anywhere

Note: Besides above, User can define his own msg types which should be >= 16

 

언제든지 이 4가지타입은 사용할 수 있다. 왜냐면 표준이라서.

 

 

we will have to define more number of netlink methods types, but these four netlink method types are actually very generic and is not tied to the problem statement that we were going to solve.

 

So to define your own netlink massage as types, be careful that you define aid greater than equal to 16.

 

아래의 사이트의 이 부분을 보자.

https://sites.uclouvain.be/SystInfo/usr/include/linux/netlink.h.html

 

#define NLMSG_NOOP                0x1        /* Nothing.                */
#define NLMSG_ERROR                0x2        /* Error                */
#define NLMSG_DONE                0x3        /* End of a dump        */
#define NLMSG_OVERRUN                0x4        /* Data lost                */

#define NLMSG_MIN_TYPE                0x10        /* < 0x10: reserved control messages */

 

반응형