Skip to content

Commit e62d2df

Browse files
Daniel Borkmanndavem330
authored andcommitted
net: ptp: use sk_unattached_filter_create() for BPF
This patch migrates an open-coded sk_run_filter() implementation with proper use of the BPF API, that is, sk_unattached_filter_create(). This migration is needed, as we will be internally transforming the filter to a different representation, and therefore needs to be decoupled. It is okay to do so as skb_timestamping_init() is called during initialization of the network stack in core initcall via sock_init(). This would effectively also allow for PTP filters to be jit compiled if bpf_jit_enable is set. For better readability, there are also some newlines introduced, also ptp_classify.h is only in kernel space. Joint work with Alexei Starovoitov. Signed-off-by: Daniel Borkmann <dborkman@redhat.com> Signed-off-by: Alexei Starovoitov <ast@plumgrid.com> Cc: Richard Cochran <richard.cochran@omicron.at> Cc: Jiri Benc <jbenc@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent fbc907f commit e62d2df

2 files changed

Lines changed: 14 additions & 11 deletions

File tree

‎include/linux/ptp_classify.h‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,7 @@
2727
#include <linux/if_vlan.h>
2828
#include <linux/ip.h>
2929
#include <linux/filter.h>
30-
#ifdef __KERNEL__
3130
#include <linux/in.h>
32-
#else
33-
#include <netinet/in.h>
34-
#endif
3531

3632
#define PTP_CLASS_NONE 0x00 /* not a PTP event message */
3733
#define PTP_CLASS_V1 0x01 /* protocol version 1 */

‎net/core/timestamping.c‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,13 @@
2323
#include <linux/skbuff.h>
2424
#include <linux/export.h>
2525

26-
static struct sock_filter ptp_filter[] = {
27-
PTP_FILTER
28-
};
26+
static struct sk_filter *ptp_insns __read_mostly;
2927

3028
static unsigned int classify(const struct sk_buff *skb)
3129
{
32-
if (likely(skb->dev &&
33-
skb->dev->phydev &&
30+
if (likely(skb->dev && skb->dev->phydev &&
3431
skb->dev->phydev->drv))
35-
return sk_run_filter(skb, ptp_filter);
32+
return SK_RUN_FILTER(ptp_insns, skb);
3633
else
3734
return PTP_CLASS_NONE;
3835
}
@@ -60,11 +57,13 @@ void skb_clone_tx_timestamp(struct sk_buff *skb)
6057
if (likely(phydev->drv->txtstamp)) {
6158
if (!atomic_inc_not_zero(&sk->sk_refcnt))
6259
return;
60+
6361
clone = skb_clone(skb, GFP_ATOMIC);
6462
if (!clone) {
6563
sock_put(sk);
6664
return;
6765
}
66+
6867
clone->sk = sk;
6968
phydev->drv->txtstamp(phydev, clone, type);
7069
}
@@ -89,12 +88,15 @@ void skb_complete_tx_timestamp(struct sk_buff *skb,
8988
}
9089

9190
*skb_hwtstamps(skb) = *hwtstamps;
91+
9292
serr = SKB_EXT_ERR(skb);
9393
memset(serr, 0, sizeof(*serr));
9494
serr->ee.ee_errno = ENOMSG;
9595
serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
9696
skb->sk = NULL;
97+
9798
err = sock_queue_err_skb(sk, skb);
99+
98100
sock_put(sk);
99101
if (err)
100102
kfree_skb(skb);
@@ -135,5 +137,10 @@ EXPORT_SYMBOL_GPL(skb_defer_rx_timestamp);
135137

136138
void __init skb_timestamping_init(void)
137139
{
138-
BUG_ON(sk_chk_filter(ptp_filter, ARRAY_SIZE(ptp_filter)));
140+
static struct sock_filter ptp_filter[] = { PTP_FILTER };
141+
struct sock_fprog ptp_prog = {
142+
.len = ARRAY_SIZE(ptp_filter), .filter = ptp_filter,
143+
};
144+
145+
BUG_ON(sk_unattached_filter_create(&ptp_insns, &ptp_prog));
139146
}

0 commit comments

Comments
 (0)