ptp working

This commit is contained in:
Oscar Zhao 2021-01-18 12:19:13 -05:00
parent e9e15caea8
commit 0500dc1c21
3 changed files with 49 additions and 7 deletions

View File

@ -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");
}

View File

@ -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
@ -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;
}

View File

@ -1,16 +1,19 @@
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <netinet/in.h>
#include <rte_config.h>
#include <rte_common.h>
#include <rte_eal.h>
#include <rte_ethdev.h>
#include <rte_cycles.h>
#include <rte_lcore.h>
#include <rte_mbuf.h>
#include <rte_byteorder.h>
#include <rte_config.h>
#include <rte_ether.h>
#include <rte_launch.h>
#include <rte_log.h>
#include <atomic>
#include <vector>
#include <fstream>
#include <unistd.h>
#include "pkt.h"
@ -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) {
@ -251,6 +267,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)
@ -357,6 +377,8 @@ int main(int argc, char* argv[])
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);
}