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