1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Shared Memory Communications over RDMA (SMC-R) and RoCE
4 *
5 * Macros for SMC statistics
6 *
7 * Copyright IBM Corp. 2021
8 *
9 * Author(s):  Guvenc Gulce
10 */
11
12#ifndef NET_SMC_SMC_STATS_H_
13#define NET_SMC_SMC_STATS_H_
14#include <linux/init.h>
15#include <linux/mutex.h>
16#include <linux/percpu.h>
17#include <linux/ctype.h>
18#include <linux/smc.h>
19
20#include "smc_clc.h"
21
22#define SMC_MAX_FBACK_RSN_CNT 30
23
24enum {
25	SMC_BUF_8K,
26	SMC_BUF_16K,
27	SMC_BUF_32K,
28	SMC_BUF_64K,
29	SMC_BUF_128K,
30	SMC_BUF_256K,
31	SMC_BUF_512K,
32	SMC_BUF_1024K,
33	SMC_BUF_G_1024K,
34	SMC_BUF_MAX,
35};
36
37struct smc_stats_fback {
38	int	fback_code;
39	u16	count;
40};
41
42struct smc_stats_rsn {
43	struct	smc_stats_fback srv[SMC_MAX_FBACK_RSN_CNT];
44	struct	smc_stats_fback clnt[SMC_MAX_FBACK_RSN_CNT];
45	u64			srv_fback_cnt;
46	u64			clnt_fback_cnt;
47};
48
49struct smc_stats_rmbcnt {
50	u64	buf_size_small_peer_cnt;
51	u64	buf_size_small_cnt;
52	u64	buf_full_peer_cnt;
53	u64	buf_full_cnt;
54	u64	reuse_cnt;
55	u64	alloc_cnt;
56	u64	dgrade_cnt;
57};
58
59struct smc_stats_memsize {
60	u64	buf[SMC_BUF_MAX];
61};
62
63struct smc_stats_tech {
64	struct smc_stats_memsize tx_rmbsize;
65	struct smc_stats_memsize rx_rmbsize;
66	struct smc_stats_memsize tx_pd;
67	struct smc_stats_memsize rx_pd;
68	struct smc_stats_rmbcnt rmb_tx;
69	struct smc_stats_rmbcnt rmb_rx;
70	u64			clnt_v1_succ_cnt;
71	u64			clnt_v2_succ_cnt;
72	u64			srv_v1_succ_cnt;
73	u64			srv_v2_succ_cnt;
74	u64			urg_data_cnt;
75	u64			splice_cnt;
76	u64			cork_cnt;
77	u64			ndly_cnt;
78	u64			rx_bytes;
79	u64			tx_bytes;
80	u64			rx_cnt;
81	u64			tx_cnt;
82};
83
84struct smc_stats {
85	struct smc_stats_tech	smc[2];
86	u64			clnt_hshake_err_cnt;
87	u64			srv_hshake_err_cnt;
88};
89
90#define SMC_STAT_PAYLOAD_SUB(_smc_stats, _tech, key, _len, _rc) \
91do { \
92	typeof(_smc_stats) stats = (_smc_stats); \
93	typeof(_tech) t = (_tech); \
94	typeof(_len) l = (_len); \
95	int _pos; \
96	typeof(_rc) r = (_rc); \
97	int m = SMC_BUF_MAX - 1; \
98	this_cpu_inc((*stats).smc[t].key ## _cnt); \
99	if (r <= 0 || l <= 0) \
100		break; \
101	_pos = fls64((l - 1) >> 13); \
102	_pos = (_pos <= m) ? _pos : m; \
103	this_cpu_inc((*stats).smc[t].key ## _pd.buf[_pos]); \
104	this_cpu_add((*stats).smc[t].key ## _bytes, r); \
105} \
106while (0)
107
108#define SMC_STAT_TX_PAYLOAD(_smc, length, rcode) \
109do { \
110	typeof(_smc) __smc = _smc; \
111	struct net *_net = sock_net(&__smc->sk); \
112	struct smc_stats __percpu *_smc_stats = _net->smc.smc_stats; \
113	typeof(length) _len = (length); \
114	typeof(rcode) _rc = (rcode); \
115	bool is_smcd = !__smc->conn.lnk; \
116	if (is_smcd) \
117		SMC_STAT_PAYLOAD_SUB(_smc_stats, SMC_TYPE_D, tx, _len, _rc); \
118	else \
119		SMC_STAT_PAYLOAD_SUB(_smc_stats, SMC_TYPE_R, tx, _len, _rc); \
120} \
121while (0)
122
123#define SMC_STAT_RX_PAYLOAD(_smc, length, rcode) \
124do { \
125	typeof(_smc) __smc = _smc; \
126	struct net *_net = sock_net(&__smc->sk); \
127	struct smc_stats __percpu *_smc_stats = _net->smc.smc_stats; \
128	typeof(length) _len = (length); \
129	typeof(rcode) _rc = (rcode); \
130	bool is_smcd = !__smc->conn.lnk; \
131	if (is_smcd) \
132		SMC_STAT_PAYLOAD_SUB(_smc_stats, SMC_TYPE_D, rx, _len, _rc); \
133	else \
134		SMC_STAT_PAYLOAD_SUB(_smc_stats, SMC_TYPE_R, rx, _len, _rc); \
135} \
136while (0)
137
138#define SMC_STAT_RMB_SIZE_SUB(_smc_stats, _tech, k, _len) \
139do { \
140	typeof(_len) _l = (_len); \
141	typeof(_tech) t = (_tech); \
142	int _pos; \
143	int m = SMC_BUF_MAX - 1; \
144	if (_l <= 0) \
145		break; \
146	_pos = fls((_l - 1) >> 13); \
147	_pos = (_pos <= m) ? _pos : m; \
148	this_cpu_inc((*(_smc_stats)).smc[t].k ## _rmbsize.buf[_pos]); \
149} \
150while (0)
151
152#define SMC_STAT_RMB_SUB(_smc_stats, type, t, key) \
153	this_cpu_inc((*(_smc_stats)).smc[t].rmb ## _ ## key.type ## _cnt)
154
155#define SMC_STAT_RMB_SIZE(_smc, _is_smcd, _is_rx, _len) \
156do { \
157	struct net *_net = sock_net(&(_smc)->sk); \
158	struct smc_stats __percpu *_smc_stats = _net->smc.smc_stats; \
159	typeof(_is_smcd) is_d = (_is_smcd); \
160	typeof(_is_rx) is_r = (_is_rx); \
161	typeof(_len) l = (_len); \
162	if ((is_d) && (is_r)) \
163		SMC_STAT_RMB_SIZE_SUB(_smc_stats, SMC_TYPE_D, rx, l); \
164	if ((is_d) && !(is_r)) \
165		SMC_STAT_RMB_SIZE_SUB(_smc_stats, SMC_TYPE_D, tx, l); \
166	if (!(is_d) && (is_r)) \
167		SMC_STAT_RMB_SIZE_SUB(_smc_stats, SMC_TYPE_R, rx, l); \
168	if (!(is_d) && !(is_r)) \
169		SMC_STAT_RMB_SIZE_SUB(_smc_stats, SMC_TYPE_R, tx, l); \
170} \
171while (0)
172
173#define SMC_STAT_RMB(_smc, type, _is_smcd, _is_rx) \
174do { \
175	struct net *net = sock_net(&(_smc)->sk); \
176	struct smc_stats __percpu *_smc_stats = net->smc.smc_stats; \
177	typeof(_is_smcd) is_d = (_is_smcd); \
178	typeof(_is_rx) is_r = (_is_rx); \
179	if ((is_d) && (is_r)) \
180		SMC_STAT_RMB_SUB(_smc_stats, type, SMC_TYPE_D, rx); \
181	if ((is_d) && !(is_r)) \
182		SMC_STAT_RMB_SUB(_smc_stats, type, SMC_TYPE_D, tx); \
183	if (!(is_d) && (is_r)) \
184		SMC_STAT_RMB_SUB(_smc_stats, type, SMC_TYPE_R, rx); \
185	if (!(is_d) && !(is_r)) \
186		SMC_STAT_RMB_SUB(_smc_stats, type, SMC_TYPE_R, tx); \
187} \
188while (0)
189
190#define SMC_STAT_BUF_REUSE(smc, is_smcd, is_rx) \
191	SMC_STAT_RMB(smc, reuse, is_smcd, is_rx)
192
193#define SMC_STAT_RMB_ALLOC(smc, is_smcd, is_rx) \
194	SMC_STAT_RMB(smc, alloc, is_smcd, is_rx)
195
196#define SMC_STAT_RMB_DOWNGRADED(smc, is_smcd, is_rx) \
197	SMC_STAT_RMB(smc, dgrade, is_smcd, is_rx)
198
199#define SMC_STAT_RMB_TX_PEER_FULL(smc, is_smcd) \
200	SMC_STAT_RMB(smc, buf_full_peer, is_smcd, false)
201
202#define SMC_STAT_RMB_TX_FULL(smc, is_smcd) \
203	SMC_STAT_RMB(smc, buf_full, is_smcd, false)
204
205#define SMC_STAT_RMB_TX_PEER_SIZE_SMALL(smc, is_smcd) \
206	SMC_STAT_RMB(smc, buf_size_small_peer, is_smcd, false)
207
208#define SMC_STAT_RMB_TX_SIZE_SMALL(smc, is_smcd) \
209	SMC_STAT_RMB(smc, buf_size_small, is_smcd, false)
210
211#define SMC_STAT_RMB_RX_SIZE_SMALL(smc, is_smcd) \
212	SMC_STAT_RMB(smc, buf_size_small, is_smcd, true)
213
214#define SMC_STAT_RMB_RX_FULL(smc, is_smcd) \
215	SMC_STAT_RMB(smc, buf_full, is_smcd, true)
216
217#define SMC_STAT_INC(_smc, type) \
218do { \
219	typeof(_smc) __smc = _smc; \
220	bool is_smcd = !(__smc)->conn.lnk; \
221	struct net *net = sock_net(&(__smc)->sk); \
222	struct smc_stats __percpu *smc_stats = net->smc.smc_stats; \
223	if ((is_smcd)) \
224		this_cpu_inc(smc_stats->smc[SMC_TYPE_D].type); \
225	else \
226		this_cpu_inc(smc_stats->smc[SMC_TYPE_R].type); \
227} \
228while (0)
229
230#define SMC_STAT_CLNT_SUCC_INC(net, _aclc) \
231do { \
232	typeof(_aclc) acl = (_aclc); \
233	bool is_v2 = (acl->hdr.version == SMC_V2); \
234	bool is_smcd = (acl->hdr.typev1 == SMC_TYPE_D); \
235	struct smc_stats __percpu *smc_stats = (net)->smc.smc_stats; \
236	if (is_v2 && is_smcd) \
237		this_cpu_inc(smc_stats->smc[SMC_TYPE_D].clnt_v2_succ_cnt); \
238	else if (is_v2 && !is_smcd) \
239		this_cpu_inc(smc_stats->smc[SMC_TYPE_R].clnt_v2_succ_cnt); \
240	else if (!is_v2 && is_smcd) \
241		this_cpu_inc(smc_stats->smc[SMC_TYPE_D].clnt_v1_succ_cnt); \
242	else if (!is_v2 && !is_smcd) \
243		this_cpu_inc(smc_stats->smc[SMC_TYPE_R].clnt_v1_succ_cnt); \
244} \
245while (0)
246
247#define SMC_STAT_SERV_SUCC_INC(net, _ini) \
248do { \
249	typeof(_ini) i = (_ini); \
250	bool is_smcd = (i->is_smcd); \
251	u8 version = is_smcd ? i->smcd_version : i->smcr_version; \
252	bool is_v2 = (version & SMC_V2); \
253	typeof(net->smc.smc_stats) smc_stats = (net)->smc.smc_stats; \
254	if (is_v2 && is_smcd) \
255		this_cpu_inc(smc_stats->smc[SMC_TYPE_D].srv_v2_succ_cnt); \
256	else if (is_v2 && !is_smcd) \
257		this_cpu_inc(smc_stats->smc[SMC_TYPE_R].srv_v2_succ_cnt); \
258	else if (!is_v2 && is_smcd) \
259		this_cpu_inc(smc_stats->smc[SMC_TYPE_D].srv_v1_succ_cnt); \
260	else if (!is_v2 && !is_smcd) \
261		this_cpu_inc(smc_stats->smc[SMC_TYPE_R].srv_v1_succ_cnt); \
262} \
263while (0)
264
265int smc_nl_get_stats(struct sk_buff *skb, struct netlink_callback *cb);
266int smc_nl_get_fback_stats(struct sk_buff *skb, struct netlink_callback *cb);
267int smc_stats_init(struct net *net);
268void smc_stats_exit(struct net *net);
269
270#endif /* NET_SMC_SMC_STATS_H_ */
271