net: ptp: Add usermode support to net_eth_get_ptp_clock_by_index()

Make net_eth_get_ptp_clock_by_index() clock API to work with user space.
Create also unit test for testing this user mode support.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
This commit is contained in:
Jukka Rissanen 2019-05-27 12:44:01 +08:00
commit 95e8498c27
4 changed files with 77 additions and 14 deletions

View file

@ -13,6 +13,7 @@
#ifndef ZEPHYR_INCLUDE_NET_ETHERNET_H_ #ifndef ZEPHYR_INCLUDE_NET_ETHERNET_H_
#define ZEPHYR_INCLUDE_NET_ETHERNET_H_ #define ZEPHYR_INCLUDE_NET_ETHERNET_H_
#include <kernel.h>
#include <zephyr/types.h> #include <zephyr/types.h>
#include <stdbool.h> #include <stdbool.h>
#include <atomic.h> #include <atomic.h>
@ -691,16 +692,7 @@ static inline struct device *net_eth_get_ptp_clock(struct net_if *iface)
* @return Pointer to PTP clock if found, NULL if not found or if this * @return Pointer to PTP clock if found, NULL if not found or if this
* ethernet interface index does not support PTP. * ethernet interface index does not support PTP.
*/ */
#if defined(CONFIG_PTP_CLOCK) __syscall struct device *net_eth_get_ptp_clock_by_index(int index);
struct device *net_eth_get_ptp_clock_by_index(int index);
#else
static inline struct device *net_eth_get_ptp_clock_by_index(int index)
{
ARG_UNUSED(index);
return NULL;
}
#endif
/** /**
* @brief Return gPTP port number attached to this interface. * @brief Return gPTP port number attached to this interface.
@ -730,6 +722,8 @@ static inline int net_eth_get_ptp_port(struct net_if *iface)
void net_eth_set_ptp_port(struct net_if *iface, int port); void net_eth_set_ptp_port(struct net_if *iface, int port);
#endif /* CONFIG_NET_GPTP */ #endif /* CONFIG_NET_GPTP */
#include <syscalls/ethernet.h>
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View file

@ -19,6 +19,8 @@ LOG_MODULE_REGISTER(net_ethernet, CONFIG_NET_L2_ETHERNET_LOG_LEVEL);
#include <net/lldp.h> #include <net/lldp.h>
#endif #endif
#include <syscall_handler.h>
#include "arp.h" #include "arp.h"
#include "eth_stats.h" #include "eth_stats.h"
#include "net_private.h" #include "net_private.h"
@ -988,7 +990,7 @@ struct device *net_eth_get_ptp_clock(struct net_if *iface)
#endif /* CONFIG_PTP_CLOCK */ #endif /* CONFIG_PTP_CLOCK */
#if defined(CONFIG_PTP_CLOCK) #if defined(CONFIG_PTP_CLOCK)
struct device *net_eth_get_ptp_clock_by_index(int index) struct device *z_impl_net_eth_get_ptp_clock_by_index(int index)
{ {
struct net_if *iface; struct net_if *iface;
@ -999,7 +1001,21 @@ struct device *net_eth_get_ptp_clock_by_index(int index)
return net_eth_get_ptp_clock(iface); return net_eth_get_ptp_clock(iface);
} }
#endif
#ifdef CONFIG_USERSPACE
Z_SYSCALL_HANDLER(net_eth_get_ptp_clock_by_index, index)
{
return (u32_t)z_impl_net_eth_get_ptp_clock_by_index(index);
}
#endif /* CONFIG_USERSPACE */
#else /* CONFIG_PTP_CLOCK */
struct device *z_impl_net_eth_get_ptp_clock_by_index(int index)
{
ARG_UNUSED(index);
return NULL;
}
#endif /* CONFIG_PTP_CLOCK */
#if defined(CONFIG_NET_GPTP) #if defined(CONFIG_NET_GPTP)
int net_eth_get_ptp_port(struct net_if *iface) int net_eth_get_ptp_port(struct net_if *iface)

View file

@ -24,3 +24,5 @@ CONFIG_NET_SHELL=n
CONFIG_PTP_CLOCK=y CONFIG_PTP_CLOCK=y
CONFIG_ETH_NATIVE_POSIX=n CONFIG_ETH_NATIVE_POSIX=n
CONFIG_COVERAGE=n CONFIG_COVERAGE=n
CONFIG_TEST_USERSPACE=y
CONFIG_HEAP_MEM_POOL_SIZE=128

