github.com/cilium/cilium@v1.16.2/bpf/lib/metrics.h (about)

     1  /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
     2  /* Copyright Authors of Cilium */
     3  
     4  /*
     5   * Data metrics collection functions
     6   *
     7   */
     8  #pragma once
     9  
    10  #include "common.h"
    11  #include "utils.h"
    12  #include "maps.h"
    13  #include "dbg.h"
    14  
    15  /**
    16   * update_metrics
    17   * @direction:	1: Ingress 2: Egress
    18   * @reason:	reason for forwarding or dropping packet.
    19   *		reason is 0 if packet is being forwarded, else reason
    20   *		is the drop error code.
    21   * Update the metrics map.
    22   */
    23  #define update_metrics(bytes, direction, reason) \
    24  		_update_metrics(bytes, direction, reason, __MAGIC_LINE__, __MAGIC_FILE__)
    25  static __always_inline void _update_metrics(__u64 bytes, __u8 direction,
    26  					    __u8 reason, __u16 line, __u8 file)
    27  {
    28  	struct metrics_value *entry, new_entry = {};
    29  	struct metrics_key key = {};
    30  
    31  	key.reason = reason;
    32  	key.dir    = direction;
    33  	key.line   = line;
    34  	key.file   = file;
    35  
    36  	entry = map_lookup_elem(&METRICS_MAP, &key);
    37  	if (entry) {
    38  		entry->count += 1;
    39  		entry->bytes += bytes;
    40  	} else {
    41  		new_entry.count = 1;
    42  		new_entry.bytes = bytes;
    43  		map_update_elem(&METRICS_MAP, &key, &new_entry, 0);
    44  	}
    45  }
    46  
    47  /**
    48   * ct_to_metrics_dir
    49   * @direction:	1: Ingress 2: Egress 3: Service
    50   * Convert a CT direction into the corresponding one for metrics.
    51   */
    52  static __always_inline enum metric_dir ct_to_metrics_dir(enum ct_dir ct_dir)
    53  {
    54  	switch (ct_dir) {
    55  	case CT_INGRESS:
    56  		return METRIC_INGRESS;
    57  	case CT_EGRESS:
    58  		return METRIC_EGRESS;
    59  	case CT_SERVICE:
    60  		return METRIC_SERVICE;
    61  	default:
    62  		return 0;
    63  	}
    64  }