]> git.proxmox.com Git - mirror_ovs.git/blame - lib/tc.c
lib/tc: add ingress ratelimiting support for tc-offload
[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);
0227bf09 600 flower->mask.tunnel.id = OVS_BE64_MAX;
f98e418f
RD
601 }
602 if (attrs[TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK]) {
105e8179 603 flower->key.tunnel.ipv4.ipv4_src =
f98e418f
RD
604 nl_attr_get_be32(attrs[TCA_FLOWER_KEY_ENC_IPV4_SRC]);
605 }
606 if (attrs[TCA_FLOWER_KEY_ENC_IPV4_DST_MASK]) {
105e8179 607 flower->key.tunnel.ipv4.ipv4_dst =
f98e418f
RD
608 nl_attr_get_be32(attrs[TCA_FLOWER_KEY_ENC_IPV4_DST]);
609 }
610 if (attrs[TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK]) {
105e8179 611 flower->key.tunnel.ipv6.ipv6_src =
f98e418f
RD
612 nl_attr_get_in6_addr(attrs[TCA_FLOWER_KEY_ENC_IPV6_SRC]);
613 }
614 if (attrs[TCA_FLOWER_KEY_ENC_IPV6_DST_MASK]) {
105e8179 615 flower->key.tunnel.ipv6.ipv6_dst =
f98e418f
RD
616 nl_attr_get_in6_addr(attrs[TCA_FLOWER_KEY_ENC_IPV6_DST]);
617 }
618 if (attrs[TCA_FLOWER_KEY_ENC_UDP_DST_PORT]) {
105e8179 619 flower->key.tunnel.tp_dst =
f98e418f
RD
620 nl_attr_get_be16(attrs[TCA_FLOWER_KEY_ENC_UDP_DST_PORT]);
621 }
49a7961f 622 if (attrs[TCA_FLOWER_KEY_ENC_IP_TOS_MASK]) {
105e8179 623 flower->key.tunnel.tos =
dd83253e 624 nl_attr_get_u8(attrs[TCA_FLOWER_KEY_ENC_IP_TOS]);
49a7961f
OG
625 flower->mask.tunnel.tos =
626 nl_attr_get_u8(attrs[TCA_FLOWER_KEY_ENC_IP_TOS_MASK]);
dd83253e 627 }
49a7961f 628 if (attrs[TCA_FLOWER_KEY_ENC_IP_TTL_MASK]) {
105e8179 629 flower->key.tunnel.ttl =
dd83253e 630 nl_attr_get_u8(attrs[TCA_FLOWER_KEY_ENC_IP_TTL]);
49a7961f
OG
631 flower->mask.tunnel.ttl =
632 nl_attr_get_u8(attrs[TCA_FLOWER_KEY_ENC_IP_TTL_MASK]);
dd83253e 633 }
a468645c
PJV
634 if (attrs[TCA_FLOWER_KEY_ENC_OPTS] &&
635 attrs[TCA_FLOWER_KEY_ENC_OPTS_MASK]) {
636 err = nl_parse_flower_tunnel_opts(attrs[TCA_FLOWER_KEY_ENC_OPTS],
637 &flower->key.tunnel.metadata);
638 if (err) {
639 return err;
640 }
641
642 err = nl_parse_flower_tunnel_opts(attrs[TCA_FLOWER_KEY_ENC_OPTS_MASK],
643 &flower->mask.tunnel.metadata);
644 if (err) {
645 return err;
646 }
647
648 err = flower_tun_geneve_opt_check_len(&flower->key.tunnel.metadata,
649 &flower->mask.tunnel.metadata);
650 if (err) {
651 return err;
652 }
653 } else if (attrs[TCA_FLOWER_KEY_ENC_OPTS]) {
654 VLOG_ERR_RL(&error_rl,
655 "failed to parse tun options; no mask supplied");
656 return EINVAL;
657 } else if (attrs[TCA_FLOWER_KEY_ENC_OPTS_MASK]) {
658 VLOG_ERR_RL(&error_rl, "failed to parse tun options; no key supplied");
659 return EINVAL;
660 }
661
662 return 0;
f98e418f
RD
663}
664
665static void
666nl_parse_flower_ip(struct nlattr **attrs, struct tc_flower *flower) {
667 uint8_t ip_proto = 0;
668 struct tc_flower_key *key = &flower->key;
669 struct tc_flower_key *mask = &flower->mask;
670
671 if (attrs[TCA_FLOWER_KEY_IP_PROTO]) {
672 ip_proto = nl_attr_get_u8(attrs[TCA_FLOWER_KEY_IP_PROTO]);
673 key->ip_proto = ip_proto;
674 mask->ip_proto = UINT8_MAX;
675 }
676
83e86606 677 if (attrs[TCA_FLOWER_KEY_FLAGS_MASK]) {
7e0f69b5
IS
678 key->flags = ntohl(nl_attr_get_be32(attrs[TCA_FLOWER_KEY_FLAGS]));
679 mask->flags =
680 ntohl(nl_attr_get_be32(attrs[TCA_FLOWER_KEY_FLAGS_MASK]));
83e86606
RD
681 }
682
f98e418f
RD
683 if (attrs[TCA_FLOWER_KEY_IPV4_SRC_MASK]) {
684 key->ipv4.ipv4_src =
685 nl_attr_get_be32(attrs[TCA_FLOWER_KEY_IPV4_SRC]);
686 mask->ipv4.ipv4_src =
687 nl_attr_get_be32(attrs[TCA_FLOWER_KEY_IPV4_SRC_MASK]);
688 }
689 if (attrs[TCA_FLOWER_KEY_IPV4_DST_MASK]) {
690 key->ipv4.ipv4_dst =
691 nl_attr_get_be32(attrs[TCA_FLOWER_KEY_IPV4_DST]);
692 mask->ipv4.ipv4_dst =
693 nl_attr_get_be32(attrs[TCA_FLOWER_KEY_IPV4_DST_MASK]);
694 }
695 if (attrs[TCA_FLOWER_KEY_IPV6_SRC_MASK]) {
696 struct nlattr *attr = attrs[TCA_FLOWER_KEY_IPV6_SRC];
697 struct nlattr *attr_mask = attrs[TCA_FLOWER_KEY_IPV6_SRC_MASK];
698
699 key->ipv6.ipv6_src = nl_attr_get_in6_addr(attr);
700 mask->ipv6.ipv6_src = nl_attr_get_in6_addr(attr_mask);
701 }
702 if (attrs[TCA_FLOWER_KEY_IPV6_DST_MASK]) {
703 struct nlattr *attr = attrs[TCA_FLOWER_KEY_IPV6_DST];
704 struct nlattr *attr_mask = attrs[TCA_FLOWER_KEY_IPV6_DST_MASK];
705
706 key->ipv6.ipv6_dst = nl_attr_get_in6_addr(attr);
707 mask->ipv6.ipv6_dst = nl_attr_get_in6_addr(attr_mask);
708 }
709
710 if (ip_proto == IPPROTO_TCP) {
711 if (attrs[TCA_FLOWER_KEY_TCP_SRC_MASK]) {
2b1d9fa9 712 key->tcp_src =
f98e418f 713 nl_attr_get_be16(attrs[TCA_FLOWER_KEY_TCP_SRC]);
2b1d9fa9 714 mask->tcp_src =
f98e418f
RD
715 nl_attr_get_be16(attrs[TCA_FLOWER_KEY_TCP_SRC_MASK]);
716 }
717 if (attrs[TCA_FLOWER_KEY_TCP_DST_MASK]) {
2b1d9fa9 718 key->tcp_dst =
f98e418f 719 nl_attr_get_be16(attrs[TCA_FLOWER_KEY_TCP_DST]);
2b1d9fa9 720 mask->tcp_dst =
f98e418f
RD
721 nl_attr_get_be16(attrs[TCA_FLOWER_KEY_TCP_DST_MASK]);
722 }
cd081043
PB
723 if (attrs[TCA_FLOWER_KEY_TCP_FLAGS_MASK]) {
724 key->tcp_flags =
725 nl_attr_get_be16(attrs[TCA_FLOWER_KEY_TCP_FLAGS]);
726 mask->tcp_flags =
727 nl_attr_get_be16(attrs[TCA_FLOWER_KEY_TCP_FLAGS_MASK]);
728 }
f98e418f
RD
729 } else if (ip_proto == IPPROTO_UDP) {
730 if (attrs[TCA_FLOWER_KEY_UDP_SRC_MASK]) {
2b1d9fa9
PB
731 key->udp_src = nl_attr_get_be16(attrs[TCA_FLOWER_KEY_UDP_SRC]);
732 mask->udp_src =
f98e418f
RD
733 nl_attr_get_be16(attrs[TCA_FLOWER_KEY_UDP_SRC_MASK]);
734 }
735 if (attrs[TCA_FLOWER_KEY_UDP_DST_MASK]) {
2b1d9fa9
PB
736 key->udp_dst = nl_attr_get_be16(attrs[TCA_FLOWER_KEY_UDP_DST]);
737 mask->udp_dst =
f98e418f
RD
738 nl_attr_get_be16(attrs[TCA_FLOWER_KEY_UDP_DST_MASK]);
739 }
4862b4e5
VB
740 } else if (ip_proto == IPPROTO_SCTP) {
741 if (attrs[TCA_FLOWER_KEY_SCTP_SRC_MASK]) {
2b1d9fa9
PB
742 key->sctp_src = nl_attr_get_be16(attrs[TCA_FLOWER_KEY_SCTP_SRC]);
743 mask->sctp_src =
4862b4e5
VB
744 nl_attr_get_be16(attrs[TCA_FLOWER_KEY_SCTP_SRC_MASK]);
745 }
746 if (attrs[TCA_FLOWER_KEY_SCTP_DST_MASK]) {
2b1d9fa9
PB
747 key->sctp_dst = nl_attr_get_be16(attrs[TCA_FLOWER_KEY_SCTP_DST]);
748 mask->sctp_dst =
4862b4e5
VB
749 nl_attr_get_be16(attrs[TCA_FLOWER_KEY_SCTP_DST_MASK]);
750 }
f98e418f 751 }
0b4b5203
PB
752
753 if (attrs[TCA_FLOWER_KEY_IP_TTL_MASK]) {
754 key->ip_ttl = nl_attr_get_u8(attrs[TCA_FLOWER_KEY_IP_TTL]);
755 mask->ip_ttl = nl_attr_get_u8(attrs[TCA_FLOWER_KEY_IP_TTL_MASK]);
756 }
dfa2ccdb
OG
757
758 if (attrs[TCA_FLOWER_KEY_IP_TOS_MASK]) {
759 key->ip_tos = nl_attr_get_u8(attrs[TCA_FLOWER_KEY_IP_TOS]);
760 mask->ip_tos = nl_attr_get_u8(attrs[TCA_FLOWER_KEY_IP_TOS_MASK]);
761 }
f98e418f
RD
762}
763
d63ca532
GT
764static enum tc_offloaded_state
765nl_get_flower_offloaded_state(struct nlattr **attrs)
766{
767 uint32_t flower_flags = 0;
768
769 if (attrs[TCA_FLOWER_FLAGS]) {
770 flower_flags = nl_attr_get_u32(attrs[TCA_FLOWER_FLAGS]);
771 if (flower_flags & TCA_CLS_FLAGS_NOT_IN_HW) {
772 return TC_OFFLOADED_STATE_NOT_IN_HW;
773 } else if (flower_flags & TCA_CLS_FLAGS_IN_HW) {
774 return TC_OFFLOADED_STATE_IN_HW;
775 }
776 }
777 return TC_OFFLOADED_STATE_UNDEFINED;
778}
779
780static void
781nl_parse_flower_flags(struct nlattr **attrs, struct tc_flower *flower)
782{
783 flower->offloaded_state = nl_get_flower_offloaded_state(attrs);
784}
785
8ada482b
PB
786static const struct nl_policy pedit_policy[] = {
787 [TCA_PEDIT_PARMS_EX] = { .type = NL_A_UNSPEC,
788 .min_len = sizeof(struct tc_pedit),
789 .optional = false, },
790 [TCA_PEDIT_KEYS_EX] = { .type = NL_A_NESTED,
791 .optional = false, },
792};
793
794static int
795nl_parse_act_pedit(struct nlattr *options, struct tc_flower *flower)
796{
0c70132c 797 struct tc_action *action;
8ada482b
PB
798 struct nlattr *pe_attrs[ARRAY_SIZE(pedit_policy)];
799 const struct tc_pedit *pe;
800 const struct tc_pedit_key *keys;
801 const struct nlattr *nla, *keys_ex, *ex_type;
802 const void *keys_attr;
803 char *rewrite_key = (void *) &flower->rewrite.key;
804 char *rewrite_mask = (void *) &flower->rewrite.mask;
805 size_t keys_ex_size, left;
d6118e62 806 int type, i = 0, err;
8ada482b
PB
807
808 if (!nl_parse_nested(options, pedit_policy, pe_attrs,
809 ARRAY_SIZE(pedit_policy))) {
810 VLOG_ERR_RL(&error_rl, "failed to parse pedit action options");
811 return EPROTO;
812 }
813
814 pe = nl_attr_get_unspec(pe_attrs[TCA_PEDIT_PARMS_EX], sizeof *pe);
815 keys = pe->keys;
816 keys_attr = pe_attrs[TCA_PEDIT_KEYS_EX];
817 keys_ex = nl_attr_get(keys_attr);
818 keys_ex_size = nl_attr_get_size(keys_attr);
819
820 NL_ATTR_FOR_EACH (nla, left, keys_ex, keys_ex_size) {
821 if (i >= pe->nkeys) {
822 break;
823 }
824
408671c4 825 if (nl_attr_type(nla) != TCA_PEDIT_KEY_EX) {
8ada482b
PB
826 VLOG_ERR_RL(&error_rl, "unable to parse legacy pedit type: %d",
827 nl_attr_type(nla));
828 return EOPNOTSUPP;
829 }
830
831 ex_type = nl_attr_find_nested(nla, TCA_PEDIT_KEY_EX_HTYPE);
832 type = nl_attr_get_u16(ex_type);
833
d6118e62
PB
834 err = csum_update_flag(flower, type);
835 if (err) {
836 return err;
837 }
838
8ada482b
PB
839 for (int j = 0; j < ARRAY_SIZE(flower_pedit_map); j++) {
840 struct flower_key_to_pedit *m = &flower_pedit_map[j];
841 int flower_off = m->flower_offset;
842 int sz = m->size;
843 int mf = m->offset;
844
845 if (m->htype != type) {
846 continue;
847 }
848
849 /* check overlap between current pedit key, which is always
850 * 4 bytes (range [off, off + 3]), and a map entry in
851 * flower_pedit_map (range [mf, mf + sz - 1]) */
852 if ((keys->off >= mf && keys->off < mf + sz)
853 || (keys->off + 3 >= mf && keys->off + 3 < mf + sz)) {
854 int diff = flower_off + (keys->off - mf);
f8b63e59
PJV
855 ovs_be32 *dst = (void *) (rewrite_key + diff);
856 ovs_be32 *dst_m = (void *) (rewrite_mask + diff);
dbcb014d 857 ovs_be32 mask, mask_word, data_word;
8ada482b
PB
858 uint32_t zero_bits;
859
dbcb014d
PJV
860 mask_word = htonl(ntohl(keys->mask) << m->boundary_shift);
861 data_word = htonl(ntohl(keys->val) << m->boundary_shift);
862 mask = ~(mask_word);
863
8ada482b
PB
864 if (keys->off < mf) {
865 zero_bits = 8 * (mf - keys->off);
f8b63e59 866 mask &= htonl(UINT32_MAX >> zero_bits);
8ada482b
PB
867 } else if (keys->off + 4 > mf + m->size) {
868 zero_bits = 8 * (keys->off + 4 - mf - m->size);
f8b63e59 869 mask &= htonl(UINT32_MAX << zero_bits);
8ada482b
PB
870 }
871
872 *dst_m |= mask;
dbcb014d 873 *dst |= data_word & mask;
8ada482b
PB
874 }
875 }
876
877 keys++;
878 i++;
879 }
880
0c70132c
CM
881 action = &flower->actions[flower->action_count++];
882 action->type = TC_ACT_PEDIT;
8ada482b
PB
883
884 return 0;
885}
886
f98e418f
RD
887static const struct nl_policy tunnel_key_policy[] = {
888 [TCA_TUNNEL_KEY_PARMS] = { .type = NL_A_UNSPEC,
889 .min_len = sizeof(struct tc_tunnel_key),
890 .optional = false, },
891 [TCA_TUNNEL_KEY_ENC_IPV4_SRC] = { .type = NL_A_U32, .optional = true, },
892 [TCA_TUNNEL_KEY_ENC_IPV4_DST] = { .type = NL_A_U32, .optional = true, },
893 [TCA_TUNNEL_KEY_ENC_IPV6_SRC] = { .type = NL_A_UNSPEC,
894 .min_len = sizeof(struct in6_addr),
895 .optional = true, },
896 [TCA_TUNNEL_KEY_ENC_IPV6_DST] = { .type = NL_A_UNSPEC,
897 .min_len = sizeof(struct in6_addr),
898 .optional = true, },
899 [TCA_TUNNEL_KEY_ENC_KEY_ID] = { .type = NL_A_U32, .optional = true, },
900 [TCA_TUNNEL_KEY_ENC_DST_PORT] = { .type = NL_A_U16, .optional = true, },
4b12e454
OG
901 [TCA_TUNNEL_KEY_ENC_TOS] = { .type = NL_A_U8, .optional = true, },
902 [TCA_TUNNEL_KEY_ENC_TTL] = { .type = NL_A_U8, .optional = true, },
202469aa 903 [TCA_TUNNEL_KEY_ENC_OPTS] = { .type = NL_A_NESTED, .optional = true, },
d9677a1f 904 [TCA_TUNNEL_KEY_NO_CSUM] = { .type = NL_A_U8, .optional = true, },
f98e418f
RD
905};
906
202469aa
PJV
907static int
908nl_parse_act_geneve_opts(const struct nlattr *in_nlattr,
909 struct tc_action *action)
910{
911 struct geneve_opt *opt = NULL;
912 const struct ofpbuf *msg;
913 uint16_t last_opt_type;
914 struct nlattr *nla;
915 struct ofpbuf buf;
916 size_t left;
917 int cnt;
918
919 nl_attr_get_nested(in_nlattr, &buf);
920 msg = &buf;
921
922 last_opt_type = TCA_TUNNEL_KEY_ENC_OPT_GENEVE_UNSPEC;
923 cnt = 0;
924 NL_ATTR_FOR_EACH (nla, left, ofpbuf_at(msg, 0, 0), msg->size) {
925 uint16_t type = nl_attr_type(nla);
926
927 switch (type) {
928 case TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS:
929 if (cnt && last_opt_type != TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA) {
930 VLOG_ERR_RL(&error_rl,
931 "failed to parse action geneve options class");
932 return EINVAL;
933 }
934
935 opt = &action->encap.data.opts.gnv[cnt];
936 opt->opt_class = nl_attr_get_be16(nla);
937 cnt += sizeof(struct geneve_opt) / 4;
938 action->encap.data.present.len += sizeof(struct geneve_opt);
939 last_opt_type = TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS;
940 break;
941 case TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE:
942 if (last_opt_type != TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS) {
943 VLOG_ERR_RL(&error_rl,
944 "failed to parse action geneve options type");
945 return EINVAL;
946 }
947
948 opt->type = nl_attr_get_u8(nla);
949 last_opt_type = TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE;
950 break;
951 case TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA:
952 if (last_opt_type != TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE) {
953 VLOG_ERR_RL(&error_rl,
954 "failed to parse action geneve options data");
955 return EINVAL;
956 }
957
958 opt->length = nl_attr_get_size(nla) / 4;
959 memcpy(opt + 1, nl_attr_get_unspec(nla, 1), opt->length * 4);
960 cnt += opt->length;
961 action->encap.data.present.len += opt->length * 4;
962 last_opt_type = TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA;
963 break;
964 }
965 }
966
967 if (last_opt_type != TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA) {
968 VLOG_ERR_RL(&error_rl,
969 "failed to parse action geneve options without data");
970 return EINVAL;
971 }
972
973 return 0;
974}
975
976static int
977nl_parse_act_tunnel_opts(struct nlattr *options, struct tc_action *action)
978{
979 const struct ofpbuf *msg;
980 struct nlattr *nla;
981 struct ofpbuf buf;
982 size_t left;
983 int err;
984
985 if (!options) {
986 return 0;
987 }
988
989 nl_attr_get_nested(options, &buf);
990 msg = &buf;
991
992 NL_ATTR_FOR_EACH (nla, left, ofpbuf_at(msg, 0, 0), msg->size) {
993 uint16_t type = nl_attr_type(nla);
994 switch (type) {
995 case TCA_TUNNEL_KEY_ENC_OPTS_GENEVE:
996 err = nl_parse_act_geneve_opts(nla, action);
997 if (err) {
998 return err;
999 }
1000
1001 break;
1002 }
1003 }
1004
1005 return 0;
1006}
1007
f98e418f
RD
1008static int
1009nl_parse_act_tunnel_key(struct nlattr *options, struct tc_flower *flower)
1010{
1011 struct nlattr *tun_attrs[ARRAY_SIZE(tunnel_key_policy)];
1012 const struct nlattr *tun_parms;
1013 const struct tc_tunnel_key *tun;
0c70132c 1014 struct tc_action *action;
202469aa 1015 int err;
f98e418f
RD
1016
1017 if (!nl_parse_nested(options, tunnel_key_policy, tun_attrs,
1018 ARRAY_SIZE(tunnel_key_policy))) {
1019 VLOG_ERR_RL(&error_rl, "failed to parse tunnel_key action options");
1020 return EPROTO;
1021 }
1022
1023 tun_parms = tun_attrs[TCA_TUNNEL_KEY_PARMS];
1024 tun = nl_attr_get_unspec(tun_parms, sizeof *tun);
1025 if (tun->t_action == TCA_TUNNEL_KEY_ACT_SET) {
1026 struct nlattr *id = tun_attrs[TCA_TUNNEL_KEY_ENC_KEY_ID];
1027 struct nlattr *dst_port = tun_attrs[TCA_TUNNEL_KEY_ENC_DST_PORT];
1028 struct nlattr *ipv4_src = tun_attrs[TCA_TUNNEL_KEY_ENC_IPV4_SRC];
1029 struct nlattr *ipv4_dst = tun_attrs[TCA_TUNNEL_KEY_ENC_IPV4_DST];
1030 struct nlattr *ipv6_src = tun_attrs[TCA_TUNNEL_KEY_ENC_IPV6_SRC];
1031 struct nlattr *ipv6_dst = tun_attrs[TCA_TUNNEL_KEY_ENC_IPV6_DST];
4b12e454
OG
1032 struct nlattr *tos = tun_attrs[TCA_TUNNEL_KEY_ENC_TOS];
1033 struct nlattr *ttl = tun_attrs[TCA_TUNNEL_KEY_ENC_TTL];
202469aa 1034 struct nlattr *tun_opt = tun_attrs[TCA_TUNNEL_KEY_ENC_OPTS];
d9677a1f 1035 struct nlattr *no_csum = tun_attrs[TCA_TUNNEL_KEY_NO_CSUM];
f98e418f 1036
0c70132c
CM
1037 action = &flower->actions[flower->action_count++];
1038 action->type = TC_ACT_ENCAP;
1039 action->encap.ipv4.ipv4_src = ipv4_src ? nl_attr_get_be32(ipv4_src) : 0;
1040 action->encap.ipv4.ipv4_dst = ipv4_dst ? nl_attr_get_be32(ipv4_dst) : 0;
f98e418f 1041 if (ipv6_src) {
0c70132c 1042 action->encap.ipv6.ipv6_src = nl_attr_get_in6_addr(ipv6_src);
f98e418f
RD
1043 }
1044 if (ipv6_dst) {
0c70132c 1045 action->encap.ipv6.ipv6_dst = nl_attr_get_in6_addr(ipv6_dst);
f98e418f 1046 }
0c70132c 1047 action->encap.id = id ? be32_to_be64(nl_attr_get_be32(id)) : 0;
0227bf09 1048 action->encap.id_present = id ? true : false;
0c70132c 1049 action->encap.tp_dst = dst_port ? nl_attr_get_be16(dst_port) : 0;
4b12e454
OG
1050 action->encap.tos = tos ? nl_attr_get_u8(tos) : 0;
1051 action->encap.ttl = ttl ? nl_attr_get_u8(ttl) : 0;
d9677a1f 1052 action->encap.no_csum = no_csum ? nl_attr_get_u8(no_csum) : 0;
202469aa
PJV
1053
1054 err = nl_parse_act_tunnel_opts(tun_opt, action);
1055 if (err) {
1056 return err;
1057 }
f98e418f 1058 } else if (tun->t_action == TCA_TUNNEL_KEY_ACT_RELEASE) {
105e8179 1059 flower->tunnel = true;
f98e418f
RD
1060 } else {
1061 VLOG_ERR_RL(&error_rl, "unknown tunnel actions: %d, %d",
1062 tun->action, tun->t_action);
1063 return EINVAL;
1064 }
1065 return 0;
1066}
1067
1068static const struct nl_policy gact_policy[] = {
1069 [TCA_GACT_PARMS] = { .type = NL_A_UNSPEC,
1070 .min_len = sizeof(struct tc_gact),
1071 .optional = false, },
1072 [TCA_GACT_TM] = { .type = NL_A_UNSPEC,
1073 .min_len = sizeof(struct tcf_t),
1074 .optional = false, },
1075};
1076
8c1e74d1
PB
1077static int
1078get_user_hz(void)
1079{
1080 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
1081 static int user_hz = 100;
1082
1083 if (ovsthread_once_start(&once)) {
1084 user_hz = sysconf(_SC_CLK_TCK);
1085 ovsthread_once_done(&once);
1086 }
1087
1088 return user_hz;
1089}
f98e418f
RD
1090
1091static void
1092nl_parse_tcf(const struct tcf_t *tm, struct tc_flower *flower)
1093{
8c1e74d1 1094 flower->lastused = time_msec() - (tm->lastuse * 1000 / get_user_hz());
f98e418f
RD
1095}
1096
1097static int
1098nl_parse_act_drop(struct nlattr *options, struct tc_flower *flower)
1099{
1100 struct nlattr *gact_attrs[ARRAY_SIZE(gact_policy)];
1101 const struct tc_gact *p;
1102 struct nlattr *gact_parms;
1103 const struct tcf_t *tm;
1104
1105 if (!nl_parse_nested(options, gact_policy, gact_attrs,
1106 ARRAY_SIZE(gact_policy))) {
1107 VLOG_ERR_RL(&error_rl, "failed to parse gact action options");
1108 return EPROTO;
1109 }
1110
1111 gact_parms = gact_attrs[TCA_GACT_PARMS];
1112 p = nl_attr_get_unspec(gact_parms, sizeof *p);
1113
1114 if (p->action != TC_ACT_SHOT) {
1115 VLOG_ERR_RL(&error_rl, "unknown gact action: %d", p->action);
1116 return EINVAL;
1117 }
1118
1119 tm = nl_attr_get_unspec(gact_attrs[TCA_GACT_TM], sizeof *tm);
1120 nl_parse_tcf(tm, flower);
1121
1122 return 0;
1123}
1124
1125static const struct nl_policy mirred_policy[] = {
1126 [TCA_MIRRED_PARMS] = { .type = NL_A_UNSPEC,
1127 .min_len = sizeof(struct tc_mirred),
1128 .optional = false, },
1129 [TCA_MIRRED_TM] = { .type = NL_A_UNSPEC,
1130 .min_len = sizeof(struct tcf_t),
1131 .optional = false, },
1132};
1133
1134static int
1135nl_parse_act_mirred(struct nlattr *options, struct tc_flower *flower)
1136{
1137
1138 struct nlattr *mirred_attrs[ARRAY_SIZE(mirred_policy)];
1139 const struct tc_mirred *m;
1140 const struct nlattr *mirred_parms;
1141 const struct tcf_t *tm;
1142 struct nlattr *mirred_tm;
0c70132c 1143 struct tc_action *action;
f98e418f
RD
1144
1145 if (!nl_parse_nested(options, mirred_policy, mirred_attrs,
1146 ARRAY_SIZE(mirred_policy))) {
1147 VLOG_ERR_RL(&error_rl, "failed to parse mirred action options");
1148 return EPROTO;
1149 }
1150
1151 mirred_parms = mirred_attrs[TCA_MIRRED_PARMS];
1152 m = nl_attr_get_unspec(mirred_parms, sizeof *m);
1153
0c70132c 1154 if (m->eaction != TCA_EGRESS_REDIR && m->eaction != TCA_EGRESS_MIRROR) {
f98e418f 1155 VLOG_ERR_RL(&error_rl, "unknown mirred action: %d, %d, %d",
0c70132c 1156 m->action, m->eaction, m->ifindex);
f98e418f
RD
1157 return EINVAL;
1158 }
1159
0c70132c
CM
1160 action = &flower->actions[flower->action_count++];
1161 action->ifindex_out = m->ifindex;
1162 action->type = TC_ACT_OUTPUT;
f98e418f
RD
1163
1164 mirred_tm = mirred_attrs[TCA_MIRRED_TM];
1165 tm = nl_attr_get_unspec(mirred_tm, sizeof *tm);
1166 nl_parse_tcf(tm, flower);
1167
1168 return 0;
1169}
1170
1171static const struct nl_policy vlan_policy[] = {
1172 [TCA_VLAN_PARMS] = { .type = NL_A_UNSPEC,
1173 .min_len = sizeof(struct tc_vlan),
1174 .optional = false, },
1175 [TCA_VLAN_PUSH_VLAN_ID] = { .type = NL_A_U16, .optional = true, },
1176 [TCA_VLAN_PUSH_VLAN_PROTOCOL] = { .type = NL_A_U16, .optional = true, },
1177 [TCA_VLAN_PUSH_VLAN_PRIORITY] = { .type = NL_A_U8, .optional = true, },
1178};
1179
1180static int
1181nl_parse_act_vlan(struct nlattr *options, struct tc_flower *flower)
1182{
1183 struct nlattr *vlan_attrs[ARRAY_SIZE(vlan_policy)];
1184 const struct tc_vlan *v;
1185 const struct nlattr *vlan_parms;
0c70132c 1186 struct tc_action *action;
f98e418f
RD
1187
1188 if (!nl_parse_nested(options, vlan_policy, vlan_attrs,
1189 ARRAY_SIZE(vlan_policy))) {
1190 VLOG_ERR_RL(&error_rl, "failed to parse vlan action options");
1191 return EPROTO;
1192 }
1193
0c70132c 1194 action = &flower->actions[flower->action_count++];
f98e418f
RD
1195 vlan_parms = vlan_attrs[TCA_VLAN_PARMS];
1196 v = nl_attr_get_unspec(vlan_parms, sizeof *v);
1197 if (v->v_action == TCA_VLAN_ACT_PUSH) {
61e8655c 1198 struct nlattr *vlan_tpid = vlan_attrs[TCA_VLAN_PUSH_VLAN_PROTOCOL];
f98e418f
RD
1199 struct nlattr *vlan_id = vlan_attrs[TCA_VLAN_PUSH_VLAN_ID];
1200 struct nlattr *vlan_prio = vlan_attrs[TCA_VLAN_PUSH_VLAN_PRIORITY];
1201
10097f3f 1202 action->vlan.vlan_push_tpid = nl_attr_get_be16(vlan_tpid);
0c70132c
CM
1203 action->vlan.vlan_push_id = nl_attr_get_u16(vlan_id);
1204 action->vlan.vlan_push_prio = vlan_prio ? nl_attr_get_u8(vlan_prio) : 0;
1205 action->type = TC_ACT_VLAN_PUSH;
f98e418f 1206 } else if (v->v_action == TCA_VLAN_ACT_POP) {
0c70132c 1207 action->type = TC_ACT_VLAN_POP;
f98e418f
RD
1208 } else {
1209 VLOG_ERR_RL(&error_rl, "unknown vlan action: %d, %d",
1210 v->action, v->v_action);
1211 return EINVAL;
1212 }
1213 return 0;
1214}
1215
d6118e62
PB
1216static const struct nl_policy csum_policy[] = {
1217 [TCA_CSUM_PARMS] = { .type = NL_A_UNSPEC,
1218 .min_len = sizeof(struct tc_csum),
1219 .optional = false, },
1220};
1221
1222static int
1223nl_parse_act_csum(struct nlattr *options, struct tc_flower *flower)
1224{
1225 struct nlattr *csum_attrs[ARRAY_SIZE(csum_policy)];
1226 const struct tc_csum *c;
1227 const struct nlattr *csum_parms;
1228
1229 if (!nl_parse_nested(options, csum_policy, csum_attrs,
1230 ARRAY_SIZE(csum_policy))) {
1231 VLOG_ERR_RL(&error_rl, "failed to parse csum action options");
1232 return EPROTO;
1233 }
1234
1235 csum_parms = csum_attrs[TCA_CSUM_PARMS];
1236 c = nl_attr_get_unspec(csum_parms, sizeof *c);
1237
1238 /* sanity checks */
1239 if (c->update_flags != flower->csum_update_flags) {
1240 VLOG_WARN_RL(&error_rl,
1241 "expected different act csum flags: 0x%x != 0x%x",
1242 flower->csum_update_flags, c->update_flags);
1243 return EINVAL;
1244 }
1245 flower->csum_update_flags = 0; /* so we know csum was handled */
1246
1247 if (flower->needs_full_ip_proto_mask
1248 && flower->mask.ip_proto != UINT8_MAX) {
1249 VLOG_WARN_RL(&error_rl, "expected full matching on flower ip_proto");
1250 return EINVAL;
1251 }
1252
1253 return 0;
1254}
1255
f98e418f
RD
1256static const struct nl_policy act_policy[] = {
1257 [TCA_ACT_KIND] = { .type = NL_A_STRING, .optional = false, },
1258 [TCA_ACT_COOKIE] = { .type = NL_A_UNSPEC, .optional = true, },
1259 [TCA_ACT_OPTIONS] = { .type = NL_A_NESTED, .optional = false, },
1260 [TCA_ACT_STATS] = { .type = NL_A_NESTED, .optional = false, },
1261};
1262
1263static const struct nl_policy stats_policy[] = {
1264 [TCA_STATS_BASIC] = { .type = NL_A_UNSPEC,
1265 .min_len = sizeof(struct gnet_stats_basic),
1266 .optional = false, },
1267};
1268
1269static int
1270nl_parse_single_action(struct nlattr *action, struct tc_flower *flower)
1271{
1272 struct nlattr *act_options;
1273 struct nlattr *act_stats;
1274 struct nlattr *act_cookie;
1275 const char *act_kind;
1276 struct nlattr *action_attrs[ARRAY_SIZE(act_policy)];
1277 struct nlattr *stats_attrs[ARRAY_SIZE(stats_policy)];
1278 struct ovs_flow_stats *stats = &flower->stats;
1279 const struct gnet_stats_basic *bs;
40c5aa11 1280 int err = 0;
f98e418f
RD
1281
1282 if (!nl_parse_nested(action, act_policy, action_attrs,
1283 ARRAY_SIZE(act_policy))) {
1284 VLOG_ERR_RL(&error_rl, "failed to parse single action options");
1285 return EPROTO;
1286 }
1287
1288 act_kind = nl_attr_get_string(action_attrs[TCA_ACT_KIND]);
1289 act_options = action_attrs[TCA_ACT_OPTIONS];
1290 act_cookie = action_attrs[TCA_ACT_COOKIE];
1291
1292 if (!strcmp(act_kind, "gact")) {
40c5aa11 1293 err = nl_parse_act_drop(act_options, flower);
f98e418f 1294 } else if (!strcmp(act_kind, "mirred")) {
40c5aa11 1295 err = nl_parse_act_mirred(act_options, flower);
f98e418f 1296 } else if (!strcmp(act_kind, "vlan")) {
40c5aa11 1297 err = nl_parse_act_vlan(act_options, flower);
f98e418f 1298 } else if (!strcmp(act_kind, "tunnel_key")) {
40c5aa11 1299 err = nl_parse_act_tunnel_key(act_options, flower);
8ada482b 1300 } else if (!strcmp(act_kind, "pedit")) {
40c5aa11 1301 err = nl_parse_act_pedit(act_options, flower);
8ada482b 1302 } else if (!strcmp(act_kind, "csum")) {
d6118e62 1303 nl_parse_act_csum(act_options, flower);
f98e418f
RD
1304 } else {
1305 VLOG_ERR_RL(&error_rl, "unknown tc action kind: %s", act_kind);
40c5aa11
RD
1306 err = EINVAL;
1307 }
1308
1309 if (err) {
1310 return err;
f98e418f
RD
1311 }
1312
1313 if (act_cookie) {
1314 flower->act_cookie.data = nl_attr_get(act_cookie);
1315 flower->act_cookie.len = nl_attr_get_size(act_cookie);
1316 }
1317
1318 act_stats = action_attrs[TCA_ACT_STATS];
1319
1320 if (!nl_parse_nested(act_stats, stats_policy, stats_attrs,
1321 ARRAY_SIZE(stats_policy))) {
1322 VLOG_ERR_RL(&error_rl, "failed to parse action stats policy");
1323 return EPROTO;
1324 }
1325
1326 bs = nl_attr_get_unspec(stats_attrs[TCA_STATS_BASIC], sizeof *bs);
1327 put_32aligned_u64(&stats->n_packets, bs->packets);
1328 put_32aligned_u64(&stats->n_bytes, bs->bytes);
1329
1330 return 0;
1331}
1332
1333#define TCA_ACT_MIN_PRIO 1
1334
1335static int
1336nl_parse_flower_actions(struct nlattr **attrs, struct tc_flower *flower)
1337{
1338 const struct nlattr *actions = attrs[TCA_FLOWER_ACT];
1339 static struct nl_policy actions_orders_policy[TCA_ACT_MAX_PRIO + 1] = {};
1340 struct nlattr *actions_orders[ARRAY_SIZE(actions_orders_policy)];
1341 const int max_size = ARRAY_SIZE(actions_orders_policy);
1342
1343 for (int i = TCA_ACT_MIN_PRIO; i < max_size; i++) {
1344 actions_orders_policy[i].type = NL_A_NESTED;
1345 actions_orders_policy[i].optional = true;
1346 }
1347
1348 if (!nl_parse_nested(actions, actions_orders_policy, actions_orders,
1349 ARRAY_SIZE(actions_orders_policy))) {
1350 VLOG_ERR_RL(&error_rl, "failed to parse flower order of actions");
1351 return EPROTO;
1352 }
1353
1354 for (int i = TCA_ACT_MIN_PRIO; i < max_size; i++) {
1355 if (actions_orders[i]) {
0c70132c
CM
1356 int err;
1357
1358 if (flower->action_count >= TCA_ACT_MAX_PRIO) {
1359 VLOG_DBG_RL(&error_rl, "Can only support %d actions", flower->action_count);
1360 return EOPNOTSUPP;
1361 }
1362 err = nl_parse_single_action(actions_orders[i], flower);
f98e418f
RD
1363
1364 if (err) {
1365 return err;
1366 }
1367 }
1368 }
1369
d6118e62
PB
1370 if (flower->csum_update_flags) {
1371 VLOG_WARN_RL(&error_rl,
1372 "expected act csum with flags: 0x%x",
1373 flower->csum_update_flags);
1374 return EINVAL;
1375 }
1376
f98e418f
RD
1377 return 0;
1378}
1379
1380static int
1381nl_parse_flower_options(struct nlattr *nl_options, struct tc_flower *flower)
1382{
1383 struct nlattr *attrs[ARRAY_SIZE(tca_flower_policy)];
a468645c 1384 int err;
f98e418f
RD
1385
1386 if (!nl_parse_nested(nl_options, tca_flower_policy,
1387 attrs, ARRAY_SIZE(tca_flower_policy))) {
1388 VLOG_ERR_RL(&error_rl, "failed to parse flower classifier options");
1389 return EPROTO;
1390 }
1391
1392 nl_parse_flower_eth(attrs, flower);
34b16955 1393 nl_parse_flower_mpls(attrs, flower);
f98e418f
RD
1394 nl_parse_flower_vlan(attrs, flower);
1395 nl_parse_flower_ip(attrs, flower);
a468645c
PJV
1396 err = nl_parse_flower_tunnel(attrs, flower);
1397 if (err) {
1398 return err;
1399 }
1400
d63ca532 1401 nl_parse_flower_flags(attrs, flower);
f98e418f
RD
1402 return nl_parse_flower_actions(attrs, flower);
1403}
1404
1405int
1406parse_netlink_to_tc_flower(struct ofpbuf *reply, struct tc_flower *flower)
1407{
1408 struct tcmsg *tc;
1409 struct nlattr *ta[ARRAY_SIZE(tca_policy)];
1410 const char *kind;
1411
1412 if (NLMSG_HDRLEN + sizeof *tc > reply->size) {
1413 return EPROTO;
1414 }
1415
1416 memset(flower, 0, sizeof *flower);
1417
1418 tc = ofpbuf_at_assert(reply, NLMSG_HDRLEN, sizeof *tc);
1419 flower->handle = tc->tcm_handle;
1420 flower->key.eth_type = (OVS_FORCE ovs_be16) tc_get_minor(tc->tcm_info);
1421 flower->mask.eth_type = OVS_BE16_MAX;
1422 flower->prio = tc_get_major(tc->tcm_info);
1423
e7f6ba22
PJV
1424 if (flower->prio == TC_RESERVED_PRIORITY_POLICE) {
1425 return 0;
1426 }
1427
f98e418f
RD
1428 if (!flower->handle) {
1429 return EAGAIN;
1430 }
1431
1432 if (!nl_policy_parse(reply, NLMSG_HDRLEN + sizeof *tc,
1433 tca_policy, ta, ARRAY_SIZE(ta))) {
1434 VLOG_ERR_RL(&error_rl, "failed to parse tca policy");
1435 return EPROTO;
1436 }
1437
1438 kind = nl_attr_get_string(ta[TCA_KIND]);
1439 if (strcmp(kind, "flower")) {
763e120d 1440 VLOG_DBG_ONCE("Unsupported filter: %s", kind);
f98e418f
RD
1441 return EPROTO;
1442 }
1443
1444 return nl_parse_flower_options(ta[TCA_OPTIONS], flower);
1445}
1446
1447int
093c9458 1448tc_dump_flower_start(int ifindex, struct nl_dump *dump, uint32_t block_id)
f98e418f
RD
1449{
1450 struct ofpbuf request;
1451 struct tcmsg *tcmsg;
093c9458 1452 int index;
f98e418f 1453
093c9458
JH
1454 index = block_id ? TCM_IFINDEX_MAGIC_BLOCK : ifindex;
1455 tcmsg = tc_make_request(index, RTM_GETTFILTER, NLM_F_DUMP, &request);
1456 tcmsg->tcm_parent = block_id ? : TC_INGRESS_PARENT;
f98e418f
RD
1457 tcmsg->tcm_info = TC_H_UNSPEC;
1458 tcmsg->tcm_handle = 0;
1459
1460 nl_dump_start(dump, NETLINK_ROUTE, &request);
1461 ofpbuf_uninit(&request);
1462
1463 return 0;
1464}
1465
1466int
093c9458 1467tc_flush(int ifindex, uint32_t block_id)
f98e418f
RD
1468{
1469 struct ofpbuf request;
1470 struct tcmsg *tcmsg;
093c9458 1471 int index;
f98e418f 1472
093c9458
JH
1473 index = block_id ? TCM_IFINDEX_MAGIC_BLOCK : ifindex;
1474 tcmsg = tc_make_request(index, RTM_DELTFILTER, NLM_F_ACK, &request);
1475 tcmsg->tcm_parent = block_id ? : TC_INGRESS_PARENT;
f98e418f
RD
1476 tcmsg->tcm_info = TC_H_UNSPEC;
1477
1478 return tc_transact(&request, NULL);
1479}
1480
1481int
093c9458 1482tc_del_filter(int ifindex, int prio, int handle, uint32_t block_id)
f98e418f
RD
1483{
1484 struct ofpbuf request;
1485 struct tcmsg *tcmsg;
1486 struct ofpbuf *reply;
1487 int error;
093c9458 1488 int index;
f98e418f 1489
093c9458
JH
1490 index = block_id ? TCM_IFINDEX_MAGIC_BLOCK : ifindex;
1491 tcmsg = tc_make_request(index, RTM_DELTFILTER, NLM_F_ECHO, &request);
1492 tcmsg->tcm_parent = block_id ? : TC_INGRESS_PARENT;
f98e418f
RD
1493 tcmsg->tcm_info = tc_make_handle(prio, 0);
1494 tcmsg->tcm_handle = handle;
1495
1496 error = tc_transact(&request, &reply);
1497 if (!error) {
1498 ofpbuf_delete(reply);
1499 }
1500 return error;
1501}
1502
1503int
093c9458
JH
1504tc_get_flower(int ifindex, int prio, int handle, struct tc_flower *flower,
1505 uint32_t block_id)
f98e418f
RD
1506{
1507 struct ofpbuf request;
1508 struct tcmsg *tcmsg;
1509 struct ofpbuf *reply;
1510 int error;
093c9458 1511 int index;
f98e418f 1512
093c9458
JH
1513 index = block_id ? TCM_IFINDEX_MAGIC_BLOCK : ifindex;
1514 tcmsg = tc_make_request(index, RTM_GETTFILTER, NLM_F_ECHO, &request);
1515 tcmsg->tcm_parent = block_id ? : TC_INGRESS_PARENT;
f98e418f
RD
1516 tcmsg->tcm_info = tc_make_handle(prio, 0);
1517 tcmsg->tcm_handle = handle;
1518
1519 error = tc_transact(&request, &reply);
1520 if (error) {
1521 return error;
1522 }
1523
1524 error = parse_netlink_to_tc_flower(reply, flower);
1525 ofpbuf_delete(reply);
1526 return error;
1527}
1528
691d20cb
PB
1529static int
1530tc_get_tc_cls_policy(enum tc_offload_policy policy)
1531{
1532 if (policy == TC_POLICY_SKIP_HW) {
1533 return TCA_CLS_FLAGS_SKIP_HW;
1534 } else if (policy == TC_POLICY_SKIP_SW) {
1535 return TCA_CLS_FLAGS_SKIP_SW;
1536 }
1537
1538 return 0;
1539}
1540
8ada482b
PB
1541static void
1542nl_msg_put_act_csum(struct ofpbuf *request, uint32_t flags)
1543{
1544 size_t offset;
1545
1546 nl_msg_put_string(request, TCA_ACT_KIND, "csum");
1547 offset = nl_msg_start_nested(request, TCA_ACT_OPTIONS);
1548 {
1549 struct tc_csum parm = { .action = TC_ACT_PIPE,
1550 .update_flags = flags };
1551
1552 nl_msg_put_unspec(request, TCA_CSUM_PARMS, &parm, sizeof parm);
1553 }
1554 nl_msg_end_nested(request, offset);
1555}
1556
1557static void
1558nl_msg_put_act_pedit(struct ofpbuf *request, struct tc_pedit *parm,
1559 struct tc_pedit_key_ex *ex)
1560{
e13bbbab 1561 size_t ksize = sizeof *parm + parm->nkeys * sizeof(struct tc_pedit_key);
8ada482b
PB
1562 size_t offset, offset_keys_ex, offset_key;
1563 int i;
1564
1565 nl_msg_put_string(request, TCA_ACT_KIND, "pedit");
1566 offset = nl_msg_start_nested(request, TCA_ACT_OPTIONS);
1567 {
1568 parm->action = TC_ACT_PIPE;
1569
1570 nl_msg_put_unspec(request, TCA_PEDIT_PARMS_EX, parm, ksize);
1571 offset_keys_ex = nl_msg_start_nested(request, TCA_PEDIT_KEYS_EX);
1572 for (i = 0; i < parm->nkeys; i++, ex++) {
1573 offset_key = nl_msg_start_nested(request, TCA_PEDIT_KEY_EX);
1574 nl_msg_put_u16(request, TCA_PEDIT_KEY_EX_HTYPE, ex->htype);
1575 nl_msg_put_u16(request, TCA_PEDIT_KEY_EX_CMD, ex->cmd);
1576 nl_msg_end_nested(request, offset_key);
1577 }
1578 nl_msg_end_nested(request, offset_keys_ex);
1579 }
1580 nl_msg_end_nested(request, offset);
1581}
1582
f98e418f 1583static void
10097f3f 1584nl_msg_put_act_push_vlan(struct ofpbuf *request, ovs_be16 tpid,
61e8655c 1585 uint16_t vid, uint8_t prio)
f98e418f
RD
1586{
1587 size_t offset;
1588
1589 nl_msg_put_string(request, TCA_ACT_KIND, "vlan");
1590 offset = nl_msg_start_nested(request, TCA_ACT_OPTIONS);
1591 {
1592 struct tc_vlan parm = { .action = TC_ACT_PIPE,
1593 .v_action = TCA_VLAN_ACT_PUSH };
1594
1595 nl_msg_put_unspec(request, TCA_VLAN_PARMS, &parm, sizeof parm);
10097f3f 1596 nl_msg_put_be16(request, TCA_VLAN_PUSH_VLAN_PROTOCOL, tpid);
f98e418f
RD
1597 nl_msg_put_u16(request, TCA_VLAN_PUSH_VLAN_ID, vid);
1598 nl_msg_put_u8(request, TCA_VLAN_PUSH_VLAN_PRIORITY, prio);
1599 }
1600 nl_msg_end_nested(request, offset);
1601}
1602
1603static void
1604nl_msg_put_act_pop_vlan(struct ofpbuf *request)
1605{
1606 size_t offset;
1607
1608 nl_msg_put_string(request, TCA_ACT_KIND, "vlan");
1609 offset = nl_msg_start_nested(request, TCA_ACT_OPTIONS);
1610 {
1611 struct tc_vlan parm = { .action = TC_ACT_PIPE,
1612 .v_action = TCA_VLAN_ACT_POP };
1613
1614 nl_msg_put_unspec(request, TCA_VLAN_PARMS, &parm, sizeof parm);
1615 }
1616 nl_msg_end_nested(request, offset);
1617}
1618
1619static void
1620nl_msg_put_act_tunnel_key_release(struct ofpbuf *request)
1621{
1622 size_t offset;
1623
1624 nl_msg_put_string(request, TCA_ACT_KIND, "tunnel_key");
1625 offset = nl_msg_start_nested(request, TCA_ACT_OPTIONS);
1626 {
1627 struct tc_tunnel_key tun = { .action = TC_ACT_PIPE,
1628 .t_action = TCA_TUNNEL_KEY_ACT_RELEASE };
1629
1630 nl_msg_put_unspec(request, TCA_TUNNEL_KEY_PARMS, &tun, sizeof tun);
1631 }
1632 nl_msg_end_nested(request, offset);
1633}
1634
202469aa
PJV
1635static void
1636nl_msg_put_act_tunnel_geneve_option(struct ofpbuf *request,
1637 struct tun_metadata tun_metadata)
1638{
1639 const struct geneve_opt *opt;
1640 size_t outer, inner;
1641 int len, cnt = 0;
1642
1643 len = tun_metadata.present.len;
1644 if (!len) {
1645 return;
1646 }
1647
1648 outer = nl_msg_start_nested(request, TCA_TUNNEL_KEY_ENC_OPTS);
1649
1650 while (len) {
1651 opt = &tun_metadata.opts.gnv[cnt];
1652 inner = nl_msg_start_nested(request, TCA_TUNNEL_KEY_ENC_OPTS_GENEVE);
1653
1654 nl_msg_put_be16(request, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS,
1655 opt->opt_class);
1656 nl_msg_put_u8(request, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE, opt->type);
1657 nl_msg_put_unspec(request, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA, opt + 1,
1658 opt->length * 4);
1659
1660 cnt += sizeof(struct geneve_opt) / 4 + opt->length;
1661 len -= sizeof(struct geneve_opt) + opt->length * 4;
1662
1663 nl_msg_end_nested(request, inner);
1664 }
1665
1666 nl_msg_end_nested(request, outer);
1667}
1668
f98e418f 1669static void
0227bf09
AN
1670nl_msg_put_act_tunnel_key_set(struct ofpbuf *request, bool id_present,
1671 ovs_be64 id, ovs_be32 ipv4_src,
1672 ovs_be32 ipv4_dst, struct in6_addr *ipv6_src,
202469aa
PJV
1673 struct in6_addr *ipv6_dst,
1674 ovs_be16 tp_dst, uint8_t tos, uint8_t ttl,
d9677a1f
EB
1675 struct tun_metadata tun_metadata,
1676 uint8_t no_csum)
f98e418f
RD
1677{
1678 size_t offset;
1679
1680 nl_msg_put_string(request, TCA_ACT_KIND, "tunnel_key");
1681 offset = nl_msg_start_nested(request, TCA_ACT_OPTIONS);
1682 {
1683 struct tc_tunnel_key tun = { .action = TC_ACT_PIPE,
1684 .t_action = TCA_TUNNEL_KEY_ACT_SET };
1685
1686 nl_msg_put_unspec(request, TCA_TUNNEL_KEY_PARMS, &tun, sizeof tun);
1687
1688 ovs_be32 id32 = be64_to_be32(id);
0227bf09
AN
1689 if (id_present) {
1690 nl_msg_put_be32(request, TCA_TUNNEL_KEY_ENC_KEY_ID, id32);
1691 }
f98e418f
RD
1692 if (ipv4_dst) {
1693 nl_msg_put_be32(request, TCA_TUNNEL_KEY_ENC_IPV4_SRC, ipv4_src);
1694 nl_msg_put_be32(request, TCA_TUNNEL_KEY_ENC_IPV4_DST, ipv4_dst);
1695 } else if (!is_all_zeros(ipv6_dst, sizeof *ipv6_dst)) {
1696 nl_msg_put_in6_addr(request, TCA_TUNNEL_KEY_ENC_IPV6_DST,
1697 ipv6_dst);
1698 nl_msg_put_in6_addr(request, TCA_TUNNEL_KEY_ENC_IPV6_SRC,
1699 ipv6_src);
1700 }
4b12e454
OG
1701 if (tos) {
1702 nl_msg_put_u8(request, TCA_TUNNEL_KEY_ENC_TOS, tos);
1703 }
1704 if (ttl) {
1705 nl_msg_put_u8(request, TCA_TUNNEL_KEY_ENC_TTL, ttl);
1706 }
e48f49e0
EB
1707 if (tp_dst) {
1708 nl_msg_put_be16(request, TCA_TUNNEL_KEY_ENC_DST_PORT, tp_dst);
1709 }
202469aa 1710 nl_msg_put_act_tunnel_geneve_option(request, tun_metadata);
d9677a1f 1711 nl_msg_put_u8(request, TCA_TUNNEL_KEY_NO_CSUM, no_csum);
f98e418f
RD
1712 }
1713 nl_msg_end_nested(request, offset);
1714}
1715
1716static void
1717nl_msg_put_act_drop(struct ofpbuf *request)
1718{
1719 size_t offset;
1720
1721 nl_msg_put_string(request, TCA_ACT_KIND, "gact");
1722 offset = nl_msg_start_nested(request, TCA_ACT_OPTIONS);
1723 {
1724 struct tc_gact p = { .action = TC_ACT_SHOT };
1725
1726 nl_msg_put_unspec(request, TCA_GACT_PARMS, &p, sizeof p);
1727 }
1728 nl_msg_end_nested(request, offset);
1729}
1730
1731static void
00a0a011
CM
1732nl_msg_put_act_mirred(struct ofpbuf *request, int ifindex, int action,
1733 int eaction)
f98e418f
RD
1734{
1735 size_t offset;
1736
1737 nl_msg_put_string(request, TCA_ACT_KIND, "mirred");
1738 offset = nl_msg_start_nested(request, TCA_ACT_OPTIONS);
1739 {
00a0a011
CM
1740 struct tc_mirred m = { .action = action,
1741 .eaction = eaction,
f98e418f
RD
1742 .ifindex = ifindex };
1743
1744 nl_msg_put_unspec(request, TCA_MIRRED_PARMS, &m, sizeof m);
1745 }
1746 nl_msg_end_nested(request, offset);
1747}
1748
1749static inline void
1750nl_msg_put_act_cookie(struct ofpbuf *request, struct tc_cookie *ck) {
1751 if (ck->len) {
1752 nl_msg_put_unspec(request, TCA_ACT_COOKIE, ck->data, ck->len);
1753 }
1754}
1755
8ada482b
PB
1756/* Given flower, a key_to_pedit map entry, calculates the rest,
1757 * where:
1758 *
1759 * mask, data - pointers of where read the first word of flower->key/mask.
1760 * current_offset - which offset to use for the first pedit action.
1761 * cnt - max pedits actions to use.
1762 * first_word_mask/last_word_mask - the mask to use for the first/last read
1763 * (as we read entire words). */
f98e418f 1764static void
8ada482b 1765calc_offsets(struct tc_flower *flower, struct flower_key_to_pedit *m,
f8b63e59
PJV
1766 int *cur_offset, int *cnt, ovs_be32 *last_word_mask,
1767 ovs_be32 *first_word_mask, ovs_be32 **mask, ovs_be32 **data)
8ada482b
PB
1768{
1769 int start_offset, max_offset, total_size;
1770 int diff, right_zero_bits, left_zero_bits;
1771 char *rewrite_key = (void *) &flower->rewrite.key;
1772 char *rewrite_mask = (void *) &flower->rewrite.mask;
1773
1774 max_offset = m->offset + m->size;
1775 start_offset = ROUND_DOWN(m->offset, 4);
1776 diff = m->offset - start_offset;
1777 total_size = max_offset - start_offset;
0d9f0cd4 1778 right_zero_bits = 8 * (4 - ((max_offset % 4) ? : 4));
8ada482b
PB
1779 left_zero_bits = 8 * (m->offset - start_offset);
1780
1781 *cur_offset = start_offset;
1782 *cnt = (total_size / 4) + (total_size % 4 ? 1 : 0);
f8b63e59
PJV
1783 *last_word_mask = htonl(UINT32_MAX << right_zero_bits);
1784 *first_word_mask = htonl(UINT32_MAX >> left_zero_bits);
8ada482b
PB
1785 *data = (void *) (rewrite_key + m->flower_offset - diff);
1786 *mask = (void *) (rewrite_mask + m->flower_offset - diff);
1787}
1788
d6118e62 1789static inline int
8ada482b
PB
1790csum_update_flag(struct tc_flower *flower,
1791 enum pedit_header_type htype) {
d6118e62
PB
1792 /* Explictily specifiy the csum flags so HW can return EOPNOTSUPP
1793 * if it doesn't support a checksum recalculation of some headers.
1794 * And since OVS allows a flow such as
1795 * eth(dst=<mac>),eth_type(0x0800) actions=set(ipv4(src=<new_ip>))
1796 * we need to force a more specific flow as this can, for example,
1797 * need a recalculation of icmp checksum if the packet that passes
d5ac6458 1798 * is ICMPv6 and tcp checksum if its tcp. */
d6118e62
PB
1799
1800 switch (htype) {
1801 case TCA_PEDIT_KEY_EX_HDR_TYPE_IP4:
8ada482b 1802 flower->csum_update_flags |= TCA_CSUM_UPDATE_FLAG_IPV4HDR;
eeb0ca88 1803 /* Fall through. */
d6118e62
PB
1804 case TCA_PEDIT_KEY_EX_HDR_TYPE_IP6:
1805 case TCA_PEDIT_KEY_EX_HDR_TYPE_TCP:
1806 case TCA_PEDIT_KEY_EX_HDR_TYPE_UDP:
8ada482b 1807 if (flower->key.ip_proto == IPPROTO_TCP) {
d6118e62 1808 flower->needs_full_ip_proto_mask = true;
8ada482b
PB
1809 flower->csum_update_flags |= TCA_CSUM_UPDATE_FLAG_TCP;
1810 } else if (flower->key.ip_proto == IPPROTO_UDP) {
d6118e62 1811 flower->needs_full_ip_proto_mask = true;
8ada482b 1812 flower->csum_update_flags |= TCA_CSUM_UPDATE_FLAG_UDP;
d5ac6458
JL
1813 } else if (flower->key.ip_proto == IPPROTO_ICMP) {
1814 flower->needs_full_ip_proto_mask = true;
1815 } else if (flower->key.ip_proto == IPPROTO_ICMPV6) {
d6118e62 1816 flower->needs_full_ip_proto_mask = true;
8ada482b 1817 flower->csum_update_flags |= TCA_CSUM_UPDATE_FLAG_ICMP;
d6118e62
PB
1818 } else {
1819 VLOG_WARN_RL(&error_rl,
1820 "can't offload rewrite of IP/IPV6 with ip_proto: %d",
1821 flower->key.ip_proto);
1822 break;
8ada482b 1823 }
eeb0ca88 1824 /* Fall through. */
d6118e62
PB
1825 case TCA_PEDIT_KEY_EX_HDR_TYPE_ETH:
1826 return 0; /* success */
1827
1828 case TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK:
1829 case __PEDIT_HDR_TYPE_MAX:
1830 default:
1831 break;
8ada482b 1832 }
d6118e62
PB
1833
1834 return EOPNOTSUPP;
8ada482b
PB
1835}
1836
1837static int
1838nl_msg_put_flower_rewrite_pedits(struct ofpbuf *request,
1839 struct tc_flower *flower)
1840{
1841 struct {
1842 struct tc_pedit sel;
1843 struct tc_pedit_key keys[MAX_PEDIT_OFFSETS];
1844 struct tc_pedit_key_ex keys_ex[MAX_PEDIT_OFFSETS];
1845 } sel = {
1846 .sel = {
1847 .nkeys = 0
1848 }
1849 };
d6118e62 1850 int i, j, err;
8ada482b
PB
1851
1852 for (i = 0; i < ARRAY_SIZE(flower_pedit_map); i++) {
1853 struct flower_key_to_pedit *m = &flower_pedit_map[i];
1854 struct tc_pedit_key *pedit_key = NULL;
1855 struct tc_pedit_key_ex *pedit_key_ex = NULL;
f8b63e59 1856 ovs_be32 *mask, *data, first_word_mask, last_word_mask;
8ada482b
PB
1857 int cnt = 0, cur_offset = 0;
1858
1859 if (!m->size) {
1860 continue;
1861 }
1862
1863 calc_offsets(flower, m, &cur_offset, &cnt, &last_word_mask,
1864 &first_word_mask, &mask, &data);
1865
1866 for (j = 0; j < cnt; j++, mask++, data++, cur_offset += 4) {
f8b63e59 1867 ovs_be32 mask_word = *mask;
dbcb014d 1868 ovs_be32 data_word = *data;
8ada482b
PB
1869
1870 if (j == 0) {
1871 mask_word &= first_word_mask;
1872 }
1873 if (j == cnt - 1) {
1874 mask_word &= last_word_mask;
1875 }
1876 if (!mask_word) {
1877 continue;
1878 }
1879 if (sel.sel.nkeys == MAX_PEDIT_OFFSETS) {
1880 VLOG_WARN_RL(&error_rl, "reached too many pedit offsets: %d",
1881 MAX_PEDIT_OFFSETS);
1882 return EOPNOTSUPP;
1883 }
1884
1885 pedit_key = &sel.keys[sel.sel.nkeys];
1886 pedit_key_ex = &sel.keys_ex[sel.sel.nkeys];
1887 pedit_key_ex->cmd = TCA_PEDIT_KEY_EX_CMD_SET;
1888 pedit_key_ex->htype = m->htype;
1889 pedit_key->off = cur_offset;
dbcb014d
PJV
1890 mask_word = htonl(ntohl(mask_word) >> m->boundary_shift);
1891 data_word = htonl(ntohl(data_word) >> m->boundary_shift);
8ada482b 1892 pedit_key->mask = ~mask_word;
dbcb014d 1893 pedit_key->val = data_word & mask_word;
8ada482b 1894 sel.sel.nkeys++;
d6118e62
PB
1895
1896 err = csum_update_flag(flower, m->htype);
1897 if (err) {
1898 return err;
1899 }
1900
1901 if (flower->needs_full_ip_proto_mask) {
1902 flower->mask.ip_proto = UINT8_MAX;
1903 }
8ada482b
PB
1904 }
1905 }
1906 nl_msg_put_act_pedit(request, &sel.sel, sel.keys_ex);
1907
1908 return 0;
1909}
1910
1911static int
f98e418f
RD
1912nl_msg_put_flower_acts(struct ofpbuf *request, struct tc_flower *flower)
1913{
1914 size_t offset;
1915 size_t act_offset;
0c70132c
CM
1916 uint16_t act_index = 1;
1917 struct tc_action *action;
1918 int i, ifindex = 0;
f98e418f
RD
1919
1920 offset = nl_msg_start_nested(request, TCA_FLOWER_ACT);
1921 {
8ada482b
PB
1922 int error;
1923
105e8179 1924 if (flower->tunnel) {
8ada482b 1925 act_offset = nl_msg_start_nested(request, act_index++);
0c70132c 1926 nl_msg_put_act_tunnel_key_release(request);
8ada482b 1927 nl_msg_end_nested(request, act_offset);
0c70132c 1928 }
f98e418f 1929
0c70132c
CM
1930 action = flower->actions;
1931 for (i = 0; i < flower->action_count; i++, action++) {
1932 switch (action->type) {
1933 case TC_ACT_PEDIT: {
a7ce5b85 1934 act_offset = nl_msg_start_nested(request, act_index++);
0c70132c
CM
1935 error = nl_msg_put_flower_rewrite_pedits(request, flower);
1936 if (error) {
1937 return error;
1938 }
a7ce5b85 1939 nl_msg_end_nested(request, act_offset);
0c70132c
CM
1940
1941 if (flower->csum_update_flags) {
1942 act_offset = nl_msg_start_nested(request, act_index++);
1943 nl_msg_put_act_csum(request, flower->csum_update_flags);
1944 nl_msg_end_nested(request, act_offset);
1945 }
1946 }
1947 break;
1948 case TC_ACT_ENCAP: {
1949 act_offset = nl_msg_start_nested(request, act_index++);
0227bf09
AN
1950 nl_msg_put_act_tunnel_key_set(request, action->encap.id_present,
1951 action->encap.id,
0c70132c
CM
1952 action->encap.ipv4.ipv4_src,
1953 action->encap.ipv4.ipv4_dst,
1954 &action->encap.ipv6.ipv6_src,
1955 &action->encap.ipv6.ipv6_dst,
4b12e454
OG
1956 action->encap.tp_dst,
1957 action->encap.tos,
202469aa 1958 action->encap.ttl,
d9677a1f
EB
1959 action->encap.data,
1960 action->encap.no_csum);
0c70132c
CM
1961 nl_msg_end_nested(request, act_offset);
1962 }
1963 break;
1964 case TC_ACT_VLAN_POP: {
1965 act_offset = nl_msg_start_nested(request, act_index++);
1966 nl_msg_put_act_pop_vlan(request);
1967 nl_msg_end_nested(request, act_offset);
1968 }
1969 break;
1970 case TC_ACT_VLAN_PUSH: {
1971 act_offset = nl_msg_start_nested(request, act_index++);
1972 nl_msg_put_act_push_vlan(request,
61e8655c 1973 action->vlan.vlan_push_tpid,
0c70132c
CM
1974 action->vlan.vlan_push_id,
1975 action->vlan.vlan_push_prio);
1976 nl_msg_end_nested(request, act_offset);
1977 }
1978 break;
1979 case TC_ACT_OUTPUT: {
1980 ifindex = action->ifindex_out;
1981 if (ifindex < 1) {
1982 VLOG_ERR_RL(&error_rl, "%s: invalid ifindex: %d, type: %d",
1983 __func__, ifindex, action->type);
1984 return EINVAL;
1985 }
1986 act_offset = nl_msg_start_nested(request, act_index++);
00a0a011
CM
1987 if (i == flower->action_count - 1) {
1988 nl_msg_put_act_mirred(request, ifindex, TC_ACT_STOLEN,
1989 TCA_EGRESS_REDIR);
1990 } else {
1991 nl_msg_put_act_mirred(request, ifindex, TC_ACT_PIPE,
1992 TCA_EGRESS_MIRROR);
1993 }
0c70132c
CM
1994 nl_msg_put_act_cookie(request, &flower->act_cookie);
1995 nl_msg_end_nested(request, act_offset);
1996 }
1997 break;
a7ce5b85 1998 }
8ada482b 1999 }
0c70132c
CM
2000 }
2001 if (!ifindex) {
2002 act_offset = nl_msg_start_nested(request, act_index++);
2003 nl_msg_put_act_drop(request);
2004 nl_msg_put_act_cookie(request, &flower->act_cookie);
2005 nl_msg_end_nested(request, act_offset);
f98e418f
RD
2006 }
2007 nl_msg_end_nested(request, offset);
8ada482b
PB
2008
2009 return 0;
f98e418f
RD
2010}
2011
2012static void
2013nl_msg_put_masked_value(struct ofpbuf *request, uint16_t type,
2014 uint16_t mask_type, const void *data,
2015 const void *mask_data, size_t len)
2016{
2017 if (mask_type != TCA_FLOWER_UNSPEC) {
2018 if (is_all_zeros(mask_data, len)) {
2019 return;
2020 }
2021 nl_msg_put_unspec(request, mask_type, mask_data, len);
2022 }
2023 nl_msg_put_unspec(request, type, data, len);
2024}
2025
a468645c
PJV
2026static void
2027nl_msg_put_flower_tunnel_opts(struct ofpbuf *request, uint16_t type,
2028 struct tun_metadata metadata)
2029{
2030 struct geneve_opt *opt;
2031 size_t outer, inner;
2032 int len, cnt = 0;
2033
2034 len = metadata.present.len;
2035 if (!len) {
2036 return;
2037 }
2038
2039 outer = nl_msg_start_nested(request, type);
2040 while (len) {
2041 opt = &metadata.opts.gnv[cnt];
2042 inner = nl_msg_start_nested(request, TCA_FLOWER_KEY_ENC_OPTS_GENEVE);
2043
2044 nl_msg_put_be16(request, TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS,
2045 opt->opt_class);
2046 nl_msg_put_u8(request, TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE, opt->type);
2047 nl_msg_put_unspec(request, TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA, opt + 1,
2048 opt->length * 4);
2049
2050 cnt += sizeof(struct geneve_opt) / 4 + opt->length;
2051 len -= sizeof(struct geneve_opt) + opt->length * 4;
2052
2053 nl_msg_end_nested(request, inner);
2054 }
2055 nl_msg_end_nested(request, outer);
2056}
2057
f98e418f
RD
2058static void
2059nl_msg_put_flower_tunnel(struct ofpbuf *request, struct tc_flower *flower)
2060{
105e8179
OG
2061 ovs_be32 ipv4_src = flower->key.tunnel.ipv4.ipv4_src;
2062 ovs_be32 ipv4_dst = flower->key.tunnel.ipv4.ipv4_dst;
2063 struct in6_addr *ipv6_src = &flower->key.tunnel.ipv6.ipv6_src;
2064 struct in6_addr *ipv6_dst = &flower->key.tunnel.ipv6.ipv6_dst;
2065 ovs_be16 tp_dst = flower->key.tunnel.tp_dst;
2066 ovs_be32 id = be64_to_be32(flower->key.tunnel.id);
2067 uint8_t tos = flower->key.tunnel.tos;
2068 uint8_t ttl = flower->key.tunnel.ttl;
49a7961f
OG
2069 uint8_t tos_mask = flower->mask.tunnel.tos;
2070 uint8_t ttl_mask = flower->mask.tunnel.ttl;
0227bf09 2071 ovs_be64 id_mask = flower->mask.tunnel.id;
f98e418f 2072
f98e418f
RD
2073 if (ipv4_dst) {
2074 nl_msg_put_be32(request, TCA_FLOWER_KEY_ENC_IPV4_SRC, ipv4_src);
2075 nl_msg_put_be32(request, TCA_FLOWER_KEY_ENC_IPV4_DST, ipv4_dst);
2076 } else if (!is_all_zeros(ipv6_dst, sizeof *ipv6_dst)) {
2077 nl_msg_put_in6_addr(request, TCA_FLOWER_KEY_ENC_IPV6_SRC, ipv6_src);
2078 nl_msg_put_in6_addr(request, TCA_FLOWER_KEY_ENC_IPV6_DST, ipv6_dst);
2079 }
49a7961f 2080 if (tos_mask) {
dd83253e 2081 nl_msg_put_u8(request, TCA_FLOWER_KEY_ENC_IP_TOS, tos);
49a7961f 2082 nl_msg_put_u8(request, TCA_FLOWER_KEY_ENC_IP_TOS_MASK, tos_mask);
dd83253e 2083 }
49a7961f 2084 if (ttl_mask) {
dd83253e 2085 nl_msg_put_u8(request, TCA_FLOWER_KEY_ENC_IP_TTL, ttl);
49a7961f 2086 nl_msg_put_u8(request, TCA_FLOWER_KEY_ENC_IP_TTL_MASK, ttl_mask);
dd83253e 2087 }
e48f49e0
EB
2088 if (tp_dst) {
2089 nl_msg_put_be16(request, TCA_FLOWER_KEY_ENC_UDP_DST_PORT, tp_dst);
2090 }
0227bf09
AN
2091 if (id_mask) {
2092 nl_msg_put_be32(request, TCA_FLOWER_KEY_ENC_KEY_ID, id);
2093 }
a468645c
PJV
2094 nl_msg_put_flower_tunnel_opts(request, TCA_FLOWER_KEY_ENC_OPTS,
2095 flower->key.tunnel.metadata);
2096 nl_msg_put_flower_tunnel_opts(request, TCA_FLOWER_KEY_ENC_OPTS_MASK,
2097 flower->mask.tunnel.metadata);
f98e418f
RD
2098}
2099
bb170644
PB
2100#define FLOWER_PUT_MASKED_VALUE(member, type) \
2101 nl_msg_put_masked_value(request, type, type##_MASK, &flower->key.member, \
2102 &flower->mask.member, sizeof flower->key.member)
2103
8ada482b 2104static int
f98e418f
RD
2105nl_msg_put_flower_options(struct ofpbuf *request, struct tc_flower *flower)
2106{
8ada482b 2107
f98e418f 2108 uint16_t host_eth_type = ntohs(flower->key.eth_type);
b5ad40a9 2109 bool is_vlan = eth_type_vlan(flower->key.eth_type);
f9885dc5 2110 bool is_qinq = is_vlan && eth_type_vlan(flower->key.encap_eth_type[0]);
34b16955 2111 bool is_mpls = eth_type_mpls(flower->key.eth_type);
8ada482b
PB
2112 int err;
2113
d6118e62
PB
2114 /* need to parse acts first as some acts require changing the matching
2115 * see csum_update_flag() */
8ada482b
PB
2116 err = nl_msg_put_flower_acts(request, flower);
2117 if (err) {
2118 return err;
2119 }
f98e418f
RD
2120
2121 if (is_vlan) {
f9885dc5
JL
2122 if (is_qinq) {
2123 host_eth_type = ntohs(flower->key.encap_eth_type[1]);
2124 } else {
2125 host_eth_type = ntohs(flower->key.encap_eth_type[0]);
2126 }
f98e418f
RD
2127 }
2128
34b16955
PJV
2129 if (is_mpls) {
2130 host_eth_type = ntohs(flower->key.encap_eth_type[0]);
2131 }
2132
bb170644
PB
2133 FLOWER_PUT_MASKED_VALUE(dst_mac, TCA_FLOWER_KEY_ETH_DST);
2134 FLOWER_PUT_MASKED_VALUE(src_mac, TCA_FLOWER_KEY_ETH_SRC);
f98e418f
RD
2135
2136 if (host_eth_type == ETH_P_IP || host_eth_type == ETH_P_IPV6) {
b4496fc9 2137 FLOWER_PUT_MASKED_VALUE(ip_ttl, TCA_FLOWER_KEY_IP_TTL);
dfa2ccdb 2138 FLOWER_PUT_MASKED_VALUE(ip_tos, TCA_FLOWER_KEY_IP_TOS);
b4496fc9 2139
f98e418f
RD
2140 if (flower->mask.ip_proto && flower->key.ip_proto) {
2141 nl_msg_put_u8(request, TCA_FLOWER_KEY_IP_PROTO,
2142 flower->key.ip_proto);
2143 }
2144
83e86606 2145 if (flower->mask.flags) {
7e0f69b5 2146 nl_msg_put_be32(request, TCA_FLOWER_KEY_FLAGS,
83e86606 2147 htonl(flower->key.flags));
7e0f69b5 2148 nl_msg_put_be32(request, TCA_FLOWER_KEY_FLAGS_MASK,
83e86606
RD
2149 htonl(flower->mask.flags));
2150 }
2151
f98e418f 2152 if (flower->key.ip_proto == IPPROTO_UDP) {
2b1d9fa9
PB
2153 FLOWER_PUT_MASKED_VALUE(udp_src, TCA_FLOWER_KEY_UDP_SRC);
2154 FLOWER_PUT_MASKED_VALUE(udp_dst, TCA_FLOWER_KEY_UDP_DST);
f98e418f 2155 } else if (flower->key.ip_proto == IPPROTO_TCP) {
2b1d9fa9
PB
2156 FLOWER_PUT_MASKED_VALUE(tcp_src, TCA_FLOWER_KEY_TCP_SRC);
2157 FLOWER_PUT_MASKED_VALUE(tcp_dst, TCA_FLOWER_KEY_TCP_DST);
cd081043 2158 FLOWER_PUT_MASKED_VALUE(tcp_flags, TCA_FLOWER_KEY_TCP_FLAGS);
4862b4e5 2159 } else if (flower->key.ip_proto == IPPROTO_SCTP) {
2b1d9fa9
PB
2160 FLOWER_PUT_MASKED_VALUE(sctp_src, TCA_FLOWER_KEY_SCTP_SRC);
2161 FLOWER_PUT_MASKED_VALUE(sctp_dst, TCA_FLOWER_KEY_SCTP_DST);
f98e418f
RD
2162 }
2163 }
2164
2165 if (host_eth_type == ETH_P_IP) {
bb170644
PB
2166 FLOWER_PUT_MASKED_VALUE(ipv4.ipv4_src, TCA_FLOWER_KEY_IPV4_SRC);
2167 FLOWER_PUT_MASKED_VALUE(ipv4.ipv4_dst, TCA_FLOWER_KEY_IPV4_DST);
f98e418f 2168 } else if (host_eth_type == ETH_P_IPV6) {
bb170644
PB
2169 FLOWER_PUT_MASKED_VALUE(ipv6.ipv6_src, TCA_FLOWER_KEY_IPV6_SRC);
2170 FLOWER_PUT_MASKED_VALUE(ipv6.ipv6_dst, TCA_FLOWER_KEY_IPV6_DST);
f98e418f
RD
2171 }
2172
2173 nl_msg_put_be16(request, TCA_FLOWER_KEY_ETH_TYPE, flower->key.eth_type);
2174
34b16955
PJV
2175 if (is_mpls) {
2176 if (mpls_lse_to_ttl(flower->mask.mpls_lse)) {
2177 nl_msg_put_u8(request, TCA_FLOWER_KEY_MPLS_TTL,
2178 mpls_lse_to_ttl(flower->key.mpls_lse));
2179 }
2180 if (mpls_lse_to_tc(flower->mask.mpls_lse)) {
2181 nl_msg_put_u8(request, TCA_FLOWER_KEY_MPLS_TC,
2182 mpls_lse_to_tc(flower->key.mpls_lse));
2183 }
2184 if (mpls_lse_to_bos(flower->mask.mpls_lse)) {
2185 nl_msg_put_u8(request, TCA_FLOWER_KEY_MPLS_BOS,
2186 mpls_lse_to_bos(flower->key.mpls_lse));
2187 }
2188 if (mpls_lse_to_label(flower->mask.mpls_lse)) {
2189 nl_msg_put_u32(request, TCA_FLOWER_KEY_MPLS_LABEL,
2190 mpls_lse_to_label(flower->key.mpls_lse));
2191 }
2192 }
2193
f98e418f 2194 if (is_vlan) {
7f02f26c 2195 if (flower->mask.vlan_id[0]) {
f98e418f 2196 nl_msg_put_u16(request, TCA_FLOWER_KEY_VLAN_ID,
f9885dc5 2197 flower->key.vlan_id[0]);
7f02f26c
PJV
2198 }
2199 if (flower->mask.vlan_prio[0]) {
f98e418f 2200 nl_msg_put_u8(request, TCA_FLOWER_KEY_VLAN_PRIO,
f9885dc5 2201 flower->key.vlan_prio[0]);
f98e418f 2202 }
f9885dc5 2203 if (flower->key.encap_eth_type[0]) {
f98e418f 2204 nl_msg_put_be16(request, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
f9885dc5
JL
2205 flower->key.encap_eth_type[0]);
2206 }
2207
2208 if (is_qinq) {
7f02f26c 2209 if (flower->mask.vlan_id[1]) {
f9885dc5
JL
2210 nl_msg_put_u16(request, TCA_FLOWER_KEY_CVLAN_ID,
2211 flower->key.vlan_id[1]);
7f02f26c
PJV
2212 }
2213 if (flower->mask.vlan_prio[1]) {
f9885dc5
JL
2214 nl_msg_put_u8(request, TCA_FLOWER_KEY_CVLAN_PRIO,
2215 flower->key.vlan_prio[1]);
2216 }
2217 if (flower->key.encap_eth_type[1]) {
2218 nl_msg_put_be16(request, TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
2219 flower->key.encap_eth_type[1]);
2220 }
f98e418f
RD
2221 }
2222 }
2223
691d20cb 2224 nl_msg_put_u32(request, TCA_FLOWER_FLAGS, tc_get_tc_cls_policy(tc_policy));
f98e418f 2225
105e8179 2226 if (flower->tunnel) {
f98e418f
RD
2227 nl_msg_put_flower_tunnel(request, flower);
2228 }
2229
8ada482b 2230 return 0;
f98e418f
RD
2231}
2232
2233int
2234tc_replace_flower(int ifindex, uint16_t prio, uint32_t handle,
093c9458 2235 struct tc_flower *flower, uint32_t block_id)
f98e418f
RD
2236{
2237 struct ofpbuf request;
2238 struct tcmsg *tcmsg;
2239 struct ofpbuf *reply;
2240 int error = 0;
2241 size_t basic_offset;
2242 uint16_t eth_type = (OVS_FORCE uint16_t) flower->key.eth_type;
093c9458 2243 int index;
f98e418f 2244
093c9458
JH
2245 index = block_id ? TCM_IFINDEX_MAGIC_BLOCK : ifindex;
2246 tcmsg = tc_make_request(index, RTM_NEWTFILTER, NLM_F_CREATE | NLM_F_ECHO,
2247 &request);
2248 tcmsg->tcm_parent = block_id ? : TC_INGRESS_PARENT;
f98e418f
RD
2249 tcmsg->tcm_info = tc_make_handle(prio, eth_type);
2250 tcmsg->tcm_handle = handle;
2251
2252 nl_msg_put_string(&request, TCA_KIND, "flower");
2253 basic_offset = nl_msg_start_nested(&request, TCA_OPTIONS);
2254 {
8ada482b
PB
2255 error = nl_msg_put_flower_options(&request, flower);
2256
2257 if (error) {
2258 ofpbuf_uninit(&request);
2259 return error;
2260 }
f98e418f
RD
2261 }
2262 nl_msg_end_nested(&request, basic_offset);
2263
2264 error = tc_transact(&request, &reply);
2265 if (!error) {
2266 struct tcmsg *tc =
2267 ofpbuf_at_assert(reply, NLMSG_HDRLEN, sizeof *tc);
2268
2269 flower->prio = tc_get_major(tc->tcm_info);
2270 flower->handle = tc->tcm_handle;
2271 ofpbuf_delete(reply);
2272 }
2273
2274 return error;
2275}
691d20cb
PB
2276
2277void
2278tc_set_policy(const char *policy)
2279{
2280 if (!policy) {
2281 return;
2282 }
2283
2284 if (!strcmp(policy, "skip_sw")) {
2285 tc_policy = TC_POLICY_SKIP_SW;
2286 } else if (!strcmp(policy, "skip_hw")) {
2287 tc_policy = TC_POLICY_SKIP_HW;
2288 } else if (!strcmp(policy, "none")) {
2289 tc_policy = TC_POLICY_NONE;
2290 } else {
2291 VLOG_WARN("tc: Invalid policy '%s'", policy);
2292 return;
2293 }
2294
2295 VLOG_INFO("tc: Using policy '%s'", policy);
2296}