diff mbox series

[wpan-next,v3,02/11] ieee802154: Internal PAN management

Message ID 20230918150809.275058-3-miquel.raynal@bootlin.com (mailing list archive)
State Superseded
Headers show
Series ieee802154: Associations between devices | expand

Commit Message

Miquel Raynal Sept. 18, 2023, 3:08 p.m. UTC
Introduce structures to describe peer devices in a PAN as well as a few
related helpers. We basically care about:
- Our unique parent after associating with a coordinator.
- Peer devices, children, which successfully associated with us.

Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
---
 include/net/cfg802154.h | 46 +++++++++++++++++++++++++
 net/ieee802154/Makefile |  2 +-
 net/ieee802154/core.c   |  2 ++
 net/ieee802154/pan.c    | 75 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 124 insertions(+), 1 deletion(-)
 create mode 100644 net/ieee802154/pan.c

Comments

Stefan Schmidt Sept. 20, 2023, 6:08 p.m. UTC | #1
Hello.

On 18.09.23 17:08, Miquel Raynal wrote:
> Introduce structures to describe peer devices in a PAN as well as a few
> related helpers. We basically care about:
> - Our unique parent after associating with a coordinator.
> - Peer devices, children, which successfully associated with us.
> 
> Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
> ---
>   include/net/cfg802154.h | 46 +++++++++++++++++++++++++
>   net/ieee802154/Makefile |  2 +-
>   net/ieee802154/core.c   |  2 ++
>   net/ieee802154/pan.c    | 75 +++++++++++++++++++++++++++++++++++++++++
>   4 files changed, 124 insertions(+), 1 deletion(-)
>   create mode 100644 net/ieee802154/pan.c
> 
> diff --git a/include/net/cfg802154.h b/include/net/cfg802154.h
> index f79ce133e51a..6c7193b4873c 100644
> --- a/include/net/cfg802154.h
> +++ b/include/net/cfg802154.h
> @@ -303,6 +303,22 @@ struct ieee802154_coord_desc {
>   	bool gts_permit;
>   };
>   
> +/**
> + * struct ieee802154_pan_device - PAN device information
> + * @pan_id: the PAN ID of this device
> + * @mode: the preferred mode to reach the device
> + * @short_addr: the short address of this device
> + * @extended_addr: the extended address of this device
> + * @node: the list node
> + */
> +struct ieee802154_pan_device {
> +	__le16 pan_id;
> +	u8 mode;
> +	__le16 short_addr;
> +	__le64 extended_addr;
> +	struct list_head node;
> +};
> +
>   /**
>    * struct cfg802154_scan_request - Scan request
>    *
> @@ -478,6 +494,11 @@ struct wpan_dev {
>   
>   	/* fallback for acknowledgment bit setting */
>   	bool ackreq;
> +
> +	/* Associations */
> +	struct mutex association_lock;
> +	struct ieee802154_pan_device *parent;
> +	struct list_head children;
>   };
>   
>   #define to_phy(_dev)	container_of(_dev, struct wpan_phy, dev)
> @@ -529,4 +550,29 @@ static inline const char *wpan_phy_name(struct wpan_phy *phy)
>   void ieee802154_configure_durations(struct wpan_phy *phy,
>   				    unsigned int page, unsigned int channel);
>   
> +/**
> + * cfg802154_device_is_associated - Checks whether we are associated to any device
> + * @wpan_dev: the wpan device
> + */
> +bool cfg802154_device_is_associated(struct wpan_dev *wpan_dev);

The return value still missing in kdoc. Seems you missed this from my 
last review. :-)

