11/*
2- * Copyright (c) 2007-2013 Nicira, Inc.
2+ * Copyright (c) 2007-2014 Nicira, Inc.
33 *
44 * This program is free software; you can redistribute it and/or
55 * modify it under the terms of version 2 of the GNU General Public
3535#include <net/sctp/checksum.h>
3636
3737#include "datapath.h"
38+ #include "flow.h"
3839#include "vport.h"
3940
4041static int do_execute_actions (struct datapath * dp , struct sk_buff * skb ,
4142 struct sw_flow_key * key ,
4243 const struct nlattr * attr , int len );
4344
45+ struct deferred_action {
46+ struct sk_buff * skb ;
47+ const struct nlattr * actions ;
48+
49+ /* Store pkt_key clone when creating deferred action. */
50+ struct sw_flow_key pkt_key ;
51+ };
52+
53+ #define DEFERRED_ACTION_FIFO_SIZE 10
54+ struct action_fifo {
55+ int head ;
56+ int tail ;
57+ /* Deferred action fifo queue storage. */
58+ struct deferred_action fifo [DEFERRED_ACTION_FIFO_SIZE ];
59+ };
60+
61+ static struct action_fifo __percpu * action_fifos ;
62+ static DEFINE_PER_CPU (int , exec_actions_level );
63+
64+ static void action_fifo_init (struct action_fifo * fifo )
65+ {
66+ fifo -> head = 0 ;
67+ fifo -> tail = 0 ;
68+ }
69+
70+ static bool action_fifo_is_empty (struct action_fifo * fifo )
71+ {
72+ return (fifo -> head == fifo -> tail );
73+ }
74+
75+ static struct deferred_action * action_fifo_get (struct action_fifo * fifo )
76+ {
77+ if (action_fifo_is_empty (fifo ))
78+ return NULL ;
79+
80+ return & fifo -> fifo [fifo -> tail ++ ];
81+ }
82+
83+ static struct deferred_action * action_fifo_put (struct action_fifo * fifo )
84+ {
85+ if (fifo -> head >= DEFERRED_ACTION_FIFO_SIZE - 1 )
86+ return NULL ;
87+
88+ return & fifo -> fifo [fifo -> head ++ ];
89+ }
90+
91+ /* Return true if fifo is not full */
92+ static struct deferred_action * add_deferred_actions (struct sk_buff * skb ,
93+ struct sw_flow_key * key ,
94+ const struct nlattr * attr )
95+ {
96+ struct action_fifo * fifo ;
97+ struct deferred_action * da ;
98+
99+ fifo = this_cpu_ptr (action_fifos );
100+ da = action_fifo_put (fifo );
101+ if (da ) {
102+ da -> skb = skb ;
103+ da -> actions = attr ;
104+ da -> pkt_key = * key ;
105+ }
106+
107+ return da ;
108+ }
109+
44110static int make_writable (struct sk_buff * skb , int write_len )
45111{
46112 if (!pskb_may_pull (skb , write_len ))
@@ -485,8 +551,29 @@ static int sample(struct datapath *dp, struct sk_buff *skb,
485551 /* Skip the sample action when out of memory. */
486552 return 0 ;
487553
488- /* do_execute_actions() will consume the cloned skb. */
489- return do_execute_actions (dp , skb , key , a , rem );
554+ if (!add_deferred_actions (skb , key , a )) {
555+ if (net_ratelimit ())
556+ pr_warn ("%s: deferred actions limit reached, dropping sample action\n" ,
557+ ovs_dp_name (dp ));
558+
559+ kfree_skb (skb );
560+ }
561+ return 0 ;
562+ }
563+
564+ static void execute_hash (struct sk_buff * skb , struct sw_flow_key * key ,
565+ const struct nlattr * attr )
566+ {
567+ struct ovs_action_hash * hash_act = nla_data (attr );
568+ u32 hash = 0 ;
569+
570+ /* OVS_HASH_ALG_L4 is the only possible hash algorithm. */
571+ hash = skb_get_hash (skb );
572+ hash = jhash_1word (hash , hash_act -> hash_basis );
573+ if (!hash )
574+ hash = 0x1 ;
575+
576+ key -> ovs_flow_hash = hash ;
490577}
491578
492579static int execute_set_action (struct sk_buff * skb ,
@@ -535,6 +622,44 @@ static int execute_set_action(struct sk_buff *skb,
535622 return err ;
536623}
537624
625+ static int execute_recirc (struct datapath * dp , struct sk_buff * skb ,
626+ struct sw_flow_key * key ,
627+ const struct nlattr * a , int rem )
628+ {
629+ struct deferred_action * da ;
630+ int err ;
631+
632+ err = ovs_flow_key_update (skb , key );
633+ if (err )
634+ return err ;
635+
636+ if (!last_action (a , rem )) {
637+ /* Recirc action is the not the last action
638+ * of the action list, need to clone the skb.
639+ */
640+ skb = skb_clone (skb , GFP_ATOMIC );
641+
642+ /* Skip the recirc action when out of memory, but
643+ * continue on with the rest of the action list.
644+ */
645+ if (!skb )
646+ return 0 ;
647+ }
648+
649+ da = add_deferred_actions (skb , key , NULL );
650+ if (da ) {
651+ da -> pkt_key .recirc_id = nla_get_u32 (a );
652+ } else {
653+ kfree_skb (skb );
654+
655+ if (net_ratelimit ())
656+ pr_warn ("%s: deferred action limit reached, drop recirc action\n" ,
657+ ovs_dp_name (dp ));
658+ }
659+
660+ return 0 ;
661+ }
662+
538663/* Execute a list of actions against 'skb'. */
539664static int do_execute_actions (struct datapath * dp , struct sk_buff * skb ,
540665 struct sw_flow_key * key ,
@@ -566,6 +691,10 @@ static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
566691 output_userspace (dp , skb , key , a );
567692 break ;
568693
694+ case OVS_ACTION_ATTR_HASH :
695+ execute_hash (skb , key , a );
696+ break ;
697+
569698 case OVS_ACTION_ATTR_PUSH_VLAN :
570699 err = push_vlan (skb , nla_data (a ));
571700 if (unlikely (err )) /* skb already freed. */
@@ -576,6 +705,17 @@ static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
576705 err = pop_vlan (skb );
577706 break ;
578707
708+ case OVS_ACTION_ATTR_RECIRC :
709+ err = execute_recirc (dp , skb , key , a , rem );
710+ if (last_action (a , rem )) {
711+ /* If this is the last action, the skb has
712+ * been consumed or freed.
713+ * Return immediately.
714+ */
715+ return err ;
716+ }
717+ break ;
718+
579719 case OVS_ACTION_ATTR_SET :
580720 err = execute_set_action (skb , nla_data (a ));
581721 break ;
@@ -601,12 +741,63 @@ static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
601741 return 0 ;
602742}
603743
744+ static void process_deferred_actions (struct datapath * dp )
745+ {
746+ struct action_fifo * fifo = this_cpu_ptr (action_fifos );
747+
748+ /* Do not touch the FIFO in case there is no deferred actions. */
749+ if (action_fifo_is_empty (fifo ))
750+ return ;
751+
752+ /* Finishing executing all deferred actions. */
753+ do {
754+ struct deferred_action * da = action_fifo_get (fifo );
755+ struct sk_buff * skb = da -> skb ;
756+ struct sw_flow_key * key = & da -> pkt_key ;
757+ const struct nlattr * actions = da -> actions ;
758+
759+ if (actions )
760+ do_execute_actions (dp , skb , key , actions ,
761+ nla_len (actions ));
762+ else
763+ ovs_dp_process_packet (skb , key );
764+ } while (!action_fifo_is_empty (fifo ));
765+
766+ /* Reset FIFO for the next packet. */
767+ action_fifo_init (fifo );
768+ }
769+
604770/* Execute a list of actions against 'skb'. */
605771int ovs_execute_actions (struct datapath * dp , struct sk_buff * skb ,
606772 struct sw_flow_key * key )
607773{
608- struct sw_flow_actions * acts = rcu_dereference (OVS_CB (skb )-> flow -> sf_acts );
774+ int level = this_cpu_read (exec_actions_level );
775+ struct sw_flow_actions * acts ;
776+ int err ;
777+
778+ acts = rcu_dereference (OVS_CB (skb )-> flow -> sf_acts );
779+
780+ this_cpu_inc (exec_actions_level );
781+ err = do_execute_actions (dp , skb , key ,
782+ acts -> actions , acts -> actions_len );
783+
784+ if (!level )
785+ process_deferred_actions (dp );
786+
787+ this_cpu_dec (exec_actions_level );
788+ return err ;
789+ }
790+
791+ int action_fifos_init (void )
792+ {
793+ action_fifos = alloc_percpu (struct action_fifo );
794+ if (!action_fifos )
795+ return - ENOMEM ;
609796
610- return do_execute_actions (dp , skb , key ,
611- acts -> actions , acts -> actions_len );
797+ return 0 ;
798+ }
799+
800+ void action_fifos_exit (void )
801+ {
802+ free_percpu (action_fifos );
612803}
0 commit comments