nmp-object.c 76.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* nm-platform.c - Handle runtime kernel networking configuration
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Copyright (C) 2015 Red Hat, Inc.
 */

21
#include "nm-default.h"
22

23 24
#include "nmp-object.h"

25 26 27 28
#include <unistd.h>

#include "nm-utils.h"

29 30 31
#include "nm-core-utils.h"
#include "nm-platform-utils.h"

32 33
/*********************************************************************************************/

34 35
#define _NMLOG_DOMAIN LOGD_PLATFORM
#define _NMLOG(level, obj, ...) \
36 37 38
    G_STMT_START { \
        const NMLogLevel __level = (level); \
        \
39
        if (nm_logging_enabled (__level, _NMLOG_DOMAIN)) { \
40 41
            const NMPObject *const __obj = (obj); \
            \
42
            _nm_log (__level, _NMLOG_DOMAIN, 0, \
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
                     "nmp-object[%p/%s]: " _NM_UTILS_MACRO_FIRST (__VA_ARGS__), \
                     __obj, \
                     (__obj ? NMP_OBJECT_GET_CLASS (__obj)->obj_type_name : "???") \
                     _NM_UTILS_MACRO_REST (__VA_ARGS__)); \
        } \
    } G_STMT_END

/*********************************************************************************************/

struct _NMPCache {
	/* the cache contains only one hash table for all object types, and similarly
	 * it contains only one NMMultiIndex.
	 * This works, because different object types don't ever compare equal and
	 * because their index ids also don't overlap.
	 *
	 * For routes and addresses, the cache contains an address if (and only if) the
	 * object was reported via netlink.
	 * For links, the cache contain a link if it was reported by either netlink
	 * or udev. That means, a link object can be alive, even if it was already
	 * removed via netlink.
	 *
	 * This effectively merges the udev-device cache into the NMPCache.
	 */

	GHashTable *idx_main;
	NMMultiIndex *idx_multi;

	gboolean use_udev;
};

/******************************************************************/

static inline guint
_id_hash_ip6_addr (const struct in6_addr *addr)
{
	guint hash = (guint) 0x897da53981a13ULL;
	int i;

	for (i = 0; i < sizeof (*addr); i++)
		hash = (hash * 33) + ((const guint8 *) addr)[i];
	return hash;
}

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
static int
_vlan_xgress_qos_mappings_cmp (guint n_map,
                               const NMVlanQosMapping *map1,
                               const NMVlanQosMapping *map2)
{
	guint i;

	for (i = 0; i < n_map; i++) {
		if (map1[i].from != map2[i].from)
			return map1[i].from < map2[i].from ? -1 : 1;
		if (map1[i].to != map2[i].to)
			return map1[i].to < map2[i].to ? -1 : 1;
	}
	return 0;
}

static void
_vlan_xgress_qos_mappings_cpy (guint *dst_n_map,
                               const NMVlanQosMapping **dst_map,
                               guint src_n_map,
                               const NMVlanQosMapping *src_map)
{
	if (src_n_map == 0) {
		g_clear_pointer (dst_map, g_free);
		*dst_n_map = 0;
	} else if (   src_n_map != *dst_n_map
	           || _vlan_xgress_qos_mappings_cmp (src_n_map, *dst_map, src_map) != 0) {
		g_clear_pointer (dst_map, g_free);
		*dst_n_map = src_n_map;
		if (src_n_map > 0)
			*dst_map = g_memdup (src_map, sizeof (*src_map) * src_n_map);
	}
}

120 121
/******************************************************************/

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
static const char *
_link_get_driver (GUdevDevice *udev_device, const char *kind, const char *ifname)
{
	const char *driver = NULL;

	nm_assert (kind == g_intern_string (kind));

	if (udev_device) {
		driver = nmp_utils_udev_get_driver (udev_device);
		if (driver)
			return driver;
	}

	if (kind)
		return kind;

	if (ifname) {
		char *d;

		if (nmp_utils_ethtool_get_driver_info (ifname, &d, NULL, NULL)) {
			driver = d && d[0] ? g_intern_string (d) : NULL;
			g_free (d);
			if (driver)
				return driver;
		}
	}

	return "unknown";
}

void
_nmp_object_fixup_link_udev_fields (NMPObject *obj, gboolean use_udev)
{
	const char *driver = NULL;
	gboolean initialized = FALSE;

158
	nm_assert (NMP_OBJECT_GET_TYPE (obj) == NMP_OBJECT_TYPE_LINK);
159 160 161 162 163 164 165 166 167

	/* The link contains internal fields that are combined by
	 * properties from netlink and udev. Update those properties */

	/* When a link is not in netlink, it's udev fields don't matter. */
	if (obj->_link.netlink.is_in_netlink) {
		driver = _link_get_driver (obj->_link.udev.device,
		                           obj->link.kind,
		                           obj->link.name);
168
		if (obj->_link.udev.device)
169
			initialized = TRUE;
170
		else if (!use_udev) {
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
			/* If we don't use udev, we immediately mark the link as initialized.
			 *
			 * For that, we consult @use_udev argument, that is cached via
			 * nmp_cache_use_udev_get(). It is on purpose not to test
			 * for a writable /sys on every call. A minor reason for that is
			 * performance, but the real reason is reproducibility.
			 * */
			initialized = TRUE;
		}
	}

	obj->link.driver = driver;
	obj->link.initialized = initialized;
}

static void
_nmp_object_fixup_link_master_connected (NMPObject *obj, const NMPCache *cache)
{
189
	nm_assert (NMP_OBJECT_GET_TYPE (obj) == NMP_OBJECT_TYPE_LINK);
190 191 192 193 194 195 196 197

	if (nmp_cache_link_connected_needs_toggle (cache, obj, NULL, NULL))
		obj->link.connected = !obj->link.connected;
}

/******************************************************************/

const NMPClass *
198
nmp_class_from_type (NMPObjectType obj_type)
199
{
200
	g_return_val_if_fail (obj_type > NMP_OBJECT_TYPE_UNKNOWN && obj_type <= NMP_OBJECT_TYPE_MAX, NULL);
201 202 203 204 205 206 207 208 209 210 211 212 213

	return &_nmp_classes[obj_type - 1];
}

/******************************************************************/

NMPObject *
nmp_object_ref (NMPObject *obj)
{
	g_return_val_if_fail (NMP_OBJECT_IS_VALID (obj), NULL);
	g_return_val_if_fail (obj->_ref_count != NMP_REF_COUNT_STACKINIT, NULL);
	obj->_ref_count++;

214
	_LOGt (obj, "ref: %d", obj->_ref_count);
215 216 217 218 219 220 221 222 223 224

	return obj;
}

void
nmp_object_unref (NMPObject *obj)
{
	if (obj) {
		g_return_if_fail (obj->_ref_count > 0);
		g_return_if_fail (obj->_ref_count != NMP_REF_COUNT_STACKINIT);
225
		_LOGt (obj, "%s: %d",
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
		       obj->_ref_count <= 1 ? "destroy" : "unref",
		       obj->_ref_count - 1);
		if (--obj->_ref_count <= 0) {
			const NMPClass *klass = obj->_class;

			nm_assert (!obj->is_cached);
			if (klass->cmd_obj_dispose)
				klass->cmd_obj_dispose (obj);
			g_slice_free1 (klass->sizeof_data + G_STRUCT_OFFSET (NMPObject, object), obj);
		}
	}
}

static void
_vt_cmd_obj_dispose_link (NMPObject *obj)
{
	g_clear_object (&obj->_link.udev.device);
243
	nmp_object_unref (obj->_link.netlink.lnk);
244 245
}

246 247 248 249 250 251 252
static void
_vt_cmd_obj_dispose_lnk_vlan (NMPObject *obj)
{
	g_free ((gpointer) obj->_lnk_vlan.ingress_qos_map);
	g_free ((gpointer) obj->_lnk_vlan.egress_qos_map);
}

253 254 255 256 257 258 259 260 261 262 263 264
static NMPObject *
_nmp_object_new_from_class (const NMPClass *klass)
{
	NMPObject *obj;

	nm_assert (klass);
	nm_assert (klass->sizeof_data > 0);
	nm_assert (klass->sizeof_public > 0 && klass->sizeof_public <= klass->sizeof_data);

	obj = g_slice_alloc0 (klass->sizeof_data + G_STRUCT_OFFSET (NMPObject, object));
	obj->_class = klass;
	obj->_ref_count = 1;
265
	_LOGt (obj, "new");
266 267 268 269
	return obj;
}

NMPObject *
270
nmp_object_new (NMPObjectType obj_type, const NMPlatformObject *plobj)
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
{
	const NMPClass *klass = nmp_class_from_type (obj_type);
	NMPObject *obj;

	obj = _nmp_object_new_from_class (klass);
	if (plobj)
		memcpy (&obj->object, plobj, klass->sizeof_public);
	return obj;
}

NMPObject *
nmp_object_new_link (int ifindex)
{
	NMPObject *obj;

286
	obj = nmp_object_new (NMP_OBJECT_TYPE_LINK, NULL);
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
	obj->link.ifindex = ifindex;
	return obj;
}

/******************************************************************/

static const NMPObject *
_nmp_object_stackinit_from_class (NMPObject *obj, const NMPClass *klass)
{
	nm_assert (klass);

	memset (obj, 0, sizeof (NMPObject));
	obj->_class = klass;
	obj->_ref_count = NMP_REF_COUNT_STACKINIT;
	return obj;
}

