Skip to content

Commit 02f62d4

Browse files
committed
core: consistently use _nm_ip_config_add_obj() when adding route/address to ip-config
_nm_ip_config_add_obj() does some additional checking, like setting the ifindex. We shall not bypass this also during bulk-update (replace). Add options @merge and @append_force to make _nm_ip_config_add_obj() suitable in those cases too, and use it.
1 parent f59669c commit 02f62d4

File tree

3 files changed

+92
-67
lines changed

3 files changed

+92
-67
lines changed

src/nm-ip4-config.c

Lines changed: 69 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,9 @@ _nm_ip_config_add_obj (NMDedupMultiIndex *multi_idx,
163163
NMIPConfigDedupMultiIdxType *idx_type,
164164
int ifindex,
165165
const NMPObject *obj_new,
166-
const NMPlatformObject *pl_new)
166+
const NMPlatformObject *pl_new,
167+
gboolean merge,
168+
gboolean append_force)
167169
{
168170
NMPObject obj_new_stackinit;
169171
const NMDedupMultiEntry *entry_old;
@@ -200,58 +202,62 @@ _nm_ip_config_add_obj (NMDedupMultiIndex *multi_idx,
200202
const NMPObject *obj_old = entry_old->obj;
201203

202204
if (nmp_object_equal (obj_new, obj_old))
203-
return FALSE;
204-
205-
switch (idx_type->obj_type) {
206-
case NMP_OBJECT_TYPE_IP4_ADDRESS:
207-
case NMP_OBJECT_TYPE_IP6_ADDRESS:
208-
/* we want to keep the maximum addr_source. But since we expect
209-
* that usually we already add the maxiumum right away, we first try to
210-
* add the new address (replacing the old one). Only if we later
211-
* find out that addr_source is now lower, we fix it.
212-
*/
213-
if (obj_new->ip_address.addr_source < obj_old->ip_address.addr_source) {
214-
obj_new = nmp_object_stackinit_obj (&obj_new_stackinit, obj_new);
215-
obj_new_stackinit.ip_address.addr_source = obj_old->ip_address.addr_source;
216-
modified = TRUE;
217-
}
205+
goto append_force_and_out;
206+
207+
/* if @merge, we merge the new object with the existing one.
208+
* Otherwise, we replace it entirely. */
209+
if (merge) {
210+
switch (idx_type->obj_type) {
211+
case NMP_OBJECT_TYPE_IP4_ADDRESS:
212+
case NMP_OBJECT_TYPE_IP6_ADDRESS:
213+
/* we want to keep the maximum addr_source. But since we expect
214+
* that usually we already add the maxiumum right away, we first try to
215+
* add the new address (replacing the old one). Only if we later
216+
* find out that addr_source is now lower, we fix it.
217+
*/
218+
if (obj_new->ip_address.addr_source < obj_old->ip_address.addr_source) {
219+
obj_new = nmp_object_stackinit_obj (&obj_new_stackinit, obj_new);
220+
obj_new_stackinit.ip_address.addr_source = obj_old->ip_address.addr_source;
221+
modified = TRUE;
222+
}
218223

219-
/* for addresses that we read from the kernel, we keep the timestamps as defined
220-
* by the previous source (item_old). The reason is, that the other source configured the lifetimes
221-
* with "what should be" and the kernel values are "what turned out after configuring it".
222-
*
223-
* For other sources, the longer lifetime wins. */
224-
if ( ( obj_new->ip_address.addr_source == NM_IP_CONFIG_SOURCE_KERNEL
225-
&& obj_old->ip_address.addr_source != NM_IP_CONFIG_SOURCE_KERNEL)
226-
|| nm_platform_ip_address_cmp_expiry (NMP_OBJECT_CAST_IP_ADDRESS (obj_old), NMP_OBJECT_CAST_IP_ADDRESS(obj_new)) > 0) {
227-
obj_new = nmp_object_stackinit_obj (&obj_new_stackinit, obj_new);
228-
obj_new_stackinit.ip_address.timestamp = NMP_OBJECT_CAST_IP_ADDRESS (obj_old)->timestamp;
229-
obj_new_stackinit.ip_address.lifetime = NMP_OBJECT_CAST_IP_ADDRESS (obj_old)->lifetime;
230-
obj_new_stackinit.ip_address.preferred = NMP_OBJECT_CAST_IP_ADDRESS (obj_old)->preferred;
231-
modified = TRUE;
232-
}
233-
break;
234-
case NMP_OBJECT_TYPE_IP4_ROUTE:
235-
case NMP_OBJECT_TYPE_IP6_ROUTE:
236-
/* we want to keep the maximum rt_source. But since we expect
237-
* that usually we already add the maxiumum right away, we first try to
238-
* add the new route (replacing the old one). Only if we later
239-
* find out that rt_source is now lower, we fix it.
240-
*/
241-
if (obj_new->ip_route.rt_source < obj_old->ip_route.rt_source) {
242-
obj_new = nmp_object_stackinit_obj (&obj_new_stackinit, obj_new);
243-
obj_new_stackinit.ip_route.rt_source = obj_old->ip_route.rt_source;
244-
modified = TRUE;
224+
/* for addresses that we read from the kernel, we keep the timestamps as defined
225+
* by the previous source (item_old). The reason is, that the other source configured the lifetimes
226+
* with "what should be" and the kernel values are "what turned out after configuring it".
227+
*
228+
* For other sources, the longer lifetime wins. */
229+
if ( ( obj_new->ip_address.addr_source == NM_IP_CONFIG_SOURCE_KERNEL
230+
&& obj_old->ip_address.addr_source != NM_IP_CONFIG_SOURCE_KERNEL)
231+
|| nm_platform_ip_address_cmp_expiry (NMP_OBJECT_CAST_IP_ADDRESS (obj_old), NMP_OBJECT_CAST_IP_ADDRESS(obj_new)) > 0) {
232+
obj_new = nmp_object_stackinit_obj (&obj_new_stackinit, obj_new);
233+
obj_new_stackinit.ip_address.timestamp = NMP_OBJECT_CAST_IP_ADDRESS (obj_old)->timestamp;
234+
obj_new_stackinit.ip_address.lifetime = NMP_OBJECT_CAST_IP_ADDRESS (obj_old)->lifetime;
235+
obj_new_stackinit.ip_address.preferred = NMP_OBJECT_CAST_IP_ADDRESS (obj_old)->preferred;
236+
modified = TRUE;
237+
}
238+
break;
239+
case NMP_OBJECT_TYPE_IP4_ROUTE:
240+
case NMP_OBJECT_TYPE_IP6_ROUTE:
241+
/* we want to keep the maximum rt_source. But since we expect
242+
* that usually we already add the maxiumum right away, we first try to
243+
* add the new route (replacing the old one). Only if we later
244+
* find out that rt_source is now lower, we fix it.
245+
*/
246+
if (obj_new->ip_route.rt_source < obj_old->ip_route.rt_source) {
247+
obj_new = nmp_object_stackinit_obj (&obj_new_stackinit, obj_new);
248+
obj_new_stackinit.ip_route.rt_source = obj_old->ip_route.rt_source;
249+
modified = TRUE;
250+
}
251+
break;
252+
default:
253+
nm_assert_not_reached ();
254+
break;
245255
}
246-
break;
247-
default:
248-
nm_assert_not_reached ();
249-
break;
250-
}
251256

252-
if ( modified
253-
&& nmp_object_equal (obj_new, obj_old))
254-
return FALSE;
257+
if ( modified
258+
&& nmp_object_equal (obj_new, obj_old))
259+
goto append_force_and_out;
260+
}
255261
}
256262

257263
if (!nm_dedup_multi_index_add_full (multi_idx,
@@ -268,6 +274,13 @@ _nm_ip_config_add_obj (NMDedupMultiIndex *multi_idx,
268274
}
269275

270276
return TRUE;
277+
278+
append_force_and_out:
279+
if (append_force) {
280+
if (nm_dedup_multi_entry_reorder (entry_old, NULL, TRUE))
281+
return TRUE;
282+
}
283+
return FALSE;
271284
}
272285

273286
/**
@@ -1955,7 +1968,9 @@ _add_address (NMIP4Config *self, const NMPObject *obj_new, const NMPlatformIP4Ad
19551968
&priv->idx_ip4_addresses_,
19561969
priv->ifindex,
19571970
obj_new,
1958-
(const NMPlatformObject *) new))
1971+
(const NMPlatformObject *) new,
1972+
TRUE,
1973+
FALSE))
19591974
_notify_addresses (self);
19601975
}
19611976

@@ -2092,7 +2107,9 @@ _add_route (NMIP4Config *self, const NMPObject *obj_new, const NMPlatformIP4Rout
20922107
&priv->idx_ip4_routes_,
20932108
priv->ifindex,
20942109
obj_new,
2095-
(const NMPlatformObject *) new))
2110+
(const NMPlatformObject *) new,
2111+
TRUE,
2112+
FALSE))
20962113
_notify_routes (self);
20972114
}
20982115

src/nm-ip4-config.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ gboolean _nm_ip_config_add_obj (NMDedupMultiIndex *multi_idx,
9292
NMIPConfigDedupMultiIdxType *idx_type,
9393
int ifindex,
9494
const NMPObject *obj_new,
95-
const NMPlatformObject *pl_new);
95+
const NMPlatformObject *pl_new,
96+
gboolean merge,
97+
gboolean append_force);
9698

9799
const NMDedupMultiEntry *_nm_ip_config_lookup_ip_route (const NMDedupMultiIndex *multi_idx,
98100
const NMIPConfigDedupMultiIdxType *idx_type,

src/nm-ip6-config.c

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,12 +1246,13 @@ nm_ip6_config_replace (NMIP6Config *dst, const NMIP6Config *src, gboolean *relev
12461246
has_minor_changes = TRUE;
12471247
nm_dedup_multi_index_dirty_set_idx (dst_priv->multi_idx, &dst_priv->idx_ip6_addresses);
12481248
nm_dedup_multi_iter_for_each (&ipconf_iter_src, head_entry_src) {
1249-
nm_dedup_multi_index_add (dst_priv->multi_idx,
1250-
&dst_priv->idx_ip6_addresses,
1251-
ipconf_iter_src.current->obj,
1252-
NM_DEDUP_MULTI_IDX_MODE_APPEND_FORCE,
1253-
NULL,
1254-
NULL);
1249+
_nm_ip_config_add_obj (dst_priv->multi_idx,
1250+
&dst_priv->idx_ip6_addresses_,
1251+
dst_priv->ifindex,
1252+
ipconf_iter_src.current->obj,
1253+
NULL,
1254+
FALSE,
1255+
TRUE);
12551256
}
12561257
nm_dedup_multi_index_dirty_remove_idx (dst_priv->multi_idx, &dst_priv->idx_ip6_addresses, FALSE);
12571258
_notify_addresses (dst);
@@ -1290,12 +1291,13 @@ nm_ip6_config_replace (NMIP6Config *dst, const NMIP6Config *src, gboolean *relev
12901291
has_minor_changes = TRUE;
12911292
nm_dedup_multi_index_dirty_set_idx (dst_priv->multi_idx, &dst_priv->idx_ip6_routes);
12921293
nm_dedup_multi_iter_for_each (&ipconf_iter_src, head_entry_src) {
1293-
nm_dedup_multi_index_add (dst_priv->multi_idx,
1294-
&dst_priv->idx_ip6_routes,
1295-
ipconf_iter_src.current->obj,
1296-
NM_DEDUP_MULTI_IDX_MODE_APPEND_FORCE,
1297-
NULL,
1298-
NULL);
1294+
_nm_ip_config_add_obj (dst_priv->multi_idx,
1295+
&dst_priv->idx_ip6_routes_,
1296+
dst_priv->ifindex,
1297+
ipconf_iter_src.current->obj,
1298+
NULL,
1299+
FALSE,
1300+
TRUE);
12991301
}
13001302
nm_dedup_multi_index_dirty_remove_idx (dst_priv->multi_idx, &dst_priv->idx_ip6_routes, FALSE);
13011303
_notify_routes (dst);
@@ -1537,7 +1539,9 @@ _add_address (NMIP6Config *self,
15371539
&priv->idx_ip6_addresses_,
15381540
priv->ifindex,
15391541
obj_new,
1540-
(const NMPlatformObject *) new))
1542+
(const NMPlatformObject *) new,
1543+
TRUE,
1544+
FALSE))
15411545
_notify_addresses (self);
15421546
}
15431547

@@ -1714,7 +1718,9 @@ _add_route (NMIP6Config *self, const NMPObject *obj_new, const NMPlatformIP6Rout
17141718
&priv->idx_ip6_routes_,
17151719
priv->ifindex,
17161720
obj_new,
1717-
(const NMPlatformObject *) new))
1721+
(const NMPlatformObject *) new,
1722+
TRUE,
1723+
FALSE))
17181724
_notify_routes (self);
17191725
}
17201726

0 commit comments

Comments
 (0)