[dpdk-dev] [PATCH 02/19] pmd: Add PMD_INIT_NONPCI macros

Neil Horman nhorman at tuxdriver.com
Thu Apr 10 22:49:52 CEST 2014


This macro will allow nonpci based pmd driver to register an init function with
the core eal library so that they don't need to be referenced at link time.  The
macro expands to a constructor that calls an eal library registration function,
passing a pointer to the pmd's init routine.

Signed-off-by: Neil Horman <nhorman at tuxdriver.com>
---
 lib/librte_eal/common/Makefile                 |  2 +-
 lib/librte_eal/common/eal_common_nonpci_devs.c | 60 +++++++++++++++++++++++++-
 lib/librte_eal/common/include/rte_pmd.h        | 59 +++++++++++++++++++++++++
 3 files changed, 119 insertions(+), 2 deletions(-)
 create mode 100644 lib/librte_eal/common/include/rte_pmd.h

diff --git a/lib/librte_eal/common/Makefile b/lib/librte_eal/common/Makefile
index b9f3b88..e52b665 100644
--- a/lib/librte_eal/common/Makefile
+++ b/lib/librte_eal/common/Makefile
@@ -38,7 +38,7 @@ INC += rte_pci_dev_ids.h rte_per_lcore.h rte_prefetch.h rte_random.h
 INC += rte_rwlock.h rte_spinlock.h rte_tailq.h rte_interrupts.h rte_alarm.h
 INC += rte_string_fns.h rte_cpuflags.h rte_version.h rte_tailq_elem.h
 INC += rte_eal_memconfig.h rte_malloc_heap.h
-INC += rte_hexdump.h rte_devargs.h
+INC += rte_hexdump.h rte_devargs.h rte_pmd.h
 
 ifeq ($(CONFIG_RTE_INSECURE_FUNCTION_WARNING),y)
 INC += rte_warnings.h
diff --git a/lib/librte_eal/common/eal_common_nonpci_devs.c b/lib/librte_eal/common/eal_common_nonpci_devs.c
index 71cbb1e..0f94b65 100644
--- a/lib/librte_eal/common/eal_common_nonpci_devs.c
+++ b/lib/librte_eal/common/eal_common_nonpci_devs.c
@@ -34,6 +34,8 @@
 
 #include <string.h>
 #include <inttypes.h>
+#include <string.h>
+#include <sys/queue.h>
 #include <rte_string_fns.h>
 #ifdef RTE_LIBRTE_PMD_RING
 #include <rte_eth_ring.h>
@@ -46,6 +48,8 @@
 #endif
 #include <rte_debug.h>
 #include <rte_devargs.h>
+#include <rte_pmd.h>
+#include <rte_log.h>
 #include "eal_private.h"
 
 struct device_init {
@@ -79,17 +83,54 @@ struct device_init dev_types[] = {
 		}
 };
 
+TAILQ_HEAD(pmd_nonpci_list, pmd_nonpci_entry);
+
+/* Definition for shared object drivers. */
+struct pmd_nonpci_entry {
+        TAILQ_ENTRY(pmd_noncpi_entry) next;
+
+        char    name[PATH_MAX];
+	int (*pmd_initfn)(const char *, const char *);
+};
+
+/* List of external loadable drivers */
+static struct pmd_nonpci_list pmd_nonpci_init_list =
+TAILQ_HEAD_INITIALIZER(pmd_nonpci_init_list);
+
+void rte_eal_nonpci_dev_init_register(const char *name, int (*dev_initfn)(const char *, const char *))
+{
+	struct pmd_nonpci_entry *new = malloc(sizeof(struct pmd_nonpci_entry));
+
+	if (!new) {
+		printf("Not enough memory to register %s\n", name);
+		goto out;
+	}
+
+	memset(new->name, 0, PATH_MAX);
+	strncpy(new->name, name, PATH_MAX);
+	new->name[PATH_MAX-1] = 0;
+	new->pmd_initfn = dev_initfn;
+	printf("REGISTERING INIT ROUTINE FOR %s\n", new->name);
+	TAILQ_INSERT_TAIL(&pmd_nonpci_init_list, new, next);
+out:
+	return;	
+}
+
+
 int
 rte_eal_non_pci_ethdev_init(void)
 {
 	struct rte_devargs *devargs;
+	struct pmd_nonpci_entry *entry;
 	uint8_t i;
+	int found = 0;
 
 	/* call the init function for each virtual device */
 	TAILQ_FOREACH(devargs, &devargs_list, next) {
 
 		if (devargs->type != RTE_DEVTYPE_VIRTUAL)
 			continue;
+		found = 0;
 
 		for (i = 0; i < NUM_DEV_TYPES; i++) {
 			/* search a driver prefix in virtual device name */
@@ -98,14 +139,31 @@ rte_eal_non_pci_ethdev_init(void)
 				     sizeof(dev_types[i].dev_prefix) - 1)) {
 				dev_types[i].init_fn(devargs->virtual.drv_name,
 						     devargs->args);
+				found = 1;
 				break;
 			}
 		}
 
-		if (i == NUM_DEV_TYPES) {
+		if (found)
+			continue;
+
+		TAILQ_FOREACH(entry, &pmd_nonpci_init_list, next) {
+			if (!entry->name)
+				continue;
+			if (!strncmp(entry->name,
+				     devargs->virtual.drv_name,
+				     strlen(entry->name))) {
+				RTE_LOG(INFO, PMD, "Initalizing pmd %s\n", devargs->virtual.drv_name);
+				found = 1;
+				entry->pmd_initfn(devargs->virtual.drv_name, devargs->args);
+			}
+		}
+
+		if (!found) {
 			rte_panic("no driver found for %s\n",
 				  devargs->virtual.drv_name);
 		}
 	}
+
 	return 0;
 }
diff --git a/lib/librte_eal/common/include/rte_pmd.h b/lib/librte_eal/common/include/rte_pmd.h
new file mode 100644
index 0000000..75a4895
--- /dev/null
+++ b/lib/librte_eal/common/include/rte_pmd.h
@@ -0,0 +1,59 @@
+/*-
+ *   BSD LICENSE
+ * 
+ *   Copyright(c) 2010-2014 Neil Horman <nhorman at tuxdriver.com. 
+ *   All rights reserved.
+ * 
+ *   Redistribution and use in source and binary forms, with or without
+ *   modification, are permitted provided that the following conditions
+ *   are met:
+ * 
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in
+ *       the documentation and/or other materials provided with the
+ *       distribution.
+ *     * Neither the name of Intel Corporation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ * 
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/**
+ * @file
+ *
+ * API for error cause tracking
+ */
+
+#ifndef _RTE_PMD_H_
+#define _RTE_PMD_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern void rte_eal_nonpci_dev_init_register(const char *name, int (*dev_initfn)(const char *, const char *));
+#define PMD_INIT_NONPCI(x,n)\
+void devinitfn_ ##x(void);\
+void __attribute__((constructor, used)) devinitfn_ ##x(void)\
+{\
+	rte_eal_nonpci_dev_init_register(n,x);\
+}
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _RTE_PMD_H_ */
-- 
1.8.3.1



More information about the dev mailing list