Skip to content

Commit 3759332

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 f4ab97d commit 3759332

File tree

5 files changed

+222
-1
lines changed

5 files changed

+222
-1
lines changed

drivers/ethernet/Kconfig.native_posix

+8
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

+180-1
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

3338
#if defined(CONFIG_NET_L2_ETHERNET)
@@ -67,6 +72,107 @@ static struct eth_context *get_context(struct net_if *iface)
6772
return net_if_get_device(iface)->driver_data;
6873
}
6974

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

195+
update_gptp(iface, pkt, true);
196+
89197
net_pkt_unref(pkt);
90198

91199
SYS_LOG_DBG("Send pkt %p len %d", pkt, count);
@@ -130,6 +238,7 @@ static struct net_if *get_iface(struct eth_context *context,
130238
static int read_data(struct eth_context *ctx, int fd)
131239
{
132240
u16_t vlan_tag = NET_VLAN_TAG_UNSPEC;
241+
struct net_if *iface;
133242
struct net_pkt *pkt;
134243
struct net_buf *frag;
135244
int ret;
@@ -172,12 +281,25 @@ static int read_data(struct eth_context *ctx, int fd)
172281
net_pkt_set_vlan_tci(pkt, ntohs(hdr_vlan->vlan.tci));
173282
vlan_tag = net_pkt_vlan_tag(pkt);
174283
}
284+
285+
#if CONFIG_NET_TC_RX_COUNT > 1
286+
{
287+
enum net_priority prio;
288+
289+
prio = net_vlan2priority(net_pkt_vlan_priority(pkt));
290+
net_pkt_set_priority(pkt, prio);
291+
}
292+
#endif
175293
}
176294
#endif
177295

178296
SYS_LOG_DBG("Recv pkt %p len %d", pkt, net_pkt_get_len(pkt));
179297

180-
if (net_recv_data(get_iface(ctx, vlan_tag), pkt) < 0) {
298+
iface = get_iface(ctx, vlan_tag);
299+
300+
update_gptp(iface, pkt, false);
301+
302+
if (net_recv_data(iface, pkt) < 0) {
181303
net_pkt_unref(pkt);
182304
}
183305

@@ -283,3 +405,60 @@ ETH_NET_DEVICE_INIT(eth_native_posix, CONFIG_ETH_NATIVE_POSIX_DRV_NAME,
283405
eth_init, &eth_context_data, NULL,
284406
CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &eth_if_api,
285407
_ETH_MTU);
408+
409+
#if defined(CONFIG_ETH_NATIVE_POSIX_PTP_CLOCK)
410+
static int ptp_clock_set_native_posix(struct ptp_clock *clk,
411+
struct net_ptp_time *tm)
412+
{
413+
ARG_UNUSED(clk);
414+
ARG_UNUSED(tm);
415+
416+
/* We cannot set the host device time so this function
417+
* does nothing.
418+
*/
419+
420+
return 0;
421+
}
422+
423+
static int ptp_clock_get_native_posix(struct ptp_clock *clk,
424+
struct net_ptp_time *tm)
425+
{
426+
return eth_clock_gettime(tm);
427+
}
428+
429+
static int ptp_clock_adjust_native_posix(struct ptp_clock *clk,
430+
int increment)
431+
{
432+
ARG_UNUSED(clk);
433+
ARG_UNUSED(increment);
434+
435+
/* We cannot adjust the host device time so this function
436+
* does nothing.
437+
*/
438+
439+
return 0;
440+
}
441+
442+
static int ptp_clock_rate_adjust_native_posix(struct ptp_clock *clk,
443+
float ratio)
444+
{
445+
ARG_UNUSED(clk);
446+
ARG_UNUSED(ratio);
447+
448+
/* We cannot adjust the host device time so this function
449+
* does nothing.
450+
*/
451+
452+
return 0;
453+
}
454+
455+
static const struct ptp_clock_driver_api api = {
456+
.set = ptp_clock_set_native_posix,
457+
.get = ptp_clock_get_native_posix,
458+
.adjust = ptp_clock_adjust_native_posix,
459+
.rate_adjust = ptp_clock_rate_adjust_native_posix,
460+
};
461+
462+
PTP_CLOCK_DEVICE_INIT(eth_native_posix, api);
463+
464+
#endif /* CONFIG_ETH_NATIVE_POSIX_PTP_CLOCK */

drivers/ethernet/eth_native_posix_adapt.c

+23
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

+4
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

+7
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)