Skip to contentSkip to conversation

net: ethernet: use ip_hdrlen() instead of bit shift

PATCH Superseded Moon Yeounsu 4 participants 10 messages Aug 2, 2024 – Aug 5, 2024

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: ethernet: use ip_hdrlen() instead of bit shift

ip_hdr(skb)->ihl << 2 are the same as ip_hdrlen(skb) Therefore, we should use a well-defined function not a bit shift to find the header length.

It also compress two lines at a single line.

---
 drivers/net/ethernet/jme.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/net/ethernet/jme.c +3 -5
index b06e24562973..83b185c995df 100644
@@ -946,15 +946,13 @@ jme_udpsum(struct sk_buff *skb)
 	if (skb->protocol != htons(ETH_P_IP))
 		return csum;
 	skb_set_network_header(skb, ETH_HLEN);
+
 	if ((ip_hdr(skb)->protocol != IPPROTO_UDP) ||
-	    (skb->len < (ETH_HLEN +
-			(ip_hdr(skb)->ihl << 2) +
-			sizeof(struct udphdr)))) {
+	    (skb->len < (ETH_HLEN + (ip_hdrlen(skb)) + sizeof(struct udphdr)))) {
 		skb_reset_network_header(skb);
 		return csum;
 	}
-	skb_set_transport_header(skb,
-			ETH_HLEN + (ip_hdr(skb)->ihl << 2));
+	skb_set_transport_header(skb, ETH_HLEN + (ip_hdrlen(skb)));
 	csum = udp_hdr(skb)->check;
 	skb_reset_transport_header(skb);
 	skb_reset_network_header(skb);
Signature · 1 line
2.45.2
#1 lore raw

Le 02/08/2024 à 07:44, Moon Yeounsu a écrit :

Quoted 11 lines

ip_hdr(skb)->ihl << 2 are the same as ip_hdrlen(skb) Therefore, we should use a well-defined function not a bit shift to find the header length.

It also compress two lines at a single line.

---
  drivers/net/ethernet/jme.c | 8 +++-----
  1 file changed, 3 insertions(+), 5 deletions(-)

Hi,

Quoted 5 lines
drivers/net/ethernet/jme.c +0 -0
index b06e24562973..83b185c995df 100644

drivers/net/ethernet/jme.c jme_udpsum(struct sk_buff *skb) · line 951

Quoted 9 lines
drivers/net/ethernet/jme.c +2 -3
@@ -946,15 +946,13 @@ jme_udpsum(struct sk_buff *skb)
  	if (skb->protocol != htons(ETH_P_IP))
  		return csum;
  	skb_set_network_header(skb, ETH_HLEN);
+
  	if ((ip_hdr(skb)->protocol != IPPROTO_UDP) ||
-	    (skb->len < (ETH_HLEN +
-			(ip_hdr(skb)->ihl << 2) +
-			sizeof(struct udphdr)))) {
+	    (skb->len < (ETH_HLEN + (ip_hdrlen(skb)) + sizeof(struct udphdr)))) {

The extra () around "ip_hdrlen(skb)" can be remove.
Also maybe the ones around "ETH_HLEN + ip_hdrlen(skb)" could also be removed.

drivers/net/ethernet/jme.c jme_udpsum(struct sk_buff *skb) · line 955

Quoted 6 lines
drivers/net/ethernet/jme.c +1 -2
  		skb_reset_network_header(skb);
  		return csum;
  	}
-	skb_set_transport_header(skb,
-			ETH_HLEN + (ip_hdr(skb)->ihl << 2));
+	skb_set_transport_header(skb, ETH_HLEN + (ip_hdrlen(skb)));

Same here, the extra () around "ip_hdrlen(skb)" can be remove.

CJ

Quoted 3 lines
drivers/net/ethernet/jme.c +0 -0
  	csum = udp_hdr(skb)->check;
  	skb_reset_transport_header(skb);
  	skb_reset_network_header(skb);
