Added KUnit tests for ibmveth_remove_buffer_from_pool and
ibmveth_rxq_get_buffer under new IBMVETH_KUNIT_TEST config option.

Signed-off-by: Dave Marquardt <davem...@linux.ibm.com>
---
 drivers/net/ethernet/ibm/Kconfig   |  13 +++
 drivers/net/ethernet/ibm/ibmveth.c | 125 +++++++++++++++++++++++++++++
 2 files changed, 138 insertions(+)

diff --git a/drivers/net/ethernet/ibm/Kconfig b/drivers/net/ethernet/ibm/Kconfig
index c0c112d95b89..4f4b23465c47 100644
--- a/drivers/net/ethernet/ibm/Kconfig
+++ b/drivers/net/ethernet/ibm/Kconfig
@@ -27,6 +27,19 @@ config IBMVETH
          To compile this driver as a module, choose M here. The module will
          be called ibmveth.
 
+config IBMVETH_KUNIT_TEST
+       bool "KUnit test for IBM LAN Virtual Ethernet support" if 
!KUNIT_ALL_TESTS
+       depends on KUNIT
+       depends on KUNIT=y && IBMVETH=y
+       default KUNIT_ALL_TESTS
+       help
+         This builds unit tests for the IBM LAN Virtual Ethernet driver.
+
+         For more information on KUnit and unit tests in general, please refer
+         to the KUnit documentation in Documentation/dev-tools/kunit/.
+
+         If unsure, say N.
+
 source "drivers/net/ethernet/ibm/emac/Kconfig"
 
 config EHEA
diff --git a/drivers/net/ethernet/ibm/ibmveth.c 
b/drivers/net/ethernet/ibm/ibmveth.c
index 59158284ec43..e56cfc82ddd0 100644
--- a/drivers/net/ethernet/ibm/ibmveth.c
+++ b/drivers/net/ethernet/ibm/ibmveth.c
@@ -2040,3 +2040,128 @@ static void __exit ibmveth_module_exit(void)
 
 module_init(ibmveth_module_init);
 module_exit(ibmveth_module_exit);