View file

@ -60,6 +60,7 @@ static struct in6_addr ll_addr = { { { 0xfe, 0x80, 0x43, 0xb8, 0, 0, 0, 0,
/* Keep track of all ethernet interfaces */ /* Keep track of all ethernet interfaces */
static struct net_if *eth_interfaces[MAX_NUM_INTERFACES]; static struct net_if *eth_interfaces[MAX_NUM_INTERFACES];
static ZTEST_BMEM int ptp_clocks[MAX_NUM_INTERFACES - 1];
static int ptp_interface[MAX_NUM_INTERFACES - 1]; static int ptp_interface[MAX_NUM_INTERFACES - 1];
static int non_ptp_interface; static int non_ptp_interface;
static bool test_failed; static bool test_failed;
@ -298,7 +299,9 @@ static void iface_cb(struct net_if *iface, void *user_data)
if (!clk) { if (!clk) {
non_ptp_interface = ud->eth_if_count; non_ptp_interface = ud->eth_if_count;
} else { } else {
ptp_interface[ptp_iface_idx++] = ud->eth_if_count; ptp_interface[ptp_iface_idx] = ud->eth_if_count;
ptp_clocks[ptp_iface_idx] = net_if_get_by_iface(iface);
ptp_iface_idx++;
} }
eth_interfaces[ud->eth_if_count++] = iface; eth_interfaces[ud->eth_if_count++] = iface;
@ -455,6 +458,52 @@ static void test_ptp_clock_iface_2(void)
test_ptp_clock_iface(ptp_interface[1]); test_ptp_clock_iface(ptp_interface[1]);
} }
static ZTEST_BMEM struct device *clk0;
static ZTEST_BMEM struct device *clk1;
static void test_ptp_clock_get_by_index(void)
{
struct device *clk, *clk_by_index;
int idx;
idx = ptp_interface[0];
clk = net_eth_get_ptp_clock(eth_interfaces[idx]);
zassert_not_null(clk, "PTP 0 not found");
clk0 = clk;
clk_by_index = net_eth_get_ptp_clock_by_index(ptp_clocks[0]);
zassert_not_null(clk_by_index, "PTP 0 not found");
zassert_equal(clk, clk_by_index, "Interface index %d invalid", idx);
idx = ptp_interface[1];
clk = net_eth_get_ptp_clock(eth_interfaces[idx]);
zassert_not_null(clk, "PTP 1 not found");
clk1 = clk;
clk_by_index = net_eth_get_ptp_clock_by_index(ptp_clocks[1]);
zassert_not_null(clk_by_index, "PTP 1 not found");
zassert_equal(clk, clk_by_index, "Interface index %d invalid", idx);
}
static void test_ptp_clock_get_by_index_user(void)
{
struct device *clk_by_index;
clk_by_index = net_eth_get_ptp_clock_by_index(ptp_clocks[0]);
zassert_not_null(clk_by_index, "PTP 0 not found");
zassert_equal(clk0, clk_by_index, "Invalid PTP clock 0");
clk_by_index = net_eth_get_ptp_clock_by_index(ptp_clocks[1]);
zassert_not_null(clk_by_index, "PTP 1 not found");
zassert_equal(clk1, clk_by_index, "Invalid PTP clock 1");
}
void test_main(void) void test_main(void)
{ {
ztest_test_suite(ptp_clock_test, ztest_test_suite(ptp_clock_test,
@ -462,7 +511,9 @@ void test_main(void)
ztest_unit_test(address_setup), ztest_unit_test(address_setup),
ztest_unit_test(test_ptp_clock_interfaces), ztest_unit_test(test_ptp_clock_interfaces),
ztest_unit_test(test_ptp_clock_iface_1), ztest_unit_test(test_ptp_clock_iface_1),
ztest_unit_test(test_ptp_clock_iface_2) ztest_unit_test(test_ptp_clock_iface_2),
ztest_unit_test(test_ptp_clock_get_by_index),
ztest_user_unit_test(test_ptp_clock_get_by_index_user)
); );
ztest_run_test_suite(ptp_clock_test); ztest_run_test_suite(ptp_clock_test);