2021-06-01 18:58:30 +02:00
/*
2024-06-26 15:40:32 +02:00
* SPDX - FileCopyrightText : 2021 - 2024 Espressif Systems ( Shanghai ) CO LTD
2021-06-01 18:58:30 +02:00
*
* SPDX - License - Identifier : Apache - 2.0
*/
# pragma once
2024-06-26 15:40:32 +02:00
# include <stdalign.h>
2021-06-01 18:58:30 +02:00
# include "esp_err.h"
2024-06-26 15:40:32 +02:00
2021-06-01 18:58:30 +02:00
# define L2TAP_VFS_DEFAULT_PATH " / dev / net / tap"
# define L2TAP_VFS_CONFIG_DEFAULT() \
{ \
2021-12-01 09:53:25 +01:00
. base_path = L2TAP_VFS_DEFAULT_PATH , \
2021-06-01 18:58:30 +02:00
}
# ifdef __cplusplus
extern " C " {
# endif
2021-12-01 09:53:25 +01:00
typedef void * l2tap_iodriver_handle ;
2022-05-11 15:52:09 +08:00
/**
* @ brief L2Tap VFS config parameters
*
*/
2021-06-01 18:58:30 +02:00
typedef struct {
2022-05-11 15:52:09 +08:00
const char * base_path ; /*!< vfs base path */
2021-06-01 18:58:30 +02:00
} l2tap_vfs_config_t ;
typedef enum {
2024-06-26 15:40:32 +02:00
L2TAP_S_RCV_FILTER , /*!< Set Ethertype filter, frames with this type to be passed to the file descriptor. */
L2TAP_G_RCV_FILTER , /*!< Get current Ethertype filter. */
L2TAP_S_INTF_DEVICE , /*!< Bound the file descriptor to a specific Network Interface is identified by its ``if_key``. */
L2TAP_G_INTF_DEVICE , /*!< Get the Network Interface ``if_key`` the file descriptor is bound to. */
L2TAP_S_DEVICE_DRV_HNDL , /*!< Bound the file descriptor to a specific Network Interface identified by IO Driver handle. */
L2TAP_G_DEVICE_DRV_HNDL , /*!< Get the Network Interface IO Driver handle the file descriptor is bound to. */
L2TAP_S_TIMESTAMP_EN , /*!< Enables the hardware Time Stamping (TS) processing by the file descriptor. TS needs to be supported by hardware and enabled in the IO driver. */
2021-06-01 18:58:30 +02:00
} l2tap_ioctl_opt_t ;
2024-06-26 15:40:32 +02:00
/**
* @ brief Information Record ( IREC ) Header Type indicates expected type of Header Data
*
*/
typedef enum {
L2TAP_IREC_INVALID = - 1 , /*!< Indicate invalid IREC (data is not valid)*/
L2TAP_IREC_TIME_STAMP = 1 /*!< To retrieve time stamp in `struct timespec` format */
} l2tap_irec_type_t ;
/**
* @ brief Information Record ( IREC )
*
*/
typedef struct
{
size_t len ; /*!< Length of the record including header and data*/
l2tap_irec_type_t type ; /*!< Type of the record */
alignas ( long long ) uint8_t data [ ] ; /*!< Records Data aligned to double word */
} l2tap_irec_hdr_t ;
/**
* @ brief Extended Buffer
*
* @ attention Use macros when allocating buffer for Information Records and when manipulating with data in the records
* to ensure proper memory alignment
*
*/
typedef struct {
size_t info_recs_len ; /*!< Length of Information Records buffer */
void * info_recs_buff ; /*!< Buffer holding extended information (IRECs) related to IO frames */
size_t buff_len ; /*!< Length of the actual IO Frame buffer */
void * buff ; /*!< Pointer to the IO Frame buffer */
} l2tap_extended_buff_t ;
/**
* @ brief Macros for operations with Information Records
*
* Align to double word size to each info record starts aligned in memory even if not aligned
* info data size is used by previous record . Double word alignment ( at 32 - bit arch ) is needed when accessing
* double word variables or structs containing double word variables .
*
*/
# define L2TAP_ALIGN(size) (((size) + sizeof(long long) - 1U) & ~(sizeof(long long) - 1U))
# define L2TAP_IREC_LEN(size) (sizeof(l2tap_irec_hdr_t) + size)
# define L2TAP_IREC_SPACE(size) (L2TAP_ALIGN(L2TAP_IREC_LEN(size)))
# define L2TAP_IREC_FIRST(ext_buff) (ext_buff)->info_recs_len >= sizeof(l2tap_irec_hdr_t) ? (l2tap_irec_hdr_t *)(ext_buff)->info_recs_buff : NULL
# define L2TAP_IREC_NEXT(ext_buff, curr_rec) (((curr_rec) == NULL) ? L2TAP_IREC_FIRST(ext_buff) : \
( uint8_t * ) ( curr_rec ) + L2TAP_ALIGN ( ( curr_rec ) - > len ) + L2TAP_ALIGN ( sizeof ( l2tap_irec_hdr_t ) ) > \
( uint8_t * ) ( ext_buff ) - > info_recs_buff + ( ext_buff ) - > info_recs_len ? \
NULL : \
( l2tap_irec_hdr_t * ) ( void * ) ( ( ( uint8_t * ) ( curr_rec ) + L2TAP_ALIGN ( ( curr_rec ) - > len ) ) ) )
2021-06-01 18:58:30 +02:00
/**
* @ brief Add L2 TAP virtual filesystem driver
*
* This function must be called prior usage of ESP - NETIF L2 TAP Interface
*
2024-06-26 15:40:32 +02:00
* @ param config L2 TAP virtual filesystem driver configuration . Default base path / dev / net / tap is used when this parameter is NULL .
2021-06-01 18:58:30 +02:00
* @ return esp_err_t
* - ESP_OK on success
*/
esp_err_t esp_vfs_l2tap_intf_register ( l2tap_vfs_config_t * config ) ;
/**
* @ brief Removes L2 TAP virtual filesystem driver
*
2024-06-26 15:40:32 +02:00
* @ param base_path Base path to the L2 TAP virtual filesystem driver . Default path / dev / net / tap is used when this parameter is NULL .
2021-06-01 18:58:30 +02:00
* @ return esp_err_t
* - ESP_OK on success
*/
esp_err_t esp_vfs_l2tap_intf_unregister ( const char * base_path ) ;
2021-12-01 09:53:25 +01:00
/**
* @ brief Filters received Ethernet L2 frames into L2 TAP infrastructure .
*
2022-05-11 15:52:09 +08:00
* @ param driver_handle handle of driver at which the frame was received
2021-12-01 09:53:25 +01:00
* @ param buff received L2 frame
* @ param size input length of the L2 frame which is set to 0 when frame is filtered into L2 TAP
2024-06-26 15:40:32 +02:00
* @ param info extra information about received Ethernet frame
2021-12-01 09:53:25 +01:00
* @ return esp_err_t
* - ESP_OK is always returned
*/
2024-06-26 15:40:32 +02:00
esp_err_t esp_vfs_l2tap_eth_filter_frame ( l2tap_iodriver_handle driver_handle , void * buff , size_t * size , void * info ) ;
/**
* @ brief Wrapper over L2 TAP filter function to ensure backward compatibility .
*
* This macro is provided for backward compatibility with the original ` esp_vfs_l2tap_eth_filter ` function .
* It calls ` esp_vfs_l2tap_eth_filter_frame ( ) ` with the ` info ` parameter set to ` NULL ` , which means
* L2 TAP features that depend on extra information about the received Ethernet frame ( e . g . , timestamps )
* will not work as expected .
*
* @ note For new implementations , it is recommended to use ` esp_vfs_l2tap_eth_filter_frame ( ) ` directly to
* take advantage of the extended functionality .
*/
# define esp_vfs_l2tap_eth_filter(drv_hndl, buf, size) esp_vfs_l2tap_eth_filter_frame(drv_hndl, buf, size, NULL)
2021-12-01 09:53:25 +01:00
2021-06-01 18:58:30 +02:00
# ifdef __cplusplus
}
# endif