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>
31 #include <net/ip_fib.h>
32 #include <net/ip6_route.h>
33 #include <net/rtnetlink.h>
34 #include <net/route.h>
35 #include <net/addrconf.h>
38 #define DRV_NAME "vrf"
39 #define DRV_VERSION "1.0"
41 #define vrf_is_slave(dev) ((dev)->flags & IFF_SLAVE)
43 #define vrf_master_get_rcu(dev) \
44 ((struct net_device *)rcu_dereference(dev->rx_handler_data))
52 struct u64_stats_sync syncp
;
55 static struct dst_entry
*vrf_ip_check(struct dst_entry
*dst
, u32 cookie
)
60 static int vrf_ip_local_out(struct sk_buff
*skb
)
62 return ip_local_out(skb
);
65 static unsigned int vrf_v4_mtu(const struct dst_entry
*dst
)
67 /* TO-DO: return max ethernet size? */
71 static void vrf_dst_destroy(struct dst_entry
*dst
)
73 /* our dst lives forever - or until the device is closed */
76 static unsigned int vrf_default_advmss(const struct dst_entry
*dst
)
81 static struct dst_ops vrf_dst_ops
= {
83 .local_out
= vrf_ip_local_out
,
84 .check
= vrf_ip_check
,
86 .destroy
= vrf_dst_destroy
,
87 .default_advmss
= vrf_default_advmss
,
90 static bool is_ip_rx_frame(struct sk_buff
*skb
)
92 switch (skb
->protocol
) {
94 case htons(ETH_P_IPV6
):
100 /* note: already called with rcu_read_lock */
101 static rx_handler_result_t
vrf_handle_frame(struct sk_buff
**pskb
)
103 struct sk_buff
*skb
= *pskb
;
105 if (is_ip_rx_frame(skb
)) {
106 struct net_device
*dev
= vrf_master_get_rcu(skb
->dev
);
107 struct pcpu_dstats
*dstats
= this_cpu_ptr(dev
->dstats
);
109 u64_stats_update_begin(&dstats
->syncp
);
111 dstats
->rx_bytes
+= skb
->len
;
112 u64_stats_update_end(&dstats
->syncp
);
116 return RX_HANDLER_ANOTHER
;
118 return RX_HANDLER_PASS
;
121 static struct rtnl_link_stats64
*vrf_get_stats64(struct net_device
*dev
,
122 struct rtnl_link_stats64
*stats
)
126 for_each_possible_cpu(i
) {
127 const struct pcpu_dstats
*dstats
;
128 u64 tbytes
, tpkts
, tdrops
, rbytes
, rpkts
;
131 dstats
= per_cpu_ptr(dev
->dstats
, i
);
133 start
= u64_stats_fetch_begin_irq(&dstats
->syncp
);
134 tbytes
= dstats
->tx_bytes
;
135 tpkts
= dstats
->tx_pkts
;
136 tdrops
= dstats
->tx_drps
;
137 rbytes
= dstats
->rx_bytes
;
138 rpkts
= dstats
->rx_pkts
;
139 } while (u64_stats_fetch_retry_irq(&dstats
->syncp
, start
));
140 stats
->tx_bytes
+= tbytes
;
141 stats
->tx_packets
+= tpkts
;
142 stats
->tx_dropped
+= tdrops
;
143 stats
->rx_bytes
+= rbytes
;
144 stats
->rx_packets
+= rpkts
;
149 static netdev_tx_t
vrf_process_v6_outbound(struct sk_buff
*skb
,
150 struct net_device
*dev
)
155 static int vrf_send_v4_prep(struct sk_buff
*skb
, struct flowi4
*fl4
,
156 struct net_device
*vrf_dev
)
161 rt
= ip_route_output_flow(dev_net(vrf_dev
), fl4
, NULL
);
165 /* TO-DO: what about broadcast ? */
166 if (rt
->rt_type
!= RTN_UNICAST
&& rt
->rt_type
!= RTN_LOCAL
) {
172 skb_dst_set(skb
, &rt
->dst
);
178 static netdev_tx_t
vrf_process_v4_outbound(struct sk_buff
*skb
,
179 struct net_device
*vrf_dev
)
181 struct iphdr
*ip4h
= ip_hdr(skb
);
182 int ret
= NET_XMIT_DROP
;
183 struct flowi4 fl4
= {
184 /* needed to match OIF rule */
185 .flowi4_oif
= vrf_dev
->ifindex
,
186 .flowi4_iif
= LOOPBACK_IFINDEX
,
187 .flowi4_tos
= RT_TOS(ip4h
->tos
),
188 .flowi4_flags
= FLOWI_FLAG_ANYSRC
| FLOWI_FLAG_VRFSRC
,
189 .daddr
= ip4h
->daddr
,
192 if (vrf_send_v4_prep(skb
, &fl4
, vrf_dev
))
196 ip4h
->saddr
= inet_select_addr(skb_dst(skb
)->dev
, 0,
200 ret
= ip_local_out(skb
);
201 if (unlikely(net_xmit_eval(ret
)))
202 vrf_dev
->stats
.tx_errors
++;
204 ret
= NET_XMIT_SUCCESS
;
209 vrf_dev
->stats
.tx_errors
++;
214 static netdev_tx_t
is_ip_tx_frame(struct sk_buff
*skb
, struct net_device
*dev
)
216 switch (skb
->protocol
) {
217 case htons(ETH_P_IP
):
218 return vrf_process_v4_outbound(skb
, dev
);
219 case htons(ETH_P_IPV6
):
220 return vrf_process_v6_outbound(skb
, dev
);
222 return NET_XMIT_DROP
;
226 static netdev_tx_t
vrf_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
228 netdev_tx_t ret
= is_ip_tx_frame(skb
, dev
);
230 if (likely(ret
== NET_XMIT_SUCCESS
|| ret
== NET_XMIT_CN
)) {
231 struct pcpu_dstats
*dstats
= this_cpu_ptr(dev
->dstats
);
233 u64_stats_update_begin(&dstats
->syncp
);
235 dstats
->tx_bytes
+= skb
->len
;
236 u64_stats_update_end(&dstats
->syncp
);
238 this_cpu_inc(dev
->dstats
->tx_drps
);
244 static netdev_tx_t
vrf_finish(struct sock
*sk
, struct sk_buff
*skb
)
246 return dev_queue_xmit(skb
);
249 static int vrf_output(struct sock
*sk
, struct sk_buff
*skb
)
251 struct net_device
*dev
= skb_dst(skb
)->dev
;
253 IP_UPD_PO_STATS(dev_net(dev
), IPSTATS_MIB_OUT
, skb
->len
);
256 skb
->protocol
= htons(ETH_P_IP
);
258 return NF_HOOK_COND(NFPROTO_IPV4
, NF_INET_POST_ROUTING
, sk
, skb
,
261 !(IPCB(skb
)->flags
& IPSKB_REROUTED
));
264 static void vrf_rtable_destroy(struct net_vrf
*vrf
)
266 struct dst_entry
*dst
= (struct dst_entry
*)vrf
->rth
;
272 static struct rtable
*vrf_rtable_create(struct net_device
*dev
)
276 rth
= dst_alloc(&vrf_dst_ops
, dev
, 2,
278 (DST_HOST
| DST_NOPOLICY
| DST_NOXFRM
));
280 rth
->dst
.output
= vrf_output
;
281 rth
->rt_genid
= rt_genid_ipv4(dev_net(dev
));
283 rth
->rt_type
= RTN_UNICAST
;
284 rth
->rt_is_input
= 0;
288 rth
->rt_uses_gateway
= 0;
289 INIT_LIST_HEAD(&rth
->rt_uncached
);
290 rth
->rt_uncached_list
= NULL
;
291 rth
->rt_lwtstate
= NULL
;
297 /**************************** device handling ********************/
299 /* cycle interface to flush neighbor cache and move routes across tables */
300 static void cycle_netdev(struct net_device
*dev
)
302 unsigned int flags
= dev
->flags
;
305 if (!netif_running(dev
))
308 ret
= dev_change_flags(dev
, flags
& ~IFF_UP
);
310 ret
= dev_change_flags(dev
, flags
);
314 "Failed to cycle device %s; route tables might be wrong!\n",
319 static struct slave
*__vrf_find_slave_dev(struct slave_queue
*queue
,
320 struct net_device
*dev
)
322 struct list_head
*head
= &queue
->all_slaves
;
325 list_for_each_entry(slave
, head
, list
) {
326 if (slave
->dev
== dev
)
333 /* inverse of __vrf_insert_slave */
334 static void __vrf_remove_slave(struct slave_queue
*queue
, struct slave
*slave
)
336 list_del(&slave
->list
);
339 static void __vrf_insert_slave(struct slave_queue
*queue
, struct slave
*slave
)
341 list_add(&slave
->list
, &queue
->all_slaves
);
344 static int do_vrf_add_slave(struct net_device
*dev
, struct net_device
*port_dev
)
346 struct net_vrf_dev
*vrf_ptr
= kmalloc(sizeof(*vrf_ptr
), GFP_KERNEL
);
347 struct slave
*slave
= kzalloc(sizeof(*slave
), GFP_KERNEL
);
348 struct slave
*duplicate_slave
;
349 struct net_vrf
*vrf
= netdev_priv(dev
);
350 struct slave_queue
*queue
= &vrf
->queue
;
353 if (!slave
|| !vrf_ptr
)
356 slave
->dev
= port_dev
;
358 vrf_ptr
->ifindex
= dev
->ifindex
;
359 vrf_ptr
->tb_id
= vrf
->tb_id
;
361 duplicate_slave
= __vrf_find_slave_dev(queue
, port_dev
);
362 if (duplicate_slave
) {
367 __vrf_insert_slave(queue
, slave
);
369 /* register the packet handler for slave ports */
370 ret
= netdev_rx_handler_register(port_dev
, vrf_handle_frame
, dev
);
373 "Device %s failed to register rx_handler\n",
378 ret
= netdev_master_upper_dev_link(port_dev
, dev
);
382 port_dev
->flags
|= IFF_SLAVE
;
384 rcu_assign_pointer(port_dev
->vrf_ptr
, vrf_ptr
);
385 cycle_netdev(port_dev
);
390 netdev_rx_handler_unregister(port_dev
);
392 __vrf_remove_slave(queue
, slave
);
399 static int vrf_add_slave(struct net_device
*dev
, struct net_device
*port_dev
)
401 if (!netif_is_vrf(dev
) || netif_is_vrf(port_dev
) ||
402 vrf_is_slave(port_dev
))
405 return do_vrf_add_slave(dev
, port_dev
);
408 /* inverse of do_vrf_add_slave */
409 static int do_vrf_del_slave(struct net_device
*dev
, struct net_device
*port_dev
)
411 struct net_vrf_dev
*vrf_ptr
= rtnl_dereference(port_dev
->vrf_ptr
);
412 struct net_vrf
*vrf
= netdev_priv(dev
);
413 struct slave_queue
*queue
= &vrf
->queue
;
416 RCU_INIT_POINTER(port_dev
->vrf_ptr
, NULL
);
418 netdev_upper_dev_unlink(port_dev
, dev
);
419 port_dev
->flags
&= ~IFF_SLAVE
;
421 netdev_rx_handler_unregister(port_dev
);
423 /* after netdev_rx_handler_unregister for synchronize_rcu */
426 cycle_netdev(port_dev
);
428 slave
= __vrf_find_slave_dev(queue
, port_dev
);
430 __vrf_remove_slave(queue
, slave
);
437 static int vrf_del_slave(struct net_device
*dev
, struct net_device
*port_dev
)
439 if (!netif_is_vrf(dev
))
442 return do_vrf_del_slave(dev
, port_dev
);
445 static void vrf_dev_uninit(struct net_device
*dev
)
447 struct net_vrf
*vrf
= netdev_priv(dev
);
448 struct slave_queue
*queue
= &vrf
->queue
;
449 struct list_head
*head
= &queue
->all_slaves
;
450 struct slave
*slave
, *next
;
452 vrf_rtable_destroy(vrf
);
454 list_for_each_entry_safe(slave
, next
, head
, list
)
455 vrf_del_slave(dev
, slave
->dev
);
457 free_percpu(dev
->dstats
);
461 static int vrf_dev_init(struct net_device
*dev
)
463 struct net_vrf
*vrf
= netdev_priv(dev
);
465 INIT_LIST_HEAD(&vrf
->queue
.all_slaves
);
467 dev
->dstats
= netdev_alloc_pcpu_stats(struct pcpu_dstats
);
471 /* create the default dst which points back to us */
472 vrf
->rth
= vrf_rtable_create(dev
);
476 dev
->flags
= IFF_MASTER
| IFF_NOARP
;
481 free_percpu(dev
->dstats
);
487 static const struct net_device_ops vrf_netdev_ops
= {
488 .ndo_init
= vrf_dev_init
,
489 .ndo_uninit
= vrf_dev_uninit
,
490 .ndo_start_xmit
= vrf_xmit
,
491 .ndo_get_stats64
= vrf_get_stats64
,
492 .ndo_add_slave
= vrf_add_slave
,
493 .ndo_del_slave
= vrf_del_slave
,
496 static void vrf_get_drvinfo(struct net_device
*dev
,
497 struct ethtool_drvinfo
*info
)
499 strlcpy(info
->driver
, DRV_NAME
, sizeof(info
->driver
));
500 strlcpy(info
->version
, DRV_VERSION
, sizeof(info
->version
));
503 static const struct ethtool_ops vrf_ethtool_ops
= {
504 .get_drvinfo
= vrf_get_drvinfo
,
507 static void vrf_setup(struct net_device
*dev
)
511 /* Initialize the device structure. */
512 dev
->netdev_ops
= &vrf_netdev_ops
;
513 dev
->ethtool_ops
= &vrf_ethtool_ops
;
514 dev
->destructor
= free_netdev
;
516 /* Fill in device structure with ethernet-generic values. */
517 eth_hw_addr_random(dev
);
519 /* don't acquire vrf device's netif_tx_lock when transmitting */
520 dev
->features
|= NETIF_F_LLTX
;
522 /* don't allow vrf devices to change network namespaces. */
523 dev
->features
|= NETIF_F_NETNS_LOCAL
;
526 static int vrf_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
528 if (tb
[IFLA_ADDRESS
]) {
529 if (nla_len(tb
[IFLA_ADDRESS
]) != ETH_ALEN
)
531 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
])))
532 return -EADDRNOTAVAIL
;
537 static void vrf_dellink(struct net_device
*dev
, struct list_head
*head
)
539 struct net_vrf_dev
*vrf_ptr
= rtnl_dereference(dev
->vrf_ptr
);
541 RCU_INIT_POINTER(dev
->vrf_ptr
, NULL
);
542 kfree_rcu(vrf_ptr
, rcu
);
543 unregister_netdevice_queue(dev
, head
);
546 static int vrf_newlink(struct net
*src_net
, struct net_device
*dev
,
547 struct nlattr
*tb
[], struct nlattr
*data
[])
549 struct net_vrf
*vrf
= netdev_priv(dev
);
550 struct net_vrf_dev
*vrf_ptr
;
553 if (!data
|| !data
[IFLA_VRF_TABLE
])
556 vrf
->tb_id
= nla_get_u32(data
[IFLA_VRF_TABLE
]);
558 dev
->priv_flags
|= IFF_VRF_MASTER
;
561 vrf_ptr
= kmalloc(sizeof(*dev
->vrf_ptr
), GFP_KERNEL
);
565 vrf_ptr
->ifindex
= dev
->ifindex
;
566 vrf_ptr
->tb_id
= vrf
->tb_id
;
568 err
= register_netdevice(dev
);
572 rcu_assign_pointer(dev
->vrf_ptr
, vrf_ptr
);
582 static size_t vrf_nl_getsize(const struct net_device
*dev
)
584 return nla_total_size(sizeof(u32
)); /* IFLA_VRF_TABLE */
587 static int vrf_fillinfo(struct sk_buff
*skb
,
588 const struct net_device
*dev
)
590 struct net_vrf
*vrf
= netdev_priv(dev
);
592 return nla_put_u32(skb
, IFLA_VRF_TABLE
, vrf
->tb_id
);
595 static const struct nla_policy vrf_nl_policy
[IFLA_VRF_MAX
+ 1] = {
596 [IFLA_VRF_TABLE
] = { .type
= NLA_U32
},
599 static struct rtnl_link_ops vrf_link_ops __read_mostly
= {
601 .priv_size
= sizeof(struct net_vrf
),
603 .get_size
= vrf_nl_getsize
,
604 .policy
= vrf_nl_policy
,
605 .validate
= vrf_validate
,
606 .fill_info
= vrf_fillinfo
,
608 .newlink
= vrf_newlink
,
609 .dellink
= vrf_dellink
,
611 .maxtype
= IFLA_VRF_MAX
,
614 static int vrf_device_event(struct notifier_block
*unused
,
615 unsigned long event
, void *ptr
)
617 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
619 /* only care about unregister events to drop slave references */
620 if (event
== NETDEV_UNREGISTER
) {
621 struct net_vrf_dev
*vrf_ptr
= rtnl_dereference(dev
->vrf_ptr
);
622 struct net_device
*vrf_dev
;
624 if (!vrf_ptr
|| netif_is_vrf(dev
))
627 vrf_dev
= __dev_get_by_index(dev_net(dev
), vrf_ptr
->ifindex
);
629 vrf_del_slave(vrf_dev
, dev
);
635 static struct notifier_block vrf_notifier_block __read_mostly
= {
636 .notifier_call
= vrf_device_event
,
639 static int __init
vrf_init_module(void)
643 vrf_dst_ops
.kmem_cachep
=
644 kmem_cache_create("vrf_ip_dst_cache",
645 sizeof(struct rtable
), 0,
646 SLAB_HWCACHE_ALIGN
| SLAB_PANIC
,
649 if (!vrf_dst_ops
.kmem_cachep
)
652 register_netdevice_notifier(&vrf_notifier_block
);
654 rc
= rtnl_link_register(&vrf_link_ops
);
661 unregister_netdevice_notifier(&vrf_notifier_block
);
662 kmem_cache_destroy(vrf_dst_ops
.kmem_cachep
);
666 static void __exit
vrf_cleanup_module(void)
668 rtnl_link_unregister(&vrf_link_ops
);
669 unregister_netdevice_notifier(&vrf_notifier_block
);
670 kmem_cache_destroy(vrf_dst_ops
.kmem_cachep
);
673 module_init(vrf_init_module
);
674 module_exit(vrf_cleanup_module
);
675 MODULE_AUTHOR("Shrijeet Mukherjee, David Ahern");
676 MODULE_DESCRIPTION("Device driver to instantiate VRF domains");
677 MODULE_LICENSE("GPL");
678 MODULE_ALIAS_RTNL_LINK(DRV_NAME
);
679 MODULE_VERSION(DRV_VERSION
);