24#include <seastar/core/deleter.hh>
25#include <seastar/core/temporary_buffer.hh>
26#include <seastar/net/const.hh>
27#include <seastar/util/std-compat.hh>
28#include <seastar/util/modules.hh>
44SEASTAR_MODULE_EXPORT_BEGIN
52 ip_protocol_num protocol = ip_protocol_num::unused;
53 bool needs_csum =
false;
54 uint8_t ip_hdr_len = 20;
55 uint8_t tcp_hdr_len = 20;
56 uint8_t udp_hdr_len = 8;
57 bool needs_ip_csum =
false;
58 bool reassembled =
false;
59 uint16_t tso_seg_size = 0;
61 std::optional<uint16_t> vlan_tci;
89 static constexpr size_t internal_data_size = 128 - 16;
90 static constexpr size_t default_nr_frags = 4;
92 struct pseudo_vector {
95 pseudo_vector(
fragment* start,
size_t nr) noexcept
96 : _start(start), _finish(_start + nr) {}
97 fragment* begin()
noexcept {
return _start; }
98 fragment* end()
noexcept {
return _finish; }
99 fragment& operator[](
size_t idx)
noexcept {
return _start[idx]; }
106 uint16_t _nr_frags = 0;
107 uint16_t _allocated_frags;
109 std::optional<uint32_t> _rss_hash;
110 char _data[internal_data_size];
111 unsigned _headroom = internal_data_size;
116 impl(
size_t nr_frags = default_nr_frags)
noexcept;
118 impl(
fragment frag,
size_t nr_frags = default_nr_frags);
120 pseudo_vector fragments()
noexcept {
return { _frags, _nr_frags }; }
122 static std::unique_ptr<impl> allocate(
size_t nr_frags) {
123 nr_frags = std::max(nr_frags, default_nr_frags);
124 return std::unique_ptr<impl>(
new (nr_frags)
impl(nr_frags));
127 static std::unique_ptr<impl>
copy(
impl* old,
size_t nr) {
128 auto n = allocate(nr);
129 n->_deleter = std::move(old->_deleter);
131 n->_nr_frags = old->_nr_frags;
132 n->_headroom = old->_headroom;
133 n->_offload_info = old->_offload_info;
134 n->_rss_hash = old->_rss_hash;
135 std::copy(old->_frags, old->_frags + old->_nr_frags, n->_frags);
136 old->copy_internal_fragment_to(n.get());
140 static std::unique_ptr<impl>
copy(
impl* old) {
141 return copy(old, old->_nr_frags);
144 static std::unique_ptr<impl> allocate_if_needed(std::unique_ptr<impl> old,
size_t extra_frags) {
145 if (old->_allocated_frags >= old->_nr_frags + extra_frags) {
148 return copy(old.get(), std::max<size_t>(old->_nr_frags + extra_frags, 2 * old->_nr_frags));
150 void*
operator new(
size_t size,
size_t nr_frags = default_nr_frags) {
151 assert(nr_frags == uint16_t(nr_frags));
152 return ::operator
new(size + nr_frags *
sizeof(
fragment));
155 void operator delete(
void* ptr, size_t) {
156 return ::operator
delete(ptr);
159 void operator delete(
void* ptr) {
160 return ::operator
delete(ptr);
163 bool using_internal_data()
const noexcept {
165 && _frags[0].base >= _data
166 && _frags[0].base < _data + internal_data_size;
169 void unuse_internal_data() {
170 if (!using_internal_data()) {
173 auto buf =
static_cast<char*
>(::malloc(_frags[0].size));
175 throw std::bad_alloc();
177 deleter d = make_free_deleter(buf);
178 std::copy(_frags[0].base, _frags[0].base + _frags[0].size, buf);
179 _frags[0].base = buf;
180 d.
append(std::move(_deleter));
181 _deleter = std::move(d);
182 _headroom = internal_data_size;
184 void copy_internal_fragment_to(
impl* to)
noexcept {
185 if (!using_internal_data()) {
188 to->_frags[0].base = to->_data + _headroom;
189 std::copy(_frags[0].base, _frags[0].base + _frags[0].size,
193 packet(std::unique_ptr<impl>&&
impl) noexcept : _impl(std::move(
impl)) {}
194 std::unique_ptr<impl> _impl;
196 static packet from_static_data(
const char* data,
size_t len)
noexcept {
207 packet(
const char* data,
size_t len);
215 template <
typename Iterator>
235 new (
this)
packet(std::move(x));
240 unsigned len()
const noexcept {
return _impl->_len; }
241 unsigned memory()
const noexcept {
return len() +
sizeof(packet::impl); }
243 fragment frag(
unsigned idx)
const noexcept {
return _impl->_frags[idx]; }
244 fragment& frag(
unsigned idx)
noexcept {
return _impl->_frags[idx]; }
246 unsigned nr_frags()
const noexcept {
return _impl->_nr_frags; }
247 pseudo_vector fragments()
const noexcept {
return { _impl->_frags, _impl->_nr_frags }; }
248 fragment* fragment_array()
const noexcept {
return _impl->_frags; }
252 packet share(
size_t offset,
size_t len);
256 void trim_front(
size_t how_much)
noexcept;
257 void trim_back(
size_t how_much)
noexcept;
260 template <
typename Header>
261 Header* get_header(
size_t offset = 0);
264 char* get_header(
size_t offset,
size_t size);
267 template <
typename Header>
268 Header* prepend_header(
size_t extra_size = 0);
271 char* prepend_uninitialized_header(
size_t size);
273 packet free_on_cpu(
unsigned cpu, std::function<
void()> cb = []{});
275 void linearize() {
return linearize(0, len()); }
277 void reset()
noexcept { _impl.reset(); }
279 void reserve(
int n_frags) {
280 if (n_frags > _impl->_nr_frags) {
281 auto extra = n_frags - _impl->_nr_frags;
282 _impl = impl::allocate_if_needed(std::move(_impl), extra);
285 std::optional<uint32_t> rss_hash()
const noexcept {
286 return _impl->_rss_hash;
288 std::optional<uint32_t> set_rss_hash(uint32_t hash)
noexcept {
289 return _impl->_rss_hash = hash;
293 template <
typename Func>
294 void release_into(Func&& func) {
296 if (_impl->using_internal_data()) {
297 auto&& f = frag(idx++);
300 while (idx < nr_frags()) {
301 auto&& f = frag(idx++);
305 std::vector<temporary_buffer<char>> release() {
306 std::vector<temporary_buffer<char>> ret;
307 ret.reserve(_impl->_nr_frags);
309 ret.push_back(std::move(frag));
313 explicit operator bool()
noexcept {
316 static packet make_null_packet()
noexcept {
320 void linearize(
size_t at_frag,
size_t desired_size);
321 bool allocate_headroom(
size_t size);
323 struct offload_info get_offload_info() const noexcept {
return _impl->_offload_info; }
324 struct offload_info& offload_info_ref()
noexcept {
return _impl->_offload_info; }
325 void set_offload_info(
struct offload_info oi)
noexcept { _impl->_offload_info = oi; }
328std::ostream& operator<<(std::ostream& os,
const packet& p);
330SEASTAR_MODULE_EXPORT_END
333packet::packet(
packet&& x) noexcept
334 : _impl(std::move(x._impl)) {
338packet::impl::impl(
size_t nr_frags) noexcept
339 : _len(0), _allocated_frags(nr_frags) {
343packet::impl::impl(fragment frag,
size_t nr_frags)
344 : _len(frag.size), _allocated_frags(nr_frags) {
345 assert(_allocated_frags > _nr_frags);
346 if (frag.size <= internal_data_size) {
347 _headroom -= frag.size;
348 _frags[0] = { _data + _headroom, frag.size };
350 auto buf =
static_cast<char*
>(::malloc(frag.size));
352 throw std::bad_alloc();
354 deleter d = make_free_deleter(buf);
355 _frags[0] = { buf, frag.size };
356 _deleter.append(std::move(d));
358 std::copy(frag.base, frag.base + frag.size, _frags[0].base);
364 : _impl(
impl::allocate(1)) {
368packet::packet(
size_t nr_frags)
369 : _impl(
impl::allocate(nr_frags)) {
373packet::packet(fragment frag) : _impl(new
impl(frag)) {
377packet::packet(
const char* data,
size_t size) : packet(fragment{const_cast<char*>(data), size}) {
381packet::packet(fragment frag, deleter d)
382 : _impl(
impl::allocate(1)) {
383 _impl->_deleter = std::move(d);
384 _impl->_frags[_impl->_nr_frags++] = frag;
385 _impl->_len = frag.size;
389packet::packet(std::vector<fragment> frag, deleter d)
390 : _impl(
impl::allocate(frag.size())) {
391 _impl->_deleter = std::move(d);
392 std::copy(frag.begin(), frag.end(), _impl->_frags);
393 _impl->_nr_frags = frag.size();
395 for (
auto&& f : _impl->fragments()) {
396 _impl->_len += f.size;
400template <
typename Iterator>
402packet::packet(Iterator begin, Iterator end, deleter del) {
403 unsigned nr_frags = 0, len = 0;
404 nr_frags = std::distance(begin, end);
405 std::for_each(begin, end, [&] (
const fragment& frag) { len += frag.size; });
406 _impl = impl::allocate(nr_frags);
407 _impl->_deleter = std::move(del);
409 _impl->_nr_frags = nr_frags;
410 std::copy(begin, end, _impl->_frags);
414packet::packet(packet&& x, fragment frag)
415 : _impl(
impl::allocate_if_needed(
std::move(x._impl), 1)) {
416 _impl->_len += frag.size;
417 std::unique_ptr<char[]> buf(
new char[frag.size]);
418 std::copy(frag.base, frag.base + frag.size, buf.get());
419 _impl->_frags[_impl->_nr_frags++] = {buf.get(), frag.size};
420 _impl->_deleter = make_deleter(std::move(_impl->_deleter), [buf = buf.release()] {
427packet::allocate_headroom(
size_t size) {
428 if (_impl->_headroom >= size) {
430 if (!_impl->using_internal_data()) {
431 _impl = impl::allocate_if_needed(std::move(_impl), 1);
432 std::copy_backward(_impl->_frags, _impl->_frags + _impl->_nr_frags,
433 _impl->_frags + _impl->_nr_frags + 1);
434 _impl->_frags[0] = { _impl->_data + internal_data_size, 0 };
437 _impl->_headroom -= size;
438 _impl->_frags[0].base -= size;
439 _impl->_frags[0].size += size;
448packet::packet(fragment frag, packet&& x)
449 : _impl(
std::move(x._impl)) {
451 if (allocate_headroom(frag.size)) {
452 std::copy(frag.base, frag.base + frag.size, _impl->_frags[0].base);
456 _impl->unuse_internal_data();
457 _impl = impl::allocate_if_needed(std::move(_impl), 1);
458 _impl->_len += frag.size;
459 std::unique_ptr<char[]> buf(
new char[frag.size]);
460 std::copy(frag.base, frag.base + frag.size, buf.get());
461 std::copy_backward(_impl->_frags, _impl->_frags + _impl->_nr_frags,
462 _impl->_frags + _impl->_nr_frags + 1);
464 _impl->_frags[0] = {buf.get(), frag.size};
465 _impl->_deleter = make_deleter(std::move(_impl->_deleter),
466 [buf = std::move(buf)] {});
471packet::packet(packet&& x, fragment frag, deleter d)
472 : _impl(
impl::allocate_if_needed(
std::move(x._impl), 1)) {
473 _impl->_len += frag.size;
474 _impl->_frags[_impl->_nr_frags++] = frag;
475 d.append(std::move(_impl->_deleter));
476 _impl->_deleter = std::move(d);
480packet::packet(packet&& x, deleter d)
481 : _impl(
std::move(x._impl)) {
482 _impl->_deleter.append(std::move(d));
486packet::packet(packet&& x, temporary_buffer<char> buf)
487 : packet(
std::move(x), fragment{buf.get_write(), buf.size()}, buf.release()) {
491packet::packet(temporary_buffer<char> buf)
492 : packet(fragment{buf.get_write(), buf.size()}, buf.release()) {}
495void packet::append(packet&& p) {
497 *
this = std::move(p);
500 _impl = impl::allocate_if_needed(std::move(_impl), p._impl->_nr_frags);
501 _impl->_len += p._impl->_len;
502 p._impl->unuse_internal_data();
503 std::copy(p._impl->_frags, p._impl->_frags + p._impl->_nr_frags,
504 _impl->_frags + _impl->_nr_frags);
505 _impl->_nr_frags += p._impl->_nr_frags;
506 p._impl->_deleter.append(std::move(_impl->_deleter));
507 _impl->_deleter = std::move(p._impl->_deleter);
511char* packet::get_header(
size_t offset,
size_t size) {
512 if (offset + size > _impl->_len) {
516 while (i != _impl->_nr_frags && offset >= _impl->_frags[i].size) {
517 offset -= _impl->_frags[i++].size;
519 if (i == _impl->_nr_frags) {
522 if (offset + size > _impl->_frags[i].size) {
523 linearize(i, offset + size);
525 return _impl->_frags[i].base + offset;
528template <
typename Header>
530Header* packet::get_header(
size_t offset) {
531 return reinterpret_cast<Header*
>(get_header(offset,
sizeof(Header)));
535void packet::trim_front(
size_t how_much)
noexcept {
536 assert(how_much <= _impl->_len);
537 _impl->_len -= how_much;
539 while (how_much && how_much >= _impl->_frags[i].size) {
540 how_much -= _impl->_frags[i++].size;
542 std::copy(_impl->_frags + i, _impl->_frags + _impl->_nr_frags, _impl->_frags);
543 _impl->_nr_frags -= i;
544 if (!_impl->using_internal_data()) {
545 _impl->_headroom = internal_data_size;
548 if (_impl->using_internal_data()) {
549 _impl->_headroom += how_much;
551 _impl->_frags[0].base += how_much;
552 _impl->_frags[0].size -= how_much;
557void packet::trim_back(
size_t how_much)
noexcept {
558 assert(how_much <= _impl->_len);
559 _impl->_len -= how_much;
560 size_t i = _impl->_nr_frags - 1;
561 while (how_much && how_much >= _impl->_frags[i].size) {
562 how_much -= _impl->_frags[i--].size;
564 _impl->_nr_frags = i + 1;
566 _impl->_frags[i].size -= how_much;
567 if (i == 0 && _impl->using_internal_data()) {
568 _impl->_headroom += how_much;
573template <
typename Header>
575packet::prepend_header(
size_t extra_size) {
576 auto h = prepend_uninitialized_header(
sizeof(Header) + extra_size);
577 return new (h) Header{};
582char* packet::prepend_uninitialized_header(
size_t size) {
583 if (!allocate_headroom(size)) {
585 _impl->unuse_internal_data();
587 if (!allocate_headroom(size)) {
590 _impl = impl::allocate_if_needed(std::move(_impl), 1);
591 std::unique_ptr<char[]> buf(
new char[size]);
592 std::copy_backward(_impl->_frags, _impl->_frags + _impl->_nr_frags,
593 _impl->_frags + _impl->_nr_frags + 1);
595 _impl->_frags[0] = {buf.get(), size};
596 _impl->_deleter = make_deleter(std::move(_impl->_deleter),
597 [buf = std::move(buf)] {});
600 return _impl->_frags[0].base;
604packet packet::share() {
605 return share(0, _impl->_len);
609packet packet::share(
size_t offset,
size_t len) {
610 _impl->unuse_internal_data();
612 n._impl = impl::allocate_if_needed(std::move(n._impl), _impl->_nr_frags);
614 while (offset > 0 && offset >= _impl->_frags[idx].size) {
615 offset -= _impl->_frags[idx++].size;
617 while (n._impl->_len < len) {
618 auto& f = _impl->_frags[idx++];
619 auto fsize = std::min(len - n._impl->_len, f.size - offset);
620 n._impl->_frags[n._impl->_nr_frags++] = { f.base + offset, fsize };
621 n._impl->_len += fsize;
624 n._impl->_offload_info = _impl->_offload_info;
625 assert(!n._impl->_deleter);
626 n._impl->_deleter = _impl->_deleter.share();
Definition: deleter.hh:52
void append(deleter d)
Definition: deleter.hh:220
holds the implementation parts of the metrics layer, do not use directly.
Seastar API namespace.
Definition: abort_on_ebadf.hh:26
future copy(input_stream< CharType > &in, output_stream< CharType > &out)
copy all the content from the input stream to the output stream
Definition: iostream-impl.hh:550