net/core/dst_cache.c

Source file repositories/reference/linux-study-clean/net/core/dst_cache.c

File Facts

System
Linux kernel
Corpus path
net/core/dst_cache.c
Extension
.c
Size
4801 bytes
Lines
211
Domain
Networking Core
Bucket
Sockets, Protocols, Packet Path, And Network Policy
Inferred role
Networking Core: exported/initcall integration point
Status
integration implementation candidate

Why This File Exists

Networking stack implementation surface: socket APIs, protocol dispatch, packet flow, routing, filtering, and network namespaces.

Dependency Surface

Detected Declarations

Annotated Snippet

struct dst_cache_pcpu {
	unsigned long refresh_ts;
	struct dst_entry *dst;
	local_lock_t bh_lock;
	u32 cookie;
	union {
		struct in_addr in_saddr;
		struct in6_addr in6_saddr;
	};
};

static void dst_cache_per_cpu_dst_set(struct dst_cache_pcpu *dst_cache,
				      struct dst_entry *dst, u32 cookie)
{
	DEBUG_NET_WARN_ON_ONCE(!in_softirq());
	dst_release(dst_cache->dst);
	if (dst)
		dst_hold(dst);

	dst_cache->cookie = cookie;
	dst_cache->dst = dst;
}

static struct dst_entry *dst_cache_per_cpu_get(struct dst_cache *dst_cache,
					       struct dst_cache_pcpu *idst)
{
	struct dst_entry *dst;

	DEBUG_NET_WARN_ON_ONCE(!in_softirq());
	dst = idst->dst;
	if (!dst)
		goto fail;

	/* the cache already hold a dst reference; it can't go away */
	dst_hold(dst);

	if (unlikely(!time_after(idst->refresh_ts,
				 READ_ONCE(dst_cache->reset_ts)) ||
		     (READ_ONCE(dst->obsolete) && !dst->ops->check(dst, idst->cookie)))) {
		dst_cache_per_cpu_dst_set(idst, NULL, 0);
		dst_release(dst);
		goto fail;
	}
	return dst;

fail:
	idst->refresh_ts = jiffies;
	return NULL;
}

struct dst_entry *dst_cache_get(struct dst_cache *dst_cache)
{
	struct dst_entry *dst;

	if (!dst_cache->cache)
		return NULL;

	local_lock_nested_bh(&dst_cache->cache->bh_lock);
	dst = dst_cache_per_cpu_get(dst_cache, this_cpu_ptr(dst_cache->cache));
	local_unlock_nested_bh(&dst_cache->cache->bh_lock);
	return dst;
}
EXPORT_SYMBOL_GPL(dst_cache_get);

struct rtable *dst_cache_get_ip4(struct dst_cache *dst_cache, __be32 *saddr)
{
	struct dst_cache_pcpu *idst;
	struct dst_entry *dst;

	if (!dst_cache->cache)
		return NULL;

	local_lock_nested_bh(&dst_cache->cache->bh_lock);
	idst = this_cpu_ptr(dst_cache->cache);
	dst = dst_cache_per_cpu_get(dst_cache, idst);
	if (!dst) {
		local_unlock_nested_bh(&dst_cache->cache->bh_lock);
		return NULL;
	}

	*saddr = idst->in_saddr.s_addr;
	local_unlock_nested_bh(&dst_cache->cache->bh_lock);
	return dst_rtable(dst);
}
EXPORT_SYMBOL_GPL(dst_cache_get_ip4);

void dst_cache_set_ip4(struct dst_cache *dst_cache, struct dst_entry *dst,
		       __be32 saddr)
{
	struct dst_cache_pcpu *idst;

Annotation

Implementation Notes