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> |
Chenbo Feng | 5ed1799 | 2018-03-13 21:30:49 -0700 | [diff] [blame] | 19 | #include "bpf_shared.h" |
Chenbo Feng | 36575d0 | 2018-02-05 15:19:15 -0800 | [diff] [blame] | 20 | |
| 21 | #define ELF_SEC(NAME) __attribute__((section(NAME), used)) |
| 22 | |
| 23 | struct uid_tag { |
| 24 | uint32_t uid; |
| 25 | uint32_t tag; |
| 26 | }; |
| 27 | |
| 28 | struct stats_key { |
| 29 | uint32_t uid; |
| 30 | uint32_t tag; |
| 31 | uint32_t counterSet; |
| 32 | uint32_t ifaceIndex; |
| 33 | }; |
| 34 | |
| 35 | struct stats_value { |
| 36 | uint64_t rxPackets; |
| 37 | uint64_t rxBytes; |
| 38 | uint64_t txPackets; |
| 39 | uint64_t txBytes; |
| 40 | }; |
| 41 | |
| 42 | /* helper functions called from eBPF programs written in C */ |
| 43 | static void* (*find_map_entry)(uint64_t map, void* key) = (void*)BPF_FUNC_map_lookup_elem; |
| 44 | static int (*write_to_map_entry)(uint64_t map, void* key, void* value, |
| 45 | uint64_t flags) = (void*)BPF_FUNC_map_update_elem; |
| 46 | static int (*delete_map_entry)(uint64_t map, void* key) = (void*)BPF_FUNC_map_delete_elem; |
| 47 | static uint64_t (*get_socket_cookie)(struct __sk_buff* skb) = (void*)BPF_FUNC_get_socket_cookie; |
| 48 | static uint32_t (*get_socket_uid)(struct __sk_buff* skb) = (void*)BPF_FUNC_get_socket_uid; |
| 49 | static int (*bpf_skb_load_bytes)(struct __sk_buff* skb, int off, void* to, |
| 50 | int len) = (void*)BPF_FUNC_skb_load_bytes; |
| 51 | |
| 52 | #define BPF_PASS 1 |
| 53 | #define BPF_DROP 0 |
Chenbo Feng | 5ed1799 | 2018-03-13 21:30:49 -0700 | [diff] [blame] | 54 | #define BPF_EGRESS 0 |
| 55 | #define BPF_INGRESS 1 |
| 56 | |
| 57 | static __always_inline int xt_bpf_count(struct __sk_buff* skb, int type) { |
| 58 | uint32_t key = skb->ifindex; |
| 59 | struct stats_value* value; |
| 60 | |
| 61 | value = find_map_entry(IFACE_STATS_MAP, &key); |
| 62 | if (!value) { |
| 63 | struct stats_value newValue = {}; |
| 64 | write_to_map_entry(IFACE_STATS_MAP, &key, &newValue, BPF_NOEXIST); |
| 65 | value = find_map_entry(IFACE_STATS_MAP, &key); |
| 66 | } |
| 67 | if (value) { |
| 68 | if (type == BPF_EGRESS) { |
| 69 | __sync_fetch_and_add(&value->txPackets, 1); |
| 70 | __sync_fetch_and_add(&value->txBytes, skb->len); |
| 71 | } else if (type == BPF_INGRESS) { |
| 72 | __sync_fetch_and_add(&value->rxPackets, 1); |
| 73 | __sync_fetch_and_add(&value->rxBytes, skb->len); |
| 74 | } |
| 75 | } |
| 76 | return BPF_PASS; |
| 77 | } |
Chenbo Feng | 2cc3d6a | 2018-03-06 02:13:51 -0800 | [diff] [blame] | 78 | |
| 79 | static __always_inline inline void bpf_update_stats(struct __sk_buff* skb, uint64_t map, |
| 80 | int direction, struct stats_key key) { |
| 81 | struct stats_value* value; |
| 82 | value = find_map_entry(map, &key); |
| 83 | if (!value) { |
| 84 | struct stats_value newValue = {}; |
| 85 | write_to_map_entry(map, &key, &newValue, BPF_NOEXIST); |
| 86 | value = find_map_entry(map, &key); |
| 87 | } |
| 88 | if (value) { |
| 89 | if (direction == BPF_INGRESS) { |
| 90 | __sync_fetch_and_add(&value->rxPackets, 1); |
| 91 | __sync_fetch_and_add(&value->rxBytes, skb->len); |
| 92 | } else { |
| 93 | __sync_fetch_and_add(&value->txPackets, 1); |
| 94 | __sync_fetch_and_add(&value->txBytes, skb->len); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | static __always_inline inline int bpf_traffic_account(struct __sk_buff* skb, int direction) { |
| 100 | uint64_t cookie = get_socket_cookie(skb); |
| 101 | struct uid_tag* utag = find_map_entry(COOKIE_TAG_MAP, &cookie); |
| 102 | uint32_t uid, tag; |
| 103 | if (utag) { |
| 104 | uid = utag->uid; |
| 105 | tag = utag->tag; |
| 106 | } else { |
| 107 | uid = get_socket_uid(skb); |
| 108 | tag = 0; |
| 109 | } |
| 110 | |
| 111 | struct stats_key key = {.uid = uid, .tag = tag, .counterSet = 0, .ifaceIndex = skb->ifindex}; |
| 112 | |
| 113 | uint32_t* counterSet; |
| 114 | counterSet = find_map_entry(UID_COUNTERSET_MAP, &uid); |
| 115 | if (counterSet) key.counterSet = *counterSet; |
| 116 | |
| 117 | int ret; |
| 118 | if (tag) { |
| 119 | bpf_update_stats(skb, TAG_STATS_MAP, direction, key); |
| 120 | } |
| 121 | |
| 122 | key.tag = 0; |
| 123 | bpf_update_stats(skb, UID_STATS_MAP, direction, key); |
| 124 | return BPF_PASS; |
| 125 | } |