From 97002d280f819bd25e4bf97f12461e7d6d3cde8c Mon Sep 17 00:00:00 2001
From: jjoseph <jomily.joseph@iopsys.eu>
Date: Thu, 21 Jan 2021 12:10:20 +0530
Subject: [PATCH] Feature #3095 : Module Doc & Test: ethmngr
- Adding test platform and test stub for libethernet
---
Makefile | 4 +++
ethernet.c | 6 ++--
ethernet.h | 2 ++
test_stub/stub.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++
test_stub/stub.h | 85 +++++++++++++++++++++++++++++++++++++++++++
5 files changed, 188 insertions(+), 2 deletions(-)
create mode 100644 test_stub/stub.c
create mode 100644 test_stub/stub.h
diff --git a/Makefile b/Makefile
index 66e7f0d..36e1b76 100644
--- a/Makefile
+++ b/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/ethernet.c b/ethernet.c
index ea30513..abec12f 100644
--- a/ethernet.c
+++ b/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/ethernet.h b/ethernet.h
index 0116428..97618bc 100644
--- a/ethernet.h
+++ b/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/test_stub/stub.c b/test_stub/stub.c
new file mode 100644
index 0000000..9cb4948
--- /dev/null
+++ b/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/test_stub/stub.h b/test_stub/stub.h
new file mode 100644
index 0000000..b8e557f
--- /dev/null
+++ b/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 */
--
GitLab