28
28
#include <net/net_if.h>
29
29
#include <net/ethernet.h>
30
30
31
+ #if defined(CONFIG_ETH_NATIVE_POSIX_PTP_CLOCK )
32
+ #include <ptp_clock.h>
33
+ #include <net/gptp_messages.h>
34
+ #endif
35
+
31
36
#include "eth_native_posix_priv.h"
32
37
33
38
#if defined(CONFIG_NET_L2_ETHERNET )
@@ -67,6 +72,107 @@ static struct eth_context *get_context(struct net_if *iface)
67
72
return net_if_get_device (iface )-> driver_data ;
68
73
}
69
74
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
+
70
176
static int eth_send (struct net_if * iface , struct net_pkt * pkt )
71
177
{
72
178
struct eth_context * ctx = get_context (iface );
@@ -86,6 +192,8 @@ static int eth_send(struct net_if *iface, struct net_pkt *pkt)
86
192
frag = frag -> frags ;
87
193
}
88
194
195
+ update_gptp (iface , pkt , true);
196
+
89
197
net_pkt_unref (pkt );
90
198
91
199
SYS_LOG_DBG ("Send pkt %p len %d" , pkt , count );
@@ -130,6 +238,7 @@ static struct net_if *get_iface(struct eth_context *context,
130
238
static int read_data (struct eth_context * ctx , int fd )
131
239
{
132
240
u16_t vlan_tag = NET_VLAN_TAG_UNSPEC ;
241
+ struct net_if * iface ;
133
242
struct net_pkt * pkt ;
134
243
struct net_buf * frag ;
135
244
int ret ;
@@ -172,12 +281,25 @@ static int read_data(struct eth_context *ctx, int fd)
172
281
net_pkt_set_vlan_tci (pkt , ntohs (hdr_vlan -> vlan .tci ));
173
282
vlan_tag = net_pkt_vlan_tag (pkt );
174
283
}
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
175
293
}
176
294
#endif
177
295
178
296
SYS_LOG_DBG ("Recv pkt %p len %d" , pkt , net_pkt_get_len (pkt ));
179
297
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 ) {
181
303
net_pkt_unref (pkt );
182
304
}
183
305
@@ -283,3 +405,60 @@ ETH_NET_DEVICE_INIT(eth_native_posix, CONFIG_ETH_NATIVE_POSIX_DRV_NAME,
283
405
eth_init , & eth_context_data , NULL ,
284
406
CONFIG_KERNEL_INIT_PRIORITY_DEFAULT , & eth_if_api ,
285
407
_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 */
0 commit comments