net: ethernet: Define and use Ethernet frame and datagram size

Remove magic numbers from Ethernet drivers and tests by defining
NET_ETH_MAX_DATAGRAM_SIZE and NET_ETH_MAX_FRAME_SIZE.

Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
This commit is contained in:
Andrei Emeltchenko 2019-03-14 14:06:54 +02:00 committed by Kumar Gala
commit 99403c5b13
22 changed files with 49 additions and 42 deletions

View file

@ -388,5 +388,5 @@ NET_DEVICE_INIT(eth_dw_0, CONFIG_ETH_DW_0_NAME,
eth_setup, &eth_0_runtime, eth_setup, &eth_0_runtime,
&eth_config_0, CONFIG_ETH_INIT_PRIORITY, &api_funcs, &eth_config_0, CONFIG_ETH_INIT_PRIORITY, &api_funcs,
ETHERNET_L2, NET_L2_GET_CTX_TYPE(ETHERNET_L2), ETHERNET_L2, NET_L2_GET_CTX_TYPE(ETHERNET_L2),
ETH_DW_MTU); NET_ETH_MTU);
#endif /* CONFIG_ETH_DW_0 */ #endif /* CONFIG_ETH_DW_0 */

View file

@ -142,8 +142,6 @@ struct eth_rx_desc {
u8_t *buf2_ptr; u8_t *buf2_ptr;
}; };
#define ETH_DW_MTU 1500
/* Driver metadata associated with each Ethernet device */ /* Driver metadata associated with each Ethernet device */
struct eth_runtime { struct eth_runtime {
u32_t base_addr; u32_t base_addr;
@ -156,7 +154,7 @@ struct eth_runtime {
/* Receive descriptor */ /* Receive descriptor */
volatile struct eth_rx_desc rx_desc; volatile struct eth_rx_desc rx_desc;
/* Receive DMA packet buffer */ /* Receive DMA packet buffer */
volatile u8_t rx_buf[ETH_DW_MTU]; volatile u8_t rx_buf[NET_ETH_MTU];
union { union {
struct { struct {

View file

@ -233,4 +233,4 @@ NET_DEVICE_INIT(eth_e1000,
&e1000_api, &e1000_api,
ETHERNET_L2, ETHERNET_L2,
NET_L2_GET_CTX_TYPE(ETHERNET_L2), NET_L2_GET_CTX_TYPE(ETHERNET_L2),
E1000_MTU); NET_ETH_MTU);

View file

@ -30,8 +30,6 @@ extern "C" {
#define RDESC_STA_DD (1) /* Descriptor Done */ #define RDESC_STA_DD (1) /* Descriptor Done */
#define TDESC_STA_DD (1) /* Descriptor Done */ #define TDESC_STA_DD (1) /* Descriptor Done */
#define E1000_MTU 1500
#define ETH_ALEN 6 /* TODO: Add a global reusable definition in OS */ #define ETH_ALEN 6 /* TODO: Add a global reusable definition in OS */
enum e1000_reg_t { enum e1000_reg_t {
@ -82,8 +80,8 @@ struct e1000_dev {
struct pci_dev_info pci; struct pci_dev_info pci;
struct net_if *iface; struct net_if *iface;
u8_t mac[ETH_ALEN]; u8_t mac[ETH_ALEN];
u8_t txb[E1000_MTU]; u8_t txb[NET_ETH_MTU];
u8_t rxb[E1000_MTU]; u8_t rxb[NET_ETH_MTU];
}; };
static const char *e1000_reg_to_string(enum e1000_reg_t r) static const char *e1000_reg_to_string(enum e1000_reg_t r)

View file

@ -774,6 +774,7 @@ static const struct eth_enc28j60_config eth_enc28j60_0_config = {
NET_DEVICE_INIT(enc28j60_0, DT_MICROCHIP_ENC28J60_0_LABEL, NET_DEVICE_INIT(enc28j60_0, DT_MICROCHIP_ENC28J60_0_LABEL,
eth_enc28j60_init, &eth_enc28j60_0_runtime, eth_enc28j60_init, &eth_enc28j60_0_runtime,
&eth_enc28j60_0_config, CONFIG_ETH_INIT_PRIORITY, &api_funcs, &eth_enc28j60_0_config, CONFIG_ETH_INIT_PRIORITY, &api_funcs,
ETHERNET_L2, NET_L2_GET_CTX_TYPE(ETHERNET_L2), 1500); ETHERNET_L2, NET_L2_GET_CTX_TYPE(ETHERNET_L2),
NET_ETH_MTU);
#endif /* CONFIG_ETH_ENC28J60_0 */ #endif /* CONFIG_ETH_ENC28J60_0 */

View file

@ -102,7 +102,7 @@ struct eth_context {
* Note that we do not copy FCS into this buffer thus the * Note that we do not copy FCS into this buffer thus the
* size is 1514 bytes. * size is 1514 bytes.
*/ */
u8_t frame_buf[1500 + 14]; /* Max MTU + ethernet header size */ u8_t frame_buf[NET_ETH_MAX_FRAME_SIZE]; /* Max MTU + ethernet header */
}; };
static void eth_0_config_func(void); static void eth_0_config_func(void);
@ -1012,7 +1012,7 @@ static struct eth_context eth_0_context = {
ETH_NET_DEVICE_INIT(eth_mcux_0, DT_ETH_MCUX_0_NAME, eth_0_init, ETH_NET_DEVICE_INIT(eth_mcux_0, DT_ETH_MCUX_0_NAME, eth_0_init,
&eth_0_context, NULL, CONFIG_ETH_INIT_PRIORITY, &eth_0_context, NULL, CONFIG_ETH_INIT_PRIORITY,
&api_funcs, 1500); &api_funcs, NET_ETH_MTU);
static void eth_0_config_func(void) static void eth_0_config_func(void)
{ {

View file

@ -36,10 +36,6 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME);
#include "eth_native_posix_priv.h" #include "eth_native_posix_priv.h"
#if defined(CONFIG_NET_L2_ETHERNET)
#define _ETH_MTU 1500
#endif
#define NET_BUF_TIMEOUT K_MSEC(100) #define NET_BUF_TIMEOUT K_MSEC(100)
#if defined(CONFIG_NET_VLAN) #if defined(CONFIG_NET_VLAN)
@ -49,8 +45,8 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME);
#endif #endif
struct eth_context { struct eth_context {
u8_t recv[_ETH_MTU + ETH_HDR_LEN]; u8_t recv[NET_ETH_MTU + ETH_HDR_LEN];
u8_t send[_ETH_MTU + ETH_HDR_LEN]; u8_t send[NET_ETH_MTU + ETH_HDR_LEN];
u8_t mac_addr[6]; u8_t mac_addr[6];
struct net_linkaddr ll_addr; struct net_linkaddr ll_addr;
struct net_if *iface; struct net_if *iface;
@ -509,7 +505,7 @@ static const struct ethernet_api eth_if_api = {
ETH_NET_DEVICE_INIT(eth_native_posix, ETH_NATIVE_POSIX_DRV_NAME, ETH_NET_DEVICE_INIT(eth_native_posix, ETH_NATIVE_POSIX_DRV_NAME,
eth_init, &eth_context_data, NULL, eth_init, &eth_context_data, NULL,
CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &eth_if_api, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &eth_if_api,
_ETH_MTU); NET_ETH_MTU);
#if defined(CONFIG_ETH_NATIVE_POSIX_PTP_CLOCK) #if defined(CONFIG_ETH_NATIVE_POSIX_PTP_CLOCK)
struct ptp_context { struct ptp_context {

View file

@ -19,7 +19,7 @@
*/ */
#define GMAC_MULTIPLE_TX_PACKETS 0 #define GMAC_MULTIPLE_TX_PACKETS 0
#define GMAC_MTU 1500 #define GMAC_MTU NET_ETH_MTU
#define GMAC_FRAME_SIZE_MAX (GMAC_MTU + 18) #define GMAC_FRAME_SIZE_MAX (GMAC_MTU + 18)
/** Cache alignment */ /** Cache alignment */

View file

@ -472,7 +472,7 @@ static int smsc_write_tx_fifo(const u8_t *buf, u32_t len, bool is_last)
static int eth_tx(struct device *dev, struct net_pkt *pkt) static int eth_tx(struct device *dev, struct net_pkt *pkt)
{ {
u16_t total_len = net_pkt_get_len(pkt); u16_t total_len = net_pkt_get_len(pkt);
static u8_t tx_buf[1514] __aligned(4); static u8_t tx_buf[NET_ETH_MAX_FRAME_SIZE] __aligned(4);
u32_t txcmd_a, txcmd_b; u32_t txcmd_a, txcmd_b;
u32_t tx_stat; u32_t tx_stat;
int res; int res;
@ -679,5 +679,5 @@ static struct eth_context eth_0_context;
ETH_NET_DEVICE_INIT(eth_smsc911x_0, "smsc911x_0", ETH_NET_DEVICE_INIT(eth_smsc911x_0, "smsc911x_0",
eth_init, &eth_0_context, eth_init, &eth_0_context,
NULL/*&eth_config_0*/, CONFIG_ETH_INIT_PRIORITY, &api_funcs, NULL /*&eth_config_0*/, CONFIG_ETH_INIT_PRIORITY, &api_funcs,
1500/*MTU*/); NET_ETH_MTU /*MTU*/);

View file

@ -358,4 +358,4 @@ NET_DEVICE_INIT(eth_stellaris, DT_ETH_DRV_NAME,
eth_stellaris_dev_init, &eth_data, &eth_cfg, eth_stellaris_dev_init, &eth_data, &eth_cfg,
CONFIG_ETH_INIT_PRIORITY, CONFIG_ETH_INIT_PRIORITY,
&eth_stellaris_apis, ETHERNET_L2, &eth_stellaris_apis, ETHERNET_L2,
NET_L2_GET_CTX_TYPE(ETHERNET_L2), ETH_MTU); NET_L2_GET_CTX_TYPE(ETHERNET_L2), NET_ETH_MTU);

View file

@ -8,8 +8,6 @@
#ifndef ETH_STELLARIS_PRIV_H_ #ifndef ETH_STELLARIS_PRIV_H_
#define ETH_STELLARIS_PRIV_H_ #define ETH_STELLARIS_PRIV_H_
#define ETH_MTU 1500
#define DEV_DATA(dev) \ #define DEV_DATA(dev) \
((struct eth_stellaris_runtime *)(dev)->driver_data) ((struct eth_stellaris_runtime *)(dev)->driver_data)
#define DEV_CFG(dev) \ #define DEV_CFG(dev) \

View file

@ -9,7 +9,7 @@
#include <kernel.h> #include <kernel.h>
#include <zephyr/types.h> #include <zephyr/types.h>
#define ETH_STM32_HAL_MTU 1500 #define ETH_STM32_HAL_MTU NET_ETH_MTU
#define ETH_STM32_HAL_FRAME_SIZE_MAX (ETH_STM32_HAL_MTU + 18) #define ETH_STM32_HAL_FRAME_SIZE_MAX (ETH_STM32_HAL_MTU + 18)
/* Definition of the Ethernet driver buffers size and count */ /* Definition of the Ethernet driver buffers size and count */

View file

@ -72,6 +72,8 @@ struct net_eth_addr {
#endif #endif
#define NET_ETH_MINIMAL_FRAME_SIZE 60 #define NET_ETH_MINIMAL_FRAME_SIZE 60
#define NET_ETH_MTU 1500
#define NET_ETH_MAX_FRAME_SIZE (NET_ETH_MTU + sizeof(struct net_eth_hdr))
/** @endcond */ /** @endcond */

View file

@ -291,13 +291,15 @@ ETH_NET_DEVICE_INIT(eth_offloading_disabled_test,
"eth_offloading_disabled_test", "eth_offloading_disabled_test",
eth_init, &eth_context_offloading_disabled, eth_init, &eth_context_offloading_disabled,
NULL, CONFIG_ETH_INIT_PRIORITY, NULL, CONFIG_ETH_INIT_PRIORITY,
&api_funcs_offloading_disabled, 1500); &api_funcs_offloading_disabled,
NET_ETH_MTU);
ETH_NET_DEVICE_INIT(eth_offloading_enabled_test, ETH_NET_DEVICE_INIT(eth_offloading_enabled_test,
"eth_offloading_enabled_test", "eth_offloading_enabled_test",
eth_init, &eth_context_offloading_enabled, eth_init, &eth_context_offloading_enabled,
NULL, CONFIG_ETH_INIT_PRIORITY, NULL, CONFIG_ETH_INIT_PRIORITY,
&api_funcs_offloading_enabled, 1500); &api_funcs_offloading_enabled,
NET_ETH_MTU);
struct user_data { struct user_data {
int eth_if_count; int eth_if_count;

View file

@ -302,7 +302,8 @@ static int eth_fake_init(struct device *dev)
} }
ETH_NET_DEVICE_INIT(eth_fake, "eth_fake", eth_fake_init, &eth_fake_data, ETH_NET_DEVICE_INIT(eth_fake, "eth_fake", eth_fake_init, &eth_fake_data,
NULL, CONFIG_ETH_INIT_PRIORITY, &eth_fake_api_funcs, 1500); NULL, CONFIG_ETH_INIT_PRIORITY, &eth_fake_api_funcs,
NET_ETH_MTU);
static void test_change_mac_when_up(void) static void test_change_mac_when_up(void)
{ {

View file

@ -258,7 +258,8 @@ static int eth_fake_init(struct device *dev)
} }
ETH_NET_DEVICE_INIT(eth_fake, "eth_fake", eth_fake_init, &eth_fake_data, ETH_NET_DEVICE_INIT(eth_fake, "eth_fake", eth_fake_init, &eth_fake_data,
NULL, CONFIG_ETH_INIT_PRIORITY, &eth_fake_api_funcs, 1500); NULL, CONFIG_ETH_INIT_PRIORITY, &eth_fake_api_funcs,
NET_ETH_MTU);
#if NET_LOG_LEVEL >= LOG_LEVEL_DBG #if NET_LOG_LEVEL >= LOG_LEVEL_DBG
static const char *iface2str(struct net_if *iface) static const char *iface2str(struct net_if *iface)

View file

@ -75,7 +75,8 @@ static const struct dummy_api fake_dev_api = {
NET_DEVICE_INIT(fake_dev, "fake_dev", NET_DEVICE_INIT(fake_dev, "fake_dev",
fake_dev_init, NULL, NULL, fake_dev_init, NULL, NULL,
CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
&fake_dev_api, _ETH_L2_LAYER, _ETH_L2_CTX_TYPE, 1500); &fake_dev_api, _ETH_L2_LAYER, _ETH_L2_CTX_TYPE,
NET_ETH_MTU);
/*********************\ /*********************\
* UTILITY FUNCTIONS * * UTILITY FUNCTIONS *

View file

@ -143,10 +143,12 @@ static int eth_fake_init(struct device *dev)
} }
ETH_NET_DEVICE_INIT(eth_fake1, "eth_fake1", eth_fake_init, &eth_fake_data1, ETH_NET_DEVICE_INIT(eth_fake1, "eth_fake1", eth_fake_init, &eth_fake_data1,
NULL, CONFIG_ETH_INIT_PRIORITY, &eth_fake_api_funcs, 1500); NULL, CONFIG_ETH_INIT_PRIORITY, &eth_fake_api_funcs,
NET_ETH_MTU);
ETH_NET_DEVICE_INIT(eth_fake2, "eth_fake2", eth_fake_init, &eth_fake_data2, ETH_NET_DEVICE_INIT(eth_fake2, "eth_fake2", eth_fake_init, &eth_fake_data2,
NULL, CONFIG_ETH_INIT_PRIORITY, &eth_fake_api_funcs, 1500); NULL, CONFIG_ETH_INIT_PRIORITY, &eth_fake_api_funcs,
NET_ETH_MTU);
#if NET_LOG_LEVEL >= LOG_LEVEL_DBG #if NET_LOG_LEVEL >= LOG_LEVEL_DBG
static const char *iface2str(struct net_if *iface) static const char *iface2str(struct net_if *iface)

View file

@ -155,13 +155,16 @@ static int eth_init(struct device *dev)
} }
ETH_NET_DEVICE_INIT(eth_test_1, "eth_test_1", eth_init, &eth_context_1, NULL, ETH_NET_DEVICE_INIT(eth_test_1, "eth_test_1", eth_init, &eth_context_1, NULL,
CONFIG_ETH_INIT_PRIORITY, &api_funcs, 1500); CONFIG_ETH_INIT_PRIORITY, &api_funcs,
NET_ETH_MTU);
ETH_NET_DEVICE_INIT(eth_test_2, "eth_test_2", eth_init, &eth_context_2, NULL, ETH_NET_DEVICE_INIT(eth_test_2, "eth_test_2", eth_init, &eth_context_2, NULL,
CONFIG_ETH_INIT_PRIORITY, &api_funcs, 1500); CONFIG_ETH_INIT_PRIORITY, &api_funcs,
NET_ETH_MTU);
ETH_NET_DEVICE_INIT(eth_test_3, "eth_test_3", eth_init, &eth_context_3, NULL, ETH_NET_DEVICE_INIT(eth_test_3, "eth_test_3", eth_init, &eth_context_3, NULL,
CONFIG_ETH_INIT_PRIORITY, &api_funcs, 1500); CONFIG_ETH_INIT_PRIORITY, &api_funcs,
NET_ETH_MTU);
static u64_t timestamp_to_nsec(struct net_ptp_time *ts) static u64_t timestamp_to_nsec(struct net_ptp_time *ts)
{ {

View file

@ -267,7 +267,8 @@ static int eth_init(struct device *dev)
*/ */
NET_DEVICE_INIT(eth_test, "eth_test", eth_init, &eth_context, NET_DEVICE_INIT(eth_test, "eth_test", eth_init, &eth_context,
NULL, CONFIG_ETH_INIT_PRIORITY, &api_funcs, NULL, CONFIG_ETH_INIT_PRIORITY, &api_funcs,
DUMMY_L2, NET_L2_GET_CTX_TYPE(DUMMY_L2), 1500); DUMMY_L2, NET_L2_GET_CTX_TYPE(DUMMY_L2),
NET_ETH_MTU);
static void address_setup(void) static void address_setup(void)
{ {

View file

@ -153,10 +153,12 @@ static int eth_init(struct device *dev)
} }
ETH_NET_DEVICE_INIT(eth_test, "eth_test", eth_init, &eth_context, ETH_NET_DEVICE_INIT(eth_test, "eth_test", eth_init, &eth_context,
NULL, CONFIG_ETH_INIT_PRIORITY, &api_funcs, 1500); NULL, CONFIG_ETH_INIT_PRIORITY, &api_funcs,
NET_ETH_MTU);
ETH_NET_DEVICE_INIT(eth_test2, "eth_test2", eth_init, &eth_context2, ETH_NET_DEVICE_INIT(eth_test2, "eth_test2", eth_init, &eth_context2,
NULL, CONFIG_ETH_INIT_PRIORITY, &api_funcs, 1500); NULL, CONFIG_ETH_INIT_PRIORITY, &api_funcs,
NET_ETH_MTU);
static void timestamp_callback(struct net_pkt *pkt) static void timestamp_callback(struct net_pkt *pkt)
{ {

View file

@ -172,7 +172,7 @@ static int eth_vlan_init(struct device *dev)
ETH_NET_DEVICE_INIT(eth_vlan_test, "eth_vlan_test", eth_vlan_init, ETH_NET_DEVICE_INIT(eth_vlan_test, "eth_vlan_test", eth_vlan_init,
&eth_vlan_context, NULL, CONFIG_ETH_INIT_PRIORITY, &eth_vlan_context, NULL, CONFIG_ETH_INIT_PRIORITY,
&api_funcs, 1500); &api_funcs, NET_ETH_MTU);
static int eth_init(struct device *dev) static int eth_init(struct device *dev)
{ {
@ -189,7 +189,8 @@ static int eth_init(struct device *dev)
*/ */
NET_DEVICE_INIT(eth_test, "eth_test", eth_init, &eth_vlan_context, NET_DEVICE_INIT(eth_test, "eth_test", eth_init, &eth_vlan_context,
NULL, CONFIG_ETH_INIT_PRIORITY, &api_funcs, NULL, CONFIG_ETH_INIT_PRIORITY, &api_funcs,
ETHERNET_L2, NET_L2_GET_CTX_TYPE(ETHERNET_L2), 1500); ETHERNET_L2, NET_L2_GET_CTX_TYPE(ETHERNET_L2),
NET_ETH_MTU);
struct net_if_test { struct net_if_test {
u8_t idx; /* not used for anything, just a dummy value */ u8_t idx; /* not used for anything, just a dummy value */