Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v4.19-rc3 308 lines 7.7 kB view raw
1/* 2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net> 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 20struct nft_counter { 21 s64 bytes; 22 s64 packets; 23}; 24 25struct nft_counter_percpu_priv { 26 struct nft_counter __percpu *counter; 27}; 28 29static DEFINE_PER_CPU(seqcount_t, nft_counter_seq); 30 31static inline void nft_counter_do_eval(struct nft_counter_percpu_priv *priv, 32 struct nft_regs *regs, 33 const struct nft_pktinfo *pkt) 34{ 35 struct nft_counter *this_cpu; 36 seqcount_t *myseq; 37 38 local_bh_disable(); 39 this_cpu = this_cpu_ptr(priv->counter); 40 myseq = this_cpu_ptr(&nft_counter_seq); 41 42 write_seqcount_begin(myseq); 43 44 this_cpu->bytes += pkt->skb->len; 45 this_cpu->packets++; 46 47 write_seqcount_end(myseq); 48 local_bh_enable(); 49} 50 51static inline void nft_counter_obj_eval(struct nft_object *obj, 52 struct nft_regs *regs, 53 const struct nft_pktinfo *pkt) 54{ 55 struct nft_counter_percpu_priv *priv = nft_obj_data(obj); 56 57 nft_counter_do_eval(priv, regs, pkt); 58} 59 60static int nft_counter_do_init(const struct nlattr * const tb[], 61 struct nft_counter_percpu_priv *priv) 62{ 63 struct nft_counter __percpu *cpu_stats; 64 struct nft_counter *this_cpu; 65 66 cpu_stats = alloc_percpu(struct nft_counter); 67 if (cpu_stats == NULL) 68 return -ENOMEM; 69 70 preempt_disable(); 71 this_cpu = this_cpu_ptr(cpu_stats); 72 if (tb[NFTA_COUNTER_PACKETS]) { 73 this_cpu->packets = 74 be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS])); 75 } 76 if (tb[NFTA_COUNTER_BYTES]) { 77 this_cpu->bytes = 78 be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES])); 79 } 80 preempt_enable(); 81 priv->counter = cpu_stats; 82 return 0; 83} 84 85static int nft_counter_obj_init(const struct nft_ctx *ctx, 86 const struct nlattr * const tb[], 87 struct nft_object *obj) 88{ 89 struct nft_counter_percpu_priv *priv = nft_obj_data(obj); 90 91 return nft_counter_do_init(tb, priv); 92} 93 94static void nft_counter_do_destroy(struct nft_counter_percpu_priv *priv) 95{ 96 free_percpu(priv->counter); 97} 98 99static void nft_counter_obj_destroy(const struct nft_ctx *ctx, 100 struct nft_object *obj) 101{ 102 struct nft_counter_percpu_priv *priv = nft_obj_data(obj); 103 104 nft_counter_do_destroy(priv); 105} 106 107static void nft_counter_reset(struct nft_counter_percpu_priv __percpu *priv, 108 struct nft_counter *total) 109{ 110 struct nft_counter *this_cpu; 111 112 local_bh_disable(); 113 this_cpu = this_cpu_ptr(priv->counter); 114 this_cpu->packets -= total->packets; 115 this_cpu->bytes -= total->bytes; 116 local_bh_enable(); 117} 118 119static void nft_counter_fetch(struct nft_counter_percpu_priv *priv, 120 struct nft_counter *total) 121{ 122 struct nft_counter *this_cpu; 123 const seqcount_t *myseq; 124 u64 bytes, packets; 125 unsigned int seq; 126 int cpu; 127 128 memset(total, 0, sizeof(*total)); 129 for_each_possible_cpu(cpu) { 130 myseq = per_cpu_ptr(&nft_counter_seq, cpu); 131 this_cpu = per_cpu_ptr(priv->counter, cpu); 132 do { 133 seq = read_seqcount_begin(myseq); 134 bytes = this_cpu->bytes; 135 packets = this_cpu->packets; 136 } while (read_seqcount_retry(myseq, seq)); 137 138 total->bytes += bytes; 139 total->packets += packets; 140 } 141} 142 143static int nft_counter_do_dump(struct sk_buff *skb, 144 struct nft_counter_percpu_priv *priv, 145 bool reset) 146{ 147 struct nft_counter total; 148 149 nft_counter_fetch(priv, &total); 150 151 if (nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes), 152 NFTA_COUNTER_PAD) || 153 nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.packets), 154 NFTA_COUNTER_PAD)) 155 goto nla_put_failure; 156 157 if (reset) 158 nft_counter_reset(priv, &total); 159 160 return 0; 161 162nla_put_failure: 163 return -1; 164} 165 166static int nft_counter_obj_dump(struct sk_buff *skb, 167 struct nft_object *obj, bool reset) 168{ 169 struct nft_counter_percpu_priv *priv = nft_obj_data(obj); 170 171 return nft_counter_do_dump(skb, priv, reset); 172} 173 174static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = { 175 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 }, 176 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 }, 177}; 178 179static struct nft_object_type nft_counter_obj_type; 180static const struct nft_object_ops nft_counter_obj_ops = { 181 .type = &nft_counter_obj_type, 182 .size = sizeof(struct nft_counter_percpu_priv), 183 .eval = nft_counter_obj_eval, 184 .init = nft_counter_obj_init, 185 .destroy = nft_counter_obj_destroy, 186 .dump = nft_counter_obj_dump, 187}; 188 189static struct nft_object_type nft_counter_obj_type __read_mostly = { 190 .type = NFT_OBJECT_COUNTER, 191 .ops = &nft_counter_obj_ops, 192 .maxattr = NFTA_COUNTER_MAX, 193 .policy = nft_counter_policy, 194 .owner = THIS_MODULE, 195}; 196 197static void nft_counter_eval(const struct nft_expr *expr, 198 struct nft_regs *regs, 199 const struct nft_pktinfo *pkt) 200{ 201 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr); 202 203 nft_counter_do_eval(priv, regs, pkt); 204} 205 206static int nft_counter_dump(struct sk_buff *skb, const struct nft_expr *expr) 207{ 208 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr); 209 210 return nft_counter_do_dump(skb, priv, false); 211} 212 213static int nft_counter_init(const struct nft_ctx *ctx, 214 const struct nft_expr *expr, 215 const struct nlattr * const tb[]) 216{ 217 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr); 218 219 return nft_counter_do_init(tb, priv); 220} 221 222static void nft_counter_destroy(const struct nft_ctx *ctx, 223 const struct nft_expr *expr) 224{ 225 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr); 226 227 nft_counter_do_destroy(priv); 228} 229 230static int nft_counter_clone(struct nft_expr *dst, const struct nft_expr *src) 231{ 232 struct nft_counter_percpu_priv *priv = nft_expr_priv(src); 233 struct nft_counter_percpu_priv *priv_clone = nft_expr_priv(dst); 234 struct nft_counter __percpu *cpu_stats; 235 struct nft_counter *this_cpu; 236 struct nft_counter total; 237 238 nft_counter_fetch(priv, &total); 239 240 cpu_stats = alloc_percpu_gfp(struct nft_counter, GFP_ATOMIC); 241 if (cpu_stats == NULL) 242 return -ENOMEM; 243 244 preempt_disable(); 245 this_cpu = this_cpu_ptr(cpu_stats); 246 this_cpu->packets = total.packets; 247 this_cpu->bytes = total.bytes; 248 preempt_enable(); 249 250 priv_clone->counter = cpu_stats; 251 return 0; 252} 253 254static struct nft_expr_type nft_counter_type; 255static const struct nft_expr_ops nft_counter_ops = { 256 .type = &nft_counter_type, 257 .size = NFT_EXPR_SIZE(sizeof(struct nft_counter_percpu_priv)), 258 .eval = nft_counter_eval, 259 .init = nft_counter_init, 260 .destroy = nft_counter_destroy, 261 .destroy_clone = nft_counter_destroy, 262 .dump = nft_counter_dump, 263 .clone = nft_counter_clone, 264}; 265 266static struct nft_expr_type nft_counter_type __read_mostly = { 267 .name = "counter", 268 .ops = &nft_counter_ops, 269 .policy = nft_counter_policy, 270 .maxattr = NFTA_COUNTER_MAX, 271 .flags = NFT_EXPR_STATEFUL, 272 .owner = THIS_MODULE, 273}; 274 275static int __init nft_counter_module_init(void) 276{ 277 int cpu, err; 278 279 for_each_possible_cpu(cpu) 280 seqcount_init(per_cpu_ptr(&nft_counter_seq, cpu)); 281 282 err = nft_register_obj(&nft_counter_obj_type); 283 if (err < 0) 284 return err; 285 286 err = nft_register_expr(&nft_counter_type); 287 if (err < 0) 288 goto err1; 289 290 return 0; 291err1: 292 nft_unregister_obj(&nft_counter_obj_type); 293 return err; 294} 295 296static void __exit nft_counter_module_exit(void) 297{ 298 nft_unregister_expr(&nft_counter_type); 299 nft_unregister_obj(&nft_counter_obj_type); 300} 301 302module_init(nft_counter_module_init); 303module_exit(nft_counter_module_exit); 304 305MODULE_LICENSE("GPL"); 306MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); 307MODULE_ALIAS_NFT_EXPR("counter"); 308MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_COUNTER);