[qca-nss-clients] Add eth_rx netlink module

Support Multicast event notifications

Change-Id: Iad1cb74fde656cb933a6ef02860278892f3837f2
Signed-off-by: Wayne Tan <wtan@codeaurora.org>
diff --git a/netlink/Makefile b/netlink/Makefile
index 1a73c1c..c2ed18d 100644
--- a/netlink/Makefile
+++ b/netlink/Makefile
@@ -9,6 +9,7 @@
 ccflags-y += -DCONFIG_NSS_NLIPSEC=1
 ccflags-y += -DCONFIG_NSS_NLOAM=1
 ccflags-y += -DCONFIG_NSS_NLGRE_REDIR_FAMILY=1
+ccflags-y += -DCONFIG_NSS_NLETHRX=1
 
 qca-nss-netlink-objs := nss_nl.o
 qca-nss-netlink-objs += nss_nlgre_redir_family.o
@@ -20,6 +21,7 @@
 qca-nss-netlink-objs += nss_nlipv6.o
 qca-nss-netlink-objs += nss_nlipsec.o
 qca-nss-netlink-objs += nss_nloam.o
+qca-nss-netlink-objs += nss_nlethrx.o
 
 CAPWAP_ENABLED:=CONFIG_PACKAGE_kmod-qca-nss-drv-capwapmgr=y
 CAPWAP_CONFIG:=$(shell  grep $(CAPWAP_ENABLED) $(TOPDIR)/.config)
diff --git a/netlink/include/nss_nlcmn_if.h b/netlink/include/nss_nlcmn_if.h
index 478eaf1..8ac0fdf 100644
--- a/netlink/include/nss_nlcmn_if.h
+++ b/netlink/include/nss_nlcmn_if.h
@@ -41,6 +41,7 @@
  * @brief NSS subsystems in alphabetical order for nssinfo tool
  */
 enum nss_nlcmn_subsys {
+	NSS_NLCMN_SUBSYS_ETHRX,
 	NSS_NLCMN_SUBSYS_IPV4,
 	NSS_NLCMN_SUBSYS_MAX
 };
diff --git a/netlink/include/nss_nlethrx_if.h b/netlink/include/nss_nlethrx_if.h
new file mode 100644
index 0000000..91e2ee9
--- /dev/null
+++ b/netlink/include/nss_nlethrx_if.h
@@ -0,0 +1,33 @@
+/*
+ **************************************************************************
+ * Copyright (c) 2020, The Linux Foundation. All rights reserved.
+ *
+ * Permission to use, copy, modify, and/or distribute this software for
+ * any purpose with or without fee is hereby granted, provided that the
+ * above copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
+ * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ **************************************************************************
+ */
+
+/*
+ * @file nss_nlethrx_if.h
+ *	NSS Netlink eth_rx headers
+ */
+#ifndef __NSS_NLETHRX_IF_H
+#define __NSS_NLETHRX_IF_H
+#include "nss_eth_rx.h"
+
+/**
+ * eth_rx forwarding Family
+ */
+#define NSS_NLETHRX_FAMILY "nss_nlethrx"
+#define NSS_NLETHRX_MCAST_GRP "nss_nlethrx_mc"
+
+#endif /* __NSS_NLETHRX_IF_H */
diff --git a/netlink/nss_nl.c b/netlink/nss_nl.c
index d67e563..b425fce 100644
--- a/netlink/nss_nl.c
+++ b/netlink/nss_nl.c
@@ -48,6 +48,8 @@
 #include "nss_nlipv6_if.h"
 #include "nss_nloam.h"
 #include "nss_nloam_if.h"
+#include "nss_nlethrx.h"
+#include "nss_nlethrx_if.h"
 #if defined (CONFIG_NSS_NLCRYPTO)
 #include "nss_nlcrypto_if.h"
 #else
@@ -157,6 +159,15 @@
 		.exit = NSS_NLDTLS_EXIT,		/* exit */
 		.valid = CONFIG_NSS_NLDTLS		/* 1 or 0 */
 	},
+	{
+		/*
+		 * NSS_NLETHRX
+		 */
+		.name = NSS_NLETHRX_FAMILY,		/* ethrx */
+		.entry = NSS_NLETHRX_INIT,		/* init */
+		.exit = NSS_NLETHRX_EXIT,		/* exit */
+		.valid = CONFIG_NSS_NLETHRX		/* 1 or 0 */
+	},
 };
 
 #define NSS_NL_FAMILY_HANDLER_SZ ARRAY_SIZE(family_handlers)
