Skip to content

Commit 4c6d203

Browse files
committed
Merge branch 'netpoll-code-organization-improvements'
Breno Leitao says: ==================== netpoll: Code organization improvements The netpoll_setup() function has grown complex over time, mixing different error handling and concerns like carrier waiting, IPv4 address retrieval, and IPv6 address retrieval all within a single function, which is huge (127 LoC). This patch series refactors the netpoll_setup() function to improve code organization and readability by extracting logical blocks into dedicated helper functions. netpoll_setup() length is reduced to 72 LoC. This series breaks down these responsibilities into focused helper functions. The changes are purely structural with no functional modifications. This changes were tested with the netconsole tests and the netpoll selftest (WIP)[1] Link: https://lore.kernel.org/[email protected] [1] ==================== Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
2 parents 8a97590 + 6ad7969 commit 4c6d203

File tree

1 file changed

+91
-61
lines changed

1 file changed

+91
-61
lines changed

net/core/netpoll.c

Lines changed: 91 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -583,13 +583,97 @@ static char *egress_dev(struct netpoll *np, char *buf)
583583
return buf;
584584
}
585585

586+
static void netpoll_wait_carrier(struct netpoll *np, struct net_device *ndev,
587+
unsigned int timeout)
588+
{
589+
unsigned long atmost;
590+
591+
atmost = jiffies + timeout * HZ;
592+
while (!netif_carrier_ok(ndev)) {
593+
if (time_after(jiffies, atmost)) {
594+
np_notice(np, "timeout waiting for carrier\n");
595+
break;
596+
}
597+
msleep(1);
598+
}
599+
}
600+
601+
/*
602+
* Take the IPv6 from ndev and populate local_ip structure in netpoll
603+
*/
604+
static int netpoll_take_ipv6(struct netpoll *np, struct net_device *ndev)
605+
{
606+
char buf[MAC_ADDR_STR_LEN + 1];
607+
int err = -EDESTADDRREQ;
608+
struct inet6_dev *idev;
609+
610+
if (!IS_ENABLED(CONFIG_IPV6)) {
611+
np_err(np, "IPv6 is not supported %s, aborting\n",
612+
egress_dev(np, buf));
613+
return -EINVAL;
614+
}
615+
616+
idev = __in6_dev_get(ndev);
617+
if (idev) {
618+
struct inet6_ifaddr *ifp;
619+
620+
read_lock_bh(&idev->lock);
621+
list_for_each_entry(ifp, &idev->addr_list, if_list) {
622+
if (!!(ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL) !=
623+
!!(ipv6_addr_type(&np->remote_ip.in6) & IPV6_ADDR_LINKLOCAL))
624+
continue;
625+
/* Got the IP, let's return */
626+
np->local_ip.in6 = ifp->addr;
627+
err = 0;
628+
break;
629+
}
630+
read_unlock_bh(&idev->lock);
631+
}
632+
if (err) {
633+
np_err(np, "no IPv6 address for %s, aborting\n",
634+
egress_dev(np, buf));
635+
return err;
636+
}
637+
638+
np_info(np, "local IPv6 %pI6c\n", &np->local_ip.in6);
639+
return 0;
640+
}
641+
642+
/*
643+
* Take the IPv4 from ndev and populate local_ip structure in netpoll
644+
*/
645+
static int netpoll_take_ipv4(struct netpoll *np, struct net_device *ndev)
646+
{
647+
char buf[MAC_ADDR_STR_LEN + 1];
648+
const struct in_ifaddr *ifa;
649+
struct in_device *in_dev;
650+
651+
in_dev = __in_dev_get_rtnl(ndev);
652+
if (!in_dev) {
653+
np_err(np, "no IP address for %s, aborting\n",
654+
egress_dev(np, buf));
655+
return -EDESTADDRREQ;
656+
}
657+
658+
ifa = rtnl_dereference(in_dev->ifa_list);
659+
if (!ifa) {
660+
np_err(np, "no IP address for %s, aborting\n",
661+
egress_dev(np, buf));
662+
return -EDESTADDRREQ;
663+
}
664+
665+
np->local_ip.ip = ifa->ifa_local;
666+
np_info(np, "local IP %pI4\n", &np->local_ip.ip);
667+
668+
return 0;
669+
}
670+
586671
int netpoll_setup(struct netpoll *np)
587672
{
588673
struct net *net = current->nsproxy->net_ns;
589674
char buf[MAC_ADDR_STR_LEN + 1];
590675
struct net_device *ndev = NULL;
591676
bool ip_overwritten = false;
592-
struct in_device *in_dev;
593677
int err;
594678

595679
rtnl_lock();
@@ -613,85 +697,31 @@ int netpoll_setup(struct netpoll *np)
613697
}
614698

