Chenbo Feng | 36575d0 | 2018-02-05 15:19:15 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 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/bpf.h> |
| 18 | #include <stdint.h> |
| 19 | |
| 20 | #define ELF_SEC(NAME) __attribute__((section(NAME), used)) |
| 21 | |
| 22 | struct uid_tag { |
| 23 | uint32_t uid; |
| 24 | uint32_t tag; |
| 25 | }; |
| 26 | |
| 27 | struct stats_key { |
| 28 | uint32_t uid; |
| 29 | uint32_t tag; |
| 30 | uint32_t counterSet; |
| 31 | uint32_t ifaceIndex; |
| 32 | }; |
| 33 | |
| 34 | struct stats_value { |
| 35 | uint64_t rxPackets; |
| 36 | uint64_t rxBytes; |
| 37 | uint64_t txPackets; |
| 38 | uint64_t txBytes; |
| 39 | }; |
| 40 | |
| 41 | /* helper functions called from eBPF programs written in C */ |
| 42 | static void* (*find_map_entry)(uint64_t map, void* key) = (void*)BPF_FUNC_map_lookup_elem; |
| 43 | static int (*write_to_map_entry)(uint64_t map, void* key, void* value, |
| 44 | uint64_t flags) = (void*)BPF_FUNC_map_update_elem; |
| 45 | static int (*delete_map_entry)(uint64_t map, void* key) = (void*)BPF_FUNC_map_delete_elem; |
| 46 | static uint64_t (*get_socket_cookie)(struct __sk_buff* skb) = (void*)BPF_FUNC_get_socket_cookie; |
| 47 | static uint32_t (*get_socket_uid)(struct __sk_buff* skb) = (void*)BPF_FUNC_get_socket_uid; |
| 48 | static int (*bpf_skb_load_bytes)(struct __sk_buff* skb, int off, void* to, |
| 49 | int len) = (void*)BPF_FUNC_skb_load_bytes; |
| 50 | |
| 51 | #define BPF_PASS 1 |
| 52 | #define BPF_DROP 0 |