const NMPObject *
305
nmp_object_stackinit (NMPObject *obj, NMPObjectType obj_type, const NMPlatformObject *plobj)
306 307 308 309 310 311 312 313 314 315 316 317
{
	const NMPClass *klass = nmp_class_from_type (obj_type);

	_nmp_object_stackinit_from_class (obj, klass);
	if (plobj)
		memcpy (&obj->object, plobj, klass->sizeof_public);
	return obj;
}

const NMPObject *
nmp_object_stackinit_id  (NMPObject *obj, const NMPObject *src)
{
318 319
	const NMPClass *klass;

320 321 322
	nm_assert (NMP_OBJECT_IS_VALID (src));
	nm_assert (obj);

323 324 325 326 327
	klass = NMP_OBJECT_GET_CLASS (src);
	if (!klass->cmd_obj_stackinit_id)
		nmp_object_stackinit (obj, klass->obj_type, NULL);
	else
		klass->cmd_obj_stackinit_id (obj, src);
328 329 330 331 332 333
	return obj;
}

const NMPObject *
nmp_object_stackinit_id_link (NMPObject *obj, int ifindex)
{
334
	nmp_object_stackinit (obj, NMP_OBJECT_TYPE_LINK, NULL);
335 336 337 338 339 340 341 342 343 344 345
	obj->link.ifindex = ifindex;
	return obj;
}

static void
_vt_cmd_obj_stackinit_id_link (NMPObject *obj, const NMPObject *src)
{
	nmp_object_stackinit_id_link (obj, src->link.ifindex);
}

const NMPObject *
346
nmp_object_stackinit_id_ip4_address (NMPObject *obj, int ifindex, guint32 address, guint8 plen, guint32 peer_address)
347
{
348
	nmp_object_stackinit (obj, NMP_OBJECT_TYPE_IP4_ADDRESS, NULL);
349 350 351
	obj->ip4_address.ifindex = ifindex;
	obj->ip4_address.address = address;
	obj->ip4_address.plen = plen;
352
	obj->ip4_address.peer_address = peer_address;
353 354 355 356 357 358
	return obj;
}

static void
_vt_cmd_obj_stackinit_id_ip4_address (NMPObject *obj, const NMPObject *src)
{
359
	nmp_object_stackinit_id_ip4_address (obj, src->ip_address.ifindex, src->ip4_address.address, src->ip_address.plen, src->ip4_address.peer_address);
360 361 362
}

const NMPObject *
363
nmp_object_stackinit_id_ip6_address (NMPObject *obj, int ifindex, const struct in6_addr *address, guint8 plen)
364
{
365
	nmp_object_stackinit (obj, NMP_OBJECT_TYPE_IP6_ADDRESS, NULL);
366 367 368 369 370 371 372 373 374 375 376 377 378 379
	obj->ip4_address.ifindex = ifindex;
	if (address)
		obj->ip6_address.address = *address;
	obj->ip6_address.plen = plen;
	return obj;
}

static void
_vt_cmd_obj_stackinit_id_ip6_address (NMPObject *obj, const NMPObject *src)
{
	nmp_object_stackinit_id_ip6_address (obj, src->ip_address.ifindex, &src->ip6_address.address, src->ip_address.plen);
}

const NMPObject *
380
nmp_object_stackinit_id_ip4_route (NMPObject *obj, int ifindex, guint32 network, guint8 plen, guint32 metric)
381
{
382
	nmp_object_stackinit (obj, NMP_OBJECT_TYPE_IP4_ROUTE, NULL);
383 384 385 386 387 388 389 390 391 392 393 394 395 396
	obj->ip4_route.ifindex = ifindex;
	obj->ip4_route.network = network;
	obj->ip4_route.plen = plen;
	obj->ip4_route.metric = metric;
	return obj;
}

static void
_vt_cmd_obj_stackinit_id_ip4_route (NMPObject *obj, const NMPObject *src)
{
	nmp_object_stackinit_id_ip4_route (obj, src->ip_route.ifindex, src->ip4_route.network, src->ip_route.plen, src->ip_route.metric);
}

const NMPObject *
397
nmp_object_stackinit_id_ip6_route (NMPObject *obj, int ifindex, const struct in6_addr *network, guint8 plen, guint32 metric)
398
{
399
	nmp_object_stackinit (obj, NMP_OBJECT_TYPE_IP6_ROUTE, NULL);
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
	obj->ip6_route.ifindex = ifindex;
	if (network)
		obj->ip6_route.network = *network;
	obj->ip6_route.plen = plen;
	obj->ip6_route.metric = metric;
	return obj;
}

static void
_vt_cmd_obj_stackinit_id_ip6_route (NMPObject *obj, const NMPObject *src)
{
	nmp_object_stackinit_id_ip6_route (obj, src->ip_route.ifindex, &src->ip6_route.network, src->ip_route.plen, src->ip_route.metric);
}

/******************************************************************/

const char *
nmp_object_to_string (const NMPObject *obj, NMPObjectToStringMode to_string_mode, char *buf, gsize buf_size)
{
	const NMPClass *klass;
420
	char buf2[sizeof (_nm_utils_to_string_buffer)];
421

422
	if (!nm_utils_to_string_buffer_init_null (obj, &buf, &buf_size))
423 424 425 426 427 428
		return buf;

	g_return_val_if_fail (NMP_OBJECT_IS_VALID (obj), NULL);

	klass = NMP_OBJECT_GET_CLASS (obj);

429 430 431
	if (klass->cmd_obj_to_string)
		return klass->cmd_obj_to_string (obj, to_string_mode, buf, buf_size);

432 433
	switch (to_string_mode) {
	case NMP_OBJECT_TO_STRING_ID:
434 435 436 437
		if (!klass->cmd_plobj_to_string_id) {
			g_snprintf (buf, buf_size, "%p", obj);
			return buf;
		}
438 439
		return klass->cmd_plobj_to_string_id (&obj->object, buf, buf_size);
	case NMP_OBJECT_TO_STRING_ALL:
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
		g_snprintf (buf, buf_size,
		            "[%s,%p,%d,%ccache,%calive,%cvisible; %s]",
		            klass->obj_type_name, obj, obj->_ref_count,
		            obj->is_cached ? '+' : '-',
		            nmp_object_is_alive (obj) ? '+' : '-',
		            nmp_object_is_visible (obj) ? '+' : '-',
		            NMP_OBJECT_GET_CLASS (obj)->cmd_plobj_to_string (&obj->object, buf2, sizeof (buf2)));
		return buf;
	case NMP_OBJECT_TO_STRING_PUBLIC:
		NMP_OBJECT_GET_CLASS (obj)->cmd_plobj_to_string (&obj->object, buf, buf_size);
		return buf;
	default:
		g_return_val_if_reached ("ERROR");
	}
}
455

456 457 458 459 460 461
static const char *
_vt_cmd_obj_to_string_link (const NMPObject *obj, NMPObjectToStringMode to_string_mode, char *buf, gsize buf_size)
{
	const NMPClass *klass = NMP_OBJECT_GET_CLASS (obj);
	char buf2[sizeof (_nm_utils_to_string_buffer)];
	char buf3[sizeof (_nm_utils_to_string_buffer)];
462

463 464 465 466
	switch (to_string_mode) {
	case NMP_OBJECT_TO_STRING_ID:
		return klass->cmd_plobj_to_string_id (&obj->object, buf, buf_size);
	case NMP_OBJECT_TO_STRING_ALL:
467
		g_snprintf (buf, buf_size,
468
		            "[%s,%p,%d,%ccache,%calive,%cvisible,%cin-nl,%p; %s]",
469 470 471 472
		            klass->obj_type_name, obj, obj->_ref_count,
		            obj->is_cached ? '+' : '-',
		            nmp_object_is_alive (obj) ? '+' : '-',
		            nmp_object_is_visible (obj) ? '+' : '-',
473 474 475
		            obj->_link.netlink.is_in_netlink ? '+' : '-',
		            obj->_link.udev.device,
		            nmp_object_to_string (obj, NMP_OBJECT_TO_STRING_PUBLIC, buf2, sizeof (buf2)));
476 477
		return buf;
	case NMP_OBJECT_TO_STRING_PUBLIC:
478
		if (obj->_link.netlink.lnk) {
479 480 481 482 483 484 485
			NMP_OBJECT_GET_CLASS (obj)->cmd_plobj_to_string (&obj->object, buf2, sizeof (buf2));
			nmp_object_to_string (obj->_link.netlink.lnk, NMP_OBJECT_TO_STRING_PUBLIC, buf3, sizeof (buf3));
			g_snprintf (buf, buf_size,
			            "%s; %s",
			            buf2, buf3);
		} else
			NMP_OBJECT_GET_CLASS (obj)->cmd_plobj_to_string (&obj->object, buf, buf_size);
486 487 488 489 490 491
		return buf;
	default:
		g_return_val_if_reached ("ERROR");
	}
}

