Skip to content

Commit 95ec3eb

Browse files
committed
packet: Add 'cpu' fanout policy.
Unfortunately we have to use a real modulus here as the multiply trick won't work as effectively with cpu numbers as it does with rxhash values. Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 81b16ba commit 95ec3eb

File tree

2 files changed

+29
-37
lines changed

2 files changed

+29
-37
lines changed

include/linux/if_packet.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ struct sockaddr_ll {
5353

5454
#define PACKET_FANOUT_HASH 0
5555
#define PACKET_FANOUT_LB 1
56+
#define PACKET_FANOUT_CPU 2
5657
#define PACKET_FANOUT_FLAG_DEFRAG 0x8000
5758

5859
struct tpacket_stats {

net/packet/af_packet.c

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,13 @@ static struct sock *fanout_demux_lb(struct packet_fanout *f, struct sk_buff *skb
447447
return f->arr[cur];
448448
}
449449

450+
static struct sock *fanout_demux_cpu(struct packet_fanout *f, struct sk_buff *skb, unsigned int num)
451+
{
452+
unsigned int cpu = smp_processor_id();
453+
454+
return f->arr[cpu % num];
455+
}
456+
450457
static struct sk_buff *fanout_check_defrag(struct sk_buff *skb)
451458
{
452459
const struct iphdr *iph;
@@ -482,8 +489,8 @@ static struct sk_buff *fanout_check_defrag(struct sk_buff *skb)
482489
return skb;
483490
}
484491

485-
static int packet_rcv_fanout_hash(struct sk_buff *skb, struct net_device *dev,
486-
struct packet_type *pt, struct net_device *orig_dev)
492+
static int packet_rcv_fanout(struct sk_buff *skb, struct net_device *dev,
493+
struct packet_type *pt, struct net_device *orig_dev)
487494
{
488495
struct packet_fanout *f = pt->af_packet_priv;
489496
unsigned int num = f->num_members;
@@ -496,35 +503,25 @@ static int packet_rcv_fanout_hash(struct sk_buff *skb, struct net_device *dev,
496503
return 0;
497504
}
498505

499-
if (f->defrag) {
500-
skb = fanout_check_defrag(skb);
501-
if (!skb)
502-
return 0;
503-
}
504-
505-
skb_get_rxhash(skb);
506-
507-
sk = fanout_demux_hash(f, skb, num);
508-
po = pkt_sk(sk);
509-
510-
return po->prot_hook.func(skb, dev, &po->prot_hook, orig_dev);
511-
}
512-
513-
static int packet_rcv_fanout_lb(struct sk_buff *skb, struct net_device *dev,
514-
struct packet_type *pt, struct net_device *orig_dev)
515-
{
516-
struct packet_fanout *f = pt->af_packet_priv;
517-
unsigned int num = f->num_members;
518-
struct packet_sock *po;
519-
struct sock *sk;
520-
521-
if (!net_eq(dev_net(dev), read_pnet(&f->net)) ||
522-
!num) {
523-
kfree_skb(skb);
524-
return 0;
506+
switch (f->type) {
507+
case PACKET_FANOUT_HASH:
508+
default:
509+
if (f->defrag) {
510+
skb = fanout_check_defrag(skb);
511+
if (!skb)
512+
return 0;
513+
}
514+
skb_get_rxhash(skb);
515+
sk = fanout_demux_hash(f, skb, num);
516+
break;
517+
case PACKET_FANOUT_LB:
518+
sk = fanout_demux_lb(f, skb, num);
519+
break;
520+
case PACKET_FANOUT_CPU:
521+
sk = fanout_demux_cpu(f, skb, num);
522+
break;
525523
}
526524

527-
sk = fanout_demux_lb(f, skb, num);
528525
po = pkt_sk(sk);
529526

530527
return po->prot_hook.func(skb, dev, &po->prot_hook, orig_dev);
@@ -571,6 +568,7 @@ static int fanout_add(struct sock *sk, u16 id, u16 type_flags)
571568
switch (type) {
572569
case PACKET_FANOUT_HASH:
573570
case PACKET_FANOUT_LB:
571+
case PACKET_FANOUT_CPU:
574572
break;
575573
default:
576574
return -EINVAL;
@@ -606,14 +604,7 @@ static int fanout_add(struct sock *sk, u16 id, u16 type_flags)
606604
atomic_set(&match->sk_ref, 0);
607605
match->prot_hook.type = po->prot_hook.type;
608606
match->prot_hook.dev = po->prot_hook.dev;
609-
switch (type) {
610-
case PACKET_FANOUT_HASH:
611-
match->prot_hook.func = packet_rcv_fanout_hash;
612-
break;
613-
case PACKET_FANOUT_LB:
614-
match->prot_hook.func = packet_rcv_fanout_lb;
615-
break;
616-
}
607+
match->prot_hook.func = packet_rcv_fanout;
617608
match->prot_hook.af_packet_priv = match;
618609
dev_add_pack(&match->prot_hook);
619610
list_add(&match->list, &fanout_list);

0 commit comments

Comments
 (0)