Linux Audio

Check our new training course

Loading...
v3.1
 
  1/*
  2 *
  3 *		SNMP MIB entries for the IP subsystem.
  4 *		
  5 *		Alan Cox <gw4pts@gw4pts.ampr.org>
  6 *
  7 *		We don't chose to implement SNMP in the kernel (this would
  8 *		be silly as SNMP is a pain in the backside in places). We do
  9 *		however need to collect the MIB statistics and export them
 10 *		out of /proc (eventually)
 11 *
 12 *		This program is free software; you can redistribute it and/or
 13 *		modify it under the terms of the GNU General Public License
 14 *		as published by the Free Software Foundation; either version
 15 *		2 of the License, or (at your option) any later version.
 16 *
 17 */
 18 
 19#ifndef _SNMP_H
 20#define _SNMP_H
 21
 22#include <linux/cache.h>
 23#include <linux/snmp.h>
 24#include <linux/smp.h>
 25
 26/*
 27 * Mibs are stored in array of unsigned long.
 28 */
 29/*
 30 * struct snmp_mib{}
 31 *  - list of entries for particular API (such as /proc/net/snmp)
 32 *  - name of entries.
 33 */
 34struct snmp_mib {
 35	const char *name;
 36	int entry;
 37};
 38
 39#define SNMP_MIB_ITEM(_name,_entry)	{	\
 40	.name = _name,				\
 41	.entry = _entry,			\
 42}
 43
 44#define SNMP_MIB_SENTINEL {	\
 45	.name = NULL,		\
 46	.entry = 0,		\
 47}
 48
 49/*
 50 * We use unsigned longs for most mibs but u64 for ipstats.
 51 */
 52#include <linux/u64_stats_sync.h>
 53
 54/* IPstats */
 55#define IPSTATS_MIB_MAX	__IPSTATS_MIB_MAX
 56struct ipstats_mib {
 57	/* mibs[] must be first field of struct ipstats_mib */
 58	u64		mibs[IPSTATS_MIB_MAX];
 59	struct u64_stats_sync syncp;
 60};
 61
 62/* ICMP */
 63#define ICMP_MIB_MAX	__ICMP_MIB_MAX
 64struct icmp_mib {
 65	unsigned long	mibs[ICMP_MIB_MAX];
 66};
 67
 68#define ICMPMSG_MIB_MAX	__ICMPMSG_MIB_MAX
 69struct icmpmsg_mib {
 70	unsigned long	mibs[ICMPMSG_MIB_MAX];
 71};
 72
 73/* ICMP6 (IPv6-ICMP) */
 74#define ICMP6_MIB_MAX	__ICMP6_MIB_MAX
 75/* per network ns counters */
 76struct icmpv6_mib {
 77	unsigned long	mibs[ICMP6_MIB_MAX];
 78};
 79/* per device counters, (shared on all cpus) */
 80struct icmpv6_mib_device {
 81	atomic_long_t	mibs[ICMP6_MIB_MAX];
 82};
 83
 84#define ICMP6MSG_MIB_MAX  __ICMP6MSG_MIB_MAX
 85/* per network ns counters */
 86struct icmpv6msg_mib {
 87	unsigned long	mibs[ICMP6MSG_MIB_MAX];
 88};
 89/* per device counters, (shared on all cpus) */
 90struct icmpv6msg_mib_device {
 91	atomic_long_t	mibs[ICMP6MSG_MIB_MAX];
 92};
 93
 94
 95/* TCP */
 96#define TCP_MIB_MAX	__TCP_MIB_MAX
 97struct tcp_mib {
 98	unsigned long	mibs[TCP_MIB_MAX];
 99};
