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