]> git.proxmox.com Git - mirror_ovs.git/blame - datapath/actions.c
util: New function nullable_xstrdup().
[mirror_ovs.git] / datapath / actions.c
CommitLineData
064af421 1/*
e23775f2 2 * Copyright (c) 2007-2015 Nicira, Inc.
a14bc59f 3 *
a9a29d22
JG
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301, USA
064af421
BP
17 */
18
e9141eec
PS
19#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
064af421
BP
21#include <linux/skbuff.h>
22#include <linux/in.h>
23#include <linux/ip.h>
077257b8 24#include <linux/openvswitch.h>
a94ebc39 25#include <linux/netfilter_ipv6.h>
10f72e3d 26#include <linux/sctp.h>
064af421
BP
27#include <linux/tcp.h>
28#include <linux/udp.h>
29#include <linux/in6.h>
401eeb92 30#include <linux/if_arp.h>
064af421 31#include <linux/if_vlan.h>
a0fb56c1 32
a94ebc39 33#include <net/dst.h>
064af421 34#include <net/ip.h>
bc7a5acd 35#include <net/ipv6.h>
064af421 36#include <net/checksum.h>
530180fd 37#include <net/dsfield.h>
2baf0e0c 38#include <net/mpls.h>
10f72e3d 39#include <net/sctp/checksum.h>
f2459fe7 40
f2459fe7 41#include "datapath.h"
a94ebc39 42#include "conntrack.h"
ccf43786 43#include "gso.h"
f2459fe7 44#include "vport.h"
064af421 45
e74d4817
PS
46static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
47 struct sw_flow_key *key,
48 const struct nlattr *attr, int len);
49
2c8c4fb7
AZ
50struct deferred_action {
51 struct sk_buff *skb;
52 const struct nlattr *actions;
53
54 /* Store pkt_key clone when creating deferred action. */
55 struct sw_flow_key pkt_key;
56};
57
a94ebc39
JS
58#define MAX_L2_LEN (VLAN_ETH_HLEN + 3 * MPLS_HLEN)
59struct ovs_frag_data {
60 unsigned long dst;
61 struct vport *vport;
86c2eb45 62 struct ovs_gso_cb cb;
a94ebc39
JS
63 __be16 inner_protocol;
64 __u16 vlan_tci;
65 __be16 vlan_proto;
66 unsigned int l2_len;
67 u8 l2_data[MAX_L2_LEN];
68};
69
70static DEFINE_PER_CPU(struct ovs_frag_data, ovs_frag_data_storage);
71
2c8c4fb7
AZ
72#define DEFERRED_ACTION_FIFO_SIZE 10
73struct action_fifo {
74 int head;
75 int tail;
76 /* Deferred action fifo queue storage. */
77 struct deferred_action fifo[DEFERRED_ACTION_FIFO_SIZE];
78};
79
80static struct action_fifo __percpu *action_fifos;
81#define EXEC_ACTIONS_LEVEL_LIMIT 4 /* limit used to detect packet
af465b67
PS
82 * looping by the network stack
83 */
2c8c4fb7
AZ
84static DEFINE_PER_CPU(int, exec_actions_level);
85
86static void action_fifo_init(struct action_fifo *fifo)
87{
88 fifo->head = 0;
89 fifo->tail = 0;
90}
91
f1f60b85 92static bool action_fifo_is_empty(const struct action_fifo *fifo)
2c8c4fb7
AZ
93{
94 return (fifo->head == fifo->tail);
95}
96
e74d4817 97static struct deferred_action *action_fifo_get(struct action_fifo *fifo)
2c8c4fb7
AZ
98{
99 if (action_fifo_is_empty(fifo))
100 return NULL;
101
102 return &fifo->fifo[fifo->tail++];
103}
104
e74d4817 105static struct deferred_action *action_fifo_put(struct action_fifo *fifo)
2c8c4fb7
AZ
106{
107 if (fifo->head >= DEFERRED_ACTION_FIFO_SIZE - 1)
108 return NULL;
109
110 return &fifo->fifo[fifo->head++];
111}
112
e74d4817
PS
113/* Return queue entry if fifo is not full */
114static struct deferred_action *add_deferred_actions(struct sk_buff *skb,
f1f60b85 115 const struct sw_flow_key *key,
e74d4817 116 const struct nlattr *attr)
2c8c4fb7
AZ
117{
118 struct action_fifo *fifo;
119 struct deferred_action *da;
120
121 fifo = this_cpu_ptr(action_fifos);
122 da = action_fifo_put(fifo);
123 if (da) {
124 da->skb = skb;
125 da->actions = attr;
e74d4817 126 da->pkt_key = *key;
2c8c4fb7
AZ
127 }
128
e74d4817 129 return da;
e16138e2
PS
130}
131
e74d4817 132static void invalidate_flow_key(struct sw_flow_key *key)
e16138e2 133{
e74d4817 134 key->eth.type = htons(0);
e16138e2
PS
135}
136
f1f60b85 137static bool is_flow_key_valid(const struct sw_flow_key *key)
e16138e2 138{
e74d4817 139 return !!key->eth.type;
e16138e2
PS
140}
141
e74d4817 142static int push_mpls(struct sk_buff *skb, struct sw_flow_key *key,
ccf43786
SH
143 const struct ovs_action_push_mpls *mpls)
144{
145 __be32 *new_mpls_lse;
146 struct ethhdr *hdr;
147
2baf0e0c 148 /* Networking stack do not allow simultaneous Tunnel and MPLS GSO. */
8063e095 149 if (skb->encapsulation)
2baf0e0c
PS
150 return -ENOTSUPP;
151
ccf43786
SH
152 if (skb_cow_head(skb, MPLS_HLEN) < 0)
153 return -ENOMEM;
154
155 skb_push(skb, MPLS_HLEN);
156 memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
157 skb->mac_len);
158 skb_reset_mac_header(skb);
159
2baf0e0c 160 new_mpls_lse = (__be32 *)skb_mpls_header(skb);
ccf43786
SH
161 *new_mpls_lse = mpls->mpls_lse;
162
163 if (skb->ip_summed == CHECKSUM_COMPLETE)
164 skb->csum = csum_add(skb->csum, csum_partial(new_mpls_lse,
165 MPLS_HLEN, 0));
166
167 hdr = eth_hdr(skb);
168 hdr->h_proto = mpls->mpls_ethertype;
169 if (!ovs_skb_get_inner_protocol(skb))
170 ovs_skb_set_inner_protocol(skb, skb->protocol);
171 skb->protocol = mpls->mpls_ethertype;
2baf0e0c 172
e74d4817 173 invalidate_flow_key(key);
ccf43786
SH
174 return 0;
175}
176
e74d4817
PS
177static int pop_mpls(struct sk_buff *skb, struct sw_flow_key *key,
178 const __be16 ethertype)
ccf43786
SH
179{
180 struct ethhdr *hdr;
181 int err;
182
5cce04b6 183 err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
ccf43786
SH
184 if (unlikely(err))
185 return err;
186
f021a62f 187 skb_postpull_rcsum(skb, skb_mpls_header(skb), MPLS_HLEN);
ccf43786
SH
188
189 memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
190 skb->mac_len);
191
192 __skb_pull(skb, MPLS_HLEN);
193 skb_reset_mac_header(skb);
194
2baf0e0c 195 /* skb_mpls_header() is used to locate the ethertype
ccf43786
SH
196 * field correctly in the presence of VLAN tags.
197 */
2baf0e0c 198 hdr = (struct ethhdr *)(skb_mpls_header(skb) - ETH_HLEN);
ccf43786
SH
199 hdr->h_proto = ethertype;
200 if (eth_p_mpls(skb->protocol))
201 skb->protocol = ethertype;
2baf0e0c 202
e74d4817 203 invalidate_flow_key(key);
ccf43786
SH
204 return 0;
205}
206
b940b3d7
JR
207static int set_mpls(struct sk_buff *skb, struct sw_flow_key *flow_key,
208 const __be32 *mpls_lse, const __be32 *mask)
ccf43786 209{
2baf0e0c 210 __be32 *stack;
b940b3d7 211 __be32 lse;
ccf43786
SH
212 int err;
213
5cce04b6 214 err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
ccf43786
SH
215 if (unlikely(err))
216 return err;
217
2baf0e0c 218 stack = (__be32 *)skb_mpls_header(skb);
e281bb23 219 lse = OVS_MASKED(*stack, *mpls_lse, *mask);
ccf43786 220 if (skb->ip_summed == CHECKSUM_COMPLETE) {
b940b3d7
JR
221 __be32 diff[] = { ~(*stack), lse };
222
ccf43786
SH
223 skb->csum = ~csum_partial((char *)diff, sizeof(diff),
224 ~skb->csum);
225 }
226
b940b3d7
JR
227 *stack = lse;
228 flow_key->mpls.top_lse = lse;
ccf43786
SH
229 return 0;
230}
231
e74d4817 232static int pop_vlan(struct sk_buff *skb, struct sw_flow_key *key)
064af421 233{
d9065a90 234 int err;
10db8b20 235
97894370 236 err = skb_vlan_pop(skb);
efd8a18e 237 if (skb_vlan_tag_present(skb))
97894370
TG
238 invalidate_flow_key(key);
239 else
e74d4817 240 key->eth.tci = 0;
97894370 241 return err;
d9065a90
PS
242}
243
e74d4817
PS
244static int push_vlan(struct sk_buff *skb, struct sw_flow_key *key,
245 const struct ovs_action_push_vlan *vlan)
d9065a90 246{
efd8a18e 247 if (skb_vlan_tag_present(skb))
e74d4817 248 invalidate_flow_key(key);
97894370 249 else
e74d4817 250 key->eth.tci = vlan->vlan_tci;
97894370
TG
251 return skb_vlan_push(skb, vlan->vlan_tpid,
252 ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
064af421
BP
253}
254
b940b3d7
JR
255/* 'src' is already properly masked. */
256static void ether_addr_copy_masked(u8 *dst_, const u8 *src_, const u8 *mask_)
257{
258 u16 *dst = (u16 *)dst_;
259 const u16 *src = (const u16 *)src_;
260 const u16 *mask = (const u16 *)mask_;
261
e281bb23
JS
262 OVS_SET_MASKED(dst[0], src[0], mask[0]);
263 OVS_SET_MASKED(dst[1], src[1], mask[1]);
264 OVS_SET_MASKED(dst[2], src[2], mask[2]);
b940b3d7
JR
265}
266
267static int set_eth_addr(struct sk_buff *skb, struct sw_flow_key *flow_key,
268 const struct ovs_key_ethernet *key,
269 const struct ovs_key_ethernet *mask)
ca78c6b6 270{
4edb9ae9 271 int err;
b940b3d7 272
5cce04b6 273 err = skb_ensure_writable(skb, ETH_HLEN);
4edb9ae9
PS
274 if (unlikely(err))
275 return err;
276
237c4f2a 277 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
3cfede14 278
b940b3d7
JR
279 ether_addr_copy_masked(eth_hdr(skb)->h_source, key->eth_src,
280 mask->eth_src);
281 ether_addr_copy_masked(eth_hdr(skb)->h_dest, key->eth_dst,
282 mask->eth_dst);
4edb9ae9 283
237c4f2a 284 ovs_skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
3cfede14 285
b940b3d7
JR
286 ether_addr_copy(flow_key->eth.src, eth_hdr(skb)->h_source);
287 ether_addr_copy(flow_key->eth.dst, eth_hdr(skb)->h_dest);
4edb9ae9 288 return 0;
ca78c6b6
BP
289}
290
efdb0c9f
GG
291static void update_ip_l4_checksum(struct sk_buff *skb, struct iphdr *nh,
292 __be32 addr, __be32 new_addr)
ca78c6b6
BP
293{
294 int transport_len = skb->len - skb_transport_offset(skb);
4edb9ae9 295
efdb0c9f
GG
296 if (nh->frag_off & htons(IP_OFFSET))
297 return;
298
4edb9ae9 299 if (nh->protocol == IPPROTO_TCP) {
ca78c6b6 300 if (likely(transport_len >= sizeof(struct tcphdr)))
4edb9ae9 301 inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
8063e095 302 addr, new_addr, true);
4edb9ae9 303 } else if (nh->protocol == IPPROTO_UDP) {
55ce87bc
JG
304 if (likely(transport_len >= sizeof(struct udphdr))) {
305 struct udphdr *uh = udp_hdr(skb);
306
237c4f2a 307 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
55ce87bc 308 inet_proto_csum_replace4(&uh->check, skb,
8063e095 309 addr, new_addr, true);
55ce87bc
JG
310 if (!uh->check)
311 uh->check = CSUM_MANGLED_0;
312 }
313 }
ca78c6b6 314 }
4edb9ae9 315
efdb0c9f
GG
316}
317
318static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
319 __be32 *addr, __be32 new_addr)
320{
321 update_ip_l4_checksum(skb, nh, *addr, new_addr);
4edb9ae9 322 csum_replace4(&nh->check, *addr, new_addr);
e2f3178f 323 skb_clear_hash(skb);
4edb9ae9 324 *addr = new_addr;
ca78c6b6
BP
325}
326
bc7a5acd
AA
327static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto,
328 __be32 addr[4], const __be32 new_addr[4])
329{
330 int transport_len = skb->len - skb_transport_offset(skb);
331
00894212 332 if (l4_proto == NEXTHDR_TCP) {
bc7a5acd
AA
333 if (likely(transport_len >= sizeof(struct tcphdr)))
334 inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb,
8063e095 335 addr, new_addr, true);
00894212 336 } else if (l4_proto == NEXTHDR_UDP) {
bc7a5acd
AA
337 if (likely(transport_len >= sizeof(struct udphdr))) {
338 struct udphdr *uh = udp_hdr(skb);
339
237c4f2a 340 if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
bc7a5acd 341 inet_proto_csum_replace16(&uh->check, skb,
8063e095 342 addr, new_addr, true);
bc7a5acd
AA
343 if (!uh->check)
344 uh->check = CSUM_MANGLED_0;
345 }
346 }
00894212
JG
347 } else if (l4_proto == NEXTHDR_ICMP) {
348 if (likely(transport_len >= sizeof(struct icmp6hdr)))
349 inet_proto_csum_replace16(&icmp6_hdr(skb)->icmp6_cksum,
8063e095 350 skb, addr, new_addr, true);
bc7a5acd
AA
351 }
352}
353
b940b3d7
JR
354static void mask_ipv6_addr(const __be32 old[4], const __be32 addr[4],
355 const __be32 mask[4], __be32 masked[4])
356{
e281bb23
JS
357 masked[0] = OVS_MASKED(old[0], addr[0], mask[0]);
358 masked[1] = OVS_MASKED(old[1], addr[1], mask[1]);
359 masked[2] = OVS_MASKED(old[2], addr[2], mask[2]);
360 masked[3] = OVS_MASKED(old[3], addr[3], mask[3]);
b940b3d7
JR
361}
362
bc7a5acd
AA
363static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto,
364 __be32 addr[4], const __be32 new_addr[4],
365 bool recalculate_csum)
366{
51cf5e71 367 if (likely(recalculate_csum))
bc7a5acd
AA
368 update_ipv6_checksum(skb, l4_proto, addr, new_addr);
369
e2f3178f 370 skb_clear_hash(skb);
bc7a5acd
AA
371 memcpy(addr, new_addr, sizeof(__be32[4]));
372}
373
b940b3d7 374static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl, u32 mask)
bc7a5acd 375{
b940b3d7 376 /* Bits 21-24 are always unmasked, so this retains their values. */
e281bb23
JS
377 OVS_SET_MASKED(nh->flow_lbl[0], (u8)(fl >> 16), (u8)(mask >> 16));
378 OVS_SET_MASKED(nh->flow_lbl[1], (u8)(fl >> 8), (u8)(mask >> 8));
379 OVS_SET_MASKED(nh->flow_lbl[2], (u8)fl, (u8)mask);
bc7a5acd
AA
380}
381
b940b3d7
JR
382static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl,
383 u8 mask)
bc7a5acd 384{
e281bb23 385 new_ttl = OVS_MASKED(nh->ttl, new_ttl, mask);
bc7a5acd 386
a61680c6
JP
387 csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
388 nh->ttl = new_ttl;
389}
390
b940b3d7
JR
391static int set_ipv4(struct sk_buff *skb, struct sw_flow_key *flow_key,
392 const struct ovs_key_ipv4 *key,
393 const struct ovs_key_ipv4 *mask)
064af421 394{
ca78c6b6 395 struct iphdr *nh;
b940b3d7 396 __be32 new_addr;
10db8b20 397 int err;
ca78c6b6 398
5cce04b6
TG
399 err = skb_ensure_writable(skb, skb_network_offset(skb) +
400 sizeof(struct iphdr));
10db8b20
JG
401 if (unlikely(err))
402 return err;
ca78c6b6
BP
403
404 nh = ip_hdr(skb);
ca78c6b6 405
b940b3d7
JR
406 /* Setting an IP addresses is typically only a side effect of
407 * matching on them in the current userspace implementation, so it
408 * makes sense to check if the value actually changed.
409 */
410 if (mask->ipv4_src) {
e281bb23 411 new_addr = OVS_MASKED(nh->saddr, key->ipv4_src, mask->ipv4_src);
ca78c6b6 412
b940b3d7
JR
413 if (unlikely(new_addr != nh->saddr)) {
414 set_ip_addr(skb, nh, &nh->saddr, new_addr);
415 flow_key->ipv4.addr.src = new_addr;
416 }
e16138e2 417 }
b940b3d7 418 if (mask->ipv4_dst) {
e281bb23 419 new_addr = OVS_MASKED(nh->daddr, key->ipv4_dst, mask->ipv4_dst);
a4a26436 420
b940b3d7
JR
421 if (unlikely(new_addr != nh->daddr)) {
422 set_ip_addr(skb, nh, &nh->daddr, new_addr);
423 flow_key->ipv4.addr.dst = new_addr;
424 }
e16138e2 425 }
b940b3d7
JR
426 if (mask->ipv4_tos) {
427 ipv4_change_dsfield(nh, ~mask->ipv4_tos, key->ipv4_tos);
428 flow_key->ip.tos = nh->tos;
429 }
430 if (mask->ipv4_ttl) {
431 set_ip_ttl(skb, nh, key->ipv4_ttl, mask->ipv4_ttl);
432 flow_key->ip.ttl = nh->ttl;
e16138e2 433 }
a61680c6 434
10db8b20 435 return 0;
064af421
BP
436}
437
b940b3d7
JR
438static bool is_ipv6_mask_nonzero(const __be32 addr[4])
439{
440 return !!(addr[0] | addr[1] | addr[2] | addr[3]);
441}
442
443static int set_ipv6(struct sk_buff *skb, struct sw_flow_key *flow_key,
444 const struct ovs_key_ipv6 *key,
445 const struct ovs_key_ipv6 *mask)
bc7a5acd
AA
446{
447 struct ipv6hdr *nh;
448 int err;
bc7a5acd 449
5cce04b6
TG
450 err = skb_ensure_writable(skb, skb_network_offset(skb) +
451 sizeof(struct ipv6hdr));
bc7a5acd
AA
452 if (unlikely(err))
453 return err;
454
455 nh = ipv6_hdr(skb);
bc7a5acd 456
b940b3d7
JR
457 /* Setting an IP addresses is typically only a side effect of
458 * matching on them in the current userspace implementation, so it
459 * makes sense to check if the value actually changed.
460 */
461 if (is_ipv6_mask_nonzero(mask->ipv6_src)) {
462 __be32 *saddr = (__be32 *)&nh->saddr;
463 __be32 masked[4];
464
465 mask_ipv6_addr(saddr, key->ipv6_src, mask->ipv6_src, masked);
466
467 if (unlikely(memcmp(saddr, masked, sizeof(masked)))) {
49a8eef8 468 set_ipv6_addr(skb, flow_key->ip.proto, saddr, masked,
b940b3d7
JR
469 true);
470 memcpy(&flow_key->ipv6.addr.src, masked,
471 sizeof(flow_key->ipv6.addr.src));
472 }
473 }
474 if (is_ipv6_mask_nonzero(mask->ipv6_dst)) {
bc7a5acd 475 unsigned int offset = 0;
8abaa53c 476 int flags = IP6_FH_F_SKIP_RH;
bc7a5acd 477 bool recalc_csum = true;
b940b3d7
JR
478 __be32 *daddr = (__be32 *)&nh->daddr;
479 __be32 masked[4];
480
481 mask_ipv6_addr(daddr, key->ipv6_dst, mask->ipv6_dst, masked);
482
483 if (unlikely(memcmp(daddr, masked, sizeof(masked)))) {
484 if (ipv6_ext_hdr(nh->nexthdr))
485 recalc_csum = (ipv6_find_hdr(skb, &offset,
486 NEXTHDR_ROUTING,
487 NULL, &flags)
488 != NEXTHDR_ROUTING);
489
49a8eef8 490 set_ipv6_addr(skb, flow_key->ip.proto, daddr, masked,
b940b3d7
JR
491 recalc_csum);
492 memcpy(&flow_key->ipv6.addr.dst, masked,
493 sizeof(flow_key->ipv6.addr.dst));
494 }
495 }
496 if (mask->ipv6_tclass) {
497 ipv6_change_dsfield(nh, ~mask->ipv6_tclass, key->ipv6_tclass);
498 flow_key->ip.tos = ipv6_get_dsfield(nh);
499 }
500 if (mask->ipv6_label) {
501 set_ipv6_fl(nh, ntohl(key->ipv6_label),
502 ntohl(mask->ipv6_label));
503 flow_key->ipv6.label =
504 *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
505 }
506 if (mask->ipv6_hlimit) {
e281bb23
JS
507 OVS_SET_MASKED(nh->hop_limit, key->ipv6_hlimit,
508 mask->ipv6_hlimit);
b940b3d7 509 flow_key->ip.ttl = nh->hop_limit;
bc7a5acd 510 }
bc7a5acd
AA
511 return 0;
512}
513
5cce04b6 514/* Must follow skb_ensure_writable() since that can move the skb data. */
4edb9ae9 515static void set_tp_port(struct sk_buff *skb, __be16 *port,
b940b3d7 516 __be16 new_port, __sum16 *check)
959a2ecd 517{
8063e095 518 inet_proto_csum_replace2(check, skb, *port, new_port, false);
4edb9ae9 519 *port = new_port;
55ce87bc
JG
520}
521
b940b3d7
JR
522static int set_udp(struct sk_buff *skb, struct sw_flow_key *flow_key,
523 const struct ovs_key_udp *key,
524 const struct ovs_key_udp *mask)
4edb9ae9
PS
525{
526 struct udphdr *uh;
b940b3d7 527 __be16 src, dst;
4edb9ae9 528 int err;
10db8b20 529
5cce04b6
TG
530 err = skb_ensure_writable(skb, skb_transport_offset(skb) +
531 sizeof(struct udphdr));
10db8b20
JG
532 if (unlikely(err))
533 return err;
534
4edb9ae9 535 uh = udp_hdr(skb);
b940b3d7 536 /* Either of the masks is non-zero, so do not bother checking them. */
e281bb23
JS
537 src = OVS_MASKED(uh->source, key->udp_src, mask->udp_src);
538 dst = OVS_MASKED(uh->dest, key->udp_dst, mask->udp_dst);
4edb9ae9 539
b940b3d7
JR
540 if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) {
541 if (likely(src != uh->source)) {
542 set_tp_port(skb, &uh->source, src, &uh->check);
543 flow_key->tp.src = src;
544 }
545 if (likely(dst != uh->dest)) {
546 set_tp_port(skb, &uh->dest, dst, &uh->check);
547 flow_key->tp.dst = dst;
548 }
549
550 if (unlikely(!uh->check))
551 uh->check = CSUM_MANGLED_0;
552 } else {
553 uh->source = src;
554 uh->dest = dst;
555 flow_key->tp.src = src;
556 flow_key->tp.dst = dst;
e16138e2 557 }
10db8b20 558
b940b3d7
JR
559 skb_clear_hash(skb);
560
10db8b20 561 return 0;
959a2ecd
JP
562}
563
b940b3d7
JR
564static int set_tcp(struct sk_buff *skb, struct sw_flow_key *flow_key,
565 const struct ovs_key_tcp *key,
566 const struct ovs_key_tcp *mask)
064af421 567{
4edb9ae9 568 struct tcphdr *th;
b940b3d7 569 __be16 src, dst;
10db8b20 570 int err;
064af421 571
5cce04b6
TG
572 err = skb_ensure_writable(skb, skb_transport_offset(skb) +
573 sizeof(struct tcphdr));
10db8b20
JG
574 if (unlikely(err))
575 return err;
ca78c6b6 576
4edb9ae9 577 th = tcp_hdr(skb);
e281bb23 578 src = OVS_MASKED(th->source, key->tcp_src, mask->tcp_src);
b940b3d7
JR
579 if (likely(src != th->source)) {
580 set_tp_port(skb, &th->source, src, &th->check);
581 flow_key->tp.src = src;
582 }
e281bb23 583 dst = OVS_MASKED(th->dest, key->tcp_dst, mask->tcp_dst);
b940b3d7
JR
584 if (likely(dst != th->dest)) {
585 set_tp_port(skb, &th->dest, dst, &th->check);
586 flow_key->tp.dst = dst;
e16138e2 587 }
b940b3d7 588 skb_clear_hash(skb);
ca78c6b6 589
10db8b20 590 return 0;
064af421
BP
591}
592
b940b3d7
JR
593static int set_sctp(struct sk_buff *skb, struct sw_flow_key *flow_key,
594 const struct ovs_key_sctp *key,
595 const struct ovs_key_sctp *mask)
10f72e3d 596{
b940b3d7 597 unsigned int sctphoff = skb_transport_offset(skb);
10f72e3d 598 struct sctphdr *sh;
b940b3d7 599 __le32 old_correct_csum, new_csum, old_csum;
10f72e3d 600 int err;
10f72e3d 601
5cce04b6 602 err = skb_ensure_writable(skb, sctphoff + sizeof(struct sctphdr));
10f72e3d
JS
603 if (unlikely(err))
604 return err;
605
606 sh = sctp_hdr(skb);
b940b3d7
JR
607 old_csum = sh->checksum;
608 old_correct_csum = sctp_compute_cksum(skb, sctphoff);
10f72e3d 609
e281bb23
JS
610 sh->source = OVS_MASKED(sh->source, key->sctp_src, mask->sctp_src);
611 sh->dest = OVS_MASKED(sh->dest, key->sctp_dst, mask->sctp_dst);
10f72e3d 612
b940b3d7 613 new_csum = sctp_compute_cksum(skb, sctphoff);
10f72e3d 614
b940b3d7
JR
615 /* Carry any checksum errors through. */
616 sh->checksum = old_csum ^ old_correct_csum ^ new_csum;
10f72e3d 617
b940b3d7
JR
618 skb_clear_hash(skb);
619 flow_key->tp.src = sh->source;
620 flow_key->tp.dst = sh->dest;
10f72e3d
JS
621
622 return 0;
623}
624
a94ebc39
JS
625static int ovs_vport_output(OVS_VPORT_OUTPUT_PARAMS)
626{
627 struct ovs_frag_data *data = get_pcpu_ptr(ovs_frag_data_storage);
628 struct vport *vport = data->vport;
629
630 if (skb_cow_head(skb, data->l2_len) < 0) {
631 kfree_skb(skb);
632 return -ENOMEM;
633 }
634
635 __skb_dst_copy(skb, data->dst);
86c2eb45 636 *OVS_GSO_CB(skb) = data->cb;
a94ebc39
JS
637 ovs_skb_set_inner_protocol(skb, data->inner_protocol);
638 skb->vlan_tci = data->vlan_tci;
639 skb->vlan_proto = data->vlan_proto;
640
641 /* Reconstruct the MAC header. */
642 skb_push(skb, data->l2_len);
643 memcpy(skb->data, &data->l2_data, data->l2_len);
644 ovs_skb_postpush_rcsum(skb, skb->data, data->l2_len);
645 skb_reset_mac_header(skb);
646
647 ovs_vport_send(vport, skb);
648 return 0;
649}
650
651static unsigned int
652ovs_dst_get_mtu(const struct dst_entry *dst)
653{
654 return dst->dev->mtu;
655}
656
657static struct dst_ops ovs_dst_ops = {
658 .family = AF_UNSPEC,
659 .mtu = ovs_dst_get_mtu,
660};
661
662/* prepare_frag() is called once per (larger-than-MTU) frame; its inverse is
663 * ovs_vport_output(), which is called once per fragmented packet.
664 */
665static void prepare_frag(struct vport *vport, struct sk_buff *skb)
666{
667 unsigned int hlen = skb_network_offset(skb);
668 struct ovs_frag_data *data;
669
670 data = get_pcpu_ptr(ovs_frag_data_storage);
671 data->dst = (unsigned long) skb_dst(skb);
672 data->vport = vport;
86c2eb45 673 data->cb = *OVS_GSO_CB(skb);
a94ebc39
JS
674 data->inner_protocol = ovs_skb_get_inner_protocol(skb);
675 data->vlan_tci = skb->vlan_tci;
676 data->vlan_proto = skb->vlan_proto;
677 data->l2_len = hlen;
678 memcpy(&data->l2_data, skb->data, hlen);
679
680 memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
681 skb_pull(skb, hlen);
682}
683
684static void ovs_fragment(struct vport *vport, struct sk_buff *skb, u16 mru,
685 __be16 ethertype)
686{
687 if (skb_network_offset(skb) > MAX_L2_LEN) {
688 OVS_NLERR(1, "L2 header too long to fragment");
c05e2094 689 goto err;
a94ebc39
JS
690 }
691
692 if (ethertype == htons(ETH_P_IP)) {
693 struct dst_entry ovs_dst;
694 unsigned long orig_dst;
695
696 prepare_frag(vport, skb);
697 dst_init(&ovs_dst, &ovs_dst_ops, NULL, 1,
698 DST_OBSOLETE_NONE, DST_NOCOUNT);
699 ovs_dst.dev = vport->dev;
700
701 orig_dst = (unsigned long) skb_dst(skb);
702 skb_dst_set_noref(skb, &ovs_dst);
703 IPCB(skb)->frag_max_size = mru;
704
705 ip_do_fragment(skb->sk, skb, ovs_vport_output);
706 refdst_drop(orig_dst);
707 } else if (ethertype == htons(ETH_P_IPV6)) {
708 const struct nf_ipv6_ops *v6ops = nf_get_ipv6_ops();
709 unsigned long orig_dst;
710 struct rt6_info ovs_rt;
711
712 if (!v6ops) {
c05e2094 713 goto err;
a94ebc39
JS
714 }
715
716 prepare_frag(vport, skb);
717 memset(&ovs_rt, 0, sizeof(ovs_rt));
718 dst_init(&ovs_rt.dst, &ovs_dst_ops, NULL, 1,
719 DST_OBSOLETE_NONE, DST_NOCOUNT);
720 ovs_rt.dst.dev = vport->dev;
721
722 orig_dst = (unsigned long) skb_dst(skb);
723 skb_dst_set_noref(skb, &ovs_rt.dst);
724 IP6CB(skb)->frag_max_size = mru;
725
726 v6ops->fragment(skb->sk, skb, ovs_vport_output);
727 refdst_drop(orig_dst);
728 } else {
729 WARN_ONCE(1, "Failed fragment ->%s: eth=%04x, MRU=%d, MTU=%d.",
730 ovs_vport_name(vport), ntohs(ethertype), mru,
731 vport->dev->mtu);
c05e2094 732 goto err;
a94ebc39 733 }
c05e2094
JS
734
735 return;
736err:
737 kfree_skb(skb);
a94ebc39 738}
a94ebc39
JS
739
740static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port,
741 struct sw_flow_key *key)
064af421 742{
fe90efd9 743 struct vport *vport = ovs_vport_rcu(dp, out_port);
064af421 744
a94ebc39
JS
745 if (likely(vport)) {
746 u16 mru = OVS_CB(skb)->mru;
4c7804f1
WT
747 u32 cutlen = OVS_CB(skb)->cutlen;
748
749 if (unlikely(cutlen > 0)) {
750 if (skb->len - cutlen > ETH_HLEN)
751 pskb_trim(skb, skb->len - cutlen);
752 else
753 pskb_trim(skb, ETH_HLEN);
754 }
a94ebc39
JS
755
756 if (likely(!mru || (skb->len <= mru + ETH_HLEN))) {
757 ovs_vport_send(vport, skb);
758 } else if (mru <= vport->dev->mtu) {
759 __be16 ethertype = key->eth.type;
760
761 if (!is_flow_key_valid(key)) {
762 if (eth_p_mpls(skb->protocol))
763 ethertype = ovs_skb_get_inner_protocol(skb);
764 else
765 ethertype = vlan_get_protocol(skb);
766 }
767
768 ovs_fragment(vport, skb, mru, ethertype);
769 } else {
770 OVS_NLERR(true, "Cannot fragment IP frames");
771 kfree_skb(skb);
772 }
773 } else {
f15c8639 774 kfree_skb(skb);
a94ebc39 775 }
064af421 776}
98403001 777static int output_userspace(struct datapath *dp, struct sk_buff *skb,
0e469d3b 778 struct sw_flow_key *key, const struct nlattr *attr,
4c7804f1
WT
779 const struct nlattr *actions, int actions_len,
780 uint32_t cutlen)
064af421 781{
e23775f2 782 struct ip_tunnel_info info;
856081f6 783 struct dp_upcall_info upcall;
98403001
BP
784 const struct nlattr *a;
785 int rem;
856081f6 786
0e469d3b 787 memset(&upcall, 0, sizeof(upcall));
df2c07f4 788 upcall.cmd = OVS_PACKET_CMD_ACTION;
a94ebc39 789 upcall.mru = OVS_CB(skb)->mru;
98403001
BP
790
791 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
792 a = nla_next(a, &rem)) {
793 switch (nla_type(a)) {
794 case OVS_USERSPACE_ATTR_USERDATA:
795 upcall.userdata = a;
796 break;
797
798 case OVS_USERSPACE_ATTR_PID:
28aea917 799 upcall.portid = nla_get_u32(a);
98403001 800 break;
8b7ea2d4
WZ
801
802 case OVS_USERSPACE_ATTR_EGRESS_TUN_PORT: {
803 /* Get out tunnel info. */
804 struct vport *vport;
805
806 vport = ovs_vport_rcu(dp, nla_get_u32(a));
807 if (vport) {
808 int err;
809
e23775f2 810 upcall.egress_tun_info = &info;
8b7ea2d4 811 err = ovs_vport_get_egress_tun_info(vport, skb,
e23775f2
PS
812 &upcall);
813 if (err)
814 upcall.egress_tun_info = NULL;
8b7ea2d4 815 }
e23775f2 816
8b7ea2d4 817 break;
98403001 818 }
8b7ea2d4 819
0e469d3b
NM
820 case OVS_USERSPACE_ATTR_ACTIONS: {
821 /* Include actions. */
822 upcall.actions = actions;
823 upcall.actions_len = actions_len;
824 break;
825 }
826
8b7ea2d4 827 } /* End of switch. */
98403001
BP
828 }
829
4c7804f1 830 return ovs_dp_upcall(dp, skb, key, &upcall, cutlen);
064af421
BP
831}
832
6ff686f2 833static int sample(struct datapath *dp, struct sk_buff *skb,
0e469d3b
NM
834 struct sw_flow_key *key, const struct nlattr *attr,
835 const struct nlattr *actions, int actions_len)
6ff686f2
PS
836{
837 const struct nlattr *acts_list = NULL;
838 const struct nlattr *a;
839 int rem;
4c7804f1 840 u32 cutlen = 0;
6ff686f2
PS
841
842 for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
843 a = nla_next(a, &rem)) {
c02c4967
WZ
844 u32 probability;
845
6ff686f2
PS
846 switch (nla_type(a)) {
847 case OVS_SAMPLE_ATTR_PROBABILITY:
c02c4967
WZ
848 probability = nla_get_u32(a);
849 if (!probability || prandom_u32() > probability)
6ff686f2
PS
850 return 0;
851 break;
852
853 case OVS_SAMPLE_ATTR_ACTIONS:
854 acts_list = a;
855 break;
856 }
857 }
858
fbf4f74d
SH
859 rem = nla_len(acts_list);
860 a = nla_data(acts_list);
861
d7ff93d7
AZ
862 /* Actions list is empty, do nothing */
863 if (unlikely(!rem))
864 return 0;
e16138e2 865
d7ff93d7 866 /* The only known usage of sample action is having a single user-space
4c7804f1 867 * action, or having a truncate action followed by a single user-space
d7ff93d7
AZ
868 * action. Treat this usage as a special case.
869 * The output_userspace() should clone the skb to be sent to the
e74d4817
PS
870 * user space. This skb will be consumed by its caller.
871 */
4c7804f1
WT
872 if (unlikely(nla_type(a) == OVS_ACTION_ATTR_TRUNC)) {
873 struct ovs_action_trunc *trunc = nla_data(a);
874
875 if (skb->len > trunc->max_len)
876 cutlen = skb->len - trunc->max_len;
877
878 a = nla_next(a, &rem);
879 }
880
d7ff93d7 881 if (likely(nla_type(a) == OVS_ACTION_ATTR_USERSPACE &&
684b5f5d 882 nla_is_last(a, rem)))
4c7804f1
WT
883 return output_userspace(dp, skb, key, a, actions,
884 actions_len, cutlen);
d7ff93d7
AZ
885
886 skb = skb_clone(skb, GFP_ATOMIC);
887 if (!skb)
888 /* Skip the sample action when out of memory. */
889 return 0;
890
e74d4817 891 if (!add_deferred_actions(skb, key, a)) {
2c8c4fb7
AZ
892 if (net_ratelimit())
893 pr_warn("%s: deferred actions limit reached, dropping sample action\n",
894 ovs_dp_name(dp));
fbf4f74d 895
2c8c4fb7
AZ
896 kfree_skb(skb);
897 }
2c8c4fb7 898 return 0;
6ff686f2
PS
899}
900
e74d4817
PS
901static void execute_hash(struct sk_buff *skb, struct sw_flow_key *key,
902 const struct nlattr *attr)
7804df20 903{
7804df20
AZ
904 struct ovs_action_hash *hash_act = nla_data(attr);
905 u32 hash = 0;
906
907 /* OVS_HASH_ALG_L4 is the only possible hash algorithm. */
e2f3178f 908 hash = skb_get_hash(skb);
7804df20
AZ
909 hash = jhash_1word(hash, hash_act->hash_basis);
910 if (!hash)
911 hash = 0x1;
912
913 key->ovs_flow_hash = hash;
914}
915
b940b3d7
JR
916static int execute_set_action(struct sk_buff *skb,
917 struct sw_flow_key *flow_key,
918 const struct nlattr *a)
919{
920 /* Only tunnel set execution is supported without a mask. */
921 if (nla_type(a) == OVS_KEY_ATTR_TUNNEL_INFO) {
e23775f2
PS
922 struct ovs_tunnel_info *tun = nla_data(a);
923
924 ovs_skb_dst_drop(skb);
925 ovs_dst_hold((struct dst_entry *)tun->tun_dst);
926 ovs_skb_dst_set(skb, (struct dst_entry *)tun->tun_dst);
b940b3d7
JR
927 return 0;
928 }
929
930 return -EINVAL;
b940b3d7
JR
931}
932
933/* Mask is at the midpoint of the data. */
934#define get_mask(a, type) ((const type)nla_data(a) + 1)
935
936static int execute_masked_set_action(struct sk_buff *skb,
937 struct sw_flow_key *flow_key,
938 const struct nlattr *a)
4edb9ae9 939{
15c39847 940 int err = 0;
4edb9ae9 941
b940b3d7 942 switch (nla_type(a)) {
abff858b 943 case OVS_KEY_ATTR_PRIORITY:
e281bb23
JS
944 OVS_SET_MASKED(skb->priority, nla_get_u32(a),
945 *get_mask(a, u32 *));
b940b3d7 946 flow_key->phy.priority = skb->priority;
abff858b
PS
947 break;
948
72e8bf28 949 case OVS_KEY_ATTR_SKB_MARK:
e281bb23 950 OVS_SET_MASKED(skb->mark, nla_get_u32(a), *get_mask(a, u32 *));
b940b3d7 951 flow_key->phy.skb_mark = skb->mark;
72e8bf28
AA
952 break;
953
f0cd669f 954 case OVS_KEY_ATTR_TUNNEL_INFO:
b940b3d7
JR
955 /* Masked data not supported for tunnel. */
956 err = -EINVAL;
4edb9ae9
PS
957 break;
958
959 case OVS_KEY_ATTR_ETHERNET:
b940b3d7
JR
960 err = set_eth_addr(skb, flow_key, nla_data(a),
961 get_mask(a, struct ovs_key_ethernet *));
4edb9ae9
PS
962 break;
963
964 case OVS_KEY_ATTR_IPV4:
b940b3d7
JR
965 err = set_ipv4(skb, flow_key, nla_data(a),
966 get_mask(a, struct ovs_key_ipv4 *));
4edb9ae9
PS
967 break;
968
bc7a5acd 969 case OVS_KEY_ATTR_IPV6:
b940b3d7
JR
970 err = set_ipv6(skb, flow_key, nla_data(a),
971 get_mask(a, struct ovs_key_ipv6 *));
bc7a5acd
AA
972 break;
973
4edb9ae9 974 case OVS_KEY_ATTR_TCP:
b940b3d7
JR
975 err = set_tcp(skb, flow_key, nla_data(a),
976 get_mask(a, struct ovs_key_tcp *));
4edb9ae9
PS
977 break;
978
979 case OVS_KEY_ATTR_UDP:
b940b3d7
JR
980 err = set_udp(skb, flow_key, nla_data(a),
981 get_mask(a, struct ovs_key_udp *));
4edb9ae9 982 break;
10f72e3d
JS
983
984 case OVS_KEY_ATTR_SCTP:
b940b3d7
JR
985 err = set_sctp(skb, flow_key, nla_data(a),
986 get_mask(a, struct ovs_key_sctp *));
10f72e3d 987 break;
ccf43786
SH
988
989 case OVS_KEY_ATTR_MPLS:
b940b3d7
JR
990 err = set_mpls(skb, flow_key, nla_data(a), get_mask(a,
991 __be32 *));
ccf43786 992 break;
a94ebc39
JS
993
994 case OVS_KEY_ATTR_CT_STATE:
995 case OVS_KEY_ATTR_CT_ZONE:
372ce973 996 case OVS_KEY_ATTR_CT_MARK:
c05e2094 997 case OVS_KEY_ATTR_CT_LABELS:
a94ebc39
JS
998 err = -EINVAL;
999 break;
4edb9ae9 1000 }
15c39847 1001
4edb9ae9
PS
1002 return err;
1003}
1004
a6059080 1005static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
7d16c847
PS
1006 struct sw_flow_key *key,
1007 const struct nlattr *a, int rem)
a6059080 1008{
e74d4817
PS
1009 struct deferred_action *da;
1010
1011 if (!is_flow_key_valid(key)) {
867e37ba
AZ
1012 int err;
1013
e74d4817 1014 err = ovs_flow_key_update(skb, key);
867e37ba
AZ
1015 if (err)
1016 return err;
867e37ba 1017 }
e74d4817 1018 BUG_ON(!is_flow_key_valid(key));
a6059080 1019
684b5f5d 1020 if (!nla_is_last(a, rem)) {
e16138e2 1021 /* Recirc action is the not the last action
e74d4817
PS
1022 * of the action list, need to clone the skb.
1023 */
e16138e2
PS
1024 skb = skb_clone(skb, GFP_ATOMIC);
1025
1026 /* Skip the recirc action when out of memory, but
e74d4817
PS
1027 * continue on with the rest of the action list.
1028 */
e16138e2
PS
1029 if (!skb)
1030 return 0;
2c8c4fb7 1031 }
a6059080 1032
e74d4817
PS
1033 da = add_deferred_actions(skb, key, NULL);
1034 if (da) {
1035 da->pkt_key.recirc_id = nla_get_u32(a);
2c8c4fb7
AZ
1036 } else {
1037 kfree_skb(skb);
1038
1039 if (net_ratelimit())
1040 pr_warn("%s: deferred action limit reached, drop recirc action\n",
1041 ovs_dp_name(dp));
867e37ba 1042 }
a6059080
AZ
1043
1044 return 0;
1045}
1046
064af421 1047/* Execute a list of actions against 'skb'. */
871dfe07 1048static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
e74d4817
PS
1049 struct sw_flow_key *key,
1050 const struct nlattr *attr, int len)
064af421
BP
1051{
1052 /* Every output action needs a separate clone of 'skb', but the common
1053 * case is just a single output action, so that doing a clone and
1054 * then freeing the original skbuff is wasteful. So the following code
e74d4817
PS
1055 * is slightly obscure just to avoid that.
1056 */
064af421 1057 int prev_port = -1;
cdee00fd 1058 const struct nlattr *a;
10db8b20 1059 int rem;
72b06300 1060
6ff686f2 1061 for (a = attr, rem = len; rem > 0;
a4af2475 1062 a = nla_next(a, &rem)) {
10db8b20
JG
1063 int err = 0;
1064
fe90efd9
AZ
1065 if (unlikely(prev_port != -1)) {
1066 struct sk_buff *out_skb = skb_clone(skb, GFP_ATOMIC);
1067
1068 if (out_skb)
a94ebc39 1069 do_output(dp, out_skb, prev_port, key);
fe90efd9 1070
4c7804f1 1071 OVS_CB(skb)->cutlen = 0;
064af421
BP
1072 prev_port = -1;
1073 }
1074
cdee00fd 1075 switch (nla_type(a)) {
df2c07f4 1076 case OVS_ACTION_ATTR_OUTPUT:
cdee00fd 1077 prev_port = nla_get_u32(a);
064af421
BP
1078 break;
1079
4c7804f1
WT
1080 case OVS_ACTION_ATTR_TRUNC: {
1081 struct ovs_action_trunc *trunc = nla_data(a);
1082
1083 if (skb->len > trunc->max_len)
1084 OVS_CB(skb)->cutlen = skb->len - trunc->max_len;
1085 break;
1086 }
1087
df2c07f4 1088 case OVS_ACTION_ATTR_USERSPACE:
4c7804f1
WT
1089 output_userspace(dp, skb, key, a, attr,
1090 len, OVS_CB(skb)->cutlen);
1091 OVS_CB(skb)->cutlen = 0;
064af421 1092 break;
7804df20
AZ
1093
1094 case OVS_ACTION_ATTR_HASH:
e74d4817 1095 execute_hash(skb, key, a);
7804df20 1096 break;
064af421 1097
ccf43786 1098 case OVS_ACTION_ATTR_PUSH_MPLS:
e74d4817 1099 err = push_mpls(skb, key, nla_data(a));
ccf43786
SH
1100 break;
1101
1102 case OVS_ACTION_ATTR_POP_MPLS:
e74d4817 1103 err = pop_mpls(skb, key, nla_get_be16(a));
ccf43786
SH
1104 break;
1105
fea393b1 1106 case OVS_ACTION_ATTR_PUSH_VLAN:
e74d4817 1107 err = push_vlan(skb, key, nla_data(a));
064af421
BP
1108 break;
1109
fea393b1 1110 case OVS_ACTION_ATTR_POP_VLAN:
e74d4817 1111 err = pop_vlan(skb, key);
064af421
BP
1112 break;
1113
e16138e2 1114 case OVS_ACTION_ATTR_RECIRC:
e74d4817 1115 err = execute_recirc(dp, skb, key, a, rem);
684b5f5d 1116 if (nla_is_last(a, rem)) {
867e37ba
AZ
1117 /* If this is the last action, the skb has
1118 * been consumed or freed.
e74d4817
PS
1119 * Return immediately.
1120 */
867e37ba
AZ
1121 return err;
1122 }
a6059080 1123 break;
a6059080 1124
4edb9ae9 1125 case OVS_ACTION_ATTR_SET:
e74d4817 1126 err = execute_set_action(skb, key, nla_data(a));
064af421 1127 break;
c1c9c9c4 1128
b940b3d7
JR
1129 case OVS_ACTION_ATTR_SET_MASKED:
1130 case OVS_ACTION_ATTR_SET_TO_MASKED:
1131 err = execute_masked_set_action(skb, key, nla_data(a));
1132 break;
1133
6ff686f2 1134 case OVS_ACTION_ATTR_SAMPLE:
0e469d3b 1135 err = sample(dp, skb, key, a, attr, len);
6ff686f2 1136 break;
a94ebc39
JS
1137
1138 case OVS_ACTION_ATTR_CT:
c05e2094
JS
1139 if (!is_flow_key_valid(key)) {
1140 err = ovs_flow_key_update(skb, key);
1141 if (err)
1142 return err;
1143 }
1144
a94ebc39
JS
1145 err = ovs_ct_execute(ovs_dp_get_net(dp), skb, key,
1146 nla_data(a));
1147
1148 /* Hide stolen IP fragments from user space. */
c05e2094
JS
1149 if (err)
1150 return err == -EINPROGRESS ? 0 : err;
a94ebc39 1151 break;
6ff686f2 1152 }
15c39847 1153
10db8b20
JG
1154 if (unlikely(err)) {
1155 kfree_skb(skb);
1156 return err;
1157 }
064af421 1158 }
6c222e55 1159
fbf4f74d 1160 if (prev_port != -1)
a94ebc39 1161 do_output(dp, skb, prev_port, key);
fbf4f74d 1162 else
5b95ab0e 1163 consume_skb(skb);
10db8b20 1164
a5225dd6 1165 return 0;
064af421 1166}
871dfe07 1167
2c8c4fb7
AZ
1168static void process_deferred_actions(struct datapath *dp)
1169{
1170 struct action_fifo *fifo = this_cpu_ptr(action_fifos);
1171
1172 /* Do not touch the FIFO in case there is no deferred actions. */
1173 if (action_fifo_is_empty(fifo))
1174 return;
1175
1176 /* Finishing executing all deferred actions. */
1177 do {
1178 struct deferred_action *da = action_fifo_get(fifo);
1179 struct sk_buff *skb = da->skb;
7d16c847 1180 struct sw_flow_key *key = &da->pkt_key;
2c8c4fb7
AZ
1181 const struct nlattr *actions = da->actions;
1182
1183 if (actions)
7d16c847 1184 do_execute_actions(dp, skb, key, actions,
2c8c4fb7
AZ
1185 nla_len(actions));
1186 else
7d16c847 1187 ovs_dp_process_packet(skb, key);
2c8c4fb7
AZ
1188 } while (!action_fifo_is_empty(fifo));
1189
1190 /* Reset FIFO for the next packet. */
1191 action_fifo_init(fifo);
1192}
1193
871dfe07 1194/* Execute a list of actions against 'skb'. */
2c8c4fb7 1195int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
7d16c847
PS
1196 const struct sw_flow_actions *acts,
1197 struct sw_flow_key *key)
2c8c4fb7
AZ
1198{
1199 int level = this_cpu_read(exec_actions_level);
1200 int err;
1201
1202 if (unlikely(level >= EXEC_ACTIONS_LEVEL_LIMIT)) {
1203 if (net_ratelimit())
1204 pr_warn("%s: packet loop detected, dropping.\n",
1205 ovs_dp_name(dp));
1206
1207 kfree_skb(skb);
1208 return -ELOOP;
1209 }
1210
1211 this_cpu_inc(exec_actions_level);
7d16c847
PS
1212 err = do_execute_actions(dp, skb, key,
1213 acts->actions, acts->actions_len);
2c8c4fb7
AZ
1214
1215 if (!level)
1216 process_deferred_actions(dp);
1217
1218 this_cpu_dec(exec_actions_level);
1219
1220 /* This return status currently does not reflect the errors
1221 * encounted during deferred actions execution. Probably needs to
e74d4817
PS
1222 * be fixed in the future.
1223 */
2c8c4fb7
AZ
1224 return err;
1225}
1226
1227int action_fifos_init(void)
1228{
1229 action_fifos = alloc_percpu(struct action_fifo);
1230 if (!action_fifos)
1231 return -ENOMEM;
1232
1233 return 0;
1234}
1235
1236void action_fifos_exit(void)
60759b2b 1237{
2c8c4fb7 1238 free_percpu(action_fifos);
871dfe07 1239}