Skip to content

Commit aecd67b

Browse files
netoptimizerAlexei Starovoitov
authored andcommitted
xdp: base API for new XDP rx-queue info concept
This patch only introduce the core data structures and API functions. All XDP enabled drivers must use the API before this info can used. There is a need for XDP to know more about the RX-queue a given XDP frames have arrived on. For both the XDP bpf-prog and kernel side. Instead of extending xdp_buff each time new info is needed, the patch creates a separate read-mostly struct xdp_rxq_info, that contains this info. We stress this data/cache-line is for read-only info. This is NOT for dynamic per packet info, use the data_meta for such use-cases. The performance advantage is this info can be setup at RX-ring init time, instead of updating N-members in xdp_buff. A possible (driver level) micro optimization is that xdp_buff->rxq assignment could be done once per XDP/NAPI loop. The extra pointer deref only happens for program needing access to this info (thus, no slowdown to existing use-cases). Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 5f103c5 commit aecd67b

4 files changed

Lines changed: 117 additions & 1 deletion

File tree

‎include/linux/filter.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <linux/set_memory.h>
2121
#include <linux/kallsyms.h>
2222

23+
#include <net/xdp.h>
2324
#include <net/sch_generic.h>
2425

2526
#include <uapi/linux/filter.h>
@@ -503,6 +504,7 @@ struct xdp_buff {
503504
void *data_end;
504505
void *data_meta;
505506
void *data_hard_start;
507+
struct xdp_rxq_info *rxq;
506508
};
507509

508510
/* Compute the linear packet data range [data, data_end) which

‎include/net/xdp.h‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* include/net/xdp.h
2+
*
3+
* Copyright (c) 2017 Jesper Dangaard Brouer, Red Hat Inc.
4+
* Released under terms in GPL version 2. See COPYING.
5+
*/
6+
#ifndef __LINUX_NET_XDP_H__
7+
#define __LINUX_NET_XDP_H__
8+
9+
/**
10+
* DOC: XDP RX-queue information
11+
*
12+
* The XDP RX-queue info (xdp_rxq_info) is associated with the driver
13+
* level RX-ring queues. It is information that is specific to how
14+
* the driver have configured a given RX-ring queue.
15+
*
16+
* Each xdp_buff frame received in the driver carry a (pointer)
17+
* reference to this xdp_rxq_info structure. This provides the XDP
18+
* data-path read-access to RX-info for both kernel and bpf-side
19+
* (limited subset).
20+
*
21+
* For now, direct access is only safe while running in NAPI/softirq
22+
* context. Contents is read-mostly and must not be updated during
23+
* driver NAPI/softirq poll.
24+
*
25+
* The driver usage API is a register and unregister API.
26+
*
27+
* The struct is not directly tied to the XDP prog. A new XDP prog
28+
* can be attached as long as it doesn't change the underlying
29+
* RX-ring. If the RX-ring does change significantly, the NIC driver
30+
* naturally need to stop the RX-ring before purging and reallocating
31+
* memory. In that process the driver MUST call unregistor (which
32+
* also apply for driver shutdown and unload). The register API is
33+
* also mandatory during RX-ring setup.
34+
*/
35+
36+
struct xdp_rxq_info {
37+
struct net_device *dev;
38+
u32 queue_index;
39+
u32 reg_state;
40+
} ____cacheline_aligned; /* perf critical, avoid false-sharing */
41+
42+
int xdp_rxq_info_reg(struct xdp_rxq_info *xdp_rxq,
43+
struct net_device *dev, u32 queue_index);
44+
void xdp_rxq_info_unreg(struct xdp_rxq_info *xdp_rxq);
45+
void xdp_rxq_info_unused(struct xdp_rxq_info *xdp_rxq);
46+
47+
#endif /* __LINUX_NET_XDP_H__ */

‎net/core/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ obj-$(CONFIG_SYSCTL) += sysctl_net_core.o
1111
obj-y += dev.o ethtool.o dev_addr_lists.o dst.o netevent.o \
1212
neighbour.o rtnetlink.o utils.o link_watch.o filter.o \
1313
sock_diag.o dev_ioctl.o tso.o sock_reuseport.o \
14-
fib_notifier.o
14+
fib_notifier.o xdp.o
1515

1616
obj-y += net-sysfs.o
1717
obj-$(CONFIG_PROC_FS) += net-procfs.o

‎net/core/xdp.c‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* net/core/xdp.c
2+
*
3+
* Copyright (c) 2017 Jesper Dangaard Brouer, Red Hat Inc.
4+
* Released under terms in GPL version 2. See COPYING.
5+
*/
6+
#include <linux/types.h>
7+
#include <linux/mm.h>
8+
9+
#include <net/xdp.h>
10+
11+
#define REG_STATE_NEW 0x0
12+
#define REG_STATE_REGISTERED 0x1
13+
#define REG_STATE_UNREGISTERED 0x2
14+
#define REG_STATE_UNUSED 0x3
15+
16+
void xdp_rxq_info_unreg(struct xdp_rxq_info *xdp_rxq)
17+
{
18+
/* Simplify driver cleanup code paths, allow unreg "unused" */
19+
if (xdp_rxq->reg_state == REG_STATE_UNUSED)
20+
return;
21+
22+
WARN(!(xdp_rxq->reg_state == REG_STATE_REGISTERED), "Driver BUG");
23+
24+
xdp_rxq->reg_state = REG_STATE_UNREGISTERED;
25+
xdp_rxq->dev = NULL;
26+
}
27+
EXPORT_SYMBOL_GPL(xdp_rxq_info_unreg);
28+
29+
static void xdp_rxq_info_init(struct xdp_rxq_info *xdp_rxq)
30+
{
31+
memset(xdp_rxq, 0, sizeof(*xdp_rxq));
32+
}
33+
34+
/* Returns 0 on success, negative on failure */
35+
int xdp_rxq_info_reg(struct xdp_rxq_info *xdp_rxq,
36+
struct net_device *dev, u32 queue_index)
37+
{
38+
if (xdp_rxq->reg_state == REG_STATE_UNUSED) {
39+
WARN(1, "Driver promised not to register this");
40+
return -EINVAL;
41+
}
42+
43+
if (xdp_rxq->reg_state == REG_STATE_REGISTERED) {
44+
WARN(1, "Missing unregister, handled but fix driver");
45+
xdp_rxq_info_unreg(xdp_rxq);
46+
}
47+
48+
if (!dev) {
49+
WARN(1, "Missing net_device from driver");
50+
return -ENODEV;
51+
}
52+
53+
/* State either UNREGISTERED or NEW */
54+
xdp_rxq_info_init(xdp_rxq);
55+
xdp_rxq->dev = dev;
56+
xdp_rxq->queue_index = queue_index;
57+
58+
xdp_rxq->reg_state = REG_STATE_REGISTERED;
59+
return 0;
60+
}
61+
EXPORT_SYMBOL_GPL(xdp_rxq_info_reg);
62+
63+
void xdp_rxq_info_unused(struct xdp_rxq_info *xdp_rxq)
64+
{
65+
xdp_rxq->reg_state = REG_STATE_UNUSED;
66+
}
67+
EXPORT_SYMBOL_GPL(xdp_rxq_info_unused);

0 commit comments

Comments
 (0)