[posix] allow set POSIX TUN device at runtime (#12175)

This commit allows users to pass the POSIX TUN device path
through a command line flag when starting the daemon / cli
when `OT_PLATFORM_NETIF` is enabled.
This commit is contained in:
Tongze Wang
2025-12-01 12:23:22 -08:00
committed by GitHub
parent 774dc2b1e9
commit 923bf40134
3 changed files with 31 additions and 5 deletions
+17
View File
@@ -140,9 +140,15 @@ enum
OT_POSIX_OPT_RADIO_VERSION,
OT_POSIX_OPT_REAL_TIME_SIGNAL,
#if OPENTHREAD_CONFIG_PLATFORM_NETIF_ENABLE
OT_POSIX_OPT_TUN_DEVICE,
#endif
};
static const struct option kOptions[] = {
#if OPENTHREAD_CONFIG_PLATFORM_NETIF_ENABLE
{"tun-device", required_argument, NULL, OT_POSIX_OPT_TUN_DEVICE},
#endif
{"backbone-interface-name", required_argument, NULL, OT_POSIX_OPT_BACKBONE_INTERFACE_NAME},
{"debug-level", required_argument, NULL, OT_POSIX_OPT_DEBUG_LEVEL},
{"dry-run", no_argument, NULL, OT_POSIX_OPT_DRY_RUN},
@@ -161,6 +167,9 @@ static void PrintUsage(const char *aProgramName, FILE *aStream, int aExitCode)
"Syntax:\n"
" %s [Options] RadioURL [RadioURL]\n"
"Options:\n"
#if OPENTHREAD_CONFIG_PLATFORM_NETIF_ENABLE
" --tun-device POSIX TUN Device.\n"
#endif
" -B --backbone-interface-name Backbone network interface name.\n"
" -d --debug-level Debug level of logging.\n"
" -h --help Display this usage information.\n"
@@ -189,6 +198,9 @@ static void ParseArg(int aArgCount, char *aArgVector[], PosixConfig *aConfig)
aConfig->mPlatformConfig.mSpeedUpFactor = 1;
aConfig->mLogLevel = OT_LOG_LEVEL_CRIT;
aConfig->mPlatformConfig.mInterfaceName = OPENTHREAD_POSIX_CONFIG_THREAD_NETIF_DEFAULT_NAME;
#if OPENTHREAD_CONFIG_PLATFORM_NETIF_ENABLE
aConfig->mPlatformConfig.mTunDevice = NULL;
#endif
#ifdef SIGRTMIN
aConfig->mPlatformConfig.mRealTimeSignal = SIGRTMIN;
#endif
@@ -255,6 +267,11 @@ static void ParseArg(int aArgCount, char *aArgVector[], PosixConfig *aConfig)
aConfig->mPlatformConfig.mRealTimeSignal = atoi(optarg);
}
break;
#endif
#if OPENTHREAD_CONFIG_PLATFORM_NETIF_ENABLE
case OT_POSIX_OPT_TUN_DEVICE:
aConfig->mPlatformConfig.mTunDevice = optarg;
break;
#endif
case '?':
PrintUsage(aArgVector[0], stderr, OT_EXIT_INVALID_ARGUMENTS);
@@ -83,6 +83,8 @@ typedef struct otPlatformCoprocessorUrls
*/
typedef struct otPlatformConfig
{
const char *mTunDevice; ///< The POSIX TUN device path.
///< Used if `OT_PLATFORM_NETIF` is enabled.
const char *mBackboneInterfaceName; ///< Backbone network interface name.
const char *mInterfaceName; ///< Thread network interface name.
otPlatformCoprocessorUrls mCoprocessorUrls; ///< Coprocessor URLs.
+12 -5
View File
@@ -2033,7 +2033,7 @@ static void platformConfigureTunDevice(otPlatformConfig *aPlatformConfig)
struct ifreq ifr;
const char *interfaceName;
sTunFd = open(OPENTHREAD_POSIX_TUN_DEVICE, O_RDWR | O_CLOEXEC | O_NONBLOCK);
sTunFd = open(aPlatformConfig->mTunDevice, O_RDWR | O_CLOEXEC | O_NONBLOCK);
VerifyOrDie(sTunFd >= 0, OT_EXIT_ERROR_ERRNO);
memset(&ifr, 0, sizeof(ifr));
@@ -2134,9 +2134,7 @@ static void platformConfigureTunDevice(otPlatformConfig *aPlatformConfig)
const char *last_slash;
const char *path;
(void)aPlatformConfig;
path = OPENTHREAD_POSIX_TUN_DEVICE;
path = aPlatformConfig->mTunDevice;
sTunFd = open(path, O_RDWR | O_NONBLOCK);
VerifyOrDie(sTunFd >= 0, OT_EXIT_ERROR_ERRNO);
@@ -2150,7 +2148,7 @@ static void platformConfigureTunDevice(otPlatformConfig *aPlatformConfig)
err = ioctl(sTunFd, TUNSIFHEAD, &flags);
VerifyOrDie(err == 0, OT_EXIT_ERROR_ERRNO);
last_slash = strrchr(OPENTHREAD_POSIX_TUN_DEVICE, '/');
last_slash = strrchr(path, '/');
VerifyOrDie(last_slash != nullptr, OT_EXIT_ERROR_ERRNO);
last_slash++;
@@ -2234,6 +2232,15 @@ void platformNetifInit(otPlatformConfig *aPlatformConfig)
(void)LogNote;
(void)LogDebg;
#if defined(__linux__) || defined(__NetBSD__) || \
(defined(__APPLE__) && (OPENTHREAD_POSIX_CONFIG_MACOS_TUN_OPTION == OT_POSIX_CONFIG_MACOS_TUN)) || \
defined(__FreeBSD__)
if (aPlatformConfig->mTunDevice == nullptr)
{
aPlatformConfig->mTunDevice = OPENTHREAD_POSIX_TUN_DEVICE;
}
#endif
sIpFd = ot::Posix::SocketWithCloseExec(AF_INET6, SOCK_DGRAM, IPPROTO_IP, ot::Posix::kSocketNonBlock);
VerifyOrDie(sIpFd >= 0, OT_EXIT_ERROR_ERRNO);