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