492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
static const char *
_vt_cmd_obj_to_string_lnk_vlan (const NMPObject *obj, NMPObjectToStringMode to_string_mode, char *buf, gsize buf_size)
{
	const NMPClass *klass = NMP_OBJECT_GET_CLASS (obj);
	char buf2[sizeof (_nm_utils_to_string_buffer)];
	char *b;
	gsize l;

	klass = NMP_OBJECT_GET_CLASS (obj);

	switch (to_string_mode) {
	case NMP_OBJECT_TO_STRING_ID:
		g_snprintf (buf, buf_size, "%p", obj);
		return buf;
	case NMP_OBJECT_TO_STRING_ALL:

		g_snprintf (buf, buf_size,
		            "[%s,%p,%d,%ccache,%calive,%cvisible; %s]",
		            klass->obj_type_name, obj, obj->_ref_count,
		            obj->is_cached ? '+' : '-',
		            nmp_object_is_alive (obj) ? '+' : '-',
		            nmp_object_is_visible (obj) ? '+' : '-',
		            nmp_object_to_string (obj, NMP_OBJECT_TO_STRING_PUBLIC, buf2, sizeof (buf2)));
		return buf;
	case NMP_OBJECT_TO_STRING_PUBLIC:
		NMP_OBJECT_GET_CLASS (obj)->cmd_plobj_to_string (&obj->object, buf, buf_size);

		b = buf;
		l = strlen (b);
		b += l;
		buf_size -= l;

		if (obj->_lnk_vlan.n_ingress_qos_map) {
			nm_platform_vlan_qos_mapping_to_string (" ingress-qos-map",
			                                        obj->_lnk_vlan.ingress_qos_map,
			                                        obj->_lnk_vlan.n_ingress_qos_map,
			                                        b,
			                                        buf_size);
			l = strlen (b);
			b += l;
			buf_size -= l;
		}
		if (obj->_lnk_vlan.n_egress_qos_map) {
			nm_platform_vlan_qos_mapping_to_string (" egress-qos-map",
			                                        obj->_lnk_vlan.egress_qos_map,
			                                        obj->_lnk_vlan.n_egress_qos_map,
			                                        b,
			                                        buf_size);
			l = strlen (b);
			b += l;
			buf_size -= l;
		}

		return buf;
	default:
		g_return_val_if_reached ("ERROR");
	}
}

551 552 553 554 555 556
#define _vt_cmd_plobj_to_string_id(type, plat_type, ...) \
static const char * \
_vt_cmd_plobj_to_string_id_##type (const NMPlatformObject *_obj, char *buf, gsize buf_len) \
{ \
	plat_type *const obj = (plat_type *) _obj; \
	char buf1[NM_UTILS_INET_ADDRSTRLEN]; \
557
	char buf2[NM_UTILS_INET_ADDRSTRLEN]; \
558 559
	\
	(void) buf1; \
560
	(void) buf2; \
561 562 563 564
	g_snprintf (buf, buf_len, \
	            __VA_ARGS__); \
	return buf; \
}
565 566
_vt_cmd_plobj_to_string_id (link,        NMPlatformLink,       "%d",            obj->ifindex);
_vt_cmd_plobj_to_string_id (ip4_address, NMPlatformIP4Address, "%d: %s/%d%s%s", obj->ifindex, nm_utils_inet4_ntop ( obj->address, buf1), obj->plen,
567 568
                                                               obj->peer_address != obj->address ? "," : "",
                                                               obj->peer_address != obj->address ? nm_utils_inet4_ntop (obj->peer_address & nm_utils_ip4_prefix_to_netmask (obj->plen), buf2) : "");
569
_vt_cmd_plobj_to_string_id (ip6_address, NMPlatformIP6Address, "%d: %s",        obj->ifindex, nm_utils_inet6_ntop (&obj->address, buf1));
570 571
_vt_cmd_plobj_to_string_id (ip4_route,   NMPlatformIP4Route,   "%d: %s/%d %d",  obj->ifindex, nm_utils_inet4_ntop ( obj->network, buf1), obj->plen, obj->metric);
_vt_cmd_plobj_to_string_id (ip6_route,   NMPlatformIP6Route,   "%d: %s/%d %d",  obj->ifindex, nm_utils_inet6_ntop (&obj->network, buf1), obj->plen, obj->metric);
572 573 574 575

int
nmp_object_cmp (const NMPObject *obj1, const NMPObject *obj2)
{
576 577
	const NMPClass *klass1, *klass2;

578 579 580 581 582 583 584 585 586 587
	if (obj1 == obj2)
		return 0;
	if (!obj1)
		return -1;
	if (!obj2)
		return 1;

	g_return_val_if_fail (NMP_OBJECT_IS_VALID (obj1), -1);
	g_return_val_if_fail (NMP_OBJECT_IS_VALID (obj2), 1);

588 589
	klass1 = NMP_OBJECT_GET_CLASS (obj1);
	klass2 = NMP_OBJECT_GET_CLASS (obj2);
590

591 592
	if (klass1 != klass2)
		return klass1->obj_type < klass2->obj_type ? -1 : 1;
593

594 595 596
	if (klass1->cmd_obj_cmp)
		return klass1->cmd_obj_cmp (obj1, obj2);
	return klass1->cmd_plobj_cmp (&obj1->object, &obj2->object);
597 598
}

599 600
static int
_vt_cmd_obj_cmp_link (const NMPObject *obj1, const NMPObject *obj2)
601
{
602 603 604 605 606 607 608
	int i;

	i = nm_platform_link_cmp (&obj1->link, &obj2->link);
	if (i)
		return i;
	if (obj1->_link.netlink.is_in_netlink != obj2->_link.netlink.is_in_netlink)
		return obj1->_link.netlink.is_in_netlink ? -1 : 1;
609 610 611
	i = nmp_object_cmp (obj1->_link.netlink.lnk, obj2->_link.netlink.lnk);
	if (i)
		return i;
612 613 614 615 616 617 618 619 620 621 622 623 624 625
	if (obj1->_link.udev.device != obj2->_link.udev.device) {
		if (!obj1->_link.udev.device)
			return -1;
		if (!obj2->_link.udev.device)
			return 1;

		/* Only compare based on pointer values. That is ugly because it's not a
		 * stable sort order, but probably udev gives us always the same GUdevDevice
		 * instance.
		 *
		 * Have this check as very last. */
		return (obj1->_link.udev.device < obj2->_link.udev.device) ? -1 : 1;
	}
	return 0;
626 627
}

628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
static int
_vt_cmd_obj_cmp_lnk_vlan (const NMPObject *obj1, const NMPObject *obj2)
{
	int c;

	c = nm_platform_lnk_vlan_cmp (&obj1->lnk_vlan, &obj2->lnk_vlan);
	if (c)
		return c;

	if (obj1->_lnk_vlan.n_ingress_qos_map != obj2->_lnk_vlan.n_ingress_qos_map)
		return obj1->_lnk_vlan.n_ingress_qos_map < obj2->_lnk_vlan.n_ingress_qos_map ? -1 : 1;
	if (obj1->_lnk_vlan.n_egress_qos_map != obj2->_lnk_vlan.n_egress_qos_map)
		return obj1->_lnk_vlan.n_egress_qos_map < obj2->_lnk_vlan.n_egress_qos_map ? -1 : 1;

	c = _vlan_xgress_qos_mappings_cmp (obj1->_lnk_vlan.n_ingress_qos_map, obj1->_lnk_vlan.ingress_qos_map, obj2->_lnk_vlan.ingress_qos_map);
	if (c)
		return c;
	c = _vlan_xgress_qos_mappings_cmp (obj1->_lnk_vlan.n_egress_qos_map, obj1->_lnk_vlan.egress_qos_map, obj2->_lnk_vlan.egress_qos_map);

	return c;
}

650 651
gboolean
nmp_object_equal (const NMPObject *obj1, const NMPObject *obj2)
652
{
653
	return nmp_object_cmp (obj1, obj2) == 0;
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
}

/* @src is a const object, which is not entirely correct for link types, where
 * we increase the ref count for src->_link.udev.device.
 * Hence, nmp_object_copy() can violate the const promise of @src.
 * */
void
nmp_object_copy (NMPObject *dst, const NMPObject *src, gboolean id_only)
{
	g_return_if_fail (NMP_OBJECT_IS_VALID (dst));
	g_return_if_fail (NMP_OBJECT_IS_VALID (src));
	g_return_if_fail (!NMP_OBJECT_IS_STACKINIT (dst));

	if (src != dst) {
		const NMPClass *klass = NMP_OBJECT_GET_CLASS (dst);

		g_return_if_fail (klass == NMP_OBJECT_GET_CLASS (src));

672 673 674 675
		if (id_only) {
			if (klass->cmd_plobj_id_copy)
				klass->cmd_plobj_id_copy (&dst->object, &src->object);
		} else if (klass->cmd_obj_copy)
676
			klass->cmd_obj_copy (dst, src);
677 678
		else
			memcpy (&dst->object, &src->object, klass->sizeof_data);
679 680 681
	}
}

682 683 684 685 686 687
static void
_vt_cmd_obj_copy_link (NMPObject *dst, const NMPObject *src)
{
	if (dst->_link.udev.device != src->_link.udev.device) {
		if (src->_link.udev.device)
			g_object_ref (src->_link.udev.device);
688 689 690 691 692 693 694 695
		if (dst->_link.udev.device)
			g_object_unref (dst->_link.udev.device);
	}
	if (dst->_link.netlink.lnk != src->_link.netlink.lnk) {
		if (src->_link.netlink.lnk)
			nmp_object_ref (src->_link.netlink.lnk);
		if (dst->_link.netlink.lnk)
			nmp_object_unref (dst->_link.netlink.lnk);
696 697 698 699
	}
	dst->_link = src->_link;
}

