Skip to content

Commit 197569f

Browse files
committed
Merge branch 'TC-datapath-hash-api'
Ariel Levkovich says: ==================== TC datapath hash api Hash based packet classification allows user to set up rules that provide load balancing of traffic across multiple vports and for ECMP path selection while keeping the number of rule at minimum. Instead of matching on exact flow spec, which requires a rule per flow, user can define rules based on a their hash value and distribute the flows to different buckets. The number of rules in this case will be constant and equal to the number of buckets. The series introduces an extention to the cls flower classifier and allows user to add rules that match on the hash value that is stored in skb->hash while assuming the value was set prior to the classification. Setting the skb->hash can be done in various ways and is not defined in this series - for example: 1. By the device driver upon processing an rx packet. 2. Using tc action bpf with a program which computes and sets the skb->hash value. $ tc filter add dev ens1f0_0 ingress \ prio 1 chain 2 proto ip \ flower hash 0x0/0xf \ action mirred egress redirect dev ens1f0_1 $ tc filter add dev ens1f0_0 ingress \ prio 1 chain 2 proto ip \ flower hash 0x1/0xf \ action mirred egress redirect dev ens1f0_2 v3 -> v4: *Drop hash setting code leaving only the classidication parts. Setting the hash will be possible via existing tc action bpf. v2 -> v3: *Split hash algorithm option into 2 different actions. Asym_l4 available via act_skbedit and bpf via new act_hash. ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
2 parents 4a062d6 + 5923b8f commit 197569f

5 files changed

Lines changed: 49 additions & 0 deletions

File tree

‎include/linux/skbuff.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,6 +1342,10 @@ skb_flow_dissect_tunnel_info(const struct sk_buff *skb,
13421342
struct flow_dissector *flow_dissector,
13431343
void *target_container);
13441344

1345+
void skb_flow_dissect_hash(const struct sk_buff *skb,
1346+
struct flow_dissector *flow_dissector,
1347+
void *target_container);
1348+
13451349
static inline __u32 skb_get_hash(struct sk_buff *skb)
13461350
{
13471351
if (!skb->l4_hash && !skb->sw_hash)

‎include/net/flow_dissector.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,14 @@ struct flow_dissector_key_ct {
243243
u32 ct_labels[4];
244244
};
245245

246+
/**
247+
* struct flow_dissector_key_hash:
248+
* @hash: hash value
249+
*/
250+
struct flow_dissector_key_hash {
251+
u32 hash;
252+
};
253+
246254
enum flow_dissector_key_id {
247255
FLOW_DISSECTOR_KEY_CONTROL, /* struct flow_dissector_key_control */
248256
FLOW_DISSECTOR_KEY_BASIC, /* struct flow_dissector_key_basic */
@@ -271,6 +279,7 @@ enum flow_dissector_key_id {
271279
FLOW_DISSECTOR_KEY_ENC_OPTS, /* struct flow_dissector_key_enc_opts */
272280
FLOW_DISSECTOR_KEY_META, /* struct flow_dissector_key_meta */
273281
FLOW_DISSECTOR_KEY_CT, /* struct flow_dissector_key_ct */
282+
FLOW_DISSECTOR_KEY_HASH, /* struct flow_dissector_key_hash */
274283

275284
FLOW_DISSECTOR_KEY_MAX,
276285
};

‎include/uapi/linux/pkt_cls.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,9 @@ enum {
578578

579579
TCA_FLOWER_KEY_MPLS_OPTS,
580580

581+
TCA_FLOWER_KEY_HASH, /* u32 */
582+
TCA_FLOWER_KEY_HASH_MASK, /* u32 */
583+
581584
__TCA_FLOWER_MAX,
582585
};
583586

‎net/core/flow_dissector.c‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,23 @@ skb_flow_dissect_tunnel_info(const struct sk_buff *skb,
383383
}
384384
EXPORT_SYMBOL(skb_flow_dissect_tunnel_info);
385385

386+
void skb_flow_dissect_hash(const struct sk_buff *skb,
387+
struct flow_dissector *flow_dissector,
388+
void *target_container)
389+
{
390+
struct flow_dissector_key_hash *key;
391+
392+
if (!dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_HASH))
393+
return;
394+
395+
key = skb_flow_dissector_target(flow_dissector,
396+
FLOW_DISSECTOR_KEY_HASH,
397+
target_container);
398+
399+
key->hash = skb_get_hash_raw(skb);
400+
}
401+
EXPORT_SYMBOL(skb_flow_dissect_hash);
402+
386403
static enum flow_dissect_ret
387404
__skb_flow_dissect_mpls(const struct sk_buff *skb,
388405
struct flow_dissector *flow_dissector,

‎net/sched/cls_flower.c‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ struct fl_flow_key {
6464
};
6565
} tp_range;
6666
struct flow_dissector_key_ct ct;
67+
struct flow_dissector_key_hash hash;
6768
} __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
6869

