12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * net/core/gen_stats.c
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Authors: Thomas Graf <tgraf@suug.ch>
61da177e4SLinus Torvalds * Jamal Hadi Salim
71da177e4SLinus Torvalds * Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
81da177e4SLinus Torvalds *
98c498935SMauro Carvalho Chehab * See Documentation/networking/gen_stats.rst
101da177e4SLinus Torvalds */
111da177e4SLinus Torvalds
121da177e4SLinus Torvalds #include <linux/types.h>
131da177e4SLinus Torvalds #include <linux/kernel.h>
141da177e4SLinus Torvalds #include <linux/module.h>
151da177e4SLinus Torvalds #include <linux/interrupt.h>
161da177e4SLinus Torvalds #include <linux/socket.h>
171da177e4SLinus Torvalds #include <linux/rtnetlink.h>
181da177e4SLinus Torvalds #include <linux/gen_stats.h>
191e90474cSPatrick McHardy #include <net/netlink.h>
201da177e4SLinus Torvalds #include <net/gen_stats.h>
2167c9e627SAhmed S. Darwish #include <net/sch_generic.h>
221da177e4SLinus Torvalds
231da177e4SLinus Torvalds static inline int
gnet_stats_copy(struct gnet_dump * d,int type,void * buf,int size,int padattr)249854518eSNicolas Dichtel gnet_stats_copy(struct gnet_dump *d, int type, void *buf, int size, int padattr)
251da177e4SLinus Torvalds {
269854518eSNicolas Dichtel if (nla_put_64bit(d->skb, type, size, buf, padattr))
2714ad6647SDavid S. Miller goto nla_put_failure;
281da177e4SLinus Torvalds return 0;
291da177e4SLinus Torvalds
301e90474cSPatrick McHardy nla_put_failure:
31edb09eb1SEric Dumazet if (d->lock)
32edb09eb1SEric Dumazet spin_unlock_bh(d->lock);
331c4cff0cSIgnacy Gawędzki kfree(d->xstats);
341c4cff0cSIgnacy Gawędzki d->xstats = NULL;
351c4cff0cSIgnacy Gawędzki d->xstats_len = 0;
361da177e4SLinus Torvalds return -1;
371da177e4SLinus Torvalds }
381da177e4SLinus Torvalds
391da177e4SLinus Torvalds /**
401da177e4SLinus Torvalds * gnet_stats_start_copy_compat - start dumping procedure in compatibility mode
411da177e4SLinus Torvalds * @skb: socket buffer to put statistics TLVs into
421da177e4SLinus Torvalds * @type: TLV type for top level statistic TLV
431da177e4SLinus Torvalds * @tc_stats_type: TLV type for backward compatibility struct tc_stats TLV
441da177e4SLinus Torvalds * @xstats_type: TLV type for backward compatibility xstats TLV
451da177e4SLinus Torvalds * @lock: statistics lock
461da177e4SLinus Torvalds * @d: dumping handle
47e0d194adSEric Dumazet * @padattr: padding attribute
481da177e4SLinus Torvalds *
491da177e4SLinus Torvalds * Initializes the dumping handle, grabs the statistic lock and appends
501da177e4SLinus Torvalds * an empty TLV header to the socket buffer for use a container for all
511da177e4SLinus Torvalds * other statistic TLVS.
521da177e4SLinus Torvalds *
531da177e4SLinus Torvalds * The dumping handle is marked to be in backward compatibility mode telling
541da177e4SLinus Torvalds * all gnet_stats_copy_XXX() functions to fill a local copy of struct tc_stats.
551da177e4SLinus Torvalds *
561da177e4SLinus Torvalds * Returns 0 on success or -1 if the room in the socket buffer was not sufficient.
571da177e4SLinus Torvalds */
581da177e4SLinus Torvalds int
gnet_stats_start_copy_compat(struct sk_buff * skb,int type,int tc_stats_type,int xstats_type,spinlock_t * lock,struct gnet_dump * d,int padattr)591da177e4SLinus Torvalds gnet_stats_start_copy_compat(struct sk_buff *skb, int type, int tc_stats_type,
609854518eSNicolas Dichtel int xstats_type, spinlock_t *lock,
619854518eSNicolas Dichtel struct gnet_dump *d, int padattr)
629a429c49SEric Dumazet __acquires(lock)
631da177e4SLinus Torvalds {
641da177e4SLinus Torvalds memset(d, 0, sizeof(*d));
651da177e4SLinus Torvalds
661da177e4SLinus Torvalds if (type)
671e90474cSPatrick McHardy d->tail = (struct nlattr *)skb_tail_pointer(skb);
681da177e4SLinus Torvalds d->skb = skb;
691da177e4SLinus Torvalds d->compat_tc_stats = tc_stats_type;
701da177e4SLinus Torvalds d->compat_xstats = xstats_type;
719854518eSNicolas Dichtel d->padattr = padattr;
72edb09eb1SEric Dumazet if (lock) {
73edb09eb1SEric Dumazet d->lock = lock;
74edb09eb1SEric Dumazet spin_lock_bh(lock);
75edb09eb1SEric Dumazet }
76d5a672acSToke Høiland-Jørgensen if (d->tail) {
77d5a672acSToke Høiland-Jørgensen int ret = gnet_stats_copy(d, type, NULL, 0, padattr);
78d5a672acSToke Høiland-Jørgensen
79d5a672acSToke Høiland-Jørgensen /* The initial attribute added in gnet_stats_copy() may be
80d5a672acSToke Høiland-Jørgensen * preceded by a padding attribute, in which case d->tail will
81d5a672acSToke Høiland-Jørgensen * end up pointing at the padding instead of the real attribute.
82d5a672acSToke Høiland-Jørgensen * Fix this so gnet_stats_finish_copy() adjusts the length of
83d5a672acSToke Høiland-Jørgensen * the right attribute.
84d5a672acSToke Høiland-Jørgensen */
85d5a672acSToke Høiland-Jørgensen if (ret == 0 && d->tail->nla_type == padattr)
86d5a672acSToke Høiland-Jørgensen d->tail = (struct nlattr *)((char *)d->tail +
87d5a672acSToke Høiland-Jørgensen NLA_ALIGN(d->tail->nla_len));
88d5a672acSToke Høiland-Jørgensen return ret;
89d5a672acSToke Høiland-Jørgensen }
901da177e4SLinus Torvalds
911da177e4SLinus Torvalds return 0;
921da177e4SLinus Torvalds }
939e34a5b5SEric Dumazet EXPORT_SYMBOL(gnet_stats_start_copy_compat);
941da177e4SLinus Torvalds
951da177e4SLinus Torvalds /**
969854518eSNicolas Dichtel * gnet_stats_start_copy - start dumping procedure in compatibility mode
971da177e4SLinus Torvalds * @skb: socket buffer to put statistics TLVs into
981da177e4SLinus Torvalds * @type: TLV type for top level statistic TLV
991da177e4SLinus Torvalds * @lock: statistics lock
1001da177e4SLinus Torvalds * @d: dumping handle
101e0d194adSEric Dumazet * @padattr: padding attribute
1021da177e4SLinus Torvalds *
1031da177e4SLinus Torvalds * Initializes the dumping handle, grabs the statistic lock and appends
1041da177e4SLinus Torvalds * an empty TLV header to the socket buffer for use a container for all
1051da177e4SLinus Torvalds * other statistic TLVS.
1061da177e4SLinus Torvalds *
1071da177e4SLinus Torvalds * Returns 0 on success or -1 if the room in the socket buffer was not sufficient.
1081da177e4SLinus Torvalds */
1091da177e4SLinus Torvalds int
gnet_stats_start_copy(struct sk_buff * skb,int type,spinlock_t * lock,struct gnet_dump * d,int padattr)1101da177e4SLinus Torvalds gnet_stats_start_copy(struct sk_buff *skb, int type, spinlock_t *lock,
1119854518eSNicolas Dichtel struct gnet_dump *d, int padattr)
1121da177e4SLinus Torvalds {
1139854518eSNicolas Dichtel return gnet_stats_start_copy_compat(skb, type, 0, 0, lock, d, padattr);
1141da177e4SLinus Torvalds }
1159e34a5b5SEric Dumazet EXPORT_SYMBOL(gnet_stats_start_copy);
1161da177e4SLinus Torvalds
11767c9e627SAhmed S. Darwish /* Must not be inlined, due to u64_stats seqcount_t lockdep key */
gnet_stats_basic_sync_init(struct gnet_stats_basic_sync * b)11850dc9a85SAhmed S. Darwish void gnet_stats_basic_sync_init(struct gnet_stats_basic_sync *b)
11967c9e627SAhmed S. Darwish {
12050dc9a85SAhmed S. Darwish u64_stats_set(&b->bytes, 0);
12150dc9a85SAhmed S. Darwish u64_stats_set(&b->packets, 0);
12267c9e627SAhmed S. Darwish u64_stats_init(&b->syncp);
12367c9e627SAhmed S. Darwish }
12450dc9a85SAhmed S. Darwish EXPORT_SYMBOL(gnet_stats_basic_sync_init);
12567c9e627SAhmed S. Darwish
gnet_stats_add_basic_cpu(struct gnet_stats_basic_sync * bstats,struct gnet_stats_basic_sync __percpu * cpu)12650dc9a85SAhmed S. Darwish static void gnet_stats_add_basic_cpu(struct gnet_stats_basic_sync *bstats,
12750dc9a85SAhmed S. Darwish struct gnet_stats_basic_sync __percpu *cpu)
12822e0f8b9SJohn Fastabend {
129f56940daSAhmed S. Darwish u64 t_bytes = 0, t_packets = 0;
13022e0f8b9SJohn Fastabend int i;
13122e0f8b9SJohn Fastabend
13222e0f8b9SJohn Fastabend for_each_possible_cpu(i) {
13350dc9a85SAhmed S. Darwish struct gnet_stats_basic_sync *bcpu = per_cpu_ptr(cpu, i);
13422e0f8b9SJohn Fastabend unsigned int start;
135d0083d98SEric Dumazet u64 bytes, packets;
13622e0f8b9SJohn Fastabend
13722e0f8b9SJohn Fastabend do {
138*d120d1a6SThomas Gleixner start = u64_stats_fetch_begin(&bcpu->syncp);
13950dc9a85SAhmed S. Darwish bytes = u64_stats_read(&bcpu->bytes);
14050dc9a85SAhmed S. Darwish packets = u64_stats_read(&bcpu->packets);
141*d120d1a6SThomas Gleixner } while (u64_stats_fetch_retry(&bcpu->syncp, start));
14222e0f8b9SJohn Fastabend
143f56940daSAhmed S. Darwish t_bytes += bytes;
144f56940daSAhmed S. Darwish t_packets += packets;
14522e0f8b9SJohn Fastabend }
146f56940daSAhmed S. Darwish _bstats_update(bstats, t_bytes, t_packets);
14722e0f8b9SJohn Fastabend }
14822e0f8b9SJohn Fastabend
gnet_stats_add_basic(struct gnet_stats_basic_sync * bstats,struct gnet_stats_basic_sync __percpu * cpu,struct gnet_stats_basic_sync * b,bool running)14929cbcd85SAhmed S. Darwish void gnet_stats_add_basic(struct gnet_stats_basic_sync *bstats,
15050dc9a85SAhmed S. Darwish struct gnet_stats_basic_sync __percpu *cpu,
15129cbcd85SAhmed S. Darwish struct gnet_stats_basic_sync *b, bool running)
15222e0f8b9SJohn Fastabend {
15329cbcd85SAhmed S. Darwish unsigned int start;
154fbf307c8SSebastian Andrzej Siewior u64 bytes = 0;
155fbf307c8SSebastian Andrzej Siewior u64 packets = 0;
156edb09eb1SEric Dumazet
157e22db7bdSSebastian Andrzej Siewior WARN_ON_ONCE((cpu || running) && in_hardirq());
15829cbcd85SAhmed S. Darwish
15922e0f8b9SJohn Fastabend if (cpu) {
160fbf307c8SSebastian Andrzej Siewior gnet_stats_add_basic_cpu(bstats, cpu);
161edb09eb1SEric Dumazet return;
162edb09eb1SEric Dumazet }
163edb09eb1SEric Dumazet do {
164edb09eb1SEric Dumazet if (running)
165*d120d1a6SThomas Gleixner start = u64_stats_fetch_begin(&b->syncp);
16650dc9a85SAhmed S. Darwish bytes = u64_stats_read(&b->bytes);
16750dc9a85SAhmed S. Darwish packets = u64_stats_read(&b->packets);
168*d120d1a6SThomas Gleixner } while (running && u64_stats_fetch_retry(&b->syncp, start));
169fbf307c8SSebastian Andrzej Siewior
170f56940daSAhmed S. Darwish _bstats_update(bstats, bytes, packets);
17122e0f8b9SJohn Fastabend }
172fbf307c8SSebastian Andrzej Siewior EXPORT_SYMBOL(gnet_stats_add_basic);
17322e0f8b9SJohn Fastabend
gnet_stats_read_basic(u64 * ret_bytes,u64 * ret_packets,struct gnet_stats_basic_sync __percpu * cpu,struct gnet_stats_basic_sync * b,bool running)174c5c6e589SSebastian Andrzej Siewior static void gnet_stats_read_basic(u64 *ret_bytes, u64 *ret_packets,
175c5c6e589SSebastian Andrzej Siewior struct gnet_stats_basic_sync __percpu *cpu,
176c5c6e589SSebastian Andrzej Siewior struct gnet_stats_basic_sync *b, bool running)
177c5c6e589SSebastian Andrzej Siewior {
178c5c6e589SSebastian Andrzej Siewior unsigned int start;
179c5c6e589SSebastian Andrzej Siewior
180c5c6e589SSebastian Andrzej Siewior if (cpu) {
181c5c6e589SSebastian Andrzej Siewior u64 t_bytes = 0, t_packets = 0;
182c5c6e589SSebastian Andrzej Siewior int i;
183c5c6e589SSebastian Andrzej Siewior
184c5c6e589SSebastian Andrzej Siewior for_each_possible_cpu(i) {
185c5c6e589SSebastian Andrzej Siewior struct gnet_stats_basic_sync *bcpu = per_cpu_ptr(cpu, i);
186c5c6e589SSebastian Andrzej Siewior unsigned int start;
187c5c6e589SSebastian Andrzej Siewior u64 bytes, packets;
188c5c6e589SSebastian Andrzej Siewior
189c5c6e589SSebastian Andrzej Siewior do {
190*d120d1a6SThomas Gleixner start = u64_stats_fetch_begin(&bcpu->syncp);
191c5c6e589SSebastian Andrzej Siewior bytes = u64_stats_read(&bcpu->bytes);
192c5c6e589SSebastian Andrzej Siewior packets = u64_stats_read(&bcpu->packets);
193*d120d1a6SThomas Gleixner } while (u64_stats_fetch_retry(&bcpu->syncp, start));
194c5c6e589SSebastian Andrzej Siewior
195c5c6e589SSebastian Andrzej Siewior t_bytes += bytes;
196c5c6e589SSebastian Andrzej Siewior t_packets += packets;
197c5c6e589SSebastian Andrzej Siewior }
198c5c6e589SSebastian Andrzej Siewior *ret_bytes = t_bytes;
199c5c6e589SSebastian Andrzej Siewior *ret_packets = t_packets;
200c5c6e589SSebastian Andrzej Siewior return;
201c5c6e589SSebastian Andrzej Siewior }
202c5c6e589SSebastian Andrzej Siewior do {
203c5c6e589SSebastian Andrzej Siewior if (running)
204*d120d1a6SThomas Gleixner start = u64_stats_fetch_begin(&b->syncp);
205c5c6e589SSebastian Andrzej Siewior *ret_bytes = u64_stats_read(&b->bytes);
206c5c6e589SSebastian Andrzej Siewior *ret_packets = u64_stats_read(&b->packets);
207*d120d1a6SThomas Gleixner } while (running && u64_stats_fetch_retry(&b->syncp, start));
208c5c6e589SSebastian Andrzej Siewior }
209c5c6e589SSebastian Andrzej Siewior
2105d70a670SWei Yongjun static int
___gnet_stats_copy_basic(struct gnet_dump * d,struct gnet_stats_basic_sync __percpu * cpu,struct gnet_stats_basic_sync * b,int type,bool running)21129cbcd85SAhmed S. Darwish ___gnet_stats_copy_basic(struct gnet_dump *d,
21250dc9a85SAhmed S. Darwish struct gnet_stats_basic_sync __percpu *cpu,
21350dc9a85SAhmed S. Darwish struct gnet_stats_basic_sync *b,
21429cbcd85SAhmed S. Darwish int type, bool running)
2155e111210SEelco Chaudron {
21650dc9a85SAhmed S. Darwish u64 bstats_bytes, bstats_packets;
2175e111210SEelco Chaudron
218c5c6e589SSebastian Andrzej Siewior gnet_stats_read_basic(&bstats_bytes, &bstats_packets, cpu, b, running);
21950dc9a85SAhmed S. Darwish
2205e111210SEelco Chaudron if (d->compat_tc_stats && type == TCA_STATS_BASIC) {
22150dc9a85SAhmed S. Darwish d->tc_stats.bytes = bstats_bytes;
22250dc9a85SAhmed S. Darwish d->tc_stats.packets = bstats_packets;
2235e111210SEelco Chaudron }
2245e111210SEelco Chaudron
2255e111210SEelco Chaudron if (d->tail) {
2265e111210SEelco Chaudron struct gnet_stats_basic sb;
227b33e699fSEric Dumazet int res;
2285e111210SEelco Chaudron
2295e111210SEelco Chaudron memset(&sb, 0, sizeof(sb));
23050dc9a85SAhmed S. Darwish sb.bytes = bstats_bytes;
23150dc9a85SAhmed S. Darwish sb.packets = bstats_packets;
232b33e699fSEric Dumazet res = gnet_stats_copy(d, type, &sb, sizeof(sb), TCA_STATS_PAD);
23350dc9a85SAhmed S. Darwish if (res < 0 || sb.packets == bstats_packets)
234b33e699fSEric Dumazet return res;
235b33e699fSEric Dumazet /* emit 64bit stats only if needed */
23650dc9a85SAhmed S. Darwish return gnet_stats_copy(d, TCA_STATS_PKT64, &bstats_packets,
23750dc9a85SAhmed S. Darwish sizeof(bstats_packets), TCA_STATS_PAD);
2385e111210SEelco Chaudron }
2395e111210SEelco Chaudron return 0;
2405e111210SEelco Chaudron }
2415e111210SEelco Chaudron
2421da177e4SLinus Torvalds /**
2431da177e4SLinus Torvalds * gnet_stats_copy_basic - copy basic statistics into statistic TLV
2441da177e4SLinus Torvalds * @d: dumping handle
245b002fdccSLuis de Bethencourt * @cpu: copy statistic per cpu
2461da177e4SLinus Torvalds * @b: basic statistics
24729cbcd85SAhmed S. Darwish * @running: true if @b represents a running qdisc, thus @b's
24829cbcd85SAhmed S. Darwish * internal values might change during basic reads.
24929cbcd85SAhmed S. Darwish * Only used if @cpu is NULL
25029cbcd85SAhmed S. Darwish *
25129cbcd85SAhmed S. Darwish * Context: task; must not be run from IRQ or BH contexts
2521da177e4SLinus Torvalds *
2531da177e4SLinus Torvalds * Appends the basic statistics to the top level TLV created by
2541da177e4SLinus Torvalds * gnet_stats_start_copy().
2551da177e4SLinus Torvalds *
2561da177e4SLinus Torvalds * Returns 0 on success or -1 with the statistic lock released
2571da177e4SLinus Torvalds * if the room in the socket buffer was not sufficient.
2581da177e4SLinus Torvalds */
2591da177e4SLinus Torvalds int
gnet_stats_copy_basic(struct gnet_dump * d,struct gnet_stats_basic_sync __percpu * cpu,struct gnet_stats_basic_sync * b,bool running)26029cbcd85SAhmed S. Darwish gnet_stats_copy_basic(struct gnet_dump *d,
26150dc9a85SAhmed S. Darwish struct gnet_stats_basic_sync __percpu *cpu,
26229cbcd85SAhmed S. Darwish struct gnet_stats_basic_sync *b,
26329cbcd85SAhmed S. Darwish bool running)
2641da177e4SLinus Torvalds {
26529cbcd85SAhmed S. Darwish return ___gnet_stats_copy_basic(d, cpu, b, TCA_STATS_BASIC, running);
2661da177e4SLinus Torvalds }
2679e34a5b5SEric Dumazet EXPORT_SYMBOL(gnet_stats_copy_basic);
2681da177e4SLinus Torvalds
2691da177e4SLinus Torvalds /**
2705e111210SEelco Chaudron * gnet_stats_copy_basic_hw - copy basic hw statistics into statistic TLV
2715e111210SEelco Chaudron * @d: dumping handle
2725e111210SEelco Chaudron * @cpu: copy statistic per cpu
2735e111210SEelco Chaudron * @b: basic statistics
27429cbcd85SAhmed S. Darwish * @running: true if @b represents a running qdisc, thus @b's
27529cbcd85SAhmed S. Darwish * internal values might change during basic reads.
27629cbcd85SAhmed S. Darwish * Only used if @cpu is NULL
27729cbcd85SAhmed S. Darwish *
27829cbcd85SAhmed S. Darwish * Context: task; must not be run from IRQ or BH contexts
2795e111210SEelco Chaudron *
2805e111210SEelco Chaudron * Appends the basic statistics to the top level TLV created by
2815e111210SEelco Chaudron * gnet_stats_start_copy().
2825e111210SEelco Chaudron *
2835e111210SEelco Chaudron * Returns 0 on success or -1 with the statistic lock released
2845e111210SEelco Chaudron * if the room in the socket buffer was not sufficient.
2855e111210SEelco Chaudron */
2865e111210SEelco Chaudron int
gnet_stats_copy_basic_hw(struct gnet_dump * d,struct gnet_stats_basic_sync __percpu * cpu,struct gnet_stats_basic_sync * b,bool running)28729cbcd85SAhmed S. Darwish gnet_stats_copy_basic_hw(struct gnet_dump *d,
28850dc9a85SAhmed S. Darwish struct gnet_stats_basic_sync __percpu *cpu,
28929cbcd85SAhmed S. Darwish struct gnet_stats_basic_sync *b,
29029cbcd85SAhmed S. Darwish bool running)
2915e111210SEelco Chaudron {
29229cbcd85SAhmed S. Darwish return ___gnet_stats_copy_basic(d, cpu, b, TCA_STATS_BASIC_HW, running);
2935e111210SEelco Chaudron }
2945e111210SEelco Chaudron EXPORT_SYMBOL(gnet_stats_copy_basic_hw);
2955e111210SEelco Chaudron
2965e111210SEelco Chaudron /**
2971da177e4SLinus Torvalds * gnet_stats_copy_rate_est - copy rate estimator statistics into statistics TLV
2981da177e4SLinus Torvalds * @d: dumping handle
2991c0d32fdSEric Dumazet * @rate_est: rate estimator
3001da177e4SLinus Torvalds *
3011da177e4SLinus Torvalds * Appends the rate estimator statistics to the top level TLV created by
3021da177e4SLinus Torvalds * gnet_stats_start_copy().
3031da177e4SLinus Torvalds *
3041da177e4SLinus Torvalds * Returns 0 on success or -1 with the statistic lock released
3051da177e4SLinus Torvalds * if the room in the socket buffer was not sufficient.
3061da177e4SLinus Torvalds */
3071da177e4SLinus Torvalds int
gnet_stats_copy_rate_est(struct gnet_dump * d,struct net_rate_estimator __rcu ** rate_est)308d250a5f9SEric Dumazet gnet_stats_copy_rate_est(struct gnet_dump *d,
3091c0d32fdSEric Dumazet struct net_rate_estimator __rcu **rate_est)
3101da177e4SLinus Torvalds {
3111c0d32fdSEric Dumazet struct gnet_stats_rate_est64 sample;
31245203a3bSEric Dumazet struct gnet_stats_rate_est est;
31345203a3bSEric Dumazet int res;
31445203a3bSEric Dumazet
3151c0d32fdSEric Dumazet if (!gen_estimator_read(rate_est, &sample))
316d250a5f9SEric Dumazet return 0;
3171c0d32fdSEric Dumazet est.bps = min_t(u64, UINT_MAX, sample.bps);
31845203a3bSEric Dumazet /* we have some time before reaching 2^32 packets per second */
3191c0d32fdSEric Dumazet est.pps = sample.pps;
32045203a3bSEric Dumazet
3211da177e4SLinus Torvalds if (d->compat_tc_stats) {
32245203a3bSEric Dumazet d->tc_stats.bps = est.bps;
32345203a3bSEric Dumazet d->tc_stats.pps = est.pps;
3241da177e4SLinus Torvalds }
3251da177e4SLinus Torvalds
32645203a3bSEric Dumazet if (d->tail) {
3279854518eSNicolas Dichtel res = gnet_stats_copy(d, TCA_STATS_RATE_EST, &est, sizeof(est),
3289854518eSNicolas Dichtel TCA_STATS_PAD);
3291c0d32fdSEric Dumazet if (res < 0 || est.bps == sample.bps)
33045203a3bSEric Dumazet return res;
33145203a3bSEric Dumazet /* emit 64bit stats only if needed */
3321c0d32fdSEric Dumazet return gnet_stats_copy(d, TCA_STATS_RATE_EST64, &sample,
3331c0d32fdSEric Dumazet sizeof(sample), TCA_STATS_PAD);
33445203a3bSEric Dumazet }
3351da177e4SLinus Torvalds
3361da177e4SLinus Torvalds return 0;
3371da177e4SLinus Torvalds }
3389e34a5b5SEric Dumazet EXPORT_SYMBOL(gnet_stats_copy_rate_est);
3391da177e4SLinus Torvalds
gnet_stats_add_queue_cpu(struct gnet_stats_queue * qstats,const struct gnet_stats_queue __percpu * q)340448e163fSSebastian Andrzej Siewior static void gnet_stats_add_queue_cpu(struct gnet_stats_queue *qstats,
341448e163fSSebastian Andrzej Siewior const struct gnet_stats_queue __percpu *q)
342448e163fSSebastian Andrzej Siewior {
343448e163fSSebastian Andrzej Siewior int i;
344448e163fSSebastian Andrzej Siewior
345448e163fSSebastian Andrzej Siewior for_each_possible_cpu(i) {
346448e163fSSebastian Andrzej Siewior const struct gnet_stats_queue *qcpu = per_cpu_ptr(q, i);
347448e163fSSebastian Andrzej Siewior
348de64b6b6SZhengchao Shao qstats->qlen += qcpu->qlen;
349448e163fSSebastian Andrzej Siewior qstats->backlog += qcpu->backlog;
350448e163fSSebastian Andrzej Siewior qstats->drops += qcpu->drops;
351448e163fSSebastian Andrzej Siewior qstats->requeues += qcpu->requeues;
352448e163fSSebastian Andrzej Siewior qstats->overlimits += qcpu->overlimits;
353448e163fSSebastian Andrzej Siewior }
354448e163fSSebastian Andrzej Siewior }
355448e163fSSebastian Andrzej Siewior
gnet_stats_add_queue(struct gnet_stats_queue * qstats,const struct gnet_stats_queue __percpu * cpu,const struct gnet_stats_queue * q)356448e163fSSebastian Andrzej Siewior void gnet_stats_add_queue(struct gnet_stats_queue *qstats,
357448e163fSSebastian Andrzej Siewior const struct gnet_stats_queue __percpu *cpu,
358448e163fSSebastian Andrzej Siewior const struct gnet_stats_queue *q)
359448e163fSSebastian Andrzej Siewior {
360448e163fSSebastian Andrzej Siewior if (cpu) {
361448e163fSSebastian Andrzej Siewior gnet_stats_add_queue_cpu(qstats, cpu);
362448e163fSSebastian Andrzej Siewior } else {
363448e163fSSebastian Andrzej Siewior qstats->qlen += q->qlen;
364448e163fSSebastian Andrzej Siewior qstats->backlog += q->backlog;
365448e163fSSebastian Andrzej Siewior qstats->drops += q->drops;
366448e163fSSebastian Andrzej Siewior qstats->requeues += q->requeues;
367448e163fSSebastian Andrzej Siewior qstats->overlimits += q->overlimits;
368448e163fSSebastian Andrzej Siewior }
369448e163fSSebastian Andrzej Siewior }
370448e163fSSebastian Andrzej Siewior EXPORT_SYMBOL(gnet_stats_add_queue);
371448e163fSSebastian Andrzej Siewior
3721da177e4SLinus Torvalds /**
3731da177e4SLinus Torvalds * gnet_stats_copy_queue - copy queue statistics into statistics TLV
3741da177e4SLinus Torvalds * @d: dumping handle
375b0ab6f92SJohn Fastabend * @cpu_q: per cpu queue statistics
3761da177e4SLinus Torvalds * @q: queue statistics
37764015853SJohn Fastabend * @qlen: queue length statistics
3781da177e4SLinus Torvalds *
3791da177e4SLinus Torvalds * Appends the queue statistics to the top level TLV created by
380b0ab6f92SJohn Fastabend * gnet_stats_start_copy(). Using per cpu queue statistics if
381b0ab6f92SJohn Fastabend * they are available.
3821da177e4SLinus Torvalds *
3831da177e4SLinus Torvalds * Returns 0 on success or -1 with the statistic lock released
3841da177e4SLinus Torvalds * if the room in the socket buffer was not sufficient.
3851da177e4SLinus Torvalds */
3861da177e4SLinus Torvalds int
gnet_stats_copy_queue(struct gnet_dump * d,struct gnet_stats_queue __percpu * cpu_q,struct gnet_stats_queue * q,__u32 qlen)38764015853SJohn Fastabend gnet_stats_copy_queue(struct gnet_dump *d,
388b0ab6f92SJohn Fastabend struct gnet_stats_queue __percpu *cpu_q,
38964015853SJohn Fastabend struct gnet_stats_queue *q, __u32 qlen)
3901da177e4SLinus Torvalds {
391b0ab6f92SJohn Fastabend struct gnet_stats_queue qstats = {0};
392b0ab6f92SJohn Fastabend
39310940eb7SSebastian Andrzej Siewior gnet_stats_add_queue(&qstats, cpu_q, q);
39410940eb7SSebastian Andrzej Siewior qstats.qlen = qlen;
39564015853SJohn Fastabend
3961da177e4SLinus Torvalds if (d->compat_tc_stats) {
397b0ab6f92SJohn Fastabend d->tc_stats.drops = qstats.drops;
398b0ab6f92SJohn Fastabend d->tc_stats.qlen = qstats.qlen;
399b0ab6f92SJohn Fastabend d->tc_stats.backlog = qstats.backlog;
400b0ab6f92SJohn Fastabend d->tc_stats.overlimits = qstats.overlimits;
4011da177e4SLinus Torvalds }
4021da177e4SLinus Torvalds
4031da177e4SLinus Torvalds if (d->tail)
404b0ab6f92SJohn Fastabend return gnet_stats_copy(d, TCA_STATS_QUEUE,
4059854518eSNicolas Dichtel &qstats, sizeof(qstats),
4069854518eSNicolas Dichtel TCA_STATS_PAD);
4071da177e4SLinus Torvalds
4081da177e4SLinus Torvalds return 0;
4091da177e4SLinus Torvalds }
4109e34a5b5SEric Dumazet EXPORT_SYMBOL(gnet_stats_copy_queue);
4111da177e4SLinus Torvalds
4121da177e4SLinus Torvalds /**
4131da177e4SLinus Torvalds * gnet_stats_copy_app - copy application specific statistics into statistics TLV
4141da177e4SLinus Torvalds * @d: dumping handle
4151da177e4SLinus Torvalds * @st: application specific statistics data
4161da177e4SLinus Torvalds * @len: length of data
4171da177e4SLinus Torvalds *
418e793c0f7SMasanari Iida * Appends the application specific statistics to the top level TLV created by
4191da177e4SLinus Torvalds * gnet_stats_start_copy() and remembers the data for XSTATS if the dumping
4201da177e4SLinus Torvalds * handle is in backward compatibility mode.
4211da177e4SLinus Torvalds *
4221da177e4SLinus Torvalds * Returns 0 on success or -1 with the statistic lock released
4231da177e4SLinus Torvalds * if the room in the socket buffer was not sufficient.
4241da177e4SLinus Torvalds */
4251da177e4SLinus Torvalds int
gnet_stats_copy_app(struct gnet_dump * d,void * st,int len)4261da177e4SLinus Torvalds gnet_stats_copy_app(struct gnet_dump *d, void *st, int len)
4271da177e4SLinus Torvalds {
4281da177e4SLinus Torvalds if (d->compat_xstats) {
4291c4cff0cSIgnacy Gawędzki d->xstats = kmemdup(st, len, GFP_ATOMIC);
4301c4cff0cSIgnacy Gawędzki if (!d->xstats)
4311c4cff0cSIgnacy Gawędzki goto err_out;
4321da177e4SLinus Torvalds d->xstats_len = len;
4331da177e4SLinus Torvalds }
4341da177e4SLinus Torvalds
4351da177e4SLinus Torvalds if (d->tail)
4369854518eSNicolas Dichtel return gnet_stats_copy(d, TCA_STATS_APP, st, len,
4379854518eSNicolas Dichtel TCA_STATS_PAD);
4381da177e4SLinus Torvalds
4391da177e4SLinus Torvalds return 0;
4401c4cff0cSIgnacy Gawędzki
4411c4cff0cSIgnacy Gawędzki err_out:
442edb09eb1SEric Dumazet if (d->lock)
4431c4cff0cSIgnacy Gawędzki spin_unlock_bh(d->lock);
444edb09eb1SEric Dumazet d->xstats_len = 0;
4451c4cff0cSIgnacy Gawędzki return -1;
4461da177e4SLinus Torvalds }
4479e34a5b5SEric Dumazet EXPORT_SYMBOL(gnet_stats_copy_app);
4481da177e4SLinus Torvalds
4491da177e4SLinus Torvalds /**
4501da177e4SLinus Torvalds * gnet_stats_finish_copy - finish dumping procedure
4511da177e4SLinus Torvalds * @d: dumping handle
4521da177e4SLinus Torvalds *
4531da177e4SLinus Torvalds * Corrects the length of the top level TLV to include all TLVs added
4541da177e4SLinus Torvalds * by gnet_stats_copy_XXX() calls. Adds the backward compatibility TLVs
4551da177e4SLinus Torvalds * if gnet_stats_start_copy_compat() was used and releases the statistics
4561da177e4SLinus Torvalds * lock.
4571da177e4SLinus Torvalds *
4581da177e4SLinus Torvalds * Returns 0 on success or -1 with the statistic lock released
4591da177e4SLinus Torvalds * if the room in the socket buffer was not sufficient.
4601da177e4SLinus Torvalds */
4611da177e4SLinus Torvalds int
gnet_stats_finish_copy(struct gnet_dump * d)4621da177e4SLinus Torvalds gnet_stats_finish_copy(struct gnet_dump *d)
4631da177e4SLinus Torvalds {
4641da177e4SLinus Torvalds if (d->tail)
4651e90474cSPatrick McHardy d->tail->nla_len = skb_tail_pointer(d->skb) - (u8 *)d->tail;
4661da177e4SLinus Torvalds
4671da177e4SLinus Torvalds if (d->compat_tc_stats)
4681da177e4SLinus Torvalds if (gnet_stats_copy(d, d->compat_tc_stats, &d->tc_stats,
4699854518eSNicolas Dichtel sizeof(d->tc_stats), d->padattr) < 0)
4701da177e4SLinus Torvalds return -1;
4711da177e4SLinus Torvalds
4721da177e4SLinus Torvalds if (d->compat_xstats && d->xstats) {
4731da177e4SLinus Torvalds if (gnet_stats_copy(d, d->compat_xstats, d->xstats,
4749854518eSNicolas Dichtel d->xstats_len, d->padattr) < 0)
4751da177e4SLinus Torvalds return -1;
4761da177e4SLinus Torvalds }
4771da177e4SLinus Torvalds
478edb09eb1SEric Dumazet if (d->lock)
479edb09eb1SEric Dumazet spin_unlock_bh(d->lock);
4801c4cff0cSIgnacy Gawędzki kfree(d->xstats);
4811c4cff0cSIgnacy Gawędzki d->xstats = NULL;
4821c4cff0cSIgnacy Gawędzki d->xstats_len = 0;
4831da177e4SLinus Torvalds return 0;
4841da177e4SLinus Torvalds }
4851da177e4SLinus Torvalds EXPORT_SYMBOL(gnet_stats_finish_copy);
486