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