hc
2024-05-10 23fa18eaa71266feff7ba8d83022d9e1cc83c65a
kernel/include/net/xdp.h
....@@ -1,10 +1,12 @@
1
+/* SPDX-License-Identifier: GPL-2.0-only */
12 /* include/net/xdp.h
23 *
34 * Copyright (c) 2017 Jesper Dangaard Brouer, Red Hat Inc.
4
- * Released under terms in GPL version 2. See COPYING.
55 */
66 #ifndef __LINUX_NET_XDP_H__
77 #define __LINUX_NET_XDP_H__
8
+
9
+#include <linux/skbuff.h> /* skb_shared_info */
810
911 /**
1012 * DOC: XDP RX-queue information
....@@ -37,7 +39,7 @@
3739 MEM_TYPE_PAGE_SHARED = 0, /* Split-page refcnt based model */
3840 MEM_TYPE_PAGE_ORDER0, /* Orig XDP full page model */
3941 MEM_TYPE_PAGE_POOL,
40
- MEM_TYPE_ZERO_COPY,
42
+ MEM_TYPE_XSK_BUFF_POOL,
4143 MEM_TYPE_MAX,
4244 };
4345
....@@ -52,10 +54,6 @@
5254
5355 struct page_pool;
5456
55
-struct zero_copy_allocator {
56
- void (*free)(struct zero_copy_allocator *zca, unsigned long handle);
57
-};
58
-
5957 struct xdp_rxq_info {
6058 struct net_device *dev;
6159 u32 queue_index;
....@@ -63,25 +61,63 @@
6361 struct xdp_mem_info mem;
6462 } ____cacheline_aligned; /* perf critical, avoid false-sharing */
6563
64
+struct xdp_txq_info {
65
+ struct net_device *dev;
66
+};
67
+
6668 struct xdp_buff {
6769 void *data;
6870 void *data_end;
6971 void *data_meta;
7072 void *data_hard_start;
71
- unsigned long handle;
7273 struct xdp_rxq_info *rxq;
74
+ struct xdp_txq_info *txq;
75
+ u32 frame_sz; /* frame size to deduce data_hard_end/reserved tailroom*/
7376 };
77
+
78
+/* Reserve memory area at end-of data area.
79
+ *
80
+ * This macro reserves tailroom in the XDP buffer by limiting the
81
+ * XDP/BPF data access to data_hard_end. Notice same area (and size)
82
+ * is used for XDP_PASS, when constructing the SKB via build_skb().
83
+ */
84
+#define xdp_data_hard_end(xdp) \
85
+ ((xdp)->data_hard_start + (xdp)->frame_sz - \
86
+ SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
87
+
88
+static inline struct skb_shared_info *
89
+xdp_get_shared_info_from_buff(struct xdp_buff *xdp)
90
+{
91
+ return (struct skb_shared_info *)xdp_data_hard_end(xdp);
92
+}
7493
7594 struct xdp_frame {
7695 void *data;
7796 u16 len;
7897 u16 headroom;
79
- u16 metasize;
98
+ u32 metasize:8;
99
+ u32 frame_sz:24;
80100 /* Lifetime of xdp_rxq_info is limited to NAPI/enqueue time,
81101 * while mem info is valid on remote CPU.
82102 */
83103 struct xdp_mem_info mem;
84104 struct net_device *dev_rx; /* used by cpumap */
105
+};
106
+
107
+
108
+static inline struct skb_shared_info *
109
+xdp_get_shared_info_from_frame(struct xdp_frame *frame)
110
+{
111
+ void *data_hard_start = frame->data - frame->headroom - sizeof(*frame);
112
+
113
+ return (struct skb_shared_info *)(data_hard_start + frame->frame_sz -
114
+ SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
115
+}
116
+
117
+struct xdp_cpumap_stats {
118
+ unsigned int redirect;
119
+ unsigned int pass;
120
+ unsigned int drop;
85121 };
86122
87123 /* Clear kernel pointers in xdp_frame */
....@@ -91,32 +127,63 @@
91127 frame->dev_rx = NULL;
92128 }
93129
94
-/* Convert xdp_buff to xdp_frame */
95
-static inline
96
-struct xdp_frame *convert_to_xdp_frame(struct xdp_buff *xdp)
97
-{
98
- struct xdp_frame *xdp_frame;
99
- int metasize;
100
- int headroom;
130
+/* Avoids inlining WARN macro in fast-path */
131
+void xdp_warn(const char *msg, const char *func, const int line);
132
+#define XDP_WARN(msg) xdp_warn(msg, __func__, __LINE__)
101133
102
- /* TODO: implement clone, copy, use "native" MEM_TYPE */
103
- if (xdp->rxq->mem.type == MEM_TYPE_ZERO_COPY)
104
- return NULL;
134
+struct xdp_frame *xdp_convert_zc_to_xdp_frame(struct xdp_buff *xdp);
135
+
136
+static inline
137
+void xdp_convert_frame_to_buff(struct xdp_frame *frame, struct xdp_buff *xdp)
138
+{
139
+ xdp->data_hard_start = frame->data - frame->headroom - sizeof(*frame);
140
+ xdp->data = frame->data;
141
+ xdp->data_end = frame->data + frame->len;
142
+ xdp->data_meta = frame->data - frame->metasize;
143
+ xdp->frame_sz = frame->frame_sz;
144
+}
145
+
146
+static inline
147
+int xdp_update_frame_from_buff(struct xdp_buff *xdp,
148
+ struct xdp_frame *xdp_frame)
149
+{
150
+ int metasize, headroom;
105151
106152 /* Assure headroom is available for storing info */
107153 headroom = xdp->data - xdp->data_hard_start;
108154 metasize = xdp->data - xdp->data_meta;
109155 metasize = metasize > 0 ? metasize : 0;
110156 if (unlikely((headroom - metasize) < sizeof(*xdp_frame)))
111
- return NULL;
157
+ return -ENOSPC;
112158
113
- /* Store info in top of packet */
114
- xdp_frame = xdp->data_hard_start;
159
+ /* Catch if driver didn't reserve tailroom for skb_shared_info */
160
+ if (unlikely(xdp->data_end > xdp_data_hard_end(xdp))) {
161
+ XDP_WARN("Driver BUG: missing reserved tailroom");
162
+ return -ENOSPC;
163
+ }
115164
116165 xdp_frame->data = xdp->data;
117166 xdp_frame->len = xdp->data_end - xdp->data;
118167 xdp_frame->headroom = headroom - sizeof(*xdp_frame);
119168 xdp_frame->metasize = metasize;
169
+ xdp_frame->frame_sz = xdp->frame_sz;
170
+
171
+ return 0;
172
+}
173
+
174
+/* Convert xdp_buff to xdp_frame */
175
+static inline
176
+struct xdp_frame *xdp_convert_buff_to_frame(struct xdp_buff *xdp)
177
+{
178
+ struct xdp_frame *xdp_frame;
179
+
180
+ if (xdp->rxq->mem.type == MEM_TYPE_XSK_BUFF_POOL)
181
+ return xdp_convert_zc_to_xdp_frame(xdp);
182
+
183
+ /* Store info in top of packet */
184
+ xdp_frame = xdp->data_hard_start;
185
+ if (unlikely(xdp_update_frame_from_buff(xdp, xdp_frame) < 0))
186
+ return NULL;
120187
121188 /* rxq only valid until napi_schedule ends, convert to xdp_mem_info */
122189 xdp_frame->mem = xdp->rxq->mem;
....@@ -128,6 +195,21 @@
128195 void xdp_return_frame_rx_napi(struct xdp_frame *xdpf);
129196 void xdp_return_buff(struct xdp_buff *xdp);
130197
198
+/* When sending xdp_frame into the network stack, then there is no
199
+ * return point callback, which is needed to release e.g. DMA-mapping
200
+ * resources with page_pool. Thus, have explicit function to release
201
+ * frame resources.
202
+ */
203
+void __xdp_release_frame(void *data, struct xdp_mem_info *mem);
204
+static inline void xdp_release_frame(struct xdp_frame *xdpf)
205
+{
206
+ struct xdp_mem_info *mem = &xdpf->mem;
207
+
208
+ /* Curr only page_pool needs this */
209
+ if (mem->type == MEM_TYPE_PAGE_POOL)
210
+ __xdp_release_frame(xdpf->data, mem);
211
+}
212
+
131213 int xdp_rxq_info_reg(struct xdp_rxq_info *xdp_rxq,
132214 struct net_device *dev, u32 queue_index);
133215 void xdp_rxq_info_unreg(struct xdp_rxq_info *xdp_rxq);
....@@ -135,6 +217,7 @@
135217 bool xdp_rxq_info_is_reg(struct xdp_rxq_info *xdp_rxq);
136218 int xdp_rxq_info_reg_mem_model(struct xdp_rxq_info *xdp_rxq,
137219 enum xdp_mem_type type, void *allocator);
220
+void xdp_rxq_info_unreg_mem_model(struct xdp_rxq_info *xdp_rxq);
138221
139222 /* Drivers not supporting XDP metadata can use this helper, which
140223 * rejects any room expansion for metadata as a result.
....@@ -157,11 +240,9 @@
157240 };
158241
159242 struct netdev_bpf;
160
-int xdp_attachment_query(struct xdp_attachment_info *info,
161
- struct netdev_bpf *bpf);
162
-bool xdp_attachment_flags_ok(struct xdp_attachment_info *info,
163
- struct netdev_bpf *bpf);
164243 void xdp_attachment_setup(struct xdp_attachment_info *info,
165244 struct netdev_bpf *bpf);
166245
246
+#define DEV_MAP_BULK_SIZE 16
247
+
167248 #endif /* __LINUX_NET_XDP_H__ */