← Documents Documentation/networking/gen_stats.rst GitHub 원문 ↗

Linux 6.18.37 · Networking

Generic networking statistics for netlink users

qdisc 통계를 수집해 netlink TLV로 내보내고 이전 tc_stats 형식과 호환하며 속도 추정기를 관리하는 API를 설명합니다.

Source pathDocumentation/networking/gen_stats.rst
Source versionLinux v6.18.37
TranslationDUJINLABS 전문 번역 + 해설

요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.

1. 요약·해설

원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.

요약·해설

gen_stats.rst:1-129

`gnet_stats` API는 통계 구조체, 일관된 잠금, 새 TLV 형식과 이전 형식의 동시 출력, EWMA 기반 속도 추정기 수명 주기를 하나의 공통 계층으로 제공합니다.

통계 수명 주기
dequeue()에서 갱신start_copy통계 TLV 복사finish_copy사용자 공간
TCA_RATEgen_new_estimator주기적 갱신gen_kill_estimator

수집부터 제거까지의 흐름입니다.

2. 영어 원문 전체

번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.

원문 전체 펼치기
1 .. SPDX-License-Identifier: GPL-2.0
2
3 ===============================================
4 Generic networking statistics for netlink users
5 ===============================================
6
7 Statistic counters are grouped into structs:
8
9 ==================== ===================== =====================
10 Struct TLV type Description
11 ==================== ===================== =====================
12 gnet_stats_basic TCA_STATS_BASIC Basic statistics
13 gnet_stats_rate_est TCA_STATS_RATE_EST Rate estimator
14 gnet_stats_queue TCA_STATS_QUEUE Queue statistics
15 none TCA_STATS_APP Application specific
16 ==================== ===================== =====================
17
18
19 Collecting:
20 -----------
21
22 Declare the statistic structs you need::
23
24 struct mystruct {
25 struct gnet_stats_basic bstats;
26 struct gnet_stats_queue qstats;
27 ...
28 };
29
30 Update statistics, in dequeue() methods only, (while owning qdisc->running)::
31
32 mystruct->tstats.packet++;
33 mystruct->qstats.backlog += skb->pkt_len;
34
35
36 Export to userspace (Dump):
37 ---------------------------
38
39 ::
40
41 my_dumping_routine(struct sk_buff *skb, ...)
42 {
43 struct gnet_dump dump;
44
45 if (gnet_stats_start_copy(skb, TCA_STATS2, &mystruct->lock, &dump,
46 TCA_PAD) < 0)
47 goto rtattr_failure;
48
49 if (gnet_stats_copy_basic(&dump, &mystruct->bstats) < 0 ||
50 gnet_stats_copy_queue(&dump, &mystruct->qstats) < 0 ||
51 gnet_stats_copy_app(&dump, &xstats, sizeof(xstats)) < 0)
52 goto rtattr_failure;
53
54 if (gnet_stats_finish_copy(&dump) < 0)
55 goto rtattr_failure;
56 ...
57 }
58
59 TCA_STATS/TCA_XSTATS backward compatibility:
60 --------------------------------------------
61
62 Prior users of struct tc_stats and xstats can maintain backward
63 compatibility by calling the compat wrappers to keep providing the
64 existing TLV types::
65
66 my_dumping_routine(struct sk_buff *skb, ...)
67 {
68 if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS,
69 TCA_XSTATS, &mystruct->lock, &dump,
70 TCA_PAD) < 0)
71 goto rtattr_failure;
72 ...
73 }
74
75 A struct tc_stats will be filled out during gnet_stats_copy_* calls
76 and appended to the skb. TCA_XSTATS is provided if gnet_stats_copy_app
77 was called.
78
79
80 Locking:
81 --------
82
83 Locks are taken before writing and released once all statistics have
84 been written. Locks are always released in case of an error. You
85 are responsible for making sure that the lock is initialized.
86
87
88 Rate Estimator:
89 ---------------
90
91 0) Prepare an estimator attribute. Most likely this would be in user
92 space. The value of this TLV should contain a tc_estimator structure.
93 As usual, such a TLV needs to be 32 bit aligned and therefore the
94 length needs to be appropriately set, etc. The estimator interval
95 and ewma log need to be converted to the appropriate values.
96 tc_estimator.c::tc_setup_estimator() is advisable to be used as the
97 conversion routine. It does a few clever things. It takes a time
98 interval in microsecs, a time constant also in microsecs and a struct
99 tc_estimator to be populated. The returned tc_estimator can be
100 transported to the kernel. Transfer such a structure in a TLV of type
101 TCA_RATE to your code in the kernel.
102
103 In the kernel when setting up:
104
105 1) make sure you have basic stats and rate stats setup first.
106 2) make sure you have initialized stats lock that is used to setup such
107 stats.
108 3) Now initialize a new estimator::
109
110 int ret = gen_new_estimator(my_basicstats,my_rate_est_stats,
111 mystats_lock, attr_with_tcestimator_struct);
112
113 if ret == 0
114 success
115 else
116 failed
117
118 From now on, every time you dump my_rate_est_stats it will contain
119 up-to-date info.
120
121 Once you are done, call gen_kill_estimator(my_basicstats,
122 my_rate_est_stats) Make sure that my_basicstats and my_rate_est_stats
123 are still valid (i.e still exist) at the time of making this call.
124
125
126 Authors:
127 --------
128 - Thomas Graf <[email protected]>
129 - Jamal Hadi Salim <[email protected]>
130

