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