br_vlan_tunnel.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Bridge per vlan tunnel port dst_metadata handling code
  3. *
  4. * Authors:
  5. * Roopa Prabhu <roopa@cumulusnetworks.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/rtnetlink.h>
  15. #include <linux/slab.h>
  16. #include <net/switchdev.h>
  17. #include <net/dst_metadata.h>
  18. #include "br_private.h"
  19. #include "br_private_tunnel.h"
  20. static inline int br_vlan_tunid_cmp(struct rhashtable_compare_arg *arg,
  21. const void *ptr)
  22. {
  23. const struct net_bridge_vlan *vle = ptr;
  24. __be64 tunid = *(__be64 *)arg->key;
  25. return vle->tinfo.tunnel_id != tunid;
  26. }
  27. static const struct rhashtable_params br_vlan_tunnel_rht_params = {
  28. .head_offset = offsetof(struct net_bridge_vlan, tnode),
  29. .key_offset = offsetof(struct net_bridge_vlan, tinfo.tunnel_id),
  30. .key_len = sizeof(__be64),
  31. .nelem_hint = 3,
  32. .locks_mul = 1,
  33. .obj_cmpfn = br_vlan_tunid_cmp,
  34. .automatic_shrinking = true,
  35. };
  36. static struct net_bridge_vlan *br_vlan_tunnel_lookup(struct rhashtable *tbl,
  37. u64 tunnel_id)
  38. {
  39. return rhashtable_lookup_fast(tbl, &tunnel_id,
  40. br_vlan_tunnel_rht_params);
  41. }
  42. void vlan_tunnel_info_del(struct net_bridge_vlan_group *vg,
  43. struct net_bridge_vlan *vlan)
  44. {
  45. if (!vlan->tinfo.tunnel_dst)
  46. return;
  47. rhashtable_remove_fast(&vg->tunnel_hash, &vlan->tnode,
  48. br_vlan_tunnel_rht_params);
  49. vlan->tinfo.tunnel_id = 0;
  50. dst_release(&vlan->tinfo.tunnel_dst->dst);
  51. vlan->tinfo.tunnel_dst = NULL;
  52. }
  53. static int __vlan_tunnel_info_add(struct net_bridge_vlan_group *vg,
  54. struct net_bridge_vlan *vlan, u32 tun_id)
  55. {
  56. struct metadata_dst *metadata = NULL;
  57. __be64 key = key32_to_tunnel_id(cpu_to_be32(tun_id));
  58. int err;
  59. if (vlan->tinfo.tunnel_dst)
  60. return -EEXIST;
  61. metadata = __ip_tun_set_dst(0, 0, 0, 0, 0, TUNNEL_KEY,
  62. key, 0);
  63. if (!metadata)
  64. return -EINVAL;
  65. metadata->u.tun_info.mode |= IP_TUNNEL_INFO_TX | IP_TUNNEL_INFO_BRIDGE;
  66. vlan->tinfo.tunnel_dst = metadata;
  67. vlan->tinfo.tunnel_id = key;
  68. err = rhashtable_lookup_insert_fast(&vg->tunnel_hash, &vlan->tnode,
  69. br_vlan_tunnel_rht_params);
  70. if (err)
  71. goto out;
  72. return 0;
  73. out:
  74. dst_release(&vlan->tinfo.tunnel_dst->dst);
  75. vlan->tinfo.tunnel_dst = NULL;
  76. vlan->tinfo.tunnel_id = 0;
  77. return err;
  78. }
  79. /* Must be protected by RTNL.
  80. * Must be called with vid in range from 1 to 4094 inclusive.
  81. */
  82. int nbp_vlan_tunnel_info_add(struct net_bridge_port *port, u16 vid, u32 tun_id)
  83. {
  84. struct net_bridge_vlan_group *vg;
  85. struct net_bridge_vlan *vlan;
  86. ASSERT_RTNL();
  87. vg = nbp_vlan_group(port);
  88. vlan = br_vlan_find(vg, vid);
  89. if (!vlan)
  90. return -EINVAL;
  91. return __vlan_tunnel_info_add(vg, vlan, tun_id);
  92. }
  93. /* Must be protected by RTNL.
  94. * Must be called with vid in range from 1 to 4094 inclusive.
  95. */
  96. int nbp_vlan_tunnel_info_delete(struct net_bridge_port *port, u16 vid)
  97. {
  98. struct net_bridge_vlan_group *vg;
  99. struct net_bridge_vlan *v;
  100. ASSERT_RTNL();
  101. vg = nbp_vlan_group(port);
  102. v = br_vlan_find(vg, vid);
  103. if (!v)
  104. return -ENOENT;
  105. vlan_tunnel_info_del(vg, v);
  106. return 0;
  107. }
  108. static void __vlan_tunnel_info_flush(struct net_bridge_vlan_group *vg)
  109. {
  110. struct net_bridge_vlan *vlan, *tmp;
  111. list_for_each_entry_safe(vlan, tmp, &vg->vlan_list, vlist)
  112. vlan_tunnel_info_del(vg, vlan);
  113. }
  114. void nbp_vlan_tunnel_info_flush(struct net_bridge_port *port)
  115. {
  116. struct net_bridge_vlan_group *vg;
  117. ASSERT_RTNL();
  118. vg = nbp_vlan_group(port);
  119. __vlan_tunnel_info_flush(vg);
  120. }
  121. int vlan_tunnel_init(struct net_bridge_vlan_group *vg)
  122. {
  123. return rhashtable_init(&vg->tunnel_hash, &br_vlan_tunnel_rht_params);
  124. }
  125. void vlan_tunnel_deinit(struct net_bridge_vlan_group *vg)
  126. {
  127. rhashtable_destroy(&vg->tunnel_hash);
  128. }
  129. int br_handle_ingress_vlan_tunnel(struct sk_buff *skb,
  130. struct net_bridge_port *p,
  131. struct net_bridge_vlan_group *vg)
  132. {
  133. struct ip_tunnel_info *tinfo = skb_tunnel_info(skb);
  134. struct net_bridge_vlan *vlan;
  135. if (!vg || !tinfo)
  136. return 0;
  137. /* if already tagged, ignore */
  138. if (skb_vlan_tagged(skb))
  139. return 0;
  140. /* lookup vid, given tunnel id */
  141. vlan = br_vlan_tunnel_lookup(&vg->tunnel_hash, tinfo->key.tun_id);
  142. if (!vlan)
  143. return 0;
  144. skb_dst_drop(skb);
  145. __vlan_hwaccel_put_tag(skb, p->br->vlan_proto, vlan->vid);
  146. return 0;
  147. }
  148. int br_handle_egress_vlan_tunnel(struct sk_buff *skb,
  149. struct net_bridge_vlan *vlan)
  150. {
  151. int err;
  152. if (!vlan || !vlan->tinfo.tunnel_id)
  153. return 0;
  154. if (unlikely(!skb_vlan_tag_present(skb)))
  155. return 0;
  156. skb_dst_drop(skb);
  157. err = skb_vlan_pop(skb);
  158. if (err)
  159. return err;
  160. skb_dst_set(skb, dst_clone(&vlan->tinfo.tunnel_dst->dst));
  161. return 0;
  162. }