Merge branch 'feature/sockets_files_shared_fd_space' into 'master'

lwip & vfs: POSIX I/O functions operate on sockets and files (first stage, no select() yet)

See merge request !1352
This commit is contained in:
Angus Gratton 2017-10-17 14:17:09 +08:00
commit 90bf40587e
16 changed files with 254 additions and 53 deletions

View file

@ -3139,13 +3139,14 @@ static void lwip_socket_drop_registered_memberships(int s)
(default initialization is to 0) */
int sa = s + 1;
int i;
struct lwip_sock *sock = get_socket(s);
LWIP_ASSERT("socket has no netconn", sockets[s].conn != NULL);
LWIP_ASSERT("socket has no netconn", sock->conn != NULL);
for (i = 0; i < LWIP_SOCKET_MAX_MEMBERSHIPS; i++) {
if (socket_multicast_memberships[i].sa == sa) {
socket_multicast_memberships[i].sa = 0;
netconn_join_leave_group(sockets[s].conn,
netconn_join_leave_group(sock->conn,
&socket_multicast_memberships[i].multi_addr,
&socket_multicast_memberships[i].if_addr,
NETCONN_LEAVE);

View file

@ -431,9 +431,9 @@ typedef struct ip_mreq {
/* Make FD_SETSIZE match NUM_SOCKETS in socket.c */
#define FD_SETSIZE MEMP_NUM_NETCONN
#define FDSETSAFESET(n, code) do { \
if (((n) - LWIP_SOCKET_OFFSET < MEMP_NUM_NETCONN) && (((int)(n) - LWIP_SOCKET_OFFSET) >= 0)) { \
if (n >= LWIP_SOCKET_OFFSET && ((n) - LWIP_SOCKET_OFFSET < MEMP_NUM_NETCONN) && (((int)(n) - LWIP_SOCKET_OFFSET) >= 0)) { \
code; }} while(0)
#define FDSETSAFEGET(n, code) (((n) - LWIP_SOCKET_OFFSET < MEMP_NUM_NETCONN) && (((int)(n) - LWIP_SOCKET_OFFSET) >= 0) ?\
#define FDSETSAFEGET(n, code) (n >= LWIP_SOCKET_OFFSET && ((n) - LWIP_SOCKET_OFFSET < MEMP_NUM_NETCONN) && (((int)(n) - LWIP_SOCKET_OFFSET) >= 0) ?\
(code) : 0)
#define FD_SET(n, p) FDSETSAFESET(n, (p)->fd_bits[((n)-LWIP_SOCKET_OFFSET)/8] |= (1 << (((n)-LWIP_SOCKET_OFFSET) & 7)))
#define FD_CLR(n, p) FDSETSAFESET(n, (p)->fd_bits[((n)-LWIP_SOCKET_OFFSET)/8] &= ~(1 << (((n)-LWIP_SOCKET_OFFSET) & 7)))

View file

@ -37,6 +37,7 @@
#include "freertos/task.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"
#include "arch/vfs_lwip.h"
#ifdef __cplusplus
extern "C" {

View file

@ -0,0 +1,28 @@
// Copyright 2017 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifdef __cplusplus
extern "C" {
#endif
/* Internal declarations used to ingreate LWIP port layer
to ESP-IDF VFS for POSIX I/O.
*/
extern unsigned lwip_socket_offset;
void esp_vfs_lwip_sockets_register();
#ifdef __cplusplus
}
#endif

View file

@ -34,6 +34,7 @@
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/fcntl.h>
#include "esp_task.h"
@ -687,6 +688,19 @@
#define ETHARP_TRUST_IP_MAC CONFIG_LWIP_ETHARP_TRUST_IP_MAC
/**
* POSIX I/O functions are mapped to LWIP via the VFS layer
* (see port/vfs_lwip.c)
*/
#define LWIP_POSIX_SOCKETS_IO_NAMES 0
/**
* Socket offset is also determined via the VFS layer at
* filesystem registration time (see port/vfs_lwip.c)
*/
#define LWIP_SOCKET_OFFSET lwip_socket_offset
/* Enable all Espressif-only options */
#define ESP_LWIP 1

View file

@ -433,6 +433,7 @@ sys_init(void)
if (ERR_OK != sys_mutex_new(&g_lwip_protect_mutex)) {
ESP_LOGE(TAG, "sys_init: failed to init lwip protect mutex\n");
}
esp_vfs_lwip_sockets_register();
}
/*-----------------------------------------------------------------------------------*/

View file

@ -0,0 +1,77 @@
// Copyright 2017 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <string.h>
#include <stdbool.h>
#include <stdarg.h>
#include <sys/errno.h>
#include <sys/lock.h>
#include <sys/fcntl.h>
#include "esp_vfs.h"
#include "esp_vfs_dev.h"
#include "esp_attr.h"
#include "soc/uart_struct.h"
#include "lwip/sockets.h"
#include "sdkconfig.h"
/* LWIP is a special case for VFS use.
From the LWIP side:
- We set LWIP_SOCKET_OFFSET dynamically at VFS registration time so that native LWIP socket functions & VFS functions
see the same fd space. This is necessary to mix POSIX file operations defined in VFS with POSIX socket operations defined
in LWIP, without needing to wrap all of them.
From the VFS side:
- ESP_VFS_FLAG_SHARED_FD_SPACE is set, so unlike other VFS implementations the FDs that the LWIP "VFS" sees and the
FDs that the user sees are the same FDs.
*/
unsigned lwip_socket_offset;
static int lwip_fcntl_r_wrapper(int fd, int cmd, va_list args);
static int lwip_ioctl_r_wrapper(int fd, int cmd, va_list args);
void esp_vfs_lwip_sockets_register()
{
esp_vfs_t vfs = {
.flags = ESP_VFS_FLAG_DEFAULT | ESP_VFS_FLAG_SHARED_FD_SPACE,
.write = &lwip_write_r,
.open = NULL,
.fstat = NULL,
.close = &lwip_close_r,
.read = &lwip_read_r,
.fcntl = &lwip_fcntl_r_wrapper,
.ioctl = &lwip_ioctl_r_wrapper,
};
unsigned max_fd;
ESP_ERROR_CHECK(esp_vfs_register_socket_space(&vfs, NULL, &lwip_socket_offset, &max_fd));
/* LWIP can't be allowed to create more sockets than fit in the per-VFS fd space. Currently this isn't configurable
* but it's set much larger than CONFIG_LWIP_MAX_SOCKETS should ever be (max 2^12 FDs).
*/
assert(CONFIG_LWIP_MAX_SOCKETS <= max_fd - lwip_socket_offset);
}
static int lwip_fcntl_r_wrapper(int fd, int cmd, va_list args)
{
return lwip_fcntl_r(fd, cmd, va_arg(args, int));
}
static int lwip_ioctl_r_wrapper(int fd, int cmd, va_list args)
{
return lwip_ioctl_r(fd, cmd, va_arg(args, void *));
}

Binary file not shown.

Binary file not shown.

View file

@ -77,6 +77,7 @@ lib_a-close.o
lib_a-creat.o
lib_a-environ.o
lib_a-fclose.o
lib_a-fcntlr.o
lib_a-isalnum.o
lib_a-isalpha.o
lib_a-isascii.o
@ -131,6 +132,7 @@ lib_a-strupr.o
lib_a-stdio.o
lib_a-syssbrk.o
lib_a-sysclose.o
lib_a-sysfcntl.o
lib_a-sysopen.o
creat.o
lib_a-sysread.o

View file

@ -22,7 +22,6 @@ To register an FS driver, application needs to define in instance of esp_vfs_t s
::
esp_vfs_t myfs = {
.fd_offset = 0,
.flags = ESP_VFS_FLAG_DEFAULT,
.write = &myfs_write,
.open = &myfs_open,
@ -43,7 +42,7 @@ Case 1: API functions are declared without an extra context pointer (FS driver i
.flags = ESP_VFS_FLAG_DEFAULT,
.write = &myfs_write,
// ... other members initialized
// When registering FS, context pointer (third argument) is NULL:
ESP_ERROR_CHECK(esp_vfs_register("/data", &myfs, NULL));
@ -55,7 +54,7 @@ Case 2: API functions are declared with an extra context pointer (FS driver supp
.flags = ESP_VFS_FLAG_CONTEXT_PTR,
.write_p = &myfs_write,
// ... other members initialized
// When registering FS, pass the FS context pointer into the third argument
// (hypothetical myfs_mount function is used for illustrative purposes)
myfs_t* myfs_inst1 = myfs_mount(partition1->offset, partition1->size);
@ -100,37 +99,34 @@ File descriptors
It is suggested that filesystem drivers should use small positive integers as file descriptors. VFS component assumes that ``CONFIG_MAX_FD_BITS`` bits (12 by default) are sufficient to represent a file descriptor.
If filesystem is configured with an option to offset all file descriptors by a constant value, such value should be passed to ``fd_offset`` field of ``esp_vfs_t`` structure. VFS component will then remove this offset when working with FDs of that specific FS, bringing them into the range of small positive integers.
While file descriptors returned by VFS component to newlib library are rarely seen by the application, the following details may be useful for debugging purposes. File descriptors returned by VFS component are composed of two parts: FS driver ID, and the actual file descriptor. Because newlib stores file descriptors as 16-bit integers, VFS component is also limited by 16 bits to store both parts.
While file descriptors returned by VFS component to newlib library are rarely seen by the application, the following details may be useful for debugging purposes. File descriptors returned by VFS component are composed of two parts: FS driver ID, and the actual file descriptor. Because newlib stores file descriptors as 16-bit integers, VFS component is also limited by 16 bits to store both parts.
Lower ``CONFIG_MAX_FD_BITS`` bits are used to store zero-based file descriptor. The per-filesystem FD obtained from the FS ``open`` call, and this result is stored in the lower bits of the FD. Higher bits are used to save the index of FS in the internal table of registered filesystems.
Lower ``CONFIG_MAX_FD_BITS`` bits are used to store zero-based file descriptor. If FS driver has a non-zero ``fd_offset`` field, this ``fd_offset`` is subtracted FDs obtained from the FS ``open`` call, and the result is stored in the lower bits of the FD. Higher bits are used to save the index of FS in the internal table of registered filesystems.
When VFS component receives a call from newlib which has a file descriptor, this file descriptor is translated back to the FS-specific file descriptor. First, higher bits of FD are used to identify the FS. Then ``fd_offset`` field of the FS is added to the lower ``CONFIG_MAX_FD_BITS`` bits of the fd, and resulting FD is passed to the FS driver.
When VFS component receives a call from newlib which has a file descriptor, this file descriptor is translated back to the FS-specific file descriptor. First, higher bits of FD are used to identify the FS. Then only the lower ``CONFIG_MAX_FD_BITS`` bits of the fd are masked in, and resulting FD is passed to the FS driver.
.. highlight:: none
::
FD as seen by newlib FD as seen by FS driver
+-----+
+-------+---------------+ | | +------------------------+
| FS id | Zero—based FD | +---------------> sum +----> |
+---+---+------+--------+ | | | +------------------------+
| | | +--^--+
| +--------------+ |
| |
| +-------------+ |
| | Table of | |
| | registered | |
| | filesystems | |
| +-------------+ +-------------+ |
+-------> entry +----> esp_vfs_t | |
index +-------------+ | structure | |
| | | | |
| | | + fd_offset +---+
+-------------+ | |
+-------------+
+-------+---------------+ +------------------------+
| FS id | Zero—based FD | +--------------------------> |
+---+---+------+--------+ | +------------------------+
| | |
| +--------------+
|
| +-------------+
| | Table of |
| | registered |
| | filesystems |
| +-------------+ +-------------+
+-------> entry +----> esp_vfs_t |
index +-------------+ | structure |
| | | |
| | | |
+-------------+ +-------------+
Standard IO streams (stdin, stdout, stderr)
@ -170,4 +166,3 @@ Such a design has the following consequences:
- It is possible to set ``stdin``, ``stdout``, and ``stderr`` for any given task without affecting other tasks, e.g. by doing ``stdin = fopen("/dev/uart/1", "r")``.
- Closing default ``stdin``, ``stdout``, or ``stderr`` using ``fclose`` will close the ``FILE`` stream object — this will affect all other tasks.
- To change the default ``stdin``, ``stdout``, ``stderr`` streams for new tasks, modify ``_GLOBAL_REENT->_stdin`` (``_stdout``, ``_stderr``) before creating the task.

View file

@ -43,16 +43,30 @@ extern "C" {
*/
#define ESP_VFS_FLAG_CONTEXT_PTR 1
/**
* Flag which indicates that the FD space of the VFS implementation should be made
* the same as the FD space in newlib. This means that the normal masking off
* of VFS-independent fd bits is ignored and the full user-facing fd is passed to
* the VFS implementation.
*
* Set the p_minimum_fd & p_maximum_fd pointers when registering the socket in
* order to know what range of FDs can be used with the registered VFS.
*
* This is mostly useful for LWIP which shares the socket FD space with
* socket-specific functions.
*
*/
#define ESP_VFS_FLAG_SHARED_FD_SPACE 2
/**
* @brief VFS definition structure
*
* This structure should be filled with pointers to corresponding
* FS driver functions.
*
* If the FS implementation has an option to use certain offset for
* all file descriptors, this value should be passed into fd_offset
* field. Otherwise VFS component will translate all FDs to start
* at zero offset.
* VFS component will translate all FDs so that the filesystem implementation
* sees them starting at zero. The caller sees a global FD which is prefixed
* with an pre-filesystem-implementation.
*
* Some FS implementations expect some state (e.g. pointer to some structure)
* to be passed in as a first argument. For these implementations,
@ -67,8 +81,7 @@ extern "C" {
*/
typedef struct
{
int fd_offset; /*!< file descriptor offset, determined by the FS driver */
int flags; /*!< ESP_VFS_FLAG_CONTEXT_PTR or ESP_VFS_FLAG_DEFAULT */
int flags; /*!< ESP_VFS_FLAG_CONTEXT_PTR or ESP_VFS_FLAG_DEFAULT, plus optionally ESP_VFS_FLAG_SHARED_FD_SPACE */
union {
ssize_t (*write_p)(void* p, int fd, const void * data, size_t size);
ssize_t (*write)(int fd, const void * data, size_t size);
@ -145,6 +158,10 @@ typedef struct
int (*fcntl_p)(void* ctx, int fd, int cmd, va_list args);
int (*fcntl)(int fd, int cmd, va_list args);
};
union {
int (*ioctl_p)(void* ctx, int fd, int cmd, va_list args);
int (*ioctl)(int fd, int cmd, va_list args);
};
} esp_vfs_t;
@ -170,6 +187,22 @@ typedef struct
esp_err_t esp_vfs_register(const char* base_path, const esp_vfs_t* vfs, void* ctx);
/**
* Special case function for registering a VFS that uses a method other than
* open() to open new file descriptors.
*
* This is a special-purpose function intended for registering LWIP sockets to VFS.
*
* @param vfs Pointer to esp_vfs_t. Meaning is the same as for esp_vfs_register().
* @param ctx Pointer to context structure. Meaning is the same as for esp_vfs_register().
* @param p_min_fd If non-NULL, on success this variable is written with the minimum (global/user-facing) FD that this VFS will use. This is useful when ESP_VFS_FLAG_SHARED_FD_SPACE is set in vfs->flags.
* @param p_max_fd If non-NULL, on success this variable is written with one higher than the maximum (global/user-facing) FD that this VFS will use. This is useful when ESP_VFS_FLAG_SHARED_FD_SPACE is set in vfs->flags.
*
* @return ESP_OK if successful, ESP_ERR_NO_MEM if too many VFSes are
* registered.
*/
esp_err_t esp_vfs_register_socket_space(const esp_vfs_t *vfs, void *ctx, unsigned *p_min_fd, unsigned *p_max_fd);
/**
* Unregister a virtual filesystem for given path prefix
*

View file

@ -12,11 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef __ESP_VFS_DEV_H__
#define __ESP_VFS_DEV_H__
#pragma once
#include "esp_vfs.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Line ending settings
*/
@ -81,4 +84,6 @@ void esp_vfs_dev_uart_use_nonblocking(int uart_num);
*/
void esp_vfs_dev_uart_use_driver(int uart_num);
#endif //__ESP_VFS_DEV_H__
#ifdef __cplusplus
}
#endif

View file

@ -41,6 +41,8 @@
#define VFS_INDEX_MASK (VFS_MAX_COUNT << CONFIG_MAX_FD_BITS)
#define VFS_INDEX_S CONFIG_MAX_FD_BITS
#define LEN_PATH_PREFIX_IGNORED SIZE_MAX /* special length value for VFS which is never recognised by open() */
typedef struct vfs_entry_ {
esp_vfs_t vfs; // contains pointers to VFS functions
char path_prefix[ESP_VFS_PATH_MAX]; // path prefix mapped to this VFS
@ -52,14 +54,15 @@ typedef struct vfs_entry_ {
static vfs_entry_t* s_vfs[VFS_MAX_COUNT] = { 0 };
static size_t s_vfs_count = 0;
esp_err_t esp_vfs_register(const char* base_path, const esp_vfs_t* vfs, void* ctx)
static esp_err_t esp_vfs_register_common(const char* base_path, size_t len, const esp_vfs_t* vfs, void* ctx, unsigned *p_minimum_fd, unsigned *p_maximum_fd)
{
size_t len = strlen(base_path);
if ((len != 0 && len < 2)|| len > ESP_VFS_PATH_MAX) {
return ESP_ERR_INVALID_ARG;
}
if ((len > 0 && base_path[0] != '/') || base_path[len - 1] == '/') {
return ESP_ERR_INVALID_ARG;
if (len != LEN_PATH_PREFIX_IGNORED) {
if ((len != 0 && len < 2) || (len > ESP_VFS_PATH_MAX)) {
return ESP_ERR_INVALID_ARG;
}
if ((len > 0 && base_path[0] != '/') || base_path[len - 1] == '/') {
return ESP_ERR_INVALID_ARG;
}
}
vfs_entry_t *entry = (vfs_entry_t*) malloc(sizeof(vfs_entry_t));
if (entry == NULL) {
@ -79,14 +82,36 @@ esp_err_t esp_vfs_register(const char* base_path, const esp_vfs_t* vfs, void* ct
++s_vfs_count;
}
s_vfs[index] = entry;
strcpy(entry->path_prefix, base_path); // we have already verified argument length
if (len != LEN_PATH_PREFIX_IGNORED) {
strcpy(entry->path_prefix, base_path); // we have already verified argument length
} else {
bzero(entry->path_prefix, sizeof(entry->path_prefix));
}
memcpy(&entry->vfs, vfs, sizeof(esp_vfs_t));
entry->path_prefix_len = len;
entry->ctx = ctx;
entry->offset = index;
if (p_minimum_fd != NULL) {
*p_minimum_fd = index << VFS_INDEX_S;
}
if (p_maximum_fd != NULL) {
*p_maximum_fd = (index + 1) << VFS_INDEX_S;
}
return ESP_OK;
}
esp_err_t esp_vfs_register(const char* base_path, const esp_vfs_t* vfs, void* ctx)
{
return esp_vfs_register_common(base_path, strlen(base_path), vfs, ctx, NULL, NULL);
}
esp_err_t esp_vfs_register_socket_space(const esp_vfs_t *vfs, void *ctx, unsigned *p_min_fd, unsigned *p_max_fd)
{
return esp_vfs_register_common("", LEN_PATH_PREFIX_IGNORED, vfs, ctx, p_min_fd, p_max_fd);
}
esp_err_t esp_vfs_unregister(const char* base_path)
{
for (size_t i = 0; i < s_vfs_count; ++i) {
@ -114,7 +139,11 @@ static const vfs_entry_t* get_vfs_for_fd(int fd)
static int translate_fd(const vfs_entry_t* vfs, int fd)
{
return (fd & VFS_FD_MASK) + vfs->vfs.fd_offset;
if (vfs->vfs.flags & ESP_VFS_FLAG_SHARED_FD_SPACE) {
return fd;
} else {
return fd & VFS_FD_MASK;
}
}
static const char* translate_path(const vfs_entry_t* vfs, const char* src_path)
@ -134,7 +163,7 @@ static const vfs_entry_t* get_vfs_for_path(const char* path)
size_t len = strlen(path);
for (size_t i = 0; i < s_vfs_count; ++i) {
const vfs_entry_t* vfs = s_vfs[i];
if (!vfs) {
if (!vfs || vfs->path_prefix_len == LEN_PATH_PREFIX_IGNORED) {
continue;
}
// match path prefix
@ -226,8 +255,7 @@ int esp_vfs_open(struct _reent *r, const char * path, int flags, int mode)
if (ret < 0) {
return ret;
}
assert(ret >= vfs->vfs.fd_offset);
return ret - vfs->vfs.fd_offset + (vfs->offset << VFS_INDEX_S);
return ret + (vfs->offset << VFS_INDEX_S);
}
ssize_t esp_vfs_write(struct _reent *r, int fd, const void * data, size_t size)
@ -489,3 +517,20 @@ int fcntl(int fd, int cmd, ...)
va_end(args);
return ret;
}
int ioctl(int fd, int cmd, ...)
{
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
struct _reent* r = __getreent();
if (vfs == NULL) {
__errno_r(r) = EBADF;
return -1;
}
int local_fd = translate_fd(vfs, fd);
int ret;
va_list args;
va_start(args, cmd);
CHECK_AND_CALL(ret, r, vfs, ioctl, local_fd, cmd, args);
va_end(args);
return ret;
}

View file

@ -263,7 +263,6 @@ static int uart_fcntl(int fd, int cmd, va_list args)
void esp_vfs_dev_uart_register()
{
esp_vfs_t vfs = {
.fd_offset = 0,
.flags = ESP_VFS_FLAG_DEFAULT,
.write = &uart_write,
.open = &uart_open,