blob: b6c0f08bd80d2e54610a4e3d1a0af4ce9abd6988 [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
Björn Töpelfbfc504a2018-05-02 13:01:28 +020036bool xsk_is_setup_for_bpf_map(struct xdp_sock *xs)
37{
Björn Töpel173d3ad2018-06-04 14:05:55 +020038 return READ_ONCE(xs->rx) && READ_ONCE(xs->umem) &&
39 READ_ONCE(xs->umem->fq);
Björn Töpelfbfc504a2018-05-02 13:01:28 +020040}
41
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020042void xsk_set_rx_need_wakeup(struct xdp_umem *umem)
43{
44 if (umem->need_wakeup & XDP_WAKEUP_RX)
45 return;
46
47 umem->fq->ring->flags |= XDP_RING_NEED_WAKEUP;
48 umem->need_wakeup |= XDP_WAKEUP_RX;
49}
50EXPORT_SYMBOL(xsk_set_rx_need_wakeup);
51
52void xsk_set_tx_need_wakeup(struct xdp_umem *umem)
53{
54 struct xdp_sock *xs;
55
56 if (umem->need_wakeup & XDP_WAKEUP_TX)
57 return;
58
59 rcu_read_lock();
Magnus Karlssone4e5aef2020-05-04 15:33:51 +020060 list_for_each_entry_rcu(xs, &umem->xsk_tx_list, list) {
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020061 xs->tx->ring->flags |= XDP_RING_NEED_WAKEUP;
62 }
63 rcu_read_unlock();
64
65 umem->need_wakeup |= XDP_WAKEUP_TX;
66}
67EXPORT_SYMBOL(xsk_set_tx_need_wakeup);
68
69void xsk_clear_rx_need_wakeup(struct xdp_umem *umem)
70{
71 if (!(umem->need_wakeup & XDP_WAKEUP_RX))
72 return;
73
74 umem->fq->ring->flags &= ~XDP_RING_NEED_WAKEUP;
75 umem->need_wakeup &= ~XDP_WAKEUP_RX;
76}
77EXPORT_SYMBOL(xsk_clear_rx_need_wakeup);
78
79void xsk_clear_tx_need_wakeup(struct xdp_umem *umem)
80{
81 struct xdp_sock *xs;
82
83 if (!(umem->need_wakeup & XDP_WAKEUP_TX))
84 return;
85
86 rcu_read_lock();
Magnus Karlssone4e5aef2020-05-04 15:33:51 +020087 list_for_each_entry_rcu(xs, &umem->xsk_tx_list, list) {
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020088 xs->tx->ring->flags &= ~XDP_RING_NEED_WAKEUP;
89 }
90 rcu_read_unlock();
91
92 umem->need_wakeup &= ~XDP_WAKEUP_TX;
93}
94EXPORT_SYMBOL(xsk_clear_tx_need_wakeup);
95
96bool xsk_umem_uses_need_wakeup(struct xdp_umem *umem)
97{
98 return umem->flags & XDP_UMEM_USES_NEED_WAKEUP;
99}
100EXPORT_SYMBOL(xsk_umem_uses_need_wakeup);
101
Björn Töpel26062b12020-05-20 21:21:02 +0200102void xp_release(struct xdp_buff_xsk *xskb)
103{
104 xskb->pool->free_heads[xskb->pool->free_heads_cnt++] = xskb;
105}
106
107static u64 xp_get_handle(struct xdp_buff_xsk *xskb)
108{
109 u64 offset = xskb->xdp.data - xskb->xdp.data_hard_start;
110
111 offset += xskb->pool->headroom;
112 if (!xskb->pool->unaligned)
113 return xskb->orig_addr + offset;
114 return xskb->orig_addr + (offset << XSK_UNALIGNED_BUF_OFFSET_SHIFT);
115}
116
Björn Töpel2b434702020-05-20 21:20:53 +0200117static int __xsk_rcv_zc(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
Kevin Laatzc05cd362019-08-27 02:25:22 +0000118{
Björn Töpel2b434702020-05-20 21:20:53 +0200119 struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
120 u64 addr;
121 int err;
Kevin Laatzc05cd362019-08-27 02:25:22 +0000122
Björn Töpel2b434702020-05-20 21:20:53 +0200123 addr = xp_get_handle(xskb);
124 err = xskq_prod_reserve_desc(xs->rx, addr, len);
125 if (err) {
126 xs->rx_dropped++;
127 return err;
128 }
Kevin Laatzc05cd362019-08-27 02:25:22 +0000129
Björn Töpel2b434702020-05-20 21:20:53 +0200130 xp_release(xskb);
131 return 0;
132}
Kevin Laatzc05cd362019-08-27 02:25:22 +0000133
Björn Töpel2b434702020-05-20 21:20:53 +0200134static void xsk_copy_xdp(struct xdp_buff *to, struct xdp_buff *from, u32 len)
135{
136 void *from_buf, *to_buf;
137 u32 metalen;
138
139 if (unlikely(xdp_data_meta_unsupported(from))) {
140 from_buf = from->data;
141 to_buf = to->data;
142 metalen = 0;
143 } else {
144 from_buf = from->data_meta;
145 metalen = from->data - from->data_meta;
146 to_buf = to->data - metalen;
Kevin Laatzc05cd362019-08-27 02:25:22 +0000147 }
148
149 memcpy(to_buf, from_buf, len + metalen);
150}
151
Björn Töpel2b434702020-05-20 21:20:53 +0200152static int __xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len,
153 bool explicit_free)
Björn Töpel173d3ad2018-06-04 14:05:55 +0200154{
Björn Töpel2b434702020-05-20 21:20:53 +0200155 struct xdp_buff *xsk_xdp;
Björn Töpel4e64c832018-06-04 13:57:11 +0200156 int err;
Björn Töpelc4971762018-05-02 13:01:27 +0200157
Björn Töpel2b434702020-05-20 21:20:53 +0200158 if (len > xsk_umem_get_rx_frame_size(xs->umem)) {
Björn Töpela509a952018-06-04 13:57:12 +0200159 xs->rx_dropped++;
Björn Töpelc4971762018-05-02 13:01:27 +0200160 return -ENOSPC;
Björn Töpela509a952018-06-04 13:57:12 +0200161 }
Björn Töpelc4971762018-05-02 13:01:27 +0200162
Björn Töpel2b434702020-05-20 21:20:53 +0200163 xsk_xdp = xsk_buff_alloc(xs->umem);
164 if (!xsk_xdp) {
Björn Töpela509a952018-06-04 13:57:12 +0200165 xs->rx_dropped++;
Björn Töpel2b434702020-05-20 21:20:53 +0200166 return -ENOSPC;
167 }
Björn Töpelc4971762018-05-02 13:01:27 +0200168
Björn Töpel2b434702020-05-20 21:20:53 +0200169 xsk_copy_xdp(xsk_xdp, xdp, len);
170 err = __xsk_rcv_zc(xs, xsk_xdp, len);
171 if (err) {
172 xsk_buff_free(xsk_xdp);
173 return err;
174 }
175 if (explicit_free)
176 xdp_return_buff(xdp);
177 return 0;
Björn Töpelc4971762018-05-02 13:01:27 +0200178}
179
Björn Töpel42fddcc2019-09-04 13:49:12 +0200180static bool xsk_is_bound(struct xdp_sock *xs)
181{
182 if (READ_ONCE(xs->state) == XSK_BOUND) {
183 /* Matches smp_wmb() in bind(). */
184 smp_rmb();
185 return true;
186 }
187 return false;
188}
189
Björn Töpel2b434702020-05-20 21:20:53 +0200190static int xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp,
191 bool explicit_free)
Björn Töpelc4971762018-05-02 13:01:27 +0200192{
Björn Töpel173d3ad2018-06-04 14:05:55 +0200193 u32 len;
Björn Töpelc4971762018-05-02 13:01:27 +0200194
Björn Töpel42fddcc2019-09-04 13:49:12 +0200195 if (!xsk_is_bound(xs))
196 return -EINVAL;
197
Björn Töpel173d3ad2018-06-04 14:05:55 +0200198 if (xs->dev != xdp->rxq->dev || xs->queue_id != xdp->rxq->queue_index)
199 return -EINVAL;
Björn Töpelc4971762018-05-02 13:01:27 +0200200
Björn Töpel173d3ad2018-06-04 14:05:55 +0200201 len = xdp->data_end - xdp->data;
202
Björn Töpel08078922020-05-20 21:21:00 +0200203 return xdp->rxq->mem.type == MEM_TYPE_XSK_BUFF_POOL ?
Björn Töpel2b434702020-05-20 21:20:53 +0200204 __xsk_rcv_zc(xs, xdp, len) :
205 __xsk_rcv(xs, xdp, len, explicit_free);
Björn Töpelc4971762018-05-02 13:01:27 +0200206}
207
Björn Töpeld8179912019-11-01 12:03:46 +0100208static void xsk_flush(struct xdp_sock *xs)
Björn Töpelc4971762018-05-02 13:01:27 +0200209{
Magnus Karlsson59e35e52019-12-19 13:39:23 +0100210 xskq_prod_submit(xs->rx);
Magnus Karlsson30744a62020-02-10 16:27:12 +0100211 __xskq_cons_release(xs->umem->fq);
Björn Töpel43a825a2020-01-20 10:29:17 +0100212 sock_def_readable(&xs->sk);
Björn Töpelc4971762018-05-02 13:01:27 +0200213}
214
215int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
216{
217 int err;
218
Ilya Maximetsbf0bdd12019-07-03 15:09:16 +0300219 spin_lock_bh(&xs->rx_lock);
Björn Töpel2b434702020-05-20 21:20:53 +0200220 err = xsk_rcv(xs, xdp, false);
221 xsk_flush(xs);
Ilya Maximetsbf0bdd12019-07-03 15:09:16 +0300222 spin_unlock_bh(&xs->rx_lock);
Björn Töpelc4971762018-05-02 13:01:27 +0200223 return err;
224}
225
Björn Töpele312b9e2019-12-19 07:10:02 +0100226int __xsk_map_redirect(struct xdp_sock *xs, struct xdp_buff *xdp)
Björn Töpeld8179912019-11-01 12:03:46 +0100227{
Björn Töpele312b9e2019-12-19 07:10:02 +0100228 struct list_head *flush_list = this_cpu_ptr(&xskmap_flush_list);
Björn Töpeld8179912019-11-01 12:03:46 +0100229 int err;
230
Björn Töpel2b434702020-05-20 21:20:53 +0200231 err = xsk_rcv(xs, xdp, true);
Björn Töpeld8179912019-11-01 12:03:46 +0100232 if (err)
233 return err;
234
235 if (!xs->flush_node.prev)
236 list_add(&xs->flush_node, flush_list);
237
238 return 0;
239}
240
Björn Töpele312b9e2019-12-19 07:10:02 +0100241void __xsk_map_flush(void)
Björn Töpeld8179912019-11-01 12:03:46 +0100242{
Björn Töpele312b9e2019-12-19 07:10:02 +0100243 struct list_head *flush_list = this_cpu_ptr(&xskmap_flush_list);
Björn Töpeld8179912019-11-01 12:03:46 +0100244 struct xdp_sock *xs, *tmp;
245
246 list_for_each_entry_safe(xs, tmp, flush_list, flush_node) {
247 xsk_flush(xs);
248 __list_del_clearprev(&xs->flush_node);
249 }
250}
251
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200252void xsk_umem_complete_tx(struct xdp_umem *umem, u32 nb_entries)
253{
Magnus Karlsson59e35e52019-12-19 13:39:23 +0100254 xskq_prod_submit_n(umem->cq, nb_entries);
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200255}
256EXPORT_SYMBOL(xsk_umem_complete_tx);
257
258void xsk_umem_consume_tx_done(struct xdp_umem *umem)
259{
260 struct xdp_sock *xs;
261
262 rcu_read_lock();
Magnus Karlssone4e5aef2020-05-04 15:33:51 +0200263 list_for_each_entry_rcu(xs, &umem->xsk_tx_list, list) {
Magnus Karlsson30744a62020-02-10 16:27:12 +0100264 __xskq_cons_release(xs->tx);
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200265 xs->sk.sk_write_space(&xs->sk);
266 }
267 rcu_read_unlock();
268}
269EXPORT_SYMBOL(xsk_umem_consume_tx_done);
270
Maxim Mikityanskiy4bce4e52019-06-26 17:35:28 +0300271bool xsk_umem_consume_tx(struct xdp_umem *umem, struct xdp_desc *desc)
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200272{
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200273 struct xdp_sock *xs;
274
275 rcu_read_lock();
Magnus Karlssone4e5aef2020-05-04 15:33:51 +0200276 list_for_each_entry_rcu(xs, &umem->xsk_tx_list, list) {
Magnus Karlssonc5ed924b2019-12-19 13:39:26 +0100277 if (!xskq_cons_peek_desc(xs->tx, desc, umem))
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200278 continue;
279
Tobias Klauser0a058612020-04-22 01:29:27 +0200280 /* This is the backpressure mechanism for the Tx path.
Magnus Karlsson15d8c912019-12-19 13:39:30 +0100281 * Reserve space in the completion queue and only proceed
282 * if there is space in it. This avoids having to implement
283 * any buffering in the Tx path.
284 */
Magnus Karlsson59e35e52019-12-19 13:39:23 +0100285 if (xskq_prod_reserve_addr(umem->cq, desc->addr))
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200286 goto out;
287
Magnus Karlssonc5ed924b2019-12-19 13:39:26 +0100288 xskq_cons_release(xs->tx);
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200289 rcu_read_unlock();
290 return true;
291 }
292
293out:
294 rcu_read_unlock();
295 return false;
296}
297EXPORT_SYMBOL(xsk_umem_consume_tx);
298
Maxim Mikityanskiy06870682019-12-17 16:20:42 +0000299static int xsk_wakeup(struct xdp_sock *xs, u8 flags)
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200300{
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200301 struct net_device *dev = xs->dev;
Maxim Mikityanskiy06870682019-12-17 16:20:42 +0000302 int err;
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200303
Maxim Mikityanskiy06870682019-12-17 16:20:42 +0000304 rcu_read_lock();
305 err = dev->netdev_ops->ndo_xsk_wakeup(dev, xs->queue_id, flags);
306 rcu_read_unlock();
307
308 return err;
309}
310
311static int xsk_zc_xmit(struct xdp_sock *xs)
312{
313 return xsk_wakeup(xs, XDP_WAKEUP_TX);
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200314}
315
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200316static void xsk_destruct_skb(struct sk_buff *skb)
317{
Björn Töpelbbff2f32018-06-04 13:57:13 +0200318 u64 addr = (u64)(long)skb_shinfo(skb)->destructor_arg;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200319 struct xdp_sock *xs = xdp_sk(skb->sk);
Magnus Karlssona9744f72018-06-29 09:48:20 +0200320 unsigned long flags;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200321
Magnus Karlssona9744f72018-06-29 09:48:20 +0200322 spin_lock_irqsave(&xs->tx_completion_lock, flags);
Magnus Karlsson59e35e52019-12-19 13:39:23 +0100323 xskq_prod_submit_addr(xs->umem->cq, addr);
Magnus Karlssona9744f72018-06-29 09:48:20 +0200324 spin_unlock_irqrestore(&xs->tx_completion_lock, flags);
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200325
326 sock_wfree(skb);
327}
328
Magnus Karlssondf551052019-10-02 08:31:59 +0200329static int xsk_generic_xmit(struct sock *sk)
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200330{
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200331 struct xdp_sock *xs = xdp_sk(sk);
Magnus Karlssondf551052019-10-02 08:31:59 +0200332 u32 max_batch = TX_BATCH_SIZE;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200333 bool sent_frame = false;
334 struct xdp_desc desc;
335 struct sk_buff *skb;
336 int err = 0;
337
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200338 mutex_lock(&xs->mutex);
339
Ilya Maximets67571642019-07-04 17:25:03 +0300340 if (xs->queue_id >= xs->dev->real_num_tx_queues)
341 goto out;
342
Magnus Karlssonc5ed924b2019-12-19 13:39:26 +0100343 while (xskq_cons_peek_desc(xs->tx, &desc, xs->umem)) {
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200344 char *buffer;
Björn Töpelbbff2f32018-06-04 13:57:13 +0200345 u64 addr;
346 u32 len;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200347
348 if (max_batch-- == 0) {
349 err = -EAGAIN;
350 goto out;
351 }
352
Magnus Karlsson09210c42018-07-11 10:12:52 +0200353 len = desc.len;
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200354 skb = sock_alloc_send_skb(sk, len, 1, &err);
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200355 if (unlikely(!skb)) {
356 err = -EAGAIN;
357 goto out;
358 }
359
360 skb_put(skb, len);
Björn Töpelbbff2f32018-06-04 13:57:13 +0200361 addr = desc.addr;
Björn Töpel2b434702020-05-20 21:20:53 +0200362 buffer = xsk_buff_raw_get_data(xs->umem, addr);
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200363 err = skb_store_bits(skb, 0, buffer, len);
Tobias Klauser0a058612020-04-22 01:29:27 +0200364 /* This is the backpressure mechanism for the Tx path.
Magnus Karlsson15d8c912019-12-19 13:39:30 +0100365 * Reserve space in the completion queue and only proceed
366 * if there is space in it. This avoids having to implement
367 * any buffering in the Tx path.
368 */
Magnus Karlsson59e35e52019-12-19 13:39:23 +0100369 if (unlikely(err) || xskq_prod_reserve(xs->umem->cq)) {
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200370 kfree_skb(skb);
371 goto out;
372 }
373
374 skb->dev = xs->dev;
375 skb->priority = sk->sk_priority;
376 skb->mark = sk->sk_mark;
Kevin Laatzc05cd362019-08-27 02:25:22 +0000377 skb_shinfo(skb)->destructor_arg = (void *)(long)desc.addr;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200378 skb->destructor = xsk_destruct_skb;
379
380 err = dev_direct_xmit(skb, xs->queue_id);
Magnus Karlssonc5ed924b2019-12-19 13:39:26 +0100381 xskq_cons_release(xs->tx);
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200382 /* Ignore NET_XMIT_CN as packet might have been sent */
383 if (err == NET_XMIT_DROP || err == NETDEV_TX_BUSY) {
Magnus Karlssonfe588682018-06-29 09:48:18 +0200384 /* SKB completed but not sent */
385 err = -EBUSY;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200386 goto out;
387 }
388
389 sent_frame = true;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200390 }
391
392out:
393 if (sent_frame)
394 sk->sk_write_space(sk);
395
396 mutex_unlock(&xs->mutex);
397 return err;
398}
399
Magnus Karlssondf551052019-10-02 08:31:59 +0200400static int __xsk_sendmsg(struct sock *sk)
401{
402 struct xdp_sock *xs = xdp_sk(sk);
403
404 if (unlikely(!(xs->dev->flags & IFF_UP)))
405 return -ENETDOWN;
406 if (unlikely(!xs->tx))
407 return -ENOBUFS;
408
409 return xs->zc ? xsk_zc_xmit(xs) : xsk_generic_xmit(sk);
410}
411
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200412static int xsk_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
413{
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200414 bool need_wait = !(m->msg_flags & MSG_DONTWAIT);
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200415 struct sock *sk = sock->sk;
416 struct xdp_sock *xs = xdp_sk(sk);
417
Björn Töpel42fddcc2019-09-04 13:49:12 +0200418 if (unlikely(!xsk_is_bound(xs)))
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200419 return -ENXIO;
Magnus Karlssondf551052019-10-02 08:31:59 +0200420 if (unlikely(need_wait))
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200421 return -EOPNOTSUPP;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200422
Magnus Karlssondf551052019-10-02 08:31:59 +0200423 return __xsk_sendmsg(sk);
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200424}
425
Luc Van Oostenryck5d946c52019-11-20 01:10:42 +0100426static __poll_t xsk_poll(struct file *file, struct socket *sock,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700427 struct poll_table_struct *wait)
Björn Töpelc4971762018-05-02 13:01:27 +0200428{
Luc Van Oostenryck5d946c52019-11-20 01:10:42 +0100429 __poll_t mask = datagram_poll(file, sock, wait);
Magnus Karlssondf551052019-10-02 08:31:59 +0200430 struct sock *sk = sock->sk;
431 struct xdp_sock *xs = xdp_sk(sk);
Björn Töpel42fddcc2019-09-04 13:49:12 +0200432 struct xdp_umem *umem;
433
434 if (unlikely(!xsk_is_bound(xs)))
435 return mask;
436
Björn Töpel42fddcc2019-09-04 13:49:12 +0200437 umem = xs->umem;
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200438
Magnus Karlssondf551052019-10-02 08:31:59 +0200439 if (umem->need_wakeup) {
Maxim Mikityanskiy06870682019-12-17 16:20:42 +0000440 if (xs->zc)
441 xsk_wakeup(xs, umem->need_wakeup);
Magnus Karlssondf551052019-10-02 08:31:59 +0200442 else
443 /* Poll needs to drive Tx also in copy mode */
444 __xsk_sendmsg(sk);
445 }
Björn Töpelc4971762018-05-02 13:01:27 +0200446
Magnus Karlsson59e35e52019-12-19 13:39:23 +0100447 if (xs->rx && !xskq_prod_is_empty(xs->rx))
Luc Van Oostenryck5d946c52019-11-20 01:10:42 +0100448 mask |= EPOLLIN | EPOLLRDNORM;
Magnus Karlssonc5ed924b2019-12-19 13:39:26 +0100449 if (xs->tx && !xskq_cons_is_full(xs->tx))
Luc Van Oostenryck5d946c52019-11-20 01:10:42 +0100450 mask |= EPOLLOUT | EPOLLWRNORM;
Björn Töpelc4971762018-05-02 13:01:27 +0200451
452 return mask;
453}
454
Björn Töpelb9b6b682018-05-02 13:01:25 +0200455static int xsk_init_queue(u32 entries, struct xsk_queue **queue,
456 bool umem_queue)
Magnus Karlsson423f3832018-05-02 13:01:24 +0200457{
458 struct xsk_queue *q;
459
460 if (entries == 0 || *queue || !is_power_of_2(entries))
461 return -EINVAL;
462
Björn Töpelb9b6b682018-05-02 13:01:25 +0200463 q = xskq_create(entries, umem_queue);
Magnus Karlsson423f3832018-05-02 13:01:24 +0200464 if (!q)
465 return -ENOMEM;
466
Björn Töpel37b07692018-05-22 09:35:01 +0200467 /* Make sure queue is ready before it can be seen by others */
468 smp_wmb();
Björn Töpel94a99762019-09-04 13:49:10 +0200469 WRITE_ONCE(*queue, q);
Magnus Karlsson423f3832018-05-02 13:01:24 +0200470 return 0;
471}
472
Ilya Maximets455302d2019-06-28 11:04:07 +0300473static void xsk_unbind_dev(struct xdp_sock *xs)
474{
475 struct net_device *dev = xs->dev;
476
Björn Töpel42fddcc2019-09-04 13:49:12 +0200477 if (xs->state != XSK_BOUND)
Ilya Maximets455302d2019-06-28 11:04:07 +0300478 return;
Björn Töpel42fddcc2019-09-04 13:49:12 +0200479 WRITE_ONCE(xs->state, XSK_UNBOUND);
Ilya Maximets455302d2019-06-28 11:04:07 +0300480
481 /* Wait for driver to stop using the xdp socket. */
482 xdp_del_sk_umem(xs->umem, xs);
483 xs->dev = NULL;
484 synchronize_net();
485 dev_put(dev);
486}
487
Björn Töpel0402acd2019-08-15 11:30:13 +0200488static struct xsk_map *xsk_get_map_list_entry(struct xdp_sock *xs,
489 struct xdp_sock ***map_entry)
490{
491 struct xsk_map *map = NULL;
492 struct xsk_map_node *node;
493
494 *map_entry = NULL;
495
496 spin_lock_bh(&xs->map_list_lock);
497 node = list_first_entry_or_null(&xs->map_list, struct xsk_map_node,
498 node);
499 if (node) {
500 WARN_ON(xsk_map_inc(node->map));
501 map = node->map;
502 *map_entry = node->map_entry;
503 }
504 spin_unlock_bh(&xs->map_list_lock);
505 return map;
506}
507
508static void xsk_delete_from_maps(struct xdp_sock *xs)
509{
510 /* This function removes the current XDP socket from all the
511 * maps it resides in. We need to take extra care here, due to
512 * the two locks involved. Each map has a lock synchronizing
513 * updates to the entries, and each socket has a lock that
514 * synchronizes access to the list of maps (map_list). For
515 * deadlock avoidance the locks need to be taken in the order
516 * "map lock"->"socket map list lock". We start off by
517 * accessing the socket map list, and take a reference to the
518 * map to guarantee existence between the
519 * xsk_get_map_list_entry() and xsk_map_try_sock_delete()
520 * calls. Then we ask the map to remove the socket, which
521 * tries to remove the socket from the map. Note that there
522 * might be updates to the map between
523 * xsk_get_map_list_entry() and xsk_map_try_sock_delete().
524 */
525 struct xdp_sock **map_entry = NULL;
526 struct xsk_map *map;
527
528 while ((map = xsk_get_map_list_entry(xs, &map_entry))) {
529 xsk_map_try_sock_delete(map, xs, map_entry);
530 xsk_map_put(map);
531 }
532}
533
Björn Töpelc0c77d82018-05-02 13:01:23 +0200534static int xsk_release(struct socket *sock)
535{
536 struct sock *sk = sock->sk;
Magnus Karlsson965a9902018-05-02 13:01:26 +0200537 struct xdp_sock *xs = xdp_sk(sk);
Björn Töpelc0c77d82018-05-02 13:01:23 +0200538 struct net *net;
539
540 if (!sk)
541 return 0;
542
543 net = sock_net(sk);
544
Björn Töpel1d0dc062019-01-24 19:59:37 +0100545 mutex_lock(&net->xdp.lock);
546 sk_del_node_init_rcu(sk);
547 mutex_unlock(&net->xdp.lock);
548
Björn Töpelc0c77d82018-05-02 13:01:23 +0200549 local_bh_disable();
550 sock_prot_inuse_add(net, sk->sk_prot, -1);
551 local_bh_enable();
552
Björn Töpel0402acd2019-08-15 11:30:13 +0200553 xsk_delete_from_maps(xs);
Björn Töpel42fddcc2019-09-04 13:49:12 +0200554 mutex_lock(&xs->mutex);
Ilya Maximets455302d2019-06-28 11:04:07 +0300555 xsk_unbind_dev(xs);
Björn Töpel42fddcc2019-09-04 13:49:12 +0200556 mutex_unlock(&xs->mutex);
Magnus Karlsson965a9902018-05-02 13:01:26 +0200557
Björn Töpel541d7fd2018-10-05 13:25:15 +0200558 xskq_destroy(xs->rx);
559 xskq_destroy(xs->tx);
560
Björn Töpelc0c77d82018-05-02 13:01:23 +0200561 sock_orphan(sk);
562 sock->sk = NULL;
563
564 sk_refcnt_debug_release(sk);
565 sock_put(sk);
566
567 return 0;
568}
569
Magnus Karlsson965a9902018-05-02 13:01:26 +0200570static struct socket *xsk_lookup_xsk_from_fd(int fd)
571{
572 struct socket *sock;
573 int err;
574
575 sock = sockfd_lookup(fd, &err);
576 if (!sock)
577 return ERR_PTR(-ENOTSOCK);
578
579 if (sock->sk->sk_family != PF_XDP) {
580 sockfd_put(sock);
581 return ERR_PTR(-ENOPROTOOPT);
582 }
583
584 return sock;
585}
586
587static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
588{
589 struct sockaddr_xdp *sxdp = (struct sockaddr_xdp *)addr;
590 struct sock *sk = sock->sk;
Magnus Karlsson965a9902018-05-02 13:01:26 +0200591 struct xdp_sock *xs = xdp_sk(sk);
Björn Töpel959b71d2018-05-22 09:34:56 +0200592 struct net_device *dev;
Björn Töpel173d3ad2018-06-04 14:05:55 +0200593 u32 flags, qid;
Magnus Karlsson965a9902018-05-02 13:01:26 +0200594 int err = 0;
595
596 if (addr_len < sizeof(struct sockaddr_xdp))
597 return -EINVAL;
598 if (sxdp->sxdp_family != AF_XDP)
599 return -EINVAL;
600
Björn Töpelf54ba392019-03-08 08:57:26 +0100601 flags = sxdp->sxdp_flags;
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200602 if (flags & ~(XDP_SHARED_UMEM | XDP_COPY | XDP_ZEROCOPY |
603 XDP_USE_NEED_WAKEUP))
Björn Töpelf54ba392019-03-08 08:57:26 +0100604 return -EINVAL;
605
Ilya Maximets5464c3a2019-07-08 14:03:44 +0300606 rtnl_lock();
Magnus Karlsson965a9902018-05-02 13:01:26 +0200607 mutex_lock(&xs->mutex);
Ilya Maximets455302d2019-06-28 11:04:07 +0300608 if (xs->state != XSK_READY) {
Björn Töpel959b71d2018-05-22 09:34:56 +0200609 err = -EBUSY;
610 goto out_release;
611 }
612
Magnus Karlsson965a9902018-05-02 13:01:26 +0200613 dev = dev_get_by_index(sock_net(sk), sxdp->sxdp_ifindex);
614 if (!dev) {
615 err = -ENODEV;
616 goto out_release;
617 }
618
Magnus Karlssonf6145902018-05-02 13:01:32 +0200619 if (!xs->rx && !xs->tx) {
Magnus Karlsson965a9902018-05-02 13:01:26 +0200620 err = -EINVAL;
621 goto out_unlock;
622 }
623
Björn Töpel173d3ad2018-06-04 14:05:55 +0200624 qid = sxdp->sxdp_queue_id;
Björn Töpel173d3ad2018-06-04 14:05:55 +0200625
626 if (flags & XDP_SHARED_UMEM) {
Magnus Karlsson965a9902018-05-02 13:01:26 +0200627 struct xdp_sock *umem_xs;
628 struct socket *sock;
629
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200630 if ((flags & XDP_COPY) || (flags & XDP_ZEROCOPY) ||
631 (flags & XDP_USE_NEED_WAKEUP)) {
Björn Töpel173d3ad2018-06-04 14:05:55 +0200632 /* Cannot specify flags for shared sockets. */
633 err = -EINVAL;
634 goto out_unlock;
635 }
636
Magnus Karlsson965a9902018-05-02 13:01:26 +0200637 if (xs->umem) {
638 /* We have already our own. */
639 err = -EINVAL;
640 goto out_unlock;
641 }
642
643 sock = xsk_lookup_xsk_from_fd(sxdp->sxdp_shared_umem_fd);
644 if (IS_ERR(sock)) {
645 err = PTR_ERR(sock);
646 goto out_unlock;
647 }
648
649 umem_xs = xdp_sk(sock->sk);
Björn Töpel42fddcc2019-09-04 13:49:12 +0200650 if (!xsk_is_bound(umem_xs)) {
Magnus Karlsson965a9902018-05-02 13:01:26 +0200651 err = -EBADF;
652 sockfd_put(sock);
653 goto out_unlock;
Björn Töpel42fddcc2019-09-04 13:49:12 +0200654 }
655 if (umem_xs->dev != dev || umem_xs->queue_id != qid) {
Magnus Karlsson965a9902018-05-02 13:01:26 +0200656 err = -EINVAL;
657 sockfd_put(sock);
658 goto out_unlock;
659 }
660
661 xdp_get_umem(umem_xs->umem);
Björn Töpel9764f4b2019-09-04 13:49:11 +0200662 WRITE_ONCE(xs->umem, umem_xs->umem);
Magnus Karlsson965a9902018-05-02 13:01:26 +0200663 sockfd_put(sock);
664 } else if (!xs->umem || !xdp_umem_validate_queues(xs->umem)) {
665 err = -EINVAL;
666 goto out_unlock;
Björn Töpelc4971762018-05-02 13:01:27 +0200667 } else {
668 /* This xsk has its own umem. */
Björn Töpel173d3ad2018-06-04 14:05:55 +0200669 err = xdp_umem_assign_dev(xs->umem, dev, qid, flags);
670 if (err)
671 goto out_unlock;
Magnus Karlsson965a9902018-05-02 13:01:26 +0200672 }
673
Magnus Karlsson965a9902018-05-02 13:01:26 +0200674 xs->dev = dev;
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200675 xs->zc = xs->umem->zc;
676 xs->queue_id = qid;
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200677 xdp_add_sk_umem(xs->umem, xs);
Magnus Karlsson965a9902018-05-02 13:01:26 +0200678
679out_unlock:
Björn Töpel42fddcc2019-09-04 13:49:12 +0200680 if (err) {
Magnus Karlsson965a9902018-05-02 13:01:26 +0200681 dev_put(dev);
Björn Töpel42fddcc2019-09-04 13:49:12 +0200682 } else {
683 /* Matches smp_rmb() in bind() for shared umem
684 * sockets, and xsk_is_bound().
685 */
686 smp_wmb();
687 WRITE_ONCE(xs->state, XSK_BOUND);
688 }
Magnus Karlsson965a9902018-05-02 13:01:26 +0200689out_release:
690 mutex_unlock(&xs->mutex);
Ilya Maximets5464c3a2019-07-08 14:03:44 +0300691 rtnl_unlock();
Magnus Karlsson965a9902018-05-02 13:01:26 +0200692 return err;
693}
694
Kevin Laatzc05cd362019-08-27 02:25:22 +0000695struct xdp_umem_reg_v1 {
696 __u64 addr; /* Start of packet data area */
697 __u64 len; /* Length of packet data area */
698 __u32 chunk_size;
699 __u32 headroom;
700};
701
Björn Töpelc0c77d82018-05-02 13:01:23 +0200702static int xsk_setsockopt(struct socket *sock, int level, int optname,
703 char __user *optval, unsigned int optlen)
704{
705 struct sock *sk = sock->sk;
706 struct xdp_sock *xs = xdp_sk(sk);
707 int err;
708
709 if (level != SOL_XDP)
710 return -ENOPROTOOPT;
711
712 switch (optname) {
Björn Töpelb9b6b682018-05-02 13:01:25 +0200713 case XDP_RX_RING:
Magnus Karlssonf6145902018-05-02 13:01:32 +0200714 case XDP_TX_RING:
Björn Töpelb9b6b682018-05-02 13:01:25 +0200715 {
716 struct xsk_queue **q;
717 int entries;
718
719 if (optlen < sizeof(entries))
720 return -EINVAL;
721 if (copy_from_user(&entries, optval, sizeof(entries)))
722 return -EFAULT;
723
724 mutex_lock(&xs->mutex);
Ilya Maximets455302d2019-06-28 11:04:07 +0300725 if (xs->state != XSK_READY) {
726 mutex_unlock(&xs->mutex);
727 return -EBUSY;
728 }
Magnus Karlssonf6145902018-05-02 13:01:32 +0200729 q = (optname == XDP_TX_RING) ? &xs->tx : &xs->rx;
Björn Töpelb9b6b682018-05-02 13:01:25 +0200730 err = xsk_init_queue(entries, q, false);
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200731 if (!err && optname == XDP_TX_RING)
732 /* Tx needs to be explicitly woken up the first time */
733 xs->tx->ring->flags |= XDP_RING_NEED_WAKEUP;
Björn Töpelb9b6b682018-05-02 13:01:25 +0200734 mutex_unlock(&xs->mutex);
735 return err;
736 }
Björn Töpelc0c77d82018-05-02 13:01:23 +0200737 case XDP_UMEM_REG:
738 {
Kevin Laatzc05cd362019-08-27 02:25:22 +0000739 size_t mr_size = sizeof(struct xdp_umem_reg);
740 struct xdp_umem_reg mr = {};
Björn Töpelc0c77d82018-05-02 13:01:23 +0200741 struct xdp_umem *umem;
742
Kevin Laatzc05cd362019-08-27 02:25:22 +0000743 if (optlen < sizeof(struct xdp_umem_reg_v1))
744 return -EINVAL;
745 else if (optlen < sizeof(mr))
746 mr_size = sizeof(struct xdp_umem_reg_v1);
747
748 if (copy_from_user(&mr, optval, mr_size))
Björn Töpelc0c77d82018-05-02 13:01:23 +0200749 return -EFAULT;
750
751 mutex_lock(&xs->mutex);
Ilya Maximets455302d2019-06-28 11:04:07 +0300752 if (xs->state != XSK_READY || xs->umem) {
Björn Töpelc0c77d82018-05-02 13:01:23 +0200753 mutex_unlock(&xs->mutex);
Björn Töpela49049e2018-05-22 09:35:02 +0200754 return -EBUSY;
755 }
756
757 umem = xdp_umem_create(&mr);
758 if (IS_ERR(umem)) {
759 mutex_unlock(&xs->mutex);
760 return PTR_ERR(umem);
Björn Töpelc0c77d82018-05-02 13:01:23 +0200761 }
762
763 /* Make sure umem is ready before it can be seen by others */
764 smp_wmb();
Björn Töpel9764f4b2019-09-04 13:49:11 +0200765 WRITE_ONCE(xs->umem, umem);
Björn Töpelc0c77d82018-05-02 13:01:23 +0200766 mutex_unlock(&xs->mutex);
767 return 0;
768 }
Magnus Karlsson423f3832018-05-02 13:01:24 +0200769 case XDP_UMEM_FILL_RING:
Magnus Karlssonfe230832018-05-02 13:01:31 +0200770 case XDP_UMEM_COMPLETION_RING:
Magnus Karlsson423f3832018-05-02 13:01:24 +0200771 {
772 struct xsk_queue **q;
773 int entries;
774
Magnus Karlsson423f3832018-05-02 13:01:24 +0200775 if (copy_from_user(&entries, optval, sizeof(entries)))
776 return -EFAULT;
777
778 mutex_lock(&xs->mutex);
Ilya Maximets455302d2019-06-28 11:04:07 +0300779 if (xs->state != XSK_READY) {
780 mutex_unlock(&xs->mutex);
781 return -EBUSY;
782 }
Björn Töpela49049e2018-05-22 09:35:02 +0200783 if (!xs->umem) {
784 mutex_unlock(&xs->mutex);
785 return -EINVAL;
786 }
787
Magnus Karlssonfe230832018-05-02 13:01:31 +0200788 q = (optname == XDP_UMEM_FILL_RING) ? &xs->umem->fq :
789 &xs->umem->cq;
Björn Töpelb9b6b682018-05-02 13:01:25 +0200790 err = xsk_init_queue(entries, q, true);
Björn Töpel2b434702020-05-20 21:20:53 +0200791 if (optname == XDP_UMEM_FILL_RING)
792 xp_set_fq(xs->umem->pool, *q);
Magnus Karlsson423f3832018-05-02 13:01:24 +0200793 mutex_unlock(&xs->mutex);
794 return err;
795 }
Björn Töpelc0c77d82018-05-02 13:01:23 +0200796 default:
797 break;
798 }
799
800 return -ENOPROTOOPT;
801}
802
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200803static void xsk_enter_rxtx_offsets(struct xdp_ring_offset_v1 *ring)
804{
805 ring->producer = offsetof(struct xdp_rxtx_ring, ptrs.producer);
806 ring->consumer = offsetof(struct xdp_rxtx_ring, ptrs.consumer);
807 ring->desc = offsetof(struct xdp_rxtx_ring, desc);
808}
809
810static void xsk_enter_umem_offsets(struct xdp_ring_offset_v1 *ring)
811{
812 ring->producer = offsetof(struct xdp_umem_ring, ptrs.producer);
813 ring->consumer = offsetof(struct xdp_umem_ring, ptrs.consumer);
814 ring->desc = offsetof(struct xdp_umem_ring, desc);
815}
816
Magnus Karlssonaf75d9e2018-05-02 13:01:35 +0200817static int xsk_getsockopt(struct socket *sock, int level, int optname,
818 char __user *optval, int __user *optlen)
819{
820 struct sock *sk = sock->sk;
821 struct xdp_sock *xs = xdp_sk(sk);
822 int len;
823
824 if (level != SOL_XDP)
825 return -ENOPROTOOPT;
826
827 if (get_user(len, optlen))
828 return -EFAULT;
829 if (len < 0)
830 return -EINVAL;
831
832 switch (optname) {
833 case XDP_STATISTICS:
834 {
835 struct xdp_statistics stats;
836
837 if (len < sizeof(stats))
838 return -EINVAL;
839
840 mutex_lock(&xs->mutex);
841 stats.rx_dropped = xs->rx_dropped;
842 stats.rx_invalid_descs = xskq_nb_invalid_descs(xs->rx);
843 stats.tx_invalid_descs = xskq_nb_invalid_descs(xs->tx);
844 mutex_unlock(&xs->mutex);
845
846 if (copy_to_user(optval, &stats, sizeof(stats)))
847 return -EFAULT;
848 if (put_user(sizeof(stats), optlen))
849 return -EFAULT;
850
851 return 0;
852 }
Björn Töpelb3a9e0b2018-05-22 09:34:59 +0200853 case XDP_MMAP_OFFSETS:
854 {
855 struct xdp_mmap_offsets off;
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200856 struct xdp_mmap_offsets_v1 off_v1;
857 bool flags_supported = true;
858 void *to_copy;
Björn Töpelb3a9e0b2018-05-22 09:34:59 +0200859
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200860 if (len < sizeof(off_v1))
Björn Töpelb3a9e0b2018-05-22 09:34:59 +0200861 return -EINVAL;
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200862 else if (len < sizeof(off))
863 flags_supported = false;
Björn Töpelb3a9e0b2018-05-22 09:34:59 +0200864
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200865 if (flags_supported) {
866 /* xdp_ring_offset is identical to xdp_ring_offset_v1
867 * except for the flags field added to the end.
868 */
869 xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1 *)
870 &off.rx);
871 xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1 *)
872 &off.tx);
873 xsk_enter_umem_offsets((struct xdp_ring_offset_v1 *)
874 &off.fr);
875 xsk_enter_umem_offsets((struct xdp_ring_offset_v1 *)
876 &off.cr);
877 off.rx.flags = offsetof(struct xdp_rxtx_ring,
878 ptrs.flags);
879 off.tx.flags = offsetof(struct xdp_rxtx_ring,
880 ptrs.flags);
881 off.fr.flags = offsetof(struct xdp_umem_ring,
882 ptrs.flags);
883 off.cr.flags = offsetof(struct xdp_umem_ring,
884 ptrs.flags);
Björn Töpelb3a9e0b2018-05-22 09:34:59 +0200885
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200886 len = sizeof(off);
887 to_copy = &off;
888 } else {
889 xsk_enter_rxtx_offsets(&off_v1.rx);
890 xsk_enter_rxtx_offsets(&off_v1.tx);
891 xsk_enter_umem_offsets(&off_v1.fr);
892 xsk_enter_umem_offsets(&off_v1.cr);
Björn Töpelb3a9e0b2018-05-22 09:34:59 +0200893
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200894 len = sizeof(off_v1);
895 to_copy = &off_v1;
896 }
897
898 if (copy_to_user(optval, to_copy, len))
Björn Töpelb3a9e0b2018-05-22 09:34:59 +0200899 return -EFAULT;
900 if (put_user(len, optlen))
901 return -EFAULT;
902
903 return 0;
904 }
Maxim Mikityanskiy2640d3c2019-06-26 17:35:25 +0300905 case XDP_OPTIONS:
906 {
907 struct xdp_options opts = {};
908
909 if (len < sizeof(opts))
910 return -EINVAL;
911
912 mutex_lock(&xs->mutex);
913 if (xs->zc)
914 opts.flags |= XDP_OPTIONS_ZEROCOPY;
915 mutex_unlock(&xs->mutex);
916
917 len = sizeof(opts);
918 if (copy_to_user(optval, &opts, len))
919 return -EFAULT;
920 if (put_user(len, optlen))
921 return -EFAULT;
922
923 return 0;
924 }
Magnus Karlssonaf75d9e2018-05-02 13:01:35 +0200925 default:
926 break;
927 }
928
929 return -EOPNOTSUPP;
930}
931
Magnus Karlsson423f3832018-05-02 13:01:24 +0200932static int xsk_mmap(struct file *file, struct socket *sock,
933 struct vm_area_struct *vma)
934{
Geert Uytterhoevena5a16e42018-06-07 15:37:34 +0200935 loff_t offset = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
Magnus Karlsson423f3832018-05-02 13:01:24 +0200936 unsigned long size = vma->vm_end - vma->vm_start;
937 struct xdp_sock *xs = xdp_sk(sock->sk);
938 struct xsk_queue *q = NULL;
Björn Töpel37b07692018-05-22 09:35:01 +0200939 struct xdp_umem *umem;
Magnus Karlsson423f3832018-05-02 13:01:24 +0200940 unsigned long pfn;
941 struct page *qpg;
942
Björn Töpel42fddcc2019-09-04 13:49:12 +0200943 if (READ_ONCE(xs->state) != XSK_READY)
Ilya Maximets455302d2019-06-28 11:04:07 +0300944 return -EBUSY;
945
Björn Töpelb9b6b682018-05-02 13:01:25 +0200946 if (offset == XDP_PGOFF_RX_RING) {
Björn Töpel37b07692018-05-22 09:35:01 +0200947 q = READ_ONCE(xs->rx);
Magnus Karlssonf6145902018-05-02 13:01:32 +0200948 } else if (offset == XDP_PGOFF_TX_RING) {
Björn Töpel37b07692018-05-22 09:35:01 +0200949 q = READ_ONCE(xs->tx);
Björn Töpelb9b6b682018-05-02 13:01:25 +0200950 } else {
Björn Töpel37b07692018-05-22 09:35:01 +0200951 umem = READ_ONCE(xs->umem);
952 if (!umem)
Björn Töpelb9b6b682018-05-02 13:01:25 +0200953 return -EINVAL;
Magnus Karlsson423f3832018-05-02 13:01:24 +0200954
Magnus Karlssone6762c82019-02-08 14:13:50 +0100955 /* Matches the smp_wmb() in XDP_UMEM_REG */
956 smp_rmb();
Björn Töpelb9b6b682018-05-02 13:01:25 +0200957 if (offset == XDP_UMEM_PGOFF_FILL_RING)
Björn Töpel37b07692018-05-22 09:35:01 +0200958 q = READ_ONCE(umem->fq);
Magnus Karlssonfe230832018-05-02 13:01:31 +0200959 else if (offset == XDP_UMEM_PGOFF_COMPLETION_RING)
Björn Töpel37b07692018-05-22 09:35:01 +0200960 q = READ_ONCE(umem->cq);
Björn Töpelb9b6b682018-05-02 13:01:25 +0200961 }
Magnus Karlsson423f3832018-05-02 13:01:24 +0200962
963 if (!q)
964 return -EINVAL;
965
Magnus Karlssone6762c82019-02-08 14:13:50 +0100966 /* Matches the smp_wmb() in xsk_init_queue */
967 smp_rmb();
Magnus Karlsson423f3832018-05-02 13:01:24 +0200968 qpg = virt_to_head_page(q->ring);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -0700969 if (size > page_size(qpg))
Magnus Karlsson423f3832018-05-02 13:01:24 +0200970 return -EINVAL;
971
972 pfn = virt_to_phys(q->ring) >> PAGE_SHIFT;
973 return remap_pfn_range(vma, vma->vm_start, pfn,
974 size, vma->vm_page_prot);
975}
976
Ilya Maximets455302d2019-06-28 11:04:07 +0300977static int xsk_notifier(struct notifier_block *this,
978 unsigned long msg, void *ptr)
979{
980 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
981 struct net *net = dev_net(dev);
982 struct sock *sk;
983
984 switch (msg) {
985 case NETDEV_UNREGISTER:
986 mutex_lock(&net->xdp.lock);
987 sk_for_each(sk, &net->xdp.list) {
988 struct xdp_sock *xs = xdp_sk(sk);
989
990 mutex_lock(&xs->mutex);
991 if (xs->dev == dev) {
992 sk->sk_err = ENETDOWN;
993 if (!sock_flag(sk, SOCK_DEAD))
994 sk->sk_error_report(sk);
995
996 xsk_unbind_dev(xs);
997
998 /* Clear device references in umem. */
999 xdp_umem_clear_dev(xs->umem);
1000 }
1001 mutex_unlock(&xs->mutex);
1002 }
1003 mutex_unlock(&net->xdp.lock);
1004 break;
1005 }
1006 return NOTIFY_DONE;
1007}
1008
Björn Töpelc0c77d82018-05-02 13:01:23 +02001009static struct proto xsk_proto = {
1010 .name = "XDP",
1011 .owner = THIS_MODULE,
1012 .obj_size = sizeof(struct xdp_sock),
1013};
1014
1015static const struct proto_ops xsk_proto_ops = {
Björn Töpelc2f43742018-05-18 14:00:24 +02001016 .family = PF_XDP,
1017 .owner = THIS_MODULE,
1018 .release = xsk_release,
1019 .bind = xsk_bind,
1020 .connect = sock_no_connect,
1021 .socketpair = sock_no_socketpair,
1022 .accept = sock_no_accept,
1023 .getname = sock_no_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001024 .poll = xsk_poll,
Björn Töpelc2f43742018-05-18 14:00:24 +02001025 .ioctl = sock_no_ioctl,
1026 .listen = sock_no_listen,
1027 .shutdown = sock_no_shutdown,
1028 .setsockopt = xsk_setsockopt,
1029 .getsockopt = xsk_getsockopt,
1030 .sendmsg = xsk_sendmsg,
1031 .recvmsg = sock_no_recvmsg,
1032 .mmap = xsk_mmap,
1033 .sendpage = sock_no_sendpage,
Björn Töpelc0c77d82018-05-02 13:01:23 +02001034};
1035
Björn Töpel11fe9262019-02-21 13:07:38 +01001036static void xsk_destruct(struct sock *sk)
1037{
1038 struct xdp_sock *xs = xdp_sk(sk);
1039
1040 if (!sock_flag(sk, SOCK_DEAD))
1041 return;
1042
1043 xdp_put_umem(xs->umem);
1044
1045 sk_refcnt_debug_dec(sk);
1046}
1047
Björn Töpelc0c77d82018-05-02 13:01:23 +02001048static int xsk_create(struct net *net, struct socket *sock, int protocol,
1049 int kern)
1050{
1051 struct sock *sk;
1052 struct xdp_sock *xs;
1053
1054 if (!ns_capable(net->user_ns, CAP_NET_RAW))
1055 return -EPERM;
1056 if (sock->type != SOCK_RAW)
1057 return -ESOCKTNOSUPPORT;
1058
1059 if (protocol)
1060 return -EPROTONOSUPPORT;
1061
1062 sock->state = SS_UNCONNECTED;
1063
1064 sk = sk_alloc(net, PF_XDP, GFP_KERNEL, &xsk_proto, kern);
1065 if (!sk)
1066 return -ENOBUFS;
1067
1068 sock->ops = &xsk_proto_ops;
1069
1070 sock_init_data(sock, sk);
1071
1072 sk->sk_family = PF_XDP;
1073
Björn Töpel11fe9262019-02-21 13:07:38 +01001074 sk->sk_destruct = xsk_destruct;
1075 sk_refcnt_debug_inc(sk);
1076
Björn Töpelcee27162018-10-08 19:40:16 +02001077 sock_set_flag(sk, SOCK_RCU_FREE);
1078
Björn Töpelc0c77d82018-05-02 13:01:23 +02001079 xs = xdp_sk(sk);
Ilya Maximets455302d2019-06-28 11:04:07 +03001080 xs->state = XSK_READY;
Björn Töpelc0c77d82018-05-02 13:01:23 +02001081 mutex_init(&xs->mutex);
Ilya Maximetsbf0bdd12019-07-03 15:09:16 +03001082 spin_lock_init(&xs->rx_lock);
Magnus Karlssona9744f72018-06-29 09:48:20 +02001083 spin_lock_init(&xs->tx_completion_lock);
Björn Töpelc0c77d82018-05-02 13:01:23 +02001084
Björn Töpel0402acd2019-08-15 11:30:13 +02001085 INIT_LIST_HEAD(&xs->map_list);
1086 spin_lock_init(&xs->map_list_lock);
1087
Björn Töpel1d0dc062019-01-24 19:59:37 +01001088 mutex_lock(&net->xdp.lock);
1089 sk_add_node_rcu(sk, &net->xdp.list);
1090 mutex_unlock(&net->xdp.lock);
1091
Björn Töpelc0c77d82018-05-02 13:01:23 +02001092 local_bh_disable();
1093 sock_prot_inuse_add(net, &xsk_proto, 1);
1094 local_bh_enable();
1095
1096 return 0;
1097}
1098
1099static const struct net_proto_family xsk_family_ops = {
1100 .family = PF_XDP,
1101 .create = xsk_create,
1102 .owner = THIS_MODULE,
1103};
1104
Ilya Maximets455302d2019-06-28 11:04:07 +03001105static struct notifier_block xsk_netdev_notifier = {
1106 .notifier_call = xsk_notifier,
1107};
1108
Björn Töpel1d0dc062019-01-24 19:59:37 +01001109static int __net_init xsk_net_init(struct net *net)
1110{
1111 mutex_init(&net->xdp.lock);
1112 INIT_HLIST_HEAD(&net->xdp.list);
1113 return 0;
1114}
1115
1116static void __net_exit xsk_net_exit(struct net *net)
1117{
1118 WARN_ON_ONCE(!hlist_empty(&net->xdp.list));
1119}
1120
1121static struct pernet_operations xsk_net_ops = {
1122 .init = xsk_net_init,
1123 .exit = xsk_net_exit,
1124};
1125
Björn Töpelc0c77d82018-05-02 13:01:23 +02001126static int __init xsk_init(void)
1127{
Björn Töpele312b9e2019-12-19 07:10:02 +01001128 int err, cpu;
Björn Töpelc0c77d82018-05-02 13:01:23 +02001129
1130 err = proto_register(&xsk_proto, 0 /* no slab */);
1131 if (err)
1132 goto out;
1133
1134 err = sock_register(&xsk_family_ops);
1135 if (err)
1136 goto out_proto;
1137
Björn Töpel1d0dc062019-01-24 19:59:37 +01001138 err = register_pernet_subsys(&xsk_net_ops);
1139 if (err)
1140 goto out_sk;
Ilya Maximets455302d2019-06-28 11:04:07 +03001141
1142 err = register_netdevice_notifier(&xsk_netdev_notifier);
1143 if (err)
1144 goto out_pernet;
1145
Björn Töpele312b9e2019-12-19 07:10:02 +01001146 for_each_possible_cpu(cpu)
1147 INIT_LIST_HEAD(&per_cpu(xskmap_flush_list, cpu));
Björn Töpelc0c77d82018-05-02 13:01:23 +02001148 return 0;
1149
Ilya Maximets455302d2019-06-28 11:04:07 +03001150out_pernet:
1151 unregister_pernet_subsys(&xsk_net_ops);
Björn Töpel1d0dc062019-01-24 19:59:37 +01001152out_sk:
1153 sock_unregister(PF_XDP);
Björn Töpelc0c77d82018-05-02 13:01:23 +02001154out_proto:
1155 proto_unregister(&xsk_proto);
1156out:
1157 return err;
1158}
1159
1160fs_initcall(xsk_init);