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
timespec64_to_ns(const struct timespec * ts)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
ns_to_timeval(int64_t nsec)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
port_init(uint16_t port,struct rte_mempool * mbuf_pool)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
print_clock_info(struct ptpv2_data_slave_ordinary * ptp_data)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
delta_eval(struct ptpv2_data_slave_ordinary * ptp_data)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
parse_sync(struct ptpv2_data_slave_ordinary * ptp_data,uint16_t rx_tstamp_idx)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
parse_fup(struct ptpv2_data_slave_ordinary * ptp_data)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 delay_req_msg *req_msg;
390 struct rte_mbuf *created_pkt;
391 struct tstamp *origin_tstamp;
392 struct rte_ether_addr eth_multicast = ether_multicast;
393 size_t pkt_size;
394 int wait_us;
395 struct rte_mbuf *m = ptp_data->m;
396 int ret;
397
398 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
399 ptp_hdr = (struct ptp_header *)(rte_pktmbuf_mtod(m, char *)
400 + sizeof(struct rte_ether_hdr));
401 if (memcmp(&ptp_data->master_clock_id,
402 &ptp_hdr->source_port_id.clock_id,
403 sizeof(struct clock_id)) != 0)
404 return;
405
406 ptp_data->seqID_FOLLOWUP = rte_be_to_cpu_16(ptp_hdr->seq_id);
407 ptp_msg = (struct ptp_message *) (rte_pktmbuf_mtod(m, char *) +
408 sizeof(struct rte_ether_hdr));
409
410 origin_tstamp = &ptp_msg->follow_up.precise_origin_tstamp;
411 ptp_data->tstamp1.tv_nsec = ntohl(origin_tstamp->ns);
412 ptp_data->tstamp1.tv_sec =
413 ((uint64_t)ntohl(origin_tstamp->sec_lsb)) |
414 (((uint64_t)ntohs(origin_tstamp->sec_msb)) << 32);
415
416 if (ptp_data->seqID_FOLLOWUP == ptp_data->seqID_SYNC) {
417 ret = rte_eth_macaddr_get(ptp_data->portid, ð_addr);
418 if (ret != 0) {
419 printf("\nCore %u: port %u failed to get MAC address: %s\n",
420 rte_lcore_id(), ptp_data->portid,
421 rte_strerror(-ret));
422 return;
423 }
424
425 created_pkt = rte_pktmbuf_alloc(mbuf_pool);
426 pkt_size = sizeof(struct rte_ether_hdr) +
427 sizeof(struct delay_req_msg);
428
429 if (rte_pktmbuf_append(created_pkt, pkt_size) == NULL) {
430 rte_pktmbuf_free(created_pkt);
431 return;
432 }
433 created_pkt->data_len = pkt_size;
434 created_pkt->pkt_len = pkt_size;
435 eth_hdr = rte_pktmbuf_mtod(created_pkt, struct rte_ether_hdr *);
436 rte_ether_addr_copy(ð_addr, ð_hdr->src_addr);
437
438 /* Set multicast address 01-1B-19-00-00-00. */
439 rte_ether_addr_copy(ð_multicast, ð_hdr->dst_addr);
440
441 eth_hdr->ether_type = htons(PTP_PROTOCOL);
442 req_msg = rte_pktmbuf_mtod_offset(created_pkt,
443 struct delay_req_msg *, sizeof(struct
444 rte_ether_hdr));
445
446 req_msg->hdr.seq_id = htons(ptp_data->seqID_SYNC);
447 req_msg->hdr.msg_type = DELAY_REQ;
448 req_msg->hdr.ver = 2;
449 req_msg->hdr.control = 1;
450 req_msg->hdr.log_message_interval = 127;
451 req_msg->hdr.message_length =
452 htons(sizeof(struct delay_req_msg));
453 req_msg->hdr.domain_number = ptp_hdr->domain_number;
454
455 /* Set up clock id. */
456 client_clkid =
457 &req_msg->hdr.source_port_id.clock_id;
458
459 client_clkid->id[0] = eth_hdr->src_addr.addr_bytes[0];
460 client_clkid->id[1] = eth_hdr->src_addr.addr_bytes[1];
461 client_clkid->id[2] = eth_hdr->src_addr.addr_bytes[2];
462 client_clkid->id[3] = 0xFF;
463 client_clkid->id[4] = 0xFE;
464 client_clkid->id[5] = eth_hdr->src_addr.addr_bytes[3];
465 client_clkid->id[6] = eth_hdr->src_addr.addr_bytes[4];
466 client_clkid->id[7] = eth_hdr->src_addr.addr_bytes[5];
467
468 rte_memcpy(&ptp_data->client_clock_id,
469 client_clkid,
470 sizeof(struct clock_id));
471
472 /* Enable flag for hardware timestamping. */
473 created_pkt->ol_flags |= RTE_MBUF_F_TX_IEEE1588_TMST;
474
475 /*Read value from NIC to prevent latching with old value. */
476 rte_eth_timesync_read_tx_timestamp(ptp_data->portid,
477 &ptp_data->tstamp3);
478
479 /* Transmit the packet. */
480 rte_eth_tx_burst(ptp_data->portid, 0, &created_pkt, 1);
481
482 wait_us = 0;
483 ptp_data->tstamp3.tv_nsec = 0;
484 ptp_data->tstamp3.tv_sec = 0;
485
486 /* Wait at least 1 us to read TX timestamp. */
487 while ((rte_eth_timesync_read_tx_timestamp(ptp_data->portid,
488 &ptp_data->tstamp3) < 0) && (wait_us < 1000)) {
489 rte_delay_us(1);
490 wait_us++;
491 }
492 }
493 }
494
495 /*
496 * Update the kernel time with the difference between it and the current NIC
497 * time.
498 */
499 static inline void
update_kernel_time(void)500 update_kernel_time(void)
501 {
502 int64_t nsec;
503 struct timespec net_time, sys_time;
504
505 clock_gettime(CLOCK_REALTIME, &sys_time);
506 rte_eth_timesync_read_time(ptp_data.current_ptp_port, &net_time);
507
508 nsec = (int64_t)timespec64_to_ns(&net_time) -
509 (int64_t)timespec64_to_ns(&sys_time);
510
511 ptp_data.new_adj = ns_to_timeval(nsec);
512
513 /*
514 * If difference between kernel time and system time in NIC is too big
515 * (more than +/- 20 microseconds), use clock_settime to set directly
516 * the kernel time, as adjtime is better for small adjustments (takes
517 * longer to adjust the time).
518 */
519
520 if (nsec > KERNEL_TIME_ADJUST_LIMIT || nsec < -KERNEL_TIME_ADJUST_LIMIT)
521 clock_settime(CLOCK_REALTIME, &net_time);
522 else
523 adjtime(&ptp_data.new_adj, 0);
524
525
526 }
527
528 /*
529 * Parse the DELAY_RESP message.
530 */
531 static void
parse_drsp(struct ptpv2_data_slave_ordinary * ptp_data)532 parse_drsp(struct ptpv2_data_slave_ordinary *ptp_data)
533 {
534 struct rte_mbuf *m = ptp_data->m;
535 struct ptp_message *ptp_msg;
536 struct tstamp *rx_tstamp;
537 uint16_t seq_id;
538
539 ptp_msg = (struct ptp_message *) (rte_pktmbuf_mtod(m, char *) +
540 sizeof(struct rte_ether_hdr));
541 seq_id = rte_be_to_cpu_16(ptp_msg->delay_resp.hdr.seq_id);
542 if (memcmp(&ptp_data->client_clock_id,
543 &ptp_msg->delay_resp.req_port_id.clock_id,
544 sizeof(struct clock_id)) == 0) {
545 if (seq_id == ptp_data->seqID_FOLLOWUP) {
546 rx_tstamp = &ptp_msg->delay_resp.rx_tstamp;
547 ptp_data->tstamp4.tv_nsec = ntohl(rx_tstamp->ns);
548 ptp_data->tstamp4.tv_sec =
549 ((uint64_t)ntohl(rx_tstamp->sec_lsb)) |
550 (((uint64_t)ntohs(rx_tstamp->sec_msb)) << 32);
551
552 /* Evaluate the delta for adjustment. */
553 ptp_data->delta = delta_eval(ptp_data);
554
555 rte_eth_timesync_adjust_time(ptp_data->portid,
556 ptp_data->delta);
557
558 ptp_data->current_ptp_port = ptp_data->portid;
559
560 /* Update kernel time if enabled in app parameters. */
561 if (ptp_data->kernel_time_set == 1)
562 update_kernel_time();
563
564
565
566 }
567 }
568 }
569
570 /* This function processes PTP packets, implementing slave PTP IEEE1588 L2
571 * functionality.
572 */
573
574 /* Parse ptp frames. 8< */
575 static void
parse_ptp_frames(uint16_t portid,struct rte_mbuf * m)576 parse_ptp_frames(uint16_t portid, struct rte_mbuf *m) {
577 struct ptp_header *ptp_hdr;
578 struct rte_ether_hdr *eth_hdr;
579 uint16_t eth_type;
580
581 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
582 eth_type = rte_be_to_cpu_16(eth_hdr->ether_type);
583
584 if (eth_type == PTP_PROTOCOL) {
585 ptp_data.m = m;
586 ptp_data.portid = portid;
587 ptp_hdr = (struct ptp_header *)(rte_pktmbuf_mtod(m, char *)
588 + sizeof(struct rte_ether_hdr));
589
590 switch (ptp_hdr->msg_type) {
591 case SYNC:
592 parse_sync(&ptp_data, m->timesync);
593 break;
594 case FOLLOW_UP:
595 parse_fup(&ptp_data);
596 break;
597 case DELAY_RESP:
598 parse_drsp(&ptp_data);
599 print_clock_info(&ptp_data);
600 break;
601 default:
602 break;
603 }
604 }
605 }
606 /* >8 End of function processes PTP packets. */
607
608 /*
609 * The lcore main. This is the main thread that does the work, reading from an
610 * input port and writing to an output port.
611 */
612 static __rte_noreturn void
lcore_main(void)613 lcore_main(void)
614 {
615 uint16_t portid;
616 unsigned nb_rx;
617 struct rte_mbuf *m;
618
619 printf("\nCore %u Waiting for SYNC packets. [Ctrl+C to quit]\n",
620 rte_lcore_id());
621
622 /* Run until the application is quit or killed. */
623
624 while (1) {
625 /* Read packet from RX queues. 8< */
626 for (portid = 0; portid < ptp_enabled_port_nb; portid++) {
627
628 portid = ptp_enabled_ports[portid];
629 nb_rx = rte_eth_rx_burst(portid, 0, &m, 1);
630
631 if (likely(nb_rx == 0))
632 continue;
633
634 /* Packet is parsed to determine which type. 8< */
635 if (m->ol_flags & RTE_MBUF_F_RX_IEEE1588_PTP)
636 parse_ptp_frames(portid, m);
637 /* >8 End of packet is parsed to determine which type. */
638
639 rte_pktmbuf_free(m);
640 }
641 /* >8 End of read packets from RX queues. */
642 }
643 }
644
645 static void
print_usage(const char * prgname)646 print_usage(const char *prgname)
647 {
648 printf("%s [EAL options] -- -p PORTMASK -T VALUE\n"
649 " -T VALUE: 0 - Disable, 1 - Enable Linux Clock"
650 " Synchronization (0 default)\n"
651 " -p PORTMASK: hexadecimal bitmask of ports to configure\n",
652 prgname);
653 }
654
655 static int
ptp_parse_portmask(const char * portmask)656 ptp_parse_portmask(const char *portmask)
657 {
658 char *end = NULL;
659 unsigned long pm;
660
661 /* Parse the hexadecimal string. */
662 pm = strtoul(portmask, &end, 16);
663
664 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
665 return 0;
666
667 return pm;
668 }
669
670 static int
parse_ptp_kernel(const char * param)671 parse_ptp_kernel(const char *param)
672 {
673 char *end = NULL;
674 unsigned long pm;
675
676 /* Parse the hexadecimal string. */
677 pm = strtoul(param, &end, 16);
678
679 if ((param[0] == '\0') || (end == NULL) || (*end != '\0'))
680 return -1;
681 if (pm == 0)
682 return 0;
683
684 return 1;
685 }
686
687 /* Parse the commandline arguments. */
688 static int
ptp_parse_args(int argc,char ** argv)689 ptp_parse_args(int argc, char **argv)
690 {
691 int opt, ret;
692 char **argvopt;
693 int option_index;
694 char *prgname = argv[0];
695 static struct option lgopts[] = { {NULL, 0, 0, 0} };
696
697 argvopt = argv;
698
699 while ((opt = getopt_long(argc, argvopt, "p:T:",
700 lgopts, &option_index)) != EOF) {
701
702 switch (opt) {
703
704 /* Portmask. */
705 case 'p':
706 ptp_enabled_port_mask = ptp_parse_portmask(optarg);
707 if (ptp_enabled_port_mask == 0) {
708 printf("invalid portmask\n");
709 print_usage(prgname);
710 return -1;
711 }
712 break;
713 /* Time synchronization. */
714 case 'T':
715 ret = parse_ptp_kernel(optarg);
716 if (ret < 0) {
717 print_usage(prgname);
718 return -1;
719 }
720
721 ptp_data.kernel_time_set = ret;
722 break;
723
724 default:
725 print_usage(prgname);
726 return -1;
727 }
728 }
729
730 argv[optind-1] = prgname;
731
732 optind = 1; /* Reset getopt lib. */
733
734 return 0;
735 }
736
737 /*
738 * The main function, which does initialization and calls the per-lcore
739 * functions.
740 */
741 int
main(int argc,char * argv[])742 main(int argc, char *argv[])
743 {
744 unsigned nb_ports;
745
746 uint16_t portid;
747
748 /* Initialize the Environment Abstraction Layer (EAL). 8< */
749 int ret = rte_eal_init(argc, argv);
750
751 if (ret < 0)
752 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
753 /* >8 End of initialization of EAL. */
754
755 memset(&ptp_data, '\0', sizeof(struct ptpv2_data_slave_ordinary));
756
757 /* Parse specific arguments. 8< */
758 argc -= ret;
759 argv += ret;
760
761 ret = ptp_parse_args(argc, argv);
762 if (ret < 0)
763 rte_exit(EXIT_FAILURE, "Error with PTP initialization\n");
764 /* >8 End of parsing specific arguments. */
765
766 /* Check that there is an even number of ports to send/receive on. */
767 nb_ports = rte_eth_dev_count_avail();
768
769 /* Creates a new mempool in memory to hold the mbufs. 8< */
770 mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS * nb_ports,
771 MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
772 /* >8 End of a new mempool in memory to hold the mbufs. */
773
774 if (mbuf_pool == NULL)
775 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
776
777 /* Initialize all ports. 8< */
778 RTE_ETH_FOREACH_DEV(portid) {
779 if ((ptp_enabled_port_mask & (1 << portid)) != 0) {
780 if (port_init(portid, mbuf_pool) == 0) {
781 ptp_enabled_ports[ptp_enabled_port_nb] = portid;
782 ptp_enabled_port_nb++;
783 } else {
784 rte_exit(EXIT_FAILURE,
785 "Cannot init port %"PRIu8 "\n",
786 portid);
787 }
788 } else
789 printf("Skipping disabled port %u\n", portid);
790 }
791 /* >8 End of initialization of all ports. */
792
793 if (ptp_enabled_port_nb == 0) {
794 rte_exit(EXIT_FAILURE,
795 "All available ports are disabled."
796 " Please set portmask.\n");
797 }
798
799 if (rte_lcore_count() > 1)
800 printf("\nWARNING: Too many lcores enabled. Only 1 used.\n");
801
802 /* Call lcore_main on the main core only. */
803 lcore_main();
804
805 /* clean up the EAL */
806 rte_eal_cleanup();
807
808 return 0;
809 }
810