NDN-DPDK
High-Speed Named Data Networking Forwarder
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
tlv-encoder.h
Go to the documentation of this file.
1 #ifndef NDNDPDK_NDNI_TLV_ENCODER_H
2 #define NDNDPDK_NDNI_TLV_ENCODER_H
3 
6 #include "common.h"
7 
9 static __rte_always_inline uint8_t
10 TlvEncoder_SizeofVarNum(uint32_t n) {
11  if (likely(n < 0xFD)) {
12  return 1;
13  } else if (likely(n <= UINT16_MAX)) {
14  return 3;
15  } else {
16  return 5;
17  }
18 }
19 
25 __attribute__((nonnull)) static __rte_always_inline size_t
26 TlvEncoder_WriteVarNum(uint8_t* room, uint32_t n) {
27  if (likely(n < 0xFD)) {
28  room[0] = n;
29  return 1;
30  } else if (likely(n <= UINT16_MAX)) {
31  room[0] = 0xFD;
32  unaligned_uint16_t* b = RTE_PTR_ADD(room, 1);
33  *b = rte_cpu_to_be_16(n);
34  return 3;
35  } else {
36  room[0] = 0xFE;
37  unaligned_uint32_t* b = RTE_PTR_ADD(room, 1);
38  *b = rte_cpu_to_be_32(n);
39  return 5;
40  }
41 }
42 
47 __attribute__((nonnull)) static __rte_always_inline void
48 TlvEncoder_PrependTL(struct rte_mbuf* m, uint32_t type, uint32_t length) {
49  uint8_t* room = (uint8_t*)rte_pktmbuf_prepend(m, TlvEncoder_SizeofVarNum(type) +
50  TlvEncoder_SizeofVarNum(length));
51  room += TlvEncoder_WriteVarNum(room, type);
52  TlvEncoder_WriteVarNum(room, length);
53 }
54 
60 #define TlvEncoder_ConstTL1(type, length) \
61  __extension__({ \
62  static_assert(__builtin_constant_p((type)), ""); \
63  static_assert(__builtin_constant_p((length)), ""); \
64  static_assert((type) < 0xFD, ""); \
65  static_assert((length) <= UINT8_MAX, ""); \
66  rte_cpu_to_be_16(((type) << 8) | (length)); \
67  })
68 
74 #define TlvEncoder_ConstTL3(type, length) \
75  __extension__({ \
76  static_assert(__builtin_constant_p((type)), ""); \
77  static_assert(__builtin_constant_p((length)), ""); \
78  static_assert(0xFD <= (type) && (type) <= UINT16_MAX, ""); \
79  static_assert((length) <= UINT8_MAX, ""); \
80  rte_cpu_to_be_32(0xFD000000 | ((type) << 8) | (length)); \
81  })
82 
83 #endif // NDNDPDK_NDNI_TLV_ENCODER_H