]> git.proxmox.com Git - mirror_ovs.git/blame - lib/tc.c
compat: Add tc ct action and flower matches defines for older kernels
[mirror_ovs.git] / lib / tc.c
CommitLineData
c1c5c723 1/*
ef3767f5 2 * Copyright (c) 2009-2017 Nicira, Inc.
f98e418f 3 * Copyright (c) 2016 Mellanox Technologies, Ltd.
c1c5c723
PB
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <config.h>
19#include "tc.h"
ef3767f5 20
c1c5c723 21#include <errno.h>
f98e418f 22#include <linux/if_ether.h>
4aa2dc04 23#include <linux/if_packet.h>
f98e418f 24#include <linux/rtnetlink.h>
8ada482b 25#include <linux/tc_act/tc_csum.h>
f98e418f
RD
26#include <linux/tc_act/tc_gact.h>
27#include <linux/tc_act/tc_mirred.h>
55412eac 28#include <linux/tc_act/tc_mpls.h>
8ada482b 29#include <linux/tc_act/tc_pedit.h>
4aa2dc04 30#include <linux/tc_act/tc_skbedit.h>
f98e418f
RD
31#include <linux/tc_act/tc_tunnel_key.h>
32#include <linux/tc_act/tc_vlan.h>
33#include <linux/gen_stats.h>
34#include <net/if.h>
8c1e74d1 35#include <unistd.h>
ef3767f5 36
f98e418f 37#include "byte-order.h"
c1c5c723
PB
38#include "netlink-socket.h"
39#include "netlink.h"
40#include "openvswitch/ofpbuf.h"
8ada482b 41#include "openvswitch/util.h"
c1c5c723 42#include "openvswitch/vlog.h"
f98e418f
RD
43#include "packets.h"
44#include "timeval.h"
ef3767f5 45#include "unaligned.h"
c1c5c723 46
8ada482b
PB
47#define MAX_PEDIT_OFFSETS 32
48
093c9458
JH
49#ifndef TCM_IFINDEX_MAGIC_BLOCK
50#define TCM_IFINDEX_MAGIC_BLOCK (0xFFFFFFFFU)
51#endif
52
53#if TCA_MAX < 14
54#define TCA_INGRESS_BLOCK 13
55#endif
56
c1c5c723
PB
57VLOG_DEFINE_THIS_MODULE(tc);
58
f98e418f
RD
59static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(60, 5);
60
691d20cb
PB
61enum tc_offload_policy {
62 TC_POLICY_NONE,
63 TC_POLICY_SKIP_SW,
64 TC_POLICY_SKIP_HW
65};
66
67static enum tc_offload_policy tc_policy = TC_POLICY_NONE;
68
8ada482b
PB
69struct tc_pedit_key_ex {
70 enum pedit_header_type htype;
71 enum pedit_cmd cmd;
72};
73
74struct flower_key_to_pedit {
75 enum pedit_header_type htype;
8ada482b 76 int offset;
fbaf1bf9 77 int flower_offset;
8ada482b 78 int size;
dbcb014d 79 int boundary_shift;
8ada482b
PB
80};
81
82static struct flower_key_to_pedit flower_pedit_map[] = {
83 {
84 TCA_PEDIT_KEY_EX_HDR_TYPE_IP4,
85 12,
86 offsetof(struct tc_flower_key, ipv4.ipv4_src),
dbcb014d
PJV
87 MEMBER_SIZEOF(struct tc_flower_key, ipv4.ipv4_src),
88 0
8ada482b
PB
89 }, {
90 TCA_PEDIT_KEY_EX_HDR_TYPE_IP4,
91 16,
92 offsetof(struct tc_flower_key, ipv4.ipv4_dst),
dbcb014d
PJV
93 MEMBER_SIZEOF(struct tc_flower_key, ipv4.ipv4_dst),
94 0
8ada482b
PB
95 }, {
96 TCA_PEDIT_KEY_EX_HDR_TYPE_IP4,
97 8,
98 offsetof(struct tc_flower_key, ipv4.rewrite_ttl),
dbcb014d
PJV
99 MEMBER_SIZEOF(struct tc_flower_key, ipv4.rewrite_ttl),
100 0
95431229
PJV
101 }, {
102 TCA_PEDIT_KEY_EX_HDR_TYPE_IP4,
103 1,
104 offsetof(struct tc_flower_key, ipv4.rewrite_tos),
dbcb014d
PJV
105 MEMBER_SIZEOF(struct tc_flower_key, ipv4.rewrite_tos),
106 0
46df7fac
EB
107 }, {
108 TCA_PEDIT_KEY_EX_HDR_TYPE_IP6,
109 7,
110 offsetof(struct tc_flower_key, ipv6.rewrite_hlimit),
dbcb014d
PJV
111 MEMBER_SIZEOF(struct tc_flower_key, ipv6.rewrite_hlimit),
112 0
8ada482b
PB
113 }, {
114 TCA_PEDIT_KEY_EX_HDR_TYPE_IP6,
115 8,
116 offsetof(struct tc_flower_key, ipv6.ipv6_src),
dbcb014d
PJV
117 MEMBER_SIZEOF(struct tc_flower_key, ipv6.ipv6_src),
118 0
8ada482b
PB
119 }, {
120 TCA_PEDIT_KEY_EX_HDR_TYPE_IP6,
121 24,
122 offsetof(struct tc_flower_key, ipv6.ipv6_dst),
dbcb014d
PJV
123 MEMBER_SIZEOF(struct tc_flower_key, ipv6.ipv6_dst),
124 0
125 }, {
126 TCA_PEDIT_KEY_EX_HDR_TYPE_IP6,
127 0,
128 offsetof(struct tc_flower_key, ipv6.rewrite_tclass),
129 MEMBER_SIZEOF(struct tc_flower_key, ipv6.rewrite_tclass),
130 4
8ada482b
PB
131 }, {
132 TCA_PEDIT_KEY_EX_HDR_TYPE_ETH,
133 6,
134 offsetof(struct tc_flower_key, src_mac),
dbcb014d
PJV
135 MEMBER_SIZEOF(struct tc_flower_key, src_mac),
136 0
8ada482b
PB
137 }, {
138 TCA_PEDIT_KEY_EX_HDR_TYPE_ETH,
139 0,
140 offsetof(struct tc_flower_key, dst_mac),
dbcb014d
PJV
141 MEMBER_SIZEOF(struct tc_flower_key, dst_mac),
142 0
8ada482b
PB
143 }, {
144 TCA_PEDIT_KEY_EX_HDR_TYPE_ETH,
145 12,
146 offsetof(struct tc_flower_key, eth_type),
dbcb014d
PJV
147 MEMBER_SIZEOF(struct tc_flower_key, eth_type),
148 0
8ada482b
PB
149 }, {
150 TCA_PEDIT_KEY_EX_HDR_TYPE_TCP,
151 0,
152 offsetof(struct tc_flower_key, tcp_src),
dbcb014d
PJV
153 MEMBER_SIZEOF(struct tc_flower_key, tcp_src),
154 0
8ada482b
PB
155 }, {
156 TCA_PEDIT_KEY_EX_HDR_TYPE_TCP,
157 2,
158 offsetof(struct tc_flower_key, tcp_dst),
dbcb014d
PJV
159 MEMBER_SIZEOF(struct tc_flower_key, tcp_dst),
160 0
8ada482b
PB
161 }, {
162 TCA_PEDIT_KEY_EX_HDR_TYPE_UDP,
163 0,
164 offsetof(struct tc_flower_key, udp_src),
dbcb014d
PJV
165 MEMBER_SIZEOF(struct tc_flower_key, udp_src),
166 0
8ada482b
PB
167 }, {
168 TCA_PEDIT_KEY_EX_HDR_TYPE_UDP,
169 2,
170 offsetof(struct tc_flower_key, udp_dst),
dbcb014d
PJV
171 MEMBER_SIZEOF(struct tc_flower_key, udp_dst),
172 0
8ada482b
PB
173 },
174};
175
d6118e62
PB
176static inline int
177csum_update_flag(struct tc_flower *flower,
178 enum pedit_header_type htype);
179
c1c5c723
PB
180struct tcmsg *
181tc_make_request(int ifindex, int type, unsigned int flags,
182 struct ofpbuf *request)
183{
184 struct tcmsg *tcmsg;
185
186 ofpbuf_init(request, 512);
187 nl_msg_put_nlmsghdr(request, sizeof *tcmsg, type, NLM_F_REQUEST | flags);
188 tcmsg = ofpbuf_put_zeros(request, sizeof *tcmsg);
189 tcmsg->tcm_family = AF_UNSPEC;
190 tcmsg->tcm_ifindex = ifindex;
191 /* Caller should fill in tcmsg->tcm_handle. */
192 /* Caller should fill in tcmsg->tcm_parent. */
193
194 return tcmsg;
195}
196
197int
198tc_transact(struct ofpbuf *request, struct ofpbuf **replyp)
199{
200 int error = nl_transact(NETLINK_ROUTE, request, replyp);
201 ofpbuf_uninit(request);
202 return error;
203}
204
95255018 205/* Adds or deletes a root qdisc on device with specified ifindex.
c1c5c723 206 *
95255018
JH
207 * The tc_qdisc_hook parameter determines if the qdisc is added on device
208 * ingress or egress.
209 *
210 * If tc_qdisc_hook is TC_INGRESS, this function is equivalent to running the
211 * following when 'add' is true:
c1c5c723
PB
212 * /sbin/tc qdisc add dev <devname> handle ffff: ingress
213 *
214 * This function is equivalent to running the following when 'add' is false:
215 * /sbin/tc qdisc del dev <devname> handle ffff: ingress
216 *
95255018
JH
217 * If tc_qdisc_hook is TC_EGRESS, this function is equivalent to:
218 * /sbin/tc qdisc (add|del) dev <devname> handle ffff: clsact
219 *
c1c5c723
PB
220 * Where dev <devname> is the device with specified ifindex name.
221 *
222 * The configuration and stats may be seen with the following command:
223 * /sbin/tc -s qdisc show dev <devname>
224 *
093c9458
JH
225 * If block_id is greater than 0, then the ingress qdisc is added to a block.
226 * In this case, it is equivalent to running (when 'add' is true):
227 * /sbin/tc qdisc add dev <devname> ingress_block <block_id> ingress
228 *
c1c5c723
PB
229 * Returns 0 if successful, otherwise a positive errno value.
230 */
231int
95255018
JH
232tc_add_del_qdisc(int ifindex, bool add, uint32_t block_id,
233 enum tc_qdisc_hook hook)
c1c5c723
PB
234{
235 struct ofpbuf request;
236 struct tcmsg *tcmsg;
237 int error;
238 int type = add ? RTM_NEWQDISC : RTM_DELQDISC;
239 int flags = add ? NLM_F_EXCL | NLM_F_CREATE : 0;
240
241 tcmsg = tc_make_request(ifindex, type, flags, &request);
95255018
JH
242
243 if (hook == TC_EGRESS) {
244 tcmsg->tcm_handle = TC_H_MAKE(TC_H_CLSACT, 0);
245 tcmsg->tcm_parent = TC_H_CLSACT;
246 nl_msg_put_string(&request, TCA_KIND, "clsact");
247 } else {
248 tcmsg->tcm_handle = TC_H_MAKE(TC_H_INGRESS, 0);
249 tcmsg->tcm_parent = TC_H_INGRESS;
250 nl_msg_put_string(&request, TCA_KIND, "ingress");
251 }
252
c1c5c723 253 nl_msg_put_unspec(&request, TCA_OPTIONS, NULL, 0);
95255018 254 if (hook == TC_INGRESS && block_id) {
093c9458
JH
255 nl_msg_put_u32(&request, TCA_INGRESS_BLOCK, block_id);
256 }
c1c5c723
PB
257
258 error = tc_transact(&request, NULL);
259 if (error) {
260 /* If we're deleting the qdisc, don't worry about some of the
261 * error conditions. */
262 if (!add && (error == ENOENT || error == EINVAL)) {
263 return 0;
264 }
265 return error;
266 }
267
268 return 0;
269}
f98e418f
RD
270
271static const struct nl_policy tca_policy[] = {
272 [TCA_KIND] = { .type = NL_A_STRING, .optional = false, },
273 [TCA_OPTIONS] = { .type = NL_A_NESTED, .optional = false, },
274 [TCA_STATS] = { .type = NL_A_UNSPEC,
275 .min_len = sizeof(struct tc_stats), .optional = true, },
276 [TCA_STATS2] = { .type = NL_A_NESTED, .optional = true, },
277};
278
279static const struct nl_policy tca_flower_policy[] = {
280 [TCA_FLOWER_CLASSID] = { .type = NL_A_U32, .optional = true, },
281 [TCA_FLOWER_INDEV] = { .type = NL_A_STRING, .max_len = IFNAMSIZ,
282 .optional = true, },
283 [TCA_FLOWER_KEY_ETH_SRC] = { .type = NL_A_UNSPEC,
284 .min_len = ETH_ALEN, .optional = true, },
285 [TCA_FLOWER_KEY_ETH_DST] = { .type = NL_A_UNSPEC,
286 .min_len = ETH_ALEN, .optional = true, },
287 [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .type = NL_A_UNSPEC,
288 .min_len = ETH_ALEN,
289 .optional = true, },
290 [TCA_FLOWER_KEY_ETH_DST_MASK] = { .type = NL_A_UNSPEC,
291 .min_len = ETH_ALEN,
292 .optional = true, },
293 [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NL_A_U16, .optional = false, },
294 [TCA_FLOWER_FLAGS] = { .type = NL_A_U32, .optional = false, },
295 [TCA_FLOWER_ACT] = { .type = NL_A_NESTED, .optional = false, },
296 [TCA_FLOWER_KEY_IP_PROTO] = { .type = NL_A_U8, .optional = true, },
297 [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NL_A_U32, .optional = true, },
298 [TCA_FLOWER_KEY_IPV4_DST] = {.type = NL_A_U32, .optional = true, },
299 [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NL_A_U32, .optional = true, },
300 [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NL_A_U32, .optional = true, },
301 [TCA_FLOWER_KEY_IPV6_SRC] = { .type = NL_A_UNSPEC,
302 .min_len = sizeof(struct in6_addr),
303 .optional = true, },
304 [TCA_FLOWER_KEY_IPV6_DST] = { .type = NL_A_UNSPEC,
305 .min_len = sizeof(struct in6_addr),
306 .optional = true, },
307 [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .type = NL_A_UNSPEC,
308 .min_len = sizeof(struct in6_addr),
309 .optional = true, },
310 [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .type = NL_A_UNSPEC,
311 .min_len = sizeof(struct in6_addr),
312 .optional = true, },
313 [TCA_FLOWER_KEY_TCP_SRC] = { .type = NL_A_U16, .optional = true, },
314 [TCA_FLOWER_KEY_TCP_DST] = { .type = NL_A_U16, .optional = true, },
315 [TCA_FLOWER_KEY_TCP_SRC_MASK] = { .type = NL_A_U16, .optional = true, },
316 [TCA_FLOWER_KEY_TCP_DST_MASK] = { .type = NL_A_U16, .optional = true, },
317 [TCA_FLOWER_KEY_UDP_SRC] = { .type = NL_A_U16, .optional = true, },
318 [TCA_FLOWER_KEY_UDP_DST] = { .type = NL_A_U16, .optional = true, },
319 [TCA_FLOWER_KEY_UDP_SRC_MASK] = { .type = NL_A_U16, .optional = true, },
320 [TCA_FLOWER_KEY_UDP_DST_MASK] = { .type = NL_A_U16, .optional = true, },
4862b4e5
VB
321 [TCA_FLOWER_KEY_SCTP_SRC] = { .type = NL_A_U16, .optional = true, },
322 [TCA_FLOWER_KEY_SCTP_DST] = { .type = NL_A_U16, .optional = true, },
323 [TCA_FLOWER_KEY_SCTP_SRC_MASK] = { .type = NL_A_U16, .optional = true, },
324 [TCA_FLOWER_KEY_SCTP_DST_MASK] = { .type = NL_A_U16, .optional = true, },
34b16955
PJV
325 [TCA_FLOWER_KEY_MPLS_TTL] = { .type = NL_A_U8, .optional = true, },
326 [TCA_FLOWER_KEY_MPLS_TC] = { .type = NL_A_U8, .optional = true, },
327 [TCA_FLOWER_KEY_MPLS_BOS] = { .type = NL_A_U8, .optional = true, },
328 [TCA_FLOWER_KEY_MPLS_LABEL] = { .type = NL_A_U32, .optional = true, },
f98e418f
RD
329 [TCA_FLOWER_KEY_VLAN_ID] = { .type = NL_A_U16, .optional = true, },
330 [TCA_FLOWER_KEY_VLAN_PRIO] = { .type = NL_A_U8, .optional = true, },
331 [TCA_FLOWER_KEY_VLAN_ETH_TYPE] = { .type = NL_A_U16, .optional = true, },
332 [TCA_FLOWER_KEY_ENC_KEY_ID] = { .type = NL_A_U32, .optional = true, },
333 [TCA_FLOWER_KEY_ENC_IPV4_SRC] = { .type = NL_A_U32, .optional = true, },
334 [TCA_FLOWER_KEY_ENC_IPV4_DST] = { .type = NL_A_U32, .optional = true, },
335 [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NL_A_U32,
336 .optional = true, },
337 [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NL_A_U32,
338 .optional = true, },
339 [TCA_FLOWER_KEY_ENC_IPV6_SRC] = { .type = NL_A_UNSPEC,
340 .min_len = sizeof(struct in6_addr),
341 .optional = true, },
342 [TCA_FLOWER_KEY_ENC_IPV6_DST] = { .type = NL_A_UNSPEC,
343 .min_len = sizeof(struct in6_addr),
344 .optional = true, },
345 [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .type = NL_A_UNSPEC,
346 .min_len = sizeof(struct in6_addr),
347 .optional = true, },
348 [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .type = NL_A_UNSPEC,
349 .min_len = sizeof(struct in6_addr),
350 .optional = true, },
351 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT] = { .type = NL_A_U16,
352 .optional = true, },
83e86606
RD
353 [TCA_FLOWER_KEY_FLAGS] = { .type = NL_A_BE32, .optional = true, },
354 [TCA_FLOWER_KEY_FLAGS_MASK] = { .type = NL_A_BE32, .optional = true, },
0b4b5203
PB
355 [TCA_FLOWER_KEY_IP_TTL] = { .type = NL_A_U8,
356 .optional = true, },
357 [TCA_FLOWER_KEY_IP_TTL_MASK] = { .type = NL_A_U8,
358 .optional = true, },
dfa2ccdb
OG
359 [TCA_FLOWER_KEY_IP_TOS] = { .type = NL_A_U8,
360 .optional = true, },
361 [TCA_FLOWER_KEY_IP_TOS_MASK] = { .type = NL_A_U8,
362 .optional = true, },
cd081043
PB
363 [TCA_FLOWER_KEY_TCP_FLAGS] = { .type = NL_A_U16,
364 .optional = true, },
365 [TCA_FLOWER_KEY_TCP_FLAGS_MASK] = { .type = NL_A_U16,
366 .optional = true, },
f9885dc5
JL
367 [TCA_FLOWER_KEY_CVLAN_ID] = { .type = NL_A_U16, .optional = true, },
368 [TCA_FLOWER_KEY_CVLAN_PRIO] = { .type = NL_A_U8, .optional = true, },
369 [TCA_FLOWER_KEY_CVLAN_ETH_TYPE] = { .type = NL_A_U16, .optional = true, },
dd83253e
OG
370 [TCA_FLOWER_KEY_ENC_IP_TOS] = { .type = NL_A_U8,
371 .optional = true, },
372 [TCA_FLOWER_KEY_ENC_IP_TOS_MASK] = { .type = NL_A_U8,
373 .optional = true, },
374 [TCA_FLOWER_KEY_ENC_IP_TTL] = { .type = NL_A_U8,
375 .optional = true, },
376 [TCA_FLOWER_KEY_ENC_IP_TTL_MASK] = { .type = NL_A_U8,
377 .optional = true, },
a468645c
PJV
378 [TCA_FLOWER_KEY_ENC_OPTS] = { .type = NL_A_NESTED, .optional = true, },
379 [TCA_FLOWER_KEY_ENC_OPTS_MASK] = { .type = NL_A_NESTED,
380 .optional = true, },
f98e418f
RD
381};
382
383static void
384nl_parse_flower_eth(struct nlattr **attrs, struct tc_flower *flower)
385{
386 const struct eth_addr *eth;
387
388 if (attrs[TCA_FLOWER_KEY_ETH_SRC_MASK]) {
389 eth = nl_attr_get_unspec(attrs[TCA_FLOWER_KEY_ETH_SRC], ETH_ALEN);
390 memcpy(&flower->key.src_mac, eth, sizeof flower->key.src_mac);
391
392 eth = nl_attr_get_unspec(attrs[TCA_FLOWER_KEY_ETH_SRC_MASK], ETH_ALEN);
393 memcpy(&flower->mask.src_mac, eth, sizeof flower->mask.src_mac);
394 }
395 if (attrs[TCA_FLOWER_KEY_ETH_DST_MASK]) {
396 eth = nl_attr_get_unspec(attrs[TCA_FLOWER_KEY_ETH_DST], ETH_ALEN);
397 memcpy(&flower->key.dst_mac, eth, sizeof flower->key.dst_mac);
398
399 eth = nl_attr_get_unspec(attrs[TCA_FLOWER_KEY_ETH_DST_MASK], ETH_ALEN);
400 memcpy(&flower->mask.dst_mac, eth, sizeof flower->mask.dst_mac);
401 }
402}
403
34b16955
PJV
404static void
405nl_parse_flower_mpls(struct nlattr **attrs, struct tc_flower *flower)
406{
407 uint8_t ttl, tc, bos;
408 uint32_t label;
409
410 if (!eth_type_mpls(flower->key.eth_type)) {
411 return;
412 }
413
414 flower->key.encap_eth_type[0] =
415 nl_attr_get_be16(attrs[TCA_FLOWER_KEY_ETH_TYPE]);
416 flower->key.mpls_lse = 0;
417 flower->mask.mpls_lse = 0;
418
419 if (attrs[TCA_FLOWER_KEY_MPLS_TTL]) {
420 ttl = nl_attr_get_u8(attrs[TCA_FLOWER_KEY_MPLS_TTL]);
421 set_mpls_lse_ttl(&flower->key.mpls_lse, ttl);
422 set_mpls_lse_ttl(&flower->mask.mpls_lse, 0xff);
423 }
424
425 if (attrs[TCA_FLOWER_KEY_MPLS_BOS]) {
426 bos = nl_attr_get_u8(attrs[TCA_FLOWER_KEY_MPLS_BOS]);
427 set_mpls_lse_bos(&flower->key.mpls_lse, bos);
428 set_mpls_lse_ttl(&flower->mask.mpls_lse, 0xff);
429 }
430
431 if (attrs[TCA_FLOWER_KEY_MPLS_TC]) {
432 tc = nl_attr_get_u8(attrs[TCA_FLOWER_KEY_MPLS_TC]);
433 set_mpls_lse_tc(&flower->key.mpls_lse, tc);
434 set_mpls_lse_tc(&flower->mask.mpls_lse, 0xff);
435 }
436
437 if (attrs[TCA_FLOWER_KEY_MPLS_LABEL]) {
438 label = nl_attr_get_u32(attrs[TCA_FLOWER_KEY_MPLS_LABEL]);
439 set_mpls_lse_label(&flower->key.mpls_lse, htonl(label));
440 set_mpls_lse_label(&flower->mask.mpls_lse, OVS_BE32_MAX);
441 }
442}
443
f98e418f
RD
444static void
445nl_parse_flower_vlan(struct nlattr **attrs, struct tc_flower *flower)
446{
f9885dc5
JL
447 ovs_be16 encap_ethtype;
448
b5ad40a9 449 if (!eth_type_vlan(flower->key.eth_type)) {
f98e418f
RD
450 return;
451 }
452
f9885dc5 453 flower->key.encap_eth_type[0] =
f98e418f
RD
454 nl_attr_get_be16(attrs[TCA_FLOWER_KEY_ETH_TYPE]);
455
456 if (attrs[TCA_FLOWER_KEY_VLAN_ID]) {
f9885dc5 457 flower->key.vlan_id[0] =
f98e418f 458 nl_attr_get_u16(attrs[TCA_FLOWER_KEY_VLAN_ID]);
7f02f26c 459 flower->mask.vlan_id[0] = 0xffff;
f98e418f
RD
460 }
461 if (attrs[TCA_FLOWER_KEY_VLAN_PRIO]) {
f9885dc5 462 flower->key.vlan_prio[0] =
f98e418f 463 nl_attr_get_u8(attrs[TCA_FLOWER_KEY_VLAN_PRIO]);
7f02f26c 464 flower->mask.vlan_prio[0] = 0xff;
f98e418f 465 }
f9885dc5
JL
466
467 if (!attrs[TCA_FLOWER_KEY_VLAN_ETH_TYPE]) {
468 return;
469 }
470
471 encap_ethtype = nl_attr_get_be16(attrs[TCA_FLOWER_KEY_VLAN_ETH_TYPE]);
472 if (!eth_type_vlan(encap_ethtype)) {
473 return;
474 }
475
476 flower->key.encap_eth_type[1] = flower->key.encap_eth_type[0];
477 flower->key.encap_eth_type[0] = encap_ethtype;
478
479 if (attrs[TCA_FLOWER_KEY_CVLAN_ID]) {
480 flower->key.vlan_id[1] =
481 nl_attr_get_u16(attrs[TCA_FLOWER_KEY_CVLAN_ID]);
7f02f26c 482 flower->mask.vlan_id[1] = 0xffff;
f9885dc5
JL
483 }
484 if (attrs[TCA_FLOWER_KEY_CVLAN_PRIO]) {
485 flower->key.vlan_prio[1] =
486 nl_attr_get_u8(attrs[TCA_FLOWER_KEY_CVLAN_PRIO]);
7f02f26c 487 flower->mask.vlan_prio[1] = 0xff;
f9885dc5 488 }
f98e418f
RD
489}
490
a468645c
PJV
491static int
492nl_parse_geneve_key(const struct nlattr *in_nlattr,
493 struct tun_metadata *metadata)
494{
495 struct geneve_opt *opt = NULL;
496 const struct ofpbuf *msg;
497 uint16_t last_opt_type;
498 struct nlattr *nla;
499 struct ofpbuf buf;
500 size_t left;
501 int cnt;
502
503 nl_attr_get_nested(in_nlattr, &buf);
504 msg = &buf;
505
506 last_opt_type = TCA_FLOWER_KEY_ENC_OPT_GENEVE_UNSPEC;
507 cnt = 0;
508 NL_ATTR_FOR_EACH (nla, left, ofpbuf_at(msg, 0, 0), msg->size) {
509 uint16_t type = nl_attr_type(nla);
510
511 switch (type) {
512 case TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS:
513 if (cnt && last_opt_type != TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA) {
514 VLOG_ERR_RL(&error_rl, "failed to parse tun options class");
515 return EINVAL;
516 }
517
518 opt = &metadata->opts.gnv[cnt];
519 opt->opt_class = nl_attr_get_be16(nla);
520 cnt += sizeof(struct geneve_opt) / 4;
521 metadata->present.len += sizeof(struct geneve_opt);
522 last_opt_type = TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS;
523 break;
524 case TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE:
525 if (last_opt_type != TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS) {
526 VLOG_ERR_RL(&error_rl, "failed to parse tun options type");
527 return EINVAL;
528 }
529
530 opt->type = nl_attr_get_u8(nla);
531 last_opt_type = TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE;
532 break;
533 case TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA:
534 if (last_opt_type != TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE) {
535 VLOG_ERR_RL(&error_rl, "failed to parse tun options data");
536 return EINVAL;
537 }
538
539 opt->length = nl_attr_get_size(nla) / 4;
540 memcpy(opt + 1, nl_attr_get_unspec(nla, 1), opt->length * 4);
541 cnt += opt->length;
542 metadata->present.len += opt->length * 4;
543 last_opt_type = TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA;
544 break;
545 }
546 }
547
548 if (last_opt_type != TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA) {
549 VLOG_ERR_RL(&error_rl, "failed to parse tun options without data");
550 return EINVAL;
551 }
552
553 return 0;
554}
555
556static int
557nl_parse_flower_tunnel_opts(struct nlattr *options,
558 struct tun_metadata *metadata)
559{
560 const struct ofpbuf *msg;
561 struct nlattr *nla;
562 struct ofpbuf buf;
563 size_t left;
564 int err;
565
566 nl_attr_get_nested(options, &buf);
567 msg = &buf;
568
569 NL_ATTR_FOR_EACH (nla, left, ofpbuf_at(msg, 0, 0), msg->size) {
570 uint16_t type = nl_attr_type(nla);
571 switch (type) {
572 case TCA_FLOWER_KEY_ENC_OPTS_GENEVE:
573 err = nl_parse_geneve_key(nla, metadata);
574 if (err) {
575 return err;
576 }
577
578 break;
579 }
580 }
581
582 return 0;
583}
584
585static int
586flower_tun_geneve_opt_check_len(struct tun_metadata *key,
587 struct tun_metadata *mask)
588{
589 const struct geneve_opt *opt, *opt_mask;
590 int len, cnt = 0;
591
592 len = key->present.len;
593 while (len) {
594 opt = &key->opts.gnv[cnt];
595 opt_mask = &mask->opts.gnv[cnt];
596
597 if (opt->length != opt_mask->length) {
598 VLOG_ERR_RL(&error_rl,
599 "failed to parse tun options; key/mask length differ");
600 return EINVAL;
601 }
602
603 cnt += sizeof(struct geneve_opt) / 4 + opt->length;
604 len -= sizeof(struct geneve_opt) + opt->length * 4;
605 }
606
607 return 0;
608}
609
610static int
f98e418f
RD
611nl_parse_flower_tunnel(struct nlattr **attrs, struct tc_flower *flower)
612{
a468645c
PJV
613 int err;
614
f98e418f
RD
615 if (attrs[TCA_FLOWER_KEY_ENC_KEY_ID]) {
616 ovs_be32 id = nl_attr_get_be32(attrs[TCA_FLOWER_KEY_ENC_KEY_ID]);
617
105e8179 618 flower->key.tunnel.id = be32_to_be64(id);
0227bf09 619 flower->mask.tunnel.id = OVS_BE64_MAX;
f98e418f
RD
620 }
621 if (attrs[TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK]) {
105e8179 622 flower->key.tunnel.ipv4.ipv4_src =
f98e418f
RD
623 nl_attr_get_be32(attrs[TCA_FLOWER_KEY_ENC_IPV4_SRC]);
624 }
625 if (attrs[TCA_FLOWER_KEY_ENC_IPV4_DST_MASK]) {
105e8179 626 flower->key.tunnel.ipv4.ipv4_dst =
f98e418f
RD
627 nl_attr_get_be32(attrs[TCA_FLOWER_KEY_ENC_IPV4_DST]);
628 }
629 if (attrs[TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK]) {
105e8179 630 flower->key.tunnel.ipv6.ipv6_src =
f98e418f
RD
631 nl_attr_get_in6_addr(attrs[TCA_FLOWER_KEY_ENC_IPV6_SRC]);
632 }
633 if (attrs[TCA_FLOWER_KEY_ENC_IPV6_DST_MASK]) {
105e8179 634 flower->key.tunnel.ipv6.ipv6_dst =
f98e418f
RD
635 nl_attr_get_in6_addr(attrs[TCA_FLOWER_KEY_ENC_IPV6_DST]);
636 }
637 if (attrs[TCA_FLOWER_KEY_ENC_UDP_DST_PORT]) {
105e8179 638 flower->key.tunnel.tp_dst =
f98e418f
RD
639 nl_attr_get_be16(attrs[TCA_FLOWER_KEY_ENC_UDP_DST_PORT]);
640 }
49a7961f 641 if (attrs[TCA_FLOWER_KEY_ENC_IP_TOS_MASK]) {
105e8179 642 flower->key.tunnel.tos =
dd83253e 643 nl_attr_get_u8(attrs[TCA_FLOWER_KEY_ENC_IP_TOS]);
49a7961f
OG
644 flower->mask.tunnel.tos =
645 nl_attr_get_u8(attrs[TCA_FLOWER_KEY_ENC_IP_TOS_MASK]);
dd83253e 646 }
49a7961f 647 if (attrs[TCA_FLOWER_KEY_ENC_IP_TTL_MASK]) {
105e8179 648 flower->key.tunnel.ttl =
dd83253e 649 nl_attr_get_u8(attrs[TCA_FLOWER_KEY_ENC_IP_TTL]);
49a7961f
OG
650 flower->mask.tunnel.ttl =
651 nl_attr_get_u8(attrs[TCA_FLOWER_KEY_ENC_IP_TTL_MASK]);
dd83253e 652 }
a468645c
PJV
653 if (attrs[TCA_FLOWER_KEY_ENC_OPTS] &&
654 attrs[TCA_FLOWER_KEY_ENC_OPTS_MASK]) {
655 err = nl_parse_flower_tunnel_opts(attrs[TCA_FLOWER_KEY_ENC_OPTS],
656 &flower->key.tunnel.metadata);
657 if (err) {
658 return err;
659 }
660
661 err = nl_parse_flower_tunnel_opts(attrs[TCA_FLOWER_KEY_ENC_OPTS_MASK],
662 &flower->mask.tunnel.metadata);
663 if (err) {
664 return err;
665 }
666
667 err = flower_tun_geneve_opt_check_len(&flower->key.tunnel.metadata,
668 &flower->mask.tunnel.metadata);
669 if (err) {
670 return err;
671 }
672 } else if (attrs[TCA_FLOWER_KEY_ENC_OPTS]) {
673 VLOG_ERR_RL(&error_rl,
674 "failed to parse tun options; no mask supplied");
675 return EINVAL;
676 } else if (attrs[TCA_FLOWER_KEY_ENC_OPTS_MASK]) {
677 VLOG_ERR_RL(&error_rl, "failed to parse tun options; no key supplied");
678 return EINVAL;
679 }
680
681 return 0;
f98e418f
RD
682}
683
684static void
685nl_parse_flower_ip(struct nlattr **attrs, struct tc_flower *flower) {
686 uint8_t ip_proto = 0;
687 struct tc_flower_key *key = &flower->key;
688 struct tc_flower_key *mask = &flower->mask;
689
690 if (attrs[TCA_FLOWER_KEY_IP_PROTO]) {
691 ip_proto = nl_attr_get_u8(attrs[TCA_FLOWER_KEY_IP_PROTO]);
692 key->ip_proto = ip_proto;
693 mask->ip_proto = UINT8_MAX;
694 }
695
83e86606 696 if (attrs[TCA_FLOWER_KEY_FLAGS_MASK]) {
7e0f69b5
IS
697 key->flags = ntohl(nl_attr_get_be32(attrs[TCA_FLOWER_KEY_FLAGS]));
698 mask->flags =
699 ntohl(nl_attr_get_be32(attrs[TCA_FLOWER_KEY_FLAGS_MASK]));
83e86606
RD
700 }
701
f98e418f
RD
702 if (attrs[TCA_FLOWER_KEY_IPV4_SRC_MASK]) {
703 key->ipv4.ipv4_src =
704 nl_attr_get_be32(attrs[TCA_FLOWER_KEY_IPV4_SRC]);
705 mask->ipv4.ipv4_src =
706 nl_attr_get_be32(attrs[TCA_FLOWER_KEY_IPV4_SRC_MASK]);
707 }
708 if (attrs[TCA_FLOWER_KEY_IPV4_DST_MASK]) {
709 key->ipv4.ipv4_dst =
710 nl_attr_get_be32(attrs[TCA_FLOWER_KEY_IPV4_DST]);
711 mask->ipv4.ipv4_dst =
712 nl_attr_get_be32(attrs[TCA_FLOWER_KEY_IPV4_DST_MASK]);
713 }
714 if (attrs[TCA_FLOWER_KEY_IPV6_SRC_MASK]) {
715 struct nlattr *attr = attrs[TCA_FLOWER_KEY_IPV6_SRC];
716 struct nlattr *attr_mask = attrs[TCA_FLOWER_KEY_IPV6_SRC_MASK];
717
718 key->ipv6.ipv6_src = nl_attr_get_in6_addr(attr);
719 mask->ipv6.ipv6_src = nl_attr_get_in6_addr(attr_mask);
720 }
721 if (attrs[TCA_FLOWER_KEY_IPV6_DST_MASK]) {
722 struct nlattr *attr = attrs[TCA_FLOWER_KEY_IPV6_DST];
723 struct nlattr *attr_mask = attrs[TCA_FLOWER_KEY_IPV6_DST_MASK];
724
725 key->ipv6.ipv6_dst = nl_attr_get_in6_addr(attr);
726 mask->ipv6.ipv6_dst = nl_attr_get_in6_addr(attr_mask);
727 }
728
729 if (ip_proto == IPPROTO_TCP) {
730 if (attrs[TCA_FLOWER_KEY_TCP_SRC_MASK]) {
2b1d9fa9 731 key->tcp_src =
f98e418f 732 nl_attr_get_be16(attrs[TCA_FLOWER_KEY_TCP_SRC]);
2b1d9fa9 733 mask->tcp_src =
f98e418f
RD
734 nl_attr_get_be16(attrs[TCA_FLOWER_KEY_TCP_SRC_MASK]);
735 }
736 if (attrs[TCA_FLOWER_KEY_TCP_DST_MASK]) {
2b1d9fa9 737 key->tcp_dst =
f98e418f 738 nl_attr_get_be16(attrs[TCA_FLOWER_KEY_TCP_DST]);
2b1d9fa9 739 mask->tcp_dst =
f98e418f
RD
740 nl_attr_get_be16(attrs[TCA_FLOWER_KEY_TCP_DST_MASK]);
741 }
cd081043
PB
742 if (attrs[TCA_FLOWER_KEY_TCP_FLAGS_MASK]) {
743 key->tcp_flags =
744 nl_attr_get_be16(attrs[TCA_FLOWER_KEY_TCP_FLAGS]);
745 mask->tcp_flags =
746 nl_attr_get_be16(attrs[TCA_FLOWER_KEY_TCP_FLAGS_MASK]);
747 }
f98e418f
RD
748 } else if (ip_proto == IPPROTO_UDP) {
749 if (attrs[TCA_FLOWER_KEY_UDP_SRC_MASK]) {
2b1d9fa9
PB
750 key->udp_src = nl_attr_get_be16(attrs[TCA_FLOWER_KEY_UDP_SRC]);
751 mask->udp_src =
f98e418f
RD
752 nl_attr_get_be16(attrs[TCA_FLOWER_KEY_UDP_SRC_MASK]);
753 }
754 if (attrs[TCA_FLOWER_KEY_UDP_DST_MASK]) {
2b1d9fa9
PB
755 key->udp_dst = nl_attr_get_be16(attrs[TCA_FLOWER_KEY_UDP_DST]);
756 mask->udp_dst =
f98e418f
RD
757 nl_attr_get_be16(attrs[TCA_FLOWER_KEY_UDP_DST_MASK]);
758 }
4862b4e5
VB
759 } else if (ip_proto == IPPROTO_SCTP) {
760 if (attrs[TCA_FLOWER_KEY_SCTP_SRC_MASK]) {
2b1d9fa9
PB
761 key->sctp_src = nl_attr_get_be16(attrs[TCA_FLOWER_KEY_SCTP_SRC]);
762 mask->sctp_src =
4862b4e5
VB
763 nl_attr_get_be16(attrs[TCA_FLOWER_KEY_SCTP_SRC_MASK]);
764 }
765 if (attrs[TCA_FLOWER_KEY_SCTP_DST_MASK]) {
2b1d9fa9
PB
766 key->sctp_dst = nl_attr_get_be16(attrs[TCA_FLOWER_KEY_SCTP_DST]);
767 mask->sctp_dst =
4862b4e5
VB
768 nl_attr_get_be16(attrs[TCA_FLOWER_KEY_SCTP_DST_MASK]);
769 }
f98e418f 770 }
0b4b5203
PB
771
772 if (attrs[TCA_FLOWER_KEY_IP_TTL_MASK]) {
773 key->ip_ttl = nl_attr_get_u8(attrs[TCA_FLOWER_KEY_IP_TTL]);
774 mask->ip_ttl = nl_attr_get_u8(attrs[TCA_FLOWER_KEY_IP_TTL_MASK]);
775 }
dfa2ccdb
OG
776
777 if (attrs[TCA_FLOWER_KEY_IP_TOS_MASK]) {
778 key->ip_tos = nl_attr_get_u8(attrs[TCA_FLOWER_KEY_IP_TOS]);
779 mask->ip_tos = nl_attr_get_u8(attrs[TCA_FLOWER_KEY_IP_TOS_MASK]);
780 }
f98e418f
RD
781}
782
d63ca532
GT
783static enum tc_offloaded_state
784nl_get_flower_offloaded_state(struct nlattr **attrs)
785{
786 uint32_t flower_flags = 0;
787
788 if (attrs[TCA_FLOWER_FLAGS]) {
789 flower_flags = nl_attr_get_u32(attrs[TCA_FLOWER_FLAGS]);
790 if (flower_flags & TCA_CLS_FLAGS_NOT_IN_HW) {
791 return TC_OFFLOADED_STATE_NOT_IN_HW;
792 } else if (flower_flags & TCA_CLS_FLAGS_IN_HW) {
793 return TC_OFFLOADED_STATE_IN_HW;
794 }
795 }
796 return TC_OFFLOADED_STATE_UNDEFINED;
797}
798
799static void
800nl_parse_flower_flags(struct nlattr **attrs, struct tc_flower *flower)
801{
802 flower->offloaded_state = nl_get_flower_offloaded_state(attrs);
803}
804
8ada482b
PB
805static const struct nl_policy pedit_policy[] = {
806 [TCA_PEDIT_PARMS_EX] = { .type = NL_A_UNSPEC,
807 .min_len = sizeof(struct tc_pedit),
808 .optional = false, },
809 [TCA_PEDIT_KEYS_EX] = { .type = NL_A_NESTED,
810 .optional = false, },
811};
812
813static int
814nl_parse_act_pedit(struct nlattr *options, struct tc_flower *flower)
815{
0c70132c 816 struct tc_action *action;
8ada482b
PB
817 struct nlattr *pe_attrs[ARRAY_SIZE(pedit_policy)];
818 const struct tc_pedit *pe;
819 const struct tc_pedit_key *keys;
820 const struct nlattr *nla, *keys_ex, *ex_type;
821 const void *keys_attr;
822 char *rewrite_key = (void *) &flower->rewrite.key;
823 char *rewrite_mask = (void *) &flower->rewrite.mask;
824 size_t keys_ex_size, left;
d6118e62 825 int type, i = 0, err;
8ada482b
PB
826
827 if (!nl_parse_nested(options, pedit_policy, pe_attrs,
828 ARRAY_SIZE(pedit_policy))) {
829 VLOG_ERR_RL(&error_rl, "failed to parse pedit action options");
830 return EPROTO;
831 }
832
833 pe = nl_attr_get_unspec(pe_attrs[TCA_PEDIT_PARMS_EX], sizeof *pe);
834 keys = pe->keys;
835 keys_attr = pe_attrs[TCA_PEDIT_KEYS_EX];
836 keys_ex = nl_attr_get(keys_attr);
837 keys_ex_size = nl_attr_get_size(keys_attr);
838
839 NL_ATTR_FOR_EACH (nla, left, keys_ex, keys_ex_size) {
840 if (i >= pe->nkeys) {
841 break;
842 }
843
408671c4 844 if (nl_attr_type(nla) != TCA_PEDIT_KEY_EX) {
8ada482b
PB
845 VLOG_ERR_RL(&error_rl, "unable to parse legacy pedit type: %d",
846 nl_attr_type(nla));
847 return EOPNOTSUPP;
848 }
849
850 ex_type = nl_attr_find_nested(nla, TCA_PEDIT_KEY_EX_HTYPE);
851 type = nl_attr_get_u16(ex_type);
852
d6118e62
PB
853 err = csum_update_flag(flower, type);
854 if (err) {
855 return err;
856 }
857
8ada482b
PB
858 for (int j = 0; j < ARRAY_SIZE(flower_pedit_map); j++) {
859 struct flower_key_to_pedit *m = &flower_pedit_map[j];
860 int flower_off = m->flower_offset;
861 int sz = m->size;
862 int mf = m->offset;
863
864 if (m->htype != type) {
865 continue;
866 }
867
868 /* check overlap between current pedit key, which is always
869 * 4 bytes (range [off, off + 3]), and a map entry in
870 * flower_pedit_map (range [mf, mf + sz - 1]) */
871 if ((keys->off >= mf && keys->off < mf + sz)
872 || (keys->off + 3 >= mf && keys->off + 3 < mf + sz)) {
873 int diff = flower_off + (keys->off - mf);
f8b63e59
PJV
874 ovs_be32 *dst = (void *) (rewrite_key + diff);
875 ovs_be32 *dst_m = (void *) (rewrite_mask + diff);
dbcb014d 876 ovs_be32 mask, mask_word, data_word;
8ada482b
PB
877 uint32_t zero_bits;
878
dbcb014d
PJV
879 mask_word = htonl(ntohl(keys->mask) << m->boundary_shift);
880 data_word = htonl(ntohl(keys->val) << m->boundary_shift);
881 mask = ~(mask_word);
882
8ada482b
PB
883 if (keys->off < mf) {
884 zero_bits = 8 * (mf - keys->off);
f8b63e59 885 mask &= htonl(UINT32_MAX >> zero_bits);
8ada482b
PB
886 } else if (keys->off + 4 > mf + m->size) {
887 zero_bits = 8 * (keys->off + 4 - mf - m->size);
f8b63e59 888 mask &= htonl(UINT32_MAX << zero_bits);
8ada482b
PB
889 }
890
891 *dst_m |= mask;
dbcb014d 892 *dst |= data_word & mask;
8ada482b
PB
893 }
894 }
895
896 keys++;
897 i++;
898 }
899
0c70132c
CM
900 action = &flower->actions[flower->action_count++];
901 action->type = TC_ACT_PEDIT;
8ada482b
PB
902
903 return 0;
904}
905
f98e418f
RD
906static const struct nl_policy tunnel_key_policy[] = {
907 [TCA_TUNNEL_KEY_PARMS] = { .type = NL_A_UNSPEC,
908 .min_len = sizeof(struct tc_tunnel_key),
909 .optional = false, },
910 [TCA_TUNNEL_KEY_ENC_IPV4_SRC] = { .type = NL_A_U32, .optional = true, },
911 [TCA_TUNNEL_KEY_ENC_IPV4_DST] = { .type = NL_A_U32, .optional = true, },
912 [TCA_TUNNEL_KEY_ENC_IPV6_SRC] = { .type = NL_A_UNSPEC,
913 .min_len = sizeof(struct in6_addr),
914 .optional = true, },
915 [TCA_TUNNEL_KEY_ENC_IPV6_DST] = { .type = NL_A_UNSPEC,
916 .min_len = sizeof(struct in6_addr),
917 .optional = true, },
918 [TCA_TUNNEL_KEY_ENC_KEY_ID] = { .type = NL_A_U32, .optional = true, },
919 [TCA_TUNNEL_KEY_ENC_DST_PORT] = { .type = NL_A_U16, .optional = true, },
4b12e454
OG
920 [TCA_TUNNEL_KEY_ENC_TOS] = { .type = NL_A_U8, .optional = true, },
921 [TCA_TUNNEL_KEY_ENC_TTL] = { .type = NL_A_U8, .optional = true, },
202469aa 922 [TCA_TUNNEL_KEY_ENC_OPTS] = { .type = NL_A_NESTED, .optional = true, },
d9677a1f 923 [TCA_TUNNEL_KEY_NO_CSUM] = { .type = NL_A_U8, .optional = true, },
f98e418f
RD
924};
925
202469aa
PJV
926static int
927nl_parse_act_geneve_opts(const struct nlattr *in_nlattr,
928 struct tc_action *action)
929{
930 struct geneve_opt *opt = NULL;
931 const struct ofpbuf *msg;
932 uint16_t last_opt_type;
933 struct nlattr *nla;
934 struct ofpbuf buf;
935 size_t left;
936 int cnt;
937
938 nl_attr_get_nested(in_nlattr, &buf);
939 msg = &buf;
940
941 last_opt_type = TCA_TUNNEL_KEY_ENC_OPT_GENEVE_UNSPEC;
942 cnt = 0;
943 NL_ATTR_FOR_EACH (nla, left, ofpbuf_at(msg, 0, 0), msg->size) {
944 uint16_t type = nl_attr_type(nla);
945
946 switch (type) {
947 case TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS:
948 if (cnt && last_opt_type != TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA) {
949 VLOG_ERR_RL(&error_rl,
950 "failed to parse action geneve options class");
951 return EINVAL;
952 }
953
954 opt = &action->encap.data.opts.gnv[cnt];
955 opt->opt_class = nl_attr_get_be16(nla);
956 cnt += sizeof(struct geneve_opt) / 4;
957 action->encap.data.present.len += sizeof(struct geneve_opt);
958 last_opt_type = TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS;
959 break;
960 case TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE:
961 if (last_opt_type != TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS) {
962 VLOG_ERR_RL(&error_rl,
963 "failed to parse action geneve options type");
964 return EINVAL;
965 }
966
967 opt->type = nl_attr_get_u8(nla);
968 last_opt_type = TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE;
969 break;
970 case TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA:
971 if (last_opt_type != TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE) {
972 VLOG_ERR_RL(&error_rl,
973 "failed to parse action geneve options data");
974 return EINVAL;
975 }
976
977 opt->length = nl_attr_get_size(nla) / 4;
978 memcpy(opt + 1, nl_attr_get_unspec(nla, 1), opt->length * 4);
979 cnt += opt->length;
980 action->encap.data.present.len += opt->length * 4;
981 last_opt_type = TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA;
982 break;
983 }
984 }
985
986 if (last_opt_type != TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA) {
987 VLOG_ERR_RL(&error_rl,
988 "failed to parse action geneve options without data");
989 return EINVAL;
990 }
991
992 return 0;
993}
994
995static int
996nl_parse_act_tunnel_opts(struct nlattr *options, struct tc_action *action)
997{
998 const struct ofpbuf *msg;
999 struct nlattr *nla;
1000 struct ofpbuf buf;
1001 size_t left;
1002 int err;
1003
1004 if (!options) {
1005 return 0;
1006 }
1007
1008 nl_attr_get_nested(options, &buf);
1009 msg = &buf;
1010
1011 NL_ATTR_FOR_EACH (nla, left, ofpbuf_at(msg, 0, 0), msg->size) {
1012 uint16_t type = nl_attr_type(nla);
1013 switch (type) {
1014 case TCA_TUNNEL_KEY_ENC_OPTS_GENEVE:
1015 err = nl_parse_act_geneve_opts(nla, action);
1016 if (err) {
1017 return err;
1018 }
1019
1020 break;
1021 }
1022 }
1023
1024 return 0;
1025}
1026
f98e418f
RD
1027static int
1028nl_parse_act_tunnel_key(struct nlattr *options, struct tc_flower *flower)
1029{
1030 struct nlattr *tun_attrs[ARRAY_SIZE(tunnel_key_policy)];
1031 const struct nlattr *tun_parms;
1032 const struct tc_tunnel_key *tun;
0c70132c 1033 struct tc_action *action;
202469aa 1034 int err;
f98e418f
RD
1035
1036 if (!nl_parse_nested(options, tunnel_key_policy, tun_attrs,
1037 ARRAY_SIZE(tunnel_key_policy))) {
1038 VLOG_ERR_RL(&error_rl, "failed to parse tunnel_key action options");
1039 return EPROTO;
1040 }
1041
1042 tun_parms = tun_attrs[TCA_TUNNEL_KEY_PARMS];
1043 tun = nl_attr_get_unspec(tun_parms, sizeof *tun);
1044 if (tun->t_action == TCA_TUNNEL_KEY_ACT_SET) {
1045 struct nlattr *id = tun_attrs[TCA_TUNNEL_KEY_ENC_KEY_ID];
1046 struct nlattr *dst_port = tun_attrs[TCA_TUNNEL_KEY_ENC_DST_PORT];
1047 struct nlattr *ipv4_src = tun_attrs[TCA_TUNNEL_KEY_ENC_IPV4_SRC];
1048 struct nlattr *ipv4_dst = tun_attrs[TCA_TUNNEL_KEY_ENC_IPV4_DST];
1049 struct nlattr *ipv6_src = tun_attrs[TCA_TUNNEL_KEY_ENC_IPV6_SRC];
1050 struct nlattr *ipv6_dst = tun_attrs[TCA_TUNNEL_KEY_ENC_IPV6_DST];
4b12e454
OG
1051 struct nlattr *tos = tun_attrs[TCA_TUNNEL_KEY_ENC_TOS];
1052 struct nlattr *ttl = tun_attrs[TCA_TUNNEL_KEY_ENC_TTL];
202469aa 1053 struct nlattr *tun_opt = tun_attrs[TCA_TUNNEL_KEY_ENC_OPTS];
d9677a1f 1054 struct nlattr *no_csum = tun_attrs[TCA_TUNNEL_KEY_NO_CSUM];
f98e418f 1055
0c70132c
CM
1056 action = &flower->actions[flower->action_count++];
1057 action->type = TC_ACT_ENCAP;
1058 action->encap.ipv4.ipv4_src = ipv4_src ? nl_attr_get_be32(ipv4_src) : 0;
1059 action->encap.ipv4.ipv4_dst = ipv4_dst ? nl_attr_get_be32(ipv4_dst) : 0;
f98e418f 1060 if (ipv6_src) {
0c70132c 1061 action->encap.ipv6.ipv6_src = nl_attr_get_in6_addr(ipv6_src);
f98e418f
RD
1062 }
1063 if (ipv6_dst) {
0c70132c 1064 action->encap.ipv6.ipv6_dst = nl_attr_get_in6_addr(ipv6_dst);
f98e418f 1065 }
0c70132c 1066 action->encap.id = id ? be32_to_be64(nl_attr_get_be32(id)) : 0;
0227bf09 1067 action->encap.id_present = id ? true : false;
0c70132c 1068 action->encap.tp_dst = dst_port ? nl_attr_get_be16(dst_port) : 0;
4b12e454
OG
1069 action->encap.tos = tos ? nl_attr_get_u8(tos) : 0;
1070 action->encap.ttl = ttl ? nl_attr_get_u8(ttl) : 0;
d9677a1f 1071 action->encap.no_csum = no_csum ? nl_attr_get_u8(no_csum) : 0;
202469aa
PJV
1072
1073 err = nl_parse_act_tunnel_opts(tun_opt, action);
1074 if (err) {
1075 return err;
1076 }
f98e418f 1077 } else if (tun->t_action == TCA_TUNNEL_KEY_ACT_RELEASE) {
105e8179 1078 flower->tunnel = true;
f98e418f
RD
1079 } else {
1080 VLOG_ERR_RL(&error_rl, "unknown tunnel actions: %d, %d",
1081 tun->action, tun->t_action);
1082 return EINVAL;
1083 }
1084 return 0;
1085}
1086
1087static const struct nl_policy gact_policy[] = {
1088 [TCA_GACT_PARMS] = { .type = NL_A_UNSPEC,
1089 .min_len = sizeof(struct tc_gact),
1090 .optional = false, },
1091 [TCA_GACT_TM] = { .type = NL_A_UNSPEC,
1092 .min_len = sizeof(struct tcf_t),
1093 .optional = false, },
1094};
1095
8c1e74d1
PB
1096static int
1097get_user_hz(void)
1098{
1099 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1100 static int user_hz = 100;
1101
1102 if (ovsthread_once_start(&once)) {
1103 user_hz = sysconf(_SC_CLK_TCK);
1104 ovsthread_once_done(&once);
1105 }
1106
1107 return user_hz;
1108}
f98e418f
RD
1109
1110static void
1111nl_parse_tcf(const struct tcf_t *tm, struct tc_flower *flower)
1112{
8c1e74d1 1113 flower->lastused = time_msec() - (tm->lastuse * 1000 / get_user_hz());
f98e418f
RD
1114}
1115
1116static int
1117nl_parse_act_drop(struct nlattr *options, struct tc_flower *flower)
1118{
1119 struct nlattr *gact_attrs[ARRAY_SIZE(gact_policy)];
1120 const struct tc_gact *p;
1121 struct nlattr *gact_parms;
1122 const struct tcf_t *tm;
1123
1124 if (!nl_parse_nested(options, gact_policy, gact_attrs,
1125 ARRAY_SIZE(gact_policy))) {
1126 VLOG_ERR_RL(&error_rl, "failed to parse gact action options");
1127 return EPROTO;
1128 }
1129
1130 gact_parms = gact_attrs[TCA_GACT_PARMS];
1131 p = nl_attr_get_unspec(gact_parms, sizeof *p);
1132
1133 if (p->action != TC_ACT_SHOT) {
1134 VLOG_ERR_RL(&error_rl, "unknown gact action: %d", p->action);
1135 return EINVAL;
1136 }
1137
1138 tm = nl_attr_get_unspec(gact_attrs[TCA_GACT_TM], sizeof *tm);
1139 nl_parse_tcf(tm, flower);
1140
1141 return 0;
1142}
1143
1144static const struct nl_policy mirred_policy[] = {
1145 [TCA_MIRRED_PARMS] = { .type = NL_A_UNSPEC,
1146 .min_len = sizeof(struct tc_mirred),
1147 .optional = false, },
1148 [TCA_MIRRED_TM] = { .type = NL_A_UNSPEC,
1149 .min_len = sizeof(struct tcf_t),
1150 .optional = false, },
1151};
1152
1153static int
1154nl_parse_act_mirred(struct nlattr *options, struct tc_flower *flower)
1155{
1156
1157 struct nlattr *mirred_attrs[ARRAY_SIZE(mirred_policy)];
1158 const struct tc_mirred *m;
1159 const struct nlattr *mirred_parms;
1160 const struct tcf_t *tm;
1161 struct nlattr *mirred_tm;
0c70132c 1162 struct tc_action *action;
f98e418f
RD
1163
1164 if (!nl_parse_nested(options, mirred_policy, mirred_attrs,
1165 ARRAY_SIZE(mirred_policy))) {
1166 VLOG_ERR_RL(&error_rl, "failed to parse mirred action options");
1167 return EPROTO;
1168 }
1169
1170 mirred_parms = mirred_attrs[TCA_MIRRED_PARMS];
1171 m = nl_attr_get_unspec(mirred_parms, sizeof *m);
1172
4aa2dc04
JH
1173 if (m->eaction != TCA_EGRESS_REDIR && m->eaction != TCA_EGRESS_MIRROR &&
1174 m->eaction != TCA_INGRESS_REDIR && m->eaction != TCA_INGRESS_MIRROR) {
f98e418f 1175 VLOG_ERR_RL(&error_rl, "unknown mirred action: %d, %d, %d",
0c70132c 1176 m->action, m->eaction, m->ifindex);
f98e418f
RD
1177 return EINVAL;
1178 }
1179
0c70132c 1180 action = &flower->actions[flower->action_count++];
4aa2dc04
JH
1181 action->out.ifindex_out = m->ifindex;
1182 if (m->eaction == TCA_INGRESS_REDIR || m->eaction == TCA_INGRESS_MIRROR) {
1183 action->out.ingress = true;
1184 } else {
1185 action->out.ingress = false;
1186 }
0c70132c 1187 action->type = TC_ACT_OUTPUT;
f98e418f
RD
1188
1189 mirred_tm = mirred_attrs[TCA_MIRRED_TM];
1190 tm = nl_attr_get_unspec(mirred_tm, sizeof *tm);
1191 nl_parse_tcf(tm, flower);
1192
1193 return 0;
1194}
1195
1196static const struct nl_policy vlan_policy[] = {
1197 [TCA_VLAN_PARMS] = { .type = NL_A_UNSPEC,
1198 .min_len = sizeof(struct tc_vlan),
1199 .optional = false, },
1200 [TCA_VLAN_PUSH_VLAN_ID] = { .type = NL_A_U16, .optional = true, },
1201 [TCA_VLAN_PUSH_VLAN_PROTOCOL] = { .type = NL_A_U16, .optional = true, },
1202 [TCA_VLAN_PUSH_VLAN_PRIORITY] = { .type = NL_A_U8, .optional = true, },
1203};
1204
1205static int
1206nl_parse_act_vlan(struct nlattr *options, struct tc_flower *flower)
1207{
1208 struct nlattr *vlan_attrs[ARRAY_SIZE(vlan_policy)];
1209 const struct tc_vlan *v;
1210 const struct nlattr *vlan_parms;
0c70132c 1211 struct tc_action *action;
f98e418f
RD
1212
1213 if (!nl_parse_nested(options, vlan_policy, vlan_attrs,
1214 ARRAY_SIZE(vlan_policy))) {
1215 VLOG_ERR_RL(&error_rl, "failed to parse vlan action options");
1216 return EPROTO;
1217 }
1218
0c70132c 1219 action = &flower->actions[flower->action_count++];
f98e418f
RD
1220 vlan_parms = vlan_attrs[TCA_VLAN_PARMS];
1221 v = nl_attr_get_unspec(vlan_parms, sizeof *v);
1222 if (v->v_action == TCA_VLAN_ACT_PUSH) {
61e8655c 1223 struct nlattr *vlan_tpid = vlan_attrs[TCA_VLAN_PUSH_VLAN_PROTOCOL];
f98e418f
RD
1224 struct nlattr *vlan_id = vlan_attrs[TCA_VLAN_PUSH_VLAN_ID];
1225 struct nlattr *vlan_prio = vlan_attrs[TCA_VLAN_PUSH_VLAN_PRIORITY];
1226
10097f3f 1227 action->vlan.vlan_push_tpid = nl_attr_get_be16(vlan_tpid);
0c70132c
CM
1228 action->vlan.vlan_push_id = nl_attr_get_u16(vlan_id);
1229 action->vlan.vlan_push_prio = vlan_prio ? nl_attr_get_u8(vlan_prio) : 0;
1230 action->type = TC_ACT_VLAN_PUSH;
f98e418f 1231 } else if (v->v_action == TCA_VLAN_ACT_POP) {
0c70132c 1232 action->type = TC_ACT_VLAN_POP;
f98e418f
RD
1233 } else {
1234 VLOG_ERR_RL(&error_rl, "unknown vlan action: %d, %d",
1235 v->action, v->v_action);
1236 return EINVAL;
1237 }
1238 return 0;
1239}
1240
55412eac
JH
1241static const struct nl_policy mpls_policy[] = {
1242 [TCA_MPLS_PARMS] = { .type = NL_A_UNSPEC,
1243 .min_len = sizeof(struct tc_mpls),
1244 .optional = false, },
1245 [TCA_MPLS_PROTO] = { .type = NL_A_U16, .optional = true, },
283dcf85
JH
1246 [TCA_MPLS_LABEL] = { .type = NL_A_U32, .optional = true, },
1247 [TCA_MPLS_TC] = { .type = NL_A_U8, .optional = true, },
1248 [TCA_MPLS_TTL] = { .type = NL_A_U8, .optional = true, },
1249 [TCA_MPLS_BOS] = { .type = NL_A_U8, .optional = true, },
55412eac
JH
1250};
1251
1252static int
1253nl_parse_act_mpls(struct nlattr *options, struct tc_flower *flower)
1254{
1255 struct nlattr *mpls_attrs[ARRAY_SIZE(mpls_policy)];
1256 const struct nlattr *mpls_parms;
1257 struct nlattr *mpls_proto;
283dcf85 1258 struct nlattr *mpls_label;
55412eac
JH
1259 struct tc_action *action;
1260 const struct tc_mpls *m;
283dcf85
JH
1261 struct nlattr *mpls_ttl;
1262 struct nlattr *mpls_bos;
1263 struct nlattr *mpls_tc;
55412eac
JH
1264
1265 if (!nl_parse_nested(options, mpls_policy, mpls_attrs,
1266 ARRAY_SIZE(mpls_policy))) {
1267 VLOG_ERR_RL(&error_rl, "failed to parse mpls action options");
1268 return EPROTO;
1269 }
1270
1271 action = &flower->actions[flower->action_count++];
1272 mpls_parms = mpls_attrs[TCA_MPLS_PARMS];
1273 m = nl_attr_get_unspec(mpls_parms, sizeof *m);
1274
1275 switch (m->m_action) {
1276 case TCA_MPLS_ACT_POP:
1277 mpls_proto = mpls_attrs[TCA_MPLS_PROTO];
1278 if (mpls_proto) {
1279 action->mpls.proto = nl_attr_get_be16(mpls_proto);
1280 }
1281 action->type = TC_ACT_MPLS_POP;
1282 break;
283dcf85
JH
1283 case TCA_MPLS_ACT_PUSH:
1284 mpls_proto = mpls_attrs[TCA_MPLS_PROTO];
1285 if (mpls_proto) {
1286 action->mpls.proto = nl_attr_get_be16(mpls_proto);
1287 }
1288 mpls_label = mpls_attrs[TCA_MPLS_LABEL];
1289 if (mpls_label) {
1290 action->mpls.label = nl_attr_get_u32(mpls_label);
1291 }
1292 mpls_tc = mpls_attrs[TCA_MPLS_TC];
1293 if (mpls_tc) {
1294 action->mpls.tc = nl_attr_get_u8(mpls_tc);
1295 }
1296 mpls_ttl = mpls_attrs[TCA_MPLS_TTL];
1297 if (mpls_ttl) {
1298 action->mpls.ttl = nl_attr_get_u8(mpls_ttl);
1299 }
1300 mpls_bos = mpls_attrs[TCA_MPLS_BOS];
1301 if (mpls_bos) {
1302 action->mpls.bos = nl_attr_get_u8(mpls_bos);
1303 }
1304 action->type = TC_ACT_MPLS_PUSH;
1305 break;
a8f005cf
JH
1306 case TCA_MPLS_ACT_MODIFY:
1307 mpls_label = mpls_attrs[TCA_MPLS_LABEL];
1308 if (mpls_label) {
1309 action->mpls.label = nl_attr_get_u32(mpls_label);
1310 }
1311 mpls_tc = mpls_attrs[TCA_MPLS_TC];
1312 if (mpls_tc) {
1313 action->mpls.tc = nl_attr_get_u8(mpls_tc);
1314 }
1315 mpls_ttl = mpls_attrs[TCA_MPLS_TTL];
1316 if (mpls_ttl) {
1317 action->mpls.ttl = nl_attr_get_u8(mpls_ttl);
1318 }
1319 mpls_bos = mpls_attrs[TCA_MPLS_BOS];
1320 if (mpls_bos) {
1321 action->mpls.bos = nl_attr_get_u8(mpls_bos);
1322 }
1323 action->type = TC_ACT_MPLS_SET;
1324 break;
55412eac
JH
1325 default:
1326 VLOG_ERR_RL(&error_rl, "unknown mpls action: %d, %d",
1327 m->action, m->m_action);
1328 return EINVAL;
1329 }
1330
1331 return 0;
1332}
1333
d6118e62
PB
1334static const struct nl_policy csum_policy[] = {
1335 [TCA_CSUM_PARMS] = { .type = NL_A_UNSPEC,
1336 .min_len = sizeof(struct tc_csum),
1337 .optional = false, },
1338};
1339
1340static int
1341nl_parse_act_csum(struct nlattr *options, struct tc_flower *flower)
1342{
1343 struct nlattr *csum_attrs[ARRAY_SIZE(csum_policy)];
1344 const struct tc_csum *c;
1345 const struct nlattr *csum_parms;
1346
1347 if (!nl_parse_nested(options, csum_policy, csum_attrs,
1348 ARRAY_SIZE(csum_policy))) {
1349 VLOG_ERR_RL(&error_rl, "failed to parse csum action options");
1350 return EPROTO;
1351 }
1352
1353 csum_parms = csum_attrs[TCA_CSUM_PARMS];
1354 c = nl_attr_get_unspec(csum_parms, sizeof *c);
1355
1356 /* sanity checks */
1357 if (c->update_flags != flower->csum_update_flags) {
1358 VLOG_WARN_RL(&error_rl,
1359 "expected different act csum flags: 0x%x != 0x%x",
1360 flower->csum_update_flags, c->update_flags);
1361 return EINVAL;
1362 }
1363 flower->csum_update_flags = 0; /* so we know csum was handled */
1364
1365 if (flower->needs_full_ip_proto_mask
1366 && flower->mask.ip_proto != UINT8_MAX) {
1367 VLOG_WARN_RL(&error_rl, "expected full matching on flower ip_proto");
1368 return EINVAL;
1369 }
1370
1371 return 0;
1372}
1373
f98e418f
RD
1374static const struct nl_policy act_policy[] = {
1375 [TCA_ACT_KIND] = { .type = NL_A_STRING, .optional = false, },
1376 [TCA_ACT_COOKIE] = { .type = NL_A_UNSPEC, .optional = true, },
1377 [TCA_ACT_OPTIONS] = { .type = NL_A_NESTED, .optional = false, },
1378 [TCA_ACT_STATS] = { .type = NL_A_NESTED, .optional = false, },
1379};
1380
1381static const struct nl_policy stats_policy[] = {
1382 [TCA_STATS_BASIC] = { .type = NL_A_UNSPEC,
1383 .min_len = sizeof(struct gnet_stats_basic),
1384 .optional = false, },
1385};
1386
1387static int
1388nl_parse_single_action(struct nlattr *action, struct tc_flower *flower)
1389{
1390 struct nlattr *act_options;
1391 struct nlattr *act_stats;
1392 struct nlattr *act_cookie;
1393 const char *act_kind;
1394 struct nlattr *action_attrs[ARRAY_SIZE(act_policy)];
1395 struct nlattr *stats_attrs[ARRAY_SIZE(stats_policy)];
1396 struct ovs_flow_stats *stats = &flower->stats;
1397 const struct gnet_stats_basic *bs;
40c5aa11 1398 int err = 0;
f98e418f
RD
1399
1400 if (!nl_parse_nested(action, act_policy, action_attrs,
1401 ARRAY_SIZE(act_policy))) {
1402 VLOG_ERR_RL(&error_rl, "failed to parse single action options");
1403 return EPROTO;
1404 }
1405
1406 act_kind = nl_attr_get_string(action_attrs[TCA_ACT_KIND]);
1407 act_options = action_attrs[TCA_ACT_OPTIONS];
1408 act_cookie = action_attrs[TCA_ACT_COOKIE];
1409
1410 if (!strcmp(act_kind, "gact")) {
40c5aa11 1411 err = nl_parse_act_drop(act_options, flower);
f98e418f 1412 } else if (!strcmp(act_kind, "mirred")) {
40c5aa11 1413 err = nl_parse_act_mirred(act_options, flower);
f98e418f 1414 } else if (!strcmp(act_kind, "vlan")) {
40c5aa11 1415 err = nl_parse_act_vlan(act_options, flower);
55412eac
JH
1416 } else if (!strcmp(act_kind, "mpls")) {
1417 err = nl_parse_act_mpls(act_options, flower);
f98e418f 1418 } else if (!strcmp(act_kind, "tunnel_key")) {
40c5aa11 1419 err = nl_parse_act_tunnel_key(act_options, flower);
8ada482b 1420 } else if (!strcmp(act_kind, "pedit")) {
40c5aa11 1421 err = nl_parse_act_pedit(act_options, flower);
8ada482b 1422 } else if (!strcmp(act_kind, "csum")) {
d6118e62 1423 nl_parse_act_csum(act_options, flower);
4aa2dc04
JH
1424 } else if (!strcmp(act_kind, "skbedit")) {
1425 /* Added for TC rule only (not in OvS rule) so ignore. */
f98e418f
RD
1426 } else {
1427 VLOG_ERR_RL(&error_rl, "unknown tc action kind: %s", act_kind);
40c5aa11
RD
1428 err = EINVAL;
1429 }
1430
1431 if (err) {
1432 return err;
f98e418f
RD
1433 }
1434
1435 if (act_cookie) {
1436 flower->act_cookie.data = nl_attr_get(act_cookie);
1437 flower->act_cookie.len = nl_attr_get_size(act_cookie);
1438 }
1439
1440 act_stats = action_attrs[TCA_ACT_STATS];
1441
1442 if (!nl_parse_nested(act_stats, stats_policy, stats_attrs,
1443 ARRAY_SIZE(stats_policy))) {
1444 VLOG_ERR_RL(&error_rl, "failed to parse action stats policy");
1445 return EPROTO;
1446 }
1447
1448 bs = nl_attr_get_unspec(stats_attrs[TCA_STATS_BASIC], sizeof *bs);
1449 put_32aligned_u64(&stats->n_packets, bs->packets);
1450 put_32aligned_u64(&stats->n_bytes, bs->bytes);
1451
1452 return 0;
1453}
1454
1455#define TCA_ACT_MIN_PRIO 1
1456
1457static int
1458nl_parse_flower_actions(struct nlattr **attrs, struct tc_flower *flower)
1459{
1460 const struct nlattr *actions = attrs[TCA_FLOWER_ACT];
d0fbb09f 1461 static struct nl_policy actions_orders_policy[TCA_ACT_MAX_NUM + 1] = {};
f98e418f
RD
1462 struct nlattr *actions_orders[ARRAY_SIZE(actions_orders_policy)];
1463 const int max_size = ARRAY_SIZE(actions_orders_policy);
1464
1465 for (int i = TCA_ACT_MIN_PRIO; i < max_size; i++) {
1466 actions_orders_policy[i].type = NL_A_NESTED;
1467 actions_orders_policy[i].optional = true;
1468 }
1469
1470 if (!nl_parse_nested(actions, actions_orders_policy, actions_orders,
1471 ARRAY_SIZE(actions_orders_policy))) {
1472 VLOG_ERR_RL(&error_rl, "failed to parse flower order of actions");
1473 return EPROTO;
1474 }
1475
1476 for (int i = TCA_ACT_MIN_PRIO; i < max_size; i++) {
1477 if (actions_orders[i]) {
0c70132c
CM
1478 int err;
1479
d0fbb09f
CM
1480 if (flower->action_count >= TCA_ACT_MAX_NUM) {
1481 VLOG_DBG_RL(&error_rl, "Can only support %d actions", TCA_ACT_MAX_NUM);
0c70132c
CM
1482 return EOPNOTSUPP;
1483 }
1484 err = nl_parse_single_action(actions_orders[i], flower);
f98e418f
RD
1485
1486 if (err) {
1487 return err;
1488 }
1489 }
1490 }
1491
d6118e62
PB
1492 if (flower->csum_update_flags) {
1493 VLOG_WARN_RL(&error_rl,
1494 "expected act csum with flags: 0x%x",
1495 flower->csum_update_flags);
1496 return EINVAL;
1497 }
1498
f98e418f
RD
1499 return 0;
1500}
1501
1502static int
1503nl_parse_flower_options(struct nlattr *nl_options, struct tc_flower *flower)
1504{
1505 struct nlattr *attrs[ARRAY_SIZE(tca_flower_policy)];
a468645c 1506 int err;
f98e418f
RD
1507
1508 if (!nl_parse_nested(nl_options, tca_flower_policy,
1509 attrs, ARRAY_SIZE(tca_flower_policy))) {
1510 VLOG_ERR_RL(&error_rl, "failed to parse flower classifier options");
1511 return EPROTO;
1512 }
1513
1514 nl_parse_flower_eth(attrs, flower);
34b16955 1515 nl_parse_flower_mpls(attrs, flower);
f98e418f
RD
1516 nl_parse_flower_vlan(attrs, flower);
1517 nl_parse_flower_ip(attrs, flower);
a468645c
PJV
1518 err = nl_parse_flower_tunnel(attrs, flower);
1519 if (err) {
1520 return err;
1521 }
1522
d63ca532 1523 nl_parse_flower_flags(attrs, flower);
f98e418f
RD
1524 return nl_parse_flower_actions(attrs, flower);
1525}
1526
1527int
1528parse_netlink_to_tc_flower(struct ofpbuf *reply, struct tc_flower *flower)
1529{
1530 struct tcmsg *tc;
1531 struct nlattr *ta[ARRAY_SIZE(tca_policy)];
1532 const char *kind;
1533
1534 if (NLMSG_HDRLEN + sizeof *tc > reply->size) {
1535 return EPROTO;
1536 }
1537
1538 memset(flower, 0, sizeof *flower);
1539
1540 tc = ofpbuf_at_assert(reply, NLMSG_HDRLEN, sizeof *tc);
1541 flower->handle = tc->tcm_handle;
1542 flower->key.eth_type = (OVS_FORCE ovs_be16) tc_get_minor(tc->tcm_info);
1543 flower->mask.eth_type = OVS_BE16_MAX;
1544 flower->prio = tc_get_major(tc->tcm_info);
1545
e7f6ba22
PJV
1546 if (flower->prio == TC_RESERVED_PRIORITY_POLICE) {
1547 return 0;
1548 }
1549
f98e418f
RD
1550 if (!flower->handle) {
1551 return EAGAIN;
1552 }
1553
1554 if (!nl_policy_parse(reply, NLMSG_HDRLEN + sizeof *tc,
1555 tca_policy, ta, ARRAY_SIZE(ta))) {
1556 VLOG_ERR_RL(&error_rl, "failed to parse tca policy");
1557 return EPROTO;
1558 }
1559
1560 kind = nl_attr_get_string(ta[TCA_KIND]);
1561 if (strcmp(kind, "flower")) {
763e120d 1562 VLOG_DBG_ONCE("Unsupported filter: %s", kind);
f98e418f
RD
1563 return EPROTO;
1564 }
1565
1566 return nl_parse_flower_options(ta[TCA_OPTIONS], flower);
1567}
1568
1569int
95255018
JH
1570tc_dump_flower_start(int ifindex, struct nl_dump *dump, uint32_t block_id,
1571 enum tc_qdisc_hook hook)
f98e418f
RD
1572{
1573 struct ofpbuf request;
1574 struct tcmsg *tcmsg;
093c9458 1575 int index;
f98e418f 1576
093c9458
JH
1577 index = block_id ? TCM_IFINDEX_MAGIC_BLOCK : ifindex;
1578 tcmsg = tc_make_request(index, RTM_GETTFILTER, NLM_F_DUMP, &request);
95255018
JH
1579 tcmsg->tcm_parent = (hook == TC_EGRESS) ?
1580 TC_EGRESS_PARENT : (block_id ? : TC_INGRESS_PARENT);
f98e418f
RD
1581 tcmsg->tcm_info = TC_H_UNSPEC;
1582 tcmsg->tcm_handle = 0;
1583
1584 nl_dump_start(dump, NETLINK_ROUTE, &request);
1585 ofpbuf_uninit(&request);
1586
1587 return 0;
1588}
1589
1590int
95255018 1591tc_flush(int ifindex, uint32_t block_id, enum tc_qdisc_hook hook)
f98e418f
RD
1592{
1593 struct ofpbuf request;
1594 struct tcmsg *tcmsg;
093c9458 1595 int index;
f98e418f 1596
093c9458
JH
1597 index = block_id ? TCM_IFINDEX_MAGIC_BLOCK : ifindex;
1598 tcmsg = tc_make_request(index, RTM_DELTFILTER, NLM_F_ACK, &request);
95255018
JH
1599 tcmsg->tcm_parent = (hook == TC_EGRESS) ?
1600 TC_EGRESS_PARENT : (block_id ? : TC_INGRESS_PARENT);
f98e418f
RD
1601 tcmsg->tcm_info = TC_H_UNSPEC;
1602
1603 return tc_transact(&request, NULL);
1604}
1605
1606int
95255018
JH
1607tc_del_filter(int ifindex, int prio, int handle, uint32_t block_id,
1608 enum tc_qdisc_hook hook)
f98e418f
RD
1609{
1610 struct ofpbuf request;
1611 struct tcmsg *tcmsg;
1612 struct ofpbuf *reply;
1613 int error;
093c9458 1614 int index;
f98e418f 1615
093c9458
JH
1616 index = block_id ? TCM_IFINDEX_MAGIC_BLOCK : ifindex;
1617 tcmsg = tc_make_request(index, RTM_DELTFILTER, NLM_F_ECHO, &request);
95255018
JH
1618 tcmsg->tcm_parent = (hook == TC_EGRESS) ?
1619 TC_EGRESS_PARENT : (block_id ? : TC_INGRESS_PARENT);
f98e418f
RD
1620 tcmsg->tcm_info = tc_make_handle(prio, 0);
1621 tcmsg->tcm_handle = handle;
1622
1623 error = tc_transact(&request, &reply);
1624 if (!error) {
1625 ofpbuf_delete(reply);
1626 }
1627 return error;
1628}
1629
1630int
093c9458 1631tc_get_flower(int ifindex, int prio, int handle, struct tc_flower *flower,
95255018 1632 uint32_t block_id, enum tc_qdisc_hook hook)
f98e418f
RD
1633{
1634 struct ofpbuf request;
1635 struct tcmsg *tcmsg;
1636 struct ofpbuf *reply;
1637 int error;
093c9458 1638 int index;
f98e418f 1639
093c9458
JH
1640 index = block_id ? TCM_IFINDEX_MAGIC_BLOCK : ifindex;
1641 tcmsg = tc_make_request(index, RTM_GETTFILTER, NLM_F_ECHO, &request);
95255018
JH
1642 tcmsg->tcm_parent = (hook == TC_EGRESS) ?
1643 TC_EGRESS_PARENT : (block_id ? : TC_INGRESS_PARENT);
f98e418f
RD
1644 tcmsg->tcm_info = tc_make_handle(prio, 0);
1645 tcmsg->tcm_handle = handle;
1646
1647 error = tc_transact(&request, &reply);
1648 if (error) {
1649 return error;
1650 }
1651
1652 error = parse_netlink_to_tc_flower(reply, flower);
1653 ofpbuf_delete(reply);
1654 return error;
1655}
1656
691d20cb
PB
1657static int
1658tc_get_tc_cls_policy(enum tc_offload_policy policy)
1659{
1660 if (policy == TC_POLICY_SKIP_HW) {
1661 return TCA_CLS_FLAGS_SKIP_HW;
1662 } else if (policy == TC_POLICY_SKIP_SW) {
1663 return TCA_CLS_FLAGS_SKIP_SW;
1664 }
1665
1666 return 0;
1667}
1668
8ada482b
PB
1669static void
1670nl_msg_put_act_csum(struct ofpbuf *request, uint32_t flags)
1671{
1672 size_t offset;
1673
1674 nl_msg_put_string(request, TCA_ACT_KIND, "csum");
1675 offset = nl_msg_start_nested(request, TCA_ACT_OPTIONS);
1676 {
1677 struct tc_csum parm = { .action = TC_ACT_PIPE,
1678 .update_flags = flags };
1679
1680 nl_msg_put_unspec(request, TCA_CSUM_PARMS, &parm, sizeof parm);
1681 }
1682 nl_msg_end_nested(request, offset);
1683}
1684
1685static void
1686nl_msg_put_act_pedit(struct ofpbuf *request, struct tc_pedit *parm,
1687 struct tc_pedit_key_ex *ex)
1688{
e13bbbab 1689 size_t ksize = sizeof *parm + parm->nkeys * sizeof(struct tc_pedit_key);
8ada482b
PB
1690 size_t offset, offset_keys_ex, offset_key;
1691 int i;
1692
1693 nl_msg_put_string(request, TCA_ACT_KIND, "pedit");
1694 offset = nl_msg_start_nested(request, TCA_ACT_OPTIONS);
1695 {
1696 parm->action = TC_ACT_PIPE;
1697
1698 nl_msg_put_unspec(request, TCA_PEDIT_PARMS_EX, parm, ksize);
1699 offset_keys_ex = nl_msg_start_nested(request, TCA_PEDIT_KEYS_EX);
1700 for (i = 0; i < parm->nkeys; i++, ex++) {
1701 offset_key = nl_msg_start_nested(request, TCA_PEDIT_KEY_EX);
1702 nl_msg_put_u16(request, TCA_PEDIT_KEY_EX_HTYPE, ex->htype);
1703 nl_msg_put_u16(request, TCA_PEDIT_KEY_EX_CMD, ex->cmd);
1704 nl_msg_end_nested(request, offset_key);
1705 }
1706 nl_msg_end_nested(request, offset_keys_ex);
1707 }
1708 nl_msg_end_nested(request, offset);
1709}
1710
f98e418f 1711static void
10097f3f 1712nl_msg_put_act_push_vlan(struct ofpbuf *request, ovs_be16 tpid,
61e8655c 1713 uint16_t vid, uint8_t prio)
f98e418f
RD
1714{
1715 size_t offset;
1716
1717 nl_msg_put_string(request, TCA_ACT_KIND, "vlan");
1718 offset = nl_msg_start_nested(request, TCA_ACT_OPTIONS);
1719 {
1720 struct tc_vlan parm = { .action = TC_ACT_PIPE,
1721 .v_action = TCA_VLAN_ACT_PUSH };
1722
1723 nl_msg_put_unspec(request, TCA_VLAN_PARMS, &parm, sizeof parm);
10097f3f 1724 nl_msg_put_be16(request, TCA_VLAN_PUSH_VLAN_PROTOCOL, tpid);
f98e418f
RD
1725 nl_msg_put_u16(request, TCA_VLAN_PUSH_VLAN_ID, vid);
1726 nl_msg_put_u8(request, TCA_VLAN_PUSH_VLAN_PRIORITY, prio);
1727 }
1728 nl_msg_end_nested(request, offset);
1729}
1730
1731static void
1732nl_msg_put_act_pop_vlan(struct ofpbuf *request)
1733{
1734 size_t offset;
1735
1736 nl_msg_put_string(request, TCA_ACT_KIND, "vlan");
1737 offset = nl_msg_start_nested(request, TCA_ACT_OPTIONS);
1738 {
1739 struct tc_vlan parm = { .action = TC_ACT_PIPE,
1740 .v_action = TCA_VLAN_ACT_POP };
1741
1742 nl_msg_put_unspec(request, TCA_VLAN_PARMS, &parm, sizeof parm);
1743 }
1744 nl_msg_end_nested(request, offset);
1745}
1746
55412eac
JH
1747static void
1748nl_msg_put_act_pop_mpls(struct ofpbuf *request, ovs_be16 proto)
1749{
1750 size_t offset;
1751
1752 nl_msg_put_string(request, TCA_ACT_KIND, "mpls");
1753 offset = nl_msg_start_nested(request, TCA_ACT_OPTIONS | NLA_F_NESTED);
1754 {
1755 struct tc_mpls parm = { .action = TC_ACT_PIPE,
1756 .m_action = TCA_MPLS_ACT_POP };
1757
1758 nl_msg_put_unspec(request, TCA_MPLS_PARMS, &parm, sizeof parm);
1759 nl_msg_put_be16(request, TCA_MPLS_PROTO, proto);
1760 }
1761 nl_msg_end_nested(request, offset);
1762}
1763
283dcf85
JH
1764static void
1765nl_msg_put_act_push_mpls(struct ofpbuf *request, ovs_be16 proto,
1766 uint32_t label, uint8_t tc, uint8_t ttl, uint8_t bos)
1767{
1768 size_t offset;
1769
1770 nl_msg_put_string(request, TCA_ACT_KIND, "mpls");
1771 offset = nl_msg_start_nested(request, TCA_ACT_OPTIONS | NLA_F_NESTED);
1772 {
1773 struct tc_mpls parm = { .action = TC_ACT_PIPE,
1774 .m_action = TCA_MPLS_ACT_PUSH };
1775
1776 nl_msg_put_unspec(request, TCA_MPLS_PARMS, &parm, sizeof parm);
1777 nl_msg_put_be16(request, TCA_MPLS_PROTO, proto);
1778 nl_msg_put_u32(request, TCA_MPLS_LABEL, label);
1779 nl_msg_put_u8(request, TCA_MPLS_TC, tc);
1780 nl_msg_put_u8(request, TCA_MPLS_TTL, ttl);
1781 nl_msg_put_u8(request, TCA_MPLS_BOS, bos);
1782 }
1783 nl_msg_end_nested(request, offset);
1784}
1785
a8f005cf
JH
1786static void
1787nl_msg_put_act_set_mpls(struct ofpbuf *request, uint32_t label, uint8_t tc,
1788 uint8_t ttl, uint8_t bos)
1789{
1790 size_t offset;
1791
1792 nl_msg_put_string(request, TCA_ACT_KIND, "mpls");
1793 offset = nl_msg_start_nested(request, TCA_ACT_OPTIONS | NLA_F_NESTED);
1794 {
1795 struct tc_mpls parm = { .action = TC_ACT_PIPE,
1796 .m_action = TCA_MPLS_ACT_MODIFY };
1797
1798 nl_msg_put_unspec(request, TCA_MPLS_PARMS, &parm, sizeof parm);
1799 nl_msg_put_u32(request, TCA_MPLS_LABEL, label);
1800 nl_msg_put_u8(request, TCA_MPLS_TC, tc);
1801 nl_msg_put_u8(request, TCA_MPLS_TTL, ttl);
1802 nl_msg_put_u8(request, TCA_MPLS_BOS, bos);
1803 }
1804 nl_msg_end_nested(request, offset);
1805}
1806
f98e418f
RD
1807static void
1808nl_msg_put_act_tunnel_key_release(struct ofpbuf *request)
1809{
1810 size_t offset;
1811
1812 nl_msg_put_string(request, TCA_ACT_KIND, "tunnel_key");
1813 offset = nl_msg_start_nested(request, TCA_ACT_OPTIONS);
1814 {
1815 struct tc_tunnel_key tun = { .action = TC_ACT_PIPE,
1816 .t_action = TCA_TUNNEL_KEY_ACT_RELEASE };
1817
1818 nl_msg_put_unspec(request, TCA_TUNNEL_KEY_PARMS, &tun, sizeof tun);
1819 }
1820 nl_msg_end_nested(request, offset);
1821}
1822
202469aa
PJV
1823static void
1824nl_msg_put_act_tunnel_geneve_option(struct ofpbuf *request,
1825 struct tun_metadata tun_metadata)
1826{
1827 const struct geneve_opt *opt;
1828 size_t outer, inner;
1829 int len, cnt = 0;
1830
1831 len = tun_metadata.present.len;
1832 if (!len) {
1833 return;
1834 }
1835
1836 outer = nl_msg_start_nested(request, TCA_TUNNEL_KEY_ENC_OPTS);
1837
1838 while (len) {
1839 opt = &tun_metadata.opts.gnv[cnt];
1840 inner = nl_msg_start_nested(request, TCA_TUNNEL_KEY_ENC_OPTS_GENEVE);
1841
1842 nl_msg_put_be16(request, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS,
1843 opt->opt_class);
1844 nl_msg_put_u8(request, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE, opt->type);
1845 nl_msg_put_unspec(request, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA, opt + 1,
1846 opt->length * 4);
1847
1848 cnt += sizeof(struct geneve_opt) / 4 + opt->length;
1849 len -= sizeof(struct geneve_opt) + opt->length * 4;
1850
1851 nl_msg_end_nested(request, inner);
1852 }
1853
1854 nl_msg_end_nested(request, outer);
1855}
1856
f98e418f 1857static void
0227bf09
AN
1858nl_msg_put_act_tunnel_key_set(struct ofpbuf *request, bool id_present,
1859 ovs_be64 id, ovs_be32 ipv4_src,
1860 ovs_be32 ipv4_dst, struct in6_addr *ipv6_src,
202469aa
PJV
1861 struct in6_addr *ipv6_dst,
1862 ovs_be16 tp_dst, uint8_t tos, uint8_t ttl,
d9677a1f
EB
1863 struct tun_metadata tun_metadata,
1864 uint8_t no_csum)
f98e418f
RD
1865{
1866 size_t offset;
1867
1868 nl_msg_put_string(request, TCA_ACT_KIND, "tunnel_key");
1869 offset = nl_msg_start_nested(request, TCA_ACT_OPTIONS);
1870 {
1871 struct tc_tunnel_key tun = { .action = TC_ACT_PIPE,
1872 .t_action = TCA_TUNNEL_KEY_ACT_SET };
1873
1874 nl_msg_put_unspec(request, TCA_TUNNEL_KEY_PARMS, &tun, sizeof tun);
1875
1876 ovs_be32 id32 = be64_to_be32(id);
0227bf09
AN
1877 if (id_present) {
1878 nl_msg_put_be32(request, TCA_TUNNEL_KEY_ENC_KEY_ID, id32);
1879 }
f98e418f
RD
1880 if (ipv4_dst) {
1881 nl_msg_put_be32(request, TCA_TUNNEL_KEY_ENC_IPV4_SRC, ipv4_src);
1882 nl_msg_put_be32(request, TCA_TUNNEL_KEY_ENC_IPV4_DST, ipv4_dst);
1883 } else if (!is_all_zeros(ipv6_dst, sizeof *ipv6_dst)) {
1884 nl_msg_put_in6_addr(request, TCA_TUNNEL_KEY_ENC_IPV6_DST,
1885 ipv6_dst);
1886 nl_msg_put_in6_addr(request, TCA_TUNNEL_KEY_ENC_IPV6_SRC,
1887 ipv6_src);
1888 }
4b12e454
OG
1889 if (tos) {
1890 nl_msg_put_u8(request, TCA_TUNNEL_KEY_ENC_TOS, tos);
1891 }
1892 if (ttl) {
1893 nl_msg_put_u8(request, TCA_TUNNEL_KEY_ENC_TTL, ttl);
1894 }
e48f49e0
EB
1895 if (tp_dst) {
1896 nl_msg_put_be16(request, TCA_TUNNEL_KEY_ENC_DST_PORT, tp_dst);
1897 }
202469aa 1898 nl_msg_put_act_tunnel_geneve_option(request, tun_metadata);
d9677a1f 1899 nl_msg_put_u8(request, TCA_TUNNEL_KEY_NO_CSUM, no_csum);
f98e418f
RD
1900 }
1901 nl_msg_end_nested(request, offset);
1902}
1903
1904static void
1905nl_msg_put_act_drop(struct ofpbuf *request)
1906{
1907 size_t offset;
1908
1909 nl_msg_put_string(request, TCA_ACT_KIND, "gact");
1910 offset = nl_msg_start_nested(request, TCA_ACT_OPTIONS);
1911 {
1912 struct tc_gact p = { .action = TC_ACT_SHOT };
1913
1914 nl_msg_put_unspec(request, TCA_GACT_PARMS, &p, sizeof p);
1915 }
1916 nl_msg_end_nested(request, offset);
1917}
1918
4aa2dc04
JH
1919static void
1920nl_msg_put_act_skbedit_to_host(struct ofpbuf *request)
1921{
1922 size_t offset;
1923
1924 nl_msg_put_string(request, TCA_ACT_KIND, "skbedit");
1925 offset = nl_msg_start_nested(request, TCA_ACT_OPTIONS);
1926 {
1927 struct tc_skbedit s = { .action = TC_ACT_PIPE };
1928
1929 nl_msg_put_unspec(request, TCA_SKBEDIT_PARMS, &s, sizeof s);
1930 nl_msg_put_be16(request, TCA_SKBEDIT_PTYPE, PACKET_HOST);
1931 }
1932 nl_msg_end_nested(request, offset);
1933}
1934
f98e418f 1935static void
00a0a011
CM
1936nl_msg_put_act_mirred(struct ofpbuf *request, int ifindex, int action,
1937 int eaction)
f98e418f
RD
1938{
1939 size_t offset;
1940
1941 nl_msg_put_string(request, TCA_ACT_KIND, "mirred");
1942 offset = nl_msg_start_nested(request, TCA_ACT_OPTIONS);
1943 {
00a0a011
CM
1944 struct tc_mirred m = { .action = action,
1945 .eaction = eaction,
f98e418f
RD
1946 .ifindex = ifindex };
1947
1948 nl_msg_put_unspec(request, TCA_MIRRED_PARMS, &m, sizeof m);
1949 }
1950 nl_msg_end_nested(request, offset);
1951}
1952
1953static inline void
1954nl_msg_put_act_cookie(struct ofpbuf *request, struct tc_cookie *ck) {
1955 if (ck->len) {
1956 nl_msg_put_unspec(request, TCA_ACT_COOKIE, ck->data, ck->len);
1957 }
1958}
1959
292d5bd9
VB
1960static inline void
1961nl_msg_put_act_flags(struct ofpbuf *request) {
1962 struct nla_bitfield32 act_flags = { TCA_ACT_FLAGS_NO_PERCPU_STATS,
1963 TCA_ACT_FLAGS_NO_PERCPU_STATS };
1964
1965 nl_msg_put_unspec(request, TCA_ACT_FLAGS, &act_flags, sizeof act_flags);
1966}
1967
8ada482b
PB
1968/* Given flower, a key_to_pedit map entry, calculates the rest,
1969 * where:
1970 *
1971 * mask, data - pointers of where read the first word of flower->key/mask.
1972 * current_offset - which offset to use for the first pedit action.
1973 * cnt - max pedits actions to use.
1974 * first_word_mask/last_word_mask - the mask to use for the first/last read
1975 * (as we read entire words). */
f98e418f 1976static void
8ada482b 1977calc_offsets(struct tc_flower *flower, struct flower_key_to_pedit *m,
f8b63e59
PJV
1978 int *cur_offset, int *cnt, ovs_be32 *last_word_mask,
1979 ovs_be32 *first_word_mask, ovs_be32 **mask, ovs_be32 **data)
8ada482b
PB
1980{
1981 int start_offset, max_offset, total_size;
1982 int diff, right_zero_bits, left_zero_bits;
1983 char *rewrite_key = (void *) &flower->rewrite.key;
1984 char *rewrite_mask = (void *) &flower->rewrite.mask;
1985
1986 max_offset = m->offset + m->size;
1987 start_offset = ROUND_DOWN(m->offset, 4);
1988 diff = m->offset - start_offset;
1989 total_size = max_offset - start_offset;
0d9f0cd4 1990 right_zero_bits = 8 * (4 - ((max_offset % 4) ? : 4));
8ada482b
PB
1991 left_zero_bits = 8 * (m->offset - start_offset);
1992
1993 *cur_offset = start_offset;
1994 *cnt = (total_size / 4) + (total_size % 4 ? 1 : 0);
f8b63e59
PJV
1995 *last_word_mask = htonl(UINT32_MAX << right_zero_bits);
1996 *first_word_mask = htonl(UINT32_MAX >> left_zero_bits);
8ada482b
PB
1997 *data = (void *) (rewrite_key + m->flower_offset - diff);
1998 *mask = (void *) (rewrite_mask + m->flower_offset - diff);
1999}
2000
d6118e62 2001static inline int
8ada482b
PB
2002csum_update_flag(struct tc_flower *flower,
2003 enum pedit_header_type htype) {
d6118e62
PB
2004 /* Explictily specifiy the csum flags so HW can return EOPNOTSUPP
2005 * if it doesn't support a checksum recalculation of some headers.
2006 * And since OVS allows a flow such as
2007 * eth(dst=<mac>),eth_type(0x0800) actions=set(ipv4(src=<new_ip>))
2008 * we need to force a more specific flow as this can, for example,
2009 * need a recalculation of icmp checksum if the packet that passes
d5ac6458 2010 * is ICMPv6 and tcp checksum if its tcp. */
d6118e62
PB
2011
2012 switch (htype) {
2013 case TCA_PEDIT_KEY_EX_HDR_TYPE_IP4:
8ada482b 2014 flower->csum_update_flags |= TCA_CSUM_UPDATE_FLAG_IPV4HDR;
eeb0ca88 2015 /* Fall through. */
d6118e62
PB
2016 case TCA_PEDIT_KEY_EX_HDR_TYPE_IP6:
2017 case TCA_PEDIT_KEY_EX_HDR_TYPE_TCP:
2018 case TCA_PEDIT_KEY_EX_HDR_TYPE_UDP:
8ada482b 2019 if (flower->key.ip_proto == IPPROTO_TCP) {
d6118e62 2020 flower->needs_full_ip_proto_mask = true;
8ada482b
PB
2021 flower->csum_update_flags |= TCA_CSUM_UPDATE_FLAG_TCP;
2022 } else if (flower->key.ip_proto == IPPROTO_UDP) {
d6118e62 2023 flower->needs_full_ip_proto_mask = true;
8ada482b 2024 flower->csum_update_flags |= TCA_CSUM_UPDATE_FLAG_UDP;
d5ac6458
JL
2025 } else if (flower->key.ip_proto == IPPROTO_ICMP) {
2026 flower->needs_full_ip_proto_mask = true;
2027 } else if (flower->key.ip_proto == IPPROTO_ICMPV6) {
d6118e62 2028 flower->needs_full_ip_proto_mask = true;
8ada482b 2029 flower->csum_update_flags |= TCA_CSUM_UPDATE_FLAG_ICMP;
d6118e62
PB
2030 } else {
2031 VLOG_WARN_RL(&error_rl,
2032 "can't offload rewrite of IP/IPV6 with ip_proto: %d",
2033 flower->key.ip_proto);
2034 break;
8ada482b 2035 }
eeb0ca88 2036 /* Fall through. */
d6118e62
PB
2037 case TCA_PEDIT_KEY_EX_HDR_TYPE_ETH:
2038 return 0; /* success */
2039
2040 case TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK:
2041 case __PEDIT_HDR_TYPE_MAX:
2042 default:
2043 break;
8ada482b 2044 }
d6118e62
PB
2045
2046 return EOPNOTSUPP;
8ada482b
PB
2047}
2048
2049static int
2050nl_msg_put_flower_rewrite_pedits(struct ofpbuf *request,
2051 struct tc_flower *flower)
2052{
2053 struct {
2054 struct tc_pedit sel;
2055 struct tc_pedit_key keys[MAX_PEDIT_OFFSETS];
2056 struct tc_pedit_key_ex keys_ex[MAX_PEDIT_OFFSETS];
2057 } sel = {
2058 .sel = {
2059 .nkeys = 0
2060 }
2061 };
d6118e62 2062 int i, j, err;
8ada482b
PB
2063
2064 for (i = 0; i < ARRAY_SIZE(flower_pedit_map); i++) {
2065 struct flower_key_to_pedit *m = &flower_pedit_map[i];
2066 struct tc_pedit_key *pedit_key = NULL;
2067 struct tc_pedit_key_ex *pedit_key_ex = NULL;
f8b63e59 2068 ovs_be32 *mask, *data, first_word_mask, last_word_mask;
8ada482b
PB
2069 int cnt = 0, cur_offset = 0;
2070
2071 if (!m->size) {
2072 continue;
2073 }
2074
2075 calc_offsets(flower, m, &cur_offset, &cnt, &last_word_mask,
2076 &first_word_mask, &mask, &data);
2077
2078 for (j = 0; j < cnt; j++, mask++, data++, cur_offset += 4) {
f8b63e59 2079 ovs_be32 mask_word = *mask;
dbcb014d 2080 ovs_be32 data_word = *data;
8ada482b
PB
2081
2082 if (j == 0) {
2083 mask_word &= first_word_mask;
2084 }
2085 if (j == cnt - 1) {
2086 mask_word &= last_word_mask;
2087 }
2088 if (!mask_word) {
2089 continue;
2090 }
2091 if (sel.sel.nkeys == MAX_PEDIT_OFFSETS) {
2092 VLOG_WARN_RL(&error_rl, "reached too many pedit offsets: %d",
2093 MAX_PEDIT_OFFSETS);
2094 return EOPNOTSUPP;
2095 }
2096
2097 pedit_key = &sel.keys[sel.sel.nkeys];
2098 pedit_key_ex = &sel.keys_ex[sel.sel.nkeys];
2099 pedit_key_ex->cmd = TCA_PEDIT_KEY_EX_CMD_SET;
2100 pedit_key_ex->htype = m->htype;
2101 pedit_key->off = cur_offset;
dbcb014d
PJV
2102 mask_word = htonl(ntohl(mask_word) >> m->boundary_shift);
2103 data_word = htonl(ntohl(data_word) >> m->boundary_shift);
8ada482b 2104 pedit_key->mask = ~mask_word;
dbcb014d 2105 pedit_key->val = data_word & mask_word;
8ada482b 2106 sel.sel.nkeys++;
d6118e62
PB
2107
2108 err = csum_update_flag(flower, m->htype);
2109 if (err) {
2110 return err;
2111 }
2112
2113 if (flower->needs_full_ip_proto_mask) {
2114 flower->mask.ip_proto = UINT8_MAX;
2115 }
8ada482b
PB
2116 }
2117 }
2118 nl_msg_put_act_pedit(request, &sel.sel, sel.keys_ex);
2119
2120 return 0;
2121}
2122
2123static int
f98e418f
RD
2124nl_msg_put_flower_acts(struct ofpbuf *request, struct tc_flower *flower)
2125{
2126 size_t offset;
2127 size_t act_offset;
0c70132c
CM
2128 uint16_t act_index = 1;
2129 struct tc_action *action;
2130 int i, ifindex = 0;
4aa2dc04 2131 bool ingress;
f98e418f
RD
2132
2133 offset = nl_msg_start_nested(request, TCA_FLOWER_ACT);
2134 {
8ada482b
PB
2135 int error;
2136
105e8179 2137 if (flower->tunnel) {
8ada482b 2138 act_offset = nl_msg_start_nested(request, act_index++);
0c70132c 2139 nl_msg_put_act_tunnel_key_release(request);
292d5bd9 2140 nl_msg_put_act_flags(request);
8ada482b 2141 nl_msg_end_nested(request, act_offset);
0c70132c 2142 }
f98e418f 2143
0c70132c
CM
2144 action = flower->actions;
2145 for (i = 0; i < flower->action_count; i++, action++) {
2146 switch (action->type) {
2147 case TC_ACT_PEDIT: {
a7ce5b85 2148 act_offset = nl_msg_start_nested(request, act_index++);
0c70132c
CM
2149 error = nl_msg_put_flower_rewrite_pedits(request, flower);
2150 if (error) {
2151 return error;
2152 }
a7ce5b85 2153 nl_msg_end_nested(request, act_offset);
0c70132c
CM
2154
2155 if (flower->csum_update_flags) {
2156 act_offset = nl_msg_start_nested(request, act_index++);
2157 nl_msg_put_act_csum(request, flower->csum_update_flags);
292d5bd9 2158 nl_msg_put_act_flags(request);
0c70132c
CM
2159 nl_msg_end_nested(request, act_offset);
2160 }
2161 }
2162 break;
2163 case TC_ACT_ENCAP: {
2164 act_offset = nl_msg_start_nested(request, act_index++);
0227bf09
AN
2165 nl_msg_put_act_tunnel_key_set(request, action->encap.id_present,
2166 action->encap.id,
0c70132c
CM
2167 action->encap.ipv4.ipv4_src,
2168 action->encap.ipv4.ipv4_dst,
2169 &action->encap.ipv6.ipv6_src,
2170 &action->encap.ipv6.ipv6_dst,
4b12e454
OG
2171 action->encap.tp_dst,
2172 action->encap.tos,
202469aa 2173 action->encap.ttl,
d9677a1f
EB
2174 action->encap.data,
2175 action->encap.no_csum);
292d5bd9 2176 nl_msg_put_act_flags(request);
0c70132c
CM
2177 nl_msg_end_nested(request, act_offset);
2178 }
2179 break;
2180 case TC_ACT_VLAN_POP: {
2181 act_offset = nl_msg_start_nested(request, act_index++);
2182 nl_msg_put_act_pop_vlan(request);
292d5bd9 2183 nl_msg_put_act_flags(request);
0c70132c
CM
2184 nl_msg_end_nested(request, act_offset);
2185 }
2186 break;
2187 case TC_ACT_VLAN_PUSH: {
2188 act_offset = nl_msg_start_nested(request, act_index++);
2189 nl_msg_put_act_push_vlan(request,
61e8655c 2190 action->vlan.vlan_push_tpid,
0c70132c
CM
2191 action->vlan.vlan_push_id,
2192 action->vlan.vlan_push_prio);
292d5bd9 2193 nl_msg_put_act_flags(request);
0c70132c
CM
2194 nl_msg_end_nested(request, act_offset);
2195 }
2196 break;
55412eac
JH
2197 case TC_ACT_MPLS_POP: {
2198 act_offset = nl_msg_start_nested(request, act_index++);
2199 nl_msg_put_act_pop_mpls(request, action->mpls.proto);
2200 nl_msg_end_nested(request, act_offset);
2201 }
2202 break;
283dcf85
JH
2203 case TC_ACT_MPLS_PUSH: {
2204 act_offset = nl_msg_start_nested(request, act_index++);
2205 nl_msg_put_act_push_mpls(request, action->mpls.proto,
2206 action->mpls.label, action->mpls.tc,
2207 action->mpls.ttl, action->mpls.bos);
2208 nl_msg_end_nested(request, act_offset);
2209 }
2210 break;
a8f005cf
JH
2211 case TC_ACT_MPLS_SET: {
2212 act_offset = nl_msg_start_nested(request, act_index++);
2213 nl_msg_put_act_set_mpls(request, action->mpls.label,
2214 action->mpls.tc, action->mpls.ttl,
2215 action->mpls.bos);
2216 nl_msg_end_nested(request, act_offset);
2217 }
2218 break;
0c70132c 2219 case TC_ACT_OUTPUT: {
4aa2dc04
JH
2220 ingress = action->out.ingress;
2221 ifindex = action->out.ifindex_out;
0c70132c
CM
2222 if (ifindex < 1) {
2223 VLOG_ERR_RL(&error_rl, "%s: invalid ifindex: %d, type: %d",
2224 __func__, ifindex, action->type);
2225 return EINVAL;
2226 }
4aa2dc04
JH
2227
2228 if (ingress) {
2229 /* If redirecting to ingress (internal port) ensure
2230 * pkt_type on skb is set to PACKET_HOST. */
2231 act_offset = nl_msg_start_nested(request, act_index++);
2232 nl_msg_put_act_skbedit_to_host(request);
2233 nl_msg_end_nested(request, act_offset);
2234 }
2235
0c70132c 2236 act_offset = nl_msg_start_nested(request, act_index++);
00a0a011 2237 if (i == flower->action_count - 1) {
4aa2dc04
JH
2238 if (ingress) {
2239 nl_msg_put_act_mirred(request, ifindex, TC_ACT_STOLEN,
2240 TCA_INGRESS_REDIR);
2241 } else {
2242 nl_msg_put_act_mirred(request, ifindex, TC_ACT_STOLEN,
2243 TCA_EGRESS_REDIR);
2244 }
00a0a011 2245 } else {
4aa2dc04
JH
2246 if (ingress) {
2247 nl_msg_put_act_mirred(request, ifindex, TC_ACT_PIPE,
2248 TCA_INGRESS_MIRROR);
2249 } else {
2250 nl_msg_put_act_mirred(request, ifindex, TC_ACT_PIPE,
2251 TCA_EGRESS_MIRROR);
2252 }
00a0a011 2253 }
0c70132c 2254 nl_msg_put_act_cookie(request, &flower->act_cookie);
292d5bd9 2255 nl_msg_put_act_flags(request);
0c70132c
CM
2256 nl_msg_end_nested(request, act_offset);
2257 }
2258 break;
a7ce5b85 2259 }
8ada482b 2260 }
0c70132c
CM
2261 }
2262 if (!ifindex) {
2263 act_offset = nl_msg_start_nested(request, act_index++);
2264 nl_msg_put_act_drop(request);
2265 nl_msg_put_act_cookie(request, &flower->act_cookie);
292d5bd9 2266 nl_msg_put_act_flags(request);
0c70132c 2267 nl_msg_end_nested(request, act_offset);
f98e418f
RD
2268 }
2269 nl_msg_end_nested(request, offset);
8ada482b
PB
2270
2271 return 0;
f98e418f
RD
2272}
2273
2274static void
2275nl_msg_put_masked_value(struct ofpbuf *request, uint16_t type,
2276 uint16_t mask_type, const void *data,
2277 const void *mask_data, size_t len)
2278{
2279 if (mask_type != TCA_FLOWER_UNSPEC) {
2280 if (is_all_zeros(mask_data, len)) {
2281 return;
2282 }
2283 nl_msg_put_unspec(request, mask_type, mask_data, len);
2284 }
2285 nl_msg_put_unspec(request, type, data, len);
2286}
2287
a468645c
PJV
2288static void
2289nl_msg_put_flower_tunnel_opts(struct ofpbuf *request, uint16_t type,
2290 struct tun_metadata metadata)
2291{
2292 struct geneve_opt *opt;
2293 size_t outer, inner;
2294 int len, cnt = 0;
2295
2296 len = metadata.present.len;
2297 if (!len) {
2298 return;
2299 }
2300
2301 outer = nl_msg_start_nested(request, type);
2302 while (len) {
2303 opt = &metadata.opts.gnv[cnt];
2304 inner = nl_msg_start_nested(request, TCA_FLOWER_KEY_ENC_OPTS_GENEVE);
2305
2306 nl_msg_put_be16(request, TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS,
2307 opt->opt_class);
2308 nl_msg_put_u8(request, TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE, opt->type);
2309 nl_msg_put_unspec(request, TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA, opt + 1,
2310 opt->length * 4);
2311
2312 cnt += sizeof(struct geneve_opt) / 4 + opt->length;
2313 len -= sizeof(struct geneve_opt) + opt->length * 4;
2314
2315 nl_msg_end_nested(request, inner);
2316 }
2317 nl_msg_end_nested(request, outer);
2318}
2319
f98e418f
RD
2320static void
2321nl_msg_put_flower_tunnel(struct ofpbuf *request, struct tc_flower *flower)
2322{
105e8179
OG
2323 ovs_be32 ipv4_src = flower->key.tunnel.ipv4.ipv4_src;
2324 ovs_be32 ipv4_dst = flower->key.tunnel.ipv4.ipv4_dst;
2325 struct in6_addr *ipv6_src = &flower->key.tunnel.ipv6.ipv6_src;
2326 struct in6_addr *ipv6_dst = &flower->key.tunnel.ipv6.ipv6_dst;
2327 ovs_be16 tp_dst = flower->key.tunnel.tp_dst;
2328 ovs_be32 id = be64_to_be32(flower->key.tunnel.id);
2329 uint8_t tos = flower->key.tunnel.tos;
2330 uint8_t ttl = flower->key.tunnel.ttl;
49a7961f
OG
2331 uint8_t tos_mask = flower->mask.tunnel.tos;
2332 uint8_t ttl_mask = flower->mask.tunnel.ttl;
0227bf09 2333 ovs_be64 id_mask = flower->mask.tunnel.id;
f98e418f 2334
f98e418f
RD
2335 if (ipv4_dst) {
2336 nl_msg_put_be32(request, TCA_FLOWER_KEY_ENC_IPV4_SRC, ipv4_src);
2337 nl_msg_put_be32(request, TCA_FLOWER_KEY_ENC_IPV4_DST, ipv4_dst);
2338 } else if (!is_all_zeros(ipv6_dst, sizeof *ipv6_dst)) {
2339 nl_msg_put_in6_addr(request, TCA_FLOWER_KEY_ENC_IPV6_SRC, ipv6_src);
2340 nl_msg_put_in6_addr(request, TCA_FLOWER_KEY_ENC_IPV6_DST, ipv6_dst);
2341 }
49a7961f 2342 if (tos_mask) {
dd83253e 2343 nl_msg_put_u8(request, TCA_FLOWER_KEY_ENC_IP_TOS, tos);
49a7961f 2344 nl_msg_put_u8(request, TCA_FLOWER_KEY_ENC_IP_TOS_MASK, tos_mask);
dd83253e 2345 }
49a7961f 2346 if (ttl_mask) {
dd83253e 2347 nl_msg_put_u8(request, TCA_FLOWER_KEY_ENC_IP_TTL, ttl);
49a7961f 2348 nl_msg_put_u8(request, TCA_FLOWER_KEY_ENC_IP_TTL_MASK, ttl_mask);
dd83253e 2349 }
e48f49e0
EB
2350 if (tp_dst) {
2351 nl_msg_put_be16(request, TCA_FLOWER_KEY_ENC_UDP_DST_PORT, tp_dst);
2352 }
0227bf09
AN
2353 if (id_mask) {
2354 nl_msg_put_be32(request, TCA_FLOWER_KEY_ENC_KEY_ID, id);
2355 }
a468645c
PJV
2356 nl_msg_put_flower_tunnel_opts(request, TCA_FLOWER_KEY_ENC_OPTS,
2357 flower->key.tunnel.metadata);
2358 nl_msg_put_flower_tunnel_opts(request, TCA_FLOWER_KEY_ENC_OPTS_MASK,
2359 flower->mask.tunnel.metadata);
f98e418f
RD
2360}
2361
bb170644
PB
2362#define FLOWER_PUT_MASKED_VALUE(member, type) \
2363 nl_msg_put_masked_value(request, type, type##_MASK, &flower->key.member, \
2364 &flower->mask.member, sizeof flower->key.member)
2365
8ada482b 2366static int
f98e418f
RD
2367nl_msg_put_flower_options(struct ofpbuf *request, struct tc_flower *flower)
2368{
8ada482b 2369
f98e418f 2370 uint16_t host_eth_type = ntohs(flower->key.eth_type);
b5ad40a9 2371 bool is_vlan = eth_type_vlan(flower->key.eth_type);
f9885dc5 2372 bool is_qinq = is_vlan && eth_type_vlan(flower->key.encap_eth_type[0]);
34b16955 2373 bool is_mpls = eth_type_mpls(flower->key.eth_type);
8ada482b
PB
2374 int err;
2375
d6118e62
PB
2376 /* need to parse acts first as some acts require changing the matching
2377 * see csum_update_flag() */
8ada482b
PB
2378 err = nl_msg_put_flower_acts(request, flower);
2379 if (err) {
2380 return err;
2381 }
f98e418f
RD
2382
2383 if (is_vlan) {
f9885dc5
JL
2384 if (is_qinq) {
2385 host_eth_type = ntohs(flower->key.encap_eth_type[1]);
2386 } else {
2387 host_eth_type = ntohs(flower->key.encap_eth_type[0]);
2388 }
f98e418f
RD
2389 }
2390
34b16955
PJV
2391 if (is_mpls) {
2392 host_eth_type = ntohs(flower->key.encap_eth_type[0]);
2393 }
2394
bb170644
PB
2395 FLOWER_PUT_MASKED_VALUE(dst_mac, TCA_FLOWER_KEY_ETH_DST);
2396 FLOWER_PUT_MASKED_VALUE(src_mac, TCA_FLOWER_KEY_ETH_SRC);
f98e418f
RD
2397
2398 if (host_eth_type == ETH_P_IP || host_eth_type == ETH_P_IPV6) {
b4496fc9 2399 FLOWER_PUT_MASKED_VALUE(ip_ttl, TCA_FLOWER_KEY_IP_TTL);
dfa2ccdb 2400 FLOWER_PUT_MASKED_VALUE(ip_tos, TCA_FLOWER_KEY_IP_TOS);
b4496fc9 2401
f98e418f
RD
2402 if (flower->mask.ip_proto && flower->key.ip_proto) {
2403 nl_msg_put_u8(request, TCA_FLOWER_KEY_IP_PROTO,
2404 flower->key.ip_proto);
2405 }
2406
83e86606 2407 if (flower->mask.flags) {
7e0f69b5 2408 nl_msg_put_be32(request, TCA_FLOWER_KEY_FLAGS,
83e86606 2409 htonl(flower->key.flags));
7e0f69b5 2410 nl_msg_put_be32(request, TCA_FLOWER_KEY_FLAGS_MASK,
83e86606
RD
2411 htonl(flower->mask.flags));
2412 }
2413
f98e418f 2414 if (flower->key.ip_proto == IPPROTO_UDP) {
2b1d9fa9
PB
2415 FLOWER_PUT_MASKED_VALUE(udp_src, TCA_FLOWER_KEY_UDP_SRC);
2416 FLOWER_PUT_MASKED_VALUE(udp_dst, TCA_FLOWER_KEY_UDP_DST);
f98e418f 2417 } else if (flower->key.ip_proto == IPPROTO_TCP) {
2b1d9fa9
PB
2418 FLOWER_PUT_MASKED_VALUE(tcp_src, TCA_FLOWER_KEY_TCP_SRC);
2419 FLOWER_PUT_MASKED_VALUE(tcp_dst, TCA_FLOWER_KEY_TCP_DST);
cd081043 2420 FLOWER_PUT_MASKED_VALUE(tcp_flags, TCA_FLOWER_KEY_TCP_FLAGS);
4862b4e5 2421 } else if (flower->key.ip_proto == IPPROTO_SCTP) {
2b1d9fa9
PB
2422 FLOWER_PUT_MASKED_VALUE(sctp_src, TCA_FLOWER_KEY_SCTP_SRC);
2423 FLOWER_PUT_MASKED_VALUE(sctp_dst, TCA_FLOWER_KEY_SCTP_DST);
f98e418f
RD
2424 }
2425 }
2426
2427 if (host_eth_type == ETH_P_IP) {
bb170644
PB
2428 FLOWER_PUT_MASKED_VALUE(ipv4.ipv4_src, TCA_FLOWER_KEY_IPV4_SRC);
2429 FLOWER_PUT_MASKED_VALUE(ipv4.ipv4_dst, TCA_FLOWER_KEY_IPV4_DST);
f98e418f 2430 } else if (host_eth_type == ETH_P_IPV6) {
bb170644
PB
2431 FLOWER_PUT_MASKED_VALUE(ipv6.ipv6_src, TCA_FLOWER_KEY_IPV6_SRC);
2432 FLOWER_PUT_MASKED_VALUE(ipv6.ipv6_dst, TCA_FLOWER_KEY_IPV6_DST);
f98e418f
RD
2433 }
2434
2435 nl_msg_put_be16(request, TCA_FLOWER_KEY_ETH_TYPE, flower->key.eth_type);
2436
34b16955
PJV
2437 if (is_mpls) {
2438 if (mpls_lse_to_ttl(flower->mask.mpls_lse)) {
2439 nl_msg_put_u8(request, TCA_FLOWER_KEY_MPLS_TTL,
2440 mpls_lse_to_ttl(flower->key.mpls_lse));
2441 }
2442 if (mpls_lse_to_tc(flower->mask.mpls_lse)) {
2443 nl_msg_put_u8(request, TCA_FLOWER_KEY_MPLS_TC,
2444 mpls_lse_to_tc(flower->key.mpls_lse));
2445 }
2446 if (mpls_lse_to_bos(flower->mask.mpls_lse)) {
2447 nl_msg_put_u8(request, TCA_FLOWER_KEY_MPLS_BOS,
2448 mpls_lse_to_bos(flower->key.mpls_lse));
2449 }
2450 if (mpls_lse_to_label(flower->mask.mpls_lse)) {
2451 nl_msg_put_u32(request, TCA_FLOWER_KEY_MPLS_LABEL,
2452 mpls_lse_to_label(flower->key.mpls_lse));
2453 }
2454 }
2455
f98e418f 2456 if (is_vlan) {
7f02f26c 2457 if (flower->mask.vlan_id[0]) {
f98e418f 2458 nl_msg_put_u16(request, TCA_FLOWER_KEY_VLAN_ID,
f9885dc5 2459 flower->key.vlan_id[0]);
7f02f26c
PJV
2460 }
2461 if (flower->mask.vlan_prio[0]) {
f98e418f 2462 nl_msg_put_u8(request, TCA_FLOWER_KEY_VLAN_PRIO,
f9885dc5 2463 flower->key.vlan_prio[0]);
f98e418f 2464 }
f9885dc5 2465 if (flower->key.encap_eth_type[0]) {
f98e418f 2466 nl_msg_put_be16(request, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
f9885dc5
JL
2467 flower->key.encap_eth_type[0]);
2468 }
2469
2470 if (is_qinq) {
7f02f26c 2471 if (flower->mask.vlan_id[1]) {
f9885dc5
JL
2472 nl_msg_put_u16(request, TCA_FLOWER_KEY_CVLAN_ID,
2473 flower->key.vlan_id[1]);
7f02f26c
PJV
2474 }
2475 if (flower->mask.vlan_prio[1]) {
f9885dc5
JL
2476 nl_msg_put_u8(request, TCA_FLOWER_KEY_CVLAN_PRIO,
2477 flower->key.vlan_prio[1]);
2478 }
2479 if (flower->key.encap_eth_type[1]) {
2480 nl_msg_put_be16(request, TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
2481 flower->key.encap_eth_type[1]);
2482 }
f98e418f
RD
2483 }
2484 }
2485
691d20cb 2486 nl_msg_put_u32(request, TCA_FLOWER_FLAGS, tc_get_tc_cls_policy(tc_policy));
f98e418f 2487
105e8179 2488 if (flower->tunnel) {
f98e418f
RD
2489 nl_msg_put_flower_tunnel(request, flower);
2490 }
2491
8ada482b 2492 return 0;
f98e418f
RD
2493}
2494
2495int
2496tc_replace_flower(int ifindex, uint16_t prio, uint32_t handle,
95255018
JH
2497 struct tc_flower *flower, uint32_t block_id,
2498 enum tc_qdisc_hook hook)
f98e418f
RD
2499{
2500 struct ofpbuf request;
2501 struct tcmsg *tcmsg;
2502 struct ofpbuf *reply;
2503 int error = 0;
2504 size_t basic_offset;
2505 uint16_t eth_type = (OVS_FORCE uint16_t) flower->key.eth_type;
093c9458 2506 int index;
f98e418f 2507
093c9458
JH
2508 index = block_id ? TCM_IFINDEX_MAGIC_BLOCK : ifindex;
2509 tcmsg = tc_make_request(index, RTM_NEWTFILTER, NLM_F_CREATE | NLM_F_ECHO,
2510 &request);
95255018
JH
2511 tcmsg->tcm_parent = (hook == TC_EGRESS) ?
2512 TC_EGRESS_PARENT : (block_id ? : TC_INGRESS_PARENT);
f98e418f
RD
2513 tcmsg->tcm_info = tc_make_handle(prio, eth_type);
2514 tcmsg->tcm_handle = handle;
2515
2516 nl_msg_put_string(&request, TCA_KIND, "flower");
2517 basic_offset = nl_msg_start_nested(&request, TCA_OPTIONS);
2518 {
8ada482b
PB
2519 error = nl_msg_put_flower_options(&request, flower);
2520
2521 if (error) {
2522 ofpbuf_uninit(&request);
2523 return error;
2524 }
f98e418f
RD
2525 }
2526 nl_msg_end_nested(&request, basic_offset);
2527
2528 error = tc_transact(&request, &reply);
2529 if (!error) {
2530 struct tcmsg *tc =
2531 ofpbuf_at_assert(reply, NLMSG_HDRLEN, sizeof *tc);
2532
2533 flower->prio = tc_get_major(tc->tcm_info);
2534 flower->handle = tc->tcm_handle;
2535 ofpbuf_delete(reply);
2536 }
2537
2538 return error;
2539}
691d20cb
PB
2540
2541void
2542tc_set_policy(const char *policy)
2543{
2544 if (!policy) {
2545 return;
2546 }
2547
2548 if (!strcmp(policy, "skip_sw")) {
2549 tc_policy = TC_POLICY_SKIP_SW;
2550 } else if (!strcmp(policy, "skip_hw")) {
2551 tc_policy = TC_POLICY_SKIP_HW;
2552 } else if (!strcmp(policy, "none")) {
2553 tc_policy = TC_POLICY_NONE;
2554 } else {
2555 VLOG_WARN("tc: Invalid policy '%s'", policy);
2556 return;
2557 }
2558
2559 VLOG_INFO("tc: Using policy '%s'", policy);
2560}