summaryrefslogtreecommitdiff
path: root/server/InterfaceController.cpp
diff options
context:
space:
mode:
authorNathan Harold <nharold@google.com>2018-04-19 13:10:19 -0700
committerNathan Harold <nharold@google.com>2018-04-26 16:11:55 -0700
commit172f8e4b7aef86b4727f6c8336b7c9ecdf981da2 (patch)
treed89dde209035afe952e9ed479a57a506e2def325 /server/InterfaceController.cpp
parent9c40455ddc4a2bcf6594523aaeb263a1cb34cdb1 (diff)
Factor getIfaceNames() from getIfaceList()
getIfaceList first walks the list of interfaces from the sysfs, then it calls individually for each of those interfaces to get the ifindex for them. Because each of the calls to retrieve the ifindex means a netlink call, this could possibly cause performance problems (unconfirmed) on the netlink interface. Since the names are independently useful and are quick to fetch, this function is now factored in to 2 parts: one which fetches the names and a separate function which performs the original operation of fetching the names and mapping them to if_indices. Bug: 74560705 Test: netd_integration_test - GetIfaceListTest Merged-In: I1f888c31e992c8f7d51f3c67434ffef6d75b065d Change-Id: I1f888c31e992c8f7d51f3c67434ffef6d75b065d (cherry picked from commit dfe2a6f43de4aba2780c861c750db8e4f1fb22e3)
Diffstat (limited to 'server/InterfaceController.cpp')
-rw-r--r--server/InterfaceController.cpp24
1 files changed, 18 insertions, 6 deletions
diff --git a/server/InterfaceController.cpp b/server/InterfaceController.cpp
index 50c8f8d2..743ad99e 100644
--- a/server/InterfaceController.cpp
+++ b/server/InterfaceController.cpp
@@ -43,6 +43,7 @@ using android::base::StringPrintf;
using android::base::WriteStringToFile;
using android::net::INetd;
using android::net::RouteController;
+using android::netdutils::isOk;
using android::netdutils::Status;
using android::netdutils::StatusOr;
using android::netdutils::makeSlice;
@@ -388,8 +389,8 @@ void InterfaceController::setIPv6OptimisticMode(const char *value) {
setOnAllInterfaces(ipv6_proc_path, "use_optimistic", value);
}
-StatusOr<std::map<std::string, uint32_t>> InterfaceController::getIfaceList() {
- std::map<std::string, uint32_t> ifacePairs;
+StatusOr<std::vector<std::string>> InterfaceController::getIfaceNames() {
+ std::vector<std::string> ifaceNames;
DIR* d;
struct dirent* de;
@@ -398,11 +399,22 @@ StatusOr<std::map<std::string, uint32_t>> InterfaceController::getIfaceList() {
}
while ((de = readdir(d))) {
if (de->d_name[0] == '.') continue;
- uint32_t ifaceIndex = if_nametoindex(de->d_name);
- if (ifaceIndex) {
- ifacePairs.insert(std::pair<std::string, uint32_t>(de->d_name, ifaceIndex));
- }
+ ifaceNames.push_back(std::string(de->d_name));
}
closedir(d);
+ return ifaceNames;
+}
+
+StatusOr<std::map<std::string, uint32_t>> InterfaceController::getIfaceList() {
+ std::map<std::string, uint32_t> ifacePairs;
+
+ ASSIGN_OR_RETURN(auto ifaceNames, getIfaceNames());
+
+ for (const auto& name : ifaceNames) {
+ uint32_t ifaceIndex = if_nametoindex(name.c_str());
+ if (ifaceIndex) {
+ ifacePairs.insert(std::pair<std::string, uint32_t>(name, ifaceIndex));
+ }
+ }
return ifacePairs;
}