요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.
1. 요약·해설
원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.
2. 영어 원문 전체
번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.
원문 전체 펼치기
.. SPDX-License-Identifier: BSD-3-Clause
.. _kernel_netlink:
===================================
Netlink notes for kernel developers
===================================
General guidance
================
Attribute enums
---------------
Older families often define "null" attributes and commands with value
of ``0`` and named ``unspec``. This is supported (``type: unused``)
but should be avoided in new families. The ``unspec`` enum values are
not used in practice, so just set the value of the first attribute to ``1``.
Message enums
-------------
Use the same command IDs for requests and replies. This makes it easier
to match them up, and we have plenty of ID space.
Use separate command IDs for notifications. This makes it easier to
sort the notifications from replies (and present them to the user
application via a different API than replies).
Answer requests
---------------
Older families do not reply to all of the commands, especially NEW / ADD
commands. User only gets information whether the operation succeeded or
not via the ACK. Try to find useful data to return. Once the command is
added whether it replies with a full message or only an ACK is uAPI and
cannot be changed. It's better to err on the side of replying.
Specifically NEW and ADD commands should reply with information identifying
the created object such as the allocated object's ID (without having to
resort to using ``NLM_F_ECHO``).
NLM_F_ECHO
----------
Make sure to pass the request info to genl_notify() to allow ``NLM_F_ECHO``
to take effect. This is useful for programs that need precise feedback
from the kernel (for example for logging purposes).
Support dump consistency
------------------------
If iterating over objects during dump may skip over objects or repeat
them - make sure to report dump inconsistency with ``NLM_F_DUMP_INTR``.
This is usually implemented by maintaining a generation id for the
structure and recording it in the ``seq`` member of struct netlink_callback.
Netlink specification
=====================
Documentation of the Netlink specification parts which are only relevant
to the kernel space.
Globals
-------
kernel-policy
~~~~~~~~~~~~~
Defines whether the kernel validation policy is ``global`` i.e. the same for all
operations of the family, defined for each operation individually - ``per-op``,
or separately for each operation and operation type (do vs dump) - ``split``.
New families should use ``per-op`` (default) to be able to narrow down the
attributes accepted by a specific command.
checks
------
Documentation for the ``checks`` sub-sections of attribute specs.
unterminated-ok
~~~~~~~~~~~~~~~
Accept strings without the null-termination (for legacy families only).
Switches from the ``NLA_NUL_STRING`` to ``NLA_STRING`` policy type.
max-len
~~~~~~~
Defines max length for a binary or string attribute (corresponding
to the ``len`` member of struct nla_policy). For string attributes terminating
null character is not counted towards ``max-len``.
The field may either be a literal integer value or a name of a defined
constant. String types may reduce the constant by one
(i.e. specify ``max-len: CONST - 1``) to reserve space for the terminating
character so implementations should recognize such pattern.
min-len
~~~~~~~
Similar to ``max-len`` but defines minimum length.
3. 한국어 전문 번역
영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.
Netlink attribute enum 지침
1-18SPDX license 식별자는 BSD-3-Clause입니다.
Kernel Netlink anchor는 `kernel_netlink`입니다.
Kernel developer를 위한 Netlink 참고 사항
일반 지침 (General guidance)
Attribute enums
오래된 family는 값이 `0`이고 이름이 `unspec`인 'null' attribute와 command를 정의하는 경우가 많습니다. 이 형태는 `type: unused`로 지원되지만 새 family에서는 피해야 합니다. `unspec` enum 값은 실제로 사용하지 않으므로 첫 attribute의 값을 바로 `1`로 설정하십시오.
Netlink message enum 지침
19-29Message enums
Request와 reply에는 같은 command ID를 사용하십시오. 서로 대응시키기 쉽고 ID 공간도 충분합니다.
Notification에는 별도의 command ID를 사용하십시오. 그러면 notification과 reply를 쉽게 구분하고 user application에 reply와 다른 API로 제공할 수 있습니다.
Request에 유용한 data로 응답
30-42Answer requests
오래된 family는 모든 command, 특히 `NEW` 또는 `ADD` command에 reply하지 않는 경우가 있습니다. 사용자는 ACK를 통해 operation의 성공 여부만 알게 됩니다. 반환할 유용한 data를 찾으십시오. Command가 추가된 뒤 full message로 reply할지 ACK만 보낼지는 uAPI가 되어 변경할 수 없습니다. 가능하면 reply하는 쪽을 선택하는 편이 낫습니다.
특히 `NEW`와 `ADD` command는 `NLM_F_ECHO`에 의존하지 않고도 할당된 object ID처럼 생성한 object를 식별할 정보를 reply해야 합니다.
NLM_F_ECHO 처리
43-49NLM_F_ECHO
`NLM_F_ECHO`가 효과를 내도록 request information을 `genl_notify()`에 전달해야 합니다. 이는 logging처럼 kernel로부터 정확한 feedback이 필요한 program에 유용합니다.
Dump consistency 지원
50-57Support dump consistency
Dump 중 object를 순회하면서 object를 건너뛰거나 반복할 수 있다면 `NLM_F_DUMP_INTR`로 dump inconsistency를 반드시 보고해야 합니다. 일반적으로 structure의 generation ID를 유지하고 `struct netlink_callback`의 `seq` member에 기록하여 구현합니다.
Netlink specification의 kernel policy
58-75Netlink specification
이 절은 kernel space에만 관련된 Netlink specification 부분을 설명합니다.
Globals
kernel-policy
`kernel-policy`는 kernel validation policy가 family의 모든 operation에 같은 `global`인지, operation마다 별도로 정의하는 `per-op`인지, operation과 operation type인 do 또는 dump마다 별도로 정의하는 `split`인지 지정합니다. 새 family는 특정 command가 받아들이는 attribute를 좁힐 수 있도록 기본값인 `per-op`을 사용해야 합니다.
Attribute check와 unterminated string
76-86checks
Attribute specification의 `checks` subsection에 관한 문서입니다.
unterminated-ok
Legacy family에 한해 null termination이 없는 string을 허용합니다. Policy type을 `NLA_NUL_STRING`에서 `NLA_STRING`으로 전환합니다.
Attribute 길이 constraint
87-102max-len
`max-len`은 binary 또는 string attribute의 최대 길이를 정의하며 `struct nla_policy`의 `len` member에 대응합니다. String attribute의 terminating null character는 `max-len`에 포함하지 않습니다.
Field에는 literal integer 값이나 정의된 constant 이름을 사용할 수 있습니다. String type은 terminating character의 공간을 예약하기 위해 constant에서 1을 뺀 `max-len: CONST - 1` 형태를 사용할 수 있으므로 implementation은 이 pattern을 인식해야 합니다.
min-len
`min-len`은 `max-len`과 비슷하지만 최소 길이를 정의합니다.
요약과 해설
netlink.rst:1-102새 Netlink family는 `unspec = 0` 관습을 피하고 첫 attribute를 1부터 시작하는 것이 좋습니다. Request와 reply는 같은 command ID를 사용하되 notification에는 별도 ID를 부여해야 합니다.
Command는 ACK만 보내기보다 생성 object ID 같은 유용한 data를 reply해야 합니다. `NLM_F_ECHO`를 지원하려면 request 정보를 `genl_notify()`에 전달하고, 일관되지 않을 수 있는 dump는 `NLM_F_DUMP_INTR`로 표시해야 합니다.
새 family의 validation policy는 command별 attribute 집합을 제한할 수 있는 `per-op`이 기본입니다. String termination과 `max-len` 또는 `min-len` constraint는 legacy compatibility와 terminating null 공간을 정확히 반영해야 합니다.