diff --git a/netlink/nss_nlethrx.c b/netlink/nss_nlethrx.c
new file mode 100644
index 0000000..9ec1bbb
--- /dev/null
+++ b/netlink/nss_nlethrx.c
@@ -0,0 +1,172 @@
+/*
+ **************************************************************************
+ * Copyright (c) 2020, The Linux Foundation. All rights reserved.
+ *
+ * Permission to use, copy, modify, and/or distribute this software for
+ * any purpose with or without fee is hereby granted, provided that the
+ * above copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
+ * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ **************************************************************************
+ */
+
+/*
+ * nss_nlethrx.c
+ *	NSS Netlink eth_rx Handler
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/version.h>
+#include <linux/netlink.h>
+#include <linux/version.h>
+#include <linux/vmalloc.h>
+
+#include <net/genetlink.h>
+#include <net/sock.h>
+
+#include <nss_api_if.h>
+#include <nss_cmn.h>
+#include <nss_nl_if.h>
+#include "nss_nl.h"
+#include "nss_nlcmn_if.h"
+#include "nss_nlethrx_if.h"
+#include "nss_eth_rx.h"
+
+/*
+ * prototypes
+ */
+static int nss_nlethrx_ops_get_stats(struct sk_buff *skb, struct genl_info *info);
+static int nss_nlethrx_process_notify(struct notifier_block *nb, unsigned long val, void *data);
+
+/*
+ * eth_rx family definition
+ */
+static struct genl_family nss_nlethrx_family = {
+	.id = GENL_ID_GENERATE,						/* Auto generate ID */
+	.name = NSS_NLETHRX_FAMILY,					/* family name string */
+	.hdrsize = sizeof(struct nss_eth_rx_stats_notification),	/* NSS NETLINK eth_rx rule */
+	.version = NSS_NL_VER,						/* Set it to NSS_NLETHRX version */
+	.maxattr = NSS_STATS_EVENT_MAX,					/* maximum commands supported */
+	.netnsok = true,
+	.pre_doit = NULL,
+	.post_doit = NULL,
+};
+
+/*
+ * multicast group for sending message status & events
+ */
+static const struct genl_multicast_group nss_nlethrx_mcgrp[] = {
+	{.name = NSS_NLETHRX_MCAST_GRP},
+};
+
+/*
+ * operation table called by the generic netlink layer based on the command
+ */
+static struct genl_ops nss_nlethrx_ops[] = {
+	{.cmd = NSS_STATS_EVENT_NOTIFY, .doit = nss_nlethrx_ops_get_stats,},
+};
+
+/*
+ * statistics call back handler for eth_rx from NSS
+ */
+static struct notifier_block nss_eth_rx_stats_notifier_nb = {
+	.notifier_call = nss_nlethrx_process_notify,
+};
+
+/*
+ * nss_nlethrx_ops_get_stats()
+ *	get stats handler
+ */
+static int nss_nlethrx_ops_get_stats(struct sk_buff *skb, struct genl_info *info)
+{
+	return 0;
+}
+
+/*
+ * nss_nlethrx_process_notify()
+ *	process notification messages from NSS
+ */
+static int nss_nlethrx_process_notify(struct notifier_block *nb, unsigned long val, void *data)
+{
+	struct sk_buff *skb;
+	struct nss_eth_rx_stats_notification *nl_stats, *nss_stats;
+
+	skb = nss_nl_new_msg(&nss_nlethrx_family, NSS_NLCMN_SUBSYS_ETHRX);
+	if (!skb) {
+		nss_nl_error("unable to allocate NSS_NLETHRX event\n");
+		return NOTIFY_DONE;
+	}
+
+	nl_stats = nss_nl_get_data(skb);
+	nss_stats = (struct nss_eth_rx_stats_notification *)data;
+	memcpy(nl_stats, nss_stats, sizeof(struct nss_eth_rx_stats_notification));
+	nss_nl_mcast_event(&nss_nlethrx_family, skb);
+
+	return NOTIFY_DONE;
+}
+
+/*
+ * nss_nlethrx_init()
+ *	handler init
+ */
+bool nss_nlethrx_init(void)
+{
+	int error,ret;
+
+	nss_nl_info_always("Init NSS netlink eth_rx handler\n");
+
+	/*
+	 * register NETLINK ops with the family
+	 */
+	error = genl_register_family_with_ops_groups(&nss_nlethrx_family, nss_nlethrx_ops, nss_nlethrx_mcgrp);
+	if (error) {
+		nss_nl_info_always("Error: unable to register eth_rx family\n");
+		return false;
+	}
+
+	/*
+	 * register device call back handler for ethrx from NSS
+	 */
+	ret = nss_eth_rx_stats_register_notifier(&nss_eth_rx_stats_notifier_nb);
+	if (ret) {
+		nss_nl_info_always("Error: retreiving the NSS Context \n");
+		genl_unregister_family(&nss_nlethrx_family);
+		return false;
+	}
+
+	return true;
+}
+
+/*
+ * nss_nlethrx_exit()
+ *	handler exit
+ */
+bool nss_nlethrx_exit(void)
+{
+	int error;
+
+	nss_nl_info_always("Exit NSS netlink eth_rx handler\n");
+
+	/*
+	 * Unregister the device callback handler for ethrx
+	 */
+	nss_eth_rx_stats_unregister_notifier(&nss_eth_rx_stats_notifier_nb);
+
+	/*
+	 * unregister the ops family
+	 */
+	error = genl_unregister_family(&nss_nlethrx_family);
+	if (error) {
+		nss_nl_info_always("unable to unregister eth_rx NETLINK family\n");
+		return false;
+	}
+
+	return true;
+}
diff --git a/netlink/nss_nlethrx.h b/netlink/nss_nlethrx.h
new file mode 100644
index 0000000..1081034
--- /dev/null
+++ b/netlink/nss_nlethrx.h
@@ -0,0 +1,37 @@
+/*
+ **************************************************************************
+ * Copyright (c) 2020, The Linux Foundation. All rights reserved.
+ *
+ * Permission to use, copy, modify, and/or distribute this software for
+ * any purpose with or without fee is hereby granted, provided that the
+ * above copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
+ * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ **************************************************************************
+ */
+
+/*
+ * nss_nlethrx.h
+ *      NSS Netlink eth_rx API definitions
+ */
+#ifndef __NSS_NLETHRX_H
+#define __NSS_NLETHRX_H
+
+bool nss_nlethrx_init(void);
+bool nss_nlethrx_exit(void);
+
+#if defined(CONFIG_NSS_NLETHRX)
+#define NSS_NLETHRX_INIT nss_nlethrx_init
+#define NSS_NLETHRX_EXIT nss_nlethrx_exit
+#else
+#define NSS_NLETHRX_INIT 0
+#define NSS_NLETHRX_EXIT 0
+#endif /* !CONFIG_NSS_NLETHRX */
+
+#endif /* __NSS_NLETHRX_H */