#2 lore raw
Quoted 3 lines · Christophe JAILLET

The extra () around "ip_hdrlen(skb)" can be remove.
Also maybe the ones around "ETH_HLEN + ip_hdrlen(skb)" could also be removed.

Okay, I'll send the next patch which the parenthesis are removed! But... The parenthesis around ETH_HLEN + ip_hdrlen(skb) + sizeof(struct udphdr)
should be retained, because it makes a clear boundary.

Quoted 9 lines

drivers/net/ethernet/jme.c jme_udpsum(struct sk_buff *skb) · line 955

Quoted 7 lines
              skb_reset_network_header(skb);
              return csum;
      }
drivers/net/ethernet/jme.c +1 -2
-     skb_set_transport_header(skb,
-                     ETH_HLEN + (ip_hdr(skb)->ihl << 2));
+     skb_set_transport_header(skb, ETH_HLEN + (ip_hdrlen(skb)));

Same here, the extra () around "ip_hdrlen(skb)" can be remove.

I'll remove it also.

Quoted 7 lines

CJ

Quoted 3 lines
      csum = udp_hdr(skb)->check;
      skb_reset_transport_header(skb);
      skb_reset_network_header(skb);

Thank you for reviewing ^오^

reviewer
#3 lore raw
In reply to Moon Yeounsu
Quoted 7 lines · Moon Yeounsu

ip_hdr(skb)->ihl << 2 are the same as ip_hdrlen(skb) Therefore, we should use a well-defined function not a bit shift to find the header length.

It also compress two lines at a single line.

Firstly, I think this clean-up is both correct and safe. Safe because ip_hdrlen() only relies on ip_hdr(), which is already used in the same code path. And correct because ip_hdrlen multiplies ihl by 4, which is clearly equivalent to a left shift of 2 bits.

However, I do wonder about the value of clean-ups for what appears to be a very old driver, which hasn't received a new feature for quite sometime

And further, I wonder if we should update this driver from "Maintained" to "Odd Fixes" as the maintainer, "Guo-Fu Tseng" <[email protected]>, doesn't seem to have been seen by lore since early 2020.

https://lore.kernel.org/netdev/[email protected]/

Quoted 9 lines
---
 drivers/net/ethernet/jme.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/net/ethernet/jme.c +0 -0
index b06e24562973..83b185c995df 100644

drivers/net/ethernet/jme.c jme_udpsum(struct sk_buff *skb) · line 951

Quoted 9 lines
drivers/net/ethernet/jme.c +2 -3
@@ -946,15 +946,13 @@ jme_udpsum(struct sk_buff *skb)
 	if (skb->protocol != htons(ETH_P_IP))
 		return csum;
 	skb_set_network_header(skb, ETH_HLEN);
+
 	if ((ip_hdr(skb)->protocol != IPPROTO_UDP) ||
-	    (skb->len < (ETH_HLEN +
-			(ip_hdr(skb)->ihl << 2) +
-			sizeof(struct udphdr)))) {
+	    (skb->len < (ETH_HLEN + (ip_hdrlen(skb)) + sizeof(struct udphdr)))) {

The parentheses around the call to ip_hdrlen are unnecessary. And this line is now too long: networking codes till prefers code to be 80 columns wide or less.

drivers/net/ethernet/jme.c jme_udpsum(struct sk_buff *skb) · line 955

Quoted 6 lines
drivers/net/ethernet/jme.c +1 -2
 		skb_reset_network_header(skb);
 		return csum;
 	}
-	skb_set_transport_header(skb,
-			ETH_HLEN + (ip_hdr(skb)->ihl << 2));
+	skb_set_transport_header(skb, ETH_HLEN + (ip_hdrlen(skb)));

Unnecessary parentheses here too.

Quoted 3 lines
drivers/net/ethernet/jme.c +0 -0
 	csum = udp_hdr(skb)->check;
 	skb_reset_transport_header(skb);
 	skb_reset_network_header(skb);
