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