xref: /openbmc/linux/include/net/xdp_sock.h (revision dd2934a95701576203b2f61e8ded4e4a2f9183ea)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* AF_XDP internal functions
3  * Copyright(c) 2018 Intel Corporation.
4  */
5 
6 #ifndef _LINUX_XDP_SOCK_H
7 #define _LINUX_XDP_SOCK_H
8 
9 #include <linux/workqueue.h>
10 #include <linux/if_xdp.h>
11 #include <linux/mutex.h>
12 #include <linux/spinlock.h>
13 #include <linux/mm.h>
14 #include <net/sock.h>
15 
16 struct net_device;
17 struct xsk_queue;
18 
19 struct xdp_umem_page {
20 	void *addr;
21 	dma_addr_t dma;
22 };
23 
24 struct xdp_umem {
25 	struct xsk_queue *fq;
26 	struct xsk_queue *cq;
27 	struct xdp_umem_page *pages;
28 	u64 chunk_mask;
29 	u64 size;
30 	u32 headroom;
31 	u32 chunk_size_nohr;
32 	struct user_struct *user;
33 	struct pid *pid;
34 	unsigned long address;
35 	refcount_t users;
36 	struct work_struct work;
37 	struct page **pgs;
38 	u32 npgs;
39 	struct net_device *dev;
40 	u16 queue_id;
41 	bool zc;
42 	spinlock_t xsk_list_lock;
43 	struct list_head xsk_list;
44 };
45 
46 struct xdp_sock {
47 	/* struct sock must be the first member of struct xdp_sock */
48 	struct sock sk;
49 	struct xsk_queue *rx;
50 	struct net_device *dev;
51 	struct xdp_umem *umem;
52 	struct list_head flush_node;
53 	u16 queue_id;
54 	struct xsk_queue *tx ____cacheline_aligned_in_smp;
55 	struct list_head list;
56 	bool zc;
57 	/* Protects multiple processes in the control path */
58 	struct mutex mutex;
59 	/* Mutual exclusion of NAPI TX thread and sendmsg error paths
60 	 * in the SKB destructor callback.
61 	 */
62 	spinlock_t tx_completion_lock;
63 	u64 rx_dropped;
64 };
65 
66 struct xdp_buff;
67 #ifdef CONFIG_XDP_SOCKETS
68 int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp);
69 int xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp);
70 void xsk_flush(struct xdp_sock *xs);
71 bool xsk_is_setup_for_bpf_map(struct xdp_sock *xs);
72 /* Used from netdev driver */
73 u64 *xsk_umem_peek_addr(struct xdp_umem *umem, u64 *addr);
74 void xsk_umem_discard_addr(struct xdp_umem *umem);
75 void xsk_umem_complete_tx(struct xdp_umem *umem, u32 nb_entries);
76 bool xsk_umem_consume_tx(struct xdp_umem *umem, dma_addr_t *dma, u32 *len);
77 void xsk_umem_consume_tx_done(struct xdp_umem *umem);
78 
79 static inline char *xdp_umem_get_data(struct xdp_umem *umem, u64 addr)
80 {
81 	return umem->pages[addr >> PAGE_SHIFT].addr + (addr & (PAGE_SIZE - 1));
82 }
83 
84 static inline dma_addr_t xdp_umem_get_dma(struct xdp_umem *umem, u64 addr)
85 {
86 	return umem->pages[addr >> PAGE_SHIFT].dma + (addr & (PAGE_SIZE - 1));
87 }
88 #else
89 static inline int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
90 {
91 	return -ENOTSUPP;
92 }
93 
94 static inline int xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
95 {
96 	return -ENOTSUPP;
97 }
98 
99 static inline void xsk_flush(struct xdp_sock *xs)
100 {
101 }
102 
103 static inline bool xsk_is_setup_for_bpf_map(struct xdp_sock *xs)
104 {
105 	return false;
106 }
107 
108 static inline u64 *xsk_umem_peek_addr(struct xdp_umem *umem, u64 *addr)
109 {
110 	return NULL;
111 }
112 
113 static inline void xsk_umem_discard_addr(struct xdp_umem *umem)
114 {
115 }
116 
117 static inline void xsk_umem_complete_tx(struct xdp_umem *umem, u32 nb_entries)
118 {
119 }
120 
121 static inline bool xsk_umem_consume_tx(struct xdp_umem *umem, dma_addr_t *dma,
122 				       u32 *len)
123 {
124 	return false;
125 }
126 
127 static inline void xsk_umem_consume_tx_done(struct xdp_umem *umem)
128 {
129 }
130 
131 static inline char *xdp_umem_get_data(struct xdp_umem *umem, u64 addr)
132 {
133 	return NULL;
134 }
135 
136 static inline dma_addr_t xdp_umem_get_dma(struct xdp_umem *umem, u64 addr)
137 {
138 	return 0;
139 }
140 #endif /* CONFIG_XDP_SOCKETS */
141 
142 #endif /* _LINUX_XDP_SOCK_H */
143