net_pkt.h 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164
  1. /** @file
  2. * @brief Network packet buffer descriptor API
  3. *
  4. * Network data is passed between different parts of the stack via
  5. * net_buf struct.
  6. */
  7. /*
  8. * Copyright (c) 2016 Intel Corporation
  9. *
  10. * SPDX-License-Identifier: Apache-2.0
  11. */
  12. /* Data buffer API - used for all data to/from net */
  13. #ifndef ZEPHYR_INCLUDE_NET_NET_PKT_H_
  14. #define ZEPHYR_INCLUDE_NET_NET_PKT_H_
  15. #include <zephyr/types.h>
  16. #include <stdbool.h>
  17. #include <net/buf.h>
  18. #include <net/net_core.h>
  19. #include <net/net_linkaddr.h>
  20. #include <net/net_ip.h>
  21. #include <net/net_if.h>
  22. #include <net/net_context.h>
  23. #include <net/ethernet_vlan.h>
  24. #include <net/ptp_time.h>
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. /**
  29. * @brief Network packet management library
  30. * @defgroup net_pkt Network Packet Library
  31. * @ingroup networking
  32. * @{
  33. */
  34. struct net_context;
  35. struct canbus_net_isotp_tx_ctx;
  36. struct canbus_net_isotp_rx_ctx;
  37. /* buffer cursor used in net_pkt */
  38. struct net_pkt_cursor {
  39. /** Current net_buf pointer by the cursor */
  40. struct net_buf *buf;
  41. /** Current position in the data buffer of the net_buf */
  42. uint8_t *pos;
  43. };
  44. /**
  45. * @brief Network packet.
  46. *
  47. * Note that if you add new fields into net_pkt, remember to update
  48. * net_pkt_clone() function.
  49. */
  50. struct net_pkt {
  51. /**
  52. * The fifo is used by RX/TX threads and by socket layer. The net_pkt
  53. * is queued via fifo to the processing thread.
  54. */
  55. intptr_t fifo;
  56. /** Slab pointer from where it belongs to */
  57. struct k_mem_slab *slab;
  58. /** buffer holding the packet */
  59. union {
  60. struct net_buf *frags;
  61. struct net_buf *buffer;
  62. };
  63. /** Internal buffer iterator used for reading/writing */
  64. struct net_pkt_cursor cursor;
  65. /** Network connection context */
  66. struct net_context *context;
  67. /** Network interface */
  68. struct net_if *iface;
  69. /** @cond ignore */
  70. #if defined(CONFIG_NET_ROUTING)
  71. struct net_if *orig_iface; /* Original network interface */
  72. #endif
  73. #if defined(CONFIG_NET_PKT_TIMESTAMP)
  74. /** Timestamp if available. */
  75. struct net_ptp_time timestamp;
  76. #endif
  77. #if defined(CONFIG_NET_PKT_RXTIME_STATS) || defined(CONFIG_NET_PKT_TXTIME_STATS)
  78. struct {
  79. /** Create time in cycles */
  80. uint32_t create_time;
  81. #if defined(CONFIG_NET_PKT_TXTIME_STATS_DETAIL) || \
  82. defined(CONFIG_NET_PKT_RXTIME_STATS_DETAIL)
  83. /** Collect extra statistics for net_pkt processing
  84. * from various points in the IP stack. See networking
  85. * documentation where these points are located and how
  86. * to interpret the results.
  87. */
  88. struct {
  89. uint32_t stat[NET_PKT_DETAIL_STATS_COUNT];
  90. int count;
  91. } detail;
  92. #endif /* CONFIG_NET_PKT_TXTIME_STATS_DETAIL ||
  93. CONFIG_NET_PKT_RXTIME_STATS_DETAIL */
  94. };
  95. #endif /* CONFIG_NET_PKT_RXTIME_STATS || CONFIG_NET_PKT_TXTIME_STATS */
  96. #if defined(CONFIG_NET_PKT_TXTIME)
  97. /** Network packet TX time in the future (in nanoseconds) */
  98. uint64_t txtime;
  99. #endif /* CONFIG_NET_PKT_TXTIME */
  100. /** Reference counter */
  101. atomic_t atomic_ref;
  102. /* Filled by layer 2 when network packet is received. */
  103. struct net_linkaddr lladdr_src;
  104. struct net_linkaddr lladdr_dst;
  105. #if defined(CONFIG_NET_TCP2)
  106. /** Allow placing the packet into sys_slist_t */
  107. sys_snode_t next;
  108. #endif
  109. uint8_t ip_hdr_len; /* pre-filled in order to avoid func call */
  110. uint8_t overwrite : 1; /* Is packet content being overwritten? */
  111. uint8_t sent_or_eof: 1; /* For outgoing packet: is this sent or not
  112. * For incoming packet of a socket: last
  113. * packet before EOF
  114. * Used only if defined(CONFIG_NET_TCP)
  115. */
  116. union {
  117. uint8_t pkt_queued: 1; /* For outgoing packet: is this packet
  118. * queued to be sent but has not reached
  119. * the driver yet.
  120. * Used only if defined(CONFIG_NET_TCP)
  121. */
  122. uint8_t gptp_pkt: 1; /* For outgoing packet: is this packet
  123. * a GPTP packet.
  124. * Used only if defined (CONFIG_NET_GPTP)
  125. */
  126. };
  127. uint8_t forwarding : 1; /* Are we forwarding this pkt
  128. * Used only if defined(CONFIG_NET_ROUTE)
  129. */
  130. uint8_t family : 3; /* IPv4 vs IPv6 */
  131. union {
  132. uint8_t ipv4_auto_arp_msg : 1; /* Is this pkt IPv4 autoconf ARP
  133. * message. Used only if
  134. * defined(CONFIG_NET_IPV4_AUTO).
  135. * Note: family needs to be
  136. * AF_INET.
  137. */
  138. uint8_t lldp_pkt : 1; /* Is this pkt an LLDP message.
  139. * Used only if
  140. * defined(CONFIG_NET_LLDP).
  141. * Note: family needs to be
  142. * AF_UNSPEC.
  143. */
  144. uint8_t ppp_msg : 1; /* This is a PPP message */
  145. };
  146. #if defined(CONFIG_NET_TCP)
  147. uint8_t tcp_first_msg : 1; /* Is this the first time this pkt is
  148. * sent, or is this a resend of a TCP
  149. * segment.
  150. */
  151. #endif
  152. uint8_t captured : 1; /* Set to 1 if this packet is already being
  153. * captured
  154. */
  155. uint8_t l2_bridged : 1; /* set to 1 if this packet comes from a bridge
  156. * and already contains its L2 header to be
  157. * preserved. Useful only if
  158. * defined(CONFIG_NET_ETHERNET_BRIDGE).
  159. */
  160. union {
  161. /* IPv6 hop limit or IPv4 ttl for this network packet.
  162. * The value is shared between IPv6 and IPv4.
  163. */
  164. uint8_t ipv6_hop_limit;
  165. uint8_t ipv4_ttl;
  166. };
  167. union {
  168. #if defined(CONFIG_NET_IPV4)
  169. uint8_t ipv4_opts_len; /* Length if IPv4 Header Options */
  170. #endif
  171. #if defined(CONFIG_NET_IPV6)
  172. uint16_t ipv6_ext_len; /* length of extension headers */
  173. #endif
  174. };
  175. /** Network packet priority, can be left out in which case packet
  176. * is not prioritised.
  177. */
  178. uint8_t priority;
  179. #if defined(CONFIG_NET_VLAN)
  180. /* VLAN TCI (Tag Control Information). This contains the Priority
  181. * Code Point (PCP), Drop Eligible Indicator (DEI) and VLAN
  182. * Identifier (VID, called more commonly VLAN tag). This value is
  183. * kept in host byte order.
  184. */
  185. uint16_t vlan_tci;
  186. #endif /* CONFIG_NET_VLAN */
  187. #if defined(CONFIG_NET_IPV6)
  188. /* Where is the start of the last header before payload data
  189. * in IPv6 packet. This is offset value from start of the IPv6
  190. * packet. Note that this value should be updated by who ever
  191. * adds IPv6 extension headers to the network packet.
  192. */
  193. uint16_t ipv6_prev_hdr_start;
  194. #if defined(CONFIG_NET_IPV6_FRAGMENT)
  195. uint16_t ipv6_fragment_flags; /* Fragment offset and M (More Fragment) flag */
  196. uint32_t ipv6_fragment_id; /* Fragment id */
  197. uint16_t ipv6_frag_hdr_start; /* Where starts the fragment header */
  198. #endif /* CONFIG_NET_IPV6_FRAGMENT */
  199. uint8_t ipv6_ext_opt_len; /* IPv6 ND option length */
  200. uint8_t ipv6_next_hdr; /* What is the very first next header */
  201. #endif /* CONFIG_NET_IPV6 */
  202. #if defined(CONFIG_IEEE802154)
  203. uint8_t ieee802154_rssi; /* Received Signal Strength Indication */
  204. uint8_t ieee802154_lqi; /* Link Quality Indicator */
  205. uint8_t ieee802154_arb : 1; /* ACK Request Bit is set in the frame */
  206. uint8_t ieee802154_ack_fpb : 1; /* Frame Pending Bit was set in the ACK */
  207. uint8_t ieee802154_frame_secured : 1; /* Frame is authenticated and
  208. * encrypted according to its
  209. * Auxiliary Security Header
  210. */
  211. uint8_t ieee802154_mac_hdr_rdy : 1; /* Indicates if frame's MAC header
  212. * is ready to be transmitted or if
  213. * it requires further modifications,
  214. * e.g. Frame Counter injection.
  215. */
  216. #if defined(CONFIG_IEEE802154_2015)
  217. uint8_t ieee802154_fv2015 : 1; /* Frame version is IEEE 802.15.4-2015 */
  218. uint8_t ieee802154_ack_seb : 1; /* Security Enabled Bit was set in the ACK */
  219. uint32_t ieee802154_ack_fc; /* Frame counter set in the ACK */
  220. uint8_t ieee802154_ack_keyid; /* Key index set in the ACK */
  221. #endif
  222. #endif
  223. #if defined(CONFIG_NET_L2_CANBUS)
  224. union {
  225. struct canbus_isotp_tx_ctx *canbus_tx_ctx;
  226. struct canbus_isotp_rx_ctx *canbus_rx_ctx;
  227. };
  228. #endif
  229. /* @endcond */
  230. };
  231. /** @cond ignore */
  232. /* The interface real ll address */
  233. static inline struct net_linkaddr *net_pkt_lladdr_if(struct net_pkt *pkt)
  234. {
  235. return net_if_get_link_addr(pkt->iface);
  236. }
  237. static inline struct net_context *net_pkt_context(struct net_pkt *pkt)
  238. {
  239. return pkt->context;
  240. }
  241. static inline void net_pkt_set_context(struct net_pkt *pkt,
  242. struct net_context *ctx)
  243. {
  244. pkt->context = ctx;
  245. }
  246. static inline struct net_if *net_pkt_iface(struct net_pkt *pkt)
  247. {
  248. return pkt->iface;
  249. }
  250. static inline void net_pkt_set_iface(struct net_pkt *pkt, struct net_if *iface)
  251. {
  252. pkt->iface = iface;
  253. /* If the network interface is set in pkt, then also set the type of
  254. * the network address that is stored in pkt. This is done here so
  255. * that the address type is properly set and is not forgotten.
  256. */
  257. if (iface) {
  258. pkt->lladdr_src.type = net_if_get_link_addr(iface)->type;
  259. pkt->lladdr_dst.type = net_if_get_link_addr(iface)->type;
  260. }
  261. }
  262. static inline struct net_if *net_pkt_orig_iface(struct net_pkt *pkt)
  263. {
  264. #if defined(CONFIG_NET_ROUTING)
  265. return pkt->orig_iface;
  266. #else
  267. return pkt->iface;
  268. #endif
  269. }
  270. static inline void net_pkt_set_orig_iface(struct net_pkt *pkt,
  271. struct net_if *iface)
  272. {
  273. #if defined(CONFIG_NET_ROUTING)
  274. pkt->orig_iface = iface;
  275. #endif
  276. }
  277. static inline uint8_t net_pkt_family(struct net_pkt *pkt)
  278. {
  279. return pkt->family;
  280. }
  281. static inline void net_pkt_set_family(struct net_pkt *pkt, uint8_t family)
  282. {
  283. pkt->family = family;
  284. }
  285. static inline bool net_pkt_is_gptp(struct net_pkt *pkt)
  286. {
  287. return !!(pkt->gptp_pkt);
  288. }
  289. static inline void net_pkt_set_gptp(struct net_pkt *pkt, bool is_gptp)
  290. {
  291. pkt->gptp_pkt = is_gptp;
  292. }
  293. static inline bool net_pkt_is_captured(struct net_pkt *pkt)
  294. {
  295. return !!(pkt->captured);
  296. }
  297. static inline void net_pkt_set_captured(struct net_pkt *pkt, bool is_captured)
  298. {
  299. pkt->captured = is_captured;
  300. }
  301. static inline bool net_pkt_is_l2_bridged(struct net_pkt *pkt)
  302. {
  303. return IS_ENABLED(CONFIG_NET_ETHERNET_BRIDGE) ? !!(pkt->l2_bridged) : 0;
  304. }
  305. static inline void net_pkt_set_l2_bridged(struct net_pkt *pkt, bool is_l2_bridged)
  306. {
  307. if (IS_ENABLED(CONFIG_NET_ETHERNET_BRIDGE)) {
  308. pkt->l2_bridged = is_l2_bridged;
  309. }
  310. }
  311. static inline uint8_t net_pkt_ip_hdr_len(struct net_pkt *pkt)
  312. {
  313. return pkt->ip_hdr_len;
  314. }
  315. static inline void net_pkt_set_ip_hdr_len(struct net_pkt *pkt, uint8_t len)
  316. {
  317. pkt->ip_hdr_len = len;
  318. }
  319. static inline uint8_t net_pkt_sent(struct net_pkt *pkt)
  320. {
  321. return pkt->sent_or_eof;
  322. }
  323. static inline void net_pkt_set_sent(struct net_pkt *pkt, bool sent)
  324. {
  325. pkt->sent_or_eof = sent;
  326. }
  327. static inline uint8_t net_pkt_queued(struct net_pkt *pkt)
  328. {
  329. return pkt->pkt_queued;
  330. }
  331. static inline void net_pkt_set_queued(struct net_pkt *pkt, bool send)
  332. {
  333. pkt->pkt_queued = send;
  334. }
  335. static inline uint8_t net_pkt_tcp_1st_msg(struct net_pkt *pkt)
  336. {
  337. #if defined(CONFIG_NET_TCP)
  338. return pkt->tcp_first_msg;
  339. #else
  340. return true;
  341. #endif
  342. }
  343. static inline void net_pkt_set_tcp_1st_msg(struct net_pkt *pkt, bool is_1st)
  344. {
  345. #if defined(CONFIG_NET_TCP)
  346. pkt->tcp_first_msg = is_1st;
  347. #else
  348. ARG_UNUSED(pkt);
  349. ARG_UNUSED(is_1st);
  350. #endif
  351. }
  352. #if defined(CONFIG_NET_SOCKETS)
  353. static inline uint8_t net_pkt_eof(struct net_pkt *pkt)
  354. {
  355. return pkt->sent_or_eof;
  356. }
  357. static inline void net_pkt_set_eof(struct net_pkt *pkt, bool eof)
  358. {
  359. pkt->sent_or_eof = eof;
  360. }
  361. #endif
  362. #if defined(CONFIG_NET_ROUTE)
  363. static inline bool net_pkt_forwarding(struct net_pkt *pkt)
  364. {
  365. return pkt->forwarding;
  366. }
  367. static inline void net_pkt_set_forwarding(struct net_pkt *pkt, bool forward)
  368. {
  369. pkt->forwarding = forward;
  370. }
  371. #else
  372. static inline bool net_pkt_forwarding(struct net_pkt *pkt)
  373. {
  374. return false;
  375. }
  376. #endif
  377. #if defined(CONFIG_NET_IPV4)
  378. static inline uint8_t net_pkt_ipv4_ttl(struct net_pkt *pkt)
  379. {
  380. return pkt->ipv4_ttl;
  381. }
  382. static inline void net_pkt_set_ipv4_ttl(struct net_pkt *pkt,
  383. uint8_t ttl)
  384. {
  385. pkt->ipv4_ttl = ttl;
  386. }
  387. static inline uint8_t net_pkt_ipv4_opts_len(struct net_pkt *pkt)
  388. {
  389. return pkt->ipv4_opts_len;
  390. }
  391. static inline void net_pkt_set_ipv4_opts_len(struct net_pkt *pkt,
  392. uint8_t opts_len)
  393. {
  394. pkt->ipv4_opts_len = opts_len;
  395. }
  396. #else
  397. static inline uint8_t net_pkt_ipv4_ttl(struct net_pkt *pkt)
  398. {
  399. ARG_UNUSED(pkt);
  400. return 0;
  401. }
  402. static inline void net_pkt_set_ipv4_ttl(struct net_pkt *pkt,
  403. uint8_t ttl)
  404. {
  405. ARG_UNUSED(pkt);
  406. ARG_UNUSED(ttl);
  407. }
  408. static inline uint8_t net_pkt_ipv4_opts_len(struct net_pkt *pkt)
  409. {
  410. ARG_UNUSED(pkt);
  411. return 0;
  412. }
  413. static inline void net_pkt_set_ipv4_opts_len(struct net_pkt *pkt,
  414. uint8_t opts_len)
  415. {
  416. ARG_UNUSED(pkt);
  417. ARG_UNUSED(opts_len);
  418. }
  419. #endif
  420. #if defined(CONFIG_NET_IPV6)
  421. static inline uint8_t net_pkt_ipv6_ext_opt_len(struct net_pkt *pkt)
  422. {
  423. return pkt->ipv6_ext_opt_len;
  424. }
  425. static inline void net_pkt_set_ipv6_ext_opt_len(struct net_pkt *pkt,
  426. uint8_t len)
  427. {
  428. pkt->ipv6_ext_opt_len = len;
  429. }
  430. static inline uint8_t net_pkt_ipv6_next_hdr(struct net_pkt *pkt)
  431. {
  432. return pkt->ipv6_next_hdr;
  433. }
  434. static inline void net_pkt_set_ipv6_next_hdr(struct net_pkt *pkt,
  435. uint8_t next_hdr)
  436. {
  437. pkt->ipv6_next_hdr = next_hdr;
  438. }
  439. static inline uint16_t net_pkt_ipv6_ext_len(struct net_pkt *pkt)
  440. {
  441. return pkt->ipv6_ext_len;
  442. }
  443. static inline void net_pkt_set_ipv6_ext_len(struct net_pkt *pkt, uint16_t len)
  444. {
  445. pkt->ipv6_ext_len = len;
  446. }
  447. static inline uint16_t net_pkt_ipv6_hdr_prev(struct net_pkt *pkt)
  448. {
  449. return pkt->ipv6_prev_hdr_start;
  450. }
  451. static inline void net_pkt_set_ipv6_hdr_prev(struct net_pkt *pkt,
  452. uint16_t offset)
  453. {
  454. pkt->ipv6_prev_hdr_start = offset;
  455. }
  456. static inline uint8_t net_pkt_ipv6_hop_limit(struct net_pkt *pkt)
  457. {
  458. return pkt->ipv6_hop_limit;
  459. }
  460. static inline void net_pkt_set_ipv6_hop_limit(struct net_pkt *pkt,
  461. uint8_t hop_limit)
  462. {
  463. pkt->ipv6_hop_limit = hop_limit;
  464. }
  465. #else /* CONFIG_NET_IPV6 */
  466. static inline uint8_t net_pkt_ipv6_ext_opt_len(struct net_pkt *pkt)
  467. {
  468. ARG_UNUSED(pkt);
  469. return 0;
  470. }
  471. static inline void net_pkt_set_ipv6_ext_opt_len(struct net_pkt *pkt,
  472. uint8_t len)
  473. {
  474. ARG_UNUSED(pkt);
  475. ARG_UNUSED(len);
  476. }
  477. static inline uint8_t net_pkt_ipv6_next_hdr(struct net_pkt *pkt)
  478. {
  479. ARG_UNUSED(pkt);
  480. return 0;
  481. }
  482. static inline void net_pkt_set_ipv6_next_hdr(struct net_pkt *pkt,
  483. uint8_t next_hdr)
  484. {
  485. ARG_UNUSED(pkt);
  486. ARG_UNUSED(next_hdr);
  487. }
  488. static inline uint16_t net_pkt_ipv6_ext_len(struct net_pkt *pkt)
  489. {
  490. ARG_UNUSED(pkt);
  491. return 0;
  492. }
  493. static inline void net_pkt_set_ipv6_ext_len(struct net_pkt *pkt, uint16_t len)
  494. {
  495. ARG_UNUSED(pkt);
  496. ARG_UNUSED(len);
  497. }
  498. static inline uint16_t net_pkt_ipv6_hdr_prev(struct net_pkt *pkt)
  499. {
  500. ARG_UNUSED(pkt);
  501. return 0;
  502. }
  503. static inline void net_pkt_set_ipv6_hdr_prev(struct net_pkt *pkt,
  504. uint16_t offset)
  505. {
  506. ARG_UNUSED(pkt);
  507. ARG_UNUSED(offset);
  508. }
  509. static inline uint8_t net_pkt_ipv6_hop_limit(struct net_pkt *pkt)
  510. {
  511. ARG_UNUSED(pkt);
  512. return 0;
  513. }
  514. static inline void net_pkt_set_ipv6_hop_limit(struct net_pkt *pkt,
  515. uint8_t hop_limit)
  516. {
  517. ARG_UNUSED(pkt);
  518. ARG_UNUSED(hop_limit);
  519. }
  520. #endif /* CONFIG_NET_IPV6 */
  521. static inline uint16_t net_pkt_ip_opts_len(struct net_pkt *pkt)
  522. {
  523. #if defined(CONFIG_NET_IPV6)
  524. return pkt->ipv6_ext_len;
  525. #elif defined(CONFIG_NET_IPV4)
  526. return pkt->ipv4_opts_len;
  527. #else
  528. ARG_UNUSED(pkt);
  529. return 0;
  530. #endif
  531. }
  532. #if defined(CONFIG_NET_IPV6_FRAGMENT)
  533. static inline uint16_t net_pkt_ipv6_fragment_start(struct net_pkt *pkt)
  534. {
  535. return pkt->ipv6_frag_hdr_start;
  536. }
  537. static inline void net_pkt_set_ipv6_fragment_start(struct net_pkt *pkt,
  538. uint16_t start)
  539. {
  540. pkt->ipv6_frag_hdr_start = start;
  541. }
  542. static inline uint16_t net_pkt_ipv6_fragment_offset(struct net_pkt *pkt)
  543. {
  544. return pkt->ipv6_fragment_flags & NET_IPV6_FRAGH_OFFSET_MASK;
  545. }
  546. static inline bool net_pkt_ipv6_fragment_more(struct net_pkt *pkt)
  547. {
  548. return (pkt->ipv6_fragment_flags & 0x01) != 0;
  549. }
  550. static inline void net_pkt_set_ipv6_fragment_flags(struct net_pkt *pkt,
  551. uint16_t flags)
  552. {
  553. pkt->ipv6_fragment_flags = flags;
  554. }
  555. static inline uint32_t net_pkt_ipv6_fragment_id(struct net_pkt *pkt)
  556. {
  557. return pkt->ipv6_fragment_id;
  558. }
  559. static inline void net_pkt_set_ipv6_fragment_id(struct net_pkt *pkt,
  560. uint32_t id)
  561. {
  562. pkt->ipv6_fragment_id = id;
  563. }
  564. #else /* CONFIG_NET_IPV6_FRAGMENT */
  565. static inline uint16_t net_pkt_ipv6_fragment_start(struct net_pkt *pkt)
  566. {
  567. ARG_UNUSED(pkt);
  568. return 0;
  569. }
  570. static inline void net_pkt_set_ipv6_fragment_start(struct net_pkt *pkt,
  571. uint16_t start)
  572. {
  573. ARG_UNUSED(pkt);
  574. ARG_UNUSED(start);
  575. }
  576. static inline uint16_t net_pkt_ipv6_fragment_offset(struct net_pkt *pkt)
  577. {
  578. ARG_UNUSED(pkt);
  579. return 0;
  580. }
  581. static inline bool net_pkt_ipv6_fragment_more(struct net_pkt *pkt)
  582. {
  583. ARG_UNUSED(pkt);
  584. return 0;
  585. }
  586. static inline void net_pkt_set_ipv6_fragment_flags(struct net_pkt *pkt,
  587. uint16_t flags)
  588. {
  589. ARG_UNUSED(pkt);
  590. ARG_UNUSED(flags);
  591. }
  592. static inline uint32_t net_pkt_ipv6_fragment_id(struct net_pkt *pkt)
  593. {
  594. ARG_UNUSED(pkt);
  595. return 0;
  596. }
  597. static inline void net_pkt_set_ipv6_fragment_id(struct net_pkt *pkt,
  598. uint32_t id)
  599. {
  600. ARG_UNUSED(pkt);
  601. ARG_UNUSED(id);
  602. }
  603. #endif /* CONFIG_NET_IPV6_FRAGMENT */
  604. static inline uint8_t net_pkt_priority(struct net_pkt *pkt)
  605. {
  606. return pkt->priority;
  607. }
  608. static inline void net_pkt_set_priority(struct net_pkt *pkt,
  609. uint8_t priority)
  610. {
  611. pkt->priority = priority;
  612. }
  613. #if defined(CONFIG_NET_VLAN)
  614. static inline uint16_t net_pkt_vlan_tag(struct net_pkt *pkt)
  615. {
  616. return net_eth_vlan_get_vid(pkt->vlan_tci);
  617. }
  618. static inline void net_pkt_set_vlan_tag(struct net_pkt *pkt, uint16_t tag)
  619. {
  620. pkt->vlan_tci = net_eth_vlan_set_vid(pkt->vlan_tci, tag);
  621. }
  622. static inline uint8_t net_pkt_vlan_priority(struct net_pkt *pkt)
  623. {
  624. return net_eth_vlan_get_pcp(pkt->vlan_tci);
  625. }
  626. static inline void net_pkt_set_vlan_priority(struct net_pkt *pkt,
  627. uint8_t priority)
  628. {
  629. pkt->vlan_tci = net_eth_vlan_set_pcp(pkt->vlan_tci, priority);
  630. }
  631. static inline bool net_pkt_vlan_dei(struct net_pkt *pkt)
  632. {
  633. return net_eth_vlan_get_dei(pkt->vlan_tci);
  634. }
  635. static inline void net_pkt_set_vlan_dei(struct net_pkt *pkt, bool dei)
  636. {
  637. pkt->vlan_tci = net_eth_vlan_set_dei(pkt->vlan_tci, dei);
  638. }
  639. static inline void net_pkt_set_vlan_tci(struct net_pkt *pkt, uint16_t tci)
  640. {
  641. pkt->vlan_tci = tci;
  642. }
  643. static inline uint16_t net_pkt_vlan_tci(struct net_pkt *pkt)
  644. {
  645. return pkt->vlan_tci;
  646. }
  647. #else
  648. static inline uint16_t net_pkt_vlan_tag(struct net_pkt *pkt)
  649. {
  650. return NET_VLAN_TAG_UNSPEC;
  651. }
  652. static inline void net_pkt_set_vlan_tag(struct net_pkt *pkt, uint16_t tag)
  653. {
  654. ARG_UNUSED(pkt);
  655. ARG_UNUSED(tag);
  656. }
  657. static inline uint8_t net_pkt_vlan_priority(struct net_pkt *pkt)
  658. {
  659. ARG_UNUSED(pkt);
  660. return 0;
  661. }
  662. static inline bool net_pkt_vlan_dei(struct net_pkt *pkt)
  663. {
  664. return false;
  665. }
  666. static inline void net_pkt_set_vlan_dei(struct net_pkt *pkt, bool dei)
  667. {
  668. ARG_UNUSED(pkt);
  669. ARG_UNUSED(dei);
  670. }
  671. static inline uint16_t net_pkt_vlan_tci(struct net_pkt *pkt)
  672. {
  673. return NET_VLAN_TAG_UNSPEC; /* assumes priority is 0 */
  674. }
  675. static inline void net_pkt_set_vlan_tci(struct net_pkt *pkt, uint16_t tci)
  676. {
  677. ARG_UNUSED(pkt);
  678. ARG_UNUSED(tci);
  679. }
  680. #endif
  681. #if defined(CONFIG_NET_PKT_TIMESTAMP)
  682. static inline struct net_ptp_time *net_pkt_timestamp(struct net_pkt *pkt)
  683. {
  684. return &pkt->timestamp;
  685. }
  686. static inline void net_pkt_set_timestamp(struct net_pkt *pkt,
  687. struct net_ptp_time *timestamp)
  688. {
  689. pkt->timestamp.second = timestamp->second;
  690. pkt->timestamp.nanosecond = timestamp->nanosecond;
  691. }
  692. #else
  693. static inline struct net_ptp_time *net_pkt_timestamp(struct net_pkt *pkt)
  694. {
  695. ARG_UNUSED(pkt);
  696. return NULL;
  697. }
  698. static inline void net_pkt_set_timestamp(struct net_pkt *pkt,
  699. struct net_ptp_time *timestamp)
  700. {
  701. ARG_UNUSED(pkt);
  702. ARG_UNUSED(timestamp);
  703. }
  704. #endif /* CONFIG_NET_PKT_TIMESTAMP */
  705. #if defined(CONFIG_NET_PKT_RXTIME_STATS) || defined(CONFIG_NET_PKT_TXTIME_STATS)
  706. static inline uint32_t net_pkt_create_time(struct net_pkt *pkt)
  707. {
  708. return pkt->create_time;
  709. }
  710. static inline void net_pkt_set_create_time(struct net_pkt *pkt,
  711. uint32_t create_time)
  712. {
  713. pkt->create_time = create_time;
  714. }
  715. #else
  716. static inline uint32_t net_pkt_create_time(struct net_pkt *pkt)
  717. {
  718. ARG_UNUSED(pkt);
  719. return 0U;
  720. }
  721. static inline void net_pkt_set_create_time(struct net_pkt *pkt,
  722. uint32_t create_time)
  723. {
  724. ARG_UNUSED(pkt);
  725. ARG_UNUSED(create_time);
  726. }
  727. #endif /* CONFIG_NET_PKT_RXTIME_STATS || CONFIG_NET_PKT_TXTIME_STATS */
  728. #if defined(CONFIG_NET_PKT_TXTIME)
  729. static inline uint64_t net_pkt_txtime(struct net_pkt *pkt)
  730. {
  731. return pkt->txtime;
  732. }
  733. static inline void net_pkt_set_txtime(struct net_pkt *pkt, uint64_t txtime)
  734. {
  735. pkt->txtime = txtime;
  736. }
  737. #else
  738. static inline uint64_t net_pkt_txtime(struct net_pkt *pkt)
  739. {
  740. ARG_UNUSED(pkt);
  741. return 0;
  742. }
  743. static inline void net_pkt_set_txtime(struct net_pkt *pkt, uint64_t txtime)
  744. {
  745. ARG_UNUSED(pkt);
  746. ARG_UNUSED(txtime);
  747. }
  748. #endif /* CONFIG_NET_PKT_TXTIME */
  749. #if defined(CONFIG_NET_PKT_TXTIME_STATS_DETAIL) || \
  750. defined(CONFIG_NET_PKT_RXTIME_STATS_DETAIL)
  751. static inline uint32_t *net_pkt_stats_tick(struct net_pkt *pkt)
  752. {
  753. return pkt->detail.stat;
  754. }
  755. static inline int net_pkt_stats_tick_count(struct net_pkt *pkt)
  756. {
  757. return pkt->detail.count;
  758. }
  759. static inline void net_pkt_stats_tick_reset(struct net_pkt *pkt)
  760. {
  761. memset(&pkt->detail, 0, sizeof(pkt->detail));
  762. }
  763. static ALWAYS_INLINE void net_pkt_set_stats_tick(struct net_pkt *pkt,
  764. uint32_t tick)
  765. {
  766. if (pkt->detail.count >= NET_PKT_DETAIL_STATS_COUNT) {
  767. NET_ERR("Detail stats count overflow (%d >= %d)",
  768. pkt->detail.count, NET_PKT_DETAIL_STATS_COUNT);
  769. return;
  770. }
  771. pkt->detail.stat[pkt->detail.count++] = tick;
  772. }
  773. #define net_pkt_set_tx_stats_tick(pkt, tick) net_pkt_set_stats_tick(pkt, tick)
  774. #define net_pkt_set_rx_stats_tick(pkt, tick) net_pkt_set_stats_tick(pkt, tick)
  775. #else
  776. static inline uint32_t *net_pkt_stats_tick(struct net_pkt *pkt)
  777. {
  778. ARG_UNUSED(pkt);
  779. return NULL;
  780. }
  781. static inline int net_pkt_stats_tick_count(struct net_pkt *pkt)
  782. {
  783. ARG_UNUSED(pkt);
  784. return 0;
  785. }
  786. static inline void net_pkt_stats_tick_reset(struct net_pkt *pkt)
  787. {
  788. ARG_UNUSED(pkt);
  789. }
  790. static inline void net_pkt_set_stats_tick(struct net_pkt *pkt, uint32_t tick)
  791. {
  792. ARG_UNUSED(pkt);
  793. ARG_UNUSED(tick);
  794. }
  795. #define net_pkt_set_tx_stats_tick(pkt, tick)
  796. #define net_pkt_set_rx_stats_tick(pkt, tick)
  797. #endif /* CONFIG_NET_PKT_TXTIME_STATS_DETAIL ||
  798. CONFIG_NET_PKT_RXTIME_STATS_DETAIL */
  799. static inline size_t net_pkt_get_len(struct net_pkt *pkt)
  800. {
  801. return net_buf_frags_len(pkt->frags);
  802. }
  803. static inline uint8_t *net_pkt_data(struct net_pkt *pkt)
  804. {
  805. return pkt->frags->data;
  806. }
  807. static inline uint8_t *net_pkt_ip_data(struct net_pkt *pkt)
  808. {
  809. return pkt->frags->data;
  810. }
  811. static inline bool net_pkt_is_empty(struct net_pkt *pkt)
  812. {
  813. return !pkt->buffer || !net_pkt_data(pkt) || pkt->buffer->len == 0;
  814. }
  815. static inline struct net_linkaddr *net_pkt_lladdr_src(struct net_pkt *pkt)
  816. {
  817. return &pkt->lladdr_src;
  818. }
  819. static inline struct net_linkaddr *net_pkt_lladdr_dst(struct net_pkt *pkt)
  820. {
  821. return &pkt->lladdr_dst;
  822. }
  823. static inline void net_pkt_lladdr_swap(struct net_pkt *pkt)
  824. {
  825. uint8_t *addr = net_pkt_lladdr_src(pkt)->addr;
  826. net_pkt_lladdr_src(pkt)->addr = net_pkt_lladdr_dst(pkt)->addr;
  827. net_pkt_lladdr_dst(pkt)->addr = addr;
  828. }
  829. static inline void net_pkt_lladdr_clear(struct net_pkt *pkt)
  830. {
  831. net_pkt_lladdr_src(pkt)->addr = NULL;
  832. net_pkt_lladdr_src(pkt)->len = 0U;
  833. }
  834. #if defined(CONFIG_IEEE802154) || defined(CONFIG_IEEE802154_RAW_MODE)
  835. static inline uint8_t net_pkt_ieee802154_rssi(struct net_pkt *pkt)
  836. {
  837. return pkt->ieee802154_rssi;
  838. }
  839. static inline void net_pkt_set_ieee802154_rssi(struct net_pkt *pkt,
  840. uint8_t rssi)
  841. {
  842. pkt->ieee802154_rssi = rssi;
  843. }
  844. static inline uint8_t net_pkt_ieee802154_lqi(struct net_pkt *pkt)
  845. {
  846. return pkt->ieee802154_lqi;
  847. }
  848. static inline void net_pkt_set_ieee802154_lqi(struct net_pkt *pkt,
  849. uint8_t lqi)
  850. {
  851. pkt->ieee802154_lqi = lqi;
  852. }
  853. static inline bool net_pkt_ieee802154_arb(struct net_pkt *pkt)
  854. {
  855. return pkt->ieee802154_arb;
  856. }
  857. static inline void net_pkt_set_ieee802154_arb(struct net_pkt *pkt, bool arb)
  858. {
  859. pkt->ieee802154_arb = arb;
  860. }
  861. static inline bool net_pkt_ieee802154_ack_fpb(struct net_pkt *pkt)
  862. {
  863. return pkt->ieee802154_ack_fpb;
  864. }
  865. static inline void net_pkt_set_ieee802154_ack_fpb(struct net_pkt *pkt,
  866. bool fpb)
  867. {
  868. pkt->ieee802154_ack_fpb = fpb;
  869. }
  870. static inline bool net_pkt_ieee802154_frame_secured(struct net_pkt *pkt)
  871. {
  872. return pkt->ieee802154_frame_secured;
  873. }
  874. static inline void net_pkt_set_ieee802154_frame_secured(struct net_pkt *pkt,
  875. bool secured)
  876. {
  877. pkt->ieee802154_frame_secured = secured;
  878. }
  879. static inline bool net_pkt_ieee802154_mac_hdr_rdy(struct net_pkt *pkt)
  880. {
  881. return pkt->ieee802154_mac_hdr_rdy;
  882. }
  883. static inline void net_pkt_set_ieee802154_mac_hdr_rdy(struct net_pkt *pkt,
  884. bool rdy)
  885. {
  886. pkt->ieee802154_mac_hdr_rdy = rdy;
  887. }
  888. #if defined(CONFIG_IEEE802154_2015)
  889. static inline bool net_pkt_ieee802154_fv2015(struct net_pkt *pkt)
  890. {
  891. return pkt->ieee802154_fv2015;
  892. }
  893. static inline void net_pkt_set_ieee802154_fv2015(struct net_pkt *pkt, bool fv2015)
  894. {
  895. pkt->ieee802154_fv2015 = fv2015;
  896. }
  897. static inline bool net_pkt_ieee802154_ack_seb(struct net_pkt *pkt)
  898. {
  899. return pkt->ieee802154_ack_seb;
  900. }
  901. static inline void net_pkt_set_ieee802154_ack_seb(struct net_pkt *pkt, bool seb)
  902. {
  903. pkt->ieee802154_ack_seb = seb;
  904. }
  905. static inline uint32_t net_pkt_ieee802154_ack_fc(struct net_pkt *pkt)
  906. {
  907. return pkt->ieee802154_ack_fc;
  908. }
  909. static inline void net_pkt_set_ieee802154_ack_fc(struct net_pkt *pkt,
  910. uint32_t fc)
  911. {
  912. pkt->ieee802154_ack_fc = fc;
  913. }
  914. static inline uint8_t net_pkt_ieee802154_ack_keyid(struct net_pkt *pkt)
  915. {
  916. return pkt->ieee802154_ack_keyid;
  917. }
  918. static inline void net_pkt_set_ieee802154_ack_keyid(struct net_pkt *pkt,
  919. uint8_t keyid)
  920. {
  921. pkt->ieee802154_ack_keyid = keyid;
  922. }
  923. #endif /* CONFIG_IEEE802154_2015 */
  924. #endif /* CONFIG_IEEE802154 || CONFIG_IEEE802154_RAW_MODE */
  925. #if defined(CONFIG_NET_IPV4_AUTO)
  926. static inline bool net_pkt_ipv4_auto(struct net_pkt *pkt)
  927. {
  928. return pkt->ipv4_auto_arp_msg;
  929. }
  930. static inline void net_pkt_set_ipv4_auto(struct net_pkt *pkt,
  931. bool is_auto_arp_msg)
  932. {
  933. pkt->ipv4_auto_arp_msg = is_auto_arp_msg;
  934. }
  935. #else /* CONFIG_NET_IPV4_AUTO */
  936. static inline bool net_pkt_ipv4_auto(struct net_pkt *pkt)
  937. {
  938. ARG_UNUSED(pkt);
  939. return false;
  940. }
  941. static inline void net_pkt_set_ipv4_auto(struct net_pkt *pkt,
  942. bool is_auto_arp_msg)
  943. {
  944. ARG_UNUSED(pkt);
  945. ARG_UNUSED(is_auto_arp_msg);
  946. }
  947. #endif /* CONFIG_NET_IPV4_AUTO */
  948. #if defined(CONFIG_NET_LLDP)
  949. static inline bool net_pkt_is_lldp(struct net_pkt *pkt)
  950. {
  951. return pkt->lldp_pkt;
  952. }
  953. static inline void net_pkt_set_lldp(struct net_pkt *pkt, bool is_lldp)
  954. {
  955. pkt->lldp_pkt = is_lldp;
  956. }
  957. #else
  958. static inline bool net_pkt_is_lldp(struct net_pkt *pkt)
  959. {
  960. ARG_UNUSED(pkt);
  961. return false;
  962. }
  963. static inline void net_pkt_set_lldp(struct net_pkt *pkt, bool is_lldp)
  964. {
  965. ARG_UNUSED(pkt);
  966. ARG_UNUSED(is_lldp);
  967. }
  968. #endif /* CONFIG_NET_LLDP */
  969. #if defined(CONFIG_NET_PPP)
  970. static inline bool net_pkt_is_ppp(struct net_pkt *pkt)
  971. {
  972. return pkt->ppp_msg;
  973. }
  974. static inline void net_pkt_set_ppp(struct net_pkt *pkt,
  975. bool is_ppp_msg)
  976. {
  977. pkt->ppp_msg = is_ppp_msg;
  978. }
  979. #else /* CONFIG_NET_PPP */
  980. static inline bool net_pkt_is_ppp(struct net_pkt *pkt)
  981. {
  982. ARG_UNUSED(pkt);
  983. return false;
  984. }
  985. static inline void net_pkt_set_ppp(struct net_pkt *pkt,
  986. bool is_ppp_msg)
  987. {
  988. ARG_UNUSED(pkt);
  989. ARG_UNUSED(is_ppp_msg);
  990. }
  991. #endif /* CONFIG_NET_PPP */
  992. #define NET_IPV6_HDR(pkt) ((struct net_ipv6_hdr *)net_pkt_ip_data(pkt))
  993. #define NET_IPV4_HDR(pkt) ((struct net_ipv4_hdr *)net_pkt_ip_data(pkt))
  994. static inline void net_pkt_set_src_ipv6_addr(struct net_pkt *pkt)
  995. {
  996. net_if_ipv6_select_src_addr(net_context_get_iface(
  997. net_pkt_context(pkt)),
  998. &NET_IPV6_HDR(pkt)->src);
  999. }
  1000. static inline void net_pkt_set_overwrite(struct net_pkt *pkt, bool overwrite)
  1001. {
  1002. pkt->overwrite = overwrite;
  1003. }
  1004. static inline bool net_pkt_is_being_overwritten(struct net_pkt *pkt)
  1005. {
  1006. return pkt->overwrite;
  1007. }
  1008. /* @endcond */
  1009. /**
  1010. * @brief Create a net_pkt slab
  1011. *
  1012. * A net_pkt slab is used to store meta-information about
  1013. * network packets. It must be coupled with a data fragment pool
  1014. * (:c:macro:`NET_PKT_DATA_POOL_DEFINE`) used to store the actual
  1015. * packet data. The macro can be used by an application to define
  1016. * additional custom per-context TX packet slabs (see
  1017. * :c:func:`net_context_setup_pools`).
  1018. *
  1019. * @param name Name of the slab.
  1020. * @param count Number of net_pkt in this slab.
  1021. */
  1022. #define NET_PKT_SLAB_DEFINE(name, count) \
  1023. K_MEM_SLAB_DEFINE(name, sizeof(struct net_pkt), count, 4)
  1024. /* Backward compatibility macro */
  1025. #define NET_PKT_TX_SLAB_DEFINE(name, count) NET_PKT_SLAB_DEFINE(name, count)
  1026. /**
  1027. * @brief Create a data fragment net_buf pool
  1028. *
  1029. * A net_buf pool is used to store actual data for
  1030. * network packets. It must be coupled with a net_pkt slab
  1031. * (:c:macro:`NET_PKT_SLAB_DEFINE`) used to store the packet
  1032. * meta-information. The macro can be used by an application to
  1033. * define additional custom per-context TX packet pools (see
  1034. * :c:func:`net_context_setup_pools`).
  1035. *
  1036. * @param name Name of the pool.
  1037. * @param count Number of net_buf in this pool.
  1038. */
  1039. #define NET_PKT_DATA_POOL_DEFINE(name, count) \
  1040. NET_BUF_POOL_DEFINE(name, count, CONFIG_NET_BUF_DATA_SIZE, \
  1041. CONFIG_NET_BUF_USER_DATA_SIZE, NULL)
  1042. /** @cond INTERNAL_HIDDEN */
  1043. #if defined(CONFIG_NET_DEBUG_NET_PKT_ALLOC) || \
  1044. (CONFIG_NET_PKT_LOG_LEVEL >= LOG_LEVEL_DBG)
  1045. #define NET_PKT_DEBUG_ENABLED
  1046. #endif
  1047. #if defined(NET_PKT_DEBUG_ENABLED)
  1048. /* Debug versions of the net_pkt functions that are used when tracking
  1049. * buffer usage.
  1050. */
  1051. struct net_buf *net_pkt_get_reserve_data_debug(struct net_buf_pool *pool,
  1052. k_timeout_t timeout,
  1053. const char *caller,
  1054. int line);
  1055. #define net_pkt_get_reserve_data(pool, timeout) \
  1056. net_pkt_get_reserve_data_debug(pool, timeout, __func__, __LINE__)
  1057. struct net_buf *net_pkt_get_reserve_rx_data_debug(k_timeout_t timeout,
  1058. const char *caller,
  1059. int line);
  1060. #define net_pkt_get_reserve_rx_data(timeout) \
  1061. net_pkt_get_reserve_rx_data_debug(timeout, __func__, __LINE__)
  1062. struct net_buf *net_pkt_get_reserve_tx_data_debug(k_timeout_t timeout,
  1063. const char *caller,
  1064. int line);
  1065. #define net_pkt_get_reserve_tx_data(timeout) \
  1066. net_pkt_get_reserve_tx_data_debug(timeout, __func__, __LINE__)
  1067. struct net_buf *net_pkt_get_frag_debug(struct net_pkt *pkt,
  1068. k_timeout_t timeout,
  1069. const char *caller, int line);
  1070. #define net_pkt_get_frag(pkt, timeout) \
  1071. net_pkt_get_frag_debug(pkt, timeout, __func__, __LINE__)
  1072. void net_pkt_unref_debug(struct net_pkt *pkt, const char *caller, int line);
  1073. #define net_pkt_unref(pkt) net_pkt_unref_debug(pkt, __func__, __LINE__)
  1074. struct net_pkt *net_pkt_ref_debug(struct net_pkt *pkt, const char *caller,
  1075. int line);
  1076. #define net_pkt_ref(pkt) net_pkt_ref_debug(pkt, __func__, __LINE__)
  1077. struct net_buf *net_pkt_frag_ref_debug(struct net_buf *frag,
  1078. const char *caller, int line);
  1079. #define net_pkt_frag_ref(frag) net_pkt_frag_ref_debug(frag, __func__, __LINE__)
  1080. void net_pkt_frag_unref_debug(struct net_buf *frag,
  1081. const char *caller, int line);
  1082. #define net_pkt_frag_unref(frag) \
  1083. net_pkt_frag_unref_debug(frag, __func__, __LINE__)
  1084. struct net_buf *net_pkt_frag_del_debug(struct net_pkt *pkt,
  1085. struct net_buf *parent,
  1086. struct net_buf *frag,
  1087. const char *caller, int line);
  1088. #define net_pkt_frag_del(pkt, parent, frag) \
  1089. net_pkt_frag_del_debug(pkt, parent, frag, __func__, __LINE__)
  1090. void net_pkt_frag_add_debug(struct net_pkt *pkt, struct net_buf *frag,
  1091. const char *caller, int line);
  1092. #define net_pkt_frag_add(pkt, frag) \
  1093. net_pkt_frag_add_debug(pkt, frag, __func__, __LINE__)
  1094. void net_pkt_frag_insert_debug(struct net_pkt *pkt, struct net_buf *frag,
  1095. const char *caller, int line);
  1096. #define net_pkt_frag_insert(pkt, frag) \
  1097. net_pkt_frag_insert_debug(pkt, frag, __func__, __LINE__)
  1098. #endif /* CONFIG_NET_DEBUG_NET_PKT_ALLOC ||
  1099. * CONFIG_NET_PKT_LOG_LEVEL >= LOG_LEVEL_DBG
  1100. */
  1101. /** @endcond */
  1102. /**
  1103. * @brief Print fragment list and the fragment sizes
  1104. *
  1105. * @details Only available if debugging is activated.
  1106. *
  1107. * @param pkt Network pkt.
  1108. */
  1109. #if defined(NET_PKT_DEBUG_ENABLED)
  1110. void net_pkt_print_frags(struct net_pkt *pkt);
  1111. #else
  1112. #define net_pkt_print_frags(pkt)
  1113. #endif
  1114. /**
  1115. * @brief Get RX DATA buffer from pool.
  1116. * Normally you should use net_pkt_get_frag() instead.
  1117. *
  1118. * @details Normally this version is not useful for applications
  1119. * but is mainly used by network fragmentation code.
  1120. *
  1121. * @param timeout Affects the action taken should the net buf pool be empty.
  1122. * If K_NO_WAIT, then return immediately. If K_FOREVER, then
  1123. * wait as long as necessary. Otherwise, wait up to the specified time.
  1124. *
  1125. * @return Network buffer if successful, NULL otherwise.
  1126. */
  1127. #if !defined(NET_PKT_DEBUG_ENABLED)
  1128. struct net_buf *net_pkt_get_reserve_rx_data(k_timeout_t timeout);
  1129. #endif
  1130. /**
  1131. * @brief Get TX DATA buffer from pool.
  1132. * Normally you should use net_pkt_get_frag() instead.
  1133. *
  1134. * @details Normally this version is not useful for applications
  1135. * but is mainly used by network fragmentation code.
  1136. *
  1137. * @param timeout Affects the action taken should the net buf pool be empty.
  1138. * If K_NO_WAIT, then return immediately. If K_FOREVER, then
  1139. * wait as long as necessary. Otherwise, wait up to the specified time.
  1140. *
  1141. * @return Network buffer if successful, NULL otherwise.
  1142. */
  1143. #if !defined(NET_PKT_DEBUG_ENABLED)
  1144. struct net_buf *net_pkt_get_reserve_tx_data(k_timeout_t timeout);
  1145. #endif
  1146. /**
  1147. * @brief Get a data fragment that might be from user specific
  1148. * buffer pool or from global DATA pool.
  1149. *
  1150. * @param pkt Network packet.
  1151. * @param timeout Affects the action taken should the net buf pool be empty.
  1152. * If K_NO_WAIT, then return immediately. If K_FOREVER, then
  1153. * wait as long as necessary. Otherwise, wait up to the specified time.
  1154. *
  1155. * @return Network buffer if successful, NULL otherwise.
  1156. */
  1157. #if !defined(NET_PKT_DEBUG_ENABLED)
  1158. struct net_buf *net_pkt_get_frag(struct net_pkt *pkt, k_timeout_t timeout);
  1159. #endif
  1160. /**
  1161. * @brief Place packet back into the available packets slab
  1162. *
  1163. * @details Releases the packet to other use. This needs to be
  1164. * called by application after it has finished with the packet.
  1165. *
  1166. * @param pkt Network packet to release.
  1167. *
  1168. */
  1169. #if !defined(NET_PKT_DEBUG_ENABLED)
  1170. void net_pkt_unref(struct net_pkt *pkt);
  1171. #endif
  1172. /**
  1173. * @brief Increase the packet ref count
  1174. *
  1175. * @details Mark the packet to be used still.
  1176. *
  1177. * @param pkt Network packet to ref.
  1178. *
  1179. * @return Network packet if successful, NULL otherwise.
  1180. */
  1181. #if !defined(NET_PKT_DEBUG_ENABLED)
  1182. struct net_pkt *net_pkt_ref(struct net_pkt *pkt);
  1183. #endif
  1184. /**
  1185. * @brief Increase the packet fragment ref count
  1186. *
  1187. * @details Mark the fragment to be used still.
  1188. *
  1189. * @param frag Network fragment to ref.
  1190. *
  1191. * @return a pointer on the referenced Network fragment.
  1192. */
  1193. #if !defined(NET_PKT_DEBUG_ENABLED)
  1194. struct net_buf *net_pkt_frag_ref(struct net_buf *frag);
  1195. #endif
  1196. /**
  1197. * @brief Decrease the packet fragment ref count
  1198. *
  1199. * @param frag Network fragment to unref.
  1200. */
  1201. #if !defined(NET_PKT_DEBUG_ENABLED)
  1202. void net_pkt_frag_unref(struct net_buf *frag);
  1203. #endif
  1204. /**
  1205. * @brief Delete existing fragment from a packet
  1206. *
  1207. * @param pkt Network packet from which frag belongs to.
  1208. * @param parent parent fragment of frag, or NULL if none.
  1209. * @param frag Fragment to delete.
  1210. *
  1211. * @return Pointer to the following fragment, or NULL if it had no
  1212. * further fragments.
  1213. */
  1214. #if !defined(NET_PKT_DEBUG_ENABLED)
  1215. struct net_buf *net_pkt_frag_del(struct net_pkt *pkt,
  1216. struct net_buf *parent,
  1217. struct net_buf *frag);
  1218. #endif
  1219. /**
  1220. * @brief Add a fragment to a packet at the end of its fragment list
  1221. *
  1222. * @param pkt pkt Network packet where to add the fragment
  1223. * @param frag Fragment to add
  1224. */
  1225. #if !defined(NET_PKT_DEBUG_ENABLED)
  1226. void net_pkt_frag_add(struct net_pkt *pkt, struct net_buf *frag);
  1227. #endif
  1228. /**
  1229. * @brief Insert a fragment to a packet at the beginning of its fragment list
  1230. *
  1231. * @param pkt pkt Network packet where to insert the fragment
  1232. * @param frag Fragment to insert
  1233. */
  1234. #if !defined(NET_PKT_DEBUG_ENABLED)
  1235. void net_pkt_frag_insert(struct net_pkt *pkt, struct net_buf *frag);
  1236. #endif
  1237. /**
  1238. * @brief Compact the fragment list of a packet.
  1239. *
  1240. * @details After this there is no more any free space in individual fragments.
  1241. * @param pkt Network packet.
  1242. *
  1243. * @return True if compact success, False otherwise.
  1244. */
  1245. bool net_pkt_compact(struct net_pkt *pkt);
  1246. /**
  1247. * @brief Get information about predefined RX, TX and DATA pools.
  1248. *
  1249. * @param rx Pointer to RX pool is returned.
  1250. * @param tx Pointer to TX pool is returned.
  1251. * @param rx_data Pointer to RX DATA pool is returned.
  1252. * @param tx_data Pointer to TX DATA pool is returned.
  1253. */
  1254. void net_pkt_get_info(struct k_mem_slab **rx,
  1255. struct k_mem_slab **tx,
  1256. struct net_buf_pool **rx_data,
  1257. struct net_buf_pool **tx_data);
  1258. /** @cond INTERNAL_HIDDEN */
  1259. #if defined(CONFIG_NET_DEBUG_NET_PKT_ALLOC)
  1260. /**
  1261. * @brief Debug helper to print out the buffer allocations
  1262. */
  1263. void net_pkt_print(void);
  1264. typedef void (*net_pkt_allocs_cb_t)(struct net_pkt *pkt,
  1265. struct net_buf *buf,
  1266. const char *func_alloc,
  1267. int line_alloc,
  1268. const char *func_free,
  1269. int line_free,
  1270. bool in_use,
  1271. void *user_data);
  1272. void net_pkt_allocs_foreach(net_pkt_allocs_cb_t cb, void *user_data);
  1273. const char *net_pkt_slab2str(struct k_mem_slab *slab);
  1274. const char *net_pkt_pool2str(struct net_buf_pool *pool);
  1275. #else
  1276. #define net_pkt_print(...)
  1277. #endif /* CONFIG_NET_DEBUG_NET_PKT_ALLOC */
  1278. /* New allocator, and API are defined below.
  1279. * This will be simpler when time will come to get rid of former API above.
  1280. */
  1281. #if defined(NET_PKT_DEBUG_ENABLED)
  1282. struct net_pkt *net_pkt_alloc_debug(k_timeout_t timeout,
  1283. const char *caller, int line);
  1284. #define net_pkt_alloc(_timeout) \
  1285. net_pkt_alloc_debug(_timeout, __func__, __LINE__)
  1286. struct net_pkt *net_pkt_alloc_from_slab_debug(struct k_mem_slab *slab,
  1287. k_timeout_t timeout,
  1288. const char *caller, int line);
  1289. #define net_pkt_alloc_from_slab(_slab, _timeout) \
  1290. net_pkt_alloc_from_slab_debug(_slab, _timeout, __func__, __LINE__)
  1291. struct net_pkt *net_pkt_rx_alloc_debug(k_timeout_t timeout,
  1292. const char *caller, int line);
  1293. #define net_pkt_rx_alloc(_timeout) \
  1294. net_pkt_rx_alloc_debug(_timeout, __func__, __LINE__)
  1295. struct net_pkt *net_pkt_alloc_on_iface_debug(struct net_if *iface,
  1296. k_timeout_t timeout,
  1297. const char *caller,
  1298. int line);
  1299. #define net_pkt_alloc_on_iface(_iface, _timeout) \
  1300. net_pkt_alloc_on_iface_debug(_iface, _timeout, __func__, __LINE__)
  1301. struct net_pkt *net_pkt_rx_alloc_on_iface_debug(struct net_if *iface,
  1302. k_timeout_t timeout,
  1303. const char *caller,
  1304. int line);
  1305. #define net_pkt_rx_alloc_on_iface(_iface, _timeout) \
  1306. net_pkt_rx_alloc_on_iface_debug(_iface, _timeout, \
  1307. __func__, __LINE__)
  1308. int net_pkt_alloc_buffer_debug(struct net_pkt *pkt,
  1309. size_t size,
  1310. enum net_ip_protocol proto,
  1311. k_timeout_t timeout,
  1312. const char *caller, int line);
  1313. #define net_pkt_alloc_buffer(_pkt, _size, _proto, _timeout) \
  1314. net_pkt_alloc_buffer_debug(_pkt, _size, _proto, _timeout, \
  1315. __func__, __LINE__)
  1316. struct net_pkt *net_pkt_alloc_with_buffer_debug(struct net_if *iface,
  1317. size_t size,
  1318. sa_family_t family,
  1319. enum net_ip_protocol proto,
  1320. k_timeout_t timeout,
  1321. const char *caller,
  1322. int line);
  1323. #define net_pkt_alloc_with_buffer(_iface, _size, _family, \
  1324. _proto, _timeout) \
  1325. net_pkt_alloc_with_buffer_debug(_iface, _size, _family, \
  1326. _proto, _timeout, \
  1327. __func__, __LINE__)
  1328. struct net_pkt *net_pkt_rx_alloc_with_buffer_debug(struct net_if *iface,
  1329. size_t size,
  1330. sa_family_t family,
  1331. enum net_ip_protocol proto,
  1332. k_timeout_t timeout,
  1333. const char *caller,
  1334. int line);
  1335. #define net_pkt_rx_alloc_with_buffer(_iface, _size, _family, \
  1336. _proto, _timeout) \
  1337. net_pkt_rx_alloc_with_buffer_debug(_iface, _size, _family, \
  1338. _proto, _timeout, \
  1339. __func__, __LINE__)
  1340. #endif /* NET_PKT_DEBUG_ENABLED */
  1341. /** @endcond */
  1342. /**
  1343. * @brief Allocate an initialized net_pkt
  1344. *
  1345. * @details for the time being, 2 pools are used. One for TX and one for RX.
  1346. * This allocator has to be used for TX.
  1347. *
  1348. * @param timeout Maximum time to wait for an allocation.
  1349. *
  1350. * @return a pointer to a newly allocated net_pkt on success, NULL otherwise.
  1351. */
  1352. #if !defined(NET_PKT_DEBUG_ENABLED)
  1353. struct net_pkt *net_pkt_alloc(k_timeout_t timeout);
  1354. #endif
  1355. /**
  1356. * @brief Allocate an initialized net_pkt from a specific slab
  1357. *
  1358. * @details unlike net_pkt_alloc() which uses core slabs, this one will use
  1359. * an external slab (see NET_PKT_SLAB_DEFINE()).
  1360. * Do _not_ use it unless you know what you are doing. Basically, only
  1361. * net_context should be using this, in order to allocate packet and
  1362. * then buffer on its local slab/pool (if any).
  1363. *
  1364. * @param slab The slab to use for allocating the packet
  1365. * @param timeout Maximum time to wait for an allocation.
  1366. *
  1367. * @return a pointer to a newly allocated net_pkt on success, NULL otherwise.
  1368. */
  1369. #if !defined(NET_PKT_DEBUG_ENABLED)
  1370. struct net_pkt *net_pkt_alloc_from_slab(struct k_mem_slab *slab,
  1371. k_timeout_t timeout);
  1372. #endif
  1373. /**
  1374. * @brief Allocate an initialized net_pkt for RX
  1375. *
  1376. * @details for the time being, 2 pools are used. One for TX and one for RX.
  1377. * This allocator has to be used for RX.
  1378. *
  1379. * @param timeout Maximum time to wait for an allocation.
  1380. *
  1381. * @return a pointer to a newly allocated net_pkt on success, NULL otherwise.
  1382. */
  1383. #if !defined(NET_PKT_DEBUG_ENABLED)
  1384. struct net_pkt *net_pkt_rx_alloc(k_timeout_t timeout);
  1385. #endif
  1386. /**
  1387. * @brief Allocate a network packet for a specific network interface.
  1388. *
  1389. * @param iface The network interface the packet is supposed to go through.
  1390. * @param timeout Maximum time to wait for an allocation.
  1391. *
  1392. * @return a pointer to a newly allocated net_pkt on success, NULL otherwise.
  1393. */
  1394. #if !defined(NET_PKT_DEBUG_ENABLED)
  1395. struct net_pkt *net_pkt_alloc_on_iface(struct net_if *iface,
  1396. k_timeout_t timeout);
  1397. /* Same as above but specifically for RX packet */
  1398. struct net_pkt *net_pkt_rx_alloc_on_iface(struct net_if *iface,
  1399. k_timeout_t timeout);
  1400. #endif
  1401. /**
  1402. * @brief Allocate buffer for a net_pkt
  1403. *
  1404. * @details: such allocator will take into account space necessary for headers,
  1405. * MTU, and existing buffer (if any). Beware that, due to all these
  1406. * criteria, the allocated size might be smaller/bigger than
  1407. * requested one.
  1408. *
  1409. * @param pkt The network packet requiring buffer to be allocated.
  1410. * @param size The size of buffer being requested.
  1411. * @param proto The IP protocol type (can be 0 for none).
  1412. * @param timeout Maximum time to wait for an allocation.
  1413. *
  1414. * @return 0 on success, negative errno code otherwise.
  1415. */
  1416. #if !defined(NET_PKT_DEBUG_ENABLED)
  1417. int net_pkt_alloc_buffer(struct net_pkt *pkt,
  1418. size_t size,
  1419. enum net_ip_protocol proto,
  1420. k_timeout_t timeout);
  1421. #endif
  1422. /**
  1423. * @brief Allocate a network packet and buffer at once
  1424. *
  1425. * @param iface The network interface the packet is supposed to go through.
  1426. * @param size The size of buffer.
  1427. * @param family The family to which the packet belongs.
  1428. * @param proto The IP protocol type (can be 0 for none).
  1429. * @param timeout Maximum time to wait for an allocation.
  1430. *
  1431. * @return a pointer to a newly allocated net_pkt on success, NULL otherwise.
  1432. */
  1433. #if !defined(NET_PKT_DEBUG_ENABLED)
  1434. struct net_pkt *net_pkt_alloc_with_buffer(struct net_if *iface,
  1435. size_t size,
  1436. sa_family_t family,
  1437. enum net_ip_protocol proto,
  1438. k_timeout_t timeout);
  1439. /* Same as above but specifically for RX packet */
  1440. struct net_pkt *net_pkt_rx_alloc_with_buffer(struct net_if *iface,
  1441. size_t size,
  1442. sa_family_t family,
  1443. enum net_ip_protocol proto,
  1444. k_timeout_t timeout);
  1445. #endif
  1446. /**
  1447. * @brief Append a buffer in packet
  1448. *
  1449. * @param pkt Network packet where to append the buffer
  1450. * @param buffer Buffer to append
  1451. */
  1452. void net_pkt_append_buffer(struct net_pkt *pkt, struct net_buf *buffer);
  1453. /**
  1454. * @brief Get available buffer space from a pkt
  1455. *
  1456. * @note Reserved bytes (headroom) in any of the fragments are not considered to
  1457. * be available.
  1458. *
  1459. * @param pkt The net_pkt which buffer availability should be evaluated
  1460. *
  1461. * @return the amount of buffer available
  1462. */
  1463. size_t net_pkt_available_buffer(struct net_pkt *pkt);
  1464. /**
  1465. * @brief Get available buffer space for payload from a pkt
  1466. *
  1467. * @note Reserved bytes (headroom) in any of the fragments are not considered to
  1468. * be available.
  1469. *
  1470. * @details Unlike net_pkt_available_buffer(), this will take into account
  1471. * the headers space.
  1472. *
  1473. * @param pkt The net_pkt which payload buffer availability should
  1474. * be evaluated
  1475. * @param proto The IP protocol type (can be 0 for none).
  1476. *
  1477. * @return the amount of buffer available for payload
  1478. */
  1479. size_t net_pkt_available_payload_buffer(struct net_pkt *pkt,
  1480. enum net_ip_protocol proto);
  1481. /**
  1482. * @brief Trim net_pkt buffer
  1483. *
  1484. * @details This will basically check for unused buffers and deallocates
  1485. * them relevantly
  1486. *
  1487. * @param pkt The net_pkt which buffer will be trimmed
  1488. */
  1489. void net_pkt_trim_buffer(struct net_pkt *pkt);
  1490. /**
  1491. * @brief Remove @a length bytes from tail of packet
  1492. *
  1493. * @details This function does not take packet cursor into account. It is a
  1494. * helper to remove unneeded bytes from tail of packet (like appended
  1495. * CRC). It takes care of buffer deallocation if removed bytes span
  1496. * whole buffer(s).
  1497. *
  1498. * @param pkt Network packet
  1499. * @param length Number of bytes to be removed
  1500. *
  1501. * @retval 0 On success.
  1502. * @retval -EINVAL If packet length is shorter than @a length.
  1503. */
  1504. int net_pkt_remove_tail(struct net_pkt *pkt, size_t length);
  1505. /**
  1506. * @brief Initialize net_pkt cursor
  1507. *
  1508. * @details This will initialize the net_pkt cursor from its buffer.
  1509. *
  1510. * @param pkt The net_pkt whose cursor is going to be initialized
  1511. */
  1512. void net_pkt_cursor_init(struct net_pkt *pkt);
  1513. /**
  1514. * @brief Backup net_pkt cursor
  1515. *
  1516. * @param pkt The net_pkt whose cursor is going to be backed up
  1517. * @param backup The cursor where to backup net_pkt cursor
  1518. */
  1519. static inline void net_pkt_cursor_backup(struct net_pkt *pkt,
  1520. struct net_pkt_cursor *backup)
  1521. {
  1522. backup->buf = pkt->cursor.buf;
  1523. backup->pos = pkt->cursor.pos;
  1524. }
  1525. /**
  1526. * @brief Restore net_pkt cursor from a backup
  1527. *
  1528. * @param pkt The net_pkt whose cursor is going to be restored
  1529. * @param backup The cursor from where to restore net_pkt cursor
  1530. */
  1531. static inline void net_pkt_cursor_restore(struct net_pkt *pkt,
  1532. struct net_pkt_cursor *backup)
  1533. {
  1534. pkt->cursor.buf = backup->buf;
  1535. pkt->cursor.pos = backup->pos;
  1536. }
  1537. /**
  1538. * @brief Returns current position of the cursor
  1539. *
  1540. * @param pkt The net_pkt whose cursor position is going to be returned
  1541. *
  1542. * @return cursor's position
  1543. */
  1544. static inline void *net_pkt_cursor_get_pos(struct net_pkt *pkt)
  1545. {
  1546. return pkt->cursor.pos;
  1547. }
  1548. /**
  1549. * @brief Skip some data from a net_pkt
  1550. *
  1551. * @details net_pkt's cursor should be properly initialized
  1552. * Cursor position will be updated after the operation.
  1553. * Depending on the value of pkt->overwrite bit, this function
  1554. * will affect the buffer length or not. If it's true, it will
  1555. * advance the cursor to the requested length. If it's false,
  1556. * it will do the same but if the cursor was already also at the
  1557. * end of existing data, it will increment the buffer length.
  1558. * So in this case, its behavior is just like net_pkt_write or
  1559. * net_pkt_memset, difference being that it will not affect the
  1560. * buffer content itself (which may be just garbage then).
  1561. *
  1562. * @param pkt The net_pkt whose cursor will be updated to skip given
  1563. * amount of data from the buffer.
  1564. * @param length Amount of data to skip in the buffer
  1565. *
  1566. * @return 0 in success, negative errno code otherwise.
  1567. */
  1568. int net_pkt_skip(struct net_pkt *pkt, size_t length);
  1569. /**
  1570. * @brief Memset some data in a net_pkt
  1571. *
  1572. * @details net_pkt's cursor should be properly initialized and,
  1573. * if needed, positioned using net_pkt_skip.
  1574. * Cursor position will be updated after the operation.
  1575. *
  1576. * @param pkt The net_pkt whose buffer to fill starting at the current
  1577. * cursor position.
  1578. * @param byte The byte to write in memory
  1579. * @param length Amount of data to memset with given byte
  1580. *
  1581. * @return 0 in success, negative errno code otherwise.
  1582. */
  1583. int net_pkt_memset(struct net_pkt *pkt, int byte, size_t length);
  1584. /**
  1585. * @brief Copy data from a packet into another one.
  1586. *
  1587. * @details Both net_pkt cursors should be properly initialized and,
  1588. * if needed, positioned using net_pkt_skip.
  1589. * The cursors will be updated after the operation.
  1590. *
  1591. * @param pkt_dst Destination network packet.
  1592. * @param pkt_src Source network packet.
  1593. * @param length Length of data to be copied.
  1594. *
  1595. * @return 0 on success, negative errno code otherwise.
  1596. */
  1597. int net_pkt_copy(struct net_pkt *pkt_dst,
  1598. struct net_pkt *pkt_src,
  1599. size_t length);
  1600. /**
  1601. * @brief Clone pkt and its buffer.
  1602. *
  1603. * @param pkt Original pkt to be cloned
  1604. * @param timeout Timeout to wait for free buffer
  1605. *
  1606. * @return NULL if error, cloned packet otherwise.
  1607. */
  1608. struct net_pkt *net_pkt_clone(struct net_pkt *pkt, k_timeout_t timeout);
  1609. /**
  1610. * @brief Clone pkt and increase the refcount of its buffer.
  1611. *
  1612. * @param pkt Original pkt to be shallow cloned
  1613. * @param timeout Timeout to wait for free packet
  1614. *
  1615. * @return NULL if error, cloned packet otherwise.
  1616. */
  1617. struct net_pkt *net_pkt_shallow_clone(struct net_pkt *pkt,
  1618. k_timeout_t timeout);
  1619. /**
  1620. * @brief Read some data from a net_pkt
  1621. *
  1622. * @details net_pkt's cursor should be properly initialized and,
  1623. * if needed, positioned using net_pkt_skip.
  1624. * Cursor position will be updated after the operation.
  1625. *
  1626. * @param pkt The network packet from where to read some data
  1627. * @param data The destination buffer where to copy the data
  1628. * @param length The amount of data to copy
  1629. *
  1630. * @return 0 on success, negative errno code otherwise.
  1631. */
  1632. int net_pkt_read(struct net_pkt *pkt, void *data, size_t length);
  1633. /* Read uint8_t data data a net_pkt */
  1634. static inline int net_pkt_read_u8(struct net_pkt *pkt, uint8_t *data)
  1635. {
  1636. return net_pkt_read(pkt, data, 1);
  1637. }
  1638. /**
  1639. * @brief Read uint16_t big endian data from a net_pkt
  1640. *
  1641. * @details net_pkt's cursor should be properly initialized and,
  1642. * if needed, positioned using net_pkt_skip.
  1643. * Cursor position will be updated after the operation.
  1644. *
  1645. * @param pkt The network packet from where to read
  1646. * @param data The destination uint16_t where to copy the data
  1647. *
  1648. * @return 0 on success, negative errno code otherwise.
  1649. */
  1650. int net_pkt_read_be16(struct net_pkt *pkt, uint16_t *data);
  1651. /**
  1652. * @brief Read uint16_t little endian data from a net_pkt
  1653. *
  1654. * @details net_pkt's cursor should be properly initialized and,
  1655. * if needed, positioned using net_pkt_skip.
  1656. * Cursor position will be updated after the operation.
  1657. *
  1658. * @param pkt The network packet from where to read
  1659. * @param data The destination uint16_t where to copy the data
  1660. *
  1661. * @return 0 on success, negative errno code otherwise.
  1662. */
  1663. int net_pkt_read_le16(struct net_pkt *pkt, uint16_t *data);
  1664. /**
  1665. * @brief Read uint32_t big endian data from a net_pkt
  1666. *
  1667. * @details net_pkt's cursor should be properly initialized and,
  1668. * if needed, positioned using net_pkt_skip.
  1669. * Cursor position will be updated after the operation.
  1670. *
  1671. * @param pkt The network packet from where to read
  1672. * @param data The destination uint32_t where to copy the data
  1673. *
  1674. * @return 0 on success, negative errno code otherwise.
  1675. */
  1676. int net_pkt_read_be32(struct net_pkt *pkt, uint32_t *data);
  1677. /**
  1678. * @brief Write data into a net_pkt
  1679. *
  1680. * @details net_pkt's cursor should be properly initialized and,
  1681. * if needed, positioned using net_pkt_skip.
  1682. * Cursor position will be updated after the operation.
  1683. *
  1684. * @param pkt The network packet where to write
  1685. * @param data Data to be written
  1686. * @param length Length of the data to be written
  1687. *
  1688. * @return 0 on success, negative errno code otherwise.
  1689. */
  1690. int net_pkt_write(struct net_pkt *pkt, const void *data, size_t length);
  1691. /* Write uint8_t data into a net_pkt. */
  1692. static inline int net_pkt_write_u8(struct net_pkt *pkt, uint8_t data)
  1693. {
  1694. return net_pkt_write(pkt, &data, sizeof(uint8_t));
  1695. }
  1696. /* Write uint16_t big endian data into a net_pkt. */
  1697. static inline int net_pkt_write_be16(struct net_pkt *pkt, uint16_t data)
  1698. {
  1699. uint16_t data_be16 = htons(data);
  1700. return net_pkt_write(pkt, &data_be16, sizeof(uint16_t));
  1701. }
  1702. /* Write uint32_t big endian data into a net_pkt. */
  1703. static inline int net_pkt_write_be32(struct net_pkt *pkt, uint32_t data)
  1704. {
  1705. uint32_t data_be32 = htonl(data);
  1706. return net_pkt_write(pkt, &data_be32, sizeof(uint32_t));
  1707. }
  1708. /* Write uint32_t little endian data into a net_pkt. */
  1709. static inline int net_pkt_write_le32(struct net_pkt *pkt, uint32_t data)
  1710. {
  1711. uint32_t data_le32 = sys_cpu_to_le32(data);
  1712. return net_pkt_write(pkt, &data_le32, sizeof(uint32_t));
  1713. }
  1714. /* Write uint16_t little endian data into a net_pkt. */
  1715. static inline int net_pkt_write_le16(struct net_pkt *pkt, uint16_t data)
  1716. {
  1717. uint16_t data_le16 = sys_cpu_to_le16(data);
  1718. return net_pkt_write(pkt, &data_le16, sizeof(uint16_t));
  1719. }
  1720. /**
  1721. * @brief Get the amount of data which can be read from current cursor position
  1722. *
  1723. * @param pkt Network packet
  1724. *
  1725. * @return Amount of data which can be read from current pkt cursor
  1726. */
  1727. size_t net_pkt_remaining_data(struct net_pkt *pkt);
  1728. /**
  1729. * @brief Update the overall length of a packet
  1730. *
  1731. * @details Unlike net_pkt_pull() below, this does not take packet cursor
  1732. * into account. It's mainly a helper dedicated for ipv4 and ipv6
  1733. * input functions. It shrinks the overall length by given parameter.
  1734. *
  1735. * @param pkt Network packet
  1736. * @param length The new length of the packet
  1737. *
  1738. * @return 0 on success, negative errno code otherwise.
  1739. */
  1740. int net_pkt_update_length(struct net_pkt *pkt, size_t length);
  1741. /**
  1742. * @brief Remove data from the packet at current location
  1743. *
  1744. * @details net_pkt's cursor should be properly initialized and,
  1745. * eventually, properly positioned using net_pkt_skip/read/write.
  1746. * Note that net_pkt's cursor is reset by this function.
  1747. *
  1748. * @param pkt Network packet
  1749. * @param length Number of bytes to be removed
  1750. *
  1751. * @return 0 on success, negative errno code otherwise.
  1752. */
  1753. int net_pkt_pull(struct net_pkt *pkt, size_t length);
  1754. /**
  1755. * @brief Get the actual offset in the packet from its cursor
  1756. *
  1757. * @param pkt Network packet.
  1758. *
  1759. * @return a valid offset on success, 0 otherwise as there is nothing that
  1760. * can be done to evaluate the offset.
  1761. */
  1762. uint16_t net_pkt_get_current_offset(struct net_pkt *pkt);
  1763. /**
  1764. * @brief Check if a data size could fit contiguously
  1765. *
  1766. * @details net_pkt's cursor should be properly initialized and,
  1767. * if needed, positioned using net_pkt_skip.
  1768. *
  1769. * @param pkt Network packet.
  1770. * @param size The size to check for contiguity
  1771. *
  1772. * @return true if that is the case, false otherwise.
  1773. */
  1774. bool net_pkt_is_contiguous(struct net_pkt *pkt, size_t size);
  1775. /**
  1776. * Get the contiguous buffer space
  1777. *
  1778. * @param pkt Network packet
  1779. *
  1780. * @return The available contiguous buffer space in bytes starting from the
  1781. * current cursor position. 0 in case of an error.
  1782. */
  1783. size_t net_pkt_get_contiguous_len(struct net_pkt *pkt);
  1784. struct net_pkt_data_access {
  1785. #if !defined(CONFIG_NET_HEADERS_ALWAYS_CONTIGUOUS)
  1786. void *data;
  1787. #endif
  1788. const size_t size;
  1789. };
  1790. #if defined(CONFIG_NET_HEADERS_ALWAYS_CONTIGUOUS)
  1791. #define NET_PKT_DATA_ACCESS_DEFINE(_name, _type) \
  1792. struct net_pkt_data_access _name = { \
  1793. .size = sizeof(_type), \
  1794. }
  1795. #define NET_PKT_DATA_ACCESS_CONTIGUOUS_DEFINE(_name, _type) \
  1796. NET_PKT_DATA_ACCESS_DEFINE(_name, _type)
  1797. #else
  1798. #define NET_PKT_DATA_ACCESS_DEFINE(_name, _type) \
  1799. _type _hdr_##_name; \
  1800. struct net_pkt_data_access _name = { \
  1801. .data = &_hdr_##_name, \
  1802. .size = sizeof(_type), \
  1803. }
  1804. #define NET_PKT_DATA_ACCESS_CONTIGUOUS_DEFINE(_name, _type) \
  1805. struct net_pkt_data_access _name = { \
  1806. .data = NULL, \
  1807. .size = sizeof(_type), \
  1808. }
  1809. #endif /* CONFIG_NET_HEADERS_ALWAYS_CONTIGUOUS */
  1810. /**
  1811. * @brief Get data from a network packet in a contiguous way
  1812. *
  1813. * @details net_pkt's cursor should be properly initialized and,
  1814. * if needed, positioned using net_pkt_skip.
  1815. * Cursor position will be updated after the operation.
  1816. *
  1817. * @param pkt The network packet from where to get the data.
  1818. * @param access A pointer to a valid net_pkt_data_access describing the
  1819. * data to get in a contiguous way.
  1820. *
  1821. * @return a pointer to the requested contiguous data, NULL otherwise.
  1822. */
  1823. void *net_pkt_get_data(struct net_pkt *pkt,
  1824. struct net_pkt_data_access *access);
  1825. /**
  1826. * @brief Set contiguous data into a network packet
  1827. *
  1828. * @details net_pkt's cursor should be properly initialized and,
  1829. * if needed, positioned using net_pkt_skip.
  1830. * Cursor position will be updated after the operation.
  1831. *
  1832. * @param pkt The network packet to where the data should be set.
  1833. * @param access A pointer to a valid net_pkt_data_access describing the
  1834. * data to set.
  1835. *
  1836. * @return 0 on success, a negative errno otherwise.
  1837. */
  1838. int net_pkt_set_data(struct net_pkt *pkt,
  1839. struct net_pkt_data_access *access);
  1840. /**
  1841. * Acknowledge previously contiguous data taken from a network packet
  1842. * Packet needs to be set to overwrite mode.
  1843. */
  1844. static inline int net_pkt_acknowledge_data(struct net_pkt *pkt,
  1845. struct net_pkt_data_access *access)
  1846. {
  1847. return net_pkt_skip(pkt, access->size);
  1848. }
  1849. /**
  1850. * @}
  1851. */
  1852. #ifdef __cplusplus
  1853. }
  1854. #endif
  1855. #endif /* ZEPHYR_INCLUDE_NET_NET_PKT_H_ */