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