]> git.proxmox.com Git - mirror_ovs.git/blame - lib/odp-execute.c
tests: Fix syntax in another ODP test.
[mirror_ovs.git] / lib / odp-execute.c
CommitLineData
f094af7b 1/*
e60e935b 2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
f094af7b
SH
3 * Copyright (c) 2013 Simon Horman
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 "odp-execute.h"
b2befd5b 20#include <sys/types.h>
1368af0c 21#include <netinet/in.h>
b2befd5b 22#include <arpa/inet.h>
e60e935b 23#include <netinet/icmp6.h>
6d670e7f 24#include <netinet/ip6.h>
f094af7b
SH
25#include <stdlib.h>
26#include <string.h>
27
e14deea0 28#include "dp-packet.h"
758c456d 29#include "dpif.h"
f094af7b 30#include "netlink.h"
837eefc7 31#include "odp-netlink.h"
6c13071b 32#include "odp-util.h"
f094af7b 33#include "packets.h"
c6bf49f3 34#include "flow.h"
f6c8a6b1 35#include "unaligned.h"
f094af7b 36#include "util.h"
fc052306 37#include "csum.h"
1fe178d2 38#include "conntrack.h"
f094af7b 39
6d670e7f 40/* Masked copy of an ethernet address. 'src' is already properly masked. */
f094af7b 41static void
74ff3298
JR
42ether_addr_copy_masked(struct eth_addr *dst, const struct eth_addr src,
43 const struct eth_addr mask)
6d670e7f
JR
44{
45 int i;
46
74ff3298
JR
47 for (i = 0; i < ARRAY_SIZE(dst->be16); i++) {
48 dst->be16[i] = src.be16[i] | (dst->be16[i] & ~mask.be16[i]);
6d670e7f
JR
49 }
50}
51
52static void
cf62fa4c 53odp_eth_set_addrs(struct dp_packet *packet, const struct ovs_key_ethernet *key,
6d670e7f 54 const struct ovs_key_ethernet *mask)
f094af7b 55{
2482b0b0 56 struct eth_header *eh = dp_packet_eth(packet);
f094af7b 57
cf3b7538 58 if (eh) {
6d670e7f 59 if (!mask) {
74ff3298
JR
60 eh->eth_src = key->eth_src;
61 eh->eth_dst = key->eth_dst;
6d670e7f 62 } else {
74ff3298
JR
63 ether_addr_copy_masked(&eh->eth_src, key->eth_src, mask->eth_src);
64 ether_addr_copy_masked(&eh->eth_dst, key->eth_dst, mask->eth_dst);
6d670e7f 65 }
cf3b7538 66 }
f094af7b
SH
67}
68
6d670e7f 69static void
cf62fa4c 70odp_set_ipv4(struct dp_packet *packet, const struct ovs_key_ipv4 *key,
6d670e7f
JR
71 const struct ovs_key_ipv4 *mask)
72{
cf62fa4c 73 struct ip_header *nh = dp_packet_l3(packet);
fc052306
ZB
74 ovs_be32 ip_src_nh;
75 ovs_be32 ip_dst_nh;
76 ovs_be32 new_ip_src;
77 ovs_be32 new_ip_dst;
78 uint8_t new_tos;
79 uint8_t new_ttl;
6d670e7f 80
fc052306
ZB
81 if (mask->ipv4_src) {
82 ip_src_nh = get_16aligned_be32(&nh->ip_src);
83 new_ip_src = key->ipv4_src | (ip_src_nh & ~mask->ipv4_src);
84
85 if (ip_src_nh != new_ip_src) {
86 packet_set_ipv4_addr(packet, &nh->ip_src, new_ip_src);
87 }
88 }
89
90 if (mask->ipv4_dst) {
91 ip_dst_nh = get_16aligned_be32(&nh->ip_dst);
92 new_ip_dst = key->ipv4_dst | (ip_dst_nh & ~mask->ipv4_dst);
93
94 if (ip_dst_nh != new_ip_dst) {
95 packet_set_ipv4_addr(packet, &nh->ip_dst, new_ip_dst);
96 }
97 }
98
99 if (mask->ipv4_tos) {
100 new_tos = key->ipv4_tos | (nh->ip_tos & ~mask->ipv4_tos);
101
102 if (nh->ip_tos != new_tos) {
103 nh->ip_csum = recalc_csum16(nh->ip_csum,
104 htons((uint16_t) nh->ip_tos),
105 htons((uint16_t) new_tos));
106 nh->ip_tos = new_tos;
107 }
108 }
109
110 if (OVS_LIKELY(mask->ipv4_ttl)) {
111 new_ttl = key->ipv4_ttl | (nh->ip_ttl & ~mask->ipv4_ttl);
112
113 if (OVS_LIKELY(nh->ip_ttl != new_ttl)) {
114 nh->ip_csum = recalc_csum16(nh->ip_csum, htons(nh->ip_ttl << 8),
115 htons(new_ttl << 8));
116 nh->ip_ttl = new_ttl;
117 }
118 }
6d670e7f
JR
119}
120
932c96b7
JR
121static struct in6_addr *
122mask_ipv6_addr(const ovs_16aligned_be32 *old, const struct in6_addr *addr,
123 const struct in6_addr *mask, struct in6_addr *masked)
6d670e7f 124{
932c96b7 125#ifdef s6_addr32
6d670e7f 126 for (int i = 0; i < 4; i++) {
932c96b7
JR
127 masked->s6_addr32[i] = addr->s6_addr32[i]
128 | (get_16aligned_be32(&old[i]) & ~mask->s6_addr32[i]);
6d670e7f 129 }
932c96b7
JR
130#else
131 const uint8_t *old8 = (const uint8_t *)old;
132 for (int i = 0; i < 16; i++) {
133 masked->s6_addr[i] = addr->s6_addr[i] | (old8[i] & ~mask->s6_addr[i]);
134 }
135#endif
6d670e7f
JR
136 return masked;
137}
138
139static void
cf62fa4c 140odp_set_ipv6(struct dp_packet *packet, const struct ovs_key_ipv6 *key,
6d670e7f
JR
141 const struct ovs_key_ipv6 *mask)
142{
cf62fa4c 143 struct ovs_16aligned_ip6_hdr *nh = dp_packet_l3(packet);
932c96b7 144 struct in6_addr sbuf, dbuf;
6d670e7f
JR
145 uint8_t old_tc = ntohl(get_16aligned_be32(&nh->ip6_flow)) >> 20;
146 ovs_be32 old_fl = get_16aligned_be32(&nh->ip6_flow) & htonl(0xfffff);
147
148 packet_set_ipv6(
149 packet,
932c96b7
JR
150 mask_ipv6_addr(nh->ip6_src.be32, &key->ipv6_src, &mask->ipv6_src,
151 &sbuf),
152 mask_ipv6_addr(nh->ip6_dst.be32, &key->ipv6_dst, &mask->ipv6_dst,
153 &dbuf),
6d670e7f
JR
154 key->ipv6_tclass | (old_tc & ~mask->ipv6_tclass),
155 key->ipv6_label | (old_fl & ~mask->ipv6_label),
156 key->ipv6_hlimit | (nh->ip6_hlim & ~mask->ipv6_hlimit));
157}
158
159static void
cf62fa4c 160odp_set_tcp(struct dp_packet *packet, const struct ovs_key_tcp *key,
6d670e7f
JR
161 const struct ovs_key_tcp *mask)
162{
cf62fa4c 163 struct tcp_header *th = dp_packet_l4(packet);
6d670e7f 164
cf62fa4c 165 if (OVS_LIKELY(th && dp_packet_get_tcp_payload(packet))) {
b8778a0d
JR
166 packet_set_tcp_port(packet,
167 key->tcp_src | (th->tcp_src & ~mask->tcp_src),
168 key->tcp_dst | (th->tcp_dst & ~mask->tcp_dst));
169 }
6d670e7f
JR
170}
171
172static void
cf62fa4c 173odp_set_udp(struct dp_packet *packet, const struct ovs_key_udp *key,
6d670e7f
JR
174 const struct ovs_key_udp *mask)
175{
cf62fa4c 176 struct udp_header *uh = dp_packet_l4(packet);
6d670e7f 177
cf62fa4c 178 if (OVS_LIKELY(uh && dp_packet_get_udp_payload(packet))) {
b8778a0d
JR
179 packet_set_udp_port(packet,
180 key->udp_src | (uh->udp_src & ~mask->udp_src),
181 key->udp_dst | (uh->udp_dst & ~mask->udp_dst));
182 }
6d670e7f
JR
183}
184
185static void
cf62fa4c 186odp_set_sctp(struct dp_packet *packet, const struct ovs_key_sctp *key,
6d670e7f
JR
187 const struct ovs_key_sctp *mask)
188{
cf62fa4c 189 struct sctp_header *sh = dp_packet_l4(packet);
6d670e7f 190
cf62fa4c 191 if (OVS_LIKELY(sh && dp_packet_get_sctp_payload(packet))) {
b8778a0d
JR
192 packet_set_sctp_port(packet,
193 key->sctp_src | (sh->sctp_src & ~mask->sctp_src),
194 key->sctp_dst | (sh->sctp_dst & ~mask->sctp_dst));
195 }
6d670e7f
JR
196}
197
f094af7b 198static void
6c13071b
SH
199odp_set_tunnel_action(const struct nlattr *a, struct flow_tnl *tun_key)
200{
500db308 201 ovs_assert(odp_tun_key_from_attr(a, tun_key) != ODP_FIT_ERROR);
6c13071b
SH
202}
203
f6c8a6b1 204static void
cf62fa4c 205set_arp(struct dp_packet *packet, const struct ovs_key_arp *key,
6d670e7f 206 const struct ovs_key_arp *mask)
f6c8a6b1 207{
cf62fa4c 208 struct arp_eth_header *arp = dp_packet_l3(packet);
f6c8a6b1 209
6d670e7f
JR
210 if (!mask) {
211 arp->ar_op = key->arp_op;
74ff3298 212 arp->ar_sha = key->arp_sha;
6d670e7f 213 put_16aligned_be32(&arp->ar_spa, key->arp_sip);
74ff3298 214 arp->ar_tha = key->arp_tha;
6d670e7f
JR
215 put_16aligned_be32(&arp->ar_tpa, key->arp_tip);
216 } else {
217 ovs_be32 ar_spa = get_16aligned_be32(&arp->ar_spa);
218 ovs_be32 ar_tpa = get_16aligned_be32(&arp->ar_tpa);
219
220 arp->ar_op = key->arp_op | (arp->ar_op & ~mask->arp_op);
74ff3298 221 ether_addr_copy_masked(&arp->ar_sha, key->arp_sha, mask->arp_sha);
6d670e7f
JR
222 put_16aligned_be32(&arp->ar_spa,
223 key->arp_sip | (ar_spa & ~mask->arp_sip));
74ff3298 224 ether_addr_copy_masked(&arp->ar_tha, key->arp_tha, mask->arp_tha);
6d670e7f
JR
225 put_16aligned_be32(&arp->ar_tpa,
226 key->arp_tip | (ar_tpa & ~mask->arp_tip));
227 }
f6c8a6b1
BP
228}
229
e60e935b 230static void
cf62fa4c 231odp_set_nd(struct dp_packet *packet, const struct ovs_key_nd *key,
e60e935b
SRCSA
232 const struct ovs_key_nd *mask)
233{
cf62fa4c 234 const struct ovs_nd_msg *ns = dp_packet_l4(packet);
86d46f3c 235 const struct ovs_nd_lla_opt *lla_opt = dp_packet_get_nd_payload(packet);
e60e935b 236
86d46f3c 237 if (OVS_LIKELY(ns && lla_opt)) {
cf62fa4c 238 int bytes_remain = dp_packet_l4_size(packet) - sizeof(*ns);
932c96b7 239 struct in6_addr tgt_buf;
74ff3298
JR
240 struct eth_addr sll_buf = eth_addr_zero;
241 struct eth_addr tll_buf = eth_addr_zero;
e60e935b 242
86d46f3c
ZKL
243 while (bytes_remain >= ND_LLA_OPT_LEN && lla_opt->len != 0) {
244 if (lla_opt->type == ND_OPT_SOURCE_LINKADDR
245 && lla_opt->len == 1) {
246 sll_buf = lla_opt->mac;
74ff3298 247 ether_addr_copy_masked(&sll_buf, key->nd_sll, mask->nd_sll);
e60e935b
SRCSA
248
249 /* A packet can only contain one SLL or TLL option */
250 break;
86d46f3c
ZKL
251 } else if (lla_opt->type == ND_OPT_TARGET_LINKADDR
252 && lla_opt->len == 1) {
253 tll_buf = lla_opt->mac;
74ff3298 254 ether_addr_copy_masked(&tll_buf, key->nd_tll, mask->nd_tll);
e60e935b
SRCSA
255
256 /* A packet can only contain one SLL or TLL option */
257 break;
258 }
259
86d46f3c
ZKL
260 lla_opt += lla_opt->len;
261 bytes_remain -= lla_opt->len * ND_LLA_OPT_LEN;
e60e935b
SRCSA
262 }
263
264 packet_set_nd(packet,
932c96b7
JR
265 mask_ipv6_addr(ns->target.be32, &key->nd_target,
266 &mask->nd_target, &tgt_buf),
e60e935b
SRCSA
267 sll_buf,
268 tll_buf);
269 }
270}
271
3d2fbd70
JS
272/* Set the NSH header. Assumes the NSH header is present and matches the
273 * MD format of the key. The slow path must take case of that. */
274static void
81fdabb9 275odp_set_nsh(struct dp_packet *packet, const struct nlattr *a, bool has_mask)
3d2fbd70 276{
81fdabb9 277 struct ovs_key_nsh key, mask;
3d2fbd70 278 struct nsh_hdr *nsh = dp_packet_l3(packet);
9a180f2c 279 uint8_t mdtype = nsh_md_type(nsh);
f59cb331 280 ovs_be32 path_hdr;
3d2fbd70 281
81fdabb9
YY
282 if (has_mask) {
283 odp_nsh_key_from_attr(a, &key, &mask);
284 } else {
285 odp_nsh_key_from_attr(a, &key, NULL);
286 }
287
288 if (!has_mask) {
289 nsh_set_flags_and_ttl(nsh, key.flags, key.ttl);
290 put_16aligned_be32(&nsh->path_hdr, key.path_hdr);
9a180f2c 291 switch (mdtype) {
3d2fbd70
JS
292 case NSH_M_TYPE1:
293 for (int i = 0; i < 4; i++) {
81fdabb9 294 put_16aligned_be32(&nsh->md1.context[i], key.context[i]);
3d2fbd70
JS
295 }
296 break;
297 case NSH_M_TYPE2:
3d2fbd70 298 default:
1fc11c59
JS
299 /* No support for setting any other metadata format yet. */
300 break;
3d2fbd70
JS
301 }
302 } else {
17553f27
YY
303 uint8_t flags = nsh_get_flags(nsh);
304 uint8_t ttl = nsh_get_ttl(nsh);
305
81fdabb9
YY
306 flags = key.flags | (flags & ~mask.flags);
307 ttl = key.ttl | (ttl & ~mask.ttl);
17553f27 308 nsh_set_flags_and_ttl(nsh, flags, ttl);
3d2fbd70 309
17553f27
YY
310 uint32_t spi = ntohl(nsh_get_spi(nsh));
311 uint8_t si = nsh_get_si(nsh);
81fdabb9
YY
312 uint32_t spi_mask = nsh_path_hdr_to_spi_uint32(mask.path_hdr);
313 uint8_t si_mask = nsh_path_hdr_to_si(mask.path_hdr);
f59cb331
YY
314 if (spi_mask == 0x00ffffff) {
315 spi_mask = UINT32_MAX;
316 }
81fdabb9
YY
317 spi = nsh_path_hdr_to_spi_uint32(key.path_hdr) | (spi & ~spi_mask);
318 si = nsh_path_hdr_to_si(key.path_hdr) | (si & ~si_mask);
17553f27
YY
319 path_hdr = nsh_get_path_hdr(nsh);
320 nsh_path_hdr_set_spi(&path_hdr, htonl(spi));
321 nsh_path_hdr_set_si(&path_hdr, si);
3d2fbd70 322 put_16aligned_be32(&nsh->path_hdr, path_hdr);
9a180f2c 323 switch (mdtype) {
3d2fbd70
JS
324 case NSH_M_TYPE1:
325 for (int i = 0; i < 4; i++) {
f59cb331 326 ovs_be32 p = get_16aligned_be32(&nsh->md1.context[i]);
81fdabb9
YY
327 ovs_be32 k = key.context[i];
328 ovs_be32 m = mask.context[i];
f59cb331 329 put_16aligned_be32(&nsh->md1.context[i], k | (p & ~m));
3d2fbd70
JS
330 }
331 break;
332 case NSH_M_TYPE2:
3d2fbd70 333 default:
1fc11c59
JS
334 /* No support for setting any other metadata format yet. */
335 break;
3d2fbd70
JS
336 }
337 }
338}
339
6c13071b 340static void
e14deea0 341odp_execute_set_action(struct dp_packet *packet, const struct nlattr *a)
f094af7b
SH
342{
343 enum ovs_key_attr type = nl_attr_type(a);
344 const struct ovs_key_ipv4 *ipv4_key;
345 const struct ovs_key_ipv6 *ipv6_key;
41ccaa24 346 struct pkt_metadata *md = &packet->md;
f094af7b
SH
347
348 switch (type) {
349 case OVS_KEY_ATTR_PRIORITY:
09f9da0b 350 md->skb_priority = nl_attr_get_u32(a);
6c13071b
SH
351 break;
352
f094af7b 353 case OVS_KEY_ATTR_TUNNEL:
09f9da0b 354 odp_set_tunnel_action(a, &md->tunnel);
6c13071b
SH
355 break;
356
f094af7b 357 case OVS_KEY_ATTR_SKB_MARK:
09f9da0b 358 md->pkt_mark = nl_attr_get_u32(a);
f094af7b
SH
359 break;
360
361 case OVS_KEY_ATTR_ETHERNET:
cf62fa4c 362 odp_eth_set_addrs(packet, nl_attr_get(a), NULL);
f094af7b
SH
363 break;
364
f59cb331 365 case OVS_KEY_ATTR_NSH: {
81fdabb9 366 odp_set_nsh(packet, a, false);
3d2fbd70 367 break;
f59cb331 368 }
3d2fbd70 369
f094af7b
SH
370 case OVS_KEY_ATTR_IPV4:
371 ipv4_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_ipv4));
cf62fa4c 372 packet_set_ipv4(packet, ipv4_key->ipv4_src,
91088554
DDP
373 ipv4_key->ipv4_dst, ipv4_key->ipv4_tos,
374 ipv4_key->ipv4_ttl);
f094af7b
SH
375 break;
376
377 case OVS_KEY_ATTR_IPV6:
378 ipv6_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_ipv6));
932c96b7 379 packet_set_ipv6(packet, &ipv6_key->ipv6_src, &ipv6_key->ipv6_dst,
91088554
DDP
380 ipv6_key->ipv6_tclass, ipv6_key->ipv6_label,
381 ipv6_key->ipv6_hlimit);
f094af7b
SH
382 break;
383
384 case OVS_KEY_ATTR_TCP:
cf62fa4c 385 if (OVS_LIKELY(dp_packet_get_tcp_payload(packet))) {
b8778a0d
JR
386 const struct ovs_key_tcp *tcp_key
387 = nl_attr_get_unspec(a, sizeof(struct ovs_key_tcp));
388
cf62fa4c 389 packet_set_tcp_port(packet, tcp_key->tcp_src,
b8778a0d
JR
390 tcp_key->tcp_dst);
391 }
f094af7b
SH
392 break;
393
2b06df54 394 case OVS_KEY_ATTR_UDP:
cf62fa4c 395 if (OVS_LIKELY(dp_packet_get_udp_payload(packet))) {
b8778a0d
JR
396 const struct ovs_key_udp *udp_key
397 = nl_attr_get_unspec(a, sizeof(struct ovs_key_udp));
398
cf62fa4c 399 packet_set_udp_port(packet, udp_key->udp_src,
b8778a0d
JR
400 udp_key->udp_dst);
401 }
f094af7b
SH
402 break;
403
c6bcb685 404 case OVS_KEY_ATTR_SCTP:
cf62fa4c 405 if (OVS_LIKELY(dp_packet_get_sctp_payload(packet))) {
b8778a0d
JR
406 const struct ovs_key_sctp *sctp_key
407 = nl_attr_get_unspec(a, sizeof(struct ovs_key_sctp));
408
cf62fa4c 409 packet_set_sctp_port(packet, sctp_key->sctp_src,
b8778a0d
JR
410 sctp_key->sctp_dst);
411 }
c6bcb685
JS
412 break;
413
2b06df54 414 case OVS_KEY_ATTR_MPLS:
cf62fa4c 415 set_mpls_lse(packet, nl_attr_get_be32(a));
6d670e7f 416 break;
f094af7b 417
f6c8a6b1 418 case OVS_KEY_ATTR_ARP:
cf62fa4c 419 set_arp(packet, nl_attr_get(a), NULL);
f6c8a6b1
BP
420 break;
421
f6ecf944
JP
422 case OVS_KEY_ATTR_ICMP:
423 case OVS_KEY_ATTR_ICMPV6:
424 if (OVS_LIKELY(dp_packet_get_icmp_payload(packet))) {
425 const struct ovs_key_icmp *icmp_key
426 = nl_attr_get_unspec(a, sizeof(struct ovs_key_icmp));
427
428 packet_set_icmp(packet, icmp_key->icmp_type, icmp_key->icmp_code);
429 }
430 break;
431
e60e935b 432 case OVS_KEY_ATTR_ND:
cf62fa4c 433 if (OVS_LIKELY(dp_packet_get_nd_payload(packet))) {
e60e935b
SRCSA
434 const struct ovs_key_nd *nd_key
435 = nl_attr_get_unspec(a, sizeof(struct ovs_key_nd));
932c96b7 436 packet_set_nd(packet, &nd_key->nd_target, nd_key->nd_sll,
74ff3298 437 nd_key->nd_tll);
e60e935b
SRCSA
438 }
439 break;
440
572f732a 441 case OVS_KEY_ATTR_DP_HASH:
61a2647e 442 md->dp_hash = nl_attr_get_u32(a);
572f732a
AZ
443 break;
444
445 case OVS_KEY_ATTR_RECIRC_ID:
446 md->recirc_id = nl_attr_get_u32(a);
447 break;
448
2b06df54 449 case OVS_KEY_ATTR_UNSPEC:
beb75a40 450 case OVS_KEY_ATTR_PACKET_TYPE:
2b06df54
JS
451 case OVS_KEY_ATTR_ENCAP:
452 case OVS_KEY_ATTR_ETHERTYPE:
453 case OVS_KEY_ATTR_IN_PORT:
454 case OVS_KEY_ATTR_VLAN:
dc235f7f 455 case OVS_KEY_ATTR_TCP_FLAGS:
07659514 456 case OVS_KEY_ATTR_CT_STATE:
c30b4cea
JR
457 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4:
458 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6:
07659514 459 case OVS_KEY_ATTR_CT_ZONE:
8e53fe8c 460 case OVS_KEY_ATTR_CT_MARK:
9daf2348 461 case OVS_KEY_ATTR_CT_LABELS:
2b06df54
JS
462 case __OVS_KEY_ATTR_MAX:
463 default:
428b2edd 464 OVS_NOT_REACHED();
f094af7b
SH
465 }
466}
467
6d670e7f
JR
468#define get_mask(a, type) ((const type *)(const void *)(a + 1) + 1)
469
470static void
e14deea0 471odp_execute_masked_set_action(struct dp_packet *packet,
41ccaa24 472 const struct nlattr *a)
6d670e7f 473{
41ccaa24 474 struct pkt_metadata *md = &packet->md;
6d670e7f
JR
475 enum ovs_key_attr type = nl_attr_type(a);
476 struct mpls_hdr *mh;
477
478 switch (type) {
479 case OVS_KEY_ATTR_PRIORITY:
480 md->skb_priority = nl_attr_get_u32(a)
481 | (md->skb_priority & ~*get_mask(a, uint32_t));
482 break;
483
484 case OVS_KEY_ATTR_SKB_MARK:
485 md->pkt_mark = nl_attr_get_u32(a)
486 | (md->pkt_mark & ~*get_mask(a, uint32_t));
487 break;
488
489 case OVS_KEY_ATTR_ETHERNET:
cf62fa4c 490 odp_eth_set_addrs(packet, nl_attr_get(a),
6d670e7f
JR
491 get_mask(a, struct ovs_key_ethernet));
492 break;
493
f59cb331 494 case OVS_KEY_ATTR_NSH: {
81fdabb9 495 odp_set_nsh(packet, a, true);
3d2fbd70 496 break;
f59cb331 497 }
3d2fbd70 498
6d670e7f 499 case OVS_KEY_ATTR_IPV4:
cf62fa4c 500 odp_set_ipv4(packet, nl_attr_get(a),
6d670e7f
JR
501 get_mask(a, struct ovs_key_ipv4));
502 break;
503
504 case OVS_KEY_ATTR_IPV6:
cf62fa4c 505 odp_set_ipv6(packet, nl_attr_get(a),
6d670e7f
JR
506 get_mask(a, struct ovs_key_ipv6));
507 break;
508
509 case OVS_KEY_ATTR_TCP:
cf62fa4c 510 odp_set_tcp(packet, nl_attr_get(a),
6d670e7f
JR
511 get_mask(a, struct ovs_key_tcp));
512 break;
513
514 case OVS_KEY_ATTR_UDP:
cf62fa4c 515 odp_set_udp(packet, nl_attr_get(a),
6d670e7f
JR
516 get_mask(a, struct ovs_key_udp));
517 break;
518
519 case OVS_KEY_ATTR_SCTP:
cf62fa4c 520 odp_set_sctp(packet, nl_attr_get(a),
6d670e7f
JR
521 get_mask(a, struct ovs_key_sctp));
522 break;
523
524 case OVS_KEY_ATTR_MPLS:
cf62fa4c 525 mh = dp_packet_l2_5(packet);
6d670e7f
JR
526 if (mh) {
527 put_16aligned_be32(&mh->mpls_lse, nl_attr_get_be32(a)
528 | (get_16aligned_be32(&mh->mpls_lse)
529 & ~*get_mask(a, ovs_be32)));
530 }
531 break;
532
533 case OVS_KEY_ATTR_ARP:
cf62fa4c 534 set_arp(packet, nl_attr_get(a),
6d670e7f
JR
535 get_mask(a, struct ovs_key_arp));
536 break;
537
e60e935b 538 case OVS_KEY_ATTR_ND:
cf62fa4c 539 odp_set_nd(packet, nl_attr_get(a),
e60e935b
SRCSA
540 get_mask(a, struct ovs_key_nd));
541 break;
542
6d670e7f 543 case OVS_KEY_ATTR_DP_HASH:
f7c2f97d 544 md->dp_hash = nl_attr_get_u32(a)
2bc1bbd2 545 | (md->dp_hash & ~*get_mask(a, uint32_t));
6d670e7f
JR
546 break;
547
548 case OVS_KEY_ATTR_RECIRC_ID:
549 md->recirc_id = nl_attr_get_u32(a)
550 | (md->recirc_id & ~*get_mask(a, uint32_t));
551 break;
552
553 case OVS_KEY_ATTR_TUNNEL: /* Masked data not supported for tunnel. */
beb75a40 554 case OVS_KEY_ATTR_PACKET_TYPE:
6d670e7f 555 case OVS_KEY_ATTR_UNSPEC:
07659514
JS
556 case OVS_KEY_ATTR_CT_STATE:
557 case OVS_KEY_ATTR_CT_ZONE:
8e53fe8c 558 case OVS_KEY_ATTR_CT_MARK:
9daf2348 559 case OVS_KEY_ATTR_CT_LABELS:
c30b4cea
JR
560 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4:
561 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6:
6d670e7f
JR
562 case OVS_KEY_ATTR_ENCAP:
563 case OVS_KEY_ATTR_ETHERTYPE:
564 case OVS_KEY_ATTR_IN_PORT:
565 case OVS_KEY_ATTR_VLAN:
566 case OVS_KEY_ATTR_ICMP:
567 case OVS_KEY_ATTR_ICMPV6:
6d670e7f
JR
568 case OVS_KEY_ATTR_TCP_FLAGS:
569 case __OVS_KEY_ATTR_MAX:
570 default:
571 OVS_NOT_REACHED();
572 }
573}
574
f094af7b 575static void
e14deea0 576odp_execute_sample(void *dp, struct dp_packet *packet, bool steal,
41ccaa24 577 const struct nlattr *action,
1164afb6 578 odp_execute_cb dp_execute_action)
f094af7b
SH
579{
580 const struct nlattr *subactions = NULL;
581 const struct nlattr *a;
1895cc8d 582 struct dp_packet_batch pb;
f094af7b
SH
583 size_t left;
584
585 NL_NESTED_FOR_EACH_UNSAFE (a, left, action) {
586 int type = nl_attr_type(a);
587
588 switch ((enum ovs_sample_attr) type) {
589 case OVS_SAMPLE_ATTR_PROBABILITY:
590 if (random_uint32() >= nl_attr_get_u32(a)) {
1164afb6 591 if (steal) {
e14deea0 592 dp_packet_delete(packet);
1164afb6 593 }
f094af7b
SH
594 return;
595 }
596 break;
597
598 case OVS_SAMPLE_ATTR_ACTIONS:
599 subactions = a;
600 break;
601
602 case OVS_SAMPLE_ATTR_UNSPEC:
603 case __OVS_SAMPLE_ATTR_MAX:
604 default:
428b2edd 605 OVS_NOT_REACHED();
f094af7b
SH
606 }
607 }
608
70a0573d
AZ
609 if (!steal) {
610 /* The 'subactions' may modify the packet, but the modification
611 * should not propagate beyond this sample action. Make a copy
612 * the packet in case we don't own the packet, so that the
613 * 'subactions' are only applid to the clone. 'odp_execute_actions'
614 * will free the clone. */
615 packet = dp_packet_clone(packet);
616 }
72c84bc2 617 dp_packet_batch_init_packet(&pb, packet);
70a0573d 618 odp_execute_actions(dp, &pb, true, nl_attr_get(subactions),
1164afb6 619 nl_attr_get_size(subactions), dp_execute_action);
f094af7b
SH
620}
621
535e3acf 622static void
911b7e9b 623odp_execute_clone(void *dp, struct dp_packet_batch *batch, bool steal,
535e3acf
AZ
624 const struct nlattr *actions,
625 odp_execute_cb dp_execute_action)
626{
535e3acf
AZ
627 if (!steal) {
628 /* The 'actions' may modify the packet, but the modification
629 * should not propagate beyond this clone action. Make a copy
630 * the packet in case we don't own the packet, so that the
631 * 'actions' are only applied to the clone. 'odp_execute_actions'
632 * will free the clone. */
911b7e9b
SC
633 struct dp_packet_batch clone_pkt_batch;
634 dp_packet_batch_clone(&clone_pkt_batch, batch);
635 dp_packet_batch_reset_cutlen(batch);
636 odp_execute_actions(dp, &clone_pkt_batch, true, nl_attr_get(actions),
535e3acf 637 nl_attr_get_size(actions), dp_execute_action);
911b7e9b
SC
638 }
639 else {
640 odp_execute_actions(dp, batch, true, nl_attr_get(actions),
641 nl_attr_get_size(actions), dp_execute_action);
642 }
535e3acf
AZ
643}
644
db8bb9a5
JS
645static bool
646requires_datapath_assistance(const struct nlattr *a)
647{
648 enum ovs_action_attr type = nl_attr_type(a);
649
650 switch (type) {
651 /* These only make sense in the context of a datapath. */
652 case OVS_ACTION_ATTR_OUTPUT:
653 case OVS_ACTION_ATTR_TUNNEL_PUSH:
654 case OVS_ACTION_ATTR_TUNNEL_POP:
655 case OVS_ACTION_ATTR_USERSPACE:
656 case OVS_ACTION_ATTR_RECIRC:
07659514 657 case OVS_ACTION_ATTR_CT:
5dddf960 658 case OVS_ACTION_ATTR_METER:
db8bb9a5
JS
659 return true;
660
661 case OVS_ACTION_ATTR_SET:
662 case OVS_ACTION_ATTR_SET_MASKED:
663 case OVS_ACTION_ATTR_PUSH_VLAN:
664 case OVS_ACTION_ATTR_POP_VLAN:
665 case OVS_ACTION_ATTR_SAMPLE:
666 case OVS_ACTION_ATTR_HASH:
667 case OVS_ACTION_ATTR_PUSH_MPLS:
668 case OVS_ACTION_ATTR_POP_MPLS:
aaca4fe0 669 case OVS_ACTION_ATTR_TRUNC:
6fcecb85
YY
670 case OVS_ACTION_ATTR_PUSH_ETH:
671 case OVS_ACTION_ATTR_POP_ETH:
535e3acf 672 case OVS_ACTION_ATTR_CLONE:
f59cb331
YY
673 case OVS_ACTION_ATTR_PUSH_NSH:
674 case OVS_ACTION_ATTR_POP_NSH:
1fe178d2 675 case OVS_ACTION_ATTR_CT_CLEAR:
db8bb9a5
JS
676 return false;
677
678 case OVS_ACTION_ATTR_UNSPEC:
679 case __OVS_ACTION_ATTR_MAX:
680 OVS_NOT_REACHED();
681 }
682
683 return false;
684}
685
78b76327
BP
686/* Executes all of the 'actions_len' bytes of datapath actions in 'actions' on
687 * the packets in 'batch'. If 'steal' is true, possibly modifies and
688 * definitely free the packets in 'batch', otherwise leaves 'batch' unchanged.
689 *
690 * Some actions (e.g. output actions) can only be executed by a datapath. This
691 * function implements those actions by passing the action and the packets to
692 * 'dp_execute_action' (along with 'dp'). If 'dp_execute_action' is passed a
a8ab5889
DB
693 * true 'steal' parameter then it must definitely free the packets passed into
694 * it. The packet can be modified whether 'steal' is false or true. If a
695 * packet is removed from the batch, then the fate of the packet is determined
696 * by the code that does this removal, irrespective of the value of 'steal'.
697 * Otherwise, if the packet is not removed from the batch and 'steal' is false
698 * then the packet could either be cloned or not. */
1164afb6 699void
1895cc8d 700odp_execute_actions(void *dp, struct dp_packet_batch *batch, bool steal,
1164afb6
DDP
701 const struct nlattr *actions, size_t actions_len,
702 odp_execute_cb dp_execute_action)
f094af7b 703{
72c84bc2 704 struct dp_packet *packet;
f094af7b
SH
705 const struct nlattr *a;
706 unsigned int left;
8cbf4f47 707
f094af7b
SH
708 NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) {
709 int type = nl_attr_type(a);
1164afb6 710 bool last_action = (left <= NLA_ALIGN(a->nla_len));
f094af7b 711
db8bb9a5 712 if (requires_datapath_assistance(a)) {
09f9da0b
JR
713 if (dp_execute_action) {
714 /* Allow 'dp_execute_action' to steal the packet data if we do
715 * not need it any more. */
7d7ded7a 716 bool should_steal = steal && last_action;
1164afb6 717
7d7ded7a 718 dp_execute_action(dp, batch, a, should_steal);
1164afb6 719
e5fd79c5
VDA
720 if (last_action || batch->count == 0) {
721 /* We do not need to free the packets.
722 * Either dp_execute_actions() has stolen them
723 * or the batch is freed due to errors. In either
724 * case we do not need to execute further actions.
725 */
1164afb6
DDP
726 return;
727 }
c51876c3 728 }
db8bb9a5
JS
729 continue;
730 }
f094af7b 731
db8bb9a5 732 switch ((enum ovs_action_attr) type) {
6a0b0d3b 733
c6bf49f3
AZ
734 case OVS_ACTION_ATTR_HASH: {
735 const struct ovs_action_hash *hash_act = nl_attr_get(a);
736
6a0b0d3b 737 /* Calculate a hash value directly. This might not match the
c6bf49f3
AZ
738 * value computed by the datapath, but it is much less expensive,
739 * and the current use case (bonding) does not require a strict
740 * match to work properly. */
6a0b0d3b
JS
741 switch (hash_act->hash_alg) {
742 case OVS_HASH_ALG_L4: {
c6bf49f3
AZ
743 struct flow flow;
744 uint32_t hash;
745
e883448e 746 DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
95a6cb34
IM
747 /* RSS hash can be used here instead of 5tuple for
748 * performance reasons. */
749 if (dp_packet_rss_valid(packet)) {
750 hash = dp_packet_get_rss_hash(packet);
751 hash = hash_int(hash, hash_act->hash_basis);
752 } else {
753 flow_extract(packet, &flow);
754 hash = flow_hash_5tuple(&flow, hash_act->hash_basis);
755 }
72c84bc2 756 packet->md.dp_hash = hash;
8cbf4f47 757 }
6a0b0d3b
JS
758 break;
759 }
760 case OVS_HASH_ALG_SYM_L4: {
761 struct flow flow;
762 uint32_t hash;
763
764 DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
765 flow_extract(packet, &flow);
766 hash = flow_hash_symmetric_l3l4(&flow,
767 hash_act->hash_basis,
768 false);
769 packet->md.dp_hash = hash;
770 }
771 break;
772 }
773 default:
c6bf49f3
AZ
774 /* Assert on unknown hash algorithm. */
775 OVS_NOT_REACHED();
776 }
777 break;
778 }
779
f094af7b
SH
780 case OVS_ACTION_ATTR_PUSH_VLAN: {
781 const struct ovs_action_push_vlan *vlan = nl_attr_get(a);
8cbf4f47 782
e883448e 783 DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
72c84bc2 784 eth_push_vlan(packet, vlan->vlan_tpid, vlan->vlan_tci);
8cbf4f47 785 }
f094af7b
SH
786 break;
787 }
788
789 case OVS_ACTION_ATTR_POP_VLAN:
e883448e 790 DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
72c84bc2 791 eth_pop_vlan(packet);
8cbf4f47 792 }
f094af7b
SH
793 break;
794
795 case OVS_ACTION_ATTR_PUSH_MPLS: {
796 const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
8cbf4f47 797
e883448e 798 DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
72c84bc2 799 push_mpls(packet, mpls->mpls_ethertype, mpls->mpls_lse);
8cbf4f47 800 }
f094af7b
SH
801 break;
802 }
803
804 case OVS_ACTION_ATTR_POP_MPLS:
e883448e 805 DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
72c84bc2 806 pop_mpls(packet, nl_attr_get_be16(a));
8cbf4f47 807 }
f094af7b
SH
808 break;
809
810 case OVS_ACTION_ATTR_SET:
e883448e 811 DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
72c84bc2 812 odp_execute_set_action(packet, nl_attr_get(a));
6d670e7f
JR
813 }
814 break;
815
816 case OVS_ACTION_ATTR_SET_MASKED:
e883448e 817 DP_PACKET_BATCH_FOR_EACH(i, packet, batch) {
72c84bc2 818 odp_execute_masked_set_action(packet, nl_attr_get(a));
8cbf4f47 819 }
f094af7b
SH
820 break;
821
822 case OVS_ACTION_ATTR_SAMPLE:
e883448e 823 DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
72c84bc2 824 odp_execute_sample(dp, packet, steal && last_action, a,
1164afb6
DDP
825 dp_execute_action);
826 }
827
828 if (last_action) {
829 /* We do not need to free the packets. odp_execute_sample() has
830 * stolen them*/
831 return;
8cbf4f47 832 }
f094af7b
SH
833 break;
834
aaca4fe0
WT
835 case OVS_ACTION_ATTR_TRUNC: {
836 const struct ovs_action_trunc *trunc =
837 nl_attr_get_unspec(a, sizeof *trunc);
838
839 batch->trunc = true;
e883448e 840 DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
72c84bc2 841 dp_packet_set_cutlen(packet, trunc->max_len);
aaca4fe0
WT
842 }
843 break;
844 }
845
535e3acf 846 case OVS_ACTION_ATTR_CLONE:
911b7e9b
SC
847 odp_execute_clone(dp, batch, steal && last_action, a,
848 dp_execute_action);
535e3acf
AZ
849 if (last_action) {
850 /* We do not need to free the packets. odp_execute_clone() has
851 * stolen them. */
852 return;
853 }
760ba862 854 break;
5dddf960
JR
855 case OVS_ACTION_ATTR_METER:
856 /* Not implemented yet. */
535e3acf 857 break;
88fc5281
JS
858 case OVS_ACTION_ATTR_PUSH_ETH: {
859 const struct ovs_action_push_eth *eth = nl_attr_get(a);
860
e883448e 861 DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
88fc5281
JS
862 push_eth(packet, &eth->addresses.eth_dst,
863 &eth->addresses.eth_src);
864 }
865 break;
866 }
867
868 case OVS_ACTION_ATTR_POP_ETH:
e883448e 869 DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
88fc5281
JS
870 pop_eth(packet);
871 }
872 break;
535e3acf 873
f59cb331
YY
874 case OVS_ACTION_ATTR_PUSH_NSH: {
875 uint32_t buffer[NSH_HDR_MAX_LEN / 4];
876 struct nsh_hdr *nsh_hdr = ALIGNED_CAST(struct nsh_hdr *, buffer);
877 nsh_reset_ver_flags_ttl_len(nsh_hdr);
878 odp_nsh_hdr_from_attr(nl_attr_get(a), nsh_hdr, NSH_HDR_MAX_LEN);
e883448e 879 DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
f59cb331 880 push_nsh(packet, nsh_hdr);
1fc11c59
JS
881 }
882 break;
883 }
f59cb331 884 case OVS_ACTION_ATTR_POP_NSH: {
95e620b4
BB
885 size_t i;
886 const size_t num = dp_packet_batch_size(batch);
1fc11c59
JS
887
888 DP_PACKET_BATCH_REFILL_FOR_EACH (i, num, packet, batch) {
f59cb331 889 if (pop_nsh(packet)) {
1fc11c59
JS
890 dp_packet_batch_refill(batch, packet, i);
891 } else {
892 dp_packet_delete(packet);
893 }
894 }
895 break;
896 }
1fe178d2 897 case OVS_ACTION_ATTR_CT_CLEAR:
e883448e 898 DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
1fe178d2
EG
899 conntrack_clear(packet);
900 }
901 break;
1fc11c59 902
db8bb9a5
JS
903 case OVS_ACTION_ATTR_OUTPUT:
904 case OVS_ACTION_ATTR_TUNNEL_PUSH:
905 case OVS_ACTION_ATTR_TUNNEL_POP:
906 case OVS_ACTION_ATTR_USERSPACE:
907 case OVS_ACTION_ATTR_RECIRC:
07659514 908 case OVS_ACTION_ATTR_CT:
f094af7b
SH
909 case OVS_ACTION_ATTR_UNSPEC:
910 case __OVS_ACTION_ATTR_MAX:
428b2edd 911 OVS_NOT_REACHED();
f094af7b
SH
912 }
913 }
8cbf4f47 914
72c84bc2 915 dp_packet_delete_batch(batch, steal);
da546e07 916}