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