Netdev List
 help / color / mirror / Atom feed
* [PATCH v2 1/4] net: add helper for device lookup by destination address
       [not found] <20260727151652.3660476-1-nilay@linux.ibm.com>
@ 2026-07-27 15:16 ` Nilay Shroff
  2026-07-30 23:15   ` Jakub Kicinski
  0 siblings, 1 reply; 3+ messages in thread
From: Nilay Shroff @ 2026-07-27 15:16 UTC (permalink / raw)
  To: linux-nvme, linux-kernel
  Cc: kbusch, hch, hare, sagi, chaitanyak, gjoyce, Nilay Shroff,
	David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman, netdev

Add netdev_get_by_addr(), a helper that looks up the routing table to
retrieve the network device associated with a destination address. The
helper also supports netdev reference tracking.

Cc: "David S. Miller" <davem@davemloft.net>
Cc: Eric Dumazet <edumazet@google.com>
Cc: Jakub Kicinski <kuba@kernel.org>
Cc: Paolo Abeni <pabeni@redhat.com>
Cc: Simon Horman <horms@kernel.org>
Cc: netdev@vger.kernel.org
Signed-off-by: Nilay Shroff <nilay@linux.ibm.com>
---
 include/linux/netdevice.h |  5 +++
 net/core/dev.c            | 84 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 89 insertions(+)

diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
index 9981d637f8b5..f43c3aa8486c 100644
--- a/include/linux/netdevice.h
+++ b/include/linux/netdevice.h
@@ -3487,6 +3487,11 @@ struct net_device *netdev_get_by_index(struct net *net, int ifindex,
 struct net_device *netdev_get_by_index_lock(struct net *net, int ifindex);
 struct net_device *netdev_get_by_name(struct net *net, const char *name,
 				      netdevice_tracker *tracker, gfp_t gfp);
+struct net_device *netdev_get_by_addr(struct net *net,
+				      struct sockaddr_storage *src,
+				      struct sockaddr_storage *dest,
+				      netdevice_tracker *tracker,
+				      gfp_t gfp);
 struct net_device *netdev_get_by_flags_rcu(struct net *net, netdevice_tracker *tracker,
 					   unsigned short flags, unsigned short mask);
 struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex);
diff --git a/net/core/dev.c b/net/core/dev.c
index 5933c5dab09e..fd8afcd26de4 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -163,6 +163,8 @@
 #include <net/page_pool/memory_provider.h>
 #include <net/rps.h>
 #include <linux/phy_link_topology.h>
+#include <net/route.h>
+#include <net/ip6_route.h>
 
 #include "dev.h"
 #include "devmem.h"
@@ -944,6 +946,88 @@ struct net_device *netdev_get_by_name(struct net *net, const char *name,
 }
 EXPORT_SYMBOL(netdev_get_by_name);
 
