summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--common/Android.bp1
-rw-r--r--common/device/com/android/net/module/util/FdEventsReader.java21
-rw-r--r--common/native/bpf_headers/include/bpf/BpfMap.h17
3 files changed, 35 insertions, 4 deletions
diff --git a/common/Android.bp b/common/Android.bp
index e0b75c5c..1d0f49ba 100644
--- a/common/Android.bp
+++ b/common/Android.bp
@@ -127,7 +127,6 @@ java_library {
"//frameworks/libs/net/common/testutils:__subpackages__",
"//packages/modules/Connectivity:__subpackages__",
"//packages/modules/NetworkStack:__subpackages__",
- "//frameworks/base/services/core",
],
libs: [
"androidx.annotation_annotation",
diff --git a/common/device/com/android/net/module/util/FdEventsReader.java b/common/device/com/android/net/module/util/FdEventsReader.java
index ecf8e77c..4825992f 100644
--- a/common/device/com/android/net/module/util/FdEventsReader.java
+++ b/common/device/com/android/net/module/util/FdEventsReader.java
@@ -69,6 +69,7 @@ import java.io.IOException;
* @param <BufferType> the type of the buffer used to read data.
*/
public abstract class FdEventsReader<BufferType> {
+ private static final String TAG = FdEventsReader.class.getSimpleName();
private static final int FD_EVENTS = EVENT_INPUT | EVENT_ERROR;
private static final int UNREGISTER_THIS_FD = 0;
@@ -167,6 +168,18 @@ public abstract class FdEventsReader<BufferType> {
protected void handlePacket(@NonNull BufferType recvbuf, int length) {}
/**
+ * Called by the subclasses of FdEventsReader, decide whether it should stop reading packet or
+ * just ignore the specific error other than EAGAIN or EINTR.
+ *
+ * @return {@code true} if this FdEventsReader should stop reading from the socket.
+ * {@code false} if it should continue.
+ */
+ protected boolean handleReadError(@NonNull ErrnoException e) {
+ logError("readPacket error: ", e);
+ return true; // by default, stop reading on any error.
+ }
+
+ /**
* Called by the main loop to log errors. In some cases |e| may be null.
*/
protected void logError(@NonNull String msg, @Nullable Exception e) {}
@@ -234,8 +247,10 @@ public abstract class FdEventsReader<BufferType> {
} else if (e.errno == OsConstants.EINTR) {
continue;
} else {
- if (isRunning()) logError("readPacket error: ", e);
- break;
+ if (!isRunning()) break;
+ final boolean shouldStop = handleReadError(e);
+ if (shouldStop) break;
+ continue;
}
} catch (Exception e) {
if (isRunning()) logError("readPacket error: ", e);
@@ -246,7 +261,7 @@ public abstract class FdEventsReader<BufferType> {
handlePacket(mBuffer, bytesRead);
} catch (Exception e) {
logError("handlePacket error: ", e);
- Log.wtf(FdEventsReader.class.getSimpleName(), "Error handling packet", e);
+ Log.wtf(TAG, "Error handling packet", e);
}
}
diff --git a/common/native/bpf_headers/include/bpf/BpfMap.h b/common/native/bpf_headers/include/bpf/BpfMap.h
index bdffc0f0..d07d6104 100644
--- a/common/native/bpf_headers/include/bpf/BpfMap.h
+++ b/common/native/bpf_headers/include/bpf/BpfMap.h
@@ -102,6 +102,23 @@ class BpfMap {
// Function that tries to get map from a pinned path.
base::Result<void> init(const char* path);
+#ifdef TEST_BPF_MAP
+ // due to Android SELinux limitations which prevent map creation by anyone besides the bpfloader
+ // this should only ever be used by test code, it is equivalent to:
+ // .reset(createMap(type, keysize, valuesize, max_entries, map_flags)
+ // TODO: derive map_flags from BpfMap vs BpfMapRO
+ base::Result<void> resetMap(bpf_map_type map_type, uint32_t max_entries, uint32_t map_flags = 0) {
+ int map_fd = createMap(map_type, sizeof(Key), sizeof(Value), max_entries, map_flags);
+ if (map_fd < 0) {
+ auto err = ErrnoErrorf("Unable to create map.");
+ mMapFd.reset();
+ return err;
+ };
+ mMapFd.reset(map_fd);
+ return {};
+ }
+#endif
+
// Iterate through the map and handle each key retrieved based on the filter
// without modification of map content.
base::Result<void> iterate(