[qca-nss-clients] Add c2c_rx netlink module

Support Multicast event notifications

Change-Id: Ice7a11ded9e6301344464926af9ec39c32cfbbd9
Signed-off-by: Wayne Tan <wtan@codeaurora.org>
diff --git a/netlink/Makefile b/netlink/Makefile
index 0d29509..0e7d357 100644
--- a/netlink/Makefile
+++ b/netlink/Makefile
@@ -14,6 +14,7 @@
 ccflags-y += -DCONFIG_NSS_NLDYNAMIC_INTERFACE=1
 ccflags-y += -DCONFIG_NSS_NLN2H=1
 ccflags-y += -DCONFIG_NSS_NLC2C_TX=1
+ccflags-y += -DCONFIG_NSS_NLC2C_RX=1
 
 qca-nss-netlink-objs := nss_nl.o
 qca-nss-netlink-objs += nss_nlgre_redir_family.o
@@ -30,6 +31,7 @@
 qca-nss-netlink-objs += nss_nldynamic_interface.o
 qca-nss-netlink-objs += nss_nln2h.o
 qca-nss-netlink-objs += nss_nlc2c_tx.o
+qca-nss-netlink-objs += nss_nlc2c_rx.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_nlc2c_rx_if.h b/netlink/include/nss_nlc2c_rx_if.h
new file mode 100644
index 0000000..312d869
--- /dev/null
+++ b/netlink/include/nss_nlc2c_rx_if.h
@@ -0,0 +1,32 @@
+/*
+ **************************************************************************
+ * 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_nlc2c_rx_if.h
+ *	NSS Netlink c2c_rx headers
+ */
+#ifndef __NSS_NLC2C_RX_IF_H
+#define __NSS_NLC2C_RX_IF_H
+
+/**
+ * c2c_rx forwarding Family
+ */
+#define NSS_NLC2C_RX_FAMILY "nss_nlc2c_rx"
+#define NSS_NLC2C_RX_MCAST_GRP "nss_nlc2c_rx_mc"
+
+#endif /* __NSS_NLC2C_RX_IF_H */
diff --git a/netlink/include/nss_nlcmn_if.h b/netlink/include/nss_nlcmn_if.h
index 511f1bf..2050516 100644
--- a/netlink/include/nss_nlcmn_if.h
+++ b/netlink/include/nss_nlcmn_if.h
@@ -42,6 +42,7 @@
  */
 enum nss_nlcmn_subsys {
 	NSS_NLCMN_SUBSYS_CAPWAP,
+	NSS_NLCMN_SUBSYS_C2C_RX,
 	NSS_NLCMN_SUBSYS_C2C_TX,
 	NSS_NLCMN_SUBSYS_DYNAMIC_INTERFACE,
 	NSS_NLCMN_SUBSYS_EDMA,
diff --git a/netlink/nss_nl.c b/netlink/nss_nl.c
index 4d2663c..37b2dec 100644
--- a/netlink/nss_nl.c
+++ b/netlink/nss_nl.c
@@ -60,6 +60,8 @@
 #include "nss_nln2h_if.h"
 #include "nss_nlc2c_tx.h"
 #include "nss_nlc2c_tx_if.h"
+#include "nss_nlc2c_rx.h"
+#include "nss_nlc2c_rx_if.h"
 #if defined (CONFIG_NSS_NLCRYPTO)
 #include "nss_nlcrypto_if.h"
 #else
@@ -214,6 +216,15 @@
 		.exit = NSS_NLC2C_TX_EXIT,		/* exit */
 		.valid = CONFIG_NSS_NLC2C_TX		/* 1 or 0 */
 	},
+	{
+		/*
+		 * NSS_NLC2C_RX
+		 */
+		.name = NSS_NLC2C_RX_FAMILY,		/* c2c_rx */
+		.entry = NSS_NLC2C_RX_INIT,		/* init */
+		.exit = NSS_NLC2C_RX_EXIT,		/* exit */
+		.valid = CONFIG_NSS_NLC2C_RX		/* 1 or 0 */
+	},
 };
 
 #define NSS_NL_FAMILY_HANDLER_SZ ARRAY_SIZE(family_handlers)
