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