• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2018 Politecnico di Torino
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "BPF.h"
18 #include "catch.hpp"
19 
20 const std::string BPF_PROGRAM1 = R"(
21 BPF_TABLE_SHARED("array", int, int, mysharedtable, 1024);
22 )";
23 
24 const std::string BPF_PROGRAM2 = R"(
25 BPF_TABLE("extern", int, int, mysharedtable, 1024);
26 )";
27 
28 TEST_CASE("test shared table", "[shared_table]") {
29   // deploy 4 ebpf programs: _ns1_a and _ns1_b are in ns1, _ns2_a and _ns2_b in ns2
30   ebpf::BPF bpf_ns1_a(0, nullptr, false, "ns1");
31   ebpf::BPF bpf_ns1_b(0, nullptr, false, "ns1");
32   ebpf::BPF bpf_ns2_a(0, nullptr, false, "ns2");
33   ebpf::BPF bpf_ns2_b(0, nullptr, false, "ns2");
34 
35   ebpf::StatusTuple res(0);
36 
37   res = bpf_ns1_a.init(BPF_PROGRAM1);
38   REQUIRE(res.code() == 0);
39 
40   res = bpf_ns1_b.init(BPF_PROGRAM2);
41   REQUIRE(res.code() == 0);
42 
43   res = bpf_ns2_a.init(BPF_PROGRAM1);
44   REQUIRE(res.code() == 0);
45 
46   res = bpf_ns2_b.init(BPF_PROGRAM2);
47   REQUIRE(res.code() == 0);
48 
49   // get references to all tables
50   ebpf::BPFArrayTable<int> t_ns1_a = bpf_ns1_a.get_array_table<int>("mysharedtable");
51   ebpf::BPFArrayTable<int> t_ns1_b = bpf_ns1_b.get_array_table<int>("mysharedtable");
52   ebpf::BPFArrayTable<int> t_ns2_a = bpf_ns2_a.get_array_table<int>("mysharedtable");
53   ebpf::BPFArrayTable<int> t_ns2_b = bpf_ns2_b.get_array_table<int>("mysharedtable");
54 
55   // test that tables within the same ns are shared
56   int v1, v2, v3;
57   res = t_ns1_a.update_value(13, 42);
58   REQUIRE(res.code() == 0);
59 
60   res = t_ns1_b.get_value(13, v1);
61   REQUIRE(res.code() == 0);
62   REQUIRE(v1 == 42);
63 
64   // test that tables are isolated within different ns
65   res = t_ns2_a.update_value(13, 69);
66   REQUIRE(res.code() == 0);
67 
68   res = t_ns2_b.get_value(13, v2);
69   REQUIRE(res.code() == 0);
70   REQUIRE(v2 == 69);
71 
72   res = t_ns1_b.get_value(13, v3);
73   REQUIRE(res.code() == 0);
74   REQUIRE(v3 == 42);  // value should still be 42
75 }
76