+/**
+ *	netdev_get_by_addr() - find an egress device by destination address
+ *	@net: the applicable net namespace
+ *	@src: optional source address
+ *	@dest: destination address
+ *	@tracker: tracking onject for the acquired reference
+ *	@gfp: allocation flag for the tracker
+ *
+ *	Find an interface by looking up route table entry based on dest address
+ *	and an optional src address. The returned handle has the usage count
+ *	incremented and the caller must use netdev_put() to release it when it
+ *	is no longer needed. %NULL is returned if dest is not specified, or src
+ *	and dest belong to different address families, or no matching device is
+ *	found.
+ */
+struct net_device *netdev_get_by_addr(struct net *net,
+				      struct sockaddr_storage *src,
+				      struct sockaddr_storage *dest,
+				      netdevice_tracker *tracker,
+				      gfp_t gfp)
+{
+	struct net_device *dev = NULL;
+
+	if (!dest)
+		return NULL;
+
+	if (src && src->ss_family != dest->ss_family)
+		return NULL;
+
+	if (dest->ss_family == AF_INET) {
+		struct rtable *rt;
+		struct flowi4 fl4 = {};
+
+		fl4.daddr = ((struct sockaddr_in *)dest)->sin_addr.s_addr;
+		if (src)
+			fl4.saddr = ((struct sockaddr_in *)src)->sin_addr.s_addr;
+
+		rt = ip_route_output_key(net, &fl4);
+		if (IS_ERR(rt))
+			return NULL;
+
+		dev = dst_dev(&rt->dst);
+		/*
+		 * Get reference to netdev as ip_rt_put() will release netdev
+		 * reference.
+		 */
+		if (dev)
+			netdev_hold(dev, tracker, gfp);
+
+		ip_rt_put(rt);
+
+#if IS_ENABLED(CONFIG_IPV6)
+	} else if (dest->ss_family == AF_INET6) {
+		struct dst_entry *dst;
+		struct flowi6 fl6 = {};
+
+		fl6.daddr = ((struct sockaddr_in6 *)dest)->sin6_addr;
+		if (src)
+			fl6.saddr = ((struct sockaddr_in6 *)src)->sin6_addr;
+
+		dst = ip6_route_output(net, NULL, &fl6);
+		if (dst->error) {
+			dst_release(dst);
+			return NULL;
+		}
+
+		dev = dst_dev(dst);
+		/*
+		 * Get reference to netdev as dst_release() will
+		 * release the netdev reference.
+		 */
+		if (dev)
+			netdev_hold(dev, tracker, gfp);
+
+		dst_release(dst);
+#endif
+	}
+
+	return dev;
+}
+EXPORT_SYMBOL(netdev_get_by_addr);
+
 /**
  *	__dev_get_by_index - find a device by its ifindex
  *	@net: the applicable net namespace
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2 1/4] net: add helper for device lookup by destination address
  2026-07-27 15:16 ` [PATCH v2 1/4] net: add helper for device lookup by destination address Nilay Shroff
@ 2026-07-30 23:15   ` Jakub Kicinski
  2026-07-31  6:10     ` Nilay Shroff
  0 siblings, 1 reply; 3+ messages in thread
From: Jakub Kicinski @ 2026-07-30 23:15 UTC (permalink / raw)
  To: Nilay Shroff
  Cc: linux-nvme, linux-kernel, kbusch, hch, hare, sagi, chaitanyak,
	gjoyce, David S. Miller, Eric Dumazet, Paolo Abeni, Simon Horman,
	netdev

On Mon, 27 Jul 2026 20:46:44 +0530 Nilay Shroff wrote:
> Add netdev_get_by_addr(), a helper that looks up the routing table to
> retrieve the network device associated with a destination address. The
> helper also supports netdev reference tracking.

This does not explain why you're doing this.
Please CC the whole series to netdev or write better commit messages.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2 1/4] net: add helper for device lookup by destination address
  2026-07-30 23:15   ` Jakub Kicinski
@ 2026-07-31  6:10     ` Nilay Shroff
  0 siblings, 0 replies; 3+ messages in thread
From: Nilay Shroff @ 2026-07-31  6:10 UTC (permalink / raw)
  To: Jakub Kicinski
  Cc: linux-nvme, linux-kernel, kbusch, hch, hare, sagi, chaitanyak,
	gjoyce, David S. Miller, Eric Dumazet, Paolo Abeni, Simon Horman,
	netdev

On 7/31/26 4:45 AM, Jakub Kicinski wrote:
> On Mon, 27 Jul 2026 20:46:44 +0530 Nilay Shroff wrote:
>> Add netdev_get_by_addr(), a helper that looks up the routing table to
>> retrieve the network device associated with a destination address. The
>> helper also supports netdev reference tracking.
> 
> This does not explain why you're doing this.
> Please CC the whole series to netdev or write better commit messages.

Yeah sure my bad, I would resend the series including all netdev folks in Cc.

Thanks,
--Nilay

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-07-31  6:11 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20260727151652.3660476-1-nilay@linux.ibm.com>
2026-07-27 15:16 ` [PATCH v2 1/4] net: add helper for device lookup by destination address Nilay Shroff
2026-07-30 23:15   ` Jakub Kicinski
2026-07-31  6:10     ` Nilay Shroff

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox