1*387f9dfdSAndroid Build Coastguard Worker#!/usr/bin/env bcc-lua 2*387f9dfdSAndroid Build Coastguard Worker--[[ 3*387f9dfdSAndroid Build Coastguard WorkerCopyright 2016 Marek Vavrusa <[email protected]> 4*387f9dfdSAndroid Build Coastguard Worker 5*387f9dfdSAndroid Build Coastguard WorkerLicensed under the Apache License, Version 2.0 (the "License"); 6*387f9dfdSAndroid Build Coastguard Workeryou may not use this file except in compliance with the License. 7*387f9dfdSAndroid Build Coastguard WorkerYou may obtain a copy of the License at 8*387f9dfdSAndroid Build Coastguard Worker 9*387f9dfdSAndroid Build Coastguard Workerhttp://www.apache.org/licenses/LICENSE-2.0 10*387f9dfdSAndroid Build Coastguard Worker 11*387f9dfdSAndroid Build Coastguard WorkerUnless required by applicable law or agreed to in writing, software 12*387f9dfdSAndroid Build Coastguard Workerdistributed under the License is distributed on an "AS IS" BASIS, 13*387f9dfdSAndroid Build Coastguard WorkerWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14*387f9dfdSAndroid Build Coastguard WorkerSee the License for the specific language governing permissions and 15*387f9dfdSAndroid Build Coastguard Workerlimitations under the License. 16*387f9dfdSAndroid Build Coastguard Worker]] 17*387f9dfdSAndroid Build Coastguard Worker-- This program counts total bytes received per-protocol in 64-bit counters. 18*387f9dfdSAndroid Build Coastguard Worker-- The map backend is array in this case to avoid key allocations. 19*387f9dfdSAndroid Build Coastguard Worker-- increments counter for each packet of given type seen 20*387f9dfdSAndroid Build Coastguard Worker-- Rewrite of https://github.com/torvalds/linux/blob/master/samples/bpf/sock_example.c 21*387f9dfdSAndroid Build Coastguard Workerlocal ffi = require("ffi") 22*387f9dfdSAndroid Build Coastguard Workerlocal bpf = require("bpf") 23*387f9dfdSAndroid Build Coastguard Workerlocal S = require("syscall") 24*387f9dfdSAndroid Build Coastguard Worker 25*387f9dfdSAndroid Build Coastguard Worker-- Shared part of the program 26*387f9dfdSAndroid Build Coastguard Workerlocal map = bpf.map('array', 256, ffi.typeof('uint32_t'), ffi.typeof('uint64_t')) 27*387f9dfdSAndroid Build Coastguard Worker-- Kernel-space part of the program 28*387f9dfdSAndroid Build Coastguard Workerbpf.socket('lo', function (skb) 29*387f9dfdSAndroid Build Coastguard Worker local proto = pkt.ip.proto -- Get byte (ip.proto) from frame at [23] 30*387f9dfdSAndroid Build Coastguard Worker xadd(map[proto], skb.len) -- Atomic `map[proto] += <payload length>` 31*387f9dfdSAndroid Build Coastguard Workerend) 32*387f9dfdSAndroid Build Coastguard Worker-- User-space part of the program 33*387f9dfdSAndroid Build Coastguard Workerfor _ = 1, 10 do 34*387f9dfdSAndroid Build Coastguard Worker local icmp, udp, tcp = map[1], map[17], map[6] 35*387f9dfdSAndroid Build Coastguard Worker print(string.format('TCP %d UDP %d ICMP %d bytes', 36*387f9dfdSAndroid Build Coastguard Worker tonumber(tcp or 0), tonumber(udp or 0), tonumber(icmp or 0))) 37*387f9dfdSAndroid Build Coastguard Worker S.sleep(1) 38*387f9dfdSAndroid Build Coastguard Workerend