diff --git a/netlink/nss_nlc2c_rx.c b/netlink/nss_nlc2c_rx.c
new file mode 100644
index 0000000..73e8e60
--- /dev/null
+++ b/netlink/nss_nlc2c_rx.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_nlc2c_rx.c
+ *	NSS Netlink c2c_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 <linux/notifier.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_nlc2c_rx_if.h"
+#include "nss_c2c_rx.h"
+
+/*
+ * prototypes
+ */
+static int nss_nlc2c_rx_ops_get_stats(struct sk_buff *skb, struct genl_info *info);
+static int nss_nlc2c_rx_process_notify(struct notifier_block *nb,  unsigned long val, void *data);
+
+/*
+ * c2c_rx family definition
+ */
+static struct genl_family nss_nlc2c_rx_family = {
+	.id = GENL_ID_GENERATE,						/* Auto generate ID */
+	.name = NSS_NLC2C_RX_FAMILY,					/* family name string */
+	.hdrsize = sizeof(struct nss_c2c_rx_stats_notification),	/* NSS NETLINK c2c_rx stats */
+	.version = NSS_NL_VER,						/* Set it to NSS_NLC2C_RX 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_nlc2c_rx_mcgrp[] = {
+	{.name = NSS_NLC2C_RX_MCAST_GRP},
+};
+
+/*
+ * operation table called by the generic netlink layer based on the command
+ */
+static struct genl_ops nss_nlc2c_rx_ops[] = {
+	{.cmd = NSS_STATS_EVENT_NOTIFY, .doit = nss_nlc2c_rx_ops_get_stats},
+};
+
+/*
+ * stats call back handler for c2c_rx from NSS
+ */
+static struct notifier_block nss_c2c_rx_stats_notifier_nb = {
+	.notifier_call = nss_nlc2c_rx_process_notify,
+};
+
+/*
+ * nss_nlc2c_rx_ops_get_stats()
+ *	get stats handler
+ */
+static int nss_nlc2c_rx_ops_get_stats(struct sk_buff *skb, struct genl_info *info)
+{
+	return 0;
+}
+
+/*
+ * nss_nlc2c_rx_process_notify()
+ *	process notification messages from NSS
+ */
+static int nss_nlc2c_rx_process_notify(struct notifier_block *nb, unsigned long val, void *data)
+{
+	struct sk_buff *skb;
+	struct nss_c2c_rx_stats_notification *nss_stats, *nl_stats;
+
+	skb = nss_nl_new_msg(&nss_nlc2c_rx_family, NSS_NLCMN_SUBSYS_C2C_RX);
+	if (!skb) {
+		nss_nl_error("unable to allocate NSS_NLC2C_RX event\n");
+		return NOTIFY_DONE;
+	}
+
+	nl_stats = nss_nl_get_data(skb);
+	nss_stats = (struct nss_c2c_rx_stats_notification *)data;
+	memcpy(nl_stats, nss_stats, sizeof(struct nss_c2c_rx_stats_notification));
+	nss_nl_mcast_event(&nss_nlc2c_rx_family, skb);
+
+	return NOTIFY_DONE;
+}
+
+/*
+ * nss_nlc2c_rx_init()
+ *	handler init
+ */
+bool nss_nlc2c_rx_init(void)
+{
+	int error,ret;
+
+	nss_nl_info_always("Init NSS netlink c2c_rx handler\n");
+
+	/*
+	 * register NETLINK ops with the family
+	 */
+	error = genl_register_family_with_ops_groups(&nss_nlc2c_rx_family, nss_nlc2c_rx_ops, nss_nlc2c_rx_mcgrp);
+	if (error) {
+		nss_nl_info_always("Error: unable to register c2c_rx family\n");
+		return false;
+	}
+
+	/*
+	 * register device call back handler for c2c_rx from NSS
+	 */
+	ret = nss_c2c_rx_stats_register_notifier(&nss_c2c_rx_stats_notifier_nb);
+	if (ret) {
+		nss_nl_info_always("Error: retreiving the NSS Context \n");
+		genl_unregister_family(&nss_nlc2c_rx_family);
+		return false;
+	}
+
+	return true;
+}
+
+/*
+ * nss_nlc2c_rx_exit()
+ *	handler exit
+ */
+bool nss_nlc2c_rx_exit(void)
+{
+	int error;
+
+	nss_nl_info_always("Exit NSS netlink c2c_rx handler\n");
+
+	/*
+	 * Unregister the device callback handler for c2c_rx
+	 */
+	nss_c2c_rx_stats_unregister_notifier(&nss_c2c_rx_stats_notifier_nb);
+
+	/*
+	 * unregister the ops family
+	 */
+	error = genl_unregister_family(&nss_nlc2c_rx_family);
+	if (error) {
+		nss_nl_info_always("unable to unregister c2c_rx NETLINK family\n");
+		return false;
+	}
+
+	return true;
+}
diff --git a/netlink/nss_nlc2c_rx.h b/netlink/nss_nlc2c_rx.h
new file mode 100644
index 0000000..a7edf12
--- /dev/null
+++ b/netlink/nss_nlc2c_rx.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_nlc2c_rx.h
+ *	NSS Netlink c2c_rx API definitions
+ */
+#ifndef __NSS_NLC2C_RX_H
+#define __NSS_NLC2C_RX_H
+
+bool nss_nlc2c_rx_init(void);
+bool nss_nlc2c_rx_exit(void);
+
+#if defined(CONFIG_NSS_NLC2C_RX)
+#define NSS_NLC2C_RX_INIT nss_nlc2c_rx_init
+#define NSS_NLC2C_RX_EXIT nss_nlc2c_rx_exit
+#else
+#define NSS_NLC2C_RX_INIT 0
+#define NSS_NLC2C_RX_EXIT 0
+#endif /* !CONFIG_NSS_NLC2C_RX */
+
+#endif /* __NSS_NLC2C_RX_H */