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