LCOV - code coverage report
Current view: top level - zephyr/net - lldp.h Coverage Total Hit
Test: new.info Lines: 100.0 % 23 23
Test Date: 2025-09-25 19:22:35

            Line data    Source code
       1            1 : /** @file
       2              :  @brief LLDP definitions and handler
       3              : 
       4              :  This is not to be included by the application.
       5              :  */
       6              : 
       7              : /*
       8              :  * Copyright (c) 2017 Intel Corporation
       9              :  *
      10              :  * SPDX-License-Identifier: Apache-2.0
      11              :  */
      12              : 
      13              : #ifndef ZEPHYR_INCLUDE_NET_LLDP_H_
      14              : #define ZEPHYR_INCLUDE_NET_LLDP_H_
      15              : 
      16              : /**
      17              :  * @brief LLDP definitions and helpers
      18              :  * @defgroup lldp Link Layer Discovery Protocol definitions and helpers
      19              :  * @since 1.13
      20              :  * @version 0.8.0
      21              :  * @ingroup networking
      22              :  * @{
      23              :  */
      24              : 
      25              : #include <zephyr/net/net_if.h>
      26              : #include <zephyr/net/net_pkt.h>
      27              : 
      28              : #ifdef __cplusplus
      29              : extern "C" {
      30              : #endif
      31              : 
      32              : /** @cond INTERNAL_HIDDEN */
      33              : 
      34              : #define LLDP_TLV_GET_LENGTH(type_length)        (type_length & BIT_MASK(9))
      35              : #define LLDP_TLV_GET_TYPE(type_length)          ((uint8_t)(type_length >> 9))
      36              : 
      37              : /* LLDP Definitions */
      38              : 
      39              : /* According to the spec, End of LLDPDU TLV value is constant. */
      40              : #define NET_LLDP_END_LLDPDU_VALUE 0x0000
      41              : 
      42              : /*
      43              :  * For the Chassis ID TLV Value, if subtype is a MAC address then we must
      44              :  * use values from CONFIG_NET_LLDP_CHASSIS_ID_MAC0 through
      45              :  * CONFIG_NET_LLDP_CHASSIS_ID_MAC5. If not, we use CONFIG_NET_LLDP_CHASSIS_ID.
      46              :  *
      47              :  * FIXME: implement a similar scheme for subtype 5 (network address).
      48              :  */
      49              : #if defined(CONFIG_NET_LLDP_CHASSIS_ID_SUBTYPE)
      50              : #if (CONFIG_NET_LLDP_CHASSIS_ID_SUBTYPE == 4)
      51              : #define NET_LLDP_CHASSIS_ID_VALUE               \
      52              :         {                                       \
      53              :           CONFIG_NET_LLDP_CHASSIS_ID_MAC0,      \
      54              :           CONFIG_NET_LLDP_CHASSIS_ID_MAC1,      \
      55              :           CONFIG_NET_LLDP_CHASSIS_ID_MAC2,      \
      56              :           CONFIG_NET_LLDP_CHASSIS_ID_MAC3,      \
      57              :           CONFIG_NET_LLDP_CHASSIS_ID_MAC4,      \
      58              :           CONFIG_NET_LLDP_CHASSIS_ID_MAC5       \
      59              :         }
      60              : 
      61              : #define NET_LLDP_CHASSIS_ID_VALUE_LEN (6)
      62              : #else
      63              : #define NET_LLDP_CHASSIS_ID_VALUE CONFIG_NET_LLDP_CHASSIS_ID
      64              : #define NET_LLDP_CHASSIS_ID_VALUE_LEN (sizeof(CONFIG_NET_LLDP_CHASSIS_ID) - 1)
      65              : #endif
      66              : #else
      67              : #define NET_LLDP_CHASSIS_ID_VALUE 0
      68              : #define NET_LLDP_CHASSIS_ID_VALUE_LEN 0
      69              : #endif
      70              : 
      71              : /*
      72              :  * For the Port ID TLV Value, if subtype is a MAC address then we must
      73              :  * use values from CONFIG_NET_LLDP_PORT_ID_MAC0 through
      74              :  * CONFIG_NET_LLDP_PORT_ID_MAC5. If not, we use CONFIG_NET_LLDP_PORT_ID.
      75              :  *
      76              :  * FIXME: implement a similar scheme for subtype 4 (network address).
      77              :  */
      78              : #if defined(CONFIG_NET_LLDP_PORT_ID_SUBTYPE)
      79              : #if (CONFIG_NET_LLDP_PORT_ID_SUBTYPE == 3)
      80              : #define NET_LLDP_PORT_ID_VALUE          \
      81              :         {                               \
      82              :           CONFIG_NET_LLDP_PORT_ID_MAC0, \
      83              :           CONFIG_NET_LLDP_PORT_ID_MAC1, \
      84              :           CONFIG_NET_LLDP_PORT_ID_MAC2, \
      85              :           CONFIG_NET_LLDP_PORT_ID_MAC3, \
      86              :           CONFIG_NET_LLDP_PORT_ID_MAC4, \
      87              :           CONFIG_NET_LLDP_PORT_ID_MAC5  \
      88              :         }
      89              : 
      90              : #define NET_LLDP_PORT_ID_VALUE_LEN (6)
      91              : #else
      92              : #define NET_LLDP_PORT_ID_VALUE CONFIG_NET_LLDP_PORT_ID
      93              : #define NET_LLDP_PORT_ID_VALUE_LEN (sizeof(CONFIG_NET_LLDP_PORT_ID) - 1)
      94              : #endif
      95              : #else
      96              : #define NET_LLDP_PORT_ID_VALUE 0
      97              : #define NET_LLDP_PORT_ID_VALUE_LEN 0
      98              : #endif
      99              : 
     100              : /*
     101              :  * TLVs Length.
     102              :  * Note that TLVs that have a subtype must have a byte added to their length.
     103              :  */
     104              : #define NET_LLDP_CHASSIS_ID_TLV_LEN (NET_LLDP_CHASSIS_ID_VALUE_LEN + 1)
     105              : #define NET_LLDP_PORT_ID_TLV_LEN (NET_LLDP_PORT_ID_VALUE_LEN + 1)
     106              : #define NET_LLDP_TTL_TLV_LEN (2)
     107              : 
     108              : /*
     109              :  * Time to Live value.
     110              :  * Calculate based on section 9.2.5.22 from LLDP spec.
     111              :  *
     112              :  * FIXME: when the network interface is about to be ‘disabled’ TTL shall be set
     113              :  * to zero so LLDP Rx agents can invalidate the entry related to this node.
     114              :  */
     115              : #if defined(CONFIG_NET_LLDP_TX_INTERVAL) && defined(CONFIG_NET_LLDP_TX_HOLD)
     116              : #define NET_LLDP_TTL \
     117              :         MIN((CONFIG_NET_LLDP_TX_INTERVAL * CONFIG_NET_LLDP_TX_HOLD) + 1, 65535)
     118              : #endif
     119              : 
     120              : 
     121              : struct net_if;
     122              : 
     123              : /** @endcond */
     124              : 
     125              : /** TLV Types. Please refer to table 8-1 from IEEE 802.1AB standard. */
     126            1 : enum net_lldp_tlv_type {
     127              :         LLDP_TLV_END_LLDPDU          = 0, /**< End Of LLDPDU (optional)      */
     128              :         LLDP_TLV_CHASSIS_ID          = 1, /**< Chassis ID (mandatory)        */
     129              :         LLDP_TLV_PORT_ID             = 2, /**< Port ID (mandatory)           */
     130              :         LLDP_TLV_TTL                 = 3, /**< Time To Live (mandatory)      */
     131              :         LLDP_TLV_PORT_DESC           = 4, /**< Port Description (optional)   */
     132              :         LLDP_TLV_SYSTEM_NAME         = 5, /**< System Name (optional)        */
     133              :         LLDP_TLV_SYSTEM_DESC         = 6, /**< System Description (optional) */
     134              :         LLDP_TLV_SYSTEM_CAPABILITIES = 7, /**< System Capability (optional)  */
     135              :         LLDP_TLV_MANAGEMENT_ADDR     = 8, /**< Management Address (optional) */
     136              :         /* Types 9 - 126 are reserved. */
     137              :         LLDP_TLV_ORG_SPECIFIC       = 127, /**< Org specific TLVs (optional) */
     138              : };
     139              : 
     140              : /** Chassis ID TLV, see chapter 8.5.2 in IEEE 802.1AB */
     141            1 : struct net_lldp_chassis_tlv {
     142              :         /** 7 bits for type, 9 bits for length */
     143            1 :         uint16_t type_length;
     144              :         /** ID subtype */
     145            1 :         uint8_t subtype;
     146              :         /** Chassis ID value */
     147            1 :         uint8_t value[NET_LLDP_CHASSIS_ID_VALUE_LEN];
     148              : } __packed;
     149              : 
     150              : /** Port ID TLV, see chapter 8.5.3 in IEEE 802.1AB */
     151            1 : struct net_lldp_port_tlv {
     152              :         /** 7 bits for type, 9 bits for length */
     153            1 :         uint16_t type_length;
     154              :         /** ID subtype */
     155            1 :         uint8_t subtype;
     156              :         /** Port ID value */
     157            1 :         uint8_t value[NET_LLDP_PORT_ID_VALUE_LEN];
     158              : } __packed;
     159              : 
     160              : /** Time To Live TLV, see chapter 8.5.4 in IEEE 802.1AB */
     161            1 : struct net_lldp_time_to_live_tlv {
     162              :         /** 7 bits for type, 9 bits for length */
     163            1 :         uint16_t type_length;
     164              :         /** Time To Live (TTL) value */
     165            1 :         uint16_t ttl;
     166              : } __packed;
     167              : 
     168              : /**
     169              :  * LLDP Data Unit (LLDPDU) shall contain the following ordered TLVs
     170              :  * as stated in "8.2 LLDPDU format" from the IEEE 802.1AB
     171              :  */
     172            1 : struct net_lldpdu {
     173            1 :         struct net_lldp_chassis_tlv chassis_id; /**< Mandatory Chassis TLV */
     174            1 :         struct net_lldp_port_tlv port_id;       /**< Mandatory Port TLV */
     175            1 :         struct net_lldp_time_to_live_tlv ttl;   /**< Mandatory TTL TLV */
     176              : } __packed;
     177              : 
     178              : /**
     179              :  * @brief Set the LLDP data unit for a network interface.
     180              :  *
     181              :  * @param iface Network interface
     182              :  * @param lldpdu LLDP data unit struct
     183              :  *
     184              :  * @return 0 if ok, <0 if error
     185              :  */
     186            1 : int net_lldp_config(struct net_if *iface, const struct net_lldpdu *lldpdu);
     187              : 
     188              : /**
     189              :  * @brief Set the Optional LLDP TLVs for a network interface.
     190              :  *
     191              :  * @param iface Network interface
     192              :  * @param tlv LLDP optional TLVs following mandatory part
     193              :  * @param len Length of the optional TLVs
     194              :  *
     195              :  * @return 0 if ok, <0 if error
     196              :  */
     197            1 : int net_lldp_config_optional(struct net_if *iface, const uint8_t *tlv,
     198              :                              size_t len);
     199              : 
     200              : /**
     201              :  * @brief Initialize LLDP engine.
     202              :  */
     203            1 : void net_lldp_init(void);
     204              : 
     205              : /**
     206              :  * @brief LLDP Receive packet callback
     207              :  *
     208              :  * Callback gets called upon receiving packet. It is responsible for
     209              :  * freeing packet or indicating to the stack that it needs to free packet
     210              :  * by returning correct net_verdict.
     211              :  *
     212              :  * Returns:
     213              :  *  - NET_DROP, if packet was invalid, rejected or we want the stack to free it.
     214              :  *    In this case the core stack will free the packet.
     215              :  *  - NET_OK, if the packet was accepted, in this case the ownership of the
     216              :  *    net_pkt goes to callback and core network stack will forget it.
     217              :  */
     218              : typedef enum net_verdict (*net_lldp_recv_cb_t)(struct net_if *iface,
     219              :                                                struct net_pkt *pkt);
     220              : 
     221              : /**
     222              :  * @brief Register LLDP Rx callback function
     223              :  *
     224              :  * @param iface Network interface
     225              :  * @param cb Callback function
     226              :  *
     227              :  * @return 0 if ok, < 0 if error
     228              :  */
     229            1 : int net_lldp_register_callback(struct net_if *iface, net_lldp_recv_cb_t cb);
     230              : 
     231              : /**
     232              :  * @brief Set LLDP protocol data unit (LLDPDU) for the network interface.
     233              :  *
     234              :  * @param iface Network interface
     235              :  *
     236              :  * @return <0 if error, index in lldp array if iface is found there
     237              :  */
     238              : #if defined(CONFIG_NET_LLDP)
     239              : int net_lldp_set_lldpdu(struct net_if *iface);
     240              : #else
     241            1 : #define net_lldp_set_lldpdu(iface)
     242              : #endif
     243              : 
     244              : /**
     245              :  * @brief Unset LLDP protocol data unit (LLDPDU) for the network interface.
     246              :  *
     247              :  * @param iface Network interface
     248              :  */
     249              : #if defined(CONFIG_NET_LLDP)
     250              : void net_lldp_unset_lldpdu(struct net_if *iface);
     251              : #else
     252            1 : #define net_lldp_unset_lldpdu(iface)
     253              : #endif
     254              : 
     255              : #ifdef __cplusplus
     256              : }
     257              : #endif
     258              : 
     259              : /**
     260              :  * @}
     261              :  */
     262              : 
     263              : #endif /* ZEPHYR_INCLUDE_NET_LLDP_H_ */
        

Generated by: LCOV version 2.0-1