]>
Commit | Line | Data |
---|---|---|
96518518 | 1 | /* |
ef1f7df9 | 2 | * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net> |
96518518 PM |
3 | * |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License version 2 as | |
6 | * published by the Free Software Foundation. | |
7 | * | |
8 | * Development of this code funded by Astaro AG (http://www.astaro.com/) | |
9 | */ | |
10 | ||
11 | #include <linux/kernel.h> | |
12 | #include <linux/init.h> | |
13 | #include <linux/module.h> | |
14 | #include <linux/seqlock.h> | |
15 | #include <linux/netlink.h> | |
16 | #include <linux/netfilter.h> | |
17 | #include <linux/netfilter/nf_tables.h> | |
18 | #include <net/netfilter/nf_tables.h> | |
19 | ||
20 | struct nft_counter { | |
96518518 PM |
21 | u64 bytes; |
22 | u64 packets; | |
23 | }; | |
24 | ||
0c45e769 PNA |
25 | struct nft_counter_percpu { |
26 | struct nft_counter counter; | |
27 | struct u64_stats_sync syncp; | |
28 | }; | |
29 | ||
30 | struct nft_counter_percpu_priv { | |
31 | struct nft_counter_percpu __percpu *counter; | |
32 | }; | |
33 | ||
96518518 | 34 | static void nft_counter_eval(const struct nft_expr *expr, |
a55e22e9 | 35 | struct nft_regs *regs, |
96518518 PM |
36 | const struct nft_pktinfo *pkt) |
37 | { | |
0c45e769 PNA |
38 | struct nft_counter_percpu_priv *priv = nft_expr_priv(expr); |
39 | struct nft_counter_percpu *this_cpu; | |
40 | ||
41 | local_bh_disable(); | |
42 | this_cpu = this_cpu_ptr(priv->counter); | |
43 | u64_stats_update_begin(&this_cpu->syncp); | |
44 | this_cpu->counter.bytes += pkt->skb->len; | |
45 | this_cpu->counter.packets++; | |
46 | u64_stats_update_end(&this_cpu->syncp); | |
47 | local_bh_enable(); | |
96518518 PM |
48 | } |
49 | ||
50 | static int nft_counter_dump(struct sk_buff *skb, const struct nft_expr *expr) | |
51 | { | |
0c45e769 PNA |
52 | struct nft_counter_percpu_priv *priv = nft_expr_priv(expr); |
53 | struct nft_counter_percpu *cpu_stats; | |
54 | struct nft_counter total; | |
55 | u64 bytes, packets; | |
96518518 | 56 | unsigned int seq; |
0c45e769 PNA |
57 | int cpu; |
58 | ||
59 | memset(&total, 0, sizeof(total)); | |
60 | for_each_possible_cpu(cpu) { | |
61 | cpu_stats = per_cpu_ptr(priv->counter, cpu); | |
62 | do { | |
63 | seq = u64_stats_fetch_begin_irq(&cpu_stats->syncp); | |
64 | bytes = cpu_stats->counter.bytes; | |
65 | packets = cpu_stats->counter.packets; | |
66 | } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, seq)); | |
67 | ||
68 | total.packets += packets; | |
69 | total.bytes += bytes; | |
70 | } | |
71 | ||
72 | if (nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes)) || | |
73 | nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.packets))) | |
96518518 PM |
74 | goto nla_put_failure; |
75 | return 0; | |
76 | ||
77 | nla_put_failure: | |
78 | return -1; | |
79 | } | |
80 | ||
81 | static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = { | |
82 | [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 }, | |
83 | [NFTA_COUNTER_BYTES] = { .type = NLA_U64 }, | |
84 | }; | |
85 | ||
86 | static int nft_counter_init(const struct nft_ctx *ctx, | |
87 | const struct nft_expr *expr, | |
88 | const struct nlattr * const tb[]) | |
89 | { | |
0c45e769 PNA |
90 | struct nft_counter_percpu_priv *priv = nft_expr_priv(expr); |
91 | struct nft_counter_percpu __percpu *cpu_stats; | |
92 | struct nft_counter_percpu *this_cpu; | |
93 | ||
94 | cpu_stats = netdev_alloc_pcpu_stats(struct nft_counter_percpu); | |
95 | if (cpu_stats == NULL) | |
96 | return ENOMEM; | |
97 | ||
98 | preempt_disable(); | |
99 | this_cpu = this_cpu_ptr(cpu_stats); | |
100 | if (tb[NFTA_COUNTER_PACKETS]) { | |
101 | this_cpu->counter.packets = | |
102 | be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS])); | |
103 | } | |
104 | if (tb[NFTA_COUNTER_BYTES]) { | |
105 | this_cpu->counter.bytes = | |
106 | be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES])); | |
107 | } | |
108 | preempt_enable(); | |
109 | priv->counter = cpu_stats; | |
110 | return 0; | |
111 | } | |
96518518 | 112 | |
0c45e769 PNA |
113 | static void nft_counter_destroy(const struct nft_ctx *ctx, |
114 | const struct nft_expr *expr) | |
115 | { | |
116 | struct nft_counter_percpu_priv *priv = nft_expr_priv(expr); | |
96518518 | 117 | |
0c45e769 | 118 | free_percpu(priv->counter); |
96518518 PM |
119 | } |
120 | ||
ef1f7df9 PM |
121 | static struct nft_expr_type nft_counter_type; |
122 | static const struct nft_expr_ops nft_counter_ops = { | |
123 | .type = &nft_counter_type, | |
0c45e769 | 124 | .size = NFT_EXPR_SIZE(sizeof(struct nft_counter_percpu_priv)), |
96518518 PM |
125 | .eval = nft_counter_eval, |
126 | .init = nft_counter_init, | |
0c45e769 | 127 | .destroy = nft_counter_destroy, |
96518518 PM |
128 | .dump = nft_counter_dump, |
129 | }; | |
130 | ||
ef1f7df9 PM |
131 | static struct nft_expr_type nft_counter_type __read_mostly = { |
132 | .name = "counter", | |
133 | .ops = &nft_counter_ops, | |
134 | .policy = nft_counter_policy, | |
135 | .maxattr = NFTA_COUNTER_MAX, | |
151d799a | 136 | .flags = NFT_EXPR_STATEFUL, |
ef1f7df9 PM |
137 | .owner = THIS_MODULE, |
138 | }; | |
139 | ||
96518518 PM |
140 | static int __init nft_counter_module_init(void) |
141 | { | |
ef1f7df9 | 142 | return nft_register_expr(&nft_counter_type); |
96518518 PM |
143 | } |
144 | ||
145 | static void __exit nft_counter_module_exit(void) | |
146 | { | |
ef1f7df9 | 147 | nft_unregister_expr(&nft_counter_type); |
96518518 PM |
148 | } |
149 | ||
150 | module_init(nft_counter_module_init); | |
151 | module_exit(nft_counter_module_exit); | |
152 | ||
153 | MODULE_LICENSE("GPL"); | |
154 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | |
155 | MODULE_ALIAS_NFT_EXPR("counter"); |