[posix-host] set network interface name (#4573)

When platform netif is enabled, the TUN device's name should be able to
set from command. This PR use the existing `-I` to allow user provide a
wanted name.
This commit is contained in:
Yakun Xu
2020-02-19 11:47:55 -08:00
committed by GitHub
parent abf108a9b1
commit cc3e5ba6f4
5 changed files with 25 additions and 5 deletions
+9
View File
@@ -80,6 +80,15 @@ check() {
OT_CLI_CMD="$(pwd)/$(ls output/posix/*linux*/bin/ot-cli) ${OT_NCP_PATH} ${CORE_PTY}"
fi
# Cover setting a valid network interface name.
readonly VALID_NETIF_NAME="wan$(date +%H%M%S)"
sudo ${OT_CLI_CMD} -I "${VALID_NETIF_NAME}" -n
# Cover setting a too long(max is 15 characters) network interface name.
# Expect exit code to be 2(OT_EXIT_INVALID_ARGUMENTS).
readonly INVALID_NETIF_NAME="wan0123456789123"
sudo ${OT_CLI_CMD} -I "${INVALID_NETIF_NAME}" -n || test $? = 2
if [[ "${DAEMON}" = 1 ]]; then
sudo ${OT_CLI_CMD} panid 0xface | grep 'Done' || die 'failed to set panid with ot-ctl'
fi
+1 -1
View File
@@ -180,7 +180,7 @@ static void PrintUsage(const char *aProgramName, FILE *aStream, int aExitCode)
static void ParseArg(int aArgCount, char *aArgVector[], PosixConfig *aConfig)
{
memset(aConfig, 0, sizeof(PosixConfig));
memset(aConfig, 0, sizeof(*aConfig));
aConfig->mPlatformConfig.mSpeedUpFactor = 1;
aConfig->mPlatformConfig.mResetRadio = true;
+12 -2
View File
@@ -384,7 +384,7 @@ exit:
return;
}
void platformNetifInit(otInstance *aInstance)
void platformNetifInit(otInstance *aInstance, const char *aInterfaceName)
{
struct ifreq ifr;
@@ -410,7 +410,17 @@ void platformNetifInit(otInstance *aInstance)
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
strncpy(ifr.ifr_name, "wpan%d", IFNAMSIZ);
if (aInterfaceName)
{
VerifyOrDie(strlen(aInterfaceName) < IFNAMSIZ, OT_EXIT_INVALID_ARGUMENTS);
strncpy(ifr.ifr_name, aInterfaceName, IFNAMSIZ);
}
else
{
strncpy(ifr.ifr_name, "wpan%d", IFNAMSIZ);
}
VerifyOrExit(ioctl(sTunFd, TUNSETIFF, static_cast<void *>(&ifr)) == 0,
otLogCritPlat("Unable to configure tun device %s", OPENTHREAD_POSIX_TUN_DEVICE));
+2 -1
View File
@@ -276,9 +276,10 @@ void platformUartProcess(const fd_set *aReadFdSet, const fd_set *aWriteFdSet, co
* This function initializes platform netif.
*
* @param[in] aInstance A pointer to the OpenThread instance.
* @param[in] aInterfaceName A pointer to Thread network interface name.
*
*/
void platformNetifInit(otInstance *aInstance);
void platformNetifInit(otInstance *aInstance, const char *aInterfaceName);
/**
* This function updates the file descriptor sets with file descriptors used by platform netif module.
+1 -1
View File
@@ -59,7 +59,7 @@ otInstance *otSysInit(otPlatformConfig *aPlatformConfig)
assert(instance != NULL);
#if OPENTHREAD_CONFIG_PLATFORM_NETIF_ENABLE
platformNetifInit(instance);
platformNetifInit(instance, aPlatformConfig->mInterfaceName);
#elif OPENTHREAD_CONFIG_PLATFORM_UDP_ENABLE
platformUdpInit(aPlatformConfig->mInterfaceName);
#endif