]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
netpoll: factor out UDP checksum calculation into helper
authorBreno Leitao <leitao@debian.org>
Wed, 2 Jul 2025 10:06:34 +0000 (03:06 -0700)
committerJakub Kicinski <kuba@kernel.org>
Tue, 8 Jul 2025 01:52:55 +0000 (18:52 -0700)
Extract UDP checksum calculation logic from netpoll_send_udp()
into a new static helper function netpoll_udp_checksum(). This
reduces code duplication and improves readability for both IPv4
and IPv6 cases.

No functional change intended.

Signed-off-by: Breno Leitao <leitao@debian.org>
Reviewed-by: Simon Horman <horms@kernel.org>
Link: https://patch.msgid.link/20250702-netpoll_untagle_ip-v2-2-13cf3db24e2b@debian.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
net/core/netpoll.c

index ac0ae9630654a57dd43839126bd635fb5282b2f2..24e6ad2da80967604f48de6bbc3a74f3aadda3fd 100644 (file)
@@ -372,6 +372,31 @@ out:
        return ret;
 }
 
+static void netpoll_udp_checksum(struct netpoll *np, struct sk_buff *skb,
+                                int len)
+{
+       struct udphdr *udph;
+       int udp_len;
+
+       udp_len = len + sizeof(struct udphdr);
+       udph = udp_hdr(skb);
+
+       /* check needs to be set, since it will be consumed in csum_partial */
+       udph->check = 0;
+       if (np->ipv6)
+               udph->check = csum_ipv6_magic(&np->local_ip.in6,
+                                             &np->remote_ip.in6,
+                                             udp_len, IPPROTO_UDP,
+                                             csum_partial(udph, udp_len, 0));
+       else
+               udph->check = csum_tcpudp_magic(np->local_ip.ip,
+                                               np->remote_ip.ip,
+                                               udp_len, IPPROTO_UDP,
+                                               csum_partial(udph, udp_len, 0));
+       if (udph->check == 0)
+               udph->check = CSUM_MANGLED_0;
+}
+
 netdev_tx_t netpoll_send_skb(struct netpoll *np, struct sk_buff *skb)
 {
        unsigned long flags;
@@ -425,15 +450,8 @@ int netpoll_send_udp(struct netpoll *np, const char *msg, int len)
        udph->dest = htons(np->remote_port);
        udph->len = htons(udp_len);
 
-       udph->check = 0;
+       netpoll_udp_checksum(np, skb, len);
        if (np->ipv6) {
-               udph->check = csum_ipv6_magic(&np->local_ip.in6,
-                                             &np->remote_ip.in6,
-                                             udp_len, IPPROTO_UDP,
-                                             csum_partial(udph, udp_len, 0));
-               if (udph->check == 0)
-                       udph->check = CSUM_MANGLED_0;
-
                skb_push(skb, sizeof(struct ipv6hdr));
                skb_reset_network_header(skb);
                ip6h = ipv6_hdr(skb);
@@ -454,13 +472,6 @@ int netpoll_send_udp(struct netpoll *np, const char *msg, int len)
                skb_reset_mac_header(skb);
                skb->protocol = eth->h_proto = htons(ETH_P_IPV6);
        } else {
-               udph->check = csum_tcpudp_magic(np->local_ip.ip,
-                                               np->remote_ip.ip,
-                                               udp_len, IPPROTO_UDP,
-                                               csum_partial(udph, udp_len, 0));
-               if (udph->check == 0)
-                       udph->check = CSUM_MANGLED_0;
-
                skb_push(skb, sizeof(struct iphdr));
                skb_reset_network_header(skb);
                iph = ip_hdr(skb);