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

     1  /*
     2   *  Copyright (C) 2018 Authors of Cilium
     3   *
     4   *  This program is free software; you can redistribute it and/or modify
     5   *  it under the terms of the GNU General Public License as published by
     6   *  the Free Software Foundation; either version 2 of the License, or
     7   *  (at your option) any later version.
     8   *
     9   *  This program is distributed in the hope that it will be useful,
    10   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   *  GNU General Public License for more details.
    13   *
    14   *  You should have received a copy of the GNU General Public License
    15   *  along with this program; if not, write to the Free Software
    16   *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    17   */
    18  /*
    19   * Data metrics collection functions
    20   *
    21   */
    22  
    23  #ifndef __LIB_METRICS__
    24  #define __LIB_METRICS__
    25  
    26  
    27  #include "common.h"
    28  #include "utils.h"
    29  #include "maps.h"
    30  #include "dbg.h"
    31  #include <stdint.h>
    32  #include <stdbool.h>
    33  
    34  
    35  /**
    36   * update_metrics
    37   * @direction:	1: Ingress 2: Egress
    38   * @reason:	reason for forwarding or dropping packet.
    39              	reason is 0 if packet is being forwarded, else reason
    40              	is the drop error code.
    41   * Update the metrics map.
    42   */
    43  static inline void update_metrics(__u32 bytes, __u8 direction, __u8 reason)
    44  {
    45      struct metrics_value *entry, newEntry = {};
    46      struct metrics_key key = {};
    47  
    48      key.reason = reason;
    49      key.dir    = direction;
    50  
    51  
    52      if ((entry = map_lookup_elem(&METRICS_MAP, &key))) {
    53              entry->count += 1;
    54              entry->bytes += (__u64)bytes;
    55      } else {
    56              newEntry.count = 1;
    57              newEntry.bytes = (__u64)bytes;
    58              map_update_elem(&METRICS_MAP, &key, &newEntry, 0);
    59      }
    60  }
    61  
    62  #endif /* __LIB_METRICS__ */