Commit 6f19b2c1 authored by Beniamino Galvani's avatar Beniamino Galvani Committed by David S. Miller

vxlan: use generic function for tunnel IPv4 route lookup

The route lookup can be done now via generic function
udp_tunnel_dst_lookup() to replace the custom implementations in
vxlan_get_route().

Note that this patch only touches IPv4, while IPv6 still uses
vxlan6_get_route(). After IPv6 route lookup gets converted as well,
vxlan_xmit_one() can be simplified by removing local variables that
will be passed via "struct ip_tunnel_key", such as remote_ip,
local_ip, flow_flags, label.
Suggested-by: default avatarGuillaume Nault <gnault@redhat.com>
Signed-off-by: default avatarBeniamino Galvani <b.galvani@gmail.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent daa2ba7e
...@@ -2215,57 +2215,6 @@ static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst, ...@@ -2215,57 +2215,6 @@ static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst,
return 0; return 0;
} }
static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan, struct net_device *dev,
struct vxlan_sock *sock4,
struct sk_buff *skb, int oif, u8 tos,
__be32 daddr, __be32 *saddr, __be16 dport, __be16 sport,
__u8 flow_flags, struct dst_cache *dst_cache,
const struct ip_tunnel_info *info)
{
bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
struct rtable *rt = NULL;
struct flowi4 fl4;
if (!sock4)
return ERR_PTR(-EIO);
if (tos && !info)
use_cache = false;
if (use_cache) {
rt = dst_cache_get_ip4(dst_cache, saddr);
if (rt)
return rt;
}
memset(&fl4, 0, sizeof(fl4));
fl4.flowi4_oif = oif;
fl4.flowi4_tos = RT_TOS(tos);
fl4.flowi4_mark = skb->mark;
fl4.flowi4_proto = IPPROTO_UDP;
fl4.daddr = daddr;
fl4.saddr = *saddr;
fl4.fl4_dport = dport;
fl4.fl4_sport = sport;
fl4.flowi4_flags = flow_flags;
rt = ip_route_output_key(vxlan->net, &fl4);
if (!IS_ERR(rt)) {
if (rt->dst.dev == dev) {
netdev_dbg(dev, "circular route to %pI4\n", &daddr);
ip_rt_put(rt);
return ERR_PTR(-ELOOP);
}
*saddr = fl4.saddr;
if (use_cache)
dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr);
} else {
netdev_dbg(dev, "no route to %pI4\n", &daddr);
return ERR_PTR(-ENETUNREACH);
}
return rt;
}
#if IS_ENABLED(CONFIG_IPV6) #if IS_ENABLED(CONFIG_IPV6)
static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan, static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan,
struct net_device *dev, struct net_device *dev,
...@@ -2418,30 +2367,38 @@ void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev, ...@@ -2418,30 +2367,38 @@ void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
{ {
struct dst_cache *dst_cache; struct dst_cache *dst_cache;
struct ip_tunnel_info *info; struct ip_tunnel_info *info;
struct ip_tunnel_key *pkey;
struct ip_tunnel_key key;
struct vxlan_dev *vxlan = netdev_priv(dev); struct vxlan_dev *vxlan = netdev_priv(dev);
const struct iphdr *old_iph = ip_hdr(skb); const struct iphdr *old_iph = ip_hdr(skb);
union vxlan_addr *dst; union vxlan_addr *dst;
union vxlan_addr remote_ip, local_ip; union vxlan_addr remote_ip;
struct vxlan_metadata _md; struct vxlan_metadata _md;
struct vxlan_metadata *md = &_md; struct vxlan_metadata *md = &_md;
unsigned int pkt_len = skb->len; unsigned int pkt_len = skb->len;
__be16 src_port = 0, dst_port; __be16 src_port = 0, dst_port;
struct dst_entry *ndst = NULL; struct dst_entry *ndst = NULL;
__u8 tos, ttl, flow_flags = 0; __u8 tos, ttl;
int ifindex; int ifindex;
int err; int err;
u32 flags = vxlan->cfg.flags; u32 flags = vxlan->cfg.flags;
bool use_cache;
bool udp_sum = false; bool udp_sum = false;
bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev)); bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev));
__be32 vni = 0; __be32 vni = 0;
#if IS_ENABLED(CONFIG_IPV6) #if IS_ENABLED(CONFIG_IPV6)
union vxlan_addr local_ip;
__be32 label; __be32 label;
#endif #endif
info = skb_tunnel_info(skb); info = skb_tunnel_info(skb);
use_cache = ip_tunnel_dst_cache_usable(skb, info);
if (rdst) { if (rdst) {
dst = &rdst->remote_ip; dst = &rdst->remote_ip;
memset(&key, 0, sizeof(key));
pkey = &key;
if (vxlan_addr_any(dst)) { if (vxlan_addr_any(dst)) {
if (did_rsc) { if (did_rsc) {
/* short-circuited back to local bridge */ /* short-circuited back to local bridge */
...@@ -2455,7 +2412,15 @@ void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev, ...@@ -2455,7 +2412,15 @@ void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port; dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
vni = (rdst->remote_vni) ? : default_vni; vni = (rdst->remote_vni) ? : default_vni;
ifindex = rdst->remote_ifindex; ifindex = rdst->remote_ifindex;
local_ip = vxlan->cfg.saddr;
if (dst->sa.sa_family == AF_INET) {
key.u.ipv4.src = vxlan->cfg.saddr.sin.sin_addr.s_addr;
key.u.ipv4.dst = rdst->remote_ip.sin.sin_addr.s_addr;
} else {
key.u.ipv6.src = vxlan->cfg.saddr.sin6.sin6_addr;
key.u.ipv6.dst = rdst->remote_ip.sin6.sin6_addr;
}
dst_cache = &rdst->dst_cache; dst_cache = &rdst->dst_cache;
md->gbp = skb->mark; md->gbp = skb->mark;
if (flags & VXLAN_F_TTL_INHERIT) { if (flags & VXLAN_F_TTL_INHERIT) {
...@@ -2469,12 +2434,15 @@ void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev, ...@@ -2469,12 +2434,15 @@ void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
tos = vxlan->cfg.tos; tos = vxlan->cfg.tos;
if (tos == 1) if (tos == 1)
tos = ip_tunnel_get_dsfield(old_iph, skb); tos = ip_tunnel_get_dsfield(old_iph, skb);
if (tos && !info)
use_cache = false;
if (dst->sa.sa_family == AF_INET) if (dst->sa.sa_family == AF_INET)
udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX); udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX);
else else
udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX); udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
#if IS_ENABLED(CONFIG_IPV6) #if IS_ENABLED(CONFIG_IPV6)
local_ip = vxlan->cfg.saddr;
label = vxlan->cfg.label; label = vxlan->cfg.label;
#endif #endif
} else { } else {
...@@ -2486,14 +2454,15 @@ void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev, ...@@ -2486,14 +2454,15 @@ void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
remote_ip.sa.sa_family = ip_tunnel_info_af(info); remote_ip.sa.sa_family = ip_tunnel_info_af(info);
if (remote_ip.sa.sa_family == AF_INET) { if (remote_ip.sa.sa_family == AF_INET) {
remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst; remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
local_ip.sin.sin_addr.s_addr = info->key.u.ipv4.src;
} else { } else {
remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst; remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
#if IS_ENABLED(CONFIG_IPV6)
local_ip.sin6.sin6_addr = info->key.u.ipv6.src; local_ip.sin6.sin6_addr = info->key.u.ipv6.src;
#endif
} }
dst = &remote_ip; dst = &remote_ip;
pkey = &info->key;
dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port; dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
flow_flags = info->key.flow_flags;
vni = tunnel_id_to_key32(info->key.tun_id); vni = tunnel_id_to_key32(info->key.tun_id);
ifindex = 0; ifindex = 0;
dst_cache = &info->dst_cache; dst_cache = &info->dst_cache;
...@@ -2517,15 +2486,14 @@ void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev, ...@@ -2517,15 +2486,14 @@ void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock); struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
struct rtable *rt; struct rtable *rt;
__be16 df = 0; __be16 df = 0;
__be32 saddr;
if (!ifindex) if (!ifindex)
ifindex = sock4->sock->sk->sk_bound_dev_if; ifindex = sock4->sock->sk->sk_bound_dev_if;
rt = vxlan_get_route(vxlan, dev, sock4, skb, ifindex, tos, rt = udp_tunnel_dst_lookup(skb, dev, vxlan->net, ifindex,
dst->sin.sin_addr.s_addr, &saddr, pkey, src_port, dst_port,
&local_ip.sin.sin_addr.s_addr, tos, use_cache ? dst_cache : NULL);
dst_port, src_port, flow_flags,
dst_cache, info);
if (IS_ERR(rt)) { if (IS_ERR(rt)) {
err = PTR_ERR(rt); err = PTR_ERR(rt);
goto tx_error; goto tx_error;
...@@ -2561,16 +2529,13 @@ void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev, ...@@ -2561,16 +2529,13 @@ void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
} else if (err) { } else if (err) {
if (info) { if (info) {
struct ip_tunnel_info *unclone; struct ip_tunnel_info *unclone;
struct in_addr src, dst;
unclone = skb_tunnel_info_unclone(skb); unclone = skb_tunnel_info_unclone(skb);
if (unlikely(!unclone)) if (unlikely(!unclone))
goto tx_error; goto tx_error;
src = remote_ip.sin.sin_addr; unclone->key.u.ipv4.src = pkey->u.ipv4.dst;
dst = local_ip.sin.sin_addr; unclone->key.u.ipv4.dst = saddr;
unclone->key.u.ipv4.src = src.s_addr;
unclone->key.u.ipv4.dst = dst.s_addr;
} }
vxlan_encap_bypass(skb, vxlan, vxlan, vni, false); vxlan_encap_bypass(skb, vxlan, vxlan, vni, false);
dst_release(ndst); dst_release(ndst);
...@@ -2584,8 +2549,8 @@ void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev, ...@@ -2584,8 +2549,8 @@ void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
if (err < 0) if (err < 0)
goto tx_error; goto tx_error;
udp_tunnel_xmit_skb(rt, sock4->sock->sk, skb, local_ip.sin.sin_addr.s_addr, udp_tunnel_xmit_skb(rt, sock4->sock->sk, skb, saddr,
dst->sin.sin_addr.s_addr, tos, ttl, df, pkey->u.ipv4.dst, tos, ttl, df,
src_port, dst_port, xnet, !udp_sum); src_port, dst_port, xnet, !udp_sum);
#if IS_ENABLED(CONFIG_IPV6) #if IS_ENABLED(CONFIG_IPV6)
} else { } else {
...@@ -3286,11 +3251,14 @@ static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb) ...@@ -3286,11 +3251,14 @@ static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock); struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
struct rtable *rt; struct rtable *rt;
rt = vxlan_get_route(vxlan, dev, sock4, skb, 0, info->key.tos, if (!sock4)
info->key.u.ipv4.dst, return -EIO;
&info->key.u.ipv4.src, dport, sport,
info->key.flow_flags, &info->dst_cache, rt = udp_tunnel_dst_lookup(skb, dev, vxlan->net, 0,
info); &info->key.u.ipv4.src,
&info->key,
sport, dport, info->key.tos,
&info->dst_cache);
if (IS_ERR(rt)) if (IS_ERR(rt))
return PTR_ERR(rt); return PTR_ERR(rt);
ip_rt_put(rt); ip_rt_put(rt);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment