Skip to contentSkip to conversation

app/testpmd: fix VLAN header parsing

PATCH Superseded Raslan Darawsheh 4 participants 17 messages Mar 23, 2025 – Mar 24, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 app/testpmd: fix VLAN header parsing

Updated the get_ethertype_by_ptype function to correctly parse VLAN headers.

Fixes 76730c7b9b5a ("app/testpmd: use packet type parsing API")
---
 app/test-pmd/csumonly.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
app/test-pmd/csumonly.c +5 -3
index 5b906eaa53..302cc4cc66 100644
@@ -468,6 +468,7 @@ get_ethertype_by_ptype(struct rte_ether_hdr *eth_hdr, uint32_t ptype)
 {
 	struct rte_vlan_hdr *vlan_hdr;
 	uint16_t ethertype;
+	uint32_t i = 0;
 
 	switch (ptype) {
 	case RTE_PTYPE_L3_IPV4:
@@ -486,10 +487,11 @@ get_ethertype_by_ptype(struct rte_ether_hdr *eth_hdr, uint32_t ptype)
 		return _htons(RTE_ETHER_TYPE_IPV6);
 	default:
 		ethertype = eth_hdr->ether_type;
-		while (eth_hdr->ether_type == _htons(RTE_ETHER_TYPE_VLAN) ||
-			eth_hdr->ether_type == _htons(RTE_ETHER_TYPE_QINQ)) {
+		while (ethertype == _htons(RTE_ETHER_TYPE_VLAN) ||
+			ethertype == _htons(RTE_ETHER_TYPE_QINQ)) {
 			vlan_hdr = (struct rte_vlan_hdr *)
-				((char *)eth_hdr + sizeof(*eth_hdr));
+				((char *)eth_hdr + sizeof(*eth_hdr) +
+				(i * sizeof(struct rte_vlan_hdr)));
 			ethertype = vlan_hdr->eth_proto;
 		}
 		return ethertype;
Signature · 1 line
2.39.5 (Apple Git-154)
#1 lore raw
Quoted 34 lines

From: Raslan Darawsheh [mailto:[email protected]]
Sent: Sunday, 23 March 2025 13.28

Updated the get_ethertype_by_ptype function to correctly parse VLAN headers.

Fixes 76730c7b9b5a ("app/testpmd: use packet type parsing API")
---
 app/test-pmd/csumonly.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
app/test-pmd/csumonly.c +1 -0
index 5b906eaa53..302cc4cc66 100644
@@ -468,6 +468,7 @@ get_ethertype_by_ptype(struct rte_ether_hdr
 *eth_hdr, uint32_t ptype)
 {
 	struct rte_vlan_hdr *vlan_hdr;
 	uint16_t ethertype;
+	uint32_t i = 0;
 
 	switch (ptype) {
app/test-pmd/csumonly.c +0 -1
 	case RTE_PTYPE_L3_IPV4:
@@ -486,10 +487,11 @@ get_ethertype_by_ptype(struct rte_ether_hdr
 *eth_hdr, uint32_t ptype)
 		return _htons(RTE_ETHER_TYPE_IPV6);
 	default:
 		ethertype = eth_hdr->ether_type;
-		while (eth_hdr->ether_type == _htons(RTE_ETHER_TYPE_VLAN)

||

app/test-pmd/csumonly.c get_ethertype_by_ptype(struct rte_ether_hdr *eth_hdr, uint32_t ptype) · line 494

Quoted 7 lines
app/test-pmd/csumonly.c +4 -2
-			eth_hdr->ether_type == _htons(RTE_ETHER_TYPE_QINQ)) {
+		while (ethertype == _htons(RTE_ETHER_TYPE_VLAN) ||
+			ethertype == _htons(RTE_ETHER_TYPE_QINQ)) {
 			vlan_hdr = (struct rte_vlan_hdr *)
-				((char *)eth_hdr + sizeof(*eth_hdr));
+				((char *)eth_hdr + sizeof(*eth_hdr) +
+				(i * sizeof(struct rte_vlan_hdr)));

Doesn't work; "i" is not incremented, and remains 0.
Instead do this: Initialize (struct rte_vlan_hdr *)vlan_hdr=RTE_PTR_ADD(eth_hdr, offsetof(rte_ether_hdr, ether_type)) before the loop, and move vlan_hdr+=RTE_VLAN_HLEN inside the loop.

Quoted 5 lines
app/test-pmd/csumonly.c +0 -1
 			ethertype = vlan_hdr->eth_proto;
 		}
 		return ethertype;
--

2.39.5 (Apple Git-154)

#2 lore raw
Quoted 36 lines
Quoted 36 lines

From: Raslan Darawsheh [mailto:[email protected]]
Sent: Sunday, 23 March 2025 13.28

Updated the get_ethertype_by_ptype function to correctly parse VLAN headers.

Fixes 76730c7b9b5a ("app/testpmd: use packet type parsing API")
Quoted 1 line
---
 app/test-pmd/csumonly.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
app/test-pmd/csumonly.c +1 -0
index 5b906eaa53..302cc4cc66 100644
@@ -468,6 +468,7 @@ get_ethertype_by_ptype(struct rte_ether_hdr
 *eth_hdr, uint32_t ptype)
 {
       struct rte_vlan_hdr *vlan_hdr;
       uint16_t ethertype;
+     uint32_t i = 0;
 
       switch (ptype) {
       case RTE_PTYPE_L3_IPV4:
app/test-pmd/csumonly.c +0 -1
@@ -486,10 +487,11 @@ get_ethertype_by_ptype(struct rte_ether_hdr
 *eth_hdr, uint32_t ptype)
               return _htons(RTE_ETHER_TYPE_IPV6);
       default:
               ethertype = eth_hdr->ether_type;
-             while (eth_hdr->ether_type == _htons(RTE_ETHER_TYPE_VLAN)

||

app/test-pmd/csumonly.c +1 -1
-                     eth_hdr->ether_type == _htons(RTE_ETHER_TYPE_QINQ)) {
+             while (ethertype == _htons(RTE_ETHER_TYPE_VLAN) ||
Quoted 8 lines

app/test-pmd/csumonly.c get_ethertype_by_ptype(struct rte_ether_hdr *eth_hdr, uint32_t ptype) · line 494

Quoted 5 lines
app/test-pmd/csumonly.c +3 -1
+                     ethertype == _htons(RTE_ETHER_TYPE_QINQ)) {
                       vlan_hdr = (struct rte_vlan_hdr *)
-                             ((char *)eth_hdr + sizeof(*eth_hdr));
+                             ((char *)eth_hdr + sizeof(*eth_hdr) +
+                             (i * sizeof(struct rte_vlan_hdr)));

Doesn't work; "i" is not incremented, and remains 0.
Instead do this: Initialize (struct rte_vlan_hdr *)vlan_hdr=RTE_PTR_ADD(eth_hdr, offsetof(rte_ether_hdr, ether_type)) before the loop, and move vlan_hdr+=RTE_VLAN_HLEN inside the loop.

Yes you are correct I had a (i++ *) but must have slipped while rebasing will handle your way in v2

Quoted 5 lines
Quoted 5 lines
                       ethertype = vlan_hdr->eth_proto;
               }
               return ethertype;

--
2.39.5 (Apple Git-154)

Kindest regards
Raslan Darawsheh

#3 lore raw

Comment inline below.

From: Raslan Darawsheh [mailto:[email protected]]
Sent: Monday, 24 March 2025 08.35

Quoted 36 lines
Quoted 36 lines

From: Raslan Darawsheh [mailto:[email protected]]
Sent: Sunday, 23 March 2025 13.28

Updated the get_ethertype_by_ptype function to correctly parse VLAN headers.

Fixes 76730c7b9b5a ("app/testpmd: use packet type parsing API")
Quoted 1 line
---
 app/test-pmd/csumonly.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
app/test-pmd/csumonly.c +1 -0
index 5b906eaa53..302cc4cc66 100644
@@ -468,6 +468,7 @@ get_ethertype_by_ptype(struct rte_ether_hdr
 *eth_hdr, uint32_t ptype)
 {
       struct rte_vlan_hdr *vlan_hdr;
       uint16_t ethertype;
+     uint32_t i = 0;
 
       switch (ptype) {
       case RTE_PTYPE_L3_IPV4:
app/test-pmd/csumonly.c +0 -1
@@ -486,10 +487,11 @@ get_ethertype_by_ptype(struct rte_ether_hdr
 *eth_hdr, uint32_t ptype)
               return _htons(RTE_ETHER_TYPE_IPV6);
       default:
               ethertype = eth_hdr->ether_type;
-             while (eth_hdr->ether_type == _htons(RTE_ETHER_TYPE_VLAN)

||

app/test-pmd/csumonly.c +1 -1
-                     eth_hdr->ether_type == _htons(RTE_ETHER_TYPE_QINQ)) {
+             while (ethertype == _htons(RTE_ETHER_TYPE_VLAN) ||
Quoted 8 lines

app/test-pmd/csumonly.c get_ethertype_by_ptype(struct rte_ether_hdr *eth_hdr, uint32_t ptype) · line 494

Quoted 5 lines
app/test-pmd/csumonly.c +3 -1
+                     ethertype == _htons(RTE_ETHER_TYPE_QINQ)) {
                       vlan_hdr = (struct rte_vlan_hdr *)
-                             ((char *)eth_hdr + sizeof(*eth_hdr));
+                             ((char *)eth_hdr + sizeof(*eth_hdr) +
+                             (i * sizeof(struct rte_vlan_hdr)));

Doesn't work; "i" is not incremented, and remains 0.
Instead do this: Initialize (struct rte_vlan_hdr *)vlan_hdr=RTE_PTR_ADD(eth_hdr, offsetof(rte_ether_hdr, ether_type)) before the loop, and move vlan_hdr+=RTE_VLAN_HLEN inside the loop.

Yes you are correct I had a (i++ *) but must have slipped while rebasing will handle your way in v2

MB: Noticed a bug in my suggestion: Due to the type of vlan_hdr, moving it should be vlan_hdr++, not vlan_hdr+=RTE_VLAN_HLEN.

MB: And you might want to impose a maximum boundary to the loop, as suggested by Stephen. E.g. break out of the loop if vlan_hdr>RTE_PTR_ADD(eth_hdr, offsetof(rte_ether_hdr, ether_type))+4*sizeof(struct rte_vlan_hdr). Supporting up to 4 stacked VLAN tags should suffice.

Quoted 5 lines
Quoted 5 lines
                       ethertype = vlan_hdr->eth_proto;
               }
               return ethertype;

--
2.39.5 (Apple Git-154)

Kindest regards

Raslan Darawsheh

#4 lore raw
In reply to Raslan Darawsheh

Comment inline below.

From: Raslan Darawsheh [mailto:[email protected]]
Sent: Monday, 24 March 2025 08.35

Quoted 36 lines
Quoted 36 lines

From: Raslan Darawsheh [mailto:[email protected]]
Sent: Sunday, 23 March 2025 13.28

Updated the get_ethertype_by_ptype function to correctly parse VLAN headers.

Fixes 76730c7b9b5a ("app/testpmd: use packet type parsing API")
Quoted 1 line
---
 app/test-pmd/csumonly.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
app/test-pmd/csumonly.c +1 -0
index 5b906eaa53..302cc4cc66 100644
@@ -468,6 +468,7 @@ get_ethertype_by_ptype(struct rte_ether_hdr
 *eth_hdr, uint32_t ptype)
 {
       struct rte_vlan_hdr *vlan_hdr;
       uint16_t ethertype;
+     uint32_t i = 0;
 
       switch (ptype) {
       case RTE_PTYPE_L3_IPV4:
app/test-pmd/csumonly.c +0 -1
@@ -486,10 +487,11 @@ get_ethertype_by_ptype(struct rte_ether_hdr
 *eth_hdr, uint32_t ptype)
               return _htons(RTE_ETHER_TYPE_IPV6);
       default:
               ethertype = eth_hdr->ether_type;
-             while (eth_hdr->ether_type == _htons(RTE_ETHER_TYPE_VLAN)

||

app/test-pmd/csumonly.c +1 -1
-                     eth_hdr->ether_type == _htons(RTE_ETHER_TYPE_QINQ)) {
+             while (ethertype == _htons(RTE_ETHER_TYPE_VLAN) ||
Quoted 8 lines

app/test-pmd/csumonly.c get_ethertype_by_ptype(struct rte_ether_hdr *eth_hdr, uint32_t ptype) · line 494

Quoted 5 lines
app/test-pmd/csumonly.c +3 -1
+                     ethertype == _htons(RTE_ETHER_TYPE_QINQ)) {
                       vlan_hdr = (struct rte_vlan_hdr *)
-                             ((char *)eth_hdr + sizeof(*eth_hdr));
+                             ((char *)eth_hdr + sizeof(*eth_hdr) +
+                             (i * sizeof(struct rte_vlan_hdr)));

Doesn't work; "i" is not incremented, and remains 0.
Instead do this: Initialize (struct rte_vlan_hdr *)vlan_hdr=RTE_PTR_ADD(eth_hdr, offsetof(rte_ether_hdr, ether_type)) before the loop, and move vlan_hdr+=RTE_VLAN_HLEN inside the loop.

Yes you are correct I had a (i++ *) but must have slipped while rebasing will handle your way in v2

MB: Noticed a bug in my suggestion: Due to the type of vlan_hdr, moving it should be vlan_hdr++, not vlan_hdr+=RTE_VLAN_HLEN.

MB: And you might want to impose a maximum boundary to the loop, as suggested by Stephen. E.g. break out of the loop if vlan_hdr>RTE_PTR_ADD(eth_hdr, offsetof(rte_ether_hdr, ether_type)+4*sizeof(struct rte_vlan_hdr)). Supporting up to 4 stacked VLAN tags should suffice.

Quoted 5 lines
Quoted 5 lines
                       ethertype = vlan_hdr->eth_proto;
               }
               return ethertype;

--
2.39.5 (Apple Git-154)

Kindest regards

Raslan Darawsheh

#5 lore raw

Comment inline below.

From: Raslan Darawsheh [mailto:[email protected]]
Sent: Monday, 24 March 2025 08.35

Quoted 36 lines
Quoted 36 lines

From: Raslan Darawsheh [mailto:[email protected]]
Sent: Sunday, 23 March 2025 13.28

Updated the get_ethertype_by_ptype function to correctly parse VLAN headers.

Fixes 76730c7b9b5a ("app/testpmd: use packet type parsing API")
Quoted 1 line
---
 app/test-pmd/csumonly.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
app/test-pmd/csumonly.c +1 -0
index 5b906eaa53..302cc4cc66 100644
@@ -468,6 +468,7 @@ get_ethertype_by_ptype(struct rte_ether_hdr
 *eth_hdr, uint32_t ptype)
 {
       struct rte_vlan_hdr *vlan_hdr;
       uint16_t ethertype;
+     uint32_t i = 0;
 
       switch (ptype) {
       case RTE_PTYPE_L3_IPV4:
app/test-pmd/csumonly.c +0 -1
@@ -486,10 +487,11 @@ get_ethertype_by_ptype(struct rte_ether_hdr
 *eth_hdr, uint32_t ptype)
               return _htons(RTE_ETHER_TYPE_IPV6);
       default:
               ethertype = eth_hdr->ether_type;
-             while (eth_hdr->ether_type == _htons(RTE_ETHER_TYPE_VLAN)

||

app/test-pmd/csumonly.c +1 -1
-                     eth_hdr->ether_type == _htons(RTE_ETHER_TYPE_QINQ)) {
+             while (ethertype == _htons(RTE_ETHER_TYPE_VLAN) ||
Quoted 8 lines

app/test-pmd/csumonly.c get_ethertype_by_ptype(struct rte_ether_hdr *eth_hdr, uint32_t ptype) · line 494

Quoted 5 lines
app/test-pmd/csumonly.c +3 -1
+                     ethertype == _htons(RTE_ETHER_TYPE_QINQ)) {
                       vlan_hdr = (struct rte_vlan_hdr *)
-                             ((char *)eth_hdr + sizeof(*eth_hdr));
+                             ((char *)eth_hdr + sizeof(*eth_hdr) +
+                             (i * sizeof(struct rte_vlan_hdr)));

Doesn't work; "i" is not incremented, and remains 0.
Instead do this: Initialize (struct rte_vlan_hdr *)vlan_hdr=RTE_PTR_ADD(eth_hdr, offsetof(rte_ether_hdr, ether_type)) before the loop, and move vlan_hdr+=RTE_VLAN_HLEN inside the loop.

Yes you are correct I had a (i++ *) but must have slipped while rebasing will handle your way in v2
MB: Noticed a bug in my suggestion: Due to the type of vlan_hdr, moving it should be vlan_hdr++, not vlan_hdr+=RTE_VLAN_HLEN.
Yes sure will handle,
MB: And you might want to impose a maximum boundary to the loop, as suggested by Stephen. E.g. break out of the loop if vlan_hdr>RTE_PTR_ADD(eth_hdr, offsetof(rte_ether_hdr, ether_type)+4*sizeof(struct rte_vlan_hdr)). Supporting up to 4 stacked VLAN tags should suffice.
I’m not sure this should be added to this patch, as I tend to believe it’s less critical to handle for this release, and also, this might cause inconsistent results, think of the case if we send five stacked vlan tags, then we’ll return ethertype as VLAN as we skipped the last one.

Quoted 5 lines
Quoted 5 lines
                       ethertype = vlan_hdr->eth_proto;
               }
               return ethertype;

--
2.39.5 (Apple Git-154)

Kindest regards
Raslan Darawsheh

#6 lore raw

Answer inline below.

From: Raslan Darawsheh [mailto:[email protected]]
Sent: Monday, 24 March 2025 09.32

Comment inline below.

From: Raslan Darawsheh [mailto:[email protected]]
Sent: Monday, 24 March 2025 08.35

Quoted 36 lines
Quoted 36 lines

From: Raslan Darawsheh [mailto:[email protected]]
Sent: Sunday, 23 March 2025 13.28

Updated the get_ethertype_by_ptype function to correctly parse VLAN headers.

Fixes 76730c7b9b5a ("app/testpmd: use packet type parsing API")
Quoted 1 line
---
 app/test-pmd/csumonly.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
app/test-pmd/csumonly.c +1 -0
index 5b906eaa53..302cc4cc66 100644
@@ -468,6 +468,7 @@ get_ethertype_by_ptype(struct rte_ether_hdr
 *eth_hdr, uint32_t ptype)
 {
       struct rte_vlan_hdr *vlan_hdr;
       uint16_t ethertype;
+     uint32_t i = 0;
 
       switch (ptype) {
       case RTE_PTYPE_L3_IPV4:
app/test-pmd/csumonly.c +0 -1
@@ -486,10 +487,11 @@ get_ethertype_by_ptype(struct rte_ether_hdr
 *eth_hdr, uint32_t ptype)
               return _htons(RTE_ETHER_TYPE_IPV6);
       default:
               ethertype = eth_hdr->ether_type;
-             while (eth_hdr->ether_type == _htons(RTE_ETHER_TYPE_VLAN)

||

app/test-pmd/csumonly.c +1 -1
-                     eth_hdr->ether_type == _htons(RTE_ETHER_TYPE_QINQ)) {
+             while (ethertype == _htons(RTE_ETHER_TYPE_VLAN) ||
Quoted 8 lines

app/test-pmd/csumonly.c get_ethertype_by_ptype(struct rte_ether_hdr *eth_hdr, uint32_t ptype) · line 494

Quoted 5 lines
app/test-pmd/csumonly.c +3 -1
+                     ethertype == _htons(RTE_ETHER_TYPE_QINQ)) {
                       vlan_hdr = (struct rte_vlan_hdr *)
-                             ((char *)eth_hdr + sizeof(*eth_hdr));
+                             ((char *)eth_hdr + sizeof(*eth_hdr) +
+                             (i * sizeof(struct rte_vlan_hdr)));

Doesn't work; "i" is not incremented, and remains 0.
Instead do this: Initialize (struct rte_vlan_hdr *)vlan_hdr=RTE_PTR_ADD(eth_hdr, offsetof(rte_ether_hdr, ether_type)) before the loop, and move vlan_hdr+=RTE_VLAN_HLEN inside the loop.

Yes you are correct I had a (i++ *) but must have slipped while rebasing will handle your way in v2

MB: Noticed a bug in my suggestion: Due to the type of vlan_hdr, moving it should be vlan_hdr++, not vlan_hdr+=RTE_VLAN_HLEN.

Yes sure will handle,

MB: And you might want to impose a maximum boundary to the loop, as suggested by Stephen. E.g. break out of the loop if vlan_hdr>RTE_PTR_ADD(eth_hdr, offsetof(rte_ether_hdr, ether_type)+4*sizeof(struct rte_vlan_hdr)). Supporting up to 4 stacked VLAN tags should suffice.

I'm not sure this should be added to this patch, as I tend to believe it's less critical to handle for this release, and also, this might cause inconsistent results, think of the case if we send five stacked vlan tags, then we'll return ethertype as VLAN as we skipped the last one.

MB:

It should go in this patch, to prevent code analysis tools from warning about unbounded loop causing potential buffer overrun - the bug described by Stephen.

IMO, a stack of 4 VLAN tags should suffice, but you can make it 8 to give it some extra margin. More than 8 VLAN tags is never going to happen with real traffic.

In reality, most NICs can only strip 1 or 2 VLAN tags, and would return ethertype as VLAN. But this can be ignored for this patch.

Quoted 5 lines
Quoted 5 lines
                       ethertype = vlan_hdr->eth_proto;
               }
               return ethertype;

--
2.39.5 (Apple Git-154)

Kindest regards

Raslan Darawsheh

#7 lore raw
In reply to Raslan Darawsheh
Quoted 22 lines · On Sun, 23 Mar 2025 14:28:22 +0200 Raslan Darawsheh <[email protected]> wrote:
---
 app/test-pmd/csumonly.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
app/test-pmd/csumonly.c +1 -1
index 5b906eaa53..302cc4cc66 100644
@@ -468,6 +468,7 @@ get_ethertype_by_ptype(struct rte_ether_hdr *eth_hdr, uint32_t ptype)
 {
 	struct rte_vlan_hdr *vlan_hdr;
 	uint16_t ethertype;
+	uint32_t i = 0;
 
 	switch (ptype) {
 	case RTE_PTYPE_L3_IPV4:
@@ -486,10 +487,11 @@ get_ethertype_by_ptype(struct rte_ether_hdr *eth_hdr, uint32_t ptype)
 		return _htons(RTE_ETHER_TYPE_IPV6);
 	default:
 		ethertype = eth_hdr->ether_type;
-		while (eth_hdr->ether_type == _htons(RTE_ETHER_TYPE_VLAN) ||

app/test-pmd/csumonly.c get_ethertype_by_ptype(struct rte_ether_hdr *eth_hdr, uint32_t ptype) · line 497

Quoted 10 lines
app/test-pmd/csumonly.c +4 -2
-			eth_hdr->ether_type == _htons(RTE_ETHER_TYPE_QINQ)) {
+		while (ethertype == _htons(RTE_ETHER_TYPE_VLAN) ||
+			ethertype == _htons(RTE_ETHER_TYPE_QINQ)) {
 			vlan_hdr = (struct rte_vlan_hdr *)
-				((char *)eth_hdr + sizeof(*eth_hdr));
+				((char *)eth_hdr + sizeof(*eth_hdr) +
+				(i * sizeof(struct rte_vlan_hdr)));
 			ethertype = vlan_hdr->eth_proto;
 		}
 		return ethertype;

A loop like this is prone to getting attacked with a malicious packet. You should cut it off after a few vlan headers.
Also. what if packet is truncated, shouldn't be reading past end of data. And what if packet is fragmented, you need to use rte_pktmbuf_read()

#8 lore raw
Quoted 16 lines
Quoted 16 lines
---
 app/test-pmd/csumonly.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
app/test-pmd/csumonly.c +1 -0
index 5b906eaa53..302cc4cc66 100644
@@ -468,6 +468,7 @@ get_ethertype_by_ptype(struct rte_ether_hdr *eth_hdr, uint32_t ptype)
 {
       struct rte_vlan_hdr *vlan_hdr;
       uint16_t ethertype;
+     uint32_t i = 0;
 
Quoted 12 lines

app/test-pmd/csumonly.c get_ethertype_by_ptype(struct rte_ether_hdr *eth_hdr, uint32_t ptype) · line 492

Quoted 12 lines
app/test-pmd/csumonly.c +2 -3
       switch (ptype) {
       case RTE_PTYPE_L3_IPV4:
@@ -486,10 +487,11 @@ get_ethertype_by_ptype(struct rte_ether_hdr *eth_hdr, uint32_t ptype)
               return _htons(RTE_ETHER_TYPE_IPV6);
       default:
               ethertype = eth_hdr->ether_type;
-             while (eth_hdr->ether_type == _htons(RTE_ETHER_TYPE_VLAN) ||
-                     eth_hdr->ether_type == _htons(RTE_ETHER_TYPE_QINQ)) {
+             while (ethertype == _htons(RTE_ETHER_TYPE_VLAN) ||
+                     ethertype == _htons(RTE_ETHER_TYPE_QINQ)) {
                       vlan_hdr = (struct rte_vlan_hdr *)
-                             ((char *)eth_hdr + sizeof(*eth_hdr));

v> + ((char *)eth_hdr + sizeof(*eth_hdr) +
v> + (i * sizeof(struct rte_vlan_hdr)));

Quoted 8 lines

app/test-pmd/csumonly.c get_ethertype_by_ptype(struct rte_ether_hdr *eth_hdr, uint32_t ptype) · line 497

Quoted 3 lines
app/test-pmd/csumonly.c +0 -0
                       ethertype = vlan_hdr->eth_proto;
               }
               return ethertype;

A loop like this is prone to getting attacked with a malicious packet. You should cut it off after a few vlan headers.
Also. what if packet is truncated, shouldn't be reading past end of data. And what if packet is fragmented, you need to use rte_pktmbuf_read()

I’m trying to fix the current loop not really changing the logic, and I’m not sure we handled these cases originally. If needed, we can issue a separate patch for fixing these cases.

Kindest regards
Raslan Darawsheh

#9 lore raw
In reply to Raslan Darawsheh

23/03/2025 13:28, Raslan Darawsheh:

Quoted 2 lines

Updated the get_ethertype_by_ptype function to correctly parse VLAN headers.

You should describe what is broken and how.

Quoted 2 lines
Fixes 76730c7b9b5a ("app/testpmd: use packet type parsing API")

Do you think it is critical for the release?

#10 lore raw
Quoted 5 lines
Quoted 3 lines

Updated the get_ethertype_by_ptype function to correctly parse VLAN headers.

You should describe what is broken and how.

I’ll handle in v2

Quoted 4 lines
Quoted 2 lines
Fixes 76730c7b9b5a ("app/testpmd: use packet type parsing API")

Do you think it is critical for the release?

Yes, as it’s causing an infinite loop if you send a QinQ packet with csum fwd, and it will cause tested to get stuck forever.

#11 lore raw

PATCH v2 1/1 app/testpmd: fix VLAN header parsing

In reply to Raslan Darawsheh

When processing VLAN or QinQ packets, an infinite loop occurred, preventing the csum forward engine from proceeding and causing testpmd to hang during shutdown attempts.

Updated the get_ethertype_by_ptype function to correctly parse VLAN headers.

Fixes 76730c7b9b5a ("app/testpmd: use packet type parsing API")

---
v2: update commit log
fixed pointer handling to actually move the pointer

---
 app/test-pmd/csumonly.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
app/test-pmd/csumonly.c +5 -4
index 5b906eaa53..7bd1040c72 100644
@@ -486,10 +486,11 @@ get_ethertype_by_ptype(struct rte_ether_hdr *eth_hdr, uint32_t ptype)
 		return _htons(RTE_ETHER_TYPE_IPV6);
 	default:
 		ethertype = eth_hdr->ether_type;
-		while (eth_hdr->ether_type == _htons(RTE_ETHER_TYPE_VLAN) ||
-			eth_hdr->ether_type == _htons(RTE_ETHER_TYPE_QINQ)) {
-			vlan_hdr = (struct rte_vlan_hdr *)
-				((char *)eth_hdr + sizeof(*eth_hdr));
+		vlan_hdr = (struct rte_vlan_hdr *) RTE_PTR_ADD(eth_hdr,
+				 offsetof(struct rte_ether_hdr, ether_type));
+		while ((ethertype == _htons(RTE_ETHER_TYPE_VLAN) ||
+			ethertype == _htons(RTE_ETHER_TYPE_QINQ))) {
+			vlan_hdr++;
 			ethertype = vlan_hdr->eth_proto;
 		}
 		return ethertype;
Signature · 1 line
2.39.5 (Apple Git-154)
#12 lore raw
Quoted 32 lines

From: Raslan Darawsheh [mailto:[email protected]]
Sent: Monday, 24 March 2025 09.50

When processing VLAN or QinQ packets, an infinite loop occurred, preventing the csum forward engine from proceeding and causing testpmd to hang during shutdown attempts.

Updated the get_ethertype_by_ptype function to correctly parse VLAN headers.

Fixes 76730c7b9b5a ("app/testpmd: use packet type parsing API")

---
v2: update commit log
fixed pointer handling to actually move the pointer

---
 app/test-pmd/csumonly.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
app/test-pmd/csumonly.c +0 -1
index 5b906eaa53..7bd1040c72 100644
@@ -486,10 +486,11 @@ get_ethertype_by_ptype(struct rte_ether_hdr
 *eth_hdr, uint32_t ptype)
 		return _htons(RTE_ETHER_TYPE_IPV6);
 	default:
 		ethertype = eth_hdr->ether_type;
-		while (eth_hdr->ether_type == _htons(RTE_ETHER_TYPE_VLAN)

||

app/test-pmd/csumonly.c get_ethertype_by_ptype(struct rte_ether_hdr *eth_hdr, uint32_t ptype) · line 492

Quoted 7 lines
app/test-pmd/csumonly.c +4 -3
-			eth_hdr->ether_type == _htons(RTE_ETHER_TYPE_QINQ)) {
-			vlan_hdr = (struct rte_vlan_hdr *)
-				((char *)eth_hdr + sizeof(*eth_hdr));
+		vlan_hdr = (struct rte_vlan_hdr *) RTE_PTR_ADD(eth_hdr,
+				 offsetof(struct rte_ether_hdr, ether_type));
+		while ((ethertype == _htons(RTE_ETHER_TYPE_VLAN) ||
+			ethertype == _htons(RTE_ETHER_TYPE_QINQ))) {

Two details:
1. Lines continuing on the next line must be indented by two TABs.
2. Please add the boundary check, as requested by Stephen. (I realize this mail may have crossed my response arguing why it is required.)

Quoted 6 lines
app/test-pmd/csumonly.c +1 -1
+			vlan_hdr++;
 			ethertype = vlan_hdr->eth_proto;
 		}
 		return ethertype;
--

2.39.5 (Apple Git-154)

#13 lore raw
Quoted 11 lines

app/test-pmd/csumonly.c get_ethertype_by_ptype(struct rte_ether_hdr *eth_hdr, uint32_t ptype) · line 492

Quoted 7 lines
app/test-pmd/csumonly.c +4 -3
-                     eth_hdr->ether_type == _htons(RTE_ETHER_TYPE_QINQ)) {
-                     vlan_hdr = (struct rte_vlan_hdr *)
-                             ((char *)eth_hdr + sizeof(*eth_hdr));
+             vlan_hdr = (struct rte_vlan_hdr *) RTE_PTR_ADD(eth_hdr,
+                              offsetof(struct rte_ether_hdr, ether_type));
+             while ((ethertype == _htons(RTE_ETHER_TYPE_VLAN) ||
+                     ethertype == _htons(RTE_ETHER_TYPE_QINQ))) {

Two details:
1. Lines continuing on the next line must be indented by two TABs.
2. Please add the boundary check, as requested by Stephen. (I realize this mail may have crossed my response arguing why it is required.)

Sure, I’ve sent a V3 having these fixed thanks for your review and comments.

Quoted 6 lines
Quoted 6 lines
+                     vlan_hdr++;
                       ethertype = vlan_hdr->eth_proto;
               }
               return ethertype;

--
2.39.5 (Apple Git-154)

#14 lore raw

PATCH v3 1/1 app/testpmd: fix VLAN header parsing

In reply to Raslan Darawsheh

When processing VLAN or QinQ packets, an infinite loop occurred, preventing the csum forward engine from proceeding and causing testpmd to hang during shutdown attempts.

Updated the get_ethertype_by_ptype function to correctly parse VLAN headers.

Fixes 76730c7b9b5a ("app/testpmd: use packet type parsing API")

---
v3: fixed wrong indentation
add boundaries check for 8 VLANs at max

v2: update commit log
fixed pointer handling to actually move the pointer

---
 app/test-pmd/csumonly.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
app/test-pmd/csumonly.c +11 -5
index 5b906eaa53..710967ca4d 100644
@@ -59,6 +59,8 @@
 #define GRE_SUPPORTED_FIELDS	(GRE_CHECKSUM_PRESENT | GRE_KEY_PRESENT |\
 				 GRE_SEQUENCE_PRESENT)
 
+#define MAX_VLAN_HEADERS 8
+
 /* We cannot use rte_cpu_to_be_16() on a constant in a switch/case */
 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
 #define _htons(x) ((uint16_t)((((x) & 0x00ffU) << 8) | (((x) & 0xff00U) >> 8)))
@@ -466,9 +468,10 @@ pkts_ip_csum_recalc(struct rte_mbuf **pkts_burst, const uint16_t nb_pkts, uint64
 static uint32_t
 get_ethertype_by_ptype(struct rte_ether_hdr *eth_hdr, uint32_t ptype)
 {
-	struct rte_vlan_hdr *vlan_hdr;
+	struct rte_vlan_hdr *vlan_hdr, *max_vlans;
 	uint16_t ethertype;
 
+
 	switch (ptype) {
 	case RTE_PTYPE_L3_IPV4:
 	case RTE_PTYPE_L3_IPV4_EXT:
@@ -486,10 +489,13 @@ get_ethertype_by_ptype(struct rte_ether_hdr *eth_hdr, uint32_t ptype)
 		return _htons(RTE_ETHER_TYPE_IPV6);
 	default:
 		ethertype = eth_hdr->ether_type;
-		while (eth_hdr->ether_type == _htons(RTE_ETHER_TYPE_VLAN) ||
-			eth_hdr->ether_type == _htons(RTE_ETHER_TYPE_QINQ)) {
-			vlan_hdr = (struct rte_vlan_hdr *)
-				((char *)eth_hdr + sizeof(*eth_hdr));
+		vlan_hdr = (struct rte_vlan_hdr *) RTE_PTR_ADD(eth_hdr,
+					offsetof(struct rte_ether_hdr, ether_type));
+		max_vlans = vlan_hdr + MAX_VLAN_HEADERS;
+		while (((ethertype == _htons(RTE_ETHER_TYPE_VLAN) ||
+			ethertype == _htons(RTE_ETHER_TYPE_QINQ))) &&
+			vlan_hdr < max_vlans) {
+			vlan_hdr++;
 			ethertype = vlan_hdr->eth_proto;
 		}
 		return ethertype;
Signature · 1 line
2.39.5 (Apple Git-154)
Reviewed-by on v3 1/1
#15 lore raw
Quoted 44 lines

From: Raslan Darawsheh [mailto:[email protected]]
Sent: Monday, 24 March 2025 10.34

When processing VLAN or QinQ packets, an infinite loop occurred, preventing the csum forward engine from proceeding and causing testpmd to hang during shutdown attempts.

Updated the get_ethertype_by_ptype function to correctly parse VLAN headers.

Fixes 76730c7b9b5a ("app/testpmd: use packet type parsing API")

---
v3: fixed wrong indentation
add boundaries check for 8 VLANs at max

v2: update commit log
fixed pointer handling to actually move the pointer

---
 app/test-pmd/csumonly.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
app/test-pmd/csumonly.c +2 -0
index 5b906eaa53..710967ca4d 100644
@@ -59,6 +59,8 @@
 #define GRE_SUPPORTED_FIELDS	(GRE_CHECKSUM_PRESENT | GRE_KEY_PRESENT
 |\
 				 GRE_SEQUENCE_PRESENT)
 
+#define MAX_VLAN_HEADERS 8
+
 /* We cannot use rte_cpu_to_be_16() on a constant in a switch/case */
 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN

#define _htons(x) ((uint16_t)((((x) & 0x00ffU) << 8) | (((x) &
0xff00U) >> 8)))

app/test-pmd/csumonly.c +0 -0
@@ -466,9 +468,10 @@ pkts_ip_csum_recalc(struct rte_mbuf **pkts_burst,

const uint16_t nb_pkts, uint64
static uint32_t
get_ethertype_by_ptype(struct rte_ether_hdr *eth_hdr, uint32_t ptype) {

app/test-pmd/csumonly.c pkts_ip_csum_recalc(struct rte_mbuf **pkts_burst, const uint16_t nb_pkts, uint64 · line 474

Quoted 5 lines
app/test-pmd/csumonly.c +2 -1
-	struct rte_vlan_hdr *vlan_hdr;
+	struct rte_vlan_hdr *vlan_hdr, *max_vlans;
 	uint16_t ethertype;
 
+

Please omit adding an extra empty line.

Quoted 15 lines
app/test-pmd/csumonly.c +0 -1
 	switch (ptype) {
 	case RTE_PTYPE_L3_IPV4:
 	case RTE_PTYPE_L3_IPV4_EXT:
@@ -486,10 +489,13 @@ get_ethertype_by_ptype(struct rte_ether_hdr
 *eth_hdr, uint32_t ptype)
 		return _htons(RTE_ETHER_TYPE_IPV6);
 	default:
 		ethertype = eth_hdr->ether_type;
-		while (eth_hdr->ether_type == _htons(RTE_ETHER_TYPE_VLAN)

||

app/test-pmd/csumonly.c +2 -3
-			eth_hdr->ether_type == _htons(RTE_ETHER_TYPE_QINQ)) {
-			vlan_hdr = (struct rte_vlan_hdr *)
-				((char *)eth_hdr + sizeof(*eth_hdr));
+		vlan_hdr = (struct rte_vlan_hdr *) RTE_PTR_ADD(eth_hdr,
+					offsetof(struct rte_ether_hdr,
Quoted 1 line

ether_type));

Above line indentation with 2, not 3 TABs.

app/test-pmd/csumonly.c get_ethertype_by_ptype(struct rte_ether_hdr *eth_hdr, uint32_t ptype) · line 495

Quoted 2 lines
app/test-pmd/csumonly.c +2 -0
+		max_vlans = vlan_hdr + MAX_VLAN_HEADERS;
+		while (((ethertype == _htons(RTE_ETHER_TYPE_VLAN) ||

Superfluous parenthesis: "while ( ((x || y)) && z)".

app/test-pmd/csumonly.c get_ethertype_by_ptype(struct rte_ether_hdr *eth_hdr, uint32_t ptype) · line 497

Quoted 2 lines
app/test-pmd/csumonly.c +2 -0
+			ethertype == _htons(RTE_ETHER_TYPE_QINQ))) &&
+			vlan_hdr < max_vlans) {

Above two lines indentation with 2, not 1 TAB.

Quoted 6 lines
app/test-pmd/csumonly.c +1 -1
+			vlan_hdr++;
 			ethertype = vlan_hdr->eth_proto;
 		}
 		return ethertype;
--

2.39.5 (Apple Git-154)

With above nits fixed (can be fixed when merging),

#16 lore raw

24/03/2025 14:18, Morten Brørup:

Quoted 52 lines
Quoted 44 lines

From: Raslan Darawsheh [mailto:[email protected]]
Sent: Monday, 24 March 2025 10.34

When processing VLAN or QinQ packets, an infinite loop occurred, preventing the csum forward engine from proceeding and causing testpmd to hang during shutdown attempts.

Updated the get_ethertype_by_ptype function to correctly parse VLAN headers.

Fixes 76730c7b9b5a ("app/testpmd: use packet type parsing API")

---
v3: fixed wrong indentation
add boundaries check for 8 VLANs at max

v2: update commit log
fixed pointer handling to actually move the pointer

---
 app/test-pmd/csumonly.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
app/test-pmd/csumonly.c +2 -0
index 5b906eaa53..710967ca4d 100644
@@ -59,6 +59,8 @@
 #define GRE_SUPPORTED_FIELDS	(GRE_CHECKSUM_PRESENT | GRE_KEY_PRESENT
 |\
 				 GRE_SEQUENCE_PRESENT)
 
+#define MAX_VLAN_HEADERS 8
+
 /* We cannot use rte_cpu_to_be_16() on a constant in a switch/case */
 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN

#define _htons(x) ((uint16_t)((((x) & 0x00ffU) << 8) | (((x) &
0xff00U) >> 8)))

app/test-pmd/csumonly.c +0 -0
@@ -466,9 +468,10 @@ pkts_ip_csum_recalc(struct rte_mbuf **pkts_burst,

const uint16_t nb_pkts, uint64
static uint32_t
get_ethertype_by_ptype(struct rte_ether_hdr *eth_hdr, uint32_t ptype) {

app/test-pmd/csumonly.c pkts_ip_csum_recalc(struct rte_mbuf **pkts_burst, const uint16_t nb_pkts, uint64 · line 474

Quoted 5 lines
app/test-pmd/csumonly.c +2 -1
-	struct rte_vlan_hdr *vlan_hdr;
+	struct rte_vlan_hdr *vlan_hdr, *max_vlans;
 	uint16_t ethertype;
 
+

Please omit adding an extra empty line.

Quoted 14 lines
Quoted 10 lines
app/test-pmd/csumonly.c +0 -1
 	switch (ptype) {
 	case RTE_PTYPE_L3_IPV4:
 	case RTE_PTYPE_L3_IPV4_EXT:
@@ -486,10 +489,13 @@ get_ethertype_by_ptype(struct rte_ether_hdr
 *eth_hdr, uint32_t ptype)
 		return _htons(RTE_ETHER_TYPE_IPV6);
 	default:
 		ethertype = eth_hdr->ether_type;
-		while (eth_hdr->ether_type == _htons(RTE_ETHER_TYPE_VLAN)

||

app/test-pmd/csumonly.c get_ethertype_by_ptype(struct rte_ether_hdr *eth_hdr, uint32_t ptype) · line 492

Quoted 4 lines
app/test-pmd/csumonly.c +1 -3
-			eth_hdr->ether_type == _htons(RTE_ETHER_TYPE_QINQ)) {
-			vlan_hdr = (struct rte_vlan_hdr *)
-				((char *)eth_hdr + sizeof(*eth_hdr));
+		vlan_hdr = (struct rte_vlan_hdr *) RTE_PTR_ADD(eth_hdr,

useless type cast

Quoted 23 lines
Quoted 2 lines
app/test-pmd/csumonly.c +1 -0
+					offsetof(struct rte_ether_hdr,

ether_type));

Above line indentation with 2, not 3 TABs.

app/test-pmd/csumonly.c get_ethertype_by_ptype(struct rte_ether_hdr *eth_hdr, uint32_t ptype) · line 495

Quoted 2 lines
app/test-pmd/csumonly.c +2 -0
+		max_vlans = vlan_hdr + MAX_VLAN_HEADERS;
+		while (((ethertype == _htons(RTE_ETHER_TYPE_VLAN) ||

Superfluous parenthesis: "while ( ((x || y)) && z)".

app/test-pmd/csumonly.c get_ethertype_by_ptype(struct rte_ether_hdr *eth_hdr, uint32_t ptype) · line 497

Quoted 2 lines
app/test-pmd/csumonly.c +2 -0
+			ethertype == _htons(RTE_ETHER_TYPE_QINQ))) &&
+			vlan_hdr < max_vlans) {

Above two lines indentation with 2, not 1 TAB.

Quoted 6 lines
app/test-pmd/csumonly.c +1 -1
+			vlan_hdr++;
 			ethertype = vlan_hdr->eth_proto;
 		}
 		return ethertype;
--

2.39.5 (Apple Git-154)

With above nits fixed (can be fixed when merging),

Quoted 1 line

All nits fixed and applied, thanks.