l3mdev.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * net/l3mdev/l3mdev.c - L3 master device implementation
  3. * Copyright (c) 2015 Cumulus Networks
  4. * Copyright (c) 2015 David Ahern <dsa@cumulusnetworks.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/netdevice.h>
  12. #include <net/l3mdev.h>
  13. /**
  14. * l3mdev_master_ifindex - get index of L3 master device
  15. * @dev: targeted interface
  16. */
  17. int l3mdev_master_ifindex_rcu(struct net_device *dev)
  18. {
  19. int ifindex = 0;
  20. if (!dev)
  21. return 0;
  22. if (netif_is_l3_master(dev)) {
  23. ifindex = dev->ifindex;
  24. } else if (netif_is_l3_slave(dev)) {
  25. struct net_device *master;
  26. master = netdev_master_upper_dev_get_rcu(dev);
  27. if (master)
  28. ifindex = master->ifindex;
  29. }
  30. return ifindex;
  31. }
  32. EXPORT_SYMBOL_GPL(l3mdev_master_ifindex_rcu);
  33. /**
  34. * l3mdev_fib_table - get FIB table id associated with an L3
  35. * master interface
  36. * @dev: targeted interface
  37. */
  38. u32 l3mdev_fib_table_rcu(const struct net_device *dev)
  39. {
  40. u32 tb_id = 0;
  41. if (!dev)
  42. return 0;
  43. if (netif_is_l3_master(dev)) {
  44. if (dev->l3mdev_ops->l3mdev_fib_table)
  45. tb_id = dev->l3mdev_ops->l3mdev_fib_table(dev);
  46. } else if (netif_is_l3_slave(dev)) {
  47. /* Users of netdev_master_upper_dev_get_rcu need non-const,
  48. * but current inet_*type functions take a const
  49. */
  50. struct net_device *_dev = (struct net_device *) dev;
  51. const struct net_device *master;
  52. master = netdev_master_upper_dev_get_rcu(_dev);
  53. if (master &&
  54. master->l3mdev_ops->l3mdev_fib_table)
  55. tb_id = master->l3mdev_ops->l3mdev_fib_table(master);
  56. }
  57. return tb_id;
  58. }
  59. EXPORT_SYMBOL_GPL(l3mdev_fib_table_rcu);
  60. u32 l3mdev_fib_table_by_index(struct net *net, int ifindex)
  61. {
  62. struct net_device *dev;
  63. u32 tb_id = 0;
  64. if (!ifindex)
  65. return 0;
  66. rcu_read_lock();
  67. dev = dev_get_by_index_rcu(net, ifindex);
  68. if (dev)
  69. tb_id = l3mdev_fib_table_rcu(dev);
  70. rcu_read_unlock();
  71. return tb_id;
  72. }
  73. EXPORT_SYMBOL_GPL(l3mdev_fib_table_by_index);