1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Shared Memory Communications over RDMA (SMC-R) and RoCE
5 * Macros for SMC statistics
7 * Copyright IBM Corp. 2021
9 * Author(s): Guvenc Gulce
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>
22 #define SMC_MAX_FBACK_RSN_CNT 36
37 struct smc_stats_fback
{
42 struct 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
];
49 struct smc_stats_rmbcnt
{
50 u64 buf_size_small_peer_cnt
;
51 u64 buf_size_small_cnt
;
52 u64 buf_full_peer_cnt
;
59 struct smc_stats_memsize
{
63 struct 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
;
87 struct smc_stats_tech smc
[2];
88 u64 clnt_hshake_err_cnt
;
89 u64 srv_hshake_err_cnt
;
92 #define SMC_STAT_PAYLOAD_SUB(_smc_stats, _tech, key, _len, _rc) \
94 typeof(_smc_stats) stats = (_smc_stats); \
95 typeof(_tech) t = (_tech); \
96 typeof(_len) l = (_len); \
98 typeof(_rc) r = (_rc); \
99 int m = SMC_BUF_MAX - 1; \
100 this_cpu_inc((*stats).smc[t].key ## _cnt); \
101 if (r <= 0 || l <= 0) \
103 _pos = fls64((l - 1) >> 13); \
104 _pos = (_pos <= m) ? _pos : m; \
105 this_cpu_inc((*stats).smc[t].key ## _pd.buf[_pos]); \
106 this_cpu_add((*stats).smc[t].key ## _bytes, r); \
110 #define SMC_STAT_TX_PAYLOAD(_smc, length, rcode) \
112 typeof(_smc) __smc = _smc; \
113 struct net *_net = sock_net(&__smc->sk); \
114 struct smc_stats __percpu *_smc_stats = _net->smc.smc_stats; \
115 typeof(length) _len = (length); \
116 typeof(rcode) _rc = (rcode); \
117 bool is_smcd = !__smc->conn.lnk; \
119 SMC_STAT_PAYLOAD_SUB(_smc_stats, SMC_TYPE_D, tx, _len, _rc); \
121 SMC_STAT_PAYLOAD_SUB(_smc_stats, SMC_TYPE_R, tx, _len, _rc); \
125 #define SMC_STAT_RX_PAYLOAD(_smc, length, rcode) \
127 typeof(_smc) __smc = _smc; \
128 struct net *_net = sock_net(&__smc->sk); \
129 struct smc_stats __percpu *_smc_stats = _net->smc.smc_stats; \
130 typeof(length) _len = (length); \
131 typeof(rcode) _rc = (rcode); \
132 bool is_smcd = !__smc->conn.lnk; \
134 SMC_STAT_PAYLOAD_SUB(_smc_stats, SMC_TYPE_D, rx, _len, _rc); \
136 SMC_STAT_PAYLOAD_SUB(_smc_stats, SMC_TYPE_R, rx, _len, _rc); \
140 #define SMC_STAT_RMB_SIZE_SUB(_smc_stats, _tech, k, _is_add, _len) \
142 typeof(_smc_stats) stats = (_smc_stats); \
143 typeof(_is_add) is_a = (_is_add); \
144 typeof(_len) _l = (_len); \
145 typeof(_tech) t = (_tech); \
147 int m = SMC_BUF_MAX - 1; \
151 _pos = fls((_l - 1) >> 13); \
152 _pos = (_pos <= m) ? _pos : m; \
153 this_cpu_inc((*stats).smc[t].k ## _rmbsize.buf[_pos]); \
154 this_cpu_add((*stats).smc[t].k ## _rmbuse, _l); \
156 this_cpu_sub((*stats).smc[t].k ## _rmbuse, _l); \
161 #define SMC_STAT_RMB_SUB(_smc_stats, type, t, key) \
162 this_cpu_inc((*(_smc_stats)).smc[t].rmb ## _ ## key.type ## _cnt)
164 #define SMC_STAT_RMB_SIZE(_smc, _is_smcd, _is_rx, _is_add, _len) \
166 struct net *_net = sock_net(&(_smc)->sk); \
167 struct smc_stats __percpu *_smc_stats = _net->smc.smc_stats; \
168 typeof(_is_add) is_add = (_is_add); \
169 typeof(_is_smcd) is_d = (_is_smcd); \
170 typeof(_is_rx) is_r = (_is_rx); \
171 typeof(_len) l = (_len); \
172 if ((is_d) && (is_r)) \
173 SMC_STAT_RMB_SIZE_SUB(_smc_stats, SMC_TYPE_D, rx, is_add, l); \
174 if ((is_d) && !(is_r)) \
175 SMC_STAT_RMB_SIZE_SUB(_smc_stats, SMC_TYPE_D, tx, is_add, l); \
176 if (!(is_d) && (is_r)) \
177 SMC_STAT_RMB_SIZE_SUB(_smc_stats, SMC_TYPE_R, rx, is_add, l); \
178 if (!(is_d) && !(is_r)) \
179 SMC_STAT_RMB_SIZE_SUB(_smc_stats, SMC_TYPE_R, tx, is_add, l); \
183 #define SMC_STAT_RMB(_smc, type, _is_smcd, _is_rx) \
185 struct net *net = sock_net(&(_smc)->sk); \
186 struct smc_stats __percpu *_smc_stats = net->smc.smc_stats; \
187 typeof(_is_smcd) is_d = (_is_smcd); \
188 typeof(_is_rx) is_r = (_is_rx); \
189 if ((is_d) && (is_r)) \
190 SMC_STAT_RMB_SUB(_smc_stats, type, SMC_TYPE_D, rx); \
191 if ((is_d) && !(is_r)) \
192 SMC_STAT_RMB_SUB(_smc_stats, type, SMC_TYPE_D, tx); \
193 if (!(is_d) && (is_r)) \
194 SMC_STAT_RMB_SUB(_smc_stats, type, SMC_TYPE_R, rx); \
195 if (!(is_d) && !(is_r)) \
196 SMC_STAT_RMB_SUB(_smc_stats, type, SMC_TYPE_R, tx); \
200 #define SMC_STAT_BUF_REUSE(smc, is_smcd, is_rx) \
201 SMC_STAT_RMB(smc, reuse, is_smcd, is_rx)
203 #define SMC_STAT_RMB_ALLOC(smc, is_smcd, is_rx) \
204 SMC_STAT_RMB(smc, alloc, is_smcd, is_rx)
206 #define SMC_STAT_RMB_DOWNGRADED(smc, is_smcd, is_rx) \
207 SMC_STAT_RMB(smc, dgrade, is_smcd, is_rx)
209 #define SMC_STAT_RMB_TX_PEER_FULL(smc, is_smcd) \
210 SMC_STAT_RMB(smc, buf_full_peer, is_smcd, false)
212 #define SMC_STAT_RMB_TX_FULL(smc, is_smcd) \
213 SMC_STAT_RMB(smc, buf_full, is_smcd, false)
215 #define SMC_STAT_RMB_TX_PEER_SIZE_SMALL(smc, is_smcd) \
216 SMC_STAT_RMB(smc, buf_size_small_peer, is_smcd, false)
218 #define SMC_STAT_RMB_TX_SIZE_SMALL(smc, is_smcd) \
219 SMC_STAT_RMB(smc, buf_size_small, is_smcd, false)
221 #define SMC_STAT_RMB_RX_SIZE_SMALL(smc, is_smcd) \
222 SMC_STAT_RMB(smc, buf_size_small, is_smcd, true)
224 #define SMC_STAT_RMB_RX_FULL(smc, is_smcd) \
225 SMC_STAT_RMB(smc, buf_full, is_smcd, true)
227 #define SMC_STAT_INC(_smc, type) \
229 typeof(_smc) __smc = _smc; \
230 bool is_smcd = !(__smc)->conn.lnk; \
231 struct net *net = sock_net(&(__smc)->sk); \
232 struct smc_stats __percpu *smc_stats = net->smc.smc_stats; \
234 this_cpu_inc(smc_stats->smc[SMC_TYPE_D].type); \
236 this_cpu_inc(smc_stats->smc[SMC_TYPE_R].type); \
240 #define SMC_STAT_CLNT_SUCC_INC(net, _aclc) \
242 typeof(_aclc) acl = (_aclc); \
243 bool is_v2 = (acl->hdr.version == SMC_V2); \
244 bool is_smcd = (acl->hdr.typev1 == SMC_TYPE_D); \
245 struct smc_stats __percpu *smc_stats = (net)->smc.smc_stats; \
246 if (is_v2 && is_smcd) \
247 this_cpu_inc(smc_stats->smc[SMC_TYPE_D].clnt_v2_succ_cnt); \
248 else if (is_v2 && !is_smcd) \
249 this_cpu_inc(smc_stats->smc[SMC_TYPE_R].clnt_v2_succ_cnt); \
250 else if (!is_v2 && is_smcd) \
251 this_cpu_inc(smc_stats->smc[SMC_TYPE_D].clnt_v1_succ_cnt); \
252 else if (!is_v2 && !is_smcd) \
253 this_cpu_inc(smc_stats->smc[SMC_TYPE_R].clnt_v1_succ_cnt); \
257 #define SMC_STAT_SERV_SUCC_INC(net, _ini) \
259 typeof(_ini) i = (_ini); \
260 bool is_smcd = (i->is_smcd); \
261 u8 version = is_smcd ? i->smcd_version : i->smcr_version; \
262 bool is_v2 = (version & SMC_V2); \
263 typeof(net->smc.smc_stats) smc_stats = (net)->smc.smc_stats; \
264 if (is_v2 && is_smcd) \
265 this_cpu_inc(smc_stats->smc[SMC_TYPE_D].srv_v2_succ_cnt); \
266 else if (is_v2 && !is_smcd) \
267 this_cpu_inc(smc_stats->smc[SMC_TYPE_R].srv_v2_succ_cnt); \
268 else if (!is_v2 && is_smcd) \
269 this_cpu_inc(smc_stats->smc[SMC_TYPE_D].srv_v1_succ_cnt); \
270 else if (!is_v2 && !is_smcd) \
271 this_cpu_inc(smc_stats->smc[SMC_TYPE_R].srv_v1_succ_cnt); \
275 int smc_nl_get_stats(struct sk_buff
*skb
, struct netlink_callback
*cb
);
276 int smc_nl_get_fback_stats(struct sk_buff
*skb
, struct netlink_callback
*cb
);
277 int smc_stats_init(struct net
*net
);
278 void smc_stats_exit(struct net
*net
);
280 #endif /* NET_SMC_SMC_STATS_H_ */