blob: 55092feeaf36e3715b8086a8087cda2776fbe341 [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>
Björn Töpelc0c77d82018-05-02 13:01:23 +020025#include <net/xdp_sock.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
Maxim Mikityanskiyd57d7642019-06-26 17:35:24 +030042bool xsk_umem_has_addrs(struct xdp_umem *umem, u32 cnt)
43{
Magnus Karlssonc5ed924b2019-12-19 13:39:26 +010044 return xskq_cons_has_entries(umem->fq, cnt);
Maxim Mikityanskiyd57d7642019-06-26 17:35:24 +030045}
46EXPORT_SYMBOL(xsk_umem_has_addrs);
47
Magnus Karlsson03896ef2019-12-19 13:39:27 +010048bool xsk_umem_peek_addr(struct xdp_umem *umem, u64 *addr)
Björn Töpelc4971762018-05-02 13:01:27 +020049{
Magnus Karlssonc5ed924b2019-12-19 13:39:26 +010050 return xskq_cons_peek_addr(umem->fq, addr, umem);
Björn Töpel173d3ad2018-06-04 14:05:55 +020051}
52EXPORT_SYMBOL(xsk_umem_peek_addr);
53
Magnus Karlssonf8509aa2019-12-19 13:39:28 +010054void xsk_umem_release_addr(struct xdp_umem *umem)
Björn Töpel173d3ad2018-06-04 14:05:55 +020055{
Magnus Karlssonc5ed924b2019-12-19 13:39:26 +010056 xskq_cons_release(umem->fq);
Björn Töpel173d3ad2018-06-04 14:05:55 +020057}
Magnus Karlssonf8509aa2019-12-19 13:39:28 +010058EXPORT_SYMBOL(xsk_umem_release_addr);
Björn Töpel173d3ad2018-06-04 14:05:55 +020059
Magnus Karlsson77cd0d72019-08-14 09:27:17 +020060void xsk_set_rx_need_wakeup(struct xdp_umem *umem)
61{
62 if (umem->need_wakeup & XDP_WAKEUP_RX)
63 return;
64
65 umem->fq->ring->flags |= XDP_RING_NEED_WAKEUP;
66 umem->need_wakeup |= XDP_WAKEUP_RX;
67}
68EXPORT_SYMBOL(xsk_set_rx_need_wakeup);
69
70void xsk_set_tx_need_wakeup(struct xdp_umem *umem)
71{
72 struct xdp_sock *xs;
73
74 if (umem->need_wakeup & XDP_WAKEUP_TX)
75 return;
76
77 rcu_read_lock();
78 list_for_each_entry_rcu(xs, &umem->xsk_list, list) {
79 xs->tx->ring->flags |= XDP_RING_NEED_WAKEUP;
80 }
81 rcu_read_unlock();
82
83 umem->need_wakeup |= XDP_WAKEUP_TX;
84}
85EXPORT_SYMBOL(xsk_set_tx_need_wakeup);
86
87void xsk_clear_rx_need_wakeup(struct xdp_umem *umem)
88{
89 if (!(umem->need_wakeup & XDP_WAKEUP_RX))
90 return;
91
92 umem->fq->ring->flags &= ~XDP_RING_NEED_WAKEUP;
93 umem->need_wakeup &= ~XDP_WAKEUP_RX;
94}
95EXPORT_SYMBOL(xsk_clear_rx_need_wakeup);
96
97void xsk_clear_tx_need_wakeup(struct xdp_umem *umem)
98{
99 struct xdp_sock *xs;
100
101 if (!(umem->need_wakeup & XDP_WAKEUP_TX))
102 return;
103
104 rcu_read_lock();
105 list_for_each_entry_rcu(xs, &umem->xsk_list, list) {
106 xs->tx->ring->flags &= ~XDP_RING_NEED_WAKEUP;
107 }
108 rcu_read_unlock();
109
110 umem->need_wakeup &= ~XDP_WAKEUP_TX;
111}
112EXPORT_SYMBOL(xsk_clear_tx_need_wakeup);
113
114bool xsk_umem_uses_need_wakeup(struct xdp_umem *umem)
115{
116 return umem->flags & XDP_UMEM_USES_NEED_WAKEUP;
117}
118EXPORT_SYMBOL(xsk_umem_uses_need_wakeup);
119
Kevin Laatzc05cd362019-08-27 02:25:22 +0000120/* If a buffer crosses a page boundary, we need to do 2 memcpy's, one for
121 * each page. This is only required in copy mode.
122 */
123static void __xsk_rcv_memcpy(struct xdp_umem *umem, u64 addr, void *from_buf,
124 u32 len, u32 metalen)
125{
126 void *to_buf = xdp_umem_get_data(umem, addr);
127
128 addr = xsk_umem_add_offset_to_addr(addr);
Magnus Karlsson03896ef2019-12-19 13:39:27 +0100129 if (xskq_cons_crosses_non_contig_pg(umem, addr, len + metalen)) {
Kevin Laatzc05cd362019-08-27 02:25:22 +0000130 void *next_pg_addr = umem->pages[(addr >> PAGE_SHIFT) + 1].addr;
131 u64 page_start = addr & ~(PAGE_SIZE - 1);
132 u64 first_len = PAGE_SIZE - (addr - page_start);
133
134 memcpy(to_buf, from_buf, first_len + metalen);
135 memcpy(next_pg_addr, from_buf + first_len, len - first_len);
136
137 return;
138 }
139
140 memcpy(to_buf, from_buf, len + metalen);
141}
142
Björn Töpel173d3ad2018-06-04 14:05:55 +0200143static int __xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
144{
Kevin Laatzc05cd362019-08-27 02:25:22 +0000145 u64 offset = xs->umem->headroom;
146 u64 addr, memcpy_addr;
147 void *from_buf;
Björn Töpel18baed22018-08-30 15:12:48 +0200148 u32 metalen;
Björn Töpel4e64c832018-06-04 13:57:11 +0200149 int err;
Björn Töpelc4971762018-05-02 13:01:27 +0200150
Magnus Karlssonc5ed924b2019-12-19 13:39:26 +0100151 if (!xskq_cons_peek_addr(xs->umem->fq, &addr, xs->umem) ||
Björn Töpel18baed22018-08-30 15:12:48 +0200152 len > xs->umem->chunk_size_nohr - XDP_PACKET_HEADROOM) {
Björn Töpela509a952018-06-04 13:57:12 +0200153 xs->rx_dropped++;
Björn Töpelc4971762018-05-02 13:01:27 +0200154 return -ENOSPC;
Björn Töpela509a952018-06-04 13:57:12 +0200155 }
Björn Töpelc4971762018-05-02 13:01:27 +0200156
Björn Töpel18baed22018-08-30 15:12:48 +0200157 if (unlikely(xdp_data_meta_unsupported(xdp))) {
158 from_buf = xdp->data;
159 metalen = 0;
160 } else {
161 from_buf = xdp->data_meta;
162 metalen = xdp->data - xdp->data_meta;
163 }
164
Kevin Laatzc05cd362019-08-27 02:25:22 +0000165 memcpy_addr = xsk_umem_adjust_offset(xs->umem, addr, offset);
166 __xsk_rcv_memcpy(xs->umem, memcpy_addr, from_buf, len, metalen);
167
168 offset += metalen;
169 addr = xsk_umem_adjust_offset(xs->umem, addr, offset);
Magnus Karlsson59e35e52019-12-19 13:39:23 +0100170 err = xskq_prod_reserve_desc(xs->rx, addr, len);
Björn Töpel173d3ad2018-06-04 14:05:55 +0200171 if (!err) {
Magnus Karlssonc5ed924b2019-12-19 13:39:26 +0100172 xskq_cons_release(xs->umem->fq);
Björn Töpel173d3ad2018-06-04 14:05:55 +0200173 xdp_return_buff(xdp);
174 return 0;
175 }
176
177 xs->rx_dropped++;
178 return err;
179}
180
181static int __xsk_rcv_zc(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
182{
Magnus Karlsson59e35e52019-12-19 13:39:23 +0100183 int err = xskq_prod_reserve_desc(xs->rx, xdp->handle, len);
Björn Töpel173d3ad2018-06-04 14:05:55 +0200184
Jakub Kicinski2d55d612018-07-27 20:20:08 -0700185 if (err)
Björn Töpela509a952018-06-04 13:57:12 +0200186 xs->rx_dropped++;
Björn Töpelc4971762018-05-02 13:01:27 +0200187
188 return err;
189}
190
Björn Töpel42fddcc2019-09-04 13:49:12 +0200191static bool xsk_is_bound(struct xdp_sock *xs)
192{
193 if (READ_ONCE(xs->state) == XSK_BOUND) {
194 /* Matches smp_wmb() in bind(). */
195 smp_rmb();
196 return true;
197 }
198 return false;
199}
200
Björn Töpeld8179912019-11-01 12:03:46 +0100201static int xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
Björn Töpelc4971762018-05-02 13:01:27 +0200202{
Björn Töpel173d3ad2018-06-04 14:05:55 +0200203 u32 len;
Björn Töpelc4971762018-05-02 13:01:27 +0200204
Björn Töpel42fddcc2019-09-04 13:49:12 +0200205 if (!xsk_is_bound(xs))
206 return -EINVAL;
207
Björn Töpel173d3ad2018-06-04 14:05:55 +0200208 if (xs->dev != xdp->rxq->dev || xs->queue_id != xdp->rxq->queue_index)
209 return -EINVAL;
Björn Töpelc4971762018-05-02 13:01:27 +0200210
Björn Töpel173d3ad2018-06-04 14:05:55 +0200211 len = xdp->data_end - xdp->data;
212
213 return (xdp->rxq->mem.type == MEM_TYPE_ZERO_COPY) ?
214 __xsk_rcv_zc(xs, xdp, len) : __xsk_rcv(xs, xdp, len);
Björn Töpelc4971762018-05-02 13:01:27 +0200215}
216
Björn Töpeld8179912019-11-01 12:03:46 +0100217static void xsk_flush(struct xdp_sock *xs)
Björn Töpelc4971762018-05-02 13:01:27 +0200218{
Magnus Karlsson59e35e52019-12-19 13:39:23 +0100219 xskq_prod_submit(xs->rx);
Björn Töpelc4971762018-05-02 13:01:27 +0200220 xs->sk.sk_data_ready(&xs->sk);
221}
222
223int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
224{
Björn Töpel18baed22018-08-30 15:12:48 +0200225 u32 metalen = xdp->data - xdp->data_meta;
Björn Töpel173d3ad2018-06-04 14:05:55 +0200226 u32 len = xdp->data_end - xdp->data;
Kevin Laatzc05cd362019-08-27 02:25:22 +0000227 u64 offset = xs->umem->headroom;
Björn Töpel173d3ad2018-06-04 14:05:55 +0200228 void *buffer;
229 u64 addr;
Björn Töpelc4971762018-05-02 13:01:27 +0200230 int err;
231
Ilya Maximetsbf0bdd12019-07-03 15:09:16 +0300232 spin_lock_bh(&xs->rx_lock);
233
234 if (xs->dev != xdp->rxq->dev || xs->queue_id != xdp->rxq->queue_index) {
235 err = -EINVAL;
236 goto out_unlock;
237 }
Björn Töpel5d902372018-06-12 12:02:56 +0200238
Magnus Karlssonc5ed924b2019-12-19 13:39:26 +0100239 if (!xskq_cons_peek_addr(xs->umem->fq, &addr, xs->umem) ||
Björn Töpel18baed22018-08-30 15:12:48 +0200240 len > xs->umem->chunk_size_nohr - XDP_PACKET_HEADROOM) {
Ilya Maximetsbf0bdd12019-07-03 15:09:16 +0300241 err = -ENOSPC;
242 goto out_drop;
Björn Töpel173d3ad2018-06-04 14:05:55 +0200243 }
Björn Töpelc4971762018-05-02 13:01:27 +0200244
Kevin Laatzc05cd362019-08-27 02:25:22 +0000245 addr = xsk_umem_adjust_offset(xs->umem, addr, offset);
Björn Töpel173d3ad2018-06-04 14:05:55 +0200246 buffer = xdp_umem_get_data(xs->umem, addr);
Björn Töpel18baed22018-08-30 15:12:48 +0200247 memcpy(buffer, xdp->data_meta, len + metalen);
Kevin Laatzc05cd362019-08-27 02:25:22 +0000248
249 addr = xsk_umem_adjust_offset(xs->umem, addr, metalen);
Magnus Karlsson59e35e52019-12-19 13:39:23 +0100250 err = xskq_prod_reserve_desc(xs->rx, addr, len);
Ilya Maximetsbf0bdd12019-07-03 15:09:16 +0300251 if (err)
252 goto out_drop;
Björn Töpel173d3ad2018-06-04 14:05:55 +0200253
Magnus Karlssonc5ed924b2019-12-19 13:39:26 +0100254 xskq_cons_release(xs->umem->fq);
Magnus Karlsson59e35e52019-12-19 13:39:23 +0100255 xskq_prod_submit(xs->rx);
Ilya Maximetsbf0bdd12019-07-03 15:09:16 +0300256
257 spin_unlock_bh(&xs->rx_lock);
258
259 xs->sk.sk_data_ready(&xs->sk);
260 return 0;
261
262out_drop:
Björn Töpel173d3ad2018-06-04 14:05:55 +0200263 xs->rx_dropped++;
Ilya Maximetsbf0bdd12019-07-03 15:09:16 +0300264out_unlock:
265 spin_unlock_bh(&xs->rx_lock);
Björn Töpelc4971762018-05-02 13:01:27 +0200266 return err;
267}
268
Björn Töpele312b9e2019-12-19 07:10:02 +0100269int __xsk_map_redirect(struct xdp_sock *xs, struct xdp_buff *xdp)
Björn Töpeld8179912019-11-01 12:03:46 +0100270{
Björn Töpele312b9e2019-12-19 07:10:02 +0100271 struct list_head *flush_list = this_cpu_ptr(&xskmap_flush_list);
Björn Töpeld8179912019-11-01 12:03:46 +0100272 int err;
273
274 err = xsk_rcv(xs, xdp);
275 if (err)
276 return err;
277
278 if (!xs->flush_node.prev)
279 list_add(&xs->flush_node, flush_list);
280
281 return 0;
282}
283
Björn Töpele312b9e2019-12-19 07:10:02 +0100284void __xsk_map_flush(void)
Björn Töpeld8179912019-11-01 12:03:46 +0100285{
Björn Töpele312b9e2019-12-19 07:10:02 +0100286 struct list_head *flush_list = this_cpu_ptr(&xskmap_flush_list);
Björn Töpeld8179912019-11-01 12:03:46 +0100287 struct xdp_sock *xs, *tmp;
288
289 list_for_each_entry_safe(xs, tmp, flush_list, flush_node) {
290 xsk_flush(xs);
291 __list_del_clearprev(&xs->flush_node);
292 }
293}
294
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200295void xsk_umem_complete_tx(struct xdp_umem *umem, u32 nb_entries)
296{
Magnus Karlsson59e35e52019-12-19 13:39:23 +0100297 xskq_prod_submit_n(umem->cq, nb_entries);
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200298}
299EXPORT_SYMBOL(xsk_umem_complete_tx);
300
301void xsk_umem_consume_tx_done(struct xdp_umem *umem)
302{
303 struct xdp_sock *xs;
304
305 rcu_read_lock();
306 list_for_each_entry_rcu(xs, &umem->xsk_list, list) {
307 xs->sk.sk_write_space(&xs->sk);
308 }
309 rcu_read_unlock();
310}
311EXPORT_SYMBOL(xsk_umem_consume_tx_done);
312
Maxim Mikityanskiy4bce4e52019-06-26 17:35:28 +0300313bool xsk_umem_consume_tx(struct xdp_umem *umem, struct xdp_desc *desc)
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200314{
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200315 struct xdp_sock *xs;
316
317 rcu_read_lock();
318 list_for_each_entry_rcu(xs, &umem->xsk_list, list) {
Magnus Karlssonc5ed924b2019-12-19 13:39:26 +0100319 if (!xskq_cons_peek_desc(xs->tx, desc, umem))
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200320 continue;
321
Magnus Karlsson59e35e52019-12-19 13:39:23 +0100322 if (xskq_prod_reserve_addr(umem->cq, desc->addr))
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200323 goto out;
324
Magnus Karlssonc5ed924b2019-12-19 13:39:26 +0100325 xskq_cons_release(xs->tx);
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200326 rcu_read_unlock();
327 return true;
328 }
329
330out:
331 rcu_read_unlock();
332 return false;
333}
334EXPORT_SYMBOL(xsk_umem_consume_tx);
335
Magnus Karlssondf551052019-10-02 08:31:59 +0200336static int xsk_zc_xmit(struct xdp_sock *xs)
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200337{
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200338 struct net_device *dev = xs->dev;
339
Magnus Karlsson9116e5e2019-08-14 09:27:16 +0200340 return dev->netdev_ops->ndo_xsk_wakeup(dev, xs->queue_id,
341 XDP_WAKEUP_TX);
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200342}
343
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200344static void xsk_destruct_skb(struct sk_buff *skb)
345{
Björn Töpelbbff2f32018-06-04 13:57:13 +0200346 u64 addr = (u64)(long)skb_shinfo(skb)->destructor_arg;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200347 struct xdp_sock *xs = xdp_sk(skb->sk);
Magnus Karlssona9744f72018-06-29 09:48:20 +0200348 unsigned long flags;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200349
Magnus Karlssona9744f72018-06-29 09:48:20 +0200350 spin_lock_irqsave(&xs->tx_completion_lock, flags);
Magnus Karlsson59e35e52019-12-19 13:39:23 +0100351 xskq_prod_submit_addr(xs->umem->cq, addr);
Magnus Karlssona9744f72018-06-29 09:48:20 +0200352 spin_unlock_irqrestore(&xs->tx_completion_lock, flags);
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200353
354 sock_wfree(skb);
355}
356
Magnus Karlssondf551052019-10-02 08:31:59 +0200357static int xsk_generic_xmit(struct sock *sk)
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200358{
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200359 struct xdp_sock *xs = xdp_sk(sk);
Magnus Karlssondf551052019-10-02 08:31:59 +0200360 u32 max_batch = TX_BATCH_SIZE;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200361 bool sent_frame = false;
362 struct xdp_desc desc;
363 struct sk_buff *skb;
364 int err = 0;
365
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200366 mutex_lock(&xs->mutex);
367
Ilya Maximets67571642019-07-04 17:25:03 +0300368 if (xs->queue_id >= xs->dev->real_num_tx_queues)
369 goto out;
370
Magnus Karlssonc5ed924b2019-12-19 13:39:26 +0100371 while (xskq_cons_peek_desc(xs->tx, &desc, xs->umem)) {
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200372 char *buffer;
Björn Töpelbbff2f32018-06-04 13:57:13 +0200373 u64 addr;
374 u32 len;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200375
376 if (max_batch-- == 0) {
377 err = -EAGAIN;
378 goto out;
379 }
380
Magnus Karlsson09210c42018-07-11 10:12:52 +0200381 len = desc.len;
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200382 skb = sock_alloc_send_skb(sk, len, 1, &err);
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200383 if (unlikely(!skb)) {
384 err = -EAGAIN;
385 goto out;
386 }
387
388 skb_put(skb, len);
Björn Töpelbbff2f32018-06-04 13:57:13 +0200389 addr = desc.addr;
390 buffer = xdp_umem_get_data(xs->umem, addr);
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200391 err = skb_store_bits(skb, 0, buffer, len);
Magnus Karlsson59e35e52019-12-19 13:39:23 +0100392 if (unlikely(err) || xskq_prod_reserve(xs->umem->cq)) {
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200393 kfree_skb(skb);
394 goto out;
395 }
396
397 skb->dev = xs->dev;
398 skb->priority = sk->sk_priority;
399 skb->mark = sk->sk_mark;
Kevin Laatzc05cd362019-08-27 02:25:22 +0000400 skb_shinfo(skb)->destructor_arg = (void *)(long)desc.addr;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200401 skb->destructor = xsk_destruct_skb;
402
403 err = dev_direct_xmit(skb, xs->queue_id);
Magnus Karlssonc5ed924b2019-12-19 13:39:26 +0100404 xskq_cons_release(xs->tx);
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200405 /* Ignore NET_XMIT_CN as packet might have been sent */
406 if (err == NET_XMIT_DROP || err == NETDEV_TX_BUSY) {
Magnus Karlssonfe588682018-06-29 09:48:18 +0200407 /* SKB completed but not sent */
408 err = -EBUSY;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200409 goto out;
410 }
411
412 sent_frame = true;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200413 }
414
415out:
416 if (sent_frame)
417 sk->sk_write_space(sk);
418
419 mutex_unlock(&xs->mutex);
420 return err;
421}
422
Magnus Karlssondf551052019-10-02 08:31:59 +0200423static int __xsk_sendmsg(struct sock *sk)
424{
425 struct xdp_sock *xs = xdp_sk(sk);
426
427 if (unlikely(!(xs->dev->flags & IFF_UP)))
428 return -ENETDOWN;
429 if (unlikely(!xs->tx))
430 return -ENOBUFS;
431
432 return xs->zc ? xsk_zc_xmit(xs) : xsk_generic_xmit(sk);
433}
434
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200435static int xsk_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
436{
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200437 bool need_wait = !(m->msg_flags & MSG_DONTWAIT);
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200438 struct sock *sk = sock->sk;
439 struct xdp_sock *xs = xdp_sk(sk);
440
Björn Töpel42fddcc2019-09-04 13:49:12 +0200441 if (unlikely(!xsk_is_bound(xs)))
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200442 return -ENXIO;
Magnus Karlssondf551052019-10-02 08:31:59 +0200443 if (unlikely(need_wait))
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200444 return -EOPNOTSUPP;
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200445
Magnus Karlssondf551052019-10-02 08:31:59 +0200446 return __xsk_sendmsg(sk);
Magnus Karlsson35fcde72018-05-02 13:01:34 +0200447}
448
Luc Van Oostenryck5d946c52019-11-20 01:10:42 +0100449static __poll_t xsk_poll(struct file *file, struct socket *sock,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700450 struct poll_table_struct *wait)
Björn Töpelc4971762018-05-02 13:01:27 +0200451{
Luc Van Oostenryck5d946c52019-11-20 01:10:42 +0100452 __poll_t mask = datagram_poll(file, sock, wait);
Magnus Karlssondf551052019-10-02 08:31:59 +0200453 struct sock *sk = sock->sk;
454 struct xdp_sock *xs = xdp_sk(sk);
Björn Töpel42fddcc2019-09-04 13:49:12 +0200455 struct net_device *dev;
456 struct xdp_umem *umem;
457
458 if (unlikely(!xsk_is_bound(xs)))
459 return mask;
460
461 dev = xs->dev;
462 umem = xs->umem;
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200463
Magnus Karlssondf551052019-10-02 08:31:59 +0200464 if (umem->need_wakeup) {
465 if (dev->netdev_ops->ndo_xsk_wakeup)
466 dev->netdev_ops->ndo_xsk_wakeup(dev, xs->queue_id,
467 umem->need_wakeup);
468 else
469 /* Poll needs to drive Tx also in copy mode */
470 __xsk_sendmsg(sk);
471 }
Björn Töpelc4971762018-05-02 13:01:27 +0200472
Magnus Karlsson59e35e52019-12-19 13:39:23 +0100473 if (xs->rx && !xskq_prod_is_empty(xs->rx))
Luc Van Oostenryck5d946c52019-11-20 01:10:42 +0100474 mask |= EPOLLIN | EPOLLRDNORM;
Magnus Karlssonc5ed924b2019-12-19 13:39:26 +0100475 if (xs->tx && !xskq_cons_is_full(xs->tx))
Luc Van Oostenryck5d946c52019-11-20 01:10:42 +0100476 mask |= EPOLLOUT | EPOLLWRNORM;
Björn Töpelc4971762018-05-02 13:01:27 +0200477
478 return mask;
479}
480
Björn Töpelb9b6b682018-05-02 13:01:25 +0200481static int xsk_init_queue(u32 entries, struct xsk_queue **queue,
482 bool umem_queue)
Magnus Karlsson423f3832018-05-02 13:01:24 +0200483{
484 struct xsk_queue *q;
485
486 if (entries == 0 || *queue || !is_power_of_2(entries))
487 return -EINVAL;
488
Björn Töpelb9b6b682018-05-02 13:01:25 +0200489 q = xskq_create(entries, umem_queue);
Magnus Karlsson423f3832018-05-02 13:01:24 +0200490 if (!q)
491 return -ENOMEM;
492
Björn Töpel37b07692018-05-22 09:35:01 +0200493 /* Make sure queue is ready before it can be seen by others */
494 smp_wmb();
Björn Töpel94a99762019-09-04 13:49:10 +0200495 WRITE_ONCE(*queue, q);
Magnus Karlsson423f3832018-05-02 13:01:24 +0200496 return 0;
497}
498
Ilya Maximets455302d2019-06-28 11:04:07 +0300499static void xsk_unbind_dev(struct xdp_sock *xs)
500{
501 struct net_device *dev = xs->dev;
502
Björn Töpel42fddcc2019-09-04 13:49:12 +0200503 if (xs->state != XSK_BOUND)
Ilya Maximets455302d2019-06-28 11:04:07 +0300504 return;
Björn Töpel42fddcc2019-09-04 13:49:12 +0200505 WRITE_ONCE(xs->state, XSK_UNBOUND);
Ilya Maximets455302d2019-06-28 11:04:07 +0300506
507 /* Wait for driver to stop using the xdp socket. */
508 xdp_del_sk_umem(xs->umem, xs);
509 xs->dev = NULL;
510 synchronize_net();
511 dev_put(dev);
512}
513
Björn Töpel0402acd2019-08-15 11:30:13 +0200514static struct xsk_map *xsk_get_map_list_entry(struct xdp_sock *xs,
515 struct xdp_sock ***map_entry)
516{
517 struct xsk_map *map = NULL;
518 struct xsk_map_node *node;
519
520 *map_entry = NULL;
521
522 spin_lock_bh(&xs->map_list_lock);
523 node = list_first_entry_or_null(&xs->map_list, struct xsk_map_node,
524 node);
525 if (node) {
526 WARN_ON(xsk_map_inc(node->map));
527 map = node->map;
528 *map_entry = node->map_entry;
529 }
530 spin_unlock_bh(&xs->map_list_lock);
531 return map;
532}
533
534static void xsk_delete_from_maps(struct xdp_sock *xs)
535{
536 /* This function removes the current XDP socket from all the
537 * maps it resides in. We need to take extra care here, due to
538 * the two locks involved. Each map has a lock synchronizing
539 * updates to the entries, and each socket has a lock that
540 * synchronizes access to the list of maps (map_list). For
541 * deadlock avoidance the locks need to be taken in the order
542 * "map lock"->"socket map list lock". We start off by
543 * accessing the socket map list, and take a reference to the
544 * map to guarantee existence between the
545 * xsk_get_map_list_entry() and xsk_map_try_sock_delete()
546 * calls. Then we ask the map to remove the socket, which
547 * tries to remove the socket from the map. Note that there
548 * might be updates to the map between
549 * xsk_get_map_list_entry() and xsk_map_try_sock_delete().
550 */
551 struct xdp_sock **map_entry = NULL;
552 struct xsk_map *map;
553
554 while ((map = xsk_get_map_list_entry(xs, &map_entry))) {
555 xsk_map_try_sock_delete(map, xs, map_entry);
556 xsk_map_put(map);
557 }
558}
559
Björn Töpelc0c77d82018-05-02 13:01:23 +0200560static int xsk_release(struct socket *sock)
561{
562 struct sock *sk = sock->sk;
Magnus Karlsson965a9902018-05-02 13:01:26 +0200563 struct xdp_sock *xs = xdp_sk(sk);
Björn Töpelc0c77d82018-05-02 13:01:23 +0200564 struct net *net;
565
566 if (!sk)
567 return 0;
568
569 net = sock_net(sk);
570
Björn Töpel1d0dc062019-01-24 19:59:37 +0100571 mutex_lock(&net->xdp.lock);
572 sk_del_node_init_rcu(sk);
573 mutex_unlock(&net->xdp.lock);
574
Björn Töpelc0c77d82018-05-02 13:01:23 +0200575 local_bh_disable();
576 sock_prot_inuse_add(net, sk->sk_prot, -1);
577 local_bh_enable();
578
Björn Töpel0402acd2019-08-15 11:30:13 +0200579 xsk_delete_from_maps(xs);
Björn Töpel42fddcc2019-09-04 13:49:12 +0200580 mutex_lock(&xs->mutex);
Ilya Maximets455302d2019-06-28 11:04:07 +0300581 xsk_unbind_dev(xs);
Björn Töpel42fddcc2019-09-04 13:49:12 +0200582 mutex_unlock(&xs->mutex);
Magnus Karlsson965a9902018-05-02 13:01:26 +0200583
Björn Töpel541d7fd2018-10-05 13:25:15 +0200584 xskq_destroy(xs->rx);
585 xskq_destroy(xs->tx);
586
Björn Töpelc0c77d82018-05-02 13:01:23 +0200587 sock_orphan(sk);
588 sock->sk = NULL;
589
590 sk_refcnt_debug_release(sk);
591 sock_put(sk);
592
593 return 0;
594}
595
Magnus Karlsson965a9902018-05-02 13:01:26 +0200596static struct socket *xsk_lookup_xsk_from_fd(int fd)
597{
598 struct socket *sock;
599 int err;
600
601 sock = sockfd_lookup(fd, &err);
602 if (!sock)
603 return ERR_PTR(-ENOTSOCK);
604
605 if (sock->sk->sk_family != PF_XDP) {
606 sockfd_put(sock);
607 return ERR_PTR(-ENOPROTOOPT);
608 }
609
610 return sock;
611}
612
Kevin Laatzc05cd362019-08-27 02:25:22 +0000613/* Check if umem pages are contiguous.
614 * If zero-copy mode, use the DMA address to do the page contiguity check
615 * For all other modes we use addr (kernel virtual address)
616 * Store the result in the low bits of addr.
617 */
618static void xsk_check_page_contiguity(struct xdp_umem *umem, u32 flags)
619{
620 struct xdp_umem_page *pgs = umem->pages;
621 int i, is_contig;
622
623 for (i = 0; i < umem->npgs - 1; i++) {
624 is_contig = (flags & XDP_ZEROCOPY) ?
625 (pgs[i].dma + PAGE_SIZE == pgs[i + 1].dma) :
626 (pgs[i].addr + PAGE_SIZE == pgs[i + 1].addr);
627 pgs[i].addr += is_contig << XSK_NEXT_PG_CONTIG_SHIFT;
628 }
629}
630
Magnus Karlsson965a9902018-05-02 13:01:26 +0200631static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
632{
633 struct sockaddr_xdp *sxdp = (struct sockaddr_xdp *)addr;
634 struct sock *sk = sock->sk;
Magnus Karlsson965a9902018-05-02 13:01:26 +0200635 struct xdp_sock *xs = xdp_sk(sk);
Björn Töpel959b71d2018-05-22 09:34:56 +0200636 struct net_device *dev;
Björn Töpel173d3ad2018-06-04 14:05:55 +0200637 u32 flags, qid;
Magnus Karlsson965a9902018-05-02 13:01:26 +0200638 int err = 0;
639
640 if (addr_len < sizeof(struct sockaddr_xdp))
641 return -EINVAL;
642 if (sxdp->sxdp_family != AF_XDP)
643 return -EINVAL;
644
Björn Töpelf54ba392019-03-08 08:57:26 +0100645 flags = sxdp->sxdp_flags;
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200646 if (flags & ~(XDP_SHARED_UMEM | XDP_COPY | XDP_ZEROCOPY |
647 XDP_USE_NEED_WAKEUP))
Björn Töpelf54ba392019-03-08 08:57:26 +0100648 return -EINVAL;
649
Ilya Maximets5464c3a2019-07-08 14:03:44 +0300650 rtnl_lock();
Magnus Karlsson965a9902018-05-02 13:01:26 +0200651 mutex_lock(&xs->mutex);
Ilya Maximets455302d2019-06-28 11:04:07 +0300652 if (xs->state != XSK_READY) {
Björn Töpel959b71d2018-05-22 09:34:56 +0200653 err = -EBUSY;
654 goto out_release;
655 }
656
Magnus Karlsson965a9902018-05-02 13:01:26 +0200657 dev = dev_get_by_index(sock_net(sk), sxdp->sxdp_ifindex);
658 if (!dev) {
659 err = -ENODEV;
660 goto out_release;
661 }
662
Magnus Karlssonf6145902018-05-02 13:01:32 +0200663 if (!xs->rx && !xs->tx) {
Magnus Karlsson965a9902018-05-02 13:01:26 +0200664 err = -EINVAL;
665 goto out_unlock;
666 }
667
Björn Töpel173d3ad2018-06-04 14:05:55 +0200668 qid = sxdp->sxdp_queue_id;
Björn Töpel173d3ad2018-06-04 14:05:55 +0200669
670 if (flags & XDP_SHARED_UMEM) {
Magnus Karlsson965a9902018-05-02 13:01:26 +0200671 struct xdp_sock *umem_xs;
672 struct socket *sock;
673
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200674 if ((flags & XDP_COPY) || (flags & XDP_ZEROCOPY) ||
675 (flags & XDP_USE_NEED_WAKEUP)) {
Björn Töpel173d3ad2018-06-04 14:05:55 +0200676 /* Cannot specify flags for shared sockets. */
677 err = -EINVAL;
678 goto out_unlock;
679 }
680
Magnus Karlsson965a9902018-05-02 13:01:26 +0200681 if (xs->umem) {
682 /* We have already our own. */
683 err = -EINVAL;
684 goto out_unlock;
685 }
686
687 sock = xsk_lookup_xsk_from_fd(sxdp->sxdp_shared_umem_fd);
688 if (IS_ERR(sock)) {
689 err = PTR_ERR(sock);
690 goto out_unlock;
691 }
692
693 umem_xs = xdp_sk(sock->sk);
Björn Töpel42fddcc2019-09-04 13:49:12 +0200694 if (!xsk_is_bound(umem_xs)) {
Magnus Karlsson965a9902018-05-02 13:01:26 +0200695 err = -EBADF;
696 sockfd_put(sock);
697 goto out_unlock;
Björn Töpel42fddcc2019-09-04 13:49:12 +0200698 }
699 if (umem_xs->dev != dev || umem_xs->queue_id != qid) {
Magnus Karlsson965a9902018-05-02 13:01:26 +0200700 err = -EINVAL;
701 sockfd_put(sock);
702 goto out_unlock;
703 }
704
705 xdp_get_umem(umem_xs->umem);
Björn Töpel9764f4b2019-09-04 13:49:11 +0200706 WRITE_ONCE(xs->umem, umem_xs->umem);
Magnus Karlsson965a9902018-05-02 13:01:26 +0200707 sockfd_put(sock);
708 } else if (!xs->umem || !xdp_umem_validate_queues(xs->umem)) {
709 err = -EINVAL;
710 goto out_unlock;
Björn Töpelc4971762018-05-02 13:01:27 +0200711 } else {
712 /* This xsk has its own umem. */
Magnus Karlsson93ee30f2018-08-31 13:40:02 +0200713 xskq_set_umem(xs->umem->fq, xs->umem->size,
714 xs->umem->chunk_mask);
715 xskq_set_umem(xs->umem->cq, xs->umem->size,
716 xs->umem->chunk_mask);
Björn Töpel173d3ad2018-06-04 14:05:55 +0200717
718 err = xdp_umem_assign_dev(xs->umem, dev, qid, flags);
719 if (err)
720 goto out_unlock;
Kevin Laatzc05cd362019-08-27 02:25:22 +0000721
722 xsk_check_page_contiguity(xs->umem, flags);
Magnus Karlsson965a9902018-05-02 13:01:26 +0200723 }
724
Magnus Karlsson965a9902018-05-02 13:01:26 +0200725 xs->dev = dev;
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200726 xs->zc = xs->umem->zc;
727 xs->queue_id = qid;
Magnus Karlsson93ee30f2018-08-31 13:40:02 +0200728 xskq_set_umem(xs->rx, xs->umem->size, xs->umem->chunk_mask);
729 xskq_set_umem(xs->tx, xs->umem->size, xs->umem->chunk_mask);
Magnus Karlssonac98d8a2018-06-04 14:05:57 +0200730 xdp_add_sk_umem(xs->umem, xs);
Magnus Karlsson965a9902018-05-02 13:01:26 +0200731
732out_unlock:
Björn Töpel42fddcc2019-09-04 13:49:12 +0200733 if (err) {
Magnus Karlsson965a9902018-05-02 13:01:26 +0200734 dev_put(dev);
Björn Töpel42fddcc2019-09-04 13:49:12 +0200735 } else {
736 /* Matches smp_rmb() in bind() for shared umem
737 * sockets, and xsk_is_bound().
738 */
739 smp_wmb();
740 WRITE_ONCE(xs->state, XSK_BOUND);
741 }
Magnus Karlsson965a9902018-05-02 13:01:26 +0200742out_release:
743 mutex_unlock(&xs->mutex);
Ilya Maximets5464c3a2019-07-08 14:03:44 +0300744 rtnl_unlock();
Magnus Karlsson965a9902018-05-02 13:01:26 +0200745 return err;
746}
747
Kevin Laatzc05cd362019-08-27 02:25:22 +0000748struct xdp_umem_reg_v1 {
749 __u64 addr; /* Start of packet data area */
750 __u64 len; /* Length of packet data area */
751 __u32 chunk_size;
752 __u32 headroom;
753};
754
Björn Töpelc0c77d82018-05-02 13:01:23 +0200755static int xsk_setsockopt(struct socket *sock, int level, int optname,
756 char __user *optval, unsigned int optlen)
757{
758 struct sock *sk = sock->sk;
759 struct xdp_sock *xs = xdp_sk(sk);
760 int err;
761
762 if (level != SOL_XDP)
763 return -ENOPROTOOPT;
764
765 switch (optname) {
Björn Töpelb9b6b682018-05-02 13:01:25 +0200766 case XDP_RX_RING:
Magnus Karlssonf6145902018-05-02 13:01:32 +0200767 case XDP_TX_RING:
Björn Töpelb9b6b682018-05-02 13:01:25 +0200768 {
769 struct xsk_queue **q;
770 int entries;
771
772 if (optlen < sizeof(entries))
773 return -EINVAL;
774 if (copy_from_user(&entries, optval, sizeof(entries)))
775 return -EFAULT;
776
777 mutex_lock(&xs->mutex);
Ilya Maximets455302d2019-06-28 11:04:07 +0300778 if (xs->state != XSK_READY) {
779 mutex_unlock(&xs->mutex);
780 return -EBUSY;
781 }
Magnus Karlssonf6145902018-05-02 13:01:32 +0200782 q = (optname == XDP_TX_RING) ? &xs->tx : &xs->rx;
Björn Töpelb9b6b682018-05-02 13:01:25 +0200783 err = xsk_init_queue(entries, q, false);
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200784 if (!err && optname == XDP_TX_RING)
785 /* Tx needs to be explicitly woken up the first time */
786 xs->tx->ring->flags |= XDP_RING_NEED_WAKEUP;
Björn Töpelb9b6b682018-05-02 13:01:25 +0200787 mutex_unlock(&xs->mutex);
788 return err;
789 }
Björn Töpelc0c77d82018-05-02 13:01:23 +0200790 case XDP_UMEM_REG:
791 {
Kevin Laatzc05cd362019-08-27 02:25:22 +0000792 size_t mr_size = sizeof(struct xdp_umem_reg);
793 struct xdp_umem_reg mr = {};
Björn Töpelc0c77d82018-05-02 13:01:23 +0200794 struct xdp_umem *umem;
795
Kevin Laatzc05cd362019-08-27 02:25:22 +0000796 if (optlen < sizeof(struct xdp_umem_reg_v1))
797 return -EINVAL;
798 else if (optlen < sizeof(mr))
799 mr_size = sizeof(struct xdp_umem_reg_v1);
800
801 if (copy_from_user(&mr, optval, mr_size))
Björn Töpelc0c77d82018-05-02 13:01:23 +0200802 return -EFAULT;
803
804 mutex_lock(&xs->mutex);
Ilya Maximets455302d2019-06-28 11:04:07 +0300805 if (xs->state != XSK_READY || xs->umem) {
Björn Töpelc0c77d82018-05-02 13:01:23 +0200806 mutex_unlock(&xs->mutex);
Björn Töpela49049e2018-05-22 09:35:02 +0200807 return -EBUSY;
808 }
809
810 umem = xdp_umem_create(&mr);
811 if (IS_ERR(umem)) {
812 mutex_unlock(&xs->mutex);
813 return PTR_ERR(umem);
Björn Töpelc0c77d82018-05-02 13:01:23 +0200814 }
815
816 /* Make sure umem is ready before it can be seen by others */
817 smp_wmb();
Björn Töpel9764f4b2019-09-04 13:49:11 +0200818 WRITE_ONCE(xs->umem, umem);
Björn Töpelc0c77d82018-05-02 13:01:23 +0200819 mutex_unlock(&xs->mutex);
820 return 0;
821 }
Magnus Karlsson423f3832018-05-02 13:01:24 +0200822 case XDP_UMEM_FILL_RING:
Magnus Karlssonfe230832018-05-02 13:01:31 +0200823 case XDP_UMEM_COMPLETION_RING:
Magnus Karlsson423f3832018-05-02 13:01:24 +0200824 {
825 struct xsk_queue **q;
826 int entries;
827
Magnus Karlsson423f3832018-05-02 13:01:24 +0200828 if (copy_from_user(&entries, optval, sizeof(entries)))
829 return -EFAULT;
830
831 mutex_lock(&xs->mutex);
Ilya Maximets455302d2019-06-28 11:04:07 +0300832 if (xs->state != XSK_READY) {
833 mutex_unlock(&xs->mutex);
834 return -EBUSY;
835 }
Björn Töpela49049e2018-05-22 09:35:02 +0200836 if (!xs->umem) {
837 mutex_unlock(&xs->mutex);
838 return -EINVAL;
839 }
840
Magnus Karlssonfe230832018-05-02 13:01:31 +0200841 q = (optname == XDP_UMEM_FILL_RING) ? &xs->umem->fq :
842 &xs->umem->cq;
Björn Töpelb9b6b682018-05-02 13:01:25 +0200843 err = xsk_init_queue(entries, q, true);
Magnus Karlsson423f3832018-05-02 13:01:24 +0200844 mutex_unlock(&xs->mutex);
845 return err;
846 }
Björn Töpelc0c77d82018-05-02 13:01:23 +0200847 default:
848 break;
849 }
850
851 return -ENOPROTOOPT;
852}
853
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200854static void xsk_enter_rxtx_offsets(struct xdp_ring_offset_v1 *ring)
855{
856 ring->producer = offsetof(struct xdp_rxtx_ring, ptrs.producer);
857 ring->consumer = offsetof(struct xdp_rxtx_ring, ptrs.consumer);
858 ring->desc = offsetof(struct xdp_rxtx_ring, desc);
859}
860
861static void xsk_enter_umem_offsets(struct xdp_ring_offset_v1 *ring)
862{
863 ring->producer = offsetof(struct xdp_umem_ring, ptrs.producer);
864 ring->consumer = offsetof(struct xdp_umem_ring, ptrs.consumer);
865 ring->desc = offsetof(struct xdp_umem_ring, desc);
866}
867
Magnus Karlssonaf75d9e2018-05-02 13:01:35 +0200868static int xsk_getsockopt(struct socket *sock, int level, int optname,
869 char __user *optval, int __user *optlen)
870{
871 struct sock *sk = sock->sk;
872 struct xdp_sock *xs = xdp_sk(sk);
873 int len;
874
875 if (level != SOL_XDP)
876 return -ENOPROTOOPT;
877
878 if (get_user(len, optlen))
879 return -EFAULT;
880 if (len < 0)
881 return -EINVAL;
882
883 switch (optname) {
884 case XDP_STATISTICS:
885 {
886 struct xdp_statistics stats;
887
888 if (len < sizeof(stats))
889 return -EINVAL;
890
891 mutex_lock(&xs->mutex);
892 stats.rx_dropped = xs->rx_dropped;
893 stats.rx_invalid_descs = xskq_nb_invalid_descs(xs->rx);
894 stats.tx_invalid_descs = xskq_nb_invalid_descs(xs->tx);
895 mutex_unlock(&xs->mutex);
896
897 if (copy_to_user(optval, &stats, sizeof(stats)))
898 return -EFAULT;
899 if (put_user(sizeof(stats), optlen))
900 return -EFAULT;
901
902 return 0;
903 }
Björn Töpelb3a9e0b2018-05-22 09:34:59 +0200904 case XDP_MMAP_OFFSETS:
905 {
906 struct xdp_mmap_offsets off;
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200907 struct xdp_mmap_offsets_v1 off_v1;
908 bool flags_supported = true;
909 void *to_copy;
Björn Töpelb3a9e0b2018-05-22 09:34:59 +0200910
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200911 if (len < sizeof(off_v1))
Björn Töpelb3a9e0b2018-05-22 09:34:59 +0200912 return -EINVAL;
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200913 else if (len < sizeof(off))
914 flags_supported = false;
Björn Töpelb3a9e0b2018-05-22 09:34:59 +0200915
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200916 if (flags_supported) {
917 /* xdp_ring_offset is identical to xdp_ring_offset_v1
918 * except for the flags field added to the end.
919 */
920 xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1 *)
921 &off.rx);
922 xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1 *)
923 &off.tx);
924 xsk_enter_umem_offsets((struct xdp_ring_offset_v1 *)
925 &off.fr);
926 xsk_enter_umem_offsets((struct xdp_ring_offset_v1 *)
927 &off.cr);
928 off.rx.flags = offsetof(struct xdp_rxtx_ring,
929 ptrs.flags);
930 off.tx.flags = offsetof(struct xdp_rxtx_ring,
931 ptrs.flags);
932 off.fr.flags = offsetof(struct xdp_umem_ring,
933 ptrs.flags);
934 off.cr.flags = offsetof(struct xdp_umem_ring,
935 ptrs.flags);
Björn Töpelb3a9e0b2018-05-22 09:34:59 +0200936
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200937 len = sizeof(off);
938 to_copy = &off;
939 } else {
940 xsk_enter_rxtx_offsets(&off_v1.rx);
941 xsk_enter_rxtx_offsets(&off_v1.tx);
942 xsk_enter_umem_offsets(&off_v1.fr);
943 xsk_enter_umem_offsets(&off_v1.cr);
Björn Töpelb3a9e0b2018-05-22 09:34:59 +0200944
Magnus Karlsson77cd0d72019-08-14 09:27:17 +0200945 len = sizeof(off_v1);
946 to_copy = &off_v1;
947 }
948
949 if (copy_to_user(optval, to_copy, len))
Björn Töpelb3a9e0b2018-05-22 09:34:59 +0200950 return -EFAULT;
951 if (put_user(len, optlen))
952 return -EFAULT;
953
954 return 0;
955 }
Maxim Mikityanskiy2640d3c2019-06-26 17:35:25 +0300956 case XDP_OPTIONS:
957 {
958 struct xdp_options opts = {};
959
960 if (len < sizeof(opts))
961 return -EINVAL;
962
963 mutex_lock(&xs->mutex);
964 if (xs->zc)
965 opts.flags |= XDP_OPTIONS_ZEROCOPY;
966 mutex_unlock(&xs->mutex);
967
968 len = sizeof(opts);
969 if (copy_to_user(optval, &opts, len))
970 return -EFAULT;
971 if (put_user(len, optlen))
972 return -EFAULT;
973
974 return 0;
975 }
Magnus Karlssonaf75d9e2018-05-02 13:01:35 +0200976 default:
977 break;
978 }
979
980 return -EOPNOTSUPP;
981}
982
Magnus Karlsson423f3832018-05-02 13:01:24 +0200983static int xsk_mmap(struct file *file, struct socket *sock,
984 struct vm_area_struct *vma)
985{
Geert Uytterhoevena5a16e42018-06-07 15:37:34 +0200986 loff_t offset = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
Magnus Karlsson423f3832018-05-02 13:01:24 +0200987 unsigned long size = vma->vm_end - vma->vm_start;
988 struct xdp_sock *xs = xdp_sk(sock->sk);
989 struct xsk_queue *q = NULL;
Björn Töpel37b07692018-05-22 09:35:01 +0200990 struct xdp_umem *umem;
Magnus Karlsson423f3832018-05-02 13:01:24 +0200991 unsigned long pfn;
992 struct page *qpg;
993
Björn Töpel42fddcc2019-09-04 13:49:12 +0200994 if (READ_ONCE(xs->state) != XSK_READY)
Ilya Maximets455302d2019-06-28 11:04:07 +0300995 return -EBUSY;
996
Björn Töpelb9b6b682018-05-02 13:01:25 +0200997 if (offset == XDP_PGOFF_RX_RING) {
Björn Töpel37b07692018-05-22 09:35:01 +0200998 q = READ_ONCE(xs->rx);
Magnus Karlssonf6145902018-05-02 13:01:32 +0200999 } else if (offset == XDP_PGOFF_TX_RING) {
Björn Töpel37b07692018-05-22 09:35:01 +02001000 q = READ_ONCE(xs->tx);
Björn Töpelb9b6b682018-05-02 13:01:25 +02001001 } else {
Björn Töpel37b07692018-05-22 09:35:01 +02001002 umem = READ_ONCE(xs->umem);
1003 if (!umem)
Björn Töpelb9b6b682018-05-02 13:01:25 +02001004 return -EINVAL;
Magnus Karlsson423f3832018-05-02 13:01:24 +02001005
Magnus Karlssone6762c82019-02-08 14:13:50 +01001006 /* Matches the smp_wmb() in XDP_UMEM_REG */
1007 smp_rmb();
Björn Töpelb9b6b682018-05-02 13:01:25 +02001008 if (offset == XDP_UMEM_PGOFF_FILL_RING)
Björn Töpel37b07692018-05-22 09:35:01 +02001009 q = READ_ONCE(umem->fq);
Magnus Karlssonfe230832018-05-02 13:01:31 +02001010 else if (offset == XDP_UMEM_PGOFF_COMPLETION_RING)
Björn Töpel37b07692018-05-22 09:35:01 +02001011 q = READ_ONCE(umem->cq);
Björn Töpelb9b6b682018-05-02 13:01:25 +02001012 }
Magnus Karlsson423f3832018-05-02 13:01:24 +02001013
1014 if (!q)
1015 return -EINVAL;
1016
Magnus Karlssone6762c82019-02-08 14:13:50 +01001017 /* Matches the smp_wmb() in xsk_init_queue */
1018 smp_rmb();
Magnus Karlsson423f3832018-05-02 13:01:24 +02001019 qpg = virt_to_head_page(q->ring);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07001020 if (size > page_size(qpg))
Magnus Karlsson423f3832018-05-02 13:01:24 +02001021 return -EINVAL;
1022
1023 pfn = virt_to_phys(q->ring) >> PAGE_SHIFT;
1024 return remap_pfn_range(vma, vma->vm_start, pfn,
1025 size, vma->vm_page_prot);
1026}
1027
Ilya Maximets455302d2019-06-28 11:04:07 +03001028static int xsk_notifier(struct notifier_block *this,
1029 unsigned long msg, void *ptr)
1030{
1031 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1032 struct net *net = dev_net(dev);
1033 struct sock *sk;
1034
1035 switch (msg) {
1036 case NETDEV_UNREGISTER:
1037 mutex_lock(&net->xdp.lock);
1038 sk_for_each(sk, &net->xdp.list) {
1039 struct xdp_sock *xs = xdp_sk(sk);
1040
1041 mutex_lock(&xs->mutex);
1042 if (xs->dev == dev) {
1043 sk->sk_err = ENETDOWN;
1044 if (!sock_flag(sk, SOCK_DEAD))
1045 sk->sk_error_report(sk);
1046
1047 xsk_unbind_dev(xs);
1048
1049 /* Clear device references in umem. */
1050 xdp_umem_clear_dev(xs->umem);
1051 }
1052 mutex_unlock(&xs->mutex);
1053 }
1054 mutex_unlock(&net->xdp.lock);
1055 break;
1056 }
1057 return NOTIFY_DONE;
1058}
1059
Björn Töpelc0c77d82018-05-02 13:01:23 +02001060static struct proto xsk_proto = {
1061 .name = "XDP",
1062 .owner = THIS_MODULE,
1063 .obj_size = sizeof(struct xdp_sock),
1064};
1065
1066static const struct proto_ops xsk_proto_ops = {
Björn Töpelc2f43742018-05-18 14:00:24 +02001067 .family = PF_XDP,
1068 .owner = THIS_MODULE,
1069 .release = xsk_release,
1070 .bind = xsk_bind,
1071 .connect = sock_no_connect,
1072 .socketpair = sock_no_socketpair,
1073 .accept = sock_no_accept,
1074 .getname = sock_no_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001075 .poll = xsk_poll,
Björn Töpelc2f43742018-05-18 14:00:24 +02001076 .ioctl = sock_no_ioctl,
1077 .listen = sock_no_listen,
1078 .shutdown = sock_no_shutdown,
1079 .setsockopt = xsk_setsockopt,
1080 .getsockopt = xsk_getsockopt,
1081 .sendmsg = xsk_sendmsg,
1082 .recvmsg = sock_no_recvmsg,
1083 .mmap = xsk_mmap,
1084 .sendpage = sock_no_sendpage,
Björn Töpelc0c77d82018-05-02 13:01:23 +02001085};
1086
Björn Töpel11fe9262019-02-21 13:07:38 +01001087static void xsk_destruct(struct sock *sk)
1088{
1089 struct xdp_sock *xs = xdp_sk(sk);
1090
1091 if (!sock_flag(sk, SOCK_DEAD))
1092 return;
1093
1094 xdp_put_umem(xs->umem);
1095
1096 sk_refcnt_debug_dec(sk);
1097}
1098
Björn Töpelc0c77d82018-05-02 13:01:23 +02001099static int xsk_create(struct net *net, struct socket *sock, int protocol,
1100 int kern)
1101{
1102 struct sock *sk;
1103 struct xdp_sock *xs;
1104
1105 if (!ns_capable(net->user_ns, CAP_NET_RAW))
1106 return -EPERM;
1107 if (sock->type != SOCK_RAW)
1108 return -ESOCKTNOSUPPORT;
1109
1110 if (protocol)
1111 return -EPROTONOSUPPORT;
1112
1113 sock->state = SS_UNCONNECTED;
1114
1115 sk = sk_alloc(net, PF_XDP, GFP_KERNEL, &xsk_proto, kern);
1116 if (!sk)
1117 return -ENOBUFS;
1118
1119 sock->ops = &xsk_proto_ops;
1120
1121 sock_init_data(sock, sk);
1122
1123 sk->sk_family = PF_XDP;
1124
Björn Töpel11fe9262019-02-21 13:07:38 +01001125 sk->sk_destruct = xsk_destruct;
1126 sk_refcnt_debug_inc(sk);
1127
Björn Töpelcee27162018-10-08 19:40:16 +02001128 sock_set_flag(sk, SOCK_RCU_FREE);
1129
Björn Töpelc0c77d82018-05-02 13:01:23 +02001130 xs = xdp_sk(sk);
Ilya Maximets455302d2019-06-28 11:04:07 +03001131 xs->state = XSK_READY;
Björn Töpelc0c77d82018-05-02 13:01:23 +02001132 mutex_init(&xs->mutex);
Ilya Maximetsbf0bdd12019-07-03 15:09:16 +03001133 spin_lock_init(&xs->rx_lock);
Magnus Karlssona9744f72018-06-29 09:48:20 +02001134 spin_lock_init(&xs->tx_completion_lock);
Björn Töpelc0c77d82018-05-02 13:01:23 +02001135
Björn Töpel0402acd2019-08-15 11:30:13 +02001136 INIT_LIST_HEAD(&xs->map_list);
1137 spin_lock_init(&xs->map_list_lock);
1138
Björn Töpel1d0dc062019-01-24 19:59:37 +01001139 mutex_lock(&net->xdp.lock);
1140 sk_add_node_rcu(sk, &net->xdp.list);
1141 mutex_unlock(&net->xdp.lock);
1142
Björn Töpelc0c77d82018-05-02 13:01:23 +02001143 local_bh_disable();
1144 sock_prot_inuse_add(net, &xsk_proto, 1);
1145 local_bh_enable();
1146
1147 return 0;
1148}
1149
1150static const struct net_proto_family xsk_family_ops = {
1151 .family = PF_XDP,
1152 .create = xsk_create,
1153 .owner = THIS_MODULE,
1154};
1155
Ilya Maximets455302d2019-06-28 11:04:07 +03001156static struct notifier_block xsk_netdev_notifier = {
1157 .notifier_call = xsk_notifier,
1158};
1159
Björn Töpel1d0dc062019-01-24 19:59:37 +01001160static int __net_init xsk_net_init(struct net *net)
1161{
1162 mutex_init(&net->xdp.lock);
1163 INIT_HLIST_HEAD(&net->xdp.list);
1164 return 0;
1165}
1166
1167static void __net_exit xsk_net_exit(struct net *net)
1168{
1169 WARN_ON_ONCE(!hlist_empty(&net->xdp.list));
1170}
1171
1172static struct pernet_operations xsk_net_ops = {
1173 .init = xsk_net_init,
1174 .exit = xsk_net_exit,
1175};
1176
Björn Töpelc0c77d82018-05-02 13:01:23 +02001177static int __init xsk_init(void)
1178{
Björn Töpele312b9e2019-12-19 07:10:02 +01001179 int err, cpu;
Björn Töpelc0c77d82018-05-02 13:01:23 +02001180
1181 err = proto_register(&xsk_proto, 0 /* no slab */);
1182 if (err)
1183 goto out;
1184
1185 err = sock_register(&xsk_family_ops);
1186 if (err)
1187 goto out_proto;
1188
Björn Töpel1d0dc062019-01-24 19:59:37 +01001189 err = register_pernet_subsys(&xsk_net_ops);
1190 if (err)
1191 goto out_sk;
Ilya Maximets455302d2019-06-28 11:04:07 +03001192
1193 err = register_netdevice_notifier(&xsk_netdev_notifier);
1194 if (err)
1195 goto out_pernet;
1196
Björn Töpele312b9e2019-12-19 07:10:02 +01001197 for_each_possible_cpu(cpu)
1198 INIT_LIST_HEAD(&per_cpu(xskmap_flush_list, cpu));
Björn Töpelc0c77d82018-05-02 13:01:23 +02001199 return 0;
1200
Ilya Maximets455302d2019-06-28 11:04:07 +03001201out_pernet:
1202 unregister_pernet_subsys(&xsk_net_ops);
Björn Töpel1d0dc062019-01-24 19:59:37 +01001203out_sk:
1204 sock_unregister(PF_XDP);
Björn Töpelc0c77d82018-05-02 13:01:23 +02001205out_proto:
1206 proto_unregister(&xsk_proto);
1207out:
1208 return err;
1209}
1210
1211fs_initcall(xsk_init);