瀏覽代碼

net: hsr: move notification handling to a common file for re-use

Move the code for handling interface notification to a common file
hsr_prp_main.c so that it can be re-used from prp main code as well.

Signed-off-by: Murali Karicheri <m-karicheri2@ti.com>
Murali Karicheri 6 年之前
父節點
當前提交
0c33f2402e
共有 4 個文件被更改,包括 129 次插入104 次删除
  1. 3 2
      net/hsr-prp/Makefile
  2. 6 102
      net/hsr-prp/hsr_main.c
  3. 118 0
      net/hsr-prp/hsr_prp_main.c
  4. 2 0
      net/hsr-prp/hsr_prp_main.h

+ 3 - 2
net/hsr-prp/Makefile

@@ -4,6 +4,7 @@
 
 obj-$(CONFIG_HSR_PRP)	+= hsr-prp.o
 
-hsr-prp-y		:= hsr_main.o hsr_prp_framereg.o hsr_prp_device.o \
-			   hsr_netlink.o hsr_prp_slave.o hsr_prp_forward.o
+hsr-prp-y		:= hsr_prp_main.o hsr_main.o hsr_prp_framereg.o \
+			   hsr_prp_device.o hsr_netlink.o hsr_prp_slave.o \
+			   hsr_prp_forward.o
 hsr-prp-$(CONFIG_DEBUG_FS) += hsr_prp_debugfs.o

+ 6 - 102
net/hsr-prp/hsr_main.c

@@ -6,106 +6,9 @@
  */
 
 #include <linux/netdevice.h>
-#include <linux/rculist.h>
-#include <linux/timer.h>
-#include <linux/etherdevice.h>
-#include "hsr_prp_main.h"
-#include "hsr_prp_device.h"
-#include "hsr_netlink.h"
-#include "hsr_prp_framereg.h"
-#include "hsr_prp_slave.h"
-
-static int hsr_netdev_notify(struct notifier_block *nb, unsigned long event,
-			     void *ptr)
-{
-	struct net_device *dev;
-	struct hsr_prp_port *port, *master;
-	struct hsr_prp_priv *priv;
-	int mtu_max;
-	int res;
-
-	dev = netdev_notifier_info_to_dev(ptr);
-	port = hsr_prp_port_get_rtnl(dev);
-	if (!port) {
-		if (!is_hsr_prp_master(dev))
-			return NOTIFY_DONE;	/* Not an HSR device */
-		priv = netdev_priv(dev);
-		port = hsr_prp_get_port(priv, HSR_PRP_PT_MASTER);
-		if (!port) {
-			/* Resend of notification concerning removed device? */
-			return NOTIFY_DONE;
-		}
-	} else {
-		priv = port->priv;
-	}
-
-	switch (event) {
-	case NETDEV_UP:		/* Administrative state DOWN */
-	case NETDEV_DOWN:	/* Administrative state UP */
-	case NETDEV_CHANGE:	/* Link (carrier) state changes */
-		hsr_prp_check_carrier_and_operstate(priv);
-		break;
-	case NETDEV_CHANGEADDR:
-		if (port->type == HSR_PRP_PT_MASTER) {
-			/* This should not happen since there's no
-			 * ndo_set_mac_address() for HSR devices - i.e. not
-			 * supported.
-			 */
-			break;
-		}
-
-		master = hsr_prp_get_port(priv, HSR_PRP_PT_MASTER);
-
-		if (port->type == HSR_PRP_PT_SLAVE_A) {
-			ether_addr_copy(master->dev->dev_addr, dev->dev_addr);
-			call_netdevice_notifiers(NETDEV_CHANGEADDR,
-						 master->dev);
-		}
 
-		/* Make sure we recognize frames from ourselves in hsr_rcv() */
-		port = hsr_prp_get_port(priv, HSR_PRP_PT_SLAVE_B);
-		res = hsr_prp_create_self_node(&priv->self_node_db,
-					       master->dev->dev_addr,
-					       port ? port->dev->dev_addr :
-						      master->dev->dev_addr);
-		if (res)
-			netdev_warn(master->dev,
-				    "Could not update HSR node address.\n");
-		break;
-	case NETDEV_CHANGEMTU:
-		if (port->type == HSR_PRP_PT_MASTER)
-			break; /* Handled in ndo_change_mtu() */
-		mtu_max = hsr_prp_get_max_mtu(port->priv);
-		master = hsr_prp_get_port(port->priv, HSR_PRP_PT_MASTER);
-		master->dev->mtu = mtu_max;
-		break;
-	case NETDEV_UNREGISTER:
-		hsr_prp_del_port(port);
-		break;
-	case NETDEV_PRE_TYPE_CHANGE:
-		/* HSR works only on Ethernet devices. Refuse slave to change
-		 * its type.
-		 */
-		return NOTIFY_BAD;
-	}
-
-	return NOTIFY_DONE;
-}
-
-struct hsr_prp_port *hsr_prp_get_port(struct hsr_prp_priv *priv,
-				      enum hsr_prp_port_type pt)
-{
-	struct hsr_prp_port *port;
-
-	hsr_prp_for_each_port(priv, port)
-		if (port->type == pt)
-			return port;
-	return NULL;
-}
-
-static struct notifier_block hsr_nb = {
-	.notifier_call = hsr_netdev_notify,	/* Slave event notifications */
-};
+#include "hsr_netlink.h"
+#include "hsr_prp_main.h"
 
 static int __init hsr_init(void)
 {
@@ -113,15 +16,16 @@ static int __init hsr_init(void)
 
 	BUILD_BUG_ON(sizeof(struct hsr_tag) != HSR_PRP_HLEN);
 
-	register_netdevice_notifier(&hsr_nb);
-	res = hsr_netlink_init();
+	res = hsr_prp_register_notifier();
+	if (!res)
+		res = hsr_netlink_init();
 
 	return res;
 }
 
 static void __exit hsr_exit(void)
 {
-	unregister_netdevice_notifier(&hsr_nb);
+	hsr_prp_unregister_notifier();
 	hsr_netlink_exit();
 }
 

