platform: add scope parameter to NMPlatformIP4Route

Cache the scope as part of the NMPlatformIP4Route and
no longer read it from libnl object when needed. Later
there will be no more libnl objects around, and we need
to scope when deleting an IPv4 route.
This commit is contained in:
Thomas Haller
2015-05-03 10:47:41 +02:00
parent 4c49d78f49
commit 619f660a3e
6 changed files with 48 additions and 5 deletions

View File

@@ -24,6 +24,7 @@
#include <unistd.h>
#include <netinet/icmp6.h>
#include <netinet/in.h>
#include <linux/rtnetlink.h>
#include "gsystem-local-alloc.h"
#include "NetworkManagerUtils.h"
@@ -1196,6 +1197,9 @@ ip4_route_add (NMPlatform *platform, int ifindex, NMIPConfigSource source,
NMFakePlatformPrivate *priv = NM_FAKE_PLATFORM_GET_PRIVATE (platform);
NMPlatformIP4Route route;
guint i;
guint8 scope;
scope = gateway == 0 ? RT_SCOPE_LINK : RT_SCOPE_UNIVERSE;
memset (&route, 0, sizeof (route));
route.source = NM_IP_CONFIG_SOURCE_KERNEL;
@@ -1206,6 +1210,7 @@ ip4_route_add (NMPlatform *platform, int ifindex, NMIPConfigSource source,
route.gateway = gateway;
route.metric = metric;
route.mss = mss;
route.scope_inv = nm_platform_route_scope_inv (scope);
if (gateway) {
for (i = 0; i < priv->ip4_routes->len; i++) {

View File

@@ -1305,6 +1305,7 @@ init_ip4_route (NMPlatformIP4Route *route, struct rtnl_route *rtnlroute)
route->metric = rtnl_route_get_priority (rtnlroute);
rtnl_route_get_metric (rtnlroute, RTAX_ADVMSS, &route->mss);
route->source = rtprot_to_source (rtnl_route_get_protocol (rtnlroute));
route->scope_inv = nm_platform_route_scope_inv (rtnl_route_get_scope (rtnlroute));
return TRUE;
}
@@ -3684,7 +3685,7 @@ build_rtnl_addr (NMPlatform *platform,
/* Tighten scope (IPv4 only) */
if (family == AF_INET && ip4_is_link_local (addr))
rtnl_addr_set_scope (rtnladdr, rtnl_str2scope ("link"));
rtnl_addr_set_scope (rtnladdr, RT_SCOPE_LINK);
/* IPv4 Broadcast address */
if (family == AF_INET) {

View File

@@ -27,6 +27,7 @@
#include <arpa/inet.h>
#include <string.h>
#include <netlink/route/addr.h>
#include <netlink/route/rtnl.h>
#include "gsystem-local-alloc.h"
#include "NetworkManagerUtils.h"
@@ -2646,6 +2647,7 @@ nm_platform_ip4_route_to_string (const NMPlatformIP4Route *route)
{
char s_network[INET_ADDRSTRLEN], s_gateway[INET_ADDRSTRLEN];
char str_dev[TO_STRING_DEV_BUF_SIZE];
char str_scope[30];
g_return_val_if_fail (route, "(unknown)");
@@ -2654,11 +2656,13 @@ nm_platform_ip4_route_to_string (const NMPlatformIP4Route *route)
_to_string_dev (NULL, route->ifindex, str_dev, sizeof (str_dev));
g_snprintf (to_string_buffer, sizeof (to_string_buffer), "%s/%d via %s%s metric %"G_GUINT32_FORMAT" mss %"G_GUINT32_FORMAT" src %s",
g_snprintf (to_string_buffer, sizeof (to_string_buffer), "%s/%d via %s%s metric %"G_GUINT32_FORMAT" mss %"G_GUINT32_FORMAT" src %s%s%s",
s_network, route->plen, s_gateway,
str_dev,
route->metric, route->mss,
source_to_string (route->source));
source_to_string (route->source),
route->scope_inv ? " scope " : "",
route->scope_inv ? (rtnl_scope2str (nm_platform_route_scope_inv (route->scope_inv), str_scope, sizeof (str_scope))) : "");
return to_string_buffer;
}
@@ -2812,6 +2816,7 @@ nm_platform_ip4_route_cmp (const NMPlatformIP4Route *a, const NMPlatformIP4Route
_CMP_FIELD (a, b, gateway);
_CMP_FIELD (a, b, metric);
_CMP_FIELD (a, b, mss);
_CMP_FIELD (a, b, scope_inv);
return 0;
}

View File

@@ -237,6 +237,10 @@ struct _NMPlatformIP4Route {
__NMPlatformIPRoute_COMMON;
in_addr_t network;
in_addr_t gateway;
/* The bitwise inverse of the route scope. It is inverted so that the
* default value (RT_SCOPE_NOWHERE) is nul. */
guint8 scope_inv;
};
G_STATIC_ASSERT (G_STRUCT_OFFSET (NMPlatformIPRoute, network_ptr) == G_STRUCT_OFFSET (NMPlatformIP4Route, network));
@@ -529,6 +533,22 @@ NMPlatform *nm_platform_try_get (void);
/******************************************************************/
/**
* nm_platform_route_scope_inv:
* @scope: the route scope, either its original value, or its inverse.
*
* This function is useful, because the constants such as RT_SCOPE_NOWHERE
* are 'int', so ~scope also gives an 'int'. This function gets the type
* casts to guint8 right.
*
* Returns: the bitwise inverse of the route scope.
* */
static inline guint8
nm_platform_route_scope_inv (guint8 scope)
{
return (guint8) ~scope;
}
const char *nm_link_type_to_string (NMLinkType link_type);
void nm_platform_set_error (NMPlatform *self, NMPlatformError error);

View File

@@ -1,5 +1,7 @@
#include "config.h"
#include <linux/rtnetlink.h>
#include "test-common.h"
#include "nm-test-utils.h"
#include "NetworkManagerUtils.h"
@@ -177,6 +179,7 @@ test_ip4_route (void)
rts[0].gateway = INADDR_ANY;
rts[0].metric = metric;
rts[0].mss = mss;
rts[0].scope_inv = nm_platform_route_scope_inv (RT_SCOPE_LINK);
rts[1].source = NM_IP_CONFIG_SOURCE_USER;
rts[1].network = network;
rts[1].plen = plen;
@@ -184,6 +187,7 @@ test_ip4_route (void)
rts[1].gateway = gateway;
rts[1].metric = metric;
rts[1].mss = mss;
rts[1].scope_inv = nm_platform_route_scope_inv (RT_SCOPE_UNIVERSE);
rts[2].source = NM_IP_CONFIG_SOURCE_USER;
rts[2].network = 0;
rts[2].plen = 0;
@@ -191,8 +195,8 @@ test_ip4_route (void)
rts[2].gateway = gateway;
rts[2].metric = metric;
rts[2].mss = mss;
rts[2].scope_inv = nm_platform_route_scope_inv (RT_SCOPE_UNIVERSE);
g_assert_cmpint (routes->len, ==, 3);
g_assert (!memcmp (routes->data, rts, sizeof (rts)));
nmtst_platform_ip4_routes_equal ((NMPlatformIP4Route *) routes->data, rts, routes->len, TRUE);
g_array_unref (routes);
@@ -288,7 +292,6 @@ test_ip6_route (void)
rts[2].metric = nm_utils_ip6_route_metric_normalize (metric);
rts[2].mss = mss;
g_assert_cmpint (routes->len, ==, 3);
g_assert (!memcmp (routes->data, rts, sizeof (rts)));
nmtst_platform_ip6_routes_equal ((NMPlatformIP6Route *) routes->data, rts, routes->len, TRUE);
g_array_unref (routes);

View File

@@ -22,6 +22,7 @@
#include <glib.h>
#include <arpa/inet.h>
#include <linux/rtnetlink.h>
#include "test-common.h"
@@ -168,6 +169,7 @@ test_ip4 (test_fixture *fixture, gconstpointer user_data)
.gateway = INADDR_ANY,
.metric = 20,
.mss = 1000,
.scope_inv = nm_platform_route_scope_inv (RT_SCOPE_LINK),
},
{
.source = NM_IP_CONFIG_SOURCE_USER,
@@ -177,6 +179,7 @@ test_ip4 (test_fixture *fixture, gconstpointer user_data)
.gateway = nmtst_inet4_from_string ("6.6.6.1"),
.metric = 21021,
.mss = 0,
.scope_inv = nm_platform_route_scope_inv (RT_SCOPE_UNIVERSE),
},
{
.source = NM_IP_CONFIG_SOURCE_USER,
@@ -186,6 +189,7 @@ test_ip4 (test_fixture *fixture, gconstpointer user_data)
.gateway = INADDR_ANY,
.metric = 22,
.mss = 0,
.scope_inv = nm_platform_route_scope_inv (RT_SCOPE_LINK),
},
};
@@ -198,6 +202,7 @@ test_ip4 (test_fixture *fixture, gconstpointer user_data)
.gateway = INADDR_ANY,
.metric = 20,
.mss = 0,
.scope_inv = nm_platform_route_scope_inv (RT_SCOPE_LINK),
},
{
.source = NM_IP_CONFIG_SOURCE_USER,
@@ -207,6 +212,7 @@ test_ip4 (test_fixture *fixture, gconstpointer user_data)
.gateway = INADDR_ANY,
.metric = 21,
.mss = 0,
.scope_inv = nm_platform_route_scope_inv (RT_SCOPE_LINK),
},
{
.source = NM_IP_CONFIG_SOURCE_USER,
@@ -216,6 +222,7 @@ test_ip4 (test_fixture *fixture, gconstpointer user_data)
.gateway = INADDR_ANY,
.metric = 22,
.mss = 0,
.scope_inv = nm_platform_route_scope_inv (RT_SCOPE_LINK),
},
};
@@ -228,6 +235,7 @@ test_ip4 (test_fixture *fixture, gconstpointer user_data)
.gateway = INADDR_ANY,
.metric = 22,
.mss = 0,
.scope_inv = nm_platform_route_scope_inv (RT_SCOPE_LINK),
},
{
.source = NM_IP_CONFIG_SOURCE_USER,
@@ -237,6 +245,7 @@ test_ip4 (test_fixture *fixture, gconstpointer user_data)
.gateway = INADDR_ANY,
.metric = 20,
.mss = 0,
.scope_inv = nm_platform_route_scope_inv (RT_SCOPE_LINK),
},
};