vrf.h 3.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
/*
 * include/net/net_vrf.h - adds vrf dev structure definitions
 * Copyright (c) 2015 Cumulus Networks
 *
 * 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 of the License, or
 * (at your option) any later version.
 */

#ifndef __LINUX_NET_VRF_H
#define __LINUX_NET_VRF_H

struct net_vrf_dev {
	struct rcu_head		rcu;
	int                     ifindex; /* ifindex of master dev */
	u32                     tb_id;   /* table id for VRF */
};

struct slave {
	struct list_head	list;
	struct net_device	*dev;
};

struct slave_queue {
	struct list_head	all_slaves;
};

struct net_vrf {
	struct slave_queue	queue;
	struct rtable           *rth;
	u32			tb_id;
};


#if IS_ENABLED(CONFIG_NET_VRF)
/* called with rcu_read_lock() */
static inline int vrf_master_ifindex_rcu(const struct net_device *dev)
{
	struct net_vrf_dev *vrf_ptr;
	int ifindex = 0;

	if (!dev)
		return 0;

46
	if (netif_is_vrf(dev)) {
47
		ifindex = dev->ifindex;
48
	} else {
49 50 51 52 53 54 55 56
		vrf_ptr = rcu_dereference(dev->vrf_ptr);
		if (vrf_ptr)
			ifindex = vrf_ptr->ifindex;
	}

	return ifindex;
}

57 58 59 60 61 62 63 64 65 66 67
static inline int vrf_master_ifindex(const struct net_device *dev)
{
	int ifindex;

	rcu_read_lock();
	ifindex = vrf_master_ifindex_rcu(dev);
	rcu_read_unlock();

	return ifindex;
}

68
/* called with rcu_read_lock */
D
David Ahern 已提交
69
static inline u32 vrf_dev_table_rcu(const struct net_device *dev)
70
{
D
David Ahern 已提交
71
	u32 tb_id = 0;
72 73 74 75 76 77 78 79 80 81 82

	if (dev) {
		struct net_vrf_dev *vrf_ptr;

		vrf_ptr = rcu_dereference(dev->vrf_ptr);
		if (vrf_ptr)
			tb_id = vrf_ptr->tb_id;
	}
	return tb_id;
}

D
David Ahern 已提交
83
static inline u32 vrf_dev_table(const struct net_device *dev)
84
{
D
David Ahern 已提交
85
	u32 tb_id;
86 87 88 89 90 91 92 93

	rcu_read_lock();
	tb_id = vrf_dev_table_rcu(dev);
	rcu_read_unlock();

	return tb_id;
}

D
David Ahern 已提交
94
static inline u32 vrf_dev_table_ifindex(struct net *net, int ifindex)
95 96
{
	struct net_device *dev;
D
David Ahern 已提交
97
	u32 tb_id = 0;
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

	if (!ifindex)
		return 0;

	rcu_read_lock();

	dev = dev_get_by_index_rcu(net, ifindex);
	if (dev)
		tb_id = vrf_dev_table_rcu(dev);

	rcu_read_unlock();

	return tb_id;
}

113
/* called with rtnl */
D
David Ahern 已提交
114
static inline u32 vrf_dev_table_rtnl(const struct net_device *dev)
115
{
D
David Ahern 已提交
116
	u32 tb_id = 0;
117 118 119 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

	if (dev) {
		struct net_vrf_dev *vrf_ptr;

		vrf_ptr = rtnl_dereference(dev->vrf_ptr);
		if (vrf_ptr)
			tb_id = vrf_ptr->tb_id;
	}
	return tb_id;
}

/* caller has already checked netif_is_vrf(dev) */
static inline struct rtable *vrf_dev_get_rth(const struct net_device *dev)
{
	struct rtable *rth = ERR_PTR(-ENETUNREACH);
	struct net_vrf *vrf = netdev_priv(dev);

	if (vrf) {
		rth = vrf->rth;
		atomic_inc(&rth->dst.__refcnt);
	}
	return rth;
}

#else
static inline int vrf_master_ifindex_rcu(const struct net_device *dev)
{
	return 0;
}

147 148 149 150 151
static inline int vrf_master_ifindex(const struct net_device *dev)
{
	return 0;
}

D
David Ahern 已提交
152
static inline u32 vrf_dev_table_rcu(const struct net_device *dev)
153 154 155 156
{
	return 0;
}

D
David Ahern 已提交
157
static inline u32 vrf_dev_table(const struct net_device *dev)
158 159 160 161
{
	return 0;
}

D
David Ahern 已提交
162
static inline u32 vrf_dev_table_ifindex(struct net *net, int ifindex)
163 164 165 166
{
	return 0;
}

D
David Ahern 已提交
167
static inline u32 vrf_dev_table_rtnl(const struct net_device *dev)
168 169 170 171 172 173 174 175 176 177 178
{
	return 0;
}

static inline struct rtable *vrf_dev_get_rth(const struct net_device *dev)
{
	return ERR_PTR(-ENETUNREACH);
}
#endif

#endif /* __LINUX_NET_VRF_H */