Transparent ethernet bridge and ping to ESP32C6 module
Posted: Fri Jan 12, 2024 7:38 am
Hello, I'm developing a project with the ESP32C6 module, which has two W5500 ethernets, with the aim that a device can send and receive data from a PC through this module via ethernet or wifi.
To do this, a transparent bridge has been made between the two ethernets (eth1/eth2) and another wifi bridge (eth1/wifi).
In addition, a static IP has been configured to the module so that I can connect to it and configure the module from a web with the selected static IP address.
Having made the eth1/eth2 bridge, the module is not able to answer the ping because everything it receives through eth1 is sent to eth2 automatically without looking if the packet it has received is really for it.
As it is not able to answer the ping I can't configure the module through the web.
Any idea how to solve this problem?
I attach an image with the schematic of the project and the code of the eth1 and eth2 configuration:
Thank you very much.
To do this, a transparent bridge has been made between the two ethernets (eth1/eth2) and another wifi bridge (eth1/wifi).
In addition, a static IP has been configured to the module so that I can connect to it and configure the module from a web with the selected static IP address.
Having made the eth1/eth2 bridge, the module is not able to answer the ping because everything it receives through eth1 is sent to eth2 automatically without looking if the packet it has received is really for it.
As it is not able to answer the ping I can't configure the module through the web.
Any idea how to solve this problem?
I attach an image with the schematic of the project and the code of the eth1 and eth2 configuration:
- static void initialize_ethernet(void)
- {
- uint8_t eth_port_cnt = 0;
- esp_eth_handle_t *eth_handles;
- eth_speed_t speed = ETH_SPEED_10M;
- eth_duplex_t duplex = ETH_DUPLEX_FULL;
- bool exp_autoneg_en = false, auto_nego_en = false;
- // Initialize Ethernet driver
- ESP_ERROR_CHECK(example_eth_init(ð_handles, ð_port_cnt));
- // The same MAC address will be used for all Ethernet ports since the bridge acts as one device
- uint8_t common_mac_addr[ETH_ADDR_LEN] = {0x00, 0x08, 0xEF, 0x11, 0x22, 0x33}; //MAC Ethernet 1 fijo
- // If internal Ethernet is not supported by ESP32x SoC, Locally Administered OUI address might be returned.
- // Note that Locally Administered OUI range should be used only when testing on a LAN under your control!
- for (int i = 0; i < eth_port_cnt; i++) {
- ESP_ERROR_CHECK(esp_eth_ioctl(eth_handles[i], ETH_CMD_S_MAC_ADDR, common_mac_addr));
- }
- // Initialize TCP/IP network interface
- ESP_ERROR_CHECK(esp_netif_init());
- // Create instances of esp-netif for Ethernet ports
- esp_netif_t **eth_netifs = calloc(eth_port_cnt, sizeof(esp_netif_t *));
- esp_netif_inherent_config_t esp_netif_config = ESP_NETIF_INHERENT_DEFAULT_ETH();
- esp_netif_config_t netif_cfg = {
- .base = &esp_netif_config,
- .stack = ESP_NETIF_NETSTACK_DEFAULT_ETH,
- };
- char if_key_str[10];
- char if_desc_str[10];
- char num_str[3];
- for (int i = 0; i < eth_port_cnt; i++) {
- itoa(i, num_str, 10);
- strcat(strcpy(if_key_str, "ETH_"), num_str);
- strcat(strcpy(if_desc_str, "eth"), num_str);
- esp_netif_config.if_key = if_key_str;
- esp_netif_config.if_desc = if_desc_str;
- esp_netif_config.route_prio = 50 - i;
- esp_netif_config.flags = 0; // esp-netif flags need to be zero when port's to be bridged
- eth_netifs[i] = esp_netif_new(&netif_cfg);
- esp_netif_ip_info_t ip_info;
- esp_netif_dhcpc_stop(eth_netif);
- IP4_ADDR(&ip_info.ip, 192, 168, 10, 1);
- IP4_ADDR(&ip_info.netmask, 255, 255, 255, 0);
- IP4_ADDR(&ip_info.gw, 192, 168, 10, 254);
- esp_netif_set_ip_info(eth_netifs[i], &ip_info);
- // Attach Ethernet driver to TCP/IP stack
- ESP_ERROR_CHECK(esp_netif_attach(eth_netifs[i], esp_eth_new_netif_glue(eth_handles[i])));
- }
- // Create instance of esp-netif for bridge interface
- esp_netif_inherent_config_t esp_netif_br_config = ESP_NETIF_INHERENT_DEFAULT_BR();
- esp_netif_config_t netif_br_cfg = {
- .base = &esp_netif_br_config,
- .stack = ESP_NETIF_NETSTACK_DEFAULT_BR,
- };
- // Bridge configuration
- bridgeif_config_t bridgeif_config = {
- .max_fdb_dyn_entries = 10, // maximum number of address entries in dynamic forwarding database
- .max_fdb_sta_entries = 2, // maximum number of address entries in static forwarding database
- .max_ports = eth_port_cnt // maximum number of ports the bridge can consist of
- };
- esp_netif_br_config.bridge_info = &bridgeif_config;
- // Set MAC address of bridge interface the same as the Ethernet interface
- memcpy(esp_netif_br_config.mac, common_mac_addr, ETH_ADDR_LEN);
- esp_netif_t *br_netif = esp_netif_new(&netif_br_cfg);
- // Create new esp netif bridge glue instance
- esp_netif_br_glue_handle_t netif_br_glue = esp_netif_br_glue_new();
- // Add Ethernet port interfaces to that esp netif bridge glue instance
- for (int i = 0; i < eth_port_cnt; i++) {
- ESP_ERROR_CHECK(esp_netif_br_glue_add_port(netif_br_glue, eth_netifs[i]));
- }
- // Attach esp netif bridge glue instance with added ports to bridge netif
- ESP_ERROR_CHECK(esp_netif_attach(br_netif, netif_br_glue));
- // Register user defined event handers
- ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, ð_event_handler, NULL));
- for (int i = 0; i < eth_port_cnt; i++) {
- // Since the MAC forwarding is performed in lwIP bridge, we need to pass all addresses through the Ethernet MACs
- bool promiscuous = true;
- //ESP_ERROR_CHECK(esp_eth_update_input_path(eth_handles[i], pkt_eth2wifi, NULL));
- esp_eth_ioctl(eth_handles[i], ETH_CMD_S_PROMISCUOUS, &promiscuous);
- // Disable autonegotiation and change speed to 10 Mbps and full duplex
- ESP_LOGI(TAG, "disable the autonegotiation and change the speed/duplex...");
- ESP_ERROR_CHECK(esp_eth_ioctl(eth_handles[i], ETH_CMD_S_AUTONEGO, &auto_nego_en));
- ESP_ERROR_CHECK(esp_eth_ioctl(eth_handles[i], ETH_CMD_G_AUTONEGO, &exp_autoneg_en));
- // set new duplex mode
- ESP_ERROR_CHECK(esp_eth_ioctl(eth_handles[i], ETH_CMD_S_DUPLEX_MODE, &duplex));
- // set new speed
- ESP_ERROR_CHECK(esp_eth_ioctl(eth_handles[i], ETH_CMD_S_SPEED, &speed));
- s_eth_handle[i] = eth_handles[i];
- // Start Ethernet driver state machine
- ESP_ERROR_CHECK(esp_eth_start(eth_handles[i]));
- }
- // --- Initialize Console ---
- esp_console_repl_t *repl = NULL;
- esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT();
- repl_config.prompt = "bridge>";
- // install console REPL environment
- esp_console_dev_uart_config_t uart_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT();
- ESP_ERROR_CHECK(esp_console_new_repl_uart(&uart_config, &repl_config, &repl));
- example_register_br_config_commands(br_netif, eth_port_cnt);
- // start console REPL
- ESP_ERROR_CHECK(esp_console_start_repl(repl));
- }