Skip to content

Commit 29dd5ec

Browse files
committed
Merge branch 'vrf-perf'
David Ahern says: ==================== net: vrf: performance improvements Device based features for VRF such as qdisc, netfilter and packet captures are implemented by switching the dst on skbuffs to its per-VRF dst. This has the effect of controlling the output function which points a function in the VRF driver. [1] The skb proceeds down the stack with dst->dev pointing to the VRF device. Netfilter, qdisc and tc rules and network taps are evaluated based on this device. Finally, the skb makes it to the vrf_xmit function which resets the dst based on a FIB lookup. The feature comes at cost - between 5 and 10% depending on test (TCP vs UDP, stream vs RR and IPv4 vs IPv6). The main cost is requiring a FIB lookup in the VRF driver for each packet sent through it. The FIB lookup is required because the real dst gets dropped so that the skb can traverse the stack with dst->dev set to the VRF device. All of that is really driven by the qdisc and not replicating the processing of __dev_queue_xmit if a qdisc is set up on the device. But, VRF devices by default do not have a qdisc and really have no need for multiple Tx queues. This means the performance overhead is inflicted upon all users for the potential use case of a qdisc being configured. The overhead can be avoided by checking if the default configuration applies to a specific VRF device before switching the dst. If a device does not have a qdisc, the pass through netfilter hooks and packet taps can be done inline without dropping the dst and thus avoiding the performance penalty. With this change performance overhead of VRF drops to neglible (difference with run-over-run variance) to 3% depending on test type. netperf performance comparison for 3 cases: 1. L3_MASTER_DEVICE compiled out 2. VRF with this patch set 3. current VRF code IPv4 ---- no-l3mdev new-vrf old-vrf TCP_RR 28778 28938* 27169 TCP_CRR 10706 10490 9770 UDP_RR 30750 29813 29256 * Although higher in the final run used for submitting this patch set, I think what this really represents is a neglible performance overhead for VRF with this change (i.e, within the +-1% variance of runs). Most notably the FIB lookups in the Tx path are avoided for TCP_RR. IPv6 ---- no-l3mdev new-vrf old-vrf TCP_RR 29495 29432 27794 TCP_CRR 10520 10338 9870 UDP_RR 26137 27019* 26511 * UDP is consistently better with VRF for two reasons: 1. Source address selection with L3 domains is considering fewer addresses since only addresses on interfaces in the domain are considered for the selection. Specifically, perf-top shows shows ipv6_get_saddr_eval, ipv6_dev_get_saddr and __ipv6_dev_get_saddr running much lower with vrf than without. 2. The VRF table contains all routes (i.e, there are no separate local and main tables per VRF). That means ip6_pol_route_output only has 1 lookup for VRF where it does 2 without it (1 in the local table and 1 in the main table). [1] http://netdevconf.org/1.2/papers/ahern-what-is-l3mdev-paper.pdf ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
2 parents a2d133b + a9ec54d commit 29dd5ec

1 file changed

Lines changed: 152 additions & 20 deletions

File tree

‎drivers/net/vrf.c‎

Lines changed: 152 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,23 @@ static void vrf_get_stats64(struct net_device *dev,
104104
}
105105
}
106106

107+
/* by default VRF devices do not have a qdisc and are expected
108+
* to be created with only a single queue.
109+
*/
110+
static bool qdisc_tx_is_default(const struct net_device *dev)
111+
{
112+
struct netdev_queue *txq;
113+
struct Qdisc *qdisc;
114+
115+
if (dev->num_tx_queues > 1)
116+
return false;
117+
118+
txq = netdev_get_tx_queue(dev, 0);
119+
qdisc = rcu_access_pointer(txq->qdisc);
120+
121+
return !qdisc->enqueue;
122+
}
123+
107124
/* Local traffic destined to local address. Reinsert the packet to rx
108125
* path, similar to loopback handling.
109126
*/
@@ -357,6 +374,29 @@ static netdev_tx_t vrf_xmit(struct sk_buff *skb, struct net_device *dev)
357374
return ret;
358375
}
359376