700 701 702 703 704 705 706 707 708 709 710 711 712 713
static void
_vt_cmd_obj_copy_lnk_vlan (NMPObject *dst, const NMPObject *src)
{
	dst->lnk_vlan = src->lnk_vlan;
	_vlan_xgress_qos_mappings_cpy (&dst->_lnk_vlan.n_ingress_qos_map,
	                               &dst->_lnk_vlan.ingress_qos_map,
	                               src->_lnk_vlan.n_ingress_qos_map,
	                               src->_lnk_vlan.ingress_qos_map);
	_vlan_xgress_qos_mappings_cpy (&dst->_lnk_vlan.n_egress_qos_map,
	                               &dst->_lnk_vlan.egress_qos_map,
	                               src->_lnk_vlan.n_egress_qos_map,
	                               src->_lnk_vlan.egress_qos_map);
}

714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
#define _vt_cmd_plobj_id_copy(type, plat_type, cmd) \
static void \
_vt_cmd_plobj_id_copy_##type (NMPlatformObject *_dst, const NMPlatformObject *_src) \
{ \
	plat_type *const dst = (plat_type *) _dst; \
	const plat_type *const src = (const plat_type *) _src; \
	{ cmd } \
}
_vt_cmd_plobj_id_copy (link, NMPlatformLink, {
	dst->ifindex = src->ifindex;
});
_vt_cmd_plobj_id_copy (ip4_address, NMPlatformIP4Address, {
	dst->ifindex = src->ifindex;
	dst->plen = src->plen;
	dst->address = src->address;
729
	dst->peer_address = src->peer_address;
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
});
_vt_cmd_plobj_id_copy (ip6_address, NMPlatformIP6Address, {
	dst->ifindex = src->ifindex;
	dst->address = src->address;
});
_vt_cmd_plobj_id_copy (ip4_route, NMPlatformIP4Route, {
	dst->ifindex = src->ifindex;
	dst->plen = src->plen;
	dst->metric = src->metric;
	dst->network = src->network;
});
_vt_cmd_plobj_id_copy (ip6_route, NMPlatformIP6Route, {
	dst->ifindex = src->ifindex;
	dst->plen = src->plen;
	dst->metric = src->metric;
	dst->network = src->network;
});

/* Uses internally nmp_object_copy(), hence it also violates the const
 * promise for @obj.
 * */
NMPObject *
nmp_object_clone (const NMPObject *obj, gboolean id_only)
{
	NMPObject *dst;

	if (!obj)
		return NULL;

	g_return_val_if_fail (NMP_OBJECT_IS_VALID (obj), NULL);

	dst = _nmp_object_new_from_class (NMP_OBJECT_GET_CLASS (obj));
	nmp_object_copy (dst, obj, id_only);
	return dst;
}

gboolean
nmp_object_id_equal (const NMPObject *obj1, const NMPObject *obj2)
{
	const NMPClass *klass;

	if (obj1 == obj2)
		return TRUE;
	if (!obj1 || !obj2)
		return FALSE;

	g_return_val_if_fail (NMP_OBJECT_IS_VALID (obj1), FALSE);
	g_return_val_if_fail (NMP_OBJECT_IS_VALID (obj2), FALSE);

	klass = NMP_OBJECT_GET_CLASS (obj1);
	return    klass == NMP_OBJECT_GET_CLASS (obj2)
781
	       && klass->cmd_plobj_id_equal
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
	       && klass->cmd_plobj_id_equal (&obj1->object, &obj2->object);
}

#define _vt_cmd_plobj_id_equal(type, plat_type, cmd) \
static gboolean \
_vt_cmd_plobj_id_equal_##type (const NMPlatformObject *_obj1, const NMPlatformObject *_obj2) \
{ \
	const plat_type *const obj1 = (const plat_type *) _obj1; \
	const plat_type *const obj2 = (const plat_type *) _obj2; \
	return (cmd); \
}
_vt_cmd_plobj_id_equal (link, NMPlatformLink,
                           obj1->ifindex == obj2->ifindex);
_vt_cmd_plobj_id_equal (ip4_address, NMPlatformIP4Address,
                           obj1->ifindex == obj2->ifindex
                        && obj1->plen == obj2->plen
798 799 800
                        && obj1->address == obj2->address
                        /* for IPv4 addresses, you can add the same local address with differing peer-adddress
                         * (IFA_ADDRESS), provided that their net-part differs. */
801
                        && ((obj1->peer_address ^ obj2->peer_address) & nm_utils_ip4_prefix_to_netmask (obj1->plen)) == 0);
802 803
_vt_cmd_plobj_id_equal (ip6_address, NMPlatformIP6Address,
                           obj1->ifindex == obj2->ifindex
804
                        /* for IPv6 addresses, the prefix length is not part of the primary identifier. */
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
                        && IN6_ARE_ADDR_EQUAL (&obj1->address, &obj2->address));
_vt_cmd_plobj_id_equal (ip4_route, NMPlatformIP4Route,
                           obj1->ifindex == obj2->ifindex
                        && obj1->plen == obj2->plen
                        && obj1->metric == obj2->metric
                        && obj1->network == obj2->network);
_vt_cmd_plobj_id_equal (ip6_route, NMPlatformIP6Route,
                           obj1->ifindex == obj2->ifindex
                        && obj1->plen == obj2->plen
                        && obj1->metric == obj2->metric
                        && IN6_ARE_ADDR_EQUAL( &obj1->network, &obj2->network));

guint
nmp_object_id_hash (const NMPObject *obj)
{
820 821
	const NMPClass *klass;

822 823 824 825
	if (!obj)
		return 0;

	g_return_val_if_fail (NMP_OBJECT_IS_VALID (obj), 0);
826 827 828 829 830 831 832 833

	klass = NMP_OBJECT_GET_CLASS (obj);

	if (klass->cmd_plobj_id_hash)
		return klass->cmd_plobj_id_hash (&obj->object);

	/* unhashable objects implement pointer equality. */
	return g_direct_hash (obj);
834 835 836 837 838 839 840 841 842 843 844 845
}

#define _vt_cmd_plobj_id_hash(type, plat_type, cmd) \
static guint \
_vt_cmd_plobj_id_hash_##type (const NMPlatformObject *_obj) \
{ \
	const plat_type *const obj = (const plat_type *) _obj; \
	guint hash; \
	{ cmd; } \
	return hash; \
}
_vt_cmd_plobj_id_hash (link, NMPlatformLink, {
846
	hash = (guint) 3982791431u;
847 848 849
	hash = hash      + ((guint) obj->ifindex);
})
_vt_cmd_plobj_id_hash (ip4_address, NMPlatformIP4Address, {
850
	hash = (guint) 3591309853u;
851 852 853
	hash = hash      + ((guint) obj->ifindex);
	hash = hash * 33 + ((guint) obj->plen);
	hash = hash * 33 + ((guint) obj->address);
854 855

	/* for IPv4 we must also consider the net-part of the peer-address (IFA_ADDRESS) */
856
	hash = hash * 33 + ((guint) (obj->peer_address & nm_utils_ip4_prefix_to_netmask (obj->plen)));
857 858
})
_vt_cmd_plobj_id_hash (ip6_address, NMPlatformIP6Address, {
859
	hash = (guint) 2907861637u;
860
	hash = hash      + ((guint) obj->ifindex);
861
	/* for IPv6 addresses, the prefix length is not part of the primary identifier. */
862 863 864
	hash = hash * 33 + _id_hash_ip6_addr (&obj->address);
})
_vt_cmd_plobj_id_hash (ip4_route, NMPlatformIP4Route, {
865
	hash = (guint) 2569857221u;
866 867 868 869 870 871
	hash = hash      + ((guint) obj->ifindex);
	hash = hash * 33 + ((guint) obj->plen);
	hash = hash * 33 + ((guint) obj->metric);
	hash = hash * 33 + ((guint) obj->network);
})
_vt_cmd_plobj_id_hash (ip6_route, NMPlatformIP6Route, {
872
	hash = (guint) 3999787007u;
873 874 875 876 877 878 879 880 881
	hash = hash      + ((guint) obj->ifindex);
	hash = hash * 33 + ((guint) obj->plen);
	hash = hash * 33 + ((guint) obj->metric);
	hash = hash * 33 + _id_hash_ip6_addr (&obj->network);
})

gboolean
nmp_object_is_alive (const NMPObject *obj)
{
882 883
	const NMPClass *klass;

884 885 886 887
	/* for convenience, allow NULL. */
	if (!obj)
		return FALSE;

888 889 890
	klass = NMP_OBJECT_GET_CLASS (obj);
	return    !klass->cmd_obj_is_alive
	       || klass->cmd_obj_is_alive (obj);
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
}

static gboolean
_vt_cmd_obj_is_alive_link (const NMPObject *obj)
{
	return obj->object.ifindex > 0 && (obj->_link.netlink.is_in_netlink || obj->_link.udev.device);
}

static gboolean
_vt_cmd_obj_is_alive_ipx_address (const NMPObject *obj)
{
	return obj->object.ifindex > 0;
}

