Skip to content

[PATCH v5] helper: ip: correct ipv4 header checksum calculation#132

Closed
lumag wants to merge 3 commits intoOpenDataPlane:masterfrom
lumag:fix-checksum
Closed

[PATCH v5] helper: ip: correct ipv4 header checksum calculation#132
lumag wants to merge 3 commits intoOpenDataPlane:masterfrom
lumag:fix-checksum

Conversation

@lumag
Copy link

@lumag lumag commented Aug 18, 2017

Current code for IPv4 header checksum calculation assumes that packet
data is aligned on 2-byte boundary, that there are no optional headers,
etc. Rewrite checksumming code to properly copy & process headers.

Signed-off-by: Dmitry Eremin-Solenikov dmitry.ereminsolenikov@linaro.org

@muvarov muvarov changed the title helper: ip: correct ipv4 header checksum calculation [PATCH v1] helper: ip: correct ipv4 header checksum calculation Aug 18, 2017
@lumag
Copy link
Author

lumag commented Aug 18, 2017

This is a fixup for https://bugs.linaro.org/show_bug.cgi?id=2976

memcpy(buf, ip, sizeof(*ip));
res = odp_packet_copy_to_mem(pkt, offset + sizeof(*ip),
nleft - sizeof(*ip),
buf + sizeof(*ip)/2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checkpatch flags this. It's a legit issue that should be fixed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Bill-Fischofer-Linaro missed that. Should be fixed now.

@muvarov muvarov changed the title [PATCH v1] helper: ip: correct ipv4 header checksum calculation [PATCH v5] helper: ip: correct ipv4 header checksum calculation Aug 18, 2017
nleft - sizeof(*ip),
buf + sizeof(*ip) / 2);
if (odp_unlikely(res < 0))
return res;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type function mismatch. odp_u16sum_t is uint16_t. According to use cases for usage of that function, this function has to be int, not odp_u16sum_t.

* @return IPv4 checksum in host cpu order, or 0 on failure
*/
static inline odp_u16sum_t odph_ipv4_csum_update(odp_packet_t pkt)
static inline void odph_ipv4_csum_update(odp_packet_t pkt)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function can return errors, why it's void? Or it has to assert() on each error, or return valid error which is checked on upper layer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. The @return is also not correct if this becomes a void function.

@Bill-Fischofer-Linaro Bill-Fischofer-Linaro dismissed their stale review August 23, 2017 15:32

@muvarov found some things my review overlooked.

@lumag
Copy link
Author

lumag commented Aug 24, 2017

@muvarov @Bill-Fischofer-Linaro updated PR to include proposed fixes plus few more changes.


for (sum = 0; len > 1; len -= 2)
sum += *buf++;
sum += odp_be_to_cpu_16(*buf++);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why this is necessary. Isn't it more efficient to calculate the checksum directly and then return the result in network byte order? The value is going to be the same, it's just how it's being returned.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, true. I updated this commit to just change documentation.

if (odp_unlikely(res < 0))
return res;

res = odph_ipv4_csum(pkt, offset, &ip, &chksum);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're deleting setting the chksum to 0 elsewhere before calling odph_ipv4_csum_update(), don't you need to set it to zero here before calling odph_ipv4_csum()? This field is supposed to be initialized to zero to calculate the checksum.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. odph_ipv4_csum() initialises checksum to 0 in the header copy. This simplifies code for header verification, etc.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I missed that on first read.

Dmitry Eremin-Solenikov added 3 commits August 25, 2017 12:26
All examples and usecases assumed network byte order for odph_chksum()
return value. Instead of changing this convention, rather document that
odph_chksum returns value in network byte order.

Signed-off-by: Dmitry Eremin-Solenikov <dmitry.ereminsolenikov@linaro.org>
Current code for IPv4 header checksum calculation assumes that packet
data is aligned on 2-byte boundary, that there are no optional headers,
etc. Rewrite checksumming code to properly copy & process headers.

Signed-off-by: Dmitry Eremin-Solenikov <dmitry.ereminsolenikov@linaro.org>
Signed-off-by: Dmitry Eremin-Solenikov <dmitry.ereminsolenikov@linaro.org>
if (odp_unlikely(res < 0))
return res;

res = odph_ipv4_csum(pkt, offset, &ip, &chksum);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I missed that on first read.

@muvarov
Copy link
Contributor

muvarov commented Aug 28, 2017

Merged.

@muvarov muvarov closed this Aug 28, 2017
@lumag lumag deleted the fix-checksum branch December 27, 2017 18:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

Comments