Skip to content

Commit 7eee992

Browse files
committed
net: gptp: Initial support for native-posix board
Allow gPTP code to be run as a linux process and communicate with gPTP daemon running in linux host. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent 1225b9e commit 7eee992

File tree

5 files changed

+218
-0
lines changed

5 files changed

+218
-0
lines changed

drivers/ethernet/Kconfig.native_posix

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ config ETH_NATIVE_POSIX_DEV_NAME
4343
help
4444
This option sets the TUN/TAP device name in your host system.
4545

46+
config ETH_NATIVE_POSIX_PTP_CLOCK
47+
bool "PTP clock driver support"
48+
default n
49+
select PTP_CLOCK
50+
depends on NET_GPTP
51+
help
52+
Enable PTP clock support.
53+
4654
config ETH_NATIVE_POSIX_RANDOM_MAC
4755
bool "Random MAC address"
4856
depends on ENTROPY_GENERATOR

drivers/ethernet/eth_native_posix.c

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
#include <net/net_if.h>
2929
#include <net/ethernet.h>
3030

31+
#if defined(CONFIG_ETH_NATIVE_POSIX_PTP_CLOCK)
32+
#include <ptp_clock.h>
33+
#include <net/gptp_messages.h>
34+
#endif
35+
3136
#include "eth_native_posix_priv.h"
3237
#include "eth_stats.h"
3338

@@ -77,6 +82,107 @@ static struct eth_context *get_context(struct net_if *iface)
7782
return net_if_get_device(iface)->driver_data;
7883
}
7984

85+
#if defined(CONFIG_NET_GPTP)
86+
static bool need_timestamping(struct gptp_hdr *hdr)
87+
{
88+
switch (hdr->message_type) {
89+
case GPTP_SYNC_MESSAGE:
90+
case GPTP_PATH_DELAY_RESP_MESSAGE:
91+
return true;
92+
default:
93+
return false;
94+
}
95+
}
96+
97+
static struct gptp_hdr *check_gptp_msg(struct net_if *iface,
98+
struct net_pkt *pkt)
99+
{
100+
struct ethernet_context *eth_ctx;
101+
struct gptp_hdr *gptp_hdr;
102+
u8_t *msg_start;
103+
104+
if (net_pkt_ll_reserve(pkt)) {
105+
msg_start = net_pkt_ll(pkt);
106+
} else {
107+
msg_start = net_pkt_ip_data(pkt);
108+
}
109+
110+
#if defined(CONFIG_NET_VLAN)
111+
eth_ctx = net_if_l2_data(iface);
112+
if (net_eth_is_vlan_enabled(eth_ctx, iface)) {
113+
struct net_eth_vlan_hdr *hdr_vlan;
114+
115+
hdr_vlan = (struct net_eth_vlan_hdr *)msg_start;
116+
if (ntohs(hdr_vlan->type) != NET_ETH_PTYPE_PTP) {
117+
return NULL;
118+
}
119+
120+
gptp_hdr = (struct gptp_hdr *)(msg_start +
121+
sizeof(struct net_eth_vlan_hdr));
122+
} else
123+
#endif
124+
{
125+
struct net_eth_hdr *hdr;
126+
127+
hdr = (struct net_eth_hdr *)msg_start;
128+
if (ntohs(hdr->type) != NET_ETH_PTYPE_PTP) {
129+
return NULL;
130+
}
131+
132+
gptp_hdr = (struct gptp_hdr *)(msg_start +
133+
sizeof(struct net_eth_hdr));
134+
}
135+
136+
return gptp_hdr;
137+
}
138+
139+
static void update_pkt_priority(struct gptp_hdr *hdr, struct net_pkt *pkt)
140+
{
141+
switch (hdr->message_type) {
142+
case GPTP_SYNC_MESSAGE:
143+
case GPTP_DELAY_REQ_MESSAGE:
144+
case GPTP_PATH_DELAY_REQ_MESSAGE:
145+
case GPTP_PATH_DELAY_RESP_MESSAGE:
146+
net_pkt_set_priority(pkt, NET_PRIORITY_CA);
147+
break;
148+
default:
149+
net_pkt_set_priority(pkt, NET_PRIORITY_IC);
150+
break;
151+
}
152+
}
153+
154+
static void update_gptp(struct net_if *iface, struct net_pkt *pkt,
155+
bool send)
156+
{
157+
struct net_ptp_time timestamp;
158+
struct gptp_hdr *hdr;
159+
int ret;
160+
161+
ret = eth_clock_gettime(&timestamp);
162+
if (ret < 0) {
163+
return;
164+
}
165+
166+
net_pkt_set_timestamp(pkt, &timestamp);
167+
168+
hdr = check_gptp_msg(iface, pkt);
169+
if (!hdr) {
170+
return;
171+
}
172+
173+
if (send) {
174+
ret = need_timestamping(hdr);
175+
if (ret) {
176+
net_if_add_tx_timestamp(pkt);
177+
}
178+
} else {
179+
update_pkt_priority(hdr, pkt);
180+
}
181+
}
182+
#else
183+
#define update_gptp(iface, pkt, send)
184+
#endif /* CONFIG_NET_GPTP */
185+
80186
static int eth_send(struct net_if *iface, struct net_pkt *pkt)
81187
{
82188
struct eth_context *ctx = get_context(iface);
@@ -110,6 +216,8 @@ static int eth_send(struct net_if *iface, struct net_pkt *pkt)
110216
}
111217
}
112218

219+
update_gptp(iface, pkt, true);
220+
113221
SYS_LOG_DBG("Send pkt %p len %d", pkt, count);
114222

