git: e3b352597e4b - stable/13 - irdma(4): use related vnet for searching netdev

From: Eric Joyner <erj_at_FreeBSD.org>
Date: Fri, 22 Sep 2023 21:57:29 UTC
The branch stable/13 has been updated by erj:

URL: https://cgit.FreeBSD.org/src/commit/?id=e3b352597e4bcb4ec94cb95db23c041da7f7e61c

commit e3b352597e4bcb4ec94cb95db23c041da7f7e61c
Author:     Bartosz Sobczak <bartosz.sobczak@intel.com>
AuthorDate: 2023-08-21 11:56:57 +0000
Commit:     Eric Joyner <erj@FreeBSD.org>
CommitDate: 2023-09-22 21:55:18 +0000

    irdma(4): use related vnet for searching netdev
    
    It was found through testing that when ULP uses individual vnet, the
    search for the correct vlan_id may failing because of no proper
    interface with given address.
    
    The solution is to use vnet associated to the connection whenever
    possible.
    
    Signed-off-by: Bartosz Sobczak <bartosz.sobczak@intel.com>
    Signed-off-by: Eric Joyner <erj@FreeBSD.org>
    
    Reviewed by:    erj@
    Sponsored by:   Intel Corporation
    Differential Revision:  https://reviews.freebsd.org/D41592
    
    (cherry picked from commit bc3a013ccd23c50bb48f17211ffc36fe85d11e18)
---
 sys/dev/irdma/fbsd_kcompat.c |  6 ++----
 sys/dev/irdma/fbsd_kcompat.h | 15 +++++++++++++++
 sys/dev/irdma/irdma_cm.c     | 35 ++++++++++++++++++++++++-----------
 sys/dev/irdma/irdma_main.h   |  5 +++--
 sys/dev/irdma/irdma_verbs.c  |  4 ++--
 5 files changed, 46 insertions(+), 19 deletions(-)

