From 0500dc1c21f7cdb1b65bd97cbb761175dbaf27f5 Mon Sep 17 00:00:00 2001 From: Oscar Zhao Date: Mon, 18 Jan 2021 12:19:13 -0500 Subject: [PATCH] ptp working --- cat/cat.cc | 9 ++++++++- inc/pkt.h | 15 ++++++++++++++- khat/khat.cc | 32 +++++++++++++++++++++++++++----- 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/cat/cat.cc b/cat/cat.cc index 4407683..f727ccc 100644 --- a/cat/cat.cc +++ b/cat/cat.cc @@ -134,7 +134,7 @@ locore_main(void * _unused __rte_unused) pkt_data = construct_udp_pkt_hdr(tx_buf, &options.s_host_mac, &options.server_mac, RTE_IPV4(192, 168, 100, 150), RTE_IPV4(192, 168, 100, 151), - 1337, 1337); + 333, 319); if (pkt_data == NULL) { rte_exit(EXIT_FAILURE, "cannot allocate space for packet_data in mbuf\n"); } @@ -264,6 +264,10 @@ port_init(uint16_t portid, struct rte_mempool *mbuf_pool) if (ret != 0) return ret; + ret = rte_eth_timesync_enable(portid); + if (ret != 0) + return ret; + /* Enable RX in promiscuous mode for the Ethernet device. */ ret = rte_eth_promiscuous_enable(portid); if (ret != 0) @@ -404,6 +408,9 @@ int main(int argc, char* argv[]) dump_options(); uint16_t core_id = rte_get_next_lcore(0, true, false); + + sleep(1); + if (rte_eal_remote_launch(locore_main, NULL, core_id) != 0) { rte_exit(EXIT_FAILURE, "failed to launch function on locore\n"); } diff --git a/inc/pkt.h b/inc/pkt.h index fe4d08e..8fbddbd 100644 --- a/inc/pkt.h +++ b/inc/pkt.h @@ -24,6 +24,8 @@ struct packet_hdr { struct rte_ether_hdr eth_hdr; struct rte_ipv4_hdr ipv4_hdr; struct rte_udp_hdr udp_hdr; + uint8_t ptp_msg_type; + uint8_t ptp_ver; } __attribute__((packed)); struct packet_data @@ -115,7 +117,7 @@ struct packet_data * construct_udp_pkt_hdr(struct rte_mbuf * buf, struct rte_ipv4_hdr * ipv4_hdr; struct rte_udp_hdr * udp_hdr; - if (pkt_data == NULL) + if (pkt_data == NULL) return NULL; // single segment @@ -153,6 +155,17 @@ struct packet_data * construct_udp_pkt_hdr(struct rte_mbuf * buf, sizeof(struct rte_udp_hdr)); buf->l4_len = sizeof(struct rte_udp_hdr); + // construct ptp header + // so basically after some experiments at least the intel NIC categorizes PTP packets as: + // 1. Dest port 319 2. these two fields must be set and don't care about others + // Experiments: + // SPORT: 319 DPORT: 319 PTP HDR: valid => SET + // SPORT: 333 DPORT: 319 PTP HDR: valid => SET + // SPORT: 319 DPORT: 333 PTP HDR: valid => NOT SET + // SPORT: 319 DPORT: 319 PTP HDR: invalid => NOT SET + pkt_data->pkt_hdr.ptp_ver = 0x2; // VER 2 + pkt_data->pkt_hdr.ptp_msg_type = 0x0; // SYNC + return pkt_data; } diff --git a/khat/khat.cc b/khat/khat.cc index ab509d3..61125ec 100644 --- a/khat/khat.cc +++ b/khat/khat.cc @@ -1,16 +1,19 @@ #include -#include +#include +#include +#include #include #include #include #include #include #include -#include -#include #include #include +#include #include +#include +#include #include #include "pkt.h" @@ -117,7 +120,7 @@ locore_main(void * _unused __rte_unused) rte_pktmbuf_free(bufs[i]); continue; } - + uint32_t dst_ip = rte_be_to_cpu_32(pkt_data->pkt_hdr.ipv4_hdr.dst_addr); uint32_t src_ip = rte_be_to_cpu_32(pkt_data->pkt_hdr.ipv4_hdr.src_addr); uint16_t src_port = rte_be_to_cpu_16(pkt_data->pkt_hdr.udp_hdr.src_port); @@ -148,6 +151,19 @@ locore_main(void * _unused __rte_unused) src_port, dst_port, rte_be_to_cpu_32(pkt_data->epoch)); + //if (bufs[i]->ol_flags & PKT_RX_IEEE1588_TMST) { + struct timespec ts; + if (rte_eth_timesync_read_rx_timestamp(options.s_portid, &ts, bufs[i]->timesync & 0x3) == 0) { + + ntr(NTR_DEP_USER1, NTR_LEVEL_DEBUG, "Timestamped sec: %lld nsec: %lld\n", ts.tv_sec, ts.tv_nsec); + } else { + ntr(NTR_DEP_USER1, NTR_LEVEL_DEBUG, "Not Timestamped!\n"); + } + // } + // else { + // ntr(NTR_DEP_USER1, NTR_LEVEL_DEBUG, "Not Timestamped!\n"); + // } + // swap s_addr and d_addr struct rte_mbuf * pkt_buf = rte_pktmbuf_alloc(options.s_pkt_mempool); if (pkt_buf == NULL) { @@ -250,7 +266,11 @@ port_init(uint16_t portid, struct rte_mempool *mbuf_pool) ret = rte_eth_macaddr_get(portid, &addr); if (ret != 0) return ret; - + + ret = rte_eth_timesync_enable(portid); + if (ret != 0) + return ret; + /* Enable RX in promiscuous mode for the Ethernet device. */ ret = rte_eth_promiscuous_enable(portid); if (ret != 0) @@ -356,6 +376,8 @@ int main(int argc, char* argv[]) if (lcore_id == RTE_MAX_LCORE) { rte_exit(EXIT_FAILURE, "cannot detect lcores.\n"); } + + sleep(1); if (rte_eal_remote_launch(locore_main, NULL, lcore_id) != 0) { rte_exit(EXIT_FAILURE, "failed to launch function on locore %d\n", lcore_id);