3. 한국어 전문 번역

영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.

통계 그룹

1-18

일반 네트워킹 통계 카운터는 구조체별로 묶입니다. `gnet_stats_basic`은 `TCA_STATS_BASIC` 기본 통계, `gnet_stats_rate_est`는 `TCA_STATS_RATE_EST` 속도 추정치, `gnet_stats_queue`는 `TCA_STATS_QUEUE` 큐 통계로 내보냅니다. 애플리케이션 전용 데이터는 고정 구조체 없이 `TCA_STATS_APP` TLV를 사용합니다.

일반 통계 TLV
구조체TLV용도
gnet_stats_basicTCA_STATS_BASIC기본 패킷/바이트 통계
gnet_stats_rate_estTCA_STATS_RATE_EST속도 추정치
gnet_stats_queueTCA_STATS_QUEUE큐와 backlog 통계
없음TCA_STATS_APP애플리케이션 전용 통계

커널 구조체와 netlink TLV의 대응입니다.

.. SPDX-License-Identifier: GPL-2.0

===============================================
Generic networking statistics for netlink users
===============================================

Statistic counters are grouped into structs:

==================== ===================== =====================
Struct               TLV type              Description
==================== ===================== =====================
gnet_stats_basic     TCA_STATS_BASIC       Basic statistics
gnet_stats_rate_est  TCA_STATS_RATE_EST    Rate estimator
gnet_stats_queue     TCA_STATS_QUEUE       Queue statistics
none                 TCA_STATS_APP         Application specific
==================== ===================== =====================

통계 수집

19-35

필요한 통계 구조체를 qdisc 전용 구조 안에 선언합니다. 예제는 `gnet_stats_basic bstats`와 `gnet_stats_queue qstats`를 둡니다. 카운터 갱신은 `qdisc->running`을 소유한 상태의 `dequeue()` 메서드에서만 수행해야 하며, 패킷 수를 증가시키고 `skb->pkt_len`을 backlog에 더하는 방식입니다.

Collecting:
-----------

Declare the statistic structs you need::

        struct mystruct {
                struct gnet_stats_basic        bstats;
                struct gnet_stats_queue        qstats;
                ...
        };

Update statistics, in dequeue() methods only, (while owning qdisc->running)::

        mystruct->tstats.packet++;
        mystruct->qstats.backlog += skb->pkt_len;

사용자 공간으로 내보내기

36-58

dump 루틴은 `struct gnet_dump`를 만들고 `gnet_stats_start_copy()`로 `TCA_STATS2` 복사를 시작합니다. 이 호출에는 출력 `skb`, 잠금, dump 상태, 정렬용 `TCA_PAD`를 전달합니다. 실패하면 공통 `rtattr_failure` 경로로 이동합니다.

그다음 `gnet_stats_copy_basic()`, `gnet_stats_copy_queue()`, `gnet_stats_copy_app()`으로 필요한 통계를 순서대로 복사합니다. 하나라도 실패하면 중단하고, 모두 성공하면 `gnet_stats_finish_copy()`로 중첩 속성을 마무리합니다.

통계 dump 순서
gnet_stats_start_copy()copy_basic()copy_queue()copy_app()gnet_stats_finish_copy()
오류rtattr_failure잠금 해제

한 잠금 범위 안에서 통계 TLV를 조립합니다.

Export to userspace (Dump):
---------------------------

::

    my_dumping_routine(struct sk_buff *skb, ...)
    {
            struct gnet_dump dump;

            if (gnet_stats_start_copy(skb, TCA_STATS2, &mystruct->lock, &dump,
                                    TCA_PAD) < 0)
                    goto rtattr_failure;

            if (gnet_stats_copy_basic(&dump, &mystruct->bstats) < 0 ||
                gnet_stats_copy_queue(&dump, &mystruct->qstats) < 0 ||
                    gnet_stats_copy_app(&dump, &xstats, sizeof(xstats)) < 0)
                    goto rtattr_failure;

            if (gnet_stats_finish_copy(&dump) < 0)
                    goto rtattr_failure;
            ...
    }

