root/tools/testing/selftests/bpf/progs/test_map_lock.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. SEC

   1 // SPDX-License-Identifier: GPL-2.0
   2 // Copyright (c) 2019 Facebook
   3 #include <linux/bpf.h>
   4 #include <linux/version.h>
   5 #include "bpf_helpers.h"
   6 
   7 #define VAR_NUM 16
   8 
   9 struct hmap_elem {
  10         struct bpf_spin_lock lock;
  11         int var[VAR_NUM];
  12 };
  13 
  14 struct {
  15         __uint(type, BPF_MAP_TYPE_HASH);
  16         __uint(max_entries, 1);
  17         __type(key, __u32);
  18         __type(value, struct hmap_elem);
  19 } hash_map SEC(".maps");
  20 
  21 struct array_elem {
  22         struct bpf_spin_lock lock;
  23         int var[VAR_NUM];
  24 };
  25 
  26 struct {
  27         __uint(type, BPF_MAP_TYPE_ARRAY);
  28         __uint(max_entries, 1);
  29         __type(key, int);
  30         __type(value, struct array_elem);
  31 } array_map SEC(".maps");
  32 
  33 SEC("map_lock_demo")
  34 int bpf_map_lock_test(struct __sk_buff *skb)
  35 {
  36         struct hmap_elem zero = {}, *val;
  37         int rnd = bpf_get_prandom_u32();
  38         int key = 0, err = 1, i;
  39         struct array_elem *q;
  40 
  41         val = bpf_map_lookup_elem(&hash_map, &key);
  42         if (!val)
  43                 goto err;
  44         /* spin_lock in hash map */
  45         bpf_spin_lock(&val->lock);
  46         for (i = 0; i < VAR_NUM; i++)
  47                 val->var[i] = rnd;
  48         bpf_spin_unlock(&val->lock);
  49 
  50         /* spin_lock in array */
  51         q = bpf_map_lookup_elem(&array_map, &key);
  52         if (!q)
  53                 goto err;
  54         bpf_spin_lock(&q->lock);
  55         for (i = 0; i < VAR_NUM; i++)
  56                 q->var[i] = rnd;
  57         bpf_spin_unlock(&q->lock);
  58         err = 0;
  59 err:
  60         return err;
  61 }
  62 char _license[] SEC("license") = "GPL";

/* [<][>][^][v][top][bottom][index][help] */