115223
eth_write_data(ctx->dev_fd, ctx->send, count);
@@ -200,6 +308,15 @@ static int read_data(struct eth_context *ctx, int fd)
200308
net_pkt_set_vlan_tci(pkt, ntohs(hdr_vlan->vlan.tci));
201309
vlan_tag = net_pkt_vlan_tag(pkt);
202310
}
311+
312+
#if CONFIG_NET_TC_RX_COUNT > 1
313+
{
314+
enum net_priority prio;
315+
316+
prio = net_vlan2priority(net_pkt_vlan_priority(pkt));
317+
net_pkt_set_priority(pkt, prio);
318+
}
319+
#endif
203320
}
204321
#endif
205322

@@ -222,6 +339,8 @@ static int read_data(struct eth_context *ctx, int fd)
222339

223340
SYS_LOG_DBG("Recv pkt %p len %d", pkt, pkt_len);
224341

342+
update_gptp(iface, pkt, false);
343+
225344
if (net_recv_data(iface, pkt) < 0) {
226345
net_pkt_unref(pkt);
227346
}
@@ -365,3 +484,60 @@ ETH_NET_DEVICE_INIT(eth_native_posix, CONFIG_ETH_NATIVE_POSIX_DRV_NAME,
365484
eth_init, &eth_context_data, NULL,
366485
CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &eth_if_api,
367486
_ETH_MTU);
487+
488+
#if defined(CONFIG_ETH_NATIVE_POSIX_PTP_CLOCK)
489+
static int ptp_clock_set_native_posix(struct ptp_clock *clk,
490+
struct net_ptp_time *tm)
491+
{
492+
ARG_UNUSED(clk);
493+
ARG_UNUSED(tm);
494+
495+
/* We cannot set the host device time so this function
496+
* does nothing.
497+
*/
498+
499+
return 0;
500+
}
501+
502+
static int ptp_clock_get_native_posix(struct ptp_clock *clk,
503+
struct net_ptp_time *tm)
504+
{
505+
return eth_clock_gettime(tm);
506+
}
507+
508+
static int ptp_clock_adjust_native_posix(struct ptp_clock *clk,
509+
int increment)
510+
{
511+
ARG_UNUSED(clk);
512+
ARG_UNUSED(increment);
513+
514+
/* We cannot adjust the host device time so this function
515+
* does nothing.
516+
*/
517+
518+
return 0;
519+
}
520+
521+
static int ptp_clock_rate_adjust_native_posix(struct ptp_clock *clk,
522+
float ratio)
523+
{
524+
ARG_UNUSED(clk);
525+
ARG_UNUSED(ratio);
526+
527+
/* We cannot adjust the host device time so this function
528+
* does nothing.
529+
*/
530+
531+
return 0;
532+
}
533+
534+
static const struct ptp_clock_driver_api api = {
535+
.set = ptp_clock_set_native_posix,
536+
.get = ptp_clock_get_native_posix,
537+
.adjust = ptp_clock_adjust_native_posix,
538+
.rate_adjust = ptp_clock_rate_adjust_native_posix,
539+
};
540+
541+
PTP_CLOCK_DEVICE_INIT(eth_native_posix, api);
542+
543+
#endif /* CONFIG_ETH_NATIVE_POSIX_PTP_CLOCK */

drivers/ethernet/eth_native_posix_adapt.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <sys/ioctl.h>
2424
#include <sys/socket.h>
2525
#include <net/if.h>
26+
#include <time.h>
2627

2728
#ifdef __linux
2829
#include <linux/if_tun.h>
@@ -38,6 +39,10 @@
3839
#include <sys_clock.h>
3940
#include <logging/sys_log.h>
4041

42+
#if defined(CONFIG_NET_GPTP)
43+
#include <net/gptp.h>
44+
#endif
45+
4146
#include "eth_native_posix_priv.h"
4247

4348
/* Note that we cannot create the TUN/TAP device from the setup script
@@ -138,3 +143,21 @@ ssize_t eth_write_data(int fd, void *buf, size_t buf_len)
138143
{
139144
return write(fd, buf, buf_len);
140145
}
146+
147+
#if defined(CONFIG_NET_GPTP)
148+
int eth_clock_gettime(struct net_ptp_time *time)
149+
{
150+
struct timespec tp;
151+
int ret;
152+
153+
ret = clock_gettime(CLOCK_MONOTONIC_RAW, &tp);
154+
if (ret < 0) {
155+
return -errno;
156+
}
157+
158+
time->second = tp.tv_sec;
159+
time->nanosecond = tp.tv_nsec;
160+
161+
return 0;
162+
}
163+
#endif /* CONFIG_NET_GPTP */

drivers/ethernet/eth_native_posix_priv.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ int eth_wait_data(int fd);
1818
ssize_t eth_read_data(int fd, void *buf, size_t buf_len);
1919
ssize_t eth_write_data(int fd, void *buf, size_t buf_len);
2020

21+
#if defined(CONFIG_NET_GPTP)
22+
int eth_clock_gettime(struct net_ptp_time *time);
23+
#endif
24+
2125
#endif /* _ETH_NATIVE_POSIX_PRIV_H */

samples/net/gptp/prj.conf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,10 @@ CONFIG_NET_TC_RX_COUNT=4
8585

8686
# Enable priority support in net_context
8787
CONFIG_NET_CONTEXT_PRIORITY=y
88+
89+
# Settings for native_posix ethernet driver (if compiled for that board)
90+
CONFIG_ETH_NATIVE_POSIX=y
91+
CONFIG_SYS_LOG_ETHERNET_LEVEL=1
92+
CONFIG_ETH_NATIVE_POSIX_PTP_CLOCK=y
93+
#CONFIG_ETH_NATIVE_POSIX_RANDOM_MAC=y
94+
CONFIG_ETH_NATIVE_POSIX_MAC_ADDR="00:00:5e:00:53:2a"

0 commit comments

Comments
 (0)