Skip to contentSkip to conversation

ieee802154: small cleanups

PATCH Superseded Alexander Aring 3 participants 9 messages Feb 10, 2015 – Feb 11, 2015

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Alexander Aring

author maintainer
#0 lore raw

cover ieee802154: small cleanups

Hi,

this patch series contains some small cleanups. THe first patch do a more correct checking on valid psdu len, this is necessary e.g. in at86rf230 promiscous mode frame length checking to avoid an access out of mpdu buffer. The last two patches simple remove some unnecessary temporary variable.

- Alex

Alexander Aring (3):
ieee802154: correct ieee802154_is_valid_psdu_len
ieee802154: cleanup ieee802154_be64_to_le64
ieee802154: cleanup ieee802154_le64_to_be64

 include/linux/ieee802154.h | 12 ++++++++++--
 include/net/mac802154.h    |  8 ++------
 2 files changed, 12 insertions(+), 8 deletions(-)
Signature · 1 line
2.2.2
author maintainer
#1 lore raw

PATCH 1/3 ieee802154: correct ieee802154_is_valid_psdu_len

This patch corrects the ieee802154_is_valid_psdu_len function that this function also checks on reserved values 6-8 for validation the psdu length.

---
 include/linux/ieee802154.h | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
include/linux/ieee802154.h +10 -2
index 6e82d88..40b0ab9 100644
@@ -28,7 +28,8 @@
 #include <asm/byteorder.h>
 
 #define IEEE802154_MTU			127
-#define IEEE802154_MIN_PSDU_LEN		5
+#define IEEE802154_ACK_PSDU_LEN		5
+#define IEEE802154_MIN_PSDU_LEN		9
 
 #define IEEE802154_PAN_ID_BROADCAST	0xffff
 #define IEEE802154_ADDR_SHORT_BROADCAST	0xffff
