Linux-libre 3.18.37-gnu
[librecmc/linux-libre.git] / net / openvswitch / actions.c
1 /*
2  * Copyright (c) 2007-2014 Nicira, Inc.
3  *
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
17  */
18
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21 #include <linux/skbuff.h>
22 #include <linux/in.h>
23 #include <linux/ip.h>
24 #include <linux/openvswitch.h>
25 #include <linux/sctp.h>
26 #include <linux/tcp.h>
27 #include <linux/udp.h>
28 #include <linux/in6.h>
29 #include <linux/if_arp.h>
30 #include <linux/if_vlan.h>
31 #include <net/ip.h>
32 #include <net/ipv6.h>
33 #include <net/checksum.h>
34 #include <net/dsfield.h>
35 #include <net/sctp/checksum.h>
36
37 #include "datapath.h"
38 #include "flow.h"
39 #include "vport.h"
40
41 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
42                               struct sw_flow_key *key,
43                               const struct nlattr *attr, int len);
44
45 struct deferred_action {
46         struct sk_buff *skb;
47         const struct nlattr *actions;
48
49         /* Store pkt_key clone when creating deferred action. */
50         struct sw_flow_key pkt_key;
51 };
52
53 #define DEFERRED_ACTION_FIFO_SIZE 10
54 struct action_fifo {
55         int head;
56         int tail;
57         /* Deferred action fifo queue storage. */
58         struct deferred_action fifo[DEFERRED_ACTION_FIFO_SIZE];
59 };
60
61 static struct action_fifo __percpu *action_fifos;
62 static DEFINE_PER_CPU(int, exec_actions_level);
63
64 static void action_fifo_init(struct action_fifo *fifo)
65 {
66         fifo->head = 0;
67         fifo->tail = 0;
68 }
69
70 static bool action_fifo_is_empty(struct action_fifo *fifo)
71 {
72         return (fifo->head == fifo->tail);
73 }
74
75 static struct deferred_action *action_fifo_get(struct action_fifo *fifo)
76 {
77         if (action_fifo_is_empty(fifo))
78                 return NULL;
79
80         return &fifo->fifo[fifo->tail++];
81 }
82
83 static struct deferred_action *action_fifo_put(struct action_fifo *fifo)
84 {
85         if (fifo->head >= DEFERRED_ACTION_FIFO_SIZE - 1)
86                 return NULL;
87
88         return &fifo->fifo[fifo->head++];
89 }
90
91 /* Return true if fifo is not full */
92 static struct deferred_action *add_deferred_actions(struct sk_buff *skb,
93                                                     struct sw_flow_key *key,
94                                                     const struct nlattr *attr)
95 {
96         struct action_fifo *fifo;
97         struct deferred_action *da;
98
99         fifo = this_cpu_ptr(action_fifos);
100         da = action_fifo_put(fifo);
101         if (da) {
102                 da->skb = skb;
103                 da->actions = attr;
104                 da->pkt_key = *key;
105         }
106
107         return da;
108 }
109
110 static int make_writable(struct sk_buff *skb, int write_len)
111 {
112         if (!pskb_may_pull(skb, write_len))
113                 return -ENOMEM;
114
115         if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
116                 return 0;
117
118         return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
119 }
120
121 /* remove VLAN header from packet and update csum accordingly. */
122 static int __pop_vlan_tci(struct sk_buff *skb, __be16 *current_tci)
123 {
124         struct vlan_hdr *vhdr;
125         int err;
126
127         err = make_writable(skb, VLAN_ETH_HLEN);
128         if (unlikely(err))
129                 return err;
130
131         if (skb->ip_summed == CHECKSUM_COMPLETE)
132                 skb->csum = csum_sub(skb->csum, csum_partial(skb->data
133                                         + (2 * ETH_ALEN), VLAN_HLEN, 0));
134
135         vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
136         *current_tci = vhdr->h_vlan_TCI;
137
138         memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
139         __skb_pull(skb, VLAN_HLEN);
140
141         vlan_set_encap_proto(skb, vhdr);
142         skb->mac_header += VLAN_HLEN;
143         if (skb_network_offset(skb) < ETH_HLEN)
144                 skb_set_network_header(skb, ETH_HLEN);
145         skb_reset_mac_len(skb);
146
147         return 0;
148 }
149
150 static int pop_vlan(struct sk_buff *skb)
151 {
152         __be16 tci;
153         int err;
154
155         if (likely(vlan_tx_tag_present(skb))) {
156                 skb->vlan_tci = 0;
157         } else {
158                 if (unlikely(skb->protocol != htons(ETH_P_8021Q) ||
159                              skb->len < VLAN_ETH_HLEN))
160                         return 0;
161
162                 err = __pop_vlan_tci(skb, &tci);
163                 if (err)
164                         return err;
165         }
166         /* move next vlan tag to hw accel tag */
167         if (likely(skb->protocol != htons(ETH_P_8021Q) ||
168                    skb->len < VLAN_ETH_HLEN))
169                 return 0;
170
171         err = __pop_vlan_tci(skb, &tci);
172         if (unlikely(err))
173                 return err;
174
175         __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(tci));
176         return 0;
177 }
178
179 static int push_vlan(struct sk_buff *skb, const struct ovs_action_push_vlan *vlan)
180 {
181         if (unlikely(vlan_tx_tag_present(skb))) {
182                 u16 current_tag;
183
184                 /* push down current VLAN tag */
185                 current_tag = vlan_tx_tag_get(skb);
186
187                 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
188                                                 current_tag);
189                 if (!skb)
190                         return -ENOMEM;
191
192                 if (skb->ip_summed == CHECKSUM_COMPLETE)
193                         skb->csum = csum_add(skb->csum, csum_partial(skb->data
194                                         + (2 * ETH_ALEN), VLAN_HLEN, 0));
195
196         }
197         __vlan_hwaccel_put_tag(skb, vlan->vlan_tpid, ntohs(vlan->vlan_tci) & ~VLAN_TAG_PRESENT);
198         return 0;
199 }
200
201 static int set_eth_addr(struct sk_buff *skb,
202                         const struct ovs_key_ethernet *eth_key)
203 {
204         int err;
205         err = make_writable(skb, ETH_HLEN);
206         if (unlikely(err))
207                 return err;
208
209         skb_postpull_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
210
211         ether_addr_copy(eth_hdr(skb)->h_source, eth_key->eth_src);
212         ether_addr_copy(eth_hdr(skb)->h_dest, eth_key->eth_dst);
213
214         ovs_skb_postpush_rcsum(skb, eth_hdr(skb), ETH_ALEN * 2);
215
216         return 0;
217 }
218
219 static void set_ip_addr(struct sk_buff *skb, struct iphdr *nh,
220                                 __be32 *addr, __be32 new_addr)
221 {
222         int transport_len = skb->len - skb_transport_offset(skb);
223
224         if (nh->protocol == IPPROTO_TCP) {
225                 if (likely(transport_len >= sizeof(struct tcphdr)))
226                         inet_proto_csum_replace4(&tcp_hdr(skb)->check, skb,
227                                                  *addr, new_addr, 1);
228         } else if (nh->protocol == IPPROTO_UDP) {
229                 if (likely(transport_len >= sizeof(struct udphdr))) {
230                         struct udphdr *uh = udp_hdr(skb);
231
232                         if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
233                                 inet_proto_csum_replace4(&uh->check, skb,
234                                                          *addr, new_addr, 1);
235                                 if (!uh->check)
236                                         uh->check = CSUM_MANGLED_0;
237                         }
238                 }
239         }
240
241         csum_replace4(&nh->check, *addr, new_addr);
242         skb_clear_hash(skb);
243         *addr = new_addr;
244 }
245
246 static void update_ipv6_checksum(struct sk_buff *skb, u8 l4_proto,
247                                  __be32 addr[4], const __be32 new_addr[4])
248 {
249         int transport_len = skb->len - skb_transport_offset(skb);
250
251         if (l4_proto == NEXTHDR_TCP) {
252                 if (likely(transport_len >= sizeof(struct tcphdr)))
253                         inet_proto_csum_replace16(&tcp_hdr(skb)->check, skb,
254                                                   addr, new_addr, 1);
255         } else if (l4_proto == NEXTHDR_UDP) {
256                 if (likely(transport_len >= sizeof(struct udphdr))) {
257                         struct udphdr *uh = udp_hdr(skb);
258
259                         if (uh->check || skb->ip_summed == CHECKSUM_PARTIAL) {
260                                 inet_proto_csum_replace16(&uh->check, skb,
261                                                           addr, new_addr, 1);
262                                 if (!uh->check)
263                                         uh->check = CSUM_MANGLED_0;
264                         }
265                 }
266         } else if (l4_proto == NEXTHDR_ICMP) {
267                 if (likely(transport_len >= sizeof(struct icmp6hdr)))
268                         inet_proto_csum_replace16(&icmp6_hdr(skb)->icmp6_cksum,
269                                                   skb, addr, new_addr, 1);
270         }
271 }
272
273 static void set_ipv6_addr(struct sk_buff *skb, u8 l4_proto,
274                           __be32 addr[4], const __be32 new_addr[4],
275                           bool recalculate_csum)
276 {
277         if (recalculate_csum)
278                 update_ipv6_checksum(skb, l4_proto, addr, new_addr);
279
280         skb_clear_hash(skb);
281         memcpy(addr, new_addr, sizeof(__be32[4]));
282 }
283
284 static void set_ipv6_tc(struct ipv6hdr *nh, u8 tc)
285 {
286         nh->priority = tc >> 4;
287         nh->flow_lbl[0] = (nh->flow_lbl[0] & 0x0F) | ((tc & 0x0F) << 4);
288 }
289
290 static void set_ipv6_fl(struct ipv6hdr *nh, u32 fl)
291 {
292         nh->flow_lbl[0] = (nh->flow_lbl[0] & 0xF0) | (fl & 0x000F0000) >> 16;
293         nh->flow_lbl[1] = (fl & 0x0000FF00) >> 8;
294         nh->flow_lbl[2] = fl & 0x000000FF;
295 }
296
297 static void set_ip_ttl(struct sk_buff *skb, struct iphdr *nh, u8 new_ttl)
298 {
299         csum_replace2(&nh->check, htons(nh->ttl << 8), htons(new_ttl << 8));
300         nh->ttl = new_ttl;
301 }
302
303 static int set_ipv4(struct sk_buff *skb, const struct ovs_key_ipv4 *ipv4_key)
304 {
305         struct iphdr *nh;
306         int err;
307
308         err = make_writable(skb, skb_network_offset(skb) +
309                                  sizeof(struct iphdr));
310         if (unlikely(err))
311                 return err;
312
313         nh = ip_hdr(skb);
314
315         if (ipv4_key->ipv4_src != nh->saddr)
316                 set_ip_addr(skb, nh, &nh->saddr, ipv4_key->ipv4_src);
317
318         if (ipv4_key->ipv4_dst != nh->daddr)
319                 set_ip_addr(skb, nh, &nh->daddr, ipv4_key->ipv4_dst);
320
321         if (ipv4_key->ipv4_tos != nh->tos)
322                 ipv4_change_dsfield(nh, 0, ipv4_key->ipv4_tos);
323
324         if (ipv4_key->ipv4_ttl != nh->ttl)
325                 set_ip_ttl(skb, nh, ipv4_key->ipv4_ttl);
326
327         return 0;
328 }
329
330 static int set_ipv6(struct sk_buff *skb, const struct ovs_key_ipv6 *ipv6_key)
331 {
332         struct ipv6hdr *nh;
333         int err;
334         __be32 *saddr;
335         __be32 *daddr;
336
337         err = make_writable(skb, skb_network_offset(skb) +
338                             sizeof(struct ipv6hdr));
339         if (unlikely(err))
340                 return err;
341
342         nh = ipv6_hdr(skb);
343         saddr = (__be32 *)&nh->saddr;
344         daddr = (__be32 *)&nh->daddr;
345
346         if (memcmp(ipv6_key->ipv6_src, saddr, sizeof(ipv6_key->ipv6_src)))
347                 set_ipv6_addr(skb, ipv6_key->ipv6_proto, saddr,
348                               ipv6_key->ipv6_src, true);
349
350         if (memcmp(ipv6_key->ipv6_dst, daddr, sizeof(ipv6_key->ipv6_dst))) {
351                 unsigned int offset = 0;
352                 int flags = IP6_FH_F_SKIP_RH;
353                 bool recalc_csum = true;
354
355                 if (ipv6_ext_hdr(nh->nexthdr))
356                         recalc_csum = ipv6_find_hdr(skb, &offset,
357                                                     NEXTHDR_ROUTING, NULL,
358                                                     &flags) != NEXTHDR_ROUTING;
359
360                 set_ipv6_addr(skb, ipv6_key->ipv6_proto, daddr,
361                               ipv6_key->ipv6_dst, recalc_csum);
362         }
363
364         set_ipv6_tc(nh, ipv6_key->ipv6_tclass);
365         set_ipv6_fl(nh, ntohl(ipv6_key->ipv6_label));
366         nh->hop_limit = ipv6_key->ipv6_hlimit;
367
368         return 0;
369 }
370
371 /* Must follow make_writable() since that can move the skb data. */
372 static void set_tp_port(struct sk_buff *skb, __be16 *port,
373                          __be16 new_port, __sum16 *check)
374 {
375         inet_proto_csum_replace2(check, skb, *port, new_port, 0);
376         *port = new_port;
377         skb_clear_hash(skb);
378 }
379
380 static void set_udp_port(struct sk_buff *skb, __be16 *port, __be16 new_port)
381 {
382         struct udphdr *uh = udp_hdr(skb);
383
384         if (uh->check && skb->ip_summed != CHECKSUM_PARTIAL) {
385                 set_tp_port(skb, port, new_port, &uh->check);
386
387                 if (!uh->check)
388                         uh->check = CSUM_MANGLED_0;
389         } else {
390                 *port = new_port;
391                 skb_clear_hash(skb);
392         }
393 }
394
395 static int set_udp(struct sk_buff *skb, const struct ovs_key_udp *udp_port_key)
396 {
397         struct udphdr *uh;
398         int err;
399
400         err = make_writable(skb, skb_transport_offset(skb) +
401                                  sizeof(struct udphdr));
402         if (unlikely(err))
403                 return err;
404
405         uh = udp_hdr(skb);
406         if (udp_port_key->udp_src != uh->source)
407                 set_udp_port(skb, &uh->source, udp_port_key->udp_src);
408
409         if (udp_port_key->udp_dst != uh->dest)
410                 set_udp_port(skb, &uh->dest, udp_port_key->udp_dst);
411
412         return 0;
413 }
414
415 static int set_tcp(struct sk_buff *skb, const struct ovs_key_tcp *tcp_port_key)
416 {
417         struct tcphdr *th;
418         int err;
419
420         err = make_writable(skb, skb_transport_offset(skb) +
421                                  sizeof(struct tcphdr));
422         if (unlikely(err))
423                 return err;
424
425         th = tcp_hdr(skb);
426         if (tcp_port_key->tcp_src != th->source)
427                 set_tp_port(skb, &th->source, tcp_port_key->tcp_src, &th->check);
428
429         if (tcp_port_key->tcp_dst != th->dest)
430                 set_tp_port(skb, &th->dest, tcp_port_key->tcp_dst, &th->check);
431
432         return 0;
433 }
434
435 static int set_sctp(struct sk_buff *skb,
436                      const struct ovs_key_sctp *sctp_port_key)
437 {
438         struct sctphdr *sh;
439         int err;
440         unsigned int sctphoff = skb_transport_offset(skb);
441
442         err = make_writable(skb, sctphoff + sizeof(struct sctphdr));
443         if (unlikely(err))
444                 return err;
445
446         sh = sctp_hdr(skb);
447         if (sctp_port_key->sctp_src != sh->source ||
448             sctp_port_key->sctp_dst != sh->dest) {
449                 __le32 old_correct_csum, new_csum, old_csum;
450
451                 old_csum = sh->checksum;
452                 old_correct_csum = sctp_compute_cksum(skb, sctphoff);
453
454                 sh->source = sctp_port_key->sctp_src;
455                 sh->dest = sctp_port_key->sctp_dst;
456
457                 new_csum = sctp_compute_cksum(skb, sctphoff);
458
459                 /* Carry any checksum errors through. */
460                 sh->checksum = old_csum ^ old_correct_csum ^ new_csum;
461
462                 skb_clear_hash(skb);
463         }
464
465         return 0;
466 }
467
468 static int do_output(struct datapath *dp, struct sk_buff *skb, int out_port)
469 {
470         struct vport *vport;
471
472         if (unlikely(!skb))
473                 return -ENOMEM;
474
475         vport = ovs_vport_rcu(dp, out_port);
476         if (unlikely(!vport)) {
477                 kfree_skb(skb);
478                 return -ENODEV;
479         }
480
481         ovs_vport_send(vport, skb);
482         return 0;
483 }
484
485 static int output_userspace(struct datapath *dp, struct sk_buff *skb,
486                             struct sw_flow_key *key, const struct nlattr *attr)
487 {
488         struct dp_upcall_info upcall;
489         const struct nlattr *a;
490         int rem;
491
492         upcall.cmd = OVS_PACKET_CMD_ACTION;
493         upcall.key = key;
494         upcall.userdata = NULL;
495         upcall.portid = 0;
496
497         for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
498                  a = nla_next(a, &rem)) {
499                 switch (nla_type(a)) {
500                 case OVS_USERSPACE_ATTR_USERDATA:
501                         upcall.userdata = a;
502                         break;
503
504                 case OVS_USERSPACE_ATTR_PID:
505                         upcall.portid = nla_get_u32(a);
506                         break;
507                 }
508         }
509
510         return ovs_dp_upcall(dp, skb, &upcall);
511 }
512
513 static bool last_action(const struct nlattr *a, int rem)
514 {
515         return a->nla_len == rem;
516 }
517
518 static int sample(struct datapath *dp, struct sk_buff *skb,
519                   struct sw_flow_key *key, const struct nlattr *attr)
520 {
521         const struct nlattr *acts_list = NULL;
522         const struct nlattr *a;
523         int rem;
524
525         for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
526                  a = nla_next(a, &rem)) {
527                 switch (nla_type(a)) {
528                 case OVS_SAMPLE_ATTR_PROBABILITY:
529                         if (prandom_u32() >= nla_get_u32(a))
530                                 return 0;
531                         break;
532
533                 case OVS_SAMPLE_ATTR_ACTIONS:
534                         acts_list = a;
535                         break;
536                 }
537         }
538
539         rem = nla_len(acts_list);
540         a = nla_data(acts_list);
541
542         /* Actions list is empty, do nothing */
543         if (unlikely(!rem))
544                 return 0;
545
546         /* The only known usage of sample action is having a single user-space
547          * action. Treat this usage as a special case.
548          * The output_userspace() should clone the skb to be sent to the
549          * user space. This skb will be consumed by its caller.
550          */
551         if (likely(nla_type(a) == OVS_ACTION_ATTR_USERSPACE &&
552                    last_action(a, rem)))
553                 return output_userspace(dp, skb, key, a);
554
555         skb = skb_clone(skb, GFP_ATOMIC);
556         if (!skb)
557                 /* Skip the sample action when out of memory. */
558                 return 0;
559
560         if (!add_deferred_actions(skb, key, a)) {
561                 if (net_ratelimit())
562                         pr_warn("%s: deferred actions limit reached, dropping sample action\n",
563                                 ovs_dp_name(dp));
564
565                 kfree_skb(skb);
566         }
567         return 0;
568 }
569
570 static void execute_hash(struct sk_buff *skb, struct sw_flow_key *key,
571                          const struct nlattr *attr)
572 {
573         struct ovs_action_hash *hash_act = nla_data(attr);
574         u32 hash = 0;
575
576         /* OVS_HASH_ALG_L4 is the only possible hash algorithm.  */
577         hash = skb_get_hash(skb);
578         hash = jhash_1word(hash, hash_act->hash_basis);
579         if (!hash)
580                 hash = 0x1;
581
582         key->ovs_flow_hash = hash;
583 }
584
585 static int execute_set_action(struct sk_buff *skb,
586                                  const struct nlattr *nested_attr)
587 {
588         int err = 0;
589
590         switch (nla_type(nested_attr)) {
591         case OVS_KEY_ATTR_PRIORITY:
592                 skb->priority = nla_get_u32(nested_attr);
593                 break;
594
595         case OVS_KEY_ATTR_SKB_MARK:
596                 skb->mark = nla_get_u32(nested_attr);
597                 break;
598
599         case OVS_KEY_ATTR_TUNNEL_INFO:
600                 OVS_CB(skb)->egress_tun_info = nla_data(nested_attr);
601                 break;
602
603         case OVS_KEY_ATTR_ETHERNET:
604                 err = set_eth_addr(skb, nla_data(nested_attr));
605                 break;
606
607         case OVS_KEY_ATTR_IPV4:
608                 err = set_ipv4(skb, nla_data(nested_attr));
609                 break;
610
611         case OVS_KEY_ATTR_IPV6:
612                 err = set_ipv6(skb, nla_data(nested_attr));
613                 break;
614
615         case OVS_KEY_ATTR_TCP:
616                 err = set_tcp(skb, nla_data(nested_attr));
617                 break;
618
619         case OVS_KEY_ATTR_UDP:
620                 err = set_udp(skb, nla_data(nested_attr));
621                 break;
622
623         case OVS_KEY_ATTR_SCTP:
624                 err = set_sctp(skb, nla_data(nested_attr));
625                 break;
626         }
627
628         return err;
629 }
630
631 static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
632                           struct sw_flow_key *key,
633                           const struct nlattr *a, int rem)
634 {
635         struct deferred_action *da;
636         int err;
637
638         err = ovs_flow_key_update(skb, key);
639         if (err)
640                 return err;
641
642         if (!last_action(a, rem)) {
643                 /* Recirc action is the not the last action
644                  * of the action list, need to clone the skb.
645                  */
646                 skb = skb_clone(skb, GFP_ATOMIC);
647
648                 /* Skip the recirc action when out of memory, but
649                  * continue on with the rest of the action list.
650                  */
651                 if (!skb)
652                         return 0;
653         }
654
655         da = add_deferred_actions(skb, key, NULL);
656         if (da) {
657                 da->pkt_key.recirc_id = nla_get_u32(a);
658         } else {
659                 kfree_skb(skb);
660
661                 if (net_ratelimit())
662                         pr_warn("%s: deferred action limit reached, drop recirc action\n",
663                                 ovs_dp_name(dp));
664         }
665
666         return 0;
667 }
668
669 /* Execute a list of actions against 'skb'. */
670 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
671                               struct sw_flow_key *key,
672                               const struct nlattr *attr, int len)
673 {
674         /* Every output action needs a separate clone of 'skb', but the common
675          * case is just a single output action, so that doing a clone and
676          * then freeing the original skbuff is wasteful.  So the following code
677          * is slightly obscure just to avoid that. */
678         int prev_port = -1;
679         const struct nlattr *a;
680         int rem;
681
682         for (a = attr, rem = len; rem > 0;
683              a = nla_next(a, &rem)) {
684                 int err = 0;
685
686                 if (prev_port != -1) {
687                         do_output(dp, skb_clone(skb, GFP_ATOMIC), prev_port);
688                         prev_port = -1;
689                 }
690
691                 switch (nla_type(a)) {
692                 case OVS_ACTION_ATTR_OUTPUT:
693                         prev_port = nla_get_u32(a);
694                         break;
695
696                 case OVS_ACTION_ATTR_USERSPACE:
697                         output_userspace(dp, skb, key, a);
698                         break;
699
700                 case OVS_ACTION_ATTR_HASH:
701                         execute_hash(skb, key, a);
702                         break;
703
704                 case OVS_ACTION_ATTR_PUSH_VLAN:
705                         err = push_vlan(skb, nla_data(a));
706                         if (unlikely(err)) /* skb already freed. */
707                                 return err;
708                         break;
709
710                 case OVS_ACTION_ATTR_POP_VLAN:
711                         err = pop_vlan(skb);
712                         break;
713
714                 case OVS_ACTION_ATTR_RECIRC:
715                         err = execute_recirc(dp, skb, key, a, rem);
716                         if (last_action(a, rem)) {
717                                 /* If this is the last action, the skb has
718                                  * been consumed or freed.
719                                  * Return immediately.
720                                  */
721                                 return err;
722                         }
723                         break;
724
725                 case OVS_ACTION_ATTR_SET:
726                         err = execute_set_action(skb, nla_data(a));
727                         break;
728
729                 case OVS_ACTION_ATTR_SAMPLE:
730                         err = sample(dp, skb, key, a);
731                         break;
732                 }
733
734                 if (unlikely(err)) {
735                         kfree_skb(skb);
736                         return err;
737                 }
738         }
739
740         if (prev_port != -1)
741                 do_output(dp, skb, prev_port);
742         else
743                 consume_skb(skb);
744
745         return 0;
746 }
747
748 static void process_deferred_actions(struct datapath *dp)
749 {
750         struct action_fifo *fifo = this_cpu_ptr(action_fifos);
751
752         /* Do not touch the FIFO in case there is no deferred actions. */
753         if (action_fifo_is_empty(fifo))
754                 return;
755
756         /* Finishing executing all deferred actions. */
757         do {
758                 struct deferred_action *da = action_fifo_get(fifo);
759                 struct sk_buff *skb = da->skb;
760                 struct sw_flow_key *key = &da->pkt_key;
761                 const struct nlattr *actions = da->actions;
762
763                 if (actions)
764                         do_execute_actions(dp, skb, key, actions,
765                                            nla_len(actions));
766                 else
767                         ovs_dp_process_packet(skb, key);
768         } while (!action_fifo_is_empty(fifo));
769
770         /* Reset FIFO for the next packet.  */
771         action_fifo_init(fifo);
772 }
773
774 /* Execute a list of actions against 'skb'. */
775 int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
776                         struct sw_flow_key *key)
777 {
778         int level = this_cpu_read(exec_actions_level);
779         struct sw_flow_actions *acts;
780         int err;
781
782         acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts);
783
784         this_cpu_inc(exec_actions_level);
785         OVS_CB(skb)->egress_tun_info = NULL;
786         err = do_execute_actions(dp, skb, key,
787                                  acts->actions, acts->actions_len);
788
789         if (!level)
790                 process_deferred_actions(dp);
791
792         this_cpu_dec(exec_actions_level);
793         return err;
794 }
795
796 int action_fifos_init(void)
797 {
798         action_fifos = alloc_percpu(struct action_fifo);
799         if (!action_fifos)
800                 return -ENOMEM;
801
802         return 0;
803 }
804
805 void action_fifos_exit(void)
806 {
807         free_percpu(action_fifos);
808 }