public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
* [PATCH v2] netlink: Adjust interface index inside copied nexthop objects too
@ 2024-04-04 16:37 Stefano Brivio
  2024-04-05  4:15 ` David Gibson
  0 siblings, 1 reply; 2+ messages in thread
From: Stefano Brivio @ 2024-04-04 16:37 UTC (permalink / raw)
  To: passt-dev

As pasta duplicates host routes into the target namespaces, interface
indices might not match, so we go through RTA_OIF attributes and fix
them up to match the identifier in the namespace.

But RTA_OIF is not the ony attribute specifying interfaces for routes:
multipath routes use RTA_MULTIPATH attributes with nexthop objects,
which contain in turn interface indices. Fix them up as well.

If we don't, and we have at least two host interfaces, and the host
interface we use as template isn't the first one (hence the
mismatching indices), we'll fail to insert multipath routes with
nexthop objects, and ultimately refuse to start as the kernel
unexpectedly gives us ENODEV.

Link: https://github.com/containers/podman/issues/22192
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
---
v2: s/interfaces/interface/ in comment

 netlink.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/netlink.c b/netlink.c
index 9b3dba2..59e9e33 100644
--- a/netlink.c
+++ b/netlink.c
@@ -546,12 +546,19 @@ int nl_route_dup(int s_src, unsigned int ifi_src,
 
 		for (rta = RTM_RTA(rtm), na = RTM_PAYLOAD(nh); RTA_OK(rta, na);
 		     rta = RTA_NEXT(rta, na)) {
+			/* RTA_OIF and RTA_MULTIPATH attributes carry the
+			 * identifier of a host interface. Change them to match
+			 * the corresponding identifier in the target namespace.
+			 */
 			if (rta->rta_type == RTA_OIF) {
-				/* The host obviously list's the host interface
-				 * id here, we need to change it to the
-				 * namespace's interface id
-				 */
 				*(unsigned int *)RTA_DATA(rta) = ifi_dst;
+			} else if (rta->rta_type == RTA_MULTIPATH) {
+				struct rtnexthop *rtnh;
+
+				for (rtnh = (struct rtnexthop *)RTA_DATA(rta);
+				     RTNH_OK(rtnh, RTA_PAYLOAD(rta));
+				     rtnh = RTNH_NEXT(rtnh))
+					rtnh->rtnh_ifindex = ifi_dst;
 			} else if (rta->rta_type == RTA_PREFSRC) {
 				/* Host routes might include a preferred source
 				 * address, which must be one of the host's
-- 
@@ -546,12 +546,19 @@ int nl_route_dup(int s_src, unsigned int ifi_src,
 
 		for (rta = RTM_RTA(rtm), na = RTM_PAYLOAD(nh); RTA_OK(rta, na);
 		     rta = RTA_NEXT(rta, na)) {
+			/* RTA_OIF and RTA_MULTIPATH attributes carry the
+			 * identifier of a host interface. Change them to match
+			 * the corresponding identifier in the target namespace.
+			 */
 			if (rta->rta_type == RTA_OIF) {
-				/* The host obviously list's the host interface
-				 * id here, we need to change it to the
-				 * namespace's interface id
-				 */
 				*(unsigned int *)RTA_DATA(rta) = ifi_dst;
+			} else if (rta->rta_type == RTA_MULTIPATH) {
+				struct rtnexthop *rtnh;
+
+				for (rtnh = (struct rtnexthop *)RTA_DATA(rta);
+				     RTNH_OK(rtnh, RTA_PAYLOAD(rta));
+				     rtnh = RTNH_NEXT(rtnh))
+					rtnh->rtnh_ifindex = ifi_dst;
 			} else if (rta->rta_type == RTA_PREFSRC) {
 				/* Host routes might include a preferred source
 				 * address, which must be one of the host's
-- 
2.43.0


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

* Re: [PATCH v2] netlink: Adjust interface index inside copied nexthop objects too
  2024-04-04 16:37 [PATCH v2] netlink: Adjust interface index inside copied nexthop objects too Stefano Brivio
@ 2024-04-05  4:15 ` David Gibson
  0 siblings, 0 replies; 2+ messages in thread
From: David Gibson @ 2024-04-05  4:15 UTC (permalink / raw)
  To: Stefano Brivio; +Cc: passt-dev

[-- Attachment #1: Type: text/plain, Size: 2457 bytes --]

On Thu, Apr 04, 2024 at 06:37:55PM +0200, Stefano Brivio wrote:
> As pasta duplicates host routes into the target namespaces, interface
> indices might not match, so we go through RTA_OIF attributes and fix
> them up to match the identifier in the namespace.
> 
> But RTA_OIF is not the ony attribute specifying interfaces for routes:
> multipath routes use RTA_MULTIPATH attributes with nexthop objects,
> which contain in turn interface indices. Fix them up as well.
> 
> If we don't, and we have at least two host interfaces, and the host
> interface we use as template isn't the first one (hence the
> mismatching indices), we'll fail to insert multipath routes with
> nexthop objects, and ultimately refuse to start as the kernel
> unexpectedly gives us ENODEV.
> 
> Link: https://github.com/containers/podman/issues/22192
> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
> ---
> v2: s/interfaces/interface/ in comment

Reviewed-by: David Gibson <david@gibson.dropbear.id.au>

> 
>  netlink.c | 15 +++++++++++----
>  1 file changed, 11 insertions(+), 4 deletions(-)
> 
> diff --git a/netlink.c b/netlink.c
> index 9b3dba2..59e9e33 100644
> --- a/netlink.c
> +++ b/netlink.c
> @@ -546,12 +546,19 @@ int nl_route_dup(int s_src, unsigned int ifi_src,
>  
>  		for (rta = RTM_RTA(rtm), na = RTM_PAYLOAD(nh); RTA_OK(rta, na);
>  		     rta = RTA_NEXT(rta, na)) {
> +			/* RTA_OIF and RTA_MULTIPATH attributes carry the
> +			 * identifier of a host interface. Change them to match
> +			 * the corresponding identifier in the target namespace.
> +			 */
>  			if (rta->rta_type == RTA_OIF) {
> -				/* The host obviously list's the host interface
> -				 * id here, we need to change it to the
> -				 * namespace's interface id
> -				 */
>  				*(unsigned int *)RTA_DATA(rta) = ifi_dst;
> +			} else if (rta->rta_type == RTA_MULTIPATH) {
> +				struct rtnexthop *rtnh;
> +
> +				for (rtnh = (struct rtnexthop *)RTA_DATA(rta);
> +				     RTNH_OK(rtnh, RTA_PAYLOAD(rta));
> +				     rtnh = RTNH_NEXT(rtnh))
> +					rtnh->rtnh_ifindex = ifi_dst;
>  			} else if (rta->rta_type == RTA_PREFSRC) {
>  				/* Host routes might include a preferred source
>  				 * address, which must be one of the host's

-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

end of thread, other threads:[~2024-04-05  4:59 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-04 16:37 [PATCH v2] netlink: Adjust interface index inside copied nexthop objects too Stefano Brivio
2024-04-05  4:15 ` David Gibson

Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).