@@ -204,11 +205,18 @@ enum {
 
 /**
  * ieee802154_is_valid_psdu_len - check if psdu len is valid
+ * available lengths:
+ *	0-4	Reserved
+ *	5	MPDU (Acknowledgment)
+ *	6-8	Reserved
+ *	9-127	MPDU
+ *
  * @len: psdu len with (MHR + payload + MFR)
  */
 static inline bool ieee802154_is_valid_psdu_len(const u8 len)
 {
-	return (len >= IEEE802154_MIN_PSDU_LEN && len <= IEEE802154_MTU);
+	return (len == IEEE802154_ACK_PSDU_LEN ||
+		(len >= IEEE802154_MIN_PSDU_LEN && len <= IEEE802154_MTU));
 }
 
 /**
Signature · 1 line
2.2.2
author maintainer
#2 lore raw

PATCH 2/3 ieee802154: cleanup ieee802154_be64_to_le64

In reply to Alexander Aring

This patch cleanups the ieee802154_be64_to_le64 function. This patch removes an unnecessary temporary variable.

---
 include/net/mac802154.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
include/net/mac802154.h +1 -3
index 8506478..a4dcefe 100644
@@ -233,9 +233,7 @@ struct ieee802154_ops {
  */
 static inline void ieee802154_be64_to_le64(void *le64_dst, const void *be64_src)
 {
-	__le64 tmp = (__force __le64)swab64p(be64_src);
-
-	memcpy(le64_dst, &tmp, IEEE802154_EXTENDED_ADDR_LEN);
+	*((__le64 *)le64_dst) = (__force __le64)swab64p(be64_src);
 }
 
 /**
Signature · 1 line
2.2.2
#3 lore raw

Re: [PATCH bluetooth-next 2/3] ieee802154: cleanup ieee802154_be64_to_le64

Quoted 13 lines · Alexander Aring

This patch cleanups the ieee802154_be64_to_le64 function. This patch removes an unnecessary temporary variable.

---
 include/net/mac802154.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
include/net/mac802154.h +0 -0
index 8506478..a4dcefe 100644

include/net/mac802154.h struct ieee802154_ops { · line 236

Quoted 7 lines
include/net/mac802154.h +1 -3
@@ -233,9 +233,7 @@ struct ieee802154_ops {
  */
 static inline void ieee802154_be64_to_le64(void *le64_dst, const void *be64_src)
 {
-	__le64 tmp = (__force __le64)swab64p(be64_src);
-
-	memcpy(le64_dst, &tmp, IEEE802154_EXTENDED_ADDR_LEN);
+	*((__le64 *)le64_dst) = (__force __le64)swab64p(be64_src);

I assume the compiler optimizes the memcpy, due to the constant length argument. It the dst always 64 bit aligned?

Marc

Signature · 4 lines
Pengutronix e.K.                  | Marc Kleine-Budde           |
Industrial Linux Solutions        | Phone: +49-231-2826-924     |
Vertretung West/Dortmund          | Fax:   +49-5121-206917-5555 |
Amtsgericht Hildesheim, HRA 2686  | http://www.pengutronix.de   |
author maintainer
#4 lore raw

Re: [PATCH bluetooth-next 2/3] ieee802154: cleanup ieee802154_be64_to_le64

Quoted 9 lines · Marc Kleine-Budde
Quoted 8 lines · Alexander Aring

This patch cleanups the ieee802154_be64_to_le64 function. This patch removes an unnecessary temporary variable.

---
 include/net/mac802154.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
Quoted 15 lines

include/net/mac802154.h struct ieee802154_ops { · line 236

Quoted 12 lines
include/net/mac802154.h +1 -3
index 8506478..a4dcefe 100644
@@ -233,9 +233,7 @@ struct ieee802154_ops {
  */
 static inline void ieee802154_be64_to_le64(void *le64_dst, const void *be64_src)
 {
-	__le64 tmp = (__force __le64)swab64p(be64_src);
-
-	memcpy(le64_dst, &tmp, IEEE802154_EXTENDED_ADDR_LEN);
+	*((__le64 *)le64_dst) = (__force __le64)swab64p(be64_src);

I assume the compiler optimizes the memcpy, due to the constant length argument. It the dst always 64 bit aligned?

should be. I can't check this, but dst and src should always 64 bit long. (Otherwise the programmer do something wrong).

It's just byte swapping a little endian 64 bit value to big endian 64 bit value. Because mac header is little and the most netdev core api uses big endian. These function are some helper function to transfer __le64 to __be64. Sometimes the __le64 and __be64 are implementated as arrays so I do this over some void pointers, like [0].

At [0] you see the assigned MAC PIB extended addr (saved as __le64) which is set to the netdevice dev_addr array (assumes big endian).

The swab64p is an architecture optimized byte swapping function.

Do you think that this patch will decrease the perfomance of this function? I know the casts looks a little bit ugly but swab64p isn't made for "endianness swapping" and assumes u64 as parameter (pointer) and result.

- Alex

[0] http://git.kernel.org/cgit/linux/kernel/git/bluetooth/bluetooth-next.git/tree/net/mac802154/iface.c#n558

#5 lore raw

Re: [PATCH bluetooth-next 2/3] ieee802154: cleanup ieee802154_be64_to_le64

Quoted 27 lines · On Wed, 11 Feb 2015 13:36:03 +0100 Alexander Aring <[email protected]> wrote:
Quoted 9 lines · Marc Kleine-Budde
Quoted 8 lines · Alexander Aring

This patch cleanups the ieee802154_be64_to_le64 function. This patch removes an unnecessary temporary variable.

---
 include/net/mac802154.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
Quoted 16 lines

include/net/mac802154.h struct ieee802154_ops { · line 237

Quoted 13 lines
include/net/mac802154.h +1 -3
index 8506478..a4dcefe 100644
@@ -233,9 +233,7 @@ struct ieee802154_ops {
  */
 static inline void ieee802154_be64_to_le64(void *le64_dst, const
 void *be64_src) {
-	__le64 tmp = (__force __le64)swab64p(be64_src);
-
-	memcpy(le64_dst, &tmp, IEEE802154_EXTENDED_ADDR_LEN);
+	*((__le64 *)le64_dst) = (__force
 __le64)swab64p(be64_src);

I assume the compiler optimizes the memcpy, due to the constant length argument. It the dst always 64 bit aligned?

Quoted 8 lines

should be. I can't check this, but dst and src should always 64 bit long. (Otherwise the programmer do something wrong).

It's just byte swapping a little endian 64 bit value to big endian 64 bit value. Because mac header is little and the most netdev core api uses big endian. These function are some helper function to transfer __le64 to __be64. Sometimes the __le64 and __be64 are implementated as arrays so I do this over some void pointers, like [0].

If you convert into an array (which happens sometimes, as you point out), you cannot assume alignment stronger than single bytes. If you do remove the memcpy, please adjust the dst argument pointer to be __le64* to catch these cases of possible misalignment.

Quoted 17 lines

At [0] you see the assigned MAC PIB extended addr (saved as __le64) which is set to the netdevice dev_addr array (assumes big endian).

The swab64p is an architecture optimized byte swapping function.

Do you think that this patch will decrease the perfomance of this function? I know the casts looks a little bit ugly but swab64p isn't made for "endianness swapping" and assumes u64 as parameter (pointer) and result.

- Alex

[0]
http://git.kernel.org/cgit/linux/kernel/git/bluetooth/bluetooth-next.git/tree/net/mac802154/iface.c#n558
-- To unsubscribe from this list: send the line "unsubscribe linux-wpan" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

author maintainer
#6 lore raw

Re: [PATCH bluetooth-next 2/3] ieee802154: cleanup ieee802154_be64_to_le64

Quoted 39 lines · Phoebe Buckheister
Quoted 27 lines · On Wed, 11 Feb 2015 13:36:03 +0100 Alexander Aring <[email protected]> wrote:
Quoted 9 lines · Marc Kleine-Budde
Quoted 8 lines · Alexander Aring

This patch cleanups the ieee802154_be64_to_le64 function. This patch removes an unnecessary temporary variable.

---
 include/net/mac802154.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
Quoted 16 lines

include/net/mac802154.h struct ieee802154_ops { · line 237

Quoted 13 lines
include/net/mac802154.h +1 -3
index 8506478..a4dcefe 100644
@@ -233,9 +233,7 @@ struct ieee802154_ops {
  */
 static inline void ieee802154_be64_to_le64(void *le64_dst, const
 void *be64_src) {
-	__le64 tmp = (__force __le64)swab64p(be64_src);
-
-	memcpy(le64_dst, &tmp, IEEE802154_EXTENDED_ADDR_LEN);
+	*((__le64 *)le64_dst) = (__force
 __le64)swab64p(be64_src);

I assume the compiler optimizes the memcpy, due to the constant length argument. It the dst always 64 bit aligned?

Quoted 8 lines

should be. I can't check this, but dst and src should always 64 bit long. (Otherwise the programmer do something wrong).

It's just byte swapping a little endian 64 bit value to big endian 64 bit value. Because mac header is little and the most netdev core api uses big endian. These function are some helper function to transfer __le64 to __be64. Sometimes the __le64 and __be64 are implementated as arrays so I do this over some void pointers, like [0].

Quoted 5 lines

If you convert into an array (which happens sometimes, as you point out), you cannot assume alignment stronger than single bytes. If you do remove the memcpy, please adjust the dst argument pointer to be __le64* to catch these cases of possible misalignment.

ah, I see (maybe).

Then I should use more "put_unaligned_le64" for ieee802154_be64_to_le64 and "put_unaligned_be64" for ieee802154_le64_to_be64.

- Alex

author maintainer
#7 lore raw

Re: [PATCH bluetooth-next 2/3] ieee802154: cleanup ieee802154_be64_to_le64

Quoted 46 lines · Alexander Aring
Quoted 39 lines · Phoebe Buckheister
Quoted 27 lines · On Wed, 11 Feb 2015 13:36:03 +0100 Alexander Aring <[email protected]> wrote:
Quoted 9 lines · Marc Kleine-Budde
Quoted 8 lines · Alexander Aring

This patch cleanups the ieee802154_be64_to_le64 function. This patch removes an unnecessary temporary variable.

---
 include/net/mac802154.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
Quoted 16 lines

include/net/mac802154.h struct ieee802154_ops { · line 237

Quoted 13 lines
include/net/mac802154.h +1 -3
index 8506478..a4dcefe 100644
@@ -233,9 +233,7 @@ struct ieee802154_ops {
  */
 static inline void ieee802154_be64_to_le64(void *le64_dst, const
 void *be64_src) {
-	__le64 tmp = (__force __le64)swab64p(be64_src);
-
-	memcpy(le64_dst, &tmp, IEEE802154_EXTENDED_ADDR_LEN);
+	*((__le64 *)le64_dst) = (__force
 __le64)swab64p(be64_src);

I assume the compiler optimizes the memcpy, due to the constant length argument. It the dst always 64 bit aligned?

Quoted 8 lines

should be. I can't check this, but dst and src should always 64 bit long. (Otherwise the programmer do something wrong).

It's just byte swapping a little endian 64 bit value to big endian 64 bit value. Because mac header is little and the most netdev core api uses big endian. These function are some helper function to transfer __le64 to __be64. Sometimes the __le64 and __be64 are implementated as arrays so I do this over some void pointers, like [0].

Quoted 5 lines

If you convert into an array (which happens sometimes, as you point out), you cannot assume alignment stronger than single bytes. If you do remove the memcpy, please adjust the dst argument pointer to be __le64* to catch these cases of possible misalignment.

Quoted 5 lines

ah, I see (maybe).

Then I should use more "put_unaligned_le64" for ieee802154_be64_to_le64 and "put_unaligned_be64" for ieee802154_le64_to_be64.

ehm, I mean just do put_unaligned64 in both functions. I need to run swab64p in every endianess case. Doesn't depend if I am on a big or little endian machine.

- Alex

author maintainer
#8 lore raw

PATCH 3/3 ieee802154: cleanup ieee802154_le64_to_be64

In reply to Alexander Aring

This patch cleanups the ieee802154_le64_to_be64 function. This patch removes an unnecessary temporary variable.

---
 include/net/mac802154.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
include/net/mac802154.h +1 -3
index a4dcefe..1362eb5 100644
@@ -243,9 +243,7 @@ static inline void ieee802154_be64_to_le64(void *le64_dst, const void *be64_src)
  */
 static inline void ieee802154_le64_to_be64(void *be64_dst, const void *le64_src)
 {
-	__be64 tmp = (__force __be64)swab64p(le64_src);
-
-	memcpy(be64_dst, &tmp, IEEE802154_EXTENDED_ADDR_LEN);
+	*((__be64 *)be64_dst) = (__force __be64)swab64p(le64_src);
 }
 
 /* Basic interface to register ieee802154 hwice */
Signature · 1 line
2.2.2