100
101/* UDP */
102#define UDP_MIB_MAX	__UDP_MIB_MAX
103struct udp_mib {
104	unsigned long	mibs[UDP_MIB_MAX];
105};
106
107/* Linux */
108#define LINUX_MIB_MAX	__LINUX_MIB_MAX
109struct linux_mib {
110	unsigned long	mibs[LINUX_MIB_MAX];
111};
112
113/* Linux Xfrm */
114#define LINUX_MIB_XFRMMAX	__LINUX_MIB_XFRMMAX
115struct linux_xfrm_mib {
116	unsigned long	mibs[LINUX_MIB_XFRMMAX];
117};
118
119#define SNMP_ARRAY_SZ 1
 
 
 
 
120
121#define DEFINE_SNMP_STAT(type, name)	\
122	__typeof__(type) __percpu *name[SNMP_ARRAY_SZ]
123#define DEFINE_SNMP_STAT_ATOMIC(type, name)	\
124	__typeof__(type) *name
125#define DECLARE_SNMP_STAT(type, name)	\
126	extern __typeof__(type) __percpu *name[SNMP_ARRAY_SZ]
127
128#define SNMP_INC_STATS_BH(mib, field)	\
129			__this_cpu_inc(mib[0]->mibs[field])
130
131#define SNMP_INC_STATS_USER(mib, field)	\
132			irqsafe_cpu_inc(mib[0]->mibs[field])
133
134#define SNMP_INC_STATS_ATOMIC_LONG(mib, field)	\
135			atomic_long_inc(&mib->mibs[field])
136
137#define SNMP_INC_STATS(mib, field)	\
138			irqsafe_cpu_inc(mib[0]->mibs[field])
139
140#define SNMP_DEC_STATS(mib, field)	\
141			irqsafe_cpu_dec(mib[0]->mibs[field])
142
143#define SNMP_ADD_STATS_BH(mib, field, addend)	\
144			__this_cpu_add(mib[0]->mibs[field], addend)
145
146#define SNMP_ADD_STATS_USER(mib, field, addend)	\
147			irqsafe_cpu_add(mib[0]->mibs[field], addend)
148
149#define SNMP_ADD_STATS(mib, field, addend)	\
150			irqsafe_cpu_add(mib[0]->mibs[field], addend)
151/*
152 * Use "__typeof__(*mib[0]) *ptr" instead of "__typeof__(mib[0]) ptr"
153 * to make @ptr a non-percpu pointer.
154 */
155#define SNMP_UPD_PO_STATS(mib, basefield, addend)	\
156	do { \
157		irqsafe_cpu_inc(mib[0]->mibs[basefield##PKTS]);		\
158		irqsafe_cpu_add(mib[0]->mibs[basefield##OCTETS], addend);	\
 
159	} while (0)
160#define SNMP_UPD_PO_STATS_BH(mib, basefield, addend)	\
161	do { \
162		__this_cpu_inc(mib[0]->mibs[basefield##PKTS]);		\
163		__this_cpu_add(mib[0]->mibs[basefield##OCTETS], addend);	\
 
164	} while (0)
165
166
167#if BITS_PER_LONG==32
168
169#define SNMP_ADD_STATS64_BH(mib, field, addend) 			\
170	do {								\
171		__typeof__(*mib[0]) *ptr = __this_cpu_ptr((mib)[0]);	\
172		u64_stats_update_begin(&ptr->syncp);			\
173		ptr->mibs[field] += addend;				\
174		u64_stats_update_end(&ptr->syncp);			\
175	} while (0)
176
177#define SNMP_ADD_STATS64_USER(mib, field, addend) 			\
178	do {								\
179		local_bh_disable();					\
180		SNMP_ADD_STATS64_BH(mib, field, addend);		\
181		local_bh_enable();					\
182	} while (0)
183
184#define SNMP_ADD_STATS64(mib, field, addend)				\
185		SNMP_ADD_STATS64_USER(mib, field, addend)
186
187#define SNMP_INC_STATS64_BH(mib, field) SNMP_ADD_STATS64_BH(mib, field, 1)
188#define SNMP_INC_STATS64_USER(mib, field) SNMP_ADD_STATS64_USER(mib, field, 1)
189#define SNMP_INC_STATS64(mib, field) SNMP_ADD_STATS64(mib, field, 1)
190#define SNMP_UPD_PO_STATS64_BH(mib, basefield, addend)			\
191	do {								\
192		__typeof__(*mib[0]) *ptr;				\
193		ptr = __this_cpu_ptr((mib)[0]);				\
194		u64_stats_update_begin(&ptr->syncp);			\
195		ptr->mibs[basefield##PKTS]++;				\
196		ptr->mibs[basefield##OCTETS] += addend;			\
197		u64_stats_update_end(&ptr->syncp);			\
198	} while (0)
199#define SNMP_UPD_PO_STATS64(mib, basefield, addend)			\
200	do {								\
201		local_bh_disable();					\
202		SNMP_UPD_PO_STATS64_BH(mib, basefield, addend);		\
203		local_bh_enable();					\
204	} while (0)
205#else
206#define SNMP_INC_STATS64_BH(mib, field)		SNMP_INC_STATS_BH(mib, field)
207#define SNMP_INC_STATS64_USER(mib, field)	SNMP_INC_STATS_USER(mib, field)
208#define SNMP_INC_STATS64(mib, field)		SNMP_INC_STATS(mib, field)
209#define SNMP_DEC_STATS64(mib, field)		SNMP_DEC_STATS(mib, field)
210#define SNMP_ADD_STATS64_BH(mib, field, addend) SNMP_ADD_STATS_BH(mib, field, addend)
211#define SNMP_ADD_STATS64_USER(mib, field, addend) SNMP_ADD_STATS_USER(mib, field, addend)
212#define SNMP_ADD_STATS64(mib, field, addend)	SNMP_ADD_STATS(mib, field, addend)
213#define SNMP_UPD_PO_STATS64(mib, basefield, addend) SNMP_UPD_PO_STATS(mib, basefield, addend)
214#define SNMP_UPD_PO_STATS64_BH(mib, basefield, addend) SNMP_UPD_PO_STATS_BH(mib, basefield, addend)
215#endif
216
217#endif
v5.9
  1/* SPDX-License-Identifier: GPL-2.0-or-later */
  2/*
  3 *
  4 *		SNMP MIB entries for the IP subsystem.
  5 *		
  6 *		Alan Cox <gw4pts@gw4pts.ampr.org>
  7 *
  8 *		We don't chose to implement SNMP in the kernel (this would
  9 *		be silly as SNMP is a pain in the backside in places). We do
 10 *		however need to collect the MIB statistics and export them
 11 *		out of /proc (eventually)
 
 
 
 
 
 
 12 */
 13 
 14#ifndef _SNMP_H
 15#define _SNMP_H
 16
 17#include <linux/cache.h>
 18#include <linux/snmp.h>
 19#include <linux/smp.h>
 20
 21/*
 22 * Mibs are stored in array of unsigned long.
 23 */
 24/*
 25 * struct snmp_mib{}
 26 *  - list of entries for particular API (such as /proc/net/snmp)
 27 *  - name of entries.
 28 */
 29struct snmp_mib {
 30	const char *name;
 31	int entry;
 32};
 33
 34#define SNMP_MIB_ITEM(_name,_entry)	{	\
 35	.name = _name,				\
 36	.entry = _entry,			\
 37}
 38
 39#define SNMP_MIB_SENTINEL {	\
 40	.name = NULL,		\
 41	.entry = 0,		\
 42}
 43
 44/*
 45 * We use unsigned longs for most mibs but u64 for ipstats.
 46 */
 47#include <linux/u64_stats_sync.h>
 48
 49/* IPstats */
 50#define IPSTATS_MIB_MAX	__IPSTATS_MIB_MAX
 51struct ipstats_mib {
 52	/* mibs[] must be first field of struct ipstats_mib */
 53	u64		mibs[IPSTATS_MIB_MAX];
 54	struct u64_stats_sync syncp;
 55};
 56
 57/* ICMP */
 58#define ICMP_MIB_MAX	__ICMP_MIB_MAX
 59struct icmp_mib {
 60	unsigned long	mibs[ICMP_MIB_MAX];
 61};
 62
 63#define ICMPMSG_MIB_MAX	__ICMPMSG_MIB_MAX
 64struct icmpmsg_mib {
 65	atomic_long_t	mibs[ICMPMSG_MIB_MAX];
 66};
 67
 68/* ICMP6 (IPv6-ICMP) */
 69#define ICMP6_MIB_MAX	__ICMP6_MIB_MAX
 70/* per network ns counters */
 71struct icmpv6_mib {
 72	unsigned long	mibs[ICMP6_MIB_MAX];
 73};
 74/* per device counters, (shared on all cpus) */
 75struct icmpv6_mib_device {
 76	atomic_long_t	mibs[ICMP6_MIB_MAX];
 77};
 78
 79#define ICMP6MSG_MIB_MAX  __ICMP6MSG_MIB_MAX
 80/* per network ns counters */
 81struct icmpv6msg_mib {
 82	atomic_long_t	mibs[ICMP6MSG_MIB_MAX];
 83};
 84/* per device counters, (shared on all cpus) */
 85struct icmpv6msg_mib_device {
 86	atomic_long_t	mibs[ICMP6MSG_MIB_MAX];
 87};
 88
 89
 90/* TCP */
 91#define TCP_MIB_MAX	__TCP_MIB_MAX
 92struct tcp_mib {
 93	unsigned long	mibs[TCP_MIB_MAX];
 94};
 95
 96/* UDP */
 97#define UDP_MIB_MAX	__UDP_MIB_MAX
 98struct udp_mib {
 99	unsigned long	mibs[UDP_MIB_MAX];
100};
101
102/* Linux */
103#define LINUX_MIB_MAX	__LINUX_MIB_MAX
104struct linux_mib {
105	unsigned long	mibs[LINUX_MIB_MAX];
106};
107
108/* Linux Xfrm */
109#define LINUX_MIB_XFRMMAX	__LINUX_MIB_XFRMMAX
110struct linux_xfrm_mib {
111	unsigned long	mibs[LINUX_MIB_XFRMMAX];
112};
113
114/* Linux TLS */
115#define LINUX_MIB_TLSMAX	__LINUX_MIB_TLSMAX
116struct linux_tls_mib {
117	unsigned long	mibs[LINUX_MIB_TLSMAX];
118};
119
120#define DEFINE_SNMP_STAT(type, name)	\
121	__typeof__(type) __percpu *name
122#define DEFINE_SNMP_STAT_ATOMIC(type, name)	\
123	__typeof__(type) *name
124#define DECLARE_SNMP_STAT(type, name)	\
125	extern __typeof__(type) __percpu *name
 
 
 
126
127#define __SNMP_INC_STATS(mib, field)	\
128			__this_cpu_inc(mib->mibs[field])
129
130#define SNMP_INC_STATS_ATOMIC_LONG(mib, field)	\
131			atomic_long_inc(&mib->mibs[field])
132
133#define SNMP_INC_STATS(mib, field)	\
134			this_cpu_inc(mib->mibs[field])
135
136#define SNMP_DEC_STATS(mib, field)	\
137			this_cpu_dec(mib->mibs[field])
138
139#define __SNMP_ADD_STATS(mib, field, addend)	\
140			__this_cpu_add(mib->mibs[field], addend)
 
 
 
141
142#define SNMP_ADD_STATS(mib, field, addend)	\
143			this_cpu_add(mib->mibs[field], addend)
 
 
 
 
144#define SNMP_UPD_PO_STATS(mib, basefield, addend)	\
145	do { \
146		__typeof__((mib->mibs) + 0) ptr = mib->mibs;	\
147		this_cpu_inc(ptr[basefield##PKTS]);		\
148		this_cpu_add(ptr[basefield##OCTETS], addend);	\
149	} while (0)
150#define __SNMP_UPD_PO_STATS(mib, basefield, addend)	\
151	do { \
152		__typeof__((mib->mibs) + 0) ptr = mib->mibs;	\
153		__this_cpu_inc(ptr[basefield##PKTS]);		\
154		__this_cpu_add(ptr[basefield##OCTETS], addend);	\
155	} while (0)
156
157
158#if BITS_PER_LONG==32
159
160#define __SNMP_ADD_STATS64(mib, field, addend) 				\
161	do {								\
162		__typeof__(*mib) *ptr = raw_cpu_ptr(mib);		\
163		u64_stats_update_begin(&ptr->syncp);			\
164		ptr->mibs[field] += addend;				\
165		u64_stats_update_end(&ptr->syncp);			\
166	} while (0)
167
168#define SNMP_ADD_STATS64(mib, field, addend) 				\
169	do {								\
170		local_bh_disable();					\
171		__SNMP_ADD_STATS64(mib, field, addend);			\
172		local_bh_enable();				\
173	} while (0)
174
175#define __SNMP_INC_STATS64(mib, field) SNMP_ADD_STATS64(mib, field, 1)
 
 
 
 
176#define SNMP_INC_STATS64(mib, field) SNMP_ADD_STATS64(mib, field, 1)
177#define __SNMP_UPD_PO_STATS64(mib, basefield, addend)			\
178	do {								\
179		__typeof__(*mib) *ptr;				\
180		ptr = raw_cpu_ptr((mib));				\
181		u64_stats_update_begin(&ptr->syncp);			\
182		ptr->mibs[basefield##PKTS]++;				\
183		ptr->mibs[basefield##OCTETS] += addend;			\
184		u64_stats_update_end(&ptr->syncp);			\
185	} while (0)
186#define SNMP_UPD_PO_STATS64(mib, basefield, addend)			\
187	do {								\
188		local_bh_disable();					\
189		__SNMP_UPD_PO_STATS64(mib, basefield, addend);		\
190		local_bh_enable();				\
191	} while (0)
192#else
193#define __SNMP_INC_STATS64(mib, field)		__SNMP_INC_STATS(mib, field)
 
194#define SNMP_INC_STATS64(mib, field)		SNMP_INC_STATS(mib, field)
195#define SNMP_DEC_STATS64(mib, field)		SNMP_DEC_STATS(mib, field)
196#define __SNMP_ADD_STATS64(mib, field, addend)	__SNMP_ADD_STATS(mib, field, addend)
 
197#define SNMP_ADD_STATS64(mib, field, addend)	SNMP_ADD_STATS(mib, field, addend)
198#define SNMP_UPD_PO_STATS64(mib, basefield, addend) SNMP_UPD_PO_STATS(mib, basefield, addend)
199#define __SNMP_UPD_PO_STATS64(mib, basefield, addend) __SNMP_UPD_PO_STATS(mib, basefield, addend)
200#endif
201
202#endif