diff --git a/libethernet/Makefile b/libethernet/Makefile index 66e7f0d33ca88a4679bd8bd5318abd31e9c9ad31..36e1b76d05e396a12108b971c17f87c8b571c0f3 100644 --- a/libethernet/Makefile +++ b/libethernet/Makefile @@ -13,6 +13,10 @@ objs_lib += bcm/bcm.o else ifeq ($(PLATFORM),MEDIATEK) objs_lib += ethsw.o XXFLAGS += -Wl,-whole-archive -lsw -Wl,-no-whole-archive +else ifeq ($(PLATFORM),TEST) +CFLAGS += -DIOPSYS_TEST -I/usr/include/libnl3 +objs_lib += test_stub/stub.o + endif all: libethernet.so diff --git a/libethernet/ethernet.c b/libethernet/ethernet.c index ea30513cf2cbbabf529aafe188ef08481f0dad9f..abec12f8df17bc4e64142c3c2768a605d857c124 100644 --- a/libethernet/ethernet.c +++ b/libethernet/ethernet.c @@ -29,16 +29,16 @@ #include <sys/ioctl.h> #include <sys/socket.h> #include <sys/types.h> -#include <net/if.h> #include <stdbool.h> #include <linux/sockios.h> #include <linux/mii.h> -#include "easy.h" #include "ethernet.h" #ifdef IOPSYS_BROADCOM extern const struct eth_ops bcm_eth_ops; +#elif IOPSYS_TEST +extern const struct eth_ops test_eth_ops; #else extern const struct eth_ops ethsw_ops; #endif @@ -46,6 +46,8 @@ extern const struct eth_ops ethsw_ops; const struct eth_ops *eth_ops[] = { #ifdef IOPSYS_BROADCOM &bcm_eth_ops, +#elif IOPSYS_TEST + &test_eth_ops, #else ðsw_ops, /* FIXME */ #endif diff --git a/libethernet/ethernet.h b/libethernet/ethernet.h index 0116428d247bb8f903930ec083abe8f922f6d5b7..97618bc6c3eda3fb187e80810e4356c1dc4a588a 100644 --- a/libethernet/ethernet.h +++ b/libethernet/ethernet.h @@ -25,6 +25,8 @@ #include <stdint.h> #include <stdbool.h> #include <linux/types.h> +#include <net/if.h> +#include "easy.h" #ifdef __cplusplus extern "C" { diff --git a/libethernet/test_stub/stub.c b/libethernet/test_stub/stub.c new file mode 100644 index 0000000000000000000000000000000000000000..9cb4948f42001ab514b4c25880832c0f7c86fb78 --- /dev/null +++ b/libethernet/test_stub/stub.c @@ -0,0 +1,93 @@ +/* + * stub.c - implements 'test' ethernet + * + * Copyright (C) 2021 iopsys Software Solutions AB. All rights reserved. + * + * Author: jomily.joseph@iopsys.eu + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#include <stdio.h> +#include <stdarg.h> +#include <string.h> +#include <stdint.h> +#include <errno.h> +#include <sys/types.h> +#include <fcntl.h> +#include <net/if.h> +#include <time.h> +#include <syslog.h> +#include <unistd.h> +#include <stdlib.h> + +#include "stub.h" + +int test_eth_set_link_settings(const char *name, struct eth_link link) +{ + return 0; +} + +int test_eth_get_link_settings(const char *name, struct eth_link link) +{ + return 0; +} + +int test_eth_poweron_phy(const char *name, struct eth_phy p) +{ + return 0; +} + +int test_eth_poweroff_phy(const char *name, struct eth_phy p) +{ + return 0; +} + +int test_eth_reset_phy(const char *name, int phy_id) +{ + return 0; +} + +static int test_eth_get_stats_from_proc(const char *ifname, struct eth_stats *s) +{ + GET_TEST_STATS(s, ifname, eth_stats, struct eth_stats); + return 0; +} + +int test_eth_get_stats(const char *ifname, struct eth_stats *s) +{ + test_eth_get_stats_from_proc(ifname, s); + GET_TEST_STATS(s, ifname, eth_stats, struct eth_stats); + + return 0; +} + +int test_eth_get_rmon_stats(const char *ifname, struct eth_rmon_stats *rmon) +{ + GET_TEST_STATS(rmon, ifname, eth_rmon_stats, struct eth_rmon_stats); + + return 0; +} + +const struct eth_ops test_eth_ops = { + .ifname = "eth", + .set_link_settings = test_eth_set_link_settings, + .get_link_settings = test_eth_get_link_settings, + .get_stats = test_eth_get_stats, + .get_rmon_stats = test_eth_get_rmon_stats, + .poweron_phy = test_eth_poweron_phy, + .poweroff_phy = test_eth_poweroff_phy, + .reset_phy = test_eth_reset_phy, +}; diff --git a/libethernet/test_stub/stub.h b/libethernet/test_stub/stub.h new file mode 100644 index 0000000000000000000000000000000000000000..b8e557f5bc7f907335fc281d683eef2436e07169 --- /dev/null +++ b/libethernet/test_stub/stub.h @@ -0,0 +1,85 @@ +/* + * stub.h - 'test' ethernet module header + * + * Copyright (C) 2021 iopsys Software Solutions AB. All rights reserved. + * + * Author: jomily.joseph@iopsys.eu + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#ifndef TEST_ETHERNET_H +#define TEST_ETHERNET_H + +#include "ethernet.h" + +#define TESTSTATS(attr) test ## _ ## attr + +#define GET_TEST_STATS(o, iface, attr, type) \ +({ \ + memcpy(o, TESTSTATS(attr), sizeof(type)); \ +}) + +const struct eth_stats test_eth_stats_data = { + .tx_bytes = 100, + .rx_bytes = 1200, + .tx_packets = 900, + .rx_packets = 100, + .tx_errors = 10, + .rx_errors = 20, + .tx_ucast_packets = 1, + .rx_ucast_packets = 2, + .tx_mcast_packets = 3, + .rx_mcast_packets = 4, + .tx_bcast_packets = 5, + .rx_bcast_packets = 6, + .tx_discard_packets = 7, + .rx_discard_packets = 8, + .rx_unknown_packets = 9, +}; + +#define test_eth_stats &test_eth_stats_data + +const struct eth_rmon_stats test_eth_rmon_stats_data = { + .tx.bytes = 230000, + .tx.packets = 355000, + .tx.bcast_packets = 2300, + .tx.mcast_packets = 3, + .tx.crc_err_packets = 43, + .tx.under_sz_packets = 2, + .tx.over_sz_packets = 300, + .tx.packets_64bytes = 900000, + .tx.packets_65to127bytes = 8200, + .tx.packets_128to255bytes = 120000, + .tx.packets_256to511bytes = 2400, + .tx.packets_512to1023bytes = 100000, + .tx.packets_1024to1518bytes = 27000, + .rx.bytes = 12000, + .rx.packets = 800000, + .rx.bcast_packets = 3, + .rx.mcast_packets = 4, + .rx.crc_err_packets = 6000, + .rx.under_sz_packets = 24, + .rx.over_sz_packets = 4500, + .rx.packets_64bytes = 6000, + .rx.packets_65to127bytes = 41000, + .rx.packets_128to255bytes = 3000000, + .rx.packets_256to511bytes = 4500, + .rx.packets_512to1023bytes = 560000, + .rx.packets_1024to1518bytes = 34000, + +}; +#define test_eth_rmon_stats &test_eth_rmon_stats_data +#endif /* TEST_ETHERNET_H */