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>
38 #include <net/l3mdev.h>
40 #define DRV_NAME "vrf"
41 #define DRV_VERSION "1.0"
43 #define vrf_is_slave(dev) ((dev)->flags & IFF_SLAVE)
45 #define vrf_master_get_rcu(dev) \
46 ((struct net_device *)rcu_dereference(dev->rx_handler_data))
54 struct u64_stats_sync syncp
;
57 static struct dst_entry
*vrf_ip_check(struct dst_entry
*dst
, u32 cookie
)
62 static int vrf_ip_local_out(struct sk_buff
*skb
)
64 return ip_local_out(skb
);
67 static unsigned int vrf_v4_mtu(const struct dst_entry
*dst
)
69 /* TO-DO: return max ethernet size? */
73 static void vrf_dst_destroy(struct dst_entry
*dst
)
75 /* our dst lives forever - or until the device is closed */
78 static unsigned int vrf_default_advmss(const struct dst_entry
*dst
)
83 static struct dst_ops vrf_dst_ops
= {
85 .local_out
= vrf_ip_local_out
,
86 .check
= vrf_ip_check
,
88 .destroy
= vrf_dst_destroy
,
89 .default_advmss
= vrf_default_advmss
,
92 static bool is_ip_rx_frame(struct sk_buff
*skb
)
94 switch (skb
->protocol
) {
96 case htons(ETH_P_IPV6
):
102 static void vrf_tx_error(struct net_device
*vrf_dev
, struct sk_buff
*skb
)
104 vrf_dev
->stats
.tx_errors
++;
108 /* note: already called with rcu_read_lock */
109 static rx_handler_result_t
vrf_handle_frame(struct sk_buff
**pskb
)
111 struct sk_buff
*skb
= *pskb
;
113 if (is_ip_rx_frame(skb
)) {
114 struct net_device
*dev
= vrf_master_get_rcu(skb
->dev
);
115 struct pcpu_dstats
*dstats
= this_cpu_ptr(dev
->dstats
);
117 u64_stats_update_begin(&dstats
->syncp
);
119 dstats
->rx_bytes
+= skb
->len
;
120 u64_stats_update_end(&dstats
->syncp
);
124 return RX_HANDLER_ANOTHER
;
126 return RX_HANDLER_PASS
;
129 static struct rtnl_link_stats64
*vrf_get_stats64(struct net_device
*dev
,
130 struct rtnl_link_stats64
*stats
)
134 for_each_possible_cpu(i
) {
135 const struct pcpu_dstats
*dstats
;
136 u64 tbytes
, tpkts
, tdrops
, rbytes
, rpkts
;
139 dstats
= per_cpu_ptr(dev
->dstats
, i
);
141 start
= u64_stats_fetch_begin_irq(&dstats
->syncp
);
142 tbytes
= dstats
->tx_bytes
;
143 tpkts
= dstats
->tx_pkts
;
144 tdrops
= dstats
->tx_drps
;
145 rbytes
= dstats
->rx_bytes
;
146 rpkts
= dstats
->rx_pkts
;
147 } while (u64_stats_fetch_retry_irq(&dstats
->syncp
, start
));
148 stats
->tx_bytes
+= tbytes
;
149 stats
->tx_packets
+= tpkts
;
150 stats
->tx_dropped
+= tdrops
;
151 stats
->rx_bytes
+= rbytes
;
152 stats
->rx_packets
+= rpkts
;
157 static netdev_tx_t
vrf_process_v6_outbound(struct sk_buff
*skb
,
158 struct net_device
*dev
)
160 vrf_tx_error(dev
, skb
);
161 return NET_XMIT_DROP
;
164 static int vrf_send_v4_prep(struct sk_buff
*skb
, struct flowi4
*fl4
,
165 struct net_device
*vrf_dev
)
170 rt
= ip_route_output_flow(dev_net(vrf_dev
), fl4
, NULL
);
174 /* TO-DO: what about broadcast ? */
175 if (rt
->rt_type
!= RTN_UNICAST
&& rt
->rt_type
!= RTN_LOCAL
) {
181 skb_dst_set(skb
, &rt
->dst
);
187 static netdev_tx_t
vrf_process_v4_outbound(struct sk_buff
*skb
,
188 struct net_device
*vrf_dev
)
190 struct iphdr
*ip4h
= ip_hdr(skb
);
191 int ret
= NET_XMIT_DROP
;
192 struct flowi4 fl4
= {
193 /* needed to match OIF rule */
194 .flowi4_oif
= vrf_dev
->ifindex
,
195 .flowi4_iif
= LOOPBACK_IFINDEX
,
196 .flowi4_tos
= RT_TOS(ip4h
->tos
),
197 .flowi4_flags
= FLOWI_FLAG_ANYSRC
| FLOWI_FLAG_VRFSRC
|
198 FLOWI_FLAG_SKIP_NH_OIF
,
199 .daddr
= ip4h
->daddr
,
202 if (vrf_send_v4_prep(skb
, &fl4
, vrf_dev
))
206 ip4h
->saddr
= inet_select_addr(skb_dst(skb
)->dev
, 0,
210 ret
= ip_local_out(skb
);
211 if (unlikely(net_xmit_eval(ret
)))
212 vrf_dev
->stats
.tx_errors
++;
214 ret
= NET_XMIT_SUCCESS
;
219 vrf_tx_error(vrf_dev
, skb
);
223 static netdev_tx_t
is_ip_tx_frame(struct sk_buff
*skb
, struct net_device
*dev
)
225 /* strip the ethernet header added for pass through VRF device */
226 __skb_pull(skb
, skb_network_offset(skb
));
228 switch (skb
->protocol
) {
229 case htons(ETH_P_IP
):
230 return vrf_process_v4_outbound(skb
, dev
);
231 case htons(ETH_P_IPV6
):
232 return vrf_process_v6_outbound(skb
, dev
);
234 vrf_tx_error(dev
, skb
);
235 return NET_XMIT_DROP
;
239 static netdev_tx_t
vrf_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
241 netdev_tx_t ret
= is_ip_tx_frame(skb
, dev
);
243 if (likely(ret
== NET_XMIT_SUCCESS
|| ret
== NET_XMIT_CN
)) {
244 struct pcpu_dstats
*dstats
= this_cpu_ptr(dev
->dstats
);
246 u64_stats_update_begin(&dstats
->syncp
);
248 dstats
->tx_bytes
+= skb
->len
;
249 u64_stats_update_end(&dstats
->syncp
);
251 this_cpu_inc(dev
->dstats
->tx_drps
);
257 /* modelled after ip_finish_output2 */
258 static int vrf_finish_output(struct net
*net
, struct sock
*sk
, struct sk_buff
*skb
)
260 struct dst_entry
*dst
= skb_dst(skb
);
261 struct rtable
*rt
= (struct rtable
*)dst
;
262 struct net_device
*dev
= dst
->dev
;
263 unsigned int hh_len
= LL_RESERVED_SPACE(dev
);
264 struct neighbour
*neigh
;
268 /* Be paranoid, rather than too clever. */
269 if (unlikely(skb_headroom(skb
) < hh_len
&& dev
->header_ops
)) {
270 struct sk_buff
*skb2
;
272 skb2
= skb_realloc_headroom(skb
, LL_RESERVED_SPACE(dev
));
278 skb_set_owner_w(skb2
, skb
->sk
);
286 nexthop
= (__force u32
)rt_nexthop(rt
, ip_hdr(skb
)->daddr
);
287 neigh
= __ipv4_neigh_lookup_noref(dev
, nexthop
);
288 if (unlikely(!neigh
))
289 neigh
= __neigh_create(&arp_tbl
, &nexthop
, dev
, false);
291 ret
= dst_neigh_output(dst
, neigh
, skb
);
293 rcu_read_unlock_bh();
295 if (unlikely(ret
< 0))
296 vrf_tx_error(skb
->dev
, skb
);
300 static int vrf_output(struct sock
*sk
, struct sk_buff
*skb
)
302 struct net_device
*dev
= skb_dst(skb
)->dev
;
303 struct net
*net
= dev_net(dev
);
305 IP_UPD_PO_STATS(net
, IPSTATS_MIB_OUT
, skb
->len
);
308 skb
->protocol
= htons(ETH_P_IP
);
310 return NF_HOOK_COND(NFPROTO_IPV4
, NF_INET_POST_ROUTING
,
311 net
, sk
, skb
, NULL
, dev
,
313 !(IPCB(skb
)->flags
& IPSKB_REROUTED
));
316 static void vrf_rtable_destroy(struct net_vrf
*vrf
)
318 struct dst_entry
*dst
= (struct dst_entry
*)vrf
->rth
;
324 static struct rtable
*vrf_rtable_create(struct net_device
*dev
)
326 struct net_vrf
*vrf
= netdev_priv(dev
);
329 rth
= dst_alloc(&vrf_dst_ops
, dev
, 2,
331 (DST_HOST
| DST_NOPOLICY
| DST_NOXFRM
));
333 rth
->dst
.output
= vrf_output
;
334 rth
->rt_genid
= rt_genid_ipv4(dev_net(dev
));
336 rth
->rt_type
= RTN_UNICAST
;
337 rth
->rt_is_input
= 0;
341 rth
->rt_uses_gateway
= 0;
342 rth
->rt_table_id
= vrf
->tb_id
;
343 INIT_LIST_HEAD(&rth
->rt_uncached
);
344 rth
->rt_uncached_list
= NULL
;
350 /**************************** device handling ********************/
352 /* cycle interface to flush neighbor cache and move routes across tables */
353 static void cycle_netdev(struct net_device
*dev
)
355 unsigned int flags
= dev
->flags
;
358 if (!netif_running(dev
))
361 ret
= dev_change_flags(dev
, flags
& ~IFF_UP
);
363 ret
= dev_change_flags(dev
, flags
);
367 "Failed to cycle device %s; route tables might be wrong!\n",
372 static struct slave
*__vrf_find_slave_dev(struct slave_queue
*queue
,
373 struct net_device
*dev
)
375 struct list_head
*head
= &queue
->all_slaves
;
378 list_for_each_entry(slave
, head
, list
) {
379 if (slave
->dev
== dev
)
386 /* inverse of __vrf_insert_slave */
387 static void __vrf_remove_slave(struct slave_queue
*queue
, struct slave
*slave
)
389 list_del(&slave
->list
);
392 static void __vrf_insert_slave(struct slave_queue
*queue
, struct slave
*slave
)
394 list_add(&slave
->list
, &queue
->all_slaves
);
397 static int do_vrf_add_slave(struct net_device
*dev
, struct net_device
*port_dev
)
399 struct slave
*slave
= kzalloc(sizeof(*slave
), GFP_KERNEL
);
400 struct net_vrf
*vrf
= netdev_priv(dev
);
401 struct slave_queue
*queue
= &vrf
->queue
;
407 slave
->dev
= port_dev
;
409 /* register the packet handler for slave ports */
410 ret
= netdev_rx_handler_register(port_dev
, vrf_handle_frame
, dev
);
413 "Device %s failed to register rx_handler\n",
418 ret
= netdev_master_upper_dev_link(port_dev
, dev
);
422 port_dev
->flags
|= IFF_SLAVE
;
423 __vrf_insert_slave(queue
, slave
);
424 cycle_netdev(port_dev
);
429 netdev_rx_handler_unregister(port_dev
);
435 static int vrf_add_slave(struct net_device
*dev
, struct net_device
*port_dev
)
437 if (netif_is_l3_master(port_dev
) || vrf_is_slave(port_dev
))
440 return do_vrf_add_slave(dev
, port_dev
);
443 /* inverse of do_vrf_add_slave */
444 static int do_vrf_del_slave(struct net_device
*dev
, struct net_device
*port_dev
)
446 struct net_vrf
*vrf
= netdev_priv(dev
);
447 struct slave_queue
*queue
= &vrf
->queue
;
450 netdev_upper_dev_unlink(port_dev
, dev
);
451 port_dev
->flags
&= ~IFF_SLAVE
;
453 netdev_rx_handler_unregister(port_dev
);
455 cycle_netdev(port_dev
);
457 slave
= __vrf_find_slave_dev(queue
, port_dev
);
459 __vrf_remove_slave(queue
, slave
);
466 static int vrf_del_slave(struct net_device
*dev
, struct net_device
*port_dev
)
468 return do_vrf_del_slave(dev
, port_dev
);
471 static void vrf_dev_uninit(struct net_device
*dev
)
473 struct net_vrf
*vrf
= netdev_priv(dev
);
474 struct slave_queue
*queue
= &vrf
->queue
;
475 struct list_head
*head
= &queue
->all_slaves
;
476 struct slave
*slave
, *next
;
478 vrf_rtable_destroy(vrf
);
480 list_for_each_entry_safe(slave
, next
, head
, list
)
481 vrf_del_slave(dev
, slave
->dev
);
483 free_percpu(dev
->dstats
);
487 static int vrf_dev_init(struct net_device
*dev
)
489 struct net_vrf
*vrf
= netdev_priv(dev
);
491 INIT_LIST_HEAD(&vrf
->queue
.all_slaves
);
493 dev
->dstats
= netdev_alloc_pcpu_stats(struct pcpu_dstats
);
497 /* create the default dst which points back to us */
498 vrf
->rth
= vrf_rtable_create(dev
);
502 dev
->flags
= IFF_MASTER
| IFF_NOARP
;
507 free_percpu(dev
->dstats
);
513 static const struct net_device_ops vrf_netdev_ops
= {
514 .ndo_init
= vrf_dev_init
,
515 .ndo_uninit
= vrf_dev_uninit
,
516 .ndo_start_xmit
= vrf_xmit
,
517 .ndo_get_stats64
= vrf_get_stats64
,
518 .ndo_add_slave
= vrf_add_slave
,
519 .ndo_del_slave
= vrf_del_slave
,
522 static u32
vrf_fib_table(const struct net_device
*dev
)
524 struct net_vrf
*vrf
= netdev_priv(dev
);
529 static struct rtable
*vrf_get_rtable(const struct net_device
*dev
,
530 const struct flowi4
*fl4
)
532 struct rtable
*rth
= NULL
;
534 if (!(fl4
->flowi4_flags
& FLOWI_FLAG_VRFSRC
)) {
535 struct net_vrf
*vrf
= netdev_priv(dev
);
538 atomic_inc(&rth
->dst
.__refcnt
);
544 static const struct l3mdev_ops vrf_l3mdev_ops
= {
545 .l3mdev_fib_table
= vrf_fib_table
,
546 .l3mdev_get_rtable
= vrf_get_rtable
,
549 static void vrf_get_drvinfo(struct net_device
*dev
,
550 struct ethtool_drvinfo
*info
)
552 strlcpy(info
->driver
, DRV_NAME
, sizeof(info
->driver
));
553 strlcpy(info
->version
, DRV_VERSION
, sizeof(info
->version
));
556 static const struct ethtool_ops vrf_ethtool_ops
= {
557 .get_drvinfo
= vrf_get_drvinfo
,
560 static void vrf_setup(struct net_device
*dev
)
564 /* Initialize the device structure. */
565 dev
->netdev_ops
= &vrf_netdev_ops
;
566 dev
->l3mdev_ops
= &vrf_l3mdev_ops
;
567 dev
->ethtool_ops
= &vrf_ethtool_ops
;
568 dev
->destructor
= free_netdev
;
570 /* Fill in device structure with ethernet-generic values. */
571 eth_hw_addr_random(dev
);
573 /* don't acquire vrf device's netif_tx_lock when transmitting */
574 dev
->features
|= NETIF_F_LLTX
;
576 /* don't allow vrf devices to change network namespaces. */
577 dev
->features
|= NETIF_F_NETNS_LOCAL
;
580 static int vrf_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
582 if (tb
[IFLA_ADDRESS
]) {
583 if (nla_len(tb
[IFLA_ADDRESS
]) != ETH_ALEN
)
585 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
])))
586 return -EADDRNOTAVAIL
;
591 static void vrf_dellink(struct net_device
*dev
, struct list_head
*head
)
593 unregister_netdevice_queue(dev
, head
);
596 static int vrf_newlink(struct net
*src_net
, struct net_device
*dev
,
597 struct nlattr
*tb
[], struct nlattr
*data
[])
599 struct net_vrf
*vrf
= netdev_priv(dev
);
602 if (!data
|| !data
[IFLA_VRF_TABLE
])
605 vrf
->tb_id
= nla_get_u32(data
[IFLA_VRF_TABLE
]);
607 dev
->priv_flags
|= IFF_L3MDEV_MASTER
;
609 err
= register_netdevice(dev
);
620 static size_t vrf_nl_getsize(const struct net_device
*dev
)
622 return nla_total_size(sizeof(u32
)); /* IFLA_VRF_TABLE */
625 static int vrf_fillinfo(struct sk_buff
*skb
,
626 const struct net_device
*dev
)
628 struct net_vrf
*vrf
= netdev_priv(dev
);
630 return nla_put_u32(skb
, IFLA_VRF_TABLE
, vrf
->tb_id
);
633 static const struct nla_policy vrf_nl_policy
[IFLA_VRF_MAX
+ 1] = {
634 [IFLA_VRF_TABLE
] = { .type
= NLA_U32
},
637 static struct rtnl_link_ops vrf_link_ops __read_mostly
= {
639 .priv_size
= sizeof(struct net_vrf
),
641 .get_size
= vrf_nl_getsize
,
642 .policy
= vrf_nl_policy
,
643 .validate
= vrf_validate
,
644 .fill_info
= vrf_fillinfo
,
646 .newlink
= vrf_newlink
,
647 .dellink
= vrf_dellink
,
649 .maxtype
= IFLA_VRF_MAX
,
652 static int vrf_device_event(struct notifier_block
*unused
,
653 unsigned long event
, void *ptr
)
655 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
657 /* only care about unregister events to drop slave references */
658 if (event
== NETDEV_UNREGISTER
) {
659 struct net_device
*vrf_dev
;
661 if (netif_is_l3_master(dev
))
664 vrf_dev
= netdev_master_upper_dev_get(dev
);
665 vrf_del_slave(vrf_dev
, dev
);
671 static struct notifier_block vrf_notifier_block __read_mostly
= {
672 .notifier_call
= vrf_device_event
,
675 static int __init
vrf_init_module(void)
679 vrf_dst_ops
.kmem_cachep
=
680 kmem_cache_create("vrf_ip_dst_cache",
681 sizeof(struct rtable
), 0,
685 if (!vrf_dst_ops
.kmem_cachep
)
688 register_netdevice_notifier(&vrf_notifier_block
);
690 rc
= rtnl_link_register(&vrf_link_ops
);
697 unregister_netdevice_notifier(&vrf_notifier_block
);
698 kmem_cache_destroy(vrf_dst_ops
.kmem_cachep
);
702 static void __exit
vrf_cleanup_module(void)
704 rtnl_link_unregister(&vrf_link_ops
);
705 unregister_netdevice_notifier(&vrf_notifier_block
);
706 kmem_cache_destroy(vrf_dst_ops
.kmem_cachep
);
709 module_init(vrf_init_module
);
710 module_exit(vrf_cleanup_module
);
711 MODULE_AUTHOR("Shrijeet Mukherjee, David Ahern");
712 MODULE_DESCRIPTION("Device driver to instantiate VRF domains");
713 MODULE_LICENSE("GPL");
714 MODULE_ALIAS_RTNL_LINK(DRV_NAME
);
715 MODULE_VERSION(DRV_VERSION
);