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