Skip to content

Commit 798c166

Browse files
azhou-niciradavem330
authored andcommitted
openvswitch: Optimize sample action for the clone use cases
With the introduction of open flow 'clone' action, the OVS user space can now translate the 'clone' action into kernel datapath 'sample' action, with 100% probability, to ensure that the clone semantics, which is that the packet seen by the clone action is the same as the packet seen by the action after clone, is faithfully carried out in the datapath. While the sample action in the datpath has the matching semantics, its implementation is only optimized for its original use. Specifically, there are two limitation: First, there is a 3 level of nesting restriction, enforced at the flow downloading time. This limit turns out to be too restrictive for the 'clone' use case. Second, the implementation avoid recursive call only if the sample action list has a single userspace action. The main optimization implemented in this series removes the static nesting limit check, instead, implement the run time recursion limit check, and recursion avoidance similar to that of the 'recirc' action. This optimization solve both raspberrypi#1 and raspberrypi#2 issues above. One related optimization attempts to avoid copying flow key as long as the actions enclosed does not change the flow key. The detection is performed only once at the flow downloading time. Another related optimization is to rewrite the action list at flow downloading time in order to save the fast path from parsing the sample action list in its original form repeatedly. Signed-off-by: Andy Zhou <azhou@ovn.org> Acked-by: Pravin B Shelar <pshelar@ovn.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 4572ef5 commit 798c166

4 files changed

Lines changed: 167 additions & 98 deletions

File tree

‎include/uapi/linux/openvswitch.h‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,10 +578,25 @@ enum ovs_sample_attr {
578578
OVS_SAMPLE_ATTR_PROBABILITY, /* u32 number */
579579
OVS_SAMPLE_ATTR_ACTIONS, /* Nested OVS_ACTION_ATTR_* attributes. */
580580
__OVS_SAMPLE_ATTR_MAX,
581+
582+
#ifdef __KERNEL__
583+
OVS_SAMPLE_ATTR_ARG /* struct sample_arg */
584+
#endif
581585
};
582586

583587
#define OVS_SAMPLE_ATTR_MAX (__OVS_SAMPLE_ATTR_MAX - 1)
584588