diff --git a/sys/dev/irdma/fbsd_kcompat.c b/sys/dev/irdma/fbsd_kcompat.c
index 5c11f7a35544..19c3943b5151 100644
--- a/sys/dev/irdma/fbsd_kcompat.c
+++ b/sys/dev/irdma/fbsd_kcompat.c
@@ -391,8 +391,7 @@ irdma_get_dst_mac(struct irdma_cm_node *cm_node, struct sockaddr *dst_sin, u8 *d
 {
 	struct ifnet *netdev = cm_node->iwdev->netdev;
 #ifdef VIMAGE
-	struct rdma_cm_id *rdma_id = (struct rdma_cm_id *)cm_node->cm_id->context;
-	struct vnet *vnet = rdma_id->route.addr.dev_addr.net;
+	struct vnet *vnet = irdma_cmid_to_vnet(cm_node->cm_id);
 #endif
 	struct ifnet *ifp;
 	struct llentry *lle;
@@ -490,8 +489,7 @@ irdma_resolve_neigh_lpb_chk(struct irdma_device *iwdev, struct irdma_cm_node *cm
 			    struct irdma_cm_info *cm_info)
 {
 #ifdef VIMAGE
-	struct rdma_cm_id *rdma_id = (struct rdma_cm_id *)cm_node->cm_id->context;
-	struct vnet *vnet = rdma_id->route.addr.dev_addr.net;
+	struct vnet *vnet = irdma_cmid_to_vnet(cm_node->cm_id);
 #endif
 	int arpindex;
 	int oldarpindex;
diff --git a/sys/dev/irdma/fbsd_kcompat.h b/sys/dev/irdma/fbsd_kcompat.h
index 630e49068a74..5e656cd9be9f 100644
--- a/sys/dev/irdma/fbsd_kcompat.h
+++ b/sys/dev/irdma/fbsd_kcompat.h
@@ -122,6 +122,7 @@ static inline int cq_validate_flags(u32 flags, u8 hw_rev)
 
 	return flags & ~IB_UVERBS_CQ_FLAGS_TIMESTAMP_COMPLETION ? -EOPNOTSUPP : 0;
 }
+
 static inline u64 *irdma_next_pbl_addr(u64 *pbl, struct irdma_pble_info **pinfo,
 				       u32 *idx)
 {
@@ -133,6 +134,20 @@ static inline u64 *irdma_next_pbl_addr(u64 *pbl, struct irdma_pble_info **pinfo,
 
 	return (*pinfo)->addr;
 }
+
+static inline struct vnet *
+irdma_cmid_to_vnet(struct iw_cm_id *cm_id)
+{
+	struct rdma_cm_id *rdma_id;
+
+	if (!cm_id)
+		return &init_net;
+
+	rdma_id = (struct rdma_cm_id *)cm_id->context;
+
+	return rdma_id->route.addr.dev_addr.net;
+}
+
 struct ib_cq *irdma_create_cq(struct ib_device *ibdev,
 			      const struct ib_cq_init_attr *attr,
 			      struct ib_ucontext *context,
diff --git a/sys/dev/irdma/irdma_cm.c b/sys/dev/irdma/irdma_cm.c
index cb508bf52cb8..d34da57cc84d 100644
--- a/sys/dev/irdma/irdma_cm.c
+++ b/sys/dev/irdma/irdma_cm.c
@@ -1625,10 +1625,13 @@ static u8 irdma_iw_get_vlan_prio(u32 *loc_addr, u8 prio, bool ipv4)
  * vlan id and mac for that address.
  */
 if_t
-irdma_netdev_vlan_ipv6(u32 *addr, u16 *vlan_id, u8 *mac)
+irdma_netdev_vlan_ipv6(struct iw_cm_id *cm_id, u32 *addr, u16 *vlan_id, u8 *mac)
 {
 	if_t ip_dev = NULL;
 	struct in6_addr laddr6;
+#ifdef VIMAGE
+	struct vnet *vnet = irdma_cmid_to_vnet(cm_id);
+#endif
 	struct ifaddr *ifa;
 	u16 scope_id = 0;
 
@@ -1642,7 +1645,11 @@ irdma_netdev_vlan_ipv6(u32 *addr, u16 *vlan_id, u8 *mac)
 	    IN6_IS_ADDR_MC_INTFACELOCAL(&laddr6))
 		scope_id = ntohs(laddr6.__u6_addr.__u6_addr16[1]);
 
+#ifdef VIMAGE
+	ip_dev = ip6_ifp_find(vnet, laddr6, scope_id);
+#else
 	ip_dev = ip6_ifp_find(&init_net, laddr6, scope_id);
+#endif
 	if (ip_dev) {
 		if (vlan_id)
 			*vlan_id = rdma_vlan_dev_vlan_id(ip_dev);
@@ -1659,12 +1666,19 @@ irdma_netdev_vlan_ipv6(u32 *addr, u16 *vlan_id, u8 *mac)
  * @addr: local IPv4 address
  */
 u16
-irdma_get_vlan_ipv4(u32 *addr)
+irdma_get_vlan_ipv4(struct iw_cm_id *cm_id, u32 *addr)
 {
 	if_t netdev;
+#ifdef VIMAGE
+	struct vnet *vnet = irdma_cmid_to_vnet(cm_id);
+#endif
 	u16 vlan_id = 0xFFFF;
 
+#ifdef VIMAGE
+	netdev = ip_ifp_find(vnet, htonl(addr[0]));
+#else
 	netdev = ip_ifp_find(&init_net, htonl(addr[0]));
+#endif
 	if (netdev) {
 		vlan_id = rdma_vlan_dev_vlan_id(netdev);
 		dev_put(netdev);
@@ -2060,8 +2074,7 @@ irdma_cm_create_ah(struct irdma_cm_node *cm_node, bool wait)
 	struct irdma_ah_info ah_info = {0};
 	struct irdma_device *iwdev = cm_node->iwdev;
 #ifdef VIMAGE
-	struct rdma_cm_id *rdma_id = (struct rdma_cm_id *)cm_node->cm_id->context;
-	struct vnet *vnet = rdma_id->route.addr.dev_addr.net;
+	struct vnet *vnet = irdma_cmid_to_vnet(cm_node->cm_id);
 #endif
 
 	ether_addr_copy(ah_info.mac_addr, if_getlladdr(iwdev->netdev));
@@ -3521,11 +3534,11 @@ irdma_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
 
 	if (((struct sockaddr_in *)&cm_id->local_addr)->sin_family == AF_INET) {
 		cm_node->ipv4 = true;
-		cm_node->vlan_id = irdma_get_vlan_ipv4(cm_node->loc_addr);
+		cm_node->vlan_id = irdma_get_vlan_ipv4(cm_id, cm_node->loc_addr);
 	} else {
 		cm_node->ipv4 = false;
-		irdma_netdev_vlan_ipv6(cm_node->loc_addr, &cm_node->vlan_id,
-				       NULL);
+		irdma_netdev_vlan_ipv6(cm_id, cm_node->loc_addr,
+				       &cm_node->vlan_id, NULL);
 	}
 	irdma_debug(&iwdev->rf->sc_dev, IRDMA_DEBUG_CM, "Accept vlan_id=%d\n",
 		    cm_node->vlan_id);
@@ -3720,7 +3733,7 @@ irdma_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
 		cm_info.rem_addr[0] = ntohl(raddr->sin_addr.s_addr);
 		cm_info.loc_port = ntohs(laddr->sin_port);
 		cm_info.rem_port = ntohs(raddr->sin_port);
-		cm_info.vlan_id = irdma_get_vlan_ipv4(cm_info.loc_addr);
+		cm_info.vlan_id = irdma_get_vlan_ipv4(cm_id, cm_info.loc_addr);
 	} else {
 		if (iwdev->vsi.mtu < IRDMA_MIN_MTU_IPV6)
 			return -EINVAL;
@@ -3732,7 +3745,7 @@ irdma_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *conn_param)
 				    raddr6->sin6_addr.__u6_addr.__u6_addr32);
 		cm_info.loc_port = ntohs(laddr6->sin6_port);
 		cm_info.rem_port = ntohs(raddr6->sin6_port);
-		irdma_netdev_vlan_ipv6(cm_info.loc_addr, &cm_info.vlan_id, NULL);
+		irdma_netdev_vlan_ipv6(cm_id, cm_info.loc_addr, &cm_info.vlan_id, NULL);
 	}
 	cm_info.cm_id = cm_id;
 	cm_info.qh_qpid = iwdev->vsi.ilq->qp_id;
@@ -3847,7 +3860,7 @@ irdma_create_listen(struct iw_cm_id *cm_id, int backlog)
 		cm_info.loc_port = ntohs(laddr->sin_port);
 
 		if (laddr->sin_addr.s_addr != htonl(INADDR_ANY)) {
-			cm_info.vlan_id = irdma_get_vlan_ipv4(cm_info.loc_addr);
+			cm_info.vlan_id = irdma_get_vlan_ipv4(cm_id, cm_info.loc_addr);
 		} else {
 			cm_info.vlan_id = 0xFFFF;
 			wildcard = true;
@@ -3861,7 +3874,7 @@ irdma_create_listen(struct iw_cm_id *cm_id, int backlog)
 				    laddr6->sin6_addr.__u6_addr.__u6_addr32);
 		cm_info.loc_port = ntohs(laddr6->sin6_port);
 		if (!IN6_IS_ADDR_UNSPECIFIED(&laddr6->sin6_addr)) {
-			irdma_netdev_vlan_ipv6(cm_info.loc_addr,
+			irdma_netdev_vlan_ipv6(cm_id, cm_info.loc_addr,
 					       &cm_info.vlan_id, NULL);
 		} else {
 			cm_info.vlan_id = 0xFFFF;
diff --git a/sys/dev/irdma/irdma_main.h b/sys/dev/irdma/irdma_main.h
index 8e304cae551f..3d6dd4e3f9db 100644
--- a/sys/dev/irdma/irdma_main.h
+++ b/sys/dev/irdma/irdma_main.h
@@ -572,8 +572,9 @@ void irdma_gen_ae(struct irdma_pci_f *rf, struct irdma_sc_qp *qp,
 		  struct irdma_gen_ae_info *info, bool wait);
 void irdma_copy_ip_ntohl(u32 *dst, __be32 *src);
 void irdma_copy_ip_htonl(__be32 *dst, u32 *src);
-u16 irdma_get_vlan_ipv4(u32 *addr);
-if_t irdma_netdev_vlan_ipv6(u32 *addr, u16 *vlan_id, u8 *mac);
+u16 irdma_get_vlan_ipv4(struct iw_cm_id *cm_id, u32 *addr);
+if_t irdma_netdev_vlan_ipv6(struct iw_cm_id *cm_id, u32 *addr, u16 *vlan_id,
+			    u8 *mac);
 struct ib_mr *irdma_reg_phys_mr(struct ib_pd *ib_pd, u64 addr, u64 size,
 				int acc, u64 *iova_start);
 int irdma_upload_qp_context(struct irdma_qp *iwqp, bool freeze, bool raw);
diff --git a/sys/dev/irdma/irdma_verbs.c b/sys/dev/irdma/irdma_verbs.c
index 288b075ab79f..c0726d25b8ec 100644
--- a/sys/dev/irdma/irdma_verbs.c
+++ b/sys/dev/irdma/irdma_verbs.c
@@ -3217,7 +3217,7 @@ irdma_attach_mcast(struct ib_qp *ibqp, union ib_gid *ibgid, u16 lid)
 	if (!ipv6_addr_v4mapped((struct in6_addr *)ibgid)) {
 		irdma_copy_ip_ntohl(ip_addr,
 				    sgid_addr.saddr_in6.sin6_addr.__u6_addr.__u6_addr32);
-		irdma_netdev_vlan_ipv6(ip_addr, &vlan_id, NULL);
+		irdma_netdev_vlan_ipv6(iwqp->cm_id, ip_addr, &vlan_id, NULL);
 		ipv4 = false;
 		irdma_debug(&iwdev->rf->sc_dev, IRDMA_DEBUG_VERBS,
 			    "qp_id=%d, IP6address=%x:%x:%x:%x\n", ibqp->qp_num,
@@ -3226,7 +3226,7 @@ irdma_attach_mcast(struct ib_qp *ibqp, union ib_gid *ibgid, u16 lid)
 	} else {
 		ip_addr[0] = ntohl(sgid_addr.saddr_in.sin_addr.s_addr);
 		ipv4 = true;
-		vlan_id = irdma_get_vlan_ipv4(ip_addr);
+		vlan_id = irdma_get_vlan_ipv4(iwqp->cm_id, ip_addr);
 		irdma_mcast_mac_v4(ip_addr, dmac);
 		irdma_debug(&iwdev->rf->sc_dev, IRDMA_DEBUG_VERBS,
 			    "qp_id=%d, IP4address=%x, MAC=%x:%x:%x:%x:%x:%x\n",