> +/**
> + * cfg802154_device_is_parent - Checks if a device is our coordinator
> + * @wpan_dev: the wpan device
> + * @target: the expected parent
> + * @return: true if @target is our coordinator
> + */
> +bool cfg802154_device_is_parent(struct wpan_dev *wpan_dev,
> +				struct ieee802154_addr *target);
> +
> +/**
> + * cfg802154_device_is_child - Checks whether a device is associated to us
> + * @wpan_dev: the wpan device
> + * @target: the expected child
> + * @return: the PAN device
> + */
> +struct ieee802154_pan_device *
> +cfg802154_device_is_child(struct wpan_dev *wpan_dev,
> +			  struct ieee802154_addr *target);
> +
>   #endif /* __NET_CFG802154_H */
> diff --git a/net/ieee802154/Makefile b/net/ieee802154/Makefile
> index f05b7bdae2aa..7bce67673e83 100644
> --- a/net/ieee802154/Makefile
> +++ b/net/ieee802154/Makefile
> @@ -4,7 +4,7 @@ obj-$(CONFIG_IEEE802154_SOCKET) += ieee802154_socket.o
>   obj-y += 6lowpan/
>   
>   ieee802154-y := netlink.o nl-mac.o nl-phy.o nl_policy.o core.o \
> -                header_ops.o sysfs.o nl802154.o trace.o
> +                header_ops.o sysfs.o nl802154.o trace.o pan.o
>   ieee802154_socket-y := socket.o
>   
>   CFLAGS_trace.o := -I$(src)
> diff --git a/net/ieee802154/core.c b/net/ieee802154/core.c
> index 57546e07e06a..cd69bdbfd59f 100644
> --- a/net/ieee802154/core.c
> +++ b/net/ieee802154/core.c
> @@ -276,6 +276,8 @@ static int cfg802154_netdev_notifier_call(struct notifier_block *nb,
>   		wpan_dev->identifier = ++rdev->wpan_dev_id;
>   		list_add_rcu(&wpan_dev->list, &rdev->wpan_dev_list);
>   		rdev->devlist_generation++;
> +		mutex_init(&wpan_dev->association_lock);
> +		INIT_LIST_HEAD(&wpan_dev->children);
>   
>   		wpan_dev->netdev = dev;
>   		break;
> diff --git a/net/ieee802154/pan.c b/net/ieee802154/pan.c
> new file mode 100644
> index 000000000000..012b5e821d54
> --- /dev/null
> +++ b/net/ieee802154/pan.c
> @@ -0,0 +1,75 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * IEEE 802.15.4 PAN management
> + *
> + * Copyright (C) 2021 Qorvo US, Inc

Feel free to extend the copyright years to 2023 as well.

regards
Stefan Schmidt
Miquel Raynal Sept. 22, 2023, 3:18 p.m. UTC | #2
Hi Stefan,

stefan@datenfreihafen.org wrote on Wed, 20 Sep 2023 20:08:56 +0200:

> Hello.
> 
> On 18.09.23 17:08, Miquel Raynal wrote:
> > Introduce structures to describe peer devices in a PAN as well as a few
> > related helpers. We basically care about:
> > - Our unique parent after associating with a coordinator.
> > - Peer devices, children, which successfully associated with us.
> > 
> > Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
> > ---
> >   include/net/cfg802154.h | 46 +++++++++++++++++++++++++
> >   net/ieee802154/Makefile |  2 +-
> >   net/ieee802154/core.c   |  2 ++
> >   net/ieee802154/pan.c    | 75 +++++++++++++++++++++++++++++++++++++++++
> >   4 files changed, 124 insertions(+), 1 deletion(-)
> >   create mode 100644 net/ieee802154/pan.c
> > 
> > diff --git a/include/net/cfg802154.h b/include/net/cfg802154.h
> > index f79ce133e51a..6c7193b4873c 100644
> > --- a/include/net/cfg802154.h
> > +++ b/include/net/cfg802154.h

[...]

> > @@ -478,6 +494,11 @@ struct wpan_dev {  
> >   >   	/* fallback for acknowledgment bit setting */  
> >   	bool ackreq;
> > +
> > +	/* Associations */
> > +	struct mutex association_lock;
> > +	struct ieee802154_pan_device *parent;
> > +	struct list_head children;
> >   };  
> >   >   #define to_phy(_dev)	container_of(_dev, struct wpan_phy, dev)  
> > @@ -529,4 +550,29 @@ static inline const char *wpan_phy_name(struct wpan_phy *phy)
> >   void ieee802154_configure_durations(struct wpan_phy *phy,
> >   				    unsigned int page, unsigned int channel);  
> >   > +/**  
> > + * cfg802154_device_is_associated - Checks whether we are associated to any device
> > + * @wpan_dev: the wpan device
> > + */
> > +bool cfg802154_device_is_associated(struct wpan_dev *wpan_dev);  
> 
> The return value still missing in kdoc. Seems you missed this from my last review. :-)