+ 118 - 0
net/hsr-prp/hsr_prp_main.c

@@ -0,0 +1,118 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright 2011-2014 Autronica Fire and Security AS
+ *
+ * Author(s):
+ *	2011-2014 Arvid Brodin, arvid.brodin@alten.se
+ */
+
+#include <linux/netdevice.h>
+#include <linux/rculist.h>
+#include <linux/timer.h>
+#include <linux/etherdevice.h>
+#include "hsr_prp_main.h"
+#include "hsr_prp_device.h"
+#include "hsr_netlink.h"
+#include "hsr_prp_framereg.h"
+#include "hsr_prp_slave.h"
+
+static int netdev_notify(struct notifier_block *nb, unsigned long event,
+			 void *ptr)
+{
+	struct net_device *dev;
+	struct hsr_prp_port *port, *master;
+	struct hsr_prp_priv *priv;
+	int mtu_max;
+	int res;
+
+	dev = netdev_notifier_info_to_dev(ptr);
+	port = hsr_prp_port_get_rtnl(dev);
+	if (!port) {
+		if (!is_hsr_prp_master(dev))
+			return NOTIFY_DONE;	/* Not an HSR device */
+		priv = netdev_priv(dev);
+		port = hsr_prp_get_port(priv, HSR_PRP_PT_MASTER);
+		if (!port) {
+			/* Resend of notification concerning removed device? */
+			return NOTIFY_DONE;
+		}
+	} else {
+		priv = port->priv;
+	}
+
+	switch (event) {
+	case NETDEV_UP:		/* Administrative state DOWN */
+	case NETDEV_DOWN:	/* Administrative state UP */
+	case NETDEV_CHANGE:	/* Link (carrier) state changes */
+		hsr_prp_check_carrier_and_operstate(priv);
+		break;
+	case NETDEV_CHANGEADDR:
+		if (port->type == HSR_PRP_PT_MASTER) {
+			/* This should not happen since there's no
+			 * ndo_set_mac_address() for HSR devices - i.e. not
+			 * supported.
+			 */
+			break;
+		}
+
+		master = hsr_prp_get_port(priv, HSR_PRP_PT_MASTER);
+
+		if (port->type == HSR_PRP_PT_SLAVE_A) {
+			ether_addr_copy(master->dev->dev_addr, dev->dev_addr);
+			call_netdevice_notifiers(NETDEV_CHANGEADDR,
+						 master->dev);
+		}
+
+		/* Make sure we recognize frames from ourselves in hsr_rcv() */
+		port = hsr_prp_get_port(priv, HSR_PRP_PT_SLAVE_B);
+		res = hsr_prp_create_self_node(&priv->self_node_db,
+					       master->dev->dev_addr,
+					       port ? port->dev->dev_addr :
+						      master->dev->dev_addr);
+		if (res)
+			netdev_warn(master->dev,
+				    "Could not update HSR node address.\n");
+		break;
+	case NETDEV_CHANGEMTU:
+		if (port->type == HSR_PRP_PT_MASTER)
+			break; /* Handled in ndo_change_mtu() */
+		mtu_max = hsr_prp_get_max_mtu(port->priv);
+		master = hsr_prp_get_port(port->priv, HSR_PRP_PT_MASTER);
+		master->dev->mtu = mtu_max;
+		break;
+	case NETDEV_UNREGISTER:
+		hsr_prp_del_port(port);
+		break;
+	case NETDEV_PRE_TYPE_CHANGE:
+		/* HSR works only on Ethernet devices. Refuse slave to change
+		 * its type.
+		 */
+		return NOTIFY_BAD;
+	}
+
+	return NOTIFY_DONE;
+}
+
+struct hsr_prp_port *hsr_prp_get_port(struct hsr_prp_priv *priv,
+				      enum hsr_prp_port_type pt)
+{
+	struct hsr_prp_port *port;
+
+	hsr_prp_for_each_port(priv, port)
+		if (port->type == pt)
+			return port;
+	return NULL;
+}
+
+static struct notifier_block hsr_nb = {
+	.notifier_call = netdev_notify,	/* Slave event notifications */
+};
+
+int hsr_prp_register_notifier(void)
+{
+	return register_netdevice_notifier(&hsr_nb);
+}
+
+void hsr_prp_unregister_notifier(void)
+{
+	unregister_netdevice_notifier(&hsr_nb);
+}

+ 2 - 0
net/hsr-prp/hsr_prp_main.h

@@ -185,6 +185,8 @@ static inline u16 hsr_get_skb_sequence_nr(struct sk_buff *skb)
 	return ntohs(hsr_ethhdr->hsr_tag.sequence_nr);
 }
 
+int hsr_prp_register_notifier(void);
+void hsr_prp_unregister_notifier(void);
 #if IS_ENABLED(CONFIG_DEBUG_FS)
 int hsr_prp_debugfs_init(struct hsr_prp_priv *priv, struct net_device *ndev);
 void hsr_prp_debugfs_term(struct hsr_prp_priv *priv);