377+
static int vrf_finish_direct(struct net *net, struct sock *sk,
378+
struct sk_buff *skb)
379+
{
380+
struct net_device *vrf_dev = skb->dev;
381+
382+
if (!list_empty(&vrf_dev->ptype_all) &&
383+
likely(skb_headroom(skb) >= ETH_HLEN)) {
384+
struct ethhdr *eth = (struct ethhdr *)skb_push(skb, ETH_HLEN);
385+
386+
ether_addr_copy(eth->h_source, vrf_dev->dev_addr);
387+
eth_zero_addr(eth->h_dest);
388+
eth->h_proto = skb->protocol;
389+
390+
rcu_read_lock_bh();
391+
dev_queue_xmit_nit(skb, vrf_dev);
392+
rcu_read_unlock_bh();
393+
394+
skb_pull(skb, ETH_HLEN);
395+
}
396+
397+
return 1;
398+
}
399+
360400
#if IS_ENABLED(CONFIG_IPV6)
361401
/* modelled after ip6_finish_output2 */
362402
static int vrf_finish_output6(struct net *net, struct sock *sk,
@@ -405,18 +445,13 @@ static int vrf_output6(struct net *net, struct sock *sk, struct sk_buff *skb)
405445
* packet to go through device based features such as qdisc, netfilter
406446
* hooks and packet sockets with skb->dev set to vrf device.
407447
*/
408-
static struct sk_buff *vrf_ip6_out(struct net_device *vrf_dev,
409-
struct sock *sk,
410-
struct sk_buff *skb)
448+
static struct sk_buff *vrf_ip6_out_redirect(struct net_device *vrf_dev,
449+
struct sk_buff *skb)
411450
{
412451
struct net_vrf *vrf = netdev_priv(vrf_dev);
413452
struct dst_entry *dst = NULL;
414453
struct rt6_info *rt6;
415454

416-
/* don't divert link scope packets */
417-
if (rt6_need_strict(&ipv6_hdr(skb)->daddr))
418-
return skb;
419-
420455
rcu_read_lock();
421456

422457
rt6 = rcu_dereference(vrf->rt6);
@@ -438,6 +473,55 @@ static struct sk_buff *vrf_ip6_out(struct net_device *vrf_dev,
438473
return skb;
439474
}
440475

476+
static int vrf_output6_direct(struct net *net, struct sock *sk,
477+
struct sk_buff *skb)
478+
{
479+
skb->protocol = htons(ETH_P_IPV6);
480+
481+
return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING,
482+
net, sk, skb, NULL, skb->dev,
483+
vrf_finish_direct,
484+
!(IPCB(skb)->flags & IPSKB_REROUTED));
485+
}
486+
487+
static struct sk_buff *vrf_ip6_out_direct(struct net_device *vrf_dev,
488+
struct sock *sk,
489+
struct sk_buff *skb)
490+
{
491+
struct net *net = dev_net(vrf_dev);
492+
int err;
493+
494+
skb->dev = vrf_dev;
495+
496+
err = nf_hook(NFPROTO_IPV6, NF_INET_LOCAL_OUT, net, sk,
497+
skb, NULL, vrf_dev, vrf_output6_direct);
498+
499+
if (likely(err == 1))
500+
err = vrf_output6_direct(net, sk, skb);
501+
502+
/* reset skb device */
503+
if (likely(err == 1))
504+
nf_reset(skb);
505+
else
506+
skb = NULL;
507+
508+
return skb;
509+
}
510+
511+
static struct sk_buff *vrf_ip6_out(struct net_device *vrf_dev,
512+
struct sock *sk,
513+
struct sk_buff *skb)
514+
{
515+
/* don't divert link scope packets */
516+
if (rt6_need_strict(&ipv6_hdr(skb)->daddr))
517+
return skb;
518+
519+
if (qdisc_tx_is_default(vrf_dev))
520+
return vrf_ip6_out_direct(vrf_dev, sk, skb);
521+
522+
return vrf_ip6_out_redirect(vrf_dev, skb);
523+
}
524+
441525
/* holding rtnl */
442526
static void vrf_rt6_release(struct net_device *dev, struct net_vrf *vrf)
443527
{
@@ -607,18 +691,13 @@ static int vrf_output(struct net *net, struct sock *sk, struct sk_buff *skb)
607691
* packet to go through device based features such as qdisc, netfilter
608692
* hooks and packet sockets with skb->dev set to vrf device.
609693
*/
610-
static struct sk_buff *vrf_ip_out(struct net_device *vrf_dev,
611-
struct sock *sk,
612-
struct sk_buff *skb)
694+
static struct sk_buff *vrf_ip_out_redirect(struct net_device *vrf_dev,
695+
struct sk_buff *skb)
613696
{
614697
struct net_vrf *vrf = netdev_priv(vrf_dev);
615698
struct dst_entry *dst = NULL;
616699
struct rtable *rth;
617700

618-
/* don't divert multicast */
619-
if (ipv4_is_multicast(ip_hdr(skb)->daddr))
620-
return skb;
621-
622701
rcu_read_lock();
623702

624703
rth = rcu_dereference(vrf->rth);
@@ -640,6 +719,55 @@ static struct sk_buff *vrf_ip_out(struct net_device *vrf_dev,
640719
return skb;
641720
}
642721

722+
static int vrf_output_direct(struct net *net, struct sock *sk,
723+
struct sk_buff *skb)
724+
{
725+
skb->protocol = htons(ETH_P_IP);
726+
727+
return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING,
728+
net, sk, skb, NULL, skb->dev,
729+
vrf_finish_direct,
730+
!(IPCB(skb)->flags & IPSKB_REROUTED));
731+
}
732+
733+
static struct sk_buff *vrf_ip_out_direct(struct net_device *vrf_dev,
734+
struct sock *sk,
735+
struct sk_buff *skb)
736+
{
737+
struct net *net = dev_net(vrf_dev);
738+
int err;
739+
740+
skb->dev = vrf_dev;
741+
742+
err = nf_hook(NFPROTO_IPV4, NF_INET_LOCAL_OUT, net, sk,
743+
skb, NULL, vrf_dev, vrf_output_direct);
744+
745+
if (likely(err == 1))
746+
err = vrf_output_direct(net, sk, skb);
747+
748+
/* reset skb device */
749+
if (likely(err == 1))
750+
nf_reset(skb);
751+
else
752+
skb = NULL;
753+
754+
return skb;
755+
}
756+
757+
static struct sk_buff *vrf_ip_out(struct net_device *vrf_dev,
758+
struct sock *sk,
759+
struct sk_buff *skb)
760+
{
761+
/* don't divert multicast */
762+
if (ipv4_is_multicast(ip_hdr(skb)->daddr))
763+
return skb;
764+
765+
if (qdisc_tx_is_default(vrf_dev))
766+
return vrf_ip_out_direct(vrf_dev, sk, skb);
767+
768+
return vrf_ip_out_redirect(vrf_dev, skb);
769+
}
770+
643771
/* called with rcu lock held */
644772
static struct sk_buff *vrf_l3_out(struct net_device *vrf_dev,
645773
struct sock *sk,
@@ -980,9 +1108,11 @@ static struct sk_buff *vrf_ip6_rcv(struct net_device *vrf_dev,
9801108
skb->dev = vrf_dev;
9811109
skb->skb_iif = vrf_dev->ifindex;
9821110

983-
skb_push(skb, skb->mac_len);
984-
dev_queue_xmit_nit(skb, vrf_dev);
985-
skb_pull(skb, skb->mac_len);
1111+
if (!list_empty(&vrf_dev->ptype_all)) {
1112+
skb_push(skb, skb->mac_len);
1113+
dev_queue_xmit_nit(skb, vrf_dev);
1114+
skb_pull(skb, skb->mac_len);
1115+
}
9861116

9871117
IP6CB(skb)->flags |= IP6SKB_L3SLAVE;
9881118
}
@@ -1023,9 +1153,11 @@ static struct sk_buff *vrf_ip_rcv(struct net_device *vrf_dev,
10231153

10241154
vrf_rx_stats(vrf_dev, skb->len);
10251155

1026-
skb_push(skb, skb->mac_len);
1027-
dev_queue_xmit_nit(skb, vrf_dev);
1028-
skb_pull(skb, skb->mac_len);
1156+
if (!list_empty(&vrf_dev->ptype_all)) {
1157+
skb_push(skb, skb->mac_len);
1158+
dev_queue_xmit_nit(skb, vrf_dev);
1159+
skb_pull(skb, skb->mac_len);
1160+
}
10291161

10301162
skb = vrf_rcv_nfhook(NFPROTO_IPV4, NF_INET_PRE_ROUTING, skb, vrf_dev);
10311163
out:

0 commit comments

Comments
 (0)