Signature · 1 line
pw-bot: cr
reviewer
#4 lore raw
Quoted 14 lines · Simon Horman
Quoted 7 lines · Moon Yeounsu

ip_hdr(skb)->ihl << 2 are the same as ip_hdrlen(skb) Therefore, we should use a well-defined function not a bit shift to find the header length.

It also compress two lines at a single line.

Firstly, I think this clean-up is both correct and safe. Safe because ip_hdrlen() only relies on ip_hdr(), which is already used in the same code path. And correct because ip_hdrlen multiplies ihl by 4, which is clearly equivalent to a left shift of 2 bits.

Quoted 8 lines

However, I do wonder about the value of clean-ups for what appears to be a very old driver, which hasn't received a new feature for quite sometime

And further, I wonder if we should update this driver from "Maintained" to "Odd Fixes" as the maintainer, "Guo-Fu Tseng" <[email protected]>, doesn't seem to have been seen by lore since early 2020.

https://lore.kernel.org/netdev/[email protected]/

By "Odd Fixes" I meant "Orphan"

...

#5 lore raw
In reply to Simon Horman
Quoted 6 lines · Simon Horman
Quoted 4 lines · Moon Yeounsu

ip_hdr(skb)->ihl << 2 are the same as ip_hdrlen(skb) Therefore, we should use a well-defined function not a bit shift to find the header length.

Quoted 8 lines
Quoted 3 lines

It also compress two lines at a single line.

Firstly, I think this clean-up is both correct and safe. Safe because ip_hdrlen() only relies on ip_hdr(), which is already used in the same code path. And correct because ip_hdrlen multiplies ihl by 4, which is clearly equivalent to a left shift of 2 bits.

Firstly, Thank you for reviewing my patch!

Quoted 3 lines

However, I do wonder about the value of clean-ups for what appears to be a very old driver, which hasn't received a new feature for quite sometime

Oh, I don't know that...

Quoted 6 lines

And further, I wonder if we should update this driver from "Maintained" to "Odd Fixes" as the maintainer, "Guo-Fu Tseng" <[email protected]>, doesn't seem to have been seen by lore since early 2020.

https://lore.kernel.org/netdev/[email protected]/

Then, how about deleting the file from the kernel if the driver isn't maintained?
Many people think like that (At least I think so)
There are files, and if there are issues, then have to fix them. Who can think unmanaged files remain in the kernel?

Quoted 14 lines
Quoted 14 lines
---
 drivers/net/ethernet/jme.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/net/ethernet/jme.c +1 -0
index b06e24562973..83b185c995df 100644
@@ -946,15 +946,13 @@ jme_udpsum(struct sk_buff *skb)
      if (skb->protocol != htons(ETH_P_IP))
              return csum;
      skb_set_network_header(skb, ETH_HLEN);
+
      if ((ip_hdr(skb)->protocol != IPPROTO_UDP) ||
Quoted 8 lines

drivers/net/ethernet/jme.c jme_udpsum(struct sk_buff *skb) · line 951

Quoted 4 lines
drivers/net/ethernet/jme.c +1 -3
-         (skb->len < (ETH_HLEN +
-                     (ip_hdr(skb)->ihl << 2) +
-                     sizeof(struct udphdr)))) {
+         (skb->len < (ETH_HLEN + (ip_hdrlen(skb)) + sizeof(struct udphdr)))) {

The parentheses around the call to ip_hdrlen are unnecessary. And this line is now too long: networking codes till prefers code to be 80 columns wide or less.

Okay, I'll keep the kernel coding style too!

Quoted 9 lines

drivers/net/ethernet/jme.c jme_udpsum(struct sk_buff *skb) · line 955

Quoted 7 lines
drivers/net/ethernet/jme.c +1 -2
              skb_reset_network_header(skb);
              return csum;
      }