+
+#ifdef CONFIG_IBMVETH_KUNIT_TEST
+#include <kunit/test.h>
+
+/**
+ * ibmveth_reset_kunit - reset routine for running in KUnit environment
+ *
+ * @w: pointer to work_struct embedded in adapter structure
+ *
+ * Context: Called in the KUnit environment. Does nothing.
+ *
+ * Return: void
+ */
+static void ibmveth_reset_kunit(struct work_struct *w)
+{
+       netdev_dbg(NULL, "reset_kunit starting\n");
+       netdev_dbg(NULL, "reset_kunit complete\n");
+}
+
+/**
+ * ibmveth_remove_buffer_from_pool_test - unit test for some of
+ *                                        ibmveth_remove_buffer_from_pool
+ * @test: pointer to kunit structure
+ *
+ * Tests the error returns from ibmveth_remove_buffer_from_pool.
+ * ibmveth_remove_buffer_from_pool also calls WARN_ON, so dmesg should be
+ * checked to see that these warnings happened.
+ *
+ * Return: void
+ */
+static void ibmveth_remove_buffer_from_pool_test(struct kunit *test)
+{
+       struct ibmveth_adapter *adapter = kunit_kzalloc(test, sizeof(*adapter), 
GFP_KERNEL);
+       struct ibmveth_buff_pool *pool;
+       u64 correlator;
+
+       KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adapter);
+
+       INIT_WORK(&adapter->work, ibmveth_reset_kunit);
+
+       /* Set sane values for buffer pools */
+       for (int i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
+               ibmveth_init_buffer_pool(&adapter->rx_buff_pool[i], i,
+                                        pool_count[i], pool_size[i],
+                                        pool_active[i]);
+
+       pool = &adapter->rx_buff_pool[0];
+       pool->skbuff = kunit_kcalloc(test, pool->size, sizeof(void *), 
GFP_KERNEL);
+       KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pool->skbuff);
+
+       correlator = ((u64)IBMVETH_NUM_BUFF_POOLS << 32) | 0;
+       KUNIT_EXPECT_EQ(test, -EINVAL, ibmveth_remove_buffer_from_pool(adapter, 
correlator, false));
+       KUNIT_EXPECT_EQ(test, -EINVAL, ibmveth_remove_buffer_from_pool(adapter, 
correlator, true));
+
+       correlator = ((u64)0 << 32) | adapter->rx_buff_pool[0].size;
+       KUNIT_EXPECT_EQ(test, -EINVAL, ibmveth_remove_buffer_from_pool(adapter, 
correlator, false));
+       KUNIT_EXPECT_EQ(test, -EINVAL, ibmveth_remove_buffer_from_pool(adapter, 
correlator, true));
+
+       correlator = (u64)0 | 0;
+       pool->skbuff[0] = NULL;
+       KUNIT_EXPECT_EQ(test, -EFAULT, ibmveth_remove_buffer_from_pool(adapter, 
correlator, false));
+       KUNIT_EXPECT_EQ(test, -EFAULT, ibmveth_remove_buffer_from_pool(adapter, 
correlator, true));
+}
+
+/**
+ * ibmveth_rxq_get_buffer_test - unit test for ibmveth_rxq_get_buffer
+ * @test: pointer to kunit structure
+ *
+ * Tests ibmveth_rxq_get_buffer. ibmveth_rxq_get_buffer also calls WARN_ON for
+ * the NULL returns, so dmesg should be checked to see that these warnings
+ * happened.
+ *
+ * Return: void
+ */
+static void ibmveth_rxq_get_buffer_test(struct kunit *test)
+{
+       struct ibmveth_adapter *adapter = kunit_kzalloc(test, sizeof(*adapter), 
GFP_KERNEL);
+       struct sk_buff *skb = kunit_kzalloc(test, sizeof(*skb), GFP_KERNEL);
+       struct ibmveth_buff_pool *pool;
+
+       KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adapter);
+       KUNIT_ASSERT_NOT_ERR_OR_NULL(test, skb);
+
+       INIT_WORK(&adapter->work, ibmveth_reset_kunit);
+
+       adapter->rx_queue.queue_len = 1;
+       adapter->rx_queue.index = 0;
+       adapter->rx_queue.queue_addr = kunit_kzalloc(test, sizeof(struct 
ibmveth_rx_q_entry),
+                                                    GFP_KERNEL);
+       KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adapter->rx_queue.queue_addr);
+
+       /* Set sane values for buffer pools */
+       for (int i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
+               ibmveth_init_buffer_pool(&adapter->rx_buff_pool[i], i,
+                                        pool_count[i], pool_size[i],
+                                        pool_active[i]);
+
+       pool = &adapter->rx_buff_pool[0];
+       pool->skbuff = kunit_kcalloc(test, pool->size, sizeof(void *), 
GFP_KERNEL);
+       KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pool->skbuff);
+
+       adapter->rx_queue.queue_addr[0].correlator = 
(u64)IBMVETH_NUM_BUFF_POOLS << 32 | 0;
+       KUNIT_EXPECT_PTR_EQ(test, NULL, ibmveth_rxq_get_buffer(adapter));
+
+       adapter->rx_queue.queue_addr[0].correlator = (u64)0 << 32 | 
adapter->rx_buff_pool[0].size;
+       KUNIT_EXPECT_PTR_EQ(test, NULL, ibmveth_rxq_get_buffer(adapter));
+
+       pool->skbuff[0] = skb;
+       adapter->rx_queue.queue_addr[0].correlator = (u64)0 << 32 | 0;
+       KUNIT_EXPECT_PTR_EQ(test, skb, ibmveth_rxq_get_buffer(adapter));
+}
+
+static struct kunit_case ibmveth_test_cases[] = {
+       KUNIT_CASE(ibmveth_remove_buffer_from_pool_test),
+       KUNIT_CASE(ibmveth_rxq_get_buffer_test),
+       {}
+};
+
+static struct kunit_suite ibmveth_test_suite = {
+       .name = "ibmveth-kunit-test",
+       .test_cases = ibmveth_test_cases,
+};
+
+kunit_test_suite(ibmveth_test_suite);
+#endif
-- 
2.49.0


Reply via email to