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