615699
if (!netif_running(ndev)) {
616-
unsigned long atmost;
617-
618700
np_info(np, "device %s not up yet, forcing it\n",
619701
egress_dev(np, buf));
620702

621703
err = dev_open(ndev, NULL);
622-
623704
if (err) {
624705
np_err(np, "failed to open %s\n", ndev->name);
625706
goto put;
626707
}
627708

628709
rtnl_unlock();
629-
atmost = jiffies + carrier_timeout * HZ;
630-
while (!netif_carrier_ok(ndev)) {
631-
if (time_after(jiffies, atmost)) {
632-
np_notice(np, "timeout waiting for carrier\n");
633-
break;
634-
}
635-
msleep(1);
636-
}
637-
710+
netpoll_wait_carrier(np, ndev, carrier_timeout);
638711
rtnl_lock();
639712
}
640713

641714
if (!np->local_ip.ip) {
642715
if (!np->ipv6) {
643-
const struct in_ifaddr *ifa;
644-
645-
in_dev = __in_dev_get_rtnl(ndev);
646-
if (!in_dev)
647-
goto put_noaddr;
648-
649-
ifa = rtnl_dereference(in_dev->ifa_list);
650-
if (!ifa) {
651-
put_noaddr:
652-
np_err(np, "no IP address for %s, aborting\n",
653-
egress_dev(np, buf));
654-
err = -EDESTADDRREQ;
716+
err = netpoll_take_ipv4(np, ndev);
717+
if (err)
655718
goto put;
656-
}
657-
658-
np->local_ip.ip = ifa->ifa_local;
659-
ip_overwritten = true;
660-
np_info(np, "local IP %pI4\n", &np->local_ip.ip);
661719
} else {
662-
#if IS_ENABLED(CONFIG_IPV6)
663-
struct inet6_dev *idev;
664-
665-
err = -EDESTADDRREQ;
666-
idev = __in6_dev_get(ndev);
667-
if (idev) {
668-
struct inet6_ifaddr *ifp;
669-
670-
read_lock_bh(&idev->lock);
671-
list_for_each_entry(ifp, &idev->addr_list, if_list) {
672-
if (!!(ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL) !=
673-
!!(ipv6_addr_type(&np->remote_ip.in6) & IPV6_ADDR_LINKLOCAL))
674-
continue;
675-
np->local_ip.in6 = ifp->addr;
676-
ip_overwritten = true;
677-
err = 0;
678-
break;
679-
}
680-
read_unlock_bh(&idev->lock);
681-
}
682-
if (err) {
683-
np_err(np, "no IPv6 address for %s, aborting\n",
684-
egress_dev(np, buf));
720+
err = netpoll_take_ipv6(np, ndev);
721+
if (err)
685722
goto put;
686-
} else
687-
np_info(np, "local IPv6 %pI6c\n", &np->local_ip.in6);
688-
#else
689-
np_err(np, "IPv6 is not supported %s, aborting\n",
690-
egress_dev(np, buf));
691-
err = -EINVAL;
692-
goto put;
693-
#endif
694723
}
724+
ip_overwritten = true;
695725
}
696726

697727
err = __netpoll_setup(np, ndev);

0 commit comments

Comments
 (0)