vrf.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * include/net/net_vrf.h - adds vrf dev structure definitions
  3. * Copyright (c) 2015 Cumulus Networks
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. */
  10. #ifndef __LINUX_NET_VRF_H
  11. #define __LINUX_NET_VRF_H
  12. struct net_vrf_dev {
  13. struct rcu_head rcu;
  14. int ifindex; /* ifindex of master dev */
  15. u32 tb_id; /* table id for VRF */
  16. };
  17. struct slave {
  18. struct list_head list;
  19. struct net_device *dev;
  20. };
  21. struct slave_queue {
  22. struct list_head all_slaves;
  23. };
  24. struct net_vrf {
  25. struct slave_queue queue;
  26. struct rtable *rth;
  27. u32 tb_id;
  28. };
  29. #if IS_ENABLED(CONFIG_NET_VRF)
  30. /* called with rcu_read_lock() */
  31. static inline int vrf_master_ifindex_rcu(const struct net_device *dev)
  32. {
  33. struct net_vrf_dev *vrf_ptr;
  34. int ifindex = 0;
  35. if (!dev)
  36. return 0;
  37. if (netif_is_vrf(dev)) {
  38. ifindex = dev->ifindex;
  39. } else {
  40. vrf_ptr = rcu_dereference(dev->vrf_ptr);
  41. if (vrf_ptr)
  42. ifindex = vrf_ptr->ifindex;
  43. }
  44. return ifindex;
  45. }
  46. static inline int vrf_master_ifindex(const struct net_device *dev)
  47. {
  48. int ifindex;
  49. rcu_read_lock();
  50. ifindex = vrf_master_ifindex_rcu(dev);
  51. rcu_read_unlock();
  52. return ifindex;
  53. }
  54. /* called with rcu_read_lock */
  55. static inline u32 vrf_dev_table_rcu(const struct net_device *dev)
  56. {
  57. u32 tb_id = 0;
  58. if (dev) {
  59. struct net_vrf_dev *vrf_ptr;
  60. vrf_ptr = rcu_dereference(dev->vrf_ptr);
  61. if (vrf_ptr)
  62. tb_id = vrf_ptr->tb_id;
  63. }
  64. return tb_id;
  65. }
  66. static inline u32 vrf_dev_table(const struct net_device *dev)
  67. {
  68. u32 tb_id;
  69. rcu_read_lock();
  70. tb_id = vrf_dev_table_rcu(dev);
  71. rcu_read_unlock();
  72. return tb_id;
  73. }
  74. static inline u32 vrf_dev_table_ifindex(struct net *net, int ifindex)
  75. {
  76. struct net_device *dev;
  77. u32 tb_id = 0;
  78. if (!ifindex)
  79. return 0;
  80. rcu_read_lock();
  81. dev = dev_get_by_index_rcu(net, ifindex);
  82. if (dev)
  83. tb_id = vrf_dev_table_rcu(dev);
  84. rcu_read_unlock();
  85. return tb_id;
  86. }
  87. /* called with rtnl */
  88. static inline u32 vrf_dev_table_rtnl(const struct net_device *dev)
  89. {
  90. u32 tb_id = 0;
  91. if (dev) {
  92. struct net_vrf_dev *vrf_ptr;
  93. vrf_ptr = rtnl_dereference(dev->vrf_ptr);
  94. if (vrf_ptr)
  95. tb_id = vrf_ptr->tb_id;
  96. }
  97. return tb_id;
  98. }
  99. /* caller has already checked netif_is_vrf(dev) */
  100. static inline struct rtable *vrf_dev_get_rth(const struct net_device *dev)
  101. {
  102. struct rtable *rth = ERR_PTR(-ENETUNREACH);
  103. struct net_vrf *vrf = netdev_priv(dev);
  104. if (vrf) {
  105. rth = vrf->rth;
  106. atomic_inc(&rth->dst.__refcnt);
  107. }
  108. return rth;
  109. }
  110. #else
  111. static inline int vrf_master_ifindex_rcu(const struct net_device *dev)
  112. {
  113. return 0;
  114. }
  115. static inline int vrf_master_ifindex(const struct net_device *dev)
  116. {
  117. return 0;
  118. }
  119. static inline u32 vrf_dev_table_rcu(const struct net_device *dev)
  120. {
  121. return 0;
  122. }
  123. static inline u32 vrf_dev_table(const struct net_device *dev)
  124. {
  125. return 0;
  126. }
  127. static inline u32 vrf_dev_table_ifindex(struct net *net, int ifindex)
  128. {
  129. return 0;
  130. }
  131. static inline u32 vrf_dev_table_rtnl(const struct net_device *dev)
  132. {
  133. return 0;
  134. }
  135. static inline struct rtable *vrf_dev_get_rth(const struct net_device *dev)
  136. {
  137. return ERR_PTR(-ENETUNREACH);
  138. }
  139. #endif
  140. #endif /* __LINUX_NET_VRF_H */