2 * vrf.c: device driver to encapsulate a VRF space
4 * Copyright (c) 2015 Cumulus Networks. All rights reserved.
5 * Copyright (c) 2015 Shrijeet Mukherjee <shm@cumulusnetworks.com>
6 * Copyright (c) 2015 David Ahern <dsa@cumulusnetworks.com>
8 * Based on dummy, team and ipvlan drivers
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/netdevice.h>
19 #include <linux/etherdevice.h>
21 #include <linux/init.h>
22 #include <linux/moduleparam.h>
23 #include <linux/netfilter.h>
24 #include <linux/rtnetlink.h>
25 #include <net/rtnetlink.h>
26 #include <linux/u64_stats_sync.h>
27 #include <linux/hashtable.h>
29 #include <linux/inetdevice.h>
32 #include <net/ip_fib.h>
33 #include <net/ip6_route.h>
34 #include <net/rtnetlink.h>
35 #include <net/route.h>
36 #include <net/addrconf.h>
37 #include <net/l3mdev.h>
39 #define DRV_NAME "vrf"
40 #define DRV_VERSION "1.0"
42 #define vrf_master_get_rcu(dev) \
43 ((struct net_device *)rcu_dereference(dev->rx_handler_data))
46 struct list_head list
;
47 struct net_device
*dev
;
51 struct list_head all_slaves
;
55 struct slave_queue queue
;
66 struct u64_stats_sync syncp
;
69 static struct dst_entry
*vrf_ip_check(struct dst_entry
*dst
, u32 cookie
)
74 static int vrf_ip_local_out(struct sk_buff
*skb
)
76 return ip_local_out(skb
);
79 static unsigned int vrf_v4_mtu(const struct dst_entry
*dst
)
81 /* TO-DO: return max ethernet size? */
85 static void vrf_dst_destroy(struct dst_entry
*dst
)
87 /* our dst lives forever - or until the device is closed */
90 static unsigned int vrf_default_advmss(const struct dst_entry
*dst
)
95 static struct dst_ops vrf_dst_ops
= {
97 .local_out
= vrf_ip_local_out
,
98 .check
= vrf_ip_check
,
100 .destroy
= vrf_dst_destroy
,
101 .default_advmss
= vrf_default_advmss
,
104 static bool is_ip_rx_frame(struct sk_buff
*skb
)
106 switch (skb
->protocol
) {
107 case htons(ETH_P_IP
):
108 case htons(ETH_P_IPV6
):
114 static void vrf_tx_error(struct net_device
*vrf_dev
, struct sk_buff
*skb
)
116 vrf_dev
->stats
.tx_errors
++;
120 /* note: already called with rcu_read_lock */
121 static rx_handler_result_t
vrf_handle_frame(struct sk_buff
**pskb
)
123 struct sk_buff
*skb
= *pskb
;
125 if (is_ip_rx_frame(skb
)) {
126 struct net_device
*dev
= vrf_master_get_rcu(skb
->dev
);
127 struct pcpu_dstats
*dstats
= this_cpu_ptr(dev
->dstats
);
129 u64_stats_update_begin(&dstats
->syncp
);
131 dstats
->rx_bytes
+= skb
->len
;
132 u64_stats_update_end(&dstats
->syncp
);
136 return RX_HANDLER_ANOTHER
;
138 return RX_HANDLER_PASS
;
141 static struct rtnl_link_stats64
*vrf_get_stats64(struct net_device
*dev
,
142 struct rtnl_link_stats64
*stats
)
146 for_each_possible_cpu(i
) {
147 const struct pcpu_dstats
*dstats
;
148 u64 tbytes
, tpkts
, tdrops
, rbytes
, rpkts
;
151 dstats
= per_cpu_ptr(dev
->dstats
, i
);
153 start
= u64_stats_fetch_begin_irq(&dstats
->syncp
);
154 tbytes
= dstats
->tx_bytes
;
155 tpkts
= dstats
->tx_pkts
;
156 tdrops
= dstats
->tx_drps
;
157 rbytes
= dstats
->rx_bytes
;
158 rpkts
= dstats
->rx_pkts
;
159 } while (u64_stats_fetch_retry_irq(&dstats
->syncp
, start
));
160 stats
->tx_bytes
+= tbytes
;
161 stats
->tx_packets
+= tpkts
;
162 stats
->tx_dropped
+= tdrops
;
163 stats
->rx_bytes
+= rbytes
;
164 stats
->rx_packets
+= rpkts
;
169 static netdev_tx_t
vrf_process_v6_outbound(struct sk_buff
*skb
,
170 struct net_device
*dev
)
172 vrf_tx_error(dev
, skb
);
173 return NET_XMIT_DROP
;
176 static int vrf_send_v4_prep(struct sk_buff
*skb
, struct flowi4
*fl4
,
177 struct net_device
*vrf_dev
)
182 rt
= ip_route_output_flow(dev_net(vrf_dev
), fl4
, NULL
);
186 /* TO-DO: what about broadcast ? */
187 if (rt
->rt_type
!= RTN_UNICAST
&& rt
->rt_type
!= RTN_LOCAL
) {
193 skb_dst_set(skb
, &rt
->dst
);
199 static netdev_tx_t
vrf_process_v4_outbound(struct sk_buff
*skb
,
200 struct net_device
*vrf_dev
)
202 struct iphdr
*ip4h
= ip_hdr(skb
);
203 int ret
= NET_XMIT_DROP
;
204 struct flowi4 fl4
= {
205 /* needed to match OIF rule */
206 .flowi4_oif
= vrf_dev
->ifindex
,
207 .flowi4_iif
= LOOPBACK_IFINDEX
,
208 .flowi4_tos
= RT_TOS(ip4h
->tos
),
209 .flowi4_flags
= FLOWI_FLAG_ANYSRC
| FLOWI_FLAG_L3MDEV_SRC
|
210 FLOWI_FLAG_SKIP_NH_OIF
,
211 .daddr
= ip4h
->daddr
,
214 if (vrf_send_v4_prep(skb
, &fl4
, vrf_dev
))
218 ip4h
->saddr
= inet_select_addr(skb_dst(skb
)->dev
, 0,
222 ret
= ip_local_out(skb
);
223 if (unlikely(net_xmit_eval(ret
)))
224 vrf_dev
->stats
.tx_errors
++;
226 ret
= NET_XMIT_SUCCESS
;
231 vrf_tx_error(vrf_dev
, skb
);
235 static netdev_tx_t
is_ip_tx_frame(struct sk_buff
*skb
, struct net_device
*dev
)
237 /* strip the ethernet header added for pass through VRF device */
238 __skb_pull(skb
, skb_network_offset(skb
));
240 switch (skb
->protocol
) {
241 case htons(ETH_P_IP
):
242 return vrf_process_v4_outbound(skb
, dev
);
243 case htons(ETH_P_IPV6
):
244 return vrf_process_v6_outbound(skb
, dev
);
246 vrf_tx_error(dev
, skb
);
247 return NET_XMIT_DROP
;
251 static netdev_tx_t
vrf_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
253 netdev_tx_t ret
= is_ip_tx_frame(skb
, dev
);
255 if (likely(ret
== NET_XMIT_SUCCESS
|| ret
== NET_XMIT_CN
)) {
256 struct pcpu_dstats
*dstats
= this_cpu_ptr(dev
->dstats
);
258 u64_stats_update_begin(&dstats
->syncp
);
260 dstats
->tx_bytes
+= skb
->len
;
261 u64_stats_update_end(&dstats
->syncp
);
263 this_cpu_inc(dev
->dstats
->tx_drps
);
269 /* modelled after ip_finish_output2 */
270 static int vrf_finish_output(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
272 struct dst_entry
*dst
= skb_dst(skb
);
273 struct rtable
*rt
= (struct rtable
*)dst
;
274 struct net_device
*dev
= dst
->dev
;
275 unsigned int hh_len
= LL_RESERVED_SPACE(dev
);
276 struct neighbour
*neigh
;
280 /* Be paranoid, rather than too clever. */
281 if (unlikely(skb_headroom(skb
) < hh_len
&& dev
->header_ops
)) {
282 struct sk_buff
*skb2
;
284 skb2
= skb_realloc_headroom(skb
, LL_RESERVED_SPACE(dev
));
290 skb_set_owner_w(skb2
, skb
->sk
);
298 nexthop
= (__force u32
)rt_nexthop(rt
, ip_hdr(skb
)->daddr
);
299 neigh
= __ipv4_neigh_lookup_noref(dev
, nexthop
);
300 if (unlikely(!neigh
))
301 neigh
= __neigh_create(&arp_tbl
, &nexthop
, dev
, false);
303 ret
= dst_neigh_output(dst
, neigh
, skb
);
305 rcu_read_unlock_bh();
307 if (unlikely(ret
< 0))
308 vrf_tx_error(skb
->dev
, skb
);
312 static int vrf_output(struct sock
*sk
, struct sk_buff
*skb
)
314 struct net_device
*dev
= skb_dst(skb
)->dev
;
315 struct net
*net
= dev_net(dev
);
317 IP_UPD_PO_STATS(net
, IPSTATS_MIB_OUT
, skb
->len
);
320 skb
->protocol
= htons(ETH_P_IP
);
322 return NF_HOOK_COND(NFPROTO_IPV4
, NF_INET_POST_ROUTING
,
323 net
, sk
, skb
, NULL
, dev
,
325 !(IPCB(skb
)->flags
& IPSKB_REROUTED
));
328 static void vrf_rtable_destroy(struct net_vrf
*vrf
)
330 struct dst_entry
*dst
= (struct dst_entry
*)vrf
->rth
;
336 static struct rtable
*vrf_rtable_create(struct net_device
*dev
)
338 struct net_vrf
*vrf
= netdev_priv(dev
);
341 rth
= dst_alloc(&vrf_dst_ops
, dev
, 2,
343 (DST_HOST
| DST_NOPOLICY
| DST_NOXFRM
));
345 rth
->dst
.output
= vrf_output
;
346 rth
->rt_genid
= rt_genid_ipv4(dev_net(dev
));
348 rth
->rt_type
= RTN_UNICAST
;
349 rth
->rt_is_input
= 0;
353 rth
->rt_uses_gateway
= 0;
354 rth
->rt_table_id
= vrf
->tb_id
;
355 INIT_LIST_HEAD(&rth
->rt_uncached
);
356 rth
->rt_uncached_list
= NULL
;
362 /**************************** device handling ********************/
364 /* cycle interface to flush neighbor cache and move routes across tables */
365 static void cycle_netdev(struct net_device
*dev
)
367 unsigned int flags
= dev
->flags
;
370 if (!netif_running(dev
))
373 ret
= dev_change_flags(dev
, flags
& ~IFF_UP
);
375 ret
= dev_change_flags(dev
, flags
);
379 "Failed to cycle device %s; route tables might be wrong!\n",
384 static struct slave
*__vrf_find_slave_dev(struct slave_queue
*queue
,
385 struct net_device
*dev
)
387 struct list_head
*head
= &queue
->all_slaves
;
390 list_for_each_entry(slave
, head
, list
) {
391 if (slave
->dev
== dev
)
398 /* inverse of __vrf_insert_slave */
399 static void __vrf_remove_slave(struct slave_queue
*queue
, struct slave
*slave
)
401 list_del(&slave
->list
);
404 static void __vrf_insert_slave(struct slave_queue
*queue
, struct slave
*slave
)
406 list_add(&slave
->list
, &queue
->all_slaves
);
409 static int do_vrf_add_slave(struct net_device
*dev
, struct net_device
*port_dev
)
411 struct slave
*slave
= kzalloc(sizeof(*slave
), GFP_KERNEL
);
412 struct net_vrf
*vrf
= netdev_priv(dev
);
413 struct slave_queue
*queue
= &vrf
->queue
;
419 slave
->dev
= port_dev
;
421 /* register the packet handler for slave ports */
422 ret
= netdev_rx_handler_register(port_dev
, vrf_handle_frame
, dev
);
425 "Device %s failed to register rx_handler\n",
430 ret
= netdev_master_upper_dev_link(port_dev
, dev
);
434 port_dev
->priv_flags
|= IFF_L3MDEV_SLAVE
;
435 __vrf_insert_slave(queue
, slave
);
436 cycle_netdev(port_dev
);
441 netdev_rx_handler_unregister(port_dev
);
447 static int vrf_add_slave(struct net_device
*dev
, struct net_device
*port_dev
)
449 if (netif_is_l3_master(port_dev
) || netif_is_l3_slave(port_dev
))
452 return do_vrf_add_slave(dev
, port_dev
);
455 /* inverse of do_vrf_add_slave */
456 static int do_vrf_del_slave(struct net_device
*dev
, struct net_device
*port_dev
)
458 struct net_vrf
*vrf
= netdev_priv(dev
);
459 struct slave_queue
*queue
= &vrf
->queue
;
462 netdev_upper_dev_unlink(port_dev
, dev
);
463 port_dev
->priv_flags
&= ~IFF_L3MDEV_SLAVE
;
465 netdev_rx_handler_unregister(port_dev
);
467 cycle_netdev(port_dev
);
469 slave
= __vrf_find_slave_dev(queue
, port_dev
);
471 __vrf_remove_slave(queue
, slave
);
478 static int vrf_del_slave(struct net_device
*dev
, struct net_device
*port_dev
)
480 return do_vrf_del_slave(dev
, port_dev
);
483 static void vrf_dev_uninit(struct net_device
*dev
)
485 struct net_vrf
*vrf
= netdev_priv(dev
);
486 struct slave_queue
*queue
= &vrf
->queue
;
487 struct list_head
*head
= &queue
->all_slaves
;
488 struct slave
*slave
, *next
;
490 vrf_rtable_destroy(vrf
);
492 list_for_each_entry_safe(slave
, next
, head
, list
)
493 vrf_del_slave(dev
, slave
->dev
);
495 free_percpu(dev
->dstats
);
499 static int vrf_dev_init(struct net_device
*dev
)
501 struct net_vrf
*vrf
= netdev_priv(dev
);
503 INIT_LIST_HEAD(&vrf
->queue
.all_slaves
);
505 dev
->dstats
= netdev_alloc_pcpu_stats(struct pcpu_dstats
);
509 /* create the default dst which points back to us */
510 vrf
->rth
= vrf_rtable_create(dev
);
514 dev
->flags
= IFF_MASTER
| IFF_NOARP
;
519 free_percpu(dev
->dstats
);
525 static const struct net_device_ops vrf_netdev_ops
= {
526 .ndo_init
= vrf_dev_init
,
527 .ndo_uninit
= vrf_dev_uninit
,
528 .ndo_start_xmit
= vrf_xmit
,
529 .ndo_get_stats64
= vrf_get_stats64
,
530 .ndo_add_slave
= vrf_add_slave
,
531 .ndo_del_slave
= vrf_del_slave
,
534 static u32
vrf_fib_table(const struct net_device
*dev
)
536 struct net_vrf
*vrf
= netdev_priv(dev
);
541 static struct rtable
*vrf_get_rtable(const struct net_device
*dev
,
542 const struct flowi4
*fl4
)
544 struct rtable
*rth
= NULL
;
546 if (!(fl4
->flowi4_flags
& FLOWI_FLAG_L3MDEV_SRC
)) {
547 struct net_vrf
*vrf
= netdev_priv(dev
);
550 atomic_inc(&rth
->dst
.__refcnt
);
556 static const struct l3mdev_ops vrf_l3mdev_ops
= {
557 .l3mdev_fib_table
= vrf_fib_table
,
558 .l3mdev_get_rtable
= vrf_get_rtable
,
561 static void vrf_get_drvinfo(struct net_device
*dev
,
562 struct ethtool_drvinfo
*info
)
564 strlcpy(info
->driver
, DRV_NAME
, sizeof(info
->driver
));
565 strlcpy(info
->version
, DRV_VERSION
, sizeof(info
->version
));
568 static const struct ethtool_ops vrf_ethtool_ops
= {
569 .get_drvinfo
= vrf_get_drvinfo
,
572 static void vrf_setup(struct net_device
*dev
)
576 /* Initialize the device structure. */
577 dev
->netdev_ops
= &vrf_netdev_ops
;
578 dev
->l3mdev_ops
= &vrf_l3mdev_ops
;
579 dev
->ethtool_ops
= &vrf_ethtool_ops
;
580 dev
->destructor
= free_netdev
;
582 /* Fill in device structure with ethernet-generic values. */
583 eth_hw_addr_random(dev
);
585 /* don't acquire vrf device's netif_tx_lock when transmitting */
586 dev
->features
|= NETIF_F_LLTX
;
588 /* don't allow vrf devices to change network namespaces. */
589 dev
->features
|= NETIF_F_NETNS_LOCAL
;
592 static int vrf_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
594 if (tb
[IFLA_ADDRESS
]) {
595 if (nla_len(tb
[IFLA_ADDRESS
]) != ETH_ALEN
)
597 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
])))
598 return -EADDRNOTAVAIL
;
603 static void vrf_dellink(struct net_device
*dev
, struct list_head
*head
)
605 unregister_netdevice_queue(dev
, head
);
608 static int vrf_newlink(struct net
*src_net
, struct net_device
*dev
,
609 struct nlattr
*tb
[], struct nlattr
*data
[])
611 struct net_vrf
*vrf
= netdev_priv(dev
);
614 if (!data
|| !data
[IFLA_VRF_TABLE
])
617 vrf
->tb_id
= nla_get_u32(data
[IFLA_VRF_TABLE
]);
619 dev
->priv_flags
|= IFF_L3MDEV_MASTER
;
621 err
= register_netdevice(dev
);
632 static size_t vrf_nl_getsize(const struct net_device
*dev
)
634 return nla_total_size(sizeof(u32
)); /* IFLA_VRF_TABLE */
637 static int vrf_fillinfo(struct sk_buff
*skb
,
638 const struct net_device
*dev
)
640 struct net_vrf
*vrf
= netdev_priv(dev
);
642 return nla_put_u32(skb
, IFLA_VRF_TABLE
, vrf
->tb_id
);
645 static const struct nla_policy vrf_nl_policy
[IFLA_VRF_MAX
+ 1] = {
646 [IFLA_VRF_TABLE
] = { .type
= NLA_U32
},
649 static struct rtnl_link_ops vrf_link_ops __read_mostly
= {
651 .priv_size
= sizeof(struct net_vrf
),
653 .get_size
= vrf_nl_getsize
,
654 .policy
= vrf_nl_policy
,
655 .validate
= vrf_validate
,
656 .fill_info
= vrf_fillinfo
,
658 .newlink
= vrf_newlink
,
659 .dellink
= vrf_dellink
,
661 .maxtype
= IFLA_VRF_MAX
,
664 static int vrf_device_event(struct notifier_block
*unused
,
665 unsigned long event
, void *ptr
)
667 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
669 /* only care about unregister events to drop slave references */
670 if (event
== NETDEV_UNREGISTER
) {
671 struct net_device
*vrf_dev
;
673 if (!netif_is_l3_slave(dev
))
676 vrf_dev
= netdev_master_upper_dev_get(dev
);
677 vrf_del_slave(vrf_dev
, dev
);
683 static struct notifier_block vrf_notifier_block __read_mostly
= {
684 .notifier_call
= vrf_device_event
,
687 static int __init
vrf_init_module(void)
691 vrf_dst_ops
.kmem_cachep
=
692 kmem_cache_create("vrf_ip_dst_cache",
693 sizeof(struct rtable
), 0,
697 if (!vrf_dst_ops
.kmem_cachep
)
700 register_netdevice_notifier(&vrf_notifier_block
);
702 rc
= rtnl_link_register(&vrf_link_ops
);
709 unregister_netdevice_notifier(&vrf_notifier_block
);
710 kmem_cache_destroy(vrf_dst_ops
.kmem_cachep
);
714 static void __exit
vrf_cleanup_module(void)
716 rtnl_link_unregister(&vrf_link_ops
);
717 unregister_netdevice_notifier(&vrf_notifier_block
);
718 kmem_cache_destroy(vrf_dst_ops
.kmem_cachep
);
721 module_init(vrf_init_module
);
722 module_exit(vrf_cleanup_module
);
723 MODULE_AUTHOR("Shrijeet Mukherjee, David Ahern");
724 MODULE_DESCRIPTION("Device driver to instantiate VRF domains");
725 MODULE_LICENSE("GPL");
726 MODULE_ALIAS_RTNL_LINK(DRV_NAME
);
727 MODULE_VERSION(DRV_VERSION
);