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