Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 | /** @file
* @brief Network buffer API
*
* Network data is passed between different parts of the stack via
* net_buf struct.
*/
/*
* Copyright (c) 2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
/* Data buffer API - used for all data to/from net */
#ifndef __NBUF_H
#define __NBUF_H
#include <stdint.h>
#include <stdbool.h>
#include <net/buf.h>
#include <net/net_core.h>
#include <net/net_linkaddr.h>
#include <net/net_ip.h>
#include <net/net_if.h>
#include <net/net_context.h>
#ifdef __cplusplus
extern "C" {
#endif
struct net_context;
struct net_nbuf {
/** Network connection context */
struct net_context *context;
/** Network context token that user can set. This is passed
* to user callback when data has been sent.
*/
void *token;
/** Network interface */
struct net_if *iface;
/** @cond ignore */
uint8_t *appdata; /* application data starts here */
uint8_t *next_hdr; /* where is the next header */
/* Filled by layer 2 when network packet is received. */
struct net_linkaddr lladdr_src;
struct net_linkaddr lladdr_dst;
uint16_t appdatalen;
uint16_t reserve; /* length of the protocol headers */
uint8_t ll_reserve; /* link layer header length */
uint8_t family; /* IPv4 vs IPv6 */
uint8_t ip_hdr_len; /* pre-filled in order to avoid func call */
uint8_t ext_len; /* length of extension headers */
uint8_t ext_bitmap;
#if defined(CONFIG_NET_IPV6)
uint8_t ext_opt_len; /* IPv6 ND option length */
#endif
#if defined(CONFIG_NET_TCP)
bool buf_sent; /* Is this net_buf sent or not */
#endif
/* @endcond */
};
/** @cond ignore */
/* The interface real ll address */
static inline struct net_linkaddr *net_nbuf_ll_if(struct net_buf *buf)
{
return net_if_get_link_addr(
((struct net_nbuf *)net_buf_user_data(buf))->iface);
}
static inline struct net_context *net_nbuf_context(struct net_buf *buf)
{
return ((struct net_nbuf *)net_buf_user_data(buf))->context;
}
static inline void net_nbuf_set_context(struct net_buf *buf,
struct net_context *ctx)
{
((struct net_nbuf *)net_buf_user_data(buf))->context = ctx;
}
static inline void *net_nbuf_token(struct net_buf *buf)
{
return ((struct net_nbuf *)net_buf_user_data(buf))->token;
}
static inline void net_nbuf_set_token(struct net_buf *buf, void *token)
{
((struct net_nbuf *)net_buf_user_data(buf))->token = token;
}
static inline struct net_if *net_nbuf_iface(struct net_buf *buf)
{
return ((struct net_nbuf *)net_buf_user_data(buf))->iface;
}
static inline void net_nbuf_set_iface(struct net_buf *buf, struct net_if *iface)
{
((struct net_nbuf *)net_buf_user_data(buf))->iface = iface;
}
static inline uint8_t net_nbuf_family(struct net_buf *buf)
{
return ((struct net_nbuf *)net_buf_user_data(buf))->family;
}
static inline void net_nbuf_set_family(struct net_buf *buf, uint8_t family)
{
((struct net_nbuf *)net_buf_user_data(buf))->family = family;
}
static inline uint8_t net_nbuf_ip_hdr_len(struct net_buf *buf)
{
return ((struct net_nbuf *) net_buf_user_data(buf))->ip_hdr_len;
}
static inline void net_nbuf_set_ip_hdr_len(struct net_buf *buf, uint8_t len)
{
((struct net_nbuf *) net_buf_user_data(buf))->ip_hdr_len = len;
}
static inline uint8_t net_nbuf_ext_len(struct net_buf *buf)
{
return ((struct net_nbuf *)net_buf_user_data(buf))->ext_len;
}
static inline void net_nbuf_set_ext_len(struct net_buf *buf, uint8_t len)
{
((struct net_nbuf *)net_buf_user_data(buf))->ext_len = len;
}
static inline uint8_t net_nbuf_ext_bitmap(struct net_buf *buf)
{
return ((struct net_nbuf *)net_buf_user_data(buf))->ext_bitmap;
}
static inline void net_nbuf_set_ext_bitmap(struct net_buf *buf, uint8_t bm)
{
((struct net_nbuf *)net_buf_user_data(buf))->ext_bitmap = bm;
}
static inline void net_nbuf_add_ext_bitmap(struct net_buf *buf, uint8_t bm)
{
((struct net_nbuf *)net_buf_user_data(buf))->ext_bitmap |= bm;
}
static inline uint8_t *net_nbuf_next_hdr(struct net_buf *buf)
{
return ((struct net_nbuf *)net_buf_user_data(buf))->next_hdr;
}
static inline void net_nbuf_set_next_hdr(struct net_buf *buf, uint8_t *hdr)
{
((struct net_nbuf *)net_buf_user_data(buf))->next_hdr = hdr;
}
#if defined(CONFIG_NET_IPV6)
static inline uint8_t net_nbuf_ext_opt_len(struct net_buf *buf)
{
return ((struct net_nbuf *)net_buf_user_data(buf))->ext_opt_len;
}
static inline void net_nbuf_set_ext_opt_len(struct net_buf *buf, uint8_t len)
{
((struct net_nbuf *)net_buf_user_data(buf))->ext_opt_len = len;
}
#endif
#if defined(CONFIG_NET_TCP)
static inline uint8_t net_nbuf_buf_sent(struct net_buf *buf)
{
return ((struct net_nbuf *)net_buf_user_data(buf))->buf_sent;
}
static inline void net_nbuf_set_buf_sent(struct net_buf *buf, bool sent)
{
((struct net_nbuf *)net_buf_user_data(buf))->buf_sent = sent;
}
#endif
static inline uint16_t net_nbuf_get_len(struct net_buf *buf)
{
return buf->len;
}
static inline void net_nbuf_set_len(struct net_buf *buf, uint16_t len)
{
buf->len = len;
}
static inline uint8_t *net_nbuf_ip_data(struct net_buf *buf)
{
return buf->frags->data;
}
static inline uint8_t *net_nbuf_udp_data(struct net_buf *buf)
{
return &buf->frags->data[net_nbuf_ip_hdr_len(buf) +
net_nbuf_ext_len(buf)];
}
static inline uint8_t *net_nbuf_tcp_data(struct net_buf *buf)
{
return &buf->frags->data[net_nbuf_ip_hdr_len(buf) +
net_nbuf_ext_len(buf)];
}
static inline uint8_t *net_nbuf_icmp_data(struct net_buf *buf)
{
return &buf->frags->data[net_nbuf_ip_hdr_len(buf) +
net_nbuf_ext_len(buf)];
}
static inline uint8_t *net_nbuf_appdata(struct net_buf *buf)
{
return ((struct net_nbuf *)net_buf_user_data(buf))->appdata;
}
static inline void net_nbuf_set_appdata(struct net_buf *buf, uint8_t *data)
{
((struct net_nbuf *)net_buf_user_data(buf))->appdata = data;
}
static inline uint16_t net_nbuf_appdatalen(struct net_buf *buf)
{
return ((struct net_nbuf *)net_buf_user_data(buf))->appdatalen;
}
static inline void net_nbuf_set_appdatalen(struct net_buf *buf, uint16_t len)
{
((struct net_nbuf *)net_buf_user_data(buf))->appdatalen = len;
}
static inline uint16_t net_nbuf_reserve(struct net_buf *buf)
{
return ((struct net_nbuf *)net_buf_user_data(buf))->reserve;
}
static inline uint8_t net_nbuf_ll_reserve(struct net_buf *buf)
{
return ((struct net_nbuf *) net_buf_user_data(buf))->ll_reserve;
}
static inline void net_nbuf_set_ll_reserve(struct net_buf *buf, uint8_t len)
{
((struct net_nbuf *) net_buf_user_data(buf))->ll_reserve = len;
}
static inline uint8_t *net_nbuf_ll(struct net_buf *buf)
{
return net_nbuf_ip_data(buf) - net_nbuf_ll_reserve(buf);
}
static inline struct net_linkaddr *net_nbuf_ll_src(struct net_buf *buf)
{
return &((struct net_nbuf *)net_buf_user_data(buf))->lladdr_src;
}
static inline struct net_linkaddr *net_nbuf_ll_dst(struct net_buf *buf)
{
return &((struct net_nbuf *)net_buf_user_data(buf))->lladdr_dst;
}
static inline void net_nbuf_ll_clear(struct net_buf *buf)
{
memset(net_nbuf_ll(buf), 0, net_nbuf_ll_reserve(buf));
net_nbuf_ll_src(buf)->addr = NULL;
net_nbuf_ll_src(buf)->len = 0;
}
static inline void net_nbuf_ll_swap(struct net_buf *buf)
{
uint8_t *addr = net_nbuf_ll_src(buf)->addr;
net_nbuf_ll_src(buf)->addr = net_nbuf_ll_dst(buf)->addr;
net_nbuf_ll_dst(buf)->addr = addr;
}
#define NET_IPV6_BUF(buf) ((struct net_ipv6_hdr *)net_nbuf_ip_data(buf))
#define NET_IPV4_BUF(buf) ((struct net_ipv4_hdr *)net_nbuf_ip_data(buf))
#define NET_ICMP_BUF(buf) ((struct net_icmp_hdr *)net_nbuf_icmp_data(buf))
#define NET_UDP_BUF(buf) ((struct net_udp_hdr *)(net_nbuf_udp_data(buf)))
#define NET_TCP_BUF(buf) ((struct net_tcp_hdr *)(net_nbuf_tcp_data(buf)))
static inline void net_nbuf_set_src_ipv6_addr(struct net_buf *buf)
{
net_if_ipv6_select_src_addr(net_context_get_iface(
net_nbuf_context(buf)),
&NET_IPV6_BUF(buf)->src);
}
/* @endcond */
#if defined(CONFIG_NET_DEBUG_NET_BUF)
/* Debug versions of the nbuf functions that are used when tracking
* buffer usage.
*/
struct net_buf *net_nbuf_get_rx_debug(struct net_context *context,
int32_t timeout,
const char *caller, int line);
#define net_nbuf_get_rx(context, timeout) \
net_nbuf_get_rx_debug(context, timeout, __func__, __LINE__)
struct net_buf *net_nbuf_get_tx_debug(struct net_context *context,
int32_t timeout,
const char *caller, int line);
#define net_nbuf_get_tx(context, timeout) \
net_nbuf_get_tx_debug(context, timeout, __func__, __LINE__)
struct net_buf *net_nbuf_get_data_debug(struct net_context *context,
int32_t timeout,
const char *caller, int line);
#define net_nbuf_get_data(context, timeout) \
net_nbuf_get_data_debug(context, timeout, __func__, __LINE__)
struct net_buf *net_nbuf_get_reserve_rx_debug(uint16_t reserve_head,
int32_t timeout,
const char *caller, int line);
#define net_nbuf_get_reserve_rx(res, timeout) \
net_nbuf_get_reserve_rx_debug(res, timeout, __func__, __LINE__)
struct net_buf *net_nbuf_get_reserve_tx_debug(uint16_t reserve_head,
int32_t timeout,
const char *caller, int line);
#define net_nbuf_get_reserve_tx(res, timeout) \
net_nbuf_get_reserve_tx_debug(res, timeout, __func__, __LINE__)
struct net_buf *net_nbuf_get_reserve_data_debug(uint16_t reserve_head,
int32_t timeout,
const char *caller, int line);
#define net_nbuf_get_reserve_data(res, timeout) \
net_nbuf_get_reserve_data_debug(res, timeout, __func__, __LINE__)
void net_nbuf_unref_debug(struct net_buf *buf, const char *caller, int line);
#define net_nbuf_unref(buf) net_nbuf_unref_debug(buf, __func__, __LINE__)
struct net_buf *net_nbuf_ref_debug(struct net_buf *buf, const char *caller,
int line);
#define net_nbuf_ref(buf) net_nbuf_ref_debug(buf, __func__, __LINE__)
/**
* @brief Print fragment list and the fragment sizes
*
* @details Only available if debugging is activated.
*
* @param buf Network buffer fragment. This should be the first fragment (data)
* in the fragment list.
*/
void net_nbuf_print_frags(struct net_buf *buf);
#else /* CONFIG_NET_DEBUG_NET_BUF */
#define net_nbuf_print_frags(...)
/**
* @brief Get buffer from the RX buffers pool.
*
* @details Get network buffer from RX buffer pool. You must have
* network context before able to use this function.
*
* @param context Network context that will be related to
* this buffer.
* @param timeout Affects the action taken should the net buf pool be empty.
* If K_NO_WAIT, then return immediately. If K_FOREVER, then
* wait as long as necessary. Otherwise, wait up to the specified
* number of milliseconds before timing out.
*
* @return Network buffer if successful, NULL otherwise.
*/
struct net_buf *net_nbuf_get_rx(struct net_context *context,
int32_t timeout);
/**
* @brief Get buffer from the TX buffers pool.
*
* @details Get network buffer from TX buffer pool. You must have
* network context before able to use this function.
*
* @param context Network context that will be related to
* this buffer.
* @param timeout Affects the action taken should the net buf pool be empty.
* If K_NO_WAIT, then return immediately. If K_FOREVER, then
* wait as long as necessary. Otherwise, wait up to the specified
* number of milliseconds before timing out.
*
* @return Network buffer if successful, NULL otherwise.
*/
struct net_buf *net_nbuf_get_tx(struct net_context *context,
int32_t timeout);
/**
* @brief Get buffer from the DATA buffers pool.
*
* @details Get network buffer from DATA buffer pool. You must have
* network context before able to use this function.
*
* @param context Network context that will be related to
* this buffer.
* @param timeout Affects the action taken should the net buf pool be empty.
* If K_NO_WAIT, then return immediately. If K_FOREVER, then
* wait as long as necessary. Otherwise, wait up to the specified
* number of milliseconds before timing out.
*
* @return Network buffer if successful, NULL otherwise.
*/
struct net_buf *net_nbuf_get_data(struct net_context *context,
int32_t timeout);
/**
* @brief Get RX buffer from pool but also reserve headroom for
* potential headers.
*
* @details Normally this version is not useful for applications
* but is mainly used by network fragmentation code.
*
* @param reserve_head How many bytes to reserve for headroom.
* @param timeout Affects the action taken should the net buf pool be empty.
* If K_NO_WAIT, then return immediately. If K_FOREVER, then
* wait as long as necessary. Otherwise, wait up to the specified
* number of milliseconds before timing out.
*
* @return Network buffer if successful, NULL otherwise.
*/
struct net_buf *net_nbuf_get_reserve_rx(uint16_t reserve_head,
int32_t timeout);
/**
* @brief Get TX buffer from pool but also reserve headroom for
* potential headers.
*
* @details Normally this version is not useful for applications
* but is mainly used by network fragmentation code.
*
* @param reserve_head How many bytes to reserve for headroom.
* @param timeout Affects the action taken should the net buf pool be empty.
* If K_NO_WAIT, then return immediately. If K_FOREVER, then
* wait as long as necessary. Otherwise, wait up to the specified
* number of milliseconds before timing out.
*
* @return Network buffer if successful, NULL otherwise.
*/
struct net_buf *net_nbuf_get_reserve_tx(uint16_t reserve_head,
int32_t timeout);
/**
* @brief Get DATA buffer from pool but also reserve headroom for
* potential headers.
*
* @details Normally this version is not useful for applications
* but is mainly used by network fragmentation code.
*
* @param reserve_head How many bytes to reserve for headroom.
* @param timeout Affects the action taken should the net buf pool be empty.
* If K_NO_WAIT, then return immediately. If K_FOREVER, then
* wait as long as necessary. Otherwise, wait up to the specified
* number of milliseconds before timing out.
*
* @return Network buffer if successful, NULL otherwise.
*/
struct net_buf *net_nbuf_get_reserve_data(uint16_t reserve_head,
int32_t timeout);
/**
* @brief Place buffer back into the available buffers pool.
*
* @details Releases the buffer to other use. This needs to be
* called by application after it has finished with
* the buffer.
*
* @param buf Network buffer to release.
*
*/
void net_nbuf_unref(struct net_buf *buf);
/**
* @brief Increase the ref count
*
* @details Mark the buffer to be used still.
*
* @param buf Network buffer to ref.
*
* @return Network buffer if successful, NULL otherwise.
*/
struct net_buf *net_nbuf_ref(struct net_buf *buf);
#endif /* CONFIG_NET_DEBUG_NET_BUF */
/**
* @brief Copy a buffer with fragments while reserving some extra space
* in destination buffer before a copy.
*
* @details Note that the original buffer is not really usable after the copy
* as the function will call net_buf_pull() internally and should be discarded.
*
* @param buf Network buffer fragment. This should be the first fragment (data)
* in the fragment list.
* @param amount Max amount of data to be copied.
* @param reserve Amount of extra data (this is not link layer header) in the
* first data fragment that is returned. The function will copy the original
* buffer right after the reserved bytes in the first destination fragment.
* @param timeout Affects the action taken should the net buf pool be empty.
* If K_NO_WAIT, then return immediately. If K_FOREVER, then
* wait as long as necessary. Otherwise, wait up to the specified
* number of milliseconds before timing out.
*
* @return New fragment list if successful, NULL otherwise.
*/
struct net_buf *net_nbuf_copy(struct net_buf *buf, size_t amount,
size_t reserve, int32_t timeout);
/**
* @brief Copy a buffer with fragments while reserving some extra space
* in destination buffer before a copy.
*
* @param buf Network buffer fragment. This should be the first fragment (data)
* in the fragment list.
* @param reserve Amount of extra data (this is not link layer header) in the
* first data fragment that is returned. The function will copy the original
* buffer right after the reserved bytes in the first destination fragment.
* @param timeout Affects the action taken should the net buf pool be empty.
* If K_NO_WAIT, then return immediately. If K_FOREVER, then
* wait as long as necessary. Otherwise, wait up to the specified
* number of milliseconds before timing out.
*
* @return New fragment list if successful, NULL otherwise.
*/
static inline struct net_buf *net_nbuf_copy_all(struct net_buf *buf,
size_t reserve,
int32_t timeout)
{
return net_nbuf_copy(buf, net_buf_frags_len(buf), reserve, timeout);
}
/**
* @brief Copy len bytes from src starting from offset to dst
*
* This routine assumes that dst is conformed by one fragment with enough space
* to store @a len bytes starting from offset at src.
*
* @param dst Destination buffer
* @param src Source buffer that may be fragmented
* @param offset Starting point to copy from
* @param len Number of bytes to copy
* @return 0 on success
* @return -ENOMEM on error
*/
int net_nbuf_linear_copy(struct net_buf *dst, struct net_buf *src,
uint16_t offset, uint16_t len);
/**
* @brief Compact the fragment list.
*
* @details After this there is no more any free space in individual fragments.
* @param buf Network buffer fragment. This should be the Tx/Rx buffer.
*
* @return True if compact success, False otherwise. (Note that it fails only
* when input is data fragment)
*
*/
bool net_nbuf_compact(struct net_buf *buf);
/**
* @brief Check if the buffer chain is compact or not.
*
* @details The compact here means that is there any free space in the
* fragments. Only the last fragment can have some free space if the fragment
* list is compact.
*
* @param buf Network buffer.
*
* @return True if there is no free space in the fragment list,
* false otherwise.
*/
bool net_nbuf_is_compact(struct net_buf *buf);
/**
* @brief Remove given amount of data from the beginning of fragment list.
* This is similar thing to do as in net_buf_pull() but this function changes
* the fragment list instead of one fragment.
*
* @param buf Network buffer fragment list.
* @param amount Max amount of data to be remove.
*
* @return Pointer to start of the fragment list if successful. NULL can be
* returned if all fragments were removed from the list.
*/
struct net_buf *net_nbuf_pull(struct net_buf *buf, size_t amount);
/**
* @brief Append data to last fragment in fragment list
*
* @details Append data to last fragment. If there is not enough space in
* last fragment then new data fragment will be created and will be added to
* fragment list. Caller has to take care of endianness if needed.
*
* @param buf Network buffer fragment list.
* @param len Total length of input data
* @param data Data to be added
* @param timeout Affects the action taken should the net buf pool be empty.
* If K_NO_WAIT, then return immediately. If K_FOREVER, then
* wait as long as necessary. Otherwise, wait up to the specified
* number of milliseconds before timing out.
*
* @return True if all the data is placed at end of fragment list,
* False otherwise (In-case of false buf might contain input
* data in the process of placing into fragments).
*/
bool net_nbuf_append(struct net_buf *buf, uint16_t len, const uint8_t *data,
int32_t timeout);
/**
* @brief Append uint8_t data to last fragment in fragment list
*
* @details Append data to last fragment. If there is not enough space in last
* fragment then new data fragment will be created and will be added to
* fragment list. Caller has to take care of endianness if needed.
*
* @param buf Network buffer fragment list.
* @param data Data to be added
*
* @return True if all the data is placed at end of fragment list,
* False otherwise (In-case of false buf might contain input
* data in the process of placing into fragments).
*/
static inline bool net_nbuf_append_u8(struct net_buf *buf, uint8_t data)
{
return net_nbuf_append(buf, 1, &data, K_FOREVER);
}
/**
* @brief Append uint16_t data to last fragment in fragment list
*
* @details Append data to last fragment. If there is not enough space in last
* fragment then new data fragment will be created and will be added to
* fragment list. Caller has to take care of endianness if needed.
*
* @param buf Network buffer fragment list.
* @param data Data to be added
*
* @return True if all the data is placed at end of fragment list,
* False otherwise (In-case of false buf might contain input data
* in the process of placing into fragments).
*/
static inline bool net_nbuf_append_be16(struct net_buf *buf, uint16_t data)
{
uint16_t value = sys_cpu_to_be16(data);
return net_nbuf_append(buf, sizeof(uint16_t), (uint8_t *)&value,
K_FOREVER);
}
/**
* @brief Append uint32_t data to last fragment in fragment list
*
* @details Append data to last fragment. If there is not enough space in last
* fragment then new data fragment will be created and will be added to
* fragment list. Caller has to take care of endianness if needed.
*
* @param buf Network buffer fragment list.
* @param data Data to be added
*
* @return True if all the data is placed at end of fragment list,
* False otherwise (In-case of false buf might contain input data
* in the process of placing into fragments).
*/
static inline bool net_nbuf_append_be32(struct net_buf *buf, uint32_t data)
{
uint32_t value = sys_cpu_to_be32(data);
return net_nbuf_append(buf, sizeof(uint32_t), (uint8_t *)&value,
K_FOREVER);
}
/**
* @brief Get data from buffer
*
* @details Get N number of bytes starting from fragment's offset. If the total
* length of data is placed in multiple framgents, this function will read from
* all fragments until it reaches N number of bytes. Caller has to take care of
* endianness if needed.
*
* @param buf Network buffer fragment.
* @param offset Offset of input buffer.
* @param pos Pointer to position of offset after reading n number of bytes,
* this is with respect to return buffer(fragment).
* @param len Total length of data to be read.
* @param data Data will be copied here.
*
* @return Pointer to the fragment or
* NULL and pos is 0 after successful read,
* NULL and pos is 0xffff otherwise.
*/
struct net_buf *net_nbuf_read(struct net_buf *buf, uint16_t offset,
uint16_t *pos, uint16_t len, uint8_t *data);
/**
* @brief Skip N number of bytes while reading buffer
*
* @details Skip N number of bytes starting from fragment's offset. If the total
* length of data is placed in multiple framgents, this function will skip from
* all fragments until it reaches N number of bytes. This function is useful
* when unwanted data (e.g. reserved or not supported data in message) is part
* of fragment and want to skip it.
*
* @param buf Network buffer fragment.
* @param offset Offset of input buffer.
* @param pos Pointer to position of offset after reading n number of bytes,
* this is with respect to return buffer(fragment).
* @param len Total length of data to be read.
*
* @return Pointer to the fragment or
* NULL and pos is 0 after successful skip,
* NULL and pos is 0xffff otherwise.
*/
static inline struct net_buf *net_nbuf_skip(struct net_buf *buf,
uint16_t offset,
uint16_t *pos, uint16_t len)
{
return net_nbuf_read(buf, offset, pos, len, NULL);
}
/**
* @brief Get a byte value from fragmented buffer
*
* @param buf Network buffer fragment.
* @param offset Offset of input buffer.
* @param pos Pointer to position of offset after reading 2 bytes,
* this is with respect to return buffer(fragment).
* @param value Value is returned
*
* @return Pointer to fragment after successful read,
* NULL otherwise (if pos is 0, NULL is not a failure case).
*/
static inline struct net_buf *net_nbuf_read_u8(struct net_buf *buf,
uint16_t offset,
uint16_t *pos,
uint8_t *value)
{
return net_nbuf_read(buf, offset, pos, 1, value);
}
/**
* @brief Get 16 bit big endian value from fragmented buffer
*
* @param buf Network buffer fragment.
* @param offset Offset of input buffer.
* @param pos Pointer to position of offset after reading 2 bytes,
* this is with respect to return buffer(fragment).
* @param value Value is returned
*
* @return Pointer to fragment after successful read,
* NULL otherwise (if pos is 0, NULL is not a failure case).
*/
struct net_buf *net_nbuf_read_be16(struct net_buf *buf, uint16_t offset,
uint16_t *pos, uint16_t *value);
/**
* @brief Get 32 bit big endian value from fragmented buffer
*
* @param buf Network buffer fragment.
* @param offset Offset of input buffer.
* @param pos Pointer to position of offset after reading 4 bytes,
* this is with respect to return buffer(fragment).
* @param value Value is returned
*
* @return Pointer to fragment after successful read,
* NULL otherwise (if pos is 0, NULL is not a failure case).
*/
struct net_buf *net_nbuf_read_be32(struct net_buf *buf, uint16_t offset,
uint16_t *pos, uint32_t *value);
/**
* @brief Write data to an arbitrary offset in a series of fragments.
*
* @details Write data to an arbitrary offset in a series of fragments.
* Offset is based on fragment 'size' and calculates from input fragment
* starting position.
*
* Size in this context refers the fragment full size without link layer header
* part. The fragment might have user written data in it, the amount of such
* data is stored in frag->len variable (the frag->len is always <= frag->size).
* If using this API, the tailroom in the fragments will be taken into use.
*
* If offset is more than already allocated length in fragment, then empty space
* or extra empty fragments is created to reach proper offset.
* If there is any data present on input fragment offset, then it will be
* 'overwritten'. Use net_nbuf_insert() api if you don't want to overwrite.
*
* Offset is calculated from starting point of data area in input fragment.
* e.g. Buf(Tx/Rx) - Frag1 - Frag2 - Frag3 - Frag4
* (Assume FRAG DATA SIZE is 100 bytes after link layer header)
*
* 1) net_nbuf_write(buf, frag2, 20, &pos, 20, data, K_FOREVER)
* In this case write starts from "frag2->data + 20",
* returns frag2, pos = 40
*
* 2) net_nbuf_write(buf, frag1, 150, &pos, 60, data, K_FOREVER)
* In this case write starts from "frag2->data + 50"
* returns frag3, pos = 10
*
* 3) net_nbuf_write(buf, frag1, 350, &pos, 30, data, K_FOREVER)
* In this case write starts from "frag4->data + 50"
* returns frag4, pos = 80
*
* 4) net_nbuf_write(buf, frag2, 110, &pos, 90, data, K_FOREVER)
* In this case write starts from "frag3->data + 10"
* returns frag4, pos = 0
*
* 5) net_nbuf_write(buf, frag4, 110, &pos, 20, data, K_FOREVER)
* In this case write creates new data fragment and starts from
* "frag5->data + 10"
* returns frag5, pos = 30
*
* If input argument frag is NULL, it will create new data fragment
* and append at the end of fragment list.
*
* @param buf Network buffer fragment list.
* @param frag Network buffer fragment.
* @param offset Offset
* @param pos Position of offset after write completed (this will be
* relative to return fragment)
* @param len Length of the data to be written.
* @param data Data to be written
* @param timeout Affects the action taken should the net buf pool be empty.
* If K_NO_WAIT, then return immediately. If K_FOREVER, then
* wait as long as necessary. Otherwise, wait up to the specified
* number of milliseconds before timing out.
*
* @return Pointer to the fragment and position (*pos) where write ended,
* NULL and pos is 0xffff otherwise.
*/
struct net_buf *net_nbuf_write(struct net_buf *buf, struct net_buf *frag,
uint16_t offset, uint16_t *pos, uint16_t len,
uint8_t *data, int32_t timeout);
/* Write uint8_t data to an arbitrary offset in fragment. */
static inline struct net_buf *net_nbuf_write_u8(struct net_buf *buf,
struct net_buf *frag,
uint16_t offset,
uint16_t *pos,
uint8_t data)
{
return net_nbuf_write(buf, frag, offset, pos, sizeof(uint8_t),
&data, K_FOREVER);
}
/* Write uint16_t big endian value to an arbitrary offset in fragment. */
static inline struct net_buf *net_nbuf_write_be16(struct net_buf *buf,
struct net_buf *frag,
uint16_t offset,
uint16_t *pos,
uint16_t data)
{
uint16_t value = htons(data);
return net_nbuf_write(buf, frag, offset, pos, sizeof(uint16_t),
(uint8_t *)&value, K_FOREVER);
}
/* Write uint32_t big endian value to an arbitrary offset in fragment. */
static inline struct net_buf *net_nbuf_write_be32(struct net_buf *buf,
struct net_buf *frag,
uint16_t offset,
uint16_t *pos,
uint32_t data)
{
uint32_t value = htonl(data);
return net_nbuf_write(buf, frag, offset, pos, sizeof(uint32_t),
(uint8_t *)&value, K_FOREVER);
}
/**
* @brief Insert data at an arbitrary offset in a series of fragments.
*
* @details Insert data at an arbitrary offset in a series of fragments. Offset
* is based on fragment length (only user written data length, any tailroom
* in fragments does not come to consideration unlike net_nbuf_write()) and
* calculates from input fragment starting position.
*
* Offset examples can be considered from net_nbuf_write() api.
* If the offset is more than already allocated fragments length then it is an
* error case.
*
* @param buf Network buffer fragment list.
* @param frag Network buffer fragment.
* @param offset Offset of fragment where insertion will start.
* @param len Length of the data to be inserted.
* @param data Data to be inserted
* @param timeout Affects the action taken should the net buf pool be empty.
* If K_NO_WAIT, then return immediately. If K_FOREVER, then
* wait as long as necessary. Otherwise, wait up to the specified
* number of milliseconds before timing out.
*
* @return True on success,
* False otherwise.
*/
bool net_nbuf_insert(struct net_buf *buf, struct net_buf *frag,
uint16_t offset, uint16_t len, uint8_t *data,
int32_t timeout);
/* Insert uint8_t data at an arbitrary offset in a series of fragments. */
static inline bool net_nbuf_insert_u8(struct net_buf *buf,
struct net_buf *frag,
uint16_t offset,
uint8_t data)
{
return net_nbuf_insert(buf, frag, offset, sizeof(uint8_t), &data,
K_FOREVER);
}
/* Insert uint16_t big endian value at an arbitrary offset in a series of
* fragments.
*/
static inline bool net_nbuf_insert_be16(struct net_buf *buf,
struct net_buf *frag,
uint16_t offset,
uint16_t data)
{
uint16_t value = htons(data);
return net_nbuf_insert(buf, frag, offset, sizeof(uint16_t),
(uint8_t *)&value, K_FOREVER);
}
/* Insert uint32_t big endian value at an arbitrary offset in a series of
* fragments.
*/
static inline bool net_nbuf_insert_be32(struct net_buf *buf,
struct net_buf *frag,
uint16_t offset,
uint32_t data)
{
uint32_t value = htonl(data);
return net_nbuf_insert(buf, frag, offset, sizeof(uint32_t),
(uint8_t *)&value, K_FOREVER);
}
/**
* @brief Get information about available free buffer count in
* various network buffer pools. The amount of free buffers is
* only returned if network buffer debugging is enabled.
*
* @param tx_size Size of TX pool. Value is returned.
* @param rx_size Size of RX pool. Value is returned.
* @param data_size Size of DATA pool. Value is returned.
* @param tx Amount of free buffers in TX pool. Value is returned.
* @param rx Amount of free buffers in RX pool. Value is returned.
* @param data Amount of free buffers in DATA pool. Value is returned.
*/
void net_nbuf_get_info(size_t *tx_size, size_t *rx_size, size_t *data_size,
int *tx, int *rx, int *data);
#if defined(CONFIG_NET_DEBUG_NET_BUF)
/**
* @brief Debug helper to print out the buffer allocations
*/
void net_nbuf_print(void);
#else
#define net_nbuf_print(...)
#endif /* CONFIG_NET_DEBUG_NET_BUF */
#ifdef __cplusplus
}
#endif
#endif /* __NBUF_H */
|