History

Merge branch 'net-openvswitch-add-sample-multicasting'

Adrian Moreno says: ==================== net: openvswitch: Add sample multicasting. ** Background ** Currently, OVS supports several packet sampling mechanisms (sFlow, per-bridge IPFIX, per-flow IPFIX). These end up being translated into a userspace action that needs to be handled by ovs-vswitchd's handler threads only to be forwarded to some third party application that will somehow process the sample and provide observability on the datapath. A particularly interesting use-case is controller-driven per-flow IPFIX sampling where the OpenFlow controller can add metadata to samples (via two 32bit integers) and this metadata is then available to the sample-collecting system for correlation. ** Problem ** The fact that sampled traffic share netlink sockets and handler thread time with upcalls, apart from being a performance bottleneck in the sample extraction itself, can severely compromise the datapath, yielding this solution unfit for highly loaded production systems. Users are left with little options other than guessing what sampling rate will be OK for their traffic pattern and system load and dealing with the lost accuracy. Looking at available infrastructure, an obvious candidated would be to use psample. However, it's current state does not help with the use-case at stake because sampled packets do not contain user-defined metadata. ** Proposal ** This series is an attempt to fix this situation by extending the existing psample infrastructure to carry a variable length user-defined cookie. The main existing user of psample is tc's act_sample. It is also extended to forward the action's cookie to psample. Finally, a new OVS action (OVS_SAMPLE_ATTR_PSAMPLE) is created. It accepts a group and an optional cookie and uses psample to multicast the packet and the metadata. ====================

Jakub Kicinski committed 2 years ago

Commit 73d4d642 parents 064fbc4 30d772a13 files · +566 −16Browse files

Unified · Split

13 files
  1. Documentation/netlink/specs/ovs_flow.yaml +17 −0
  2. include/net/psample.h +4 −1
  3. include/uapi/linux/openvswitch.h +30 −1
  4. include/uapi/linux/psample.h +10 −1
  5. net/openvswitch/Kconfig +1 −0
  6. net/openvswitch/actions.c +64 −2
  7. net/openvswitch/datapath.h +3 −0
  8. net/openvswitch/flow_netlink.c +31 −1
  9. net/openvswitch/vport.c +1 −0
  10. net/psample/psample.c +15 −1
  11. net/sched/act_sample.c +12 −0
  12. tools/testing/selftests/net/openvswitch/openvswitch.sh +112 −3
  13. tools/testing/selftests/net/openvswitch/ovs-dpctl.py +266 −6
