platform: don't check for route existence

This is the same we already did for nm-platform addresses in commit
68c3e1153c. It will help to avoid various
issues and is also a step towards support for route lifetimes.
This commit is contained in:
Pavel Šimerda
2013-08-01 00:13:10 +02:00
parent 42b4323902
commit 5dd15bd459
3 changed files with 60 additions and 38 deletions

View File

@@ -941,6 +941,7 @@ ip4_route_add (NMPlatform *platform, int ifindex, in_addr_t network, int plen,
{
NMFakePlatformPrivate *priv = NM_FAKE_PLATFORM_GET_PRIVATE (platform);
NMPlatformIP4Route route;
guint i;
memset (&route, 0, sizeof (route));
route.ifindex = ifindex;
@@ -950,8 +951,22 @@ ip4_route_add (NMPlatform *platform, int ifindex, in_addr_t network, int plen,
route.metric = metric;
route.mss = mss;
g_array_append_val (priv->ip4_routes, route);
for (i = 0; i < priv->ip4_routes->len; i++) {
NMPlatformIP4Route *item = &g_array_index (priv->ip4_routes, NMPlatformIP4Route, i);
if (item->ifindex != route.ifindex)
continue;
if (item->network != route.network)
continue;
if (item->plen != route.plen)
continue;
memcpy (item, &route, sizeof (route));
g_signal_emit_by_name (platform, NM_PLATFORM_IP4_ROUTE_CHANGED, ifindex, &route);
return TRUE;
}
g_array_append_val (priv->ip4_routes, route);
g_signal_emit_by_name (platform, NM_PLATFORM_IP4_ROUTE_ADDED, ifindex, &route);
return TRUE;
@@ -963,6 +978,7 @@ ip6_route_add (NMPlatform *platform, int ifindex, struct in6_addr network, int p
{
NMFakePlatformPrivate *priv = NM_FAKE_PLATFORM_GET_PRIVATE (platform);
NMPlatformIP6Route route;
guint i;
memset (&route, 0, sizeof (route));
route.ifindex = ifindex;
@@ -972,8 +988,22 @@ ip6_route_add (NMPlatform *platform, int ifindex, struct in6_addr network, int p
route.metric = metric;
route.mss = mss;
g_array_append_val (priv->ip6_routes, route);
for (i = 0; i < priv->ip6_routes->len; i++) {
NMPlatformIP6Route *item = &g_array_index (priv->ip6_routes, NMPlatformIP6Route, i);
if (item->ifindex != route.ifindex)
continue;
if (!IN6_ARE_ADDR_EQUAL (&item->network, &route.network))
continue;
if (item->plen != route.plen)
continue;
memcpy (item, &route, sizeof (route));
g_signal_emit_by_name (platform, NM_PLATFORM_IP6_ROUTE_CHANGED, ifindex, &route);
return TRUE;
}
g_array_append_val (priv->ip6_routes, route);
g_signal_emit_by_name (platform, NM_PLATFORM_IP6_ROUTE_ADDED, ifindex, &route);
return TRUE;

View File

@@ -1362,12 +1362,6 @@ nm_platform_ip4_route_add (int ifindex,
if (!metric)
metric = 1024;
if (nm_platform_ip4_route_exists (ifindex, network, plen, metric)) {
debug ("route already exists");
platform->error = NM_PLATFORM_ERROR_EXISTS;
return FALSE;
}
return klass->ip4_route_add (platform, ifindex, network, plen, gateway, metric, mss);
}
@@ -1384,12 +1378,6 @@ nm_platform_ip6_route_add (int ifindex,
if (!metric)
metric = 1024;
if (nm_platform_ip6_route_exists (ifindex, network, plen, metric)) {
debug ("route already exists");
platform->error = NM_PLATFORM_ERROR_EXISTS;
return FALSE;
}
return klass->ip6_route_add (platform, ifindex, network, plen, gateway, metric, mss);
}
@@ -1517,12 +1505,10 @@ nm_platform_ip4_route_sync (int ifindex, const GArray *known_routes)
for (i = 0; i < known_routes->len; i++) {
known_route = &g_array_index (known_routes, NMPlatformIP4Route, i);
if (!nm_platform_ip4_route_exists (ifindex,
known_route->network, known_route->plen, known_route->metric))
if (!nm_platform_ip4_route_add (ifindex,
known_route->network, known_route->plen, known_route->gateway,
known_route->metric, known_route->mss))
return FALSE;
if (!nm_platform_ip4_route_add (ifindex,
known_route->network, known_route->plen, known_route->gateway,
known_route->metric, known_route->mss))
return FALSE;
}
return TRUE;
@@ -1569,12 +1555,10 @@ nm_platform_ip6_route_sync (int ifindex, const GArray *known_routes)
for (i = 0; i < known_routes->len; i++) {
known_route = &g_array_index (known_routes, NMPlatformIP6Route, i);
if (!nm_platform_ip6_route_exists (ifindex,
known_route->network, known_route->plen, known_route->metric))
if (!nm_platform_ip6_route_add (ifindex,
known_route->network, known_route->plen, known_route->gateway,
known_route->metric, known_route->mss))
return FALSE;
if (!nm_platform_ip6_route_add (ifindex,
known_route->network, known_route->plen, known_route->gateway,
known_route->metric, known_route->mss))
return FALSE;
}
return TRUE;

View File

@@ -41,9 +41,10 @@ ip6_route_callback (NMPlatform *platform, int ifindex, NMPlatformIP6Route *recei
static void
test_ip4_route ()
{
SignalData *route_added = add_signal (NM_PLATFORM_IP4_ROUTE_ADDED, ip4_route_callback);
SignalData *route_removed = add_signal (NM_PLATFORM_IP4_ROUTE_REMOVED, ip4_route_callback);
int ifindex = nm_platform_link_get_ifindex (DEVICE_NAME);
SignalData *route_added = add_signal (NM_PLATFORM_IP4_ROUTE_ADDED, ip4_route_callback);
SignalData *route_changed = add_signal (NM_PLATFORM_IP4_ROUTE_CHANGED, ip4_route_callback);
SignalData *route_removed = add_signal (NM_PLATFORM_IP4_ROUTE_REMOVED, ip4_route_callback);
GArray *routes;
NMPlatformIP4Route rts[4];
in_addr_t network;
@@ -66,8 +67,9 @@ test_ip4_route ()
accept_signal (route_added);
/* Add route again */
g_assert (!nm_platform_ip4_route_add (ifindex, network, plen, gateway, metric, mss));
error (NM_PLATFORM_ERROR_EXISTS);
g_assert (nm_platform_ip4_route_add (ifindex, network, plen, gateway, metric, mss));
no_error ();
accept_signal (route_changed);
/* Add default route */
g_assert (!nm_platform_ip4_route_exists (ifindex, 0, 0, metric)); no_error ();
@@ -76,8 +78,9 @@ test_ip4_route ()
accept_signal (route_added);
/* Add default route again */
g_assert (!nm_platform_ip4_route_add (ifindex, 0, 0, gateway, metric, mss));
error (NM_PLATFORM_ERROR_EXISTS);
g_assert (nm_platform_ip4_route_add (ifindex, 0, 0, gateway, metric, mss));
no_error ();
accept_signal (route_changed);
/* Test route listing */
routes = nm_platform_ip4_route_get_all (ifindex);
@@ -114,15 +117,17 @@ test_ip4_route ()
error (NM_PLATFORM_ERROR_NOT_FOUND);
free_signal (route_added);
free_signal (route_changed);
free_signal (route_removed);
}
static void
test_ip6_route ()
{
SignalData *route_added = add_signal (NM_PLATFORM_IP6_ROUTE_ADDED, ip6_route_callback);
SignalData *route_removed = add_signal (NM_PLATFORM_IP6_ROUTE_REMOVED, ip6_route_callback);
int ifindex = nm_platform_link_get_ifindex (DEVICE_NAME);
SignalData *route_added = add_signal (NM_PLATFORM_IP6_ROUTE_ADDED, ip6_route_callback);
SignalData *route_changed = add_signal (NM_PLATFORM_IP6_ROUTE_CHANGED, ip6_route_callback);
SignalData *route_removed = add_signal (NM_PLATFORM_IP6_ROUTE_REMOVED, ip6_route_callback);
GArray *routes;
NMPlatformIP6Route rts[4];
struct in6_addr network;
@@ -145,8 +150,9 @@ test_ip6_route ()
accept_signal (route_added);
/* Add route again */
g_assert (!nm_platform_ip6_route_add (ifindex, network, plen, gateway, metric, mss));
error (NM_PLATFORM_ERROR_EXISTS);
g_assert (nm_platform_ip6_route_add (ifindex, network, plen, gateway, metric, mss));
no_error ();
accept_signal (route_changed);
/* Add default route */
g_assert (!nm_platform_ip6_route_exists (ifindex, in6addr_any, 0, metric)); no_error ();
@@ -155,8 +161,9 @@ test_ip6_route ()
accept_signal (route_added);
/* Add default route again */
g_assert (!nm_platform_ip6_route_add (ifindex, in6addr_any, 0, gateway, metric, mss));
error (NM_PLATFORM_ERROR_EXISTS);
g_assert (nm_platform_ip6_route_add (ifindex, in6addr_any, 0, gateway, metric, mss));
no_error ();
accept_signal (route_changed);
/* Test route listing */
routes = nm_platform_ip6_route_get_all (ifindex);
@@ -193,6 +200,7 @@ test_ip6_route ()
error (NM_PLATFORM_ERROR_NOT_FOUND);
free_signal (route_added);
free_signal (route_changed);
free_signal (route_removed);
}