Skip to content

Commit 7aed973

Browse files
leitaoNipaLocal
authored andcommitted
netpoll: factor out IPv4 header setup into push_ipv4() helper
Move IPv4 header construction from netpoll_send_udp() into a new static helper function push_ipv4(). This completes the refactoring started with IPv6 header handling, creating symmetric helper functions for both IP versions. Changes include: 1. Extracting IPv4 header setup logic into push_ipv4() 2. Replacing inline IPv4 code with helper call 3. Moving eth assignment after helper calls for consistency The refactoring reduces code duplication and improves maintainability by isolating IP version-specific logic. Signed-off-by: Breno Leitao <leitao@debian.org> Reviewed-by: Simon Horman <horms@kernel.org> Signed-off-by: NipaLocal <nipa@local>
1 parent 08ae484 commit 7aed973

File tree

1 file changed

+36
-26
lines changed

1 file changed

+36
-26
lines changed

net/core/netpoll.c

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -441,14 +441,44 @@ static void push_ipv6(struct netpoll *np, struct sk_buff *skb, int len)
441441
eth->h_proto = htons(ETH_P_IPV6);
442442
}
443443

444+
static void push_ipv4(struct netpoll *np, struct sk_buff *skb, int len)
445+
{
446+
static atomic_t ip_ident;
447+
struct ethhdr *eth;
448+
struct iphdr *iph;
449+
int ip_len;
450+
451+
ip_len = len + sizeof(struct udphdr) + sizeof(struct iphdr);
452+
453+
skb_push(skb, sizeof(struct iphdr));
454+
skb_reset_network_header(skb);
455+
iph = ip_hdr(skb);
456+
457+
/* iph->version = 4; iph->ihl = 5; */
458+
*(unsigned char *)iph = 0x45;
459+
iph->tos = 0;
460+
put_unaligned(htons(ip_len), &iph->tot_len);
461+
iph->id = htons(atomic_inc_return(&ip_ident));
462+
iph->frag_off = 0;
463+
iph->ttl = 64;
464+
iph->protocol = IPPROTO_UDP;
465+
iph->check = 0;
466+
put_unaligned(np->local_ip.ip, &iph->saddr);
467+
put_unaligned(np->remote_ip.ip, &iph->daddr);
468+
iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
469+
470+
eth = skb_push(skb, ETH_HLEN);
471+
skb_reset_mac_header(skb);
472+
skb->protocol = htons(ETH_P_IP);
473+
eth->h_proto = htons(ETH_P_IP);
474+
}
475+
444476
int netpoll_send_udp(struct netpoll *np, const char *msg, int len)
445477
{
446478
int total_len, ip_len, udp_len;
447479
struct sk_buff *skb;
448480
struct udphdr *udph;
449-
struct iphdr *iph;
450481
struct ethhdr *eth;
451-
static atomic_t ip_ident;
452482

453483
if (!IS_ENABLED(CONFIG_PREEMPT_RT))
454484
WARN_ON_ONCE(!irqs_disabled());
@@ -477,32 +507,12 @@ int netpoll_send_udp(struct netpoll *np, const char *msg, int len)
477507
udph->len = htons(udp_len);
478508

479509
netpoll_udp_checksum(np, skb, len);
480-
if (np->ipv6) {
510+
if (np->ipv6)
481511
push_ipv6(np, skb, len);
482-
eth = eth_hdr(skb);
483-
} else {
484-
skb_push(skb, sizeof(struct iphdr));
485-
skb_reset_network_header(skb);
486-
iph = ip_hdr(skb);
487-
488-
/* iph->version = 4; iph->ihl = 5; */
489-
*(unsigned char *)iph = 0x45;
490-
iph->tos = 0;
491-
put_unaligned(htons(ip_len), &(iph->tot_len));
492-
iph->id = htons(atomic_inc_return(&ip_ident));
493-
iph->frag_off = 0;
494-
iph->ttl = 64;
495-
iph->protocol = IPPROTO_UDP;
496-
iph->check = 0;
497-
put_unaligned(np->local_ip.ip, &(iph->saddr));
498-
put_unaligned(np->remote_ip.ip, &(iph->daddr));
499-
iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
500-
501-
eth = skb_push(skb, ETH_HLEN);
502-
skb_reset_mac_header(skb);
503-
skb->protocol = eth->h_proto = htons(ETH_P_IP);
504-
}
512+
else
513+
push_ipv4(np, skb, len);
505514

515+
eth = eth_hdr(skb);
506516
ether_addr_copy(eth->h_source, np->dev->dev_addr);
507517
ether_addr_copy(eth->h_dest, np->remote_mac);
508518

0 commit comments

Comments
 (0)