Documentation/netlink/specs/ovs_flow.yaml+17−0
@@ -727,6 +727,12 @@ attribute-sets: · 726 unmodified lines
727727 name: dec-ttl
728728 type: nest
729729 nested-attributes: dec-ttl-attrs
730+ -
731+ name: psample
732+ type: nest
733+ nested-attributes: psample-attrs
734+ doc: |
735+ Sends a packet sample to psample for external observation.
730736 -
731737 name: tunnel-key-attrs
732738 enum-name: ovs-tunnel-key-attr
@@ -938,6 +944,17 @@ attribute-sets: · 205 unmodified lines
938944 -
939945 name: gbp
940946 type: u32
947+ -
948+ name: psample-attrs
949+ enum-name: ovs-psample-attr
950+ name-prefix: ovs-psample-attr-
951+ attributes:
952+ -
953+ name: group
954+ type: u32
955+ -
956+ name: cookie
957+ type: binary
941958
942959 operations:
943960 name-prefix: ovs-flow-cmd-
include/net/psample.h+4−1
@@ -24,7 +24,10 @@ struct psample_metadata { · 23 unmodified lines
2424 u8 out_tc_valid:1,
2525 out_tc_occ_valid:1,
2626 latency_valid:1,
27− unused:5;
27+ rate_as_probability:1,
28+ unused:4;
29+ const u8 *user_cookie;
30+ u32 user_cookie_len;
2831 };
2932
3033 struct psample_group *psample_group_get(struct net *net, u32 group_num);
include/uapi/linux/openvswitch.h+30−1
@@ -649,7 +649,8 @@ enum ovs_flow_attr { · 648 unmodified lines
649649 * Actions are passed as nested attributes.
650650 *
651651 * Executes the specified actions with the given probability on a per-packet
652− * basis.
652+ * basis. Nested actions will be able to access the probability value of the
653+ * parent @OVS_ACTION_ATTR_SAMPLE.
653654 */
654655 enum ovs_sample_attr {
655656 OVS_SAMPLE_ATTR_UNSPEC,
@@ -914,6 +915,31 @@ struct check_pkt_len_arg { · 258 unmodified lines
914915 };
915916 #endif
916917
918+#define OVS_PSAMPLE_COOKIE_MAX_SIZE 16
919+/**
920+ * enum ovs_psample_attr - Attributes for %OVS_ACTION_ATTR_PSAMPLE
921+ * action.
922+ *
923+ * @OVS_PSAMPLE_ATTR_GROUP: 32-bit number to identify the source of the
924+ * sample.
925+ * @OVS_PSAMPLE_ATTR_COOKIE: An optional variable-length binary cookie that
926+ * contains user-defined metadata. The maximum length is
927+ * OVS_PSAMPLE_COOKIE_MAX_SIZE bytes.
928+ *
929+ * Sends the packet to the psample multicast group with the specified group and
930+ * cookie. It is possible to combine this action with the
931+ * %OVS_ACTION_ATTR_TRUNC action to limit the size of the sample.
932+ */
933+enum ovs_psample_attr {
934+ OVS_PSAMPLE_ATTR_GROUP = 1, /* u32 number. */
935+ OVS_PSAMPLE_ATTR_COOKIE, /* Optional, user specified cookie. */
936+
937+ /* private: */
938+ __OVS_PSAMPLE_ATTR_MAX
939+};
940+
941+#define OVS_PSAMPLE_ATTR_MAX (__OVS_PSAMPLE_ATTR_MAX - 1)
942+
917943 /**
918944 * enum ovs_action_attr - Action types.
919945 *
@@ -966,6 +992,8 @@ struct check_pkt_len_arg { · 46 unmodified lines
966992 * of l3 tunnel flag in the tun_flags field of OVS_ACTION_ATTR_ADD_MPLS
967993 * argument.
968994 * @OVS_ACTION_ATTR_DROP: Explicit drop action.
995+ * @OVS_ACTION_ATTR_PSAMPLE: Send a sample of the packet to external observers
996+ * via psample.
969997 *
970998 * Only a single header can be set with a single %OVS_ACTION_ATTR_SET. Not all
971999 * fields within a header are modifiable, e.g. the IPv4 protocol and fragment
@@ -1004,6 +1032,7 @@ enum ovs_action_attr { · 32 unmodified lines
10041032 OVS_ACTION_ATTR_ADD_MPLS, /* struct ovs_action_add_mpls. */
10051033 OVS_ACTION_ATTR_DEC_TTL, /* Nested OVS_DEC_TTL_ATTR_*. */
10061034 OVS_ACTION_ATTR_DROP, /* u32 error code. */
1035+ OVS_ACTION_ATTR_PSAMPLE, /* Nested OVS_PSAMPLE_ATTR_*. */
10071036
10081037 __OVS_ACTION_ATTR_MAX, /* Nothing past this will be accepted
10091038 * from userspace. */
include/uapi/linux/psample.h+10−1
@@ -8,7 +8,11 @@ enum { · 7 unmodified lines
88 PSAMPLE_ATTR_ORIGSIZE,
99 PSAMPLE_ATTR_SAMPLE_GROUP,
1010 PSAMPLE_ATTR_GROUP_SEQ,
11− PSAMPLE_ATTR_SAMPLE_RATE,
11+ PSAMPLE_ATTR_SAMPLE_RATE, /* u32, ratio between observed and
12+ * sampled packets or scaled probability
13+ * if PSAMPLE_ATTR_SAMPLE_PROBABILITY
14+ * is set.
15+ */
1216 PSAMPLE_ATTR_DATA,
1317 PSAMPLE_ATTR_GROUP_REFCOUNT,
1418 PSAMPLE_ATTR_TUNNEL,
@@ -19,6 +23,11 @@ enum { · 4 unmodified lines
1923 PSAMPLE_ATTR_LATENCY, /* u64, nanoseconds */
2024 PSAMPLE_ATTR_TIMESTAMP, /* u64, nanoseconds */
2125 PSAMPLE_ATTR_PROTO, /* u16 */
26+ PSAMPLE_ATTR_USER_COOKIE, /* binary, user provided data */
27+ PSAMPLE_ATTR_SAMPLE_PROBABILITY,/* no argument, interpret rate in
28+ * PSAMPLE_ATTR_SAMPLE_RATE as a
29+ * probability scaled 0 - U32_MAX.
30+ */
2231
2332 __PSAMPLE_ATTR_MAX
2433 };
net/openvswitch/Kconfig+1−0
@@ -10,6 +10,7 @@ config OPENVSWITCH · 9 unmodified lines
1010 (NF_CONNTRACK && ((!NF_DEFRAG_IPV6 || NF_DEFRAG_IPV6) && \
1111 (!NF_NAT || NF_NAT) && \
1212 (!NETFILTER_CONNCOUNT || NETFILTER_CONNCOUNT)))
13+ depends on PSAMPLE || !PSAMPLE
1314 select LIBCRC32C
1415 select MPLS
1516 select NET_MPLS_GSO
net/openvswitch/actions.c+64−2
@@ -24,6 +24,11 @@ · 23 unmodified lines
2424 #include <net/checksum.h>
2525 #include <net/dsfield.h>
2626 #include <net/mpls.h>
27+
28+#if IS_ENABLED(CONFIG_PSAMPLE)
29+#include <net/psample.h>
30+#endif
31+
2732 #include <net/sctp/checksum.h>
2833
2934 #include "datapath.h"
@@ -1043,12 +1048,15 @@ static int sample(struct datapath *dp, struct sk_buff *skb, · 1013 unmodified lines
10431048 struct nlattr *sample_arg;
10441049 int rem = nla_len(attr);
10451050 const struct sample_arg *arg;
1051+ u32 init_probability;
10461052 bool clone_flow_key;
1053+ int err;
10471054
10481055 /* The first action is always 'OVS_SAMPLE_ATTR_ARG'. */
10491056 sample_arg = nla_data(attr);
10501057 arg = nla_data(sample_arg);
10511058 actions = nla_next(sample_arg, &rem);
1059+ init_probability = OVS_CB(skb)->probability;
10521060
10531061 if ((arg->probability != U32_MAX) &&
10541062 (!arg->probability || get_random_u32() > arg->probability)) {
@@ -1057,9 +1065,16 @@ static int sample(struct datapath *dp, struct sk_buff *skb, · 2 unmodified lines
10571065 return 0;
10581066 }
10591067
1068+ OVS_CB(skb)->probability = arg->probability;
1069+
10601070 clone_flow_key = !arg->exec;
1061− return clone_execute(dp, skb, key, 0, actions, rem, last,
1062− clone_flow_key);
1071+ err = clone_execute(dp, skb, key, 0, actions, rem, last,
1072+ clone_flow_key);
1073+
1074+ if (!last)
1075+ OVS_CB(skb)->probability = init_probability;
1076+
1077+ return err;
10631078 }
10641079
10651080 /* When 'last' is true, clone() should always consume the 'skb'.
@@ -1299,6 +1314,44 @@ static int execute_dec_ttl(struct sk_buff *skb, struct sw_flow_key *key) · 233 unmodified lines
12991314 return 0;
13001315 }
13011316
1317+#if IS_ENABLED(CONFIG_PSAMPLE)
1318+static void execute_psample(struct datapath *dp, struct sk_buff *skb,
1319+ const struct nlattr *attr)
1320+{
1321+ struct psample_group psample_group = {};
1322+ struct psample_metadata md = {};
1323+ const struct nlattr *a;
1324+ u32 rate;
1325+ int rem;
1326+
1327+ nla_for_each_attr(a, nla_data(attr), nla_len(attr), rem) {
1328+ switch (nla_type(a)) {
1329+ case OVS_PSAMPLE_ATTR_GROUP:
1330+ psample_group.group_num = nla_get_u32(a);
1331+ break;
1332+
1333+ case OVS_PSAMPLE_ATTR_COOKIE:
1334+ md.user_cookie = nla_data(a);
1335+ md.user_cookie_len = nla_len(a);
1336+ break;
1337+ }
1338+ }
1339+
1340+ psample_group.net = ovs_dp_get_net(dp);
1341+ md.in_ifindex = OVS_CB(skb)->input_vport->dev->ifindex;
1342+ md.trunc_size = skb->len - OVS_CB(skb)->cutlen;
1343+ md.rate_as_probability = 1;
1344+
1345+ rate = OVS_CB(skb)->probability ? OVS_CB(skb)->probability : U32_MAX;
1346+
1347+ psample_sample_packet(&psample_group, skb, rate, &md);
1348+}
1349+#else
1350+static void execute_psample(struct datapath *dp, struct sk_buff *skb,
1351+ const struct nlattr *attr)
1352+{}
1353+#endif
1354+
13021355 /* Execute a list of actions against 'skb'. */
13031356 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
13041357 struct sw_flow_key *key,
@@ -1502,6 +1555,15 @@ static int do_execute_actions(struct datapath *dp, struct sk_buff *skb, · 197 unmodified lines
15021555 ovs_kfree_skb_reason(skb, reason);
15031556 return 0;
15041557 }
1558+
1559+ case OVS_ACTION_ATTR_PSAMPLE:
1560+ execute_psample(dp, skb, a);
1561+ OVS_CB(skb)->cutlen = 0;
1562+ if (nla_is_last(a, rem)) {
1563+ consume_skb(skb);
1564+ return 0;
1565+ }
1566+ break;
15051567 }
15061568
15071569 if (unlikely(err)) {
net/openvswitch/datapath.h+3−0
@@ -115,12 +115,15 @@ struct datapath { · 114 unmodified lines
115115 * fragmented.
116116 * @acts_origlen: The netlink size of the flow actions applied to this skb.
117117 * @cutlen: The number of bytes from the packet end to be removed.
118+ * @probability: The sampling probability that was applied to this skb; 0 means
119+ * no sampling has occurred; U32_MAX means 100% probability.
118120 */
119121 struct ovs_skb_cb {
120122 struct vport *input_vport;
121123 u16 mru;
122124 u16 acts_origlen;
123125 u32 cutlen;
126+ u32 probability;
124127 };
125128 #define OVS_CB(skb) ((struct ovs_skb_cb *)(skb)->cb)
126129
net/openvswitch/flow_netlink.c+31−1
@@ -64,6 +64,7 @@ static bool actions_may_change_flow(const struct nlattr *actions) · 63 unmodified lines
6464 case OVS_ACTION_ATTR_TRUNC:
6565 case OVS_ACTION_ATTR_USERSPACE:
6666 case OVS_ACTION_ATTR_DROP:
67+ case OVS_ACTION_ATTR_PSAMPLE:
6768 break;
6869
6970 case OVS_ACTION_ATTR_CT:
@@ -2409,7 +2410,7 @@ static void ovs_nla_free_nested_actions(const struct nlattr *actions, int len) · 2339 unmodified lines
24092410 /* Whenever new actions are added, the need to update this
24102411 * function should be considered.
24112412 */
2412− BUILD_BUG_ON(OVS_ACTION_ATTR_MAX != 24);
2413+ BUILD_BUG_ON(OVS_ACTION_ATTR_MAX != 25);
24132414
24142415 if (!actions)
24152416 return;
@@ -3157,6 +3158,28 @@ static int validate_and_copy_check_pkt_len(struct net *net, · 741 unmodified lines
31573158 return 0;
31583159 }
31593160
3161+static int validate_psample(const struct nlattr *attr)
3162+{
3163+ static const struct nla_policy policy[OVS_PSAMPLE_ATTR_MAX + 1] = {
3164+ [OVS_PSAMPLE_ATTR_GROUP] = { .type = NLA_U32 },
3165+ [OVS_PSAMPLE_ATTR_COOKIE] = {
3166+ .type = NLA_BINARY,
3167+ .len = OVS_PSAMPLE_COOKIE_MAX_SIZE,
3168+ },
3169+ };
3170+ struct nlattr *a[OVS_PSAMPLE_ATTR_MAX + 1];
3171+ int err;
3172+
3173+ if (!IS_ENABLED(CONFIG_PSAMPLE))
3174+ return -EOPNOTSUPP;
3175+
3176+ err = nla_parse_nested(a, OVS_PSAMPLE_ATTR_MAX, attr, policy, NULL);
3177+ if (err)
3178+ return err;
3179+
3180+ return a[OVS_PSAMPLE_ATTR_GROUP] ? 0 : -EINVAL;
3181+}
3182+
31603183 static int copy_action(const struct nlattr *from,
31613184 struct sw_flow_actions **sfa, bool log)
31623185 {
@@ -3212,6 +3235,7 @@ static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr, · 49 unmodified lines
32123235 [OVS_ACTION_ATTR_ADD_MPLS] = sizeof(struct ovs_action_add_mpls),
32133236 [OVS_ACTION_ATTR_DEC_TTL] = (u32)-1,
32143237 [OVS_ACTION_ATTR_DROP] = sizeof(u32),
3238+ [OVS_ACTION_ATTR_PSAMPLE] = (u32)-1,
32153239 };
32163240 const struct ovs_action_push_vlan *vlan;
32173241 int type = nla_type(a);
@@ -3490,6 +3514,12 @@ static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr, · 272 unmodified lines
34903514 return -EINVAL;
34913515 break;
34923516
3517+ case OVS_ACTION_ATTR_PSAMPLE:
3518+ err = validate_psample(a);
3519+ if (err)
3520+ return err;
3521+ break;
3522+
34933523 default:
34943524 OVS_NLERR(log, "Unknown Action type %d", type);
34953525 return -EINVAL;
net/openvswitch/vport.c+1−0
@@ -500,6 +500,7 @@ int ovs_vport_receive(struct vport *vport, struct sk_buff *skb, · 499 unmodified lines
500500 OVS_CB(skb)->input_vport = vport;
501501 OVS_CB(skb)->mru = 0;
502502 OVS_CB(skb)->cutlen = 0;
503+ OVS_CB(skb)->probability = 0;
503504 if (unlikely(dev_net(skb->dev) != ovs_dp_get_net(vport->dp))) {
504505 u32 mark;
505506
net/psample/psample.c+15−1
@@ -376,6 +376,10 @@ void psample_sample_packet(struct psample_group *group, struct sk_buff *skb, · 375 unmodified lines
376376 void *data;
377377 int ret;
378378
379+ if (!genl_has_listeners(&psample_nl_family, group->net,
380+ PSAMPLE_NL_MCGRP_SAMPLE))
381+ return;
382+
379383 meta_len = (in_ifindex ? nla_total_size(sizeof(u16)) : 0) +
380384 (out_ifindex ? nla_total_size(sizeof(u16)) : 0) +
381385 (md->out_tc_valid ? nla_total_size(sizeof(u16)) : 0) +
@@ -386,7 +390,9 @@ void psample_sample_packet(struct psample_group *group, struct sk_buff *skb, · 4 unmodified lines
386390 nla_total_size(sizeof(u32)) + /* group_num */
387391 nla_total_size(sizeof(u32)) + /* seq */
388392 nla_total_size_64bit(sizeof(u64)) + /* timestamp */
389− nla_total_size(sizeof(u16)); /* protocol */
393+ nla_total_size(sizeof(u16)) + /* protocol */
394+ (md->user_cookie_len ?
395+ nla_total_size(md->user_cookie_len) : 0); /* user cookie */
390396
391397 #ifdef CONFIG_INET
392398 tun_info = skb_tunnel_info(skb);
@@ -486,6 +492,14 @@ void psample_sample_packet(struct psample_group *group, struct sk_buff *skb, · 93 unmodified lines
486492 }
487493 #endif
488494
495+ if (md->user_cookie && md->user_cookie_len &&
496+ nla_put(nl_skb, PSAMPLE_ATTR_USER_COOKIE, md->user_cookie_len,
497+ md->user_cookie))
498+ goto error;
499+
500+ if (md->rate_as_probability)
501+ nla_put_flag(skb, PSAMPLE_ATTR_SAMPLE_PROBABILITY);
502+
489503 genlmsg_end(nl_skb, data);
490504 genlmsg_multicast_netns(&psample_nl_family, group->net, nl_skb, 0,
491505 PSAMPLE_NL_MCGRP_SAMPLE, GFP_ATOMIC);
net/sched/act_sample.c+12−0
@@ -167,7 +167,9 @@ TC_INDIRECT_SCOPE int tcf_sample_act(struct sk_buff *skb, · 166 unmodified lines
167167 {
168168 struct tcf_sample *s = to_sample(a);
169169 struct psample_group *psample_group;
170+ u8 cookie_data[TC_COOKIE_MAX_SIZE];
170171 struct psample_metadata md = {};
172+ struct tc_cookie *user_cookie;
171173 int retval;
172174
173175 tcf_lastuse_update(&s->tcf_tm);
@@ -189,6 +191,16 @@ TC_INDIRECT_SCOPE int tcf_sample_act(struct sk_buff *skb, · 15 unmodified lines
189191 if (skb_at_tc_ingress(skb) && tcf_sample_dev_ok_push(skb->dev))
190192 skb_push(skb, skb->mac_len);
191193
194+ rcu_read_lock();
195+ user_cookie = rcu_dereference(a->user_cookie);
196+ if (user_cookie) {
197+ memcpy(cookie_data, user_cookie->data,
198+ user_cookie->len);
199+ md.user_cookie = cookie_data;
200+ md.user_cookie_len = user_cookie->len;
201+ }
202+ rcu_read_unlock();
203+
192204 md.trunc_size = s->truncate ? s->trunc_size : skb->len;
193205 psample_sample_packet(psample_group, skb, s->rate, &md);
194206
tools/testing/selftests/net/openvswitch/openvswitch.sh+112−3
@@ -20,7 +20,8 @@ tests=" · 19 unmodified lines
2020 nat_related_v4 ip4-nat-related: ICMP related matches work with SNAT
2121 netlink_checks ovsnl: validate netlink attrs and settings
2222 upcall_interfaces ovs: test the upcall interfaces
23− drop_reason drop: test drop reasons are emitted"
23+ drop_reason drop: test drop reasons are emitted
24+ psample psample: Sampling packets with psample"
2425
2526 info() {
2627 [ "${ovs_dir}" != "" ] &&
@@ -105,12 +106,21 @@ ovs_netns_spawn_daemon() { · 78 unmodified lines
105106 shift
106107 netns=$1
107108 shift
108− info "spawning cmd: $*"
109− ip netns exec $netns $* >> $ovs_dir/stdout 2>> $ovs_dir/stderr &
109+ if [ "$netns" == "_default" ]; then
110+ $* >> $ovs_dir/stdout 2>> $ovs_dir/stderr &
111+ else
112+ ip netns exec $netns $* >> $ovs_dir/stdout 2>> $ovs_dir/stderr &
113+ fi
110114 pid=$!
111115 ovs_sbx "$sbx" on_exit "kill -TERM $pid 2>/dev/null"
112116 }
113117
118+ovs_spawn_daemon() {
119+ sbx=$1
120+ shift
121+ ovs_netns_spawn_daemon $sbx "_default" $*
122+}
123+
114124 ovs_add_netns_and_veths () {
115125 info "Adding netns attached: sbx:$1 dp:$2 {$3, $4, $5}"
116126 ovs_sbx "$1" ip netns add "$3" || return 1
@@ -173,6 +183,19 @@ ovs_drop_reason_count() · 56 unmodified lines
173183 return `echo "$perf_output" | grep "$pattern" | wc -l`
174184 }
175185
186+ovs_test_flow_fails () {
187+ ERR_MSG="Flow actions may not be safe on all matching packets"
188+
189+ PRE_TEST=$(dmesg | grep -c "${ERR_MSG}")
190+ ovs_add_flow $@ &> /dev/null $@ && return 1
191+ POST_TEST=$(dmesg | grep -c "${ERR_MSG}")
192+
193+ if [ "$PRE_TEST" == "$POST_TEST" ]; then
194+ return 1
195+ fi
196+ return 0
197+}
198+
176199 usage() {
177200 echo
178201 echo "$0 [OPTIONS] [TEST]..."
@@ -187,6 +210,92 @@ usage() { · 8 unmodified lines
187210 exit 1
188211 }
189212
213+
214+# psample test
215+# - use psample to observe packets
216+test_psample() {
217+ sbx_add "test_psample" || return $?
218+
219+ # Add a datapath with per-vport dispatching.
220+ ovs_add_dp "test_psample" psample -V 2:1 || return 1
221+
222+ info "create namespaces"
223+ ovs_add_netns_and_veths "test_psample" "psample" \
224+ client c0 c1 172.31.110.10/24 -u || return 1
225+ ovs_add_netns_and_veths "test_psample" "psample" \
226+ server s0 s1 172.31.110.20/24 -u || return 1
227+
228+ # Check if psample actions can be configured.
229+ ovs_add_flow "test_psample" psample \
230+ 'in_port(1),eth(),eth_type(0x0806),arp()' 'psample(group=1)' &> /dev/null
231+ if [ $? == 1 ]; then
232+ info "no support for psample - skipping"
233+ ovs_exit_sig
234+ return $ksft_skip
235+ fi
236+
237+ ovs_del_flows "test_psample" psample
238+
239+ # Test action verification.
240+ OLDIFS=$IFS
241+ IFS='*'
242+ min_key='in_port(1),eth(),eth_type(0x0800),ipv4()'
243+ for testcase in \
244+ "cookie to large"*"psample(group=1,cookie=1615141312111009080706050403020100)" \
245+ "no group with cookie"*"psample(cookie=abcd)" \
246+ "no group"*"psample()";
247+ do
248+ set -- $testcase;
249+ ovs_test_flow_fails "test_psample" psample $min_key $2
250+ if [ $? == 1 ]; then
251+ info "failed - $1"
252+ return 1
253+ fi
254+ done
255+ IFS=$OLDIFS
256+
257+ ovs_del_flows "test_psample" psample
258+ # Allow ARP
259+ ovs_add_flow "test_psample" psample \
260+ 'in_port(1),eth(),eth_type(0x0806),arp()' '2' || return 1
261+ ovs_add_flow "test_psample" psample \
262+ 'in_port(2),eth(),eth_type(0x0806),arp()' '1' || return 1
263+
264+ # Sample first 14 bytes of all traffic.
265+ ovs_add_flow "test_psample" psample \
266+ "in_port(1),eth(),eth_type(0x0800),ipv4()" \
267+ "trunc(14),psample(group=1,cookie=c0ffee),2"
268+
269+ # Sample all traffic. In this case, use a sample() action with both
270+ # psample and an upcall emulating simultaneous local sampling and
271+ # sFlow / IPFIX.
272+ nlpid=$(grep -E "listening on upcall packet handler" \
273+ $ovs_dir/s0.out | cut -d ":" -f 2 | tr -d ' ')
274+
275+ ovs_add_flow "test_psample" psample \
276+ "in_port(2),eth(),eth_type(0x0800),ipv4()" \
277+ "sample(sample=100%,actions(psample(group=2,cookie=eeff0c),userspace(pid=${nlpid},userdata=eeff0c))),1"
278+
279+ # Record psample data.
280+ ovs_spawn_daemon "test_psample" python3 $ovs_base/ovs-dpctl.py psample-events
281+
282+ # Send a single ping.
283+ sleep 1
284+ ovs_sbx "test_psample" ip netns exec client ping -I c1 172.31.110.20 -c 1 || return 1
285+ sleep 1
286+
287+ # We should have received one userspace action upcall and 2 psample packets.
288+ grep -E "userspace action command" $ovs_dir/s0.out >/dev/null 2>&1 || return 1
289+
290+ # client -> server samples should only contain the first 14 bytes of the packet.
291+ grep -E "rate:4294967295,group:1,cookie:c0ffee data:[0-9a-f]{28}$" \
292+ $ovs_dir/stdout >/dev/null 2>&1 || return 1
293+ grep -E "rate:4294967295,group:2,cookie:eeff0c" \
294+ $ovs_dir/stdout >/dev/null 2>&1 || return 1
295+
296+ return 0
297+}
298+
190299 # drop_reason test
191300 # - drop packets and verify the right drop reason is reported
192301 test_drop_reason() {
tools/testing/selftests/net/openvswitch/ovs-dpctl.py+266−6
@@ -8,6 +8,7 @@ import argparse · 7 unmodified lines
88 import errno
99 import ipaddress
1010 import logging
11+import math
1112 import multiprocessing
1213 import re
1314 import socket
@@ -27,8 +28,10 @@ try: · 13 unmodified lines
2728 from pyroute2.netlink import genlmsg
2829 from pyroute2.netlink import nla
2930 from pyroute2.netlink import nlmsg_atoms
31+ from pyroute2.netlink.event import EventSocket
3032 from pyroute2.netlink.exceptions import NetlinkError
3133 from pyroute2.netlink.generic import GenericNetlinkSocket
34+ from pyroute2.netlink.nlsocket import Marshal
3235 import pyroute2
3336 import pyroute2.iproute
3437
@@ -60,6 +63,7 @@ OVS_FLOW_CMD_DEL = 2 · 25 unmodified lines
6063 OVS_FLOW_CMD_GET = 3
6164 OVS_FLOW_CMD_SET = 4
6265
66+UINT32_MAX = 0xFFFFFFFF
6367
6468 def macstr(mac):
6569 outstr = ":".join(["%02X" % i for i in mac])
@@ -281,6 +285,75 @@ def parse_extract_field( · 215 unmodified lines
281285 return str_skipped, data
282286
283287
288+def parse_attrs(actstr, attr_desc):
289+ """Parses the given action string and returns a list of netlink
290+ attributes based on a list of attribute descriptions.
291+
292+ Each element in the attribute description list is a tuple such as:
293+ (name, attr_name, parse_func)
294+ where:
295+ name: is the string representing the attribute
296+ attr_name: is the name of the attribute as defined in the uAPI.
297+ parse_func: is a callable accepting a string and returning either
298+ a single object (the parsed attribute value) or a tuple of
299+ two values (the parsed attribute value and the remaining string)
300+
301+ Returns a list of attributes and the remaining string.
302+ """
303+ def parse_attr(actstr, key, func):
304+ actstr = actstr[len(key) :]
305+
306+ if not func:
307+ return None, actstr
308+
309+ delim = actstr[0]
310+ actstr = actstr[1:]
311+
312+ if delim == "=":
313+ pos = strcspn(actstr, ",)")
314+ ret = func(actstr[:pos])
315+ else:
316+ ret = func(actstr)
317+
318+ if isinstance(ret, tuple):
319+ (datum, actstr) = ret
320+ else:
321+ datum = ret
322+ actstr = actstr[strcspn(actstr, ",)"):]
323+
324+ if delim == "(":
325+ if not actstr or actstr[0] != ")":
326+ raise ValueError("Action contains unbalanced parentheses")
327+
328+ actstr = actstr[1:]
329+
330+ actstr = actstr[strspn(actstr, ", ") :]
331+
332+ return datum, actstr
333+
334+ attrs = []
335+ attr_desc = list(attr_desc)
336+ while actstr and actstr[0] != ")" and attr_desc:
337+ found = False
338+ for i, (key, attr, func) in enumerate(attr_desc):
339+ if actstr.startswith(key):
340+ datum, actstr = parse_attr(actstr, key, func)
341+ attrs.append([attr, datum])
342+ found = True
343+ del attr_desc[i]
344+
345+ if not found:
346+ raise ValueError("Unknown attribute: '%s'" % actstr)
347+
348+ actstr = actstr[strspn(actstr, ", ") :]
349+
350+ if actstr[0] != ")":
351+ raise ValueError("Action string contains extra garbage or has "
352+ "unbalanced parenthesis: '%s'" % actstr)
353+
354+ return attrs, actstr[1:]
355+
356+
284357 class ovs_dp_msg(genlmsg):
285358 # include the OVS version
286359 # We need a custom header rather than just being able to rely on
@@ -299,7 +372,7 @@ class ovsactions(nla): · 12 unmodified lines
299372 ("OVS_ACTION_ATTR_SET", "ovskey"),
300373 ("OVS_ACTION_ATTR_PUSH_VLAN", "none"),
301374 ("OVS_ACTION_ATTR_POP_VLAN", "flag"),
302− ("OVS_ACTION_ATTR_SAMPLE", "none"),
375+ ("OVS_ACTION_ATTR_SAMPLE", "sample"),
303376 ("OVS_ACTION_ATTR_RECIRC", "uint32"),
304377 ("OVS_ACTION_ATTR_HASH", "none"),
305378 ("OVS_ACTION_ATTR_PUSH_MPLS", "none"),
@@ -318,8 +391,85 @@ class ovsactions(nla): · 12 unmodified lines
318391 ("OVS_ACTION_ATTR_ADD_MPLS", "none"),
319392 ("OVS_ACTION_ATTR_DEC_TTL", "none"),
320393 ("OVS_ACTION_ATTR_DROP", "uint32"),
394+ ("OVS_ACTION_ATTR_PSAMPLE", "psample"),
321395 )
322396
397+ class psample(nla):
398+ nla_flags = NLA_F_NESTED
399+
400+ nla_map = (
401+ ("OVS_PSAMPLE_ATTR_UNSPEC", "none"),
402+ ("OVS_PSAMPLE_ATTR_GROUP", "uint32"),
403+ ("OVS_PSAMPLE_ATTR_COOKIE", "array(uint8)"),
404+ )
405+
406+ def dpstr(self, more=False):
407+ args = "group=%d" % self.get_attr("OVS_PSAMPLE_ATTR_GROUP")
408+
409+ cookie = self.get_attr("OVS_PSAMPLE_ATTR_COOKIE")
410+ if cookie:
411+ args += ",cookie(%s)" % \
412+ "".join(format(x, "02x") for x in cookie)
413+
414+ return "psample(%s)" % args
415+
416+ def parse(self, actstr):
417+ desc = (
418+ ("group", "OVS_PSAMPLE_ATTR_GROUP", int),
419+ ("cookie", "OVS_PSAMPLE_ATTR_COOKIE",
420+ lambda x: list(bytearray.fromhex(x)))
421+ )
422+
423+ attrs, actstr = parse_attrs(actstr, desc)
424+
425+ for attr in attrs:
426+ self["attrs"].append(attr)
427+
428+ return actstr
429+
430+ class sample(nla):
431+ nla_flags = NLA_F_NESTED
432+
433+ nla_map = (
434+ ("OVS_SAMPLE_ATTR_UNSPEC", "none"),
435+ ("OVS_SAMPLE_ATTR_PROBABILITY", "uint32"),
436+ ("OVS_SAMPLE_ATTR_ACTIONS", "ovsactions"),
437+ )
438+
439+ def dpstr(self, more=False):
440+ args = []
441+
442+ args.append("sample={:.2f}%".format(
443+ 100 * self.get_attr("OVS_SAMPLE_ATTR_PROBABILITY") /
444+ UINT32_MAX))
445+
446+ actions = self.get_attr("OVS_SAMPLE_ATTR_ACTIONS")
447+ if actions:
448+ args.append("actions(%s)" % actions.dpstr(more))
449+
450+ return "sample(%s)" % ",".join(args)
451+
452+ def parse(self, actstr):
453+ def parse_nested_actions(actstr):
454+ subacts = ovsactions()
455+ parsed_len = subacts.parse(actstr)
456+ return subacts, actstr[parsed_len :]
457+
458+ def percent_to_rate(percent):
459+ percent = float(percent.strip('%'))
460+ return int(math.floor(UINT32_MAX * (percent / 100.0) + .5))
461+
462+ desc = (
463+ ("sample", "OVS_SAMPLE_ATTR_PROBABILITY", percent_to_rate),
464+ ("actions", "OVS_SAMPLE_ATTR_ACTIONS", parse_nested_actions),
465+ )
466+ attrs, actstr = parse_attrs(actstr, desc)
467+
468+ for attr in attrs:
469+ self["attrs"].append(attr)
470+
471+ return actstr
472+
323473 class ctact(nla):
324474 nla_flags = NLA_F_NESTED
325475
@@ -441,13 +591,27 @@ class ovsactions(nla): · 115 unmodified lines
441591 print_str += "userdata="
442592 for f in self.get_attr("OVS_USERSPACE_ATTR_USERDATA"):
443593 print_str += "%x." % f
444− if self.get_attr("OVS_USERSPACE_ATTR_TUN_PORT") is not None:
594+ if self.get_attr("OVS_USERSPACE_ATTR_EGRESS_TUN_PORT") is not None:
445595 print_str += "egress_tun_port=%d" % self.get_attr(
446− "OVS_USERSPACE_ATTR_TUN_PORT"
596+ "OVS_USERSPACE_ATTR_EGRESS_TUN_PORT"
447597 )
448598 print_str += ")"
449599 return print_str
450600
601+ def parse(self, actstr):
602+ attrs_desc = (
603+ ("pid", "OVS_USERSPACE_ATTR_PID", int),
604+ ("userdata", "OVS_USERSPACE_ATTR_USERDATA",
605+ lambda x: list(bytearray.fromhex(x))),
606+ ("egress_tun_port", "OVS_USERSPACE_ATTR_EGRESS_TUN_PORT", int)
607+ )
608+
609+ attrs, actstr = parse_attrs(actstr, attrs_desc)
610+ for attr in attrs:
611+ self["attrs"].append(attr)
612+
613+ return actstr
614+
451615 def dpstr(self, more=False):
452616 print_str = ""
453617
@@ -683,6 +847,37 @@ class ovsactions(nla): · 229 unmodified lines
683847 self["attrs"].append(["OVS_ACTION_ATTR_CT", ctact])
684848 parsed = True
685849
850+ elif parse_starts_block(actstr, "sample(", False):
851+ sampleact = self.sample()
852+ actstr = sampleact.parse(actstr[len("sample(") : ])
853+ self["attrs"].append(["OVS_ACTION_ATTR_SAMPLE", sampleact])
854+ parsed = True
855+
856+ elif parse_starts_block(actstr, "psample(", False):
857+ psampleact = self.psample()
858+ actstr = psampleact.parse(actstr[len("psample(") : ])
859+ self["attrs"].append(["OVS_ACTION_ATTR_PSAMPLE", psampleact])
860+ parsed = True
861+
862+ elif parse_starts_block(actstr, "userspace(", False):
863+ uact = self.userspace()
864+ actstr = uact.parse(actstr[len("userspace(") : ])
865+ self["attrs"].append(["OVS_ACTION_ATTR_USERSPACE", uact])
866+ parsed = True
867+
868+ elif parse_starts_block(actstr, "trunc(", False):
869+ parencount += 1
870+ actstr, val = parse_extract_field(
871+ actstr,
872+ "trunc(",
873+ r"([0-9]+)",
874+ int,
875+ False,
876+ None,
877+ )
878+ self["attrs"].append(["OVS_ACTION_ATTR_TRUNC", val])
879+ parsed = True
880+
686881 actstr = actstr[strspn(actstr, ", ") :]
687882 while parencount > 0:
688883 parencount -= 1
@@ -2267,10 +2462,70 @@ class OvsFlow(GenericNetlinkSocket): · 1578 unmodified lines
22672462 print("MISS upcall[%d/%s]: %s" % (seq, pktpres, keystr), flush=True)
22682463
22692464 def execute(self, packetmsg):
2270− print("userspace execute command")
2465+ print("userspace execute command", flush=True)
22712466
22722467 def action(self, packetmsg):
2273− print("userspace action command")
2468+ print("userspace action command", flush=True)
2469+
2470+
2471+class psample_sample(genlmsg):
2472+ nla_map = (
2473+ ("PSAMPLE_ATTR_IIFINDEX", "none"),
2474+ ("PSAMPLE_ATTR_OIFINDEX", "none"),
2475+ ("PSAMPLE_ATTR_ORIGSIZE", "none"),
2476+ ("PSAMPLE_ATTR_SAMPLE_GROUP", "uint32"),
2477+ ("PSAMPLE_ATTR_GROUP_SEQ", "none"),
2478+ ("PSAMPLE_ATTR_SAMPLE_RATE", "uint32"),
2479+ ("PSAMPLE_ATTR_DATA", "array(uint8)"),
2480+ ("PSAMPLE_ATTR_GROUP_REFCOUNT", "none"),
2481+ ("PSAMPLE_ATTR_TUNNEL", "none"),
2482+ ("PSAMPLE_ATTR_PAD", "none"),
2483+ ("PSAMPLE_ATTR_OUT_TC", "none"),
2484+ ("PSAMPLE_ATTR_OUT_TC_OCC", "none"),
2485+ ("PSAMPLE_ATTR_LATENCY", "none"),
2486+ ("PSAMPLE_ATTR_TIMESTAMP", "none"),
2487+ ("PSAMPLE_ATTR_PROTO", "none"),
2488+ ("PSAMPLE_ATTR_USER_COOKIE", "array(uint8)"),
2489+ )
2490+
2491+ def dpstr(self):
2492+ fields = []
2493+ data = ""
2494+ for (attr, value) in self["attrs"]:
2495+ if attr == "PSAMPLE_ATTR_SAMPLE_GROUP":
2496+ fields.append("group:%d" % value)
2497+ if attr == "PSAMPLE_ATTR_SAMPLE_RATE":
2498+ fields.append("rate:%d" % value)
2499+ if attr == "PSAMPLE_ATTR_USER_COOKIE":
2500+ value = "".join(format(x, "02x") for x in value)
2501+ fields.append("cookie:%s" % value)
2502+ if attr == "PSAMPLE_ATTR_DATA" and len(value) > 0:
2503+ data = "data:%s" % "".join(format(x, "02x") for x in value)
2504+
2505+ return ("%s %s" % (",".join(fields), data)).strip()
2506+
2507+
2508+class psample_msg(Marshal):
2509+ PSAMPLE_CMD_SAMPLE = 0
2510+ PSAMPLE_CMD_GET_GROUP = 1
2511+ PSAMPLE_CMD_NEW_GROUP = 2
2512+ PSAMPLE_CMD_DEL_GROUP = 3
2513+ PSAMPLE_CMD_SET_FILTER = 4
2514+ msg_map = {PSAMPLE_CMD_SAMPLE: psample_sample}
2515+
2516+
2517+class PsampleEvent(EventSocket):
2518+ genl_family = "psample"
2519+ mcast_groups = ["packets"]
2520+ marshal_class = psample_msg
2521+
2522+ def read_samples(self):
2523+ while True:
2524+ try:
2525+ for msg in self.get():
2526+ print(msg.dpstr(), flush=True)
2527+ except NetlinkError as ne:
2528+ raise ne
22742529
22752530
22762531 def print_ovsdp_full(dp_lookup_rep, ifindex, ndb=NDB(), vpl=OvsVport()):
@@ -2337,7 +2592,7 @@ def main(argv): · 60 unmodified lines
23372592 help="Increment 'verbose' output counter.",
23382593 default=0,
23392594 )
2340− subparsers = parser.add_subparsers()
2595+ subparsers = parser.add_subparsers(dest="subcommand")
23412596
23422597 showdpcmd = subparsers.add_parser("show")
23432598 showdpcmd.add_argument(
@@ -2412,6 +2667,8 @@ def main(argv): · 68 unmodified lines
24122667 delfscmd = subparsers.add_parser("del-flows")
24132668 delfscmd.add_argument("flsbr", help="Datapath name")
24142669
2670+ subparsers.add_parser("psample-events")
2671+
24152672 args = parser.parse_args()
24162673
24172674 if args.verbose > 0:
@@ -2426,6 +2683,9 @@ def main(argv): · 8 unmodified lines
24262683
24272684 sys.setrecursionlimit(100000)
24282685
2686+ if args.subcommand == "psample-events":
2687+ PsampleEvent().read_samples()
2688+
24292689 if hasattr(args, "showdp"):
24302690 found = False
24312691 for iface in ndb.interfaces:

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t