aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/rfnoc/chdr_packet.cpp
blob: d4b7494e245ee4694baf2b590e27c07ee9acfbc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//
// Copyright 2019 Ettus Research, a National Instruments Brand
//
// SPDX-License-Identifier: GPL-3.0-or-later
//

#include <uhdlib/rfnoc/chdr_packet.hpp>
#include <cassert>
#include <functional>
#include <memory>

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 <size_t chdr_w, endianness_t endianness>
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<uint64_t*>(reinterpret_cast<const uint64_t*>(pkt_buff));
        _mdata_offset = _compute_mdata_offset(get_chdr_header());
    }

    virtual void refresh(void* pkt_buff, chdr_header& header, uint64_t timestamp = 0)
    {
        assert(pkt_buff);
        _pkt_buff    = reinterpret_cast<uint64_t*>(pkt_buff);
        _pkt_buff[0] = u64_from_host(header);
        if (_has_timestamp(header)) {
            _pkt_buff[1] = u64_from_host(timestamp);
        }
        _mdata_offset = _compute_mdata_offset(get_chdr_header());
    }

    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<uint64_t> 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<void*>(
            const_cast<chdr_packet_impl<chdr_w, endianness>*>(this)->get_mdata_ptr());
    }

    virtual void* get_mdata_ptr()
    {
        return reinterpret_cast<void*>(_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<void*>(
            const_cast<chdr_packet_impl<chdr_w, endianness>*>(this)->get_payload_ptr());
    }

    virtual void* get_payload_ptr()
    {
        return reinterpret_cast<void*>(
            _pkt_buff
            + (chdr_w_stride * (_mdata_offset + get_chdr_header().get_num_mdata())));
    }

    virtual size_t calculate_payload_offset(const packet_type_t pkt_type,
        const uint8_t num_mdata = 0) const
    {
        chdr_header header;
        header.set_pkt_type(pkt_type);
        return (_compute_mdata_offset(header) + num_mdata) * chdr_w_bytes;
    }

private:
    inline bool _has_timestamp(const chdr_header& header) const
    {
        return (header.get_pkt_type() == PKT_TYPE_DATA_WITH_TS);
    }

    inline size_t _compute_mdata_offset(const chdr_header& header) const
    {
        // The metadata offset depends on the chdr_w and whether we have a timestamp
        if (chdr_w == 64) {
            return _has_timestamp(header) ? 2 : 1;
        } else {
            return 1;
        }
    }

    inline static uint64_t u64_to_host(uint64_t word)
    {
        return (endianness == ENDIANNESS_BIG) ? uhd::ntohx<uint64_t>(word)
                                              : uhd::wtohx<uint64_t>(word);
    }

    inline static uint64_t u64_from_host(uint64_t word)
    {
        return (endianness == ENDIANNESS_BIG) ? uhd::htonx<uint64_t>(word)
                                              : uhd::htowx<uint64_t>(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<chdr_packet_impl<512, ENDIANNESS_BIG>>(mtu_bytes);
            case CHDR_W_256:
                return std::make_unique<chdr_packet_impl<256, ENDIANNESS_BIG>>(mtu_bytes);
            case CHDR_W_128:
                return std::make_unique<chdr_packet_impl<128, ENDIANNESS_BIG>>(mtu_bytes);
            case CHDR_W_64:
                return std::make_unique<chdr_packet_impl<64, ENDIANNESS_BIG>>(mtu_bytes);
            default:
                assert(0);
        }
    } else {
        switch (_chdr_w) {
            case CHDR_W_512:
                return std::make_unique<chdr_packet_impl<512, ENDIANNESS_LITTLE>>(
                    mtu_bytes);
            case CHDR_W_256:
                return std::make_unique<chdr_packet_impl<256, ENDIANNESS_LITTLE>>(
                    mtu_bytes);
            case CHDR_W_128:
                return std::make_unique<chdr_packet_impl<128, ENDIANNESS_LITTLE>>(
                    mtu_bytes);
            case CHDR_W_64:
                return std::make_unique<chdr_packet_impl<64, ENDIANNESS_LITTLE>>(
                    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<chdr_ctrl_packet>(make_generic(mtu_bytes));
}

chdr_strs_packet::uptr chdr_packet_factory::make_strs(size_t mtu_bytes) const
{
    return std::make_unique<chdr_strs_packet>(make_generic(mtu_bytes));
}

chdr_strc_packet::uptr chdr_packet_factory::make_strc(size_t mtu_bytes) const
{
    return std::make_unique<chdr_strc_packet>(make_generic(mtu_bytes));
}

chdr_mgmt_packet::uptr chdr_packet_factory::make_mgmt(size_t mtu_bytes) const
{
    return std::make_unique<chdr_mgmt_packet>(make_generic(mtu_bytes));
}