xref: /dpdk/examples/ptpclient/ptpclient.c (revision 2490bb897182f57de80fd924dd3ae48dda819b8c)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015 Intel Corporation
3  */
4 
5 /*
6  * This application is a simple Layer 2 PTP v2 client. It shows delta values
7  * which are used to synchronize the PHC clock. if the "-T 1" parameter is
8  * passed to the application the Linux kernel clock is also synchronized.
9  */
10 
11 #include <stdint.h>
12 #include <inttypes.h>
13 #include <rte_eal.h>
14 #include <rte_ethdev.h>
15 #include <rte_cycles.h>
16 #include <rte_lcore.h>
17 #include <rte_mbuf.h>
18 #include <rte_ip.h>
19 #include <limits.h>
20 #include <sys/time.h>
21 #include <getopt.h>
22 
23 #define RX_RING_SIZE 1024
24 #define TX_RING_SIZE 1024
25 
26 #define NUM_MBUFS            8191
27 #define MBUF_CACHE_SIZE       250
28 
29 /* Values for the PTP messageType field. */
30 #define SYNC                  0x0
31 #define DELAY_REQ             0x1
32 #define PDELAY_REQ            0x2
33 #define PDELAY_RESP           0x3
34 #define FOLLOW_UP             0x8
35 #define DELAY_RESP            0x9
36 #define PDELAY_RESP_FOLLOW_UP 0xA
37 #define ANNOUNCE              0xB
38 #define SIGNALING             0xC
39 #define MANAGEMENT            0xD
40 
41 #define NSEC_PER_SEC        1000000000L
42 #define KERNEL_TIME_ADJUST_LIMIT  20000
43 #define PTP_PROTOCOL             0x88F7
44 
45 struct rte_mempool *mbuf_pool;
46 uint32_t ptp_enabled_port_mask;
47 uint8_t ptp_enabled_port_nb;
48 static uint8_t ptp_enabled_ports[RTE_MAX_ETHPORTS];
49 
50 static const struct rte_ether_addr ether_multicast = {
51 	.addr_bytes = {0x01, 0x1b, 0x19, 0x0, 0x0, 0x0}
52 };
53 
54 /* Structs used for PTP handling. */
55 struct tstamp {
56 	uint16_t   sec_msb;
57 	uint32_t   sec_lsb;
58 	uint32_t   ns;
59 }  __rte_packed;
60 
61 struct clock_id {
62 	uint8_t id[8];
63 };
64 
65 struct port_id {
66 	struct clock_id        clock_id;
67 	uint16_t               port_number;
68 }  __rte_packed;
69 
70 struct ptp_header {
71 	uint8_t              msg_type;
72 	uint8_t              ver;
73 	uint16_t             message_length;
74 	uint8_t              domain_number;
75 	uint8_t              reserved1;
76 	uint8_t              flag_field[2];
77 	int64_t              correction;
78 	uint32_t             reserved2;
79 	struct port_id       source_port_id;
80 	uint16_t             seq_id;
81 	uint8_t              control;
82 	int8_t               log_message_interval;
83 } __rte_packed;
84 
85 struct sync_msg {
86 	struct ptp_header   hdr;
87 	struct tstamp       origin_tstamp;
88 } __rte_packed;
89 
90 struct follow_up_msg {
91 	struct ptp_header   hdr;
92 	struct tstamp       precise_origin_tstamp;
93 	uint8_t             suffix[0];
94 } __rte_packed;
95 
96 struct delay_req_msg {
97 	struct ptp_header   hdr;
98 	struct tstamp       origin_tstamp;
99 } __rte_packed;
100 
101 struct delay_resp_msg {
102 	struct ptp_header    hdr;
103 	struct tstamp        rx_tstamp;
104 	struct port_id       req_port_id;
105 	uint8_t              suffix[0];
106 } __rte_packed;
107 
108 struct ptp_message {
109 	union {
110 		struct ptp_header          header;
111 		struct sync_msg            sync;
112 		struct delay_req_msg       delay_req;
113 		struct follow_up_msg       follow_up;
114 		struct delay_resp_msg      delay_resp;
115 	} __rte_packed;
116 };
117 
118 struct ptpv2_data_slave_ordinary {
119 	struct rte_mbuf *m;
120 	struct timespec tstamp1;
121 	struct timespec tstamp2;
122 	struct timespec tstamp3;
123 	struct timespec tstamp4;
124 	struct clock_id client_clock_id;
125 	struct clock_id master_clock_id;
126 	struct timeval new_adj;
127 	int64_t delta;
128 	uint16_t portid;
129 	uint16_t seqID_SYNC;
130 	uint16_t seqID_FOLLOWUP;
131 	uint8_t ptpset;
132 	uint8_t kernel_time_set;
133 	uint16_t current_ptp_port;
134 };
135 
136 static struct ptpv2_data_slave_ordinary ptp_data;
137 
138 static inline uint64_t timespec64_to_ns(const struct timespec *ts)
139 {
140 	return ((uint64_t) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
141 }
142 
143 static struct timeval
144 ns_to_timeval(int64_t nsec)
145 {
146 	struct timespec t_spec = {0, 0};
147 	struct timeval t_eval = {0, 0};
148 	int32_t rem;
149 
150 	if (nsec == 0)
151 		return t_eval;
152 	rem = nsec % NSEC_PER_SEC;
153 	t_spec.tv_sec = nsec / NSEC_PER_SEC;
154 
155 	if (rem < 0) {
156 		t_spec.tv_sec--;
157 		rem += NSEC_PER_SEC;
158 	}
159 
160 	t_spec.tv_nsec = rem;
161 	t_eval.tv_sec = t_spec.tv_sec;
162 	t_eval.tv_usec = t_spec.tv_nsec / 1000;
163 
164 	return t_eval;
165 }
166 
167 /*
168  * Initializes a given port using global settings and with the RX buffers
169  * coming from the mbuf_pool passed as a parameter.
170  */
171 static inline int
172 port_init(uint16_t port, struct rte_mempool *mbuf_pool)
173 {
174 	struct rte_eth_dev_info dev_info;
175 	struct rte_eth_conf port_conf;
176 	const uint16_t rx_rings = 1;
177 	const uint16_t tx_rings = 1;
178 	int retval;
179 	uint16_t q;
180 	uint16_t nb_rxd = RX_RING_SIZE;
181 	uint16_t nb_txd = TX_RING_SIZE;
182 
183 	if (!rte_eth_dev_is_valid_port(port))
184 		return -1;
185 
186 	memset(&port_conf, 0, sizeof(struct rte_eth_conf));
187 
188 	retval = rte_eth_dev_info_get(port, &dev_info);
189 	if (retval != 0) {
190 		printf("Error during getting device (port %u) info: %s\n",
191 				port, strerror(-retval));
192 
193 		return retval;
194 	}
195 
196 	if (dev_info.rx_offload_capa & RTE_ETH_RX_OFFLOAD_TIMESTAMP)
197 		port_conf.rxmode.offloads |= RTE_ETH_RX_OFFLOAD_TIMESTAMP;
198 
199 	if (dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE)
200 		port_conf.txmode.offloads |=
201 			RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE;
202 	/* Force full Tx path in the driver, required for IEEE1588 */
203 	port_conf.txmode.offloads |= RTE_ETH_TX_OFFLOAD_MULTI_SEGS;
204 
205 	/* Configure the Ethernet device. */
206 	retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
207 	if (retval != 0)
208 		return retval;
209 
210 	retval = rte_eth_dev_adjust_nb_rx_tx_desc(port, &nb_rxd, &nb_txd);
211 	if (retval != 0)
212 		return retval;
213 
214 	/* Allocate and set up 1 RX queue per Ethernet port. */
215 	for (q = 0; q < rx_rings; q++) {
216 		struct rte_eth_rxconf *rxconf;
217 
218 		rxconf = &dev_info.default_rxconf;
219 		rxconf->offloads = port_conf.rxmode.offloads;
220 
221 		retval = rte_eth_rx_queue_setup(port, q, nb_rxd,
222 				rte_eth_dev_socket_id(port), rxconf, mbuf_pool);
223 
224 		if (retval < 0)
225 			return retval;
226 	}
227 
228 	/* Allocate and set up 1 TX queue per Ethernet port. */
229 	for (q = 0; q < tx_rings; q++) {
230 		struct rte_eth_txconf *txconf;
231 
232 		txconf = &dev_info.default_txconf;
233 		txconf->offloads = port_conf.txmode.offloads;
234 
235 		retval = rte_eth_tx_queue_setup(port, q, nb_txd,
236 				rte_eth_dev_socket_id(port), txconf);
237 		if (retval < 0)
238 			return retval;
239 	}
240 
241 	/* Start the Ethernet port. */
242 	retval = rte_eth_dev_start(port);
243 	if (retval < 0)
244 		return retval;
245 
246 	/* Enable timesync timestamping for the Ethernet device */
247 	retval = rte_eth_timesync_enable(port);
248 	if (retval < 0) {
249 		printf("Timesync enable failed: %d\n", retval);
250 		return retval;
251 	}
252 
253 	/* Enable RX in promiscuous mode for the Ethernet device. */
254 	retval = rte_eth_promiscuous_enable(port);
255 	if (retval != 0) {
256 		printf("Promiscuous mode enable failed: %s\n",
257 			rte_strerror(-retval));
258 		return retval;
259 	}
260 
261 	return 0;
262 }
263 
264 static void
265 print_clock_info(struct ptpv2_data_slave_ordinary *ptp_data)
266 {
267 	int64_t nsec;
268 	struct timespec net_time, sys_time;
269 
270 	printf("Master Clock id: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
271 		ptp_data->master_clock_id.id[0],
272 		ptp_data->master_clock_id.id[1],
273 		ptp_data->master_clock_id.id[2],
274 		ptp_data->master_clock_id.id[3],
275 		ptp_data->master_clock_id.id[4],
276 		ptp_data->master_clock_id.id[5],
277 		ptp_data->master_clock_id.id[6],
278 		ptp_data->master_clock_id.id[7]);
279 
280 	printf("\nT2 - Slave  Clock.  %lds %ldns",
281 			(ptp_data->tstamp2.tv_sec),
282 			(ptp_data->tstamp2.tv_nsec));
283 
284 	printf("\nT1 - Master Clock.  %lds %ldns ",
285 			ptp_data->tstamp1.tv_sec,
286 			(ptp_data->tstamp1.tv_nsec));
287 
288 	printf("\nT3 - Slave  Clock.  %lds %ldns",
289 			ptp_data->tstamp3.tv_sec,
290 			(ptp_data->tstamp3.tv_nsec));
291 
292 	printf("\nT4 - Master Clock.  %lds %ldns ",
293 			ptp_data->tstamp4.tv_sec,
294 			(ptp_data->tstamp4.tv_nsec));
295 
296 	printf("\nDelta between master and slave clocks:%"PRId64"ns\n",
297 			ptp_data->delta);
298 
299 	clock_gettime(CLOCK_REALTIME, &sys_time);
300 	rte_eth_timesync_read_time(ptp_data->current_ptp_port, &net_time);
301 
302 	time_t ts = net_time.tv_sec;
303 
304 	printf("\n\nComparison between Linux kernel Time and PTP:");
305 
306 	printf("\nCurrent PTP Time: %.24s %.9ld ns",
307 			ctime(&ts), net_time.tv_nsec);
308 
309 	nsec = (int64_t)timespec64_to_ns(&net_time) -
310 			(int64_t)timespec64_to_ns(&sys_time);
311 	ptp_data->new_adj = ns_to_timeval(nsec);
312 
313 	gettimeofday(&ptp_data->new_adj, NULL);
314 
315 	time_t tp = ptp_data->new_adj.tv_sec;
316 
317 	printf("\nCurrent SYS Time: %.24s %.6ld ns",
318 				ctime(&tp), ptp_data->new_adj.tv_usec);
319 
320 	printf("\nDelta between PTP and Linux Kernel time:%"PRId64"ns\n",
321 				nsec);
322 
323 	printf("[Ctrl+C to quit]\n");
324 
325 	/* Clear screen and put cursor in column 1, row 1 */
326 	printf("\033[2J\033[1;1H");
327 }
328 
329 static int64_t
330 delta_eval(struct ptpv2_data_slave_ordinary *ptp_data)
331 {
332 	int64_t delta;
333 	uint64_t t1 = 0;
334 	uint64_t t2 = 0;
335 	uint64_t t3 = 0;
336 	uint64_t t4 = 0;
337 
338 	t1 = timespec64_to_ns(&ptp_data->tstamp1);
339 	t2 = timespec64_to_ns(&ptp_data->tstamp2);
340 	t3 = timespec64_to_ns(&ptp_data->tstamp3);
341 	t4 = timespec64_to_ns(&ptp_data->tstamp4);
342 
343 	delta = -((int64_t)((t2 - t1) - (t4 - t3))) / 2;
344 
345 	return delta;
346 }
347 
348 /*
349  * Parse the PTP SYNC message.
350  */
351 static void
352 parse_sync(struct ptpv2_data_slave_ordinary *ptp_data, uint16_t rx_tstamp_idx)
353 {
354 	struct ptp_header *ptp_hdr;
355 
356 	ptp_hdr = (struct ptp_header *)(rte_pktmbuf_mtod(ptp_data->m, char *)
357 			+ sizeof(struct rte_ether_hdr));
358 	ptp_data->seqID_SYNC = rte_be_to_cpu_16(ptp_hdr->seq_id);
359 
360 	if (ptp_data->ptpset == 0) {
361 		rte_memcpy(&ptp_data->master_clock_id,
362 				&ptp_hdr->source_port_id.clock_id,
363 				sizeof(struct clock_id));
364 		ptp_data->ptpset = 1;
365 	}
366 
367 	if (memcmp(&ptp_hdr->source_port_id.clock_id,
368 			&ptp_hdr->source_port_id.clock_id,
369 			sizeof(struct clock_id)) == 0) {
370 
371 		if (ptp_data->ptpset == 1)
372 			rte_eth_timesync_read_rx_timestamp(ptp_data->portid,
373 					&ptp_data->tstamp2, rx_tstamp_idx);
374 	}
375 
376 }
377 
378 /*
379  * Parse the PTP FOLLOWUP message and send DELAY_REQ to the main clock.
380  */
381 static void
382 parse_fup(struct ptpv2_data_slave_ordinary *ptp_data)
383 {
384 	struct rte_ether_hdr *eth_hdr;
385 	struct rte_ether_addr eth_addr;
386 	struct ptp_header *ptp_hdr;
387 	struct clock_id *client_clkid;
388 	struct ptp_message *ptp_msg;
389 	struct rte_mbuf *created_pkt;
390 	struct tstamp *origin_tstamp;
391 	struct rte_ether_addr eth_multicast = ether_multicast;
392 	size_t pkt_size;
393 	int wait_us;
394 	struct rte_mbuf *m = ptp_data->m;
395 	int ret;
396 
397 	eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
398 	ptp_hdr = (struct ptp_header *)(rte_pktmbuf_mtod(m, char *)
399 			+ sizeof(struct rte_ether_hdr));
400 	if (memcmp(&ptp_data->master_clock_id,
401 			&ptp_hdr->source_port_id.clock_id,
402 			sizeof(struct clock_id)) != 0)
403 		return;
404 
405 	ptp_data->seqID_FOLLOWUP = rte_be_to_cpu_16(ptp_hdr->seq_id);
406 	ptp_msg = (struct ptp_message *) (rte_pktmbuf_mtod(m, char *) +
407 					  sizeof(struct rte_ether_hdr));
408 
409 	origin_tstamp = &ptp_msg->follow_up.precise_origin_tstamp;
410 	ptp_data->tstamp1.tv_nsec = ntohl(origin_tstamp->ns);
411 	ptp_data->tstamp1.tv_sec =
412 		((uint64_t)ntohl(origin_tstamp->sec_lsb)) |
413 		(((uint64_t)ntohs(origin_tstamp->sec_msb)) << 32);
414 
415 	if (ptp_data->seqID_FOLLOWUP == ptp_data->seqID_SYNC) {
416 		ret = rte_eth_macaddr_get(ptp_data->portid, &eth_addr);
417 		if (ret != 0) {
418 			printf("\nCore %u: port %u failed to get MAC address: %s\n",
419 				rte_lcore_id(), ptp_data->portid,
420 				rte_strerror(-ret));
421 			return;
422 		}
423 
424 		created_pkt = rte_pktmbuf_alloc(mbuf_pool);
425 		pkt_size = sizeof(struct rte_ether_hdr) +
426 			sizeof(struct ptp_message);
427 		created_pkt->data_len = pkt_size;
428 		created_pkt->pkt_len = pkt_size;
429 		eth_hdr = rte_pktmbuf_mtod(created_pkt, struct rte_ether_hdr *);
430 		rte_ether_addr_copy(&eth_addr, &eth_hdr->src_addr);
431 
432 		/* Set multicast address 01-1B-19-00-00-00. */
433 		rte_ether_addr_copy(&eth_multicast, &eth_hdr->dst_addr);
434 
435 		eth_hdr->ether_type = htons(PTP_PROTOCOL);
436 		ptp_msg = (struct ptp_message *)
437 			(rte_pktmbuf_mtod(created_pkt, char *) +
438 			sizeof(struct rte_ether_hdr));
439 
440 		ptp_msg->delay_req.hdr.seq_id = htons(ptp_data->seqID_SYNC);
441 		ptp_msg->delay_req.hdr.msg_type = DELAY_REQ;
442 		ptp_msg->delay_req.hdr.ver = 2;
443 		ptp_msg->delay_req.hdr.control = 1;
444 		ptp_msg->delay_req.hdr.log_message_interval = 127;
445 		ptp_msg->delay_req.hdr.message_length =
446 			htons(sizeof(struct delay_req_msg));
447 		ptp_msg->delay_req.hdr.domain_number = ptp_hdr->domain_number;
448 
449 		/* Set up clock id. */
450 		client_clkid =
451 			&ptp_msg->delay_req.hdr.source_port_id.clock_id;
452 
453 		client_clkid->id[0] = eth_hdr->src_addr.addr_bytes[0];
454 		client_clkid->id[1] = eth_hdr->src_addr.addr_bytes[1];
455 		client_clkid->id[2] = eth_hdr->src_addr.addr_bytes[2];
456 		client_clkid->id[3] = 0xFF;
457 		client_clkid->id[4] = 0xFE;
458 		client_clkid->id[5] = eth_hdr->src_addr.addr_bytes[3];
459 		client_clkid->id[6] = eth_hdr->src_addr.addr_bytes[4];
460 		client_clkid->id[7] = eth_hdr->src_addr.addr_bytes[5];
461 
462 		rte_memcpy(&ptp_data->client_clock_id,
463 			   client_clkid,
464 			   sizeof(struct clock_id));
465 
466 		/* Enable flag for hardware timestamping. */
467 		created_pkt->ol_flags |= RTE_MBUF_F_TX_IEEE1588_TMST;
468 
469 		/*Read value from NIC to prevent latching with old value. */
470 		rte_eth_timesync_read_tx_timestamp(ptp_data->portid,
471 				&ptp_data->tstamp3);
472 
473 		/* Transmit the packet. */
474 		rte_eth_tx_burst(ptp_data->portid, 0, &created_pkt, 1);
475 
476 		wait_us = 0;
477 		ptp_data->tstamp3.tv_nsec = 0;
478 		ptp_data->tstamp3.tv_sec = 0;
479 
480 		/* Wait at least 1 us to read TX timestamp. */
481 		while ((rte_eth_timesync_read_tx_timestamp(ptp_data->portid,
482 				&ptp_data->tstamp3) < 0) && (wait_us < 1000)) {
483 			rte_delay_us(1);
484 			wait_us++;
485 		}
486 	}
487 }
488 
489 /*
490  * Update the kernel time with the difference between it and the current NIC
491  * time.
492  */
493 static inline void
494 update_kernel_time(void)
495 {
496 	int64_t nsec;
497 	struct timespec net_time, sys_time;
498 
499 	clock_gettime(CLOCK_REALTIME, &sys_time);
500 	rte_eth_timesync_read_time(ptp_data.current_ptp_port, &net_time);
501 
502 	nsec = (int64_t)timespec64_to_ns(&net_time) -
503 	       (int64_t)timespec64_to_ns(&sys_time);
504 
505 	ptp_data.new_adj = ns_to_timeval(nsec);
506 
507 	/*
508 	 * If difference between kernel time and system time in NIC is too big
509 	 * (more than +/- 20 microseconds), use clock_settime to set directly
510 	 * the kernel time, as adjtime is better for small adjustments (takes
511 	 * longer to adjust the time).
512 	 */
513 
514 	if (nsec > KERNEL_TIME_ADJUST_LIMIT || nsec < -KERNEL_TIME_ADJUST_LIMIT)
515 		clock_settime(CLOCK_REALTIME, &net_time);
516 	else
517 		adjtime(&ptp_data.new_adj, 0);
518 
519 
520 }
521 
522 /*
523  * Parse the DELAY_RESP message.
524  */
525 static void
526 parse_drsp(struct ptpv2_data_slave_ordinary *ptp_data)
527 {
528 	struct rte_mbuf *m = ptp_data->m;
529 	struct ptp_message *ptp_msg;
530 	struct tstamp *rx_tstamp;
531 	uint16_t seq_id;
532 
533 	ptp_msg = (struct ptp_message *) (rte_pktmbuf_mtod(m, char *) +
534 					sizeof(struct rte_ether_hdr));
535 	seq_id = rte_be_to_cpu_16(ptp_msg->delay_resp.hdr.seq_id);
536 	if (memcmp(&ptp_data->client_clock_id,
537 		   &ptp_msg->delay_resp.req_port_id.clock_id,
538 		   sizeof(struct clock_id)) == 0) {
539 		if (seq_id == ptp_data->seqID_FOLLOWUP) {
540 			rx_tstamp = &ptp_msg->delay_resp.rx_tstamp;
541 			ptp_data->tstamp4.tv_nsec = ntohl(rx_tstamp->ns);
542 			ptp_data->tstamp4.tv_sec =
543 				((uint64_t)ntohl(rx_tstamp->sec_lsb)) |
544 				(((uint64_t)ntohs(rx_tstamp->sec_msb)) << 32);
545 
546 			/* Evaluate the delta for adjustment. */
547 			ptp_data->delta = delta_eval(ptp_data);
548 
549 			rte_eth_timesync_adjust_time(ptp_data->portid,
550 						     ptp_data->delta);
551 
552 			ptp_data->current_ptp_port = ptp_data->portid;
553 
554 			/* Update kernel time if enabled in app parameters. */
555 			if (ptp_data->kernel_time_set == 1)
556 				update_kernel_time();
557 
558 
559 
560 		}
561 	}
562 }
563 
564 /* This function processes PTP packets, implementing slave PTP IEEE1588 L2
565  * functionality.
566  */
567 
568 /* Parse ptp frames. 8< */
569 static void
570 parse_ptp_frames(uint16_t portid, struct rte_mbuf *m) {
571 	struct ptp_header *ptp_hdr;
572 	struct rte_ether_hdr *eth_hdr;
573 	uint16_t eth_type;
574 
575 	eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
576 	eth_type = rte_be_to_cpu_16(eth_hdr->ether_type);
577 
578 	if (eth_type == PTP_PROTOCOL) {
579 		ptp_data.m = m;
580 		ptp_data.portid = portid;
581 		ptp_hdr = (struct ptp_header *)(rte_pktmbuf_mtod(m, char *)
582 					+ sizeof(struct rte_ether_hdr));
583 
584 		switch (ptp_hdr->msg_type) {
585 		case SYNC:
586 			parse_sync(&ptp_data, m->timesync);
587 			break;
588 		case FOLLOW_UP:
589 			parse_fup(&ptp_data);
590 			break;
591 		case DELAY_RESP:
592 			parse_drsp(&ptp_data);
593 			print_clock_info(&ptp_data);
594 			break;
595 		default:
596 			break;
597 		}
598 	}
599 }
600 /* >8 End of function processes PTP packets. */
601 
602 /*
603  * The lcore main. This is the main thread that does the work, reading from an
604  * input port and writing to an output port.
605  */
606 static __rte_noreturn void
607 lcore_main(void)
608 {
609 	uint16_t portid;
610 	unsigned nb_rx;
611 	struct rte_mbuf *m;
612 
613 	printf("\nCore %u Waiting for SYNC packets. [Ctrl+C to quit]\n",
614 			rte_lcore_id());
615 
616 	/* Run until the application is quit or killed. */
617 
618 	while (1) {
619 		/* Read packet from RX queues. 8< */
620 		for (portid = 0; portid < ptp_enabled_port_nb; portid++) {
621 
622 			portid = ptp_enabled_ports[portid];
623 			nb_rx = rte_eth_rx_burst(portid, 0, &m, 1);
624 
625 			if (likely(nb_rx == 0))
626 				continue;
627 
628 			/* Packet is parsed to determine which type. 8< */
629 			if (m->ol_flags & RTE_MBUF_F_RX_IEEE1588_PTP)
630 				parse_ptp_frames(portid, m);
631 			/* >8 End of packet is parsed to determine which type. */
632 
633 			rte_pktmbuf_free(m);
634 		}
635 		/* >8 End of read packets from RX queues. */
636 	}
637 }
638 
639 static void
640 print_usage(const char *prgname)
641 {
642 	printf("%s [EAL options] -- -p PORTMASK -T VALUE\n"
643 		" -T VALUE: 0 - Disable, 1 - Enable Linux Clock"
644 		" Synchronization (0 default)\n"
645 		" -p PORTMASK: hexadecimal bitmask of ports to configure\n",
646 		prgname);
647 }
648 
649 static int
650 ptp_parse_portmask(const char *portmask)
651 {
652 	char *end = NULL;
653 	unsigned long pm;
654 
655 	/* Parse the hexadecimal string. */
656 	pm = strtoul(portmask, &end, 16);
657 
658 	if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
659 		return 0;
660 
661 	return pm;
662 }
663 
664 static int
665 parse_ptp_kernel(const char *param)
666 {
667 	char *end = NULL;
668 	unsigned long pm;
669 
670 	/* Parse the hexadecimal string. */
671 	pm = strtoul(param, &end, 16);
672 
673 	if ((param[0] == '\0') || (end == NULL) || (*end != '\0'))
674 		return -1;
675 	if (pm == 0)
676 		return 0;
677 
678 	return 1;
679 }
680 
681 /* Parse the commandline arguments. */
682 static int
683 ptp_parse_args(int argc, char **argv)
684 {
685 	int opt, ret;
686 	char **argvopt;
687 	int option_index;
688 	char *prgname = argv[0];
689 	static struct option lgopts[] = { {NULL, 0, 0, 0} };
690 
691 	argvopt = argv;
692 
693 	while ((opt = getopt_long(argc, argvopt, "p:T:",
694 				  lgopts, &option_index)) != EOF) {
695 
696 		switch (opt) {
697 
698 		/* Portmask. */
699 		case 'p':
700 			ptp_enabled_port_mask = ptp_parse_portmask(optarg);
701 			if (ptp_enabled_port_mask == 0) {
702 				printf("invalid portmask\n");
703 				print_usage(prgname);
704 				return -1;
705 			}
706 			break;
707 		/* Time synchronization. */
708 		case 'T':
709 			ret = parse_ptp_kernel(optarg);
710 			if (ret < 0) {
711 				print_usage(prgname);
712 				return -1;
713 			}
714 
715 			ptp_data.kernel_time_set = ret;
716 			break;
717 
718 		default:
719 			print_usage(prgname);
720 			return -1;
721 		}
722 	}
723 
724 	argv[optind-1] = prgname;
725 
726 	optind = 1; /* Reset getopt lib. */
727 
728 	return 0;
729 }
730 
731 /*
732  * The main function, which does initialization and calls the per-lcore
733  * functions.
734  */
735 int
736 main(int argc, char *argv[])
737 {
738 	unsigned nb_ports;
739 
740 	uint16_t portid;
741 
742 	/* Initialize the Environment Abstraction Layer (EAL). 8< */
743 	int ret = rte_eal_init(argc, argv);
744 
745 	if (ret < 0)
746 		rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
747 	/* >8 End of initialization of EAL. */
748 
749 	memset(&ptp_data, '\0', sizeof(struct ptpv2_data_slave_ordinary));
750 
751 	/* Parse specific arguments. 8< */
752 	argc -= ret;
753 	argv += ret;
754 
755 	ret = ptp_parse_args(argc, argv);
756 	if (ret < 0)
757 		rte_exit(EXIT_FAILURE, "Error with PTP initialization\n");
758 	/* >8 End of parsing specific arguments. */
759 
760 	/* Check that there is an even number of ports to send/receive on. */
761 	nb_ports = rte_eth_dev_count_avail();
762 
763 	/* Creates a new mempool in memory to hold the mbufs. 8< */
764 	mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS * nb_ports,
765 		MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
766 	/* >8 End of a new mempool in memory to hold the mbufs. */
767 
768 	if (mbuf_pool == NULL)
769 		rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
770 
771 	/* Initialize all ports. 8< */
772 	RTE_ETH_FOREACH_DEV(portid) {
773 		if ((ptp_enabled_port_mask & (1 << portid)) != 0) {
774 			if (port_init(portid, mbuf_pool) == 0) {
775 				ptp_enabled_ports[ptp_enabled_port_nb] = portid;
776 				ptp_enabled_port_nb++;
777 			} else {
778 				rte_exit(EXIT_FAILURE,
779 					 "Cannot init port %"PRIu8 "\n",
780 					 portid);
781 			}
782 		} else
783 			printf("Skipping disabled port %u\n", portid);
784 	}
785 	/* >8 End of initialization of all ports. */
786 
787 	if (ptp_enabled_port_nb == 0) {
788 		rte_exit(EXIT_FAILURE,
789 			"All available ports are disabled."
790 			" Please set portmask.\n");
791 	}
792 
793 	if (rte_lcore_count() > 1)
794 		printf("\nWARNING: Too many lcores enabled. Only 1 used.\n");
795 
796 	/* Call lcore_main on the main core only. */
797 	lcore_main();
798 
799 	/* clean up the EAL */
800 	rte_eal_cleanup();
801 
802 	return 0;
803 }
804