static gboolean
_vt_cmd_obj_is_alive_ipx_route (const NMPObject *obj)
{
	/* We want to ignore routes that are RTM_F_CLONED but we still
	 * let nmp_object_from_nl() create such route objects, instead of
	 * returning NULL right away.
	 *
	 * The idea is, that if we have the same route (according to its id)
	 * in the cache with !RTM_F_CLONED, an update that changes the route
	 * to be RTM_F_CLONED must remove the instance.
	 *
	 * If nmp_object_from_nl() would just return NULL, we couldn't look
	 * into the cache to see if it contains a route that now disappears
	 * (because it is cloned).
	 *
	 * Instead we create a dead object, and nmp_cache_update_netlink()
	 * will remove the old version of the update.
	 **/
	return obj->object.ifindex > 0 && (obj->ip_route.source != _NM_IP_CONFIG_SOURCE_RTM_F_CLONED);
}

gboolean
nmp_object_is_visible (const NMPObject *obj)
{
929 930
	const NMPClass *klass;

931 932 933 934
	/* for convenience, allow NULL. */
	if (!obj)
		return FALSE;

935 936
	klass = NMP_OBJECT_GET_CLASS (obj);

937 938 939 940 941
	/* a dead object is never visible. */
	if (   klass->cmd_obj_is_alive
	    && !klass->cmd_obj_is_alive (obj))
		return FALSE;

942 943
	return    !klass->cmd_obj_is_visible
	       || klass->cmd_obj_is_visible (obj);
944 945 946 947 948
}

static gboolean
_vt_cmd_obj_is_visible_link (const NMPObject *obj)
{
949
	return    obj->_link.netlink.is_in_netlink
950
	       && obj->link.name[0];
951 952 953 954
}

/******************************************************************/

955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
#define _STRUCT_SIZE(struct_type, field) \
	(G_STRUCT_OFFSET (struct_type, field) + sizeof (((struct_type *) NULL)->field))

_NM_UTILS_LOOKUP_DEFINE (static, _nmp_cache_id_size_by_type, NMPCacheIdType, guint,
	NM_UTILS_LOOKUP_DEFAULT (({ nm_assert_not_reached (); sizeof (NMPCacheId); })),
	NM_UTILS_LOOKUP_ITEM (NMP_CACHE_ID_TYPE_OBJECT_TYPE,                            _STRUCT_SIZE (NMPCacheId, object_type)),
	NM_UTILS_LOOKUP_ITEM (NMP_CACHE_ID_TYPE_OBJECT_TYPE_VISIBLE_ONLY,               _STRUCT_SIZE (NMPCacheId, object_type)),
	NM_UTILS_LOOKUP_ITEM (NMP_CACHE_ID_TYPE_ROUTES_VISIBLE_NO_DEFAULT,              _STRUCT_SIZE (NMPCacheId, object_type)),
	NM_UTILS_LOOKUP_ITEM (NMP_CACHE_ID_TYPE_ROUTES_VISIBLE_ONLY_DEFAULT,            _STRUCT_SIZE (NMPCacheId, object_type)),
	NM_UTILS_LOOKUP_ITEM (NMP_CACHE_ID_TYPE_ADDRROUTE_VISIBLE_BY_IFINDEX,           _STRUCT_SIZE (NMPCacheId, object_type_by_ifindex)),
	NM_UTILS_LOOKUP_ITEM (NMP_CACHE_ID_TYPE_ROUTES_VISIBLE_BY_IFINDEX_NO_DEFAULT,   _STRUCT_SIZE (NMPCacheId, object_type_by_ifindex)),
	NM_UTILS_LOOKUP_ITEM (NMP_CACHE_ID_TYPE_ROUTES_VISIBLE_BY_IFINDEX_ONLY_DEFAULT, _STRUCT_SIZE (NMPCacheId, object_type_by_ifindex)),
	NM_UTILS_LOOKUP_ITEM (NMP_CACHE_ID_TYPE_LINK_BY_IFNAME,                         _STRUCT_SIZE (NMPCacheId, link_by_ifname)),
	NM_UTILS_LOOKUP_ITEM_IGNORE (NMP_CACHE_ID_TYPE_NONE),
	NM_UTILS_LOOKUP_ITEM_IGNORE (__NMP_CACHE_ID_TYPE_MAX),
);

972 973 974
gboolean
nmp_cache_id_equal (const NMPCacheId *a, const NMPCacheId *b)
{
975 976 977
	if (a->_id_type != b->_id_type)
		return FALSE;
	return memcmp (a, b, _nmp_cache_id_size_by_type (a->_id_type)) == 0;
978 979 980 981 982 983
}

guint
nmp_cache_id_hash (const NMPCacheId *id)
{
	guint hash = 5381;
984
	guint i, n;
985

986 987 988 989 990 991 992
	/* for hashing we only iterate over the actually set bytes and skip the
	 * zero padding at the end (which depends on the type of the id).
	 *
	 * For the equal implementation, we don't care about that and compare the
	 * entire NMPCacheId sized struct. */
	n = _nmp_cache_id_size_by_type (id->_id_type);
	for (i = 0; i < n; i++)
993 994 995 996 997 998 999 1000
		hash = ((hash << 5) + hash) + ((char *) id)[i]; /* hash * 33 + c */
	return hash;
}

NMPCacheId *
nmp_cache_id_clone (const NMPCacheId *id)
{
	NMPCacheId *id2;
1001
	guint n;
1002

1003 1004 1005
	n = _nmp_cache_id_size_by_type (id->_id_type);
	id2 = g_slice_alloc (n);
	memcpy (id2, id, n);
1006 1007 1008 1009 1010 1011
	return id2;
}

void
nmp_cache_id_destroy (NMPCacheId *id)
{
1012 1013 1014 1015
	guint n;

	n = _nmp_cache_id_size_by_type (id->_id_type);
	g_slice_free1 (n, id);
1016 1017 1018 1019 1020 1021
}

/******************************************************************/

NMPCacheId _nmp_cache_id_static;

1022
static void
1023
_nmp_cache_id_init (NMPCacheId *id, NMPCacheIdType id_type)
1024 1025 1026
{
	memset (id, 0, sizeof (NMPCacheId));
	id->_id_type = id_type;
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
}

NMPCacheId *
nmp_cache_id_copy (NMPCacheId *id, const NMPCacheId *src)
{
	guint n;

	memset (id, 0, sizeof (NMPCacheId));
	n = _nmp_cache_id_size_by_type (src->_id_type);
	memcpy (id, src, n);
1037 1038 1039 1040
	return id;
}

NMPCacheId *
1041
nmp_cache_id_init_object_type (NMPCacheId *id, NMPObjectType obj_type, gboolean visible_only)
1042
{
1043 1044 1045
	_nmp_cache_id_init (id, visible_only
	                        ? NMP_CACHE_ID_TYPE_OBJECT_TYPE_VISIBLE_ONLY
	                        : NMP_CACHE_ID_TYPE_OBJECT_TYPE);
1046 1047 1048 1049 1050
	id->object_type.obj_type = obj_type;
	return id;
}

NMPCacheId *
1051
nmp_cache_id_init_addrroute_visible_by_ifindex (NMPCacheId *id,
1052
                                                NMPObjectType obj_type,
1053
                                                int ifindex)
1054
{
1055
	g_return_val_if_fail (NM_IN_SET (obj_type,
1056 1057
	                                 NMP_OBJECT_TYPE_IP4_ADDRESS, NMP_OBJECT_TYPE_IP4_ROUTE,
	                                 NMP_OBJECT_TYPE_IP6_ADDRESS, NMP_OBJECT_TYPE_IP6_ROUTE), NULL);
1058

1059 1060 1061 1062 1063
	if (ifindex <= 0)
		return nmp_cache_id_init_object_type (id, obj_type, TRUE);

	_nmp_cache_id_init (id, NMP_CACHE_ID_TYPE_ADDRROUTE_VISIBLE_BY_IFINDEX);
	id->object_type_by_ifindex.obj_type = obj_type;
1064
	memcpy (&id->object_type_by_ifindex._misaligned_ifindex, &ifindex, sizeof (int));
1065 1066 1067 1068
	return id;
}

NMPCacheId *
1069
nmp_cache_id_init_routes_visible (NMPCacheId *id,
1070
                                  NMPObjectType obj_type,
1071 1072 1073 1074
                                  gboolean with_default,
                                  gboolean with_non_default,
                                  int ifindex)
{
1075
	g_return_val_if_fail (NM_IN_SET (obj_type, NMP_OBJECT_TYPE_IP4_ROUTE, NMP_OBJECT_TYPE_IP6_ROUTE), NULL);
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090

	if (with_default && with_non_default) {
		if (ifindex <= 0)
			return nmp_cache_id_init_object_type (id, obj_type, TRUE);
		return nmp_cache_id_init_addrroute_visible_by_ifindex (id, obj_type, ifindex);
	}

	if (with_default)
		_nmp_cache_id_init (id, NMP_CACHE_ID_TYPE_ROUTES_VISIBLE_BY_IFINDEX_ONLY_DEFAULT);
	else if (with_non_default)
		_nmp_cache_id_init (id, NMP_CACHE_ID_TYPE_ROUTES_VISIBLE_BY_IFINDEX_NO_DEFAULT);
	else
		g_return_val_if_reached (NULL);

	id->object_type_by_ifindex.obj_type = obj_type;
1091
	memcpy (&id->object_type_by_ifindex._misaligned_ifindex, &ifindex, sizeof (int));
1092 1093 1094
	return id;
}

