]> git.proxmox.com Git - mirror_ovs.git/blame - lib/odp-execute.c
Add a new OVS action check_pkt_larger
[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{
d40533fc 201 ovs_assert(odp_tun_key_from_attr(a, tun_key, NULL) != 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
9b2b8497
VDA
230static void
231odp_set_nd_ext(struct dp_packet *packet, const struct ovs_key_nd_extensions
232 *key, const struct ovs_key_nd_extensions *mask)
233{
234 const struct ovs_nd_msg *ns = dp_packet_l4(packet);
235 ovs_16aligned_be32 reserved = ns->rso_flags;
236 uint8_t opt_type = ns->options[0].type;
237
238 if (mask->nd_reserved) {
239 put_16aligned_be32(&reserved, key->nd_reserved);
240 }
241 if (mask->nd_options_type) {
242 opt_type = key->nd_options_type;
243 }
244 packet_set_nd_ext(packet, reserved, opt_type);
245}
246
e60e935b 247static void
cf62fa4c 248odp_set_nd(struct dp_packet *packet, const struct ovs_key_nd *key,
e60e935b
SRCSA
249 const struct ovs_key_nd *mask)
250{
cf62fa4c 251 const struct ovs_nd_msg *ns = dp_packet_l4(packet);
86d46f3c 252 const struct ovs_nd_lla_opt *lla_opt = dp_packet_get_nd_payload(packet);
e60e935b 253
86d46f3c 254 if (OVS_LIKELY(ns && lla_opt)) {
cf62fa4c 255 int bytes_remain = dp_packet_l4_size(packet) - sizeof(*ns);
932c96b7 256 struct in6_addr tgt_buf;
74ff3298
JR
257 struct eth_addr sll_buf = eth_addr_zero;
258 struct eth_addr tll_buf = eth_addr_zero;
e60e935b 259
86d46f3c
ZKL
260 while (bytes_remain >= ND_LLA_OPT_LEN && lla_opt->len != 0) {
261 if (lla_opt->type == ND_OPT_SOURCE_LINKADDR
262 && lla_opt->len == 1) {
263 sll_buf = lla_opt->mac;
74ff3298 264 ether_addr_copy_masked(&sll_buf, key->nd_sll, mask->nd_sll);
e60e935b
SRCSA
265
266 /* A packet can only contain one SLL or TLL option */
267 break;
86d46f3c
ZKL
268 } else if (lla_opt->type == ND_OPT_TARGET_LINKADDR
269 && lla_opt->len == 1) {
270 tll_buf = lla_opt->mac;
74ff3298 271 ether_addr_copy_masked(&tll_buf, key->nd_tll, mask->nd_tll);
e60e935b
SRCSA
272
273 /* A packet can only contain one SLL or TLL option */
274 break;
275 }
276
86d46f3c
ZKL
277 lla_opt += lla_opt->len;
278 bytes_remain -= lla_opt->len * ND_LLA_OPT_LEN;
e60e935b
SRCSA
279 }
280
281 packet_set_nd(packet,
932c96b7
JR
282 mask_ipv6_addr(ns->target.be32, &key->nd_target,
283 &mask->nd_target, &tgt_buf),
e60e935b
SRCSA
284 sll_buf,
285 tll_buf);
286 }
287}
288
3d2fbd70
JS
289/* Set the NSH header. Assumes the NSH header is present and matches the
290 * MD format of the key. The slow path must take case of that. */
291static void
81fdabb9 292odp_set_nsh(struct dp_packet *packet, const struct nlattr *a, bool has_mask)
3d2fbd70 293{
81fdabb9 294 struct ovs_key_nsh key, mask;
3d2fbd70 295 struct nsh_hdr *nsh = dp_packet_l3(packet);
9a180f2c 296 uint8_t mdtype = nsh_md_type(nsh);
f59cb331 297 ovs_be32 path_hdr;
3d2fbd70 298
81fdabb9 299 if (has_mask) {
d40533fc 300 odp_nsh_key_from_attr(a, &key, &mask, NULL);
81fdabb9 301 } else {
d40533fc 302 odp_nsh_key_from_attr(a, &key, NULL, NULL);
81fdabb9
YY
303 }
304
305 if (!has_mask) {
306 nsh_set_flags_and_ttl(nsh, key.flags, key.ttl);
307 put_16aligned_be32(&nsh->path_hdr, key.path_hdr);
9a180f2c 308 switch (mdtype) {
3d2fbd70
JS
309 case NSH_M_TYPE1:
310 for (int i = 0; i < 4; i++) {
81fdabb9 311 put_16aligned_be32(&nsh->md1.context[i], key.context[i]);
3d2fbd70
JS
312 }
313 break;
314 case NSH_M_TYPE2:
3d2fbd70 315 default:
1fc11c59
JS
316 /* No support for setting any other metadata format yet. */
317 break;
3d2fbd70
JS
318 }
319 } else {
17553f27
YY
320 uint8_t flags = nsh_get_flags(nsh);
321 uint8_t ttl = nsh_get_ttl(nsh);
322
81fdabb9
YY
323 flags = key.flags | (flags & ~mask.flags);
324 ttl = key.ttl | (ttl & ~mask.ttl);
17553f27 325 nsh_set_flags_and_ttl(nsh, flags, ttl);
3d2fbd70 326
17553f27
YY
327 uint32_t spi = ntohl(nsh_get_spi(nsh));
328 uint8_t si = nsh_get_si(nsh);
81fdabb9
YY
329 uint32_t spi_mask = nsh_path_hdr_to_spi_uint32(mask.path_hdr);
330 uint8_t si_mask = nsh_path_hdr_to_si(mask.path_hdr);
f59cb331
YY
331 if (spi_mask == 0x00ffffff) {
332 spi_mask = UINT32_MAX;
333 }
81fdabb9
YY
334 spi = nsh_path_hdr_to_spi_uint32(key.path_hdr) | (spi & ~spi_mask);
335 si = nsh_path_hdr_to_si(key.path_hdr) | (si & ~si_mask);
17553f27
YY
336 path_hdr = nsh_get_path_hdr(nsh);
337 nsh_path_hdr_set_spi(&path_hdr, htonl(spi));
338 nsh_path_hdr_set_si(&path_hdr, si);
3d2fbd70 339 put_16aligned_be32(&nsh->path_hdr, path_hdr);
9a180f2c 340 switch (mdtype) {
3d2fbd70
JS
341 case NSH_M_TYPE1:
342 for (int i = 0; i < 4; i++) {
f59cb331 343 ovs_be32 p = get_16aligned_be32(&nsh->md1.context[i]);
81fdabb9
YY
344 ovs_be32 k = key.context[i];
345 ovs_be32 m = mask.context[i];
f59cb331 346 put_16aligned_be32(&nsh->md1.context[i], k | (p & ~m));
3d2fbd70
JS
347 }
348 break;
349 case NSH_M_TYPE2:
3d2fbd70 350 default:
1fc11c59
JS
351 /* No support for setting any other metadata format yet. */
352 break;
3d2fbd70
JS
353 }
354 }
355}
356
6c13071b 357static void
e14deea0 358odp_execute_set_action(struct dp_packet *packet, const struct nlattr *a)
f094af7b
SH
359{
360 enum ovs_key_attr type = nl_attr_type(a);
361 const struct ovs_key_ipv4 *ipv4_key;
362 const struct ovs_key_ipv6 *ipv6_key;
41ccaa24 363 struct pkt_metadata *md = &packet->md;
f094af7b
SH
364
365 switch (type) {
366 case OVS_KEY_ATTR_PRIORITY:
09f9da0b 367 md->skb_priority = nl_attr_get_u32(a);
6c13071b
SH
368 break;
369
f094af7b 370 case OVS_KEY_ATTR_TUNNEL:
09f9da0b 371 odp_set_tunnel_action(a, &md->tunnel);
6c13071b
SH
372 break;
373
f094af7b 374 case OVS_KEY_ATTR_SKB_MARK:
09f9da0b 375 md->pkt_mark = nl_attr_get_u32(a);
f094af7b
SH
376 break;
377
378 case OVS_KEY_ATTR_ETHERNET:
cf62fa4c 379 odp_eth_set_addrs(packet, nl_attr_get(a), NULL);
f094af7b
SH
380 break;
381
f59cb331 382 case OVS_KEY_ATTR_NSH: {
81fdabb9 383 odp_set_nsh(packet, a, false);
3d2fbd70 384 break;
f59cb331 385 }
3d2fbd70 386
f094af7b
SH
387 case OVS_KEY_ATTR_IPV4:
388 ipv4_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_ipv4));
cf62fa4c 389 packet_set_ipv4(packet, ipv4_key->ipv4_src,
91088554
DDP
390 ipv4_key->ipv4_dst, ipv4_key->ipv4_tos,
391 ipv4_key->ipv4_ttl);
f094af7b
SH
392 break;
393
394 case OVS_KEY_ATTR_IPV6:
395 ipv6_key = nl_attr_get_unspec(a, sizeof(struct ovs_key_ipv6));
932c96b7 396 packet_set_ipv6(packet, &ipv6_key->ipv6_src, &ipv6_key->ipv6_dst,
91088554
DDP
397 ipv6_key->ipv6_tclass, ipv6_key->ipv6_label,
398 ipv6_key->ipv6_hlimit);
f094af7b
SH
399 break;
400
401 case OVS_KEY_ATTR_TCP:
cf62fa4c 402 if (OVS_LIKELY(dp_packet_get_tcp_payload(packet))) {
b8778a0d
JR
403 const struct ovs_key_tcp *tcp_key
404 = nl_attr_get_unspec(a, sizeof(struct ovs_key_tcp));
405
cf62fa4c 406 packet_set_tcp_port(packet, tcp_key->tcp_src,
b8778a0d
JR
407 tcp_key->tcp_dst);
408 }
f094af7b
SH
409 break;
410
2b06df54 411 case OVS_KEY_ATTR_UDP:
cf62fa4c 412 if (OVS_LIKELY(dp_packet_get_udp_payload(packet))) {
b8778a0d
JR
413 const struct ovs_key_udp *udp_key
414 = nl_attr_get_unspec(a, sizeof(struct ovs_key_udp));
415
cf62fa4c 416 packet_set_udp_port(packet, udp_key->udp_src,
b8778a0d
JR
417 udp_key->udp_dst);
418 }
f094af7b
SH
419 break;
420
c6bcb685 421 case OVS_KEY_ATTR_SCTP:
cf62fa4c 422 if (OVS_LIKELY(dp_packet_get_sctp_payload(packet))) {
b8778a0d
JR
423 const struct ovs_key_sctp *sctp_key
424 = nl_attr_get_unspec(a, sizeof(struct ovs_key_sctp));
425
cf62fa4c 426 packet_set_sctp_port(packet, sctp_key->sctp_src,
b8778a0d
JR
427 sctp_key->sctp_dst);
428 }
c6bcb685
JS
429 break;
430
2b06df54 431 case OVS_KEY_ATTR_MPLS:
cf62fa4c 432 set_mpls_lse(packet, nl_attr_get_be32(a));
6d670e7f 433 break;
f094af7b 434
f6c8a6b1 435 case OVS_KEY_ATTR_ARP:
cf62fa4c 436 set_arp(packet, nl_attr_get(a), NULL);
f6c8a6b1
BP
437 break;
438
f6ecf944
JP
439 case OVS_KEY_ATTR_ICMP:
440 case OVS_KEY_ATTR_ICMPV6:
441 if (OVS_LIKELY(dp_packet_get_icmp_payload(packet))) {
442 const struct ovs_key_icmp *icmp_key
443 = nl_attr_get_unspec(a, sizeof(struct ovs_key_icmp));
444
445 packet_set_icmp(packet, icmp_key->icmp_type, icmp_key->icmp_code);
446 }
447 break;
448
e60e935b 449 case OVS_KEY_ATTR_ND:
cf62fa4c 450 if (OVS_LIKELY(dp_packet_get_nd_payload(packet))) {
e60e935b
SRCSA
451 const struct ovs_key_nd *nd_key
452 = nl_attr_get_unspec(a, sizeof(struct ovs_key_nd));
932c96b7 453 packet_set_nd(packet, &nd_key->nd_target, nd_key->nd_sll,
74ff3298 454 nd_key->nd_tll);
e60e935b
SRCSA
455 }
456 break;
457
9b2b8497
VDA
458 case OVS_KEY_ATTR_ND_EXTENSIONS:
459 if (OVS_LIKELY(dp_packet_get_nd_payload(packet))) {
460 const struct ovs_key_nd_extensions *nd_ext_key
461 = nl_attr_get_unspec(a, sizeof(struct ovs_key_nd_extensions));
462 ovs_16aligned_be32 rso_flags;
463 put_16aligned_be32(&rso_flags, nd_ext_key->nd_reserved);
464 packet_set_nd_ext(packet, rso_flags, nd_ext_key->nd_options_type);
465 }
466 break;
467
572f732a 468 case OVS_KEY_ATTR_DP_HASH:
61a2647e 469 md->dp_hash = nl_attr_get_u32(a);
572f732a
AZ
470 break;
471
472 case OVS_KEY_ATTR_RECIRC_ID:
473 md->recirc_id = nl_attr_get_u32(a);
474 break;
475
2b06df54 476 case OVS_KEY_ATTR_UNSPEC:
beb75a40 477 case OVS_KEY_ATTR_PACKET_TYPE:
2b06df54
JS
478 case OVS_KEY_ATTR_ENCAP:
479 case OVS_KEY_ATTR_ETHERTYPE:
480 case OVS_KEY_ATTR_IN_PORT:
481 case OVS_KEY_ATTR_VLAN:
dc235f7f 482 case OVS_KEY_ATTR_TCP_FLAGS:
07659514 483 case OVS_KEY_ATTR_CT_STATE:
c30b4cea
JR
484 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4:
485 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6:
07659514 486 case OVS_KEY_ATTR_CT_ZONE:
8e53fe8c 487 case OVS_KEY_ATTR_CT_MARK:
9daf2348 488 case OVS_KEY_ATTR_CT_LABELS:
2b06df54
JS
489 case __OVS_KEY_ATTR_MAX:
490 default:
428b2edd 491 OVS_NOT_REACHED();
f094af7b
SH
492 }
493}
494
6d670e7f
JR
495#define get_mask(a, type) ((const type *)(const void *)(a + 1) + 1)
496
497static void
e14deea0 498odp_execute_masked_set_action(struct dp_packet *packet,
41ccaa24 499 const struct nlattr *a)
6d670e7f 500{
41ccaa24 501 struct pkt_metadata *md = &packet->md;
6d670e7f
JR
502 enum ovs_key_attr type = nl_attr_type(a);
503 struct mpls_hdr *mh;
504
505 switch (type) {
506 case OVS_KEY_ATTR_PRIORITY:
507 md->skb_priority = nl_attr_get_u32(a)
508 | (md->skb_priority & ~*get_mask(a, uint32_t));
509 break;
510
511 case OVS_KEY_ATTR_SKB_MARK:
512 md->pkt_mark = nl_attr_get_u32(a)
513 | (md->pkt_mark & ~*get_mask(a, uint32_t));
514 break;
515
516 case OVS_KEY_ATTR_ETHERNET:
cf62fa4c 517 odp_eth_set_addrs(packet, nl_attr_get(a),
6d670e7f
JR
518 get_mask(a, struct ovs_key_ethernet));
519 break;
520
f59cb331 521 case OVS_KEY_ATTR_NSH: {
81fdabb9 522 odp_set_nsh(packet, a, true);
3d2fbd70 523 break;
f59cb331 524 }
3d2fbd70 525
6d670e7f 526 case OVS_KEY_ATTR_IPV4:
cf62fa4c 527 odp_set_ipv4(packet, nl_attr_get(a),
6d670e7f
JR
528 get_mask(a, struct ovs_key_ipv4));
529 break;
530
531 case OVS_KEY_ATTR_IPV6:
cf62fa4c 532 odp_set_ipv6(packet, nl_attr_get(a),
6d670e7f
JR
533 get_mask(a, struct ovs_key_ipv6));
534 break;
535
536 case OVS_KEY_ATTR_TCP:
cf62fa4c 537 odp_set_tcp(packet, nl_attr_get(a),
6d670e7f
JR
538 get_mask(a, struct ovs_key_tcp));
539 break;
540
541 case OVS_KEY_ATTR_UDP:
cf62fa4c 542 odp_set_udp(packet, nl_attr_get(a),
6d670e7f
JR
543 get_mask(a, struct ovs_key_udp));
544 break;
545
546 case OVS_KEY_ATTR_SCTP:
cf62fa4c 547 odp_set_sctp(packet, nl_attr_get(a),
6d670e7f
JR
548 get_mask(a, struct ovs_key_sctp));
549 break;
550
551 case OVS_KEY_ATTR_MPLS:
cf62fa4c 552 mh = dp_packet_l2_5(packet);
6d670e7f
JR
553 if (mh) {
554 put_16aligned_be32(&mh->mpls_lse, nl_attr_get_be32(a)
555 | (get_16aligned_be32(&mh->mpls_lse)
556 & ~*get_mask(a, ovs_be32)));
557 }
558 break;
559
560 case OVS_KEY_ATTR_ARP:
cf62fa4c 561 set_arp(packet, nl_attr_get(a),
6d670e7f
JR
562 get_mask(a, struct ovs_key_arp));
563 break;
564
e60e935b 565 case OVS_KEY_ATTR_ND:
cf62fa4c 566 odp_set_nd(packet, nl_attr_get(a),
e60e935b
SRCSA
567 get_mask(a, struct ovs_key_nd));
568 break;
569
9b2b8497
VDA
570 case OVS_KEY_ATTR_ND_EXTENSIONS:
571 odp_set_nd_ext(packet, nl_attr_get(a),
572 get_mask(a, struct ovs_key_nd_extensions));
573 break;
574
6d670e7f 575 case OVS_KEY_ATTR_DP_HASH:
f7c2f97d 576 md->dp_hash = nl_attr_get_u32(a)
2bc1bbd2 577 | (md->dp_hash & ~*get_mask(a, uint32_t));
6d670e7f
JR
578 break;
579
580 case OVS_KEY_ATTR_RECIRC_ID:
581 md->recirc_id = nl_attr_get_u32(a)
582 | (md->recirc_id & ~*get_mask(a, uint32_t));
583 break;
584
585 case OVS_KEY_ATTR_TUNNEL: /* Masked data not supported for tunnel. */
beb75a40 586 case OVS_KEY_ATTR_PACKET_TYPE:
6d670e7f 587 case OVS_KEY_ATTR_UNSPEC:
07659514
JS
588 case OVS_KEY_ATTR_CT_STATE:
589 case OVS_KEY_ATTR_CT_ZONE:
8e53fe8c 590 case OVS_KEY_ATTR_CT_MARK:
9daf2348 591 case OVS_KEY_ATTR_CT_LABELS:
c30b4cea
JR
592 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4:
593 case OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6:
6d670e7f
JR
594 case OVS_KEY_ATTR_ENCAP:
595 case OVS_KEY_ATTR_ETHERTYPE:
596 case OVS_KEY_ATTR_IN_PORT:
597 case OVS_KEY_ATTR_VLAN:
598 case OVS_KEY_ATTR_ICMP:
599 case OVS_KEY_ATTR_ICMPV6:
6d670e7f
JR
600 case OVS_KEY_ATTR_TCP_FLAGS:
601 case __OVS_KEY_ATTR_MAX:
602 default:
603 OVS_NOT_REACHED();
604 }
605}
606
f094af7b 607static void
e14deea0 608odp_execute_sample(void *dp, struct dp_packet *packet, bool steal,
41ccaa24 609 const struct nlattr *action,
1164afb6 610 odp_execute_cb dp_execute_action)
f094af7b
SH
611{
612 const struct nlattr *subactions = NULL;
613 const struct nlattr *a;
1895cc8d 614 struct dp_packet_batch pb;
f094af7b
SH
615 size_t left;
616
617 NL_NESTED_FOR_EACH_UNSAFE (a, left, action) {
618 int type = nl_attr_type(a);
619
620 switch ((enum ovs_sample_attr) type) {
621 case OVS_SAMPLE_ATTR_PROBABILITY:
622 if (random_uint32() >= nl_attr_get_u32(a)) {
1164afb6 623 if (steal) {
e14deea0 624 dp_packet_delete(packet);
1164afb6 625 }
f094af7b
SH
626 return;
627 }
628 break;
629
630 case OVS_SAMPLE_ATTR_ACTIONS:
631 subactions = a;
632 break;
633
634 case OVS_SAMPLE_ATTR_UNSPEC:
635 case __OVS_SAMPLE_ATTR_MAX:
636 default:
428b2edd 637 OVS_NOT_REACHED();
f094af7b
SH
638 }
639 }
640
70a0573d
AZ
641 if (!steal) {
642 /* The 'subactions' may modify the packet, but the modification
643 * should not propagate beyond this sample action. Make a copy
644 * the packet in case we don't own the packet, so that the
645 * 'subactions' are only applid to the clone. 'odp_execute_actions'
646 * will free the clone. */
647 packet = dp_packet_clone(packet);
648 }
72c84bc2 649 dp_packet_batch_init_packet(&pb, packet);
70a0573d 650 odp_execute_actions(dp, &pb, true, nl_attr_get(subactions),
1164afb6 651 nl_attr_get_size(subactions), dp_execute_action);
f094af7b
SH
652}
653
535e3acf 654static void
911b7e9b 655odp_execute_clone(void *dp, struct dp_packet_batch *batch, bool steal,
535e3acf
AZ
656 const struct nlattr *actions,
657 odp_execute_cb dp_execute_action)
658{
535e3acf
AZ
659 if (!steal) {
660 /* The 'actions' may modify the packet, but the modification
661 * should not propagate beyond this clone action. Make a copy
662 * the packet in case we don't own the packet, so that the
663 * 'actions' are only applied to the clone. 'odp_execute_actions'
664 * will free the clone. */
911b7e9b
SC
665 struct dp_packet_batch clone_pkt_batch;
666 dp_packet_batch_clone(&clone_pkt_batch, batch);
667 dp_packet_batch_reset_cutlen(batch);
668 odp_execute_actions(dp, &clone_pkt_batch, true, nl_attr_get(actions),
535e3acf 669 nl_attr_get_size(actions), dp_execute_action);
911b7e9b
SC
670 }
671 else {
672 odp_execute_actions(dp, batch, true, nl_attr_get(actions),
673 nl_attr_get_size(actions), dp_execute_action);
674 }
535e3acf
AZ
675}
676
5b34f8fc
NS
677static void
678odp_execute_check_pkt_len(void *dp, struct dp_packet *packet, bool steal,
679 const struct nlattr *action,
680 odp_execute_cb dp_execute_action)
681{
682 static const struct nl_policy ovs_cpl_policy[] = {
683 [OVS_CHECK_PKT_LEN_ATTR_PKT_LEN] = { .type = NL_A_U16 },
684 [OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER] = { .type = NL_A_NESTED },
685 [OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL]
686 = { .type = NL_A_NESTED },
687 };
688 struct nlattr *attrs[ARRAY_SIZE(ovs_cpl_policy)];
689
690 if (!nl_parse_nested(action, ovs_cpl_policy, attrs, ARRAY_SIZE(attrs))) {
691 OVS_NOT_REACHED();
692 }
693
694 const struct nlattr *a;
695 struct dp_packet_batch pb;
696
697 a = attrs[OVS_CHECK_PKT_LEN_ATTR_PKT_LEN];
698 bool is_greater = dp_packet_size(packet) > nl_attr_get_u16(a);
699 if (is_greater) {
700 a = attrs[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER];
701 } else {
702 a = attrs[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL];
703 }
704
705 if (!steal) {
706 /* The 'subactions' may modify the packet, but the modification
707 * should not propagate beyond this action. Make a copy
708 * the packet in case we don't own the packet, so that the
709 * 'subactions' are only applid to check_pkt_len. 'odp_execute_actions'
710 * will free the clone. */
711 packet = dp_packet_clone(packet);
712 }
713 /* If nl_attr_get(a) is NULL, the packet will be freed by
714 * odp_execute_actions. */
715 dp_packet_batch_init_packet(&pb, packet);
716 odp_execute_actions(dp, &pb, true, nl_attr_get(a), nl_attr_get_size(a),
717 dp_execute_action);
718}
719
db8bb9a5
JS
720static bool
721requires_datapath_assistance(const struct nlattr *a)
722{
723 enum ovs_action_attr type = nl_attr_type(a);
724
725 switch (type) {
726 /* These only make sense in the context of a datapath. */
727 case OVS_ACTION_ATTR_OUTPUT:
728 case OVS_ACTION_ATTR_TUNNEL_PUSH:
729 case OVS_ACTION_ATTR_TUNNEL_POP:
730 case OVS_ACTION_ATTR_USERSPACE:
731 case OVS_ACTION_ATTR_RECIRC:
07659514 732 case OVS_ACTION_ATTR_CT:
5dddf960 733 case OVS_ACTION_ATTR_METER:
db8bb9a5
JS
734 return true;
735
736 case OVS_ACTION_ATTR_SET:
737 case OVS_ACTION_ATTR_SET_MASKED:
738 case OVS_ACTION_ATTR_PUSH_VLAN:
739 case OVS_ACTION_ATTR_POP_VLAN:
740 case OVS_ACTION_ATTR_SAMPLE:
741 case OVS_ACTION_ATTR_HASH:
742 case OVS_ACTION_ATTR_PUSH_MPLS:
743 case OVS_ACTION_ATTR_POP_MPLS:
aaca4fe0 744 case OVS_ACTION_ATTR_TRUNC:
6fcecb85
YY
745 case OVS_ACTION_ATTR_PUSH_ETH:
746 case OVS_ACTION_ATTR_POP_ETH:
535e3acf 747 case OVS_ACTION_ATTR_CLONE:
f59cb331
YY
748 case OVS_ACTION_ATTR_PUSH_NSH:
749 case OVS_ACTION_ATTR_POP_NSH:
1fe178d2 750 case OVS_ACTION_ATTR_CT_CLEAR:
5b34f8fc 751 case OVS_ACTION_ATTR_CHECK_PKT_LEN:
db8bb9a5
JS
752 return false;
753
754 case OVS_ACTION_ATTR_UNSPEC:
755 case __OVS_ACTION_ATTR_MAX:
756 OVS_NOT_REACHED();
757 }
758
759 return false;
760}
761
78b76327
BP
762/* Executes all of the 'actions_len' bytes of datapath actions in 'actions' on
763 * the packets in 'batch'. If 'steal' is true, possibly modifies and
764 * definitely free the packets in 'batch', otherwise leaves 'batch' unchanged.
765 *
766 * Some actions (e.g. output actions) can only be executed by a datapath. This
767 * function implements those actions by passing the action and the packets to
768 * 'dp_execute_action' (along with 'dp'). If 'dp_execute_action' is passed a
a8ab5889
DB
769 * true 'steal' parameter then it must definitely free the packets passed into
770 * it. The packet can be modified whether 'steal' is false or true. If a
771 * packet is removed from the batch, then the fate of the packet is determined
772 * by the code that does this removal, irrespective of the value of 'steal'.
773 * Otherwise, if the packet is not removed from the batch and 'steal' is false
774 * then the packet could either be cloned or not. */
1164afb6 775void
1895cc8d 776odp_execute_actions(void *dp, struct dp_packet_batch *batch, bool steal,
1164afb6
DDP
777 const struct nlattr *actions, size_t actions_len,
778 odp_execute_cb dp_execute_action)
f094af7b 779{
72c84bc2 780 struct dp_packet *packet;
f094af7b
SH
781 const struct nlattr *a;
782 unsigned int left;
8cbf4f47 783
f094af7b
SH
784 NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) {
785 int type = nl_attr_type(a);
1164afb6 786 bool last_action = (left <= NLA_ALIGN(a->nla_len));
f094af7b 787
db8bb9a5 788 if (requires_datapath_assistance(a)) {
09f9da0b
JR
789 if (dp_execute_action) {
790 /* Allow 'dp_execute_action' to steal the packet data if we do
791 * not need it any more. */
7d7ded7a 792 bool should_steal = steal && last_action;
1164afb6 793
7d7ded7a 794 dp_execute_action(dp, batch, a, should_steal);
1164afb6 795
1270b6e5 796 if (last_action || dp_packet_batch_is_empty(batch)) {
e5fd79c5
VDA
797 /* We do not need to free the packets.
798 * Either dp_execute_actions() has stolen them
799 * or the batch is freed due to errors. In either
800 * case we do not need to execute further actions.
801 */
1164afb6
DDP
802 return;
803 }
c51876c3 804 }
db8bb9a5
JS
805 continue;
806 }
f094af7b 807
db8bb9a5 808 switch ((enum ovs_action_attr) type) {
6a0b0d3b 809
c6bf49f3
AZ
810 case OVS_ACTION_ATTR_HASH: {
811 const struct ovs_action_hash *hash_act = nl_attr_get(a);
812
6a0b0d3b 813 /* Calculate a hash value directly. This might not match the
c6bf49f3
AZ
814 * value computed by the datapath, but it is much less expensive,
815 * and the current use case (bonding) does not require a strict
816 * match to work properly. */
6a0b0d3b
JS
817 switch (hash_act->hash_alg) {
818 case OVS_HASH_ALG_L4: {
c6bf49f3
AZ
819 struct flow flow;
820 uint32_t hash;
821
e883448e 822 DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
95a6cb34
IM
823 /* RSS hash can be used here instead of 5tuple for
824 * performance reasons. */
825 if (dp_packet_rss_valid(packet)) {
826 hash = dp_packet_get_rss_hash(packet);
827 hash = hash_int(hash, hash_act->hash_basis);
828 } else {
829 flow_extract(packet, &flow);
830 hash = flow_hash_5tuple(&flow, hash_act->hash_basis);
831 }
72c84bc2 832 packet->md.dp_hash = hash;
8cbf4f47 833 }
6a0b0d3b
JS
834 break;
835 }
836 case OVS_HASH_ALG_SYM_L4: {
837 struct flow flow;
838 uint32_t hash;
839
840 DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
841 flow_extract(packet, &flow);
842 hash = flow_hash_symmetric_l3l4(&flow,
843 hash_act->hash_basis,
844 false);
845 packet->md.dp_hash = hash;
846 }
847 break;
848 }
849 default:
c6bf49f3
AZ
850 /* Assert on unknown hash algorithm. */
851 OVS_NOT_REACHED();
852 }
853 break;
854 }
855
f094af7b
SH
856 case OVS_ACTION_ATTR_PUSH_VLAN: {
857 const struct ovs_action_push_vlan *vlan = nl_attr_get(a);
8cbf4f47 858
e883448e 859 DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
72c84bc2 860 eth_push_vlan(packet, vlan->vlan_tpid, vlan->vlan_tci);
8cbf4f47 861 }
f094af7b
SH
862 break;
863 }
864
865 case OVS_ACTION_ATTR_POP_VLAN:
e883448e 866 DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
72c84bc2 867 eth_pop_vlan(packet);
8cbf4f47 868 }
f094af7b
SH
869 break;
870
871 case OVS_ACTION_ATTR_PUSH_MPLS: {
872 const struct ovs_action_push_mpls *mpls = nl_attr_get(a);
8cbf4f47 873
e883448e 874 DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
72c84bc2 875 push_mpls(packet, mpls->mpls_ethertype, mpls->mpls_lse);
8cbf4f47 876 }
f094af7b
SH
877 break;
878 }
879
880 case OVS_ACTION_ATTR_POP_MPLS:
e883448e 881 DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
72c84bc2 882 pop_mpls(packet, nl_attr_get_be16(a));
8cbf4f47 883 }
f094af7b
SH
884 break;
885
886 case OVS_ACTION_ATTR_SET:
e883448e 887 DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
72c84bc2 888 odp_execute_set_action(packet, nl_attr_get(a));
6d670e7f
JR
889 }
890 break;
891
892 case OVS_ACTION_ATTR_SET_MASKED:
e883448e 893 DP_PACKET_BATCH_FOR_EACH(i, packet, batch) {
72c84bc2 894 odp_execute_masked_set_action(packet, nl_attr_get(a));
8cbf4f47 895 }
f094af7b
SH
896 break;
897
898 case OVS_ACTION_ATTR_SAMPLE:
e883448e 899 DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
72c84bc2 900 odp_execute_sample(dp, packet, steal && last_action, a,
1164afb6
DDP
901 dp_execute_action);
902 }
903
904 if (last_action) {
905 /* We do not need to free the packets. odp_execute_sample() has
906 * stolen them*/
907 return;
8cbf4f47 908 }
f094af7b
SH
909 break;
910
aaca4fe0
WT
911 case OVS_ACTION_ATTR_TRUNC: {
912 const struct ovs_action_trunc *trunc =
913 nl_attr_get_unspec(a, sizeof *trunc);
914
915 batch->trunc = true;
e883448e 916 DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
72c84bc2 917 dp_packet_set_cutlen(packet, trunc->max_len);
aaca4fe0
WT
918 }
919 break;
920 }
921
535e3acf 922 case OVS_ACTION_ATTR_CLONE:
911b7e9b
SC
923 odp_execute_clone(dp, batch, steal && last_action, a,
924 dp_execute_action);
535e3acf
AZ
925 if (last_action) {
926 /* We do not need to free the packets. odp_execute_clone() has
927 * stolen them. */
928 return;
929 }
760ba862 930 break;
5dddf960
JR
931 case OVS_ACTION_ATTR_METER:
932 /* Not implemented yet. */
535e3acf 933 break;
88fc5281
JS
934 case OVS_ACTION_ATTR_PUSH_ETH: {
935 const struct ovs_action_push_eth *eth = nl_attr_get(a);
936
e883448e 937 DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
88fc5281
JS
938 push_eth(packet, &eth->addresses.eth_dst,
939 &eth->addresses.eth_src);
940 }
941 break;
942 }
943
944 case OVS_ACTION_ATTR_POP_ETH:
e883448e 945 DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
88fc5281
JS
946 pop_eth(packet);
947 }
948 break;
535e3acf 949
f59cb331
YY
950 case OVS_ACTION_ATTR_PUSH_NSH: {
951 uint32_t buffer[NSH_HDR_MAX_LEN / 4];
952 struct nsh_hdr *nsh_hdr = ALIGNED_CAST(struct nsh_hdr *, buffer);
953 nsh_reset_ver_flags_ttl_len(nsh_hdr);
954 odp_nsh_hdr_from_attr(nl_attr_get(a), nsh_hdr, NSH_HDR_MAX_LEN);
e883448e 955 DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
f59cb331 956 push_nsh(packet, nsh_hdr);
1fc11c59
JS
957 }
958 break;
959 }
f59cb331 960 case OVS_ACTION_ATTR_POP_NSH: {
95e620b4
BB
961 size_t i;
962 const size_t num = dp_packet_batch_size(batch);
1fc11c59
JS
963
964 DP_PACKET_BATCH_REFILL_FOR_EACH (i, num, packet, batch) {
f59cb331 965 if (pop_nsh(packet)) {
1fc11c59
JS
966 dp_packet_batch_refill(batch, packet, i);
967 } else {
968 dp_packet_delete(packet);
969 }
970 }
971 break;
972 }
1fe178d2 973 case OVS_ACTION_ATTR_CT_CLEAR:
e883448e 974 DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
1fe178d2
EG
975 conntrack_clear(packet);
976 }
977 break;
1fc11c59 978
5b34f8fc
NS
979 case OVS_ACTION_ATTR_CHECK_PKT_LEN:
980 DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
981 odp_execute_check_pkt_len(dp, packet, steal && last_action, a,
982 dp_execute_action);
983 }
984
985 if (last_action) {
986 /* We do not need to free the packets.
987 * odp_execute_check_pkt_len() has stolen them. */
988 return;
989 }
990 break;
991
db8bb9a5
JS
992 case OVS_ACTION_ATTR_OUTPUT:
993 case OVS_ACTION_ATTR_TUNNEL_PUSH:
994 case OVS_ACTION_ATTR_TUNNEL_POP:
995 case OVS_ACTION_ATTR_USERSPACE:
996 case OVS_ACTION_ATTR_RECIRC:
07659514 997 case OVS_ACTION_ATTR_CT:
f094af7b
SH
998 case OVS_ACTION_ATTR_UNSPEC:
999 case __OVS_ACTION_ATTR_MAX:
428b2edd 1000 OVS_NOT_REACHED();
f094af7b
SH
1001 }
1002 }
8cbf4f47 1003
72c84bc2 1004 dp_packet_delete_batch(batch, steal);
da546e07 1005}