third_party_libnl/lib/cache_mngt.c
Yegor Yefremov cee0b1b50c Add SPDX identifiers
Software Package Data Exchange identifiers help to detect source file
licenses and hence simplify the FOSS compliance process.

Signed-off-by: Yegor Yefremov <yegorslists@googlemail.com>

https://github.com/thom311/libnl/pull/219

---

FILES=(
  lib/addr.c
  lib/attr.c
  lib/cache.c
  lib/cache_mngr.c
  lib/cache_mngt.c
  lib/data.c
  lib/error.c
  lib/fib_lookup/lookup.c
  lib/fib_lookup/request.c
  lib/genl/ctrl.c
  lib/genl/family.c
  lib/genl/genl.c
  lib/genl/mngt.c
  lib/handlers.c
  lib/hash.c
  lib/hashtable.c
  lib/idiag/idiag.c
  lib/idiag/idiag_meminfo_obj.c
  lib/idiag/idiag_msg_obj.c
  lib/idiag/idiag_req_obj.c
  lib/idiag/idiag_vegasinfo_obj.c
  lib/mpls.c
  lib/msg.c
  lib/netfilter/ct.c
  lib/netfilter/ct_obj.c
  lib/netfilter/exp.c
  lib/netfilter/exp_obj.c
  lib/netfilter/log.c
  lib/netfilter/log_msg.c
  lib/netfilter/log_msg_obj.c
  lib/netfilter/log_obj.c
  lib/netfilter/netfilter.c
  lib/netfilter/nfnl.c
  lib/netfilter/queue.c
  lib/netfilter/queue_msg.c
  lib/netfilter/queue_msg_obj.c
  lib/netfilter/queue_obj.c
  lib/nl.c
  lib/object.c
  lib/route/act.c
  lib/route/addr.c
  lib/route/class.c
  lib/route/classid.c
  lib/route/cls.c
  lib/route/link.c
  lib/route/neigh.c
  lib/route/neightbl.c
  lib/route/netconf.c
  lib/route/nexthop.c
  lib/route/nexthop_encap.c
  lib/route/nh_encap_mpls.c
  lib/route/pktloc.c
  lib/route/qdisc.c
  lib/route/route.c
  lib/route/route_obj.c
  lib/route/route_utils.c
  lib/route/rtnl.c
  lib/route/rule.c
  lib/route/tc.c
  lib/socket.c
  lib/utils.c
  lib/version.c
  lib/xfrm/ae.c
  lib/xfrm/lifetime.c
  lib/xfrm/sa.c
  lib/xfrm/selector.c
  lib/xfrm/sp.c
  lib/xfrm/template.c
  src/genl-ctrl-list.c
  src/idiag-socket-details.c
  src/lib/addr.c
  src/lib/class.c
  src/lib/cls.c
  src/lib/ct.c
  src/lib/exp.c
  src/lib/link.c
  src/lib/neigh.c
  src/lib/qdisc.c
  src/lib/route.c
  src/lib/rule.c
  src/lib/tc.c
  src/lib/utils.c
  src/nf-ct-add.c
  src/nf-ct-events.c
  src/nf-ct-list.c
  src/nf-exp-add.c
  src/nf-exp-delete.c
  src/nf-exp-list.c
  src/nf-log.c
  src/nf-monitor.c
  src/nf-queue.c
  src/nl-addr-add.c
  src/nl-addr-delete.c
  src/nl-addr-list.c
  src/nl-class-add.c
  src/nl-class-delete.c
  src/nl-class-list.c
  src/nl-classid-lookup.c
  src/nl-cls-add.c
  src/nl-cls-delete.c
  src/nl-cls-list.c
  src/nl-fib-lookup.c
  src/nl-link-enslave.c
  src/nl-link-ifindex2name.c
  src/nl-link-list.c
  src/nl-link-name2ifindex.c
  src/nl-link-release.c
  src/nl-link-set.c
  src/nl-link-stats.c
  src/nl-list-caches.c
  src/nl-list-sockets.c
  src/nl-monitor.c
  src/nl-neigh-add.c
  src/nl-neigh-delete.c
  src/nl-neigh-list.c
  src/nl-neightbl-list.c
  src/nl-pktloc-lookup.c
  src/nl-qdisc-add.c
  src/nl-qdisc-delete.c
  src/nl-qdisc-list.c
  src/nl-route-add.c
  src/nl-route-delete.c
  src/nl-route-get.c
  src/nl-route-list.c
  src/nl-rule-list.c
  src/nl-tctree-list.c
  src/nl-util-addr.c
)