-     skb_set_transport_header(skb,
-                     ETH_HLEN + (ip_hdr(skb)->ihl << 2));
+     skb_set_transport_header(skb, ETH_HLEN + (ip_hdrlen(skb)));

Unnecessary parentheses here too.

Also fix it :)

Quoted 7 lines

drivers/net/ethernet/jme.c jme_udpsum(struct sk_buff *skb) · line 958

Quoted 4 lines
drivers/net/ethernet/jme.c +0 -0
      csum = udp_hdr(skb)->check;
      skb_reset_transport_header(skb);
      skb_reset_network_header(skb);

--
pw-bot: cr

Thank you for paying attention to my patch! I'm a beginner who just came to the kernel.
So... Sorry if I sounded presumptuous and didn't know much about kernels. But I don't understand why we have to pay attention to unmanaged kernel files. And why do we have to check whether the file is managed or not?

Thank you for reading my email ^오^

reviewer
#6 lore raw
Quoted 21 lines · Moon Yeounsu
Quoted 6 lines · Simon Horman
Quoted 4 lines · Moon Yeounsu

ip_hdr(skb)->ihl << 2 are the same as ip_hdrlen(skb) Therefore, we should use a well-defined function not a bit shift to find the header length.

Quoted 8 lines
Quoted 3 lines

It also compress two lines at a single line.

Firstly, I think this clean-up is both correct and safe. Safe because ip_hdrlen() only relies on ip_hdr(), which is already used in the same code path. And correct because ip_hdrlen multiplies ihl by 4, which is clearly equivalent to a left shift of 2 bits.

Firstly, Thank you for reviewing my patch!

Quoted 3 lines

However, I do wonder about the value of clean-ups for what appears to be a very old driver, which hasn't received a new feature for quite sometime

Oh, I don't know that...

Quoted 1 line
Quoted 7 lines
Quoted 5 lines

And further, I wonder if we should update this driver from "Maintained" to "Odd Fixes" as the maintainer, "Guo-Fu Tseng" <[email protected]>, doesn't seem to have been seen by lore since early 2020.

https://lore.kernel.org/netdev/[email protected]/

Then, how about deleting the file from the kernel if the driver isn't maintained?

That is a bit more severe than marking it as being unmaintained in MAINTAINERS. But I do agree that it should be considered.

Quoted 3 lines

Many people think like that (At least I think so) There are files, and if there are issues, then have to fix them. Who can think unmanaged files remain in the kernel?

And yet, they do exist. ☯

maintainer
#7 lore raw

On Sun, 4 Aug 2024 11:18:58 +0100, Simon Horman wrote

Quoted 18 lines · Moon Yeounsu
Quoted 17 lines · Simon Horman
Quoted 6 lines · Moon Yeounsu
Quoted 4 lines

ip_hdr(skb)->ihl << 2 are the same as ip_hdrlen(skb) Therefore, we should use a well-defined function not a bit shift to find the header length.

Quoted 8 lines
Quoted 3 lines

It also compress two lines at a single line.

Firstly, I think this clean-up is both correct and safe. Safe because ip_hdrlen() only relies on ip_hdr(), which is already used in the same code path. And correct because ip_hdrlen multiplies ihl by 4, which is clearly equivalent to a left shift of 2 bits.

Firstly, Thank you for reviewing my patch!

Quoted 1 line
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

However, I do wonder about the value of clean-ups for what appears to be a very old driver, which hasn't received a new feature for quite sometime

Oh, I don't know that...

Quoted 4 lines

And further, I wonder if we should update this driver from "Maintained" to "Odd Fixes" as the maintainer, "Guo-Fu Tseng" <[email protected]>, doesn't seem to have been seen by lore since early 2020.

The device has been EOLed for a lone time.
Since there is no new related chip and no new updates from the chip maker, I do agreed to make the status as "Odd Fixes".

Quoted 5 lines
Quoted 4 lines
Quoted 2 lines

https://lore.kernel.org/netdev/[email protected]/