Oops, I marked it done on my side by mistake. Sorry for that mistake.

[...]

> > diff --git a/net/ieee802154/pan.c b/net/ieee802154/pan.c
> > new file mode 100644
> > index 000000000000..012b5e821d54
> > --- /dev/null
> > +++ b/net/ieee802154/pan.c
> > @@ -0,0 +1,75 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +/*
> > + * IEEE 802.15.4 PAN management
> > + *
> > + * Copyright (C) 2021 Qorvo US, Inc  
> 
> Feel free to extend the copyright years to 2023 as well.

Right, set to 2023.

Thanks,
Miquèl
diff mbox series

Patch

diff --git a/include/net/cfg802154.h b/include/net/cfg802154.h
index f79ce133e51a..6c7193b4873c 100644
--- a/include/net/cfg802154.h
+++ b/include/net/cfg802154.h
@@ -303,6 +303,22 @@  struct ieee802154_coord_desc {
 	bool gts_permit;
 };
 
+/**
+ * struct ieee802154_pan_device - PAN device information
+ * @pan_id: the PAN ID of this device
+ * @mode: the preferred mode to reach the device
+ * @short_addr: the short address of this device
+ * @extended_addr: the extended address of this device
+ * @node: the list node
+ */
+struct ieee802154_pan_device {
+	__le16 pan_id;
+	u8 mode;
+	__le16 short_addr;
+	__le64 extended_addr;
+	struct list_head node;
+};
+
 /**
  * struct cfg802154_scan_request - Scan request
  *
@@ -478,6 +494,11 @@  struct wpan_dev {
 
 	/* fallback for acknowledgment bit setting */
 	bool ackreq;
+
+	/* Associations */
+	struct mutex association_lock;
+	struct ieee802154_pan_device *parent;
+	struct list_head children;
 };
 
 #define to_phy(_dev)	container_of(_dev, struct wpan_phy, dev)
@@ -529,4 +550,29 @@  static inline const char *wpan_phy_name(struct wpan_phy *phy)
 void ieee802154_configure_durations(struct wpan_phy *phy,
 				    unsigned int page, unsigned int channel);
 
+/**
+ * cfg802154_device_is_associated - Checks whether we are associated to any device
+ * @wpan_dev: the wpan device
+ */
+bool cfg802154_device_is_associated(struct wpan_dev *wpan_dev);
+
+/**
+ * cfg802154_device_is_parent - Checks if a device is our coordinator
+ * @wpan_dev: the wpan device
+ * @target: the expected parent
+ * @return: true if @target is our coordinator
+ */
+bool cfg802154_device_is_parent(struct wpan_dev *wpan_dev,
+				struct ieee802154_addr *target);
+
+/**
+ * cfg802154_device_is_child - Checks whether a device is associated to us
+ * @wpan_dev: the wpan device
+ * @target: the expected child
+ * @return: the PAN device
+ */
+struct ieee802154_pan_device *
+cfg802154_device_is_child(struct wpan_dev *wpan_dev,
+			  struct ieee802154_addr *target);
+
 #endif /* __NET_CFG802154_H */
diff --git a/net/ieee802154/Makefile b/net/ieee802154/Makefile
index f05b7bdae2aa..7bce67673e83 100644
--- a/net/ieee802154/Makefile
+++ b/net/ieee802154/Makefile
@@ -4,7 +4,7 @@  obj-$(CONFIG_IEEE802154_SOCKET) += ieee802154_socket.o
 obj-y += 6lowpan/
 
 ieee802154-y := netlink.o nl-mac.o nl-phy.o nl_policy.o core.o \