1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
NMPCacheId *
nmp_cache_id_init_link_by_ifname (NMPCacheId *id,
                                  const char *ifname)
{
	gsize l;

	_nmp_cache_id_init (id, NMP_CACHE_ID_TYPE_LINK_BY_IFNAME);

	if (   !ifname
	    || (l = strlen (ifname)) > sizeof (id->link_by_ifname.ifname_short))
		g_return_val_if_reached (id);

	/* the trailing NUL is dropped!! */
	memcpy (id->link_by_ifname.ifname_short, ifname, l);

	return id;
}

1113 1114 1115 1116 1117 1118 1119 1120
/******************************************************************/

static gboolean
_nmp_object_init_cache_id (const NMPObject *obj, NMPCacheIdType id_type, NMPCacheId *id, const NMPCacheId **out_id)
{
	const NMPClass *klass = NMP_OBJECT_GET_CLASS (obj);

	switch (id_type) {
1121 1122 1123 1124 1125 1126 1127 1128 1129
	case NMP_CACHE_ID_TYPE_OBJECT_TYPE:
		*out_id = nmp_cache_id_init_object_type (id, klass->obj_type, FALSE);
		return TRUE;
	case NMP_CACHE_ID_TYPE_OBJECT_TYPE_VISIBLE_ONLY:
		if (nmp_object_is_visible (obj))
			*out_id = nmp_cache_id_init_object_type (id, klass->obj_type, TRUE);
		else
			*out_id = NULL;
		return TRUE;
1130
	default:
1131 1132
		return    klass->cmd_obj_init_cache_id
		       && klass->cmd_obj_init_cache_id (obj, id_type, id, out_id);
1133 1134 1135
	}
}

1136 1137 1138 1139 1140 1141 1142
static const guint8 _supported_cache_ids_link[] = {
	NMP_CACHE_ID_TYPE_OBJECT_TYPE,
	NMP_CACHE_ID_TYPE_OBJECT_TYPE_VISIBLE_ONLY,
	NMP_CACHE_ID_TYPE_LINK_BY_IFNAME,
	0,
};

1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
static gboolean
_vt_cmd_obj_init_cache_id_link (const NMPObject *obj, NMPCacheIdType id_type, NMPCacheId *id, const NMPCacheId **out_id)
{
	switch (id_type) {
	case NMP_CACHE_ID_TYPE_LINK_BY_IFNAME:
		if (obj->link.name[0]) {
			*out_id = nmp_cache_id_init_link_by_ifname (id, obj->link.name);
			return TRUE;
		}
		break;
	default:
		return FALSE;
	}
	*out_id = NULL;
	return TRUE;
}

1160 1161 1162 1163 1164 1165 1166
static const guint8 _supported_cache_ids_ipx_address[] = {
	NMP_CACHE_ID_TYPE_OBJECT_TYPE,
	NMP_CACHE_ID_TYPE_OBJECT_TYPE_VISIBLE_ONLY,
	NMP_CACHE_ID_TYPE_ADDRROUTE_VISIBLE_BY_IFINDEX,
	0,
};

1167
static gboolean
1168
_vt_cmd_obj_init_cache_id_ipx_address (const NMPObject *obj, NMPCacheIdType id_type, NMPCacheId *id, const NMPCacheId **out_id)
1169 1170
{
	switch (id_type) {
1171
	case NMP_CACHE_ID_TYPE_ADDRROUTE_VISIBLE_BY_IFINDEX:
1172
		if (nmp_object_is_visible (obj)) {
1173 1174
			nm_assert (obj->object.ifindex > 0);
			*out_id = nmp_cache_id_init_addrroute_visible_by_ifindex (id, NMP_OBJECT_GET_TYPE (obj), obj->object.ifindex);
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
			return TRUE;
		}
		break;
	default:
		return FALSE;
	}
	*out_id = NULL;
	return TRUE;
}

1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
static const guint8 _supported_cache_ids_ipx_route[] = {
	NMP_CACHE_ID_TYPE_OBJECT_TYPE,
	NMP_CACHE_ID_TYPE_OBJECT_TYPE_VISIBLE_ONLY,
	NMP_CACHE_ID_TYPE_ADDRROUTE_VISIBLE_BY_IFINDEX,
	NMP_CACHE_ID_TYPE_ROUTES_VISIBLE_NO_DEFAULT,
	NMP_CACHE_ID_TYPE_ROUTES_VISIBLE_ONLY_DEFAULT,
	NMP_CACHE_ID_TYPE_ROUTES_VISIBLE_BY_IFINDEX_NO_DEFAULT,
	NMP_CACHE_ID_TYPE_ROUTES_VISIBLE_BY_IFINDEX_ONLY_DEFAULT,
	0,
};

1196
static gboolean
1197
_vt_cmd_obj_init_cache_id_ipx_route (const NMPObject *obj, NMPCacheIdType id_type, NMPCacheId *id, const NMPCacheId **out_id)
1198 1199
{
	switch (id_type) {
1200
	case NMP_CACHE_ID_TYPE_ADDRROUTE_VISIBLE_BY_IFINDEX:
1201
		if (nmp_object_is_visible (obj)) {
1202 1203
			nm_assert (obj->object.ifindex > 0);
			*out_id = nmp_cache_id_init_addrroute_visible_by_ifindex (id, NMP_OBJECT_GET_TYPE (obj), obj->object.ifindex);
1204 1205 1206 1207
			return TRUE;
		}
		break;
	case NMP_CACHE_ID_TYPE_ROUTES_VISIBLE_NO_DEFAULT:
1208
		if (   nmp_object_is_visible  (obj)
1209
		    && !NM_PLATFORM_IP_ROUTE_IS_DEFAULT (&obj->ip_route)) {
1210 1211
			nm_assert (obj->object.ifindex > 0);
			*out_id = nmp_cache_id_init_routes_visible (id, NMP_OBJECT_GET_TYPE (obj), FALSE, TRUE, 0);
1212 1213 1214 1215
			return TRUE;
		}
		break;
	case NMP_CACHE_ID_TYPE_ROUTES_VISIBLE_ONLY_DEFAULT:
1216
		if (   nmp_object_is_visible  (obj)
1217
		    && NM_PLATFORM_IP_ROUTE_IS_DEFAULT (&obj->ip_route)) {
1218 1219
			nm_assert (obj->object.ifindex > 0);
			*out_id = nmp_cache_id_init_routes_visible (id, NMP_OBJECT_GET_TYPE (obj), TRUE, FALSE, 0);
1220 1221 1222
			return TRUE;
		}
		break;
1223
	case NMP_CACHE_ID_TYPE_ROUTES_VISIBLE_BY_IFINDEX_NO_DEFAULT:
1224
		if (   nmp_object_is_visible (obj)
1225
		    && !NM_PLATFORM_IP_ROUTE_IS_DEFAULT (&obj->ip_route)) {
1226 1227
			nm_assert (obj->object.ifindex > 0);
			*out_id = nmp_cache_id_init_routes_visible (id, NMP_OBJECT_GET_TYPE (obj), FALSE, TRUE, obj->object.ifindex);
1228 1229 1230
			return TRUE;
		}
		break;
1231
	case NMP_CACHE_ID_TYPE_ROUTES_VISIBLE_BY_IFINDEX_ONLY_DEFAULT:
1232
		if (   nmp_object_is_visible (obj)
1233
		    && NM_PLATFORM_IP_ROUTE_IS_DEFAULT (&obj->ip_route)) {
1234 1235
			nm_assert (obj->object.ifindex > 0);
			*out_id = nmp_cache_id_init_routes_visible (id, NMP_OBJECT_GET_TYPE (obj), TRUE, FALSE, obj->object.ifindex);
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
			return TRUE;
		}
		break;
	default:
		return FALSE;
	}
	*out_id = NULL;
	return TRUE;
}

/******************************************************************/

gboolean
nmp_cache_use_udev_get (const NMPCache *cache)
{
	g_return_val_if_fail (cache, TRUE);

	return cache->use_udev;
}

/******************************************************************/

/**
 * nmp_cache_link_connected_needs_toggle:
 * @cache: the platform cache
 * @master: the link object, that is checked whether its connected property
 *   needs to be toggled.
 * @potential_slave: (allow-none): an additional link object that is treated
 *   as if it was inside @cache. If given, it shaddows a link in the cache
 *   with the same ifindex.
 * @ignore_slave: (allow-none): if set, the check will pretend that @ignore_slave
 *   is not in the cache.
 *
 * NMPlatformLink has two connected flags: (master->link.flags&IFF_LOWER_UP) (as reported
 * from netlink) and master->link.connected. For bond and bridge master, kernel reports
 * those links as IFF_LOWER_UP if they have no slaves attached. We want to present instead
 * a combined @connected flag that shows masters without slaves as down.
 *
 * Check if the connected flag of @master should be toggled according to the content
 * of @cache (including @potential_slave).
 *
 * Returns: %TRUE, if @master->link.connected should be flipped/toggled.
 **/
