]> git.proxmox.com Git - ovs.git/blame - lib/odp-execute.c
dpif-netdev: Indicate support for various ct features.
[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"
6d670e7f 20#include <arpa/inet.h>
1368af0c 21#include <netinet/in.h>
e60e935b 22#include <netinet/icmp6.h>
6d670e7f 23#include <netinet/ip6.h>
f094af7b
SH
24#include <stdlib.h>
25#include <string.h>
26
e14deea0 27#include "dp-packet.h"
758c456d 28#include "dpif.h"
f094af7b 29#include "netlink.h"
837eefc7 30#include "odp-netlink.h"
6c13071b 31#include "odp-util.h"
f094af7b 32#include "packets.h"
c6bf49f3 33#include "flow.h"
f6c8a6b1 34#include "unaligned.h"
f094af7b 35#include "util.h"
fc052306 36#include "csum.h"
f094af7b 37
6d670e7f 38/* Masked copy of an ethernet address. 'src' is already properly masked. */
f094af7b 39static void
74ff3298
JR
40ether_addr_copy_masked(struct eth_addr *dst, const struct eth_addr src,
41 const struct eth_addr mask)
6d670e7f
JR
42{
43 int i;
44
74ff3298
JR
45 for (i = 0; i < ARRAY_SIZE(dst->be16); i++) {
46 dst->be16[i] = src.be16[i] | (dst->be16[i] & ~mask.be16[i]);
6d670e7f
JR
47 }
48}
49
50static void
cf62fa4c 51odp_eth_set_addrs(struct dp_packet *packet, const struct ovs_key_ethernet *key,
6d670e7f 52 const struct ovs_key_ethernet *mask)
f094af7b 53{
2482b0b0 54 struct eth_header *eh = dp_packet_eth(packet);
f094af7b 55
cf3b7538 56 if (eh) {
6d670e7f 57 if (!mask) {
74ff3298
JR
58 eh->eth_src = key->eth_src;
59 eh->eth_dst = key->eth_dst;
6d670e7f 60 } else {
74ff3298
JR
61 ether_addr_copy_masked(&eh->eth_src, key->eth_src, mask->eth_src);
62 ether_addr_copy_masked(&eh->eth_dst, key->eth_dst, mask->eth_dst);
6d670e7f 63 }
cf3b7538 64 }
f094af7b
SH
65}
66
6d670e7f 67static void
cf62fa4c 68odp_set_ipv4(struct dp_packet *packet, const struct ovs_key_ipv4 *key,
6d670e7f
JR
69 const struct ovs_key_ipv4 *mask)
70{
cf62fa4c 71 struct ip_header *nh = dp_packet_l3(packet);
fc052306
ZB
72 ovs_be32 ip_src_nh;
73 ovs_be32 ip_dst_nh;
74 ovs_be32 new_ip_src;
75 ovs_be32 new_ip_dst;
76 uint8_t new_tos;
77 uint8_t new_ttl;
6d670e7f 78
fc052306
ZB
79 if (mask->ipv4_src) {
80 ip_src_nh = get_16aligned_be32(&nh->ip_src);
81 new_ip_src = key->ipv4_src | (ip_src_nh & ~mask->ipv4_src);
82
83 if (ip_src_nh != new_ip_src) {
84 packet_set_ipv4_addr(packet, &nh->ip_src, new_ip_src);
85 }
86 }
87
88 if (mask->ipv4_dst) {
89 ip_dst_nh = get_16aligned_be32(&nh->ip_dst);
90 new_ip_dst = key->ipv4_dst | (ip_dst_nh & ~mask->ipv4_dst);
91
92 if (ip_dst_nh != new_ip_dst) {
93 packet_set_ipv4_addr(packet, &nh->ip_dst, new_ip_dst);
94 }
95 }
96
97 if (mask->ipv4_tos) {
98 new_tos = key->ipv4_tos | (nh->ip_tos & ~mask->ipv4_tos);
99
100 if (nh->ip_tos != new_tos) {
101 nh->ip_csum = recalc_csum16(nh->ip_csum,
102 htons((uint16_t) nh->ip_tos),
103 htons((uint16_t) new_tos));
104 nh->ip_tos = new_tos;
105 }
106 }
107
108 if (OVS_LIKELY(mask->ipv4_ttl)) {
109 new_ttl = key->ipv4_ttl | (nh->ip_ttl & ~mask->ipv4_ttl);
110
111 if (OVS_LIKELY(nh->ip_ttl != new_ttl)) {
112 nh->ip_csum = recalc_csum16(nh->ip_csum, htons(nh->ip_ttl << 8),
113 htons(new_ttl << 8));
114 nh->ip_ttl = new_ttl;
115 }
116 }
6d670e7f
JR
117}
118
932c96b7
JR
119static struct in6_addr *
120mask_ipv6_addr(const ovs_16aligned_be32 *old, const struct in6_addr *addr,
121 const struct in6_addr *mask, struct in6_addr *masked)
6d670e7f 122{
932c96b7 123#ifdef s6_addr32
6d670e7f 124 for (int i = 0; i < 4; i++) {
932c96b7
JR
125 masked->s6_addr32[i] = addr->s6_addr32[i]
126 | (get_16aligned_be32(&old[i]) & ~mask->s6_addr32[i]);
6d670e7f 127 }
932c96b7
JR
128#else
129 const uint8_t *old8 = (const uint8_t *)old;
130 for (int i = 0; i < 16; i++) {
131 masked->s6_addr[i] = addr->s6_addr[i] | (old8[i] & ~mask->s6_addr[i]);
132 }
133#endif
6d670e7f
JR
134 return masked;
135}
136
137static void
cf62fa4c 138odp_set_ipv6(struct dp_packet *packet, const struct ovs_key_ipv6 *key,
6d670e7f
JR
139 const struct ovs_key_ipv6 *mask)
140{
cf62fa4c 141 struct ovs_16aligned_ip6_hdr *nh = dp_packet_l3(packet);
932c96b7 142 struct in6_addr sbuf, dbuf;
6d670e7f
JR
143 uint8_t old_tc = ntohl(get_16aligned_be32(&nh->ip6_flow)) >> 20;
144 ovs_be32 old_fl = get_16aligned_be32(&nh->ip6_flow) & htonl(0xfffff);
145
146 packet_set_ipv6(
147 packet,
932c96b7
JR
148 mask_ipv6_addr(nh->ip6_src.be32, &key->ipv6_src, &mask->ipv6_src,
149 &sbuf),
150 mask_ipv6_addr(nh->ip6_dst.be32, &key->ipv6_dst, &mask->ipv6_dst,
151 &dbuf),
6d670e7f
JR
152 key->ipv6_tclass | (old_tc & ~mask->ipv6_tclass),
153 key->ipv6_label | (old_fl & ~mask->ipv6_label),
154 key->ipv6_hlimit | (nh->ip6_hlim & ~mask->ipv6_hlimit));
155}
156
157static void
cf62fa4c 158odp_set_tcp(struct dp_packet *packet, const struct ovs_key_tcp *key,
6d670e7f
JR
159 const struct ovs_key_tcp *mask)
160{
cf62fa4c 161 struct tcp_header *th = dp_packet_l4(packet);
6d670e7f 162
cf62fa4c 163 if (OVS_LIKELY(th && dp_packet_get_tcp_payload(packet))) {
b8778a0d
JR
164 packet_set_tcp_port(packet,
165 key->tcp_src | (th->tcp_src & ~mask->tcp_src),
166 key->tcp_dst | (th->tcp_dst & ~mask->tcp_dst));
167 }
6d670e7f
JR
168}
169
170static void
cf62fa4c 171odp_set_udp(struct dp_packet *packet, const struct ovs_key_udp *key,
6d670e7f
JR
172 const struct ovs_key_udp *mask)
173{
cf62fa4c 174 struct udp_header *uh = dp_packet_l4(packet);
6d670e7f 175
cf62fa4c 176 if (OVS_LIKELY(uh && dp_packet_get_udp_payload(packet))) {
b8778a0d
JR
177 packet_set_udp_port(packet,
178 key->udp_src | (uh->udp_src & ~mask->udp_src),
179 key->udp_dst | (uh->udp_dst & ~mask->udp_dst));
180 }
6d670e7f
JR
181}
182
183static void
cf62fa4c 184odp_set_sctp(struct dp_packet *packet, const struct ovs_key_sctp *key,
6d670e7f
JR
185 const struct ovs_key_sctp *mask)
186{
cf62fa4c 187 struct sctp_header *sh = dp_packet_l4(packet);
6d670e7f 188
cf62fa4c 189 if (OVS_LIKELY(sh && dp_packet_get_sctp_payload(packet))) {
b8778a0d
JR
190 packet_set_sctp_port(packet,
191 key->sctp_src | (sh->sctp_src & ~mask->sctp_src),
192 key->sctp_dst | (sh->sctp_dst & ~mask->sctp_dst));
193 }
6d670e7f
JR
194}
195
f094af7b 196static void
6c13071b
SH
197odp_set_tunnel_action(const struct nlattr *a, struct flow_tnl *tun_key)
198{
199 enum odp_key_fitness fitness;
200
8d8ab6c2 201 fitness = odp_tun_key_from_attr(a, tun_key);
6c13071b
SH
202 ovs_assert(fitness != ODP_FIT_ERROR);
203}
204
f6c8a6b1 205static void
cf62fa4c 206set_arp(struct dp_packet *packet, const struct ovs_key_arp *key,
6d670e7f 207 const struct ovs_key_arp *mask)
f6c8a6b1 208{
cf62fa4c 209 struct arp_eth_header *arp = dp_packet_l3(packet);
f6c8a6b1 210
6d670e7f
JR
211 if (!mask) {
212 arp->ar_op = key->arp_op;
74ff3298 213 arp->ar_sha = key->arp_sha;
6d670e7f 214 put_16aligned_be32(&arp->ar_spa, key->arp_sip);
74ff3298 215 arp->ar_tha = key->arp_tha;
6d670e7f
JR
216 put_16aligned_be32(&arp->ar_tpa, key->arp_tip);
217 } else {
218 ovs_be32 ar_spa = get_16aligned_be32(&arp->ar_spa);
219 ovs_be32 ar_tpa = get_16aligned_be32(&arp->ar_tpa);
220
221 arp->ar_op = key->arp_op | (arp->ar_op & ~mask->arp_op);
74ff3298 222 ether_addr_copy_masked(&arp->ar_sha, key->arp_sha, mask->arp_sha);
6d670e7f
JR
223 put_16aligned_be32(&arp->ar_spa,
224 key->arp_sip | (ar_spa & ~mask->arp_sip));
74ff3298 225 ether_addr_copy_masked(&arp->ar_tha, key->arp_tha, mask->arp_tha);
6d670e7f
JR
226 put_16aligned_be32(&arp->ar_tpa,
227 key->arp_tip | (ar_tpa & ~mask->arp_tip));
228 }
f6c8a6b1
BP
229}
230
e60e935b 231static void
cf62fa4c 232odp_set_nd(struct dp_packet *packet, const struct ovs_key_nd *key,
e60e935b
SRCSA
233 const struct ovs_key_nd *mask)
234{
cf62fa4c 235 const struct ovs_nd_msg *ns = dp_packet_l4(packet);
86d46f3c 236 const struct ovs_nd_lla_opt *lla_opt = dp_packet_get_nd_payload(packet);
e60e935b 237
86d46f3c 238 if (OVS_LIKELY(ns && lla_opt)) {
cf62fa4c 239 int bytes_remain = dp_packet_l4_size(packet) - sizeof(*ns);
932c96b7 240 struct in6_addr tgt_buf;
74ff3298
JR
241 struct eth_addr sll_buf = eth_addr_zero;
242 struct eth_addr tll_buf = eth_addr_zero;
e60e935b 243
86d46f3c
ZKL
244 while (bytes_remain >= ND_LLA_OPT_LEN && lla_opt->len != 0) {
245 if (lla_opt->type == ND_OPT_SOURCE_LINKADDR
246 && lla_opt->len == 1) {
247 sll_buf = lla_opt->mac;
74ff3298 248 ether_addr_copy_masked(&sll_buf, key->nd_sll, mask->nd_sll);
e60e935b
SRCSA
249
250 /* A packet can only contain one SLL or TLL option */
251 break;
86d46f3c
ZKL
252 } else if (lla_opt->type == ND_OPT_TARGET_LINKADDR
253 && lla_opt->len == 1) {
254 tll_buf = lla_opt->mac;
74ff3298 255 ether_addr_copy_masked(&tll_buf, key->nd_tll, mask->nd_tll);
e60e935b
SRCSA
256
257 /* A packet can only contain one SLL or TLL option */
258 break;
259 }
260
86d46f3c
ZKL
261 lla_opt += lla_opt->len;
262 bytes_remain -= lla_opt->len * ND_LLA_OPT_LEN;
e60e935b
SRCSA
263 }
264
265 packet_set_nd(packet,
932c96b7
JR
266 mask_ipv6_addr(ns->target.be32, &key->nd_target,
267 &mask->nd_target, &tgt_buf),
e60e935b
SRCSA
268 sll_buf,
269 tll_buf);
270 }
271}
272
6c13071b 273static void
e14deea0 274odp_execute_set_action(struct dp_packet *packet, const struct nlattr *a)
f094af7b
SH
275{
276 enum ovs_key_attr type = nl_attr_type(a);
277 const struct ovs_key_ipv4 *ipv4_key;
278 const struct ovs_key_ipv6 *ipv6_key;
41ccaa24 279 struct pkt_metadata *md = &packet->md;
f094af7b
SH
280
281 switch (type) {
282 case OVS_KEY_ATTR_PRIORITY:
09f9da0b 283 md->skb_priority = nl_attr_get_u32(a);
6c13071b
SH
284 break;
285
f094af7b 286 case OVS_KEY_ATTR_TUNNEL:
09f9da0b 287 odp_set_tunnel_action(a, &md->tunnel);
6c13071b
SH
288 break;
289
f094af7b 290 case OVS_KEY_ATTR_SKB_MARK:
09f9da0b 291 md->pkt_mark = nl_attr_get_u32(a);
f094af7b
SH
292 break;
293
294 case OVS_KEY_ATTR_ETHERNET:
cf62fa4c 295 odp_eth_set_addrs(packet, nl_attr_get(a), NULL);
f094af7b
SH
296 break;
297
298 case OVS_KEY_ATTR_IPV4:
299 ipv4_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_ipv4));
cf62fa4c 300 packet_set_ipv4(packet, ipv4_key->ipv4_src,
91088554
DDP
301 ipv4_key->ipv4_dst, ipv4_key->ipv4_tos,
302 ipv4_key->ipv4_ttl);
f094af7b
SH
303 break;
304
305 case OVS_KEY_ATTR_IPV6:
306 ipv6_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_ipv6));
932c96b7 307 packet_set_ipv6(packet, &ipv6_key->ipv6_src, &ipv6_key->ipv6_dst,
91088554
DDP
308 ipv6_key->ipv6_tclass, ipv6_key->ipv6_label,
309 ipv6_key->ipv6_hlimit);
f094af7b
SH
310 break;
311
312 case OVS_KEY_ATTR_TCP:
cf62fa4c 313 if (OVS_LIKELY(dp_packet_get_tcp_payload(packet))) {
b8778a0d
JR
314 const struct ovs_key_tcp *tcp_key
315 = nl_attr_get_unspec(a, sizeof(struct ovs_key_tcp));
316
cf62fa4c 317 packet_set_tcp_port(packet, tcp_key->tcp_src,
b8778a0d
JR
318 tcp_key->tcp_dst);
319 }
f094af7b
SH
320 break;
321
2b06df54 322 case OVS_KEY_ATTR_UDP:
cf62fa4c 323 if (OVS_LIKELY(dp_packet_get_udp_payload(packet))) {
b8778a0d
JR
324 const struct ovs_key_udp *udp_key
325 = nl_attr_get_unspec(a, sizeof(struct ovs_key_udp));
326
cf62fa4c 327 packet_set_udp_port(packet, udp_key->udp_src,
b8778a0d
JR
328 udp_key->udp_dst);
329 }
f094af7b
SH
330 break;
331
c6bcb685 332 case OVS_KEY_ATTR_SCTP:
cf62fa4c 333 if (OVS_LIKELY(dp_packet_get_sctp_payload(packet))) {
b8778a0d
JR
334 const struct ovs_key_sctp *sctp_key
335 = nl_attr_get_unspec(a, sizeof(struct ovs_key_sctp));
336
cf62fa4c 337 packet_set_sctp_port(packet, sctp_key->sctp_src,
b8778a0d
JR
338 sctp_key->sctp_dst);
339 }
c6bcb685
JS
340 break;
341
2b06df54 342 case OVS_KEY_ATTR_MPLS:
cf62fa4c 343 set_mpls_lse(packet, nl_attr_get_be32(a));
6d670e7f 344 break;
f094af7b 345
f6c8a6b1 346 case OVS_KEY_ATTR_ARP:
cf62fa4c 347 set_arp(packet, nl_attr_get(a), NULL);
f6c8a6b1
BP
348 break;
349
f6ecf944
JP
350 case OVS_KEY_ATTR_ICMP:
351 case OVS_KEY_ATTR_ICMPV6:
352 if (OVS_LIKELY(dp_packet_get_icmp_payload(packet))) {
353 const struct ovs_key_icmp *icmp_key
354 = nl_attr_get_unspec(a, sizeof(struct ovs_key_icmp));
355
356 packet_set_icmp(packet, icmp_key->icmp_type, icmp_key->icmp_code);
357 }
358 break;
359
e60e935b 360 case OVS_KEY_ATTR_ND:
cf62fa4c 361 if (OVS_LIKELY(dp_packet_get_nd_payload(packet))) {
e60e935b
SRCSA
362 const struct ovs_key_nd *nd_key
363 = nl_attr_get_unspec(a, sizeof(struct ovs_key_nd));
932c96b7 364 packet_set_nd(packet, &nd_key->nd_target, nd_key->nd_sll,
74ff3298 365 nd_key->nd_tll);
e60e935b
SRCSA
366 }
367 break;
368
572f732a 369 case OVS_KEY_ATTR_DP_HASH:
61a2647e 370 md->dp_hash = nl_attr_get_u32(a);
572f732a
AZ
371 break;
372
373 case OVS_KEY_ATTR_RECIRC_ID:
374 md->recirc_id = nl_attr_get_u32(a);
375 break;
376
2b06df54 377 case OVS_KEY_ATTR_UNSPEC:
beb75a40 378 case OVS_KEY_ATTR_PACKET_TYPE:
2b06df54
JS
379 case OVS_KEY_ATTR_ENCAP:
380 case OVS_KEY_ATTR_ETHERTYPE:
381 case OVS_KEY_ATTR_IN_PORT:
382 case OVS_KEY_ATTR_VLAN:
dc235f7f 383 case OVS_KEY_ATTR_TCP_FLAGS:
07659514 384 case OVS_KEY_ATTR_CT_STATE:
c30b4cea
JR
385 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4:
386 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6:
07659514 387 case OVS_KEY_ATTR_CT_ZONE:
8e53fe8c 388 case OVS_KEY_ATTR_CT_MARK:
9daf2348 389 case OVS_KEY_ATTR_CT_LABELS:
2b06df54
JS
390 case __OVS_KEY_ATTR_MAX:
391 default:
428b2edd 392 OVS_NOT_REACHED();
f094af7b
SH
393 }
394}
395
6d670e7f
JR
396#define get_mask(a, type) ((const type *)(const void *)(a + 1) + 1)
397
398static void
e14deea0 399odp_execute_masked_set_action(struct dp_packet *packet,
41ccaa24 400 const struct nlattr *a)
6d670e7f 401{
41ccaa24 402 struct pkt_metadata *md = &packet->md;
6d670e7f
JR
403 enum ovs_key_attr type = nl_attr_type(a);
404 struct mpls_hdr *mh;
405
406 switch (type) {
407 case OVS_KEY_ATTR_PRIORITY:
408 md->skb_priority = nl_attr_get_u32(a)
409 | (md->skb_priority & ~*get_mask(a, uint32_t));
410 break;
411
412 case OVS_KEY_ATTR_SKB_MARK:
413 md->pkt_mark = nl_attr_get_u32(a)
414 | (md->pkt_mark & ~*get_mask(a, uint32_t));
415 break;
416
417 case OVS_KEY_ATTR_ETHERNET:
cf62fa4c 418 odp_eth_set_addrs(packet, nl_attr_get(a),
6d670e7f
JR
419 get_mask(a, struct ovs_key_ethernet));
420 break;
421
422 case OVS_KEY_ATTR_IPV4:
cf62fa4c 423 odp_set_ipv4(packet, nl_attr_get(a),
6d670e7f
JR
424 get_mask(a, struct ovs_key_ipv4));
425 break;
426
427 case OVS_KEY_ATTR_IPV6:
cf62fa4c 428 odp_set_ipv6(packet, nl_attr_get(a),
6d670e7f
JR
429 get_mask(a, struct ovs_key_ipv6));
430 break;
431
432 case OVS_KEY_ATTR_TCP:
cf62fa4c 433 odp_set_tcp(packet, nl_attr_get(a),
6d670e7f
JR
434 get_mask(a, struct ovs_key_tcp));
435 break;
436
437 case OVS_KEY_ATTR_UDP:
cf62fa4c 438 odp_set_udp(packet, nl_attr_get(a),
6d670e7f
JR
439 get_mask(a, struct ovs_key_udp));
440 break;
441
442 case OVS_KEY_ATTR_SCTP:
cf62fa4c 443 odp_set_sctp(packet, nl_attr_get(a),
6d670e7f
JR
444 get_mask(a, struct ovs_key_sctp));
445 break;
446
447 case OVS_KEY_ATTR_MPLS:
cf62fa4c 448 mh = dp_packet_l2_5(packet);
6d670e7f
JR
449 if (mh) {
450 put_16aligned_be32(&mh->mpls_lse, nl_attr_get_be32(a)
451 | (get_16aligned_be32(&mh->mpls_lse)
452 & ~*get_mask(a, ovs_be32)));
453 }
454 break;
455
456 case OVS_KEY_ATTR_ARP:
cf62fa4c 457 set_arp(packet, nl_attr_get(a),
6d670e7f
JR
458 get_mask(a, struct ovs_key_arp));
459 break;
460
e60e935b 461 case OVS_KEY_ATTR_ND:
cf62fa4c 462 odp_set_nd(packet, nl_attr_get(a),
e60e935b
SRCSA
463 get_mask(a, struct ovs_key_nd));
464 break;
465
6d670e7f 466 case OVS_KEY_ATTR_DP_HASH:
f7c2f97d 467 md->dp_hash = nl_attr_get_u32(a)
2bc1bbd2 468 | (md->dp_hash & ~*get_mask(a, uint32_t));
6d670e7f
JR
469 break;
470
471 case OVS_KEY_ATTR_RECIRC_ID:
472 md->recirc_id = nl_attr_get_u32(a)
473 | (md->recirc_id & ~*get_mask(a, uint32_t));
474 break;
475
476 case OVS_KEY_ATTR_TUNNEL: /* Masked data not supported for tunnel. */
beb75a40 477 case OVS_KEY_ATTR_PACKET_TYPE:
6d670e7f 478 case OVS_KEY_ATTR_UNSPEC:
07659514
JS
479 case OVS_KEY_ATTR_CT_STATE:
480 case OVS_KEY_ATTR_CT_ZONE:
8e53fe8c 481 case OVS_KEY_ATTR_CT_MARK:
9daf2348 482 case OVS_KEY_ATTR_CT_LABELS:
c30b4cea
JR
483 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4:
484 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6:
6d670e7f
JR
485 case OVS_KEY_ATTR_ENCAP:
486 case OVS_KEY_ATTR_ETHERTYPE:
487 case OVS_KEY_ATTR_IN_PORT:
488 case OVS_KEY_ATTR_VLAN:
489 case OVS_KEY_ATTR_ICMP:
490 case OVS_KEY_ATTR_ICMPV6:
6d670e7f
JR
491 case OVS_KEY_ATTR_TCP_FLAGS:
492 case __OVS_KEY_ATTR_MAX:
493 default:
494 OVS_NOT_REACHED();
495 }
496}
497
f094af7b 498static void
e14deea0 499odp_execute_sample(void *dp, struct dp_packet *packet, bool steal,
41ccaa24 500 const struct nlattr *action,
1164afb6 501 odp_execute_cb dp_execute_action)
f094af7b
SH
502{
503 const struct nlattr *subactions = NULL;
504 const struct nlattr *a;
1895cc8d 505 struct dp_packet_batch pb;
f094af7b
SH
506 size_t left;
507
508 NL_NESTED_FOR_EACH_UNSAFE (a, left, action) {
509 int type = nl_attr_type(a);
510
511 switch ((enum ovs_sample_attr) type) {
512 case OVS_SAMPLE_ATTR_PROBABILITY:
513 if (random_uint32() >= nl_attr_get_u32(a)) {
1164afb6 514 if (steal) {
e14deea0 515 dp_packet_delete(packet);
1164afb6 516 }
f094af7b
SH
517 return;
518 }
519 break;
520
521 case OVS_SAMPLE_ATTR_ACTIONS:
522 subactions = a;
523 break;
524
525 case OVS_SAMPLE_ATTR_UNSPEC:
526 case __OVS_SAMPLE_ATTR_MAX:
527 default:
428b2edd 528 OVS_NOT_REACHED();
f094af7b
SH
529 }
530 }
531
70a0573d
AZ
532 if (!steal) {
533 /* The 'subactions' may modify the packet, but the modification
534 * should not propagate beyond this sample action. Make a copy
535 * the packet in case we don't own the packet, so that the
536 * 'subactions' are only applid to the clone. 'odp_execute_actions'
537 * will free the clone. */
538 packet = dp_packet_clone(packet);
539 }
72c84bc2 540 dp_packet_batch_init_packet(&pb, packet);
70a0573d 541 odp_execute_actions(dp, &pb, true, nl_attr_get(subactions),
1164afb6 542 nl_attr_get_size(subactions), dp_execute_action);
f094af7b
SH
543}
544
535e3acf 545static void
911b7e9b 546odp_execute_clone(void *dp, struct dp_packet_batch *batch, bool steal,
535e3acf
AZ
547 const struct nlattr *actions,
548 odp_execute_cb dp_execute_action)
549{
535e3acf
AZ
550 if (!steal) {
551 /* The 'actions' may modify the packet, but the modification
552 * should not propagate beyond this clone action. Make a copy
553 * the packet in case we don't own the packet, so that the
554 * 'actions' are only applied to the clone. 'odp_execute_actions'
555 * will free the clone. */
911b7e9b
SC
556 struct dp_packet_batch clone_pkt_batch;
557 dp_packet_batch_clone(&clone_pkt_batch, batch);
558 dp_packet_batch_reset_cutlen(batch);
559 odp_execute_actions(dp, &clone_pkt_batch, true, nl_attr_get(actions),
535e3acf 560 nl_attr_get_size(actions), dp_execute_action);
911b7e9b
SC
561 }
562 else {
563 odp_execute_actions(dp, batch, true, nl_attr_get(actions),
564 nl_attr_get_size(actions), dp_execute_action);
565 }
535e3acf
AZ
566}
567
db8bb9a5
JS
568static bool
569requires_datapath_assistance(const struct nlattr *a)
570{
571 enum ovs_action_attr type = nl_attr_type(a);
572
573 switch (type) {
574 /* These only make sense in the context of a datapath. */
575 case OVS_ACTION_ATTR_OUTPUT:
576 case OVS_ACTION_ATTR_TUNNEL_PUSH:
577 case OVS_ACTION_ATTR_TUNNEL_POP:
578 case OVS_ACTION_ATTR_USERSPACE:
579 case OVS_ACTION_ATTR_RECIRC:
07659514 580 case OVS_ACTION_ATTR_CT:
5dddf960 581 case OVS_ACTION_ATTR_METER:
db8bb9a5
JS
582 return true;
583
584 case OVS_ACTION_ATTR_SET:
585 case OVS_ACTION_ATTR_SET_MASKED:
586 case OVS_ACTION_ATTR_PUSH_VLAN:
587 case OVS_ACTION_ATTR_POP_VLAN:
588 case OVS_ACTION_ATTR_SAMPLE:
589 case OVS_ACTION_ATTR_HASH:
590 case OVS_ACTION_ATTR_PUSH_MPLS:
591 case OVS_ACTION_ATTR_POP_MPLS:
aaca4fe0 592 case OVS_ACTION_ATTR_TRUNC:
6fcecb85
YY
593 case OVS_ACTION_ATTR_PUSH_ETH:
594 case OVS_ACTION_ATTR_POP_ETH:
535e3acf 595 case OVS_ACTION_ATTR_CLONE:
db8bb9a5
JS
596 return false;
597
598 case OVS_ACTION_ATTR_UNSPEC:
599 case __OVS_ACTION_ATTR_MAX:
600 OVS_NOT_REACHED();
601 }
602
603 return false;
604}
605
1164afb6 606void
1895cc8d 607odp_execute_actions(void *dp, struct dp_packet_batch *batch, bool steal,
1164afb6
DDP
608 const struct nlattr *actions, size_t actions_len,
609 odp_execute_cb dp_execute_action)
f094af7b 610{
72c84bc2 611 struct dp_packet *packet;
f094af7b
SH
612 const struct nlattr *a;
613 unsigned int left;
8cbf4f47 614
f094af7b
SH
615 NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) {
616 int type = nl_attr_type(a);
1164afb6 617 bool last_action = (left <= NLA_ALIGN(a->nla_len));
f094af7b 618
db8bb9a5 619 if (requires_datapath_assistance(a)) {
09f9da0b
JR
620 if (dp_execute_action) {
621 /* Allow 'dp_execute_action' to steal the packet data if we do
622 * not need it any more. */
1164afb6
DDP
623 bool may_steal = steal && last_action;
624
1895cc8d 625 dp_execute_action(dp, batch, a, may_steal);
1164afb6
DDP
626
627 if (last_action) {
628 /* We do not need to free the packets. dp_execute_actions()
629 * has stolen them */
630 return;
631 }
c51876c3 632 }
db8bb9a5
JS
633 continue;
634 }
f094af7b 635
db8bb9a5 636 switch ((enum ovs_action_attr) type) {
c6bf49f3
AZ
637 case OVS_ACTION_ATTR_HASH: {
638 const struct ovs_action_hash *hash_act = nl_attr_get(a);
639
640 /* Calculate a hash value directly. This might not match the
641 * value computed by the datapath, but it is much less expensive,
642 * and the current use case (bonding) does not require a strict
643 * match to work properly. */
644 if (hash_act->hash_alg == OVS_HASH_ALG_L4) {
645 struct flow flow;
646 uint32_t hash;
647
72c84bc2
AZ
648 DP_PACKET_BATCH_FOR_EACH (packet, batch) {
649 flow_extract(packet, &flow);
8cbf4f47 650 hash = flow_hash_5tuple(&flow, hash_act->hash_basis);
72c84bc2 651 packet->md.dp_hash = hash;
8cbf4f47 652 }
c6bf49f3
AZ
653 } else {
654 /* Assert on unknown hash algorithm. */
655 OVS_NOT_REACHED();
656 }
657 break;
658 }
659
f094af7b
SH
660 case OVS_ACTION_ATTR_PUSH_VLAN: {
661 const struct ovs_action_push_vlan *vlan = nl_attr_get(a);
8cbf4f47 662
72c84bc2
AZ
663 DP_PACKET_BATCH_FOR_EACH (packet, batch) {
664 eth_push_vlan(packet, vlan->vlan_tpid, vlan->vlan_tci);
8cbf4f47 665 }
f094af7b
SH
666 break;
667 }
668
669 case OVS_ACTION_ATTR_POP_VLAN:
72c84bc2
AZ
670 DP_PACKET_BATCH_FOR_EACH (packet, batch) {
671 eth_pop_vlan(packet);
8cbf4f47 672 }
f094af7b
SH
673 break;
674
675 case OVS_ACTION_ATTR_PUSH_MPLS: {
676 const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
8cbf4f47 677
72c84bc2
AZ
678 DP_PACKET_BATCH_FOR_EACH (packet, batch) {
679 push_mpls(packet, mpls->mpls_ethertype, mpls->mpls_lse);
8cbf4f47 680 }
f094af7b
SH
681 break;
682 }
683
684 case OVS_ACTION_ATTR_POP_MPLS:
72c84bc2
AZ
685 DP_PACKET_BATCH_FOR_EACH (packet, batch) {
686 pop_mpls(packet, nl_attr_get_be16(a));
8cbf4f47 687 }
f094af7b
SH
688 break;
689
690 case OVS_ACTION_ATTR_SET:
72c84bc2
AZ
691 DP_PACKET_BATCH_FOR_EACH (packet, batch) {
692 odp_execute_set_action(packet, nl_attr_get(a));
6d670e7f
JR
693 }
694 break;
695
696 case OVS_ACTION_ATTR_SET_MASKED:
72c84bc2
AZ
697 DP_PACKET_BATCH_FOR_EACH(packet, batch) {
698 odp_execute_masked_set_action(packet, nl_attr_get(a));
8cbf4f47 699 }
f094af7b
SH
700 break;
701
702 case OVS_ACTION_ATTR_SAMPLE:
72c84bc2
AZ
703 DP_PACKET_BATCH_FOR_EACH (packet, batch) {
704 odp_execute_sample(dp, packet, steal && last_action, a,
1164afb6
DDP
705 dp_execute_action);
706 }
707
708 if (last_action) {
709 /* We do not need to free the packets. odp_execute_sample() has
710 * stolen them*/
711 return;
8cbf4f47 712 }
f094af7b
SH
713 break;
714
aaca4fe0
WT
715 case OVS_ACTION_ATTR_TRUNC: {
716 const struct ovs_action_trunc *trunc =
717 nl_attr_get_unspec(a, sizeof *trunc);
718
719 batch->trunc = true;
72c84bc2
AZ
720 DP_PACKET_BATCH_FOR_EACH (packet, batch) {
721 dp_packet_set_cutlen(packet, trunc->max_len);
aaca4fe0
WT
722 }
723 break;
724 }
725
535e3acf 726 case OVS_ACTION_ATTR_CLONE:
911b7e9b
SC
727 odp_execute_clone(dp, batch, steal && last_action, a,
728 dp_execute_action);
535e3acf
AZ
729 if (last_action) {
730 /* We do not need to free the packets. odp_execute_clone() has
731 * stolen them. */
732 return;
733 }
5dddf960
JR
734 case OVS_ACTION_ATTR_METER:
735 /* Not implemented yet. */
535e3acf 736 break;
88fc5281
JS
737 case OVS_ACTION_ATTR_PUSH_ETH: {
738 const struct ovs_action_push_eth *eth = nl_attr_get(a);
739
740 DP_PACKET_BATCH_FOR_EACH (packet, batch) {
741 push_eth(packet, &eth->addresses.eth_dst,
742 &eth->addresses.eth_src);
743 }
744 break;
745 }
746
747 case OVS_ACTION_ATTR_POP_ETH:
748 DP_PACKET_BATCH_FOR_EACH (packet, batch) {
749 pop_eth(packet);
750 }
751 break;
535e3acf 752
db8bb9a5
JS
753 case OVS_ACTION_ATTR_OUTPUT:
754 case OVS_ACTION_ATTR_TUNNEL_PUSH:
755 case OVS_ACTION_ATTR_TUNNEL_POP:
756 case OVS_ACTION_ATTR_USERSPACE:
757 case OVS_ACTION_ATTR_RECIRC:
07659514 758 case OVS_ACTION_ATTR_CT:
f094af7b
SH
759 case OVS_ACTION_ATTR_UNSPEC:
760 case __OVS_ACTION_ATTR_MAX:
428b2edd 761 OVS_NOT_REACHED();
f094af7b
SH
762 }
763 }
8cbf4f47 764
72c84bc2 765 dp_packet_delete_batch(batch, steal);
da546e07 766}