-                header_ops.o sysfs.o nl802154.o trace.o
+                header_ops.o sysfs.o nl802154.o trace.o pan.o
 ieee802154_socket-y := socket.o
 
 CFLAGS_trace.o := -I$(src)
diff --git a/net/ieee802154/core.c b/net/ieee802154/core.c
index 57546e07e06a..cd69bdbfd59f 100644
--- a/net/ieee802154/core.c
+++ b/net/ieee802154/core.c
@@ -276,6 +276,8 @@  static int cfg802154_netdev_notifier_call(struct notifier_block *nb,
 		wpan_dev->identifier = ++rdev->wpan_dev_id;
 		list_add_rcu(&wpan_dev->list, &rdev->wpan_dev_list);
 		rdev->devlist_generation++;
+		mutex_init(&wpan_dev->association_lock);
+		INIT_LIST_HEAD(&wpan_dev->children);
 
 		wpan_dev->netdev = dev;
 		break;
diff --git a/net/ieee802154/pan.c b/net/ieee802154/pan.c
new file mode 100644
index 000000000000..012b5e821d54
--- /dev/null
+++ b/net/ieee802154/pan.c
@@ -0,0 +1,75 @@ 
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * IEEE 802.15.4 PAN management
+ *
+ * Copyright (C) 2021 Qorvo US, Inc
+ * Authors:
+ *   - David Girault <david.girault@qorvo.com>
+ *   - Miquel Raynal <miquel.raynal@bootlin.com>
+ */
+
+#include <linux/kernel.h>
+#include <net/cfg802154.h>
+#include <net/af_ieee802154.h>
+
+/* Checks whether a device address matches one from the PAN list */
+static bool cfg802154_device_in_pan(struct ieee802154_pan_device *pan_dev,
+				    struct ieee802154_addr *ext_dev)
+{
+	if (!pan_dev || !ext_dev)
+		return false;
+
+	/* We know the PAN device extended address, and if it is a child which
+	 * required a short address, then we also know its short address and the
+	 * (preferred) mode is set to IEEE802154_ADDR_SHORT.
+	 */
+	if (ext_dev->mode == IEEE802154_ADDR_SHORT &&
+	    pan_dev->mode != IEEE802154_ADDR_SHORT)
+		return false;
+
+	switch (ext_dev->mode) {
+	case IEEE802154_ADDR_SHORT:
+		return pan_dev->short_addr == ext_dev->short_addr;
+	case IEEE802154_ADDR_LONG:
+		return pan_dev->extended_addr == ext_dev->extended_addr;
+	default:
+		return false;
+	}
+}
+
+bool cfg802154_device_is_associated(struct wpan_dev *wpan_dev)
+{
+	bool is_assoc;
+
+	mutex_lock(&wpan_dev->association_lock);
+	is_assoc = !list_empty(&wpan_dev->children) || wpan_dev->parent;
+	mutex_unlock(&wpan_dev->association_lock);
+
+	return is_assoc;
+}
+
+bool cfg802154_device_is_parent(struct wpan_dev *wpan_dev,
+				struct ieee802154_addr *target)
+{
+	lockdep_assert_held(&wpan_dev->association_lock);
+
+	if (cfg802154_device_in_pan(wpan_dev->parent, target))
+		return true;
+
+	return false;
+}
+
+struct ieee802154_pan_device *
+cfg802154_device_is_child(struct wpan_dev *wpan_dev,
+			  struct ieee802154_addr *target)
+{
+	struct ieee802154_pan_device *child;
+
+	lockdep_assert_held(&wpan_dev->association_lock);
+
+	list_for_each_entry(child, &wpan_dev->children, node)
+		if (cfg802154_device_in_pan(child, target))
+			return child;
+
+	return NULL;
+}