gboolean
nmp_cache_link_connected_needs_toggle (const NMPCache *cache, const NMPObject *master, const NMPObject *potential_slave, const NMPObject *ignore_slave)
{
	const NMPlatformLink *const *links;
	gboolean is_lower_up = FALSE;
	guint len, i;

	if (   !master
1287
	    || NMP_OBJECT_GET_TYPE (master) != NMP_OBJECT_TYPE_LINK
1288 1289 1290 1291 1292 1293 1294
	    || master->link.ifindex <= 0
	    || !nmp_object_is_visible (master)
	    || !NM_IN_SET (master->link.type, NM_LINK_TYPE_BRIDGE, NM_LINK_TYPE_BOND))
		return FALSE;

	/* if native IFF_LOWER_UP is down, link.connected must also be down
	 * regardless of the slaves. */
1295
	if (!NM_FLAGS_HAS (master->link.n_ifi_flags, IFF_LOWER_UP))
1296 1297
		return !!master->link.connected;

1298
	if (potential_slave && NMP_OBJECT_GET_TYPE (potential_slave) != NMP_OBJECT_TYPE_LINK)
1299 1300 1301 1302 1303 1304 1305 1306 1307
		potential_slave = NULL;

	if (   potential_slave
	    && nmp_object_is_visible (potential_slave)
	    && potential_slave->link.ifindex > 0
	    && potential_slave->link.master == master->link.ifindex
	    && potential_slave->link.connected) {
		is_lower_up = TRUE;
	} else {
1308
		links = (const NMPlatformLink *const *) nmp_cache_lookup_multi (cache, nmp_cache_id_init_object_type (NMP_CACHE_ID_STATIC, NMP_OBJECT_TYPE_LINK, FALSE), &len);
1309 1310 1311 1312
		for (i = 0; i < len; i++) {
			const NMPlatformLink *link = links[i];
			const NMPObject *obj = NMP_OBJECT_UP_CAST ((NMPlatformObject *) link);

1313
			nm_assert (NMP_OBJECT_GET_TYPE (NMP_OBJECT_UP_CAST ((NMPlatformObject *) link)) == NMP_OBJECT_TYPE_LINK);
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371

			if (   (!potential_slave || potential_slave->link.ifindex != link->ifindex)
			    && ignore_slave != obj
			    && link->ifindex > 0
			    && link->master == master->link.ifindex
			    && nmp_object_is_visible (obj)
			    && link->connected) {
				is_lower_up = TRUE;
				break;
			}
		}
	}
	return !!master->link.connected != is_lower_up;
}

/**
 * nmp_cache_link_connected_needs_toggle_by_ifindex:
 * @cache:
 * @master_ifindex: the ifindex of a potential master that should be checked
 *   whether it needs toggling.
 * @potential_slave: (allow-none): passed to nmp_cache_link_connected_needs_toggle().
 *   It considers @potential_slave as being inside the cache, replacing an existing
 *   link with the same ifindex.
 * @ignore_slave: (allow-onne): passed to nmp_cache_link_connected_needs_toggle().
 *
 * The flag obj->link.connected depends on the state of other links in the
 * @cache. See also nmp_cache_link_connected_needs_toggle(). Given an ifindex
 * of a master, check if the cache contains such a master link that needs
 * toogling of the connected flag.
 *
 * Returns: NULL if there is no master link with ifindex @master_ifindex that should be toggled.
 *   Otherwise, return the link object from inside the cache with the given ifindex.
 *   The connected flag of that master should be toggled.
 */
const NMPObject *
nmp_cache_link_connected_needs_toggle_by_ifindex (const NMPCache *cache, int master_ifindex, const NMPObject *potential_slave, const NMPObject *ignore_slave)
{
	const NMPObject *master;

	if (master_ifindex > 0) {
		master = nmp_cache_lookup_link (cache, master_ifindex);
		if (nmp_cache_link_connected_needs_toggle (cache, master, potential_slave, ignore_slave))
			return master;
	}
	return NULL;
}

/******************************************************************/

const NMPlatformObject *const *
nmp_cache_lookup_multi (const NMPCache *cache, const NMPCacheId *cache_id, guint *out_len)
{
	return (const NMPlatformObject *const *) nm_multi_index_lookup (cache->idx_multi,
	                                                                (const NMMultiIndexId *) cache_id,
	                                                                out_len);
}

GArray *
1372
nmp_cache_lookup_multi_to_array (const NMPCache *cache, NMPObjectType obj_type, const NMPCacheId *cache_id)
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
{
	const NMPClass *klass = nmp_class_from_type (obj_type);
	guint len, i;
	const NMPlatformObject *const *objects;
	GArray *array;

	g_return_val_if_fail (klass, NULL);

	objects = nmp_cache_lookup_multi (cache, cache_id, &len);
	array = g_array_sized_new (FALSE, FALSE, klass->sizeof_public, len);

	for (i = 0; i < len; i++) {
		nm_assert (NMP_OBJECT_GET_CLASS (NMP_OBJECT_UP_CAST (objects[i])) == klass);
		g_array_append_vals (array, objects[i], 1);
	}
	return array;
}

const NMPObject *
nmp_cache_lookup_obj (const NMPCache *cache, const NMPObject *obj)
{
	g_return_val_if_fail (obj, NULL);

	return g_hash_table_lookup (cache->idx_main, obj);
}

const NMPObject *
nmp_cache_lookup_link (const NMPCache *cache, int ifindex)
{
	NMPObject obj_needle;

	return nmp_cache_lookup_obj (cache, nmp_object_stackinit_id_link (&obj_needle, ifindex));
}

const NMPObject *
nmp_cache_lookup_link_full (const NMPCache *cache,
                            int ifindex,
                            const char *ifname,
                            gboolean visible_only,
                            NMLinkType link_type,
                            NMPObjectMatchFn match_fn,
                            gpointer user_data)
{
	NMPObject obj_needle;
	const NMPObject *obj;
	const NMPlatformObject *const *list;
	guint i, len;
1420
	NMPCacheId cache_id, *p_cache_id;
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434

	if (ifindex > 0) {
		obj = nmp_cache_lookup_obj (cache, nmp_object_stackinit_id_link (&obj_needle, ifindex));

		if (   !obj
		    || (visible_only && !nmp_object_is_visible (obj))
		    || (link_type != NM_LINK_TYPE_NONE && obj->link.type != link_type)
		    || (ifname && strcmp (obj->link.name, ifname))
		    || (match_fn && !match_fn (obj, user_data)))
			return NULL;
		return obj;
	} else if (!ifname && !match_fn)
		return NULL;
	else {
1435 1436 1437 1438 1439 1440 1441 1442
		if (   ifname
		    && strlen (ifname) <= sizeof (cache_id.link_by_ifname.ifname_short)) {
			p_cache_id = nmp_cache_id_init_link_by_ifname (&cache_id, ifname);
			ifname = NULL;
		} else
			p_cache_id = nmp_cache_id_init_object_type (&cache_id, NMP_OBJECT_TYPE_LINK, visible_only);

		list = nmp_cache_lookup_multi (cache, p_cache_id, &len);
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
		for (i = 0; i < len; i++) {
			obj = NMP_OBJECT_UP_CAST (list[i]);

			if (link_type != NM_LINK_TYPE_NONE && obj->link.type != link_type)
				continue;
			if (ifname && strcmp (ifname, obj->link.name))
				continue;
			if (match_fn && !match_fn (obj, user_data))
				continue;

			return obj;
		}
		return NULL;
	}
}

GHashTable *
nmp_cache_lookup_all_to_hash (const NMPCache *cache,
                              NMPCacheId *cache_id,
                              GHashTable *hash)
{
	NMMultiIndexIdIter iter;
	gpointer plobj;

	nm_multi_index_id_iter_init (&iter, cache->idx_multi, (const NMMultiIndexId *) cache_id);

	if (nm_multi_index_id_iter_next (&iter, &plobj)) {
		if (!hash)
			hash = g_hash_table_new_full (NULL, NULL, (GDestroyNotify) nmp_object_unref, NULL);

		do {
			g_hash_table_add (hash, nmp_object_ref (NMP_OBJECT_UP_CAST (plobj)));
		} while (nm_multi_index_id_iter_next (&iter, &plobj));
	}

	return hash;
}

/******************************************************************/

static void
_nmp_cache_update_cache (NMPCache *cache, NMPObject *obj, gboolean remove)
{
1486
	const guint8 *id_type;
1487

1488
	for (id_type = NMP_OBJECT_GET_CLASS (obj)->supported_cache_ids; *id_type; id_type++) {
1489 1490 1491
		NMPCacheId cache_id_storage;
		const NMPCacheId *cache_id;

1492
		if (!_nmp_object_init_cache_id (obj, *id_type, &cache_id_storage, &cache_id))
1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
			continue;
		if (!cache_id)
			continue;

		/* We don't put @obj itself into the multi index, but &obj->object. As of now, all
		 * users expect a pointer to NMPlatformObject, not NMPObject.
		 * You can use NMP_OBJECT_UP_CAST() to retrieve the original @obj pointer.
		 *
		 * If need be, we could determine based on @id_type which pointer we want to store. */

		if (remove) {
			if (!nm_multi_index_remove (cache->idx_multi, &cache_id->base, &obj->object))
				g_assert_not_reached ();
		} else {
			if (!nm_multi_index_add (cache->idx_multi, &cache_id->base, &obj->object))
				g_assert_not_reached ();
		}
	}
}

static void
_nmp_cache_update_add (NMPCache *cache, NMPObject *obj)
{
	nm_assert (!obj->is_cached);
	nmp_object_ref (obj);
	nm_assert (!nm_multi_index_lookup_first_by_value (cache->idx_multi, &obj->object));
1519
	if (!nm_g_hash_table_add (cache->idx_main, obj))
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540
		g_assert_not_reached ();
	obj->is_cached = TRUE;
	_nmp_cache_update_cache (cache, obj, FALSE);
}

