blob: e28c6825e08971cca233d4a339eb540b89b54b77 [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öpela0731952020-11-30 19:52:00 +010026#include <net/busy_poll.h>
Björn Töpelb9b6b682018-05-02 13:01:25 +020027#include <net/xdp.h>
Björn Töpelc0c77d82018-05-02 13:01:23 +020028
Magnus Karlsson423f3832018-05-02 13:01:24 +020029#include "xsk_queue.h"
Björn Töpelc0c77d82018-05-02 13:01:23 +020030#include "xdp_umem.h"
Björn Töpela36b38aa2019-01-24 19:59:39 +010031#include "xsk.h"
Björn Töpelc0c77d82018-05-02 13:01:23 +020032
Magnus Karlsson35fcde72018-05-02 13:01:34 +020033#define TX_BATCH_SIZE 16
34
Björn Töpele312b9e2019-12-19 07:10:02 +010035static DEFINE_PER_CPU(struct list_head, xskmap_flush_list);
36
Magnus Karlssonc4655762020-08-28 10:26:16 +020037void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool)
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020038{
Magnus Karlssonc2d3d6a2020-08-28 10:26:19 +020039 if (pool->cached_need_wakeup & XDP_WAKEUP_RX)
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020040 return;
41
Magnus Karlsson7361f9c2020-08-28 10:26:18 +020042 pool->fq->ring->flags |= XDP_RING_NEED_WAKEUP;
Magnus Karlssonc2d3d6a2020-08-28 10:26:19 +020043 pool->cached_need_wakeup |= XDP_WAKEUP_RX;
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020044}
45EXPORT_SYMBOL(xsk_set_rx_need_wakeup);
46
Magnus Karlssonc4655762020-08-28 10:26:16 +020047void xsk_set_tx_need_wakeup(struct xsk_buff_pool *pool)
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020048{
49 struct xdp_sock *xs;
50
Magnus Karlssonc2d3d6a2020-08-28 10:26:19 +020051 if (pool->cached_need_wakeup & XDP_WAKEUP_TX)
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020052 return;
53
54 rcu_read_lock();
Magnus Karlssona5aa8e52020-08-28 10:26:20 +020055 list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) {
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020056 xs->tx->ring->flags |= XDP_RING_NEED_WAKEUP;
57 }
58 rcu_read_unlock();
59
Magnus Karlssonc2d3d6a2020-08-28 10:26:19 +020060 pool->cached_need_wakeup |= XDP_WAKEUP_TX;
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020061}
62EXPORT_SYMBOL(xsk_set_tx_need_wakeup);
63
Magnus Karlssonc4655762020-08-28 10:26:16 +020064void xsk_clear_rx_need_wakeup(struct xsk_buff_pool *pool)
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020065{
Magnus Karlssonc2d3d6a2020-08-28 10:26:19 +020066 if (!(pool->cached_need_wakeup & XDP_WAKEUP_RX))
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020067 return;
68
Magnus Karlsson7361f9c2020-08-28 10:26:18 +020069 pool->fq->ring->flags &= ~XDP_RING_NEED_WAKEUP;
Magnus Karlssonc2d3d6a2020-08-28 10:26:19 +020070 pool->cached_need_wakeup &= ~XDP_WAKEUP_RX;
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020071}
72EXPORT_SYMBOL(xsk_clear_rx_need_wakeup);
73
Magnus Karlssonc4655762020-08-28 10:26:16 +020074void xsk_clear_tx_need_wakeup(struct xsk_buff_pool *pool)
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020075{
76 struct xdp_sock *xs;
77
Magnus Karlssonc2d3d6a2020-08-28 10:26:19 +020078 if (!(pool->cached_need_wakeup & XDP_WAKEUP_TX))
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020079 return;
80
81 rcu_read_lock();
Magnus Karlssona5aa8e52020-08-28 10:26:20 +020082 list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) {
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020083 xs->tx->ring->flags &= ~XDP_RING_NEED_WAKEUP;
84 }
85 rcu_read_unlock();
86
Magnus Karlssonc2d3d6a2020-08-28 10:26:19 +020087 pool->cached_need_wakeup &= ~XDP_WAKEUP_TX;
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020088}
89EXPORT_SYMBOL(xsk_clear_tx_need_wakeup);
90
Magnus Karlssonc4655762020-08-28 10:26:16 +020091bool xsk_uses_need_wakeup(struct xsk_buff_pool *pool)
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020092{
Magnus Karlssonc2d3d6a2020-08-28 10:26:19 +020093 return pool->uses_need_wakeup;
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020094}
Magnus Karlssonc4655762020-08-28 10:26:16 +020095EXPORT_SYMBOL(xsk_uses_need_wakeup);
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020096
Magnus Karlsson1c1efc22020-08-28 10:26:17 +020097struct xsk_buff_pool *xsk_get_pool_from_qid(struct net_device *dev,
98 u16 queue_id)
99{
100 if (queue_id < dev->real_num_rx_queues)
101 return dev->_rx[queue_id].pool;
102 if (queue_id < dev->real_num_tx_queues)
103 return dev->_tx[queue_id].pool;
104
105 return NULL;
106}
107EXPORT_SYMBOL(xsk_get_pool_from_qid);
108
109void xsk_clear_pool_at_qid(struct net_device *dev, u16 queue_id)
110{
111 if (queue_id < dev->real_num_rx_queues)
112 dev->_rx[queue_id].pool = NULL;
113 if (queue_id < dev->real_num_tx_queues)
114 dev->_tx[queue_id].pool = NULL;
115}
116
117/* The buffer pool is stored both in the _rx struct and the _tx struct as we do
118 * not know if the device has more tx queues than rx, or the opposite.
119 * This might also change during run time.
120 */
121int xsk_reg_pool_at_qid(struct net_device *dev, struct xsk_buff_pool *pool,
122 u16 queue_id)
123{
124 if (queue_id >= max_t(unsigned int,
125 dev->real_num_rx_queues,
126 dev->real_num_tx_queues))
127 return -EINVAL;
128
129 if (queue_id < dev->real_num_rx_queues)
130 dev->_rx[queue_id].pool = pool;
131 if (queue_id < dev->real_num_tx_queues)
132 dev->_tx[queue_id].pool = pool;
133
134 return 0;
135}
136
Björn Töpel26062b12020-05-20 21:21:02 +0200137void xp_release(struct xdp_buff_xsk *xskb)
138{
139 xskb->pool->free_heads[xskb->pool->free_heads_cnt++] = xskb;
140}
141
142static u64 xp_get_handle(struct xdp_buff_xsk *xskb)
143{
144 u64 offset = xskb->xdp.data - xskb->xdp.data_hard_start;
145
146 offset += xskb->pool->headroom;
147 if (!xskb->pool->unaligned)
148 return xskb->orig_addr + offset;
149 return xskb->orig_addr + (offset << XSK_UNALIGNED_BUF_OFFSET_SHIFT);
150}
151
Björn Töpel2b434702020-05-20 21:20:53 +0200152static int __xsk_rcv_zc(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
Kevin Laatzc05cd362019-08-27 02:25:22 +0000153{
Björn Töpel2b434702020-05-20 21:20:53 +0200154 struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
155 u64 addr;
156 int err;
Kevin Laatzc05cd362019-08-27 02:25:22 +0000157
Björn Töpel2b434702020-05-20 21:20:53 +0200158 addr = xp_get_handle(xskb);
159 err = xskq_prod_reserve_desc(xs->rx, addr, len);
160 if (err) {
Ciara Loftus8aa5a332020-07-08 07:28:33 +0000161 xs->rx_queue_full++;
Björn Töpel2b434702020-05-20 21:20:53 +0200162 return err;
163 }
Kevin Laatzc05cd362019-08-27 02:25:22 +0000164
Björn Töpel2b434702020-05-20 21:20:53 +0200165 xp_release(xskb);
166 return 0;
167}
Kevin Laatzc05cd362019-08-27 02:25:22 +0000168
Björn Töpel2b434702020-05-20 21:20:53 +0200169static void xsk_copy_xdp(struct xdp_buff *to, struct xdp_buff *from, u32 len)
170{
171 void *from_buf, *to_buf;
172 u32 metalen;
173
174 if (unlikely(xdp_data_meta_unsupported(from))) {
175 from_buf = from->data;
176 to_buf = to->data;
177 metalen = 0;
178 } else {
179 from_buf = from->data_meta;
180 metalen = from->data - from->data_meta;
181 to_buf = to->data - metalen;
Kevin Laatzc05cd362019-08-27 02:25:22 +0000182 }
183
184 memcpy(to_buf, from_buf, len + metalen);
185}
186
Björn Töpel2b434702020-05-20 21:20:53 +0200187static int __xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len,
188 bool explicit_free)
Björn Töpel173d3ad2018-06-04 14:05:55 +0200189{
Björn Töpel2b434702020-05-20 21:20:53 +0200190 struct xdp_buff *xsk_xdp;
Björn Töpel4e64c832018-06-04 13:57:11 +0200191 int err;
Björn Töpelc4971762018-05-02 13:01:27 +0200192
Magnus Karlssonc4655762020-08-28 10:26:16 +0200193 if (len > xsk_pool_get_rx_frame_size(xs->pool)) {
Björn Töpela509a952018-06-04 13:57:12 +0200194 xs->rx_dropped++;
Björn Töpelc4971762018-05-02 13:01:27 +0200195 return -ENOSPC;
Björn Töpela509a952018-06-04 13:57:12 +0200196 }
Björn Töpelc4971762018-05-02 13:01:27 +0200197
Magnus Karlssonc4655762020-08-28 10:26:16 +0200198 xsk_xdp = xsk_buff_alloc(xs->pool);
Björn Töpel2b434702020-05-20 21:20:53 +0200199 if (!xsk_xdp) {
Björn Töpela509a952018-06-04 13:57:12 +0200200 xs->rx_dropped++;
Björn Töpel2b434702020-05-20 21:20:53 +0200201 return -ENOSPC;
202 }
Björn Töpelc4971762018-05-02 13:01:27 +0200203
Björn Töpel2b434702020-05-20 21:20:53 +0200204 xsk_copy_xdp(xsk_xdp, xdp, len);
205 err = __xsk_rcv_zc(xs, xsk_xdp, len);
206 if (err) {
207 xsk_buff_free(xsk_xdp);
208 return err;
209 }
210 if (explicit_free)
211 xdp_return_buff(xdp);
212 return 0;
Björn Töpelc4971762018-05-02 13:01:27 +0200213}
214
Björn Töpel42fddcc2019-09-04 13:49:12 +0200215static bool xsk_is_bound(struct xdp_sock *xs)
216{
217 if (READ_ONCE(xs->state) == XSK_BOUND) {
218 /* Matches smp_wmb() in bind(). */
219 smp_rmb();
220 return true;
221 }
222 return false;
223}
224
Björn Töpel2b434702020-05-20 21:20:53 +0200225static int xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp,
226 bool explicit_free)
Björn Töpelc4971762018-05-02 13:01:27 +0200227{
Björn Töpel173d3ad2018-06-04 14:05:55 +0200228 u32 len;
Björn Töpelc4971762018-05-02 13:01:27 +0200229
Björn Töpel42fddcc2019-09-04 13:49:12 +0200230 if (!xsk_is_bound(xs))
231 return -EINVAL;
232
Björn Töpel173d3ad2018-06-04 14:05:55 +0200233 if (xs->dev != xdp->rxq->dev || xs->queue_id != xdp->rxq->queue_index)
234 return -EINVAL;
Björn Töpelc4971762018-05-02 13:01:27 +0200235
Björn Töpelb02e5a02020-11-30 19:52:01 +0100236 sk_mark_napi_id_once_xdp(&xs->sk, xdp);
Björn Töpel173d3ad2018-06-04 14:05:55 +0200237 len = xdp->data_end - xdp->data;
238
Björn Töpel08078922020-05-20 21:21:00 +0200239 return xdp->rxq->mem.type == MEM_TYPE_XSK_BUFF_POOL ?
Björn Töpel2b434702020-05-20 21:20:53 +0200240 __xsk_rcv_zc(xs, xdp, len) :
241 __xsk_rcv(xs, xdp, len, explicit_free);
Björn Töpelc4971762018-05-02 13:01:27 +0200242}
243
Björn Töpeld8179912019-11-01 12:03:46 +0100244static void xsk_flush(struct xdp_sock *xs)
Björn Töpelc4971762018-05-02 13:01:27 +0200245{
Magnus Karlsson59e35e52019-12-19 13:39:23 +0100246 xskq_prod_submit(xs->rx);
Magnus Karlsson7361f9c2020-08-28 10:26:18 +0200247 __xskq_cons_release(xs->pool->fq);
Björn Töpel43a825a2020-01-20 10:29:17 +0100248 sock_def_readable(&xs->sk);
Björn Töpelc4971762018-05-02 13:01:27 +0200249}
250
251int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
252{
253 int err;
254
Ilya Maximetsbf0bdd12019-07-03 15:09:16 +0300255 spin_lock_bh(&xs->rx_lock);
Björn Töpel2b434702020-05-20 21:20:53 +0200256 err = xsk_rcv(xs, xdp, false);
257 xsk_flush(xs);
Ilya Maximetsbf0bdd12019-07-03 15:09:16 +0300258 spin_unlock_bh(&xs->rx_lock);
Björn Töpelc4971762018-05-02 13:01:27 +0200259 return err;
260}
261
Björn Töpele312b9e2019-12-19 07:10:02 +0100262int __xsk_map_redirect(struct xdp_sock *xs, struct xdp_buff *xdp)
Björn Töpeld8179912019-11-01 12:03:46 +0100263{
Björn Töpele312b9e2019-12-19 07:10:02 +0100264 struct list_head *flush_list = this_cpu_ptr(&xskmap_flush_list);
Björn Töpeld8179912019-11-01 12:03:46 +0100265 int err;
266
Björn Töpel2b434702020-05-20 21:20:53 +0200267 err = xsk_rcv(xs, xdp, true);
Björn Töpeld8179912019-11-01 12:03:46 +0100268 if (err)
269 return err;
270
271 if (!xs->flush_node.prev)
272 list_add(&xs->flush_node, flush_list);
273
274 return 0;
275}
276
Björn Töpele312b9e2019-12-19 07:10:02 +0100277void __xsk_map_flush(void)
Björn Töpeld8179912019-11-01 12:03:46 +0100278{
Björn Töpele312b9e2019-12-19 07:10:02 +0100279 struct list_head *flush_list = this_cpu_ptr(&xskmap_flush_list);
Björn Töpeld8179912019-11-01 12:03:46 +0100280 struct xdp_sock *xs, *tmp;
281
282 list_for_each_entry_safe(xs, tmp, flush_list, flush_node) {
283 xsk_flush(xs);
284 __list_del_clearprev(&xs->flush_node);
285 }
286}
287
Magnus Karlssonc4655762020-08-28 10:26:16 +0200288void xsk_tx_completed(struct xsk_buff_pool *pool, u32 nb_entries)
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200289{
Magnus Karlsson7361f9c2020-08-28 10:26:18 +0200290 xskq_prod_submit_n(pool->cq, nb_entries);
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200291}
Magnus Karlssonc4655762020-08-28 10:26:16 +0200292EXPORT_SYMBOL(xsk_tx_completed);
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200293
Magnus Karlssonc4655762020-08-28 10:26:16 +0200294void xsk_tx_release(struct xsk_buff_pool *pool)
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200295{
296 struct xdp_sock *xs;
297
298 rcu_read_lock();
Magnus Karlssona5aa8e52020-08-28 10:26:20 +0200299 list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) {
Magnus Karlsson30744a62020-02-10 16:27:12 +0100300 __xskq_cons_release(xs->tx);
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200301 xs->sk.sk_write_space(&xs->sk);
302 }
303 rcu_read_unlock();
304}
Magnus Karlssonc4655762020-08-28 10:26:16 +0200305EXPORT_SYMBOL(xsk_tx_release);
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200306
Magnus Karlssonc4655762020-08-28 10:26:16 +0200307bool xsk_tx_peek_desc(struct xsk_buff_pool *pool, struct xdp_desc *desc)
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200308{
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200309 struct xdp_sock *xs;
310
311 rcu_read_lock();
Magnus Karlssona5aa8e52020-08-28 10:26:20 +0200312 list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) {
Magnus Karlsson1c1efc22020-08-28 10:26:17 +0200313 if (!xskq_cons_peek_desc(xs->tx, desc, pool)) {
Ciara Loftus8aa5a332020-07-08 07:28:33 +0000314 xs->tx->queue_empty_descs++;
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200315 continue;
Ciara Loftus8aa5a332020-07-08 07:28:33 +0000316 }
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200317
Tobias Klauser0a058612020-04-22 01:29:27 +0200318 /* This is the backpressure mechanism for the Tx path.
Magnus Karlsson15d8c912019-12-19 13:39:30 +0100319 * Reserve space in the completion queue and only proceed
320 * if there is space in it. This avoids having to implement
321 * any buffering in the Tx path.
322 */
Magnus Karlsson7361f9c2020-08-28 10:26:18 +0200323 if (xskq_prod_reserve_addr(pool->cq, desc->addr))
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200324 goto out;
325
Magnus Karlssonc5ed924b2019-12-19 13:39:26 +0100326 xskq_cons_release(xs->tx);
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200327 rcu_read_unlock();
328 return true;
329 }
330
331out:
332 rcu_read_unlock();
333 return false;
334}
Magnus Karlssonc4655762020-08-28 10:26:16 +0200335EXPORT_SYMBOL(xsk_tx_peek_desc);
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200336
Magnus Karlsson9349eb32020-11-16 12:12:46 +0100337static u32 xsk_tx_peek_release_fallback(struct xsk_buff_pool *pool, struct xdp_desc *descs,
338 u32 max_entries)
339{
340 u32 nb_pkts = 0;
341
342 while (nb_pkts < max_entries && xsk_tx_peek_desc(pool, &descs[nb_pkts]))
343 nb_pkts++;
344
345 xsk_tx_release(pool);
346 return nb_pkts;
347}
348
349u32 xsk_tx_peek_release_desc_batch(struct xsk_buff_pool *pool, struct xdp_desc *descs,
350 u32 max_entries)
351{
352 struct xdp_sock *xs;
353 u32 nb_pkts;
354
355 rcu_read_lock();
356 if (!list_is_singular(&pool->xsk_tx_list)) {
357 /* Fallback to the non-batched version */
358 rcu_read_unlock();
359 return xsk_tx_peek_release_fallback(pool, descs, max_entries);
360 }
361
362 xs = list_first_or_null_rcu(&pool->xsk_tx_list, struct xdp_sock, tx_list);
363 if (!xs) {
364 nb_pkts = 0;
365 goto out;
366 }
367
368 nb_pkts = xskq_cons_peek_desc_batch(xs->tx, descs, pool, max_entries);
369 if (!nb_pkts) {
370 xs->tx->queue_empty_descs++;
371 goto out;
372 }
373
374 /* This is the backpressure mechanism for the Tx path. Try to
375 * reserve space in the completion queue for all packets, but
376 * if there are fewer slots available, just process that many
377 * packets. This avoids having to implement any buffering in
378 * the Tx path.
379 */
380 nb_pkts = xskq_prod_reserve_addr_batch(pool->cq, descs, nb_pkts);
381 if (!nb_pkts)
382 goto out;
383
384 xskq_cons_release_n(xs->tx, nb_pkts);
385 __xskq_cons_release(xs->tx);
386 xs->sk.sk_write_space(&xs->sk);
387
388out:
389 rcu_read_unlock();
390 return nb_pkts;
391}
392EXPORT_SYMBOL(xsk_tx_peek_release_desc_batch);
393
Maxim Mikityanskiy06870682019-12-17 16:20:42 +0000394static int xsk_wakeup(struct xdp_sock *xs, u8 flags)
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200395{
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200396 struct net_device *dev = xs->dev;
Maxim Mikityanskiy06870682019-12-17 16:20:42 +0000397 int err;
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200398
Maxim Mikityanskiy06870682019-12-17 16:20:42 +0000399 rcu_read_lock();
400 err = dev->netdev_ops->ndo_xsk_wakeup(dev, xs->queue_id, flags);
401 rcu_read_unlock();
402
403 return err;
404}
405
406static int xsk_zc_xmit(struct xdp_sock *xs)
407{
408 return xsk_wakeup(xs, XDP_WAKEUP_TX);
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200409}
410
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200411static void xsk_destruct_skb(struct sk_buff *skb)
412{
Björn Töpelbbff2f32018-06-04 13:57:13 +0200413 u64 addr = (u64)(long)skb_shinfo(skb)->destructor_arg;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200414 struct xdp_sock *xs = xdp_sk(skb->sk);
Magnus Karlssona9744f72018-06-29 09:48:20 +0200415 unsigned long flags;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200416
Magnus Karlssona9744f72018-06-29 09:48:20 +0200417 spin_lock_irqsave(&xs->tx_completion_lock, flags);
Magnus Karlsson7361f9c2020-08-28 10:26:18 +0200418 xskq_prod_submit_addr(xs->pool->cq, addr);
Magnus Karlssona9744f72018-06-29 09:48:20 +0200419 spin_unlock_irqrestore(&xs->tx_completion_lock, flags);
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200420
421 sock_wfree(skb);
422}
423
Magnus Karlssondf551052019-10-02 08:31:59 +0200424static int xsk_generic_xmit(struct sock *sk)
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200425{
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200426 struct xdp_sock *xs = xdp_sk(sk);
Magnus Karlssondf551052019-10-02 08:31:59 +0200427 u32 max_batch = TX_BATCH_SIZE;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200428 bool sent_frame = false;
429 struct xdp_desc desc;
430 struct sk_buff *skb;
431 int err = 0;
432
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200433 mutex_lock(&xs->mutex);
434
Ilya Maximets67571642019-07-04 17:25:03 +0300435 if (xs->queue_id >= xs->dev->real_num_tx_queues)
436 goto out;
437
Magnus Karlsson1c1efc22020-08-28 10:26:17 +0200438 while (xskq_cons_peek_desc(xs->tx, &desc, xs->pool)) {
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200439 char *buffer;
Björn Töpelbbff2f32018-06-04 13:57:13 +0200440 u64 addr;
441 u32 len;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200442
443 if (max_batch-- == 0) {
444 err = -EAGAIN;
445 goto out;
446 }
447
Magnus Karlsson09210c42018-07-11 10:12:52 +0200448 len = desc.len;
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200449 skb = sock_alloc_send_skb(sk, len, 1, &err);
Li RongQingaa2cad02020-06-11 13:11:06 +0800450 if (unlikely(!skb))
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200451 goto out;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200452
453 skb_put(skb, len);
Björn Töpelbbff2f32018-06-04 13:57:13 +0200454 addr = desc.addr;
Magnus Karlssonc4655762020-08-28 10:26:16 +0200455 buffer = xsk_buff_raw_get_data(xs->pool, addr);
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200456 err = skb_store_bits(skb, 0, buffer, len);
Tobias Klauser0a058612020-04-22 01:29:27 +0200457 /* This is the backpressure mechanism for the Tx path.
Magnus Karlsson15d8c912019-12-19 13:39:30 +0100458 * Reserve space in the completion queue and only proceed
459 * if there is space in it. This avoids having to implement
460 * any buffering in the Tx path.
461 */
Magnus Karlsson7361f9c2020-08-28 10:26:18 +0200462 if (unlikely(err) || xskq_prod_reserve(xs->pool->cq)) {
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200463 kfree_skb(skb);
464 goto out;
465 }
466
467 skb->dev = xs->dev;
468 skb->priority = sk->sk_priority;
469 skb->mark = sk->sk_mark;
Kevin Laatzc05cd362019-08-27 02:25:22 +0000470 skb_shinfo(skb)->destructor_arg = (void *)(long)desc.addr;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200471 skb->destructor = xsk_destruct_skb;
472
Björn Töpel36ccdf82020-11-23 18:56:00 +0100473 err = __dev_direct_xmit(skb, xs->queue_id);
Magnus Karlsson642e4502020-09-16 14:00:25 +0200474 if (err == NETDEV_TX_BUSY) {
475 /* Tell user-space to retry the send */
476 skb->destructor = sock_wfree;
477 /* Free skb without triggering the perf drop trace */
478 consume_skb(skb);
479 err = -EAGAIN;
480 goto out;
481 }
482
Magnus Karlssonc5ed924b2019-12-19 13:39:26 +0100483 xskq_cons_release(xs->tx);
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200484 /* Ignore NET_XMIT_CN as packet might have been sent */
Magnus Karlsson642e4502020-09-16 14:00:25 +0200485 if (err == NET_XMIT_DROP) {
Magnus Karlssonfe588682018-06-29 09:48:18 +0200486 /* SKB completed but not sent */
487 err = -EBUSY;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200488 goto out;
489 }
490
491 sent_frame = true;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200492 }
493
Ciara Loftus8aa5a332020-07-08 07:28:33 +0000494 xs->tx->queue_empty_descs++;
495
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200496out:
497 if (sent_frame)
498 sk->sk_write_space(sk);
499
500 mutex_unlock(&xs->mutex);
501 return err;
502}
503
Magnus Karlssondf551052019-10-02 08:31:59 +0200504static int __xsk_sendmsg(struct sock *sk)
505{
506 struct xdp_sock *xs = xdp_sk(sk);
507
508 if (unlikely(!(xs->dev->flags & IFF_UP)))
509 return -ENETDOWN;
510 if (unlikely(!xs->tx))
511 return -ENOBUFS;
512
513 return xs->zc ? xsk_zc_xmit(xs) : xsk_generic_xmit(sk);
514}
515
Björn Töpela0731952020-11-30 19:52:00 +0100516static bool xsk_no_wakeup(struct sock *sk)
517{
518#ifdef CONFIG_NET_RX_BUSY_POLL
519 /* Prefer busy-polling, skip the wakeup. */
520 return READ_ONCE(sk->sk_prefer_busy_poll) && READ_ONCE(sk->sk_ll_usec) &&
521 READ_ONCE(sk->sk_napi_id) >= MIN_NAPI_ID;
522#else
523 return false;
524#endif
525}
526
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200527static int xsk_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
528{
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200529 bool need_wait = !(m->msg_flags & MSG_DONTWAIT);
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200530 struct sock *sk = sock->sk;
531 struct xdp_sock *xs = xdp_sk(sk);
Björn Töpele3920812020-11-30 19:51:59 +0100532 struct xsk_buff_pool *pool;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200533
Björn Töpel42fddcc2019-09-04 13:49:12 +0200534 if (unlikely(!xsk_is_bound(xs)))
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200535 return -ENXIO;
Magnus Karlssondf551052019-10-02 08:31:59 +0200536 if (unlikely(need_wait))
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200537 return -EOPNOTSUPP;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200538
Björn Töpela0731952020-11-30 19:52:00 +0100539 if (sk_can_busy_loop(sk))
540 sk_busy_loop(sk, 1); /* only support non-blocking sockets */
541
542 if (xsk_no_wakeup(sk))
543 return 0;
544
Björn Töpele3920812020-11-30 19:51:59 +0100545 pool = xs->pool;
546 if (pool->cached_need_wakeup & XDP_WAKEUP_TX)
547 return __xsk_sendmsg(sk);
548 return 0;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200549}
550
Björn Töpel45a86682020-11-30 19:51:58 +0100551static int xsk_recvmsg(struct socket *sock, struct msghdr *m, size_t len, int flags)
552{
553 bool need_wait = !(flags & MSG_DONTWAIT);
554 struct sock *sk = sock->sk;
555 struct xdp_sock *xs = xdp_sk(sk);
556
Björn Töpel3546b9b2020-12-07 09:20:08 +0100557 if (unlikely(!xsk_is_bound(xs)))
558 return -ENXIO;
Björn Töpel45a86682020-11-30 19:51:58 +0100559 if (unlikely(!(xs->dev->flags & IFF_UP)))
560 return -ENETDOWN;
561 if (unlikely(!xs->rx))
562 return -ENOBUFS;
Björn Töpel45a86682020-11-30 19:51:58 +0100563 if (unlikely(need_wait))
564 return -EOPNOTSUPP;
565
Björn Töpela0731952020-11-30 19:52:00 +0100566 if (sk_can_busy_loop(sk))
567 sk_busy_loop(sk, 1); /* only support non-blocking sockets */
568
569 if (xsk_no_wakeup(sk))
570 return 0;
571
Björn Töpel45a86682020-11-30 19:51:58 +0100572 if (xs->pool->cached_need_wakeup & XDP_WAKEUP_RX && xs->zc)
573 return xsk_wakeup(xs, XDP_WAKEUP_RX);
574 return 0;
Björn Töpelc4971762018-05-02 13:01:27 +0200575}
576
Luc Van Oostenryck5d946c52019-11-20 01:10:42 +0100577static __poll_t xsk_poll(struct file *file, struct socket *sock,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700578 struct poll_table_struct *wait)
Björn Töpelc4971762018-05-02 13:01:27 +0200579{
Luc Van Oostenryck5d946c52019-11-20 01:10:42 +0100580 __poll_t mask = datagram_poll(file, sock, wait);
Magnus Karlssondf551052019-10-02 08:31:59 +0200581 struct sock *sk = sock->sk;
582 struct xdp_sock *xs = xdp_sk(sk);
Magnus Karlssonc2d3d6a2020-08-28 10:26:19 +0200583 struct xsk_buff_pool *pool;
Björn Töpel42fddcc2019-09-04 13:49:12 +0200584
585 if (unlikely(!xsk_is_bound(xs)))
586 return mask;
587
Magnus Karlssonc2d3d6a2020-08-28 10:26:19 +0200588 pool = xs->pool;
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200589
Magnus Karlssonc2d3d6a2020-08-28 10:26:19 +0200590 if (pool->cached_need_wakeup) {
Maxim Mikityanskiy06870682019-12-17 16:20:42 +0000591 if (xs->zc)
Magnus Karlssonc2d3d6a2020-08-28 10:26:19 +0200592 xsk_wakeup(xs, pool->cached_need_wakeup);
Magnus Karlssondf551052019-10-02 08:31:59 +0200593 else
594 /* Poll needs to drive Tx also in copy mode */
595 __xsk_sendmsg(sk);
596 }
Björn Töpelc4971762018-05-02 13:01:27 +0200597
Magnus Karlsson59e35e52019-12-19 13:39:23 +0100598 if (xs->rx && !xskq_prod_is_empty(xs->rx))
Luc Van Oostenryck5d946c52019-11-20 01:10:42 +0100599 mask |= EPOLLIN | EPOLLRDNORM;
Magnus Karlssonc5ed924b2019-12-19 13:39:26 +0100600 if (xs->tx && !xskq_cons_is_full(xs->tx))
Luc Van Oostenryck5d946c52019-11-20 01:10:42 +0100601 mask |= EPOLLOUT | EPOLLWRNORM;
Björn Töpelc4971762018-05-02 13:01:27 +0200602
603 return mask;
604}
605
Björn Töpelb9b6b682018-05-02 13:01:25 +0200606static int xsk_init_queue(u32 entries, struct xsk_queue **queue,
607 bool umem_queue)
Magnus Karlsson423f3832018-05-02 13:01:24 +0200608{
609 struct xsk_queue *q;
610
611 if (entries == 0 || *queue || !is_power_of_2(entries))
612 return -EINVAL;
613
Björn Töpelb9b6b682018-05-02 13:01:25 +0200614 q = xskq_create(entries, umem_queue);
Magnus Karlsson423f3832018-05-02 13:01:24 +0200615 if (!q)
616 return -ENOMEM;
617
Björn Töpel37b07692018-05-22 09:35:01 +0200618 /* Make sure queue is ready before it can be seen by others */
619 smp_wmb();
Björn Töpel94a99762019-09-04 13:49:10 +0200620 WRITE_ONCE(*queue, q);
Magnus Karlsson423f3832018-05-02 13:01:24 +0200621 return 0;
622}
623
Ilya Maximets455302d2019-06-28 11:04:07 +0300624static void xsk_unbind_dev(struct xdp_sock *xs)
625{
626 struct net_device *dev = xs->dev;
627
Björn Töpel42fddcc2019-09-04 13:49:12 +0200628 if (xs->state != XSK_BOUND)
Ilya Maximets455302d2019-06-28 11:04:07 +0300629 return;
Björn Töpel42fddcc2019-09-04 13:49:12 +0200630 WRITE_ONCE(xs->state, XSK_UNBOUND);
Ilya Maximets455302d2019-06-28 11:04:07 +0300631
632 /* Wait for driver to stop using the xdp socket. */
Magnus Karlssona5aa8e52020-08-28 10:26:20 +0200633 xp_del_xsk(xs->pool, xs);
Ilya Maximets455302d2019-06-28 11:04:07 +0300634 xs->dev = NULL;
635 synchronize_net();
636 dev_put(dev);
637}
638
Björn Töpel0402acd2019-08-15 11:30:13 +0200639static struct xsk_map *xsk_get_map_list_entry(struct xdp_sock *xs,
640 struct xdp_sock ***map_entry)
641{
642 struct xsk_map *map = NULL;
643 struct xsk_map_node *node;
644
645 *map_entry = NULL;
646
647 spin_lock_bh(&xs->map_list_lock);
648 node = list_first_entry_or_null(&xs->map_list, struct xsk_map_node,
649 node);
650 if (node) {
Zhu Yanjunbb1b25c2020-11-26 23:03:18 +0800651 bpf_map_inc(&node->map->map);
Björn Töpel0402acd2019-08-15 11:30:13 +0200652 map = node->map;
653 *map_entry = node->map_entry;
654 }
655 spin_unlock_bh(&xs->map_list_lock);
656 return map;
657}
658
659static void xsk_delete_from_maps(struct xdp_sock *xs)
660{
661 /* This function removes the current XDP socket from all the
662 * maps it resides in. We need to take extra care here, due to
663 * the two locks involved. Each map has a lock synchronizing
664 * updates to the entries, and each socket has a lock that
665 * synchronizes access to the list of maps (map_list). For
666 * deadlock avoidance the locks need to be taken in the order
667 * "map lock"->"socket map list lock". We start off by
668 * accessing the socket map list, and take a reference to the
669 * map to guarantee existence between the
670 * xsk_get_map_list_entry() and xsk_map_try_sock_delete()
671 * calls. Then we ask the map to remove the socket, which
672 * tries to remove the socket from the map. Note that there
673 * might be updates to the map between
674 * xsk_get_map_list_entry() and xsk_map_try_sock_delete().
675 */
676 struct xdp_sock **map_entry = NULL;
677 struct xsk_map *map;
678
679 while ((map = xsk_get_map_list_entry(xs, &map_entry))) {
680 xsk_map_try_sock_delete(map, xs, map_entry);
Zhu Yanjunbb1b25c2020-11-26 23:03:18 +0800681 bpf_map_put(&map->map);
Björn Töpel0402acd2019-08-15 11:30:13 +0200682 }
683}
684
Björn Töpelc0c77d82018-05-02 13:01:23 +0200685static int xsk_release(struct socket *sock)
686{
687 struct sock *sk = sock->sk;
Magnus Karlsson965a9902018-05-02 13:01:26 +0200688 struct xdp_sock *xs = xdp_sk(sk);
Björn Töpelc0c77d82018-05-02 13:01:23 +0200689 struct net *net;
690
691 if (!sk)
692 return 0;
693
694 net = sock_net(sk);
695
Björn Töpel1d0dc062019-01-24 19:59:37 +0100696 mutex_lock(&net->xdp.lock);
697 sk_del_node_init_rcu(sk);
698 mutex_unlock(&net->xdp.lock);
699
Björn Töpelc0c77d82018-05-02 13:01:23 +0200700 local_bh_disable();
701 sock_prot_inuse_add(net, sk->sk_prot, -1);
702 local_bh_enable();
703
Björn Töpel0402acd2019-08-15 11:30:13 +0200704 xsk_delete_from_maps(xs);
Björn Töpel42fddcc2019-09-04 13:49:12 +0200705 mutex_lock(&xs->mutex);
Ilya Maximets455302d2019-06-28 11:04:07 +0300706 xsk_unbind_dev(xs);
Björn Töpel42fddcc2019-09-04 13:49:12 +0200707 mutex_unlock(&xs->mutex);
Magnus Karlsson965a9902018-05-02 13:01:26 +0200708
Björn Töpel541d7fd2018-10-05 13:25:15 +0200709 xskq_destroy(xs->rx);
710 xskq_destroy(xs->tx);
Magnus Karlsson7361f9c2020-08-28 10:26:18 +0200711 xskq_destroy(xs->fq_tmp);
712 xskq_destroy(xs->cq_tmp);
Björn Töpel541d7fd2018-10-05 13:25:15 +0200713
Björn Töpelc0c77d82018-05-02 13:01:23 +0200714 sock_orphan(sk);
715 sock->sk = NULL;
716
717 sk_refcnt_debug_release(sk);
718 sock_put(sk);
719
720 return 0;
721}
722
Magnus Karlsson965a9902018-05-02 13:01:26 +0200723static struct socket *xsk_lookup_xsk_from_fd(int fd)
724{
725 struct socket *sock;
726 int err;
727
728 sock = sockfd_lookup(fd, &err);
729 if (!sock)
730 return ERR_PTR(-ENOTSOCK);
731
732 if (sock->sk->sk_family != PF_XDP) {
733 sockfd_put(sock);
734 return ERR_PTR(-ENOPROTOOPT);
735 }
736
737 return sock;
738}
739
Magnus Karlsson7361f9c2020-08-28 10:26:18 +0200740static bool xsk_validate_queues(struct xdp_sock *xs)
741{
742 return xs->fq_tmp && xs->cq_tmp;
743}
744
Magnus Karlsson965a9902018-05-02 13:01:26 +0200745static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
746{
747 struct sockaddr_xdp *sxdp = (struct sockaddr_xdp *)addr;
748 struct sock *sk = sock->sk;
Magnus Karlsson965a9902018-05-02 13:01:26 +0200749 struct xdp_sock *xs = xdp_sk(sk);
Björn Töpel959b71d2018-05-22 09:34:56 +0200750 struct net_device *dev;
Björn Töpel173d3ad2018-06-04 14:05:55 +0200751 u32 flags, qid;
Magnus Karlsson965a9902018-05-02 13:01:26 +0200752 int err = 0;
753
754 if (addr_len < sizeof(struct sockaddr_xdp))
755 return -EINVAL;
756 if (sxdp->sxdp_family != AF_XDP)
757 return -EINVAL;
758
Björn Töpelf54ba392019-03-08 08:57:26 +0100759 flags = sxdp->sxdp_flags;
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200760 if (flags & ~(XDP_SHARED_UMEM | XDP_COPY | XDP_ZEROCOPY |
761 XDP_USE_NEED_WAKEUP))
Björn Töpelf54ba392019-03-08 08:57:26 +0100762 return -EINVAL;
763
Ilya Maximets5464c3a2019-07-08 14:03:44 +0300764 rtnl_lock();
Magnus Karlsson965a9902018-05-02 13:01:26 +0200765 mutex_lock(&xs->mutex);
Ilya Maximets455302d2019-06-28 11:04:07 +0300766 if (xs->state != XSK_READY) {
Björn Töpel959b71d2018-05-22 09:34:56 +0200767 err = -EBUSY;
768 goto out_release;
769 }
770
Magnus Karlsson965a9902018-05-02 13:01:26 +0200771 dev = dev_get_by_index(sock_net(sk), sxdp->sxdp_ifindex);
772 if (!dev) {
773 err = -ENODEV;
774 goto out_release;
775 }
776
Magnus Karlssonf6145902018-05-02 13:01:32 +0200777 if (!xs->rx && !xs->tx) {
Magnus Karlsson965a9902018-05-02 13:01:26 +0200778 err = -EINVAL;
779 goto out_unlock;
780 }
781
Björn Töpel173d3ad2018-06-04 14:05:55 +0200782 qid = sxdp->sxdp_queue_id;
Björn Töpel173d3ad2018-06-04 14:05:55 +0200783
784 if (flags & XDP_SHARED_UMEM) {
Magnus Karlsson965a9902018-05-02 13:01:26 +0200785 struct xdp_sock *umem_xs;
786 struct socket *sock;
787
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200788 if ((flags & XDP_COPY) || (flags & XDP_ZEROCOPY) ||
789 (flags & XDP_USE_NEED_WAKEUP)) {
Björn Töpel173d3ad2018-06-04 14:05:55 +0200790 /* Cannot specify flags for shared sockets. */
791 err = -EINVAL;
792 goto out_unlock;
793 }
794
Magnus Karlsson965a9902018-05-02 13:01:26 +0200795 if (xs->umem) {
796 /* We have already our own. */
797 err = -EINVAL;
798 goto out_unlock;
799 }
800
801 sock = xsk_lookup_xsk_from_fd(sxdp->sxdp_shared_umem_fd);
802 if (IS_ERR(sock)) {
803 err = PTR_ERR(sock);
804 goto out_unlock;
805 }
806
807 umem_xs = xdp_sk(sock->sk);
Björn Töpel42fddcc2019-09-04 13:49:12 +0200808 if (!xsk_is_bound(umem_xs)) {
Magnus Karlsson965a9902018-05-02 13:01:26 +0200809 err = -EBADF;
810 sockfd_put(sock);
811 goto out_unlock;
Björn Töpel42fddcc2019-09-04 13:49:12 +0200812 }
Magnus Karlsson965a9902018-05-02 13:01:26 +0200813
Magnus Karlssona1132432020-08-28 10:26:26 +0200814 if (umem_xs->queue_id != qid || umem_xs->dev != dev) {
815 /* Share the umem with another socket on another qid
816 * and/or device.
817 */
Magnus Karlssonb5aea282020-08-28 10:26:25 +0200818 xs->pool = xp_create_and_assign_umem(xs,
819 umem_xs->umem);
820 if (!xs->pool) {
Magnus Karlsson1fd17c8c2020-09-26 11:26:13 +0200821 err = -ENOMEM;
Magnus Karlssonb5aea282020-08-28 10:26:25 +0200822 sockfd_put(sock);
823 goto out_unlock;
824 }
825
826 err = xp_assign_dev_shared(xs->pool, umem_xs->umem,
827 dev, qid);
828 if (err) {
829 xp_destroy(xs->pool);
Magnus Karlsson83cf5c62020-09-02 09:36:04 +0200830 xs->pool = NULL;
Magnus Karlssonb5aea282020-08-28 10:26:25 +0200831 sockfd_put(sock);
832 goto out_unlock;
833 }
834 } else {
835 /* Share the buffer pool with the other socket. */
836 if (xs->fq_tmp || xs->cq_tmp) {
837 /* Do not allow setting your own fq or cq. */
838 err = -EINVAL;
839 sockfd_put(sock);
840 goto out_unlock;
841 }
842
843 xp_get_pool(umem_xs->pool);
844 xs->pool = umem_xs->pool;
845 }
846
Magnus Karlsson965a9902018-05-02 13:01:26 +0200847 xdp_get_umem(umem_xs->umem);
Björn Töpel9764f4b2019-09-04 13:49:11 +0200848 WRITE_ONCE(xs->umem, umem_xs->umem);
Magnus Karlsson965a9902018-05-02 13:01:26 +0200849 sockfd_put(sock);
Magnus Karlsson7361f9c2020-08-28 10:26:18 +0200850 } else if (!xs->umem || !xsk_validate_queues(xs)) {
Magnus Karlsson965a9902018-05-02 13:01:26 +0200851 err = -EINVAL;
852 goto out_unlock;
Björn Töpelc4971762018-05-02 13:01:27 +0200853 } else {
854 /* This xsk has its own umem. */
Magnus Karlsson1c1efc22020-08-28 10:26:17 +0200855 xs->pool = xp_create_and_assign_umem(xs, xs->umem);
856 if (!xs->pool) {
857 err = -ENOMEM;
Björn Töpel173d3ad2018-06-04 14:05:55 +0200858 goto out_unlock;
Magnus Karlsson1c1efc22020-08-28 10:26:17 +0200859 }
860
861 err = xp_assign_dev(xs->pool, dev, qid, flags);
862 if (err) {
863 xp_destroy(xs->pool);
864 xs->pool = NULL;
Magnus Karlsson1c1efc22020-08-28 10:26:17 +0200865 goto out_unlock;
866 }
Magnus Karlsson965a9902018-05-02 13:01:26 +0200867 }
868
Magnus Karlsson965a9902018-05-02 13:01:26 +0200869 xs->dev = dev;
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200870 xs->zc = xs->umem->zc;
871 xs->queue_id = qid;
Magnus Karlssona5aa8e52020-08-28 10:26:20 +0200872 xp_add_xsk(xs->pool, xs);
Magnus Karlsson965a9902018-05-02 13:01:26 +0200873
874out_unlock:
Björn Töpel42fddcc2019-09-04 13:49:12 +0200875 if (err) {
Magnus Karlsson965a9902018-05-02 13:01:26 +0200876 dev_put(dev);
Björn Töpel42fddcc2019-09-04 13:49:12 +0200877 } else {
878 /* Matches smp_rmb() in bind() for shared umem
879 * sockets, and xsk_is_bound().
880 */
881 smp_wmb();
882 WRITE_ONCE(xs->state, XSK_BOUND);
883 }
Magnus Karlsson965a9902018-05-02 13:01:26 +0200884out_release:
885 mutex_unlock(&xs->mutex);
Ilya Maximets5464c3a2019-07-08 14:03:44 +0300886 rtnl_unlock();
Magnus Karlsson965a9902018-05-02 13:01:26 +0200887 return err;
888}
889
Kevin Laatzc05cd362019-08-27 02:25:22 +0000890struct xdp_umem_reg_v1 {
891 __u64 addr; /* Start of packet data area */
892 __u64 len; /* Length of packet data area */
893 __u32 chunk_size;
894 __u32 headroom;
895};
896
Björn Töpelc0c77d82018-05-02 13:01:23 +0200897static int xsk_setsockopt(struct socket *sock, int level, int optname,
Christoph Hellwiga7b75c52020-07-23 08:09:07 +0200898 sockptr_t optval, unsigned int optlen)
Björn Töpelc0c77d82018-05-02 13:01:23 +0200899{
900 struct sock *sk = sock->sk;
901 struct xdp_sock *xs = xdp_sk(sk);
902 int err;
903
904 if (level != SOL_XDP)
905 return -ENOPROTOOPT;
906
907 switch (optname) {
Björn Töpelb9b6b682018-05-02 13:01:25 +0200908 case XDP_RX_RING:
Magnus Karlssonf6145902018-05-02 13:01:32 +0200909 case XDP_TX_RING:
Björn Töpelb9b6b682018-05-02 13:01:25 +0200910 {
911 struct xsk_queue **q;
912 int entries;
913
914 if (optlen < sizeof(entries))
915 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +0200916 if (copy_from_sockptr(&entries, optval, sizeof(entries)))
Björn Töpelb9b6b682018-05-02 13:01:25 +0200917 return -EFAULT;
918
919 mutex_lock(&xs->mutex);
Ilya Maximets455302d2019-06-28 11:04:07 +0300920 if (xs->state != XSK_READY) {
921 mutex_unlock(&xs->mutex);
922 return -EBUSY;
923 }
Magnus Karlssonf6145902018-05-02 13:01:32 +0200924 q = (optname == XDP_TX_RING) ? &xs->tx : &xs->rx;
Björn Töpelb9b6b682018-05-02 13:01:25 +0200925 err = xsk_init_queue(entries, q, false);
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200926 if (!err && optname == XDP_TX_RING)
927 /* Tx needs to be explicitly woken up the first time */
928 xs->tx->ring->flags |= XDP_RING_NEED_WAKEUP;
Björn Töpelb9b6b682018-05-02 13:01:25 +0200929 mutex_unlock(&xs->mutex);
930 return err;
931 }
Björn Töpelc0c77d82018-05-02 13:01:23 +0200932 case XDP_UMEM_REG:
933 {
Kevin Laatzc05cd362019-08-27 02:25:22 +0000934 size_t mr_size = sizeof(struct xdp_umem_reg);
935 struct xdp_umem_reg mr = {};
Björn Töpelc0c77d82018-05-02 13:01:23 +0200936 struct xdp_umem *umem;
937
Kevin Laatzc05cd362019-08-27 02:25:22 +0000938 if (optlen < sizeof(struct xdp_umem_reg_v1))
939 return -EINVAL;
940 else if (optlen < sizeof(mr))
941 mr_size = sizeof(struct xdp_umem_reg_v1);
942
Christoph Hellwiga7b75c52020-07-23 08:09:07 +0200943 if (copy_from_sockptr(&mr, optval, mr_size))
Björn Töpelc0c77d82018-05-02 13:01:23 +0200944 return -EFAULT;
945
946 mutex_lock(&xs->mutex);
Ilya Maximets455302d2019-06-28 11:04:07 +0300947 if (xs->state != XSK_READY || xs->umem) {
Björn Töpelc0c77d82018-05-02 13:01:23 +0200948 mutex_unlock(&xs->mutex);
Björn Töpela49049e2018-05-22 09:35:02 +0200949 return -EBUSY;
950 }
951
952 umem = xdp_umem_create(&mr);
953 if (IS_ERR(umem)) {
954 mutex_unlock(&xs->mutex);
955 return PTR_ERR(umem);
Björn Töpelc0c77d82018-05-02 13:01:23 +0200956 }
957
958 /* Make sure umem is ready before it can be seen by others */
959 smp_wmb();
Björn Töpel9764f4b2019-09-04 13:49:11 +0200960 WRITE_ONCE(xs->umem, umem);
Björn Töpelc0c77d82018-05-02 13:01:23 +0200961 mutex_unlock(&xs->mutex);
962 return 0;
963 }
Magnus Karlsson423f3832018-05-02 13:01:24 +0200964 case XDP_UMEM_FILL_RING:
Magnus Karlssonfe230832018-05-02 13:01:31 +0200965 case XDP_UMEM_COMPLETION_RING:
Magnus Karlsson423f3832018-05-02 13:01:24 +0200966 {
967 struct xsk_queue **q;
968 int entries;
969
Christoph Hellwiga7b75c52020-07-23 08:09:07 +0200970 if (copy_from_sockptr(&entries, optval, sizeof(entries)))
Magnus Karlsson423f3832018-05-02 13:01:24 +0200971 return -EFAULT;
972
973 mutex_lock(&xs->mutex);
Ilya Maximets455302d2019-06-28 11:04:07 +0300974 if (xs->state != XSK_READY) {
975 mutex_unlock(&xs->mutex);
976 return -EBUSY;
977 }
Björn Töpela49049e2018-05-22 09:35:02 +0200978
Magnus Karlsson7361f9c2020-08-28 10:26:18 +0200979 q = (optname == XDP_UMEM_FILL_RING) ? &xs->fq_tmp :
980 &xs->cq_tmp;
Björn Töpelb9b6b682018-05-02 13:01:25 +0200981 err = xsk_init_queue(entries, q, true);
Magnus Karlsson423f3832018-05-02 13:01:24 +0200982 mutex_unlock(&xs->mutex);
983 return err;
984 }
Björn Töpelc0c77d82018-05-02 13:01:23 +0200985 default:
986 break;
987 }
988
989 return -ENOPROTOOPT;
990}
991
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200992static void xsk_enter_rxtx_offsets(struct xdp_ring_offset_v1 *ring)
993{
994 ring->producer = offsetof(struct xdp_rxtx_ring, ptrs.producer);
995 ring->consumer = offsetof(struct xdp_rxtx_ring, ptrs.consumer);
996 ring->desc = offsetof(struct xdp_rxtx_ring, desc);
997}
998
999static void xsk_enter_umem_offsets(struct xdp_ring_offset_v1 *ring)
1000{
1001 ring->producer = offsetof(struct xdp_umem_ring, ptrs.producer);
1002 ring->consumer = offsetof(struct xdp_umem_ring, ptrs.consumer);
1003 ring->desc = offsetof(struct xdp_umem_ring, desc);
1004}
1005
Ciara Loftus8aa5a332020-07-08 07:28:33 +00001006struct xdp_statistics_v1 {
1007 __u64 rx_dropped;
1008 __u64 rx_invalid_descs;
1009 __u64 tx_invalid_descs;
1010};
1011
Magnus Karlssonaf75d9e2018-05-02 13:01:35 +02001012static int xsk_getsockopt(struct socket *sock, int level, int optname,
1013 char __user *optval, int __user *optlen)
1014{
1015 struct sock *sk = sock->sk;
1016 struct xdp_sock *xs = xdp_sk(sk);
1017 int len;
1018
1019 if (level != SOL_XDP)
1020 return -ENOPROTOOPT;
1021
1022 if (get_user(len, optlen))
1023 return -EFAULT;
1024 if (len < 0)
1025 return -EINVAL;
1026
1027 switch (optname) {
1028 case XDP_STATISTICS:
1029 {
Peilin Ye3c4f8502020-07-28 01:36:04 -04001030 struct xdp_statistics stats = {};
Ciara Loftus8aa5a332020-07-08 07:28:33 +00001031 bool extra_stats = true;
1032 size_t stats_size;
Magnus Karlssonaf75d9e2018-05-02 13:01:35 +02001033
Ciara Loftus8aa5a332020-07-08 07:28:33 +00001034 if (len < sizeof(struct xdp_statistics_v1)) {
Magnus Karlssonaf75d9e2018-05-02 13:01:35 +02001035 return -EINVAL;
Ciara Loftus8aa5a332020-07-08 07:28:33 +00001036 } else if (len < sizeof(stats)) {
1037 extra_stats = false;
1038 stats_size = sizeof(struct xdp_statistics_v1);
1039 } else {
1040 stats_size = sizeof(stats);
1041 }
Magnus Karlssonaf75d9e2018-05-02 13:01:35 +02001042
1043 mutex_lock(&xs->mutex);
1044 stats.rx_dropped = xs->rx_dropped;
Ciara Loftus8aa5a332020-07-08 07:28:33 +00001045 if (extra_stats) {
1046 stats.rx_ring_full = xs->rx_queue_full;
1047 stats.rx_fill_ring_empty_descs =
Magnus Karlsson7361f9c2020-08-28 10:26:18 +02001048 xs->pool ? xskq_nb_queue_empty_descs(xs->pool->fq) : 0;
Ciara Loftus8aa5a332020-07-08 07:28:33 +00001049 stats.tx_ring_empty_descs = xskq_nb_queue_empty_descs(xs->tx);
1050 } else {
1051 stats.rx_dropped += xs->rx_queue_full;
1052 }
Magnus Karlssonaf75d9e2018-05-02 13:01:35 +02001053 stats.rx_invalid_descs = xskq_nb_invalid_descs(xs->rx);
1054 stats.tx_invalid_descs = xskq_nb_invalid_descs(xs->tx);
1055 mutex_unlock(&xs->mutex);
1056
Ciara Loftus8aa5a332020-07-08 07:28:33 +00001057 if (copy_to_user(optval, &stats, stats_size))
Magnus Karlssonaf75d9e2018-05-02 13:01:35 +02001058 return -EFAULT;
Ciara Loftus8aa5a332020-07-08 07:28:33 +00001059 if (put_user(stats_size, optlen))
Magnus Karlssonaf75d9e2018-05-02 13:01:35 +02001060 return -EFAULT;
1061
1062 return 0;
1063 }
Björn Töpelb3a9e0b2018-05-22 09:34:59 +02001064 case XDP_MMAP_OFFSETS:
1065 {
1066 struct xdp_mmap_offsets off;
Magnus Karlsson77cd0d72019-08-14 09:27:17 +02001067 struct xdp_mmap_offsets_v1 off_v1;
1068 bool flags_supported = true;
1069 void *to_copy;
Björn Töpelb3a9e0b2018-05-22 09:34:59 +02001070
Magnus Karlsson77cd0d72019-08-14 09:27:17 +02001071 if (len < sizeof(off_v1))
Björn Töpelb3a9e0b2018-05-22 09:34:59 +02001072 return -EINVAL;
Magnus Karlsson77cd0d72019-08-14 09:27:17 +02001073 else if (len < sizeof(off))
1074 flags_supported = false;
Björn Töpelb3a9e0b2018-05-22 09:34:59 +02001075
Magnus Karlsson77cd0d72019-08-14 09:27:17 +02001076 if (flags_supported) {
1077 /* xdp_ring_offset is identical to xdp_ring_offset_v1
1078 * except for the flags field added to the end.
1079 */
1080 xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1 *)
1081 &off.rx);
1082 xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1 *)
1083 &off.tx);
1084 xsk_enter_umem_offsets((struct xdp_ring_offset_v1 *)
1085 &off.fr);
1086 xsk_enter_umem_offsets((struct xdp_ring_offset_v1 *)
1087 &off.cr);
1088 off.rx.flags = offsetof(struct xdp_rxtx_ring,
1089 ptrs.flags);
1090 off.tx.flags = offsetof(struct xdp_rxtx_ring,
1091 ptrs.flags);
1092 off.fr.flags = offsetof(struct xdp_umem_ring,
1093 ptrs.flags);
1094 off.cr.flags = offsetof(struct xdp_umem_ring,
1095 ptrs.flags);
Björn Töpelb3a9e0b2018-05-22 09:34:59 +02001096
Magnus Karlsson77cd0d72019-08-14 09:27:17 +02001097 len = sizeof(off);
1098 to_copy = &off;
1099 } else {
1100 xsk_enter_rxtx_offsets(&off_v1.rx);
1101 xsk_enter_rxtx_offsets(&off_v1.tx);
1102 xsk_enter_umem_offsets(&off_v1.fr);
1103 xsk_enter_umem_offsets(&off_v1.cr);
Björn Töpelb3a9e0b2018-05-22 09:34:59 +02001104
Magnus Karlsson77cd0d72019-08-14 09:27:17 +02001105 len = sizeof(off_v1);
1106 to_copy = &off_v1;
1107 }
1108
1109 if (copy_to_user(optval, to_copy, len))
Björn Töpelb3a9e0b2018-05-22 09:34:59 +02001110 return -EFAULT;
1111 if (put_user(len, optlen))
1112 return -EFAULT;
1113
1114 return 0;
1115 }
Maxim Mikityanskiy2640d3c2019-06-26 17:35:25 +03001116 case XDP_OPTIONS:
1117 {
1118 struct xdp_options opts = {};
1119
1120 if (len < sizeof(opts))
1121 return -EINVAL;
1122
1123 mutex_lock(&xs->mutex);
1124 if (xs->zc)
1125 opts.flags |= XDP_OPTIONS_ZEROCOPY;
1126 mutex_unlock(&xs->mutex);
1127
1128 len = sizeof(opts);
1129 if (copy_to_user(optval, &opts, len))
1130 return -EFAULT;
1131 if (put_user(len, optlen))
1132 return -EFAULT;
1133
1134 return 0;
1135 }
Magnus Karlssonaf75d9e2018-05-02 13:01:35 +02001136 default:
1137 break;
1138 }
1139
1140 return -EOPNOTSUPP;
1141}
1142
Magnus Karlsson423f3832018-05-02 13:01:24 +02001143static int xsk_mmap(struct file *file, struct socket *sock,
1144 struct vm_area_struct *vma)
1145{
Geert Uytterhoevena5a16e42018-06-07 15:37:34 +02001146 loff_t offset = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
Magnus Karlsson423f3832018-05-02 13:01:24 +02001147 unsigned long size = vma->vm_end - vma->vm_start;
1148 struct xdp_sock *xs = xdp_sk(sock->sk);
1149 struct xsk_queue *q = NULL;
1150 unsigned long pfn;
1151 struct page *qpg;
1152
Björn Töpel42fddcc2019-09-04 13:49:12 +02001153 if (READ_ONCE(xs->state) != XSK_READY)
Ilya Maximets455302d2019-06-28 11:04:07 +03001154 return -EBUSY;
1155
Björn Töpelb9b6b682018-05-02 13:01:25 +02001156 if (offset == XDP_PGOFF_RX_RING) {
Björn Töpel37b07692018-05-22 09:35:01 +02001157 q = READ_ONCE(xs->rx);
Magnus Karlssonf6145902018-05-02 13:01:32 +02001158 } else if (offset == XDP_PGOFF_TX_RING) {
Björn Töpel37b07692018-05-22 09:35:01 +02001159 q = READ_ONCE(xs->tx);
Björn Töpelb9b6b682018-05-02 13:01:25 +02001160 } else {
Magnus Karlssone6762c82019-02-08 14:13:50 +01001161 /* Matches the smp_wmb() in XDP_UMEM_REG */
1162 smp_rmb();
Björn Töpelb9b6b682018-05-02 13:01:25 +02001163 if (offset == XDP_UMEM_PGOFF_FILL_RING)
Magnus Karlsson7361f9c2020-08-28 10:26:18 +02001164 q = READ_ONCE(xs->fq_tmp);
Magnus Karlssonfe230832018-05-02 13:01:31 +02001165 else if (offset == XDP_UMEM_PGOFF_COMPLETION_RING)
Magnus Karlsson7361f9c2020-08-28 10:26:18 +02001166 q = READ_ONCE(xs->cq_tmp);
Björn Töpelb9b6b682018-05-02 13:01:25 +02001167 }
Magnus Karlsson423f3832018-05-02 13:01:24 +02001168
1169 if (!q)
1170 return -EINVAL;
1171
Magnus Karlssone6762c82019-02-08 14:13:50 +01001172 /* Matches the smp_wmb() in xsk_init_queue */
1173 smp_rmb();
Magnus Karlsson423f3832018-05-02 13:01:24 +02001174 qpg = virt_to_head_page(q->ring);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07001175 if (size > page_size(qpg))
Magnus Karlsson423f3832018-05-02 13:01:24 +02001176 return -EINVAL;
1177
1178 pfn = virt_to_phys(q->ring) >> PAGE_SHIFT;
1179 return remap_pfn_range(vma, vma->vm_start, pfn,
1180 size, vma->vm_page_prot);
1181}
1182
Ilya Maximets455302d2019-06-28 11:04:07 +03001183static int xsk_notifier(struct notifier_block *this,
1184 unsigned long msg, void *ptr)
1185{
1186 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1187 struct net *net = dev_net(dev);
1188 struct sock *sk;
1189
1190 switch (msg) {
1191 case NETDEV_UNREGISTER:
1192 mutex_lock(&net->xdp.lock);
1193 sk_for_each(sk, &net->xdp.list) {
1194 struct xdp_sock *xs = xdp_sk(sk);
1195
1196 mutex_lock(&xs->mutex);
1197 if (xs->dev == dev) {
1198 sk->sk_err = ENETDOWN;
1199 if (!sock_flag(sk, SOCK_DEAD))
1200 sk->sk_error_report(sk);
1201
1202 xsk_unbind_dev(xs);
1203
Magnus Karlsson1c1efc22020-08-28 10:26:17 +02001204 /* Clear device references. */
1205 xp_clear_dev(xs->pool);
Ilya Maximets455302d2019-06-28 11:04:07 +03001206 }
1207 mutex_unlock(&xs->mutex);
1208 }
1209 mutex_unlock(&net->xdp.lock);
1210 break;
1211 }
1212 return NOTIFY_DONE;
1213}
1214
Björn Töpelc0c77d82018-05-02 13:01:23 +02001215static struct proto xsk_proto = {
1216 .name = "XDP",
1217 .owner = THIS_MODULE,
1218 .obj_size = sizeof(struct xdp_sock),
1219};
1220
1221static const struct proto_ops xsk_proto_ops = {
Björn Töpelc2f43742018-05-18 14:00:24 +02001222 .family = PF_XDP,
1223 .owner = THIS_MODULE,
1224 .release = xsk_release,
1225 .bind = xsk_bind,
1226 .connect = sock_no_connect,
1227 .socketpair = sock_no_socketpair,
1228 .accept = sock_no_accept,
1229 .getname = sock_no_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001230 .poll = xsk_poll,
Björn Töpelc2f43742018-05-18 14:00:24 +02001231 .ioctl = sock_no_ioctl,
1232 .listen = sock_no_listen,
1233 .shutdown = sock_no_shutdown,
1234 .setsockopt = xsk_setsockopt,
1235 .getsockopt = xsk_getsockopt,
1236 .sendmsg = xsk_sendmsg,
Björn Töpel45a86682020-11-30 19:51:58 +01001237 .recvmsg = xsk_recvmsg,
Björn Töpelc2f43742018-05-18 14:00:24 +02001238 .mmap = xsk_mmap,
1239 .sendpage = sock_no_sendpage,
Björn Töpelc0c77d82018-05-02 13:01:23 +02001240};
1241
Björn Töpel11fe9262019-02-21 13:07:38 +01001242static void xsk_destruct(struct sock *sk)
1243{
1244 struct xdp_sock *xs = xdp_sk(sk);
1245
1246 if (!sock_flag(sk, SOCK_DEAD))
1247 return;
1248
Magnus Karlssone5e1a4b2020-10-27 13:32:01 +01001249 if (!xp_put_pool(xs->pool))
Magnus Karlsson537cf4e2020-11-20 12:53:39 +01001250 xdp_put_umem(xs->umem, !xs->pool);
Björn Töpel11fe9262019-02-21 13:07:38 +01001251
1252 sk_refcnt_debug_dec(sk);
1253}
1254
Björn Töpelc0c77d82018-05-02 13:01:23 +02001255static int xsk_create(struct net *net, struct socket *sock, int protocol,
1256 int kern)
1257{
Björn Töpelc0c77d82018-05-02 13:01:23 +02001258 struct xdp_sock *xs;
Magnus Karlsson1c1efc22020-08-28 10:26:17 +02001259 struct sock *sk;
Björn Töpelc0c77d82018-05-02 13:01:23 +02001260
1261 if (!ns_capable(net->user_ns, CAP_NET_RAW))
1262 return -EPERM;
1263 if (sock->type != SOCK_RAW)
1264 return -ESOCKTNOSUPPORT;
1265
1266 if (protocol)
1267 return -EPROTONOSUPPORT;
1268
1269 sock->state = SS_UNCONNECTED;
1270
1271 sk = sk_alloc(net, PF_XDP, GFP_KERNEL, &xsk_proto, kern);
1272 if (!sk)
1273 return -ENOBUFS;
1274
1275 sock->ops = &xsk_proto_ops;
1276
1277 sock_init_data(sock, sk);
1278
1279 sk->sk_family = PF_XDP;
1280
Björn Töpel11fe9262019-02-21 13:07:38 +01001281 sk->sk_destruct = xsk_destruct;
1282 sk_refcnt_debug_inc(sk);
1283
Björn Töpelcee27162018-10-08 19:40:16 +02001284 sock_set_flag(sk, SOCK_RCU_FREE);
1285
Björn Töpelc0c77d82018-05-02 13:01:23 +02001286 xs = xdp_sk(sk);
Ilya Maximets455302d2019-06-28 11:04:07 +03001287 xs->state = XSK_READY;
Björn Töpelc0c77d82018-05-02 13:01:23 +02001288 mutex_init(&xs->mutex);
Ilya Maximetsbf0bdd12019-07-03 15:09:16 +03001289 spin_lock_init(&xs->rx_lock);
Magnus Karlssona9744f72018-06-29 09:48:20 +02001290 spin_lock_init(&xs->tx_completion_lock);
Björn Töpelc0c77d82018-05-02 13:01:23 +02001291
Björn Töpel0402acd2019-08-15 11:30:13 +02001292 INIT_LIST_HEAD(&xs->map_list);
1293 spin_lock_init(&xs->map_list_lock);
1294
Björn Töpel1d0dc062019-01-24 19:59:37 +01001295 mutex_lock(&net->xdp.lock);
1296 sk_add_node_rcu(sk, &net->xdp.list);
1297 mutex_unlock(&net->xdp.lock);
1298
Björn Töpelc0c77d82018-05-02 13:01:23 +02001299 local_bh_disable();
1300 sock_prot_inuse_add(net, &xsk_proto, 1);
1301 local_bh_enable();
1302
1303 return 0;
1304}
1305
1306static const struct net_proto_family xsk_family_ops = {
1307 .family = PF_XDP,
1308 .create = xsk_create,
1309 .owner = THIS_MODULE,
1310};
1311
Ilya Maximets455302d2019-06-28 11:04:07 +03001312static struct notifier_block xsk_netdev_notifier = {
1313 .notifier_call = xsk_notifier,
1314};
1315
Björn Töpel1d0dc062019-01-24 19:59:37 +01001316static int __net_init xsk_net_init(struct net *net)
1317{
1318 mutex_init(&net->xdp.lock);
1319 INIT_HLIST_HEAD(&net->xdp.list);
1320 return 0;
1321}
1322
1323static void __net_exit xsk_net_exit(struct net *net)
1324{
1325 WARN_ON_ONCE(!hlist_empty(&net->xdp.list));
1326}
1327
1328static struct pernet_operations xsk_net_ops = {
1329 .init = xsk_net_init,
1330 .exit = xsk_net_exit,
1331};
1332
Björn Töpelc0c77d82018-05-02 13:01:23 +02001333static int __init xsk_init(void)
1334{
Björn Töpele312b9e2019-12-19 07:10:02 +01001335 int err, cpu;
Björn Töpelc0c77d82018-05-02 13:01:23 +02001336
1337 err = proto_register(&xsk_proto, 0 /* no slab */);
1338 if (err)
1339 goto out;
1340
1341 err = sock_register(&xsk_family_ops);
1342 if (err)
1343 goto out_proto;
1344
Björn Töpel1d0dc062019-01-24 19:59:37 +01001345 err = register_pernet_subsys(&xsk_net_ops);
1346 if (err)
1347 goto out_sk;
Ilya Maximets455302d2019-06-28 11:04:07 +03001348
1349 err = register_netdevice_notifier(&xsk_netdev_notifier);
1350 if (err)
1351 goto out_pernet;
1352
Björn Töpele312b9e2019-12-19 07:10:02 +01001353 for_each_possible_cpu(cpu)
1354 INIT_LIST_HEAD(&per_cpu(xskmap_flush_list, cpu));
Björn Töpelc0c77d82018-05-02 13:01:23 +02001355 return 0;
1356
Ilya Maximets455302d2019-06-28 11:04:07 +03001357out_pernet:
1358 unregister_pernet_subsys(&xsk_net_ops);
Björn Töpel1d0dc062019-01-24 19:59:37 +01001359out_sk:
1360 sock_unregister(PF_XDP);
Björn Töpelc0c77d82018-05-02 13:01:23 +02001361out_proto:
1362 proto_unregister(&xsk_proto);
1363out:
1364 return err;
1365}
1366
1367fs_initcall(xsk_init);