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