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