6970
struct fl_flow_mask_range {
@@ -318,6 +319,7 @@ static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp,
318319
skb_flow_dissect_ct(skb, &mask->dissector, &skb_key,
319320
fl_ct_info_to_flower_map,
320321
ARRAY_SIZE(fl_ct_info_to_flower_map));
322+
skb_flow_dissect_hash(skb, &mask->dissector, &skb_key);
321323
skb_flow_dissect(skb, &mask->dissector, &skb_key, 0);
322324

323325
f = fl_mask_lookup(mask, &skb_key);
@@ -695,6 +697,9 @@ static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = {
695697
[TCA_FLOWER_KEY_CT_LABELS_MASK] = { .type = NLA_BINARY,
696698
.len = 128 / BITS_PER_BYTE },
697699
[TCA_FLOWER_FLAGS] = { .type = NLA_U32 },
700+
[TCA_FLOWER_KEY_HASH] = { .type = NLA_U32 },
701+
[TCA_FLOWER_KEY_HASH_MASK] = { .type = NLA_U32 },
702+
698703
};
699704

700705
static const struct nla_policy
@@ -1626,6 +1631,10 @@ static int fl_set_key(struct net *net, struct nlattr **tb,
16261631

16271632
fl_set_key_ip(tb, true, &key->enc_ip, &mask->enc_ip);
16281633

1634+
fl_set_key_val(tb, &key->hash.hash, TCA_FLOWER_KEY_HASH,
1635+
&mask->hash.hash, TCA_FLOWER_KEY_HASH_MASK,
1636+
sizeof(key->hash.hash));
1637+
16291638
if (tb[TCA_FLOWER_KEY_ENC_OPTS]) {
16301639
ret = fl_set_enc_opt(tb, key, mask, extack);
16311640
if (ret)
@@ -1740,6 +1749,8 @@ static void fl_init_dissector(struct flow_dissector *dissector,
17401749
FLOW_DISSECTOR_KEY_ENC_OPTS, enc_opts);
17411750
FL_KEY_SET_IF_MASKED(mask, keys, cnt,
17421751
FLOW_DISSECTOR_KEY_CT, ct);
1752+
FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1753+
FLOW_DISSECTOR_KEY_HASH, hash);
17431754

17441755
skb_flow_dissector_init(dissector, keys, cnt);
17451756
}
@@ -2960,6 +2971,11 @@ static int fl_dump_key(struct sk_buff *skb, struct net *net,
29602971
if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags))
29612972
goto nla_put_failure;
29622973

2974+
if (fl_dump_key_val(skb, &key->hash.hash, TCA_FLOWER_KEY_HASH,
2975+
&mask->hash.hash, TCA_FLOWER_KEY_HASH_MASK,
2976+
sizeof(key->hash.hash)))
2977+
goto nla_put_failure;
2978+
29632979
return 0;
29642980

29652981
nla_put_failure:

0 commit comments

Comments
 (0)