Skip to content

Commit e753e92

Browse files
LorenzoBianconiborkmann
authored andcommitted
net, xdp: Introduce __xdp_build_skb_from_frame utility routine
Introduce __xdp_build_skb_from_frame utility routine to build the skb from xdp_frame. Rely on __xdp_build_skb_from_frame in cpumap code. Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Jesper Dangaard Brouer <brouer@redhat.com> Link: https://lore.kernel.org/bpf/4f9f4c6b3dd3933770c617eb6689dbc0c6e25863.1610475660.git.lorenzo@kernel.org
1 parent 232164e commit e753e92

File tree

3 files changed

+49
-44
lines changed

3 files changed

+49
-44
lines changed

include/net/xdp.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ void xdp_warn(const char *msg, const char *func, const int line);
164164
#define XDP_WARN(msg) xdp_warn(msg, __func__, __LINE__)
165165

166166
struct xdp_frame *xdp_convert_zc_to_xdp_frame(struct xdp_buff *xdp);
167+
struct sk_buff *__xdp_build_skb_from_frame(struct xdp_frame *xdpf,
168+
struct sk_buff *skb,
169+
struct net_device *dev);
167170

168171
static inline
169172
void xdp_convert_frame_to_buff(struct xdp_frame *frame, struct xdp_buff *xdp)

kernel/bpf/cpumap.c

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -141,49 +141,6 @@ static void cpu_map_kthread_stop(struct work_struct *work)
141141
kthread_stop(rcpu->kthread);
142142
}
143143

144-
static struct sk_buff *cpu_map_build_skb(struct xdp_frame *xdpf,
145-
struct sk_buff *skb)
146-
{
147-
unsigned int hard_start_headroom;
148-
unsigned int frame_size;
149-
void *pkt_data_start;
150-
151-
/* Part of headroom was reserved to xdpf */
152-
hard_start_headroom = sizeof(struct xdp_frame) + xdpf->headroom;
153-
154-
/* Memory size backing xdp_frame data already have reserved
155-
* room for build_skb to place skb_shared_info in tailroom.
156-
*/
157-
frame_size = xdpf->frame_sz;
158-
159-
pkt_data_start = xdpf->data - hard_start_headroom;
160-
skb = build_skb_around(skb, pkt_data_start, frame_size);
161-
if (unlikely(!skb))
162-
return NULL;
163-
164-
skb_reserve(skb, hard_start_headroom);
165-
__skb_put(skb, xdpf->len);
166-
if (xdpf->metasize)
167-
skb_metadata_set(skb, xdpf->metasize);
168-
169-
/* Essential SKB info: protocol and skb->dev */
170-
skb->protocol = eth_type_trans(skb, xdpf->dev_rx);
171-
172-
/* Optional SKB info, currently missing:
173-
* - HW checksum info (skb->ip_summed)
174-
* - HW RX hash (skb_set_hash)
175-
* - RX ring dev queue index (skb_record_rx_queue)
176-
*/
177-
178-
/* Until page_pool get SKB return path, release DMA here */
179-
xdp_release_frame(xdpf);
180-
181-
/* Allow SKB to reuse area used by xdp_frame */
182-
xdp_scrub_frame(xdpf);
183-
184-
return skb;
185-
}
186-
187144
static void __cpu_map_ring_cleanup(struct ptr_ring *ring)
188145
{
189146
/* The tear-down procedure should have made sure that queue is
@@ -350,7 +307,8 @@ static int cpu_map_kthread_run(void *data)
350307
struct sk_buff *skb = skbs[i];
351308
int ret;
352309

353-
skb = cpu_map_build_skb(xdpf, skb);
310+
skb = __xdp_build_skb_from_frame(xdpf, skb,
311+
xdpf->dev_rx);
354312
if (!skb) {
355313
xdp_return_frame(xdpf);
356314
continue;

net/core/xdp.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,3 +513,47 @@ void xdp_warn(const char *msg, const char *func, const int line)
513513
WARN(1, "XDP_WARN: %s(line:%d): %s\n", func, line, msg);
514514
};
515515
EXPORT_SYMBOL_GPL(xdp_warn);
516+
517+
struct sk_buff *__xdp_build_skb_from_frame(struct xdp_frame *xdpf,
518+
struct sk_buff *skb,
519+
struct net_device *dev)
520+
{
521+
unsigned int headroom, frame_size;
522+
void *hard_start;
523+
524+
/* Part of headroom was reserved to xdpf */
525+
headroom = sizeof(*xdpf) + xdpf->headroom;
526+
527+
/* Memory size backing xdp_frame data already have reserved
528+
* room for build_skb to place skb_shared_info in tailroom.
529+
*/
530+
frame_size = xdpf->frame_sz;
531+
532+
hard_start = xdpf->data - headroom;
533+
skb = build_skb_around(skb, hard_start, frame_size);
534+
if (unlikely(!skb))
535+
return NULL;
536+
537+
skb_reserve(skb, headroom);
538+
__skb_put(skb, xdpf->len);
539+
if (xdpf->metasize)
540+
skb_metadata_set(skb, xdpf->metasize);
541+
542+
/* Essential SKB info: protocol and skb->dev */
543+
skb->protocol = eth_type_trans(skb, dev);
544+
545+
/* Optional SKB info, currently missing:
546+
* - HW checksum info (skb->ip_summed)
547+
* - HW RX hash (skb_set_hash)
548+
* - RX ring dev queue index (skb_record_rx_queue)
549+
*/
550+
551+
/* Until page_pool get SKB return path, release DMA here */
552+
xdp_release_frame(xdpf);
553+
554+
/* Allow SKB to reuse area used by xdp_frame */
555+
xdp_scrub_frame(xdpf);
556+
557+
return skb;
558+
}
559+
EXPORT_SYMBOL_GPL(__xdp_build_skb_from_frame);

0 commit comments

Comments
 (0)