packet: add sockopt to ignore outgoing packets
Subsystems (3)
PATCH 1/1 packet: add sockopt to ignore outgoing packets
Currently, the only way to ignore outgoing packets on a packet socket is via the BPF filter. With MSG_ZEROCOPY, packets that are looped into AF_PACKET are copied in dev_queue_xmit_nit(), and this copy happens even if the filter run from packet_rcv() would reject them. So the presence of a packet socket on the interface takes away the benefits of MSG_ZEROCOPY, even if the packet socket is not interested in outgoing packets. (Even when MSG_ZEROCOPY is not used, the skb is unnecessarily cloned, but the cost for that is much lower.)
Add a socket option to allow AF_PACKET sockets to ignore outgoing packets to solve this. Note that the *BSDs already have something
similar: BIOCSSEESENT/BIOCSDIRECTION and BIOCSDIRFILT.
The first intended user is lldpd.
--- include/linux/netdevice.h | 1 + include/uapi/linux/if_packet.h | 1 + net/core/dev.c | 3 +++ net/packet/af_packet.c | 15 +++++++++++++++ 4 files changed, 20 insertions(+)
include/linux/netdevice.h +1 -0
@@ -2317,6 +2317,7 @@ static inline struct sk_buff *call_gro_receive_sk(gro_receive_sk_t cb,struct packet_type {__be16 type;/* This is really htons(ether_type). */ +bool ignore_outgoing;struct net_device *dev;/* NULL is wildcarded here */ int (*func) (struct sk_buff *, struct net_device *,
include/uapi/linux/if_packet.h +1 -0
@@ -57,6 +57,7 @@ struct sockaddr_ll {#define PACKET_QDISC_BYPASS 20#define PACKET_ROLLOVER_STATS 21#define PACKET_FANOUT_DATA 22 +#define PACKET_IGNORE_OUTGOING 23#define PACKET_FANOUT_HASH 0#define PACKET_FANOUT_LB 1
net/core/dev.c +3 -0
@@ -1947,6 +1947,9 @@ static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb)if (!ptype->af_packet_priv || !skb->sk)return false; +if (ptype->ignore_outgoing) +return true; +if (ptype->id_match)return ptype->id_match (ptype, skb->sk);else if ((struct sock *)ptype->af_packet_priv == skb->sk)
net/packet/af_packet.c +15 -0
@@ -3805,6 +3805,18 @@ packet_setsockopt(struct socket *sock, int level, int optname, char __user *optvreturn fanout_set_data (po, optval, optlen); } +case PACKET_IGNORE_OUTGOING : + { +int val; + +if (optlen !=sizeof (val)) +return -EINVAL ; +if (copy_from_user (&val, optval,sizeof (val))) +return -EFAULT ; + + po->prot_hook.ignore_outgoing = !!val; +return 0 ; + }case PACKET_TX_HAS_OFF : {unsigned int val;
@@ -3928,6 +3940,9 @@ static int packet_getsockopt(struct socket *sock, int level, int optname, ((u32)po->fanout->flags << 24)) : 0);break ; +case PACKET_IGNORE_OUTGOING : + val = po->prot_hook.ignore_outgoing; +break ;case PACKET_ROLLOVER_STATS :if (!po->rollover)return -EINVAL ;
Signature · 1 line
2.11.0
Willem de Bruijn
maintainerQuoted 10 lines · Vincent Whitchurch
Currently, the only way to ignore outgoing packets on a packet socket is via the BPF filter. With
MSG_ZEROCOPY, packets that are looped intoAF_PACKETare copied indev_queue_xmit_nit(), and this copy happens even if the filter run frompacket_rcv()would reject them. So the presence of a packet socket on the interface takes away the benefits ofMSG_ZEROCOPY, even if the packet socket is not interested in outgoing packets. (Even whenMSG_ZEROCOPYis not used, the skb is unnecessarily cloned, but the cost for that is much lower.)
Quoted 5 lines
Add a socket option to allow
AF_PACKETsockets to ignore outgoing packets to solve this. Note that the *BSDs already have something
similar: BIOCSSEESENT/BIOCSDIRECTION and BIOCSDIRFILT.The first intended user is lldpd.
Clear description of the use case, thanks. I don't see a simple alternative to introducing a new socket option, either (a new ETH_P_xx protocol wildcard different from ETH_P_ALL, perhaps).
Quoted 39 lines
Signed-off-by Vincent Whitchurch <[email protected]>--- include/linux/netdevice.h | 1 + include/uapi/linux/if_packet.h | 1 + net/core/dev.c | 3 +++ net/packet/af_packet.c | 15 +++++++++++++++ 4 files changed, 20 insertions(+)include/linux/netdevice.h +1
-0@@ -2317,6 +2317,7 @@ static inline struct sk_buff *call_gro_receive_sk(gro_receive_sk_t cb,struct packet_type {__be16 type;/* This is really htons(ether_type). */ +bool ignore_outgoing;struct net_device *dev;/* NULL is wildcarded here */ int (*func) (struct sk_buff *, struct net_device *,include/uapi/linux/if_packet.h +1
-0@@ -57,6 +57,7 @@ struct sockaddr_ll {#define PACKET_QDISC_BYPASS 20#define PACKET_ROLLOVER_STATS 21#define PACKET_FANOUT_DATA 22 +#define PACKET_IGNORE_OUTGOING 23#define
PACKET_FANOUT_HASH0
#definePACKET_FANOUT_LB1net/core/dev.c +0
-0
net/core/dev.c static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb) · line 1952
Quoted 6 lines
net/core/dev.c +3
-0@@ -1947,6 +1947,9 @@ static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb)if (!ptype->af_packet_priv || !skb->sk)return false; +if (ptype->ignore_outgoing) +return true; +
This probably does not belong in skb_loop_sk, but in dev_queue_xmit_nit directly.
Quoted 3 lines
net/core/dev.c +0
-0if (ptype->id_match)return ptype->id_match (ptype, skb->sk);else if ((struct sock *)ptype->af_packet_priv == skb->sk)