blob: 5a6cdf7b320dc80a21436a046acafcb828f7aab7 [file] [log] [blame]
Björn Töpelc0c77d82018-05-02 13:01:23 +02001// SPDX-License-Identifier: GPL-2.0
2/* XDP sockets
3 *
4 * AF_XDP sockets allows a channel between XDP programs and userspace
5 * applications.
6 * Copyright(c) 2018 Intel Corporation.
7 *
Björn Töpelc0c77d82018-05-02 13:01:23 +02008 * Author(s): Björn Töpel <bjorn.topel@intel.com>
9 * Magnus Karlsson <magnus.karlsson@intel.com>
10 */
11
12#define pr_fmt(fmt) "AF_XDP: %s: " fmt, __func__
13
14#include <linux/if_xdp.h>
15#include <linux/init.h>
16#include <linux/sched/mm.h>
17#include <linux/sched/signal.h>
18#include <linux/sched/task.h>
19#include <linux/socket.h>
20#include <linux/file.h>
21#include <linux/uaccess.h>
22#include <linux/net.h>
23#include <linux/netdevice.h>
Magnus Karlssonac98d8a2018-06-04 14:05:57 +020024#include <linux/rculist.h>
Magnus Karlssona71506a2020-05-20 21:20:51 +020025#include <net/xdp_sock_drv.h>
Björn Töpelb9b6b682018-05-02 13:01:25 +020026#include <net/xdp.h>
Björn Töpelc0c77d82018-05-02 13:01:23 +020027
Magnus Karlsson423f3832018-05-02 13:01:24 +020028#include "xsk_queue.h"
Björn Töpelc0c77d82018-05-02 13:01:23 +020029#include "xdp_umem.h"
Björn Töpela36b38aa2019-01-24 19:59:39 +010030#include "xsk.h"
Björn Töpelc0c77d82018-05-02 13:01:23 +020031
Magnus Karlsson35fcde72018-05-02 13:01:34 +020032#define TX_BATCH_SIZE 16
33
Björn Töpele312b9e2019-12-19 07:10:02 +010034static DEFINE_PER_CPU(struct list_head, xskmap_flush_list);
35
Magnus Karlssonc4655762020-08-28 10:26:16 +020036void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool)
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020037{
Magnus Karlssonc2d3d6a2020-08-28 10:26:19 +020038 if (pool->cached_need_wakeup & XDP_WAKEUP_RX)
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020039 return;
40
Magnus Karlsson7361f9c2020-08-28 10:26:18 +020041 pool->fq->ring->flags |= XDP_RING_NEED_WAKEUP;
Magnus Karlssonc2d3d6a2020-08-28 10:26:19 +020042 pool->cached_need_wakeup |= XDP_WAKEUP_RX;
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020043}
44EXPORT_SYMBOL(xsk_set_rx_need_wakeup);
45
Magnus Karlssonc4655762020-08-28 10:26:16 +020046void xsk_set_tx_need_wakeup(struct xsk_buff_pool *pool)
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020047{
48 struct xdp_sock *xs;
49
Magnus Karlssonc2d3d6a2020-08-28 10:26:19 +020050 if (pool->cached_need_wakeup & XDP_WAKEUP_TX)
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020051 return;
52
53 rcu_read_lock();
Magnus Karlssona5aa8e52020-08-28 10:26:20 +020054 list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) {
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020055 xs->tx->ring->flags |= XDP_RING_NEED_WAKEUP;
56 }
57 rcu_read_unlock();
58
Magnus Karlssonc2d3d6a2020-08-28 10:26:19 +020059 pool->cached_need_wakeup |= XDP_WAKEUP_TX;
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020060}
61EXPORT_SYMBOL(xsk_set_tx_need_wakeup);
62
Magnus Karlssonc4655762020-08-28 10:26:16 +020063void xsk_clear_rx_need_wakeup(struct xsk_buff_pool *pool)
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020064{
Magnus Karlssonc2d3d6a2020-08-28 10:26:19 +020065 if (!(pool->cached_need_wakeup & XDP_WAKEUP_RX))
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020066 return;
67
Magnus Karlsson7361f9c2020-08-28 10:26:18 +020068 pool->fq->ring->flags &= ~XDP_RING_NEED_WAKEUP;
Magnus Karlssonc2d3d6a2020-08-28 10:26:19 +020069 pool->cached_need_wakeup &= ~XDP_WAKEUP_RX;
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020070}
71EXPORT_SYMBOL(xsk_clear_rx_need_wakeup);
72
Magnus Karlssonc4655762020-08-28 10:26:16 +020073void xsk_clear_tx_need_wakeup(struct xsk_buff_pool *pool)
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020074{
75 struct xdp_sock *xs;
76
Magnus Karlssonc2d3d6a2020-08-28 10:26:19 +020077 if (!(pool->cached_need_wakeup & XDP_WAKEUP_TX))
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020078 return;
79
80 rcu_read_lock();
Magnus Karlssona5aa8e52020-08-28 10:26:20 +020081 list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) {
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020082 xs->tx->ring->flags &= ~XDP_RING_NEED_WAKEUP;
83 }
84 rcu_read_unlock();
85
Magnus Karlssonc2d3d6a2020-08-28 10:26:19 +020086 pool->cached_need_wakeup &= ~XDP_WAKEUP_TX;
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020087}
88EXPORT_SYMBOL(xsk_clear_tx_need_wakeup);
89
Magnus Karlssonc4655762020-08-28 10:26:16 +020090bool xsk_uses_need_wakeup(struct xsk_buff_pool *pool)
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020091{
Magnus Karlssonc2d3d6a2020-08-28 10:26:19 +020092 return pool->uses_need_wakeup;
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020093}
Magnus Karlssonc4655762020-08-28 10:26:16 +020094EXPORT_SYMBOL(xsk_uses_need_wakeup);
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020095
Magnus Karlsson1c1efc22020-08-28 10:26:17 +020096struct xsk_buff_pool *xsk_get_pool_from_qid(struct net_device *dev,
97 u16 queue_id)
98{
99 if (queue_id < dev->real_num_rx_queues)
100 return dev->_rx[queue_id].pool;
101 if (queue_id < dev->real_num_tx_queues)
102 return dev->_tx[queue_id].pool;
103
104 return NULL;
105}
106EXPORT_SYMBOL(xsk_get_pool_from_qid);
107
108void xsk_clear_pool_at_qid(struct net_device *dev, u16 queue_id)
109{
110 if (queue_id < dev->real_num_rx_queues)
111 dev->_rx[queue_id].pool = NULL;
112 if (queue_id < dev->real_num_tx_queues)
113 dev->_tx[queue_id].pool = NULL;
114}
115
116/* The buffer pool is stored both in the _rx struct and the _tx struct as we do
117 * not know if the device has more tx queues than rx, or the opposite.
118 * This might also change during run time.
119 */
120int xsk_reg_pool_at_qid(struct net_device *dev, struct xsk_buff_pool *pool,
121 u16 queue_id)
122{
123 if (queue_id >= max_t(unsigned int,
124 dev->real_num_rx_queues,
125 dev->real_num_tx_queues))
126 return -EINVAL;
127
128 if (queue_id < dev->real_num_rx_queues)
129 dev->_rx[queue_id].pool = pool;
130 if (queue_id < dev->real_num_tx_queues)
131 dev->_tx[queue_id].pool = pool;
132
133 return 0;
134}
135
Björn Töpel26062b12020-05-20 21:21:02 +0200136void xp_release(struct xdp_buff_xsk *xskb)
137{
138 xskb->pool->free_heads[xskb->pool->free_heads_cnt++] = xskb;
139}
140
141static u64 xp_get_handle(struct xdp_buff_xsk *xskb)
142{
143 u64 offset = xskb->xdp.data - xskb->xdp.data_hard_start;
144
145 offset += xskb->pool->headroom;
146 if (!xskb->pool->unaligned)
147 return xskb->orig_addr + offset;
148 return xskb->orig_addr + (offset << XSK_UNALIGNED_BUF_OFFSET_SHIFT);
149}
150
Björn Töpel2b434702020-05-20 21:20:53 +0200151static int __xsk_rcv_zc(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
Kevin Laatzc05cd362019-08-27 02:25:22 +0000152{
Björn Töpel2b434702020-05-20 21:20:53 +0200153 struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
154 u64 addr;
155 int err;
Kevin Laatzc05cd362019-08-27 02:25:22 +0000156
Björn Töpel2b434702020-05-20 21:20:53 +0200157 addr = xp_get_handle(xskb);
158 err = xskq_prod_reserve_desc(xs->rx, addr, len);
159 if (err) {
Ciara Loftus8aa5a332020-07-08 07:28:33 +0000160 xs->rx_queue_full++;
Björn Töpel2b434702020-05-20 21:20:53 +0200161 return err;
162 }
Kevin Laatzc05cd362019-08-27 02:25:22 +0000163
Björn Töpel2b434702020-05-20 21:20:53 +0200164 xp_release(xskb);
165 return 0;
166}
Kevin Laatzc05cd362019-08-27 02:25:22 +0000167
Björn Töpel2b434702020-05-20 21:20:53 +0200168static void xsk_copy_xdp(struct xdp_buff *to, struct xdp_buff *from, u32 len)
169{
170 void *from_buf, *to_buf;
171 u32 metalen;
172
173 if (unlikely(xdp_data_meta_unsupported(from))) {
174 from_buf = from->data;
175 to_buf = to->data;
176 metalen = 0;
177 } else {
178 from_buf = from->data_meta;
179 metalen = from->data - from->data_meta;
180 to_buf = to->data - metalen;
Kevin Laatzc05cd362019-08-27 02:25:22 +0000181 }
182
183 memcpy(to_buf, from_buf, len + metalen);
184}
185
Björn Töpel2b434702020-05-20 21:20:53 +0200186static int __xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len,
187 bool explicit_free)
Björn Töpel173d3ad2018-06-04 14:05:55 +0200188{
Björn Töpel2b434702020-05-20 21:20:53 +0200189 struct xdp_buff *xsk_xdp;
Björn Töpel4e64c832018-06-04 13:57:11 +0200190 int err;
Björn Töpelc4971762018-05-02 13:01:27 +0200191
Magnus Karlssonc4655762020-08-28 10:26:16 +0200192 if (len > xsk_pool_get_rx_frame_size(xs->pool)) {
Björn Töpela509a952018-06-04 13:57:12 +0200193 xs->rx_dropped++;
Björn Töpelc4971762018-05-02 13:01:27 +0200194 return -ENOSPC;
Björn Töpela509a952018-06-04 13:57:12 +0200195 }
Björn Töpelc4971762018-05-02 13:01:27 +0200196
Magnus Karlssonc4655762020-08-28 10:26:16 +0200197 xsk_xdp = xsk_buff_alloc(xs->pool);
Björn Töpel2b434702020-05-20 21:20:53 +0200198 if (!xsk_xdp) {
Björn Töpela509a952018-06-04 13:57:12 +0200199 xs->rx_dropped++;
Björn Töpel2b434702020-05-20 21:20:53 +0200200 return -ENOSPC;
201 }
Björn Töpelc4971762018-05-02 13:01:27 +0200202
Björn Töpel2b434702020-05-20 21:20:53 +0200203 xsk_copy_xdp(xsk_xdp, xdp, len);
204 err = __xsk_rcv_zc(xs, xsk_xdp, len);
205 if (err) {
206 xsk_buff_free(xsk_xdp);
207 return err;
208 }
209 if (explicit_free)
210 xdp_return_buff(xdp);
211 return 0;
Björn Töpelc4971762018-05-02 13:01:27 +0200212}
213
Björn Töpel42fddcc2019-09-04 13:49:12 +0200214static bool xsk_is_bound(struct xdp_sock *xs)
215{
216 if (READ_ONCE(xs->state) == XSK_BOUND) {
217 /* Matches smp_wmb() in bind(). */
218 smp_rmb();
219 return true;
220 }
221 return false;
222}
223
Björn Töpel2b434702020-05-20 21:20:53 +0200224static int xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp,
225 bool explicit_free)
Björn Töpelc4971762018-05-02 13:01:27 +0200226{
Björn Töpel173d3ad2018-06-04 14:05:55 +0200227 u32 len;
Björn Töpelc4971762018-05-02 13:01:27 +0200228
Björn Töpel42fddcc2019-09-04 13:49:12 +0200229 if (!xsk_is_bound(xs))
230 return -EINVAL;
231
Björn Töpel173d3ad2018-06-04 14:05:55 +0200232 if (xs->dev != xdp->rxq->dev || xs->queue_id != xdp->rxq->queue_index)
233 return -EINVAL;
Björn Töpelc4971762018-05-02 13:01:27 +0200234
Björn Töpel173d3ad2018-06-04 14:05:55 +0200235 len = xdp->data_end - xdp->data;
236
Björn Töpel08078922020-05-20 21:21:00 +0200237 return xdp->rxq->mem.type == MEM_TYPE_XSK_BUFF_POOL ?
Björn Töpel2b434702020-05-20 21:20:53 +0200238 __xsk_rcv_zc(xs, xdp, len) :
239 __xsk_rcv(xs, xdp, len, explicit_free);
Björn Töpelc4971762018-05-02 13:01:27 +0200240}
241
Björn Töpeld8179912019-11-01 12:03:46 +0100242static void xsk_flush(struct xdp_sock *xs)
Björn Töpelc4971762018-05-02 13:01:27 +0200243{
Magnus Karlsson59e35e52019-12-19 13:39:23 +0100244 xskq_prod_submit(xs->rx);
Magnus Karlsson7361f9c2020-08-28 10:26:18 +0200245 __xskq_cons_release(xs->pool->fq);
Björn Töpel43a825a2020-01-20 10:29:17 +0100246 sock_def_readable(&xs->sk);
Björn Töpelc4971762018-05-02 13:01:27 +0200247}
248
249int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
250{
251 int err;
252
Ilya Maximetsbf0bdd12019-07-03 15:09:16 +0300253 spin_lock_bh(&xs->rx_lock);
Björn Töpel2b434702020-05-20 21:20:53 +0200254 err = xsk_rcv(xs, xdp, false);
255 xsk_flush(xs);
Ilya Maximetsbf0bdd12019-07-03 15:09:16 +0300256 spin_unlock_bh(&xs->rx_lock);
Björn Töpelc4971762018-05-02 13:01:27 +0200257 return err;
258}
259
Björn Töpele312b9e2019-12-19 07:10:02 +0100260int __xsk_map_redirect(struct xdp_sock *xs, struct xdp_buff *xdp)
Björn Töpeld8179912019-11-01 12:03:46 +0100261{
Björn Töpele312b9e2019-12-19 07:10:02 +0100262 struct list_head *flush_list = this_cpu_ptr(&xskmap_flush_list);
Björn Töpeld8179912019-11-01 12:03:46 +0100263 int err;
264
Björn Töpel2b434702020-05-20 21:20:53 +0200265 err = xsk_rcv(xs, xdp, true);
Björn Töpeld8179912019-11-01 12:03:46 +0100266 if (err)
267 return err;
268
269 if (!xs->flush_node.prev)
270 list_add(&xs->flush_node, flush_list);
271
272 return 0;
273}
274
Björn Töpele312b9e2019-12-19 07:10:02 +0100275void __xsk_map_flush(void)
Björn Töpeld8179912019-11-01 12:03:46 +0100276{
Björn Töpele312b9e2019-12-19 07:10:02 +0100277 struct list_head *flush_list = this_cpu_ptr(&xskmap_flush_list);
Björn Töpeld8179912019-11-01 12:03:46 +0100278 struct xdp_sock *xs, *tmp;
279
280 list_for_each_entry_safe(xs, tmp, flush_list, flush_node) {
281 xsk_flush(xs);
282 __list_del_clearprev(&xs->flush_node);
283 }
284}
285
Magnus Karlssonc4655762020-08-28 10:26:16 +0200286void xsk_tx_completed(struct xsk_buff_pool *pool, u32 nb_entries)
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200287{
Magnus Karlsson7361f9c2020-08-28 10:26:18 +0200288 xskq_prod_submit_n(pool->cq, nb_entries);
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200289}
Magnus Karlssonc4655762020-08-28 10:26:16 +0200290EXPORT_SYMBOL(xsk_tx_completed);
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200291
Magnus Karlssonc4655762020-08-28 10:26:16 +0200292void xsk_tx_release(struct xsk_buff_pool *pool)
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200293{
294 struct xdp_sock *xs;
295
296 rcu_read_lock();
Magnus Karlssona5aa8e52020-08-28 10:26:20 +0200297 list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) {
Magnus Karlsson30744a62020-02-10 16:27:12 +0100298 __xskq_cons_release(xs->tx);
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200299 xs->sk.sk_write_space(&xs->sk);
300 }
301 rcu_read_unlock();
302}
Magnus Karlssonc4655762020-08-28 10:26:16 +0200303EXPORT_SYMBOL(xsk_tx_release);
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200304
Magnus Karlssonc4655762020-08-28 10:26:16 +0200305bool xsk_tx_peek_desc(struct xsk_buff_pool *pool, struct xdp_desc *desc)
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200306{
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200307 struct xdp_sock *xs;
308
309 rcu_read_lock();
Magnus Karlssona5aa8e52020-08-28 10:26:20 +0200310 list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) {
Magnus Karlsson1c1efc22020-08-28 10:26:17 +0200311 if (!xskq_cons_peek_desc(xs->tx, desc, pool)) {
Ciara Loftus8aa5a332020-07-08 07:28:33 +0000312 xs->tx->queue_empty_descs++;
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200313 continue;
Ciara Loftus8aa5a332020-07-08 07:28:33 +0000314 }
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200315
Tobias Klauser0a058612020-04-22 01:29:27 +0200316 /* This is the backpressure mechanism for the Tx path.
Magnus Karlsson15d8c912019-12-19 13:39:30 +0100317 * Reserve space in the completion queue and only proceed
318 * if there is space in it. This avoids having to implement
319 * any buffering in the Tx path.
320 */
Magnus Karlsson7361f9c2020-08-28 10:26:18 +0200321 if (xskq_prod_reserve_addr(pool->cq, desc->addr))
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200322 goto out;
323
Magnus Karlssonc5ed924b2019-12-19 13:39:26 +0100324 xskq_cons_release(xs->tx);
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200325 rcu_read_unlock();
326 return true;
327 }
328
329out:
330 rcu_read_unlock();
331 return false;
332}
Magnus Karlssonc4655762020-08-28 10:26:16 +0200333EXPORT_SYMBOL(xsk_tx_peek_desc);
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200334
Maxim Mikityanskiy06870682019-12-17 16:20:42 +0000335static int xsk_wakeup(struct xdp_sock *xs, u8 flags)
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200336{
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200337 struct net_device *dev = xs->dev;
Maxim Mikityanskiy06870682019-12-17 16:20:42 +0000338 int err;
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200339
Maxim Mikityanskiy06870682019-12-17 16:20:42 +0000340 rcu_read_lock();
341 err = dev->netdev_ops->ndo_xsk_wakeup(dev, xs->queue_id, flags);
342 rcu_read_unlock();
343
344 return err;
345}
346
347static int xsk_zc_xmit(struct xdp_sock *xs)
348{
349 return xsk_wakeup(xs, XDP_WAKEUP_TX);
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200350}
351
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200352static void xsk_destruct_skb(struct sk_buff *skb)
353{
Björn Töpelbbff2f32018-06-04 13:57:13 +0200354 u64 addr = (u64)(long)skb_shinfo(skb)->destructor_arg;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200355 struct xdp_sock *xs = xdp_sk(skb->sk);
Magnus Karlssona9744f72018-06-29 09:48:20 +0200356 unsigned long flags;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200357
Magnus Karlssona9744f72018-06-29 09:48:20 +0200358 spin_lock_irqsave(&xs->tx_completion_lock, flags);
Magnus Karlsson7361f9c2020-08-28 10:26:18 +0200359 xskq_prod_submit_addr(xs->pool->cq, addr);
Magnus Karlssona9744f72018-06-29 09:48:20 +0200360 spin_unlock_irqrestore(&xs->tx_completion_lock, flags);
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200361
362 sock_wfree(skb);
363}
364
Magnus Karlssondf551052019-10-02 08:31:59 +0200365static int xsk_generic_xmit(struct sock *sk)
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200366{
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200367 struct xdp_sock *xs = xdp_sk(sk);
Magnus Karlssondf551052019-10-02 08:31:59 +0200368 u32 max_batch = TX_BATCH_SIZE;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200369 bool sent_frame = false;
370 struct xdp_desc desc;
371 struct sk_buff *skb;
372 int err = 0;
373
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200374 mutex_lock(&xs->mutex);
375
Ilya Maximets67571642019-07-04 17:25:03 +0300376 if (xs->queue_id >= xs->dev->real_num_tx_queues)
377 goto out;
378
Magnus Karlsson1c1efc22020-08-28 10:26:17 +0200379 while (xskq_cons_peek_desc(xs->tx, &desc, xs->pool)) {
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200380 char *buffer;
Björn Töpelbbff2f32018-06-04 13:57:13 +0200381 u64 addr;
382 u32 len;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200383
384 if (max_batch-- == 0) {
385 err = -EAGAIN;
386 goto out;
387 }
388
Magnus Karlsson09210c42018-07-11 10:12:52 +0200389 len = desc.len;
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200390 skb = sock_alloc_send_skb(sk, len, 1, &err);
Li RongQingaa2cad02020-06-11 13:11:06 +0800391 if (unlikely(!skb))
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200392 goto out;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200393
394 skb_put(skb, len);
Björn Töpelbbff2f32018-06-04 13:57:13 +0200395 addr = desc.addr;
Magnus Karlssonc4655762020-08-28 10:26:16 +0200396 buffer = xsk_buff_raw_get_data(xs->pool, addr);
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200397 err = skb_store_bits(skb, 0, buffer, len);
Tobias Klauser0a058612020-04-22 01:29:27 +0200398 /* This is the backpressure mechanism for the Tx path.
Magnus Karlsson15d8c912019-12-19 13:39:30 +0100399 * Reserve space in the completion queue and only proceed
400 * if there is space in it. This avoids having to implement
401 * any buffering in the Tx path.
402 */
Magnus Karlsson7361f9c2020-08-28 10:26:18 +0200403 if (unlikely(err) || xskq_prod_reserve(xs->pool->cq)) {
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200404 kfree_skb(skb);
405 goto out;
406 }
407
408 skb->dev = xs->dev;
409 skb->priority = sk->sk_priority;
410 skb->mark = sk->sk_mark;
Kevin Laatzc05cd362019-08-27 02:25:22 +0000411 skb_shinfo(skb)->destructor_arg = (void *)(long)desc.addr;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200412 skb->destructor = xsk_destruct_skb;
413
Magnus Karlsson642e4502020-09-16 14:00:25 +0200414 /* Hinder dev_direct_xmit from freeing the packet and
415 * therefore completing it in the destructor
416 */
417 refcount_inc(&skb->users);
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200418 err = dev_direct_xmit(skb, xs->queue_id);
Magnus Karlsson642e4502020-09-16 14:00:25 +0200419 if (err == NETDEV_TX_BUSY) {
420 /* Tell user-space to retry the send */
421 skb->destructor = sock_wfree;
422 /* Free skb without triggering the perf drop trace */
423 consume_skb(skb);
424 err = -EAGAIN;
425 goto out;
426 }
427
Magnus Karlssonc5ed924b2019-12-19 13:39:26 +0100428 xskq_cons_release(xs->tx);
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200429 /* Ignore NET_XMIT_CN as packet might have been sent */
Magnus Karlsson642e4502020-09-16 14:00:25 +0200430 if (err == NET_XMIT_DROP) {
Magnus Karlssonfe588682018-06-29 09:48:18 +0200431 /* SKB completed but not sent */
Magnus Karlsson642e4502020-09-16 14:00:25 +0200432 kfree_skb(skb);
Magnus Karlssonfe588682018-06-29 09:48:18 +0200433 err = -EBUSY;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200434 goto out;
435 }
436
Magnus Karlsson642e4502020-09-16 14:00:25 +0200437 consume_skb(skb);
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200438 sent_frame = true;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200439 }
440
Ciara Loftus8aa5a332020-07-08 07:28:33 +0000441 xs->tx->queue_empty_descs++;
442
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200443out:
444 if (sent_frame)
445 sk->sk_write_space(sk);
446
447 mutex_unlock(&xs->mutex);
448 return err;
449}
450
Magnus Karlssondf551052019-10-02 08:31:59 +0200451static int __xsk_sendmsg(struct sock *sk)
452{
453 struct xdp_sock *xs = xdp_sk(sk);
454
455 if (unlikely(!(xs->dev->flags & IFF_UP)))
456 return -ENETDOWN;
457 if (unlikely(!xs->tx))
458 return -ENOBUFS;
459
460 return xs->zc ? xsk_zc_xmit(xs) : xsk_generic_xmit(sk);
461}
462
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200463static int xsk_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
464{
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200465 bool need_wait = !(m->msg_flags & MSG_DONTWAIT);
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200466 struct sock *sk = sock->sk;
467 struct xdp_sock *xs = xdp_sk(sk);
468
Björn Töpel42fddcc2019-09-04 13:49:12 +0200469 if (unlikely(!xsk_is_bound(xs)))
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200470 return -ENXIO;
Magnus Karlssondf551052019-10-02 08:31:59 +0200471 if (unlikely(need_wait))
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200472 return -EOPNOTSUPP;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200473
Magnus Karlssondf551052019-10-02 08:31:59 +0200474 return __xsk_sendmsg(sk);
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200475}
476
Luc Van Oostenryck5d946c52019-11-20 01:10:42 +0100477static __poll_t xsk_poll(struct file *file, struct socket *sock,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700478 struct poll_table_struct *wait)
Björn Töpelc4971762018-05-02 13:01:27 +0200479{
Luc Van Oostenryck5d946c52019-11-20 01:10:42 +0100480 __poll_t mask = datagram_poll(file, sock, wait);
Magnus Karlssondf551052019-10-02 08:31:59 +0200481 struct sock *sk = sock->sk;
482 struct xdp_sock *xs = xdp_sk(sk);
Magnus Karlssonc2d3d6a2020-08-28 10:26:19 +0200483 struct xsk_buff_pool *pool;
Björn Töpel42fddcc2019-09-04 13:49:12 +0200484
485 if (unlikely(!xsk_is_bound(xs)))
486 return mask;
487
Magnus Karlssonc2d3d6a2020-08-28 10:26:19 +0200488 pool = xs->pool;
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200489
Magnus Karlssonc2d3d6a2020-08-28 10:26:19 +0200490 if (pool->cached_need_wakeup) {
Maxim Mikityanskiy06870682019-12-17 16:20:42 +0000491 if (xs->zc)
Magnus Karlssonc2d3d6a2020-08-28 10:26:19 +0200492 xsk_wakeup(xs, pool->cached_need_wakeup);
Magnus Karlssondf551052019-10-02 08:31:59 +0200493 else
494 /* Poll needs to drive Tx also in copy mode */
495 __xsk_sendmsg(sk);
496 }
Björn Töpelc4971762018-05-02 13:01:27 +0200497
Magnus Karlsson59e35e52019-12-19 13:39:23 +0100498 if (xs->rx && !xskq_prod_is_empty(xs->rx))
Luc Van Oostenryck5d946c52019-11-20 01:10:42 +0100499 mask |= EPOLLIN | EPOLLRDNORM;
Magnus Karlssonc5ed924b2019-12-19 13:39:26 +0100500 if (xs->tx && !xskq_cons_is_full(xs->tx))
Luc Van Oostenryck5d946c52019-11-20 01:10:42 +0100501 mask |= EPOLLOUT | EPOLLWRNORM;
Björn Töpelc4971762018-05-02 13:01:27 +0200502
503 return mask;
504}
505
Björn Töpelb9b6b682018-05-02 13:01:25 +0200506static int xsk_init_queue(u32 entries, struct xsk_queue **queue,
507 bool umem_queue)
Magnus Karlsson423f3832018-05-02 13:01:24 +0200508{
509 struct xsk_queue *q;
510
511 if (entries == 0 || *queue || !is_power_of_2(entries))
512 return -EINVAL;
513
Björn Töpelb9b6b682018-05-02 13:01:25 +0200514 q = xskq_create(entries, umem_queue);
Magnus Karlsson423f3832018-05-02 13:01:24 +0200515 if (!q)
516 return -ENOMEM;
517
Björn Töpel37b07692018-05-22 09:35:01 +0200518 /* Make sure queue is ready before it can be seen by others */
519 smp_wmb();
Björn Töpel94a99762019-09-04 13:49:10 +0200520 WRITE_ONCE(*queue, q);
Magnus Karlsson423f3832018-05-02 13:01:24 +0200521 return 0;
522}
523
Ilya Maximets455302d2019-06-28 11:04:07 +0300524static void xsk_unbind_dev(struct xdp_sock *xs)
525{
526 struct net_device *dev = xs->dev;
527
Björn Töpel42fddcc2019-09-04 13:49:12 +0200528 if (xs->state != XSK_BOUND)
Ilya Maximets455302d2019-06-28 11:04:07 +0300529 return;
Björn Töpel42fddcc2019-09-04 13:49:12 +0200530 WRITE_ONCE(xs->state, XSK_UNBOUND);
Ilya Maximets455302d2019-06-28 11:04:07 +0300531
532 /* Wait for driver to stop using the xdp socket. */
Magnus Karlssona5aa8e52020-08-28 10:26:20 +0200533 xp_del_xsk(xs->pool, xs);
Ilya Maximets455302d2019-06-28 11:04:07 +0300534 xs->dev = NULL;
535 synchronize_net();
536 dev_put(dev);
537}
538
Björn Töpel0402acd2019-08-15 11:30:13 +0200539static struct xsk_map *xsk_get_map_list_entry(struct xdp_sock *xs,
540 struct xdp_sock ***map_entry)
541{
542 struct xsk_map *map = NULL;
543 struct xsk_map_node *node;
544
545 *map_entry = NULL;
546
547 spin_lock_bh(&xs->map_list_lock);
548 node = list_first_entry_or_null(&xs->map_list, struct xsk_map_node,
549 node);
550 if (node) {
551 WARN_ON(xsk_map_inc(node->map));
552 map = node->map;
553 *map_entry = node->map_entry;
554 }
555 spin_unlock_bh(&xs->map_list_lock);
556 return map;
557}
558
559static void xsk_delete_from_maps(struct xdp_sock *xs)
560{
561 /* This function removes the current XDP socket from all the
562 * maps it resides in. We need to take extra care here, due to
563 * the two locks involved. Each map has a lock synchronizing
564 * updates to the entries, and each socket has a lock that
565 * synchronizes access to the list of maps (map_list). For
566 * deadlock avoidance the locks need to be taken in the order
567 * "map lock"->"socket map list lock". We start off by
568 * accessing the socket map list, and take a reference to the
569 * map to guarantee existence between the
570 * xsk_get_map_list_entry() and xsk_map_try_sock_delete()
571 * calls. Then we ask the map to remove the socket, which
572 * tries to remove the socket from the map. Note that there
573 * might be updates to the map between
574 * xsk_get_map_list_entry() and xsk_map_try_sock_delete().
575 */
576 struct xdp_sock **map_entry = NULL;
577 struct xsk_map *map;
578
579 while ((map = xsk_get_map_list_entry(xs, &map_entry))) {
580 xsk_map_try_sock_delete(map, xs, map_entry);
581 xsk_map_put(map);
582 }
583}
584
Björn Töpelc0c77d82018-05-02 13:01:23 +0200585static int xsk_release(struct socket *sock)
586{
587 struct sock *sk = sock->sk;
Magnus Karlsson965a9902018-05-02 13:01:26 +0200588 struct xdp_sock *xs = xdp_sk(sk);
Björn Töpelc0c77d82018-05-02 13:01:23 +0200589 struct net *net;
590
591 if (!sk)
592 return 0;
593
594 net = sock_net(sk);
595
Björn Töpel1d0dc062019-01-24 19:59:37 +0100596 mutex_lock(&net->xdp.lock);
597 sk_del_node_init_rcu(sk);
598 mutex_unlock(&net->xdp.lock);
599
Björn Töpelc0c77d82018-05-02 13:01:23 +0200600 local_bh_disable();
601 sock_prot_inuse_add(net, sk->sk_prot, -1);
602 local_bh_enable();
603
Björn Töpel0402acd2019-08-15 11:30:13 +0200604 xsk_delete_from_maps(xs);
Björn Töpel42fddcc2019-09-04 13:49:12 +0200605 mutex_lock(&xs->mutex);
Ilya Maximets455302d2019-06-28 11:04:07 +0300606 xsk_unbind_dev(xs);
Björn Töpel42fddcc2019-09-04 13:49:12 +0200607 mutex_unlock(&xs->mutex);
Magnus Karlsson965a9902018-05-02 13:01:26 +0200608
Björn Töpel541d7fd2018-10-05 13:25:15 +0200609 xskq_destroy(xs->rx);
610 xskq_destroy(xs->tx);
Magnus Karlsson7361f9c2020-08-28 10:26:18 +0200611 xskq_destroy(xs->fq_tmp);
612 xskq_destroy(xs->cq_tmp);
Björn Töpel541d7fd2018-10-05 13:25:15 +0200613
Björn Töpelc0c77d82018-05-02 13:01:23 +0200614 sock_orphan(sk);
615 sock->sk = NULL;
616
617 sk_refcnt_debug_release(sk);
618 sock_put(sk);
619
620 return 0;
621}
622
Magnus Karlsson965a9902018-05-02 13:01:26 +0200623static struct socket *xsk_lookup_xsk_from_fd(int fd)
624{
625 struct socket *sock;
626 int err;
627
628 sock = sockfd_lookup(fd, &err);
629 if (!sock)
630 return ERR_PTR(-ENOTSOCK);
631
632 if (sock->sk->sk_family != PF_XDP) {
633 sockfd_put(sock);
634 return ERR_PTR(-ENOPROTOOPT);
635 }
636
637 return sock;
638}
639
Magnus Karlsson7361f9c2020-08-28 10:26:18 +0200640static bool xsk_validate_queues(struct xdp_sock *xs)
641{
642 return xs->fq_tmp && xs->cq_tmp;
643}
644
Magnus Karlsson965a9902018-05-02 13:01:26 +0200645static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
646{
647 struct sockaddr_xdp *sxdp = (struct sockaddr_xdp *)addr;
648 struct sock *sk = sock->sk;
Magnus Karlsson965a9902018-05-02 13:01:26 +0200649 struct xdp_sock *xs = xdp_sk(sk);
Björn Töpel959b71d2018-05-22 09:34:56 +0200650 struct net_device *dev;
Björn Töpel173d3ad2018-06-04 14:05:55 +0200651 u32 flags, qid;
Magnus Karlsson965a9902018-05-02 13:01:26 +0200652 int err = 0;
653
654 if (addr_len < sizeof(struct sockaddr_xdp))
655 return -EINVAL;
656 if (sxdp->sxdp_family != AF_XDP)
657 return -EINVAL;
658
Björn Töpelf54ba392019-03-08 08:57:26 +0100659 flags = sxdp->sxdp_flags;
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200660 if (flags & ~(XDP_SHARED_UMEM | XDP_COPY | XDP_ZEROCOPY |
661 XDP_USE_NEED_WAKEUP))
Björn Töpelf54ba392019-03-08 08:57:26 +0100662 return -EINVAL;
663
Ilya Maximets5464c3a2019-07-08 14:03:44 +0300664 rtnl_lock();
Magnus Karlsson965a9902018-05-02 13:01:26 +0200665 mutex_lock(&xs->mutex);
Ilya Maximets455302d2019-06-28 11:04:07 +0300666 if (xs->state != XSK_READY) {
Björn Töpel959b71d2018-05-22 09:34:56 +0200667 err = -EBUSY;
668 goto out_release;
669 }
670
Magnus Karlsson965a9902018-05-02 13:01:26 +0200671 dev = dev_get_by_index(sock_net(sk), sxdp->sxdp_ifindex);
672 if (!dev) {
673 err = -ENODEV;
674 goto out_release;
675 }
676
Magnus Karlssonf6145902018-05-02 13:01:32 +0200677 if (!xs->rx && !xs->tx) {
Magnus Karlsson965a9902018-05-02 13:01:26 +0200678 err = -EINVAL;
679 goto out_unlock;
680 }
681
Björn Töpel173d3ad2018-06-04 14:05:55 +0200682 qid = sxdp->sxdp_queue_id;
Björn Töpel173d3ad2018-06-04 14:05:55 +0200683
684 if (flags & XDP_SHARED_UMEM) {
Magnus Karlsson965a9902018-05-02 13:01:26 +0200685 struct xdp_sock *umem_xs;
686 struct socket *sock;
687
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200688 if ((flags & XDP_COPY) || (flags & XDP_ZEROCOPY) ||
689 (flags & XDP_USE_NEED_WAKEUP)) {
Björn Töpel173d3ad2018-06-04 14:05:55 +0200690 /* Cannot specify flags for shared sockets. */
691 err = -EINVAL;
692 goto out_unlock;
693 }
694
Magnus Karlsson965a9902018-05-02 13:01:26 +0200695 if (xs->umem) {
696 /* We have already our own. */
697 err = -EINVAL;
698 goto out_unlock;
699 }
700
701 sock = xsk_lookup_xsk_from_fd(sxdp->sxdp_shared_umem_fd);
702 if (IS_ERR(sock)) {
703 err = PTR_ERR(sock);
704 goto out_unlock;
705 }
706
707 umem_xs = xdp_sk(sock->sk);
Björn Töpel42fddcc2019-09-04 13:49:12 +0200708 if (!xsk_is_bound(umem_xs)) {
Magnus Karlsson965a9902018-05-02 13:01:26 +0200709 err = -EBADF;
710 sockfd_put(sock);
711 goto out_unlock;
Björn Töpel42fddcc2019-09-04 13:49:12 +0200712 }
Magnus Karlsson965a9902018-05-02 13:01:26 +0200713
Magnus Karlssona1132432020-08-28 10:26:26 +0200714 if (umem_xs->queue_id != qid || umem_xs->dev != dev) {
715 /* Share the umem with another socket on another qid
716 * and/or device.
717 */
Magnus Karlssonb5aea282020-08-28 10:26:25 +0200718 xs->pool = xp_create_and_assign_umem(xs,
719 umem_xs->umem);
720 if (!xs->pool) {
Magnus Karlsson1fd17c8c2020-09-26 11:26:13 +0200721 err = -ENOMEM;
Magnus Karlssonb5aea282020-08-28 10:26:25 +0200722 sockfd_put(sock);
723 goto out_unlock;
724 }
725
726 err = xp_assign_dev_shared(xs->pool, umem_xs->umem,
727 dev, qid);
728 if (err) {
729 xp_destroy(xs->pool);
Magnus Karlsson83cf5c62020-09-02 09:36:04 +0200730 xs->pool = NULL;
Magnus Karlssonb5aea282020-08-28 10:26:25 +0200731 sockfd_put(sock);
732 goto out_unlock;
733 }
734 } else {
735 /* Share the buffer pool with the other socket. */
736 if (xs->fq_tmp || xs->cq_tmp) {
737 /* Do not allow setting your own fq or cq. */
738 err = -EINVAL;
739 sockfd_put(sock);
740 goto out_unlock;
741 }
742
743 xp_get_pool(umem_xs->pool);
744 xs->pool = umem_xs->pool;
745 }
746
Magnus Karlsson965a9902018-05-02 13:01:26 +0200747 xdp_get_umem(umem_xs->umem);
Björn Töpel9764f4b2019-09-04 13:49:11 +0200748 WRITE_ONCE(xs->umem, umem_xs->umem);
Magnus Karlsson965a9902018-05-02 13:01:26 +0200749 sockfd_put(sock);
Magnus Karlsson7361f9c2020-08-28 10:26:18 +0200750 } else if (!xs->umem || !xsk_validate_queues(xs)) {
Magnus Karlsson965a9902018-05-02 13:01:26 +0200751 err = -EINVAL;
752 goto out_unlock;
Björn Töpelc4971762018-05-02 13:01:27 +0200753 } else {
754 /* This xsk has its own umem. */
Magnus Karlsson1c1efc22020-08-28 10:26:17 +0200755 xs->pool = xp_create_and_assign_umem(xs, xs->umem);
756 if (!xs->pool) {
757 err = -ENOMEM;
Björn Töpel173d3ad2018-06-04 14:05:55 +0200758 goto out_unlock;
Magnus Karlsson1c1efc22020-08-28 10:26:17 +0200759 }
760
761 err = xp_assign_dev(xs->pool, dev, qid, flags);
762 if (err) {
763 xp_destroy(xs->pool);
764 xs->pool = NULL;
Magnus Karlsson1c1efc22020-08-28 10:26:17 +0200765 goto out_unlock;
766 }
Magnus Karlsson965a9902018-05-02 13:01:26 +0200767 }
768
Magnus Karlsson965a9902018-05-02 13:01:26 +0200769 xs->dev = dev;
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200770 xs->zc = xs->umem->zc;
771 xs->queue_id = qid;
Magnus Karlssona5aa8e52020-08-28 10:26:20 +0200772 xp_add_xsk(xs->pool, xs);
Magnus Karlsson965a9902018-05-02 13:01:26 +0200773
774out_unlock:
Björn Töpel42fddcc2019-09-04 13:49:12 +0200775 if (err) {
Magnus Karlsson965a9902018-05-02 13:01:26 +0200776 dev_put(dev);
Björn Töpel42fddcc2019-09-04 13:49:12 +0200777 } else {
778 /* Matches smp_rmb() in bind() for shared umem
779 * sockets, and xsk_is_bound().
780 */
781 smp_wmb();
782 WRITE_ONCE(xs->state, XSK_BOUND);
783 }
Magnus Karlsson965a9902018-05-02 13:01:26 +0200784out_release:
785 mutex_unlock(&xs->mutex);
Ilya Maximets5464c3a2019-07-08 14:03:44 +0300786 rtnl_unlock();
Magnus Karlsson965a9902018-05-02 13:01:26 +0200787 return err;
788}
789
Kevin Laatzc05cd362019-08-27 02:25:22 +0000790struct xdp_umem_reg_v1 {
791 __u64 addr; /* Start of packet data area */
792 __u64 len; /* Length of packet data area */
793 __u32 chunk_size;
794 __u32 headroom;
795};
796
Björn Töpelc0c77d82018-05-02 13:01:23 +0200797static int xsk_setsockopt(struct socket *sock, int level, int optname,
Christoph Hellwiga7b75c52020-07-23 08:09:07 +0200798 sockptr_t optval, unsigned int optlen)
Björn Töpelc0c77d82018-05-02 13:01:23 +0200799{
800 struct sock *sk = sock->sk;
801 struct xdp_sock *xs = xdp_sk(sk);
802 int err;
803
804 if (level != SOL_XDP)
805 return -ENOPROTOOPT;
806
807 switch (optname) {
Björn Töpelb9b6b682018-05-02 13:01:25 +0200808 case XDP_RX_RING:
Magnus Karlssonf6145902018-05-02 13:01:32 +0200809 case XDP_TX_RING:
Björn Töpelb9b6b682018-05-02 13:01:25 +0200810 {
811 struct xsk_queue **q;
812 int entries;
813
814 if (optlen < sizeof(entries))
815 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +0200816 if (copy_from_sockptr(&entries, optval, sizeof(entries)))
Björn Töpelb9b6b682018-05-02 13:01:25 +0200817 return -EFAULT;
818
819 mutex_lock(&xs->mutex);
Ilya Maximets455302d2019-06-28 11:04:07 +0300820 if (xs->state != XSK_READY) {
821 mutex_unlock(&xs->mutex);
822 return -EBUSY;
823 }
Magnus Karlssonf6145902018-05-02 13:01:32 +0200824 q = (optname == XDP_TX_RING) ? &xs->tx : &xs->rx;
Björn Töpelb9b6b682018-05-02 13:01:25 +0200825 err = xsk_init_queue(entries, q, false);
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200826 if (!err && optname == XDP_TX_RING)
827 /* Tx needs to be explicitly woken up the first time */
828 xs->tx->ring->flags |= XDP_RING_NEED_WAKEUP;
Björn Töpelb9b6b682018-05-02 13:01:25 +0200829 mutex_unlock(&xs->mutex);
830 return err;
831 }
Björn Töpelc0c77d82018-05-02 13:01:23 +0200832 case XDP_UMEM_REG:
833 {
Kevin Laatzc05cd362019-08-27 02:25:22 +0000834 size_t mr_size = sizeof(struct xdp_umem_reg);
835 struct xdp_umem_reg mr = {};
Björn Töpelc0c77d82018-05-02 13:01:23 +0200836 struct xdp_umem *umem;
837
Kevin Laatzc05cd362019-08-27 02:25:22 +0000838 if (optlen < sizeof(struct xdp_umem_reg_v1))
839 return -EINVAL;
840 else if (optlen < sizeof(mr))
841 mr_size = sizeof(struct xdp_umem_reg_v1);
842
Christoph Hellwiga7b75c52020-07-23 08:09:07 +0200843 if (copy_from_sockptr(&mr, optval, mr_size))
Björn Töpelc0c77d82018-05-02 13:01:23 +0200844 return -EFAULT;
845
846 mutex_lock(&xs->mutex);
Ilya Maximets455302d2019-06-28 11:04:07 +0300847 if (xs->state != XSK_READY || xs->umem) {
Björn Töpelc0c77d82018-05-02 13:01:23 +0200848 mutex_unlock(&xs->mutex);
Björn Töpela49049e2018-05-22 09:35:02 +0200849 return -EBUSY;
850 }
851
852 umem = xdp_umem_create(&mr);
853 if (IS_ERR(umem)) {
854 mutex_unlock(&xs->mutex);
855 return PTR_ERR(umem);
Björn Töpelc0c77d82018-05-02 13:01:23 +0200856 }
857
858 /* Make sure umem is ready before it can be seen by others */
859 smp_wmb();
Björn Töpel9764f4b2019-09-04 13:49:11 +0200860 WRITE_ONCE(xs->umem, umem);
Björn Töpelc0c77d82018-05-02 13:01:23 +0200861 mutex_unlock(&xs->mutex);
862 return 0;
863 }
Magnus Karlsson423f3832018-05-02 13:01:24 +0200864 case XDP_UMEM_FILL_RING:
Magnus Karlssonfe230832018-05-02 13:01:31 +0200865 case XDP_UMEM_COMPLETION_RING:
Magnus Karlsson423f3832018-05-02 13:01:24 +0200866 {
867 struct xsk_queue **q;
868 int entries;
869
Christoph Hellwiga7b75c52020-07-23 08:09:07 +0200870 if (copy_from_sockptr(&entries, optval, sizeof(entries)))
Magnus Karlsson423f3832018-05-02 13:01:24 +0200871 return -EFAULT;
872
873 mutex_lock(&xs->mutex);
Ilya Maximets455302d2019-06-28 11:04:07 +0300874 if (xs->state != XSK_READY) {
875 mutex_unlock(&xs->mutex);
876 return -EBUSY;
877 }
Björn Töpela49049e2018-05-22 09:35:02 +0200878
Magnus Karlsson7361f9c2020-08-28 10:26:18 +0200879 q = (optname == XDP_UMEM_FILL_RING) ? &xs->fq_tmp :
880 &xs->cq_tmp;
Björn Töpelb9b6b682018-05-02 13:01:25 +0200881 err = xsk_init_queue(entries, q, true);
Magnus Karlsson423f3832018-05-02 13:01:24 +0200882 mutex_unlock(&xs->mutex);
883 return err;
884 }
Björn Töpelc0c77d82018-05-02 13:01:23 +0200885 default:
886 break;
887 }
888
889 return -ENOPROTOOPT;
890}
891
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200892static void xsk_enter_rxtx_offsets(struct xdp_ring_offset_v1 *ring)
893{
894 ring->producer = offsetof(struct xdp_rxtx_ring, ptrs.producer);
895 ring->consumer = offsetof(struct xdp_rxtx_ring, ptrs.consumer);
896 ring->desc = offsetof(struct xdp_rxtx_ring, desc);
897}
898
899static void xsk_enter_umem_offsets(struct xdp_ring_offset_v1 *ring)
900{
901 ring->producer = offsetof(struct xdp_umem_ring, ptrs.producer);
902 ring->consumer = offsetof(struct xdp_umem_ring, ptrs.consumer);
903 ring->desc = offsetof(struct xdp_umem_ring, desc);
904}
905
Ciara Loftus8aa5a332020-07-08 07:28:33 +0000906struct xdp_statistics_v1 {
907 __u64 rx_dropped;
908 __u64 rx_invalid_descs;
909 __u64 tx_invalid_descs;
910};
911
Magnus Karlssonaf75d9e2018-05-02 13:01:35 +0200912static int xsk_getsockopt(struct socket *sock, int level, int optname,
913 char __user *optval, int __user *optlen)
914{
915 struct sock *sk = sock->sk;
916 struct xdp_sock *xs = xdp_sk(sk);
917 int len;
918
919 if (level != SOL_XDP)
920 return -ENOPROTOOPT;
921
922 if (get_user(len, optlen))
923 return -EFAULT;
924 if (len < 0)
925 return -EINVAL;
926
927 switch (optname) {
928 case XDP_STATISTICS:
929 {
Peilin Ye3c4f8502020-07-28 01:36:04 -0400930 struct xdp_statistics stats = {};
Ciara Loftus8aa5a332020-07-08 07:28:33 +0000931 bool extra_stats = true;
932 size_t stats_size;
Magnus Karlssonaf75d9e2018-05-02 13:01:35 +0200933
Ciara Loftus8aa5a332020-07-08 07:28:33 +0000934 if (len < sizeof(struct xdp_statistics_v1)) {
Magnus Karlssonaf75d9e2018-05-02 13:01:35 +0200935 return -EINVAL;
Ciara Loftus8aa5a332020-07-08 07:28:33 +0000936 } else if (len < sizeof(stats)) {
937 extra_stats = false;
938 stats_size = sizeof(struct xdp_statistics_v1);
939 } else {
940 stats_size = sizeof(stats);
941 }
Magnus Karlssonaf75d9e2018-05-02 13:01:35 +0200942
943 mutex_lock(&xs->mutex);
944 stats.rx_dropped = xs->rx_dropped;
Ciara Loftus8aa5a332020-07-08 07:28:33 +0000945 if (extra_stats) {
946 stats.rx_ring_full = xs->rx_queue_full;
947 stats.rx_fill_ring_empty_descs =
Magnus Karlsson7361f9c2020-08-28 10:26:18 +0200948 xs->pool ? xskq_nb_queue_empty_descs(xs->pool->fq) : 0;
Ciara Loftus8aa5a332020-07-08 07:28:33 +0000949 stats.tx_ring_empty_descs = xskq_nb_queue_empty_descs(xs->tx);
950 } else {
951 stats.rx_dropped += xs->rx_queue_full;
952 }
Magnus Karlssonaf75d9e2018-05-02 13:01:35 +0200953 stats.rx_invalid_descs = xskq_nb_invalid_descs(xs->rx);
954 stats.tx_invalid_descs = xskq_nb_invalid_descs(xs->tx);
955 mutex_unlock(&xs->mutex);
956
Ciara Loftus8aa5a332020-07-08 07:28:33 +0000957 if (copy_to_user(optval, &stats, stats_size))
Magnus Karlssonaf75d9e2018-05-02 13:01:35 +0200958 return -EFAULT;
Ciara Loftus8aa5a332020-07-08 07:28:33 +0000959 if (put_user(stats_size, optlen))
Magnus Karlssonaf75d9e2018-05-02 13:01:35 +0200960 return -EFAULT;
961
962 return 0;
963 }
Björn Töpelb3a9e0b2018-05-22 09:34:59 +0200964 case XDP_MMAP_OFFSETS:
965 {
966 struct xdp_mmap_offsets off;
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200967 struct xdp_mmap_offsets_v1 off_v1;
968 bool flags_supported = true;
969 void *to_copy;
Björn Töpelb3a9e0b2018-05-22 09:34:59 +0200970
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200971 if (len < sizeof(off_v1))
Björn Töpelb3a9e0b2018-05-22 09:34:59 +0200972 return -EINVAL;
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200973 else if (len < sizeof(off))
974 flags_supported = false;
Björn Töpelb3a9e0b2018-05-22 09:34:59 +0200975
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200976 if (flags_supported) {
977 /* xdp_ring_offset is identical to xdp_ring_offset_v1
978 * except for the flags field added to the end.
979 */
980 xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1 *)
981 &off.rx);
982 xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1 *)
983 &off.tx);
984 xsk_enter_umem_offsets((struct xdp_ring_offset_v1 *)
985 &off.fr);
986 xsk_enter_umem_offsets((struct xdp_ring_offset_v1 *)
987 &off.cr);
988 off.rx.flags = offsetof(struct xdp_rxtx_ring,
989 ptrs.flags);
990 off.tx.flags = offsetof(struct xdp_rxtx_ring,
991 ptrs.flags);
992 off.fr.flags = offsetof(struct xdp_umem_ring,
993 ptrs.flags);
994 off.cr.flags = offsetof(struct xdp_umem_ring,
995 ptrs.flags);
Björn Töpelb3a9e0b2018-05-22 09:34:59 +0200996
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200997 len = sizeof(off);
998 to_copy = &off;
999 } else {
1000 xsk_enter_rxtx_offsets(&off_v1.rx);
1001 xsk_enter_rxtx_offsets(&off_v1.tx);
1002 xsk_enter_umem_offsets(&off_v1.fr);
1003 xsk_enter_umem_offsets(&off_v1.cr);
Björn Töpelb3a9e0b2018-05-22 09:34:59 +02001004
Magnus Karlsson77cd0d72019-08-14 09:27:17 +02001005 len = sizeof(off_v1);
1006 to_copy = &off_v1;
1007 }
1008
1009 if (copy_to_user(optval, to_copy, len))
Björn Töpelb3a9e0b2018-05-22 09:34:59 +02001010 return -EFAULT;
1011 if (put_user(len, optlen))
1012 return -EFAULT;
1013
1014 return 0;
1015 }
Maxim Mikityanskiy2640d3c2019-06-26 17:35:25 +03001016 case XDP_OPTIONS:
1017 {
1018 struct xdp_options opts = {};
1019
1020 if (len < sizeof(opts))
1021 return -EINVAL;
1022
1023 mutex_lock(&xs->mutex);
1024 if (xs->zc)
1025 opts.flags |= XDP_OPTIONS_ZEROCOPY;
1026 mutex_unlock(&xs->mutex);
1027
1028 len = sizeof(opts);
1029 if (copy_to_user(optval, &opts, len))
1030 return -EFAULT;
1031 if (put_user(len, optlen))
1032 return -EFAULT;
1033
1034 return 0;
1035 }
Magnus Karlssonaf75d9e2018-05-02 13:01:35 +02001036 default:
1037 break;
1038 }
1039
1040 return -EOPNOTSUPP;
1041}
1042
Magnus Karlsson423f3832018-05-02 13:01:24 +02001043static int xsk_mmap(struct file *file, struct socket *sock,
1044 struct vm_area_struct *vma)
1045{
Geert Uytterhoevena5a16e42018-06-07 15:37:34 +02001046 loff_t offset = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
Magnus Karlsson423f3832018-05-02 13:01:24 +02001047 unsigned long size = vma->vm_end - vma->vm_start;
1048 struct xdp_sock *xs = xdp_sk(sock->sk);
1049 struct xsk_queue *q = NULL;
1050 unsigned long pfn;
1051 struct page *qpg;
1052
Björn Töpel42fddcc2019-09-04 13:49:12 +02001053 if (READ_ONCE(xs->state) != XSK_READY)
Ilya Maximets455302d2019-06-28 11:04:07 +03001054 return -EBUSY;
1055
Björn Töpelb9b6b682018-05-02 13:01:25 +02001056 if (offset == XDP_PGOFF_RX_RING) {
Björn Töpel37b07692018-05-22 09:35:01 +02001057 q = READ_ONCE(xs->rx);
Magnus Karlssonf6145902018-05-02 13:01:32 +02001058 } else if (offset == XDP_PGOFF_TX_RING) {
Björn Töpel37b07692018-05-22 09:35:01 +02001059 q = READ_ONCE(xs->tx);
Björn Töpelb9b6b682018-05-02 13:01:25 +02001060 } else {
Magnus Karlssone6762c82019-02-08 14:13:50 +01001061 /* Matches the smp_wmb() in XDP_UMEM_REG */
1062 smp_rmb();
Björn Töpelb9b6b682018-05-02 13:01:25 +02001063 if (offset == XDP_UMEM_PGOFF_FILL_RING)
Magnus Karlsson7361f9c2020-08-28 10:26:18 +02001064 q = READ_ONCE(xs->fq_tmp);
Magnus Karlssonfe230832018-05-02 13:01:31 +02001065 else if (offset == XDP_UMEM_PGOFF_COMPLETION_RING)
Magnus Karlsson7361f9c2020-08-28 10:26:18 +02001066 q = READ_ONCE(xs->cq_tmp);
Björn Töpelb9b6b682018-05-02 13:01:25 +02001067 }
Magnus Karlsson423f3832018-05-02 13:01:24 +02001068
1069 if (!q)
1070 return -EINVAL;
1071
Magnus Karlssone6762c82019-02-08 14:13:50 +01001072 /* Matches the smp_wmb() in xsk_init_queue */
1073 smp_rmb();
Magnus Karlsson423f3832018-05-02 13:01:24 +02001074 qpg = virt_to_head_page(q->ring);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07001075 if (size > page_size(qpg))
Magnus Karlsson423f3832018-05-02 13:01:24 +02001076 return -EINVAL;
1077
1078 pfn = virt_to_phys(q->ring) >> PAGE_SHIFT;
1079 return remap_pfn_range(vma, vma->vm_start, pfn,
1080 size, vma->vm_page_prot);
1081}
1082
Ilya Maximets455302d2019-06-28 11:04:07 +03001083static int xsk_notifier(struct notifier_block *this,
1084 unsigned long msg, void *ptr)
1085{
1086 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1087 struct net *net = dev_net(dev);
1088 struct sock *sk;
1089
1090 switch (msg) {
1091 case NETDEV_UNREGISTER:
1092 mutex_lock(&net->xdp.lock);
1093 sk_for_each(sk, &net->xdp.list) {
1094 struct xdp_sock *xs = xdp_sk(sk);
1095
1096 mutex_lock(&xs->mutex);
1097 if (xs->dev == dev) {
1098 sk->sk_err = ENETDOWN;
1099 if (!sock_flag(sk, SOCK_DEAD))
1100 sk->sk_error_report(sk);
1101
1102 xsk_unbind_dev(xs);
1103
Magnus Karlsson1c1efc22020-08-28 10:26:17 +02001104 /* Clear device references. */
1105 xp_clear_dev(xs->pool);
Ilya Maximets455302d2019-06-28 11:04:07 +03001106 }
1107 mutex_unlock(&xs->mutex);
1108 }
1109 mutex_unlock(&net->xdp.lock);
1110 break;
1111 }
1112 return NOTIFY_DONE;
1113}
1114
Björn Töpelc0c77d82018-05-02 13:01:23 +02001115static struct proto xsk_proto = {
1116 .name = "XDP",
1117 .owner = THIS_MODULE,
1118 .obj_size = sizeof(struct xdp_sock),
1119};
1120
1121static const struct proto_ops xsk_proto_ops = {
Björn Töpelc2f43742018-05-18 14:00:24 +02001122 .family = PF_XDP,
1123 .owner = THIS_MODULE,
1124 .release = xsk_release,
1125 .bind = xsk_bind,
1126 .connect = sock_no_connect,
1127 .socketpair = sock_no_socketpair,
1128 .accept = sock_no_accept,
1129 .getname = sock_no_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001130 .poll = xsk_poll,
Björn Töpelc2f43742018-05-18 14:00:24 +02001131 .ioctl = sock_no_ioctl,
1132 .listen = sock_no_listen,
1133 .shutdown = sock_no_shutdown,
1134 .setsockopt = xsk_setsockopt,
1135 .getsockopt = xsk_getsockopt,
1136 .sendmsg = xsk_sendmsg,
1137 .recvmsg = sock_no_recvmsg,
1138 .mmap = xsk_mmap,
1139 .sendpage = sock_no_sendpage,
Björn Töpelc0c77d82018-05-02 13:01:23 +02001140};
1141
Björn Töpel11fe9262019-02-21 13:07:38 +01001142static void xsk_destruct(struct sock *sk)
1143{
1144 struct xdp_sock *xs = xdp_sk(sk);
1145
1146 if (!sock_flag(sk, SOCK_DEAD))
1147 return;
1148
Magnus Karlssone5e1a4b2020-10-27 13:32:01 +01001149 if (!xp_put_pool(xs->pool))
Magnus Karlsson537cf4e2020-11-20 12:53:39 +01001150 xdp_put_umem(xs->umem, !xs->pool);
Björn Töpel11fe9262019-02-21 13:07:38 +01001151
1152 sk_refcnt_debug_dec(sk);
1153}
1154
Björn Töpelc0c77d82018-05-02 13:01:23 +02001155static int xsk_create(struct net *net, struct socket *sock, int protocol,
1156 int kern)
1157{
Björn Töpelc0c77d82018-05-02 13:01:23 +02001158 struct xdp_sock *xs;
Magnus Karlsson1c1efc22020-08-28 10:26:17 +02001159 struct sock *sk;
Björn Töpelc0c77d82018-05-02 13:01:23 +02001160
1161 if (!ns_capable(net->user_ns, CAP_NET_RAW))
1162 return -EPERM;
1163 if (sock->type != SOCK_RAW)
1164 return -ESOCKTNOSUPPORT;
1165
1166 if (protocol)
1167 return -EPROTONOSUPPORT;
1168
1169 sock->state = SS_UNCONNECTED;
1170
1171 sk = sk_alloc(net, PF_XDP, GFP_KERNEL, &xsk_proto, kern);
1172 if (!sk)
1173 return -ENOBUFS;
1174
1175 sock->ops = &xsk_proto_ops;
1176
1177 sock_init_data(sock, sk);
1178
1179 sk->sk_family = PF_XDP;
1180
Björn Töpel11fe9262019-02-21 13:07:38 +01001181 sk->sk_destruct = xsk_destruct;
1182 sk_refcnt_debug_inc(sk);
1183
Björn Töpelcee27162018-10-08 19:40:16 +02001184 sock_set_flag(sk, SOCK_RCU_FREE);
1185
Björn Töpelc0c77d82018-05-02 13:01:23 +02001186 xs = xdp_sk(sk);
Ilya Maximets455302d2019-06-28 11:04:07 +03001187 xs->state = XSK_READY;
Björn Töpelc0c77d82018-05-02 13:01:23 +02001188 mutex_init(&xs->mutex);
Ilya Maximetsbf0bdd12019-07-03 15:09:16 +03001189 spin_lock_init(&xs->rx_lock);
Magnus Karlssona9744f72018-06-29 09:48:20 +02001190 spin_lock_init(&xs->tx_completion_lock);
Björn Töpelc0c77d82018-05-02 13:01:23 +02001191
Björn Töpel0402acd2019-08-15 11:30:13 +02001192 INIT_LIST_HEAD(&xs->map_list);
1193 spin_lock_init(&xs->map_list_lock);
1194
Björn Töpel1d0dc062019-01-24 19:59:37 +01001195 mutex_lock(&net->xdp.lock);
1196 sk_add_node_rcu(sk, &net->xdp.list);
1197 mutex_unlock(&net->xdp.lock);
1198
Björn Töpelc0c77d82018-05-02 13:01:23 +02001199 local_bh_disable();
1200 sock_prot_inuse_add(net, &xsk_proto, 1);
1201 local_bh_enable();
1202
1203 return 0;
1204}
1205
1206static const struct net_proto_family xsk_family_ops = {
1207 .family = PF_XDP,
1208 .create = xsk_create,
1209 .owner = THIS_MODULE,
1210};
1211
Ilya Maximets455302d2019-06-28 11:04:07 +03001212static struct notifier_block xsk_netdev_notifier = {
1213 .notifier_call = xsk_notifier,
1214};
1215
Björn Töpel1d0dc062019-01-24 19:59:37 +01001216static int __net_init xsk_net_init(struct net *net)
1217{
1218 mutex_init(&net->xdp.lock);
1219 INIT_HLIST_HEAD(&net->xdp.list);
1220 return 0;
1221}
1222
1223static void __net_exit xsk_net_exit(struct net *net)
1224{
1225 WARN_ON_ONCE(!hlist_empty(&net->xdp.list));
1226}
1227
1228static struct pernet_operations xsk_net_ops = {
1229 .init = xsk_net_init,
1230 .exit = xsk_net_exit,
1231};
1232
Björn Töpelc0c77d82018-05-02 13:01:23 +02001233static int __init xsk_init(void)
1234{
Björn Töpele312b9e2019-12-19 07:10:02 +01001235 int err, cpu;
Björn Töpelc0c77d82018-05-02 13:01:23 +02001236
1237 err = proto_register(&xsk_proto, 0 /* no slab */);
1238 if (err)
1239 goto out;
1240
1241 err = sock_register(&xsk_family_ops);
1242 if (err)
1243 goto out_proto;
1244
Björn Töpel1d0dc062019-01-24 19:59:37 +01001245 err = register_pernet_subsys(&xsk_net_ops);
1246 if (err)
1247 goto out_sk;
Ilya Maximets455302d2019-06-28 11:04:07 +03001248
1249 err = register_netdevice_notifier(&xsk_netdev_notifier);
1250 if (err)
1251 goto out_pernet;
1252
Björn Töpele312b9e2019-12-19 07:10:02 +01001253 for_each_possible_cpu(cpu)
1254 INIT_LIST_HEAD(&per_cpu(xskmap_flush_list, cpu));
Björn Töpelc0c77d82018-05-02 13:01:23 +02001255 return 0;
1256
Ilya Maximets455302d2019-06-28 11:04:07 +03001257out_pernet:
1258 unregister_pernet_subsys(&xsk_net_ops);
Björn Töpel1d0dc062019-01-24 19:59:37 +01001259out_sk:
1260 sock_unregister(PF_XDP);
Björn Töpelc0c77d82018-05-02 13:01:23 +02001261out_proto:
1262 proto_unregister(&xsk_proto);
1263out:
1264 return err;
1265}
1266
1267fs_initcall(xsk_init);