• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (c) PLUMgrid, Inc.
2// Licensed under the Apache License, Version 2.0 (the "License")
3struct Ptr {
4  u64 ptr:64;
5};
6struct Counters {
7  u64 stat1:64;
8  u64 stat2:64;
9};
10Table<Ptr, Counters, FIXED_MATCH, AUTO> stats(1024);
11
12// example with on_valid syntax
13u32 sys_wr (struct proto::pt_regs *ctx) {
14  struct Ptr key = {.ptr=ctx->di};
15  struct Counters *leaf;
16  leaf = stats[key];
17  if leaf {
18    atomic_add(leaf->stat2, 1);
19  }
20  log("sys_wr: %p\n", ctx->di);
21  return 0;
22}
23
24// example with smallest available syntax
25// note: if stats[key] fails, program returns early
26u32 sys_rd (struct proto::pt_regs *ctx) {
27  struct Ptr key = {.ptr=ctx->di};
28  atomic_add(stats[key].stat1, 1);
29}
30
31// example with if/else case
32u32 sys_bpf (struct proto::pt_regs *ctx) {
33  struct Ptr key = {.ptr=ctx->di};
34  struct Counters *leaf;
35  leaf = stats[key];
36  if leaf {
37    atomic_add(leaf->stat1, 1);
38  } else {
39    log("update %llx failed\n", ctx->di);
40  }
41  return 0;
42}
43
44