1 /* 2 * Copyright (c) 2020 Facebook, Inc. 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 <linux/version.h> 18 #include <unistd.h> 19 #include <sys/types.h> 20 #include <sys/socket.h> 21 #include <string> 22 #include <vector> 23 24 #include "BPF.h" 25 #include "catch.hpp" 26 27 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 19, 0) 28 29 TEST_CASE("test cgroup storage", "[cgroup_storage]") { 30 { 31 const std::string BPF_PROGRAM = R"( 32 BPF_CGROUP_STORAGE(cg_storage1, int); 33 BPF_CGROUP_STORAGE(cg_storage2, int); 34 int test(struct bpf_sock_ops *skops) 35 { 36 struct bpf_cgroup_storage_key key = {0}; 37 u32 val = 0; 38 39 cg_storage2.lookup(&key); 40 cg_storage2.update(&key, &val); 41 cg_storage2.get_local_storage(0); 42 43 return 0; 44 } 45 )"; 46 47 // make sure program is loaded successfully 48 ebpf::BPF bpf; 49 ebpf::StatusTuple res(0); 50 res = bpf.init(BPF_PROGRAM); 51 REQUIRE(res.ok()); 52 53 auto cg_storage = bpf.get_cg_storage_table<int>("cg_storage1"); 54 struct bpf_cgroup_storage_key key = {0}; 55 int val; 56 57 // all the following lookup/update will fail since 58 // cgroup local storage only created during prog attachment time. 59 res = cg_storage.get_value(key, val); 60 REQUIRE(!res.ok()); 61 62 res = cg_storage.update_value(key, val); 63 REQUIRE(!res.ok()); 64 } 65 } 66 #endif 67 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 20, 0) 68 TEST_CASE("test percpu cgroup storage", "[percpu_cgroup_storage]") { 69 { 70 const std::string BPF_PROGRAM = R"( 71 BPF_PERCPU_CGROUP_STORAGE(cg_storage1, long long); 72 BPF_PERCPU_CGROUP_STORAGE(cg_storage2, long long); 73 int test(struct bpf_sock_ops *skops) 74 { 75 struct bpf_cgroup_storage_key key = {0}; 76 long long val = 0; 77 78 cg_storage2.lookup(&key); 79 cg_storage2.update(&key, &val); 80 cg_storage2.get_local_storage(0); 81 82 return 0; 83 } 84 )"; 85 86 // make sure program is loaded successfully 87 ebpf::BPF bpf; 88 ebpf::StatusTuple res(0); 89 res = bpf.init(BPF_PROGRAM); 90 REQUIRE(res.ok()); 91 92 auto cg_storage = bpf.get_percpu_cg_storage_table<long long>("cg_storage1"); 93 struct bpf_cgroup_storage_key key = {0}; 94 std::vector<long long> val(ebpf::BPFTable::get_possible_cpu_count()); 95 96 // all the following lookup/update will fail since 97 // cgroup local storage only created during prog attachment time. 98 res = cg_storage.get_value(key, val); 99 REQUIRE(!res.ok()); 100 101 res = cg_storage.update_value(key, val); 102 REQUIRE(!res.ok()); 103 } 104 } 105 106 #endif 107