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