Specify a eth interface for grpc connection.
When enabling wifi on seahawk, the main network interface changes
so we need to explicitly specify the eth interface we want to
use for our grpc connection.
Note that using setsockopt requires NET_RAW permission which is
only granted to limited apps. As a result, selinux permissive
mode is required for this CL to work.
Test: Manual test on seahawk, verify grpc is connected okay.
Bug: 261108682
Change-Id: I95c9f796bf40226b612edf284188ffc6d095086f
diff --git a/automotive/remoteaccess/hal/default/src/BindToDeviceSocketMutator.cpp b/automotive/remoteaccess/hal/default/src/BindToDeviceSocketMutator.cpp
new file mode 100644
index 0000000..c6a96de
--- /dev/null
+++ b/automotive/remoteaccess/hal/default/src/BindToDeviceSocketMutator.cpp
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "BindToDeviceSocketMutator.h"
+
+#include <android-base/logging.h>
+#include <errno.h>
+#include <sys/socket.h>
+
+namespace android::hardware::automotive::remoteaccess {
+
+bool BindToDeviceSocketMutator::mutateFd(int fd) {
+ int ret = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, mIfname.c_str(), mIfname.size());
+ if (ret != 0) {
+ PLOG(ERROR) << "Can't bind socket to interface " << mIfname;
+ return false;
+ }
+ return true;
+}
+
+bool bind_to_device_mutator_mutate_fd(int fd, grpc_socket_mutator* mutator) {
+ BindToDeviceSocketMutator* bsm = (BindToDeviceSocketMutator*)mutator;
+ return bsm->mutateFd(fd);
+}
+
+int bind_to_device_mutator_compare(grpc_socket_mutator* a, grpc_socket_mutator* b) {
+ return ((a) < (b) ? -1 : ((a) > (b) ? 1 : 0));
+}
+
+void bind_to_device_mutator_destroy(grpc_socket_mutator* mutator) {
+ BindToDeviceSocketMutator* bsm = (BindToDeviceSocketMutator*)mutator;
+ delete bsm;
+}
+
+grpc_socket_mutator_vtable bind_to_device_mutator_vtable = {bind_to_device_mutator_mutate_fd,
+ bind_to_device_mutator_compare,
+ bind_to_device_mutator_destroy};
+
+BindToDeviceSocketMutator::BindToDeviceSocketMutator(const std::string_view& interface_name) {
+ mIfname = interface_name;
+ grpc_socket_mutator_init(this, &bind_to_device_mutator_vtable);
+}
+
+} // namespace android::hardware::automotive::remoteaccess
diff --git a/automotive/remoteaccess/hal/default/src/BindToDeviceSocketMutator.h b/automotive/remoteaccess/hal/default/src/BindToDeviceSocketMutator.h
new file mode 100644
index 0000000..bafcc65
--- /dev/null
+++ b/automotive/remoteaccess/hal/default/src/BindToDeviceSocketMutator.h
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <grpc++/grpc++.h>
+#include <src/core/lib/iomgr/socket_mutator.h>
+#include <string>
+
+namespace android::hardware::automotive::remoteaccess {
+
+class BindToDeviceSocketMutator final : public grpc_socket_mutator {
+ public:
+ BindToDeviceSocketMutator(const std::string_view& interface_name);
+
+ bool mutateFd(int fd);
+
+ private:
+ std::string mIfname;
+};
+
+} // namespace android::hardware::automotive::remoteaccess
diff --git a/automotive/remoteaccess/hal/default/src/RemoteAccessImpl.cpp b/automotive/remoteaccess/hal/default/src/RemoteAccessImpl.cpp
index 8720c2f..d525141 100644
--- a/automotive/remoteaccess/hal/default/src/RemoteAccessImpl.cpp
+++ b/automotive/remoteaccess/hal/default/src/RemoteAccessImpl.cpp
@@ -18,45 +18,60 @@
#include "RemoteAccessService.h"
+#include "BindToDeviceSocketMutator.h"
+
+#include <android-base/logging.h>
#include <android/binder_manager.h>
#include <android/binder_process.h>
#include <grpcpp/create_channel.h>
+#include <libnetdevice/libnetdevice.h>
#include <stdlib.h>
-#include <utils/Log.h>
constexpr char SERVICE_NAME[] = "android.hardware.automotive.remoteaccess.IRemoteAccess/default";
int main(int /* argc */, char* /* argv */[]) {
- ALOGI("Registering RemoteAccessService as service...");
+ LOG(INFO) << "Registering RemoteAccessService as service...";
#ifndef GRPC_SERVICE_ADDRESS
- ALOGE("GRPC_SERVICE_ADDRESS is not defined, exiting");
+ LOG(ERROR) << "GRPC_SERVICE_ADDRESS is not defined, exiting";
exit(1);
#endif
- auto channel = grpc::CreateChannel(GRPC_SERVICE_ADDRESS, grpc::InsecureChannelCredentials());
+ grpc::ChannelArguments grpcargs = {};
+
+#ifdef GRPC_SERVICE_IFNAME
+ grpcargs.SetSocketMutator(
+ new android::hardware::automotive::remoteaccess::BindToDeviceSocketMutator(
+ GRPC_SERVICE_IFNAME));
+ LOG(DEBUG) << "GRPC_SERVICE_IFNAME specified as: " << GRPC_SERVICE_IFNAME;
+ LOG(INFO) << "Waiting for interface: " << GRPC_SERVICE_IFNAME;
+ android::netdevice::waitFor({GRPC_SERVICE_IFNAME},
+ android::netdevice::WaitCondition::PRESENT_AND_UP);
+ LOG(INFO) << "Waiting for interface: " << GRPC_SERVICE_IFNAME << " done";
+#endif
+ auto channel = grpc::CreateCustomChannel(GRPC_SERVICE_ADDRESS,
+ grpc::InsecureChannelCredentials(), grpcargs);
auto clientStub = android::hardware::automotive::remoteaccess::WakeupClient::NewStub(channel);
auto service = ndk::SharedRefBase::make<
android::hardware::automotive::remoteaccess::RemoteAccessService>(clientStub.get());
binder_exception_t err = AServiceManager_addService(service->asBinder().get(), SERVICE_NAME);
if (err != EX_NONE) {
- ALOGE("failed to register android.hardware.automotive.remote.IRemoteAccess service, "
- "exception: %d",
- err);
+ LOG(ERROR) << "failed to register android.hardware.automotive.remote.IRemoteAccess service"
+ << ", exception: " << err;
exit(1);
}
if (!ABinderProcess_setThreadPoolMaxThreadCount(1)) {
- ALOGE("%s", "failed to set thread pool max thread count");
+ LOG(ERROR) << "failed to set thread pool max thread count";
exit(1);
}
ABinderProcess_startThreadPool();
- ALOGI("RemoteAccess service Ready");
+ LOG(INFO) << "RemoteAccess service Ready";
ABinderProcess_joinThreadPool();
- ALOGW("Should not reach here");
+ LOG(ERROR) << "Should not reach here";
return 0;
}