blob: e7094fbd75685998bb5ee492367e686c133a2e15 [file] [log] [blame]
David Ahern193125d2015-08-13 14:59:10 -06001/*
2 * vrf.c: device driver to encapsulate a VRF space
3 *
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>
7 *
8 * Based on dummy, team and ipvlan drivers
9 *
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.
14 */
15
16#include <linux/module.h>
17#include <linux/kernel.h>
18#include <linux/netdevice.h>
19#include <linux/etherdevice.h>
20#include <linux/ip.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>
28
29#include <linux/inetdevice.h>
David Ahern8f583362015-08-27 10:10:50 -070030#include <net/arp.h>
David Ahern193125d2015-08-13 14:59:10 -060031#include <net/ip.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/vrf.h>
38
39#define DRV_NAME "vrf"
40#define DRV_VERSION "1.0"
41
42#define vrf_is_slave(dev) ((dev)->flags & IFF_SLAVE)
43
44#define vrf_master_get_rcu(dev) \
45 ((struct net_device *)rcu_dereference(dev->rx_handler_data))
46
47struct pcpu_dstats {
48 u64 tx_pkts;
49 u64 tx_bytes;
50 u64 tx_drps;
51 u64 rx_pkts;
52 u64 rx_bytes;
53 struct u64_stats_sync syncp;
54};
55
56static struct dst_entry *vrf_ip_check(struct dst_entry *dst, u32 cookie)
57{
58 return dst;
59}
60
61static int vrf_ip_local_out(struct sk_buff *skb)
62{
63 return ip_local_out(skb);
64}
65
66static unsigned int vrf_v4_mtu(const struct dst_entry *dst)
67{
68 /* TO-DO: return max ethernet size? */
69 return dst->dev->mtu;
70}
71
72static void vrf_dst_destroy(struct dst_entry *dst)
73{
74 /* our dst lives forever - or until the device is closed */
75}
76
77static unsigned int vrf_default_advmss(const struct dst_entry *dst)
78{
79 return 65535 - 40;
80}
81
82static struct dst_ops vrf_dst_ops = {
83 .family = AF_INET,
84 .local_out = vrf_ip_local_out,
85 .check = vrf_ip_check,
86 .mtu = vrf_v4_mtu,
87 .destroy = vrf_dst_destroy,
88 .default_advmss = vrf_default_advmss,
89};
90
91static bool is_ip_rx_frame(struct sk_buff *skb)
92{
93 switch (skb->protocol) {
94 case htons(ETH_P_IP):
95 case htons(ETH_P_IPV6):
96 return true;
97 }
98 return false;
99}
100
Nikolay Aleksandrov57b8efa2015-08-19 06:12:29 +0300101static void vrf_tx_error(struct net_device *vrf_dev, struct sk_buff *skb)
102{
103 vrf_dev->stats.tx_errors++;
104 kfree_skb(skb);
105}
106
David Ahern193125d2015-08-13 14:59:10 -0600107/* note: already called with rcu_read_lock */
108static rx_handler_result_t vrf_handle_frame(struct sk_buff **pskb)
109{
110 struct sk_buff *skb = *pskb;
111
112 if (is_ip_rx_frame(skb)) {
113 struct net_device *dev = vrf_master_get_rcu(skb->dev);
114 struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
115
116 u64_stats_update_begin(&dstats->syncp);
117 dstats->rx_pkts++;
118 dstats->rx_bytes += skb->len;
119 u64_stats_update_end(&dstats->syncp);
120
121 skb->dev = dev;
122
123 return RX_HANDLER_ANOTHER;
124 }
125 return RX_HANDLER_PASS;
126}
127
128static struct rtnl_link_stats64 *vrf_get_stats64(struct net_device *dev,
129 struct rtnl_link_stats64 *stats)
130{
131 int i;
132
133 for_each_possible_cpu(i) {
134 const struct pcpu_dstats *dstats;
135 u64 tbytes, tpkts, tdrops, rbytes, rpkts;
136 unsigned int start;
137
138 dstats = per_cpu_ptr(dev->dstats, i);
139 do {
140 start = u64_stats_fetch_begin_irq(&dstats->syncp);
141 tbytes = dstats->tx_bytes;
142 tpkts = dstats->tx_pkts;
143 tdrops = dstats->tx_drps;
144 rbytes = dstats->rx_bytes;
145 rpkts = dstats->rx_pkts;
146 } while (u64_stats_fetch_retry_irq(&dstats->syncp, start));
147 stats->tx_bytes += tbytes;
148 stats->tx_packets += tpkts;
149 stats->tx_dropped += tdrops;
150 stats->rx_bytes += rbytes;
151 stats->rx_packets += rpkts;
152 }
153 return stats;
154}
155
156static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb,
157 struct net_device *dev)
158{
Nikolay Aleksandrov57b8efa2015-08-19 06:12:29 +0300159 vrf_tx_error(dev, skb);
160 return NET_XMIT_DROP;
David Ahern193125d2015-08-13 14:59:10 -0600161}
162
163static int vrf_send_v4_prep(struct sk_buff *skb, struct flowi4 *fl4,
164 struct net_device *vrf_dev)
165{
166 struct rtable *rt;
167 int err = 1;
168
169 rt = ip_route_output_flow(dev_net(vrf_dev), fl4, NULL);
170 if (IS_ERR(rt))
171 goto out;
172
173 /* TO-DO: what about broadcast ? */
174 if (rt->rt_type != RTN_UNICAST && rt->rt_type != RTN_LOCAL) {
175 ip_rt_put(rt);
176 goto out;
177 }
178
179 skb_dst_drop(skb);
180 skb_dst_set(skb, &rt->dst);
181 err = 0;
182out:
183 return err;
184}
185
186static netdev_tx_t vrf_process_v4_outbound(struct sk_buff *skb,
187 struct net_device *vrf_dev)
188{
189 struct iphdr *ip4h = ip_hdr(skb);
190 int ret = NET_XMIT_DROP;
191 struct flowi4 fl4 = {
192 /* needed to match OIF rule */
193 .flowi4_oif = vrf_dev->ifindex,
194 .flowi4_iif = LOOPBACK_IFINDEX,
195 .flowi4_tos = RT_TOS(ip4h->tos),
196 .flowi4_flags = FLOWI_FLAG_ANYSRC | FLOWI_FLAG_VRFSRC,
197 .daddr = ip4h->daddr,
198 };
199
200 if (vrf_send_v4_prep(skb, &fl4, vrf_dev))
201 goto err;
202
203 if (!ip4h->saddr) {
204 ip4h->saddr = inet_select_addr(skb_dst(skb)->dev, 0,
205 RT_SCOPE_LINK);
206 }
207
208 ret = ip_local_out(skb);
209 if (unlikely(net_xmit_eval(ret)))
210 vrf_dev->stats.tx_errors++;
211 else
212 ret = NET_XMIT_SUCCESS;
213
214out:
215 return ret;
216err:
Nikolay Aleksandrov57b8efa2015-08-19 06:12:29 +0300217 vrf_tx_error(vrf_dev, skb);
David Ahern193125d2015-08-13 14:59:10 -0600218 goto out;
219}
220
221static netdev_tx_t is_ip_tx_frame(struct sk_buff *skb, struct net_device *dev)
222{
David Ahern8f583362015-08-27 10:10:50 -0700223 /* strip the ethernet header added for pass through VRF device */
224 __skb_pull(skb, skb_network_offset(skb));
225
David Ahern193125d2015-08-13 14:59:10 -0600226 switch (skb->protocol) {
227 case htons(ETH_P_IP):
228 return vrf_process_v4_outbound(skb, dev);
229 case htons(ETH_P_IPV6):
230 return vrf_process_v6_outbound(skb, dev);
231 default:
Nikolay Aleksandrov57b8efa2015-08-19 06:12:29 +0300232 vrf_tx_error(dev, skb);
David Ahern193125d2015-08-13 14:59:10 -0600233 return NET_XMIT_DROP;
234 }
235}
236
237static netdev_tx_t vrf_xmit(struct sk_buff *skb, struct net_device *dev)
238{
239 netdev_tx_t ret = is_ip_tx_frame(skb, dev);
240
241 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
242 struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
243
244 u64_stats_update_begin(&dstats->syncp);
245 dstats->tx_pkts++;
246 dstats->tx_bytes += skb->len;
247 u64_stats_update_end(&dstats->syncp);
248 } else {
249 this_cpu_inc(dev->dstats->tx_drps);
250 }
251
252 return ret;
253}
254
David Ahern8f583362015-08-27 10:10:50 -0700255/* modelled after ip_finish_output2 */
256static int vrf_finish_output(struct sock *sk, struct sk_buff *skb)
David Ahern193125d2015-08-13 14:59:10 -0600257{
David Ahern8f583362015-08-27 10:10:50 -0700258 struct dst_entry *dst = skb_dst(skb);
259 struct rtable *rt = (struct rtable *)dst;
260 struct net_device *dev = dst->dev;
261 unsigned int hh_len = LL_RESERVED_SPACE(dev);
262 struct neighbour *neigh;
263 u32 nexthop;
264 int ret = -EINVAL;
265
266 /* Be paranoid, rather than too clever. */
267 if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
268 struct sk_buff *skb2;
269
270 skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev));
271 if (!skb2) {
272 ret = -ENOMEM;
273 goto err;
274 }
275 if (skb->sk)
276 skb_set_owner_w(skb2, skb->sk);
277
278 consume_skb(skb);
279 skb = skb2;
280 }
281
282 rcu_read_lock_bh();
283
284 nexthop = (__force u32)rt_nexthop(rt, ip_hdr(skb)->daddr);
285 neigh = __ipv4_neigh_lookup_noref(dev, nexthop);
286 if (unlikely(!neigh))
287 neigh = __neigh_create(&arp_tbl, &nexthop, dev, false);
288 if (!IS_ERR(neigh))
289 ret = dst_neigh_output(dst, neigh, skb);
290
291 rcu_read_unlock_bh();
292err:
293 if (unlikely(ret < 0))
294 vrf_tx_error(skb->dev, skb);
295 return ret;
David Ahern193125d2015-08-13 14:59:10 -0600296}
297
298static int vrf_output(struct sock *sk, struct sk_buff *skb)
299{
300 struct net_device *dev = skb_dst(skb)->dev;
301
302 IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUT, skb->len);
303
304 skb->dev = dev;
305 skb->protocol = htons(ETH_P_IP);
306
307 return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, sk, skb,
308 NULL, dev,
David Ahern8f583362015-08-27 10:10:50 -0700309 vrf_finish_output,
David Ahern193125d2015-08-13 14:59:10 -0600310 !(IPCB(skb)->flags & IPSKB_REROUTED));
311}
312
313static void vrf_rtable_destroy(struct net_vrf *vrf)
314{
315 struct dst_entry *dst = (struct dst_entry *)vrf->rth;
316
Nikolay Aleksandrov3a4a27d2015-08-18 20:28:03 +0300317 dst_destroy(dst);
David Ahern193125d2015-08-13 14:59:10 -0600318 vrf->rth = NULL;
319}
320
321static struct rtable *vrf_rtable_create(struct net_device *dev)
322{
323 struct rtable *rth;
324
325 rth = dst_alloc(&vrf_dst_ops, dev, 2,
326 DST_OBSOLETE_NONE,
327 (DST_HOST | DST_NOPOLICY | DST_NOXFRM));
328 if (rth) {
329 rth->dst.output = vrf_output;
330 rth->rt_genid = rt_genid_ipv4(dev_net(dev));
331 rth->rt_flags = 0;
332 rth->rt_type = RTN_UNICAST;
333 rth->rt_is_input = 0;
334 rth->rt_iif = 0;
335 rth->rt_pmtu = 0;
336 rth->rt_gateway = 0;
337 rth->rt_uses_gateway = 0;
338 INIT_LIST_HEAD(&rth->rt_uncached);
339 rth->rt_uncached_list = NULL;
David Ahern193125d2015-08-13 14:59:10 -0600340 }
341
342 return rth;
343}
344
345/**************************** device handling ********************/
346
347/* cycle interface to flush neighbor cache and move routes across tables */
348static void cycle_netdev(struct net_device *dev)
349{
350 unsigned int flags = dev->flags;
351 int ret;
352
353 if (!netif_running(dev))
354 return;
355
356 ret = dev_change_flags(dev, flags & ~IFF_UP);
357 if (ret >= 0)
358 ret = dev_change_flags(dev, flags);
359
360 if (ret < 0) {
361 netdev_err(dev,
362 "Failed to cycle device %s; route tables might be wrong!\n",
363 dev->name);
364 }
365}
366
367static struct slave *__vrf_find_slave_dev(struct slave_queue *queue,
368 struct net_device *dev)
369{
370 struct list_head *head = &queue->all_slaves;
371 struct slave *slave;
372
373 list_for_each_entry(slave, head, list) {
374 if (slave->dev == dev)
375 return slave;
376 }
377
378 return NULL;
379}
380
381/* inverse of __vrf_insert_slave */
382static void __vrf_remove_slave(struct slave_queue *queue, struct slave *slave)
383{
David Ahern193125d2015-08-13 14:59:10 -0600384 list_del(&slave->list);
David Ahern193125d2015-08-13 14:59:10 -0600385}
386
387static void __vrf_insert_slave(struct slave_queue *queue, struct slave *slave)
388{
David Ahern193125d2015-08-13 14:59:10 -0600389 list_add(&slave->list, &queue->all_slaves);
David Ahern193125d2015-08-13 14:59:10 -0600390}
391
392static int do_vrf_add_slave(struct net_device *dev, struct net_device *port_dev)
393{
394 struct net_vrf_dev *vrf_ptr = kmalloc(sizeof(*vrf_ptr), GFP_KERNEL);
395 struct slave *slave = kzalloc(sizeof(*slave), GFP_KERNEL);
David Ahern193125d2015-08-13 14:59:10 -0600396 struct net_vrf *vrf = netdev_priv(dev);
397 struct slave_queue *queue = &vrf->queue;
398 int ret = -ENOMEM;
399
400 if (!slave || !vrf_ptr)
401 goto out_fail;
402
403 slave->dev = port_dev;
David Ahern193125d2015-08-13 14:59:10 -0600404 vrf_ptr->ifindex = dev->ifindex;
405 vrf_ptr->tb_id = vrf->tb_id;
406
David Ahern193125d2015-08-13 14:59:10 -0600407 /* register the packet handler for slave ports */
408 ret = netdev_rx_handler_register(port_dev, vrf_handle_frame, dev);
409 if (ret) {
410 netdev_err(port_dev,
411 "Device %s failed to register rx_handler\n",
412 port_dev->name);
Nikolay Aleksandrov15df5e72015-08-19 06:27:09 +0300413 goto out_fail;
David Ahern193125d2015-08-13 14:59:10 -0600414 }
415
416 ret = netdev_master_upper_dev_link(port_dev, dev);
417 if (ret < 0)
418 goto out_unregister;
419
420 port_dev->flags |= IFF_SLAVE;
Nikolay Aleksandrov15df5e72015-08-19 06:27:09 +0300421 __vrf_insert_slave(queue, slave);
David Ahern193125d2015-08-13 14:59:10 -0600422 rcu_assign_pointer(port_dev->vrf_ptr, vrf_ptr);
423 cycle_netdev(port_dev);
424
425 return 0;
426
427out_unregister:
428 netdev_rx_handler_unregister(port_dev);
David Ahern193125d2015-08-13 14:59:10 -0600429out_fail:
430 kfree(vrf_ptr);
431 kfree(slave);
432 return ret;
433}
434
435static int vrf_add_slave(struct net_device *dev, struct net_device *port_dev)
436{
Nikolay Aleksandrov2640b392015-08-19 06:27:10 +0300437 if (netif_is_vrf(port_dev) || vrf_is_slave(port_dev))
David Ahern193125d2015-08-13 14:59:10 -0600438 return -EINVAL;
439
440 return do_vrf_add_slave(dev, port_dev);
441}
442
443/* inverse of do_vrf_add_slave */
444static int do_vrf_del_slave(struct net_device *dev, struct net_device *port_dev)
445{
446 struct net_vrf_dev *vrf_ptr = rtnl_dereference(port_dev->vrf_ptr);
447 struct net_vrf *vrf = netdev_priv(dev);
448 struct slave_queue *queue = &vrf->queue;
449 struct slave *slave;
450
451 RCU_INIT_POINTER(port_dev->vrf_ptr, NULL);
452
453 netdev_upper_dev_unlink(port_dev, dev);
454 port_dev->flags &= ~IFF_SLAVE;
455
456 netdev_rx_handler_unregister(port_dev);
457
458 /* after netdev_rx_handler_unregister for synchronize_rcu */
459 kfree(vrf_ptr);
460
461 cycle_netdev(port_dev);
462
463 slave = __vrf_find_slave_dev(queue, port_dev);
464 if (slave)
465 __vrf_remove_slave(queue, slave);
466
467 kfree(slave);
468
469 return 0;
470}
471
472static int vrf_del_slave(struct net_device *dev, struct net_device *port_dev)
473{
David Ahern193125d2015-08-13 14:59:10 -0600474 return do_vrf_del_slave(dev, port_dev);
475}
476
477static void vrf_dev_uninit(struct net_device *dev)
478{
479 struct net_vrf *vrf = netdev_priv(dev);
480 struct slave_queue *queue = &vrf->queue;
481 struct list_head *head = &queue->all_slaves;
482 struct slave *slave, *next;
483
484 vrf_rtable_destroy(vrf);
485
486 list_for_each_entry_safe(slave, next, head, list)
487 vrf_del_slave(dev, slave->dev);
488
Nikolay Aleksandrov3a4a27d2015-08-18 20:28:03 +0300489 free_percpu(dev->dstats);
David Ahern193125d2015-08-13 14:59:10 -0600490 dev->dstats = NULL;
491}
492
493static int vrf_dev_init(struct net_device *dev)
494{
495 struct net_vrf *vrf = netdev_priv(dev);
496
497 INIT_LIST_HEAD(&vrf->queue.all_slaves);
498
499 dev->dstats = netdev_alloc_pcpu_stats(struct pcpu_dstats);
500 if (!dev->dstats)
501 goto out_nomem;
502
503 /* create the default dst which points back to us */
504 vrf->rth = vrf_rtable_create(dev);
505 if (!vrf->rth)
506 goto out_stats;
507
508 dev->flags = IFF_MASTER | IFF_NOARP;
509
510 return 0;
511
512out_stats:
513 free_percpu(dev->dstats);
514 dev->dstats = NULL;
515out_nomem:
516 return -ENOMEM;
517}
518
519static const struct net_device_ops vrf_netdev_ops = {
520 .ndo_init = vrf_dev_init,
521 .ndo_uninit = vrf_dev_uninit,
522 .ndo_start_xmit = vrf_xmit,
523 .ndo_get_stats64 = vrf_get_stats64,
524 .ndo_add_slave = vrf_add_slave,
525 .ndo_del_slave = vrf_del_slave,
526};
527
528static void vrf_get_drvinfo(struct net_device *dev,
529 struct ethtool_drvinfo *info)
530{
531 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
532 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
533}
534
535static const struct ethtool_ops vrf_ethtool_ops = {
536 .get_drvinfo = vrf_get_drvinfo,
537};
538
539static void vrf_setup(struct net_device *dev)
540{
541 ether_setup(dev);
542
543 /* Initialize the device structure. */
544 dev->netdev_ops = &vrf_netdev_ops;
545 dev->ethtool_ops = &vrf_ethtool_ops;
546 dev->destructor = free_netdev;
547
548 /* Fill in device structure with ethernet-generic values. */
549 eth_hw_addr_random(dev);
550
551 /* don't acquire vrf device's netif_tx_lock when transmitting */
552 dev->features |= NETIF_F_LLTX;
553
554 /* don't allow vrf devices to change network namespaces. */
555 dev->features |= NETIF_F_NETNS_LOCAL;
556}
557
558static int vrf_validate(struct nlattr *tb[], struct nlattr *data[])
559{
560 if (tb[IFLA_ADDRESS]) {
561 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
562 return -EINVAL;
563 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
564 return -EADDRNOTAVAIL;
565 }
566 return 0;
567}
568
569static void vrf_dellink(struct net_device *dev, struct list_head *head)
570{
571 struct net_vrf_dev *vrf_ptr = rtnl_dereference(dev->vrf_ptr);
572
573 RCU_INIT_POINTER(dev->vrf_ptr, NULL);
574 kfree_rcu(vrf_ptr, rcu);
575 unregister_netdevice_queue(dev, head);
576}
577
578static int vrf_newlink(struct net *src_net, struct net_device *dev,
579 struct nlattr *tb[], struct nlattr *data[])
580{
581 struct net_vrf *vrf = netdev_priv(dev);
582 struct net_vrf_dev *vrf_ptr;
583 int err;
584
585 if (!data || !data[IFLA_VRF_TABLE])
586 return -EINVAL;
587
588 vrf->tb_id = nla_get_u32(data[IFLA_VRF_TABLE]);
589
590 dev->priv_flags |= IFF_VRF_MASTER;
591
592 err = -ENOMEM;
593 vrf_ptr = kmalloc(sizeof(*dev->vrf_ptr), GFP_KERNEL);
594 if (!vrf_ptr)
595 goto out_fail;
596
597 vrf_ptr->ifindex = dev->ifindex;
598 vrf_ptr->tb_id = vrf->tb_id;
599
600 err = register_netdevice(dev);
601 if (err < 0)
602 goto out_fail;
603
604 rcu_assign_pointer(dev->vrf_ptr, vrf_ptr);
605
606 return 0;
607
608out_fail:
609 kfree(vrf_ptr);
610 free_netdev(dev);
611 return err;
612}
613
614static size_t vrf_nl_getsize(const struct net_device *dev)
615{
616 return nla_total_size(sizeof(u32)); /* IFLA_VRF_TABLE */
617}
618
619static int vrf_fillinfo(struct sk_buff *skb,
620 const struct net_device *dev)
621{
622 struct net_vrf *vrf = netdev_priv(dev);
623
624 return nla_put_u32(skb, IFLA_VRF_TABLE, vrf->tb_id);
625}
626
627static const struct nla_policy vrf_nl_policy[IFLA_VRF_MAX + 1] = {
628 [IFLA_VRF_TABLE] = { .type = NLA_U32 },
629};
630
631static struct rtnl_link_ops vrf_link_ops __read_mostly = {
632 .kind = DRV_NAME,
633 .priv_size = sizeof(struct net_vrf),
634
635 .get_size = vrf_nl_getsize,
636 .policy = vrf_nl_policy,
637 .validate = vrf_validate,
638 .fill_info = vrf_fillinfo,
639
640 .newlink = vrf_newlink,
641 .dellink = vrf_dellink,
642 .setup = vrf_setup,
643 .maxtype = IFLA_VRF_MAX,
644};
645
646static int vrf_device_event(struct notifier_block *unused,
647 unsigned long event, void *ptr)
648{
649 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
650
651 /* only care about unregister events to drop slave references */
652 if (event == NETDEV_UNREGISTER) {
653 struct net_vrf_dev *vrf_ptr = rtnl_dereference(dev->vrf_ptr);
654 struct net_device *vrf_dev;
655
656 if (!vrf_ptr || netif_is_vrf(dev))
657 goto out;
658
Nikolay Aleksandrov58aa9082015-08-18 20:28:04 +0300659 vrf_dev = netdev_master_upper_dev_get(dev);
660 vrf_del_slave(vrf_dev, dev);
David Ahern193125d2015-08-13 14:59:10 -0600661 }
662out:
663 return NOTIFY_DONE;
664}
665
666static struct notifier_block vrf_notifier_block __read_mostly = {
667 .notifier_call = vrf_device_event,
668};
669
670static int __init vrf_init_module(void)
671{
672 int rc;
673
674 vrf_dst_ops.kmem_cachep =
675 kmem_cache_create("vrf_ip_dst_cache",
676 sizeof(struct rtable), 0,
Nikolay Aleksandrove367da02015-08-19 06:27:07 +0300677 SLAB_HWCACHE_ALIGN,
David Ahern193125d2015-08-13 14:59:10 -0600678 NULL);
679
680 if (!vrf_dst_ops.kmem_cachep)
681 return -ENOMEM;
682
683 register_netdevice_notifier(&vrf_notifier_block);
684
685 rc = rtnl_link_register(&vrf_link_ops);
686 if (rc < 0)
687 goto error;
688
689 return 0;
690
691error:
692 unregister_netdevice_notifier(&vrf_notifier_block);
693 kmem_cache_destroy(vrf_dst_ops.kmem_cachep);
694 return rc;
695}
696
697static void __exit vrf_cleanup_module(void)
698{
699 rtnl_link_unregister(&vrf_link_ops);
700 unregister_netdevice_notifier(&vrf_notifier_block);
701 kmem_cache_destroy(vrf_dst_ops.kmem_cachep);
702}
703
704module_init(vrf_init_module);
705module_exit(vrf_cleanup_module);
706MODULE_AUTHOR("Shrijeet Mukherjee, David Ahern");
707MODULE_DESCRIPTION("Device driver to instantiate VRF domains");
708MODULE_LICENSE("GPL");
709MODULE_ALIAS_RTNL_LINK(DRV_NAME);
710MODULE_VERSION(DRV_VERSION);