static void
_nmp_cache_update_remove (NMPCache *cache, NMPObject *obj)
{
	nm_assert (obj->is_cached);
	_nmp_cache_update_cache (cache, obj, TRUE);
	obj->is_cached = FALSE;
	if (!g_hash_table_remove (cache->idx_main, obj))
		g_assert_not_reached ();

	/* @obj is possibly a dangling pointer at this point. No problem, multi-index doesn't dereference. */
	nm_assert (!nm_multi_index_lookup_first_by_value (cache->idx_multi, &obj->object));
}

static void
_nmp_cache_update_update (NMPCache *cache, NMPObject *obj, const NMPObject *new)
{
1541
	const guint8 *id_type;
1542 1543 1544 1545 1546

	nm_assert (NMP_OBJECT_GET_CLASS (obj) == NMP_OBJECT_GET_CLASS (new));
	nm_assert (obj->is_cached);
	nm_assert (!new->is_cached);

1547
	for (id_type = NMP_OBJECT_GET_CLASS (obj)->supported_cache_ids; *id_type; id_type++) {
1548 1549 1550
		NMPCacheId cache_id_storage_obj, cache_id_storage_new;
		const NMPCacheId *cache_id_obj, *cache_id_new;

1551
		if (!_nmp_object_init_cache_id (obj, *id_type, &cache_id_storage_obj, &cache_id_obj))
1552
			continue;
1553
		if (!_nmp_object_init_cache_id (new, *id_type, &cache_id_storage_new, &cache_id_new))
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594
			g_assert_not_reached ();
		if (!nm_multi_index_move (cache->idx_multi, (NMMultiIndexId *) cache_id_obj, (NMMultiIndexId *) cache_id_new, &obj->object))
			g_assert_not_reached ();
	}
	nmp_object_copy (obj, new, FALSE);
}

NMPCacheOpsType
nmp_cache_remove (NMPCache *cache, const NMPObject *obj, gboolean equals_by_ptr, NMPObject **out_obj, gboolean *out_was_visible, NMPCachePreHook pre_hook, gpointer user_data)
{
	NMPObject *old;

	nm_assert (NMP_OBJECT_IS_VALID (obj));

	old = g_hash_table_lookup (cache->idx_main, obj);
	if (!old) {
		if (out_obj)
			*out_obj = NULL;
		if (out_was_visible)
			*out_was_visible = FALSE;
		return NMP_CACHE_OPS_UNCHANGED;
	}

	if (out_obj)
		*out_obj = nmp_object_ref (old);
	if (out_was_visible)
		*out_was_visible = nmp_object_is_visible (old);
	if (equals_by_ptr && old != obj) {
		/* We found an identical object, but we only delete it if it's the same pointer as
		 * @obj. */
		return NMP_CACHE_OPS_UNCHANGED;
	}
	if (pre_hook)
		pre_hook (cache, old, NULL, NMP_CACHE_OPS_REMOVED, user_data);
	_nmp_cache_update_remove (cache, old);
	return NMP_CACHE_OPS_REMOVED;
}

NMPCacheOpsType
nmp_cache_remove_netlink (NMPCache *cache, const NMPObject *obj_needle, NMPObject **out_obj, gboolean *out_was_visible, NMPCachePreHook pre_hook, gpointer user_data)
{
1595
	if (NMP_OBJECT_GET_TYPE (obj_needle) == NMP_OBJECT_TYPE_LINK) {
1596
		NMPObject *old;
1597
		nm_auto_nmpobj NMPObject *obj = NULL;
1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680

		/* For nmp_cache_remove_netlink() we have an incomplete @obj_needle instance to be
		 * removed from netlink. Link objects are alive without being in netlink when they
		 * have a udev-device. All we want to do in this case is clear the netlink.is_in_netlink
		 * flag. */

		old = (NMPObject *) nmp_cache_lookup_link (cache, obj_needle->link.ifindex);
		if (!old) {
			if (out_obj)
				*out_obj = NULL;
			if (out_was_visible)
				*out_was_visible = FALSE;
			return NMP_CACHE_OPS_UNCHANGED;
		}

		if (out_obj)
			*out_obj = nmp_object_ref (old);
		if (out_was_visible)
			*out_was_visible = nmp_object_is_visible (old);

		if (!old->_link.netlink.is_in_netlink) {
			nm_assert (old->_link.udev.device);
			return NMP_CACHE_OPS_UNCHANGED;
		}

		if (!old->_link.udev.device) {
			/* the update would make @old invalid. Remove it. */
			if (pre_hook)
				pre_hook (cache, old, NULL, NMP_CACHE_OPS_REMOVED, user_data);
			_nmp_cache_update_remove (cache, old);
			return NMP_CACHE_OPS_REMOVED;
		}

		obj = nmp_object_clone (old, FALSE);
		obj->_link.netlink.is_in_netlink = FALSE;

		_nmp_object_fixup_link_master_connected (obj, cache);
		_nmp_object_fixup_link_udev_fields (obj, cache->use_udev);

		if (pre_hook)
			pre_hook (cache, old, obj, NMP_CACHE_OPS_UPDATED, user_data);
		_nmp_cache_update_update (cache, old, obj);
		return NMP_CACHE_OPS_UPDATED;
	} else
		return nmp_cache_remove (cache, obj_needle, FALSE, out_obj, out_was_visible, pre_hook, user_data);
}

/**
 * nmp_cache_update_netlink:
 * @cache: the platform cache
 * @obj: a #NMPObject instance as received from netlink and created via
 *    nmp_object_from_nl(). Especially for link, it must not have the udev
 *    replated fields set.
 *    This instance will be modified and might be put into the cache. When
 *    calling nmp_cache_update_netlink() you hand @obj over to the cache.
 *    Except, that the cache will increment the ref count as appropriate. You
 *    must still unref the obj to release your part of the ownership.
 * @out_obj: (allow-none): (out): return the object instance that is inside
 *    the cache. If you specify non %NULL, you must always unref the returned
 *    instance. If the return value indicates that the object was removed,
 *    the object is no longer in the cache. Even if the return value indicates
 *    that the object was unchanged, it will still return @out_obj -- if
 *    such an object is in the cache.
 * @out_was_visible: (allow-none): (out): whether the object was visible before
 *    the update operation.
 * @pre_hook: (allow-none): a callback *before* the object gets updated. You cannot
 *    influence the outcome and must not do anything beyong inspecting the changes.
 * @user_data:
 *
 * Returns: how the cache changed.
 **/
NMPCacheOpsType
nmp_cache_update_netlink (NMPCache *cache, NMPObject *obj, NMPObject **out_obj, gboolean *out_was_visible, NMPCachePreHook pre_hook, gpointer user_data)
{
	NMPObject *old;

	nm_assert (NMP_OBJECT_IS_VALID (obj));
	nm_assert (!NMP_OBJECT_IS_STACKINIT (obj));
	nm_assert (!obj->is_cached);

	/* A link object from netlink must have the udev related fields unset.
	 * We could implement to handle that, but there is no need to support such
	 * a use-case */
1681
	nm_assert (NMP_OBJECT_GET_TYPE (obj) != NMP_OBJECT_TYPE_LINK ||
1682
	           (   !obj->_link.udev.device
1683
	            && !obj->link.driver));
1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695

	old = g_hash_table_lookup (cache->idx_main, obj);

	if (out_obj)
		*out_obj = NULL;
	if (out_was_visible)
		*out_was_visible = FALSE;

	if (!old) {
		if (!nmp_object_is_alive (obj))
			return NMP_CACHE_OPS_UNCHANGED;

1696
		if (NMP_OBJECT_GET_TYPE (obj) == NMP_OBJECT_TYPE_LINK) {
1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721
			_nmp_object_fixup_link_master_connected (obj, cache);
			_nmp_object_fixup_link_udev_fields (obj, cache->use_udev);
		}

		if (out_obj)
			*out_obj = nmp_object_ref (obj);

		if (pre_hook)
			pre_hook (cache, NULL, obj, NMP_CACHE_OPS_ADDED, user_data);
		_nmp_cache_update_add (cache, obj);
		return NMP_CACHE_OPS_ADDED;
	} else if (old == obj) {
		/* updating a cached object inplace is not supported because the object contributes to hash-key
		 * for NMMultiIndex. Modifying an object that is inside NMMultiIndex means that these
		 * keys change.
		 * The problem is, that for a given object NMMultiIndex does not support (efficient)
		 * reverse lookup to get all the NMPCacheIds to which it belongs. If that would be implemented,
		 * it would be possible to implement inplace-update.
		 *
		 * There is an un-optimized reverse lookup via nm_multi_index_iter_init(), but we don't want
		 * that because we might have a large number of indexes to search.
		 *
		 * We could add efficient reverse lookup by adding a reverse index to NMMultiIndex. But that
		 * also adds some cost to support an (uncommon?) usage pattern.
		 *
1722 1723
		 * Instead we just don't support it, instead we expect the user to
		 * create a new instance from netlink.
1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737
		 *
		 * TL;DR: a cached object must never be modified.
		 */
		g_assert_not_reached ();
	} else {
		gboolean is_alive = FALSE;

		nm_assert (old->is_cached);

		if (out_obj)
			*out_obj = nmp_object_ref (old);
		if (out_was_visible)
			*out_was_visible = nmp_object_is_visible (old);