• 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 package com.android.net.module.util;
17 
18 import android.system.ErrnoException;
19 
20 import androidx.annotation.NonNull;
21 
22 import java.util.NoSuchElementException;
23 import java.util.function.BiConsumer;
24 
25 /**
26  * The interface of BpfMap. This could be used to inject for testing.
27  * So the testing code won't load the JNI and update the entries to kernel.
28  *
29  * @param <K> the key of the map.
30  * @param <V> the value of the map.
31  */
32 public interface IBpfMap<K extends Struct, V extends Struct> {
33     /** Update an existing or create a new key -> value entry in an eBbpf map. */
updateEntry(K key, V value)34     void updateEntry(K key, V value) throws ErrnoException;
35 
36     /** If the key does not exist in the map, insert key -> value entry into eBpf map. */
insertEntry(K key, V value)37     void insertEntry(K key, V value) throws ErrnoException, IllegalStateException;
38 
39     /** If the key already exists in the map, replace its value. */
replaceEntry(K key, V value)40     void replaceEntry(K key, V value) throws ErrnoException, NoSuchElementException;
41 
42     /**
43      * Update an existing or create a new key -> value entry in an eBbpf map. Returns true if
44      * inserted, false if replaced. (use updateEntry() if you don't care whether insert or replace
45      * happened).
46      */
insertOrReplaceEntry(K key, V value)47     boolean insertOrReplaceEntry(K key, V value) throws ErrnoException;
48 
49     /** Remove existing key from eBpf map. Return true if something was deleted. */
deleteEntry(K key)50     boolean deleteEntry(K key) throws ErrnoException;
51 
52     /** Returns {@code true} if this map contains no elements. */
isEmpty()53     boolean isEmpty() throws ErrnoException;
54 
55     /** Get the key after the passed-in key. */
getNextKey(@onNull K key)56     K getNextKey(@NonNull K key) throws ErrnoException;
57 
58     /** Get the first key of the eBpf map. */
getFirstKey()59     K getFirstKey() throws ErrnoException;
60 
61     /** Check whether a key exists in the map. */
containsKey(@onNull K key)62     boolean containsKey(@NonNull K key) throws ErrnoException;
63 
64     /** Retrieve a value from the map. */
getValue(@onNull K key)65     V getValue(@NonNull K key) throws ErrnoException;
66 
67     public interface ThrowingBiConsumer<T,U> {
accept(T t, U u)68         void accept(T t, U u) throws ErrnoException;
69     }
70 
71     /**
72      * Iterate through the map and handle each key -> value retrieved base on the given BiConsumer.
73      */
forEach(ThrowingBiConsumer<K, V> action)74     void forEach(ThrowingBiConsumer<K, V> action) throws ErrnoException;
75 
76     /** Clears the map. */
clear()77     void clear() throws ErrnoException;
78 }
79