]> git.proxmox.com Git - mirror_ovs.git/blame - lib/netdev-tc-offloads.c
odp-execute: Rename 'may_steal' to 'should_steal'.
[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"
ef3767f5 19
18ebd48c
PB
20#include <errno.h>
21#include <linux/if_ether.h>
ef3767f5
JS
22
23#include "dpif.h"
24#include "hash.h"
18ebd48c
PB
25#include "openvswitch/hmap.h"
26#include "openvswitch/match.h"
27#include "openvswitch/ofpbuf.h"
28#include "openvswitch/thread.h"
29#include "openvswitch/types.h"
1ae83bb2 30#include "openvswitch/util.h"
18ebd48c 31#include "openvswitch/vlog.h"
ef3767f5 32#include "netdev-linux.h"
18ebd48c
PB
33#include "netlink.h"
34#include "netlink-socket.h"
35#include "odp-netlink.h"
1ae83bb2 36#include "odp-util.h"
ef3767f5 37#include "tc.h"
18ebd48c
PB
38#include "unaligned.h"
39#include "util.h"
18ebd48c
PB
40
41VLOG_DEFINE_THIS_MODULE(netdev_tc_offloads);
42
8140a5ff
PB
43static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(60, 5);
44
9116730d 45static struct hmap ufid_tc = HMAP_INITIALIZER(&ufid_tc);
d00eeded 46static bool multi_mask_per_prio = false;
1ae83bb2
PB
47
48struct netlink_field {
49 int offset;
50 int flower_offset;
51 int size;
52};
53
54static struct netlink_field set_flower_map[][3] = {
55 [OVS_KEY_ATTR_IPV4] = {
56 { offsetof(struct ovs_key_ipv4, ipv4_src),
57 offsetof(struct tc_flower_key, ipv4.ipv4_src),
58 MEMBER_SIZEOF(struct tc_flower_key, ipv4.ipv4_src)
59 },
60 { offsetof(struct ovs_key_ipv4, ipv4_dst),
61 offsetof(struct tc_flower_key, ipv4.ipv4_dst),
62 MEMBER_SIZEOF(struct tc_flower_key, ipv4.ipv4_dst)
63 },
64 { offsetof(struct ovs_key_ipv4, ipv4_ttl),
65 offsetof(struct tc_flower_key, ipv4.rewrite_ttl),
66 MEMBER_SIZEOF(struct tc_flower_key, ipv4.rewrite_ttl)
67 },
68 },
69 [OVS_KEY_ATTR_IPV6] = {
70 { offsetof(struct ovs_key_ipv6, ipv6_src),
71 offsetof(struct tc_flower_key, ipv6.ipv6_src),
72 MEMBER_SIZEOF(struct tc_flower_key, ipv6.ipv6_src)
73 },
74 { offsetof(struct ovs_key_ipv6, ipv6_dst),
75 offsetof(struct tc_flower_key, ipv6.ipv6_dst),
76 MEMBER_SIZEOF(struct tc_flower_key, ipv6.ipv6_dst)
77 },
78 },
79 [OVS_KEY_ATTR_ETHERNET] = {
80 { offsetof(struct ovs_key_ethernet, eth_src),
81 offsetof(struct tc_flower_key, src_mac),
82 MEMBER_SIZEOF(struct tc_flower_key, src_mac)
83 },
84 { offsetof(struct ovs_key_ethernet, eth_dst),
85 offsetof(struct tc_flower_key, dst_mac),
86 MEMBER_SIZEOF(struct tc_flower_key, dst_mac)
87 },
88 },
89 [OVS_KEY_ATTR_ETHERTYPE] = {
90 { 0,
91 offsetof(struct tc_flower_key, eth_type),
92 MEMBER_SIZEOF(struct tc_flower_key, eth_type)
93 },
94 },
95 [OVS_KEY_ATTR_TCP] = {
96 { offsetof(struct ovs_key_tcp, tcp_src),
97 offsetof(struct tc_flower_key, tcp_src),
98 MEMBER_SIZEOF(struct tc_flower_key, tcp_src)
99 },
100 { offsetof(struct ovs_key_tcp, tcp_dst),
101 offsetof(struct tc_flower_key, tcp_dst),
102 MEMBER_SIZEOF(struct tc_flower_key, tcp_dst)
103 },
104 },
105 [OVS_KEY_ATTR_UDP] = {
106 { offsetof(struct ovs_key_udp, udp_src),
107 offsetof(struct tc_flower_key, udp_src),
108 MEMBER_SIZEOF(struct tc_flower_key, udp_src)
109 },
110 { offsetof(struct ovs_key_udp, udp_dst),
111 offsetof(struct tc_flower_key, udp_dst),
112 MEMBER_SIZEOF(struct tc_flower_key, udp_dst)
113 },
114 },
115};
116
9116730d
PB
117static struct ovs_mutex ufid_lock = OVS_MUTEX_INITIALIZER;
118
119/**
120 * struct ufid_tc_data - data entry for ufid_tc hmap.
121 * @ufid_node: Element in @ufid_tc hash table by ufid key.
122 * @tc_node: Element in @ufid_tc hash table by prio/handle/ifindex key.
123 * @ufid: ufid assigned to the flow
124 * @prio: tc priority
125 * @handle: tc handle
126 * @ifindex: netdev ifindex.
127 * @netdev: netdev associated with the tc rule
128 */
129struct ufid_tc_data {
130 struct hmap_node ufid_node;
131 struct hmap_node tc_node;
132 ovs_u128 ufid;
133 uint16_t prio;
134 uint32_t handle;
135 int ifindex;
136 struct netdev *netdev;
137};
138
139/* Remove matching ufid entry from ufid_tc hashmap. */
140static void
141del_ufid_tc_mapping(const ovs_u128 *ufid)
142{
143 size_t ufid_hash = hash_bytes(ufid, sizeof *ufid, 0);
144 struct ufid_tc_data *data;
145
146 ovs_mutex_lock(&ufid_lock);
147 HMAP_FOR_EACH_WITH_HASH(data, ufid_node, ufid_hash, &ufid_tc) {
148 if (ovs_u128_equals(*ufid, data->ufid)) {
149 break;
150 }
151 }
152
153 if (!data) {
154 ovs_mutex_unlock(&ufid_lock);
155 return;
156 }
157
158 hmap_remove(&ufid_tc, &data->ufid_node);
159 hmap_remove(&ufid_tc, &data->tc_node);
160 netdev_close(data->netdev);
161 free(data);
162 ovs_mutex_unlock(&ufid_lock);
163}
164
165/* Add ufid entry to ufid_tc hashmap.
166 * If entry exists already it will be replaced. */
8f283af8 167static void
9116730d
PB
168add_ufid_tc_mapping(const ovs_u128 *ufid, int prio, int handle,
169 struct netdev *netdev, int ifindex)
170{
171 size_t ufid_hash = hash_bytes(ufid, sizeof *ufid, 0);
172 size_t tc_hash = hash_int(hash_int(prio, handle), ifindex);
173 struct ufid_tc_data *new_data = xzalloc(sizeof *new_data);
174
175 del_ufid_tc_mapping(ufid);
176
177 new_data->ufid = *ufid;
178 new_data->prio = prio;
179 new_data->handle = handle;
180 new_data->netdev = netdev_ref(netdev);
181 new_data->ifindex = ifindex;
182
183 ovs_mutex_lock(&ufid_lock);
184 hmap_insert(&ufid_tc, &new_data->ufid_node, ufid_hash);
185 hmap_insert(&ufid_tc, &new_data->tc_node, tc_hash);
186 ovs_mutex_unlock(&ufid_lock);
187}
188
189/* Get ufid from ufid_tc hashmap.
190 *
191 * If netdev output param is not NULL then the function will return
192 * associated netdev on success and a refcount is taken on that netdev.
193 * The caller is then responsible to close the netdev.
194 *
195 * Returns handle if successful and fill prio and netdev for that ufid.
196 * Otherwise returns 0.
197 */
8f283af8 198static int
9116730d
PB
199get_ufid_tc_mapping(const ovs_u128 *ufid, int *prio, struct netdev **netdev)
200{
201 size_t ufid_hash = hash_bytes(ufid, sizeof *ufid, 0);
202 struct ufid_tc_data *data;
203 int handle = 0;
204
205 ovs_mutex_lock(&ufid_lock);
206 HMAP_FOR_EACH_WITH_HASH(data, ufid_node, ufid_hash, &ufid_tc) {
207 if (ovs_u128_equals(*ufid, data->ufid)) {
208 if (prio) {
209 *prio = data->prio;
210 }
211 if (netdev) {
212 *netdev = netdev_ref(data->netdev);
213 }
214 handle = data->handle;
215 break;
216 }
217 }
218 ovs_mutex_unlock(&ufid_lock);
219
220 return handle;
221}
222
223/* Find ufid entry in ufid_tc hashmap using prio, handle and netdev.
224 * The result is saved in ufid.
225 *
226 * Returns true on success.
227 */
8f7620e6 228static bool
9116730d
PB
229find_ufid(int prio, int handle, struct netdev *netdev, ovs_u128 *ufid)
230{
231 int ifindex = netdev_get_ifindex(netdev);
232 struct ufid_tc_data *data;
233 size_t tc_hash = hash_int(hash_int(prio, handle), ifindex);
234
235 ovs_mutex_lock(&ufid_lock);
236 HMAP_FOR_EACH_WITH_HASH(data, tc_node, tc_hash, &ufid_tc) {
237 if (data->prio == prio && data->handle == handle
238 && data->ifindex == ifindex) {
239 *ufid = data->ufid;
240 break;
241 }
242 }
243 ovs_mutex_unlock(&ufid_lock);
244
245 return (data != NULL);
246}
247
d86eea7c
PB
248struct prio_map_data {
249 struct hmap_node node;
250 struct tc_flower_key mask;
251 ovs_be16 protocol;
252 uint16_t prio;
253};
254
255/* Get free prio for tc flower
256 * If prio is already allocated for mask/eth_type combination then return it.
257 * If not assign new prio.
258 *
259 * Return prio on success or 0 if we are out of prios.
260 */
8f283af8 261static uint16_t
d86eea7c
PB
262get_prio_for_tc_flower(struct tc_flower *flower)
263{
264 static struct hmap prios = HMAP_INITIALIZER(&prios);
265 static struct ovs_mutex prios_lock = OVS_MUTEX_INITIALIZER;
266 static uint16_t last_prio = 0;
267 size_t key_len = sizeof(struct tc_flower_key);
d00eeded 268 size_t hash = hash_int((OVS_FORCE uint32_t) flower->key.eth_type, 0);
d86eea7c
PB
269 struct prio_map_data *data;
270 struct prio_map_data *new_data;
271
d00eeded
PB
272 if (!multi_mask_per_prio) {
273 hash = hash_bytes(&flower->mask, key_len, hash);
274 }
275
d86eea7c
PB
276 /* We can use the same prio for same mask/eth combination but must have
277 * different prio if not. Flower classifier will reject same prio for
d00eeded 278 * different mask combination unless multi mask per prio is supported. */
d86eea7c
PB
279 ovs_mutex_lock(&prios_lock);
280 HMAP_FOR_EACH_WITH_HASH(data, node, hash, &prios) {
d00eeded
PB
281 if ((multi_mask_per_prio
282 || !memcmp(&flower->mask, &data->mask, key_len))
d86eea7c
PB
283 && data->protocol == flower->key.eth_type) {
284 ovs_mutex_unlock(&prios_lock);
285 return data->prio;
286 }
287 }
288
289 if (last_prio == UINT16_MAX) {
290 /* last_prio can overflow if there will be many different kinds of
291 * flows which shouldn't happen organically. */
292 ovs_mutex_unlock(&prios_lock);
293 return 0;
294 }
295
296 new_data = xzalloc(sizeof *new_data);
297 memcpy(&new_data->mask, &flower->mask, key_len);
298 new_data->prio = ++last_prio;
299 new_data->protocol = flower->key.eth_type;
300 hmap_insert(&prios, &new_data->node, hash);
301 ovs_mutex_unlock(&prios_lock);
302
303 return new_data->prio;
304}
305
18ebd48c 306int
8140a5ff 307netdev_tc_flow_flush(struct netdev *netdev)
18ebd48c 308{
8140a5ff
PB
309 int ifindex = netdev_get_ifindex(netdev);
310
311 if (ifindex < 0) {
0164f10c 312 VLOG_ERR_RL(&error_rl, "flow_flush: failed to get ifindex for %s: %s",
8140a5ff
PB
313 netdev_get_name(netdev), ovs_strerror(-ifindex));
314 return -ifindex;
315 }
316
317 return tc_flush(ifindex);
18ebd48c
PB
318}
319
320int
321netdev_tc_flow_dump_create(struct netdev *netdev,
322 struct netdev_flow_dump **dump_out)
323{
8f7620e6
PB
324 struct netdev_flow_dump *dump;
325 int ifindex;
326
327 ifindex = netdev_get_ifindex(netdev);
328 if (ifindex < 0) {
0164f10c 329 VLOG_ERR_RL(&error_rl, "dump_create: failed to get ifindex for %s: %s",
8f7620e6
PB
330 netdev_get_name(netdev), ovs_strerror(-ifindex));
331 return -ifindex;
332 }
18ebd48c 333
8f7620e6
PB
334 dump = xzalloc(sizeof *dump);
335 dump->nl_dump = xzalloc(sizeof *dump->nl_dump);
18ebd48c 336 dump->netdev = netdev_ref(netdev);
8f7620e6 337 tc_dump_flower_start(ifindex, dump->nl_dump);
18ebd48c
PB
338
339 *dump_out = dump;
340
341 return 0;
342}
343
344int
345netdev_tc_flow_dump_destroy(struct netdev_flow_dump *dump)
346{
8f7620e6 347 nl_dump_done(dump->nl_dump);
18ebd48c 348 netdev_close(dump->netdev);
8f7620e6 349 free(dump->nl_dump);
18ebd48c 350 free(dump);
8f7620e6
PB
351 return 0;
352}
353
1ae83bb2
PB
354static void
355parse_flower_rewrite_to_netlink_action(struct ofpbuf *buf,
356 struct tc_flower *flower)
357{
358 char *mask = (char *) &flower->rewrite.mask;
359 char *data = (char *) &flower->rewrite.key;
360
361 for (int type = 0; type < ARRAY_SIZE(set_flower_map); type++) {
362 char *put = NULL;
363 size_t nested = 0;
364 int len = ovs_flow_key_attr_lens[type].len;
365
366 if (len <= 0) {
367 continue;
368 }
369
370 for (int j = 0; j < ARRAY_SIZE(set_flower_map[type]); j++) {
371 struct netlink_field *f = &set_flower_map[type][j];
372
373 if (!f->size) {
374 break;
375 }
376
377 if (!is_all_zeros(mask + f->flower_offset, f->size)) {
378 if (!put) {
379 nested = nl_msg_start_nested(buf,
380 OVS_ACTION_ATTR_SET_MASKED);
381 put = nl_msg_put_unspec_zero(buf, type, len * 2);
382 }
383
384 memcpy(put + f->offset, data + f->flower_offset, f->size);
385 memcpy(put + len + f->offset,
386 mask + f->flower_offset, f->size);
387 }
388 }
389
390 if (put) {
391 nl_msg_end_nested(buf, nested);
392 }
393 }
394}
395
8f7620e6
PB
396static int
397parse_tc_flower_to_match(struct tc_flower *flower,
398 struct match *match,
399 struct nlattr **actions,
400 struct dpif_flow_stats *stats,
0c70132c
CM
401 struct ofpbuf *buf)
402{
8f7620e6
PB
403 size_t act_off;
404 struct tc_flower_key *key = &flower->key;
405 struct tc_flower_key *mask = &flower->mask;
406 odp_port_t outport = 0;
0c70132c
CM
407 struct tc_action *action;
408 int i;
8f7620e6
PB
409
410 ofpbuf_clear(buf);
411
412 match_init_catchall(match);
413 match_set_dl_src_masked(match, key->src_mac, mask->src_mac);
414 match_set_dl_dst_masked(match, key->dst_mac, mask->dst_mac);
415
416 if (key->eth_type == htons(ETH_TYPE_VLAN)) {
417 match_set_dl_vlan(match, htons(key->vlan_id));
418 match_set_dl_vlan_pcp(match, key->vlan_prio);
419 match_set_dl_type(match, key->encap_eth_type);
420 flow_fix_vlan_tpid(&match->flow);
421 } else {
422 match_set_dl_type(match, key->eth_type);
423 }
424
83a87fae
PB
425 if (is_ip_any(&match->flow)) {
426 if (key->ip_proto) {
427 match_set_nw_proto(match, key->ip_proto);
428 }
8f7620e6 429
ab7ecf26
PB
430 match_set_nw_ttl_masked(match, key->ip_ttl, mask->ip_ttl);
431
83e86606
RD
432 if (mask->flags) {
433 uint8_t flags = 0;
434 uint8_t flags_mask = 0;
435
436 if (mask->flags & TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT) {
437 if (key->flags & TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT) {
438 flags |= FLOW_NW_FRAG_ANY;
439 }
440 flags_mask |= FLOW_NW_FRAG_ANY;
441 }
442
443 if (mask->flags & TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST) {
444 if (!(key->flags & TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST)) {
445 flags |= FLOW_NW_FRAG_LATER;
446 }
447 flags_mask |= FLOW_NW_FRAG_LATER;
448 }
449
450 match_set_nw_frag_masked(match, flags, flags_mask);
451 }
452
83a87fae
PB
453 match_set_nw_src_masked(match, key->ipv4.ipv4_src, mask->ipv4.ipv4_src);
454 match_set_nw_dst_masked(match, key->ipv4.ipv4_dst, mask->ipv4.ipv4_dst);
8f7620e6 455
83a87fae
PB
456 match_set_ipv6_src_masked(match,
457 &key->ipv6.ipv6_src, &mask->ipv6.ipv6_src);
458 match_set_ipv6_dst_masked(match,
459 &key->ipv6.ipv6_dst, &mask->ipv6.ipv6_dst);
8f7620e6 460
2b1d9fa9
PB
461 if (key->ip_proto == IPPROTO_TCP) {
462 match_set_tp_dst_masked(match, key->tcp_dst, mask->tcp_dst);
463 match_set_tp_src_masked(match, key->tcp_src, mask->tcp_src);
f2698407 464 match_set_tcp_flags_masked(match, key->tcp_flags, mask->tcp_flags);
2b1d9fa9
PB
465 } else if (key->ip_proto == IPPROTO_UDP) {
466 match_set_tp_dst_masked(match, key->udp_dst, mask->udp_dst);
467 match_set_tp_src_masked(match, key->udp_src, mask->udp_src);
a238dbb5
RD
468 } else if (key->ip_proto == IPPROTO_SCTP) {
469 match_set_tp_dst_masked(match, key->sctp_dst, mask->sctp_dst);
470 match_set_tp_src_masked(match, key->sctp_src, mask->sctp_src);
2b1d9fa9
PB
471 }
472 }
8f7620e6
PB
473
474 if (flower->tunnel.tunnel) {
475 match_set_tun_id(match, flower->tunnel.id);
476 if (flower->tunnel.ipv4.ipv4_dst) {
477 match_set_tun_src(match, flower->tunnel.ipv4.ipv4_src);
478 match_set_tun_dst(match, flower->tunnel.ipv4.ipv4_dst);
479 } else if (!is_all_zeros(&flower->tunnel.ipv6.ipv6_dst,
480 sizeof flower->tunnel.ipv6.ipv6_dst)) {
481 match_set_tun_ipv6_src(match, &flower->tunnel.ipv6.ipv6_src);
482 match_set_tun_ipv6_dst(match, &flower->tunnel.ipv6.ipv6_dst);
483 }
484 if (flower->tunnel.tp_dst) {
485 match_set_tun_tp_dst(match, flower->tunnel.tp_dst);
486 }
487 }
488
489 act_off = nl_msg_start_nested(buf, OVS_FLOW_ATTR_ACTIONS);
490 {
0c70132c
CM
491 action = flower->actions;
492 for (i = 0; i < flower->action_count; i++, action++) {
493 switch (action->type) {
494 case TC_ACT_VLAN_POP: {
495 nl_msg_put_flag(buf, OVS_ACTION_ATTR_POP_VLAN);
8f7620e6 496 }
0c70132c
CM
497 break;
498 case TC_ACT_VLAN_PUSH: {
499 struct ovs_action_push_vlan *push;
500
501 push = nl_msg_put_unspec_zero(buf, OVS_ACTION_ATTR_PUSH_VLAN,
502 sizeof *push);
503 push->vlan_tpid = htons(ETH_TYPE_VLAN);
504 push->vlan_tci = htons(action->vlan.vlan_push_id
505 | (action->vlan.vlan_push_prio << 13)
506 | VLAN_CFI);
8f7620e6 507 }
0c70132c
CM
508 break;
509 case TC_ACT_PEDIT: {
510 parse_flower_rewrite_to_netlink_action(buf, flower);
8f7620e6 511 }
0c70132c
CM
512 break;
513 case TC_ACT_ENCAP: {
514 size_t set_offset = nl_msg_start_nested(buf, OVS_ACTION_ATTR_SET);
515 size_t tunnel_offset =
516 nl_msg_start_nested(buf, OVS_KEY_ATTR_TUNNEL);
517
518 nl_msg_put_be64(buf, OVS_TUNNEL_KEY_ATTR_ID, action->encap.id);
519 if (action->encap.ipv4.ipv4_src) {
520 nl_msg_put_be32(buf, OVS_TUNNEL_KEY_ATTR_IPV4_SRC,
521 action->encap.ipv4.ipv4_src);
522 }
523 if (action->encap.ipv4.ipv4_dst) {
524 nl_msg_put_be32(buf, OVS_TUNNEL_KEY_ATTR_IPV4_DST,
525 action->encap.ipv4.ipv4_dst);
526 }
527 if (!is_all_zeros(&action->encap.ipv6.ipv6_src,
528 sizeof action->encap.ipv6.ipv6_src)) {
529 nl_msg_put_in6_addr(buf, OVS_TUNNEL_KEY_ATTR_IPV6_SRC,
530 &action->encap.ipv6.ipv6_src);
531 }
532 if (!is_all_zeros(&action->encap.ipv6.ipv6_dst,
533 sizeof action->encap.ipv6.ipv6_dst)) {
534 nl_msg_put_in6_addr(buf, OVS_TUNNEL_KEY_ATTR_IPV6_DST,
535 &action->encap.ipv6.ipv6_dst);
536 }
537 nl_msg_put_be16(buf, OVS_TUNNEL_KEY_ATTR_TP_DST,
538 action->encap.tp_dst);
8f7620e6 539
0c70132c
CM
540 nl_msg_end_nested(buf, tunnel_offset);
541 nl_msg_end_nested(buf, set_offset);
542 }
543 break;
544 case TC_ACT_OUTPUT: {
545 if (action->ifindex_out) {
546 outport = netdev_ifindex_to_odp_port(action->ifindex_out);
547 if (!outport) {
548 return ENOENT;
549 }
550 }
551 nl_msg_put_u32(buf, OVS_ACTION_ATTR_OUTPUT, odp_to_u32(outport));
552 }
553 break;
554 }
8f7620e6 555 }
8f7620e6
PB
556 }
557 nl_msg_end_nested(buf, act_off);
558
559 *actions = ofpbuf_at_assert(buf, act_off, sizeof(struct nlattr));
560
561 if (stats) {
562 memset(stats, 0, sizeof *stats);
563 stats->n_packets = get_32aligned_u64(&flower->stats.n_packets);
564 stats->n_bytes = get_32aligned_u64(&flower->stats.n_bytes);
565 stats->used = flower->lastused;
566 }
18ebd48c
PB
567
568 return 0;
569}
570
571bool
8f7620e6
PB
572netdev_tc_flow_dump_next(struct netdev_flow_dump *dump,
573 struct match *match,
574 struct nlattr **actions,
575 struct dpif_flow_stats *stats,
576 ovs_u128 *ufid,
577 struct ofpbuf *rbuffer,
578 struct ofpbuf *wbuffer)
18ebd48c 579{
8f7620e6
PB
580 struct ofpbuf nl_flow;
581
582 while (nl_dump_next(dump->nl_dump, &nl_flow, rbuffer)) {
583 struct tc_flower flower;
584 struct netdev *netdev = dump->netdev;
585
586 if (parse_netlink_to_tc_flower(&nl_flow, &flower)) {
587 continue;
588 }
589
590 if (parse_tc_flower_to_match(&flower, match, actions, stats,
591 wbuffer)) {
592 continue;
593 }
594
595 if (flower.act_cookie.len) {
596 *ufid = *((ovs_u128 *) flower.act_cookie.data);
597 } else if (!find_ufid(flower.prio, flower.handle, netdev, ufid)) {
598 continue;
599 }
600
601 match->wc.masks.in_port.odp_port = u32_to_odp(UINT32_MAX);
602 match->flow.in_port.odp_port = dump->port;
603
604 return true;
605 }
606
18ebd48c
PB
607 return false;
608}
609
1ae83bb2
PB
610static int
611parse_put_flow_set_masked_action(struct tc_flower *flower,
0c70132c 612 struct tc_action *action,
1ae83bb2
PB
613 const struct nlattr *set,
614 size_t set_len,
615 bool hasmask)
616{
617 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
40f58368
BP
618 uint64_t set_stub[1024 / 8];
619 struct ofpbuf set_buf = OFPBUF_STUB_INITIALIZER(set_stub);
620 char *set_data, *set_mask;
1ae83bb2
PB
621 char *key = (char *) &flower->rewrite.key;
622 char *mask = (char *) &flower->rewrite.mask;
623 const struct nlattr *attr;
624 int i, j, type;
625 size_t size;
626
627 /* copy so we can set attr mask to 0 for used ovs key struct members */
40f58368 628 attr = ofpbuf_put(&set_buf, set, set_len);
1ae83bb2
PB
629
630 type = nl_attr_type(attr);
631 size = nl_attr_get_size(attr) / 2;
632 set_data = CONST_CAST(char *, nl_attr_get(attr));
633 set_mask = set_data + size;
634
635 if (type >= ARRAY_SIZE(set_flower_map)
636 || !set_flower_map[type][0].size) {
637 VLOG_DBG_RL(&rl, "unsupported set action type: %d", type);
40f58368 638 ofpbuf_uninit(&set_buf);
1ae83bb2
PB
639 return EOPNOTSUPP;
640 }
641
642 for (i = 0; i < ARRAY_SIZE(set_flower_map[type]); i++) {
643 struct netlink_field *f = &set_flower_map[type][i];
644
645 if (!f->size) {
646 break;
647 }
648
649 /* copy masked value */
650 for (j = 0; j < f->size; j++) {
651 char maskval = hasmask ? set_mask[f->offset + j] : 0xFF;
652
653 key[f->flower_offset + j] = maskval & set_data[f->offset + j];
654 mask[f->flower_offset + j] = maskval;
655
656 }
657
658 /* set its mask to 0 to show it's been used. */
659 if (hasmask) {
660 memset(set_mask + f->offset, 0, f->size);
661 }
662 }
663
664 if (!is_all_zeros(&flower->rewrite, sizeof flower->rewrite)) {
0c70132c
CM
665 if (flower->rewrite.rewrite == false) {
666 flower->rewrite.rewrite = true;
667 action->type = TC_ACT_PEDIT;
668 flower->action_count++;
669 }
1ae83bb2
PB
670 }
671
672 if (hasmask && !is_all_zeros(set_mask, size)) {
673 VLOG_DBG_RL(&rl, "unsupported sub attribute of set action type %d",
674 type);
40f58368 675 ofpbuf_uninit(&set_buf);
1ae83bb2
PB
676 return EOPNOTSUPP;
677 }
678
40f58368 679 ofpbuf_uninit(&set_buf);
1ae83bb2
PB
680 return 0;
681}
682
8f283af8 683static int
0c70132c
CM
684parse_put_flow_set_action(struct tc_flower *flower, struct tc_action *action,
685 const struct nlattr *set, size_t set_len)
8f283af8 686{
518bbe99
PB
687 const struct nlattr *tunnel;
688 const struct nlattr *tun_attr;
689 size_t tun_left, tunnel_len;
690
691 if (nl_attr_type(set) != OVS_KEY_ATTR_TUNNEL) {
0c70132c
CM
692 return parse_put_flow_set_masked_action(flower, action, set,
693 set_len, false);
518bbe99
PB
694 }
695
696 tunnel = nl_attr_get(set);
697 tunnel_len = nl_attr_get_size(set);
698
0c70132c
CM
699 action->type = TC_ACT_ENCAP;
700 flower->action_count++;
518bbe99
PB
701 NL_ATTR_FOR_EACH_UNSAFE(tun_attr, tun_left, tunnel, tunnel_len) {
702 switch (nl_attr_type(tun_attr)) {
703 case OVS_TUNNEL_KEY_ATTR_ID: {
0c70132c 704 action->encap.id = nl_attr_get_be64(tun_attr);
518bbe99
PB
705 }
706 break;
707 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: {
0c70132c 708 action->encap.ipv4.ipv4_src = nl_attr_get_be32(tun_attr);
518bbe99
PB
709 }
710 break;
711 case OVS_TUNNEL_KEY_ATTR_IPV4_DST: {
0c70132c 712 action->encap.ipv4.ipv4_dst = nl_attr_get_be32(tun_attr);
518bbe99
PB
713 }
714 break;
715 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC: {
0c70132c 716 action->encap.ipv6.ipv6_src =
518bbe99
PB
717 nl_attr_get_in6_addr(tun_attr);
718 }
719 break;
720 case OVS_TUNNEL_KEY_ATTR_IPV6_DST: {
0c70132c 721 action->encap.ipv6.ipv6_dst =
518bbe99
PB
722 nl_attr_get_in6_addr(tun_attr);
723 }
724 break;
725 case OVS_TUNNEL_KEY_ATTR_TP_SRC: {
0c70132c 726 action->encap.tp_src = nl_attr_get_be16(tun_attr);
518bbe99
PB
727 }
728 break;
729 case OVS_TUNNEL_KEY_ATTR_TP_DST: {
0c70132c 730 action->encap.tp_dst = nl_attr_get_be16(tun_attr);
518bbe99
PB
731 }
732 break;
8f283af8
PB
733 }
734 }
518bbe99 735
8f283af8
PB
736 return 0;
737}
738
739static int
740test_key_and_mask(struct match *match)
741{
742 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
743 const struct flow *key = &match->flow;
744 struct flow *mask = &match->wc.masks;
745
746 if (mask->pkt_mark) {
747 VLOG_DBG_RL(&rl, "offloading attribute pkt_mark isn't supported");
748 return EOPNOTSUPP;
749 }
750
751 if (mask->recirc_id && key->recirc_id) {
752 VLOG_DBG_RL(&rl, "offloading attribute recirc_id isn't supported");
753 return EOPNOTSUPP;
754 }
755 mask->recirc_id = 0;
756
757 if (mask->dp_hash) {
758 VLOG_DBG_RL(&rl, "offloading attribute dp_hash isn't supported");
759 return EOPNOTSUPP;
760 }
761
762 if (mask->conj_id) {
763 VLOG_DBG_RL(&rl, "offloading attribute conj_id isn't supported");
764 return EOPNOTSUPP;
765 }
766
767 if (mask->skb_priority) {
768 VLOG_DBG_RL(&rl, "offloading attribute skb_priority isn't supported");
769 return EOPNOTSUPP;
770 }
771
772 if (mask->actset_output) {
773 VLOG_DBG_RL(&rl,
774 "offloading attribute actset_output isn't supported");
775 return EOPNOTSUPP;
776 }
777
778 if (mask->ct_state) {
779 VLOG_DBG_RL(&rl, "offloading attribute ct_state isn't supported");
780 return EOPNOTSUPP;
781 }
782
783 if (mask->ct_zone) {
784 VLOG_DBG_RL(&rl, "offloading attribute ct_zone isn't supported");
785 return EOPNOTSUPP;
786 }
787
788 if (mask->ct_mark) {
789 VLOG_DBG_RL(&rl, "offloading attribute ct_mark isn't supported");
790 return EOPNOTSUPP;
791 }
792
793 if (mask->packet_type && key->packet_type) {
794 VLOG_DBG_RL(&rl, "offloading attribute packet_type isn't supported");
795 return EOPNOTSUPP;
796 }
797 mask->packet_type = 0;
798
799 if (!ovs_u128_is_zero(mask->ct_label)) {
800 VLOG_DBG_RL(&rl, "offloading attribute ct_label isn't supported");
801 return EOPNOTSUPP;
802 }
803
804 for (int i = 0; i < FLOW_N_REGS; i++) {
805 if (mask->regs[i]) {
806 VLOG_DBG_RL(&rl,
807 "offloading attribute regs[%d] isn't supported", i);
808 return EOPNOTSUPP;
809 }
810 }
811
812 if (mask->metadata) {
813 VLOG_DBG_RL(&rl, "offloading attribute metadata isn't supported");
814 return EOPNOTSUPP;
815 }
816
817 if (mask->nw_tos) {
818 VLOG_DBG_RL(&rl, "offloading attribute nw_tos isn't supported");
819 return EOPNOTSUPP;
820 }
821
8f283af8
PB
822 for (int i = 0; i < FLOW_MAX_MPLS_LABELS; i++) {
823 if (mask->mpls_lse[i]) {
824 VLOG_DBG_RL(&rl, "offloading attribute mpls_lse isn't supported");
825 return EOPNOTSUPP;
826 }
827 }
828
829 if (key->dl_type == htons(ETH_TYPE_IP) &&
830 key->nw_proto == IPPROTO_ICMP) {
831 if (mask->tp_src) {
832 VLOG_DBG_RL(&rl,
833 "offloading attribute icmp_type isn't supported");
834 return EOPNOTSUPP;
835 }
836 if (mask->tp_dst) {
837 VLOG_DBG_RL(&rl,
838 "offloading attribute icmp_code isn't supported");
839 return EOPNOTSUPP;
840 }
841 } else if (key->dl_type == htons(ETH_TYPE_IP) &&
842 key->nw_proto == IPPROTO_IGMP) {
843 if (mask->tp_src) {
844 VLOG_DBG_RL(&rl,
845 "offloading attribute igmp_type isn't supported");
846 return EOPNOTSUPP;
847 }
848 if (mask->tp_dst) {
849 VLOG_DBG_RL(&rl,
850 "offloading attribute igmp_code isn't supported");
851 return EOPNOTSUPP;
852 }
853 } else if (key->dl_type == htons(ETH_TYPE_IPV6) &&
854 key->nw_proto == IPPROTO_ICMPV6) {
855 if (mask->tp_src) {
856 VLOG_DBG_RL(&rl,
857 "offloading attribute icmp_type isn't supported");
858 return EOPNOTSUPP;
859 }
860 if (mask->tp_dst) {
861 VLOG_DBG_RL(&rl,
862 "offloading attribute icmp_code isn't supported");
863 return EOPNOTSUPP;
864 }
865 }
8f283af8
PB
866
867 if (!is_all_zeros(mask, sizeof *mask)) {
868 VLOG_DBG_RL(&rl, "offloading isn't supported, unknown attribute");
869 return EOPNOTSUPP;
870 }
871
872 return 0;
873}
874
18ebd48c 875int
8f283af8
PB
876netdev_tc_flow_put(struct netdev *netdev, struct match *match,
877 struct nlattr *actions, size_t actions_len,
878 const ovs_u128 *ufid, struct offload_info *info,
18ebd48c
PB
879 struct dpif_flow_stats *stats OVS_UNUSED)
880{
8f283af8
PB
881 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
882 struct tc_flower flower;
883 const struct flow *key = &match->flow;
884 struct flow *mask = &match->wc.masks;
885 const struct flow_tnl *tnl = &match->flow.tunnel;
0c70132c 886 struct tc_action *action;
8f283af8
PB
887 struct nlattr *nla;
888 size_t left;
889 int prio = 0;
890 int handle;
891 int ifindex;
892 int err;
893
894 ifindex = netdev_get_ifindex(netdev);
895 if (ifindex < 0) {
0164f10c 896 VLOG_ERR_RL(&error_rl, "flow_put: failed to get ifindex for %s: %s",
8f283af8
PB
897 netdev_get_name(netdev), ovs_strerror(-ifindex));
898 return -ifindex;
899 }
900
901 memset(&flower, 0, sizeof flower);
902
a90120fc 903 if (flow_tnl_dst_is_set(&key->tunnel)) {
8f283af8
PB
904 VLOG_DBG_RL(&rl,
905 "tunnel: id %#" PRIx64 " src " IP_FMT
906 " dst " IP_FMT " tp_src %d tp_dst %d",
907 ntohll(tnl->tun_id),
908 IP_ARGS(tnl->ip_src), IP_ARGS(tnl->ip_dst),
909 ntohs(tnl->tp_src), ntohs(tnl->tp_dst));
910 flower.tunnel.id = tnl->tun_id;
911 flower.tunnel.ipv4.ipv4_src = tnl->ip_src;
912 flower.tunnel.ipv4.ipv4_dst = tnl->ip_dst;
913 flower.tunnel.ipv6.ipv6_src = tnl->ipv6_src;
914 flower.tunnel.ipv6.ipv6_dst = tnl->ipv6_dst;
915 flower.tunnel.tp_src = tnl->tp_src;
916 flower.tunnel.tp_dst = tnl->tp_dst;
917 flower.tunnel.tunnel = true;
8f283af8 918 }
a90120fc 919 memset(&mask->tunnel, 0, sizeof mask->tunnel);
8f283af8
PB
920
921 flower.key.eth_type = key->dl_type;
922 flower.mask.eth_type = mask->dl_type;
923
924 if (mask->vlans[0].tci) {
925 ovs_be16 vid_mask = mask->vlans[0].tci & htons(VLAN_VID_MASK);
926 ovs_be16 pcp_mask = mask->vlans[0].tci & htons(VLAN_PCP_MASK);
927 ovs_be16 cfi = mask->vlans[0].tci & htons(VLAN_CFI);
928
929 if (cfi && key->vlans[0].tci & htons(VLAN_CFI)
930 && (!vid_mask || vid_mask == htons(VLAN_VID_MASK))
931 && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK))
932 && (vid_mask || pcp_mask)) {
933 if (vid_mask) {
934 flower.key.vlan_id = vlan_tci_to_vid(key->vlans[0].tci);
935 VLOG_DBG_RL(&rl, "vlan_id: %d\n", flower.key.vlan_id);
936 }
937 if (pcp_mask) {
938 flower.key.vlan_prio = vlan_tci_to_pcp(key->vlans[0].tci);
939 VLOG_DBG_RL(&rl, "vlan_prio: %d\n", flower.key.vlan_prio);
940 }
941 flower.key.encap_eth_type = flower.key.eth_type;
942 flower.key.eth_type = htons(ETH_TYPE_VLAN);
943 } else if (mask->vlans[0].tci == htons(0xffff) &&
944 ntohs(key->vlans[0].tci) == 0) {
945 /* exact && no vlan */
946 } else {
947 /* partial mask */
948 return EOPNOTSUPP;
949 }
950 } else if (mask->vlans[1].tci) {
951 return EOPNOTSUPP;
952 }
953 memset(mask->vlans, 0, sizeof mask->vlans);
954
955 flower.key.dst_mac = key->dl_dst;
956 flower.mask.dst_mac = mask->dl_dst;
957 flower.key.src_mac = key->dl_src;
958 flower.mask.src_mac = mask->dl_src;
959 memset(&mask->dl_dst, 0, sizeof mask->dl_dst);
960 memset(&mask->dl_src, 0, sizeof mask->dl_src);
961 mask->dl_type = 0;
962 mask->in_port.odp_port = 0;
963
964 if (is_ip_any(key)) {
965 flower.key.ip_proto = key->nw_proto;
966 flower.mask.ip_proto = mask->nw_proto;
ab7ecf26
PB
967 flower.key.ip_ttl = key->nw_ttl;
968 flower.mask.ip_ttl = mask->nw_ttl;
8f283af8 969
45a60c21
RD
970 if (mask->nw_frag & FLOW_NW_FRAG_ANY) {
971 flower.mask.flags |= TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT;
972
973 if (key->nw_frag & FLOW_NW_FRAG_ANY) {
83e86606 974 flower.key.flags |= TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT;
83e86606 975
45a60c21
RD
976 if (mask->nw_frag & FLOW_NW_FRAG_LATER) {
977 flower.mask.flags |= TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST;
978
979 if (!(key->nw_frag & FLOW_NW_FRAG_LATER)) {
980 flower.key.flags |= TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST;
981 }
982 }
983 }
984
83e86606
RD
985 mask->nw_frag = 0;
986 }
987
2b1d9fa9
PB
988 if (key->nw_proto == IPPROTO_TCP) {
989 flower.key.tcp_dst = key->tp_dst;
990 flower.mask.tcp_dst = mask->tp_dst;
991 flower.key.tcp_src = key->tp_src;
992 flower.mask.tcp_src = mask->tp_src;
f2698407
PB
993 flower.key.tcp_flags = key->tcp_flags;
994 flower.mask.tcp_flags = mask->tcp_flags;
2b1d9fa9
PB
995 mask->tp_src = 0;
996 mask->tp_dst = 0;
f2698407 997 mask->tcp_flags = 0;
2b1d9fa9
PB
998 } else if (key->nw_proto == IPPROTO_UDP) {
999 flower.key.udp_dst = key->tp_dst;
1000 flower.mask.udp_dst = mask->tp_dst;
1001 flower.key.udp_src = key->tp_src;
1002 flower.mask.udp_src = mask->tp_src;
1003 mask->tp_src = 0;
1004 mask->tp_dst = 0;
1005 } else if (key->nw_proto == IPPROTO_SCTP) {
1006 flower.key.sctp_dst = key->tp_dst;
1007 flower.mask.sctp_dst = mask->tp_dst;
1008 flower.key.sctp_src = key->tp_src;
1009 flower.mask.sctp_src = mask->tp_src;
8f283af8
PB
1010 mask->tp_src = 0;
1011 mask->tp_dst = 0;
1012 }
1013
8f283af8
PB
1014 mask->nw_tos = 0;
1015 mask->nw_proto = 0;
ab7ecf26 1016 mask->nw_ttl = 0;
8f283af8
PB
1017
1018 if (key->dl_type == htons(ETH_P_IP)) {
1019 flower.key.ipv4.ipv4_src = key->nw_src;
1020 flower.mask.ipv4.ipv4_src = mask->nw_src;
1021 flower.key.ipv4.ipv4_dst = key->nw_dst;
1022 flower.mask.ipv4.ipv4_dst = mask->nw_dst;
1023 mask->nw_src = 0;
1024 mask->nw_dst = 0;
1025 } else if (key->dl_type == htons(ETH_P_IPV6)) {
1026 flower.key.ipv6.ipv6_src = key->ipv6_src;
1027 flower.mask.ipv6.ipv6_src = mask->ipv6_src;
1028 flower.key.ipv6.ipv6_dst = key->ipv6_dst;
1029 flower.mask.ipv6.ipv6_dst = mask->ipv6_dst;
1030 memset(&mask->ipv6_src, 0, sizeof mask->ipv6_src);
1031 memset(&mask->ipv6_dst, 0, sizeof mask->ipv6_dst);
1032 }
1033 }
1034
1035 err = test_key_and_mask(match);
1036 if (err) {
1037 return err;
1038 }
1039
1040 NL_ATTR_FOR_EACH(nla, left, actions, actions_len) {
0c70132c
CM
1041 if (flower.action_count >= TCA_ACT_MAX_PRIO) {
1042 VLOG_DBG_RL(&rl, "Can only support %d actions", flower.action_count);
1043 return EOPNOTSUPP;
1044 }
1045 action = &flower.actions[flower.action_count];
8f283af8
PB
1046 if (nl_attr_type(nla) == OVS_ACTION_ATTR_OUTPUT) {
1047 odp_port_t port = nl_attr_get_odp_port(nla);
dfaf79dd 1048 struct netdev *outdev = netdev_ports_get(port, info->dpif_class);
8f283af8 1049
0c70132c
CM
1050 action->ifindex_out = netdev_get_ifindex(outdev);
1051 action->type = TC_ACT_OUTPUT;
1052 flower.action_count++;
8f283af8
PB
1053 netdev_close(outdev);
1054 } else if (nl_attr_type(nla) == OVS_ACTION_ATTR_PUSH_VLAN) {
1055 const struct ovs_action_push_vlan *vlan_push = nl_attr_get(nla);
1056
0c70132c
CM
1057 action->vlan.vlan_push_id = vlan_tci_to_vid(vlan_push->vlan_tci);
1058 action->vlan.vlan_push_prio = vlan_tci_to_pcp(vlan_push->vlan_tci);
1059 action->type = TC_ACT_VLAN_PUSH;
1060 flower.action_count++;
8f283af8 1061 } else if (nl_attr_type(nla) == OVS_ACTION_ATTR_POP_VLAN) {
0c70132c
CM
1062 action->type = TC_ACT_VLAN_POP;
1063 flower.action_count++;
8f283af8
PB
1064 } else if (nl_attr_type(nla) == OVS_ACTION_ATTR_SET) {
1065 const struct nlattr *set = nl_attr_get(nla);
1066 const size_t set_len = nl_attr_get_size(nla);
1067
0c70132c 1068 err = parse_put_flow_set_action(&flower, action, set, set_len);
8f283af8
PB
1069 if (err) {
1070 return err;
1071 }
0c70132c
CM
1072 if (action->type == TC_ACT_ENCAP) {
1073 action->encap.tp_dst = info->tp_dst_port;
1074 }
1ae83bb2
PB
1075 } else if (nl_attr_type(nla) == OVS_ACTION_ATTR_SET_MASKED) {
1076 const struct nlattr *set = nl_attr_get(nla);
1077 const size_t set_len = nl_attr_get_size(nla);
1078
0c70132c
CM
1079 err = parse_put_flow_set_masked_action(&flower, action, set,
1080 set_len, true);
1ae83bb2
PB
1081 if (err) {
1082 return err;
1083 }
8f283af8
PB
1084 } else {
1085 VLOG_DBG_RL(&rl, "unsupported put action type: %d",
1086 nl_attr_type(nla));
1087 return EOPNOTSUPP;
1088 }
1089 }
1090
1091 handle = get_ufid_tc_mapping(ufid, &prio, NULL);
1092 if (handle && prio) {
1093 VLOG_DBG_RL(&rl, "updating old handle: %d prio: %d", handle, prio);
1094 tc_del_filter(ifindex, prio, handle);
1095 }
1096
1097 if (!prio) {
1098 prio = get_prio_for_tc_flower(&flower);
1099 if (prio == 0) {
1100 VLOG_ERR_RL(&rl, "couldn't get tc prio: %s", ovs_strerror(ENOSPC));
1101 return ENOSPC;
1102 }
1103 }
1104
1105 flower.act_cookie.data = ufid;
1106 flower.act_cookie.len = sizeof *ufid;
1107
1108 err = tc_replace_flower(ifindex, prio, handle, &flower);
1109 if (!err) {
1110 add_ufid_tc_mapping(ufid, flower.prio, flower.handle, netdev, ifindex);
1111 }
1112
1113 return err;
18ebd48c
PB
1114}
1115
1116int
1117netdev_tc_flow_get(struct netdev *netdev OVS_UNUSED,
7ecdef27
PB
1118 struct match *match,
1119 struct nlattr **actions,
1120 const ovs_u128 *ufid,
1121 struct dpif_flow_stats *stats,
1122 struct ofpbuf *buf)
18ebd48c 1123{
7ecdef27
PB
1124 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
1125 struct netdev *dev;
1126 struct tc_flower flower;
1127 odp_port_t in_port;
1128 int prio = 0;
1129 int ifindex;
1130 int handle;
1131 int err;
1132
1133 handle = get_ufid_tc_mapping(ufid, &prio, &dev);
1134 if (!handle) {
1135 return ENOENT;
1136 }
1137
1138 ifindex = netdev_get_ifindex(dev);
1139 if (ifindex < 0) {
0164f10c 1140 VLOG_ERR_RL(&error_rl, "flow_get: failed to get ifindex for %s: %s",
7ecdef27
PB
1141 netdev_get_name(dev), ovs_strerror(-ifindex));
1142 netdev_close(dev);
1143 return -ifindex;
1144 }
1145
1146 VLOG_DBG_RL(&rl, "flow get (dev %s prio %d handle %d)",
1147 netdev_get_name(dev), prio, handle);
1148 err = tc_get_flower(ifindex, prio, handle, &flower);
1149 netdev_close(dev);
1150 if (err) {
1151 VLOG_ERR_RL(&error_rl, "flow get failed (dev %s prio %d handle %d): %s",
1152 netdev_get_name(dev), prio, handle, ovs_strerror(err));
1153 return err;
1154 }
1155
1156 in_port = netdev_ifindex_to_odp_port(ifindex);
1157 parse_tc_flower_to_match(&flower, match, actions, stats, buf);
1158
1159 match->wc.masks.in_port.odp_port = u32_to_odp(UINT32_MAX);
1160 match->flow.in_port.odp_port = in_port;
1161
1162 return 0;
18ebd48c
PB
1163}
1164
1165int
1166netdev_tc_flow_del(struct netdev *netdev OVS_UNUSED,
30b6b047
PB
1167 const ovs_u128 *ufid,
1168 struct dpif_flow_stats *stats)
18ebd48c 1169{
52f793b8 1170 struct tc_flower flower;
30b6b047
PB
1171 struct netdev *dev;
1172 int prio = 0;
1173 int ifindex;
1174 int handle;
1175 int error;
1176
1177 handle = get_ufid_tc_mapping(ufid, &prio, &dev);
1178 if (!handle) {
1179 return ENOENT;
1180 }
1181
1182 ifindex = netdev_get_ifindex(dev);
1183 if (ifindex < 0) {
0164f10c 1184 VLOG_ERR_RL(&error_rl, "flow_del: failed to get ifindex for %s: %s",
30b6b047
PB
1185 netdev_get_name(dev), ovs_strerror(-ifindex));
1186 netdev_close(dev);
1187 return -ifindex;
1188 }
1189
52f793b8
PA
1190 if (stats) {
1191 memset(stats, 0, sizeof *stats);
1192 if (!tc_get_flower(ifindex, prio, handle, &flower)) {
1193 stats->n_packets = get_32aligned_u64(&flower.stats.n_packets);
1194 stats->n_bytes = get_32aligned_u64(&flower.stats.n_bytes);
1195 stats->used = flower.lastused;
1196 }
1197 }
1198
30b6b047
PB
1199 error = tc_del_filter(ifindex, prio, handle);
1200 del_ufid_tc_mapping(ufid);
1201
1202 netdev_close(dev);
1203
30b6b047 1204 return error;
18ebd48c
PB
1205}
1206
d00eeded
PB
1207static void
1208probe_multi_mask_per_prio(int ifindex)
1209{
1210 struct tc_flower flower;
1211 int error;
1212
1213 memset(&flower, 0, sizeof flower);
1214
1215 flower.key.eth_type = htons(ETH_P_IP);
c22ab9fb 1216 flower.mask.eth_type = OVS_BE16_MAX;
d00eeded
PB
1217 memset(&flower.key.dst_mac, 0x11, sizeof flower.key.dst_mac);
1218 memset(&flower.mask.dst_mac, 0xff, sizeof flower.mask.dst_mac);
1219
1220 error = tc_replace_flower(ifindex, 1, 1, &flower);
1221 if (error) {
1222 return;
1223 }
1224
1225 memset(&flower.key.src_mac, 0x11, sizeof flower.key.src_mac);
1226 memset(&flower.mask.src_mac, 0xff, sizeof flower.mask.src_mac);
1227
1228 error = tc_replace_flower(ifindex, 1, 2, &flower);
1229 tc_del_filter(ifindex, 1, 1);
1230
1231 if (error) {
1232 return;
1233 }
1234
1235 tc_del_filter(ifindex, 1, 2);
1236
1237 multi_mask_per_prio = true;
1238 VLOG_INFO("probe tc: multiple masks on single tc prio is supported.");
1239}
1240
18ebd48c 1241int
adbbe97f 1242netdev_tc_init_flow_api(struct netdev *netdev)
18ebd48c 1243{
d00eeded 1244 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
adbbe97f
PB
1245 int ifindex;
1246 int error;
1247
1248 ifindex = netdev_get_ifindex(netdev);
1249 if (ifindex < 0) {
0164f10c 1250 VLOG_ERR_RL(&error_rl, "init: failed to get ifindex for %s: %s",
adbbe97f
PB
1251 netdev_get_name(netdev), ovs_strerror(-ifindex));
1252 return -ifindex;
1253 }
1254
1255 error = tc_add_del_ingress_qdisc(ifindex, true);
1256
1257 if (error && error != EEXIST) {
1258 VLOG_ERR("failed adding ingress qdisc required for offloading: %s",
1259 ovs_strerror(error));
1260 return error;
1261 }
1262
1263 VLOG_INFO("added ingress qdisc to %s", netdev_get_name(netdev));
1264
d00eeded
PB
1265 if (ovsthread_once_start(&once)) {
1266 probe_multi_mask_per_prio(ifindex);
1267 ovsthread_once_done(&once);
1268 }
1269
18ebd48c
PB
1270 return 0;
1271}