1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include <linux/kernel.h>
34#include <linux/module.h>
35#include <linux/refcount.h>
36#include <linux/mlx5/driver.h>
37#include <net/vxlan.h>
38#include "mlx5_core.h"
39#include "vxlan.h"
40
41struct mlx5_vxlan {
42 struct mlx5_core_dev *mdev;
43
44 DECLARE_HASHTABLE(htable, 4);
45 struct mutex sync_lock;
46};
47
48struct mlx5_vxlan_port {
49 struct hlist_node hlist;
50 u16 udp_port;
51};
52
53static int mlx5_vxlan_core_add_port_cmd(struct mlx5_core_dev *mdev, u16 port)
54{
55 u32 in[MLX5_ST_SZ_DW(add_vxlan_udp_dport_in)] = {};
56
57 MLX5_SET(add_vxlan_udp_dport_in, in, opcode,
58 MLX5_CMD_OP_ADD_VXLAN_UDP_DPORT);
59 MLX5_SET(add_vxlan_udp_dport_in, in, vxlan_udp_port, port);
60 return mlx5_cmd_exec_in(mdev, add_vxlan_udp_dport, in);
61}
62
63static int mlx5_vxlan_core_del_port_cmd(struct mlx5_core_dev *mdev, u16 port)
64{
65 u32 in[MLX5_ST_SZ_DW(delete_vxlan_udp_dport_in)] = {};
66
67 MLX5_SET(delete_vxlan_udp_dport_in, in, opcode,
68 MLX5_CMD_OP_DELETE_VXLAN_UDP_DPORT);
69 MLX5_SET(delete_vxlan_udp_dport_in, in, vxlan_udp_port, port);
70 return mlx5_cmd_exec_in(mdev, delete_vxlan_udp_dport, in);
71}
72
73bool mlx5_vxlan_lookup_port(struct mlx5_vxlan *vxlan, u16 port)
74{
75 struct mlx5_vxlan_port *vxlanp;
76 bool found = false;
77
78 if (!mlx5_vxlan_allowed(vxlan))
79 return NULL;
80
81 rcu_read_lock();
82 hash_for_each_possible_rcu(vxlan->htable, vxlanp, hlist, port)
83 if (vxlanp->udp_port == port) {
84 found = true;
85 break;
86 }
87 rcu_read_unlock();
88
89 return found;
90}
91
92static struct mlx5_vxlan_port *vxlan_lookup_port(struct mlx5_vxlan *vxlan, u16 port)
93{
94 struct mlx5_vxlan_port *vxlanp;
95
96 hash_for_each_possible(vxlan->htable, vxlanp, hlist, port)
97 if (vxlanp->udp_port == port)
98 return vxlanp;
99 return NULL;
100}
101
102int mlx5_vxlan_add_port(struct mlx5_vxlan *vxlan, u16 port)
103{
104 struct mlx5_vxlan_port *vxlanp;
105 int ret;
106
107 vxlanp = kzalloc(sizeof(*vxlanp), GFP_KERNEL);
108 if (!vxlanp)
109 return -ENOMEM;
110 vxlanp->udp_port = port;
111
112 ret = mlx5_vxlan_core_add_port_cmd(vxlan->mdev, port);
113 if (ret) {
114 kfree(vxlanp);
115 return ret;
116 }
117
118 mutex_lock(&vxlan->sync_lock);
119 hash_add_rcu(vxlan->htable, &vxlanp->hlist, port);
120 mutex_unlock(&vxlan->sync_lock);
121
122 return 0;
123}
124
125int mlx5_vxlan_del_port(struct mlx5_vxlan *vxlan, u16 port)
126{
127 struct mlx5_vxlan_port *vxlanp;
128 int ret = 0;
129
130 mutex_lock(&vxlan->sync_lock);
131
132 vxlanp = vxlan_lookup_port(vxlan, port);
133 if (WARN_ON(!vxlanp)) {
134 ret = -ENOENT;
135 goto out_unlock;
136 }
137
138 hash_del_rcu(&vxlanp->hlist);
139 synchronize_rcu();
140 mlx5_vxlan_core_del_port_cmd(vxlan->mdev, port);
141 kfree(vxlanp);
142
143out_unlock:
144 mutex_unlock(&vxlan->sync_lock);
145 return ret;
146}
147
148struct mlx5_vxlan *mlx5_vxlan_create(struct mlx5_core_dev *mdev)
149{
150 struct mlx5_vxlan *vxlan;
151
152 if (!MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan) || !mlx5_core_is_pf(mdev))
153 return ERR_PTR(-ENOTSUPP);
154
155 vxlan = kzalloc(sizeof(*vxlan), GFP_KERNEL);
156 if (!vxlan)
157 return ERR_PTR(-ENOMEM);
158
159 vxlan->mdev = mdev;
160 mutex_init(&vxlan->sync_lock);
161 hash_init(vxlan->htable);
162
163
164 mlx5_vxlan_add_port(vxlan, IANA_VXLAN_UDP_PORT);
165
166 return vxlan;
167}
168
169void mlx5_vxlan_destroy(struct mlx5_vxlan *vxlan)
170{
171 if (!mlx5_vxlan_allowed(vxlan))
172 return;
173
174 mlx5_vxlan_del_port(vxlan, IANA_VXLAN_UDP_PORT);
175 WARN_ON(!hash_empty(vxlan->htable));
176
177 kfree(vxlan);
178}
179
180void mlx5_vxlan_reset_to_default(struct mlx5_vxlan *vxlan)
181{
182 struct mlx5_vxlan_port *vxlanp;
183 struct hlist_node *tmp;
184 int bkt;
185
186 if (!mlx5_vxlan_allowed(vxlan))
187 return;
188
189 hash_for_each_safe(vxlan->htable, bkt, tmp, vxlanp, hlist) {
190
191
192
193 if (vxlanp->udp_port == IANA_VXLAN_UDP_PORT)
194 continue;
195 mlx5_vxlan_del_port(vxlan, vxlanp->udp_port);
196 }
197}
198