sed '1s#^#/* SPDX-License-Identifier: LGPL-2.1-only */\n#' "${FILES[@]}" -i
2019-08-12 15:01:16 +02:00

444 lines
9.9 KiB
C

/* SPDX-License-Identifier: LGPL-2.1-only */
/*
* lib/cache_mngt.c Cache Management
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* Copyright (c) 2003-2012 Thomas Graf <tgraf@suug.ch>
*/
/**
* @ingroup core
* @defgroup cache_mngt Caching System
*
* Related sections in the development guide:
* - @core_doc{core_cache, Caching System}
*
* @{
*
* Header
* ------
* ~~~~{.c}
* #include <netlink/cache.h>
* ~~~~
*/
#include <netlink-private/netlink.h>
#include <netlink/netlink.h>
#include <netlink/cache.h>
#include <netlink/utils.h>
static struct nl_cache_ops *cache_ops;
static NL_RW_LOCK(cache_ops_lock);
/**
* @name Cache Operations Sets
* @{
*/
static struct nl_cache_ops *__nl_cache_ops_lookup(const char *name)
{
struct nl_cache_ops *ops;
for (ops = cache_ops; ops; ops = ops->co_next)
if (!strcmp(ops->co_name, name))
return ops;
return NULL;
}
/**
* Increment reference counter
* @arg ops Cache operations
*/
void nl_cache_ops_get(struct nl_cache_ops *ops)
{
ops->co_refcnt++;
}
/**
* Decrement reference counter
* @arg ops Cache operations
*/
void nl_cache_ops_put(struct nl_cache_ops *ops)
{
ops->co_refcnt--;
}
/**
* Lookup cache operations by name
* @arg name name of the cache type
*
* @attention This function is not safe, it does not increment the reference
* counter. Please use nl_cache_ops_lookup_safe().
*
* @return The cache operations or NULL if not found.
*/
struct nl_cache_ops *nl_cache_ops_lookup(const char *name)
{
struct nl_cache_ops *ops;
nl_read_lock(&cache_ops_lock);
ops = __nl_cache_ops_lookup(name);
nl_read_unlock(&cache_ops_lock);
return ops;
}
/**
* Lookup cache operations by name
* @arg name name of the cache type
*
* @note The reference counter of the returned cache operation is incremented
* and must be decremented after use with nl_cache_ops_put().
*
* @return The cache operations or NULL if not found.
*/
struct nl_cache_ops *nl_cache_ops_lookup_safe(const char *name)
{
struct nl_cache_ops *ops;
nl_write_lock(&cache_ops_lock);
if ((ops = __nl_cache_ops_lookup(name)))
nl_cache_ops_get(ops);
nl_write_unlock(&cache_ops_lock);
return ops;
}
static struct nl_cache_ops *__cache_ops_associate(int protocol, int msgtype)
{
int i;
struct nl_cache_ops *ops;
for (ops = cache_ops; ops; ops = ops->co_next) {
if (ops->co_protocol != protocol)
continue;
for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++)
if (ops->co_msgtypes[i].mt_id == msgtype)
return ops;
}
return NULL;
}
/**
* Associate protocol and message type to cache operations
* @arg protocol netlink protocol
* @arg msgtype netlink message type
*
* @attention This function is not safe, it does not increment the reference
* counter. Please use nl_cache_ops_associate_safe().
*
* @see nl_cache_ops_associate_safe()
*
* @return The cache operations or NULL if no match found.
*/
struct nl_cache_ops *nl_cache_ops_associate(int protocol, int msgtype)
{
struct nl_cache_ops *ops;
nl_read_lock(&cache_ops_lock);
ops = __cache_ops_associate(protocol, msgtype);
nl_read_unlock(&cache_ops_lock);
return ops;
}
/**
* Associate protocol and message type to cache operations
* @arg protocol netlink protocol
* @arg msgtype netlink message type
*
* Searches the registered cache operations for a matching protocol
* and message type.
*
* @note The reference counter of the returned cache operation is incremented
* and must be decremented after use with nl_cache_ops_put().
*
* @return The cache operations or NULL if no no match was found.
*/
struct nl_cache_ops *nl_cache_ops_associate_safe(int protocol, int msgtype)
{
struct nl_cache_ops *ops;
nl_write_lock(&cache_ops_lock);
if ((ops = __cache_ops_associate(protocol, msgtype)))
nl_cache_ops_get(ops);
nl_write_unlock(&cache_ops_lock);
return ops;
}
/**
* Lookup message type cache association
* @arg ops cache operations
* @arg msgtype netlink message type
*
* Searches for a matching message type association ing the specified
* cache operations.
*
* @attention The guranteed lifetime of the returned message type is bound
* to the lifetime of the underlying cache operations.
*
* @return A message type association or NULL.
*/
struct nl_msgtype *nl_msgtype_lookup(struct nl_cache_ops *ops, int msgtype)
{
int i;
for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++)
if (ops->co_msgtypes[i].mt_id == msgtype)
return &ops->co_msgtypes[i];
return NULL;
}
/* Must hold cache_ops_lock */
static struct nl_cache_ops *cache_ops_lookup_for_obj(struct nl_object_ops *obj_ops)
{
struct nl_cache_ops *ops;
for (ops = cache_ops; ops; ops = ops->co_next)
if (ops->co_obj_ops == obj_ops)
return ops;
return NULL;
}
/**
* Call a function for each registered cache operation
* @arg cb Callback function to be called
* @arg arg User specific argument.
*/
void nl_cache_ops_foreach(void (*cb)(struct nl_cache_ops *, void *), void *arg)
{
struct nl_cache_ops *ops;
nl_read_lock(&cache_ops_lock);
for (ops = cache_ops; ops; ops = ops->co_next)
cb(ops, arg);
nl_read_unlock(&cache_ops_lock);
}
/**
* Set default flags for caches of this type
* @arg ops Cache ops
* @arg flags Flags to set
*
* The cache operation flags will be derived to all caches allocates
* based on this set of cache operations.
*/
void nl_cache_ops_set_flags(struct nl_cache_ops *ops, unsigned int flags)
{
nl_write_lock(&cache_ops_lock);
ops->co_flags |= flags;
nl_write_unlock(&cache_ops_lock);
}
/**
* Register a set of cache operations
* @arg ops cache operations
*
* Called by users of caches to announce the avaibility of
* a certain cache type.
*
* @return 0 on success or a negative error code.
*/
int nl_cache_mngt_register(struct nl_cache_ops *ops)
{
if (!ops->co_name || !ops->co_obj_ops)
return -NLE_INVAL;
/* oo_keygen() also needs oo_compare() */
BUG_ON (ops->co_obj_ops->oo_keygen && !ops->co_obj_ops->oo_compare);
nl_write_lock(&cache_ops_lock);
if (__nl_cache_ops_lookup(ops->co_name)) {
nl_write_unlock(&cache_ops_lock);
return -NLE_EXIST;
}
ops->co_refcnt = 0;
ops->co_next = cache_ops;
cache_ops = ops;
nl_write_unlock(&cache_ops_lock);
NL_DBG(1, "Registered cache operations %s\n", ops->co_name);
return 0;
}
/**
* Unregister a set of cache operations
* @arg ops cache operations
*
* Called by users of caches to announce a set of
* cache operations is no longer available. The
* specified cache operations must have been registered
* previously using nl_cache_mngt_register()
*
* @return 0 on success or a negative error code
*/
int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
{
struct nl_cache_ops *t, **tp;
int err = 0;
nl_write_lock(&cache_ops_lock);
if (ops->co_refcnt > 0) {
err = -NLE_BUSY;
goto errout;
}
for (tp = &cache_ops; (t=*tp) != NULL; tp = &t->co_next)
if (t == ops)
break;
if (!t) {
err = -NLE_NOCACHE;
goto errout;
}
NL_DBG(1, "Unregistered cache operations %s\n", ops->co_name);
*tp = t->co_next;
errout:
nl_write_unlock(&cache_ops_lock);
return err;
}
/** @} */
/**
* @name Global Cache Provisioning/Requiring
* @{
*/
/**
* Provide a cache for global use
* @arg cache cache to provide
*
* Offers the specified cache to be used by other modules.
* Only one cache per type may be shared at a time,
* a previsouly provided caches will be overwritten.
*/
void nl_cache_mngt_provide(struct nl_cache *cache)
{
struct nl_cache_ops *ops;
nl_write_lock(&cache_ops_lock);
ops = cache_ops_lookup_for_obj(cache->c_ops->co_obj_ops);
if (!ops)
BUG();
else {
nl_cache_get(cache);
/*
* Hold a reference to the cache operations to ensure the
* ops don't go away while we use it to store the cache pointer.
*/
if (!ops->co_major_cache)
nl_cache_ops_get(ops);
ops->co_major_cache = cache;
}
nl_write_unlock(&cache_ops_lock);
}
/**
* Unprovide a cache for global use
* @arg cache cache to unprovide
*
* Cancels the offer to use a cache globally. The
* cache will no longer be returned via lookups but
* may still be in use.
*/
void nl_cache_mngt_unprovide(struct nl_cache *cache)
{
struct nl_cache_ops *ops;
nl_write_lock(&cache_ops_lock);
ops = cache_ops_lookup_for_obj(cache->c_ops->co_obj_ops);
if (!ops)
BUG();
else if (ops->co_major_cache == cache) {
nl_cache_free(ops->co_major_cache);
nl_cache_ops_put(ops);
ops->co_major_cache = NULL;
}
nl_write_unlock(&cache_ops_lock);
}
struct nl_cache *__nl_cache_mngt_require(const char *name)
{
struct nl_cache_ops *ops;
struct nl_cache *cache = NULL;
ops = nl_cache_ops_lookup_safe(name);
if (ops) {
cache = ops->co_major_cache;
nl_cache_ops_put(ops);
}
return cache;
}
/**
* Return cache previously provided via nl_cache_mngt_provide()
* @arg name Name of cache to lookup
*
* @attention This function is not safe, it does not increment the reference
* counter. Please use nl_cache_mngt_require_safe().
*
* @see nl_cache_mngt_require_safe()
*
* @return Pointer to cache or NULL if none registered
*/
struct nl_cache *nl_cache_mngt_require(const char *name)
{
struct nl_cache *cache;
if (!(cache = __nl_cache_mngt_require(name)))
NL_DBG(1, "Application BUG: Your application must "
"call nl_cache_mngt_provide() and\nprovide a valid "
"%s cache to be used for internal lookups.\nSee the "
" API documentation for more details.\n", name);
return cache;
}
/**
* Return cache previously provided via nl_cache_mngt_provide()
* @arg name Name of cache to lookup
*
* @note The reference counter of the returned cache is incremented
* and must be decremented after use with nl_cache_put().
*
* @return Pointer to cache or NULL if none registered
*/
struct nl_cache *nl_cache_mngt_require_safe(const char *name)
{
struct nl_cache *cache;
if ((cache = nl_cache_mngt_require(name)))
nl_cache_get(cache);
return cache;
}
/** @} */
/** @} */