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