Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  *
0004  *      SNMP MIB entries for the IP subsystem.
0005  *      
0006  *      Alan Cox <gw4pts@gw4pts.ampr.org>
0007  *
0008  *      We don't chose to implement SNMP in the kernel (this would
0009  *      be silly as SNMP is a pain in the backside in places). We do
0010  *      however need to collect the MIB statistics and export them
0011  *      out of /proc (eventually)
0012  */
0013  
0014 #ifndef _SNMP_H
0015 #define _SNMP_H
0016 
0017 #include <linux/cache.h>
0018 #include <linux/snmp.h>
0019 #include <linux/smp.h>
0020 
0021 /*
0022  * Mibs are stored in array of unsigned long.
0023  */
0024 /*
0025  * struct snmp_mib{}
0026  *  - list of entries for particular API (such as /proc/net/snmp)
0027  *  - name of entries.
0028  */
0029 struct snmp_mib {
0030     const char *name;
0031     int entry;
0032 };
0033 
0034 #define SNMP_MIB_ITEM(_name,_entry) {   \
0035     .name = _name,              \
0036     .entry = _entry,            \
0037 }
0038 
0039 #define SNMP_MIB_SENTINEL { \
0040     .name = NULL,       \
0041     .entry = 0,     \
0042 }
0043 
0044 /*
0045  * We use unsigned longs for most mibs but u64 for ipstats.
0046  */
0047 #include <linux/u64_stats_sync.h>
0048 
0049 /* IPstats */
0050 #define IPSTATS_MIB_MAX __IPSTATS_MIB_MAX
0051 struct ipstats_mib {
0052     /* mibs[] must be first field of struct ipstats_mib */
0053     u64     mibs[IPSTATS_MIB_MAX];
0054     struct u64_stats_sync syncp;
0055 };
0056 
0057 /* ICMP */
0058 #define ICMP_MIB_MAX    __ICMP_MIB_MAX
0059 struct icmp_mib {
0060     unsigned long   mibs[ICMP_MIB_MAX];
0061 };
0062 
0063 #define ICMPMSG_MIB_MAX __ICMPMSG_MIB_MAX
0064 struct icmpmsg_mib {
0065     atomic_long_t   mibs[ICMPMSG_MIB_MAX];
0066 };
0067 
0068 /* ICMP6 (IPv6-ICMP) */
0069 #define ICMP6_MIB_MAX   __ICMP6_MIB_MAX
0070 /* per network ns counters */
0071 struct icmpv6_mib {
0072     unsigned long   mibs[ICMP6_MIB_MAX];
0073 };
0074 /* per device counters, (shared on all cpus) */
0075 struct icmpv6_mib_device {
0076     atomic_long_t   mibs[ICMP6_MIB_MAX];
0077 };
0078 
0079 #define ICMP6MSG_MIB_MAX  __ICMP6MSG_MIB_MAX
0080 /* per network ns counters */
0081 struct icmpv6msg_mib {
0082     atomic_long_t   mibs[ICMP6MSG_MIB_MAX];
0083 };
0084 /* per device counters, (shared on all cpus) */
0085 struct icmpv6msg_mib_device {
0086     atomic_long_t   mibs[ICMP6MSG_MIB_MAX];
0087 };
0088 
0089 
0090 /* TCP */
0091 #define TCP_MIB_MAX __TCP_MIB_MAX
0092 struct tcp_mib {
0093     unsigned long   mibs[TCP_MIB_MAX];
0094 };
0095 
0096 /* UDP */
0097 #define UDP_MIB_MAX __UDP_MIB_MAX
0098 struct udp_mib {
0099     unsigned long   mibs[UDP_MIB_MAX];
0100 };
0101 
0102 /* Linux */
0103 #define LINUX_MIB_MAX   __LINUX_MIB_MAX
0104 struct linux_mib {
0105     unsigned long   mibs[LINUX_MIB_MAX];
0106 };
0107 
0108 /* Linux Xfrm */
0109 #define LINUX_MIB_XFRMMAX   __LINUX_MIB_XFRMMAX
0110 struct linux_xfrm_mib {
0111     unsigned long   mibs[LINUX_MIB_XFRMMAX];
0112 };
0113 
0114 /* Linux TLS */
0115 #define LINUX_MIB_TLSMAX    __LINUX_MIB_TLSMAX
0116 struct linux_tls_mib {
0117     unsigned long   mibs[LINUX_MIB_TLSMAX];
0118 };
0119 
0120 #define DEFINE_SNMP_STAT(type, name)    \
0121     __typeof__(type) __percpu *name
0122 #define DEFINE_SNMP_STAT_ATOMIC(type, name) \
0123     __typeof__(type) *name
0124 #define DECLARE_SNMP_STAT(type, name)   \
0125     extern __typeof__(type) __percpu *name
0126 
0127 #define __SNMP_INC_STATS(mib, field)    \
0128             __this_cpu_inc(mib->mibs[field])
0129 
0130 #define SNMP_INC_STATS_ATOMIC_LONG(mib, field)  \
0131             atomic_long_inc(&mib->mibs[field])
0132 
0133 #define SNMP_INC_STATS(mib, field)  \
0134             this_cpu_inc(mib->mibs[field])
0135 
0136 #define SNMP_DEC_STATS(mib, field)  \
0137             this_cpu_dec(mib->mibs[field])
0138 
0139 #define __SNMP_ADD_STATS(mib, field, addend)    \
0140             __this_cpu_add(mib->mibs[field], addend)
0141 
0142 #define SNMP_ADD_STATS(mib, field, addend)  \
0143             this_cpu_add(mib->mibs[field], addend)
0144 #define SNMP_UPD_PO_STATS(mib, basefield, addend)   \
0145     do { \
0146         __typeof__((mib->mibs) + 0) ptr = mib->mibs;    \
0147         this_cpu_inc(ptr[basefield##PKTS]);     \
0148         this_cpu_add(ptr[basefield##OCTETS], addend);   \
0149     } while (0)
0150 #define __SNMP_UPD_PO_STATS(mib, basefield, addend) \
0151     do { \
0152         __typeof__((mib->mibs) + 0) ptr = mib->mibs;    \
0153         __this_cpu_inc(ptr[basefield##PKTS]);       \
0154         __this_cpu_add(ptr[basefield##OCTETS], addend); \
0155     } while (0)
0156 
0157 
0158 #if BITS_PER_LONG==32
0159 
0160 #define __SNMP_ADD_STATS64(mib, field, addend)              \
0161     do {                                \
0162         __typeof__(*mib) *ptr = raw_cpu_ptr(mib);       \
0163         u64_stats_update_begin(&ptr->syncp);            \
0164         ptr->mibs[field] += addend;             \
0165         u64_stats_update_end(&ptr->syncp);          \
0166     } while (0)
0167 
0168 #define SNMP_ADD_STATS64(mib, field, addend)                \
0169     do {                                \
0170         local_bh_disable();                 \
0171         __SNMP_ADD_STATS64(mib, field, addend);         \
0172         local_bh_enable();              \
0173     } while (0)
0174 
0175 #define __SNMP_INC_STATS64(mib, field) SNMP_ADD_STATS64(mib, field, 1)
0176 #define SNMP_INC_STATS64(mib, field) SNMP_ADD_STATS64(mib, field, 1)
0177 #define __SNMP_UPD_PO_STATS64(mib, basefield, addend)           \
0178     do {                                \
0179         __typeof__(*mib) *ptr;              \
0180         ptr = raw_cpu_ptr((mib));               \
0181         u64_stats_update_begin(&ptr->syncp);            \
0182         ptr->mibs[basefield##PKTS]++;               \
0183         ptr->mibs[basefield##OCTETS] += addend;         \
0184         u64_stats_update_end(&ptr->syncp);          \
0185     } while (0)
0186 #define SNMP_UPD_PO_STATS64(mib, basefield, addend)         \
0187     do {                                \
0188         local_bh_disable();                 \
0189         __SNMP_UPD_PO_STATS64(mib, basefield, addend);      \
0190         local_bh_enable();              \
0191     } while (0)
0192 #else
0193 #define __SNMP_INC_STATS64(mib, field)      __SNMP_INC_STATS(mib, field)
0194 #define SNMP_INC_STATS64(mib, field)        SNMP_INC_STATS(mib, field)
0195 #define SNMP_DEC_STATS64(mib, field)        SNMP_DEC_STATS(mib, field)
0196 #define __SNMP_ADD_STATS64(mib, field, addend)  __SNMP_ADD_STATS(mib, field, addend)
0197 #define SNMP_ADD_STATS64(mib, field, addend)    SNMP_ADD_STATS(mib, field, addend)
0198 #define SNMP_UPD_PO_STATS64(mib, basefield, addend) SNMP_UPD_PO_STATS(mib, basefield, addend)
0199 #define __SNMP_UPD_PO_STATS64(mib, basefield, addend) __SNMP_UPD_PO_STATS(mib, basefield, addend)
0200 #endif
0201 
0202 #endif