1 /* 2 * Copyright (C) 2022 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 package com.android.net.module.util; 18 19 import java.io.IOException; 20 21 /** 22 * Contains mostly tc-related functionality. 23 */ 24 public class TcUtils { 25 static { JniUtil.getJniLibraryName()26 System.loadLibrary(JniUtil.getJniLibraryName(TcUtils.class.getPackage())); 27 } 28 29 /** 30 * Checks if the network interface uses an ethernet L2 header. 31 * 32 * @param iface the network interface. 33 * @return true if the interface uses an ethernet L2 header. 34 * @throws IOException 35 */ isEthernet(String iface)36 public static native boolean isEthernet(String iface) throws IOException; 37 38 /** 39 * Attach a tc bpf filter. 40 * 41 * Equivalent to the following 'tc' command: 42 * tc filter add dev .. in/egress prio .. protocol ipv6/ip bpf object-pinned 43 * /sys/fs/bpf/... direct-action 44 * 45 * @param ifIndex the network interface index. 46 * @param ingress ingress or egress qdisc. 47 * @param prio 48 * @param proto 49 * @param bpfProgPath 50 * @throws IOException 51 */ tcFilterAddDevBpf(int ifIndex, boolean ingress, short prio, short proto, String bpfProgPath)52 public static native void tcFilterAddDevBpf(int ifIndex, boolean ingress, short prio, 53 short proto, String bpfProgPath) throws IOException; 54 55 /** 56 * Attach a tc police action. 57 * 58 * Attaches a matchall filter to the clsact qdisc with a tc police and tc bpf action attached. 59 * This causes the ingress rate to be limited and exceeding packets to be forwarded to a bpf 60 * program (specified in bpfProgPah) that accounts for the packets before dropping them. 61 * 62 * Equivalent to the following 'tc' command: 63 * tc filter add dev .. ingress prio .. protocol .. matchall \ 64 * action police rate .. burst .. conform-exceed pipe/continue \ 65 * action bpf object-pinned .. \ 66 * drop 67 * 68 * @param ifIndex the network interface index. 69 * @param prio the filter preference. 70 * @param proto protocol. 71 * @param rateInBytesPerSec rate limit in bytes/s. 72 * @param bpfProgPath bpg program that accounts for rate exceeding packets before they are 73 * dropped. 74 * @throws IOException 75 */ tcFilterAddDevIngressPolice(int ifIndex, short prio, short proto, int rateInBytesPerSec, String bpfProgPath)76 public static native void tcFilterAddDevIngressPolice(int ifIndex, short prio, short proto, 77 int rateInBytesPerSec, String bpfProgPath) throws IOException; 78 79 /** 80 * Delete a tc filter. 81 * 82 * Equivalent to the following 'tc' command: 83 * tc filter del dev .. in/egress prio .. protocol .. 84 * 85 * @param ifIndex the network interface index. 86 * @param ingress ingress or egress qdisc. 87 * @param prio the filter preference. 88 * @param proto protocol. 89 * @throws IOException 90 */ tcFilterDelDev(int ifIndex, boolean ingress, short prio, short proto)91 public static native void tcFilterDelDev(int ifIndex, boolean ingress, short prio, 92 short proto) throws IOException; 93 94 /** 95 * Add a clsact qdisc. 96 * 97 * Equivalent to the following 'tc' command: 98 * tc qdisc add dev .. clsact 99 * 100 * @param ifIndex the network interface index. 101 * @throws IOException 102 */ tcQdiscAddDevClsact(int ifIndex)103 public static native void tcQdiscAddDevClsact(int ifIndex) throws IOException; 104 105 /** 106 * Attempt to fetch a bpf program from a path. 107 * Return true on success, false on non-existence or any other failure. 108 * 109 * @param bpfProgPath 110 */ isBpfProgramUsable(String bpfProgPath)111 public static native boolean isBpfProgramUsable(String bpfProgPath); 112 } 113