Then, how about deleting the file from the kernel if the driver isn't maintained?

Quoted 8 lines

That is a bit more severe than marking it as being unmaintained in MAINTAINERS. But I do agree that it should be considered.

Quoted 3 lines

Many people think like that (At least I think so) There are files, and if there are issues, then have to fix them. Who can think unmanaged files remain in the kernel?

And yet, they do exist. ☯

Guo-Fu Tseng

#8 lore raw

PATCH v2 1/1 net: ethernet: remove unnecessary parentheses

In reply to Moon Yeounsu

remove unnecessary parentheses surrounding ip_hdrlen(), And keep it under 80 columns long to follow the kernel coding style.

---
 drivers/net/ethernet/jme.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/net/ethernet/jme.c +3 -3
index 83b185c995df..d8be0e4dcb07 100644
@@ -947,12 +947,12 @@ jme_udpsum(struct sk_buff *skb)
 		return csum;
 	skb_set_network_header(skb, ETH_HLEN);
 
-	if ((ip_hdr(skb)->protocol != IPPROTO_UDP) ||
-	    (skb->len < (ETH_HLEN + (ip_hdrlen(skb)) + sizeof(struct udphdr)))) {
+	if (ip_hdr(skb)->protocol != IPPROTO_UDP ||
+	    skb->len < (ETH_HLEN + ip_hdrlen(skb) + sizeof(struct udphdr))) {
 		skb_reset_network_header(skb);
 		return csum;
 	}
-	skb_set_transport_header(skb, ETH_HLEN + (ip_hdrlen(skb)));
+	skb_set_transport_header(skb, ETH_HLEN + ip_hdrlen(skb));
 	csum = udp_hdr(skb)->check;
 	skb_reset_transport_header(skb);
 	skb_reset_network_header(skb);
Signature · 1 line
2.45.2
#9 lore raw

Re: [PATCH v2] net: ethernet: remove unnecessary parentheses

Hi,

this is not how things work.
This v2 depends on the previous v1. It shouldn't.

v2 should be a standalone patch which includes v1 and all modifications done afterward.

When you send a new patch you should:
- give an history of changes, below the ---. See [1]
- eventually give links to previous version
- your new version should not be threaded with the previous mails. It
should start a new thread.

Le 03/08/2024 à 04:29, Moon Yeounsu a écrit :

Quoted 5 lines

remove unnecessary parentheses surrounding ip_hdrlen(), And keep it under 80 columns long to follow the kernel coding style.

---

[1]: here.

*As an example*, I would have done as follow, but there is no rule (AFAIK) for this section.

Changes in v2:
- Remove extra () [Christophe Jaillet, Simon Horman]
- Break long lines [Simon Horman]

v1: https://lore.kernel.org/all/[email protected]/

Quoted 23 lines
  drivers/net/ethernet/jme.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
drivers/net/ethernet/jme.c +3 -3
index 83b185c995df..d8be0e4dcb07 100644
@@ -947,12 +947,12 @@ jme_udpsum(struct sk_buff *skb)
  		return csum;
  	skb_set_network_header(skb, ETH_HLEN);
 
-	if ((ip_hdr(skb)->protocol != IPPROTO_UDP) ||
-	    (skb->len < (ETH_HLEN + (ip_hdrlen(skb)) + sizeof(struct udphdr)))) {
+	if (ip_hdr(skb)->protocol != IPPROTO_UDP ||
+	    skb->len < (ETH_HLEN + ip_hdrlen(skb) + sizeof(struct udphdr))) {
  		skb_reset_network_header(skb);
  		return csum;
  	}
-	skb_set_transport_header(skb, ETH_HLEN + (ip_hdrlen(skb)));
+	skb_set_transport_header(skb, ETH_HLEN + ip_hdrlen(skb));
  	csum = udp_hdr(skb)->check;
  	skb_reset_transport_header(skb);
  	skb_reset_network_header(skb);