]> git.proxmox.com Git - mirror_ovs.git/blame - lib/netdev-tc-offloads.c
dpif-netlink: Use netdev flow put api to insert a flow
[mirror_ovs.git] / lib / netdev-tc-offloads.c
CommitLineData
18ebd48c
PB
1/*
2 * Copyright (c) 2016 Mellanox Technologies, Ltd.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <config.h>
18#include "netdev-tc-offloads.h"
19#include <errno.h>
20#include <linux/if_ether.h>
21#include "openvswitch/hmap.h"
22#include "openvswitch/match.h"
23#include "openvswitch/ofpbuf.h"
24#include "openvswitch/thread.h"
25#include "openvswitch/types.h"
26#include "openvswitch/vlog.h"
27#include "netdev-provider.h"
28#include "netlink.h"
29#include "netlink-socket.h"
30#include "odp-netlink.h"
31#include "unaligned.h"
32#include "util.h"
33#include "hash.h"
34#include "dpif.h"
35#include "tc.h"
36
37VLOG_DEFINE_THIS_MODULE(netdev_tc_offloads);
38
8140a5ff
PB
39static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(60, 5);
40
9116730d
PB
41static struct hmap ufid_tc = HMAP_INITIALIZER(&ufid_tc);
42static struct ovs_mutex ufid_lock = OVS_MUTEX_INITIALIZER;
43
44/**
45 * struct ufid_tc_data - data entry for ufid_tc hmap.
46 * @ufid_node: Element in @ufid_tc hash table by ufid key.
47 * @tc_node: Element in @ufid_tc hash table by prio/handle/ifindex key.
48 * @ufid: ufid assigned to the flow
49 * @prio: tc priority
50 * @handle: tc handle
51 * @ifindex: netdev ifindex.
52 * @netdev: netdev associated with the tc rule
53 */
54struct ufid_tc_data {
55 struct hmap_node ufid_node;
56 struct hmap_node tc_node;
57 ovs_u128 ufid;
58 uint16_t prio;
59 uint32_t handle;
60 int ifindex;
61 struct netdev *netdev;
62};
63
64/* Remove matching ufid entry from ufid_tc hashmap. */
65static void
66del_ufid_tc_mapping(const ovs_u128 *ufid)
67{
68 size_t ufid_hash = hash_bytes(ufid, sizeof *ufid, 0);
69 struct ufid_tc_data *data;
70
71 ovs_mutex_lock(&ufid_lock);
72 HMAP_FOR_EACH_WITH_HASH(data, ufid_node, ufid_hash, &ufid_tc) {
73 if (ovs_u128_equals(*ufid, data->ufid)) {
74 break;
75 }
76 }
77
78 if (!data) {
79 ovs_mutex_unlock(&ufid_lock);
80 return;
81 }
82
83 hmap_remove(&ufid_tc, &data->ufid_node);
84 hmap_remove(&ufid_tc, &data->tc_node);
85 netdev_close(data->netdev);
86 free(data);
87 ovs_mutex_unlock(&ufid_lock);
88}
89
90/* Add ufid entry to ufid_tc hashmap.
91 * If entry exists already it will be replaced. */
92static void OVS_UNUSED
93add_ufid_tc_mapping(const ovs_u128 *ufid, int prio, int handle,
94 struct netdev *netdev, int ifindex)
95{
96 size_t ufid_hash = hash_bytes(ufid, sizeof *ufid, 0);
97 size_t tc_hash = hash_int(hash_int(prio, handle), ifindex);
98 struct ufid_tc_data *new_data = xzalloc(sizeof *new_data);
99
100 del_ufid_tc_mapping(ufid);
101
102 new_data->ufid = *ufid;
103 new_data->prio = prio;
104 new_data->handle = handle;
105 new_data->netdev = netdev_ref(netdev);
106 new_data->ifindex = ifindex;
107
108 ovs_mutex_lock(&ufid_lock);
109 hmap_insert(&ufid_tc, &new_data->ufid_node, ufid_hash);
110 hmap_insert(&ufid_tc, &new_data->tc_node, tc_hash);
111 ovs_mutex_unlock(&ufid_lock);
112}
113
114/* Get ufid from ufid_tc hashmap.
115 *
116 * If netdev output param is not NULL then the function will return
117 * associated netdev on success and a refcount is taken on that netdev.
118 * The caller is then responsible to close the netdev.
119 *
120 * Returns handle if successful and fill prio and netdev for that ufid.
121 * Otherwise returns 0.
122 */
123static int OVS_UNUSED
124get_ufid_tc_mapping(const ovs_u128 *ufid, int *prio, struct netdev **netdev)
125{
126 size_t ufid_hash = hash_bytes(ufid, sizeof *ufid, 0);
127 struct ufid_tc_data *data;
128 int handle = 0;
129
130 ovs_mutex_lock(&ufid_lock);
131 HMAP_FOR_EACH_WITH_HASH(data, ufid_node, ufid_hash, &ufid_tc) {
132 if (ovs_u128_equals(*ufid, data->ufid)) {
133 if (prio) {
134 *prio = data->prio;
135 }
136 if (netdev) {
137 *netdev = netdev_ref(data->netdev);
138 }
139 handle = data->handle;
140 break;
141 }
142 }
143 ovs_mutex_unlock(&ufid_lock);
144
145 return handle;
146}
147
148/* Find ufid entry in ufid_tc hashmap using prio, handle and netdev.
149 * The result is saved in ufid.
150 *
151 * Returns true on success.
152 */
8f7620e6 153static bool
9116730d
PB
154find_ufid(int prio, int handle, struct netdev *netdev, ovs_u128 *ufid)
155{
156 int ifindex = netdev_get_ifindex(netdev);
157 struct ufid_tc_data *data;
158 size_t tc_hash = hash_int(hash_int(prio, handle), ifindex);
159
160 ovs_mutex_lock(&ufid_lock);
161 HMAP_FOR_EACH_WITH_HASH(data, tc_node, tc_hash, &ufid_tc) {
162 if (data->prio == prio && data->handle == handle
163 && data->ifindex == ifindex) {
164 *ufid = data->ufid;
165 break;
166 }
167 }
168 ovs_mutex_unlock(&ufid_lock);
169
170 return (data != NULL);
171}
172
18ebd48c 173int
8140a5ff 174netdev_tc_flow_flush(struct netdev *netdev)
18ebd48c 175{
8140a5ff
PB
176 int ifindex = netdev_get_ifindex(netdev);
177
178 if (ifindex < 0) {
179 VLOG_ERR_RL(&error_rl, "failed to get ifindex for %s: %s",
180 netdev_get_name(netdev), ovs_strerror(-ifindex));
181 return -ifindex;
182 }
183
184 return tc_flush(ifindex);
18ebd48c
PB
185}
186
187int
188netdev_tc_flow_dump_create(struct netdev *netdev,
189 struct netdev_flow_dump **dump_out)
190{
8f7620e6
PB
191 struct netdev_flow_dump *dump;
192 int ifindex;
193
194 ifindex = netdev_get_ifindex(netdev);
195 if (ifindex < 0) {
196 VLOG_ERR_RL(&error_rl, "failed to get ifindex for %s: %s",
197 netdev_get_name(netdev), ovs_strerror(-ifindex));
198 return -ifindex;
199 }
18ebd48c 200
8f7620e6
PB
201 dump = xzalloc(sizeof *dump);
202 dump->nl_dump = xzalloc(sizeof *dump->nl_dump);
18ebd48c 203 dump->netdev = netdev_ref(netdev);
8f7620e6 204 tc_dump_flower_start(ifindex, dump->nl_dump);
18ebd48c
PB
205
206 *dump_out = dump;
207
208 return 0;
209}
210
211int
212netdev_tc_flow_dump_destroy(struct netdev_flow_dump *dump)
213{
8f7620e6 214 nl_dump_done(dump->nl_dump);
18ebd48c 215 netdev_close(dump->netdev);
8f7620e6 216 free(dump->nl_dump);
18ebd48c 217 free(dump);
8f7620e6
PB
218 return 0;
219}
220
221static int
222parse_tc_flower_to_match(struct tc_flower *flower,
223 struct match *match,
224 struct nlattr **actions,
225 struct dpif_flow_stats *stats,
226 struct ofpbuf *buf) {
227 size_t act_off;
228 struct tc_flower_key *key = &flower->key;
229 struct tc_flower_key *mask = &flower->mask;
230 odp_port_t outport = 0;
231
232 if (flower->ifindex_out) {
233 outport = netdev_ifindex_to_odp_port(flower->ifindex_out);
234 if (!outport) {
235 return ENOENT;
236 }
237 }
238
239 ofpbuf_clear(buf);
240
241 match_init_catchall(match);
242 match_set_dl_src_masked(match, key->src_mac, mask->src_mac);
243 match_set_dl_dst_masked(match, key->dst_mac, mask->dst_mac);
244
245 if (key->eth_type == htons(ETH_TYPE_VLAN)) {
246 match_set_dl_vlan(match, htons(key->vlan_id));
247 match_set_dl_vlan_pcp(match, key->vlan_prio);
248 match_set_dl_type(match, key->encap_eth_type);
249 flow_fix_vlan_tpid(&match->flow);
250 } else {
251 match_set_dl_type(match, key->eth_type);
252 }
253
254 if (key->ip_proto && is_ip_any(&match->flow)) {
255 match_set_nw_proto(match, key->ip_proto);
256 }
257
258 match_set_nw_src_masked(match, key->ipv4.ipv4_src, mask->ipv4.ipv4_src);
259 match_set_nw_dst_masked(match, key->ipv4.ipv4_dst, mask->ipv4.ipv4_dst);
260
261 match_set_ipv6_src_masked(match,
262 &key->ipv6.ipv6_src, &mask->ipv6.ipv6_src);
263 match_set_ipv6_dst_masked(match,
264 &key->ipv6.ipv6_dst, &mask->ipv6.ipv6_dst);
265
266 match_set_tp_dst_masked(match, key->dst_port, mask->dst_port);
267 match_set_tp_src_masked(match, key->src_port, mask->src_port);
268
269 if (flower->tunnel.tunnel) {
270 match_set_tun_id(match, flower->tunnel.id);
271 if (flower->tunnel.ipv4.ipv4_dst) {
272 match_set_tun_src(match, flower->tunnel.ipv4.ipv4_src);
273 match_set_tun_dst(match, flower->tunnel.ipv4.ipv4_dst);
274 } else if (!is_all_zeros(&flower->tunnel.ipv6.ipv6_dst,
275 sizeof flower->tunnel.ipv6.ipv6_dst)) {
276 match_set_tun_ipv6_src(match, &flower->tunnel.ipv6.ipv6_src);
277 match_set_tun_ipv6_dst(match, &flower->tunnel.ipv6.ipv6_dst);
278 }
279 if (flower->tunnel.tp_dst) {
280 match_set_tun_tp_dst(match, flower->tunnel.tp_dst);
281 }
282 }
283
284 act_off = nl_msg_start_nested(buf, OVS_FLOW_ATTR_ACTIONS);
285 {
286 if (flower->vlan_pop) {
287 nl_msg_put_flag(buf, OVS_ACTION_ATTR_POP_VLAN);
288 }
289
290 if (flower->vlan_push_id || flower->vlan_push_prio) {
291 struct ovs_action_push_vlan *push;
292 push = nl_msg_put_unspec_zero(buf, OVS_ACTION_ATTR_PUSH_VLAN,
293 sizeof *push);
294
295 push->vlan_tpid = htons(ETH_TYPE_VLAN);
296 push->vlan_tci = htons(flower->vlan_push_id
297 | (flower->vlan_push_prio << 13)
298 | VLAN_CFI);
299 }
300
301 if (flower->set.set) {
302 size_t set_offset = nl_msg_start_nested(buf, OVS_ACTION_ATTR_SET);
303 size_t tunnel_offset =
304 nl_msg_start_nested(buf, OVS_KEY_ATTR_TUNNEL);
305
306 nl_msg_put_be64(buf, OVS_TUNNEL_KEY_ATTR_ID, flower->set.id);
307 if (flower->set.ipv4.ipv4_src) {
308 nl_msg_put_be32(buf, OVS_TUNNEL_KEY_ATTR_IPV4_SRC,
309 flower->set.ipv4.ipv4_src);
310 }
311 if (flower->set.ipv4.ipv4_dst) {
312 nl_msg_put_be32(buf, OVS_TUNNEL_KEY_ATTR_IPV4_DST,
313 flower->set.ipv4.ipv4_dst);
314 }
315 if (!is_all_zeros(&flower->set.ipv6.ipv6_src,
316 sizeof flower->set.ipv6.ipv6_src)) {
317 nl_msg_put_in6_addr(buf, OVS_TUNNEL_KEY_ATTR_IPV6_SRC,
318 &flower->set.ipv6.ipv6_src);
319 }
320 if (!is_all_zeros(&flower->set.ipv6.ipv6_dst,
321 sizeof flower->set.ipv6.ipv6_dst)) {
322 nl_msg_put_in6_addr(buf, OVS_TUNNEL_KEY_ATTR_IPV6_DST,
323 &flower->set.ipv6.ipv6_dst);
324 }
325 nl_msg_put_be16(buf, OVS_TUNNEL_KEY_ATTR_TP_DST,
326 flower->set.tp_dst);
327
328 nl_msg_end_nested(buf, tunnel_offset);
329 nl_msg_end_nested(buf, set_offset);
330 }
331
332 if (flower->ifindex_out > 0) {
333 nl_msg_put_u32(buf, OVS_ACTION_ATTR_OUTPUT, odp_to_u32(outport));
334 }
335
336 }
337 nl_msg_end_nested(buf, act_off);
338
339 *actions = ofpbuf_at_assert(buf, act_off, sizeof(struct nlattr));
340
341 if (stats) {
342 memset(stats, 0, sizeof *stats);
343 stats->n_packets = get_32aligned_u64(&flower->stats.n_packets);
344 stats->n_bytes = get_32aligned_u64(&flower->stats.n_bytes);
345 stats->used = flower->lastused;
346 }
18ebd48c
PB
347
348 return 0;
349}
350
351bool
8f7620e6
PB
352netdev_tc_flow_dump_next(struct netdev_flow_dump *dump,
353 struct match *match,
354 struct nlattr **actions,
355 struct dpif_flow_stats *stats,
356 ovs_u128 *ufid,
357 struct ofpbuf *rbuffer,
358 struct ofpbuf *wbuffer)
18ebd48c 359{
8f7620e6
PB
360 struct ofpbuf nl_flow;
361
362 while (nl_dump_next(dump->nl_dump, &nl_flow, rbuffer)) {
363 struct tc_flower flower;
364 struct netdev *netdev = dump->netdev;
365
366 if (parse_netlink_to_tc_flower(&nl_flow, &flower)) {
367 continue;
368 }
369
370 if (parse_tc_flower_to_match(&flower, match, actions, stats,
371 wbuffer)) {
372 continue;
373 }
374
375 if (flower.act_cookie.len) {
376 *ufid = *((ovs_u128 *) flower.act_cookie.data);
377 } else if (!find_ufid(flower.prio, flower.handle, netdev, ufid)) {
378 continue;
379 }
380
381 match->wc.masks.in_port.odp_port = u32_to_odp(UINT32_MAX);
382 match->flow.in_port.odp_port = dump->port;
383
384 return true;
385 }
386
18ebd48c
PB
387 return false;
388}
389
390int
391netdev_tc_flow_put(struct netdev *netdev OVS_UNUSED,
392 struct match *match OVS_UNUSED,
393 struct nlattr *actions OVS_UNUSED,
394 size_t actions_len OVS_UNUSED,
395 const ovs_u128 *ufid OVS_UNUSED,
396 struct offload_info *info OVS_UNUSED,
397 struct dpif_flow_stats *stats OVS_UNUSED)
398{
399 return EOPNOTSUPP;
400}
401
402int
403netdev_tc_flow_get(struct netdev *netdev OVS_UNUSED,
404 struct match *match OVS_UNUSED,
405 struct nlattr **actions OVS_UNUSED,
406 const ovs_u128 *ufid OVS_UNUSED,
407 struct dpif_flow_stats *stats OVS_UNUSED,
408 struct ofpbuf *buf OVS_UNUSED)
409{
410 return EOPNOTSUPP;
411}
412
413int
414netdev_tc_flow_del(struct netdev *netdev OVS_UNUSED,
415 const ovs_u128 *ufid OVS_UNUSED,
416 struct dpif_flow_stats *stats OVS_UNUSED)
417{
418 return EOPNOTSUPP;
419}
420
421int
422netdev_tc_init_flow_api(struct netdev *netdev OVS_UNUSED)
423{
424 return 0;
425}