From 86369858f719b3a55adedf70916899f121c41cc3 Mon Sep 17 00:00:00 2001 From: rob-the-dude <43481893+rob-the-dude@users.noreply.github.com> Date: Tue, 19 May 2020 17:26:26 -0700 Subject: [PATCH] [posix] allow configuration of macOS tunnel driver selection outside of code (#4980) In order to enable automated testing of mac OS, we're likely to need the ability to build for utun instead of tun, so make that option configurable via external options, and make the default utun, which will be easier for most developers to use (the caveats are documented in netif.cpp). --- src/posix/platform/netif.cpp | 29 ++++++++++---------- src/posix/platform/openthread-posix-config.h | 24 ++++++++++++++++ 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/posix/platform/netif.cpp b/src/posix/platform/netif.cpp index 9b890f25a..8bc6c9bb3 100644 --- a/src/posix/platform/netif.cpp +++ b/src/posix/platform/netif.cpp @@ -58,12 +58,11 @@ // If you want mDNS support, then you can't use Apple utun. I use the non-Apple driver // pretty much exclusively, because mDNS is a requirement. -#define USE_APPLE_UTUN 0 -#define USE_TUN 1 - -#if USE_TUN && USE_APPLE_UTUN -#error "You may not use both tun and utun." -#endif // USE_TUN && USE_APPLE_UTUN +#if !(defined(OPENTHREAD_POSIX_CONFIG_MACOS_TUN_OPTION) && \ + ((OPENTHREAD_POSIX_CONFIG_MACOS_TUN_OPTION == OT_POSIX_CONFIG_MACOS_UTUN) || \ + (OPENTHREAD_POSIX_CONFIG_MACOS_TUN_OPTION == OT_POSIX_CONFIG_MACOS_TUN))) +#error "Unexpected tunnel driver selection" +#endif #endif // defined(__APPLE__) @@ -118,16 +117,16 @@ extern int #include // ND6_INFINITE_LIFETIME #ifdef __APPLE__ -#if USE_APPLE_UTUN +#if OPENTHREAD_POSIX_CONFIG_MACOS_TUN_OPTION == OT_POSIX_CONFIG_MACOS_UTUN #include -#endif // USE_APPLE_UTUN +#endif -#if USE_TUN +#if OPENTHREAD_POSIX_CONFIG_MACOS_TUN_OPTION == OT_POSIX_CONFIG_MACOS_TUN #include // FIX ME: include the tun_ioctl.h file (challenging, as it's location depends on where the developer puts it) #define TUNSIFHEAD _IOW('t', 96, int) #define TUNGIFHEAD _IOR('t', 97, int) -#endif // USE_TUN +#endif #include #endif // defined(__APPLE__) @@ -156,9 +155,9 @@ extern int #elif defined(__NetBSD__) || defined(__FreeBSD__) #define OPENTHREAD_POSIX_TUN_DEVICE "/dev/tun0" #elif defined(__APPLE__) -#if USE_APPLE_UTUN +#if OPENTHREAD_POSIX_CONFIG_MACOS_TUN_OPTION == OT_POSIX_CONFIG_MACOS_UTUN #define OPENTHREAD_POSIX_TUN_DEVICE // not used - calculated dynamically -#elif USE_TUN +#elif OPENTHREAD_POSIX_CONFIG_MACOS_TUN_OPTION == OT_POSIX_CONFIG_MACOS_TUN #define OPENTHREAD_POSIX_TUN_DEVICE "/dev/tun0" #endif #else @@ -1207,7 +1206,7 @@ static void platformConfigureTunDevice(otInstance *aInstance, } #endif -#if defined(__APPLE__) && USE_APPLE_UTUN +#if defined(__APPLE__) && (OPENTHREAD_POSIX_CONFIG_MACOS_TUN_OPTION == OT_POSIX_CONFIG_MACOS_UTUN) static void platformConfigureTunDevice(otInstance *aInstance, const char *aInterfaceName, char * deviceName, @@ -1262,7 +1261,9 @@ exit: } #endif -#if defined(__NetBSD__) || (defined(__APPLE__) && USE_TUN) || defined(__FreeBSD__) +#if defined(__NetBSD__) || \ + (defined(__APPLE__) && (OPENTHREAD_POSIX_CONFIG_MACOS_TUN_OPTION == OT_POSIX_CONFIG_MACOS_TUN)) || \ + defined(__FreeBSD__) static void platformConfigureTunDevice(otInstance *aInstance, const char *aInterfaceName, char * deviceName, diff --git a/src/posix/platform/openthread-posix-config.h b/src/posix/platform/openthread-posix-config.h index fd4b8a028..223e8e26d 100644 --- a/src/posix/platform/openthread-posix-config.h +++ b/src/posix/platform/openthread-posix-config.h @@ -101,4 +101,28 @@ #define OPENTHREAD_POSIX_CONFIG_MAX_POWER_TABLE_ENABLE 0 #endif +#ifdef __APPLE__ + +/** + * Use built-in utun driver on mac OS + */ +#define OT_POSIX_CONFIG_MACOS_UTUN 1 + +/** + * Use open-source tun driver on mac OS + */ +#define OT_POSIX_CONFIG_MACOS_TUN 2 + +/** + * @def OPENTHREAD_POSIX_CONFIG_MACOS_TUN_OPTION + * + * This setting configures which tunnel driver to use. + * + */ +#ifndef OPENTHREAD_POSIX_CONFIG_MACOS_TUN_OPTION +#define OPENTHREAD_POSIX_CONFIG_MACOS_TUN_OPTION OT_POSIX_CONFIG_MACOS_UTUN +#endif + +#endif // __APPLE__ + #endif // OPENTHREAD_PLATFORM_CONFIG_H_