1
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
36
37
41
42
43
44
45
46
47
48
49
53
54
55
56
57
58
62
67
68
69
70
74
75
76
77
81
82
83
84
85
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
135
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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
183
187
193
194
195
196
197
198
199
200
201
202
203
204
205
206
209
210
211
219
220
221
222
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/* ... */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <errno.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_event.h"
#include "esp_err.h"
#include "esp_eth.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "sdkconfig.h"
#include "esp_vfs_l2tap.h"
#include "lwip/prot/ethernet.h"
#include "arpa/inet.h"
#include "protocol_examples_common.h"18 includes
#if !defined(CONFIG_EXAMPLE_CONNECT_ETHERNET)
#error Ethernet interface is not configured to connect.
#endif
#define ETH_INTERFACE "ETH_DEF"
#define ETH_TYPE_FILTER_BLOCK 0x2220
#define ETH_TYPE_FILTER_NOBLOCK 0x2221
#define ETH_TYPE_FILTER_TX 0x2223
#define INVALID_FD -15 defines
typedef struct {
struct eth_hdr header;
char payload[44];
}{ ... } test_vfs_eth_tap_msg_t;
static const char *TAG = "l2tap_example";
static int init_l2tap_fd(int flags, uint16_t eth_type_filter)
{
int fd = open("/dev/net/tap", flags);
if (fd < 0) {
ESP_LOGE(TAG, "Unable to open L2 TAP interface: errno %d", errno);
goto error;
}{...}
ESP_LOGI(TAG, "/dev/net/tap fd %d successfully opened", fd);
flags = 0;
flags = fcntl(fd, F_GETFL);
if (flags == -1) {
ESP_LOGE(TAG, "Unable to get L2 TAP fd %d status flag: errno %d", fd, errno);
goto error;
}{...}
if (flags & O_NONBLOCK) {
ESP_LOGI(TAG, "L2 TAP fd %d configured in non-blocking mode", fd);
}{...} else {
ESP_LOGI(TAG, "L2 TAP fd %d configured in blocking mode", fd);
}{...}
int ret;
if ((ret = ioctl(fd, L2TAP_S_INTF_DEVICE, ETH_INTERFACE)) == -1) {
ESP_LOGE(TAG, "Unable to bound L2 TAP fd %d with Ethernet device: errno %d", fd, errno);
goto error;
}{...}
ESP_LOGI(TAG, "L2 TAP fd %d successfully bound to `%s`", fd, ETH_INTERFACE);
if ((ret = ioctl(fd, L2TAP_S_RCV_FILTER, ð_type_filter)) == -1) {
ESP_LOGE(TAG, "Unable to configure fd %d Ethernet type receive filter: errno %d", fd, errno);
goto error;
}{...}
ESP_LOGI(TAG, "L2 TAP fd %d Ethernet type filter configured to 0x%x", fd, eth_type_filter);
return fd;
error:
if (fd != INVALID_FD) {
close(fd);
}{...}
return INVALID_FD;
}{ ... }
static void create_echo_frame(test_vfs_eth_tap_msg_t *in_frame, test_vfs_eth_tap_msg_t *out_frame, int len)
{
esp_eth_handle_t eth_hndl = get_example_eth_handle();
uint8_t mac_addr[ETH_ADDR_LEN];
esp_eth_ioctl(eth_hndl, ETH_CMD_G_MAC_ADDR, mac_addr);
memcpy(out_frame->header.src.addr, mac_addr, ETH_ADDR_LEN);
memcpy(out_frame->header.dest.addr, in_frame->header.src.addr, ETH_ADDR_LEN);
memcpy(&out_frame->header.type, &in_frame->header.type, sizeof(uint16_t));
memcpy(out_frame->payload, in_frame->payload, len - ETH_HEADER_LEN);
}{ ... }
static void echo_l2tap_task(void *pvParameters)
{
uint8_t rx_buffer[128];
int eth_tap_fd;
if ((eth_tap_fd = init_l2tap_fd(0, ETH_TYPE_FILTER_BLOCK)) == INVALID_FD) {
goto error;
}{...}
while (1) {
ssize_t len = read(eth_tap_fd, rx_buffer, sizeof(rx_buffer));
if (len > 0) {
test_vfs_eth_tap_msg_t *recv_msg = (test_vfs_eth_tap_msg_t *)rx_buffer;
ESP_LOGI(TAG, "fd %d received %d bytes from %.2x:%.2x:%.2x:%.2x:%.2x:%.2x", eth_tap_fd,
len, recv_msg->header.src.addr[0], recv_msg->header.src.addr[1], recv_msg->header.src.addr[2],
recv_msg->header.src.addr[3], recv_msg->header.src.addr[4], recv_msg->header.src.addr[5]);
test_vfs_eth_tap_msg_t echo_msg;
create_echo_frame(recv_msg, &echo_msg, len);
ssize_t ret = write(eth_tap_fd, &echo_msg, len);
if (ret == -1) {
ESP_LOGE(TAG, "L2 TAP fd %d write error: errno: %d", eth_tap_fd, errno);
break;
}{...}
}{...} else {
ESP_LOGE(TAG, "L2 TAP fd %d read error: errno %d", eth_tap_fd, errno);
break;
}{...}
}{...}
close(eth_tap_fd);
error:
vTaskDelete(NULL);
}{ ... }
static void nonblock_l2tap_echo_task(void *pvParameters)
{
uint8_t rx_buffer[128];
int eth_tap_fd;
if ((eth_tap_fd = init_l2tap_fd(O_NONBLOCK, ETH_TYPE_FILTER_NOBLOCK)) == INVALID_FD) {
goto error;
}{...}
while (1) {
struct timeval tv;
tv.tv_sec = 5;
tv.tv_usec = 0;
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(eth_tap_fd, &rfds);
int ret_sel = select(eth_tap_fd + 1, &rfds, NULL, NULL, &tv);
if (ret_sel > 0) {
ssize_t len = read(eth_tap_fd, rx_buffer, sizeof(rx_buffer));
if (len > 0) {
test_vfs_eth_tap_msg_t *recv_msg = (test_vfs_eth_tap_msg_t *)rx_buffer;
ESP_LOGI(TAG, "fd %d received %d bytes from %.2x:%.2x:%.2x:%.2x:%.2x:%.2x", eth_tap_fd,
len, recv_msg->header.src.addr[0], recv_msg->header.src.addr[1], recv_msg->header.src.addr[2],
recv_msg->header.src.addr[3], recv_msg->header.src.addr[4], recv_msg->header.src.addr[5]);
test_vfs_eth_tap_msg_t echo_msg;
create_echo_frame(recv_msg, &echo_msg, len);
ssize_t ret = write(eth_tap_fd, &echo_msg, len);
if (ret == -1) {
ESP_LOGE(TAG, "L2 TAP fd %d write error: errno: %d", eth_tap_fd, errno);
break;
}{...}
}{...} else {
ESP_LOGE(TAG, "L2 TAP fd %d read error: errno %d", eth_tap_fd, errno);
break;
}{...}
}{...} else if (ret_sel == 0) {
ESP_LOGD(TAG, "L2 TAP select timeout");
}{...} else {
ESP_LOGE(TAG, "L2 TAP select error: errno %d", errno);
break;
}{...}
}{...}
close(eth_tap_fd);
error:
vTaskDelete(NULL);
}{ ... }
static void hello_tx_l2tap_task(void *pvParameters)
{
uint16_t eth_type_filter = ETH_TYPE_FILTER_TX;
int eth_tap_fd;
if ((eth_tap_fd = init_l2tap_fd(0, eth_type_filter)) == INVALID_FD) {
goto error;
}{...}
test_vfs_eth_tap_msg_t hello_msg = {
.header = {
.src.addr = {0},
.dest.addr = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
.type = htons(eth_type_filter)
}{...},
.payload = "ESP32 hello to everybody!"
}{...};
esp_eth_handle_t eth_hndl = get_example_eth_handle();
esp_eth_ioctl(eth_hndl, ETH_CMD_G_MAC_ADDR, hello_msg.header.src.addr);
while (1) {
ssize_t ret = write(eth_tap_fd, &hello_msg, ETH_HEADER_LEN + strlen(hello_msg.payload));
if (ret == -1) {
ESP_LOGE(TAG, "L2 TAP fd %d write error: errno: %d", eth_tap_fd, errno);
break;
}{...}
vTaskDelay(pdMS_TO_TICKS(3000));
}{...}
close(eth_tap_fd);
error:
vTaskDelete(NULL);
}{ ... }
void app_main(void)
{
ESP_ERROR_CHECK(esp_vfs_l2tap_intf_register(NULL));
ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
/* ... */
ESP_ERROR_CHECK(example_connect());
esp_eth_handle_t eth_hndl = get_example_eth_handle();
uint8_t mac_addr[ETH_ADDR_LEN];
esp_eth_ioctl(eth_hndl, ETH_CMD_G_MAC_ADDR, mac_addr);
ESP_LOGI(TAG, "Ethernet HW Addr %02x:%02x:%02x:%02x:%02x:%02x",
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
xTaskCreate(echo_l2tap_task, "echo", 4096, NULL, 6, NULL);
xTaskCreate(nonblock_l2tap_echo_task, "echo_no-block", 4096, NULL, 5, NULL);
xTaskCreate(hello_tx_l2tap_task, "hello_tx", 4096, NULL, 4, NULL);
}{ ... }