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