589+
#ifdef __KERNEL__
590+
struct sample_arg {
591+
bool exec; /* When true, actions in sample will not
592+
* change flow keys. False otherwise.
593+
*/
594+
u32 probability; /* Same value as
595+
* 'OVS_SAMPLE_ATTR_PROBABILITY'.
596+
*/
597+
};
598+
#endif
599+
585600
/**
586601
* enum ovs_userspace_attr - Attributes for %OVS_ACTION_ATTR_USERSPACE action.
587602
* @OVS_USERSPACE_ATTR_PID: u32 Netlink PID to which the %OVS_PACKET_CMD_ACTION

‎net/openvswitch/actions.c‎

Lines changed: 55 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -928,73 +928,70 @@ static int output_userspace(struct datapath *dp, struct sk_buff *skb,
928928
return ovs_dp_upcall(dp, skb, key, &upcall, cutlen);
929929
}
930930

931+
/* When 'last' is true, sample() should always consume the 'skb'.
932+
* Otherwise, sample() should keep 'skb' intact regardless what
933+
* actions are executed within sample().
934+
*/
931935
static int sample(struct datapath *dp, struct sk_buff *skb,
932936
struct sw_flow_key *key, const struct nlattr *attr,
933-
const struct nlattr *actions, int actions_len)
937+
bool last)
934938
{
935-
const struct nlattr *acts_list = NULL;
936-
const struct nlattr *a;
937-
int rem;
938-
u32 cutlen = 0;
939+
struct nlattr *actions;
940+
struct nlattr *sample_arg;
941+
struct sw_flow_key *orig_key = key;
942+
int rem = nla_len(attr);
943+
int err = 0;
944+
const struct sample_arg *arg;
939945

940-
for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
941-
a = nla_next(a, &rem)) {
942-
u32 probability;
946+
/* The first action is always 'OVS_SAMPLE_ATTR_ARG'. */
947+
sample_arg = nla_data(attr);
948+
arg = nla_data(sample_arg);
949+
actions = nla_next(sample_arg, &rem);
943950

944-
switch (nla_type(a)) {
945-
case OVS_SAMPLE_ATTR_PROBABILITY:
946-
probability = nla_get_u32(a);
947-
if (!probability || prandom_u32() > probability)
948-
return 0;
949-
break;
950-
951-
case OVS_SAMPLE_ATTR_ACTIONS:
952-
acts_list = a;
953-
break;
954-
}
951+
if ((arg->probability != U32_MAX) &&
952+
(!arg->probability || prandom_u32() > arg->probability)) {
953+
if (last)
954+
consume_skb(skb);
955+
return 0;
955956
}
956957

957-
rem = nla_len(acts_list);
958-
a = nla_data(acts_list);
959-
960-
/* Actions list is empty, do nothing */
961-
if (unlikely(!rem))
958+
/* Unless the last action, sample works on the clone of SKB. */
959+
skb = last ? skb : skb_clone(skb, GFP_ATOMIC);
960+
if (!skb) {
961+
/* Out of memory, skip this sample action.
962+
*/
962963
return 0;
964+
}
963965

964-
/* The only known usage of sample action is having a single user-space
965-
* action, or having a truncate action followed by a single user-space
966-
* action. Treat this usage as a special case.
967-
* The output_userspace() should clone the skb to be sent to the
968-
* user space. This skb will be consumed by its caller.
966+
/* In case the sample actions won't change 'key',
967+
* it can be used directly to execute sample actions.
968+
* Otherwise, allocate a new key from the
969+
* next recursion level of 'flow_keys'. If
970+
* successful, execute the sample actions without
971+
* deferring.
972+
*
973+
* Defer the sample actions if the recursion
974+
* limit has been reached.
969975
*/
970-
if (unlikely(nla_type(a) == OVS_ACTION_ATTR_TRUNC)) {
971-
struct ovs_action_trunc *trunc = nla_data(a);
972-
973-
if (skb->len > trunc->max_len)
974-
cutlen = skb->len - trunc->max_len;
975-
976-
a = nla_next(a, &rem);
976+
if (!arg->exec) {
977+
__this_cpu_inc(exec_actions_level);
978+
key = clone_key(key);
977979
}
978980

979-
if (likely(nla_type(a) == OVS_ACTION_ATTR_USERSPACE &&
980-
nla_is_last(a, rem)))
981-
return output_userspace(dp, skb, key, a, actions,
982-
actions_len, cutlen);
981+
if (key) {
982+
err = do_execute_actions(dp, skb, key, actions, rem);
983+
} else if (!add_deferred_actions(skb, orig_key, actions, rem)) {
983984

984-
skb = skb_clone(skb, GFP_ATOMIC);
985-
if (!skb)
986-
/* Skip the sample action when out of memory. */
987-
return 0;
988-
989-
if (!add_deferred_actions(skb, key, nla_data(acts_list),
990-
nla_len(acts_list))) {
991985
if (net_ratelimit())
992-
pr_warn("%s: deferred actions limit reached, dropping sample action\n",
986+
pr_warn("%s: deferred action limit reached, drop sample action\n",
993987
ovs_dp_name(dp));
994-
995988
kfree_skb(skb);
996989
}
997-
return 0;
990+
991+
if (!arg->exec)
992+
__this_cpu_dec(exec_actions_level);
993+
994+
return err;
998995
}
999996

1000997
static void execute_hash(struct sk_buff *skb, struct sw_flow_key *key,
@@ -1244,9 +1241,15 @@ static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
12441241
err = execute_masked_set_action(skb, key, nla_data(a));
12451242
break;
12461243

1247-
case OVS_ACTION_ATTR_SAMPLE:
1248-
err = sample(dp, skb, key, a, attr, len);
1244+
case OVS_ACTION_ATTR_SAMPLE: {
1245+
bool last = nla_is_last(a, rem);
1246+
1247+
err = sample(dp, skb, key, a, last);
1248+
if (last)
1249+
return err;
1250+
12491251
break;
1252+
}
12501253

12511254
case OVS_ACTION_ATTR_CT:
12521255
if (!is_flow_key_valid(key)) {

‎net/openvswitch/datapath.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@
3434
#define DP_MAX_PORTS USHRT_MAX
3535
#define DP_VPORT_HASH_BUCKETS 1024
3636

37-
#define SAMPLE_ACTION_DEPTH 3
38-
3937
/**
4038
* struct dp_stats_percpu - per-cpu packet processing statistics for a given
4139
* datapath.

0 commit comments

Comments
 (0)