TCA_STATS/TCA_XSTATS 호환성

59-79

기존 `struct tc_stats`와 확장 통계 사용자는 `gnet_stats_start_copy_compat()` 호환 래퍼로 `TCA_STATS2`와 함께 이전 `TCA_STATS`, `TCA_XSTATS` TLV도 계속 제공할 수 있습니다. 이후 `gnet_stats_copy_*` 호출 과정에서 `struct tc_stats`가 채워져 `skb`에 추가됩니다.

`gnet_stats_copy_app()`을 호출한 경우에만 `TCA_XSTATS`가 제공됩니다. 따라서 새 중첩 통계 형식으로 전환하면서도 오래된 사용자 공간 도구가 기대하는 형식을 유지할 수 있습니다.

TCA_STATS/TCA_XSTATS backward compatibility:
--------------------------------------------

Prior users of struct tc_stats and xstats can maintain backward
compatibility by calling the compat wrappers to keep providing the
existing TLV types::

    my_dumping_routine(struct sk_buff *skb, ...)
    {
        if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS,
                                        TCA_XSTATS, &mystruct->lock, &dump,
                                        TCA_PAD) < 0)
                    goto rtattr_failure;
            ...
    }

A struct tc_stats will be filled out during gnet_stats_copy_* calls
and appended to the skb. TCA_XSTATS is provided if gnet_stats_copy_app
was called.

잠금

80-87

통계 쓰기를 시작하기 전에 잠금을 획득하고 모든 통계를 기록한 뒤 해제합니다. 중간 오류가 발생해도 잠금은 항상 해제됩니다. 다만 잠금 객체 자체를 올바르게 초기화하는 책임은 호출자에게 있습니다.

Locking:
--------

Locks are taken before writing and released once all statistics have
been written. Locks are always released in case of an error. You
are responsible for making sure that the lock is initialized.

속도 추정기

88-125

먼저 보통 사용자 공간에서 `tc_estimator` 구조를 담은 추정기 TLV를 준비합니다. TLV는 32비트 정렬과 올바른 길이를 갖춰야 하며 interval과 EWMA log를 커널 형식으로 변환해야 합니다. `tc_estimator.c::tc_setup_estimator()`는 마이크로초 단위 측정 간격과 시간 상수를 받아 전송 가능한 `tc_estimator`를 채우므로 권장되는 변환 함수입니다. 완성한 구조체는 `TCA_RATE` TLV로 커널 코드에 전달합니다.

커널에서는 기본 통계와 속도 통계를 먼저 준비하고, 이 통계에 사용할 잠금을 초기화합니다. 이후 `gen_new_estimator(my_basicstats, my_rate_est_stats, mystats_lock, attr_with_tcestimator_struct)`를 호출합니다. 반환값 0은 성공이며, 이후 `my_rate_est_stats`를 dump할 때마다 최신 추정치가 들어 있습니다.

사용이 끝나면 `gen_kill_estimator(my_basicstats, my_rate_est_stats)`를 호출합니다. 제거 시점에도 두 통계 객체가 존재하고 유효해야 합니다.

Rate Estimator:
---------------

0) Prepare an estimator attribute. Most likely this would be in user
   space. The value of this TLV should contain a tc_estimator structure.
   As usual, such a TLV needs to be 32 bit aligned and therefore the
   length needs to be appropriately set, etc. The estimator interval
   and ewma log need to be converted to the appropriate values.
   tc_estimator.c::tc_setup_estimator() is advisable to be used as the
   conversion routine. It does a few clever things. It takes a time
   interval in microsecs, a time constant also in microsecs and a struct
   tc_estimator to  be populated. The returned tc_estimator can be
   transported to the kernel.  Transfer such a structure in a TLV of type
   TCA_RATE to your code in the kernel.

In the kernel when setting up:

1) make sure you have basic stats and rate stats setup first.
2) make sure you have initialized stats lock that is used to setup such
   stats.
3) Now initialize a new estimator::

    int ret = gen_new_estimator(my_basicstats,my_rate_est_stats,
        mystats_lock, attr_with_tcestimator_struct);

    if ret == 0
        success
    else
        failed

From now on, every time you dump my_rate_est_stats it will contain
up-to-date info.

Once you are done, call gen_kill_estimator(my_basicstats,
my_rate_est_stats) Make sure that my_basicstats and my_rate_est_stats
are still valid (i.e still exist) at the time of making this call.

작성자

126-129

이 문서는 Thomas Graf와 Jamal Hadi Salim이 작성했습니다.

Authors:
--------
- Thomas Graf <[email protected]>
- Jamal Hadi Salim <[email protected]>