DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
TxEngine.hpp
Go to the documentation of this file.
1
31#ifndef DPDKLIBS_INCLUDE_DPDKLIBS_SENDER_TXENGINE_HPP_
32#define DPDKLIBS_INCLUDE_DPDKLIBS_SENDER_TXENGINE_HPP_
33
34#include <cstddef>
35#include <cstdint>
36#include <cstring>
37
39
40enum class TxOutcome
41{
42 kSent, // backend took ownership of the buffer
43 kAllocFailed, // no buffer was available
44 kPrepareFailed, // append rejected: insufficient tailroom
45 kTxFailed // backend refused the packet; buffer freed
46};
47
48template<typename Backend>
50{
51public:
52 BufGuard(Backend& backend, void* buf)
53 : m_backend(backend)
54 , m_buf(buf)
55 {
56 }
58 {
59 if (m_buf != nullptr) {
60 m_backend.free(m_buf);
61 }
62 }
63 BufGuard(const BufGuard&) = delete;
64 BufGuard& operator=(const BufGuard&) = delete;
65 BufGuard(BufGuard&&) = delete;
67
68 void release() { m_buf = nullptr; }
69
70private:
71 Backend& m_backend;
72 void* m_buf;
73};
74
75// Allocate, build, and transmit one packet.
76//
77// - build(dst) writes exactly packet_bytes into dst.
78// - If pre_tx_copy is non-null, the constructed bytes are copied there before
79// the transmit attempt. The buffer is not readable after a successful
80// transmit, so this copy is the only record of the transmitted bytes.
81// - on_sent() runs only after a successful transmit.
82template<typename Backend, typename BuildFn, typename OnSentFn>
84send_packet(Backend& backend, std::size_t packet_bytes, std::uint8_t* pre_tx_copy, BuildFn&& build, OnSentFn&& on_sent)
85{
86 void* buf = backend.alloc();
87 if (buf == nullptr) {
89 }
90 BufGuard<Backend> guard(backend, buf);
91
92 std::uint8_t* dst = backend.prepare(buf, packet_bytes);
93 if (dst == nullptr) {
95 }
96
97 build(dst);
98 if (pre_tx_copy != nullptr) {
99 std::memcpy(pre_tx_copy, dst, packet_bytes);
100 }
101
102 if (!backend.transmit(buf)) {
104 }
105
106 guard.release();
107 on_sent();
108 return TxOutcome::kSent;
109}
110
111} // namespace dunedaq::dpdklibs::sender
112
113#endif // DPDKLIBS_INCLUDE_DPDKLIBS_SENDER_TXENGINE_HPP_
BufGuard(Backend &backend, void *buf)
Definition TxEngine.hpp:52
BufGuard & operator=(BufGuard &&)=delete
BufGuard & operator=(const BufGuard &)=delete
BufGuard(const BufGuard &)=delete
TxOutcome send_packet(Backend &backend, std::size_t packet_bytes, std::uint8_t *pre_tx_copy, BuildFn &&build, OnSentFn &&on_sent)
Definition TxEngine.hpp:84