From b4bc85ac972b2e4f3e8e0b092947267f9f8aeee4 Mon Sep 17 00:00:00 2001 From: Ashish Chaudhari Date: Sat, 25 May 2019 00:14:36 -0700 Subject: rfnoc: Moved chdr types/packet class out of chdr dir - Moved chdr_packet and chdr_types from rfnoc/chdr to rfnoc and updated all references - Moved non-CHDR definitions to rfnoc_common.hpp --- host/lib/rfnoc/CMakeLists.txt | 3 +- host/lib/rfnoc/chdr/CMakeLists.txt | 15 -- host/lib/rfnoc/chdr/chdr_packet.cpp | 213 ----------------- host/lib/rfnoc/chdr/chdr_types.cpp | 427 ---------------------------------- host/lib/rfnoc/chdr_ctrl_endpoint.cpp | 6 +- host/lib/rfnoc/chdr_packet.cpp | 214 +++++++++++++++++ host/lib/rfnoc/chdr_types.cpp | 427 ++++++++++++++++++++++++++++++++++ host/lib/rfnoc/ctrlport_endpoint.cpp | 4 +- host/lib/rfnoc/mgmt_portal.cpp | 2 +- 9 files changed, 649 insertions(+), 662 deletions(-) delete mode 100644 host/lib/rfnoc/chdr/CMakeLists.txt delete mode 100644 host/lib/rfnoc/chdr/chdr_packet.cpp delete mode 100644 host/lib/rfnoc/chdr/chdr_types.cpp create mode 100644 host/lib/rfnoc/chdr_packet.cpp create mode 100644 host/lib/rfnoc/chdr_types.cpp (limited to 'host/lib/rfnoc') diff --git a/host/lib/rfnoc/CMakeLists.txt b/host/lib/rfnoc/CMakeLists.txt index 4b36a8db4..45904a572 100644 --- a/host/lib/rfnoc/CMakeLists.txt +++ b/host/lib/rfnoc/CMakeLists.txt @@ -20,6 +20,8 @@ LIBUHD_APPEND_SOURCES( ${CMAKE_CURRENT_SOURCE_DIR}/block_ctrl_impl.cpp ${CMAKE_CURRENT_SOURCE_DIR}/blockdef_xml_impl.cpp ${CMAKE_CURRENT_SOURCE_DIR}/block_id.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/chdr_types.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/chdr_packet.cpp ${CMAKE_CURRENT_SOURCE_DIR}/client_zero.cpp ${CMAKE_CURRENT_SOURCE_DIR}/ctrl_iface.cpp ${CMAKE_CURRENT_SOURCE_DIR}/graph_impl.cpp @@ -58,5 +60,4 @@ LIBUHD_APPEND_SOURCES( ${CMAKE_CURRENT_SOURCE_DIR}/replay_block_ctrl_impl.cpp ) -INCLUDE_SUBDIRECTORY(chdr) INCLUDE_SUBDIRECTORY(nocscript) diff --git a/host/lib/rfnoc/chdr/CMakeLists.txt b/host/lib/rfnoc/chdr/CMakeLists.txt deleted file mode 100644 index fca46fe4a..000000000 --- a/host/lib/rfnoc/chdr/CMakeLists.txt +++ /dev/null @@ -1,15 +0,0 @@ -# -# Copyright 2014-2015,2017 Ettus Research LLC -# Copyright 2018 Ettus Research, a National Instruments Company -# -# SPDX-License-Identifier: GPL-3.0-or-later -# - -######################################################################## -# This file included, use CMake directory variables -######################################################################## - -LIBUHD_APPEND_SOURCES( - ${CMAKE_CURRENT_SOURCE_DIR}/chdr_types.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/chdr_packet.cpp -) diff --git a/host/lib/rfnoc/chdr/chdr_packet.cpp b/host/lib/rfnoc/chdr/chdr_packet.cpp deleted file mode 100644 index b058115ab..000000000 --- a/host/lib/rfnoc/chdr/chdr_packet.cpp +++ /dev/null @@ -1,213 +0,0 @@ -// -// Copyright 2019 Ettus Research, a National Instruments Brand -// -// SPDX-License-Identifier: GPL-3.0-or-later -// - -#include -#include -#include -#include - -using namespace uhd; -using namespace uhd::rfnoc::chdr; - -chdr_packet::~chdr_packet() = default; - -//------------------------------------------------------------ -// chdr_packet -//------------------------------------------------------------ - -template -class chdr_packet_impl : public chdr_packet -{ -public: - chdr_packet_impl() = delete; - chdr_packet_impl(size_t mtu_bytes) : _mtu_bytes(mtu_bytes) {} - ~chdr_packet_impl() = default; - - virtual void refresh(const void* pkt_buff) const - { - assert(pkt_buff); - _pkt_buff = const_cast(reinterpret_cast(pkt_buff)); - _compute_mdata_offset(); - } - - virtual void refresh(void* pkt_buff, chdr_header& header, uint64_t timestamp = 0) - { - assert(pkt_buff); - _pkt_buff = reinterpret_cast(pkt_buff); - _pkt_buff[0] = u64_from_host(header); - if (_has_timestamp(header)) { - _pkt_buff[1] = timestamp; - } - _compute_mdata_offset(); - } - - virtual void update_payload_size(size_t payload_size_bytes) - { - chdr_header header = get_chdr_header(); - header.set_length(((_mdata_offset + header.get_num_mdata()) * chdr_w_bytes) - + payload_size_bytes); - _pkt_buff[0] = u64_from_host(header); - } - - virtual endianness_t get_byte_order() const - { - return endianness; - } - - virtual size_t get_mtu_bytes() const - { - return _mtu_bytes; - } - - virtual chdr_header get_chdr_header() const - { - assert(_pkt_buff); - return std::move(chdr_header(u64_to_host(_pkt_buff[0]))); - } - - virtual boost::optional get_timestamp() const - { - if (_has_timestamp(get_chdr_header())) { - // In a unit64_t buffer, the timestamp is always immediately after the header - // regardless of chdr_w. - return u64_to_host(_pkt_buff[1]); - } else { - return boost::none; - } - } - - virtual size_t get_mdata_size() const - { - return get_chdr_header().get_num_mdata() * chdr_w_bytes; - } - - virtual const void* get_mdata_const_ptr() const - { - return const_cast( - const_cast*>(this)->get_mdata_ptr()); - } - - virtual void* get_mdata_ptr() - { - return reinterpret_cast(_pkt_buff + (chdr_w_stride * _mdata_offset)); - } - - virtual size_t get_payload_size() const - { - return get_chdr_header().get_length() - get_mdata_size() - - (chdr_w_bytes * _mdata_offset); - } - - virtual const void* get_payload_const_ptr() const - { - return const_cast( - const_cast*>(this)->get_payload_ptr()); - } - - virtual void* get_payload_ptr() - { - return reinterpret_cast( - _pkt_buff - + (chdr_w_stride * (_mdata_offset + get_chdr_header().get_num_mdata()))); - } - -private: - inline bool _has_timestamp(const chdr_header& header) const - { - return (header.get_pkt_type() == PKT_TYPE_DATA_WITH_TS); - } - - inline void _compute_mdata_offset() const - { - // The metadata offset depends on the chdr_w and whether we have a timestamp - if (chdr_w == 64) { - _mdata_offset = _has_timestamp(get_chdr_header()) ? 2 : 1; - } else { - _mdata_offset = 1; - } - } - - inline static uint64_t u64_to_host(uint64_t word) - { - return (endianness == ENDIANNESS_BIG) ? uhd::ntohx(word) - : uhd::wtohx(word); - } - - inline static uint64_t u64_from_host(uint64_t word) - { - return (endianness == ENDIANNESS_BIG) ? uhd::htonx(word) - : uhd::htowx(word); - } - - static const size_t chdr_w_bytes = (chdr_w / 8); - static const size_t chdr_w_stride = (chdr_w / 64); - - // Packet state - const size_t _mtu_bytes = 0; - mutable uint64_t* _pkt_buff = nullptr; - mutable size_t _mdata_offset = 0; -}; - -chdr_packet_factory::chdr_packet_factory(chdr_w_t chdr_w, endianness_t endianness) - : _chdr_w(chdr_w), _endianness(endianness) -{ -} - -chdr_packet::uptr chdr_packet_factory::make_generic(size_t mtu_bytes) const -{ - if (_endianness == ENDIANNESS_BIG) { - switch (_chdr_w) { - case CHDR_W_512: - return std::make_unique>(mtu_bytes); - case CHDR_W_256: - return std::make_unique>(mtu_bytes); - case CHDR_W_128: - return std::make_unique>(mtu_bytes); - case CHDR_W_64: - return std::make_unique>(mtu_bytes); - default: - assert(0); - } - } else { - switch (_chdr_w) { - case CHDR_W_512: - return std::make_unique>( - mtu_bytes); - case CHDR_W_256: - return std::make_unique>( - mtu_bytes); - case CHDR_W_128: - return std::make_unique>( - mtu_bytes); - case CHDR_W_64: - return std::make_unique>( - mtu_bytes); - default: - assert(0); - } - } - return chdr_packet::uptr(); -} - -chdr_ctrl_packet::uptr chdr_packet_factory::make_ctrl(size_t mtu_bytes) const -{ - return std::make_unique(make_generic(mtu_bytes)); -} - -chdr_strs_packet::uptr chdr_packet_factory::make_strs(size_t mtu_bytes) const -{ - return std::make_unique(make_generic(mtu_bytes)); -} - -chdr_strc_packet::uptr chdr_packet_factory::make_strc(size_t mtu_bytes) const -{ - return std::make_unique(make_generic(mtu_bytes)); -} - -chdr_mgmt_packet::uptr chdr_packet_factory::make_mgmt(size_t mtu_bytes) const -{ - return std::make_unique(make_generic(mtu_bytes)); -} diff --git a/host/lib/rfnoc/chdr/chdr_types.cpp b/host/lib/rfnoc/chdr/chdr_types.cpp deleted file mode 100644 index 8920e4fe3..000000000 --- a/host/lib/rfnoc/chdr/chdr_types.cpp +++ /dev/null @@ -1,427 +0,0 @@ -// -// Copyright 2019 Ettus Research, a National Instruments Brand -// -// SPDX-License-Identifier: GPL-3.0-or-later -// - -#include -#include -#include -#include -#include - -using namespace uhd; -using namespace uhd::rfnoc::chdr; - -//---------------------------------------------------- -// Utility Functions -//---------------------------------------------------- - -static inline constexpr uint64_t mask_u64(size_t width) -{ - return ((uint64_t(1) << width) - 1); -} - -template -static inline constexpr field_t get_field_u64( - uint64_t flat_hdr, size_t offset, size_t width) -{ - return static_cast((flat_hdr >> offset) & mask_u64(width)); -} - -//---------------------------------------------------- -// CHDR Control Payload -//---------------------------------------------------- - -void ctrl_payload::populate_header(chdr_header& header) const -{ - header.set_pkt_type(PKT_TYPE_CTRL); - header.set_eob(false); - header.set_eov(false); - header.set_num_mdata(0); -} - -size_t ctrl_payload::serialize(uint64_t* buff, - size_t max_size_bytes, - const std::function& conv_byte_order) const -{ - UHD_ASSERT_THROW((data_vtr.size() > 0 && data_vtr.size() < 16)); - // We assume that buff has room to hold the entire packet - size_t ptr = 0; - - // Populate control header - buff[ptr++] = conv_byte_order( - ((static_cast(dst_port) & mask_u64(DST_PORT_WIDTH)) << DST_PORT_OFFSET) - | ((static_cast(src_port) & mask_u64(SRC_PORT_WIDTH)) - << SRC_PORT_OFFSET) - | ((static_cast(data_vtr.size()) & mask_u64(NUM_DATA_WIDTH)) - << NUM_DATA_OFFSET) - | ((static_cast(seq_num) & mask_u64(SEQ_NUM_WIDTH)) << SEQ_NUM_OFFSET) - | ((static_cast(timestamp.is_initialized() ? 1 : 0) - & mask_u64(HAS_TIME_WIDTH)) - << HAS_TIME_OFFSET) - | ((static_cast(is_ack) & mask_u64(IS_ACK_WIDTH)) << IS_ACK_OFFSET) - | ((static_cast(src_epid) & mask_u64(SRC_EPID_WIDTH)) - << SRC_EPID_OFFSET)); - - // Populate optional timestamp - if (timestamp.is_initialized()) { - buff[ptr++] = conv_byte_order(timestamp.get()); - } - - // Populate control operation word - buff[ptr++] = conv_byte_order( - ((static_cast(address) & mask_u64(ADDRESS_WIDTH)) << ADDRESS_OFFSET) - | ((static_cast(byte_enable) & mask_u64(BYTE_ENABLE_WIDTH)) - << BYTE_ENABLE_OFFSET) - | ((static_cast(op_code) & mask_u64(OPCODE_WIDTH)) << OPCODE_OFFSET) - | ((static_cast(status) & mask_u64(STATUS_WIDTH)) << STATUS_OFFSET) - | (static_cast(data_vtr[0]) << HI_DATA_OFFSET)); - - // Populate the rest of the data - for (size_t i = 1; i < data_vtr.size(); i += 2) { - const uint32_t hi_data = - (((i + 2) >= data_vtr.size()) && (data_vtr.size() % 2 == 0)) - ? 0 - : data_vtr[i + 1]; - buff[ptr++] = - conv_byte_order(static_cast(hi_data) << HI_DATA_OFFSET - | static_cast(data_vtr[i]) << LO_DATA_OFFSET); - } - - // FIXME: This UHD_ASSERT_THROW is a bit late because memory has already been - // corrupted - UHD_ASSERT_THROW(ptr <= max_size_bytes); - // Return bytes written - return (ptr * sizeof(uint64_t)); -} - -void ctrl_payload::deserialize(const uint64_t* buff, - size_t max_size_bytes, - const std::function& conv_byte_order) -{ - // We assume that buff has room to hold the entire packet - size_t ptr = 0; - - // Read control header - uint64_t ctrl_header = conv_byte_order(buff[ptr++]); - data_vtr.resize(get_field_u64(ctrl_header, NUM_DATA_OFFSET, NUM_DATA_WIDTH)); - UHD_ASSERT_THROW((data_vtr.size() > 0 && data_vtr.size() < 16)); - dst_port = get_field_u64(ctrl_header, DST_PORT_OFFSET, DST_PORT_WIDTH); - src_port = get_field_u64(ctrl_header, SRC_PORT_OFFSET, SRC_PORT_WIDTH); - seq_num = get_field_u64(ctrl_header, SEQ_NUM_OFFSET, SEQ_NUM_WIDTH); - is_ack = get_field_u64(ctrl_header, IS_ACK_OFFSET, IS_ACK_WIDTH); - src_epid = get_field_u64(ctrl_header, SRC_EPID_OFFSET, SRC_EPID_WIDTH); - - // Read optional timestamp - if (get_field_u64(ctrl_header, HAS_TIME_OFFSET, HAS_TIME_WIDTH)) { - timestamp = conv_byte_order(buff[ptr++]); - } else { - timestamp = boost::none; - } - - // Read control operation word - uint64_t op_word = conv_byte_order(buff[ptr++]); - if (data_vtr.size() > 0) { - data_vtr[0] = get_field_u64(op_word, HI_DATA_OFFSET, 32); - } - address = get_field_u64(op_word, ADDRESS_OFFSET, ADDRESS_WIDTH); - byte_enable = get_field_u64(op_word, BYTE_ENABLE_OFFSET, BYTE_ENABLE_WIDTH); - op_code = get_field_u64(op_word, OPCODE_OFFSET, OPCODE_WIDTH); - status = get_field_u64(op_word, STATUS_OFFSET, STATUS_WIDTH); - - // Read the rest of the data - for (size_t i = 1; i < data_vtr.size(); i += 2) { - uint64_t data_word = conv_byte_order(buff[ptr++]); - if (((i + 2) < data_vtr.size()) || (data_vtr.size() % 2 != 0)) { - data_vtr[i + 1] = get_field_u64(data_word, HI_DATA_OFFSET, 32); - } - data_vtr[i] = get_field_u64(data_word, LO_DATA_OFFSET, 32); - } - UHD_ASSERT_THROW(ptr <= max_size_bytes); -} - -bool ctrl_payload::operator==(const ctrl_payload& rhs) const -{ - return (dst_port == rhs.dst_port) && (src_port == rhs.src_port) - && (seq_num == rhs.seq_num) - && (timestamp.is_initialized() == rhs.timestamp.is_initialized()) - && ((!timestamp.is_initialized()) || (timestamp.get() == rhs.timestamp.get())) - && (is_ack == rhs.is_ack) && (src_epid == rhs.src_epid) - && (address == rhs.address) && (data_vtr == rhs.data_vtr) - && (byte_enable == rhs.byte_enable) && (op_code == rhs.op_code) - && (status == rhs.status); -} - -const std::string ctrl_payload::to_string() const -{ - return str( - boost::format("ctrl_payload{dst_port:%d, dst_port:%d, seq_num:%d, timestamp:%s, " - "is_ack:%s, src_epid:%d, address:0x%05x, byte_enable:0x%x, " - "op_code:%d, status:%d, data[0]:0x%08x}\n") - % dst_port % src_port % int(seq_num) - % (timestamp.is_initialized() ? str(boost::format("0x%016x") % timestamp.get()) - : std::string("")) - % (is_ack ? "true" : "false") % src_epid % address % int(byte_enable) % op_code - % status % data_vtr[0]); -} - -//---------------------------------------------------- -// CHDR Stream Status Payload -//---------------------------------------------------- - -void strs_payload::populate_header(chdr_header& header) const -{ - header.set_pkt_type(PKT_TYPE_STRS); - header.set_eob(false); - header.set_eov(false); - header.set_num_mdata(0); -} - -size_t strs_payload::serialize(uint64_t* buff, - size_t max_size_bytes, - const std::function& conv_byte_order) const -{ - UHD_ASSERT_THROW(max_size_bytes >= (4 * sizeof(uint64_t))); - - // Populate first word - buff[0] = conv_byte_order( - ((static_cast(src_epid) & mask_u64(SRC_EPID_WIDTH)) << SRC_EPID_OFFSET) - | ((static_cast(status) & mask_u64(STATUS_WIDTH)) << STATUS_OFFSET) - | ((static_cast(capacity_bytes) & mask_u64(CAPACITY_BYTES_WIDTH)) - << CAPACITY_BYTES_OFFSET)); - - // Populate second word - buff[1] = conv_byte_order( - ((static_cast(capacity_pkts) & mask_u64(CAPACITY_PKTS_WIDTH)) - << CAPACITY_PKTS_OFFSET) - | ((static_cast(xfer_count_pkts) & mask_u64(XFER_COUNT_PKTS_WIDTH)) - << XFER_COUNT_PKTS_OFFSET)); - - // Populate third word - buff[2] = conv_byte_order(xfer_count_bytes); - - // Populate fourth word - buff[3] = conv_byte_order( - ((static_cast(buff_info) & mask_u64(BUFF_INFO_WIDTH)) - << BUFF_INFO_OFFSET) - | ((static_cast(status_info) & mask_u64(STATUS_INFO_WIDTH)) - << STATUS_INFO_OFFSET)); - - // Return bytes written - return (4 * sizeof(uint64_t)); -} - -void strs_payload::deserialize(const uint64_t* buff, - size_t num_elems, - const std::function& conv_byte_order) -{ - UHD_ASSERT_THROW(num_elems >= 4); - - // Read first word - uint64_t word0 = conv_byte_order(buff[0]); - src_epid = get_field_u64(word0, SRC_EPID_OFFSET, SRC_EPID_WIDTH); - status = get_field_u64(word0, STATUS_OFFSET, STATUS_WIDTH); - capacity_bytes = - get_field_u64(word0, CAPACITY_BYTES_OFFSET, CAPACITY_BYTES_WIDTH); - - // Read second word - uint64_t word1 = conv_byte_order(buff[1]); - capacity_pkts = - get_field_u64(word1, CAPACITY_PKTS_OFFSET, CAPACITY_PKTS_WIDTH); - xfer_count_pkts = - get_field_u64(word1, XFER_COUNT_PKTS_OFFSET, XFER_COUNT_PKTS_WIDTH); - - // Read third word - xfer_count_bytes = conv_byte_order(buff[2]); - - // Read fourth word - uint64_t word3 = conv_byte_order(buff[3]); - buff_info = get_field_u64(word3, BUFF_INFO_OFFSET, BUFF_INFO_WIDTH); - status_info = get_field_u64(word3, STATUS_INFO_OFFSET, STATUS_INFO_WIDTH); -} - -bool strs_payload::operator==(const strs_payload& rhs) const -{ - return (src_epid == rhs.src_epid) && (status == rhs.status) - && (capacity_bytes == rhs.capacity_bytes) - && (capacity_pkts == rhs.capacity_pkts) - && (xfer_count_pkts == rhs.xfer_count_pkts) - && (xfer_count_bytes == rhs.xfer_count_bytes) && (buff_info == rhs.buff_info) - && (status_info == rhs.status_info); -} - -const std::string strs_payload::to_string() const -{ - return str(boost::format("strs_payload{src_epid:%lu, status:%d, capacity_bytes:%lu, " - "capacity_pkts:%lu, " - "xfer_count_pkts:%lu, xfer_count_bytes:%lu, " - "buff_info:0x%x, status_info:0x%x}\n") - % src_epid % int(status) % capacity_bytes % capacity_pkts % xfer_count_pkts - % xfer_count_bytes % buff_info % status_info); -} - -//---------------------------------------------------- -// CHDR Stream Command Payload -//---------------------------------------------------- - -void strc_payload::populate_header(chdr_header& header) const -{ - header.set_pkt_type(PKT_TYPE_STRC); - header.set_eob(false); - header.set_eov(false); - header.set_num_mdata(0); -} - -size_t strc_payload::serialize(uint64_t* buff, - size_t max_size_bytes, - const std::function& conv_byte_order) const -{ - UHD_ASSERT_THROW(max_size_bytes >= (2 * sizeof(uint64_t))); - - // Populate first word - buff[0] = conv_byte_order( - ((static_cast(src_epid) & mask_u64(SRC_EPID_WIDTH)) << SRC_EPID_OFFSET) - | ((static_cast(op_code) & mask_u64(OP_CODE_WIDTH)) << OP_CODE_OFFSET) - | ((static_cast(op_data) & mask_u64(OP_DATA_WIDTH)) << OP_DATA_OFFSET) - | ((static_cast(num_pkts) & mask_u64(NUM_PKTS_WIDTH)) - << NUM_PKTS_OFFSET)); - - // Populate second word - buff[1] = conv_byte_order(num_bytes); - - // Return bytes written - return (2 * sizeof(uint64_t)); -} - -void strc_payload::deserialize(const uint64_t* buff, - size_t num_elems, - const std::function& conv_byte_order) -{ - UHD_ASSERT_THROW(num_elems >= 2); - - // Read first word - uint64_t word0 = conv_byte_order(buff[0]); - src_epid = get_field_u64(word0, SRC_EPID_OFFSET, SRC_EPID_WIDTH); - op_code = get_field_u64(word0, OP_CODE_OFFSET, OP_CODE_WIDTH); - op_data = get_field_u64(word0, OP_DATA_OFFSET, OP_DATA_WIDTH); - num_pkts = get_field_u64(word0, NUM_PKTS_OFFSET, NUM_PKTS_WIDTH); - // Read second word - num_bytes = conv_byte_order(buff[1]); -} - -bool strc_payload::operator==(const strc_payload& rhs) const -{ - return (src_epid == rhs.src_epid) && (op_code == rhs.op_code) - && (op_data == rhs.op_data) && (num_pkts == rhs.num_pkts) - && (num_bytes == rhs.num_bytes); -} - -const std::string strc_payload::to_string() const -{ - return str(boost::format("strc_payload{src_epid:%lu, op_code:%d, op_data:0x%x, " - "num_pkts:%lu, num_bytes:%lu}\n") - % src_epid % int(op_code) % int(op_data) % num_pkts % num_bytes); -} - -//---------------------------------------------------- -// CHDR Management Payload -//---------------------------------------------------- - -//! Serialize this hop into a list of 64-bit words -size_t mgmt_hop_t::serialize(std::vector& target, - const std::function& conv_byte_order) const -{ - for (size_t i = 0; i < get_num_ops(); i++) { - target.push_back( - conv_byte_order((static_cast(_ops.at(i).get_op_payload()) << 16) - | (static_cast(_ops.at(i).get_op_code()) << 8) - | (static_cast(get_num_ops() - i - 1) << 0))); - } - return get_num_ops(); -} - -//! Deserialize this hop into from list of 64-bit words -void mgmt_hop_t::deserialize( - std::list& src, const std::function& conv_byte_order) -{ - _ops.clear(); - size_t ops_remaining = 0; - do { - // TODO: Change this to a legit exception - UHD_ASSERT_THROW(!src.empty()); - - uint64_t op_word = conv_byte_order(src.front()); - ops_remaining = static_cast(op_word & 0xFF); - mgmt_op_t op(static_cast((op_word >> 8) & 0xFF), - static_cast((op_word >> 16))); - _ops.push_back(op); - src.pop_front(); - } while (ops_remaining > 0); -} - -void mgmt_payload::populate_header(chdr_header& header) const -{ - header.set_pkt_type(PKT_TYPE_MGMT); - header.set_eob(false); - header.set_eov(false); - header.set_num_mdata(0); - header.set_vc(0); - header.set_dst_epid(0); -} - -size_t mgmt_payload::serialize(uint64_t* buff, - size_t max_size_bytes, - const std::function& conv_byte_order) const -{ - std::vector target; - // Insert header - target.push_back(conv_byte_order( - (static_cast(_protover) << 48) - | (static_cast(static_cast(_chdr_w) & 0x7) << 45) - | (static_cast(get_num_hops() & 0x3FF) << 16) - | (static_cast(_src_epid) << 0))); - // Insert data from each hop - for (const auto& hop : _hops) { - hop.serialize(target, conv_byte_order); - } - UHD_ASSERT_THROW(target.size() <= max_size_bytes); - - // We use a vector and copy just for ease of implementation - // These transactions are not performance critical - std::copy(target.begin(), target.end(), buff); - return (target.size() * sizeof(uint64_t)); -} - -void mgmt_payload::deserialize(const uint64_t* buff, - size_t num_elems, - const std::function& conv_byte_order) -{ - UHD_ASSERT_THROW(num_elems > 1); - - // We use a list and copy just for ease of implementation - // These transactions are not performance critical - std::list src_list(buff, buff + num_elems); - - _hops.clear(); - - // Deframe the header - uint64_t hdr = conv_byte_order(src_list.front()); - _hops.resize(static_cast((hdr >> 16) & 0x3FF)); - _src_epid = static_cast(hdr & 0xFFFF); - _chdr_w = static_cast((hdr >> 45) & 0x7); - _protover = static_cast((hdr >> 48) & 0xFFFF); - src_list.pop_front(); - - // Populate all hops - for (size_t i = 0; i < get_num_hops(); i++) { - _hops[i].deserialize(src_list, conv_byte_order); - } -} - -const std::string mgmt_payload::to_string() const -{ - return str(boost::format( - "mgmt_payload{src_epid:%lu, chdr_w:%d, protover:0x%x, num_hops:%lu}\n") - % _src_epid % int(_chdr_w) % _protover % _hops.size()); -} diff --git a/host/lib/rfnoc/chdr_ctrl_endpoint.cpp b/host/lib/rfnoc/chdr_ctrl_endpoint.cpp index 6ded83c0f..fadba566e 100644 --- a/host/lib/rfnoc/chdr_ctrl_endpoint.cpp +++ b/host/lib/rfnoc/chdr_ctrl_endpoint.cpp @@ -8,13 +8,13 @@ #include #include #include -#include -#include #include +#include +#include #include +#include #include #include -#include using namespace uhd; using namespace uhd::rfnoc; diff --git a/host/lib/rfnoc/chdr_packet.cpp b/host/lib/rfnoc/chdr_packet.cpp new file mode 100644 index 000000000..653181c04 --- /dev/null +++ b/host/lib/rfnoc/chdr_packet.cpp @@ -0,0 +1,214 @@ +// +// Copyright 2019 Ettus Research, a National Instruments Brand +// +// SPDX-License-Identifier: GPL-3.0-or-later +// + +#include +#include +#include +#include + +using namespace uhd; +using namespace uhd::rfnoc; +using namespace uhd::rfnoc::chdr; + +chdr_packet::~chdr_packet() = default; + +//------------------------------------------------------------ +// chdr_packet +//------------------------------------------------------------ +// endianness is the link endianness, not the host endianness +template +class chdr_packet_impl : public chdr_packet +{ +public: + chdr_packet_impl() = delete; + chdr_packet_impl(size_t mtu_bytes) : _mtu_bytes(mtu_bytes) {} + ~chdr_packet_impl() = default; + + virtual void refresh(const void* pkt_buff) const + { + assert(pkt_buff); + _pkt_buff = const_cast(reinterpret_cast(pkt_buff)); + _compute_mdata_offset(); + } + + virtual void refresh(void* pkt_buff, chdr_header& header, uint64_t timestamp = 0) + { + assert(pkt_buff); + _pkt_buff = reinterpret_cast(pkt_buff); + _pkt_buff[0] = u64_from_host(header); + if (_has_timestamp(header)) { + _pkt_buff[1] = u64_from_host(timestamp); + } + _compute_mdata_offset(); + } + + virtual void update_payload_size(size_t payload_size_bytes) + { + chdr_header header = get_chdr_header(); + header.set_length(((_mdata_offset + header.get_num_mdata()) * chdr_w_bytes) + + payload_size_bytes); + _pkt_buff[0] = u64_from_host(header); + } + + virtual endianness_t get_byte_order() const + { + return endianness; + } + + virtual size_t get_mtu_bytes() const + { + return _mtu_bytes; + } + + virtual chdr_header get_chdr_header() const + { + assert(_pkt_buff); + return std::move(chdr_header(u64_to_host(_pkt_buff[0]))); + } + + virtual boost::optional get_timestamp() const + { + if (_has_timestamp(get_chdr_header())) { + // In a unit64_t buffer, the timestamp is always immediately after the header + // regardless of chdr_w. + return u64_to_host(_pkt_buff[1]); + } else { + return boost::none; + } + } + + virtual size_t get_mdata_size() const + { + return get_chdr_header().get_num_mdata() * chdr_w_bytes; + } + + virtual const void* get_mdata_const_ptr() const + { + return const_cast( + const_cast*>(this)->get_mdata_ptr()); + } + + virtual void* get_mdata_ptr() + { + return reinterpret_cast(_pkt_buff + (chdr_w_stride * _mdata_offset)); + } + + virtual size_t get_payload_size() const + { + return get_chdr_header().get_length() - get_mdata_size() + - (chdr_w_bytes * _mdata_offset); + } + + virtual const void* get_payload_const_ptr() const + { + return const_cast( + const_cast*>(this)->get_payload_ptr()); + } + + virtual void* get_payload_ptr() + { + return reinterpret_cast( + _pkt_buff + + (chdr_w_stride * (_mdata_offset + get_chdr_header().get_num_mdata()))); + } + +private: + inline bool _has_timestamp(const chdr_header& header) const + { + return (header.get_pkt_type() == PKT_TYPE_DATA_WITH_TS); + } + + inline void _compute_mdata_offset() const + { + // The metadata offset depends on the chdr_w and whether we have a timestamp + if (chdr_w == 64) { + _mdata_offset = _has_timestamp(get_chdr_header()) ? 2 : 1; + } else { + _mdata_offset = 1; + } + } + + inline static uint64_t u64_to_host(uint64_t word) + { + return (endianness == ENDIANNESS_BIG) ? uhd::ntohx(word) + : uhd::wtohx(word); + } + + inline static uint64_t u64_from_host(uint64_t word) + { + return (endianness == ENDIANNESS_BIG) ? uhd::htonx(word) + : uhd::htowx(word); + } + + static const size_t chdr_w_bytes = (chdr_w / 8); + static const size_t chdr_w_stride = (chdr_w / 64); + + // Packet state + const size_t _mtu_bytes = 0; + mutable uint64_t* _pkt_buff = nullptr; + mutable size_t _mdata_offset = 0; +}; + +chdr_packet_factory::chdr_packet_factory(chdr_w_t chdr_w, endianness_t endianness) + : _chdr_w(chdr_w), _endianness(endianness) +{ +} + +chdr_packet::uptr chdr_packet_factory::make_generic(size_t mtu_bytes) const +{ + if (_endianness == ENDIANNESS_BIG) { + switch (_chdr_w) { + case CHDR_W_512: + return std::make_unique>(mtu_bytes); + case CHDR_W_256: + return std::make_unique>(mtu_bytes); + case CHDR_W_128: + return std::make_unique>(mtu_bytes); + case CHDR_W_64: + return std::make_unique>(mtu_bytes); + default: + assert(0); + } + } else { + switch (_chdr_w) { + case CHDR_W_512: + return std::make_unique>( + mtu_bytes); + case CHDR_W_256: + return std::make_unique>( + mtu_bytes); + case CHDR_W_128: + return std::make_unique>( + mtu_bytes); + case CHDR_W_64: + return std::make_unique>( + mtu_bytes); + default: + assert(0); + } + } + return chdr_packet::uptr(); +} + +chdr_ctrl_packet::uptr chdr_packet_factory::make_ctrl(size_t mtu_bytes) const +{ + return std::make_unique(make_generic(mtu_bytes)); +} + +chdr_strs_packet::uptr chdr_packet_factory::make_strs(size_t mtu_bytes) const +{ + return std::make_unique(make_generic(mtu_bytes)); +} + +chdr_strc_packet::uptr chdr_packet_factory::make_strc(size_t mtu_bytes) const +{ + return std::make_unique(make_generic(mtu_bytes)); +} + +chdr_mgmt_packet::uptr chdr_packet_factory::make_mgmt(size_t mtu_bytes) const +{ + return std::make_unique(make_generic(mtu_bytes)); +} diff --git a/host/lib/rfnoc/chdr_types.cpp b/host/lib/rfnoc/chdr_types.cpp new file mode 100644 index 000000000..0e30dca7e --- /dev/null +++ b/host/lib/rfnoc/chdr_types.cpp @@ -0,0 +1,427 @@ +// +// Copyright 2019 Ettus Research, a National Instruments Brand +// +// SPDX-License-Identifier: GPL-3.0-or-later +// + +#include +#include +#include +#include +#include + +using namespace uhd; +using namespace uhd::rfnoc::chdr; + +//---------------------------------------------------- +// Utility Functions +//---------------------------------------------------- + +static inline constexpr uint64_t mask_u64(size_t width) +{ + return ((uint64_t(1) << width) - 1); +} + +template +static inline constexpr field_t get_field_u64( + uint64_t flat_hdr, size_t offset, size_t width) +{ + return static_cast((flat_hdr >> offset) & mask_u64(width)); +} + +//---------------------------------------------------- +// CHDR Control Payload +//---------------------------------------------------- + +void ctrl_payload::populate_header(chdr_header& header) const +{ + header.set_pkt_type(PKT_TYPE_CTRL); + header.set_eob(false); + header.set_eov(false); + header.set_num_mdata(0); +} + +size_t ctrl_payload::serialize(uint64_t* buff, + size_t max_size_bytes, + const std::function& conv_byte_order) const +{ + UHD_ASSERT_THROW((data_vtr.size() > 0 && data_vtr.size() < 16)); + // We assume that buff has room to hold the entire packet + size_t ptr = 0; + + // Populate control header + buff[ptr++] = conv_byte_order( + ((static_cast(dst_port) & mask_u64(DST_PORT_WIDTH)) << DST_PORT_OFFSET) + | ((static_cast(src_port) & mask_u64(SRC_PORT_WIDTH)) + << SRC_PORT_OFFSET) + | ((static_cast(data_vtr.size()) & mask_u64(NUM_DATA_WIDTH)) + << NUM_DATA_OFFSET) + | ((static_cast(seq_num) & mask_u64(SEQ_NUM_WIDTH)) << SEQ_NUM_OFFSET) + | ((static_cast(timestamp.is_initialized() ? 1 : 0) + & mask_u64(HAS_TIME_WIDTH)) + << HAS_TIME_OFFSET) + | ((static_cast(is_ack) & mask_u64(IS_ACK_WIDTH)) << IS_ACK_OFFSET) + | ((static_cast(src_epid) & mask_u64(SRC_EPID_WIDTH)) + << SRC_EPID_OFFSET)); + + // Populate optional timestamp + if (timestamp.is_initialized()) { + buff[ptr++] = conv_byte_order(timestamp.get()); + } + + // Populate control operation word + buff[ptr++] = conv_byte_order( + ((static_cast(address) & mask_u64(ADDRESS_WIDTH)) << ADDRESS_OFFSET) + | ((static_cast(byte_enable) & mask_u64(BYTE_ENABLE_WIDTH)) + << BYTE_ENABLE_OFFSET) + | ((static_cast(op_code) & mask_u64(OPCODE_WIDTH)) << OPCODE_OFFSET) + | ((static_cast(status) & mask_u64(STATUS_WIDTH)) << STATUS_OFFSET) + | (static_cast(data_vtr[0]) << HI_DATA_OFFSET)); + + // Populate the rest of the data + for (size_t i = 1; i < data_vtr.size(); i += 2) { + const uint32_t hi_data = + (((i + 2) >= data_vtr.size()) && (data_vtr.size() % 2 == 0)) + ? 0 + : data_vtr[i + 1]; + buff[ptr++] = + conv_byte_order(static_cast(hi_data) << HI_DATA_OFFSET + | static_cast(data_vtr[i]) << LO_DATA_OFFSET); + } + + // FIXME: This UHD_ASSERT_THROW is a bit late because memory has already been + // corrupted + UHD_ASSERT_THROW(ptr <= max_size_bytes); + // Return bytes written + return (ptr * sizeof(uint64_t)); +} + +void ctrl_payload::deserialize(const uint64_t* buff, + size_t max_size_bytes, + const std::function& conv_byte_order) +{ + // We assume that buff has room to hold the entire packet + size_t ptr = 0; + + // Read control header + uint64_t ctrl_header = conv_byte_order(buff[ptr++]); + data_vtr.resize(get_field_u64(ctrl_header, NUM_DATA_OFFSET, NUM_DATA_WIDTH)); + UHD_ASSERT_THROW((data_vtr.size() > 0 && data_vtr.size() < 16)); + dst_port = get_field_u64(ctrl_header, DST_PORT_OFFSET, DST_PORT_WIDTH); + src_port = get_field_u64(ctrl_header, SRC_PORT_OFFSET, SRC_PORT_WIDTH); + seq_num = get_field_u64(ctrl_header, SEQ_NUM_OFFSET, SEQ_NUM_WIDTH); + is_ack = get_field_u64(ctrl_header, IS_ACK_OFFSET, IS_ACK_WIDTH); + src_epid = get_field_u64(ctrl_header, SRC_EPID_OFFSET, SRC_EPID_WIDTH); + + // Read optional timestamp + if (get_field_u64(ctrl_header, HAS_TIME_OFFSET, HAS_TIME_WIDTH)) { + timestamp = conv_byte_order(buff[ptr++]); + } else { + timestamp = boost::none; + } + + // Read control operation word + uint64_t op_word = conv_byte_order(buff[ptr++]); + if (data_vtr.size() > 0) { + data_vtr[0] = get_field_u64(op_word, HI_DATA_OFFSET, 32); + } + address = get_field_u64(op_word, ADDRESS_OFFSET, ADDRESS_WIDTH); + byte_enable = get_field_u64(op_word, BYTE_ENABLE_OFFSET, BYTE_ENABLE_WIDTH); + op_code = get_field_u64(op_word, OPCODE_OFFSET, OPCODE_WIDTH); + status = get_field_u64(op_word, STATUS_OFFSET, STATUS_WIDTH); + + // Read the rest of the data + for (size_t i = 1; i < data_vtr.size(); i += 2) { + uint64_t data_word = conv_byte_order(buff[ptr++]); + if (((i + 2) < data_vtr.size()) || (data_vtr.size() % 2 != 0)) { + data_vtr[i + 1] = get_field_u64(data_word, HI_DATA_OFFSET, 32); + } + data_vtr[i] = get_field_u64(data_word, LO_DATA_OFFSET, 32); + } + UHD_ASSERT_THROW(ptr <= max_size_bytes); +} + +bool ctrl_payload::operator==(const ctrl_payload& rhs) const +{ + return (dst_port == rhs.dst_port) && (src_port == rhs.src_port) + && (seq_num == rhs.seq_num) + && (timestamp.is_initialized() == rhs.timestamp.is_initialized()) + && ((!timestamp.is_initialized()) || (timestamp.get() == rhs.timestamp.get())) + && (is_ack == rhs.is_ack) && (src_epid == rhs.src_epid) + && (address == rhs.address) && (data_vtr == rhs.data_vtr) + && (byte_enable == rhs.byte_enable) && (op_code == rhs.op_code) + && (status == rhs.status); +} + +const std::string ctrl_payload::to_string() const +{ + return str( + boost::format("ctrl_payload{dst_port:%d, dst_port:%d, seq_num:%d, timestamp:%s, " + "is_ack:%s, src_epid:%d, address:0x%05x, byte_enable:0x%x, " + "op_code:%d, status:%d, data[0]:0x%08x}\n") + % dst_port % src_port % int(seq_num) + % (timestamp.is_initialized() ? str(boost::format("0x%016x") % timestamp.get()) + : std::string("")) + % (is_ack ? "true" : "false") % src_epid % address % int(byte_enable) % op_code + % status % data_vtr[0]); +} + +//---------------------------------------------------- +// CHDR Stream Status Payload +//---------------------------------------------------- + +void strs_payload::populate_header(chdr_header& header) const +{ + header.set_pkt_type(PKT_TYPE_STRS); + header.set_eob(false); + header.set_eov(false); + header.set_num_mdata(0); +} + +size_t strs_payload::serialize(uint64_t* buff, + size_t max_size_bytes, + const std::function& conv_byte_order) const +{ + UHD_ASSERT_THROW(max_size_bytes >= (4 * sizeof(uint64_t))); + + // Populate first word + buff[0] = conv_byte_order( + ((static_cast(src_epid) & mask_u64(SRC_EPID_WIDTH)) << SRC_EPID_OFFSET) + | ((static_cast(status) & mask_u64(STATUS_WIDTH)) << STATUS_OFFSET) + | ((static_cast(capacity_bytes) & mask_u64(CAPACITY_BYTES_WIDTH)) + << CAPACITY_BYTES_OFFSET)); + + // Populate second word + buff[1] = conv_byte_order( + ((static_cast(capacity_pkts) & mask_u64(CAPACITY_PKTS_WIDTH)) + << CAPACITY_PKTS_OFFSET) + | ((static_cast(xfer_count_pkts) & mask_u64(XFER_COUNT_PKTS_WIDTH)) + << XFER_COUNT_PKTS_OFFSET)); + + // Populate third word + buff[2] = conv_byte_order(xfer_count_bytes); + + // Populate fourth word + buff[3] = conv_byte_order( + ((static_cast(buff_info) & mask_u64(BUFF_INFO_WIDTH)) + << BUFF_INFO_OFFSET) + | ((static_cast(status_info) & mask_u64(STATUS_INFO_WIDTH)) + << STATUS_INFO_OFFSET)); + + // Return bytes written + return (4 * sizeof(uint64_t)); +} + +void strs_payload::deserialize(const uint64_t* buff, + size_t num_elems, + const std::function& conv_byte_order) +{ + UHD_ASSERT_THROW(num_elems >= 4); + + // Read first word + uint64_t word0 = conv_byte_order(buff[0]); + src_epid = get_field_u64(word0, SRC_EPID_OFFSET, SRC_EPID_WIDTH); + status = get_field_u64(word0, STATUS_OFFSET, STATUS_WIDTH); + capacity_bytes = + get_field_u64(word0, CAPACITY_BYTES_OFFSET, CAPACITY_BYTES_WIDTH); + + // Read second word + uint64_t word1 = conv_byte_order(buff[1]); + capacity_pkts = + get_field_u64(word1, CAPACITY_PKTS_OFFSET, CAPACITY_PKTS_WIDTH); + xfer_count_pkts = + get_field_u64(word1, XFER_COUNT_PKTS_OFFSET, XFER_COUNT_PKTS_WIDTH); + + // Read third word + xfer_count_bytes = conv_byte_order(buff[2]); + + // Read fourth word + uint64_t word3 = conv_byte_order(buff[3]); + buff_info = get_field_u64(word3, BUFF_INFO_OFFSET, BUFF_INFO_WIDTH); + status_info = get_field_u64(word3, STATUS_INFO_OFFSET, STATUS_INFO_WIDTH); +} + +bool strs_payload::operator==(const strs_payload& rhs) const +{ + return (src_epid == rhs.src_epid) && (status == rhs.status) + && (capacity_bytes == rhs.capacity_bytes) + && (capacity_pkts == rhs.capacity_pkts) + && (xfer_count_pkts == rhs.xfer_count_pkts) + && (xfer_count_bytes == rhs.xfer_count_bytes) && (buff_info == rhs.buff_info) + && (status_info == rhs.status_info); +} + +const std::string strs_payload::to_string() const +{ + return str(boost::format("strs_payload{src_epid:%lu, status:%d, capacity_bytes:%lu, " + "capacity_pkts:%lu, " + "xfer_count_pkts:%lu, xfer_count_bytes:%lu, " + "buff_info:0x%x, status_info:0x%x}\n") + % src_epid % int(status) % capacity_bytes % capacity_pkts % xfer_count_pkts + % xfer_count_bytes % buff_info % status_info); +} + +//---------------------------------------------------- +// CHDR Stream Command Payload +//---------------------------------------------------- + +void strc_payload::populate_header(chdr_header& header) const +{ + header.set_pkt_type(PKT_TYPE_STRC); + header.set_eob(false); + header.set_eov(false); + header.set_num_mdata(0); +} + +size_t strc_payload::serialize(uint64_t* buff, + size_t max_size_bytes, + const std::function& conv_byte_order) const +{ + UHD_ASSERT_THROW(max_size_bytes >= (2 * sizeof(uint64_t))); + + // Populate first word + buff[0] = conv_byte_order( + ((static_cast(src_epid) & mask_u64(SRC_EPID_WIDTH)) << SRC_EPID_OFFSET) + | ((static_cast(op_code) & mask_u64(OP_CODE_WIDTH)) << OP_CODE_OFFSET) + | ((static_cast(op_data) & mask_u64(OP_DATA_WIDTH)) << OP_DATA_OFFSET) + | ((static_cast(num_pkts) & mask_u64(NUM_PKTS_WIDTH)) + << NUM_PKTS_OFFSET)); + + // Populate second word + buff[1] = conv_byte_order(num_bytes); + + // Return bytes written + return (2 * sizeof(uint64_t)); +} + +void strc_payload::deserialize(const uint64_t* buff, + size_t num_elems, + const std::function& conv_byte_order) +{ + UHD_ASSERT_THROW(num_elems >= 2); + + // Read first word + uint64_t word0 = conv_byte_order(buff[0]); + src_epid = get_field_u64(word0, SRC_EPID_OFFSET, SRC_EPID_WIDTH); + op_code = get_field_u64(word0, OP_CODE_OFFSET, OP_CODE_WIDTH); + op_data = get_field_u64(word0, OP_DATA_OFFSET, OP_DATA_WIDTH); + num_pkts = get_field_u64(word0, NUM_PKTS_OFFSET, NUM_PKTS_WIDTH); + // Read second word + num_bytes = conv_byte_order(buff[1]); +} + +bool strc_payload::operator==(const strc_payload& rhs) const +{ + return (src_epid == rhs.src_epid) && (op_code == rhs.op_code) + && (op_data == rhs.op_data) && (num_pkts == rhs.num_pkts) + && (num_bytes == rhs.num_bytes); +} + +const std::string strc_payload::to_string() const +{ + return str(boost::format("strc_payload{src_epid:%lu, op_code:%d, op_data:0x%x, " + "num_pkts:%lu, num_bytes:%lu}\n") + % src_epid % int(op_code) % int(op_data) % num_pkts % num_bytes); +} + +//---------------------------------------------------- +// CHDR Management Payload +//---------------------------------------------------- + +//! Serialize this hop into a list of 64-bit words +size_t mgmt_hop_t::serialize(std::vector& target, + const std::function& conv_byte_order) const +{ + for (size_t i = 0; i < get_num_ops(); i++) { + target.push_back( + conv_byte_order((static_cast(_ops.at(i).get_op_payload()) << 16) + | (static_cast(_ops.at(i).get_op_code()) << 8) + | (static_cast(get_num_ops() - i - 1) << 0))); + } + return get_num_ops(); +} + +//! Deserialize this hop into from list of 64-bit words +void mgmt_hop_t::deserialize( + std::list& src, const std::function& conv_byte_order) +{ + _ops.clear(); + size_t ops_remaining = 0; + do { + // TODO: Change this to a legit exception + UHD_ASSERT_THROW(!src.empty()); + + uint64_t op_word = conv_byte_order(src.front()); + ops_remaining = static_cast(op_word & 0xFF); + mgmt_op_t op(static_cast((op_word >> 8) & 0xFF), + static_cast((op_word >> 16))); + _ops.push_back(op); + src.pop_front(); + } while (ops_remaining > 0); +} + +void mgmt_payload::populate_header(chdr_header& header) const +{ + header.set_pkt_type(PKT_TYPE_MGMT); + header.set_eob(false); + header.set_eov(false); + header.set_num_mdata(0); + header.set_vc(0); + header.set_dst_epid(0); +} + +size_t mgmt_payload::serialize(uint64_t* buff, + size_t max_size_bytes, + const std::function& conv_byte_order) const +{ + std::vector target; + // Insert header + target.push_back(conv_byte_order( + (static_cast(_protover) << 48) + | (static_cast(static_cast(_chdr_w) & 0x7) << 45) + | (static_cast(get_num_hops() & 0x3FF) << 16) + | (static_cast(_src_epid) << 0))); + // Insert data from each hop + for (const auto& hop : _hops) { + hop.serialize(target, conv_byte_order); + } + UHD_ASSERT_THROW(target.size() <= max_size_bytes); + + // We use a vector and copy just for ease of implementation + // These transactions are not performance critical + std::copy(target.begin(), target.end(), buff); + return (target.size() * sizeof(uint64_t)); +} + +void mgmt_payload::deserialize(const uint64_t* buff, + size_t num_elems, + const std::function& conv_byte_order) +{ + UHD_ASSERT_THROW(num_elems > 1); + + // We use a list and copy just for ease of implementation + // These transactions are not performance critical + std::list src_list(buff, buff + num_elems); + + _hops.clear(); + + // Deframe the header + uint64_t hdr = conv_byte_order(src_list.front()); + _hops.resize(static_cast((hdr >> 16) & 0x3FF)); + _src_epid = static_cast(hdr & 0xFFFF); + _chdr_w = static_cast((hdr >> 45) & 0x7); + _protover = static_cast((hdr >> 48) & 0xFFFF); + src_list.pop_front(); + + // Populate all hops + for (size_t i = 0; i < get_num_hops(); i++) { + _hops[i].deserialize(src_list, conv_byte_order); + } +} + +const std::string mgmt_payload::to_string() const +{ + return str(boost::format( + "mgmt_payload{src_epid:%lu, chdr_w:%d, protover:0x%x, num_hops:%lu}\n") + % _src_epid % int(_chdr_w) % _protover % _hops.size()); +} diff --git a/host/lib/rfnoc/ctrlport_endpoint.cpp b/host/lib/rfnoc/ctrlport_endpoint.cpp index d5f4ef98c..93236940f 100644 --- a/host/lib/rfnoc/ctrlport_endpoint.cpp +++ b/host/lib/rfnoc/ctrlport_endpoint.cpp @@ -6,8 +6,8 @@ #include #include -#include -#include +#include +#include #include #include #include diff --git a/host/lib/rfnoc/mgmt_portal.cpp b/host/lib/rfnoc/mgmt_portal.cpp index fcb19997e..76d708a0e 100644 --- a/host/lib/rfnoc/mgmt_portal.cpp +++ b/host/lib/rfnoc/mgmt_portal.cpp @@ -7,7 +7,7 @@ #include #include -#include +#include #include #include #include -- cgit v1.2.3