vxlan.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (c) 2016, Mellanox Technologies, Ltd. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/kernel.h>
  33. #include <linux/module.h>
  34. #include <linux/mlx5/driver.h>
  35. #include "mlx5_core.h"
  36. #include "vxlan.h"
  37. void mlx5e_vxlan_init(struct mlx5e_priv *priv)
  38. {
  39. struct mlx5e_vxlan_db *vxlan_db = &priv->vxlan;
  40. spin_lock_init(&vxlan_db->lock);
  41. INIT_RADIX_TREE(&vxlan_db->tree, GFP_ATOMIC);
  42. }
  43. static int mlx5e_vxlan_core_add_port_cmd(struct mlx5_core_dev *mdev, u16 port)
  44. {
  45. u32 in[MLX5_ST_SZ_DW(add_vxlan_udp_dport_in)] = {0};
  46. u32 out[MLX5_ST_SZ_DW(add_vxlan_udp_dport_out)] = {0};
  47. MLX5_SET(add_vxlan_udp_dport_in, in, opcode,
  48. MLX5_CMD_OP_ADD_VXLAN_UDP_DPORT);
  49. MLX5_SET(add_vxlan_udp_dport_in, in, vxlan_udp_port, port);
  50. return mlx5_cmd_exec(mdev, in, sizeof(in), out, sizeof(out));
  51. }
  52. static int mlx5e_vxlan_core_del_port_cmd(struct mlx5_core_dev *mdev, u16 port)
  53. {
  54. u32 in[MLX5_ST_SZ_DW(delete_vxlan_udp_dport_in)] = {0};
  55. u32 out[MLX5_ST_SZ_DW(delete_vxlan_udp_dport_out)] = {0};
  56. MLX5_SET(delete_vxlan_udp_dport_in, in, opcode,
  57. MLX5_CMD_OP_DELETE_VXLAN_UDP_DPORT);
  58. MLX5_SET(delete_vxlan_udp_dport_in, in, vxlan_udp_port, port);
  59. return mlx5_cmd_exec(mdev, in, sizeof(in), out, sizeof(out));
  60. }
  61. struct mlx5e_vxlan *mlx5e_vxlan_lookup_port(struct mlx5e_priv *priv, u16 port)
  62. {
  63. struct mlx5e_vxlan_db *vxlan_db = &priv->vxlan;
  64. struct mlx5e_vxlan *vxlan;
  65. spin_lock_bh(&vxlan_db->lock);
  66. vxlan = radix_tree_lookup(&vxlan_db->tree, port);
  67. spin_unlock_bh(&vxlan_db->lock);
  68. return vxlan;
  69. }
  70. static void mlx5e_vxlan_add_port(struct work_struct *work)
  71. {
  72. struct mlx5e_vxlan_work *vxlan_work =
  73. container_of(work, struct mlx5e_vxlan_work, work);
  74. struct mlx5e_priv *priv = vxlan_work->priv;
  75. struct mlx5e_vxlan_db *vxlan_db = &priv->vxlan;
  76. u16 port = vxlan_work->port;
  77. struct mlx5e_vxlan *vxlan;
  78. int err;
  79. mutex_lock(&priv->state_lock);
  80. vxlan = mlx5e_vxlan_lookup_port(priv, port);
  81. if (vxlan) {
  82. atomic_inc(&vxlan->refcount);
  83. goto free_work;
  84. }
  85. if (mlx5e_vxlan_core_add_port_cmd(priv->mdev, port))
  86. goto free_work;
  87. vxlan = kzalloc(sizeof(*vxlan), GFP_KERNEL);
  88. if (!vxlan)
  89. goto err_delete_port;
  90. vxlan->udp_port = port;
  91. atomic_set(&vxlan->refcount, 1);
  92. spin_lock_bh(&vxlan_db->lock);
  93. err = radix_tree_insert(&vxlan_db->tree, vxlan->udp_port, vxlan);
  94. spin_unlock_bh(&vxlan_db->lock);
  95. if (err)
  96. goto err_free;
  97. goto free_work;
  98. err_free:
  99. kfree(vxlan);
  100. err_delete_port:
  101. mlx5e_vxlan_core_del_port_cmd(priv->mdev, port);
  102. free_work:
  103. mutex_unlock(&priv->state_lock);
  104. kfree(vxlan_work);
  105. }
  106. static void mlx5e_vxlan_del_port(struct work_struct *work)
  107. {
  108. struct mlx5e_vxlan_work *vxlan_work =
  109. container_of(work, struct mlx5e_vxlan_work, work);
  110. struct mlx5e_priv *priv = vxlan_work->priv;
  111. struct mlx5e_vxlan_db *vxlan_db = &priv->vxlan;
  112. u16 port = vxlan_work->port;
  113. struct mlx5e_vxlan *vxlan;
  114. bool remove = false;
  115. mutex_lock(&priv->state_lock);
  116. spin_lock_bh(&vxlan_db->lock);
  117. vxlan = radix_tree_lookup(&vxlan_db->tree, port);
  118. if (!vxlan)
  119. goto out_unlock;
  120. if (atomic_dec_and_test(&vxlan->refcount)) {
  121. radix_tree_delete(&vxlan_db->tree, port);
  122. remove = true;
  123. }
  124. out_unlock:
  125. spin_unlock_bh(&vxlan_db->lock);
  126. if (remove) {
  127. mlx5e_vxlan_core_del_port_cmd(priv->mdev, port);
  128. kfree(vxlan);
  129. }
  130. mutex_unlock(&priv->state_lock);
  131. kfree(vxlan_work);
  132. }
  133. void mlx5e_vxlan_queue_work(struct mlx5e_priv *priv, sa_family_t sa_family,
  134. u16 port, int add)
  135. {
  136. struct mlx5e_vxlan_work *vxlan_work;
  137. vxlan_work = kmalloc(sizeof(*vxlan_work), GFP_ATOMIC);
  138. if (!vxlan_work)
  139. return;
  140. if (add)
  141. INIT_WORK(&vxlan_work->work, mlx5e_vxlan_add_port);
  142. else
  143. INIT_WORK(&vxlan_work->work, mlx5e_vxlan_del_port);
  144. vxlan_work->priv = priv;
  145. vxlan_work->port = port;
  146. vxlan_work->sa_family = sa_family;
  147. queue_work(priv->wq, &vxlan_work->work);
  148. }
  149. void mlx5e_vxlan_cleanup(struct mlx5e_priv *priv)
  150. {
  151. struct mlx5e_vxlan_db *vxlan_db = &priv->vxlan;
  152. struct mlx5e_vxlan *vxlan;
  153. unsigned int port = 0;
  154. /* Lockless since we are the only radix-tree consumers, wq is disabled */
  155. while (radix_tree_gang_lookup(&vxlan_db->tree, (void **)&vxlan, port, 1)) {
  156. port = vxlan->udp_port;
  157. radix_tree_delete(&vxlan_db->tree, port);
  158. mlx5e_vxlan_core_del_port_cmd(priv->mdev, port);
  159. kfree(vxlan);
  160. }
  161. }